185 lines
4.7 KiB
C
185 lines
4.7 KiB
C
#include <curses.h>
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include "defines.h"
|
|
#include "backend.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 *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_mid(void *data){
|
|
pthread_mutex_lock(&mutex_mid);
|
|
|
|
|
|
char *path;
|
|
if((path=getcwd(NULL, 0)) == NULL) {
|
|
mid_content = malloc(sizeof(file));
|
|
mid_content[1].file_name_width = sizeof("cannot open directory");
|
|
mid_content[1].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);
|
|
|
|
}
|
|
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[1].file_name_width = sizeof("cannot open directory");
|
|
lft_content[1].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);
|
|
char *path = mid_content[selected_file_current].file_name;
|
|
pthread_mutex_unlock(&mutex_mid);
|
|
|
|
if (mid_content[selected_file_current].file_type == FILE_TYPE_DIR || mid_content[selected_file_current].file_type == FILE_TYPE_SYMLINK) {
|
|
free(rgt_content);
|
|
rgt_file_count = (unsigned long)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);
|
|
} else if (mid_content[selected_file_current].file_type == FILE_TYPE_REGULAR) {
|
|
FILE *fp = fopen(mid_content[selected_file_current].file_name, "r");
|
|
unsigned long file_size = ftell(fp);
|
|
rewind(fp);
|
|
free(rgt_content);
|
|
rgt_content = malloc(sizeof(file));
|
|
rgt_content[0].file_name = malloc(file_size);
|
|
fgets(rgt_content[0].file_name, file_size, fp);
|
|
rgt_content[0].file_size_bytes = file_size;
|
|
rgt_content[0].file_type = FILE_TYPE_REGULAR;
|
|
rgt_content[0].file_name_width = file_size;
|
|
rgt_content[0].color_pair = 0;
|
|
rgt_content[0].status = 0;
|
|
fclose(fp);
|
|
}
|
|
|
|
|
|
|
|
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(char));
|
|
mid_content = malloc(sizeof(file));
|
|
lft_content = malloc(sizeof(file));
|
|
|
|
top_content = malloc(sizeof(char));
|
|
|
|
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);
|
|
}
|