| 1 | // for finding memory leaks in debug mode with Visual Studio |
| 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" |
| 21 | #include "pt2_keyboard.h" |
| 22 | #include "pt2_textout.h" |
| 23 | #include "pt2_mouse.h" |
| 24 | #include "pt2_diskop.h" |
| 25 | #include "pt2_sampler.h" |
| 26 | #include "pt2_config.h" |
| 27 | #include "pt2_visuals.h" |
| 28 | #include "pt2_edit.h" |
| 29 | #include "pt2_module_loader.h" |
| 30 | #include "pt2_module_saver.h" |
| 31 | #include "pt2_scopes.h" |
| 32 | #include "pt2_audio.h" |
| 33 | #include "pt2_bmp.h" |
| 34 | #include "pt2_visuals_sync.h" |
| 35 | #include "pt2_sampling.h" |
| 36 | #include "pt2_askbox.h" |
| 37 | #include "pt2_replayer.h" |
| 38 | #include "pt2_textedit.h" |
| 39 | |
| 40 | #define CRASH_TEXT "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_t *song = NULL; // globalized |
| 46 | |
| 47 | static bool 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 exceptionHandler(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 handleInput(void); |
| 87 | static bool initializeVars(void); |
| 88 | static void handleSigTerm(void); |
| 89 | static void cleanUp(void); |
| 90 | |
| 91 | static void clearStructs(void) |
| 92 | { |
| 93 | memset(&keyb, 0, sizeof (keyb)); |
| 94 | memset(&mouse, 0, sizeof (mouse)); |
| 95 | memset(&video, 0, sizeof (video)); |
| 96 | memset(&editor, 0, sizeof (editor)); |
| 97 | memset(&diskop, 0, sizeof (diskop)); |
| 98 | memset(&cursor, 0, sizeof (cursor)); |
| 99 | memset(&ui, 0, sizeof (ui)); |
| 100 | memset(&config, 0, sizeof (config)); |
| 101 | memset(&audio, 0, sizeof (audio)); |
| 102 | memset(&textEdit, 0, sizeof (textEdit)); |
| 103 | |
| 104 | audio.rescanAudioDevicesSupported = true; |
| 105 | } |
| 106 | |
| 107 | int main(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 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_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL < 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_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); |
| 144 | SDL_EnableScreenSaver(); // allow screensaver to activate |
| 145 | |
| 146 | clearStructs(); |
| 147 | |
| 148 | // set up crash handler |
| 149 | #ifndef _DEBUG |
| 150 | #ifdef _WIN32 |
| 151 | SetUnhandledExceptionFilter(exceptionHandler); |
| 152 | #else |
| 153 | memset(&act, 0, sizeof (act)); |
| 154 | act.sa_handler = exceptionHandler; |
| 155 | act.sa_flags = SA_RESETHAND; |
| 156 | |
| 157 | sigaction(SIGILL | SIGABRT | SIGFPE | SIGSEGV, &act, &oldAct); |
| 158 | sigaction(SIGILL, &act, &oldAct); |
| 159 | sigaction(SIGABRT, &act, &oldAct); |
| 160 | sigaction(SIGFPE, &act, &oldAct); |
| 161 | sigaction(SIGSEGV, &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_VERSION >= 24 || (SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL >= 4) |
| 189 | SDL_SetHint("SDL_WINDOWS_NO_CLOSE_ON_ALT_F4", "1"); |
| 190 | #endif |
| 191 | |
| 192 | SDL_SetHint("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_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL == 9 |
| 209 | if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0) |
| 210 | #else |
| 211 | if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) != 0) |
| 212 | #endif |
| 213 | { |
| 214 | showErrorMsgBox("Couldn't initialize SDL: %s", SDL_GetError()); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | hpc_Init(); |
| 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_StopTextInput(); |
| 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 (!initializeVars()) |
| 250 | { |
| 251 | cleanUp(); |
| 252 | SDL_Quit(); |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | loadConfig(); |
| 257 | |
| 258 | if (!allocSamplerVars() || !setupVideo()) |
| 259 | { |
| 260 | cleanUp(); |
| 261 | SDL_Quit(); |
| 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_SetDurationInHz(&video.vblankHpc, VBLANK_HZ); |
| 278 | |
| 279 | if (!setupAudio() || !unpackBMPs()) |
| 280 | { |
| 281 | cleanUp(); |
| 282 | SDL_Quit(); |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | setupSprites(); |
| 287 | |
| 288 | song = createEmptyMod(); |
| 289 | if (song == NULL) |
| 290 | { |
| 291 | cleanUp(); |
| 292 | SDL_Quit(); |
| 293 | return 1; |
| 294 | } |
| 295 | |
| 296 | // setup GUI text pointers (for recently allocated song structure) |
| 297 | editor.currEditPatternDisp = &song->currPattern; |
| 298 | editor.currPosDisp = &song->currPos; |
| 299 | editor.currPatternDisp = &song->header.patternTable[0]; |
| 300 | editor.currPosEdPattDisp = &song->header.patternTable[0]; |
| 301 | editor.currLengthDisp = &song->header.songLength; |
| 302 | |
| 303 | if (!initScopes()) |
| 304 | { |
| 305 | cleanUp(); |
| 306 | SDL_Quit(); |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | modSetTempo(editor.initialTempo, false); |
| 311 | modSetSpeed(editor.initialSpeed); |
| 312 | |
| 313 | updateWindowTitle(MOD_NOT_MODIFIED); |
| 314 | pointerSetMode(POINTER_MODE_IDLE, DO_CARRY); |
| 315 | statusAllRight(); |
| 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 || strncmp(argv[1], "-psn_", 5))) |
| 319 | { |
| 320 | loadModFromArg(argv[1]); |
| 321 | |
| 322 | // play song |
| 323 | if (song->loaded) |
| 324 | { |
| 325 | editor.playMode = PLAY_MODE_NORMAL; |
| 326 | modPlay(DONT_SET_PATTERN, 0, DONT_SET_ROW); |
| 327 | editor.currMode = MODE_PLAY; |
| 328 | pointerSetMode(POINTER_MODE_PLAY, DO_CARRY); |
| 329 | statusAllRight(); |
| 330 | } |
| 331 | } |
| 332 | else if (!editor.configFound) |
| 333 | { |
| 334 | displayErrorMsg("CONFIG NOT FOUND!"); |
| 335 | } |
| 336 | |
| 337 | displayMainScreen(); |
| 338 | fillToVuMetersBgBuffer(); |
| 339 | updateCursorPos(); |
| 340 | |
| 341 | SDL_ShowWindow(video.window); |
| 342 | |
| 343 | if (config.startInFullscreen) |
| 344 | toggleFullscreen(); |
| 345 | |
| 346 | changePathToDesktop(); // change path to desktop now |
| 347 | diskOpSetInitPath(); // set path to custom path in config (if present) |
| 348 | |
| 349 | SDL_EventState(SDL_DROPFILE, SDL_ENABLE); |
| 350 | |
| 351 | editor.mainLoopOngoing = true; |
| 352 | hpc_ResetCounters(&video.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 (editor.programRunning) |
| 356 | { |
| 357 | beginFPSCounter(); |
| 358 | handleThreadedAskBox(); |
| 359 | sinkVisualizerBars(); |
| 360 | updateChannelSyncBuffer(); |
| 361 | readMouseXY(); |
| 362 | readKeyModifiers(); // set/clear CTRL/ALT/SHIFT/AMIGA key states |
| 363 | handleInput(); |
| 364 | updateMouseCounters(); |
| 365 | handleKeyRepeat(keyb.lastRepKey); |
| 366 | |
| 367 | if (!mouse.buttonWaiting && ui.sampleMarkingPos == -1 && !ui.forceSampleDrag && !ui.forceVolDrag && !ui.forceSampleEdit) |
| 368 | handleGUIButtonRepeat(); |
| 369 | |
| 370 | renderFrame(); |
| 371 | flipFrame(); |
| 372 | endFPSCounter(); |
| 373 | } |
| 374 | |
| 375 | cleanUp(); |
| 376 | SDL_Quit(); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void handleInput(void) |
| 382 | { |
| 383 | bool focusGained = false; |
| 384 | |
| 385 | SDL_Event event; |
| 386 | while (SDL_PollEvent(&event)) |
| 387 | { |
| 388 | if (event.type == SDL_WINDOWEVENT) |
| 389 | { |
| 390 | if (event.window.event == SDL_WINDOWEVENT_HIDDEN) |
| 391 | video.windowHidden = true; |
| 392 | else if (event.window.event == SDL_WINDOWEVENT_SHOWN) |
| 393 | video.windowHidden = false; |
| 394 | |
| 395 | if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) |
| 396 | focusGained = true; |
| 397 | |
| 398 | // reset vblank end time if we minimize window |
| 399 | if (event.window.event == SDL_WINDOWEVENT_MINIMIZED || event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) |
| 400 | hpc_ResetCounters(&video.vblankHpc); |
| 401 | } |
| 402 | |
| 403 | #ifdef _WIN32 |
| 404 | if (event.type == SDL_SYSWMEVENT) |
| 405 | handleSysMsg(event); |
| 406 | #endif |
| 407 | if (ui.editTextFlag && event.type == SDL_TEXTINPUT) |
| 408 | { |
| 409 | // text input when editing texts/numbers |
| 410 | |
| 411 | char inputChar = event.text.text[0]; |
| 412 | if (inputChar == '\0') |
| 413 | continue; |
| 414 | |
| 415 | handleTextEditInputChar(inputChar); |
| 416 | continue; // continue SDL event loop |
| 417 | } |
| 418 | else if (event.type == SDL_MOUSEWHEEL) |
| 419 | { |
| 420 | if (event.wheel.y < 0) mouseWheelDownHandler(); |
| 421 | else if (event.wheel.y > 0) mouseWheelUpHandler(); |
| 422 | } |
| 423 | else if (event.type == SDL_DROPFILE) |
| 424 | { |
| 425 | if (!video.fullscreen) |
| 426 | { |
| 427 | if (SDL_GetWindowFlags(video.window) & SDL_WINDOW_MINIMIZED) |
| 428 | SDL_RestoreWindow(video.window); |
| 429 | |
| 430 | SDL_RaiseWindow(video.window); |
| 431 | } |
| 432 | |
| 433 | loadDroppedFile(event.drop.file, (uint32_t)strlen(event.drop.file), false, true); |
| 434 | SDL_free(event.drop.file); |
| 435 | } |
| 436 | else if (event.type == 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 | handleSigTerm(); |
| 446 | |
| 447 | #ifdef __APPLE__ |
| 448 | editor.macCmdQIssued = false; // read note above |
| 449 | #endif |
| 450 | } |
| 451 | else if (event.type == SDL_KEYUP) |
| 452 | { |
| 453 | keyUpHandler(event.key.keysym.scancode); |
| 454 | } |
| 455 | else if (event.type == SDL_KEYDOWN) |
| 456 | { |
| 457 | if (editor.repeatKeyFlag || keyb.lastRepKey != event.key.keysym.scancode) |
| 458 | keyDownHandler(event.key.keysym.scancode, event.key.keysym.sym); |
| 459 | } |
| 460 | else if (event.type == SDL_MOUSEBUTTONUP) |
| 461 | { |
| 462 | #if defined __APPLE__ && defined __aarch64__ |
| 463 | armMacGhostMouseCursorFix(); |
| 464 | #endif |
| 465 | mouseButtonUpHandler(event.button.button); |
| 466 | } |
| 467 | else if (event.type == 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 || ui.askBoxShown) |
| 473 | { |
| 474 | if (ui.sampleMarkingPos == -1 && !ui.forceSampleDrag && !ui.forceVolDrag && !ui.forceSampleEdit) |
| 475 | mouseButtonDownHandler(event.button.button); |
| 476 | } |
| 477 | |
| 478 | focusGained = false; |
| 479 | } |
| 480 | #if defined __APPLE__ && defined __aarch64__ |
| 481 | else if (event.type == SDL_MOUSEMOTION) |
| 482 | { |
| 483 | armMacGhostMouseCursorFix(); |
| 484 | } |
| 485 | #endif |
| 486 | |
| 487 | if (ui.throwExit) |
| 488 | editor.programRunning = false; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static bool initializeVars(void) |
| 493 | { |
| 494 | setDefaultPalette(); |
| 495 | |
| 496 | editor.repeatKeyFlag = (SDL_GetModState() & KMOD_CAPS) ? true : false; |
| 497 | strcpy(editor.mixText, "MIX 01+02 TO 03"); |
| 498 | |
| 499 | // allocate some memory |
| 500 | |
| 501 | if (!allocDiskOpVars()) |
| 502 | goto oom; |
| 503 | |
| 504 | config.defModulesDir = (char *)calloc(PATH_MAX + 1, sizeof (char)); |
| 505 | config.defSamplesDir = (char *)calloc(PATH_MAX + 1, sizeof (char)); |
| 506 | |
| 507 | if (config.defModulesDir == NULL || config.defSamplesDir == NULL) |
| 508 | goto oom; |
| 509 | |
| 510 | turnOffVoices(); |
| 511 | |
| 512 | // set various non-zero values |
| 513 | |
| 514 | editor.vol1 = 100; |
| 515 | editor.vol2 = 100; |
| 516 | editor.note1 = 36; |
| 517 | editor.note2 = 36; |
| 518 | editor.note3 = 36; |
| 519 | editor.note4 = 36; |
| 520 | editor.f7Pos = 16; |
| 521 | editor.f8Pos = 32; |
| 522 | editor.f9Pos = 48; |
| 523 | editor.f10Pos = 63; |
| 524 | editor.oldNote1 = 36; |
| 525 | editor.oldNote2 = 36; |
| 526 | editor.oldNote3 = 36; |
| 527 | editor.oldNote4 = 36; |
| 528 | editor.tuningVol = 32; |
| 529 | editor.sampleVol = 100; |
| 530 | editor.tuningNote = 24; // C-3 |
| 531 | editor.metroSpeed = 4; |
| 532 | editor.editMoveAdd = 1; |
| 533 | editor.initialTempo = 125; |
| 534 | editor.initialSpeed = 6; |
| 535 | editor.resampleNote = 24; |
| 536 | editor.currPlayNote = 24; |
| 537 | editor.effectMacros[0] = 0x102; |
| 538 | editor.effectMacros[1] = 0x202; |
| 539 | editor.effectMacros[2] = 0x037; |
| 540 | editor.effectMacros[3] = 0x047; |
| 541 | editor.effectMacros[4] = 0x304; |
| 542 | editor.effectMacros[5] = 0xF06; |
| 543 | editor.effectMacros[6] = 0xC10; |
| 544 | editor.effectMacros[7] = 0xC20; |
| 545 | editor.effectMacros[8] = 0xE93; |
| 546 | editor.effectMacros[9] = 0xA0F; |
| 547 | editor.multiModeNext[0] = 2; |
| 548 | editor.multiModeNext[1] = 3; |
| 549 | editor.multiModeNext[2] = 4; |
| 550 | editor.multiModeNext[3] = 1; |
| 551 | editor.normalizeFiltersFlag = true; |
| 552 | editor.halveSampleFlag = true; |
| 553 | editor.markStartOfs = -1; |
| 554 | ui.sampleMarkingPos = -1; |
| 555 | ui.previousPointerMode = ui.pointerMode; |
| 556 | |
| 557 | // setup GUI text pointers |
| 558 | editor.vol1Disp = &editor.vol1; |
| 559 | editor.vol2Disp = &editor.vol2; |
| 560 | editor.sampleToDisp = &editor.sampleTo; |
| 561 | editor.lpCutOffDisp = &editor.lpCutOff; |
| 562 | editor.hpCutOffDisp = &editor.hpCutOff; |
| 563 | editor.samplePosDisp = &editor.samplePos; |
| 564 | editor.sampleVolDisp = &editor.sampleVol; |
| 565 | editor.currSampleDisp = &editor.currSample; |
| 566 | editor.metroSpeedDisp = &editor.metroSpeed; |
| 567 | editor.sampleFromDisp = &editor.sampleFrom; |
| 568 | editor.chordLengthDisp = &editor.chordLength; |
| 569 | editor.metroChannelDisp = &editor.metroChannel; |
| 570 | editor.quantizeValueDisp = &config.quantizeValue; |
| 571 | |
| 572 | editor.mod2WavFadeOutSeconds = 6; |
| 573 | |
| 574 | editor.programRunning = true; |
| 575 | return true; |
| 576 | |
| 577 | oom: |
| 578 | showErrorMsgBox("Out of memory!"); |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | static void handleSigTerm(void) |
| 583 | { |
| 584 | if (diskop.isFilling) |
| 585 | { |
| 586 | diskop.forceStopReading = true; |
| 587 | while (diskop.isFilling) SDL_Delay(5); |
| 588 | } |
| 589 | |
| 590 | if (editor.mod2WavOngoing) |
| 591 | { |
| 592 | editor.abortMod2Wav = true; |
| 593 | while (editor.mod2WavOngoing) SDL_Delay(5); |
| 594 | removeAskBox(); // removes MOD2WAV dialog |
| 595 | } |
| 596 | |
| 597 | if (song->modified) |
| 598 | { |
| 599 | resetAllScreens(); |
| 600 | |
| 601 | if (!video.fullscreen) |
| 602 | { |
| 603 | // de-minimize window and set focus so that the user sees the message box |
| 604 | if (SDL_GetWindowFlags(video.window) & SDL_WINDOW_MINIMIZED) |
| 605 | SDL_RestoreWindow(video.window); |
| 606 | |
| 607 | SDL_RaiseWindow(video.window); |
| 608 | } |
| 609 | |
| 610 | if (askBox(ASKBOX_YES_NO, "REALLY QUIT ?")) |
| 611 | ui.throwExit = true; |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | ui.throwExit = true; |
| 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 exceptionHandler(int32_t signal) |
| 891 | #endif |
| 892 | { |
| 893 | #define BACKUP_FILES_TO_TRY 1000 |
| 894 | char fileName[32]; |
| 895 | uint16_t i; |
| 896 | struct 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 (!backupMadeAfterCrash) |
| 906 | { |
| 907 | if (editor.modulesPathU != NULL && UNICHAR_CHDIR(editor.modulesPathU) == 0) |
| 908 | { |
| 909 | // find a free filename |
| 910 | for (i = 1; i < 1000; i++) |
| 911 | { |
| 912 | sprintf(fileName, "backup%03d.mod", i); |
| 913 | if (stat(fileName, &statBuffer) != 0) |
| 914 | break; // file doesn't exist, we're good |
| 915 | } |
| 916 | |
| 917 | if (i != 1000) |
| 918 | modSave(fileName); |
| 919 | } |
| 920 | |
| 921 | backupMadeAfterCrash = true; // set this flag to prevent multiple backups from being saved at once |
| 922 | showErrorMsgBox(CRASH_TEXT); |
| 923 | } |
| 924 | |
| 925 | #ifdef _WIN32 |
| 926 | return EXCEPTION_CONTINUE_SEARCH; |
| 927 | #endif |
| 928 | } |
| 929 | |
| 930 | static void cleanUp(void) // never call this inside the main loop! |
| 931 | { |
| 932 | modStop(); |
| 933 | modFree(); |
| 934 | audioClose(); |
| 935 | deAllocSamplerVars(); |
| 936 | freeDiskOpMem(); |
| 937 | freeDiskOpEntryMem(); |
| 938 | freeBMPs(); |
| 939 | videoClose(); |
| 940 | freeSprites(); |
| 941 | freeAudioDeviceList(); // pt2_sampling.c |
| 942 | |
| 943 | if (config.defModulesDir != NULL) free(config.defModulesDir); |
| 944 | if (config.defSamplesDir != NULL) free(config.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 | } |