unix.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "global.h"
  5. #include "util.h"
  6. #include "palcfg.h"
  7. #include <syslog.h>
  8. #ifndef PAL_NO_LAUNCH_UI
  9. #include <FL/Fl.H>
  10. #include <FL/Fl_Window.H>
  11. #include <FL/Fl_Box.H>
  12. #include <FL/Fl_Button.H>
  13. #include <FL/Fl_Check_Button.H>
  14. #include <FL/Fl_Radio_Round_Button.H>
  15. #include <FL/Fl_Hor_Value_Slider.H>
  16. #include <FL/Fl_Input.H>
  17. #include <FL/Fl_Int_Input.H>
  18. #include <FL/Fl_Choice.H>
  19. struct {
  20. Fl_Input* gamepath;
  21. Fl_Input* fontfile;
  22. Fl_Input* logfile;
  23. Fl_Input* msgfile;
  24. Fl_Check_Button* touch;
  25. Fl_Check_Button* aspect;
  26. Fl_Check_Button* fullscreen;
  27. Fl_Check_Button* avi;
  28. Fl_Choice* loglevel;
  29. Fl_Choice* cd;
  30. Fl_Choice* bgm;
  31. Fl_Choice* opl;
  32. Fl_Int_Input* samplerate;
  33. Fl_Check_Button* stereo;
  34. Fl_Int_Input* oplrate;
  35. Fl_Check_Button* surround;
  36. Fl_Int_Input* buffer;
  37. Fl_Hor_Value_Slider* quality;
  38. Fl_Hor_Value_Slider* music;
  39. Fl_Hor_Value_Slider* sound;
  40. } gWidgets;
  41. struct {
  42. const char* title;
  43. const char* language;
  44. const char* display;
  45. const char* audio;
  46. const char* logging;
  47. const char* gamepath;
  48. const char* msgfile;
  49. const char* fontfile;
  50. const char* logfile;
  51. const char* loglevel;
  52. const char* touch;
  53. const char* aspect;
  54. const char* fullscreen;
  55. const char* avi;
  56. const char* cd;
  57. const char* bgm;
  58. const char* opl;
  59. const char* samplerate;
  60. const char* stereo;
  61. const char* oplrate;
  62. const char* surround;
  63. const char* musvol;
  64. const char* sndvol;
  65. const char* buffer;
  66. const char* quality;
  67. const char* exit;
  68. const char* launch;
  69. const char* def;
  70. const char* levels;
  71. } gLabels[3] = {
  72. { "SDLPAL Launcher", "Language & Font", "Display", "Audio", "Logging",
  73. "Game resource path:", "Message file:", "Font file:", "Log file:", "Log level:",
  74. "Use touc&h overlay", "&Keep aspect ratio", "&Full screen", "Enable A&VI","&CD type:",
  75. "&BGM type:", "&OPL type:", "Sample rate:", "Ste&reo", "OPL rate:",
  76. "Surround O&PL", "Music volume:", "Sound volume:","Buffer:", "Quality:",
  77. "E&xit", "&Launch game", "&Default", "Verbose|Debug|Informational|Warning|Error|Fatal" },
  78. { "SDLPAL 启动器", "字体及语言设置", "显示设置", "音频设置", "日志记录设置",
  79. "游戏资源目录:", "语言文件:", "字体文件", "日志文件", "日志记录级别:",
  80. "启用触屏辅助(&H)", "保持纵横比(&K)", "全屏模式(&F)", "AVI 动画(&V)", "&CD 音源:",
  81. "&BGM 音源:", "&OPL 类型:", "采样率:", "立体声(&R)", "OPL 采样率:",
  82. "环绕声 O&PL", "音乐音量:", "音效音量:", "缓冲区:", "质量:",
  83. "退出(&X)", "启动游戏(&L)", "默认设置(&D)", "详细信息|调试信息|运行信息|普通警告|严重错误|致命错误" },
  84. { "SDLPAL 啟動器", "字體及語言設定", "顯示設定", "音訊設定", "日誌記錄設定",
  85. "遊戲資源檔夾:", "語言檔:", "字體檔:", "日誌檔:", "日誌記錄級別:",
  86. "啟用觸屏輔助(&H)", "保持縱橫比(&K)", "全屏模式(&F)", "AVI 動畫(&V)", "&CD 音源:",
  87. "&BGM 音源:", "&OPL 類型:", "取樣速率:", "立體聲(&R)", "OPL 取樣速率:",
  88. "環繞聲 O&PL", "音樂音量:", "音效音量:", "緩衝區:", "品質:",
  89. "退出(&X)", "啟動遊戲(&L)", "默認設定(&D)", "詳細信息|調試信息|運行信息|普通警告|嚴重錯誤|致命錯誤" },
  90. };
  91. void InitControls()
  92. {
  93. char buffer[64];
  94. gWidgets.gamepath->value(gConfig.pszGamePath);
  95. gWidgets.msgfile->value(gConfig.pszMsgFile);
  96. gWidgets.fontfile->value(gConfig.pszFontFile);
  97. gWidgets.logfile->value(gConfig.pszLogFile);
  98. gWidgets.loglevel->value(gConfig.iLogLevel);
  99. gWidgets.touch->value(gConfig.fUseTouchOverlay ? 1 : 0);
  100. gWidgets.aspect->value(gConfig.fKeepAspectRatio ? 1 : 0);
  101. gWidgets.fullscreen->value(gConfig.fFullScreen ? 1 : 0);
  102. gWidgets.avi->value(gConfig.fEnableAviPlay ? 1 : 0);
  103. gWidgets.cd->value(gConfig.eCDType - MUSIC_MP3);
  104. gWidgets.bgm->value(gConfig.eMusicType);
  105. gWidgets.stereo->value(gConfig.iAudioChannels == 2 ? 1 : 0);
  106. sprintf(buffer, "%d", gConfig.iSampleRate); gWidgets.samplerate->value(buffer);
  107. gWidgets.opl->value(gConfig.eOPLType);
  108. sprintf(buffer, "%d", gConfig.iOPLSampleRate); gWidgets.oplrate->value(buffer);
  109. gWidgets.surround->value(gConfig.fUseSurroundOPL ? 1 : 0);
  110. sprintf(buffer, "%d", gConfig.wAudioBufferSize); gWidgets.buffer->value(buffer);
  111. gWidgets.quality->value(gConfig.iResampleQuality);
  112. gWidgets.music->value(gConfig.iMusicVolume);
  113. gWidgets.sound->value(gConfig.iSoundVolume);
  114. }
  115. void SaveControls()
  116. {
  117. free(gConfig.pszGamePath);
  118. free(gConfig.pszMsgFile);
  119. free(gConfig.pszFontFile);
  120. free(gConfig.pszLogFile);
  121. gConfig.pszGamePath = *gWidgets.gamepath->value() ? strdup(gWidgets.gamepath->value()) : nullptr;
  122. gConfig.pszSavePath = *gWidgets.gamepath->value() ? strdup(gWidgets.gamepath->value()) : nullptr;
  123. gConfig.pszMsgFile = *gWidgets.msgfile->value() ? strdup(gWidgets.msgfile->value()) : nullptr;
  124. gConfig.pszFontFile = *gWidgets.fontfile->value() ? strdup(gWidgets.fontfile->value()) : nullptr;
  125. gConfig.pszLogFile = *gWidgets.logfile->value() ? strdup(gWidgets.logfile->value()) : nullptr;
  126. gConfig.iLogLevel = (LOGLEVEL)gWidgets.loglevel->value();
  127. gConfig.fUseTouchOverlay = gWidgets.touch->value();
  128. gConfig.fKeepAspectRatio = gWidgets.aspect->value();
  129. gConfig.fFullScreen = gWidgets.fullscreen->value();
  130. gConfig.fEnableAviPlay = gWidgets.avi->value();
  131. gConfig.eCDType = (MUSICTYPE)(gWidgets.cd->value() + MUSIC_MP3);
  132. gConfig.eMusicType = (MUSICTYPE)(gWidgets.bgm->value());
  133. gConfig.iAudioChannels = gWidgets.stereo->value() ? 2 : 1;
  134. gConfig.iSampleRate = atoi(gWidgets.samplerate->value());
  135. gConfig.eOPLType = (OPLTYPE)gWidgets.opl->value();
  136. gConfig.iOPLSampleRate = atoi(gWidgets.oplrate->value());
  137. gConfig.fUseSurroundOPL = gWidgets.surround->value();
  138. gConfig.wAudioBufferSize = atoi(gWidgets.buffer->value());
  139. gConfig.iResampleQuality = (int)gWidgets.quality->value();
  140. gConfig.iMusicVolume = (int)gWidgets.music->value();
  141. gConfig.iSoundVolume = (int)gWidgets.sound->value();
  142. gConfig.fLaunchSetting = FALSE;
  143. }
  144. int GetLanguage()
  145. {
  146. auto lang = getenv("LANG");
  147. if (!lang) return 0;
  148. if (strncasecmp(lang, "zh_", 3) == 0)
  149. {
  150. if (strncasecmp(lang + 3, "hans", 4) == 0 || strncasecmp(lang + 3, "CN", 2) == 0 || strncasecmp(lang + 3, "SG", 2) == 0)
  151. return 1;
  152. else
  153. return 2;
  154. }
  155. else
  156. return 0;
  157. }
  158. Fl_Window* InitWindow()
  159. {
  160. int lang = GetLanguage();
  161. Fl_Window* window = new Fl_Window(640, 400, gLabels[lang].title);
  162. gWidgets.gamepath = new Fl_Input(160, 9, 475, 22, gLabels[lang].gamepath);
  163. (new Fl_Box(FL_BORDER_BOX, 5, 50, 630, 55, gLabels[lang].language))->align(FL_ALIGN_TOP);
  164. gWidgets.msgfile = new Fl_Input(109, 54, 516, 22, gLabels[lang].msgfile);
  165. gWidgets.fontfile = new Fl_Input(109, 79, 516, 22, gLabels[lang].fontfile);
  166. (new Fl_Box(FL_BORDER_BOX, 5, 127, 630, 30, gLabels[lang].logging))->align(FL_ALIGN_TOP);
  167. (gWidgets.loglevel = new Fl_Choice(85, 132, 120, 20, gLabels[lang].loglevel))->add(gLabels[lang].levels);
  168. gWidgets.logfile = new Fl_Input(284, 131, 341, 22, gLabels[lang].logfile);
  169. (new Fl_Box(FL_BORDER_BOX, 5, 180, 630, 30, gLabels[lang].display))->align(FL_ALIGN_TOP);
  170. gWidgets.touch = new Fl_Check_Button(10, 185, 150, 20, gLabels[lang].touch);
  171. gWidgets.aspect = new Fl_Check_Button(200, 185, 150, 20, gLabels[lang].aspect);
  172. gWidgets.avi = new Fl_Check_Button(390, 185, 100, 20, gLabels[lang].avi);
  173. gWidgets.fullscreen = new Fl_Check_Button(530, 185, 100, 20, gLabels[lang].fullscreen);
  174. (new Fl_Box(FL_BORDER_BOX, 5, 230, 630, 130, gLabels[lang].audio))->align(FL_ALIGN_TOP);
  175. (gWidgets.cd = new Fl_Choice(84, 239, lang ? 100 : 120, 22, gLabels[lang].cd))->add("MP3|OGG");
  176. (gWidgets.bgm = new Fl_Choice(285, 239, 60, 22, gLabels[lang].bgm))->add("MIDI|RIX|MP3|OGG");
  177. gWidgets.stereo = new Fl_Check_Button(365, 240, 70, 20, gLabels[lang].stereo);
  178. gWidgets.samplerate = new Fl_Int_Input(570, 239, 60, 22, gLabels[lang].samplerate);
  179. (gWidgets.opl = new Fl_Choice(84, 269, lang ? 100 : 120, 22, gLabels[lang].opl))->add("DOSBOX|MAME|DOSBOXNEW");
  180. gWidgets.oplrate = new Fl_Int_Input(285, 269, 60, 22, gLabels[lang].oplrate);
  181. gWidgets.surround = new Fl_Check_Button(365, 270, 120, 20, gLabels[lang].surround);
  182. gWidgets.buffer = new Fl_Int_Input(570, 269, 60, 22, gLabels[lang].buffer);
  183. gWidgets.quality = new Fl_Hor_Value_Slider(72, 299, 180, 22, gLabels[lang].quality);
  184. gWidgets.quality->align(FL_ALIGN_LEFT);
  185. gWidgets.quality->bounds(0, 4);
  186. gWidgets.quality->precision(0);
  187. gWidgets.music = new Fl_Hor_Value_Slider(380, 299, 250, 22, gLabels[lang].musvol);
  188. gWidgets.music->align(FL_ALIGN_LEFT);
  189. gWidgets.music->bounds(0, 100);
  190. gWidgets.music->precision(0);
  191. gWidgets.sound = new Fl_Hor_Value_Slider(380, 329, 250, 22, gLabels[lang].sndvol);
  192. gWidgets.sound->align(FL_ALIGN_LEFT);
  193. gWidgets.sound->bounds(0, 100);
  194. gWidgets.sound->precision(0);
  195. (new Fl_Button(5, 370, 120, 24, gLabels[lang].exit))->callback([](Fl_Widget* ctrl, void* window) {
  196. if (ctrl->when() == FL_WHEN_RELEASE)
  197. static_cast<Fl_Window*>(window)->hide();
  198. }, window);
  199. (new Fl_Button(260, 370, 120, 24, gLabels[lang].launch))->callback([](Fl_Widget* ctrl, void* window) {
  200. if (ctrl->when() == FL_WHEN_RELEASE) {
  201. SaveControls();
  202. PAL_SaveConfig();
  203. static_cast<Fl_Window*>(window)->hide();
  204. }
  205. }, window);
  206. (new Fl_Button(515, 370, 120, 24, gLabels[lang].def))->callback([](Fl_Widget*) { PAL_LoadConfig(FALSE); InitControls(); });
  207. window->end();
  208. InitControls();
  209. return window;
  210. }
  211. #endif
  212. BOOL
  213. UTIL_GetScreenSize(
  214. DWORD *pdwScreenWidth,
  215. DWORD *pdwScreenHeight
  216. )
  217. {
  218. return pdwScreenWidth && pdwScreenHeight && *pdwScreenWidth && *pdwScreenHeight;
  219. }
  220. BOOL
  221. UTIL_IsAbsolutePath(
  222. LPCSTR lpszFileName
  223. )
  224. {
  225. return lpszFileName && *lpszFileName == '/';
  226. }
  227. INT
  228. UTIL_Platform_Init(
  229. int argc,
  230. char* argv[]
  231. )
  232. {
  233. openlog("sdlpal", LOG_PERROR | LOG_PID, LOG_USER);
  234. UTIL_LogAddOutputCallback([](LOGLEVEL level, const char* str, const char*)->void {
  235. const static int priorities[] = {
  236. LOG_DEBUG,
  237. LOG_DEBUG,
  238. LOG_INFO,
  239. LOG_WARNING,
  240. LOG_ERR,
  241. LOG_EMERG
  242. };
  243. syslog(priorities[level], "%s", str);
  244. }, PAL_DEFAULT_LOGLEVEL);
  245. #if !defined(UNIT_TEST) && !defined(PAL_NO_LAUNCH_UI)
  246. if (gConfig.fLaunchSetting)
  247. {
  248. Fl_Window *window = InitWindow();
  249. window->show(argc, argv);
  250. Fl::run();
  251. Fl::flush();
  252. delete window;
  253. }
  254. #else
  255. gConfig.fLaunchSetting = FALSE;
  256. #endif
  257. return 0;
  258. }
  259. VOID
  260. UTIL_Platform_Quit(
  261. VOID
  262. )
  263. {
  264. closelog();
  265. }