@@ -14,22 +14,21 @@ namespace Selector {
1414 * @param nb int, the number of files in the list
1515 * @param selected_file int, the selected file to highlight
1616 */
17- static void drawfileList (char ** filenames, int nb, int selected_file) {
18- // Initialize the name buffer
17+ static void drawfileList (char **filenames, int nb, int selected_file) {
1918 char name_buffer[FILENAME_LENGTH_MAX ];
20- // Iterate through the file list
19+ // Iterate through the file list and display them
2120 for (int i = 0 ; i < nb; i++) {
22- // Copy the filename into the buffer
2321 strncpy (name_buffer, filenames[i], FILENAME_LENGTH_MAX );
24- // Force the string to terminate with \0 because the filename can be longer than the length of the buffer
22+ // Truncate the list if needed
2523 name_buffer[26 ] = ' \0 ' ;
26- // Draw the filename, highlighted if selected
2724 extapp_drawTextLarge (name_buffer, 0 , (i + 1 ) * 20 , selected_file == i ? SELECTOR_COLOR_FG_SELECT : SELECTOR_COLOR_FG , SELECTOR_COLOR_BG , false );
28- // TODO: I don't understand it
25+
26+ // Shouldn't be necessary
2927 strncpy (name_buffer, filenames[i], FILENAME_LENGTH_MAX );
3028 }
3129}
3230
31+ // Remove element from the list
3332static char ** remove (char ** first, char ** last) {
3433 char ** result = first;
3534 while (first != last) {
@@ -49,90 +48,65 @@ static char ** remove(char ** first, char ** last) {
4948 * @return const char *, the name of the selected file, or NULL if no file is selected
5049 */
5150const char * select_file (const char * extension_to_match, int max_files) {
52- // The file list
5351 char * filenames[max_files];
54- // The selected file index in the order of the file list
5552 int selected_file = 0 ;
5653
57- // Wait for the last key to be released
5854 Peripherals::waitForKeyReleased ();
5955
60- // Clear the display
6156 extapp_pushRectUniform (0 , 0 , LCD_WIDTH , LCD_HEIGHT , SELECTOR_COLOR_BG );
62- // Draw title bar
6357 extapp_drawTextLarge (" Select a file " , 0 , 0 , SELECTOR_COLOR_HEAD_FG , SELECTOR_COLOR_HEAD_BG , false );
6458
6559 // Get the list of files from the flash memory
6660 int nb = extapp_fileListWithExtension ((const char **)filenames, max_files, " " , EXTAPP_FLASH_FILE_SYSTEM );
6761
68- // The extension of the file
6962 char * extension;
7063
7164 // If the extension to match isn't empty, filter the file list
72- // Checking without strcomp, because it's also a string littery when it is empty
7365 if (extension_to_match[0 ] != ' \0 ' ) {
74- // Iterate through the file list
7566 for (int i = 0 ; i < nb; i++) {
76- // Get the file extension by splitting his name by a dot
7767 extension = strrchr (filenames[i], ' .' );
7868
79- // Get if the file's extension doesn't match the requested extension
8069 if (strcmp (extension, extension_to_match) != 0 ) {
8170 // If the extension doesn't match, delete the file from the list
8271 filenames[i] = NULL ;
8372 }
8473 }
85- // Update the number of files
8674 nb = remove (filenames, filenames + nb) - filenames;
8775 }
8876
8977 // If no files match, draw a no file found message
9078 if (nb == 0 ) {
91- // Draw the text
9279 extapp_drawTextLarge (" No file found " , 0 , 120 , SELECTOR_COLOR_FG , SELECTOR_COLOR_BG , false );
93- // Sleep for 10 milliseconds
9480 extapp_msleep (10 );
95- // Wait for a key to be pressed
9681 Peripherals::waitForKeyPressed ();
97- // Return nothing
9882 return NULL ;
9983 } else {
10084 // Draw the file list
10185 drawfileList (filenames, nb, selected_file);
102- // Infinite loop
10386 for (;;) {
104- // Sleep for 10 milliseconds
10587 extapp_msleep (10 );
88+
10689 // Scan the keyboard
10790 uint64_t scancode = extapp_scanKeyboard ();
10891 // If the down arrow is pressed, select the next file
10992 if (scancode & SCANCODE_Down) {
110- // Select the next file, and go to the first file if the file is out of range
11193 selected_file = (selected_file + 1 ) % nb;
112- // Redraw the file list
11394 drawfileList (filenames, nb, selected_file);
114- // Wait for the key to be released, or 200 milliseconds if the key sill pressed
11595 Peripherals::waitForKeyReleasedTimeout (200 );
11696 } else if (scancode & SCANCODE_Up) {
117- // Select the previous file
11897 selected_file = (selected_file - 1 ) % nb;
119- // If the file is out of range, select the last file
12098 if (selected_file < 0 ) {
12199 selected_file = nb + selected_file;
122100 }
123- // Redraw the file list
124101 drawfileList (filenames, nb, selected_file);
125- // Wait for the key to be released, or 200 milliseconds if the key sill pressed
126102 Peripherals::waitForKeyReleasedTimeout (200 );
127103 } else if (scancode & (SCANCODE_OK | SCANCODE_EXE )) {
128104 // If OK or EXE is pressed, exit the loop
129105 break ;
130- } else if (scancode & SCANCODE_Back) {
131- // If back key is pressed, exit the menu and return nothing
106+ } else if (scancode & (SCANCODE_Back | SCANCODE_Home | SCANCODE_OnOff)) {
132107 return NULL ;
133108 }
134109 }
135- // Redraw the selected file
136110 return filenames[selected_file];
137111 }
138112}
0 commit comments