start of a major rewrite
This commit is contained in:
229
threading.c
229
threading.c
@@ -4,7 +4,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statvfs.h>
|
||||
|
||||
@@ -27,24 +26,19 @@ pthread_cond_t cond_top;
|
||||
pthread_cond_t cond_btm;
|
||||
|
||||
|
||||
file *rgt_content;
|
||||
file *mid_content;
|
||||
file *lft_content;
|
||||
dir rgt_dir;
|
||||
dir mid_dir;
|
||||
dir lft_dir;
|
||||
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
|
||||
char *btm_buffer;
|
||||
char *top_buffer; /* current path */
|
||||
|
||||
|
||||
|
||||
unsigned long rgt_file_count = 0;
|
||||
unsigned long mid_file_count = 0;
|
||||
unsigned long lft_file_count;
|
||||
unsigned long top_width;
|
||||
|
||||
|
||||
|
||||
volatile unsigned long selected_file_current = 0;
|
||||
volatile unsigned long selected_file_last = 0;
|
||||
extern unsigned int terminal_width;
|
||||
extern unsigned int status;
|
||||
extern char *global_path;
|
||||
@@ -60,38 +54,34 @@ void *thread_mid(){
|
||||
unsigned int local_status = status;
|
||||
|
||||
if (global_path == NULL) {
|
||||
mid_content = malloc(sizeof(file));
|
||||
mid_content->file_name = "cannot open directory";
|
||||
mid_file_count = 1;
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
mid_dir.current_file = NULL;
|
||||
mid_dir.file_count = 0;
|
||||
continue;
|
||||
}
|
||||
char *path = malloc(strlen(global_path)+1);
|
||||
memcpy(path, global_path, strlen(global_path)+1);
|
||||
|
||||
if (local_status & STATUS_RELOAD_DIRECTORY) {
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < mid_file_count; i++) {
|
||||
free(mid_content[i].file_name);
|
||||
}
|
||||
free(mid_content);
|
||||
mid_file_count = get_dir_size(path);
|
||||
if (mid_file_count != 0) {
|
||||
mid_content = malloc(mid_file_count * sizeof(file));
|
||||
memset(mid_content, '\0', mid_file_count * sizeof(file));
|
||||
get_dir_content(path, &mid_file_count, mid_content);
|
||||
} else {
|
||||
selected_file_current = 0;
|
||||
mid_content = malloc(sizeof(file));
|
||||
mid_content->file_type = 0;
|
||||
mid_content->file_size = 0;
|
||||
mid_content->permissions = 0;
|
||||
mid_content->color_pair = 0;
|
||||
mid_content->file_name = "";
|
||||
long index = (long)mid_dir.current_file;
|
||||
|
||||
mid_file_count = 0;
|
||||
|
||||
dir tmp;
|
||||
tmp.file_count = get_dir_size(global_path);
|
||||
tmp.file_list = malloc(tmp.file_count * sizeof(file));
|
||||
if (tmp.file_count) { /* fails if dir empty */
|
||||
get_dir_content(global_path, &tmp);
|
||||
} else { /* the hovered dir is empty */
|
||||
tmp.current_file = NULL;
|
||||
}
|
||||
long i;
|
||||
for (i = 0; i < mid_dir.file_count; i++) {
|
||||
free(mid_dir.file_list[i].file_name);
|
||||
}
|
||||
free(mid_dir.file_list);
|
||||
mid_dir.file_list = tmp.file_list;
|
||||
mid_dir.current_file = tmp.file_list;
|
||||
mid_dir.file_count = tmp.file_count;
|
||||
|
||||
update_selected_file();
|
||||
}
|
||||
|
||||
@@ -112,9 +102,8 @@ void *thread_lft(){
|
||||
unsigned int local_status = status;
|
||||
|
||||
if (global_path == NULL) {
|
||||
lft_content = malloc(sizeof(file));
|
||||
lft_content[0].file_name = "cannot open directory";
|
||||
lft_file_count = 1;
|
||||
lft_dir.current_file = NULL;
|
||||
lft_dir.file_count = 0;
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
continue;
|
||||
}
|
||||
@@ -127,14 +116,23 @@ void *thread_lft(){
|
||||
path[0] = '/';
|
||||
|
||||
if (local_status & STATUS_RELOAD_DIRECTORY) {
|
||||
lft_file_count = get_dir_size(path);
|
||||
free(lft_content);
|
||||
lft_content = malloc(lft_file_count * sizeof(file));
|
||||
memset(lft_content, '\0', lft_file_count * sizeof(file));
|
||||
get_dir_content(path, &lft_file_count, lft_content);
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < lft_dir.file_count; i++) {
|
||||
free(lft_dir.file_list[i].file_name);
|
||||
}
|
||||
free(lft_dir.file_list);
|
||||
lft_dir.file_count = get_dir_size(path);
|
||||
if (lft_dir.file_count != 0) {
|
||||
lft_dir.file_list = malloc(lft_dir.file_count * sizeof(file));
|
||||
get_dir_content(path, &lft_dir);
|
||||
} else {
|
||||
lft_dir.current_file = NULL;
|
||||
lft_dir.file_count = 0;
|
||||
|
||||
}
|
||||
}
|
||||
} else {
|
||||
lft_file_count = 0;
|
||||
lft_dir.file_count = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -147,84 +145,86 @@ void *thread_lft(){
|
||||
}
|
||||
void *thread_rgt(){
|
||||
|
||||
file file_current;
|
||||
file_current.file_name = NULL;
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_rgt);
|
||||
pthread_cond_wait(&cond_rgt, &mutex_rgt);
|
||||
|
||||
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
free(file_current.file_name);
|
||||
memcpy(&file_current, &mid_content[selected_file_current], sizeof(file));
|
||||
file_current.file_name = malloc(strlen(mid_content[selected_file_current].file_name));
|
||||
memcpy(file_current.file_name, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name)+1);
|
||||
|
||||
rgt_dir.current_file = mid_dir.current_file;
|
||||
rgt_dir.file_count = 1;
|
||||
rgt_dir.file_list = malloc(sizeof(file));
|
||||
|
||||
memcpy(rgt_dir.file_list, rgt_dir.current_file, sizeof(file));
|
||||
rgt_dir.file_list->file_name = malloc(strlen(rgt_dir.current_file->file_name)+1);
|
||||
memcpy(rgt_dir.file_list->file_name, rgt_dir.current_file->file_name, strlen(rgt_dir.current_file->file_name)+1);
|
||||
rgt_dir.current_file = rgt_dir.file_list;
|
||||
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
|
||||
if (file_current.permissions & S_IRUSR) {
|
||||
if (file_current.file_type &= FILE_TYPE_DIR) {
|
||||
if (rgt_dir.current_file->permissions & S_IRUSR) {
|
||||
if (rgt_dir.current_file->file_type &= FILE_TYPE_DIR) {
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
images_clear();
|
||||
#endif
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_file_count; i++) {
|
||||
if (rgt_content[i].file_name) {
|
||||
free(rgt_content[i].file_name);
|
||||
}
|
||||
}
|
||||
free(rgt_content);
|
||||
|
||||
rgt_file_count = get_dir_size(file_current.file_name);
|
||||
if (rgt_file_count) { /* fails if dir empty */
|
||||
rgt_content = malloc(rgt_file_count * sizeof(file));
|
||||
memset(rgt_content, '\0', rgt_file_count * sizeof(file));
|
||||
get_dir_content(file_current.file_name, &rgt_file_count, rgt_content);
|
||||
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
|
||||
|
||||
free(rgt_buffer);
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
rgt_buffer[0] = '\0';
|
||||
dir tmp;
|
||||
tmp.file_count = get_dir_size(rgt_dir.current_file->file_name);
|
||||
tmp.file_list = malloc(tmp.file_count * sizeof(file));
|
||||
if (tmp.file_count) { /* fails if dir empty */
|
||||
get_dir_content(rgt_dir.current_file->file_name, &tmp);
|
||||
} else { /* the hovered dir is empty */
|
||||
rgt_content = malloc(sizeof(file));
|
||||
rgt_content->file_type = 0;
|
||||
rgt_content->permissions = mid_content[selected_file_current].permissions;
|
||||
tmp.current_file = NULL;
|
||||
}
|
||||
} else if ((status & STATUS_DELTA_TIME) != STATUS_DELTA_TIME && mid_file_count > 0) {
|
||||
long i;
|
||||
for (i = 0; i < rgt_dir.file_count; i++) {
|
||||
free(rgt_dir.file_list[i].file_name);
|
||||
}
|
||||
free(rgt_dir.file_list);
|
||||
rgt_dir.file_list = tmp.file_list;
|
||||
rgt_dir.current_file = tmp.file_list;
|
||||
rgt_dir.file_count = tmp.file_count;
|
||||
|
||||
} else if ((status & STATUS_DELTA_TIME) != STATUS_DELTA_TIME && mid_dir.file_count > 0) {
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_file_count; i++) {
|
||||
if (rgt_content[i].file_name) {
|
||||
free(rgt_content[i].file_name);
|
||||
}
|
||||
}
|
||||
free(rgt_content);
|
||||
rgt_file_count = 0;
|
||||
rgt_content = malloc(sizeof(file));
|
||||
|
||||
free(rgt_buffer);
|
||||
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
|
||||
rgt_content->status = FILE_STATUS_HOVER;
|
||||
rgt_buffer = preview_file(&file_current);
|
||||
rgt_dir.current_file->status = FILE_STATUS_HOVER;
|
||||
rgt_buffer = preview_file(rgt_dir.current_file);
|
||||
rgt_dir.current_file->file_type = FILE_TYPE_OPEN_FILE;
|
||||
|
||||
|
||||
/*
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_dir.file_count; i++) {
|
||||
if (rgt_dir.file_list[i].file_name) {
|
||||
free(rgt_dir.file_list[i].file_name);
|
||||
}
|
||||
}
|
||||
free(rgt_dir.file_list);
|
||||
rgt_dir.file_count = 0;
|
||||
rgt_dir.file_list = malloc(sizeof(file));
|
||||
rgt_dir.current_file = rgt_dir.file_list;
|
||||
*/
|
||||
|
||||
}
|
||||
} else {
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_file_count; i++) {
|
||||
if (rgt_content[i].file_name) {
|
||||
free(rgt_content[i].file_name);
|
||||
for (i = 0; i < rgt_dir.file_count; i++) {
|
||||
if (rgt_dir.current_file[i].file_name) {
|
||||
free(rgt_dir.current_file[i].file_name);
|
||||
}
|
||||
}
|
||||
free(rgt_content);
|
||||
rgt_file_count = 0;
|
||||
rgt_content = malloc(sizeof(file));
|
||||
free(rgt_dir.file_list);
|
||||
rgt_dir.file_count = 0;
|
||||
rgt_dir.file_list = malloc(sizeof(file));
|
||||
rgt_dir.current_file = rgt_dir.file_list;
|
||||
|
||||
free(rgt_buffer);
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
rgt_buffer[0] = '\0';
|
||||
}
|
||||
/*
|
||||
free(path);
|
||||
*/
|
||||
|
||||
pthread_mutex_unlock(&mutex_rgt);
|
||||
}
|
||||
@@ -248,7 +248,7 @@ void *thread_top(){
|
||||
top_buffer = malloc(strlen(global_path)+1);
|
||||
memcpy(top_buffer, global_path, strlen(global_path)+1);
|
||||
top_width = strlen(top_buffer);
|
||||
/*printing of mid_content[selected_file_current].file_name is done directly in window.c window_top()*/
|
||||
/*printing of mid_dir.current_file->file_name is done directly in window.c window_top()*/
|
||||
|
||||
pthread_mutex_unlock(&mutex_top);
|
||||
}
|
||||
@@ -271,9 +271,9 @@ void *thread_btm(){
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
unsigned long i;
|
||||
float total_dir_size = 0;
|
||||
for(i = 0; i < mid_file_count; i++) {
|
||||
if ((mid_content[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) {
|
||||
total_dir_size += mid_content[i].file_size;
|
||||
for(i = 0; i < mid_dir.file_count; i++) {
|
||||
if ((mid_dir.file_list[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) {
|
||||
total_dir_size += mid_dir.file_list[i].file_size;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
@@ -347,17 +347,17 @@ void *thread_btm(){
|
||||
|
||||
memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size);
|
||||
|
||||
btm_buffer[0] = (S_ISLNK(mid_content[selected_file_current].permissions)) ? 'l':
|
||||
(S_ISDIR(mid_content[selected_file_current].permissions) ? 'd': '-');
|
||||
btm_buffer[1] = (mid_content[selected_file_current].permissions & S_IRUSR) ? 'r' : '-';
|
||||
btm_buffer[2] = (mid_content[selected_file_current].permissions & S_IWUSR) ? 'w' : '-';
|
||||
btm_buffer[3] = (mid_content[selected_file_current].permissions & S_IXUSR) ? 'x' : '-';
|
||||
btm_buffer[4] = (mid_content[selected_file_current].permissions & S_IRGRP) ? 'r' : '-';
|
||||
btm_buffer[5] = (mid_content[selected_file_current].permissions & S_IWGRP) ? 'w' : '-';
|
||||
btm_buffer[6] = (mid_content[selected_file_current].permissions & S_IXGRP) ? 'x' : '-';
|
||||
btm_buffer[7] = (mid_content[selected_file_current].permissions & S_IROTH) ? 'r' : '-';
|
||||
btm_buffer[8] = (mid_content[selected_file_current].permissions & S_IWOTH) ? 'w' : '-';
|
||||
btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-';
|
||||
btm_buffer[0] = (S_ISLNK(mid_dir.current_file->permissions)) ? 'l':
|
||||
(S_ISDIR(mid_dir.current_file->permissions) ? 'd': '-');
|
||||
btm_buffer[1] = (mid_dir.current_file->permissions & S_IRUSR) ? 'r' : '-';
|
||||
btm_buffer[2] = (mid_dir.current_file->permissions & S_IWUSR) ? 'w' : '-';
|
||||
btm_buffer[3] = (mid_dir.current_file->permissions & S_IXUSR) ? 'x' : '-';
|
||||
btm_buffer[4] = (mid_dir.current_file->permissions & S_IRGRP) ? 'r' : '-';
|
||||
btm_buffer[5] = (mid_dir.current_file->permissions & S_IWGRP) ? 'w' : '-';
|
||||
btm_buffer[6] = (mid_dir.current_file->permissions & S_IXGRP) ? 'x' : '-';
|
||||
btm_buffer[7] = (mid_dir.current_file->permissions & S_IROTH) ? 'r' : '-';
|
||||
btm_buffer[8] = (mid_dir.current_file->permissions & S_IWOTH) ? 'w' : '-';
|
||||
btm_buffer[9] = (mid_dir.current_file->permissions & S_IXOTH) ? 'x' : '-';
|
||||
|
||||
|
||||
|
||||
@@ -367,9 +367,6 @@ void *thread_btm(){
|
||||
}
|
||||
|
||||
void threading_init(){
|
||||
rgt_content = malloc(sizeof(file));
|
||||
mid_content = malloc(sizeof(file));
|
||||
lft_content = malloc(sizeof(file));
|
||||
|
||||
top_buffer = malloc(sizeof(char));
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
@@ -378,13 +375,6 @@ void threading_init(){
|
||||
memset(rgt_buffer, '\0', sizeof(char));
|
||||
memset(btm_buffer, '\0', sizeof(char));
|
||||
|
||||
mid_content->file_type = 0;
|
||||
mid_content->file_size = 0;
|
||||
mid_content->file_name = "";
|
||||
|
||||
rgt_content->file_type = 0;
|
||||
rgt_content->file_size = 0;
|
||||
rgt_content->file_name = "";
|
||||
|
||||
|
||||
volatile char vol; /* needed to make sure higher optimization steps dont move these around */
|
||||
@@ -400,13 +390,8 @@ void threading_init(){
|
||||
vol = pthread_cond_init(&cond_top, NULL);
|
||||
vol = pthread_cond_init(&cond_btm, NULL);
|
||||
vol;
|
||||
selected_file_current = 0;
|
||||
selected_file_last = 0;
|
||||
}
|
||||
void threading_free(){
|
||||
free(rgt_content);
|
||||
free(mid_content);
|
||||
free(lft_content);
|
||||
free(top_buffer);
|
||||
|
||||
pthread_mutex_destroy(&mutex_top);
|
||||
|
||||
Reference in New Issue
Block a user