-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathElfBox.c
1109 lines (910 loc) · 31.3 KB
/
ElfBox.c
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
/*
* About:
* A simple ELF-application launcher.
*
* Author:
* EXL
*
* License:
* MIT
*
* Application type:
* GUI + ElfLoader
*/
#include <loader.h>
#include <apps.h>
#include <uis.h>
#include <mem.h>
#include <utilities.h>
#include <dl.h>
#include <filesystem.h>
#include <resources.h>
#include "icons/icon_elf_15x15.h"
#include "icons/icon_file_15x15.h"
#include "icons/icon_disk_15x15.h"
#include "icons/icon_folder_15x15.h"
#include "icons/icon_elfbox_48x48.h"
#define TIMER_FAST_TRIGGER_MS (1)
#define MAX_VOLUMES_COUNT (4)
#define MAX_FILENAME_SYMS (64)
typedef enum {
APP_STATE_ANY,
APP_STATE_INIT,
APP_STATE_MAIN,
APP_STATE_POPUP,
APP_STATE_VIEW,
APP_STATE_MAX
} APP_STATE_T;
typedef enum {
APP_TIMER_EXIT = 0xF398,
APP_TIMER_EXIT_FAST,
APP_TIMER_TO_MAIN_VIEW
} APP_TIMER_T;
typedef enum {
APP_RESOURCE_NAME,
APP_RESOURCE_MENU,
APP_RESOURCE_HELP,
APP_RESOURCE_ABOUT,
APP_RESOURCE_EXIT,
APP_RESOURCE_ICON_DISK,
APP_RESOURCE_ICON_FOLDER,
APP_RESOURCE_ICON_FILE,
APP_RESOURCE_ICON_ELF,
APP_RESOURCE_ICON_ELFBOX,
APP_RESOURCE_ACTION_BACK,
APP_RESOURCE_ACTION_RUN,
APP_RESOURCE_ACTION_HELP,
APP_RESOURCE_ACTION_ABOUT,
APP_RESOURCE_ACTION_EXIT,
APP_RESOURCE_LIST_DESCRIPTION,
APP_RESOURCE_MAX
} APP_RESOURCES_T;
typedef enum {
APP_POPUP_WAIT,
APP_POPUP_NOT_ELF
} APP_POPUP_T;
typedef enum {
APP_VIEW_HELP,
APP_VIEW_ABOUT
} APP_VIEW_T;
typedef enum {
APP_FS_VOLUME,
APP_FS_FOLDER,
APP_FS_FOLDER_UP,
APP_FS_FILE,
APP_FS_ELF
} APP_FS_ENTITY_T;
typedef struct {
WCHAR name[MAX_FILENAME_SYMS + 1];
APP_FS_ENTITY_T type;
} APP_FS_OBJECT_T;
typedef struct {
BOOL root;
UINT16 count;
APP_FS_OBJECT_T *list;
} APP_FS_T;
typedef struct {
APPLICATION_T app; /* Must be first. */
RESOURCE_ID resources[APP_RESOURCE_MAX];
APP_POPUP_T popup;
APP_VIEW_T view;
UINT32 menu_current_item_index;
BOOL flag_from_select;
WCHAR current_path[FS_MAX_PATH_NAME_LENGTH + 1];
WCHAR current_title[FS_MAX_PATH_NAME_LENGTH + 1];
APP_FS_T fs;
#if defined(SHOW_ELF_ICONS)
LIST_IMAGE_ELEMENT_T *img_list_elements;
#endif
} APP_INSTANCE_T;
UINT32 Register(const char *elf_path_uri, const char *args, UINT32 ev_code); /* ElfPack 1.x entry point. */
static UINT32 LdrInitEventHandlersTbl(EVENT_HANDLER_ENTRY_T *tbl, UINT32 *base);
static UINT32 LdrFindEventHandlerTbl(EVENT_HANDLER_ENTRY_T *tbl, EVENT_HANDLER_T *hfn);
static UINT32 ApplicationStart(EVENT_STACK_T *ev_st, REG_ID_T reg_id, void *reg_hdl);
static UINT32 ApplicationStop(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 InitResourses(RESOURCE_ID *resources);
static UINT32 FreeResourses(RESOURCE_ID *resources);
static UINT32 HandleStateEnter(EVENT_STACK_T *ev_st, APPLICATION_T *app, ENTER_STATE_TYPE_T state);
static UINT32 HandleStateExit(EVENT_STACK_T *ev_st, APPLICATION_T *app, EXIT_STATE_TYPE_T state);
static UINT32 DeleteDialog(APPLICATION_T *app);
static UINT32 HandleEventTimerExpired(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventSelect(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventBackDir(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventHelp(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventAbout(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventExit(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 HandleEventBack(EVENT_STACK_T *ev_st, APPLICATION_T *app);
#if defined(ASYNC)
static UINT32 HandleEventSearchCompleted(EVENT_STACK_T *ev_st, APPLICATION_T *app);
#endif
static LIST_ENTRY_T *CreateList(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 UpdateList(EVENT_STACK_T *ev_st, APPLICATION_T *app, const WCHAR *directory, const WCHAR *filter);
static UINT32 UpdateVolumeList(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 UpdateFileList(EVENT_STACK_T *ev_st, APPLICATION_T *app, const WCHAR *search_string);
static UINT32 FillFileList(
EVENT_STACK_T *ev_st, APPLICATION_T *app, FS_SEARCH_COMPLETED_INDEX_T *search_index, const WCHAR *search_string
);
static int file_comparate(const void *obj_1, const void *obj_2);
static UINT32 SortFileList(EVENT_STACK_T *ev_st, APPLICATION_T *app);
static UINT32 CreateSearchString(WCHAR *search_string, const WCHAR *search_directory, const WCHAR *search_pattern);
static UINT32 AddUriFileHeader(WCHAR *out_str, const WCHAR *in_str);
static const WCHAR *GetFileNameFromPath(const WCHAR *path);
static BOOL IsDirectory(UINT16 file_attributes);
static BOOL IsElfFile(const WCHAR *path);
static const WCHAR *DirUp(WCHAR *path);
static const WCHAR *DirDown(WCHAR *path, const WCHAR *directory_name);
static UINT32 RunElfApplication(EVENT_STACK_T *ev_st, APPLICATION_T *app, const WCHAR *elf_path);
static const char g_app_name[APP_NAME_LEN] = "ElfBox";
#if defined(FTR_V600) || defined(FTR_V635)
static const UINT32 g_ev_code_base = 0x000003D9; /* Mobile QQ application event. */
#else
static const UINT32 g_ev_code_base = 0x000003DC; /* MotoMixer application event. */
#endif
static const WCHAR *g_start_path = L"/a/Elf";
static const WCHAR *g_str_app_name = L"ElfBox Launcher";
static const WCHAR *g_str_app_menu = L"ElfBox Menu";
static const WCHAR *g_str_menu_help = L"Help...";
static const WCHAR *g_str_menu_about = L"About...";
static const WCHAR *g_str_menu_exit = L"Exit";
static const WCHAR *g_str_view_help = L"Help";
static const WCHAR *g_str_popup_please_wait = L"Please wait!";
static const WCHAR *g_str_popup_sorting = L"Sorting files and folders...";
static const WCHAR *g_str_popup_cannot_run = L"Cannot Run!";
static const WCHAR *g_str_popup_not_elf = L"It is not an ELF file.";
static const WCHAR *g_str_help_content_p1 = L"A simple ELF-applications launcher.";
static const WCHAR *g_str_about_content_p1 = L"Version: 1.0";
static const WCHAR *g_str_about_content_p2 = L"\x00A9 EXL, 02-Jul-2023.";
static const WCHAR *g_str_about_content_p3 = L"https://github.com/EXL/P2kElfs/tree/master/ElfBox";
static const WCHAR *g_str_about_content_p4 = L" "; /* HACK: gap */
static EVENT_HANDLER_ENTRY_T g_state_any_hdls[] = {
{ EV_REVOKE_TOKEN, APP_HandleUITokenRevoked },
{ EV_TIMER_EXPIRED, HandleEventTimerExpired },
{ STATE_HANDLERS_END, NULL }
};
static EVENT_HANDLER_ENTRY_T g_state_init_hdls[] = {
{ EV_GRANT_TOKEN, APP_HandleUITokenGranted },
{ STATE_HANDLERS_END, NULL }
};
static EVENT_HANDLER_ENTRY_T g_state_main_hdls[] = {
{ EV_DONE, HandleEventBackDir },
{ EV_DIALOG_DONE, ApplicationStop },
{ EV_SELECT, HandleEventSelect },
{ STATE_HANDLERS_RESERVED, HandleEventHelp },
{ STATE_HANDLERS_RESERVED, HandleEventAbout },
{ STATE_HANDLERS_RESERVED, HandleEventExit },
{ STATE_HANDLERS_END, NULL }
};
static EVENT_HANDLER_ENTRY_T g_state_popup_hdls[] = {
{ EV_DONE, HandleEventBack },
{ EV_DIALOG_DONE, HandleEventBack },
#if defined(ASYNC)
{ EV_FILE_SEARCH_COMPLETED, HandleEventSearchCompleted },
#endif
{ STATE_HANDLERS_END, NULL }
};
static const STATE_HANDLERS_ENTRY_T g_state_table_hdls[] = {
{ APP_STATE_ANY, NULL, NULL, g_state_any_hdls },
{ APP_STATE_INIT, NULL, NULL, g_state_init_hdls },
{ APP_STATE_MAIN, HandleStateEnter, NULL, g_state_main_hdls },
{ APP_STATE_POPUP, HandleStateEnter, HandleStateExit, g_state_popup_hdls },
{ APP_STATE_VIEW, HandleStateEnter, HandleStateExit, g_state_popup_hdls } /* Same as popups. */
};
UINT32 Register(const char *elf_path_uri, const char *args, UINT32 ev_code) {
UINT32 status;
LOG("Reserve 'ev_code' app event: %d 0x%X.\n", ev_code, ev_code);
LdrInitEventHandlersTbl(g_state_main_hdls, &ev_code);
status = APP_Register(&g_ev_code_base, 1, g_state_table_hdls, APP_STATE_MAX, (void *) ApplicationStart);
return status;
}
static UINT32 LdrInitEventHandlersTbl(EVENT_HANDLER_ENTRY_T *tbl, UINT32 *base) {
UINT32 i = 0;
while (tbl[i].code != STATE_HANDLERS_END) {
if (tbl[i].code == STATE_HANDLERS_RESERVED) {
tbl[i].code = (*base)++;
LOG("Added my own ev_code: %d 0x%X.\n", tbl[i].code, tbl[i].code);
}
i++;
}
return *base;
}
static UINT32 LdrFindEventHandlerTbl(EVENT_HANDLER_ENTRY_T *tbl, EVENT_HANDLER_T *hfn) {
UINT32 i = 0;
while (tbl[i].code != STATE_HANDLERS_END) {
if (tbl[i].hfunc == hfn) {
return tbl[i].code;
}
i++;
}
return 0;
}
static UINT32 ApplicationStart(EVENT_STACK_T *ev_st, REG_ID_T reg_id, void *reg_hdl) {
UINT32 status;
APP_INSTANCE_T *appi;
status = RESULT_FAIL;
if (AFW_InquireRoutingStackByRegId(reg_id) != RESULT_OK) {
appi = (APP_INSTANCE_T *) APP_InitAppData((void *) APP_HandleEvent, sizeof(APP_INSTANCE_T),
reg_id, 0, 1, 1, 1, 1, 0);
InitResourses(appi->resources);
appi->menu_current_item_index = 0;
appi->popup = APP_POPUP_WAIT;
appi->view = APP_VIEW_ABOUT;
appi->flag_from_select = FALSE;
u_strcpy(appi->current_path, g_start_path);
u_strcpy(appi->current_title, g_str_app_name);
appi->fs.root = FALSE;
appi->fs.count = 0;
appi->fs.list = NULL;
status = APP_Start(ev_st, &appi->app, APP_STATE_POPUP,
g_state_table_hdls, ApplicationStop, g_app_name, 0);
}
return status;
}
static UINT32 ApplicationStop(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *appi;
status = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
APP_ConsumeEv(ev_st, app);
DeleteDialog(app);
FreeResourses(appi->resources);
status |= APP_Exit(ev_st, app, 0);
return status;
}
static UINT32 InitResourses(RESOURCE_ID *resources) {
UINT32 status;
RES_ACTION_LIST_ITEM_T action;
UIS_LIST_RESOURCE_CONTENTS_T list_description;
status = RESULT_OK;
status |= DRM_CreateResource(&resources[APP_RESOURCE_NAME], RES_TYPE_STRING,
(void *) g_str_app_name, (u_strlen(g_str_app_name) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_MENU], RES_TYPE_STRING,
(void *) g_str_app_menu, (u_strlen(g_str_app_menu) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_HELP], RES_TYPE_STRING,
(void *) g_str_menu_help, (u_strlen(g_str_menu_help) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_ABOUT], RES_TYPE_STRING,
(void *) g_str_menu_about, (u_strlen(g_str_menu_about) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_EXIT], RES_TYPE_STRING,
(void *) g_str_menu_exit, (u_strlen(g_str_menu_exit) + 1) * sizeof(WCHAR));
status |= DRM_CreateResource(&resources[APP_RESOURCE_ICON_DISK], RES_TYPE_GRAPHICS,
(void *) disk_15x15_gif, sizeof(disk_15x15_gif));
status |= DRM_CreateResource(&resources[APP_RESOURCE_ICON_FOLDER], RES_TYPE_GRAPHICS,
(void *) folder_15x15_gif, sizeof(folder_15x15_gif));
status |= DRM_CreateResource(&resources[APP_RESOURCE_ICON_FILE], RES_TYPE_GRAPHICS,
(void *) file_15x15_gif, sizeof(file_15x15_gif));
status |= DRM_CreateResource(&resources[APP_RESOURCE_ICON_ELF], RES_TYPE_GRAPHICS,
(void *) elf_15x15_gif, sizeof(elf_15x15_gif));
status |= DRM_CreateResource(&resources[APP_RESOURCE_ICON_ELFBOX], RES_TYPE_GRAPHICS,
(void *) elfbox_48x48_gif, sizeof(elfbox_48x48_gif));
action.softkey_label = resources[APP_RESOURCE_HELP];
action.list_label = resources[APP_RESOURCE_HELP];
action.softkey_priority = 0;
action.list_priority = APP_RESOURCE_MAX - APP_RESOURCE_HELP;
status |= DRM_CreateResource(&resources[APP_RESOURCE_ACTION_HELP], RES_TYPE_ACTION, &action, sizeof(action));
action.softkey_label = resources[APP_RESOURCE_ABOUT];
action.list_label = resources[APP_RESOURCE_ABOUT];
action.softkey_priority = 0;
action.list_priority = APP_RESOURCE_MAX - APP_RESOURCE_ABOUT;
status |= DRM_CreateResource(&resources[APP_RESOURCE_ACTION_ABOUT], RES_TYPE_ACTION, &action, sizeof(action));
action.softkey_label = resources[APP_RESOURCE_EXIT];
action.list_label = resources[APP_RESOURCE_EXIT];
action.softkey_priority = 0;
action.list_priority = APP_RESOURCE_MAX - APP_RESOURCE_EXIT;
status |= DRM_CreateResource(&resources[APP_RESOURCE_ACTION_EXIT], RES_TYPE_ACTION, &action, sizeof(action));
memclr(&list_description, sizeof(list_description));
list_description.list_title = resources[APP_RESOURCE_NAME];
list_description.menu_title = resources[APP_RESOURCE_MENU];
status |= DRM_CreateResource(&resources[APP_RESOURCE_LIST_DESCRIPTION], RES_TYPE_LIST_DESCR,
&list_description, sizeof(list_description));
return status;
}
static UINT32 FreeResourses(RESOURCE_ID *resources) {
UINT32 status;
UINT32 i;
status = RESULT_OK;
for (i = 0; i < APP_RESOURCE_MAX; ++i) {
if (resources[i]) {
status |= DRM_ClearResource(resources[i]);
}
}
return status;
}
static UINT32 HandleStateEnter(EVENT_STACK_T *ev_st, APPLICATION_T *app, ENTER_STATE_TYPE_T state) {
APP_INSTANCE_T *appi;
SU_PORT_T port;
CONTENT_T content;
UIS_DIALOG_T dialog;
APP_STATE_T app_state;
UINT8 notice_type;
LIST_ENTRY_T *list;
ACTIONS_T actions;
if (state != ENTER_STATE_ENTER) {
return RESULT_OK;
}
appi = (APP_INSTANCE_T *) app;
DeleteDialog(app);
port = app->port;
app_state = app->state;
dialog = DialogType_None;
memclr(&content, sizeof(CONTENT_T));
switch (app_state) {
case APP_STATE_MAIN:
list = CreateList(ev_st, app);
if (list != NULL) {
DRM_SetResource(appi->resources[APP_RESOURCE_NAME],
(void *) appi->current_title, (u_strlen(appi->current_title) + 1) * sizeof(WCHAR));
actions.action[0].operation = ACTION_OP_ADD;
actions.action[0].event = LdrFindEventHandlerTbl(g_state_main_hdls, HandleEventHelp);
actions.action[0].action_res = appi->resources[APP_RESOURCE_ACTION_HELP];
actions.action[1].operation = ACTION_OP_ADD;
actions.action[1].event = LdrFindEventHandlerTbl(g_state_main_hdls, HandleEventAbout);
actions.action[1].action_res = appi->resources[APP_RESOURCE_ACTION_ABOUT];
actions.action[2].operation = ACTION_OP_ADD;
actions.action[2].event = LdrFindEventHandlerTbl(g_state_main_hdls, HandleEventExit);
actions.action[2].action_res = appi->resources[APP_RESOURCE_ACTION_EXIT];
actions.count = 3;
dialog = UIS_CreateStaticList(&port, 0, appi->fs.count, 0, list, FALSE, 2, &actions,
appi->resources[APP_RESOURCE_LIST_DESCRIPTION]);
suFreeMem(list);
#if defined(SHOW_ELF_ICONS)
suFreeMem(appi->img_list_elements);
#endif
/* Insert cursor to proper position. */
if (appi->flag_from_select) {
if (appi->menu_current_item_index != 0) {
APP_UtilAddEvChangeListPosition(ev_st, app, appi->menu_current_item_index + 1,
0, 0, NULL);
UIS_HandleEvent(dialog, ev_st);
}
appi->flag_from_select = FALSE;
}
}
break;
case APP_STATE_POPUP:
switch (appi->popup) {
default:
case APP_POPUP_WAIT:
UpdateList(ev_st, app, appi->current_path, L"*");
notice_type = NOTICE_TYPE_WAIT_NO_KEY;
UIS_MakeContentFromString("MCq0NMCq1", &content, g_str_popup_please_wait, g_str_popup_sorting);
break;
case APP_POPUP_NOT_ELF:
notice_type = NOTICE_TYPE_FAIL;
UIS_MakeContentFromString("MCq0NMCq1", &content, g_str_popup_cannot_run, g_str_popup_not_elf);
break;
}
dialog = UIS_CreateTransientNotice(&port, &content, notice_type);
/* Reset it to default POPUP_WAIT */
appi->popup = APP_POPUP_WAIT;
break;
case APP_STATE_VIEW:
switch (appi->view) {
default:
case APP_VIEW_HELP:
UIS_MakeContentFromString("q0Nq1", &content, g_str_view_help,
g_str_help_content_p1);
break;
case APP_VIEW_ABOUT:
UIS_MakeContentFromString("q0NMCp1NMCq2NMCq3NMCq4NMCq5NMCq6", &content, g_str_app_name,
appi->resources[APP_RESOURCE_ICON_ELFBOX],
g_str_about_content_p1, g_str_about_content_p2, g_str_about_content_p3,
g_str_about_content_p4, g_str_about_content_p4);
break;
}
dialog = UIS_CreateViewer(&port, &content, NULL);
break;
default:
break;
}
if (dialog == DialogType_None) {
return RESULT_FAIL;
}
app->dialog = dialog;
return RESULT_OK;
}
static UINT32 HandleStateExit(EVENT_STACK_T *ev_st, APPLICATION_T *app, EXIT_STATE_TYPE_T state) {
if (state == EXIT_STATE_EXIT) {
DeleteDialog(app);
return RESULT_OK;
}
return RESULT_FAIL;
}
static UINT32 DeleteDialog(APPLICATION_T *app) {
if (app->dialog != DialogType_None) {
UIS_Delete(app->dialog);
app->dialog = DialogType_None;
return RESULT_OK;
}
return RESULT_FAIL;
}
static UINT32 HandleEventTimerExpired(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
EVENT_T *event;
APP_TIMER_T timer_id;
event = AFW_GetEv(ev_st);
timer_id = ((DL_TIMER_DATA_T *) event->attachment)->ID;
switch (timer_id) {
case APP_TIMER_EXIT:
/* Play an exit sound using quiet speaker. */
DL_AudPlayTone(0x00, 0xFF);
/* Fall through */
case APP_TIMER_EXIT_FAST:
return ApplicationStop(ev_st, app);
break;
case APP_TIMER_TO_MAIN_VIEW:
return APP_UtilChangeState(APP_STATE_MAIN, ev_st, app);
break;
default:
break;
}
return RESULT_OK;
}
static UINT32 HandleEventSelect(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *appi;
EVENT_T *event;
status = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
event = AFW_GetEv(ev_st);
appi->menu_current_item_index = event->data.index - 1;
switch (appi->fs.list[appi->menu_current_item_index].type) {
case APP_FS_VOLUME:
u_strcpy(appi->current_path, appi->fs.list[appi->menu_current_item_index].name);
appi->current_path[u_strlen(appi->current_path) - 1] = 0;
u_strcpy(appi->current_title, appi->current_path);
break;
case APP_FS_FOLDER:
u_strcpy(appi->current_path,
DirDown(appi->current_path, appi->fs.list[appi->menu_current_item_index].name));
u_strcpy(appi->current_title, appi->current_path);
break;
case APP_FS_FOLDER_UP:
u_strcpy(appi->current_path,
DirUp(appi->current_path));
u_strcpy(appi->current_title, appi->current_path);
break;
case APP_FS_FILE:
appi->flag_from_select = TRUE;
appi->popup = APP_POPUP_NOT_ELF;
break;
case APP_FS_ELF:
u_strcat(appi->current_path, L"/");
u_strcat(appi->current_path, appi->fs.list[appi->menu_current_item_index].name);
status |= RunElfApplication(ev_st, app, appi->current_path);
return RESULT_OK;
break;
default:
break;
}
#if defined(DEBUG)
{
char file_path[FS_MAX_PATH_NAME_LENGTH + 1];
LOG("Selected: %d %d.\n", appi->menu_current_item_index, appi->fs.list[appi->menu_current_item_index].type);
u_utoa(appi->current_path, file_path);
LOG("Current Dir: %s\n", file_path);
u_utoa(appi->current_title, file_path);
LOG("Current Title: %s\n", file_path);
}
#endif
status |= APP_UtilChangeState(APP_STATE_POPUP, ev_st, app);
return status;
}
static UINT32 HandleEventBackDir(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *appi;
status = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
if (!appi->fs.root) {
u_strcpy(appi->current_path, DirUp(appi->current_path));
u_strcpy(appi->current_title, appi->current_path);
status |= APP_UtilChangeState(APP_STATE_POPUP, ev_st, app);
} else {
status |= ApplicationStop(ev_st, app);
}
return status;
}
static UINT32 HandleEventHelp(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *appi;
status = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
appi->view = APP_VIEW_HELP;
status |= APP_UtilChangeState(APP_STATE_VIEW, ev_st, app);
return status;
}
static UINT32 HandleEventAbout(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
APP_INSTANCE_T *appi;
status = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
appi->view = APP_VIEW_ABOUT;
status |= APP_UtilChangeState(APP_STATE_VIEW, ev_st, app);
return status;
}
static UINT32 HandleEventExit(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
status = RESULT_OK;
status |= ApplicationStop(ev_st, app);
return status;
}
static UINT32 HandleEventBack(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
status = RESULT_OK;
status |= APP_UtilChangeState(APP_STATE_MAIN, ev_st, app);
return status;
}
#if defined(ASYNC)
static UINT32 HandleEventSearchCompleted(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
EVENT_T *event;
FS_SEARCH_COMPLETED_INDEX_T *search_index;
status = RESULT_OK;
event = AFW_GetEv(ev_st);
search_index = (FS_SEARCH_COMPLETED_INDEX_T *) event->attachment;
D("%d elements found.\n", search_index->search_total);
status |= FillFileList(ev_st, app, search_index, NULL);
status |= APP_ConsumeEv(ev_st, app);
status |= APP_UtilChangeState(APP_STATE_MAIN, ev_st, app);
return status;
}
#endif
static LIST_ENTRY_T *CreateList(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
UINT32 status;
INT32 result;
UINT32 i;
LIST_ENTRY_T *list_elements;
APP_INSTANCE_T *appi;
RESOURCE_ID list_icon;
BOOL custom_icon;
status = RESULT_OK;
result = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
custom_icon = FALSE;
if (appi->fs.count == 0) {
return NULL;
}
list_elements = (LIST_ENTRY_T *) suAllocMem(sizeof(LIST_ENTRY_T) * appi->fs.count, &result);
if (result != RESULT_OK) {
return NULL;
}
#if defined(SHOW_ELF_ICONS)
appi->img_list_elements =
(LIST_IMAGE_ELEMENT_T *) suAllocMem(sizeof(LIST_IMAGE_ELEMENT_T) * appi->fs.count, &result);
if (result != RESULT_OK) {
return NULL;
}
#endif
for (i = 0; i < appi->fs.count; ++i) {
memclr(&list_elements[i], sizeof(LIST_ENTRY_T));
list_elements[i].editable = FALSE;
list_elements[i].content.static_entry.formatting = TRUE;
switch (appi->fs.list[i].type) {
case APP_FS_VOLUME:
list_icon = appi->resources[APP_RESOURCE_ICON_DISK];
break;
case APP_FS_FOLDER_UP:
case APP_FS_FOLDER:
list_icon = appi->resources[APP_RESOURCE_ICON_FOLDER];
break;
default:
case APP_FS_FILE:
list_icon = appi->resources[APP_RESOURCE_ICON_FILE];
break;
case APP_FS_ELF:
list_icon = appi->resources[APP_RESOURCE_ICON_ELF];
break;
}
#if defined(SHOW_ELF_ICONS)
custom_icon = FALSE;
if (appi->fs.list[i].type == APP_FS_ELF) {
FS_MID_T mid;
WCHAR gif_uri[FS_MAX_URI_NAME_LENGTH + 1]; /* 265 */
status |= AddUriFileHeader(gif_uri, appi->current_path);
u_strcat(gif_uri, L"/");
u_strcat(gif_uri, appi->fs.list[i].name);
gif_uri[u_strlen(gif_uri) - 3] = '\0';
u_strcat(gif_uri, L"gif");
if (DL_FsFFileExist(gif_uri)) {
DL_FsGetIDFromURI(gif_uri, &mid);
appi->img_list_elements[i].image.file_id = mid;
appi->img_list_elements[i].image_type = LIST_IMAGE_DL_FS_MID_T;
appi->img_list_elements[i].image_index = i;
custom_icon = TRUE;
}
}
if (custom_icon) {
status |= UIS_MakeContentFromString("Mj0 Mq1",
&list_elements[i].content.static_entry.text, &appi->img_list_elements[i], appi->fs.list[i].name);
} else {
status |= UIS_MakeContentFromString("Mp0 Mq1",
&list_elements[i].content.static_entry.text, list_icon, appi->fs.list[i].name);
}
#else
status |= UIS_MakeContentFromString("Mp0 Mq1",
&list_elements[i].content.static_entry.text, list_icon, appi->fs.list[i].name);
#endif
}
if (status != RESULT_OK) {
suFreeMem(list_elements);
return NULL;
}
return list_elements;
}
static UINT32 UpdateList(EVENT_STACK_T *ev_st, APPLICATION_T *app, const WCHAR *directory, const WCHAR *filter) {
UINT32 status;
status = RESULT_OK;
if (!directory || !u_strcmp(directory, L"/")) {
status |= UpdateVolumeList(ev_st, app);
status |= APP_UtilStartTimer(TIMER_FAST_TRIGGER_MS, APP_TIMER_TO_MAIN_VIEW, app);
} else {
WCHAR search_string[FS_MAX_URI_NAME_LENGTH + 16]; /* 280 */
status |= CreateSearchString(search_string, directory, filter);
status |= UpdateFileList(ev_st, app, search_string);
}
return RESULT_OK;
}
static UINT32 UpdateVolumeList(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
/*
* List of volumes.
* /a/
* /b/
* /c/
*/
INT32 i;
INT32 error;
APP_INSTANCE_T *appi;
WCHAR volumes[MAX_VOLUMES_COUNT * 3];
#if 0
= {
L'/', L'a', 0xFFEE,
L'/', L'b', 0xFFEE,
L'/', L'c', 0xFFEE,
L'/', L'd', 0xFFEE
};
#endif
WCHAR *result;
appi = (APP_INSTANCE_T *) app;
appi->fs.root = TRUE;
result = DL_FsVolumeEnum(volumes);
if (!result) {
return RESULT_FAIL;
}
appi->fs.root = TRUE;
appi->fs.count = 0;
if (appi->fs.list) {
LOG("%s\n", "Cleaning previous file list.");
suFreeMem(appi->fs.list);
}
appi->fs.list = suAllocMem(sizeof(APP_FS_OBJECT_T) * MAX_VOLUMES_COUNT, &error);
if (error != RESULT_OK) {
LOG("Error: Cannot allocate %d bytes.\n", sizeof(APP_FS_OBJECT_T) * MAX_VOLUMES_COUNT);
return RESULT_FAIL;
}
for (i = 0; i < MAX_VOLUMES_COUNT; ++i) {
appi->fs.list[i].name[0] = volumes[i * 3];
appi->fs.list[i].name[1] = volumes[i * 3 + 1];
appi->fs.list[i].name[2] = volumes[i * 3];
appi->fs.list[i].name[3] = 0;
appi->fs.list[i].name[4] = 0;
appi->fs.list[i].type = APP_FS_VOLUME;
appi->fs.count += 1;
if (!volumes[i * 3 + 2]) {
break;
}
}
return RESULT_OK;
}
static UINT32 UpdateFileList(EVENT_STACK_T *ev_st, APPLICATION_T *app, const WCHAR *search_string) {
/*
* List of directories and files.
*/
UINT32 status;
APP_INSTANCE_T *appi;
IFACE_DATA_T iface;
FS_SEARCH_COMPLETED_INDEX_T search_index;
FS_SEARCH_PARAMS_T search_params;
FS_SEARCH_INFO_T *search_info;
FS_URI_FNCT_PTR complete_function;
memclr(&search_params, sizeof(FS_SEARCH_PARAMS_T));
memclr(&search_index, sizeof(FS_SEARCH_COMPLETED_INDEX_T));
status = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
iface.port = app->port;
search_info = NULL;
complete_function = NULL;
appi->fs.root = FALSE;
search_params.flags = FS_SEARCH_FOLDERS | FS_SEARCH_START_PATH;
search_params.attrib = FS_ATTR_DEFAULT;
search_params.mask = FS_ATTR_DEFAULT;
#if (defined(FTR_V600) || defined(FTR_V635)) && !defined(ASYNC)
status |= DL_FsSearch(search_params, search_string, &search_info, &complete_function, 0);
search_index.search_handle = search_info->shandle;
search_index.search_total = search_info->num;
#elif defined(ASYNC)
search_index.search_handle = DL_FsISearch(&iface, search_params, search_string, 0);
search_index.search_total = 0;
#else
status |= DL_FsSSearch(search_params, search_string, &search_index.search_handle, &search_index.search_total, 0);
#endif
D("Status: %d.\n", status);
D("Info Num: %d.\n", search_index.search_total);
if (status != RESULT_OK) {
LOG("%s\n", "Trying to fix search.");
if (search_index.search_handle) {
LOG("%s\n", "Search Handle is not 0.");
status |= DL_FsSearchClose(search_index.search_handle);
}
u_strcpy(appi->current_path, L"/");
UpdateVolumeList(ev_st, app);
status |= APP_UtilStartTimer(TIMER_FAST_TRIGGER_MS, APP_TIMER_TO_MAIN_VIEW, app);
return status;
}
#if !defined(ASYNC)
status |= FillFileList(ev_st, app, &search_index, search_string);
status |= APP_UtilStartTimer(TIMER_FAST_TRIGGER_MS, APP_TIMER_TO_MAIN_VIEW, app);
#endif
return status;
}
static UINT32 FillFileList(EVENT_STACK_T *ev_st, APPLICATION_T *app,
FS_SEARCH_COMPLETED_INDEX_T *search_index, const WCHAR *search_string) {
UINT32 status;
INT32 error;
APP_INSTANCE_T *appi;
UINT16 i;
UINT16 count;
FS_SEARCH_RESULT_T res;
status = RESULT_OK;
error = RESULT_OK;
appi = (APP_INSTANCE_T *) app;
count = 1;
LOG("Cleaning previous file list. New elements: %d\n", search_index->search_total);
suFreeMem(appi->fs.list);
appi->fs.list = NULL;
appi->fs.count = search_index->search_total;
appi->fs.count += 1;
appi->fs.list = suAllocMem(sizeof(APP_FS_OBJECT_T) * appi->fs.count, &error);
if (error) {
LOG("Error: Cannot allocate %d bytes.\n", sizeof(APP_FS_OBJECT_T) * appi->fs.count);
return RESULT_FAIL;
}
u_strcpy(appi->fs.list[0].name, L"..");
appi->fs.list[0].type = APP_FS_FOLDER_UP;
for (i = 0; i < search_index->search_total; ++i) {
status = DL_FsSearchResults(search_index->search_handle, i, &count, &res);
if (status == RESULT_OK) {
#if defined(DEBUG)
char file_path[FS_MAX_FILE_NAME_LENGTH + 1];
u_utoa(res.name, file_path);
LOG("%d Added: %s, attrib 0x%X, owner %d.\n",
i + 1, file_path, res.attrib, res.owner);
#endif
if (IsDirectory(res.attrib)) {
/*
* HACK Stage 1: Use first dot to proper files and folder sorting:
* - ..
* - .folder1
* - .folder2
* - File
* - file
*/
u_strcpy(appi->fs.list[i + 1].name, L".");
u_strcpy(appi->fs.list[i + 1].name + 1, GetFileNameFromPath(res.name));
appi->fs.list[i + 1].type = APP_FS_FOLDER;
} else {
u_strcpy(appi->fs.list[i + 1].name, GetFileNameFromPath(res.name));
appi->fs.list[i + 1].type = (IsElfFile(appi->fs.list[i + 1].name)) ? APP_FS_ELF : APP_FS_FILE;
}
} else {
LOG("DL_FsSearchResults result=%d, i=%d, size=%d\n", status, i, search_index->search_total);
D("%s\n", "Search is closed in the loop!\n");
status |= DL_FsSearchClose(search_index->search_handle);
return UpdateFileList(ev_st, app, search_string);
}
}
status |= SortFileList(ev_st, app);
D("%s\n", "Search is closed in the end of function!\n");
status |= DL_FsSearchClose(search_index->search_handle);
return status;
}
static int file_comparate(const void *obj_1, const void *obj_2) {
APP_FS_OBJECT_T *fs_obj_1;
APP_FS_OBJECT_T *fs_obj_2;
WCHAR fs_name_1[MAX_FILENAME_SYMS + 1];
WCHAR fs_name_2[MAX_FILENAME_SYMS + 1];
fs_obj_1 = (APP_FS_OBJECT_T *) obj_1;
fs_obj_2 = (APP_FS_OBJECT_T *) obj_2;
u_strcpy(fs_name_1, fs_obj_1->name);
u_strcpy(fs_name_2, fs_obj_2->name);
u_strmakeupper(fs_name_1);
u_strmakeupper(fs_name_2);
return u_strcmp(fs_name_1, fs_name_2);
}
static UINT32 SortFileList(EVENT_STACK_T *ev_st, APPLICATION_T *app) {
INT32 i;
UINT32 status;
APP_INSTANCE_T *appi;
APP_FS_OBJECT_T *objects;
status = RESULT_OK;