mirror of
https://gittea.dev/nova/th.git
synced 2025-10-21 18:30:15 -04:00
Compare commits
9 Commits
3f90802663
...
ba1e95bca3
Author | SHA1 | Date | |
---|---|---|---|
![]() |
ba1e95bca3 | ||
![]() |
922881cd45 | ||
![]() |
59964cf828 | ||
![]() |
e1c298ae50 | ||
![]() |
a57869a7a1 | ||
![]() |
520d4eca13 | ||
![]() |
963a018419 | ||
![]() |
a0a102e5f6 | ||
![]() |
0d2310cd7c |
13
dir.c
13
dir.c
@@ -320,19 +320,8 @@ void update_selected_file(){
|
||||
if (selected_file_current != selected_file_last) {
|
||||
mid_content[selected_file_last].status &= ~FILE_STATUS_HOVER;
|
||||
}
|
||||
selected_file_last = selected_file_current;
|
||||
|
||||
free(file_current->file_name);
|
||||
file_current->file_name = malloc(strlen(mid_content[selected_file_current].file_name)+1);
|
||||
strcpy(file_current->file_name, mid_content[selected_file_current].file_name);
|
||||
file_current->file_name[strlen(mid_content[selected_file_current].file_name)] = '\0';
|
||||
file_current->file_size = mid_content[selected_file_current].file_size;
|
||||
file_current->file_type = mid_content[selected_file_current].file_type;
|
||||
file_current->color_pair = mid_content[selected_file_current].color_pair;
|
||||
file_current->permissions = mid_content[selected_file_current].permissions;
|
||||
|
||||
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
|
||||
file_current->status = mid_content[selected_file_current].status;
|
||||
selected_file_last = selected_file_current;
|
||||
}
|
||||
void dir_set_selected_file_current(unsigned long selected_file_current){
|
||||
current_dir->selected_file_current = selected_file_current;
|
||||
|
@@ -43,18 +43,16 @@ char* preview_file(char *file_name, unsigned long file_size){
|
||||
|
||||
|
||||
char *mime = get_mimetype(file_name);
|
||||
images_clear();
|
||||
|
||||
if (strstr(mime, "text")) {
|
||||
file_buffer = text(file_name, &file_size);
|
||||
} else {
|
||||
} else if (strstr(mime, "image")) {
|
||||
file_buffer = generic(file_name);
|
||||
}
|
||||
|
||||
if (strstr(mime, "image")) {
|
||||
images_print(file_name);
|
||||
previewd = 1;
|
||||
} else {
|
||||
images_clear();
|
||||
} else {
|
||||
file_buffer = generic(file_name);
|
||||
}
|
||||
|
||||
free(mime);
|
||||
@@ -63,10 +61,14 @@ char* preview_file(char *file_name, unsigned long file_size){
|
||||
}
|
||||
char* text(char *path, unsigned long *file_size){
|
||||
|
||||
char *file_buffer = malloc(*file_size + 1);
|
||||
unsigned long size = (terminal_width/2) * terminal_height;
|
||||
if (size > *file_size) {
|
||||
size = *file_size;
|
||||
}
|
||||
char *file_buffer = malloc(size + 1);
|
||||
FILE *fp = fopen(path, "r");
|
||||
if (fread(file_buffer, *file_size, 1, fp) != 0) {
|
||||
file_buffer[*file_size] = '\0';
|
||||
if (fread(file_buffer, size, 1, fp) != 0) {
|
||||
file_buffer[size] = '\0';
|
||||
return file_buffer;
|
||||
} else {
|
||||
return "failed reading file";
|
||||
|
@@ -20,7 +20,6 @@ extern pthread_mutex_t mutex_mid;
|
||||
extern file *mid_content;
|
||||
extern file *lft_content;
|
||||
extern file *rgt_content;
|
||||
extern volatile file *file_current;
|
||||
|
||||
extern unsigned int terminal_height;
|
||||
extern unsigned int terminal_width;
|
||||
@@ -143,14 +142,18 @@ int read_string(WINDOW *win, int y, int x, char *str){
|
||||
if (ch == '\n') {
|
||||
err = 0;
|
||||
break;
|
||||
} else if (ch == 27) { /* esc key */
|
||||
err = 1;
|
||||
break;
|
||||
} else if (ch == '\t') { /* tab */
|
||||
memcpy(str + pass, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name));
|
||||
mvwaddstr(win, y, x +pass, mid_content[selected_file_current].file_name);
|
||||
pass += strlen(mid_content[selected_file_current].file_name);
|
||||
} else if (ch == 127) { /* backspace */
|
||||
if (pass > 0) {
|
||||
pass--;
|
||||
mvwdelch(win, y, pass);
|
||||
}
|
||||
} else if (ch == 27) { /* esc key */
|
||||
err = 1;
|
||||
break;
|
||||
} else {
|
||||
mvwaddch(win, y, x +pass, ch);
|
||||
str[pass] = ch;
|
||||
@@ -184,7 +187,6 @@ void toggle_selection(){
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
mid_content[selected_file_current].status ^= FILE_STATUS_SELECTED;
|
||||
file_current->status ^= FILE_STATUS_SELECTED;
|
||||
status |= (STATUS_UPDATE_SCREEN_MASK);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
@@ -235,8 +237,8 @@ void move_left(int passes){
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
||||
}
|
||||
void move_right(){
|
||||
if (file_current->file_type == FILE_TYPE_DIR || file_current->file_type == FILE_TYPE_SYMLINK) {
|
||||
if (chdir(file_current->file_name) != 0) {
|
||||
if (mid_content[selected_file_current].file_type == FILE_TYPE_DIR || mid_content[selected_file_current].file_type == FILE_TYPE_SYMLINK) {
|
||||
if (chdir(mid_content[selected_file_current].file_name) != 0) {
|
||||
FAIL("move_right", "unhandled error of chdir");
|
||||
} else {
|
||||
selected_file_current = dir_get_selected_file_current();
|
||||
@@ -244,12 +246,12 @@ void move_right(){
|
||||
} else {
|
||||
unsigned long i = 0;
|
||||
char match = 0;
|
||||
char *mime = get_mimetype(file_current->file_name);
|
||||
char *extension = strrchr(file_current->file_name, '.');
|
||||
char *mime = get_mimetype(mid_content[selected_file_current].file_name);
|
||||
char *extension = strrchr(mid_content[selected_file_current].file_name, '.');
|
||||
for (i = 0; i < file_extension_default_count; i++) {
|
||||
if (strstr(extension, file_extension_default_cmd[i].file_extension)) {
|
||||
char *cmd = concat(file_extension_default_cmd[i].command, " ./\"");
|
||||
cmd = concat(cmd, file_current->file_name);
|
||||
cmd = concat(cmd, mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\"");
|
||||
|
||||
|
||||
@@ -267,7 +269,7 @@ void move_right(){
|
||||
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
|
||||
|
||||
char *cmd = concat(mimetype_default_cmd[i].command, " ./\"");
|
||||
cmd = concat(cmd, file_current->file_name);
|
||||
cmd = concat(cmd, mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\"");
|
||||
btm_buffer = malloc(strlen(cmd));
|
||||
|
||||
@@ -311,7 +313,7 @@ void jump_top(){
|
||||
}
|
||||
|
||||
void open_with(){
|
||||
btm_buffer = concat("open \"", file_current->file_name);
|
||||
btm_buffer = concat("open \"", mid_content[selected_file_current].file_name);
|
||||
btm_buffer = concat(btm_buffer, "\" with:");
|
||||
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
@@ -332,7 +334,7 @@ void open_with(){
|
||||
|
||||
if (!err) {
|
||||
char *cmd = concat(str, " ./\"");
|
||||
cmd = concat(cmd, file_current->file_name);
|
||||
cmd = concat(cmd, mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\"");
|
||||
|
||||
if (system(cmd) == -1) {
|
||||
@@ -351,7 +353,7 @@ void open_with(){
|
||||
|
||||
void rename_hovered(){
|
||||
|
||||
btm_buffer = concat("rename \"", file_current->file_name);
|
||||
btm_buffer = concat("rename \"", mid_content[selected_file_current].file_name);
|
||||
btm_buffer = concat(btm_buffer, "\" to:");
|
||||
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
@@ -371,7 +373,7 @@ void rename_hovered(){
|
||||
|
||||
|
||||
if (!err) {
|
||||
char *cmd = concat("mv ./\"", file_current->file_name);
|
||||
char *cmd = concat("mv ./\"", mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\" ./\"");
|
||||
cmd = concat(cmd, str);
|
||||
cmd = concat(cmd, "\"");
|
||||
@@ -405,7 +407,7 @@ void delete(){
|
||||
if (hits) {
|
||||
btm_buffer = concat("delete:", file_str);
|
||||
} else {
|
||||
btm_buffer = concat("delete: \"", file_current->file_name);
|
||||
btm_buffer = concat("delete: \"", mid_content[selected_file_current].file_name);
|
||||
btm_buffer = concat(btm_buffer, "\"");
|
||||
}
|
||||
|
||||
@@ -444,13 +446,13 @@ void delete(){
|
||||
btm_buffer = concat("deleted: ", file_str);
|
||||
} else {
|
||||
free(btm_buffer);
|
||||
error = remove(file_current->file_name);
|
||||
error = remove(mid_content[selected_file_current].file_name);
|
||||
if (error != 0) {
|
||||
mvaddstr(terminal_height-2, 0, "could not delete: " );
|
||||
mvaddstr(terminal_height-2, strlen("could not delete: "), mid_content[i].file_name);
|
||||
btm_buffer = " ";
|
||||
} else {
|
||||
btm_buffer = concat("deleted: \"", file_current->file_name);
|
||||
btm_buffer = concat("deleted: \"", mid_content[selected_file_current].file_name);
|
||||
btm_buffer = concat(btm_buffer, "\"");
|
||||
}
|
||||
|
||||
@@ -621,7 +623,7 @@ void cmd_on_selected(int passes, int index){
|
||||
btm_buffer = concat(key_binding[index].black_magic, file_str);
|
||||
} else {
|
||||
btm_buffer = concat(key_binding[index].black_magic, "\"");
|
||||
btm_buffer = concat(btm_buffer, file_current->file_name);
|
||||
btm_buffer = concat(btm_buffer, mid_content[selected_file_current].file_name);
|
||||
btm_buffer = concat(btm_buffer, "\"");
|
||||
}
|
||||
|
||||
@@ -663,7 +665,7 @@ void cmd_on_selected(int passes, int index){
|
||||
free(btm_buffer);
|
||||
free(cmd);
|
||||
cmd = concat((char*)key_binding[index].black_magic, " \"");
|
||||
cmd = concat(cmd, file_current->file_name);
|
||||
cmd = concat(cmd, mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\"");
|
||||
if (system(cmd) != 0) {
|
||||
/*do nothing*/
|
||||
@@ -782,8 +784,10 @@ void search(){
|
||||
ch = wgetch(win_b);
|
||||
if (ch == '\n') {
|
||||
break;
|
||||
} else if (ch == 27) { /* esc key */
|
||||
break;
|
||||
} else if (ch == '\t') { /* tab */
|
||||
memcpy(search_buffer, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name));
|
||||
mvwaddstr(win_b, local_height-1, pass, mid_content[selected_file_current].file_name);
|
||||
pass = strlen(mid_content[selected_file_current].file_name);
|
||||
} else if (ch == 127) { /* backspace */
|
||||
mvwdelch(win_b, local_height-1, 1);
|
||||
wdelch(win_b);
|
||||
@@ -791,6 +795,8 @@ void search(){
|
||||
search_buffer[pass-1] = '\0';
|
||||
pass--;
|
||||
}
|
||||
} else if (ch == 27) { /* esc key */
|
||||
break;
|
||||
} else {
|
||||
search_buffer[pass] = ch;
|
||||
pass++;
|
||||
@@ -811,6 +817,8 @@ void search(){
|
||||
timeout(10);
|
||||
curs_set(0);
|
||||
|
||||
dir_set_selected_file_current(selected_file_current);
|
||||
|
||||
update_selected_file();
|
||||
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
|
||||
|
25
main.c
25
main.c
@@ -59,6 +59,11 @@ int main(){
|
||||
char threading = 0;
|
||||
terminal_width_empty_line = malloc(terminal_width);
|
||||
|
||||
pthread_create(&thread_t, NULL, thread_top, &status); /*top bar*/
|
||||
pthread_create(&thread_l, NULL, thread_lft, &status); /*parent_content slash win_l*/
|
||||
pthread_create(&thread_m, NULL, thread_mid, &status); /*current_content slash win_m*/
|
||||
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
|
||||
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/
|
||||
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
getmaxyx(stdscr, terminal_height, terminal_width);
|
||||
@@ -68,28 +73,14 @@ int main(){
|
||||
temp_width = terminal_width;
|
||||
temp_heigth = terminal_height;
|
||||
}
|
||||
if (status & STATUS_RUN_BACKEND && threading) {
|
||||
pthread_cancel(thread_b);
|
||||
pthread_cancel(thread_r);
|
||||
pthread_cancel(thread_m);
|
||||
pthread_cancel(thread_l);
|
||||
pthread_cancel(thread_t);
|
||||
}
|
||||
if (threading) {
|
||||
status &= ~(STATUS_RELOAD_DIRECTORY);
|
||||
pthread_join(thread_t, NULL);
|
||||
pthread_join(thread_l, NULL);
|
||||
pthread_join(thread_m, NULL);
|
||||
pthread_join(thread_b, NULL);
|
||||
pthread_join(thread_r, NULL);
|
||||
threading = 0;
|
||||
}
|
||||
if (status & STATUS_RUN_BACKEND) {
|
||||
pthread_create(&thread_t, NULL, thread_top, &status); /*top bar*/
|
||||
pthread_create(&thread_l, NULL, thread_lft, &status); /*parent_content slash win_l*/
|
||||
pthread_create(&thread_m, NULL, thread_mid, &status); /*current_content slash win_m*/
|
||||
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/
|
||||
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
|
||||
pthread_cond_signal(&cond_top);
|
||||
pthread_cond_signal(&cond_mid);
|
||||
pthread_cond_signal(&cond_btm);
|
||||
status &= ~(STATUS_RUN_BACKEND);
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
threading = 1;
|
||||
|
359
threading.c
359
threading.c
@@ -18,9 +18,12 @@ pthread_mutex_t mutex_lft;
|
||||
pthread_mutex_t mutex_mid;
|
||||
pthread_mutex_t mutex_rgt;
|
||||
pthread_mutex_t mutex_selection;
|
||||
pthread_mutex_t mutex_wait;
|
||||
pthread_cond_t cond_wait;
|
||||
volatile char wait_count; /* this is used to determine how many threads are waiting for cont_wait */
|
||||
pthread_cond_t cond_mid;
|
||||
pthread_cond_t cond_rgt;
|
||||
pthread_cond_t cond_lft;
|
||||
pthread_cond_t cond_top;
|
||||
pthread_cond_t cond_btm;
|
||||
|
||||
|
||||
file *rgt_content;
|
||||
file *mid_content;
|
||||
@@ -29,7 +32,6 @@ char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used f
|
||||
char *btm_buffer;
|
||||
char *top_buffer; /* current path */
|
||||
|
||||
volatile file *file_current;
|
||||
|
||||
|
||||
unsigned long rgt_file_count = 0;
|
||||
@@ -42,219 +44,218 @@ 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;
|
||||
|
||||
|
||||
void *thread_mid(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
pthread_cond_wait(&cond_mid, &mutex_mid);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
mid_content = malloc(sizeof(file));
|
||||
mid_content->file_name = "cannot open directory";
|
||||
mid_file_count = 1;
|
||||
} else {
|
||||
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
mid_content = malloc(sizeof(file));
|
||||
mid_content->file_name = "cannot open directory";
|
||||
mid_file_count = 1;
|
||||
} else {
|
||||
|
||||
|
||||
if (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->status = FILE_STATUS_DIR_EMPTY;
|
||||
mid_content->file_type = 0;
|
||||
mid_content->file_size = 0;
|
||||
mid_content->permissions = 0;
|
||||
mid_content->color_pair = 0;
|
||||
mid_content->file_name = "empty dir";
|
||||
|
||||
file_current->file_name = mid_content->file_name;
|
||||
file_current->file_size = mid_content->file_size;
|
||||
file_current->file_type = mid_content->file_type;
|
||||
file_current->color_pair = mid_content->color_pair;
|
||||
file_current->permissions = mid_content->permissions;
|
||||
file_current->status = mid_content->status;
|
||||
mid_file_count = 0;
|
||||
|
||||
while(wait_count < 2){
|
||||
/*wait for thread_rgt and thread_btm to lock*/
|
||||
if (status & STATUS_RELOAD_DIRECTORY) {
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < mid_file_count; i++) {
|
||||
free(mid_content[i].file_name);
|
||||
}
|
||||
pthread_mutex_lock(&mutex_wait);
|
||||
pthread_cond_broadcast(&cond_wait);
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
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->status = FILE_STATUS_DIR_EMPTY;
|
||||
mid_content->file_type = 0;
|
||||
mid_content->file_size = 0;
|
||||
mid_content->permissions = 0;
|
||||
mid_content->color_pair = 0;
|
||||
mid_content->file_name = "empty dir";
|
||||
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
pthread_exit(0);
|
||||
mid_file_count = 0;
|
||||
|
||||
pthread_cond_signal(&cond_rgt);
|
||||
pthread_cond_signal(&cond_lft);
|
||||
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
pthread_exit(0);
|
||||
}
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
update_selected_file();
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
}
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
update_selected_file();
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
|
||||
|
||||
pthread_cond_signal(&cond_rgt);
|
||||
pthread_cond_signal(&cond_lft);
|
||||
|
||||
}
|
||||
|
||||
|
||||
while(wait_count < 2){
|
||||
/*wait for thread_rgt and thread_btm to lock*/
|
||||
}
|
||||
pthread_mutex_lock(&mutex_wait);
|
||||
pthread_cond_broadcast(&cond_wait);
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
}
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
pthread_exit(0);
|
||||
}
|
||||
void *thread_lft(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
pthread_mutex_lock(&mutex_lft);
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
lft_content = malloc(sizeof(file));
|
||||
lft_content[0].file_name = "cannot open directory";
|
||||
lft_file_count = 1;
|
||||
} else {
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_lft);
|
||||
pthread_cond_wait(&cond_lft, &mutex_lft);
|
||||
|
||||
path[strrchr(path, '/')-path] = '\0';
|
||||
path[0] = '/';
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
lft_content = malloc(sizeof(file));
|
||||
lft_content[0].file_name = "cannot open directory";
|
||||
lft_file_count = 1;
|
||||
} else {
|
||||
|
||||
path[strrchr(path, '/')-path] = '\0';
|
||||
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);
|
||||
}
|
||||
|
||||
if (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);
|
||||
}
|
||||
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
}
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
pthread_exit(0);
|
||||
|
||||
|
||||
}
|
||||
void *thread_rgt(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
(void)status;
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
(void)local_status;
|
||||
|
||||
pthread_mutex_lock(&mutex_rgt);
|
||||
pthread_mutex_lock(&mutex_wait);
|
||||
wait_count++;
|
||||
pthread_cond_wait(&cond_wait, &mutex_wait);
|
||||
wait_count--;
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
file file_current;
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_rgt);
|
||||
pthread_cond_wait(&cond_rgt, &mutex_rgt);
|
||||
|
||||
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
char *path = malloc(strlen(file_current->file_name) + 1);
|
||||
strcpy(path, file_current->file_name);
|
||||
unsigned char file_current_type = file_current->file_type;
|
||||
unsigned long file_current_size = file_current->file_size;
|
||||
char file_current_status = file_current->status;
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
char *path = malloc(strlen(mid_content[selected_file_current].file_name) + 1);
|
||||
strcpy(path, mid_content[selected_file_current].file_name);
|
||||
file_current.file_type = mid_content[selected_file_current].file_type;
|
||||
file_current.file_size = mid_content[selected_file_current].file_size;
|
||||
file_current.status = mid_content[selected_file_current].status;
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
|
||||
if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) {
|
||||
images_clear();
|
||||
if (file_current.file_type == FILE_TYPE_DIR || file_current.file_type == FILE_TYPE_SYMLINK) {
|
||||
images_clear();
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_file_count; i++) {
|
||||
free(rgt_content[i].file_name);
|
||||
}
|
||||
free(rgt_content);
|
||||
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(path);
|
||||
rgt_content = malloc(rgt_file_count * sizeof(file));
|
||||
memset(rgt_content, '\0', rgt_file_count * sizeof(file));
|
||||
get_dir_content(path, &rgt_file_count, rgt_content);
|
||||
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
|
||||
rgt_file_count = get_dir_size(path);
|
||||
rgt_content = malloc(rgt_file_count * sizeof(file));
|
||||
memset(rgt_content, '\0', rgt_file_count * sizeof(file));
|
||||
get_dir_content(path, &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';
|
||||
} else {
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_file_count; i++) {
|
||||
free(rgt_content[i].file_name);
|
||||
}
|
||||
free(rgt_content);
|
||||
rgt_file_count = 0;
|
||||
rgt_content = malloc(sizeof(file));
|
||||
|
||||
free(rgt_buffer);
|
||||
if (file_current_status & FILE_STATUS_DIR_EMPTY) {
|
||||
rgt_buffer = "empty dir";
|
||||
free(rgt_buffer);
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
rgt_buffer[0] = '\0';
|
||||
} else {
|
||||
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
|
||||
rgt_content->status = FILE_STATUS_HOVER;
|
||||
rgt_buffer = preview_file(path, file_current_size);
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
|
||||
pthread_mutex_unlock(&mutex_rgt);
|
||||
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);
|
||||
if (file_current.status & FILE_STATUS_DIR_EMPTY) {
|
||||
rgt_buffer = "empty dir";
|
||||
} else {
|
||||
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
|
||||
rgt_content->status = FILE_STATUS_HOVER;
|
||||
rgt_buffer = preview_file(path, file_current.file_size);
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
|
||||
pthread_mutex_unlock(&mutex_rgt);
|
||||
}
|
||||
pthread_exit(0);
|
||||
}
|
||||
void *thread_top(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
(void)status;
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
(void)local_status;
|
||||
|
||||
pthread_mutex_lock(&mutex_top);
|
||||
free(top_buffer);
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_top);
|
||||
pthread_cond_wait(&cond_top, &mutex_top);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
top_buffer = malloc(sizeof("cannot open directory"));
|
||||
top_width = sizeof("cannot open directory");
|
||||
top_buffer = "cannot open directory";
|
||||
} else {
|
||||
top_buffer = getcwd(NULL, 0);
|
||||
top_width = strlen(top_buffer);
|
||||
free(top_buffer);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
top_buffer = malloc(sizeof("cannot open directory"));
|
||||
top_width = sizeof("cannot open directory");
|
||||
top_buffer = "cannot open directory";
|
||||
} else {
|
||||
top_buffer = getcwd(NULL, 0);
|
||||
top_width = strlen(top_buffer);
|
||||
}
|
||||
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_top);
|
||||
}
|
||||
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_top);
|
||||
pthread_exit(0);
|
||||
}
|
||||
void *thread_btm(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
(void)status;
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
(void)local_status;
|
||||
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
pthread_mutex_lock(&mutex_wait);
|
||||
wait_count++;
|
||||
pthread_cond_wait(&cond_wait, &mutex_wait);
|
||||
wait_count--;
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
|
||||
free(btm_buffer);
|
||||
int buffer_width = terminal_width;
|
||||
btm_buffer = malloc(buffer_width);
|
||||
memset(btm_buffer, 0, buffer_width);
|
||||
btm_buffer[0] = (S_ISDIR(file_current->permissions)) ? 'd' : '-';
|
||||
btm_buffer[1] = (file_current->permissions & S_IRUSR) ? 'r' : '-';
|
||||
btm_buffer[2] = (file_current->permissions & S_IWUSR) ? 'w' : '-';
|
||||
btm_buffer[3] = (file_current->permissions & S_IXUSR) ? 'x' : '-';
|
||||
btm_buffer[4] = (file_current->permissions & S_IRGRP) ? 'r' : '-';
|
||||
btm_buffer[5] = (file_current->permissions & S_IWGRP) ? 'w' : '-';
|
||||
btm_buffer[6] = (file_current->permissions & S_IXGRP) ? 'x' : '-';
|
||||
btm_buffer[7] = (file_current->permissions & S_IROTH) ? 'r' : '-';
|
||||
btm_buffer[8] = (file_current->permissions & S_IWOTH) ? 'w' : '-';
|
||||
btm_buffer[9] = (file_current->permissions & S_IXOTH) ? 'x' : '-';
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
pthread_cond_wait(&cond_btm, &mutex_btm);
|
||||
|
||||
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
free(btm_buffer);
|
||||
int buffer_width = terminal_width;
|
||||
btm_buffer = malloc(buffer_width);
|
||||
memset(btm_buffer, 0, buffer_width);
|
||||
btm_buffer[0] = (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' : '-';
|
||||
|
||||
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
}
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
@@ -280,11 +281,6 @@ void threading_init(){
|
||||
rgt_content->file_name = malloc(sizeof(char));
|
||||
rgt_content->file_name[0] = '\0';
|
||||
|
||||
file_current = malloc(sizeof(file));
|
||||
file_current->file_type = 0;
|
||||
file_current->file_size = 0;
|
||||
file_current->file_name = malloc(sizeof(char));
|
||||
file_current->file_name[0] = '\0';
|
||||
|
||||
volatile char vol; /* needed to make sure higher optimization steps dont move these around */
|
||||
vol = pthread_mutex_init(&mutex_top, NULL);
|
||||
@@ -293,8 +289,11 @@ void threading_init(){
|
||||
vol = pthread_mutex_init(&mutex_btm, NULL);
|
||||
vol = pthread_mutex_init(&mutex_rgt, NULL);
|
||||
vol = pthread_mutex_init(&mutex_selection, NULL);
|
||||
vol = pthread_mutex_init(&mutex_wait, NULL);
|
||||
vol = pthread_cond_init(&cond_wait, NULL);
|
||||
vol = pthread_cond_init(&cond_rgt, NULL);
|
||||
vol = pthread_cond_init(&cond_lft, NULL);
|
||||
vol = pthread_cond_init(&cond_mid, NULL);
|
||||
vol = pthread_cond_init(&cond_top, NULL);
|
||||
vol = pthread_cond_init(&cond_btm, NULL);
|
||||
vol;
|
||||
selected_file_current = 0;
|
||||
selected_file_last = 0;
|
||||
|
31
window.c
31
window.c
@@ -5,6 +5,8 @@
|
||||
#include "defines.h"
|
||||
#include "dir.h"
|
||||
|
||||
extern unsigned int terminal_height;
|
||||
extern unsigned int terminal_width;
|
||||
extern unsigned int status;
|
||||
extern char *input;
|
||||
|
||||
@@ -42,15 +44,13 @@ void window_top(WINDOW *win){
|
||||
wattroff(win, COLOR_PAIR(COLOR_PATH));
|
||||
pthread_mutex_unlock(&mutex_top);
|
||||
} else {
|
||||
wprintw(win,"loading");
|
||||
mvwaddstr(win, 0, terminal_width/2, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_btm(WINDOW *win){
|
||||
werase(win);
|
||||
unsigned long local_width;
|
||||
unsigned long local_height;
|
||||
getmaxyx(win, local_height, local_width);
|
||||
|
||||
if (pthread_mutex_trylock(&mutex_btm) == 0) {
|
||||
if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/
|
||||
mvwprintw(win, 0, 0, "%s", btm_buffer);
|
||||
@@ -59,55 +59,46 @@ void window_btm(WINDOW *win){
|
||||
/*the printing of the input char is done in user_interactions*/
|
||||
/*the printing of all possible inputs are done in user_interactions */
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, 0, terminal_width/2, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_lft(WINDOW *win){
|
||||
werase(win);
|
||||
|
||||
unsigned long local_width;
|
||||
unsigned long local_height;
|
||||
getmaxyx(win, local_height, local_width);
|
||||
if (pthread_mutex_trylock(&mutex_lft) == 0) {
|
||||
print_dir(win, 0, &lft_file_count, lft_content);
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, terminal_height/2, terminal_width/8, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_mid(WINDOW *win){
|
||||
werase(win);
|
||||
|
||||
unsigned long local_width;
|
||||
unsigned long local_height;
|
||||
getmaxyx(win, local_height, local_width);
|
||||
if (pthread_mutex_trylock(&mutex_mid) == 0) {
|
||||
if (mid_file_count == 0) {
|
||||
mvwprintw(win, 0, 0, "empty");
|
||||
mvwaddstr(win, 0, 0, "empty");
|
||||
} else {
|
||||
print_dir(win, 1, &mid_file_count, mid_content);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_rgt(WINDOW *win){
|
||||
werase(win);
|
||||
|
||||
unsigned long local_width;
|
||||
unsigned long local_height;
|
||||
getmaxyx(win, local_height, local_width);
|
||||
if (pthread_mutex_trylock(&mutex_rgt) == 0) {
|
||||
if (rgt_file_count == 0) {
|
||||
if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) {
|
||||
mvwprintw(win, 0, 0, "%s", rgt_buffer);
|
||||
mvwaddnstr(win, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width);
|
||||
} else {
|
||||
mvwprintw(win, 0, 0, "empty");
|
||||
mvwaddstr(win, 0, 0, "empty");
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +107,7 @@ void window_rgt(WINDOW *win){
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_rgt);
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user