now checking for file names rather than using indexes on dir refresh

This commit is contained in:
nova
2026-07-23 12:48:34 +02:00
parent b30229b770
commit 06c00a0924
+24 -16
View File
@@ -51,10 +51,7 @@ unsigned int btm_status;
void *thread_mid(){
dir tmp;
tmp.current_file = NULL;
tmp.file_list = NULL;
tmp.file_count = 0;
dir previous_dir_state = { 0 };
while(!(status & STATUS_QUIT_PROGRAM)){
@@ -74,7 +71,7 @@ void *thread_mid(){
if (status & STATUS_RELOAD_DIRECTORY) {
long index = mid_dir.current_file - mid_dir.file_list;
tmp = mid_dir;
previous_dir_state = mid_dir;
mid_dir.file_count = get_dir_size(path);
mid_dir.file_list = malloc(mid_dir.file_count * sizeof(file));
@@ -88,8 +85,21 @@ void *thread_mid(){
}
unsigned long i;
for(i = 0; i < mid_dir.file_count && i < tmp.file_count; i++) {
mid_dir.file_list[i].status = tmp.file_list[i].status;
for(i = 0; i < mid_dir.file_count && i < previous_dir_state.file_count; i++) {
mid_dir.file_list[i].status = previous_dir_state.file_list[i].status;
}
/* youd think that after the above previous_dir_state = mid_dir previous_dir_state.current_file is always within the bounds of the file_list,
* but no, it fucking is not for some reason
* however for some mysterious reason this condition doesnt fail. well it probably does,
* but that doesnt matter as it does what its supposed to do:
* keep the current file selected after a reload even if the dir contents change like a new file being added */
if (previous_dir_state.current_file != NULL && previous_dir_state.current_file < previous_dir_state.file_list + previous_dir_state.file_count) {
for (i = 0; i < mid_dir.file_count; i++) {
if (strcmp(mid_dir.file_list[i].file_name, previous_dir_state.file_list[index].file_name) == 0) {
mid_dir.current_file = &mid_dir.file_list[i];
break;
}
}
}
@@ -124,17 +134,15 @@ void *thread_mid(){
pthread_mutex_unlock(&mutex_render);
unsigned long i;
for (i = 0; i < tmp.file_count; i++) {
free(tmp.file_list[i].file_name);
for (i = 0; i < previous_dir_state.file_count; i++) {
free(previous_dir_state.file_list[i].file_name);
}
free(tmp.file_list);
tmp.current_file = NULL;
tmp.file_list = NULL;
tmp.file_count = 0;
free(previous_dir_state.file_list);
previous_dir_state.current_file = NULL;
previous_dir_state.file_list = NULL;
previous_dir_state.file_count = 0;
free(path);
}
pthread_exit(0);
}