/* Main function that runs all the stuff */ void gui_MenuRun(MENU *menu) { SDL_Event gui_event; MENUITEM *mi; done = 0; while(!done) { mi = menu->m + menu->itemCur; // pointer to highlit menu option while(SDL_PollEvent(&gui_event)) { if(gui_event.type == SDL_KEYDOWN) { // DINGOO A - apply parameter or enter submenu if(gui_event.key.keysym.sym == SDLK_LCTRL) if(mi->itemOnA != NULL) (*mi->itemOnA)(); // DINGOO B - exit or back to previous menu if(gui_event.key.keysym.sym == SDLK_LALT) return; // DINGOO UP - arrow down if(gui_event.key.keysym.sym == SDLK_UP) if(--menu->itemCur < 0) menu->itemCur = menu->itemNum - 1; // DINGOO DOWN - arrow up if(gui_event.key.keysym.sym == SDLK_DOWN) if(++menu->itemCur == menu->itemNum) menu->itemCur = 0; // DINGOO LEFT - decrease parameter value if(gui_event.key.keysym.sym == SDLK_LEFT) { if(mi->itemPar != NULL && *mi->itemPar > 0) *mi->itemPar -= 1; } // DINGOO RIGHT - increase parameter value if(gui_event.key.keysym.sym == SDLK_RIGHT) { if(mi->itemPar != NULL && *mi->itemPar < mi->itemParMaxValue) *mi->itemPar += 1; } } } if(!done) ShowMenu(menu); // show menu items SDL_Delay(16); gui_Flip(); } }
s32 load_file(char **wildcards, char *result) { SDL_Event gui_event; char current_dir_name[MAX__PATH]; DIR *current_dir; struct dirent *current_file; struct stat file_info; char current_dir_short[81]; u32 current_dir_length; u32 total_filenames_allocated; u32 total_dirnames_allocated; char **file_list; char **dir_list; u32 num_files; u32 num_dirs; char *file_name; u32 file_name_length; u32 ext_pos = -1; u32 chosen_file, chosen_dir; u32 dialog_result = 1; s32 return_value = 1; u32 current_file_selection; u32 current_file_scroll_value; u32 current_dir_selection; u32 current_dir_scroll_value; u32 current_file_in_scroll; u32 current_dir_in_scroll; u32 current_file_number, current_dir_number; u32 current_column = 0; u32 repeat; u32 i; if(was_used == 0) { get_home_path(); chdir(config_home_path); was_used = 1; } while(return_value == 1) { current_file_selection = 0; current_file_scroll_value = 0; current_dir_selection = 0; current_dir_scroll_value = 0; current_file_in_scroll = 0; current_dir_in_scroll = 0; total_filenames_allocated = 32; total_dirnames_allocated = 32; file_list = (char **)malloc(sizeof(char *) * 32); dir_list = (char **)malloc(sizeof(char *) * 32); memset(file_list, 0, sizeof(char *) * 32); memset(dir_list, 0, sizeof(char *) * 32); num_files = 0; num_dirs = 0; chosen_file = 0; chosen_dir = 0; getcwd(current_dir_name, MAX__PATH); current_dir = opendir(current_dir_name); fprintf(stderr, "start_dir = %s\n", config_home_path); // DEBUG printf("Current directory: %s\n", current_dir_name); do { if(current_dir) current_file = readdir(current_dir); else current_file = NULL; if(current_file) { file_name = current_file->d_name; file_name_length = strlen(file_name); if((stat(file_name, &file_info) >= 0) && ((file_name[0] != '.') || (file_name[1] == '.'))) { if(S_ISDIR(file_info.st_mode)) { dir_list[num_dirs] = (char *)malloc(file_name_length + 1); strcpy(dir_list[num_dirs], file_name); num_dirs++; } else { // Must match one of the wildcards, also ignore the . if(file_name_length >= 4) { if(file_name[file_name_length - 4] == '.') ext_pos = file_name_length - 4; else if(file_name[file_name_length - 3] == '.') ext_pos = file_name_length - 3; else ext_pos = 0; for(i = 0; wildcards[i] != NULL; i++) { if(!strcasecmp((file_name + ext_pos), wildcards[i])) { file_list[num_files] = (char *)malloc(file_name_length + 1); strcpy(file_list[num_files], file_name); num_files++; break; } } } } } if(num_files == total_filenames_allocated) { file_list = (char **)realloc(file_list, sizeof(char *) * total_filenames_allocated * 2); memset(file_list + total_filenames_allocated, 0, sizeof(char *) * total_filenames_allocated); total_filenames_allocated *= 2; } if(num_dirs == total_dirnames_allocated) { dir_list = (char **)realloc(dir_list, sizeof(char *) * total_dirnames_allocated * 2); memset(dir_list + total_dirnames_allocated, 0, sizeof(char *) * total_dirnames_allocated); total_dirnames_allocated *= 2; } } } while(current_file); qsort((void *)file_list, num_files, sizeof(char *), sort_function); qsort((void *)dir_list, num_dirs, sizeof(char *), sort_function); // DEBUG //for(i = 0; i < num_dirs; i++) printf("%s\n", dir_list[i]); //for(i = 0; i < num_files; i++) printf("%s\n", file_list[i]); closedir(current_dir); current_dir_length = strlen(current_dir_name); if(current_dir_length > 80) { memcpy(current_dir_short, "...", 3); memcpy(current_dir_short + 3, current_dir_name + current_dir_length - 77, 77); current_dir_short[80] = 0; } else { memcpy(current_dir_short, current_dir_name, current_dir_length + 1); } repeat = 1; if(num_files == 0) current_column = 1; //clear_screen(COLOR_BG); char print_buffer[81]; while(repeat) { //flip_screen(); SDL_FillRect(menuSurface, NULL, COLOR_BG); print_string(current_dir_short, COLOR_ACTIVE_ITEM, COLOR_BG, 0, 0); print_string("Press B to return to the main menu", COLOR_HELP_TEXT, COLOR_BG, 20, 220); for(i = 0, current_file_number = i + current_file_scroll_value; i < FILE_LIST_ROWS; i++, current_file_number++) { if(current_file_number < num_files) { strncpy(print_buffer,file_list[current_file_number], 38); print_buffer[38] = 0; if((current_file_number == current_file_selection) && (current_column == 0)) { print_string(print_buffer, COLOR_ACTIVE_ITEM, COLOR_BG, FILE_LIST_POSITION, ((i + 2) * 8)); } else { print_string(print_buffer, COLOR_INACTIVE_ITEM, COLOR_BG, FILE_LIST_POSITION, ((i + 2) * 8)); } } } for(i = 0, current_dir_number = i + current_dir_scroll_value; i < FILE_LIST_ROWS; i++, current_dir_number++) { if(current_dir_number < num_dirs) { strncpy(print_buffer,dir_list[current_dir_number], 13); print_buffer[14] = 0; if((current_dir_number == current_dir_selection) && (current_column == 1)) { print_string(print_buffer, COLOR_ACTIVE_ITEM, COLOR_BG, DIR_LIST_POSITION, ((i + 2) * 8)); } else { print_string(print_buffer, COLOR_INACTIVE_ITEM, COLOR_BG, DIR_LIST_POSITION, ((i + 2) * 8)); } } } // Catch input // change to read key state later while(SDL_PollEvent(&gui_event)) { if(gui_event.type == SDL_KEYDOWN) { if(gui_event.key.keysym.sym == SDLK_LCTRL) { // DINGOO A - apply parameter or enter submenu if(current_column == 1) { repeat = 0; chdir(dir_list[current_dir_selection]); } else { if(num_files != 0) { repeat = 0; return_value = 0; //strcpy(result, file_list[current_file_selection]); sprintf(result, "%s/%s", current_dir_name, file_list[current_file_selection]); break; } } } if(gui_event.key.keysym.sym == SDLK_LALT) { // DINGOO B - exit or back to previous menu return_value = -1; repeat = 0; break; } if(gui_event.key.keysym.sym == SDLK_UP) { // DINGOO UP - arrow down if(current_column == 0) { if(current_file_selection) { current_file_selection--; if(current_file_in_scroll == 0) { //clear_screen(COLOR_BG); current_file_scroll_value--; } else { current_file_in_scroll--; } } } else { if(current_dir_selection) { current_dir_selection--; if(current_dir_in_scroll == 0) { //clear_screen(COLOR_BG); current_dir_scroll_value--; } else { current_dir_in_scroll--; } } } } if(gui_event.key.keysym.sym == SDLK_DOWN) { // DINGOO DOWN - arrow up if(current_column == 0) { if(current_file_selection < (num_files - 1)) { current_file_selection++; if(current_file_in_scroll == (FILE_LIST_ROWS - 1)) { //clear_screen(COLOR_BG); current_file_scroll_value++; } else { current_file_in_scroll++; } } } else { if(current_dir_selection < (num_dirs - 1)) { current_dir_selection++; if(current_dir_in_scroll == (FILE_LIST_ROWS - 1)) { //clear_screen(COLOR_BG); current_dir_scroll_value++; } else { current_dir_in_scroll++; } } } } if(gui_event.key.keysym.sym == SDLK_LEFT) { // DINGOO LEFT - decrease parameter value if(current_column == 1) { if(num_files != 0) current_column = 0; } } if(gui_event.key.keysym.sym == SDLK_RIGHT) { // DINGOO RIGHT - increase parameter value if(current_column == 0) { if(num_dirs != 0) current_column = 1; } } } } SDL_Delay(16); gui_Flip(); } // free pointers for(i = 0; i < num_files; i++) free(file_list[i]); free(file_list); for(i = 0; i < num_dirs; i++) free(dir_list[i]); free(dir_list); } return return_value; }