| 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 | #include <ctype.h> |
| 12 | #ifndef _WIN32 |
| 13 | #include <unistd.h> |
| 14 | #include <limits.h> |
| 15 | #endif |
| 16 | #include "pt2_helpers.h" |
| 17 | #include "pt2_config.h" |
| 18 | #include "pt2_tables.h" |
| 19 | #include "pt2_sampler.h" |
| 20 | #include "pt2_diskop.h" // changePathToDesktop(), changePathToHome() |
| 21 | #include "pt2_visuals.h" // MAX_UPSCALE_FACTOR |
| 22 | |
| 23 | #ifndef _WIN32 |
| 24 | static char oldCwd[PATH_MAX]; |
| 25 | #endif |
| 26 | |
| 27 | config_t config; // globalized |
| 28 | |
| 29 | static bool loadProTrackerDotIni(FILE *f); |
| 30 | static FILE *openPTDotConfig(void); |
| 31 | static bool loadPTDotConfig(FILE *f); |
| 32 | static bool loadColorsDotIni(void); |
| 33 | |
| 34 | void loadConfig(void) |
| 35 | { |
| 36 | #ifndef _WIN32 |
| 37 | bool colorsDotIniFound; |
| 38 | #endif |
| 39 | FILE *f; |
| 40 | |
| 41 | // set default config values first |
| 42 | config.noDownsampleOnSmpLoad = false; |
| 43 | config.enableE8xEffect = false; |
| 44 | config.fullScreenStretch = false; |
| 45 | config.pattDots = false; |
| 46 | config.waveformCenterLine = true; |
| 47 | config.amigaModel = MODEL_A1200; |
| 48 | config.soundFrequency = 48000; |
| 49 | config.rememberPlayMode = false; |
| 50 | config.stereoSeparation = 20; |
| 51 | config.autoFitVideoScale = true; |
| 52 | config.videoScaleFactor = 0; // will be set later if autoFitVideoScale is set |
| 53 | config.realVuMeters = false; |
| 54 | config.modDot = false; |
| 55 | config.accidental = 0; // sharp |
| 56 | config.quantizeValue = 1; |
| 57 | config.transDel = false; |
| 58 | config.blankZeroFlag = false; |
| 59 | config.compoMode = false; |
| 60 | config.soundBufferSize = 1024; |
| 61 | config.autoCloseDiskOp = true; |
| 62 | config.vsyncOff = false; |
| 63 | config.hwMouse = true; |
| 64 | config.startInFullscreen = false; |
| 65 | config.pixelFilter = PIXELFILTER_NEAREST; |
| 66 | config.integerScaling = true; |
| 67 | config.audioInputFrequency = 44100; |
| 68 | config.mod2WavOutputFreq = 44100; |
| 69 | config.keepEditModeAfterStepPlay = false; |
| 70 | config.maxSampleLength = 65534; |
| 71 | config.restrictedPattEditClick = false; |
| 72 | |
| 73 | #ifndef _WIN32 |
| 74 | getcwd(oldCwd, PATH_MAX); |
| 75 | #endif |
| 76 | |
| 77 | // load protracker.ini |
| 78 | bool proTrackerDotIniFound = false; |
| 79 | |
| 80 | #ifdef _WIN32 |
| 81 | f = fopen("protracker.ini", "r"); |
| 82 | if (f != NULL) |
| 83 | proTrackerDotIniFound = true; |
| 84 | #else |
| 85 | // check in program executable directory (first priority) |
| 86 | f = fopen("protracker.ini", "r"); |
| 87 | if (f != NULL) |
| 88 | proTrackerDotIniFound = true; |
| 89 | |
| 90 | // check in ~/.config/protracker/ |
| 91 | if (!proTrackerDotIniFound && changePathToHome() && chdir(".config/protracker") == 0) |
| 92 | { |
| 93 | f = fopen("protracker.ini", "r"); |
| 94 | if (f != NULL) |
| 95 | proTrackerDotIniFound = true; |
| 96 | } |
| 97 | |
| 98 | // check in ~/.protracker/ |
| 99 | if (!proTrackerDotIniFound && changePathToHome() && chdir(".protracker") == 0) |
| 100 | { |
| 101 | f = fopen("protracker.ini", "r"); |
| 102 | if (f != NULL) |
| 103 | proTrackerDotIniFound = true; |
| 104 | } |
| 105 | |
| 106 | chdir(oldCwd); |
| 107 | #endif |
| 108 | |
| 109 | if (proTrackerDotIniFound) |
| 110 | loadProTrackerDotIni(f); |
| 111 | |
| 112 | editor.oldTempo = editor.initialTempo; |
| 113 | |
| 114 | // load PT.Config (if available) |
| 115 | bool ptDotConfigFound = false; |
| 116 | |
| 117 | #ifdef _WIN32 |
| 118 | f = openPTDotConfig(); |
| 119 | if (f != NULL) |
| 120 | ptDotConfigFound = true; |
| 121 | #else |
| 122 | // check in program executable directory (first priority) |
| 123 | f = openPTDotConfig(); |
| 124 | if (f != NULL) |
| 125 | ptDotConfigFound = true; |
| 126 | |
| 127 | // check in ~/.config/protracker/ |
| 128 | if (!ptDotConfigFound && changePathToHome() && chdir(".config/protracker") == 0) |
| 129 | { |
| 130 | f = openPTDotConfig(); |
| 131 | if (f != NULL) |
| 132 | ptDotConfigFound = true; |
| 133 | } |
| 134 | |
| 135 | // check in ~/.protracker/ |
| 136 | if (!ptDotConfigFound && changePathToHome() && chdir(".protracker") == 0) |
| 137 | { |
| 138 | f = openPTDotConfig(); |
| 139 | if (f != NULL) |
| 140 | ptDotConfigFound = true; |
| 141 | } |
| 142 | |
| 143 | chdir(oldCwd); |
| 144 | #endif |
| 145 | |
| 146 | if (ptDotConfigFound) |
| 147 | loadPTDotConfig(f); |
| 148 | |
| 149 | if (proTrackerDotIniFound || ptDotConfigFound) |
| 150 | editor.configFound = true; |
| 151 | |
| 152 | // load colors.ini (if available) |
| 153 | #ifdef _WIN32 |
| 154 | loadColorsDotIni(); |
| 155 | #else |
| 156 | // check in program executable directory (first priority) |
| 157 | colorsDotIniFound = loadColorsDotIni(); |
| 158 | |
| 159 | // check in ~/.config/protracker/ |
| 160 | if (!colorsDotIniFound && changePathToHome() && chdir(".config/protracker") == 0) |
| 161 | colorsDotIniFound = loadColorsDotIni(); |
| 162 | |
| 163 | // check in ~/.protracker/ |
| 164 | if (!colorsDotIniFound && changePathToHome() && chdir(".protracker") == 0) |
| 165 | loadColorsDotIni(); |
| 166 | |
| 167 | chdir(oldCwd); |
| 168 | #endif |
| 169 | |
| 170 | // make 'sample data mark' palette-invert table now |
| 171 | createSampleMarkTable(); |
| 172 | } |
| 173 | |
| 174 | static bool loadProTrackerDotIni(FILE *f) |
| 175 | { |
| 176 | fseek(f, 0, SEEK_END); |
| 177 | uint32_t configFileSize = ftell(f); |
| 178 | rewind(f); |
| 179 | |
| 180 | char *configBuffer = (char *)malloc(configFileSize + 1); |
| 181 | if (configBuffer == NULL) |
| 182 | { |
| 183 | fclose(f); |
| 184 | showErrorMsgBox("Couldn't parse protracker.ini: Out of memory!"); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | fread(configBuffer, 1, configFileSize, f); |
| 189 | configBuffer[configFileSize] = '\0'; |
| 190 | fclose(f); |
| 191 | |
| 192 | char *configLine = strtok(configBuffer, "\n"); |
| 193 | while (configLine != NULL) |
| 194 | { |
| 195 | uint32_t lineLen = (uint32_t)strlen(configLine); |
| 196 | |
| 197 | // remove CR in CRLF linefeed (if present) |
| 198 | if (lineLen > 1) |
| 199 | { |
| 200 | if (configLine[lineLen-1] == '\r') |
| 201 | { |
| 202 | configLine[lineLen-1] = '\0'; |
| 203 | lineLen--; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // COMMENT OR CATEGORY |
| 208 | if (*configLine == ';' || *configLine == '[') |
| 209 | { |
| 210 | configLine = strtok(NULL, "\n"); |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | // RESTRICTED_PATT_EDIT_CLICK |
| 215 | else if (!_strnicmp(configLine, "RESTRICTED_PATT_EDIT_CLICK=", 27)) |
| 216 | { |
| 217 | if (!_strnicmp(&configLine[27], "TRUE", 4)) |
| 218 | config.restrictedPattEditClick = true; |
| 219 | else if (!_strnicmp(&configLine[27], "FALSE", 5)) |
| 220 | config.restrictedPattEditClick = false; |
| 221 | } |
| 222 | |
| 223 | // STEPPLAY_KEEP_EDITMODE |
| 224 | else if (!_strnicmp(configLine, "STEPPLAY_KEEP_EDITMODE=", 23)) |
| 225 | { |
| 226 | if (!_strnicmp(&configLine[23], "TRUE", 4)) |
| 227 | config.keepEditModeAfterStepPlay = true; |
| 228 | else if (!_strnicmp(&configLine[23], "FALSE", 5)) |
| 229 | config.keepEditModeAfterStepPlay = false; |
| 230 | } |
| 231 | |
| 232 | // 64K_LIMIT |
| 233 | else if (!_strnicmp(configLine, "64K_LIMIT=", 10)) |
| 234 | { |
| 235 | if (!_strnicmp(&configLine[10], "TRUE", 4)) |
| 236 | config.maxSampleLength = 65534; |
| 237 | else if (!_strnicmp(&configLine[10], "FALSE", 5)) |
| 238 | config.maxSampleLength = 131070; |
| 239 | } |
| 240 | |
| 241 | // NO_DWNSMP_ON_SMP_LOAD (no dialog for 2x downsample after >22kHz sample load) |
| 242 | else if (!_strnicmp(configLine, "NO_DWNSMP_ON_SMP_LOAD=", 22)) |
| 243 | { |
| 244 | if (!_strnicmp(&configLine[22], "TRUE", 4)) config.noDownsampleOnSmpLoad = true; |
| 245 | else if (!_strnicmp(&configLine[22], "FALSE", 5)) config.noDownsampleOnSmpLoad = false; |
| 246 | } |
| 247 | |
| 248 | // ENABLE_E8X (Karplus-Strong command) |
| 249 | else if (!_strnicmp(configLine, "ENABLE_E8X=", 11)) |
| 250 | { |
| 251 | if (!_strnicmp(&configLine[11], "TRUE", 4)) config.enableE8xEffect = true; |
| 252 | else if (!_strnicmp(&configLine[11], "FALSE", 5)) config.enableE8xEffect = false; |
| 253 | } |
| 254 | |
| 255 | // HWMOUSE |
| 256 | else if (!_strnicmp(configLine, "HWMOUSE=", 8)) |
| 257 | { |
| 258 | if (!_strnicmp(&configLine[8], "TRUE", 4)) config.hwMouse = true; |
| 259 | else if (!_strnicmp(&configLine[8], "FALSE", 5)) config.hwMouse = false; |
| 260 | } |
| 261 | |
| 262 | // VSYNCOFF |
| 263 | else if (!_strnicmp(configLine, "VSYNCOFF=", 9)) |
| 264 | { |
| 265 | if (!_strnicmp(&configLine[9], "TRUE", 4)) config.vsyncOff = true; |
| 266 | else if (!_strnicmp(&configLine[9], "FALSE", 5)) config.vsyncOff = false; |
| 267 | } |
| 268 | |
| 269 | // INTEGERSCALING |
| 270 | else if (!_strnicmp(configLine, "INTEGERSCALING=", 15)) |
| 271 | { |
| 272 | if (!_strnicmp(&configLine[15], "TRUE", 4)) config.integerScaling = true; |
| 273 | else if (!_strnicmp(&configLine[15], "FALSE", 5)) config.integerScaling = false; |
| 274 | } |
| 275 | |
| 276 | // FULLSCREENSTRETCH |
| 277 | else if (!_strnicmp(configLine, "FULLSCREENSTRETCH=", 18)) |
| 278 | { |
| 279 | if (!_strnicmp(&configLine[18], "TRUE", 4)) config.fullScreenStretch = true; |
| 280 | else if (!_strnicmp(&configLine[18], "FALSE", 5)) config.fullScreenStretch = false; |
| 281 | } |
| 282 | |
| 283 | // HIDEDISKOPDATES |
| 284 | else if (!_strnicmp(configLine, "HIDEDISKOPDATES=", 16)) |
| 285 | { |
| 286 | if (!_strnicmp(&configLine[16], "TRUE", 4)) config.hideDiskOpDates = true; |
| 287 | else if (!_strnicmp(&configLine[16], "FALSE", 5)) config.hideDiskOpDates = false; |
| 288 | } |
| 289 | |
| 290 | // AUTOCLOSEDISKOP |
| 291 | else if (!_strnicmp(configLine, "AUTOCLOSEDISKOP=", 16)) |
| 292 | { |
| 293 | if (!_strnicmp(&configLine[16], "TRUE", 4)) config.autoCloseDiskOp = true; |
| 294 | else if (!_strnicmp(&configLine[16], "FALSE", 5)) config.autoCloseDiskOp = false; |
| 295 | } |
| 296 | |
| 297 | // FULLSCREEN |
| 298 | else if (!_strnicmp(configLine, "FULLSCREEN=", 11)) |
| 299 | { |
| 300 | if (!_strnicmp(&configLine[11], "TRUE", 4)) config.startInFullscreen = true; |
| 301 | else if (!_strnicmp(&configLine[11], "FALSE", 5)) config.startInFullscreen = false; |
| 302 | } |
| 303 | |
| 304 | // PIXELFILTER |
| 305 | else if (!_strnicmp(configLine, "PIXELFILTER=", 12)) |
| 306 | { |
| 307 | if (!_strnicmp(&configLine[12], "NEAREST", 7)) config.pixelFilter = PIXELFILTER_NEAREST; |
| 308 | else if (!_strnicmp(&configLine[12], "LINEAR", 6)) config.pixelFilter = PIXELFILTER_LINEAR; |
| 309 | else if (!_strnicmp(&configLine[12], "BEST", 4)) config.pixelFilter = PIXELFILTER_BEST; |
| 310 | } |
| 311 | |
| 312 | // COMPOMODE |
| 313 | else if (!_strnicmp(configLine, "COMPOMODE=", 10)) |
| 314 | { |
| 315 | if (!_strnicmp(&configLine[10], "TRUE", 4)) config.compoMode = true; |
| 316 | else if (!_strnicmp(&configLine[10], "FALSE", 5)) config.compoMode = false; |
| 317 | } |
| 318 | |
| 319 | // PATTDOTS |
| 320 | else if (!_strnicmp(configLine, "PATTDOTS=", 9)) |
| 321 | { |
| 322 | if (!_strnicmp(&configLine[9], "TRUE", 4)) config.pattDots = true; |
| 323 | else if (!_strnicmp(&configLine[9], "FALSE", 5)) config.pattDots = false; |
| 324 | } |
| 325 | |
| 326 | // BLANKZERO |
| 327 | else if (!_strnicmp(configLine, "BLANKZERO=", 10)) |
| 328 | { |
| 329 | if (!_strnicmp(&configLine[10], "TRUE", 4)) config.blankZeroFlag = true; |
| 330 | else if (!_strnicmp(&configLine[10], "FALSE", 5)) config.blankZeroFlag = false; |
| 331 | } |
| 332 | |
| 333 | // REALVUMETERS |
| 334 | else if (!_strnicmp(configLine, "REALVUMETERS=", 13)) |
| 335 | { |
| 336 | if (!_strnicmp(&configLine[13], "TRUE", 4)) config.realVuMeters = true; |
| 337 | else if (!_strnicmp(&configLine[13], "FALSE", 5)) config.realVuMeters = false; |
| 338 | } |
| 339 | |
| 340 | // ACCIDENTAL |
| 341 | else if (!_strnicmp(configLine, "ACCIDENTAL=", 11)) |
| 342 | { |
| 343 | if (!_strnicmp(&configLine[11], "SHARP", 4)) config.accidental = 0; |
| 344 | else if (!_strnicmp(&configLine[11], "FLAT", 5)) config.accidental = 1; |
| 345 | } |
| 346 | |
| 347 | // QUANTIZE |
| 348 | else if (!_strnicmp(configLine, "QUANTIZE=", 9)) |
| 349 | { |
| 350 | if (configLine[9] != '\0') |
| 351 | { |
| 352 | const int32_t num = atoi(&configLine[9]); |
| 353 | config.quantizeValue = (int16_t)(CLAMP(num, 0, 63)); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // TRANSDEL |
| 358 | else if (!_strnicmp(configLine, "TRANSDEL=", 9)) |
| 359 | { |
| 360 | if (!_strnicmp(&configLine[9], "TRUE", 4)) config.transDel = true; |
| 361 | else if (!_strnicmp(&configLine[9], "FALSE", 5)) config.transDel = false; |
| 362 | } |
| 363 | |
| 364 | // DOTTEDCENTER |
| 365 | else if (!_strnicmp(configLine, "DOTTEDCENTER=", 13)) |
| 366 | { |
| 367 | if (!_strnicmp(&configLine[13], "TRUE", 4)) config.waveformCenterLine = true; |
| 368 | else if (!_strnicmp(&configLine[13], "FALSE", 5)) config.waveformCenterLine = false; |
| 369 | } |
| 370 | |
| 371 | // MODDOT |
| 372 | else if (!_strnicmp(configLine, "MODDOT=", 7)) |
| 373 | { |
| 374 | if (!_strnicmp(&configLine[7], "TRUE", 4)) config.modDot = true; |
| 375 | else if (!_strnicmp(&configLine[7], "FALSE", 5)) config.modDot = false; |
| 376 | } |
| 377 | |
| 378 | // VIDEOSCALE |
| 379 | else if (!_strnicmp(configLine, "VIDEOSCALE=", 11)) |
| 380 | { |
| 381 | if (!_strnicmp(&configLine[11], "AUTO", 4)) |
| 382 | { |
| 383 | config.autoFitVideoScale = true; |
| 384 | config.videoScaleFactor = 0; // will be set later |
| 385 | } |
| 386 | else if (lineLen >= 13 && toupper(configLine[12]) == 'X' && isdigit(configLine[11])) |
| 387 | { |
| 388 | config.autoFitVideoScale = false; |
| 389 | config.videoScaleFactor = (int8_t)(configLine[11] - '0'); |
| 390 | config.videoScaleFactor = CLAMP(config.videoScaleFactor, 1, MAX_UPSCALE_FACTOR); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // REMEMBERPLAYMODE |
| 395 | else if (!_strnicmp(configLine, "REMEMBERPLAYMODE=", 17)) |
| 396 | { |
| 397 | if (!_strnicmp(&configLine[17], "TRUE", 4)) config.rememberPlayMode = true; |
| 398 | else if (!_strnicmp(&configLine[17], "FALSE", 5)) config.rememberPlayMode = false; |
| 399 | } |
| 400 | |
| 401 | // DEFAULTDIR |
| 402 | else if (!_strnicmp(configLine, "DEFAULTDIR=", 11)) |
| 403 | { |
| 404 | if (lineLen > 11) |
| 405 | { |
| 406 | uint32_t i = 11; |
| 407 | while (configLine[i] == ' ') i++; // remove spaces before string (if present) |
| 408 | while (configLine[lineLen-1] == ' ') lineLen--; // remove spaces after string (if present) |
| 409 | |
| 410 | lineLen -= i; |
| 411 | if (lineLen > 0) |
| 412 | strncpy(config.defModulesDir, &configLine[i], (lineLen > PATH_MAX) ? PATH_MAX : lineLen); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // DEFAULTSMPDIR |
| 417 | else if (!_strnicmp(configLine, "DEFAULTSMPDIR=", 14)) |
| 418 | { |
| 419 | if (lineLen > 14) |
| 420 | { |
| 421 | uint32_t i = 14; |
| 422 | while (configLine[i] == ' ') i++; // remove spaces before string (if present) |
| 423 | while (configLine[lineLen-1] == ' ') lineLen--; // remove spaces after string (if present) |
| 424 | |
| 425 | lineLen -= i; |
| 426 | if (lineLen > 0) |
| 427 | strncpy(config.defSamplesDir, &configLine[i], (lineLen > PATH_MAX) ? PATH_MAX : lineLen); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // FILTERMODEL |
| 432 | else if (!_strnicmp(configLine, "FILTERMODEL=", 12)) |
| 433 | { |
| 434 | if (!_strnicmp(&configLine[12], "A500", 4)) config.amigaModel = MODEL_A500; |
| 435 | else if (!_strnicmp(&configLine[12], "A1200", 5)) config.amigaModel = MODEL_A1200; |
| 436 | } |
| 437 | |
| 438 | // A500LOWPASSFILTER (deprecated, same as A4000LOWPASSFILTER) |
| 439 | else if (!_strnicmp(configLine, "A500LOWPASSFILTER=", 18)) |
| 440 | { |
| 441 | if (!_strnicmp(&configLine[18], "TRUE", 4)) config.amigaModel = MODEL_A500; |
| 442 | else if (!_strnicmp(&configLine[18], "FALSE", 5)) config.amigaModel = MODEL_A1200; |
| 443 | } |
| 444 | |
| 445 | // SAMPLINGFREQ |
| 446 | else if (!_strnicmp(configLine, "SAMPLINGFREQ=", 13)) |
| 447 | { |
| 448 | if (configLine[10] != '\0') |
| 449 | { |
| 450 | const int32_t num = atoi(&configLine[13]); |
| 451 | config.audioInputFrequency = CLAMP(num, 44100, 192000); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // MOD2WAVFREQUENCY |
| 456 | else if (!_strnicmp(configLine, "MOD2WAVFREQUENCY=", 17)) |
| 457 | { |
| 458 | if (configLine[17] != '\0') |
| 459 | { |
| 460 | const int32_t num = atoi(&configLine[17]); |
| 461 | config.mod2WavOutputFreq = CLAMP(num, MIN_AUDIO_FREQUENCY, MAX_AUDIO_FREQUENCY); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // FREQUENCY |
| 466 | else if (!_strnicmp(configLine, "FREQUENCY=", 10)) |
| 467 | { |
| 468 | if (configLine[10] != '\0') |
| 469 | { |
| 470 | const int32_t num = atoi(&configLine[10]); |
| 471 | config.soundFrequency = CLAMP(num, MIN_AUDIO_FREQUENCY, MAX_AUDIO_FREQUENCY); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // BUFFERSIZE |
| 476 | else if (!_strnicmp(configLine, "BUFFERSIZE=", 11)) |
| 477 | { |
| 478 | if (configLine[11] != '\0') |
| 479 | { |
| 480 | const int32_t num = atoi(&configLine[11]); |
| 481 | config.soundBufferSize = CLAMP(num, 128, 8192); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // STEREOSEPARATION |
| 486 | else if (!_strnicmp(configLine, "STEREOSEPARATION=", 17)) |
| 487 | { |
| 488 | if (configLine[17] != '\0') |
| 489 | { |
| 490 | const int32_t num = atoi(&configLine[17]); |
| 491 | config.stereoSeparation = (int8_t)(CLAMP(num, 0, 100)); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | configLine = strtok(NULL, "\n"); |
| 496 | } |
| 497 | |
| 498 | free(configBuffer); |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | static FILE *openPTDotConfig(void) |
| 503 | { |
| 504 | FILE *f = fopen("PT.Config", "rb"); // PT didn't read PT.Config with no number, but let's support it |
| 505 | if (f == NULL) |
| 506 | { |
| 507 | // try regular PT config filenames (PT.Config-xx) |
| 508 | char tmpFilename[16]; |
| 509 | int32_t i; |
| 510 | |
| 511 | for (i = 0; i < 100; i++) |
| 512 | { |
| 513 | sprintf(tmpFilename, "PT.Config-%02d", i); |
| 514 | |
| 515 | f = fopen(tmpFilename, "rb"); |
| 516 | if (f != NULL) |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | if (i == 100) |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | return f; |
| 525 | } |
| 526 | |
| 527 | static bool loadPTDotConfig(FILE *f) |
| 528 | { |
| 529 | char cfgString[24]; |
| 530 | uint8_t tmp8; |
| 531 | uint16_t tmp16; |
| 532 | |
| 533 | // get filesize |
| 534 | fseek(f, 0, SEEK_END); |
| 535 | uint32_t configFileSize = ftell(f); |
| 536 | if (configFileSize != 1024) |
| 537 | { |
| 538 | // not a valid PT.Config file |
| 539 | fclose(f); |
| 540 | return false; |
| 541 | } |
| 542 | rewind(f); |
| 543 | |
| 544 | // check if file is a PT.Config file |
| 545 | fread(cfgString, 1, 24, f); |
| 546 | |
| 547 | /* force version string to 2.3 so that we'll accept all versions. |
| 548 | ** AFAIK we're only loading values that were present since 1.0, |
| 549 | ** so it should be safe. */ |
| 550 | cfgString[2] = '2'; |
| 551 | cfgString[4] = '3'; |
| 552 | |
| 553 | if (strncmp(cfgString, "PT2.3 Configuration File", 24) != 0) |
| 554 | { |
| 555 | fclose(f); |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | // Palette |
| 560 | fseek(f, 154, SEEK_SET); |
| 561 | for (int32_t i = 0; i < 8; i++) |
| 562 | { |
| 563 | fread(&tmp16, 2, 1, f); // stored as Big-Endian |
| 564 | tmp16 = SWAP16(tmp16); |
| 565 | video.palette[i] = RGB12_to_RGB24(tmp16); |
| 566 | } |
| 567 | |
| 568 | // Transpose Delete (delete out of range notes on transposing) |
| 569 | fseek(f, 174, SEEK_SET); |
| 570 | fread(&tmp8, 1, 1, f); |
| 571 | config.transDel = tmp8 ? true : false; |
| 572 | config.transDel = config.transDel; |
| 573 | |
| 574 | // Note style (sharps/flats) |
| 575 | fseek(f, 200, SEEK_SET); |
| 576 | fread(&tmp8, 1, 1, f); |
| 577 | config.accidental = tmp8 ? 1 : 0; |
| 578 | config.accidental = config.accidental; |
| 579 | |
| 580 | // Multi Mode Next |
| 581 | fseek(f, 462, SEEK_SET); |
| 582 | fread(&editor.multiModeNext[0], 1, 1, f); |
| 583 | fread(&editor.multiModeNext[1], 1, 1, f); |
| 584 | fread(&editor.multiModeNext[2], 1, 1, f); |
| 585 | fread(&editor.multiModeNext[3], 1, 1, f); |
| 586 | |
| 587 | // Effect Macros |
| 588 | fseek(f, 466, SEEK_SET); |
| 589 | for (int32_t i = 0; i < 10; i++) |
| 590 | { |
| 591 | fread(&tmp16, 2, 1, f); // stored as Big-Endian |
| 592 | tmp16 = SWAP16(tmp16); |
| 593 | editor.effectMacros[i] = tmp16; |
| 594 | } |
| 595 | |
| 596 | // Timing Mode (CIA/VBLANK) |
| 597 | fseek(f, 487, SEEK_SET); |
| 598 | fread(&tmp8, 1, 1, f); |
| 599 | editor.timingMode = tmp8 ? TEMPO_MODE_CIA : TEMPO_MODE_VBLANK; |
| 600 | |
| 601 | // Blank Zeroes |
| 602 | fseek(f, 490, SEEK_SET); |
| 603 | fread(&tmp8, 1, 1, f); |
| 604 | config.blankZeroFlag = tmp8 ? true : false; |
| 605 | config.blankZeroFlag = config.blankZeroFlag; |
| 606 | |
| 607 | // Initial Tempo (don't load if timing is set to VBLANK) |
| 608 | if (editor.timingMode == TEMPO_MODE_CIA) |
| 609 | { |
| 610 | fseek(f, 497, SEEK_SET); |
| 611 | fread(&tmp8, 1, 1, f); |
| 612 | if (tmp8 < 32) tmp8 = 32; |
| 613 | editor.initialTempo = tmp8; |
| 614 | editor.oldTempo = tmp8; |
| 615 | } |
| 616 | |
| 617 | // Tuning Tone Note |
| 618 | fseek(f, 501, SEEK_SET); |
| 619 | fread(&tmp8, 1, 1, f); |
| 620 | if (tmp8 > 35) tmp8 = 35; |
| 621 | editor.tuningNote = tmp8; |
| 622 | |
| 623 | if (editor.tuningNote > 35) |
| 624 | editor.tuningNote = 35; |
| 625 | |
| 626 | // Tuning Tone Volume |
| 627 | fseek(f, 503, SEEK_SET); |
| 628 | fread(&tmp8, 1, 1, f); |
| 629 | if (tmp8 > 64) tmp8 = 64; |
| 630 | editor.tuningVol = tmp8; |
| 631 | |
| 632 | // Initial Speed |
| 633 | fseek(f, 545, SEEK_SET); |
| 634 | fread(&tmp8, 1, 1, f); |
| 635 | if (editor.timingMode == TEMPO_MODE_VBLANK) |
| 636 | { |
| 637 | editor.initialSpeed = tmp8; |
| 638 | } |
| 639 | else |
| 640 | { |
| 641 | if (tmp8 > 0x20) tmp8 = 0x20; |
| 642 | editor.initialSpeed = tmp8; |
| 643 | } |
| 644 | |
| 645 | // VU-Meter Colors |
| 646 | fseek(f, 546, SEEK_SET); |
| 647 | for (int32_t i = 0; i < 48; i++) |
| 648 | { |
| 649 | fread(&vuMeterColors[i], 2, 1, f); // stored as Big-Endian |
| 650 | vuMeterColors[i] = SWAP16(vuMeterColors[i]); |
| 651 | } |
| 652 | |
| 653 | // Spectrum Analyzer Colors |
| 654 | fseek(f, 642, SEEK_SET); |
| 655 | for (int32_t i = 0; i < 36; i++) |
| 656 | { |
| 657 | fread(&analyzerColors[i], 2, 1, f); // stored as Big-Endian |
| 658 | analyzerColors[i] = SWAP16(analyzerColors[i]); |
| 659 | } |
| 660 | |
| 661 | fclose(f); |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | static uint8_t hex2int(char ch) |
| 666 | { |
| 667 | ch = (char)toupper(ch); |
| 668 | |
| 669 | if (ch >= 'A' && ch <= 'F') |
| 670 | return 10 + (ch - 'A'); |
| 671 | else if (ch >= '0' && ch <= '9') |
| 672 | return ch - '0'; |
| 673 | |
| 674 | return 0; // not a hex |
| 675 | } |
| 676 | |
| 677 | static bool loadColorsDotIni(void) |
| 678 | { |
| 679 | FILE *f = fopen("colors.ini", "r"); |
| 680 | if (f == NULL) |
| 681 | return false; |
| 682 | |
| 683 | // get filesize |
| 684 | fseek(f, 0, SEEK_END); |
| 685 | uint32_t fileSize = ftell(f); |
| 686 | rewind(f); |
| 687 | |
| 688 | char *configBuffer = (char *)malloc(fileSize + 1); |
| 689 | if (configBuffer == NULL) |
| 690 | { |
| 691 | fclose(f); |
| 692 | showErrorMsgBox("Couldn't parse colors.ini: Out of memory!"); |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | fread(configBuffer, 1, fileSize, f); |
| 697 | configBuffer[fileSize] = '\0'; |
| 698 | fclose(f); |
| 699 | |
| 700 | // do parsing |
| 701 | char *configLine = strtok(configBuffer, "\n"); |
| 702 | while (configLine != NULL) |
| 703 | { |
| 704 | uint32_t lineLen = (uint32_t)strlen(configLine); |
| 705 | |
| 706 | // read palette |
| 707 | if (lineLen >= (sizeof ("[Palette]")-1)) |
| 708 | { |
| 709 | if (!_strnicmp("[Palette]", configLine, sizeof ("[Palette]")-1)) |
| 710 | { |
| 711 | configLine = strtok(NULL, "\n"); |
| 712 | |
| 713 | uint32_t line = 0; |
| 714 | while (configLine != NULL && line < 8) |
| 715 | { |
| 716 | uint16_t color = (hex2int(configLine[0]) << 8) | (hex2int(configLine[1]) << 4) | hex2int(configLine[2]); |
| 717 | color &= 0xFFF; |
| 718 | video.palette[line] = RGB12_to_RGB24(color); |
| 719 | |
| 720 | configLine = strtok(NULL, "\n"); |
| 721 | line++; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if (configLine == NULL) |
| 726 | break; |
| 727 | |
| 728 | lineLen = (uint32_t)strlen(configLine); |
| 729 | } |
| 730 | |
| 731 | // read VU-meter colors |
| 732 | if (lineLen >= sizeof ("[VU-meter]")-1) |
| 733 | { |
| 734 | if (!_strnicmp("[VU-meter]", configLine, sizeof ("[VU-meter]")-1)) |
| 735 | { |
| 736 | configLine = strtok(NULL, "\n"); |
| 737 | |
| 738 | uint32_t line = 0; |
| 739 | while (configLine != NULL && line < 48) |
| 740 | { |
| 741 | uint16_t color = (hex2int(configLine[0]) << 8) | (hex2int(configLine[1]) << 4) | hex2int(configLine[2]); |
| 742 | vuMeterColors[line] = color & 0xFFF; |
| 743 | |
| 744 | configLine = strtok(NULL, "\n"); |
| 745 | line++; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (configLine == NULL) |
| 750 | break; |
| 751 | |
| 752 | lineLen = (uint32_t)strlen(configLine); |
| 753 | } |
| 754 | |
| 755 | // read spectrum analyzer colors |
| 756 | if (lineLen >= sizeof ("[SpectrumAnalyzer]")-1) |
| 757 | { |
| 758 | if (!_strnicmp("[SpectrumAnalyzer]", configLine, sizeof ("[SpectrumAnalyzer]")-1)) |
| 759 | { |
| 760 | configLine = strtok(NULL, "\n"); |
| 761 | |
| 762 | uint32_t line = 0; |
| 763 | while (configLine != NULL && line < 36) |
| 764 | { |
| 765 | uint16_t color = (hex2int(configLine[0]) << 8) | (hex2int(configLine[1]) << 4) | hex2int(configLine[2]); |
| 766 | analyzerColors[line] = color & 0xFFF; |
| 767 | |
| 768 | configLine = strtok(NULL, "\n"); |
| 769 | line++; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (configLine == NULL) |
| 774 | break; |
| 775 | } |
| 776 | |
| 777 | configLine = strtok(NULL, "\n"); |
| 778 | } |
| 779 | |
| 780 | free(configBuffer); |
| 781 | return true; |
| 782 | } |