code base good enough to actually progress

This commit is contained in:
nova
2025-04-17 01:17:13 +02:00
parent 931d7026ea
commit 38338df254
13 changed files with 430 additions and 289 deletions

167
main.c
View File

@ -1,101 +1,152 @@
#define _POSIX_C_SOURCE 200809L
#include <curses.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include "threading.h"
#include "window.h"
#include "interactions.h"
#include "defines.h"
unsigned int terminal_height;
unsigned int terminal_width;
unsigned int temp_heigth = 0; //used for screen refresh
unsigned int temp_heigth = 0; /*used for screen refresh*/
unsigned int temp_width = 0;
unsigned int settings;
unsigned int file_modifiers;
unsigned int status; //bit 0 = enable
unsigned short cpu_cores; //amount of cores/threads the host system reports to have
file_data *content_l; //content of parent dir, used in left window
file_data *content_m; //content of current dir, used in main window
file_data *content_r; //content of child dir, used in right window
char *path = ".";
unsigned int status;
char input = 0;
void render_pass(WINDOW *wint, WINDOW *winb, WINDOW *winl, WINDOW *winm, WINDOW *winr);
void init();
int main() {
initscr(); //start ncurses
timeout(50); //blocking timeout of getch()
keypad(stdscr, TRUE);
init();
getmaxyx(stdscr, terminal_height, terminal_width);
WINDOW *winl = newwin(terminal_height, terminal_width/3, terminal_height, (terminal_width/3));
WINDOW *winm = newwin(terminal_height, terminal_width/3, 0, 0);
WINDOW *winr = newwin(terminal_height, terminal_width/3, terminal_height, ((terminal_width/3)*2));
WINDOW *win_t = newwin(1, terminal_width, 0, 0);
WINDOW *win_b = newwin(1, terminal_width, terminal_height-1, 0);
WINDOW *win_l = newwin(terminal_height-2, terminal_width/3, 1, 0);
WINDOW *win_m = newwin(terminal_height-2, terminal_width/3, 1, (terminal_width/3));
WINDOW *win_r = newwin(terminal_height-2, terminal_width/3, 1, ((terminal_width/3)*2));
pthread_t thread_b;
pthread_t thread_t;
pthread_t thread_l;
pthread_t thread_m;
pthread_t thread_r;
char threading = 0;
while(!(status & STATUS_QUIT_PROGRAM)){
getmaxyx(stdscr, temp_heigth, temp_width);
if (status & STATUS_RUN_BACKEND) {
pthread_t populate_l;
pthread_t populate_m;
pthread_t populate_r;
pthread_create(&populate_l, NULL, populate_dir, (void*)0); //parent_content slash win_l
pthread_create(&populate_m, NULL, populate_dir, (void*)1); //current_content slash win_m
pthread_create(&populate_r, NULL, populate_dir, (void*)2); //child_content slash win_r
pthread_join(populate_l, NULL);
pthread_join(populate_m, NULL);
pthread_join(populate_r, NULL);
status ^= STATUS_RUN_BACKEND;
status |= STATUS_UPDATE_SCREEN_0;
}
getmaxyx(stdscr, terminal_height, terminal_width);
temp_heigth -= terminal_height;
temp_width -= terminal_width;
if (!temp_heigth || !temp_width || (status & STATUS_UPDATE_SCREEN_MASK)) { //updates screen
if (status & STATUS_UPDATE_SCREEN_0) {
window_left(winl, 0, 0, content_l);
window_main(winm, 0, terminal_width/3, content_m);
window_right(winr, 0, (terminal_width/3)*2, content_r);
}
wmove(stdscr,0,0);
//status &= ~STATUS_UPDATE_SCREEN_MASK;
status = 0;
if (!(terminal_height == temp_heigth) || !(terminal_width == temp_width)) {
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RESIZE);
temp_width = terminal_width;
temp_heigth = terminal_height;
}
if (status & STATUS_RUN_BACKEND || threading) {
if (threading) {
pthread_cancel(thread_t);
pthread_cancel(thread_b);
pthread_cancel(thread_l);
pthread_cancel(thread_m);
pthread_cancel(thread_r);
threading = 0;
status &= ~STATUS_RUN_BACKEND;
status |= STATUS_UPDATE_SCREEN_0;
} else {
pthread_create(&thread_t, NULL, thread_top, win_t); /*top bar*/
pthread_create(&thread_b, NULL, thread_btm, win_b); /*bottom bar*/
pthread_create(&thread_m, NULL, thread_mid, win_m); /*parent_content slash win_l*/
pthread_create(&thread_l, NULL, thread_lft, win_l); /*current_content slash win_m*/
pthread_create(&thread_r, NULL, thread_rgt, win_r); /*child_content slash win_r*/
threading = 1;
}
}
if ((input = getch())) {
user_interactions(&input, &status, &settings);
}
render_pass(win_t, win_b, win_l, win_m, win_r);
}
free(content_l);
free(content_m);
free(content_r);
threading_free();
pthread_join(thread_l, NULL);
pthread_join(thread_r, NULL);
pthread_join(thread_m, NULL);
pthread_join(thread_t, NULL);
pthread_join(thread_b, NULL);
curs_set(1);
endwin();
refresh();
return 0;
}
//this function exists for things done at startup (initialization, reading config, etc)
void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WINDOW *win_r){
if ((status & STATUS_UPDATE_SCREEN_MASK) & STATUS_UPDATE_SCREEN_RESIZE) {
status |= STATUS_LOCK_MASK;
/*TODO: check if deallocation of window and reallocation is faster than this or not */
werase(win_t);
werase(win_b);
werase(win_l);
werase(win_m);
werase(win_r);
wresize(win_t, 1, terminal_width);
wresize(win_b, terminal_height, terminal_width/3);
wresize(win_m, terminal_height-2, terminal_width/3);
wresize(win_l, terminal_height-2, terminal_width/3);
wresize(win_r, terminal_height-2, terminal_width/3);
mvwin(win_t, 0, 0);
mvwin(win_b, terminal_height-1, 0);
mvwin(win_l, 1, 0);
mvwin(win_m, 1, (terminal_width/3));
mvwin(win_r, 1, ((terminal_width/3)*2));
status &= ~STATUS_LOCK_MASK;
status |= STATUS_UPDATE_SCREEN_0;
}
if (status & STATUS_UPDATE_SCREEN_MASK) {
status &= ~(STATUS_UPDATE_SCREEN_MASK);
window_top(win_t);
window_btm(win_b);
window_lft(win_l);
window_mid(win_m);
window_rgt(win_r);
wrefresh(win_t);
wrefresh(win_b);
wrefresh(win_l);
wrefresh(win_m);
wrefresh(win_r);
}
}
/*this function exists for things done at startup (initialization, reading config, etc)*/
void init() {
cpu_cores = get_nprocs();
//file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);
file_modifiers = FILE_MODIFIERS_SORT_BITMASK;
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
initscr(); /*start ncurses*/
noecho(); /*hide keyboard input*/
timeout(50); /*blocking timeout of getch()*/
keypad(stdscr, TRUE);
curs_set(0);
/*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
threading_init(); /* found in threading.c */
content_l = (file_data*)calloc(1, sizeof(file_data)); //allocation in order to allow a more streamlined backend
content_m = (file_data*)calloc(1, sizeof(file_data));
content_r = (file_data*)calloc(1, sizeof(file_data));
}