th/threading.c

151 lines
3.4 KiB
C

#include <curses.h>
#include <pthread.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);
free(mid_content);
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 {
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);
free(lft_content);
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] = '/';
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_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);
}