propper input handling

This commit is contained in:
nova
2025-07-03 00:58:31 +02:00
parent 0a6509310d
commit f320449572
4 changed files with 74 additions and 27 deletions

View File

@ -2,6 +2,7 @@
#include <pthread.h> #include <pthread.h>
#include <dirent.h> #include <dirent.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include "defines.h" #include "defines.h"
@ -31,14 +32,43 @@ extern unsigned long mid_file_count;
extern unsigned int status; extern unsigned int status;
int read_string(WINDOW *win, int y, int x, char *str); int read_string(WINDOW *win, int y, int x, char *str);
unsigned int timeout_time = 0;
extern char *input;
unsigned int input_pass;
int parsed_input_number;
void user_interactions(char *input, WINDOW *win_b) { void user_interactions() {
void (*func_ptr)();
char ch;
move(terminal_height, terminal_width);
ch = getch();
if(ch != ERR) {
timeout(10); /* blocking timeout of getch() */
mvaddch(terminal_height-1, (terminal_width/3)*2 +input_pass, ch);
input[input_pass] = ch;
input_pass++;
}
void (*func_ptr)(int);
unsigned long i = 0; unsigned long i = 0;
unsigned int j = 0;
for (i = 0; i < binding_count; i++) { for (i = 0; i < binding_count; i++) {
if (*input == key_binding[i].key[0]) { if (strstr(input, key_binding[i].key)) {
parsed_input_number = 0;
while((*input >= '0') && (*input <= '9')) {
parsed_input_number = (parsed_input_number * 10) + (*input - '0');
input++;
}
func_ptr = key_binding[i].func; func_ptr = key_binding[i].func;
func_ptr(); func_ptr(parsed_input_number);
memset(input, 0, 255);
input_pass = 0;
timeout(100); /* blocking timeout of getch() */
} }
} }
} }
@ -54,23 +84,38 @@ void toggle_selection(){
pthread_mutex_unlock(&mutex_mid); pthread_mutex_unlock(&mutex_mid);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_down(){ void move_down(int passes){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
if (passes == 0) {
passes++;
}
/*capping the maximum file is done inside thread_mid */ /*capping the maximum file is done inside thread_mid */
selected_file_current++; selected_file_current += passes;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_up(){ void move_up(int passes){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
if (selected_file_current != 0) { if (passes == 0) {
selected_file_current--; passes++;
}
int i;
for (i = 0; i < passes; i++) {
if (selected_file_current != 0) {
selected_file_current--;
}
} }
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_right(){ void move_right(int passes){
chdir(".."); if (passes == 0) {
passes++;
}
int i;
for (i = 0; i < passes; i++) {
chdir("..");
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
} }
void move_left(){ void move_left(){
@ -118,7 +163,6 @@ void jump_top(){
} }
int read_string(WINDOW *win, int y, int x, char *str){ int read_string(WINDOW *win, int y, int x, char *str){
noecho();
curs_set(1); curs_set(1);
timeout(-1); /* negative numbers block until enter is pressed */ timeout(-1); /* negative numbers block until enter is pressed */
@ -151,7 +195,6 @@ int read_string(WINDOW *win, int y, int x, char *str){
str[pass] = '\0'; str[pass] = '\0';
timeout(10); timeout(10);
noecho();
curs_set(0); curs_set(0);
return err; return err;
@ -311,3 +354,8 @@ void delete(){
} }
} }
void makedir(){
mkdir("mk", 755); /*magic number from default permissions as created by mkdir*/
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}

View File

@ -6,12 +6,12 @@
#endif #endif
void user_interactions(char *input, WINDOW *win_b); void user_interactions();
void quit_program(); void quit_program();
void toggle_selection(); void toggle_selection();
void move_right(); void move_right(int pssses);
void move_up(); void move_up(int passes);
void move_down(); void move_down(int passes);
void move_left(); void move_left();
void jump_bottom(); void jump_bottom();
void jump_top(); void jump_top();
@ -19,3 +19,4 @@ void toggle_hidden_files();
void open_with(); void open_with();
void rename_hovered(); void rename_hovered();
void delete(); void delete();
void makedir();

15
main.c
View File

@ -17,8 +17,6 @@ unsigned int temp_width = 0;
unsigned int settings; unsigned int settings;
unsigned int file_modifiers; unsigned int file_modifiers;
unsigned int status; unsigned int status;
unsigned int timeout_time = 0;
char input = 0;
WINDOW *win_t; WINDOW *win_t;
WINDOW *win_b; WINDOW *win_b;
@ -26,6 +24,7 @@ WINDOW *win_l;
WINDOW *win_m; WINDOW *win_m;
WINDOW *win_r; WINDOW *win_r;
char *input; /*used in user_interactions*/
void render_pass(); void render_pass();
void init(); void init();
@ -82,13 +81,7 @@ int main(){
status &= ~(STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY); status &= ~(STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
if ((input = getch())) { user_interactions();
user_interactions(&input, win_b);
timeout_time = 5;
} else {
timeout_time += 10;
}
timeout(timeout_time); /* blocking timeout of getch() */
render_pass(); render_pass();
@ -169,11 +162,13 @@ void init() {
initscr(); /* start ncurses */ initscr(); /* start ncurses */
noecho(); /* hide keyboard input */ noecho(); /* hide keyboard input */
timeout(timeout_time); /* blocking timeout of getch() */ timeout(0); /* blocking timeout of getch() */
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
curs_set(0); curs_set(0);
/*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/ /*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/
input = malloc(sizeof(char)*255); /* size of input buffer, out of bounds access will not be accounted for */
memset(input, 0, 255);
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
if (getuid() == 0) { if (getuid() == 0) {
status += STATUS_USER_ROOT; status += STATUS_USER_ROOT;

View File

@ -4,6 +4,7 @@
#include "defines.h" #include "defines.h"
extern unsigned int status; extern unsigned int status;
extern char *input;
extern unsigned int timeout_time; extern unsigned int timeout_time;
extern unsigned int color_count; extern unsigned int color_count;
@ -56,7 +57,9 @@ void window_btm(WINDOW *win){
} else { } else {
mvwprintw(win, 0, 0, "%s", btm_buffer); mvwprintw(win, 0, 0, "%s", btm_buffer);
pthread_mutex_unlock(&mutex_btm); pthread_mutex_unlock(&mutex_btm);
/*the printing of the input char is done in user_interactions*/
} }
} }
void window_lft(WINDOW *win){ void window_lft(WINDOW *win){
werase(win); werase(win);