Debian Codegraph @

src/pt2_diskop.c

raw symbols SCIP
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 <math.h>
12 #include <ctype.h>
13 #ifdef _WIN32
14 #include <direct.h>
15 #include <io.h>
16 #define WIN32_MEAN_AND_LEAN
17 #include <windows.h>
18 #include <shlobj.h> // SHGetFolderPathW()
19 #else
20 #include <unistd.h>
21 #include <dirent.h>
22 #endif
23 #include <fcntl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <time.h>
27 #include <limits.h>
28 #include "pt2_textout.h"
29 #include "pt2_diskop.h"
30 #include "pt2_tables.h"
31 #include "pt2_module_loader.h"
32 #include "pt2_config.h"
33 #include "pt2_visuals.h"
34 #include "pt2_sample_loader.h"
35 #include "pt2_bmp.h"
36 #include "pt2_askbox.h"
37 #include "pt2_replayer.h"
38
39 typedef struct fileEntry_t
40 {
41 UNICHAR *nameU;
42 char firstAnsiChar; // for handleEntryJumping();
43 char dateChanged[6 + 1];
44 bool isDir;
45 int32_t filesize;
46 } fileEntry_t;
47
48 // "look for file" flags
49 enum
50 {
51 LFF_DONE = 0,
52 LFF_SKIP = 1,
53 LFF_OK = 2
54 };
55
56 #ifdef _WIN32
57 #define PARENT_DIR_STR L".."
58 static HANDLE hFind;
59 #else
60 #define PARENT_DIR_STR ".."
61 static DIR *hFind;
62 #endif
63
64 static char fileNameBuffer[PATH_MAX + 1];
65 static UNICHAR pathTmp[PATH_MAX + 2];
66 static fileEntry_t *diskOpEntry;
67
68 void addSampleFileExt(char *fileName)
69 {
70 switch (diskop.smpSaveType)
71 {
72 case DISKOP_SMP_WAV: strcat(fileName, ".wav"); break;
73 case DISKOP_SMP_IFF: strcat(fileName, ".iff"); break;
74 default: case DISKOP_SMP_RAW: break;
75 }
76 }
77
78 static fileEntry_t *bufferCreateEmptyDir(void) // special case: creates a dir entry with a ".." directory
79 {
80 fileEntry_t *dirEntry = (fileEntry_t *)malloc(sizeof (fileEntry_t));
81 if (dirEntry == NULL)
82 return NULL;
83
84 dirEntry->nameU = UNICHAR_STRDUP(PARENT_DIR_STR);
85 if (dirEntry->nameU == NULL)
86 {
87 free(dirEntry);
88 return NULL;
89 }
90
91 dirEntry->isDir = true;
92 dirEntry->filesize = 0;
93
94 return dirEntry;
95 }
96
97 // deciding whether to load this entry into the buffer or not
98 static bool listEntry(fileEntry_t *f)
99 {
100 int32_t entryName = (int32_t)UNICHAR_STRLEN(f->nameU);
101
102 #ifdef _WIN32
103 if (f->isDir && entryName == 2 && !UNICHAR_STRNICMP(f->nameU, "..", 2))
104 return true; // always show ".." directory
105 #else
106 if (f->isDir && entryName == 2 && !UNICHAR_STRNICMP(f->nameU, "..", 2) && !(editor.currPath[0] == '/' && editor.currPath[1] == '\0'))
107 return true; // always show ".." directory (unless in root dir)
108 #endif
109
110 if (!UNICHAR_STRNICMP(f->nameU, ".", 1))
111 return false; // skip ".name" entries
112
113 if (f->isDir || diskop.mode == DISKOP_MODE_SMP)
114 return true; // list all entries
115
116 if (entryName >= 4)
117 {
118 if (!UNICHAR_STRNICMP(f->nameU, "MOD.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".MOD", 4) ||
119 !UNICHAR_STRNICMP(f->nameU, "STK.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".STK", 4) ||
120 !UNICHAR_STRNICMP(f->nameU, "M15.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".M15", 4) ||
121 !UNICHAR_STRNICMP(f->nameU, "NST.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".NST", 4) ||
122 !UNICHAR_STRNICMP(f->nameU, "UST.", 4) || !UNICHAR_STRNICMP(&f->nameU[entryName - 4], ".UST", 4) ||
123 !UNICHAR_STRNICMP(f->nameU, "PP.", 3) || !UNICHAR_STRNICMP(&f->nameU[entryName - 3], ".PP", 3) ||
124 !UNICHAR_STRNICMP(f->nameU, "NT.", 3) || !UNICHAR_STRNICMP(&f->nameU[entryName - 3], ".NT", 3))
125 {
126 return true;
127 }
128 }
129
130 return false;
131 }
132
133 static int8_t findFirst(fileEntry_t *searchRec)
134 {
135 #ifdef _WIN32
136 WIN32_FIND_DATAW fData;
137 SYSTEMTIME sysTime;
138 #else
139 struct dirent *fData;
140 struct stat st;
141 #endif
142
143 searchRec->nameU = NULL; // this one must be initialized
144
145 #ifdef _WIN32
146 hFind = FindFirstFileW(L"*", &fData);
147 if (hFind == NULL || hFind == INVALID_HANDLE_VALUE)
148 return LFF_DONE;
149
150 searchRec->nameU = UNICHAR_STRDUP(fData.cFileName);
151 if (searchRec->nameU == NULL)
152 return LFF_SKIP;
153
154 searchRec->filesize = (fData.nFileSizeHigh > 0) ? -1 : fData.nFileSizeLow;
155 searchRec->isDir = (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
156 #else
157 hFind = opendir(".");
158 if (hFind == NULL)
159 return LFF_DONE;
160
161 fData = readdir(hFind);
162 if (fData == NULL)
163 return LFF_DONE;
164
165 searchRec->nameU = UNICHAR_STRDUP(fData->d_name);
166 if (searchRec->nameU == NULL)
167 return LFF_SKIP;
168
169 searchRec->filesize = 0;
170 searchRec->isDir = false;
171
172 if (stat(fData->d_name, &st) == 0)
173 {
174 searchRec->isDir = !!(st.st_mode & S_IFDIR);
175 searchRec->filesize = ((int64_t)st.st_size > INT32_MAX) ? -1 : (int32_t)st.st_size;
176 }
177 #endif
178
179 if (searchRec->filesize < -1)
180 searchRec->filesize = -1;
181
182 if (!listEntry(searchRec))
183 {
184 // skip entry
185 free(searchRec->nameU);
186 searchRec->nameU = NULL;
187 return LFF_SKIP;
188 }
189
190 #ifdef _WIN32
191 FileTimeToSystemTime(&fData.ftLastWriteTime, &sysTime);
192 snprintf(searchRec->dateChanged, 7, "%02d%02d%02d", sysTime.wDay, sysTime.wMonth, sysTime.wYear % 100);
193 unicharToAnsi(&searchRec->firstAnsiChar, searchRec->nameU, 1);
194 #else
195 strftime(searchRec->dateChanged, 7, "%d%m%y", localtime(&st.st_mtime));
196 searchRec->firstAnsiChar = (char)searchRec->nameU[0];
197 #endif
198
199 return LFF_OK;
200 }
201
202 static int8_t findNext(fileEntry_t *searchRec)
203 {
204 #ifdef _WIN32
205 WIN32_FIND_DATAW fData;
206 SYSTEMTIME sysTime;
207 #else
208 struct dirent *fData;
209 struct stat st;
210 #endif
211
212 searchRec->nameU = NULL; // important
213
214 #ifdef _WIN32
215 if (hFind == NULL || FindNextFileW(hFind, &fData) == 0)
216 return LFF_DONE;
217
218 searchRec->nameU = UNICHAR_STRDUP(fData.cFileName);
219 if (searchRec->nameU == NULL)
220 return LFF_SKIP;
221
222 searchRec->filesize = (fData.nFileSizeHigh > 0) ? -1 : fData.nFileSizeLow;
223 searchRec->isDir = (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? true : false;
224
225 FileTimeToSystemTime(&fData.ftLastWriteTime, &sysTime);
226 #else
227 if (hFind == NULL || (fData = readdir(hFind)) == NULL)
228 return LFF_DONE;
229
230 searchRec->nameU = UNICHAR_STRDUP(fData->d_name);
231 if (searchRec->nameU == NULL)
232 return LFF_SKIP;
233
234 searchRec->filesize = 0;
235 searchRec->isDir = false;
236
237 if (stat(fData->d_name, &st) == 0)
238 {
239 searchRec->isDir = !!(st.st_mode & S_IFDIR);
240 searchRec->filesize = ((int64_t)st.st_size > INT32_MAX) ? -1 : (int32_t)st.st_size;
241 }
242 #endif
243
244 if (searchRec->filesize < -1)
245 searchRec->filesize = -1;
246
247 if (!listEntry(searchRec))
248 {
249 // skip entry
250 free(searchRec->nameU);
251 searchRec->nameU = NULL;
252 return LFF_SKIP;
253 }
254
255 #ifdef _WIN32
256 FileTimeToSystemTime(&fData.ftLastWriteTime, &sysTime);
257 snprintf(searchRec->dateChanged, 7, "%02d%02d%02d", sysTime.wDay, sysTime.wMonth, sysTime.wYear % 100);
258 unicharToAnsi(&searchRec->firstAnsiChar, searchRec->nameU, 1);
259 #else
260 strftime(searchRec->dateChanged, 7, "%d%m%y", localtime(&st.st_mtime));
261 searchRec->firstAnsiChar = (char)searchRec->nameU[0];
262 #endif
263
264 return LFF_OK;
265 }
266
267 static void findClose(void)
268 {
269 if (hFind != NULL)
270 {
271 #ifdef _WIN32
272 FindClose(hFind);
273 #else
274 closedir(hFind);
275 #endif
276 hFind = NULL;
277 }
278 }
279
280 void diskOpShowSelectText(void)
281 {
282 if (ui.pointerMode == POINTER_MODE_MSG1 || editor.errorMsgActive)
283 return;
284
285 if (diskop.mode == DISKOP_MODE_MOD)
286 setStatusMessage("SELECT MODULE", NO_CARRY);
287 else
288 setStatusMessage("SELECT SAMPLE", NO_CARRY);
289 }
290
291 void handleEntryJumping(SDL_Keycode jumpToChar) // SHIFT+character
292 {
293 if (diskOpEntry != NULL)
294 {
295 fileEntry_t *f;
296
297 // if last jump key was the same, go to next page of matching character
298 if (diskop.numEntries > DISKOP_LINES && diskop.lastEntryJumpKey == jumpToChar)
299 {
300 for (int32_t i = 0; i < DISKOP_LINES; i++)
301 {
302 int32_t offset = diskop.scrollOffset+DISKOP_LINES;
303 if (offset > diskop.numEntries-DISKOP_LINES)
304 offset = diskop.numEntries-DISKOP_LINES;
305
306 f = &diskOpEntry[offset];
307
308 if (!f->isDir && tolower(f->firstAnsiChar) == jumpToChar)
309 {
310 diskop.scrollOffset += DISKOP_LINES;
311 if (diskop.scrollOffset > diskop.numEntries-DISKOP_LINES)
312 diskop.scrollOffset = diskop.numEntries-DISKOP_LINES;
313
314 ui.updateDiskOpFileList = true;
315 return;
316 }
317 }
318 }
319
320 diskop.lastEntryJumpKey = jumpToChar;
321
322 // jump to first match from the beginning of file list
323
324 f = diskOpEntry;
325 for (int32_t i = 0; i < diskop.numEntries; i++, f++)
326 {
327 if (!f->isDir && tolower(f->firstAnsiChar) == jumpToChar)
328 {
329 if (diskop.numEntries > DISKOP_LINES)
330 {
331 diskop.scrollOffset = i;
332 if (diskop.scrollOffset > diskop.numEntries-DISKOP_LINES)
333 diskop.scrollOffset = diskop.numEntries-DISKOP_LINES;
334
335 ui.updateDiskOpFileList = true;
336 }
337
338 return;
339 }
340 }
341 }
342
343 // character not found in file list, show red mouse pointer (error)
344 editor.errorMsgActive = true;
345 editor.errorMsgBlock = true;
346 editor.errorMsgCounter = 0;
347
348 setErrPointer();
349 }
350
351 bool diskOpEntryIsEmpty(int32_t fileIndex)
352 {
353 if (diskop.scrollOffset+fileIndex >= diskop.numEntries)
354 return true;
355
356 return false;
357 }
358
359 bool diskOpEntryIsDir(int32_t fileIndex)
360 {
361 if (diskOpEntry != NULL)
362 {
363 if (!diskOpEntryIsEmpty(fileIndex))
364 return diskOpEntry[diskop.scrollOffset+fileIndex].isDir;
365 }
366
367 return false; // couldn't look up entry
368 }
369
370 char *diskOpGetAnsiEntry(int32_t fileIndex)
371 {
372 if (diskOpEntry != NULL && !diskOpEntryIsEmpty(fileIndex))
373 {
374 UNICHAR *filenameU = diskOpEntry[diskop.scrollOffset+fileIndex].nameU;
375 if (filenameU != NULL)
376 {
377 unicharToAnsi(fileNameBuffer, filenameU, PATH_MAX);
378 return fileNameBuffer;
379 }
380 }
381
382 return NULL;
383 }
384
385 UNICHAR *diskOpGetUnicodeEntry(int32_t fileIndex)
386 {
387 if (diskOpEntry != NULL && !diskOpEntryIsEmpty(fileIndex))
388 return diskOpEntry[diskop.scrollOffset+fileIndex].nameU;
389
390 return NULL;
391 }
392
393 static void setVisualPathToCwd(void)
394 {
395 memset(editor.currPath, 0, PATH_MAX + 10);
396 memset(editor.currPathU, 0, (PATH_MAX + 2) * sizeof (UNICHAR));
397 UNICHAR_GETCWD(editor.currPathU, PATH_MAX);
398
399 if (diskop.mode == DISKOP_MODE_MOD)
400 UNICHAR_STRCPY(editor.modulesPathU, editor.currPathU);
401 else
402 UNICHAR_STRCPY(editor.samplesPathU, editor.currPathU);
403
404 unicharToAnsi(editor.currPath, editor.currPathU, PATH_MAX);
405 ui.updateDiskOpPathText = true;
406 }
407
408 bool changePathToDesktop(void)
409 {
410 #ifdef _WIN32
411 UNICHAR pathU[PATH_MAX + 2];
412 if (SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, pathU) >= 0)
413 {
414 if (UNICHAR_CHDIR(pathU) == 0)
415 return true;
416 }
417
418 return false;
419 #else
420 char *homePath = getenv("HOME");
421 if (homePath != NULL && chdir(homePath) == 0)
422 {
423 chdir("Desktop"); // keep home dir in case we couldn't change the dir to desktop
424 return true;
425 }
426
427 return false;
428 #endif
429 }
430
431 #ifndef _WIN32
432 bool changePathToHome(void)
433 {
434 char *homePath = getenv("HOME");
435 if (homePath != NULL && chdir(homePath) == 0)
436 return true;
437
438 return false;
439 }
440 #endif
441
442 void setPathFromDiskOpMode(void)
443 {
444 UNICHAR_CHDIR((diskop.mode == DISKOP_MODE_MOD) ? editor.modulesPathU : editor.samplesPathU);
445 setVisualPathToCwd();
446 }
447
448 bool diskOpSetPath(UNICHAR *path, bool cache)
449 {
450 if (path != NULL && UNICHAR_CHDIR(path) == 0)
451 {
452 setVisualPathToCwd();
453
454 if (cache)
455 diskop.cached = false;
456
457 if (ui.diskOpScreenShown)
458 ui.updateDiskOpFileList = true;
459
460 diskop.scrollOffset = 0;
461 diskop.lastEntryJumpKey = SDLK_UNKNOWN;
462
463 return true;
464 }
465 else
466 {
467 setVisualPathToCwd();
468 displayErrorMsg("CAN'T OPEN DIR !");
469 return false;
470 }
471 }
472
473 void diskOpSetInitPath(void)
474 {
475 // default module path
476 if (config.defModulesDir[0] != '\0')
477 {
478 #ifdef _WIN32
479 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, config.defModulesDir, -1, pathTmp, PATH_MAX);
480 #else
481 strcpy(pathTmp, config.defModulesDir);
482 #endif
483 UNICHAR_STRCPY(editor.modulesPathU, pathTmp);
484 }
485 else
486 {
487 // no path set in config, set to current working directory
488 UNICHAR_GETCWD(editor.modulesPathU, PATH_MAX);
489 }
490
491 // default sample path
492 if (config.defSamplesDir[0] != '\0')
493 {
494 #ifdef _WIN32
495 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, config.defSamplesDir, -1, pathTmp, PATH_MAX);
496 #else
497 strcpy(pathTmp, config.defSamplesDir);
498 #endif
499 UNICHAR_STRCPY(editor.samplesPathU, pathTmp);
500 }
501 else
502 {
503 // no path set in config, set to current working directory
504 UNICHAR_GETCWD(editor.samplesPathU, PATH_MAX);
505 }
506
507 setPathFromDiskOpMode();
508 }
509
510 bool allocDiskOpVars(void)
511 {
512 editor.fileNameTmpU = (UNICHAR *)calloc(PATH_MAX + 2, sizeof (UNICHAR));
513 editor.entryNameTmp = (char *)calloc(PATH_MAX + 10, sizeof (char));
514 editor.currPath = (char *)calloc(PATH_MAX + 10, sizeof (char));
515 editor.currPathU = (UNICHAR *)calloc(PATH_MAX + 2, sizeof (UNICHAR));
516 editor.modulesPathU = (UNICHAR *)calloc(PATH_MAX + 2, sizeof (UNICHAR));
517 editor.samplesPathU = (UNICHAR *)calloc(PATH_MAX + 2, sizeof (UNICHAR));
518
519 if (editor.fileNameTmpU == NULL || editor.entryNameTmp == NULL ||
520 editor.currPath == NULL || editor.currPathU == NULL ||
521 editor.modulesPathU == NULL || editor.samplesPathU == NULL)
522 {
523 // allocated leftovers are free'd lateron
524 return false;
525 }
526
527 return true;
528 }
529
530 void freeDiskOpMem(void)
531 {
532 if (editor.fileNameTmpU != NULL) free(editor.fileNameTmpU);
533 if (editor.entryNameTmp != NULL) free(editor.entryNameTmp);
534 if (editor.currPath != NULL) free(editor.currPath);
535 if (editor.currPathU != NULL) free(editor.currPathU);
536 if (editor.modulesPathU != NULL) free(editor.modulesPathU);
537 if (editor.samplesPathU != NULL) free(editor.samplesPathU);
538 }
539
540 void freeDiskOpEntryMem(void)
541 {
542 if (diskOpEntry != NULL)
543 {
544 for (int32_t i = 0; i < diskop.numEntries; i++)
545 {
546 if (diskOpEntry[i].nameU != NULL)
547 free(diskOpEntry[i].nameU);
548 }
549
550 free(diskOpEntry);
551 diskOpEntry = NULL;
552 }
553
554 diskop.numEntries = 0;
555 }
556
557 // thanks to aTc for creating this simplified routine for qsort() (I edited it a little bit)
558 static int32_t fileEntryCompare(const void *f1, const void *f2)
559 {
560 static char fn_1[PATH_MAX+1], fn_2[PATH_MAX+1];
561
562 fileEntry_t *fe1 = (fileEntry_t *)f1;
563 fileEntry_t *fe2 = (fileEntry_t *)f2;
564
565 unicharToAnsi(fn_1, fe1->nameU, PATH_MAX);
566 unicharToAnsi(fn_2, fe2->nameU, PATH_MAX);
567
568 // ".." directories are always sorted first
569 if (fe1->isDir && strcmp(fn_1, "..") == 0) return -1;
570 if (fe2->isDir && strcmp(fn_2, "..") == 0) return 1;
571
572 /* ProTracker handles names in upper case during sorting,
573 ** convert the string case.
574 */
575 const int32_t fe1_l = (int32_t)strlen(fn_1);
576 const int32_t fe2_l = (int32_t)strlen(fn_2);
577 for (int32_t i = 0; i < fe1_l; i++) fn_1[i] = (char)toupper(fn_1[i]);
578 for (int32_t i = 0; i < fe2_l; i++) fn_2[i] = (char)toupper(fn_2[i]);
579
580 // if both entries are the same type
581 if (fe1->isDir == fe2->isDir)
582 return strcmp(fn_1, fn_2);
583
584 // different types, so one of them is a dir
585 if (fe1->isDir)
586 return -1; // first one is a dir
587
588 return 1; // second one is a dir
589 }
590
591 static void sortEntries(void)
592 {
593 if (diskop.numEntries >= 2)
594 qsort(diskOpEntry, diskop.numEntries, sizeof (fileEntry_t), fileEntryCompare);
595 }
596
597 static bool diskOpFillBuffer(void)
598 {
599 fileEntry_t tmpBuffer;
600
601 diskop.scrollOffset = 0;
602 diskop.lastEntryJumpKey = SDLK_UNKNOWN;
603
604 // do we have a path set?
605 if (editor.currPathU[0] == '\0')
606 setVisualPathToCwd();
607
608 freeDiskOpEntryMem();
609
610 // fill disk op. buffer (type, size, path, file name, date changed)
611
612 // read first file
613 uint8_t lastFindFileFlag = findFirst(&tmpBuffer);
614 if (lastFindFileFlag != LFF_DONE && lastFindFileFlag != LFF_SKIP)
615 {
616 diskOpEntry = (fileEntry_t *)malloc(sizeof (fileEntry_t) * (diskop.numEntries + 1));
617 if (diskOpEntry == NULL)
618 {
619 findClose();
620 freeDiskOpEntryMem();
621 statusOutOfMemory();
622 return false;
623 }
624
625 memcpy(&diskOpEntry[diskop.numEntries], &tmpBuffer, sizeof (fileEntry_t));
626 diskop.numEntries++;
627 }
628
629 // read remaining files
630 while (lastFindFileFlag != LFF_DONE)
631 {
632 lastFindFileFlag = findNext(&tmpBuffer);
633 if (lastFindFileFlag != LFF_DONE && lastFindFileFlag != LFF_SKIP)
634 {
635 fileEntry_t *newPtr = (fileEntry_t *)realloc(diskOpEntry, sizeof (fileEntry_t) * (diskop.numEntries + 1));
636 if (newPtr == NULL)
637 {
638 findClose();
639 freeDiskOpEntryMem();
640 statusOutOfMemory();
641 return false;
642 }
643 diskOpEntry = newPtr;
644
645 memcpy(&diskOpEntry[diskop.numEntries], &tmpBuffer, sizeof (fileEntry_t));
646 diskop.numEntries++;
647 }
648 }
649
650 findClose();
651
652 if (diskop.numEntries > 0)
653 {
654 sortEntries();
655 }
656 else
657 {
658 // access denied or out of memory - create parent directory link
659 diskOpEntry = bufferCreateEmptyDir();
660 if (diskOpEntry != NULL)
661 diskop.numEntries = 1;
662 else
663 statusOutOfMemory();
664 }
665
666 return true;
667 }
668
669 static int32_t diskOpFillThreadFunc(void *ptr)
670 {
671 (void)ptr;
672
673 diskop.isFilling = true;
674 diskOpFillBuffer();
675 diskop.isFilling = false;
676
677 ui.updateDiskOpFileList = true;
678 return true;
679 }
680
681 static void printFileSize(fileEntry_t *entry, uint16_t x, uint16_t y)
682 {
683 char tmpStr[7];
684
685 if (entry->filesize == -1) // -1 means that the original filesize is above 2GB in our directory reader
686 {
687 textOut(x, y, " >2GB", video.palette[PAL_QADSCP]);
688 return;
689 }
690
691 uint32_t fileSize = (uint32_t)entry->filesize;
692 if (fileSize <= 999999)
693 {
694 // bytes
695 snprintf(tmpStr, 7, "%06d", fileSize);
696 }
697 else if (fileSize <= 9999999)
698 {
699 // kilobytes
700 fileSize /= 1000;
701 snprintf(tmpStr, 7, "%04dKB", fileSize);
702 }
703 else
704 {
705 // megabytes
706 fileSize /= 1000000;
707 snprintf(tmpStr, 7, "%04dMB", fileSize);
708 }
709
710 // turn zeroes on the left side into spaces
711 for (int32_t i = 0; i < 7; i++)
712 {
713 if (tmpStr[i] != '0')
714 break;
715
716 tmpStr[i] = ' ';
717 }
718
719 textOut(x, y, tmpStr, video.palette[PAL_QADSCP]);
720 }
721
722 static void printEntryName(char *entryName, int32_t entryLength, int32_t maxLength, uint16_t x, uint16_t y)
723 {
724 if (entryLength > maxLength)
725 {
726 // shorten name and add ".." to end
727 for (int32_t i = 0; i < maxLength-2; i++)
728 charOut(x + (i * FONT_CHAR_W), y, entryName[i], video.palette[PAL_QADSCP]);
729
730 textOut(x + ((maxLength - 2) * FONT_CHAR_W), y, "..", video.palette[PAL_QADSCP]);
731 }
732 else
733 {
734 // print whole name
735 textOut(x, y, entryName, video.palette[PAL_QADSCP]);
736 }
737 }
738
739 void diskOpRenderFileList(void)
740 {
741 uint8_t maxFilenameChars, maxDirNameChars;
742 uint16_t textXStart;
743
744 if (config.hideDiskOpDates)
745 {
746 textXStart = 8;
747 maxFilenameChars = 30;
748 maxDirNameChars = 31;
749 }
750 else
751 {
752 textXStart = 64;
753 maxFilenameChars = 23;
754 maxDirNameChars = 24;
755 }
756
757 diskOpShowSelectText();
758
759 if (diskop.forceStopReading)
760 return;
761
762 // if needed, update the file list and add entries
763 if (!diskop.cached)
764 {
765 diskop.fillThread = SDL_CreateThread(diskOpFillThreadFunc, "file lister thread", NULL);
766 if (diskop.fillThread == NULL)
767 return;
768
769 SDL_DetachThread(diskop.fillThread);
770 diskop.cached = true;
771
772 return;
773 }
774
775 // clear list
776 fillRect(8, 35, 295, 59, video.palette[PAL_BACKGRD]);
777
778 if (diskop.isFilling || diskOpEntry == NULL)
779 return;
780
781 // list entries
782 for (int32_t i = 0; i < DISKOP_LINES; i++)
783 {
784 if (diskop.scrollOffset+i >= diskop.numEntries)
785 break;
786
787 fileEntry_t *entry = &diskOpEntry[diskop.scrollOffset+i];
788 char *entryName = diskOpGetAnsiEntry(i);
789 int32_t entryLength = (int32_t)strlen(entryName);
790
791 uint16_t x = textXStart;
792 uint16_t y = (uint8_t)(35 + (i * (FONT_CHAR_H + 1)));
793
794 if (!entry->isDir)
795 {
796 printEntryName(entryName, entryLength, maxFilenameChars, x, y);
797
798 // print modification date
799 if (!config.hideDiskOpDates)
800 textOut(8, y, entry->dateChanged, video.palette[PAL_QADSCP]);
801
802 // print file size
803 printFileSize(entry, 256, y);
804 }
805 else
806 {
807 printEntryName(entryName, entryLength, maxDirNameChars, x, y);
808 textOut(264, y, "(DIR)", video.palette[PAL_QADSCP]);
809 }
810 }
811 }
812
813 void diskOpLoadFile(uint32_t fileEntryRow, bool songModifiedCheck)
814 {
815 // if we clicked on an empty space, return...
816 if (diskOpEntryIsEmpty(fileEntryRow))
817 return;
818
819 if (diskOpEntryIsDir(fileEntryRow))
820 {
821 diskOpSetPath(diskOpGetUnicodeEntry(fileEntryRow), DISKOP_CACHE);
822 }
823 else
824 {
825 UNICHAR *filePath = diskOpGetUnicodeEntry(fileEntryRow);
826 if (filePath != NULL)
827 {
828 if (diskop.mode == DISKOP_MODE_MOD)
829 {
830 if (songModifiedCheck && song->modified)
831 {
832 if (!askBox(ASKBOX_YES_NO, "SONG IS UNSAVED !"))
833 return;
834 }
835
836 module_t *newSong = modLoad(filePath);
837 if (newSong != NULL)
838 {
839 uint8_t oldMode = editor.currMode;
840 uint8_t oldPlayMode = editor.playMode;
841
842 modStop();
843 modFree();
844
845 song = newSong;
846 setupLoadedMod();
847 song->loaded = true;
848
849 statusAllRight();
850
851 if (config.autoCloseDiskOp)
852 ui.diskOpScreenShown = false;
853
854 if (config.rememberPlayMode)
855 {
856 if (oldMode == MODE_PLAY || oldMode == MODE_RECORD)
857 {
858 editor.playMode = oldPlayMode;
859
860 if (oldPlayMode == PLAY_MODE_PATTERN || oldMode == MODE_RECORD)
861 modPlay(0, 0, 0);
862 else
863 modPlay(DONT_SET_PATTERN, 0, 0);
864
865 if (oldMode == MODE_RECORD)
866 pointerSetMode(POINTER_MODE_RECORD, DO_CARRY);
867 else
868 pointerSetMode(POINTER_MODE_PLAY, DO_CARRY);
869
870 editor.currMode = oldMode;
871 }
872 }
873 else
874 {
875 editor.currMode = MODE_IDLE;
876 editor.playMode = PLAY_MODE_NORMAL;
877 editor.songPlaying = false;
878
879 pointerSetMode(POINTER_MODE_IDLE, DO_CARRY);
880 }
881
882 displayMainScreen();
883 }
884 else
885 {
886 editor.errorMsgActive = true;
887 editor.errorMsgBlock = true;
888 editor.errorMsgCounter = 0;
889
890 // status/error message is set in the mod loader
891 setErrPointer();
892 }
893 }
894 else if (diskop.mode == DISKOP_MODE_SMP)
895 {
896 loadSample(filePath, diskOpGetAnsiEntry(fileEntryRow));
897 }
898 }
899 }
900 }
901
902 void renderDiskOpScreen(void)
903 {
904 blit32(0, 0, 320, 99, diskOpScreenBMP);
905
906 ui.updateDiskOpPathText = true;
907 ui.updatePackText = true;
908 ui.updateSaveFormatText = true;
909 ui.updateLoadMode = true;
910 ui.updateDiskOpFileList = true;
911 }
912
913 void updateDiskOp(void)
914 {
915 if (!ui.diskOpScreenShown || ui.posEdScreenShown || ui.askBoxShown)
916 return;
917
918 if (ui.updateDiskOpFileList)
919 {
920 ui.updateDiskOpFileList = false;
921 diskOpRenderFileList();
922 }
923
924 if (ui.updateLoadMode)
925 {
926 ui.updateLoadMode = false;
927
928 // clear boxes
929 fillRect(147, 3, FONT_CHAR_W, FONT_CHAR_H, video.palette[PAL_GENBKG]);
930 fillRect(147, 14, FONT_CHAR_W, FONT_CHAR_H, video.palette[PAL_GENBKG]);
931
932 // draw load mode arrow
933 if (diskop.mode == 0)
934 charOut(147, 3, ARROW_RIGHT, video.palette[PAL_GENTXT]);
935 else
936 charOut(147,14, ARROW_RIGHT, video.palette[PAL_GENTXT]);
937 }
938
939 if (ui.updatePackText)
940 {
941 ui.updatePackText = false;
942 textOutBg(120, 3, diskop.modPackFlg ? "ON " : "OFF", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
943 }
944
945 if (ui.updateSaveFormatText)
946 {
947 ui.updateSaveFormatText = false;
948 if (diskop.smpSaveType == DISKOP_SMP_WAV) textOutBg(120, 14, "WAV", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
949 else if (diskop.smpSaveType == DISKOP_SMP_IFF) textOutBg(120, 14, "IFF", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
950 else if (diskop.smpSaveType == DISKOP_SMP_RAW) textOutBg(120, 14, "RAW", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
951 }
952
953 if (ui.updateDiskOpPathText)
954 {
955 ui.updateDiskOpPathText = false;
956
957 // print disk op. path
958 for (int32_t i = 0; i < 26; i++)
959 {
960 char ch;
961
962 if (ui.editTextFlag)
963 ch = editor.currPath[textEdit.scrollOffset + i];
964 else
965 ch = editor.currPath[i];
966
967 if (ch == '\0')
968 ch = '_';
969
970 charOutBg(24 + (i * FONT_CHAR_W), 25, ch, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]);
971 }
972 }
973 }