#include #include #include #include #include #include #include #include "defines.h" #include "backend.h" #include "file_previews.h" pthread_mutex_t mutex_top; pthread_mutex_t mutex_btm; pthread_mutex_t mutex_lft; pthread_mutex_t mutex_mid; pthread_mutex_t mutex_rgt; pthread_mutex_t mutex_selection; file *rgt_content; file *mid_content; file *lft_content; char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */ file file_current; char *top_content; /* current path */ unsigned long rgt_file_count; unsigned long mid_file_count; unsigned long lft_file_count; unsigned long top_width; extern unsigned int status; extern unsigned long selected_file_current; extern unsigned long selected_file_last; void *thread_rgt(void *data); void *thread_mid(void *data){ pthread_mutex_lock(&mutex_mid); char *path; if((path=getcwd(NULL, 0)) == NULL) { mid_content = malloc(sizeof(file)); mid_content[0].file_name_width = sizeof("cannot open directory"); mid_content[0].file_name = "cannot open directory"; mid_file_count = 1; } else { if (status & STATUS_RELOAD_DIRECTORY) { free(mid_content); mid_file_count = (unsigned long)get_dir_size(path); mid_content = malloc(mid_file_count * sizeof(file)); memset(mid_content, ' ', mid_file_count * sizeof(file)); get_dir_content(path, &mid_file_count, mid_content); } pthread_mutex_lock(&mutex_selection); if (selected_file_current >= mid_file_count) { selected_file_current = mid_file_count-1; } mid_content[selected_file_current].status = FILE_STATUS_HOVER; if (selected_file_current != selected_file_last) { mid_content[selected_file_last].status &= ~FILE_STATUS_HOVER; } selected_file_last = selected_file_current; pthread_mutex_unlock(&mutex_selection); file_current.file_name = malloc(mid_content[selected_file_current].file_name_width + 1); strcpy(file_current.file_name, mid_content[selected_file_current].file_name); file_current.file_name_width = mid_content[selected_file_current].file_name_width; file_current.file_size_bytes = mid_content[selected_file_current].file_size_bytes; file_current.file_type = mid_content[selected_file_current].file_type; } free(path); pthread_mutex_unlock(&mutex_mid); pthread_exit(NULL); } void *thread_lft(void *data){ pthread_mutex_lock(&mutex_lft); /*{{{*/ char *path; if((path=getcwd(NULL, 0)) == NULL) { lft_content = malloc(sizeof(file)); lft_content[0].file_name_width = sizeof("cannot open directory"); lft_content[0].file_name = "cannot open directory"; lft_file_count = 1; } else { path[strrchr(path, '/')-path] = '\0'; path[0] = '/'; if (status & STATUS_RELOAD_DIRECTORY) { free(lft_content); lft_file_count = (unsigned long)get_dir_size(path); lft_content = malloc(lft_file_count * sizeof(file)); memset(lft_content, ' ', lft_file_count * sizeof(file)); get_dir_content(path, &lft_file_count, lft_content); } } free(path); pthread_mutex_unlock(&mutex_lft); /*}}}*/ pthread_exit(NULL); } void *thread_rgt(void *data){ pthread_mutex_lock(&mutex_rgt); pthread_mutex_lock(&mutex_mid); /* TODO(2025-06-13T01:24:43) fix the occasional wrongly coppied path */ free(rgt_content); rgt_content = malloc(sizeof(file)); rgt_content[0].file_name = malloc(file_current.file_name_width + 1); strcpy(rgt_content[0].file_name, file_current.file_name); rgt_content[0].file_name_width = file_current.file_name_width; rgt_content[0].file_size_bytes = file_current.file_size_bytes; rgt_content[0].file_type = file_current.file_type; pthread_mutex_unlock(&mutex_mid); if (rgt_content[0].file_type == FILE_TYPE_DIR || rgt_content[0].file_type == FILE_TYPE_SYMLINK) { char *path = malloc(rgt_content[0].file_name_width + 1); strcpy(path, rgt_content[0].file_name); free(rgt_content); rgt_file_count = get_dir_size(path); rgt_content = malloc(rgt_file_count * sizeof(file)); memset(rgt_content, ' ', rgt_file_count * sizeof(file)); get_dir_content(path, &rgt_file_count, rgt_content); rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN; free(path); } else if (rgt_content[0].file_type == FILE_TYPE_REGULAR) { rgt_file_count = 1; rgt_content[0].file_type = FILE_TYPE_OPEN_FILE; rgt_content[0].status = FILE_STATUS_HOVER; free(rgt_buffer); rgt_buffer = preview_file(rgt_content[0].file_name, rgt_content[0].file_size_bytes); } pthread_mutex_unlock(&mutex_rgt); pthread_exit(NULL); } void *thread_top(void *data){ pthread_mutex_lock(&mutex_top); free(top_content); char *path; if((path=getcwd(NULL, 0)) == NULL) { top_content = malloc(sizeof("cannot open directory")); top_width = sizeof("cannot open directory"); top_content = "cannot open directory"; } else { top_content = getcwd(NULL, 0); top_width = strlen(top_content); } free(path); pthread_mutex_unlock(&mutex_top); pthread_exit(NULL); } void *thread_btm(void *data){ pthread_exit(NULL); } void threading_init(){ rgt_content = malloc(sizeof(file)); mid_content = malloc(sizeof(file)); lft_content = malloc(sizeof(file)); top_content = malloc(sizeof(char)); rgt_buffer = malloc(sizeof(char)); file_current.file_type = 0; file_current.file_size_bytes = 1; file_current.file_name_width = 1; file_current.file_name = "a"; pthread_mutex_init(&mutex_top, NULL); pthread_mutex_init(&mutex_mid, NULL); pthread_mutex_init(&mutex_lft, NULL); pthread_mutex_init(&mutex_selection, NULL); selected_file_current = 0; selected_file_last = 0; } void threading_free(){ free(rgt_content); free(mid_content); free(lft_content); free(top_content); pthread_mutex_destroy(&mutex_top); pthread_mutex_destroy(&mutex_mid); pthread_mutex_destroy(&mutex_lft); }