Debian Codegraph @

src/pt2_main.c

raw symbols SCIP
1 // for finding memory leaks in debug mode with Visual Studio cxx . . $ `<file>/src/pt2_main.c`/
2 #if defined _DEBUG && defined _MSC_VER
3 #include <crtdbg.h>
4 #endif
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <string.h>
11 #ifdef _WIN32
12 #define WIN32_MEAN_AND_LEAN
13 #include <windows.h>
14 #include <SDL2/SDL_syswm.h>
15 #else
16 #include <signal.h>
17 #include <unistd.h> // chdir()
18 #endif
19 #include <sys/stat.h>
20 #include "pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/
21 #include "pt2_keyboard.h"cxx . . $ `<file>/src/pt2_keyboard.h`/
22 #include "pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/
23 #include "pt2_mouse.h"cxx . . $ `<file>/src/pt2_mouse.h`/
24 #include "pt2_diskop.h"cxx . . $ `<file>/src/pt2_diskop.h`/
25 #include "pt2_sampler.h"cxx . . $ `<file>/src/pt2_sampler.h`/
26 #include "pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/
27 #include "pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/
28 #include "pt2_edit.h"cxx . . $ `<file>/src/pt2_edit.h`/
29 #include "pt2_module_loader.h"cxx . . $ `<file>/src/pt2_module_loader.h`/
30 #include "pt2_module_saver.h"cxx . . $ `<file>/src/pt2_module_saver.h`/
31 #include "pt2_scopes.h"cxx . . $ `<file>/src/pt2_scopes.h`/
32 #include "pt2_audio.h"cxx . . $ `<file>/src/pt2_audio.h`/
33 #include "pt2_bmp.h"cxx . . $ `<file>/src/pt2_bmp.h`/
34 #include "pt2_visuals_sync.h"cxx . . $ `<file>/src/pt2_visuals_sync.h`/
35 #include "pt2_sampling.h"cxx . . $ `<file>/src/pt2_sampling.h`/
36 #include "pt2_askbox.h"cxx . . $ `<file>/src/pt2_askbox.h`/
37 #include "pt2_replayer.h"cxx . . $ `<file>/src/pt2_replayer.h`/
38 #include "pt2_textedit.h"cxx . . $ `<file>/src/pt2_textedit.h`/
39
40 #define CRASH_TEXTcxx . . $ `src/pt2_main.c:40:9`! "Oh no! The ProTracker 2 clone has crashed...\nA backup .mod was hopefully " \
41 "saved to the current module directory.\n\nPlease report this bug if you can.\n" \
42 "Try to mention what you did before the crash happened.\n" \
43 "My email is on the bottom of https://16-bits.org"
44
45 module_tcxx . . $ module_t# *songcxx . . $ song. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; // globalized
46
47 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! backupMadeAfterCrashcxx . . $ backupMadeAfterCrash.;
48
49 #ifdef _WIN32
50 #define SYSMSG_FILE_ARG (WM_USER + 1)
51 #define ARGV_SHARED_MEM_MAX_LEN ((MAX_PATH * 2) + 2)
52
53 #ifndef _DEBUG
54 bool windowsKeyIsDown;
55 HHOOK g_hKeyboardHook;
56 #endif
57
58 static HWND hWnd_to;
59 static HANDLE oneInstHandle, hMapFile;
60 static LPCTSTR sharedMemBuf;
61 static TCHAR sharedHwndName[] = TEXT("Local\\PT2CloneHwnd");
62 static TCHAR sharedFileName[] = TEXT("Local\\PT2CloneFilename");
63 static bool handleSingleInstancing(int32_t argc, char **argv);
64 static void closeSingleInstancing(void);
65 static void handleSysMsg(SDL_Event inputEvent);
66 #endif
67
68 #ifndef _DEBUG
69 #ifdef _WIN32
70 static LONG WINAPI exceptionHandler(EXCEPTION_POINTERS *ptr);
71 #else
72 static void exceptionHandlercxx . . $ exceptionHandler(d4f767463ce0a6b3).(int32_tcxx . . $ int32_t# signal);
73 #endif
74 #endif
75
76 #ifdef _WIN32
77 static void makeSureDirIsProgramDir(void);
78 static void disableWasapi(void);
79 #endif
80
81 #ifdef __APPLE__
82 static void osxSetDirToProgramDirFromArgs(char **argv);
83 static bool checkIfAppWasTranslocated(int argc, char **argv);
84 #endif
85
86 static void handleInputcxx . . $ handleInput(49f6e7a06ebc5aa8).(void);
87 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! initializeVarscxx . . $ initializeVars(c9857a73d68e59ba).(void);
88 static void handleSigTermcxx . . $ handleSigTerm(49f6e7a06ebc5aa8).(void);
89 static void cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).(void);
90
91 static void clearStructscxx . . $ clearStructs(49f6e7a06ebc5aa8).(void)
92 {
93 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&keybcxx . . $ keyb., 0, sizeof (keybcxx . . $ keyb.));
94 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&mousecxx . . $ mouse., 0, sizeof (mousecxx . . $ mouse.));
95 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&videocxx . . $ video., 0, sizeof (videocxx . . $ video.));
96 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&editorcxx . . $ editor., 0, sizeof (editorcxx . . $ editor.));
97 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&diskopcxx . . $ diskop., 0, sizeof (diskopcxx . . $ diskop.));
98 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&cursorcxx . . $ cursor., 0, sizeof (cursorcxx . . $ cursor.));
99 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&uicxx . . $ ui., 0, sizeof (uicxx . . $ ui.));
100 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&configcxx . . $ config., 0, sizeof (configcxx . . $ config.));
101 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&audiocxx . . $ audio., 0, sizeof (audiocxx . . $ audio.));
102 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&textEditcxx . . $ textEdit., 0, sizeof (textEditcxx . . $ textEdit.));
103
104 audiocxx . . $ audio..rescanAudioDevicesSupportedcxx . . $ audio_t#rescanAudioDevicesSupported. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
105 }
106
107 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
108 {
109 #ifdef _WIN32 // test for SSE/SSE2 presence very first, to make sure no SSE/SSE2 code is attempted to be ran
110 if (!SDL_HasSSE())
111 {
112 MessageBoxA(NULL, "Your computer's processor doesn't have the SSE instruction set " \
113 "which is needed for this program to run. Sorry!", "Error", MB_ICONEXCLAMATION);
114 return 0;
115 }
116
117 if (!SDL_HasSSE2())
118 {
119 MessageBoxA(NULL, "Your computer's processor doesn't have the SSE2 instruction set " \
120 "which is needed for this program to run. Sorry!", "Error", MB_ICONEXCLAMATION);
121 return 0;
122 }
123 #endif
124
125 #ifndef _WIN32
126 struct sigactioncxx . . $ sigaction# act, oldAct;
127 #endif
128
129 #if defined _WIN32 || defined __APPLE__
130 SDL_version sdlVer;
131 #endif
132
133 // for finding memory leaks in debug mode with Visual Studio
134 #if defined _DEBUG && defined _MSC_VER
135 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
136 #endif
137
138 #if SDL_MAJOR_VERSIONcxx . . $ `<external>/11196385350576350796/SDL_version.h:59:9`! == 2 && SDL_MINOR_VERSIONcxx . . $ `<external>/11196385350576350796/SDL_version.h:60:9`! == 0 && SDL_PATCHLEVELcxx . . $ `<external>/11196385350576350796/SDL_version.h:61:9`! < 5
139 #pragma message("WARNING: The SDL2 dev lib is older than ver 2.0.5. You'll get fullscreen mode issues and no audio input sampling.")
140 #pragma message("At least version 2.0.7 is recommended.")
141 #endif
142
143 SDL_SetThreadPrioritycxx . . $ SDL_SetThreadPriority(3fc85575c796fb27).(SDL_THREAD_PRIORITY_HIGHcxx . . $ SDL_THREAD_PRIORITY_HIGH.);
144 SDL_EnableScreenSavercxx . . $ SDL_EnableScreenSaver(49f6e7a06ebc5aa8).(); // allow screensaver to activate
145
146 clearStructscxx . . $ clearStructs(49f6e7a06ebc5aa8).();
147
148 // set up crash handler
149 #ifndef _DEBUG
150 #ifdef _WIN32
151 SetUnhandledExceptionFilter(exceptionHandler);
152 #else
153 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&act, 0, sizeof (act));
154 act.sa_handlercxx . . $ `<external>/16635209305659446502/sigaction.h:39:10`! = exceptionHandlercxx . . $ exceptionHandler(d4f767463ce0a6b3).;
155 act.sa_flagscxx . . $ sigaction#sa_flags. = SA_RESETHANDcxx . . $ `<external>/16635209305659446502/sigaction.h:67:10`!;
156
157 sigactioncxx . . $ sigaction(53aba09fe16b06c7).(SIGILLcxx . . $ `<external>/11773118241360591791/signum-generic.h:49:9`! | SIGABRTcxx . . $ `<external>/11773118241360591791/signum-generic.h:50:9`! | SIGFPEcxx . . $ `<external>/11773118241360591791/signum-generic.h:51:9`! | SIGSEGVcxx . . $ `<external>/11773118241360591791/signum-generic.h:52:9`!, &act, &oldAct);
158 sigactioncxx . . $ sigaction(53aba09fe16b06c7).(SIGILLcxx . . $ `<external>/11773118241360591791/signum-generic.h:49:9`!, &act, &oldAct);
159 sigactioncxx . . $ sigaction(53aba09fe16b06c7).(SIGABRTcxx . . $ `<external>/11773118241360591791/signum-generic.h:50:9`!, &act, &oldAct);
160 sigactioncxx . . $ sigaction(53aba09fe16b06c7).(SIGFPEcxx . . $ `<external>/11773118241360591791/signum-generic.h:51:9`!, &act, &oldAct);
161 sigactioncxx . . $ sigaction(53aba09fe16b06c7).(SIGSEGVcxx . . $ `<external>/11773118241360591791/signum-generic.h:52:9`!, &act, &oldAct);
162 #endif
163 #endif
164
165 // on Windows and macOS, test what version SDL2.DLL is (against library version used in compilation)
166 #if defined _WIN32 || defined __APPLE__
167 SDL_GetVersion(&sdlVer);
168 if (sdlVer.major != SDL_MAJOR_VERSION || sdlVer.minor != SDL_MINOR_VERSION || sdlVer.patch != SDL_PATCHLEVEL)
169 {
170 #ifdef _WIN32
171 showErrorMsgBox("SDL2.dll is not the expected version, the program will terminate.\n\n" \
172 "Loaded dll version: %d.%d.%d\n" \
173 "Required (compiled with) version: %d.%d.%d",
174 sdlVer.major, sdlVer.minor, sdlVer.patch,
175 SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
176 #else
177 showErrorMsgBox("The loaded SDL2 library is not the expected version, the program will terminate.\n\n" \
178 "Loaded library version: %d.%d.%d\n" \
179 "Required (compiled with) version: %d.%d.%d",
180 sdlVer.major, sdlVer.minor, sdlVer.patch,
181 SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
182 #endif
183 return 0;
184 }
185 #endif
186
187 // ALT+F4 is used in ProTracker, but is "close program" in some cases...
188 #if SDL_MINOR_VERSIONcxx . . $ `<external>/11196385350576350796/SDL_version.h:60:9`! >= 24 || (SDL_MINOR_VERSIONcxx . . $ `<external>/11196385350576350796/SDL_version.h:60:9`! == 0 && SDL_PATCHLEVELcxx . . $ `<external>/11196385350576350796/SDL_version.h:61:9`! >= 4)
189 SDL_SetHintcxx . . $ SDL_SetHint(f203032a27c3eb90).("SDL_WINDOWS_NO_CLOSE_ON_ALT_F4", "1");
190 #endif
191
192 SDL_SetHintcxx . . $ SDL_SetHint(f203032a27c3eb90).("SDL_MOUSE_FOCUS_CLICKTHROUGH", "1");
193
194 #ifdef _WIN32
195 #ifndef _MSC_VER
196 SetProcessDPIAware();
197 #endif
198
199 disableWasapi(); // disable problematic WASAPI SDL2 audio driver on Windows (causes clicks/pops sometimes...)
200 // 13.03.2020: This is still needed with SDL 2.0.12...
201 #endif
202
203 /* SDL 2.0.9 for Windows has a serious bug where you need to initialize the joystick subsystem
204 ** (even if you don't use it) or else weird things happen like random stutters, keyboard (rarely) being
205 ** reinitialized in Windows and what not.
206 ** Ref.: https://bugzilla.libsdl.org/show_bug.cgi?id=4391
207 */
208 #if defined _WIN32 && SDL_MAJOR_VERSIONcxx . . $ `<external>/11196385350576350796/SDL_version.h:59:9`! == 2 && SDL_MINOR_VERSIONcxx . . $ `<external>/11196385350576350796/SDL_version.h:60:9`! == 0 && SDL_PATCHLEVELcxx . . $ `<external>/11196385350576350796/SDL_version.h:61:9`! == 9
209 if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0)
210 #else
211 if (SDL_Initcxx . . $ SDL_Init(4c679297bf94b9eb).(SDL_INIT_AUDIOcxx . . $ `<external>/13597568925221565411/SDL.h:84:9`! | SDL_INIT_VIDEOcxx . . $ `<external>/13597568925221565411/SDL.h:85:9`!) != 0)
212 #endif
213 {
214 showErrorMsgBoxcxx . . $ showErrorMsgBox(326aeb464d16410c).("Couldn't initialize SDL: %s", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
215 return 0;
216 }
217
218 hpc_Initcxx . . $ hpc_Init(49f6e7a06ebc5aa8).();
219
220 /* Text input is started by default in SDL2, turn it off to remove ~2ms spikes per key press.
221 ** We manuallay start it again when someone clicks on a text edit box, and stop it when done.
222 ** Ref.: https://bugzilla.libsdl.org/show_bug.cgi?id=4166
223 */
224 SDL_StopTextInputcxx . . $ SDL_StopTextInput(49f6e7a06ebc5aa8).();
225
226 #ifdef __APPLE__
227 if (checkIfAppWasTranslocated(argc, argv))
228 {
229 SDL_Quit();
230 return 0;
231 }
232 #endif
233
234 #ifdef __APPLE__
235 osxSetDirToProgramDirFromArgs(argv);
236 #endif
237
238 #ifdef _WIN32
239
240 // Windows: the Win key has to be taken over to work like the Amiga key
241 #ifndef _DEBUG
242 windowsKeyIsDown = false;
243 g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, lowLevelKeyboardProc, GetModuleHandle(NULL), 0);
244 #endif
245
246 makeSureDirIsProgramDir();
247 #endif
248
249 if (!initializeVarscxx . . $ initializeVars(c9857a73d68e59ba).())
250 {
251 cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).();
252 SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).();
253 return 1;
254 }
255
256 loadConfigcxx . . $ loadConfig(49f6e7a06ebc5aa8).();
257
258 if (!allocSamplerVarscxx . . $ allocSamplerVars(c9857a73d68e59ba).() || !setupVideocxx . . $ setupVideo(c9857a73d68e59ba).())
259 {
260 cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).();
261 SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).();
262 return 1;
263 }
264
265 #ifdef _WIN32
266 // allow only one instance, and send arguments to it (song to play)
267 if (handleSingleInstancing(argc, argv))
268 {
269 cleanUp();
270 SDL_Quit();
271 return 0; // close current instance, the main instance got a message now
272 }
273
274 SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
275 #endif
276
277 hpc_SetDurationInHzcxx . . $ hpc_SetDurationInHz(342ecb9d325f249a).(&videocxx . . $ video..vblankHpccxx . . $ video_t#vblankHpc., VBLANK_HZcxx . . $ `src/pt2_header.h:58:9`!);
278
279 if (!setupAudiocxx . . $ setupAudio(c9857a73d68e59ba).() || !unpackBMPscxx . . $ unpackBMPs(c9857a73d68e59ba).())
280 {
281 cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).();
282 SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).();
283 return 1;
284 }
285
286 setupSpritescxx . . $ setupSprites(49f6e7a06ebc5aa8).();
287
288 songcxx . . $ song. = createEmptyModcxx . . $ createEmptyMod(51965c0e12ed79c4).();
289 if (songcxx . . $ song. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
290 {
291 cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).();
292 SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).();
293 return 1;
294 }
295
296 // setup GUI text pointers (for recently allocated song structure)
297 editorcxx . . $ editor..currEditPatternDispcxx . . $ editor_t#currEditPatternDisp. = &songcxx . . $ song.->currPatterncxx . . $ module_t#currPattern.;
298 editorcxx . . $ editor..currPosDispcxx . . $ editor_t#currPosDisp. = &songcxx . . $ song.->currPoscxx . . $ module_t#currPos.;
299 editorcxx . . $ editor..currPatternDispcxx . . $ editor_t#currPatternDisp. = &songcxx . . $ song.->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[0];
300 editorcxx . . $ editor..currPosEdPattDispcxx . . $ editor_t#currPosEdPattDisp. = &songcxx . . $ song.->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[0];
301 editorcxx . . $ editor..currLengthDispcxx . . $ editor_t#currLengthDisp. = &songcxx . . $ song.->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength.;
302
303 if (!initScopescxx . . $ initScopes(c9857a73d68e59ba).())
304 {
305 cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).();
306 SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).();
307 return 1;
308 }
309
310 modSetTempocxx . . $ modSetTempo(918ebd27a9ddbad3).(editorcxx . . $ editor..initialTempocxx . . $ editor_t#initialTempo., falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!);
311 modSetSpeedcxx . . $ modSetSpeed(d4f767463ce0a6b3).(editorcxx . . $ editor..initialSpeedcxx . . $ editor_t#initialSpeed.);
312
313 updateWindowTitlecxx . . $ updateWindowTitle(b45ddcbc8423892c).(MOD_NOT_MODIFIEDcxx . . $ MOD_NOT_MODIFIED.);
314 pointerSetModecxx . . $ pointerSetMode(f2ac3508b56cd10).(POINTER_MODE_IDLEcxx . . $ POINTER_MODE_IDLE., DO_CARRYcxx . . $ DO_CARRY.);
315 statusAllRightcxx . . $ statusAllRight(49f6e7a06ebc5aa8).();
316
317 // load a .MOD from the command arguments if passed (also ignore OS X < 10.9 -psn argument on double-click launch)
318 if ((argc >= 2 && argv[1][0] != '\0') && (argc != 2 || strncmpcxx . . $ strncmp(fd278af2f295fa2e).(argv[1], "-psn_", 5)))
319 {
320 loadModFromArgcxx . . $ loadModFromArg(fda57c8427c60377).(argv[1]);
321
322 // play song
323 if (songcxx . . $ song.->loadedcxx . . $ module_t#loaded.)
324 {
325 editorcxx . . $ editor..playModecxx . . $ editor_t#playMode. = PLAY_MODE_NORMALcxx . . $ PLAY_MODE_NORMAL.;
326 modPlaycxx . . $ modPlay(7b7bc4ff97a2e5fe).(DONT_SET_PATTERNcxx . . $ DONT_SET_PATTERN., 0, DONT_SET_ROWcxx . . $ DONT_SET_ROW.);
327 editorcxx . . $ editor..currModecxx . . $ editor_t#currMode. = MODE_PLAYcxx . . $ MODE_PLAY.;
328 pointerSetModecxx . . $ pointerSetMode(f2ac3508b56cd10).(POINTER_MODE_PLAYcxx . . $ POINTER_MODE_PLAY., DO_CARRYcxx . . $ DO_CARRY.);
329 statusAllRightcxx . . $ statusAllRight(49f6e7a06ebc5aa8).();
330 }
331 }
332 else if (!editorcxx . . $ editor..configFoundcxx . . $ editor_t#configFound.)
333 {
334 displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("CONFIG NOT FOUND!");
335 }
336
337 displayMainScreencxx . . $ displayMainScreen(49f6e7a06ebc5aa8).();
338 fillToVuMetersBgBuffercxx . . $ fillToVuMetersBgBuffer(49f6e7a06ebc5aa8).();
339 updateCursorPoscxx . . $ updateCursorPos(49f6e7a06ebc5aa8).();
340
341 SDL_ShowWindowcxx . . $ SDL_ShowWindow(8c8d6c3b662f39a2).(videocxx . . $ video..windowcxx . . $ video_t#window.);
342
343 if (configcxx . . $ config..startInFullscreencxx . . $ config_t#startInFullscreen.)
344 toggleFullscreencxx . . $ toggleFullscreen(49f6e7a06ebc5aa8).();
345
346 changePathToDesktopcxx . . $ changePathToDesktop(c9857a73d68e59ba).(); // change path to desktop now
347 diskOpSetInitPathcxx . . $ diskOpSetInitPath(49f6e7a06ebc5aa8).(); // set path to custom path in config (if present)
348
349 SDL_EventStatecxx . . $ SDL_EventState(a6bcbd86f19dd260).(SDL_DROPFILEcxx . . $ SDL_DROPFILE., SDL_ENABLEcxx . . $ `<external>/12778062723415653681/SDL_events.h:1143:9`!);
350
351 editorcxx . . $ editor..mainLoopOngoingcxx . . $ editor_t#mainLoopOngoing. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
352 hpc_ResetCounterscxx . . $ hpc_ResetCounters(efacbc4df292ea9a).(&videocxx . . $ video..vblankHpccxx . . $ video_t#vblankHpc.); // this must be the last thing we do before entering the main loop
353
354 // XXX: if you change anything in the main loop, make sure it goes in the askBox()(pt2_askbox.c) loop too, if needed
355 while (editorcxx . . $ editor..programRunningcxx . . $ editor_t#programRunning.)
356 {
357 beginFPSCountercxx . . $ beginFPSCounter(49f6e7a06ebc5aa8).();
358 handleThreadedAskBoxcxx . . $ handleThreadedAskBox(49f6e7a06ebc5aa8).();
359 sinkVisualizerBarscxx . . $ sinkVisualizerBars(49f6e7a06ebc5aa8).();
360 updateChannelSyncBuffercxx . . $ updateChannelSyncBuffer(49f6e7a06ebc5aa8).();
361 readMouseXYcxx . . $ readMouseXY(49f6e7a06ebc5aa8).();
362 readKeyModifierscxx . . $ readKeyModifiers(49f6e7a06ebc5aa8).(); // set/clear CTRL/ALT/SHIFT/AMIGA key states
363 handleInputcxx . . $ handleInput(49f6e7a06ebc5aa8).();
364 updateMouseCounterscxx . . $ updateMouseCounters(49f6e7a06ebc5aa8).();
365 handleKeyRepeatcxx . . $ handleKeyRepeat(decbd40bd4dd73ad).(keybcxx . . $ keyb..lastRepKeycxx . . $ keyb_t#lastRepKey.);
366
367 if (!mousecxx . . $ mouse..buttonWaitingcxx . . $ mouse_t#buttonWaiting. && uicxx . . $ ui..sampleMarkingPoscxx . . $ ui_t#sampleMarkingPos. == -1 && !uicxx . . $ ui..forceSampleDragcxx . . $ ui_t#forceSampleDrag. && !uicxx . . $ ui..forceVolDragcxx . . $ ui_t#forceVolDrag. && !uicxx . . $ ui..forceSampleEditcxx . . $ ui_t#forceSampleEdit.)
368 handleGUIButtonRepeatcxx . . $ handleGUIButtonRepeat(49f6e7a06ebc5aa8).();
369
370 renderFramecxx . . $ renderFrame(49f6e7a06ebc5aa8).();
371 flipFramecxx . . $ flipFrame(49f6e7a06ebc5aa8).();
372 endFPSCountercxx . . $ endFPSCounter(49f6e7a06ebc5aa8).();
373 }
374
375 cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).();
376 SDL_Quitcxx . . $ SDL_Quit(49f6e7a06ebc5aa8).();
377
378 return 0;
379 }
380
381 static void handleInputcxx . . $ handleInput(49f6e7a06ebc5aa8).(void)
382 {
383 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! focusGained = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
384
385 SDL_Eventcxx . . $ SDL_Event# event;
386 while (SDL_PollEventcxx . . $ SDL_PollEvent(c228975526fb3e93).(&event))
387 {
388 if (event.typecxx . . $ SDL_Event#type. == SDL_WINDOWEVENTcxx . . $ SDL_WINDOWEVENT.)
389 {
390 if (event.windowcxx . . $ SDL_Event#window..eventcxx . . $ SDL_WindowEvent#event. == SDL_WINDOWEVENT_HIDDENcxx . . $ SDL_WINDOWEVENT_HIDDEN.)
391 videocxx . . $ video..windowHiddencxx . . $ video_t#windowHidden. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
392 else if (event.windowcxx . . $ SDL_Event#window..eventcxx . . $ SDL_WindowEvent#event. == SDL_WINDOWEVENT_SHOWNcxx . . $ SDL_WINDOWEVENT_SHOWN.)
393 videocxx . . $ video..windowHiddencxx . . $ video_t#windowHidden. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
394
395 if (event.windowcxx . . $ SDL_Event#window..eventcxx . . $ SDL_WindowEvent#event. == SDL_WINDOWEVENT_FOCUS_GAINEDcxx . . $ SDL_WINDOWEVENT_FOCUS_GAINED.)
396 focusGained = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
397
398 // reset vblank end time if we minimize window
399 if (event.windowcxx . . $ SDL_Event#window..eventcxx . . $ SDL_WindowEvent#event. == SDL_WINDOWEVENT_MINIMIZEDcxx . . $ SDL_WINDOWEVENT_MINIMIZED. || event.windowcxx . . $ SDL_Event#window..eventcxx . . $ SDL_WindowEvent#event. == SDL_WINDOWEVENT_FOCUS_LOSTcxx . . $ SDL_WINDOWEVENT_FOCUS_LOST.)
400 hpc_ResetCounterscxx . . $ hpc_ResetCounters(efacbc4df292ea9a).(&videocxx . . $ video..vblankHpccxx . . $ video_t#vblankHpc.);
401 }
402
403 #ifdef _WIN32
404 if (event.type == SDL_SYSWMEVENT)
405 handleSysMsg(event);
406 #endif
407 if (uicxx . . $ ui..editTextFlagcxx . . $ ui_t#editTextFlag. && event.typecxx . . $ SDL_Event#type. == SDL_TEXTINPUTcxx . . $ SDL_TEXTINPUT.)
408 {
409 // text input when editing texts/numbers
410
411 char inputChar = event.textcxx . . $ SDL_Event#text..textcxx . . $ SDL_TextInputEvent#text.[0];
412 if (inputChar == '\0')
413 continue;
414
415 handleTextEditInputCharcxx . . $ handleTextEditInputChar(44b6ea1973a07080).(inputChar);
416 continue; // continue SDL event loop
417 }
418 else if (event.typecxx . . $ SDL_Event#type. == SDL_MOUSEWHEELcxx . . $ SDL_MOUSEWHEEL.)
419 {
420 if (event.wheelcxx . . $ SDL_Event#wheel..ycxx . . $ SDL_MouseWheelEvent#y. < 0) mouseWheelDownHandlercxx . . $ mouseWheelDownHandler(49f6e7a06ebc5aa8).();
421 else if (event.wheelcxx . . $ SDL_Event#wheel..ycxx . . $ SDL_MouseWheelEvent#y. > 0) mouseWheelUpHandlercxx . . $ mouseWheelUpHandler(49f6e7a06ebc5aa8).();
422 }
423 else if (event.typecxx . . $ SDL_Event#type. == SDL_DROPFILEcxx . . $ SDL_DROPFILE.)
424 {
425 if (!videocxx . . $ video..fullscreencxx . . $ video_t#fullscreen.)
426 {
427 if (SDL_GetWindowFlagscxx . . $ SDL_GetWindowFlags(9beabbd21f19984b).(videocxx . . $ video..windowcxx . . $ video_t#window.) & SDL_WINDOW_MINIMIZEDcxx . . $ SDL_WINDOW_MINIMIZED.)
428 SDL_RestoreWindowcxx . . $ SDL_RestoreWindow(8c8d6c3b662f39a2).(videocxx . . $ video..windowcxx . . $ video_t#window.);
429
430 SDL_RaiseWindowcxx . . $ SDL_RaiseWindow(8c8d6c3b662f39a2).(videocxx . . $ video..windowcxx . . $ video_t#window.);
431 }
432
433 loadDroppedFilecxx . . $ loadDroppedFile(90acc2fa0970f3e0).(event.dropcxx . . $ SDL_Event#drop..filecxx . . $ SDL_DropEvent#file., (uint32_tcxx . . $ uint32_t#)strlencxx . . $ strlen(751346f8406fd082).(event.dropcxx . . $ SDL_Event#drop..filecxx . . $ SDL_DropEvent#file.), falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!, truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!);
434 SDL_freecxx . . $ SDL_free(5d22bdacc48458e8).(event.dropcxx . . $ SDL_Event#drop..filecxx . . $ SDL_DropEvent#file.);
435 }
436 else if (event.typecxx . . $ SDL_Event#type. == SDL_QUITcxx . . $ SDL_QUIT.)
437 {
438 #ifdef __APPLE__
439 /* On Mac, command+Q sends a quit signal to the program.
440 ** However, command+Q is also one of the transpose keys in this ProTracker port.
441 ** Ignore the signal if command+Q was pressed.
442 */
443 if (!editor.macCmdQIssued)
444 #endif
445 handleSigTermcxx . . $ handleSigTerm(49f6e7a06ebc5aa8).();
446
447 #ifdef __APPLE__
448 editor.macCmdQIssued = false; // read note above
449 #endif
450 }
451 else if (event.typecxx . . $ SDL_Event#type. == SDL_KEYUPcxx . . $ SDL_KEYUP.)
452 {
453 keyUpHandlercxx . . $ keyUpHandler(decbd40bd4dd73ad).(event.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..scancodecxx . . $ SDL_Keysym#scancode.);
454 }
455 else if (event.typecxx . . $ SDL_Event#type. == SDL_KEYDOWNcxx . . $ SDL_KEYDOWN.)
456 {
457 if (editorcxx . . $ editor..repeatKeyFlagcxx . . $ editor_t#repeatKeyFlag. || keybcxx . . $ keyb..lastRepKeycxx . . $ keyb_t#lastRepKey. != event.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..scancodecxx . . $ SDL_Keysym#scancode.)
458 keyDownHandlercxx . . $ keyDownHandler(90a138f3be71ee4c).(event.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..scancodecxx . . $ SDL_Keysym#scancode., event.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..symcxx . . $ SDL_Keysym#sym.);
459 }
460 else if (event.typecxx . . $ SDL_Event#type. == SDL_MOUSEBUTTONUPcxx . . $ SDL_MOUSEBUTTONUP.)
461 {
462 #if defined __APPLE__ && defined __aarch64__
463 armMacGhostMouseCursorFix();
464 #endif
465 mouseButtonUpHandlercxx . . $ mouseButtonUpHandler(ac50b47256ae7414).(event.buttoncxx . . $ SDL_Event#button..buttoncxx . . $ SDL_MouseButtonEvent#button.);
466 }
467 else if (event.typecxx . . $ SDL_Event#type. == SDL_MOUSEBUTTONDOWNcxx . . $ SDL_MOUSEBUTTONDOWN.)
468 {
469 /* If program was not in focus and we clicked somewhere,
470 ** only accept the click if an ask box dialog is shown.
471 */
472 if (!focusGained || uicxx . . $ ui..askBoxShowncxx . . $ ui_t#askBoxShown.)
473 {
474 if (uicxx . . $ ui..sampleMarkingPoscxx . . $ ui_t#sampleMarkingPos. == -1 && !uicxx . . $ ui..forceSampleDragcxx . . $ ui_t#forceSampleDrag. && !uicxx . . $ ui..forceVolDragcxx . . $ ui_t#forceVolDrag. && !uicxx . . $ ui..forceSampleEditcxx . . $ ui_t#forceSampleEdit.)
475 mouseButtonDownHandlercxx . . $ mouseButtonDownHandler(ac50b47256ae7414).(event.buttoncxx . . $ SDL_Event#button..buttoncxx . . $ SDL_MouseButtonEvent#button.);
476 }
477
478 focusGained = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
479 }
480 #if defined __APPLE__ && defined __aarch64__
481 else if (event.type == SDL_MOUSEMOTION)
482 {
483 armMacGhostMouseCursorFix();
484 }
485 #endif
486
487 if (uicxx . . $ ui..throwExitcxx . . $ ui_t#throwExit.)
488 editorcxx . . $ editor..programRunningcxx . . $ editor_t#programRunning. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
489 }
490 }
491
492 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! initializeVarscxx . . $ initializeVars(c9857a73d68e59ba).(void)
493 {
494 setDefaultPalettecxx . . $ setDefaultPalette(49f6e7a06ebc5aa8).();
495
496 editorcxx . . $ editor..repeatKeyFlagcxx . . $ editor_t#repeatKeyFlag. = (SDL_GetModStatecxx . . $ SDL_GetModState(d49f17f06d681085).() & KMOD_CAPScxx . . $ KMOD_CAPS.) ? truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`! : falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
497 strcpycxx . . $ strcpy(b7b3a83273496486).(editorcxx . . $ editor..mixTextcxx . . $ editor_t#mixText., "MIX 01+02 TO 03");
498
499 // allocate some memory
500
501 if (!allocDiskOpVarscxx . . $ allocDiskOpVars(c9857a73d68e59ba).())
502 goto oom;
503
504 configcxx . . $ config..defModulesDircxx . . $ config_t#defModulesDir. = (char *)calloccxx . . $ calloc(63a7bf2941bfcd7a).(PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1, sizeof (char));
505 configcxx . . $ config..defSamplesDircxx . . $ config_t#defSamplesDir. = (char *)calloccxx . . $ calloc(63a7bf2941bfcd7a).(PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1, sizeof (char));
506
507 if (configcxx . . $ config..defModulesDircxx . . $ config_t#defModulesDir. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || configcxx . . $ config..defSamplesDircxx . . $ config_t#defSamplesDir. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
508 goto oom;
509
510 turnOffVoicescxx . . $ turnOffVoices(49f6e7a06ebc5aa8).();
511
512 // set various non-zero values
513
514 editorcxx . . $ editor..vol1cxx . . $ editor_t#vol1. = 100;
515 editorcxx . . $ editor..vol2cxx . . $ editor_t#vol2. = 100;
516 editorcxx . . $ editor..note1cxx . . $ editor_t#note1. = 36;
517 editorcxx . . $ editor..note2cxx . . $ editor_t#note2. = 36;
518 editorcxx . . $ editor..note3cxx . . $ editor_t#note3. = 36;
519 editorcxx . . $ editor..note4cxx . . $ editor_t#note4. = 36;
520 editorcxx . . $ editor..f7Poscxx . . $ editor_t#f7Pos. = 16;
521 editorcxx . . $ editor..f8Poscxx . . $ editor_t#f8Pos. = 32;
522 editorcxx . . $ editor..f9Poscxx . . $ editor_t#f9Pos. = 48;
523 editorcxx . . $ editor..f10Poscxx . . $ editor_t#f10Pos. = 63;
524 editorcxx . . $ editor..oldNote1cxx . . $ editor_t#oldNote1. = 36;
525 editorcxx . . $ editor..oldNote2cxx . . $ editor_t#oldNote2. = 36;
526 editorcxx . . $ editor..oldNote3cxx . . $ editor_t#oldNote3. = 36;
527 editorcxx . . $ editor..oldNote4cxx . . $ editor_t#oldNote4. = 36;
528 editorcxx . . $ editor..tuningVolcxx . . $ editor_t#tuningVol. = 32;
529 editorcxx . . $ editor..sampleVolcxx . . $ editor_t#sampleVol. = 100;
530 editorcxx . . $ editor..tuningNotecxx . . $ editor_t#tuningNote. = 24; // C-3
531 editorcxx . . $ editor..metroSpeedcxx . . $ editor_t#metroSpeed. = 4;
532 editorcxx . . $ editor..editMoveAddcxx . . $ editor_t#editMoveAdd. = 1;
533 editorcxx . . $ editor..initialTempocxx . . $ editor_t#initialTempo. = 125;
534 editorcxx . . $ editor..initialSpeedcxx . . $ editor_t#initialSpeed. = 6;
535 editorcxx . . $ editor..resampleNotecxx . . $ editor_t#resampleNote. = 24;
536 editorcxx . . $ editor..currPlayNotecxx . . $ editor_t#currPlayNote. = 24;
537 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[0] = 0x102;
538 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[1] = 0x202;
539 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[2] = 0x037;
540 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[3] = 0x047;
541 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[4] = 0x304;
542 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[5] = 0xF06;
543 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[6] = 0xC10;
544 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[7] = 0xC20;
545 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[8] = 0xE93;
546 editorcxx . . $ editor..effectMacroscxx . . $ editor_t#effectMacros.[9] = 0xA0F;
547 editorcxx . . $ editor..multiModeNextcxx . . $ editor_t#multiModeNext.[0] = 2;
548 editorcxx . . $ editor..multiModeNextcxx . . $ editor_t#multiModeNext.[1] = 3;
549 editorcxx . . $ editor..multiModeNextcxx . . $ editor_t#multiModeNext.[2] = 4;
550 editorcxx . . $ editor..multiModeNextcxx . . $ editor_t#multiModeNext.[3] = 1;
551 editorcxx . . $ editor..normalizeFiltersFlagcxx . . $ editor_t#normalizeFiltersFlag. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
552 editorcxx . . $ editor..halveSampleFlagcxx . . $ editor_t#halveSampleFlag. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
553 editorcxx . . $ editor..markStartOfscxx . . $ editor_t#markStartOfs. = -1;
554 uicxx . . $ ui..sampleMarkingPoscxx . . $ ui_t#sampleMarkingPos. = -1;
555 uicxx . . $ ui..previousPointerModecxx . . $ ui_t#previousPointerMode. = uicxx . . $ ui..pointerModecxx . . $ ui_t#pointerMode.;
556
557 // setup GUI text pointers
558 editorcxx . . $ editor..vol1Dispcxx . . $ editor_t#vol1Disp. = &editorcxx . . $ editor..vol1cxx . . $ editor_t#vol1.;
559 editorcxx . . $ editor..vol2Dispcxx . . $ editor_t#vol2Disp. = &editorcxx . . $ editor..vol2cxx . . $ editor_t#vol2.;
560 editorcxx . . $ editor..sampleToDispcxx . . $ editor_t#sampleToDisp. = &editorcxx . . $ editor..sampleTocxx . . $ editor_t#sampleTo.;
561 editorcxx . . $ editor..lpCutOffDispcxx . . $ editor_t#lpCutOffDisp. = &editorcxx . . $ editor..lpCutOffcxx . . $ editor_t#lpCutOff.;
562 editorcxx . . $ editor..hpCutOffDispcxx . . $ editor_t#hpCutOffDisp. = &editorcxx . . $ editor..hpCutOffcxx . . $ editor_t#hpCutOff.;
563 editorcxx . . $ editor..samplePosDispcxx . . $ editor_t#samplePosDisp. = &editorcxx . . $ editor..samplePoscxx . . $ editor_t#samplePos.;
564 editorcxx . . $ editor..sampleVolDispcxx . . $ editor_t#sampleVolDisp. = &editorcxx . . $ editor..sampleVolcxx . . $ editor_t#sampleVol.;
565 editorcxx . . $ editor..currSampleDispcxx . . $ editor_t#currSampleDisp. = &editorcxx . . $ editor..currSamplecxx . . $ editor_t#currSample.;
566 editorcxx . . $ editor..metroSpeedDispcxx . . $ editor_t#metroSpeedDisp. = &editorcxx . . $ editor..metroSpeedcxx . . $ editor_t#metroSpeed.;
567 editorcxx . . $ editor..sampleFromDispcxx . . $ editor_t#sampleFromDisp. = &editorcxx . . $ editor..sampleFromcxx . . $ editor_t#sampleFrom.;
568 editorcxx . . $ editor..chordLengthDispcxx . . $ editor_t#chordLengthDisp. = &editorcxx . . $ editor..chordLengthcxx . . $ editor_t#chordLength.;
569 editorcxx . . $ editor..metroChannelDispcxx . . $ editor_t#metroChannelDisp. = &editorcxx . . $ editor..metroChannelcxx . . $ editor_t#metroChannel.;
570 editorcxx . . $ editor..quantizeValueDispcxx . . $ editor_t#quantizeValueDisp. = &configcxx . . $ config..quantizeValuecxx . . $ config_t#quantizeValue.;
571
572 editorcxx . . $ editor..mod2WavFadeOutSecondscxx . . $ editor_t#mod2WavFadeOutSeconds. = 6;
573
574 editorcxx . . $ editor..programRunningcxx . . $ editor_t#programRunning. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
575 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
576
577 oom:
578 showErrorMsgBoxcxx . . $ showErrorMsgBox(326aeb464d16410c).("Out of memory!");
579 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
580 }
581
582 static void handleSigTermcxx . . $ handleSigTerm(49f6e7a06ebc5aa8).(void)
583 {
584 if (diskopcxx . . $ diskop..isFillingcxx . . $ diskop_t#isFilling.)
585 {
586 diskopcxx . . $ diskop..forceStopReadingcxx . . $ diskop_t#forceStopReading. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
587 while (diskopcxx . . $ diskop..isFillingcxx . . $ diskop_t#isFilling.) SDL_Delaycxx . . $ SDL_Delay(cb2f890c2fdad230).(5);
588 }
589
590 if (editorcxx . . $ editor..mod2WavOngoingcxx . . $ editor_t#mod2WavOngoing.)
591 {
592 editorcxx . . $ editor..abortMod2Wavcxx . . $ editor_t#abortMod2Wav. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
593 while (editorcxx . . $ editor..mod2WavOngoingcxx . . $ editor_t#mod2WavOngoing.) SDL_Delaycxx . . $ SDL_Delay(cb2f890c2fdad230).(5);
594 removeAskBoxcxx . . $ removeAskBox(49f6e7a06ebc5aa8).(); // removes MOD2WAV dialog
595 }
596
597 if (songcxx . . $ song.->modifiedcxx . . $ module_t#modified.)
598 {
599 resetAllScreenscxx . . $ resetAllScreens(49f6e7a06ebc5aa8).();
600
601 if (!videocxx . . $ video..fullscreencxx . . $ video_t#fullscreen.)
602 {
603 // de-minimize window and set focus so that the user sees the message box
604 if (SDL_GetWindowFlagscxx . . $ SDL_GetWindowFlags(9beabbd21f19984b).(videocxx . . $ video..windowcxx . . $ video_t#window.) & SDL_WINDOW_MINIMIZEDcxx . . $ SDL_WINDOW_MINIMIZED.)
605 SDL_RestoreWindowcxx . . $ SDL_RestoreWindow(8c8d6c3b662f39a2).(videocxx . . $ video..windowcxx . . $ video_t#window.);
606
607 SDL_RaiseWindowcxx . . $ SDL_RaiseWindow(8c8d6c3b662f39a2).(videocxx . . $ video..windowcxx . . $ video_t#window.);
608 }
609
610 if (askBoxcxx . . $ askBox(7371ee129cebb502).(ASKBOX_YES_NOcxx . . $ ASKBOX_YES_NO., "REALLY QUIT ?"))
611 uicxx . . $ ui..throwExitcxx . . $ ui_t#throwExit. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
612 }
613 else
614 {
615 uicxx . . $ ui..throwExitcxx . . $ ui_t#throwExit. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
616 }
617 }
618
619 // macOS/OS X specific routines
620 #ifdef __APPLE__
621 static void osxSetDirToProgramDirFromArgs(char **argv)
622 {
623 /* OS X/macOS: hackish way of setting the current working directory to the place where we double clicked
624 ** on the icon (for protracker.ini loading)
625 */
626
627 // if we launched from the terminal, argv[0][0] would be '.'
628 if (argv[0] != NULL && argv[0][0] == DIR_DELIMITER) // don't do the hack if we launched from the terminal
629 {
630 char *tmpPath = strdup(argv[0]);
631 if (tmpPath != NULL)
632 {
633 // cut off program filename
634 int32_t tmpPathLen = strlen(tmpPath);
635 for (int32_t i = tmpPathLen-1; i >= 0; i--)
636 {
637 if (tmpPath[i] == DIR_DELIMITER)
638 {
639 tmpPath[i] = '\0';
640 break;
641 }
642 }
643
644 chdir(tmpPath); // path to binary
645 chdir("../../../"); // we should now be in the directory where the config can be.
646
647 free(tmpPath);
648 }
649 }
650 }
651
652 static bool checkIfAppWasTranslocated(int argc, char **argv)
653 {
654 const char startOfStrToCmp[] = "/private/var/folders/";
655
656 // this is not 100% guaranteed to work, but it's Good Enough
657 if (argc > 0 && !_strnicmp(argv[0], startOfStrToCmp, sizeof (startOfStrToCmp)-1))
658 {
659 showErrorMsgBox(
660 "The program was translocated to a random sandbox environment for security reasons, and thus it can't find and load protracker.ini.\n\n" \
661 "Don't worry, this is normal. To fix the issue you need to move the program/.app somewhere to clear its QTN_FLAG_TRANSLOCATE flag.\n\n" \
662 "Instructions:\n" \
663 "1) Close the window.\n" \
664 "2) Move/drag (do NOT copy) the program (pt2-clone-macos) to another folder, then move it back to where it was. Don't move the folder, move the executable itself.\n" \
665 "3) Run the program again, and if you did it right it should be permanently fixed.\n\n" \
666 "This is not my fault, it's a security concept introduced in macOS 10.12 for unsigned programs downloaded and unzipped from the internet."
667 );
668
669 return true;
670 }
671
672 return false;
673 }
674 #endif
675
676 // Windows specific routines
677 #ifdef _WIN32
678 static void disableWasapi(void)
679 {
680 // disable problematic WASAPI SDL2 audio driver on Windows (causes clicks/pops sometimes...)
681
682 const int32_t numAudioDrivers = SDL_GetNumAudioDrivers();
683 if (numAudioDrivers <= 1)
684 return;
685
686 // look for directsound and enable it if found
687 for (int32_t i = 0; i < numAudioDrivers; i++)
688 {
689 const char *audioDriver = SDL_GetAudioDriver(i);
690 if (audioDriver != NULL && strcmp("directsound", audioDriver) == 0)
691 {
692 SDL_setenv("SDL_AUDIODRIVER", "directsound", true);
693 audio.rescanAudioDevicesSupported = false;
694 return;
695 }
696 }
697
698 // directsound is not available, try winmm
699 for (int32_t i = 0; i < numAudioDrivers; i++)
700 {
701 const char *audioDriver = SDL_GetAudioDriver(i);
702 if (audioDriver != NULL && strcmp("winmm", audioDriver) == 0)
703 {
704 SDL_setenv("SDL_AUDIODRIVER", "winmm", true);
705 audio.rescanAudioDevicesSupported = false;
706 return;
707 }
708 }
709
710 // we didn't find directsound or winmm, let's use wasapi after all...
711 }
712
713 static void makeSureDirIsProgramDir(void)
714 {
715 #ifndef _DEBUG
716 int32_t i;
717
718 // this can return two paths in Windows, but first one is .exe path
719 UNICHAR *path = GetCommandLineW();
720 if (path == NULL)
721 return;
722
723 UNICHAR *allocPtr = UNICHAR_STRDUP(path);
724 if (allocPtr == NULL)
725 return; // out of memory (but it doesn't matter)
726
727 path = allocPtr;
728
729 int32_t pathLen = (int32_t)UNICHAR_STRLEN(path);
730
731 // remove first "
732 if (path[0] == L'\"')
733 path++;
734
735 // end string if we find another " (there can be multiple paths)
736 for (i = 0; i < pathLen; i++)
737 {
738 if (path[i] == L'\"')
739 {
740 path[i] = L'\0';
741 pathLen = (int32_t)UNICHAR_STRLEN(path);
742 break;
743 }
744 }
745
746 // get path without filename now
747 for (i = pathLen-1; i >= 0; i--)
748 {
749 if (i < pathLen-1 && path[i] == L'\\')
750 {
751 path[i] = L'\0';
752 break;
753 }
754 }
755
756 if (i <= 0)
757 {
758 // argv[0] doesn't contain the path, we're most likely in the clone's directory now
759 free(allocPtr);
760 return;
761 }
762
763 // "C:" -> "C:\"
764 if (path[i] == L':')
765 path[i + 1] = L'\\';
766
767 SetCurrentDirectoryW(path); // this *can* fail, but it doesn't matter
768 free(allocPtr);
769 #else
770 return;
771 #endif
772 }
773
774 static bool instanceAlreadyOpen(void)
775 {
776 hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, sharedHwndName);
777 if (hMapFile != NULL)
778 return true; // another instance is already open
779
780 // no instance is open, let's created a shared memory file with hWnd in it
781 oneInstHandle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof (HWND), sharedHwndName);
782 if (oneInstHandle != NULL)
783 {
784 sharedMemBuf = (LPTSTR)MapViewOfFile(oneInstHandle, FILE_MAP_ALL_ACCESS, 0, 0, sizeof (HWND));
785 if (sharedMemBuf != NULL)
786 {
787 CopyMemory((PVOID)sharedMemBuf, &video.hWnd, sizeof (HWND));
788 UnmapViewOfFile(sharedMemBuf);
789 sharedMemBuf = NULL;
790 }
791 }
792
793 return false;
794 }
795
796 static bool handleSingleInstancing(int32_t argc, char **argv)
797 {
798 SDL_SysWMinfo wmInfo;
799
800 SDL_VERSION(&wmInfo.version);
801 if (!SDL_GetWindowWMInfo(video.window, &wmInfo))
802 return false;
803
804 video.hWnd = wmInfo.info.win.window;
805 if (instanceAlreadyOpen() && argc >= 2 && argv[1][0] != '\0')
806 {
807 sharedMemBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof (HWND));
808 if (sharedMemBuf != NULL)
809 {
810 memcpy(&hWnd_to, sharedMemBuf, sizeof (HWND));
811
812 UnmapViewOfFile(sharedMemBuf);
813 sharedMemBuf = NULL;
814 CloseHandle(hMapFile);
815 hMapFile = NULL;
816
817 hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, ARGV_SHARED_MEM_MAX_LEN, sharedFileName);
818 if (hMapFile != NULL)
819 {
820 sharedMemBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, ARGV_SHARED_MEM_MAX_LEN);
821 if (sharedMemBuf != NULL)
822 {
823 strcpy((char *)sharedMemBuf, argv[1]);
824
825 UnmapViewOfFile(sharedMemBuf);
826 sharedMemBuf = NULL;
827
828 SendMessage(hWnd_to, SYSMSG_FILE_ARG, 0, 0);
829 Sleep(80); // wait a bit to make sure first instance received msg
830
831 CloseHandle(hMapFile);
832 hMapFile = NULL;
833
834 return true; // quit instance now
835 }
836 }
837
838 return true;
839 }
840
841 CloseHandle(hMapFile);
842 hMapFile = NULL;
843 }
844
845 return false;
846 }
847
848 static void handleSysMsg(SDL_Event inputEvent)
849 {
850 SDL_SysWMmsg *wmMsg = inputEvent.syswm.msg;
851 if (wmMsg->subsystem == SDL_SYSWM_WINDOWS && wmMsg->msg.win.msg == SYSMSG_FILE_ARG)
852 {
853 hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, sharedFileName);
854 if (hMapFile != NULL)
855 {
856 sharedMemBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, ARGV_SHARED_MEM_MAX_LEN);
857 if (sharedMemBuf != NULL)
858 {
859 if (video.window != NULL && !video.fullscreen)
860 {
861 if (SDL_GetWindowFlags(video.window) & SDL_WINDOW_MINIMIZED)
862 SDL_RestoreWindow(video.window);
863
864 SDL_RaiseWindow(video.window);
865 }
866
867 loadDroppedFile((char *)sharedMemBuf, (uint32_t)strlen(sharedMemBuf), true, true);
868
869 UnmapViewOfFile(sharedMemBuf);
870 sharedMemBuf = NULL;
871 }
872
873 CloseHandle(hMapFile);
874 hMapFile = NULL;
875 }
876 }
877 }
878
879 void closeSingleInstancing(void)
880 {
881 if (oneInstHandle != NULL)
882 {
883 CloseHandle(oneInstHandle);
884 oneInstHandle = NULL;
885 }
886 }
887
888 static LONG WINAPI exceptionHandler(EXCEPTION_POINTERS *ptr)
889 #else
890 static void exceptionHandlercxx . . $ exceptionHandler(d4f767463ce0a6b3).(int32_tcxx . . $ int32_t# signal)
891 #endif
892 {
893 #define BACKUP_FILES_TO_TRYcxx . . $ `src/pt2_main.c:893:9`! 1000
894 char fileName[32];
895 uint16_tcxx . . $ uint16_t# i;
896 struct statcxx . . $ stat# statBuffer;
897
898 #ifdef _WIN32
899 (void)ptr;
900 if (oneInstHandle != NULL) CloseHandle(oneInstHandle);
901 #else
902 if (signal == 15) return;
903 #endif
904
905 if (!backupMadeAfterCrashcxx . . $ backupMadeAfterCrash.)
906 {
907 if (editorcxx . . $ editor..modulesPathUcxx . . $ editor_t#modulesPathU. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && UNICHAR_CHDIRcxx . . $ `src/pt2_unicode.h:36:9`!(editor.modulesPathU) == 0)
908 {
909 // find a free filename
910 for (i = 1; i < 1000; i++)
911 {
912 sprintfcxx . . $ sprintf(26c8642d26409d6d).(fileName, "backup%03d.mod", i);
913 if (statcxx . . $ stat(a9cd1e23e96f423c).(fileName, &statBuffer) != 0)
914 break; // file doesn't exist, we're good
915 }
916
917 if (i != 1000)
918 modSavecxx . . $ modSave(c6b7e35ff6de5671).(fileName);
919 }
920
921 backupMadeAfterCrashcxx . . $ backupMadeAfterCrash. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; // set this flag to prevent multiple backups from being saved at once
922 showErrorMsgBoxcxx . . $ showErrorMsgBox(326aeb464d16410c).(CRASH_TEXTcxx . . $ `src/pt2_main.c:40:9`!);
923 }
924
925 #ifdef _WIN32
926 return EXCEPTION_CONTINUE_SEARCH;
927 #endif
928 }
929
930 static void cleanUpcxx . . $ cleanUp(49f6e7a06ebc5aa8).(void) // never call this inside the main loop!
931 {
932 modStopcxx . . $ modStop(49f6e7a06ebc5aa8).();
933 modFreecxx . . $ modFree(49f6e7a06ebc5aa8).();
934 audioClosecxx . . $ audioClose(49f6e7a06ebc5aa8).();
935 deAllocSamplerVarscxx . . $ deAllocSamplerVars(49f6e7a06ebc5aa8).();
936 freeDiskOpMemcxx . . $ freeDiskOpMem(49f6e7a06ebc5aa8).();
937 freeDiskOpEntryMemcxx . . $ freeDiskOpEntryMem(49f6e7a06ebc5aa8).();
938 freeBMPscxx . . $ freeBMPs(49f6e7a06ebc5aa8).();
939 videoClosecxx . . $ videoClose(49f6e7a06ebc5aa8).();
940 freeSpritescxx . . $ freeSprites(49f6e7a06ebc5aa8).();
941 freeAudioDeviceListcxx . . $ freeAudioDeviceList(49f6e7a06ebc5aa8).(); // pt2_sampling.c
942
943 if (configcxx . . $ config..defModulesDircxx . . $ config_t#defModulesDir. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(configcxx . . $ config..defModulesDircxx . . $ config_t#defModulesDir.);
944 if (configcxx . . $ config..defSamplesDircxx . . $ config_t#defSamplesDir. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) freecxx . . $ free(5d22bdacc48458e8).(configcxx . . $ config..defSamplesDircxx . . $ config_t#defSamplesDir.);
945
946 #ifdef _WIN32
947 #ifndef _DEBUG
948 UnhookWindowsHookEx(g_hKeyboardHook);
949 #endif
950 if (oneInstHandle != NULL)
951 {
952 CloseHandle(oneInstHandle);
953 oneInstHandle = NULL;
954 }
955 #endif
956 }