main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * sfnedit/main.c
  3. *
  4. * Copyright (C) 2020 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief Scalable Screen Font Editor main function
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <signal.h>
  34. #include "lang.h"
  35. #include "ui.h"
  36. char *loc = NULL;
  37. char **lang = NULL;
  38. /*
  39. * wrapper for Windows SDL support. This partialy came from SDL_windows_main.c
  40. */
  41. #ifdef __WIN32__
  42. #include <windows.h>
  43. #include <SDL.h>
  44. static void UnEscapeQuotes(char *arg)
  45. {
  46. char *last = NULL;
  47. while (*arg) {
  48. if (*arg == '"' && (last != NULL && *last == '\\')) {
  49. char *c_curr = arg;
  50. char *c_last = last;
  51. while (*c_curr) {
  52. *c_last = *c_curr;
  53. c_last = c_curr;
  54. c_curr++;
  55. }
  56. *c_last = '\0';
  57. }
  58. last = arg;
  59. arg++;
  60. }
  61. }
  62. /* Parse a command line buffer into arguments */
  63. static int ParseCommandLine(char *cmdline, char **argv)
  64. {
  65. char *bufp;
  66. char *lastp = NULL;
  67. int argc, last_argc;
  68. argc = last_argc = 0;
  69. for (bufp = cmdline; *bufp;) {
  70. /* Skip leading whitespace */
  71. while (SDL_isspace(*bufp)) {
  72. ++bufp;
  73. }
  74. /* Skip over argument */
  75. if (*bufp == '"') {
  76. ++bufp;
  77. if (*bufp) {
  78. if (argv) {
  79. argv[argc] = bufp;
  80. }
  81. ++argc;
  82. }
  83. /* Skip over word */
  84. lastp = bufp;
  85. while (*bufp && (*bufp != '"' || *lastp == '\\')) {
  86. lastp = bufp;
  87. ++bufp;
  88. }
  89. } else {
  90. if (*bufp) {
  91. if (argv) {
  92. argv[argc] = bufp;
  93. }
  94. ++argc;
  95. }
  96. /* Skip over word */
  97. while (*bufp && !SDL_isspace(*bufp)) {
  98. ++bufp;
  99. }
  100. }
  101. if (*bufp) {
  102. if (argv) {
  103. *bufp = '\0';
  104. }
  105. ++bufp;
  106. }
  107. /* Strip out \ from \" sequences */
  108. if (argv && last_argc != argc) {
  109. UnEscapeQuotes(argv[last_argc]);
  110. }
  111. last_argc = argc;
  112. }
  113. if (argv) {
  114. argv[argc] = NULL;
  115. }
  116. return (argc);
  117. }
  118. int APIENTRY WinMain(__attribute__((unused)) HINSTANCE hInstance, __attribute__((unused)) HINSTANCE hPrevInstance,
  119. __attribute__((unused)) LPSTR lpCmdLine, __attribute__((unused)) int nCmdShow)
  120. {
  121. char *cmdline = GetCommandLineA();
  122. int ret, argc = ParseCommandLine(cmdline, NULL);
  123. char **argv = (char**)calloc(argc+2, sizeof(char*));
  124. int lid = 0;
  125. ParseCommandLine(cmdline, argv);
  126. lid = GetUserDefaultLangID(); /* GetUserDefaultUILanguage(); */
  127. /* see https://docs.microsoft.com/en-us/windows/win32/intl/language-identifier-constants-and-strings */
  128. switch(lid & 0xFF) {
  129. case 0x01: loc = "ar"; break; case 0x02: loc = "bg"; break;
  130. case 0x03: loc = "ca"; break; case 0x04: loc = "zh"; break;
  131. case 0x05: loc = "cs"; break; case 0x06: loc = "da"; break;
  132. case 0x07: loc = "de"; break; case 0x08: loc = "el"; break;
  133. case 0x0A: loc = "es"; break; case 0x0B: loc = "fi"; break;
  134. case 0x0C: loc = "fr"; break; case 0x0D: loc = "he"; break;
  135. case 0x0E: loc = "hu"; break; case 0x0F: loc = "is"; break;
  136. case 0x10: loc = "it"; break; case 0x11: loc = "jp"; break;
  137. case 0x12: loc = "ko"; break; case 0x13: loc = "nl"; break;
  138. case 0x14: loc = "no"; break; case 0x15: loc = "pl"; break;
  139. case 0x16: loc = "pt"; break; case 0x17: loc = "rm"; break;
  140. case 0x18: loc = "ro"; break; case 0x19: loc = "ru"; break;
  141. case 0x1A: loc = "hr"; break; case 0x1B: loc = "sk"; break;
  142. case 0x1C: loc = "sq"; break; case 0x1D: loc = "sv"; break;
  143. case 0x1E: loc = "th"; break; case 0x1F: loc = "tr"; break;
  144. case 0x20: loc = "ur"; break; case 0x21: loc = "id"; break;
  145. case 0x22: loc = "uk"; break; case 0x23: loc = "be"; break;
  146. case 0x24: loc = "sl"; break; case 0x25: loc = "et"; break;
  147. case 0x26: loc = "lv"; break; case 0x27: loc = "lt"; break;
  148. case 0x29: loc = "fa"; break; case 0x2A: loc = "vi"; break;
  149. case 0x2B: loc = "hy"; break; case 0x2D: loc = "bq"; break;
  150. case 0x2F: loc = "mk"; break; case 0x36: loc = "af"; break;
  151. case 0x37: loc = "ka"; break; case 0x38: loc = "fo"; break;
  152. case 0x39: loc = "hi"; break; case 0x3A: loc = "mt"; break;
  153. case 0x3C: loc = "gd"; break; case 0x3E: loc = "ms"; break;
  154. case 0x3F: loc = "kk"; break; case 0x40: loc = "ky"; break;
  155. case 0x45: loc = "bn"; break; case 0x47: loc = "gu"; break;
  156. case 0x4D: loc = "as"; break; case 0x4E: loc = "mr"; break;
  157. case 0x4F: loc = "sa"; break; case 0x53: loc = "kh"; break;
  158. case 0x54: loc = "lo"; break; case 0x56: loc = "gl"; break;
  159. case 0x5E: loc = "am"; break; case 0x62: loc = "fy"; break;
  160. case 0x68: loc = "ha"; break; case 0x6D: loc = "ba"; break;
  161. case 0x6E: loc = "lb"; break; case 0x6F: loc = "kl"; break;
  162. case 0x7E: loc = "br"; break; case 0x92: loc = "ku"; break;
  163. case 0x09: default: loc = "en"; break;
  164. }
  165. SDL_SetMainReady();
  166. ret = main(argc, argv);
  167. free(argv);
  168. exit(ret);
  169. return ret;
  170. }
  171. #endif
  172. extern uint32_t theme[];
  173. /**
  174. * Main sfnedit function
  175. */
  176. int main(int argc, char **argv)
  177. {
  178. int i;
  179. while(argc > 2 && argv[1] && argv[1][0] == '-') {
  180. if(argv[1][1] == 'l') {
  181. loc = argv[2];
  182. argv += 2;
  183. argc -= 2;
  184. } else
  185. if(argv[1][1] == 't') {
  186. ui_gettheme(argv[2]);
  187. argv += 2;
  188. argc -= 2;
  189. }
  190. }
  191. if(!loc) loc = getenv("LANG");
  192. if(!loc) loc = "en";
  193. for(i = 0; i < NUMLANGS; i++)
  194. if(!strncmp(loc, dict[i][0], strlen(dict[i][0]))) break;
  195. if(i >= NUMLANGS) { i = 0; loc = "en"; }
  196. lang = &dict[i][1];
  197. #ifndef __WIN32__
  198. signal(SIGQUIT, ui_quit);
  199. signal(SIGINT, ui_quit);
  200. #endif
  201. ui_main(argc > 1 && argv[1] ? argv[1] : NULL);
  202. ui_quit(0);
  203. return 0;
  204. }