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. #ifdef PAL_HAS_NATIVEMIDI
  20. #define MUSIC_MIN MUSIC_MIDI
  21. #else
  22. #define MUSIC_MIN MUSIC_RIX
  23. #endif
  24. struct {
  25. Fl_Input* gamepath;
  26. Fl_Radio_Round_Button* cht;
  27. Fl_Radio_Round_Button* chs;
  28. Fl_Input* msgfile;
  29. Fl_Check_Button* font;
  30. Fl_Check_Button* touch;
  31. Fl_Check_Button* aspect;
  32. Fl_Check_Button* fullscreen;
  33. Fl_Choice* cd;
  34. Fl_Choice* bgm;
  35. Fl_Choice* opl;
  36. Fl_Int_Input* samplerate;
  37. Fl_Check_Button* stereo;
  38. Fl_Int_Input* oplrate;
  39. Fl_Check_Button* surround;
  40. Fl_Int_Input* buffer;
  41. Fl_Hor_Value_Slider* quality;
  42. Fl_Hor_Value_Slider* music;
  43. Fl_Hor_Value_Slider* sound;
  44. } gWidgets;
  45. struct {
  46. const char* title;
  47. const char* language;
  48. const char* display;
  49. const char* audio;
  50. const char* gamepath;
  51. const char* cht;
  52. const char* chs;
  53. const char* msgfile;
  54. const char* font;
  55. const char* touch;
  56. const char* aspect;
  57. const char* fullscreen;
  58. const char* cd;
  59. const char* bgm;
  60. const char* opl;
  61. const char* samplerate;
  62. const char* stereo;
  63. const char* oplrate;
  64. const char* surround;
  65. const char* buffer;
  66. const char* quality;
  67. const char* musvol;
  68. const char* sndvol;
  69. const char* exit;
  70. const char* launch;
  71. const char* def;
  72. } gLabels[3] = {
  73. { "SDLPAL Launcher", "Game language", "Display", "Audio", "Game resource path:",
  74. "&Traditional Chinese", "&Simplified Chinese", "Message file:", "Use &embedded font",
  75. "Use touc&h overlay", "&Keep aspect ratio", "&Full screen", "&CD type:", "&BGM type:",
  76. "&OPL type:", "Sample rate:", "Ste&reo", "OPL rate:", "Surround O&PL", "Buffer:",
  77. "Quality:", "Music volume:", "Sound volume:", "E&xit", "&Launch game", "&Default" },
  78. { "SDLPAL 启动器", "游戏语言设置", "显示设置", "音频设置", "游戏资源目录:",
  79. "繁体中文(&T)", "简体中文(&S)", "语言文件:", "使用游戏资源内嵌字体(&E)",
  80. "启用触屏辅助(&H)", "保持纵横比(&K)", "全屏模式(&F)", "&CD 音源:", "&BGM 音源:",
  81. "&OPL 类型:", "采样率:", "立体声(&R)", "OPL 采样率:", "环绕声 O&PL", "缓冲区:",
  82. "质量:", "音乐音量:", "音效音量:", "退出(&X)", "启动游戏(&L)", "默认设置(&D)" },
  83. { "SDLPAL 啟動器", "遊戲語言設置", "顯示設定", "音訊設定", "遊戲資源目錄:",
  84. "繁體中文(&T)", "簡體中文(&S)", "語言檔:", "使用遊戲資源內嵌字體(&E)",
  85. "啟用觸屏輔助(&H)", "保持縱橫比(&K)", "全屏模式(&F)", "&CD 音源:", "&BGM 音源:",
  86. "&OPL 類型:", "取樣速率:", "立體聲(&R)", "OPL 取樣速率:", "環繞聲 O&PL", "緩衝區:",
  87. "品質:", "音樂音量:", "音效音量:", "退出(&X)", "啟動遊戲(&L)", "默認設定(&D)" },
  88. };
  89. void InitControls()
  90. {
  91. char buffer[64];
  92. gWidgets.gamepath->value(gConfig.pszGamePath);
  93. gWidgets.cht->value(gConfig.uCodePage == CP_BIG5 ? 1 : 0);
  94. gWidgets.chs->value(gConfig.uCodePage == CP_GBK ? 1 : 0);
  95. gWidgets.msgfile->value(gConfig.pszMsgFile);
  96. gWidgets.font->value(gConfig.fUseEmbeddedFonts ? 1 : 0);
  97. gWidgets.touch->value(gConfig.fUseTouchOverlay ? 1 : 0);
  98. #if SDL_VERSION_ATLEAST(2,0,0)
  99. gWidgets.aspect->value(gConfig.fKeepAspectRatio ? 1 : 0);
  100. #endif
  101. gWidgets.fullscreen->value(gConfig.fFullScreen ? 1 : 0);
  102. gWidgets.cd->value(gConfig.eCDType - MUSIC_MP3);
  103. gWidgets.bgm->value(gConfig.eMusicType - MUSIC_MIN);
  104. gWidgets.stereo->value(gConfig.iAudioChannels == 2 ? 1 : 0);
  105. sprintf(buffer, "%d", gConfig.iSampleRate); gWidgets.samplerate->value(buffer);
  106. gWidgets.opl->value(gConfig.eOPLType);
  107. sprintf(buffer, "%d", gConfig.iOPLSampleRate); gWidgets.oplrate->value(buffer);
  108. gWidgets.surround->value(gConfig.fUseSurroundOPL ? 1 : 0);
  109. sprintf(buffer, "%d", gConfig.wAudioBufferSize); gWidgets.buffer->value(buffer);
  110. gWidgets.quality->value(gConfig.iResampleQuality);
  111. gWidgets.music->value(gConfig.iMusicVolume);
  112. gWidgets.sound->value(gConfig.iSoundVolume);
  113. }
  114. void SaveControls()
  115. {
  116. free(gConfig.pszGamePath);
  117. gConfig.pszGamePath = strlen(gWidgets.gamepath->value()) ? strdup(gWidgets.gamepath->value()) : nullptr;
  118. free(gConfig.pszMsgFile);
  119. gConfig.pszMsgFile = strlen(gWidgets.msgfile->value()) ? strdup(gWidgets.msgfile->value()) : nullptr;
  120. gConfig.uCodePage = (gWidgets.cht->value() ? CP_BIG5 : CP_GBK);
  121. gConfig.fUseEmbeddedFonts = gWidgets.font->value();
  122. gConfig.fUseTouchOverlay = gWidgets.touch->value();
  123. #if SDL_VERSION_ATLEAST(2,0,0)
  124. gConfig.fKeepAspectRatio = gWidgets.aspect->value();
  125. #endif
  126. gConfig.fFullScreen = gWidgets.fullscreen->value();
  127. gConfig.eCDType = (MUSICTYPE)(gWidgets.cd->value() + MUSIC_MP3);
  128. gConfig.eMusicType = (MUSICTYPE)(gWidgets.bgm->value() + MUSIC_MIN);
  129. gConfig.iAudioChannels = gWidgets.stereo->value() ? 2 : 1;
  130. gConfig.iSampleRate = atoi(gWidgets.samplerate->value());
  131. gConfig.eOPLType = (OPLTYPE)gWidgets.opl->value();
  132. gConfig.iOPLSampleRate = atoi(gWidgets.oplrate->value());
  133. gConfig.fUseSurroundOPL = gWidgets.surround->value();
  134. gConfig.wAudioBufferSize = atoi(gWidgets.buffer->value());
  135. gConfig.iResampleQuality = (int)gWidgets.quality->value();
  136. gConfig.iMusicVolume = (int)gWidgets.music->value();
  137. gConfig.iSoundVolume = (int)gWidgets.sound->value();
  138. gConfig.fLaunchSetting = FALSE;
  139. }
  140. int GetLanguage()
  141. {
  142. auto lang = getenv("LANG");
  143. if (!lang) return 0;
  144. if (strncasecmp(lang, "zh_", 3) == 0)
  145. {
  146. if (strncasecmp(lang + 3, "hans", 4) == 0 || strncasecmp(lang + 3, "CN", 2) == 0 || strncasecmp(lang + 3, "SG", 2) == 0)
  147. return 1;
  148. else
  149. return 2;
  150. }
  151. else
  152. return 0;
  153. }
  154. Fl_Window* InitWindow()
  155. {
  156. int lang = GetLanguage();
  157. Fl_Window* window = new Fl_Window(640, 400, gLabels[lang].title);
  158. (gWidgets.gamepath = new Fl_Input(160, 9, 475, 22, gLabels[lang].gamepath))->value(gConfig.pszGamePath);
  159. (new Fl_Box(FL_BORDER_BOX, 5, 70, 310, 100, gLabels[lang].language))->align(FL_ALIGN_TOP);
  160. gWidgets.cht = new Fl_Radio_Round_Button(10, 80, 160, 20, gLabels[lang].cht);
  161. gWidgets.chs = new Fl_Radio_Round_Button(10, 110, 160, 20, gLabels[lang].chs);
  162. (gWidgets.msgfile = new Fl_Input(109, 139, 196, 22, gLabels[lang].msgfile))->value(gConfig.pszMsgFile);
  163. (new Fl_Box(FL_BORDER_BOX, 325, 70, 310, 100, gLabels[lang].display))->align(FL_ALIGN_TOP);
  164. gWidgets.font = new Fl_Check_Button(330, 80, 160, 20, gLabels[lang].font);
  165. gWidgets.touch = new Fl_Check_Button(330, 110, 160, 20, gLabels[lang].touch);
  166. gWidgets.aspect = new Fl_Check_Button(330, 140, 160, 20, gLabels[lang].aspect);
  167. gWidgets.fullscreen = new Fl_Check_Button(520, 140, 120, 20, gLabels[lang].fullscreen);
  168. (new Fl_Box(FL_BORDER_BOX, 5, 210, 630, 130, gLabels[lang].audio))->align(FL_ALIGN_TOP);
  169. (gWidgets.cd = new Fl_Choice(84, 219, lang ? 100 : 120, 22, gLabels[lang].cd))->add("MP3|OGG");
  170. (gWidgets.bgm = new Fl_Choice(285, 219, 60, 22, gLabels[lang].bgm))->add( va("%s%s",(PAL_HAS_NATIVEMIDI ? "MIDI|" : ""), "RIX|MP3|OGG") );
  171. gWidgets.stereo = new Fl_Check_Button(365, 220, 60, 20, gLabels[lang].stereo);
  172. gWidgets.samplerate = new Fl_Int_Input(570, 219, 60, 22, gLabels[lang].samplerate);
  173. (gWidgets.opl = new Fl_Choice(84, 249, lang ? 100 : 120, 22, gLabels[lang].opl))->add("DOSBOX|MAME|DOSBOXNEW");
  174. gWidgets.oplrate = new Fl_Int_Input(285, 249, 60, 22, gLabels[lang].oplrate);
  175. gWidgets.surround = new Fl_Check_Button(365, 250, 60, 20, gLabels[lang].surround);
  176. gWidgets.buffer = new Fl_Int_Input(570, 249, 60, 22, gLabels[lang].buffer);
  177. gWidgets.quality = new Fl_Hor_Value_Slider(72, 279, 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, 279, 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, 309, 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. #if defined(_DEBUG)
  259. closelog();
  260. #endif
  261. }