From 259ec9d424fc04259290f071a6c03645823418c4 Mon Sep 17 00:00:00 2001 From: Arnaud SINTES Date: Thu, 2 Nov 2023 13:05:03 +0100 Subject: [PATCH] refactor dual slots display --- src/fio-ml.c | 120 ++++++++++++++++++++++++++++++++++++++------------- src/fio-ml.h | 16 +++++++ src/lens.c | 104 +++++--------------------------------------- 3 files changed, 117 insertions(+), 123 deletions(-) diff --git a/src/fio-ml.c b/src/fio-ml.c index 4e1b39896..0f9f2ba15 100644 --- a/src/fio-ml.c +++ b/src/fio-ml.c @@ -9,18 +9,18 @@ #define CARD_DRIVE_INIT(_letter, _type) { .drive_letter = _letter, .type = _type, .cluster_size = 0, .free_space_raw = 0, .file_number = 0, .folder_number = 0, } -static struct card_info available_cards[] = { CARD_DRIVE_INIT("A","CF"), CARD_DRIVE_INIT("B", "SD"), CARD_DRIVE_INIT("C","EXT") }; +static struct card_info possible_cards[] = { CARD_DRIVE_INIT("A","CF"), CARD_DRIVE_INIT("B", "SD"), CARD_DRIVE_INIT("C","EXT") }; #if defined(CONFIG_CF_SLOT) -static struct card_info * ML_CARD = &available_cards[CARD_A]; +static struct card_info * ML_CARD = &possible_cards[CARD_A]; #else -static struct card_info * ML_CARD = &available_cards[CARD_B]; +static struct card_info * ML_CARD = &possible_cards[CARD_B]; #endif #if defined(CONFIG_DUAL_SLOT) || defined(CONFIG_CF_SLOT) -static struct card_info * SHOOTING_CARD = &available_cards[CARD_A]; +static struct card_info * SHOOTING_CARD = &possible_cards[CARD_A]; #else -static struct card_info * SHOOTING_CARD = &available_cards[CARD_B]; +static struct card_info * SHOOTING_CARD = &possible_cards[CARD_B]; #endif // File I/O wrappers for handling the dual card slot on 5D3 @@ -61,7 +61,35 @@ struct card_info* get_shooting_card() struct card_info* get_card(int cardId) { ASSERT(cardId >= 0 && cardId < 3); - return &available_cards[cardId]; + return &possible_cards[cardId]; +} + +struct actual_cards_t *get_actual_cards() +{ + static struct actual_cards_t actual_cards = {0, {NULL, NULL}}; + // init the actual cards the first time: + if (actual_cards.count == 0) + { + for (int i = 0; i < 2; i++) + { + struct card_info *p_card_info = get_card(i); + // check if drive is accessible: + char drive[8]; + snprintf(drive, 7, "%s:/", p_card_info->drive_letter); + if (!is_dir(drive)) + { + continue; + } + // reference active cards and increment actual card count: + actual_cards.infos[actual_cards.count++] = p_card_info; + // compute card total space: + double free_space_GB = ((double)(((uint64_t)get_free_space_32k(p_card_info)) << 5) / (1024 * 1024)); + p_card_info->total_space_GB = free_space_GB + get_folder_size_GB(get_dcim_dir_ex(p_card_info)); + // compute free space approximation: + update_free_space(p_card_info); + } + } + return &actual_cards; } int get_free_space_32k(const struct card_info* card) @@ -69,6 +97,14 @@ int get_free_space_32k(const struct card_info* card) return card->free_space_raw * (card->cluster_size>>10) / (32768>>10); } +void update_free_space(struct card_info *_p_card_info) +{ + // approximate free space based of total space and current DCIM folder content size: + double dcim_size_GB = get_folder_size_GB(get_dcim_dir_ex(_p_card_info)); + int free_space_GB = (int)(_p_card_info->total_space_GB - dcim_size_GB); + _p_card_info->free_space_GB = (free_space_GB > 999) ? 999 : free_space_GB; +} + static CONFIG_INT("card.test", card_test_enabled, 1); static CONFIG_INT("card.force_type", card_force_type, 0); @@ -151,8 +187,8 @@ void _card_tweaks() #ifdef CONFIG_5D3 if (card_test_enabled) { - if (available_cards[CARD_A].free_space_raw > 10) card_test(&available_cards[CARD_A]); - if (available_cards[CARD_B].free_space_raw > 10) card_test(&available_cards[CARD_B]); + if (possible_cards[CARD_A].free_space_raw > 10) card_test(&possible_cards[CARD_A]); + if (possible_cards[CARD_B].free_space_raw > 10) card_test(&possible_cards[CARD_B]); /* if it reaches this point, the cards are OK */ card_test_enabled = 0; @@ -223,17 +259,17 @@ void _find_ml_card() if (ml_cf && !ml_sd) { - ML_CARD = &available_cards[CARD_A]; + ML_CARD = &possible_cards[CARD_A]; } else if (!ml_cf && ml_sd) { - ML_CARD = &available_cards[CARD_B]; + ML_CARD = &possible_cards[CARD_B]; } else if (ml_cf && ml_sd) { /* still ambiguity? */ /* we know Canon loads autoexec.bin from the SD card first */ - ML_CARD = &available_cards[CARD_B]; + ML_CARD = &possible_cards[CARD_B]; startup_warning("ML on both cards, loading from SD."); } else @@ -245,9 +281,9 @@ void _find_ml_card() PROP_HANDLER(PROP_CARD_SELECT) { int card_select = buf[0] - 1; - if (card_select >= 0 && card_select < COUNT(available_cards)) + if (card_select >= 0 && card_select < COUNT(possible_cards)) { - SHOOTING_CARD = &available_cards[card_select]; + SHOOTING_CARD = &possible_cards[card_select]; return; } ASSERT(0); @@ -255,62 +291,62 @@ PROP_HANDLER(PROP_CARD_SELECT) PROP_HANDLER(PROP_CLUSTER_SIZE_A) { - available_cards[CARD_A].cluster_size = buf[0]; + possible_cards[CARD_A].cluster_size = buf[0]; } PROP_HANDLER(PROP_CLUSTER_SIZE_B) { - available_cards[CARD_B].cluster_size = buf[0]; + possible_cards[CARD_B].cluster_size = buf[0]; } PROP_HANDLER(PROP_CLUSTER_SIZE_C) { - available_cards[CARD_C].cluster_size = buf[0]; + possible_cards[CARD_C].cluster_size = buf[0]; } PROP_HANDLER(PROP_FREE_SPACE_A) { - available_cards[CARD_A].free_space_raw = buf[0]; + possible_cards[CARD_A].free_space_raw = buf[0]; } PROP_HANDLER(PROP_FREE_SPACE_B) { - available_cards[CARD_B].free_space_raw = buf[0]; + possible_cards[CARD_B].free_space_raw = buf[0]; } PROP_HANDLER(PROP_FREE_SPACE_C) { - available_cards[CARD_C].free_space_raw = buf[0]; + possible_cards[CARD_C].free_space_raw = buf[0]; } PROP_HANDLER(PROP_FILE_NUMBER_A) { - available_cards[CARD_A].file_number = buf[0]; + possible_cards[CARD_A].file_number = buf[0]; } PROP_HANDLER(PROP_FILE_NUMBER_B) { - available_cards[CARD_B].file_number = buf[0]; + possible_cards[CARD_B].file_number = buf[0]; } PROP_HANDLER(PROP_FILE_NUMBER_C) { - available_cards[CARD_C].file_number = buf[0]; + possible_cards[CARD_C].file_number = buf[0]; } PROP_HANDLER(PROP_FOLDER_NUMBER_A) { - available_cards[CARD_A].folder_number = buf[0]; + possible_cards[CARD_A].folder_number = buf[0]; } PROP_HANDLER(PROP_FOLDER_NUMBER_B) { - available_cards[CARD_B].folder_number = buf[0]; + possible_cards[CARD_B].folder_number = buf[0]; } PROP_HANDLER(PROP_FOLDER_NUMBER_C) { - available_cards[CARD_C].folder_number = buf[0]; + possible_cards[CARD_C].folder_number = buf[0]; } PROP_HANDLER(PROP_DCIM_DIR_SUFFIX) @@ -323,12 +359,38 @@ const char * get_dcim_dir_suffix() return dcim_dir_suffix; } -const char* get_dcim_dir() +const char *get_dcim_dir_ex(struct card_info *_p_card_info) { - snprintf(dcim_dir, sizeof(dcim_dir), "%s:/DCIM/%03d%s", SHOOTING_CARD->drive_letter, SHOOTING_CARD->folder_number, dcim_dir_suffix); + snprintf(dcim_dir, sizeof(dcim_dir), "%s:/DCIM/%03d%s", _p_card_info->drive_letter, _p_card_info->folder_number, dcim_dir_suffix); return dcim_dir; } +const char* get_dcim_dir() +{ + return get_dcim_dir_ex(SHOOTING_CARD); +} + +double get_folder_size_GB(char *_folder) +{ + struct fio_file file; + struct fio_dirent *dirent = FIO_FindFirstEx(_folder, &file); + if (IS_ERROR(dirent)) + { + return 0; + } + double cumulated_size_GB = 0; + do + { + if (file.name[0] == 0 || file.name[0] == '.' || (file.mode & ATTR_DIRECTORY)) + { + continue; + } + cumulated_size_GB += (((double)file.size) / (1024 * 1024 * 1024)); + } while (FIO_FindNextEx(dirent, &file) == 0); + FIO_FindClose(dirent); + return cumulated_size_GB; +} + static void fixup_filename(char* new_filename, const char* old_filename, int size) { #define IS_IN_ML_DIR(filename) (strncmp("ML/", filename, 3) == 0) @@ -822,8 +884,8 @@ static void fio_init() #endif #ifdef CARD_A_MAKER - available_cards[CARD_A].maker = (char*) CARD_A_MAKER; - available_cards[CARD_A].model = (char*) CARD_A_MODEL; + possible_cards[CARD_A].maker = (char*) CARD_A_MAKER; + possible_cards[CARD_A].model = (char*) CARD_A_MODEL; #endif } diff --git a/src/fio-ml.h b/src/fio-ml.h index 150ff0338..b5b3d09b4 100644 --- a/src/fio-ml.h +++ b/src/fio-ml.h @@ -16,6 +16,14 @@ struct card_info { int folder_number; char * maker; /* only for some cameras; NULL otherwise */ char * model; + double total_space_GB; // initial estimation of total card space (GB) + int free_space_GB; // dynamic free space (GB) estimation +}; + +struct actual_cards_t +{ + int count; // number of actual active cards + struct card_info *infos[2]; // pointers to valid card_info }; struct card_info * get_ml_card(); @@ -23,8 +31,12 @@ struct card_info * get_shooting_card(); struct card_info * get_card(int cardId); +struct actual_cards_t *get_actual_cards(); + int get_free_space_32k (const struct card_info * card); +void update_free_space(struct card_info *_p_card_info); + /* returns true if the specified file or directory exists */ int is_file(const char* path); int is_dir(const char* path); @@ -153,9 +165,13 @@ size_t read_file( const char * filename, void * buf, size_t size); uint8_t* read_entire_file(const char * filename, int* buf_size); +const char *get_dcim_dir_ex(struct card_info *_p_card_info); const char* get_dcim_dir(); const char* get_dcim_dir_suffix(); +// compute the cumulated file size (GB) of a given folder +double get_folder_size_GB(char *_folder); + extern int __attribute__((format(printf,2,3))) my_fprintf( FILE * file, diff --git a/src/lens.c b/src/lens.c index b570a13de..dfb5b79d1 100644 --- a/src/lens.c +++ b/src/lens.c @@ -2933,63 +2933,6 @@ static LVINFO_UPDATE_FUNC(fps_update) } } -struct card_t -{ - char *drive; // drive letter of the card - struct card_info *p_card_info; // pointer to internal card information structure - double total_space_gb; // initial estimation of total card space (GB) - int free_space_gb; // dynamic free space (GB) estimation -}; - -// compute the cumulated file size (GB) of a given folder: -double get_folder_size_gb(char *_folder) -{ - struct fio_file file; - struct fio_dirent *dirent = FIO_FindFirstEx(_folder, &file); - if (IS_ERROR(dirent)) - { - return 0; - } - double cumulated_size_gb = 0; - do - { - if (file.name[0] == 0 || file.name[0] == '.' || (file.mode & ATTR_DIRECTORY)) - { - continue; - } - cumulated_size_gb += (((double)file.size) / (1024 * 1024 * 1024)); - } while (FIO_FindNextEx(dirent, &file) == 0); - FIO_FindClose(dirent); - return cumulated_size_gb; -} - -// compute the cumulated file size (GB) of the DCIM folder of a given card drive: -double get_dcim_folder_size_gb(char *_drive) -{ - char root_folder[FIO_MAX_PATH_LENGTH]; - snprintf(root_folder, FIO_MAX_PATH_LENGTH, "%sDCIM/", _drive); - struct fio_file file; - struct fio_dirent *dirent = FIO_FindFirstEx(root_folder, &file); - if (IS_ERROR(dirent)) - { - return 0; - } - double cumulated_size_kb = 0; - do - { - // search for sub-folders: - if (file.name[0] == 0 || file.name[0] == '.' || !(file.mode & ATTR_DIRECTORY)) - { - continue; - } - char folder[FIO_MAX_PATH_LENGTH]; - snprintf(folder, FIO_MAX_PATH_LENGTH, "%s%s", root_folder, file.name); - cumulated_size_kb += get_folder_size_gb(folder); - } while (FIO_FindNextEx(dirent, &file) == 0); - FIO_FindClose(dirent); - return cumulated_size_kb; -} - static LVINFO_UPDATE_FUNC(free_space_update) { LVINFO_BUFFER(14); @@ -3001,53 +2944,26 @@ static LVINFO_UPDATE_FUNC(free_space_update) return; } - // the first time, we need to identify what card is available or not: - static struct card_t cards[2] = { - {"A:/", NULL, 0}, - {"B:/", NULL, 0}, - }; - static struct card_t *available_cards[2] = {NULL, NULL}; - static int card_count = 0; - if (card_count == 0) - { - for (int i = 0; i < 2; i++) - { - struct card_t *p_card = &cards[i]; - if (!is_dir(p_card->drive)) - { - continue; - } - p_card->p_card_info = get_card(i); - // get free space on disk and size of DCIM folder: - double free_space_gb = ((double)(((uint64_t)get_free_space_32k(p_card->p_card_info)) << 5) / (1024 * 1024)); - double dcim_space_gb = get_dcim_folder_size_gb(p_card->drive); - // we get an approximation of the total disk space: - p_card->total_space_gb = free_space_gb + dcim_space_gb; - available_cards[card_count++] = p_card; - } - } + // get actual card list: + struct actual_cards_t *p_actual_cards = get_actual_cards(); - // every time, we need to update free space for all available cards: - for (int i = 0; i < card_count; i++) + // update free space: + for (int i = 0; i < p_actual_cards->count; i++) { - struct card_t *p_card = available_cards[i]; - // get an approximation of the current free space: - double dcim_size_gb = get_dcim_folder_size_gb(p_card->drive); - int free_space_gb = (int)(p_card->total_space_gb - dcim_size_gb); - p_card->free_space_gb = (free_space_gb > 999) ? 999 : free_space_gb; + update_free_space(p_actual_cards->infos[i]); } // setup display for 1 slot: - struct card_t *p_card_1 = available_cards[0]; - if (card_count == 1) + struct card_info *p_card_1 = p_actual_cards->infos[0]; + if (p_actual_cards->count == 1) { - snprintf(buffer, sizeof(buffer), "%s:%d", p_card_1->p_card_info->type, p_card_1->free_space_gb); + snprintf(buffer, sizeof(buffer), "%s:%d", p_card_1->type, p_card_1->free_space_GB); } // setup display for 2 slots: else { - struct card_t *p_card_2 = available_cards[1]; - snprintf(buffer, sizeof(buffer), "%s:%d %s:%d", p_card_1->p_card_info->type, p_card_1->free_space_gb, p_card_2->p_card_info->type, p_card_2->free_space_gb); + struct card_info *p_card_2 = p_actual_cards->infos[1]; + snprintf(buffer, sizeof(buffer), "%s:%d %s:%d", p_card_1->type, p_card_1->free_space_GB, p_card_2->type, p_card_2->free_space_GB); } }