unix.cpp 10 KB

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