228 lines
5.4 KiB
C
228 lines
5.4 KiB
C
#include <curses.h>
|
|
#include <pthread.h>
|
|
#include <dirent.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include "defines.h"
|
|
#include "config.h"
|
|
|
|
|
|
extern unsigned int file_modifiers;
|
|
unsigned long selected_file_current;
|
|
unsigned long selected_file_last;
|
|
extern pthread_mutex_t mutex_selection;
|
|
extern pthread_mutex_t mutex_rgt;
|
|
extern pthread_mutex_t mutex_mid;
|
|
extern file *mid_content;
|
|
extern file *lft_content;
|
|
extern file *rgt_content;
|
|
extern file file_current;
|
|
|
|
|
|
extern char *rgt_buffer;
|
|
extern char *btm_buffer;
|
|
|
|
extern unsigned int status;
|
|
void open_with_pass_2();
|
|
void rename_hovered_pass_2();
|
|
int read_string(WINDOW *win, int y, int x, char *str);
|
|
|
|
void user_interactions(char *input, WINDOW *win_b) {
|
|
void (*func_ptr)();
|
|
unsigned long i = 0;
|
|
if (status & STATUS_INTERACTIONS_MASK) {
|
|
if (status & STATUS_INTERACTIONS_OPEN_WITH) {
|
|
open_with_pass_2(win_b);
|
|
} else if (status & STATUS_INTERACTIONS_RENAME) {
|
|
rename_hovered_pass_2(win_b);
|
|
}
|
|
|
|
}
|
|
for (i = 0; i < binding_count; i++) {
|
|
if (*input == key_binding[i].key[0]) {
|
|
func_ptr = key_binding[i].func;
|
|
func_ptr();
|
|
}
|
|
}
|
|
}
|
|
void quit_program(){
|
|
status = STATUS_QUIT_PROGRAM;
|
|
}
|
|
void move_down(){
|
|
pthread_mutex_lock(&mutex_selection);
|
|
/*capping the maximum file is done inside thread_mid */
|
|
selected_file_current++;
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
|
pthread_mutex_unlock(&mutex_selection);
|
|
}
|
|
void move_up(){
|
|
pthread_mutex_lock(&mutex_selection);
|
|
if (selected_file_current != 0) {
|
|
selected_file_current--;
|
|
}
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
|
pthread_mutex_unlock(&mutex_selection);
|
|
}
|
|
void move_right(){
|
|
chdir("..");
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
|
}
|
|
void move_left(){
|
|
if (file_current.file_type == FILE_TYPE_DIR || file_current.file_type == FILE_TYPE_SYMLINK) {
|
|
chdir(file_current.file_name);
|
|
} else {
|
|
unsigned long i = 0;
|
|
char *mime = get_mimetype(file_current.file_name);
|
|
for (i = 0; i < mimetype_default_count; i++) {
|
|
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, "\"");
|
|
btm_buffer = malloc(strlen(cmd));
|
|
|
|
strcpy(btm_buffer, cmd);
|
|
|
|
|
|
system(cmd);
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
|
break;
|
|
|
|
}
|
|
}
|
|
free(mime);
|
|
}
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
|
}
|
|
void toggle_hidden_files(){
|
|
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
|
}
|
|
void jump_bottom(){
|
|
pthread_mutex_lock(&mutex_selection);
|
|
selected_file_current = 0 - 1;
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
|
pthread_mutex_unlock(&mutex_selection);
|
|
}
|
|
void jump_top(){
|
|
pthread_mutex_lock(&mutex_selection);
|
|
selected_file_current = 0;
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
|
pthread_mutex_unlock(&mutex_selection);
|
|
}
|
|
|
|
int read_string(WINDOW *win, int y, int x, char *str){
|
|
noecho();
|
|
curs_set(1);
|
|
|
|
timeout(-1); /* negative numbers block until enter is pressed */
|
|
|
|
unsigned int pass = 0;
|
|
char ch;
|
|
char err = 0;
|
|
|
|
wmove(win, y, x);
|
|
while(1) {
|
|
/*ch = mvwgetch(win, y, x + pass);*/
|
|
ch = wgetch(win);
|
|
if (ch == '\n') {
|
|
err = 0;
|
|
break;
|
|
} else if (ch == 27) { /* esc key */
|
|
err = 1;
|
|
break;
|
|
} else if (ch == 127) { /* backspace */
|
|
if (pass > 0) {
|
|
pass--;
|
|
mvwdelch(win, y, pass);
|
|
}
|
|
} else {
|
|
mvwaddch(win, y, x +pass, ch);
|
|
str[pass] = ch;
|
|
pass++;
|
|
}
|
|
}
|
|
str[pass] = '\0';
|
|
|
|
timeout(10);
|
|
noecho();
|
|
curs_set(0);
|
|
|
|
return err;
|
|
}
|
|
void open_with(){
|
|
btm_buffer = concat("open \"", file_current.file_name);
|
|
btm_buffer = concat(btm_buffer, "\" with:");
|
|
status |= (STATUS_UPDATE_SCREEN_0 | STATUS_INTERACTIONS_OPEN_WITH);
|
|
|
|
}
|
|
void open_with_pass_2(WINDOW *win_b){
|
|
|
|
unsigned long local_width;
|
|
unsigned long local_height;
|
|
getmaxyx(win_b, local_height, local_width);
|
|
|
|
|
|
/* TODO(2025-06-22T01:24:36) fix fixed buffer size */
|
|
char *str = malloc(255);
|
|
memset(str, ' ', 255);
|
|
int err = read_string(win_b, local_height - 1, 0 , str);
|
|
|
|
|
|
if (!err) {
|
|
char *cmd = concat(str, " ./\"");
|
|
cmd = concat(cmd, file_current.file_name);
|
|
cmd = concat(cmd, "\"");
|
|
|
|
system(cmd);
|
|
|
|
free(btm_buffer);
|
|
btm_buffer = cmd;
|
|
|
|
}
|
|
|
|
status &= ~(STATUS_INTERACTIONS_MASK);
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
|
|
|
free(str);
|
|
}
|
|
|
|
void rename_hovered(){
|
|
|
|
btm_buffer = concat("rename \"", file_current.file_name);
|
|
btm_buffer = concat(btm_buffer, "\" to:");
|
|
status |= (STATUS_UPDATE_SCREEN_0 | STATUS_INTERACTIONS_RENAME);
|
|
|
|
}
|
|
|
|
void rename_hovered_pass_2(WINDOW *win_b){
|
|
|
|
unsigned long local_width;
|
|
unsigned long local_height;
|
|
getmaxyx(win_b, local_height, local_width);
|
|
|
|
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */
|
|
char *str = malloc(255);
|
|
memset(str, ' ', 255);
|
|
int err = read_string(win_b, local_height - 1, 0, str);
|
|
|
|
|
|
if (!err) {
|
|
char *cmd = concat("mv ./\"", file_current.file_name);
|
|
cmd = concat(cmd, "\" ./\"");
|
|
cmd = concat(cmd, str);
|
|
cmd = concat(cmd, "\"");
|
|
|
|
system(cmd);
|
|
btm_buffer = cmd;
|
|
}
|
|
|
|
status &= ~(STATUS_INTERACTIONS_MASK);
|
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
|
|
|
free(btm_buffer);
|
|
|
|
free(str);
|
|
}
|