-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathOSDFile.cpp
1106 lines (890 loc) · 45.8 KB
/
OSDFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ESPectrum, a Sinclair ZX Spectrum emulator for Espressif ESP32 SoC
Copyright (c) 2023, 2024 Víctor Iborra [Eremus] and 2023 David Crespo [dcrespo3d]
https://github.com/EremusOne/ZX-ESPectrum-IDF
Based on ZX-ESPectrum-Wiimote
Copyright (c) 2020, 2022 David Crespo [dcrespo3d]
https://github.com/dcrespo3d/ZX-ESPectrum-Wiimote
Based on previous work by Ramón Martinez and Jorge Fuertes
https://github.com/rampa069/ZX-ESPectrum
Original project by Pete Todd
https://github.com/retrogubbins/paseVGA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
To Contact the dev team you can write to zxespectrum@gmail.com or
visit https://zxespectrum.speccy.org/contacto
*/
#include <string>
#include <algorithm>
#include <sys/stat.h>
#include "errno.h"
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
using namespace std;
#include "OSDMain.h"
#include "FileUtils.h"
#include "Config.h"
#include "ESPectrum.h"
#include "cpuESP.h"
#include "Video.h"
#include "messages.h"
#include <math.h>
#include "ZXKeyb.h"
#include "pwm_audio.h"
#include "Z80_JLS/z80.h"
#include "Tape.h"
FILE *dirfile;
unsigned int OSD::elements;
unsigned int OSD::fdSearchElements;
unsigned int OSD::ndirs;
int8_t OSD::fdScrollPos;
int OSD::timeStartScroll;
int OSD::timeScroll;
uint8_t OSD::fdCursorFlash;
bool OSD::fdSearchRefresh;
void OSD::restoreBackbufferData(bool force) {
if ( !SaveRectpos ) return;
if (menu_saverect || force) {
// printf("--- OSD::restoreBackbufferData %d\n", SaveRectpos);
// Restaurar datos del backbuffer utilizando RLE o bloques sin comprimir
uint32_t j = VIDEO::SaveRect[SaveRectpos - 1]; // Obtener la dirección de inicio del bloque
SaveRectpos = j;
// printf("OSD::restoreBackbufferData j=%d\n", j);
uint16_t x = VIDEO::SaveRect[j] >> 16;
uint16_t y = VIDEO::SaveRect[j++] & 0xffff;
uint16_t w = VIDEO::SaveRect[j] >> 16;
uint16_t h = VIDEO::SaveRect[j++] & 0xffff;
// printf("OSD::restoreBackbufferData x=%hd y=%hd w=%hd h=%hd\n", x, y, w, h);
uint32_t *backbuffer32 = nullptr;
for (uint32_t m = y; m < y + h; m++) {
backbuffer32 = (uint32_t *)(VIDEO::vga.frameBuffer[m]);
for (uint32_t x_off = x >> 2; x_off < ((x + w) >> 2) + 1;) {
uint32_t run_length = VIDEO::SaveRect[j++];
if (run_length & 0x80000000) { // Bloque comprimido
run_length &= 0x7FFFFFFF; // Limpiar el bit más alto
uint32_t value = VIDEO::SaveRect[j++];
for (int k = 0; k < run_length; k++) {
backbuffer32[x_off++] = value;
}
} else { // Bloque sin comprimir
for (int k = 0; k < run_length; k++) {
backbuffer32[x_off++] = VIDEO::SaveRect[j++];
}
}
}
}
// printf("OSD::restoreBackbufferData exit %d\n", SaveRectpos);
// if ( !force )
menu_saverect = false;
}
}
void OSD::saveBackbufferData(uint16_t x, uint16_t y, uint16_t w, uint16_t h, bool force) {
if ( force || menu_saverect ) {
// printf("OSD::saveBackbufferData x=%hd y=%hd w=%hd h=%hd pos=%d\n", x, y, w, h, SaveRectpos);
// Guardar datos del backbuffer utilizando RLE o bloques sin comprimir
uint32_t start_pos = SaveRectpos;
VIDEO::SaveRect[SaveRectpos++] = ( x << 16 ) | y;
VIDEO::SaveRect[SaveRectpos++] = ( w << 16 ) | h;
for (uint32_t m = y; m < y + h; m++) {
uint32_t *backbuffer32 = (uint32_t *)(VIDEO::vga.frameBuffer[m]);
uint32_t n_start = x >> 2;
uint32_t current_value = backbuffer32[n_start];
bool raw_mode = true;
uint32_t count_pos = SaveRectpos;
VIDEO::SaveRect[SaveRectpos++] = 1; // Contador a 1
VIDEO::SaveRect[SaveRectpos++] = current_value;
for (uint32_t n = n_start + 1; n < ((x + w) >> 2) + 1; n++) {
if (backbuffer32[n] == current_value) {
if ( raw_mode ) {
if ( VIDEO::SaveRect[count_pos] != 1 ) {
// descarto el ultimo
VIDEO::SaveRect[count_pos]--;
} else {
SaveRectpos--;
}
count_pos = SaveRectpos - 1;
VIDEO::SaveRect[count_pos] = 0x80000001;
VIDEO::SaveRect[SaveRectpos++] = current_value;
raw_mode = false;
}
VIDEO::SaveRect[count_pos]++;
} else {
current_value = backbuffer32[n];
if ( !raw_mode ) {
count_pos = SaveRectpos++;
VIDEO::SaveRect[count_pos] = 0; // count a 0
}
VIDEO::SaveRect[count_pos]++;
VIDEO::SaveRect[SaveRectpos++] = current_value;
raw_mode = true;
}
}
}
// Guardar la dirección de inicio del bloque
VIDEO::SaveRect[SaveRectpos++] = start_pos;
// printf("OSD::saveBackbufferData exit %d sp: %d\n", SaveRectpos, start_pos);
}
}
void OSD::saveBackbufferData(bool force) {
OSD::saveBackbufferData(x, y, w, h, force);
}
// Función para convertir una cadena de dígitos en un número
// se agrega esta funcion porque atoul crashea si no hay digitos en el buffer
unsigned long getLong(char *buffer) {
unsigned long result = 0;
char * p = buffer;
while (p && isdigit(*p)) {
result = result * 10 + (*p - '0');
++p;
}
return result;
}
// Run a new file menu
string OSD::fileDialog(string &fdir, string title, uint8_t ftype, uint8_t mfcols, uint8_t mfrows) {
// struct stat stat_buf;
long dirfilesize;
bool reIndex;
// Columns and Rows
cols = mfcols;
mf_rows = mfrows + (Config::aspect_16_9 ? 0 : 1);
// CRT Overscan compensation
if (Config::videomode == 2) {
x = 18;
if (menu_level == 0) {
if (Config::arch[0] == 'T' && Config::ALUTK == 2) {
y = 4;
} else {
y = 12;
}
}
} else {
x = 0;
if (menu_level == 0) y = 0;
}
// Position
if (menu_level == 0) {
x += (Config::aspect_16_9 ? 24 : 4);
y += (Config::aspect_16_9 ? 4 : 8);
} else {
x += (Config::aspect_16_9 ? 24 : 4) + (48 /*60*/ * menu_level);
y += (Config::aspect_16_9 ? 4 : 8) + (4 /*8*/ * (menu_level - 1));
}
// Size
// w = (cols * OSD_FONT_W) + 2;
// h = ((mf_rows + 1) * OSD_FONT_H) + 2;
// // Check window boundaries
// if ( x + mfcols * OSD_FONT_W > (Config::aspect_16_9 ? 24 : 4) + 52 * OSD_FONT_W ) x = (Config::aspect_16_9 ? 24 : 4) + ( 51 - mfcols ) * OSD_FONT_W;
// if ( y + mfrows > (Config::aspect_16_9 ? 200 : 240) - 2 * OSD_FONT_H ) y = (Config::aspect_16_9 ? 200 : 240) - ( mfrows + 2 ) * OSD_FONT_H;
// Adjust dialog size if needed
w = (cols * OSD_FONT_W) + 2;
printf("X: %d w: %d Cols: %d scrW: %d\n",x,w,cols,scrW);
while ( x + w >= OSD::scrW - OSD_FONT_W) {
cols--;
w = (cols * OSD_FONT_W) + 2;
printf("X: %d w: %d Cols: %d scrW: %d\n",x,w,cols,scrW);
};
h = ((mf_rows + 1) * OSD_FONT_H) + 2;
printf("Y: %d h: %d mf_rows: %d scrH: %d\n",y,h,mf_rows,scrH);
while ( y + h >= OSD::scrH - OSD_FONT_H) {
mf_rows--;
h = ((mf_rows + 1) * OSD_FONT_H) + 2;
printf("Y: %d h: %d mf_rows: %d scrH: %d\n",y,h,mf_rows,scrH);
};
// Adjust begin_row & focus in case of values doesn't fit in current dialog size
// printf("Focus: %d, Begin_row: %d, mf_rows: %d\n",(int) FileUtils::fileTypes[ftype].focus,(int) FileUtils::fileTypes[ftype].begin_row,(int) mf_rows);
if (FileUtils::fileTypes[ftype].focus > mf_rows - 1) {
FileUtils::fileTypes[ftype].begin_row += FileUtils::fileTypes[ftype].focus - (mf_rows - 1);
FileUtils::fileTypes[ftype].focus = mf_rows - 1;
} else
if (FileUtils::fileTypes[ftype].focus + (FileUtils::fileTypes[ftype].begin_row - 2) < mf_rows) {
FileUtils::fileTypes[ftype].focus += FileUtils::fileTypes[ftype].begin_row - 2;
FileUtils::fileTypes[ftype].begin_row = 2;
}
// menu = title + "\n" + fdir + "\n";
menu = title + "\n" + ( fdir.length() == 1 ? fdir : fdir.substr(0,fdir.length()-1)) + "\n";
WindowDraw(); // Draw menu outline
fd_PrintRow(1, IS_INFO); // Path
reset:
// Draw blank rows
uint8_t row = 2;
for (; row < mf_rows; row++) {
VIDEO::vga.setTextColor(zxColor(0, 1), zxColor(7, 1));
menuAt(row, 0);
VIDEO::vga.print(std::string(cols, ' ').c_str());
}
// Draw shortcut help
string StatusBar = " ";
if ( ftype == DISK_TAPFILE ) // Dirty hack
StatusBar += Config::lang ? "F2 Nuevo | " : "F2 New | ";
StatusBar += Config::lang ? "F3 Buscar | F8 Borrar" : "F3 Find | F8 Delete";
if (cols > (StatusBar.length() + 11 + 2)) { // 11 from elements counter + 2 from borders
StatusBar += std::string(cols - StatusBar.length() - 12, ' ');
} else {
StatusBar = std::string(cols - 12, ' ');
}
// Print status bar
menuAt(row, 0);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
if (FileUtils::fileTypes[ftype].fdMode)
VIDEO::vga.print(std::string(cols, ' ').c_str());
else {
VIDEO::vga.print(StatusBar.c_str());
VIDEO::vga.print(std::string(12, ' ').c_str());
}
// fdSearchRefresh = true;
while(1) {
ESPectrum::showMemInfo("file dialog: before checking dir");
fdCursorFlash = 0;
reIndex = false;
string filedir = FileUtils::MountPoint + fdir;
std::vector<std::string> filexts;
size_t pos = 0;
string ss = FileUtils::fileTypes[ftype].fileExts;
while ((pos = ss.find(",")) != std::string::npos) {
filexts.push_back(ss.substr(0, pos));
ss.erase(0, pos + 1);
}
filexts.push_back(ss.substr(0));
unsigned long hash = 0; // Name checksum variables
// Get Dir Stats
int result = FileUtils::getDirStats(filedir, filexts, &hash, &elements, &ndirs);
filexts.clear(); // Clear vector
std::vector<std::string>().swap(filexts); // free memory
if ( result == -1 ) {
printf("Error opening %s\n",filedir.c_str());
FileUtils::unmountSDCard();
OSD::restoreBackbufferData();
click();
return "";
}
// Open dir file for read
printf("Checking existence of index file %s\n",(filedir + FileUtils::fileTypes[ftype].indexFilename).c_str());
dirfile = fopen((filedir + FileUtils::fileTypes[ftype].indexFilename).c_str(), "r");
if (dirfile == NULL) {
printf("No dir file found: reindexing\n");
reIndex = true;
} else {
// stat((filedir + FileUtils::fileTypes[ftype].indexFilename).c_str(), &stat_buf);
fseek(dirfile,0,SEEK_END);
dirfilesize = ftell(dirfile);
fseek(dirfile, dirfilesize - 20, SEEK_SET);
char fhash[21];
memset( fhash, '\0', sizeof(fhash));
fgets(fhash, sizeof(fhash), dirfile);
// printf("File Hash: %s\n",fhash);
// If calc hash and file hash are different refresh dir index
if ( getLong(fhash) != hash ||
dirfilesize - 20 != FILENAMELEN * ( ndirs+elements + ( filedir != ( FileUtils::MountPoint + "/" ) ? 1 : 0 ) ) ) {
reIndex = true;
}
}
ESPectrum::showMemInfo("file dialog: after checking dir");
// Force reindex (for testing)
// reIndex = ESPectrum::ESPtestvar ? true : reIndex;
// There was no index or hashes are different: reIndex
if (reIndex) {
ESPectrum::showMemInfo("file dialog: before reindex");
if ( dirfile ) {
fclose(dirfile);
dirfile = nullptr;
}
#if 1
multi_heap_info_t info;
size_t ram_consumption;
heap_caps_get_info(&info, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); // internal RAM, memory capable to store data or to create new task
printf("\n=======================================================\n");
printf("ORDENANDO CARPETA\n");
printf("=======================================================\n");
printf("\nTotal free bytes : %d\n", info.total_free_bytes);
printf("Minimum free ever : %d\n", info.minimum_free_bytes);
size_t minimum_before = info.minimum_free_bytes;
uint32_t time_start = esp_timer_get_time();
#endif
FileUtils::DirToFile(filedir, ftype, hash, ndirs + elements ); // Prepare filelist
#if 1
uint32_t time_elapsed = esp_timer_get_time() - time_start;
heap_caps_get_info(&info, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); // internal RAM, memory capable to store data or to create new task
printf("TIEMPO DE ORDENACION : %6.2f segundos\n", (float)time_elapsed / 1000000);
printf("Total free bytes despues : %d\n", info.total_free_bytes);
printf("Minimum free ever despues : %d\n", info.minimum_free_bytes);
printf("Consumo RAM : %d\n", minimum_before - info.minimum_free_bytes);
printf("\n=======================================================\n");
#endif
// stat((filedir + FileUtils::fileTypes[ftype].indexFilename).c_str(), &stat_buf);
dirfile = fopen((filedir + FileUtils::fileTypes[ftype].indexFilename).c_str(), "r");
if (dirfile == NULL) {
printf("Error opening index file\n");
FileUtils::unmountSDCard();
OSD::restoreBackbufferData();
click();
return "";
}
fseek(dirfile,0,SEEK_END);
dirfilesize = ftell(dirfile);
// Reset position
FileUtils::fileTypes[ftype].begin_row = FileUtils::fileTypes[ftype].focus = 2;
ESPectrum::showMemInfo("file dialog: after reindex");
}
if (FileUtils::fileTypes[ftype].fdMode) {
// Clean Status Bar
menuAt(row, 0);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
VIDEO::vga.print(std::string(StatusBar.length(), ' ').c_str());
// Recalc items number
long prevpos = ftell(dirfile);
unsigned int foundcount = 0;
fdSearchElements = 0;
rewind(dirfile);
char buf[FILENAMELEN+1];
string search = FileUtils::fileTypes[ftype].fileSearch;
std::transform(search.begin(), search.end(), search.begin(), ::toupper);
while(1) {
fgets(buf, sizeof(buf), dirfile);
if (feof(dirfile)) break;
if (buf[0] == ASCII_SPC) {
foundcount++;
// printf("%s",buf);
}else {
char *p = buf; while(*p) *p++ = toupper(*p);
char *pch = strstr(buf, search.c_str());
if (pch != NULL) {
foundcount++;
fdSearchElements++;
// printf("%s",buf);
}
}
}
if (foundcount) {
// Redraw rows
real_rows = foundcount + 2; // Add 2 for title and status bar
virtual_rows = (real_rows > mf_rows ? mf_rows : real_rows);
last_begin_row = last_focus = 0;
// FileUtils::fileTypes[ftype].focus = 2;
// FileUtils::fileTypes[ftype].begin_row = 2;
// fd_Redraw(title, fdir, ftype);
} else {
fseek(dirfile,prevpos,SEEK_SET);
}
fdSearchRefresh = false;
} else {
// real_rows = (stat_buf.st_size / FILENAMELEN) + 2; // Add 2 for title and status bar
real_rows = (dirfilesize / FILENAMELEN) + 2; // Add 2 for title and status bar
virtual_rows = (real_rows > mf_rows ? mf_rows : real_rows);
// printf("Real rows: %d; st_size: %d; Virtual rows: %d\n",real_rows,stat_buf.st_size,virtual_rows);
last_begin_row = last_focus = 0;
fdSearchElements = elements;
}
// printf("Focus: %d, Begin_row: %d, real_rows: %d, mf_rows: %d\n",(int) FileUtils::fileTypes[ftype].focus,(int) FileUtils::fileTypes[ftype].begin_row,(int) real_rows, (int) mf_rows);
if ((real_rows > mf_rows) && ((FileUtils::fileTypes[ftype].begin_row + mf_rows - 2) > real_rows)) {
FileUtils::fileTypes[ftype].focus += (FileUtils::fileTypes[ftype].begin_row + mf_rows - 2) - real_rows;
FileUtils::fileTypes[ftype].begin_row = real_rows - (mf_rows - 2);
// printf("Focus: %d, BeginRow: %d\n",FileUtils::fileTypes[ftype].focus,FileUtils::fileTypes[ftype].begin_row);
}
fd_Redraw(title, fdir, ftype); // Draw content
// Focus line scroll position
fdScrollPos = 0;
timeStartScroll = 0;
timeScroll = 0;
fabgl::VirtualKeyItem Menukey;
while (1) {
if (ZXKeyb::Exists) ZXKeyb::ZXKbdRead();
ESPectrum::readKbdJoy();
// Process external keyboard
if (ESPectrum::PS2Controller.keyboard()->virtualKeyAvailable()) {
timeStartScroll = 0;
timeScroll = 0;
fdScrollPos = 0;
// Print elements
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
unsigned int elem = FileUtils::fileTypes[ftype].fdMode ? fdSearchElements : elements;
if (elem) {
// menuAt(mf_rows, cols - (real_rows > virtual_rows ? 13 : 12));
menuAt(mf_rows, cols - 12);
char elements_txt[13];
int nitem = (FileUtils::fileTypes[ftype].begin_row + FileUtils::fileTypes[ftype].focus ) - (4 + ndirs) + (fdir.length() == 1);
snprintf(elements_txt, sizeof(elements_txt), "%d/%d ", nitem > 0 ? nitem : 0 , elem);
VIDEO::vga.print(std::string(12 - strlen(elements_txt), ' ').c_str());
VIDEO::vga.print(elements_txt);
} else {
menuAt(mf_rows, cols - 12);
VIDEO::vga.print(std::string(12,' ').c_str());
}
if (ESPectrum::readKbd(&Menukey)) {
if (!Menukey.down) continue;
// Search first ocurrence of letter if we're not on that letter yet
if (((Menukey.vk >= fabgl::VK_a) && (Menukey.vk <= fabgl::VK_Z)) || Menukey.vk == fabgl::VK_SPACE || ((Menukey.vk >= fabgl::VK_0) && (Menukey.vk <= fabgl::VK_9))) {
int fsearch;
if (Menukey.vk==fabgl::VK_SPACE && FileUtils::fileTypes[ftype].fdMode)
fsearch = ASCII_SPC;
else if (Menukey.vk<=fabgl::VK_9)
fsearch = Menukey.vk + 46;
else if (Menukey.vk<=fabgl::VK_z)
fsearch = Menukey.vk + 75;
else if (Menukey.vk<=fabgl::VK_Z)
fsearch = Menukey.vk + 17;
if (FileUtils::fileTypes[ftype].fdMode) {
if (FileUtils::fileTypes[ftype].fileSearch.length() < MAXSEARCHLEN) {
FileUtils::fileTypes[ftype].fileSearch += char(fsearch);
fdSearchRefresh = true;
click();
}
} else {
uint8_t letra = rowGet(menu,FileUtils::fileTypes[ftype].focus).at(0);
// printf("%d %d\n",(int)letra,fsearch);
if (toupper(letra) != toupper(fsearch)) {
// Seek first ocurrence of letter/number
long prevpos = ftell(dirfile);
char buf[FILENAMELEN+1];
int cnt = 0;
fseek(dirfile,0,SEEK_SET);
while(!feof(dirfile)) {
fgets(buf, sizeof(buf), dirfile);
// printf("%c %d\n",buf[0],int(buf[0]));
if (toupper(buf[0]) == toupper(char(fsearch))) break;
cnt++;
}
// printf("Cnt: %d Letra: %d\n",cnt,int(letra));
if (!feof(dirfile)) {
last_begin_row = FileUtils::fileTypes[ftype].begin_row;
last_focus = FileUtils::fileTypes[ftype].focus;
if (real_rows > virtual_rows) {
int m = cnt + virtual_rows - real_rows;
if (m > 0) {
FileUtils::fileTypes[ftype].focus = m + 2;
FileUtils::fileTypes[ftype].begin_row = cnt - m + 2;
} else {
FileUtils::fileTypes[ftype].focus = 2;
FileUtils::fileTypes[ftype].begin_row = cnt + 2;
}
} else {
FileUtils::fileTypes[ftype].focus = cnt + 2;
FileUtils::fileTypes[ftype].begin_row = 2;
}
// printf("Real rows: %d; Virtual rows: %d\n",real_rows,virtual_rows);
// printf("Focus: %d, Begin_row: %d\n",(int) FileUtils::fileTypes[ftype].focus,(int) FileUtils::fileTypes[ftype].begin_row);
fd_Redraw(title,fdir,ftype);
click();
} else
fseek(dirfile,prevpos,SEEK_SET);
}
}
} else if (Menukey.vk == fabgl::VK_F2 && ftype == DISK_TAPFILE) { // Dirty hack
// Clean status bar
menuAt(mf_rows, 0);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
VIDEO::vga.print(std::string(StatusBar.length(), ' ').c_str());
string new_tap = OSD::input( 1, mf_rows, Config::lang ? "Nomb: " : "Name: ", "", cols - 19 , 32, zxColor(7,1), zxColor(5,0), true );
if ( new_tap != "" ) {
fclose(dirfile);
dirfile = NULL;
FileUtils::fileTypes[ftype].begin_row = FileUtils::fileTypes[ftype].focus = 2;
return "N" + new_tap + ".tap";
} else {
// Restore status bar
menuAt(mf_rows, 0);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
VIDEO::vga.print(StatusBar.c_str());
// menuAt(mf_rows, 1);
// VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
// VIDEO::vga.print(" " " ");
}
// fd_Redraw(title, fdir, ftype);
} else if (Menukey.vk == fabgl::VK_F3) {
FileUtils::fileTypes[ftype].fdMode ^= 1;
if (FileUtils::fileTypes[ftype].fdMode) {
// Clean status bar
menuAt(mf_rows, 0);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
// VIDEO::vga.print( ftype == DISK_TAPFILE ? " " " " : " " );
// VIDEO::vga.print(std::string(cols, ' ').c_str());
VIDEO::vga.print(std::string(StatusBar.length(), ' ').c_str());
fdCursorFlash = 63;
// menuAt(mfrows + (Config::aspect_letterbox ? 0 : 1), 1);
// VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
// VIDEO::vga.print(Config::lang ? "B\xA3sq: " : "Find: ");
// VIDEO::vga.print(FileUtils::fileTypes[ftype].fileSearch.c_str());
// VIDEO::vga.setTextColor(zxColor(5, 0), zxColor(7, 1));
// VIDEO::vga.print("K");
// VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
// VIDEO::vga.print(std::string(16 - FileUtils::fileTypes[ftype].fileSearch.size(), ' ').c_str());
fdSearchRefresh = FileUtils::fileTypes[ftype].fileSearch != "";
} else {
// Restore status bar
menuAt(mf_rows, 0);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
// VIDEO::vga.print( ftype == DISK_TAPFILE ? " " " " : " " );
VIDEO::vga.print(StatusBar.c_str());
if (FileUtils::fileTypes[ftype].fileSearch != "") {
// FileUtils::fileTypes[ftype].fileSearch="";
real_rows = (dirfilesize / FILENAMELEN) + 2; // Add 2 for title and status bar
virtual_rows = (real_rows > mf_rows ? mf_rows : real_rows);
last_begin_row = last_focus = 0;
FileUtils::fileTypes[ftype].focus = 2;
FileUtils::fileTypes[ftype].begin_row = 2;
fd_Redraw(title, fdir, ftype);
}
}
click();
} else if (Menukey.vk == fabgl::VK_F8) {
click();
filedir = rowGet(menu,FileUtils::fileTypes[ftype].focus);
// printf("%s\n",filedir.c_str());
if (filedir[0] != ASCII_SPC) {
rtrim(filedir);
if ( !access(( FileUtils::MountPoint + fdir + filedir ).c_str(), W_OK) ) {
string title = MENU_DELETE_CURRENT_FILE[Config::lang];
string msg = OSD_DLG_SURE[Config::lang];
uint8_t res = msgDialog(title,msg);
menu_saverect = true;
if (res == DLG_YES) {
printf("File selected: -->%s<--\n", (FileUtils::MountPoint + fdir + filedir).c_str());
printf("File inserted: -->%s<--\n", (Tape::tapeFilePath + Tape::tapeFileName).c_str());
// if ( FileUtils::getResolvedPath( FileUtils::MountPoint + fdir + filedir ) == FileUtils::getResolvedPath( FileUtils::MountPoint + Tape::tapeFilePath + Tape::tapeFileName ) ) Tape::tapeEject();
if ( (FileUtils::MountPoint + fdir + filedir) == (Tape::tapeFilePath + Tape::tapeFileName) ) {
printf("Ejecting tape before deleting it\n");
Tape::Eject();
};
remove(( FileUtils::MountPoint + fdir + filedir ).c_str());
fd_Redraw(title, fdir, ftype);
menu_saverect = true;
goto reset;
}
} else {
OSD::osdCenteredMsg(OSD_READONLY_FILE_WARN[Config::lang], LEVEL_WARN);
}
click();
}
} else if (Menukey.vk == fabgl::VK_UP || Menukey.vk == fabgl::VK_JOY1UP || Menukey.vk == fabgl::VK_JOY2UP) {
if (FileUtils::fileTypes[ftype].focus == 2 && FileUtils::fileTypes[ftype].begin_row > 2) {
last_begin_row = FileUtils::fileTypes[ftype].begin_row;
FileUtils::fileTypes[ftype].begin_row--;
fd_Redraw(title, fdir, ftype);
} else if (FileUtils::fileTypes[ftype].focus > 2) {
last_focus = FileUtils::fileTypes[ftype].focus;
fd_PrintRow(FileUtils::fileTypes[ftype].focus--, IS_NORMAL);
fd_PrintRow(FileUtils::fileTypes[ftype].focus, IS_FOCUSED);
// printf("Focus: %d, Lastfocus: %d\n",FileUtils::fileTypes[ftype].focus,(int) last_focus);
}
click();
} else if (Menukey.vk == fabgl::VK_DOWN || Menukey.vk == fabgl::VK_JOY1DOWN || Menukey.vk == fabgl::VK_JOY2DOWN) {
if (FileUtils::fileTypes[ftype].focus == virtual_rows - 1 && FileUtils::fileTypes[ftype].begin_row + virtual_rows - 2 < real_rows) {
last_begin_row = FileUtils::fileTypes[ftype].begin_row;
FileUtils::fileTypes[ftype].begin_row++;
fd_Redraw(title, fdir, ftype);
} else if (FileUtils::fileTypes[ftype].focus < virtual_rows - 1) {
last_focus = FileUtils::fileTypes[ftype].focus;
fd_PrintRow(FileUtils::fileTypes[ftype].focus++, IS_NORMAL);
fd_PrintRow(FileUtils::fileTypes[ftype].focus, IS_FOCUSED);
// printf("Focus: %d, Lastfocus: %d\n",FileUtils::fileTypes[ftype].focus,(int) last_focus);
}
click();
} else if (Menukey.vk == fabgl::VK_PAGEUP || Menukey.vk == fabgl::VK_LEFT || Menukey.vk == fabgl::VK_JOY1LEFT || Menukey.vk == fabgl::VK_JOY2LEFT) {
if (FileUtils::fileTypes[ftype].begin_row > virtual_rows) {
FileUtils::fileTypes[ftype].focus = 2;
FileUtils::fileTypes[ftype].begin_row -= virtual_rows - 2;
} else {
FileUtils::fileTypes[ftype].focus = 2;
FileUtils::fileTypes[ftype].begin_row = 2;
}
fd_Redraw(title, fdir, ftype);
click();
} else if (Menukey.vk == fabgl::VK_PAGEDOWN || Menukey.vk == fabgl::VK_RIGHT || Menukey.vk == fabgl::VK_JOY1RIGHT || Menukey.vk == fabgl::VK_JOY2RIGHT) {
if (real_rows - FileUtils::fileTypes[ftype].begin_row - virtual_rows > virtual_rows) {
FileUtils::fileTypes[ftype].focus = 2;
FileUtils::fileTypes[ftype].begin_row += virtual_rows - 2;
} else {
FileUtils::fileTypes[ftype].focus = virtual_rows - 1;
FileUtils::fileTypes[ftype].begin_row = real_rows - virtual_rows + 2;
}
fd_Redraw(title, fdir, ftype);
click();
} else if (Menukey.vk == fabgl::VK_HOME) {
last_focus = FileUtils::fileTypes[ftype].focus;
last_begin_row = FileUtils::fileTypes[ftype].begin_row;
FileUtils::fileTypes[ftype].focus = 2;
FileUtils::fileTypes[ftype].begin_row = 2;
fd_Redraw(title, fdir, ftype);
click();
} else if (Menukey.vk == fabgl::VK_END) {
last_focus = FileUtils::fileTypes[ftype].focus;
last_begin_row = FileUtils::fileTypes[ftype].begin_row;
FileUtils::fileTypes[ftype].focus = virtual_rows - 1;
FileUtils::fileTypes[ftype].begin_row = real_rows - virtual_rows + 2;
// printf("Focus: %d, Lastfocus: %d\n",FileUtils::fileTypes[ftype].focus,(int) last_focus);
fd_Redraw(title, fdir, ftype);
click();
} else if (Menukey.vk == fabgl::VK_BACKSPACE) {
if (FileUtils::fileTypes[ftype].fdMode) {
if (FileUtils::fileTypes[ftype].fileSearch.length()) {
FileUtils::fileTypes[ftype].fileSearch.pop_back();
fdSearchRefresh = true;
click();
}
} else {
if (fdir != "/") {
fclose(dirfile);
dirfile = NULL;
fdir.pop_back();
fdir = fdir.substr(0,fdir.find_last_of("/") + 1);
FileUtils::fileTypes[ftype].begin_row = FileUtils::fileTypes[ftype].focus = 2;
// printf("Fdir: %s\n",fdir.c_str());
click();
break;
}
}
} else if (Menukey.vk == fabgl::VK_RETURN /*|| Menukey.vk == fabgl::VK_SPACE*/ || Menukey.vk == fabgl::VK_JOY1B || Menukey.vk == fabgl::VK_JOY2B || Menukey.vk == fabgl::VK_JOY1C || Menukey.vk == fabgl::VK_JOY2C) {
fclose(dirfile);
dirfile = NULL;
filedir = rowGet(menu,FileUtils::fileTypes[ftype].focus);
// printf("%s\n",filedir.c_str());
if (filedir[0] == ASCII_SPC) {
if (filedir[1] == ASCII_SPC) {
fdir.pop_back();
fdir = fdir.substr(0,fdir.find_last_of("/") + 1);
} else {
filedir.erase(0,1);
trim(filedir);
fdir = fdir + filedir + "/";
}
FileUtils::fileTypes[ftype].begin_row = FileUtils::fileTypes[ftype].focus = 2;
// printf("Fdir: %s\n",fdir.c_str());
break;
} else {
OSD::restoreBackbufferData();
rtrim(filedir);
click();
if ((Menukey.CTRL && Menukey.vk == fabgl::VK_RETURN) || Menukey.vk == fabgl::VK_JOY1C || Menukey.vk == fabgl::VK_JOY2C)
return "S" + filedir;
else
return "R" + filedir;
// return (Menukey.vk == fabgl::VK_RETURN || Menukey.vk == fabgl::VK_JOY1B || Menukey.vk == fabgl::VK_JOY2B ? "R" : "S") + filedir;
}
} else if (Menukey.vk == fabgl::VK_ESCAPE || Menukey.vk == fabgl::VK_JOY1A || Menukey.vk == fabgl::VK_JOY2A) {
OSD::restoreBackbufferData();
fclose(dirfile);
dirfile = NULL;
click();
return "";
}
}
} else {
if (timeStartScroll < 200) timeStartScroll++;
}
// Scroll focused line if signaled
if (timeStartScroll == 200) {
timeScroll++;
if (timeScroll == 50) {
fdScrollPos++;
fd_PrintRow(FileUtils::fileTypes[ftype].focus, IS_FOCUSED);
timeScroll = 0;
}
}
if (FileUtils::fileTypes[ftype].fdMode) {
if ((++fdCursorFlash & 0xf) == 0) {
menuAt(mf_rows, 1);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
VIDEO::vga.print(Config::lang ? "B\xA3sq: " : "Find: ");
// VIDEO::vga.print(FileUtils::fileTypes[ftype].fileSearch.c_str());
int ss_siz = FileUtils::fileTypes[ftype].fileSearch.size();
int max_siz = cols - 20;
if (max_siz > MAXSEARCHLEN) max_siz = MAXSEARCHLEN;
if (ss_siz < max_siz)
VIDEO::vga.print(FileUtils::fileTypes[ftype].fileSearch.c_str());
else
VIDEO::vga.print(FileUtils::fileTypes[ftype].fileSearch.substr(ss_siz - max_siz).c_str());
if (fdCursorFlash > 63) {
VIDEO::vga.setTextColor(zxColor(5, 0), zxColor(7, 1));
if (fdCursorFlash == 128) fdCursorFlash = 0;
}
VIDEO::vga.print("L");
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
// VIDEO::vga.print(std::string(MAXSEARCHLEN - FileUtils::fileTypes[ftype].fileSearch.size(), ' ').c_str());
if (ss_siz < max_siz) VIDEO::vga.print(std::string(max_siz - ss_siz, ' ').c_str());
}
if (fdSearchRefresh) {
// Recalc items number
long prevpos = ftell(dirfile);
unsigned int foundcount = 0;
fdSearchElements = 0;
rewind(dirfile);
char buf[FILENAMELEN+1];
string search = FileUtils::fileTypes[ftype].fileSearch;
std::transform(search.begin(), search.end(), search.begin(), ::toupper);
while(1) {
fgets(buf, sizeof(buf), dirfile);
if (feof(dirfile)) break;
if (buf[0] == ASCII_SPC) {
foundcount++;
// printf("%s",buf);
}else {
char *p = buf; while(*p) *p++ = toupper(*p);
char *pch = strstr(buf, search.c_str());
if (pch != NULL) {
foundcount++;
fdSearchElements++;
// printf("%s",buf);
}
}
}
if (foundcount) {
// Redraw rows
real_rows = foundcount + 2; // Add 2 for title and status bar
virtual_rows = (real_rows > mf_rows ? mf_rows : real_rows);
last_begin_row = last_focus = 0;
FileUtils::fileTypes[ftype].focus = 2;
FileUtils::fileTypes[ftype].begin_row = 2;
fd_Redraw(title, fdir, ftype);
} else {
fseek(dirfile,prevpos,SEEK_SET);
}
fdSearchRefresh = false;
}
} /*else {
if (cols > (Config::lang ? 32 : 28) + 14) {
menuAt(mfrows + (Config::aspect_16_9 ? 0 : 1), 1);
VIDEO::vga.setTextColor(zxColor(7, 1), zxColor(5, 0));
if ( ftype == DISK_TAPFILE ) { // Dirty hack
VIDEO::vga.print(Config::lang ? "F2 Nuevo | " : "F2 New | " );
}
VIDEO::vga.print(Config::lang ? "F3 Buscar | F8 Borrar" : "F3 Find | F8 Delete" );
}
}*/
vTaskDelay(5 / portTICK_PERIOD_MS);
}
}
}
// Redraw inside rows
void OSD::fd_Redraw(string title, string fdir, uint8_t ftype) {
if ((FileUtils::fileTypes[ftype].focus != last_focus) || (FileUtils::fileTypes[ftype].begin_row != last_begin_row)) {
// printf("fd_Redraw\n");
// Read bunch of rows
menu = title + "\n" + ( fdir.length() == 1 ? fdir : fdir.substr(0,fdir.length()-1)) + "\n";
char buf[FILENAMELEN+1];
if (FileUtils::fileTypes[ftype].fdMode == 0 || FileUtils::fileTypes[ftype].fileSearch == "") {
fseek(dirfile, (FileUtils::fileTypes[ftype].begin_row - 2) * FILENAMELEN, SEEK_SET);
for (int i = 2; i < virtual_rows; i++) {
fgets(buf, sizeof(buf), dirfile);
if (feof(dirfile)) break;
menu += buf;
}
} else {
rewind(dirfile);
int i = 2;
int count = 2;
string search = FileUtils::fileTypes[ftype].fileSearch;
std::transform(search.begin(), search.end(), search.begin(), ::toupper);
char upperbuf[FILENAMELEN+1];
while (1) {
fgets(buf, sizeof(buf), dirfile);
if (feof(dirfile)) break;
if (buf[0] == ASCII_SPC) {
if (i >= FileUtils::fileTypes[ftype].begin_row) {
menu += buf;
if (++count == virtual_rows) break;
}
i++;
} else {
for(int i=0; buf[i]; i++) upperbuf[i] = toupper(buf[i]);
char *pch = strstr(upperbuf, search.c_str());
if (pch != NULL) {
if (i >= FileUtils::fileTypes[ftype].begin_row) {
menu += buf;
if (++count == virtual_rows) break;
}
i++;
}
}
}
}