code base good enough to actually progress
This commit is contained in:
parent
931d7026ea
commit
38338df254
4
Makefile
4
Makefile
@ -1,5 +1,5 @@
|
|||||||
all:
|
all:
|
||||||
gcc ./main.c -std=c99 -o th -lncurses -ltinfo -Wall
|
gcc ./main.c -std=c89 -o th -lncurses -ltinfo -Wall
|
||||||
|
|
||||||
d:
|
d:
|
||||||
gcc -g -std=c99 ./main.c -o th -lncurses -ltinfo -Wall
|
gcc -g -std=c89 ./main.c -o th -lncurses -ltinfo -Wall && gdb --tui ./th
|
||||||
|
119
backend.c
119
backend.c
@ -1,100 +1,75 @@
|
|||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <string.h>
|
|
||||||
#include <strings.h>
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "sorting.h"
|
#include "sorting.h"
|
||||||
|
|
||||||
extern unsigned int settings;
|
extern unsigned int settings;
|
||||||
extern unsigned int file_modifiers;
|
extern unsigned int file_modifiers;
|
||||||
extern file_data *content_l;
|
|
||||||
extern file_data *content_m;
|
|
||||||
extern file_data *content_r;
|
|
||||||
extern char *path;
|
|
||||||
|
|
||||||
unsigned long file_count_l;
|
|
||||||
unsigned long file_count_m;
|
|
||||||
unsigned long file_count_r;
|
|
||||||
|
|
||||||
|
|
||||||
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name){
|
void get_dir_size(char *path, unsigned long *dir_length_width, unsigned long *dir_width){
|
||||||
DIR *dir = opendir(path);
|
|
||||||
*longest_name = 256; //magic number originates out of readdir(), unless i implement my own name size function, thisll do
|
|
||||||
unsigned long index = 0;
|
|
||||||
if (dir) {
|
|
||||||
struct dirent *entry;
|
|
||||||
while ( (entry=readdir(dir)) ) {
|
|
||||||
if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
|
||||||
index++;
|
|
||||||
} else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES){
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*file_count = index;
|
|
||||||
closedir(dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, file_data *dir_content){
|
|
||||||
DIR *dir = opendir(path);
|
DIR *dir = opendir(path);
|
||||||
if (dir) {
|
if (dir) {
|
||||||
unsigned long index = 0;
|
unsigned long entry_count = 0;
|
||||||
|
unsigned long max_length;
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
while ((entry=readdir(dir))) {
|
while ((entry=readdir(dir))) {
|
||||||
if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||||
dir_content[index].file_name = entry->d_name;
|
unsigned int current_length = 0;
|
||||||
dir_content[index].file_type = entry->d_type;
|
unsigned int i = 0;
|
||||||
index++;
|
for (; entry->d_name[i] != '\0'; i++) {
|
||||||
} else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
|
current_length++;
|
||||||
dir_content[index].file_name = entry->d_name;
|
}
|
||||||
dir_content[index].file_type = entry->d_type;
|
if (current_length > max_length) {
|
||||||
index++;
|
/*dynamic filename length to save on memory*/
|
||||||
|
max_length = current_length;
|
||||||
|
}
|
||||||
|
entry_count++;
|
||||||
|
} else {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dir_length_width[0] = entry_count;
|
||||||
|
dir_length_width[1] = max_length;
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_dir(WINDOW *win, unsigned long file_count, file_data *dir_content){
|
void get_dir_content(char *path, unsigned long *dir_length_width, unsigned long *dir_width, char *dir_content){
|
||||||
for (unsigned long i = 0; i < file_count; i++ ){ //skip index 0 as it is used for metadata like file count
|
struct dirent **entry;
|
||||||
if (dir_content[i].file_name) {
|
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
|
||||||
wprintw(win, "%s",dir_content[i].file_name);
|
dir_length_width[0] = scandir(path, &entry, NULL, alphasort);
|
||||||
wmove(win, i, 1);
|
|
||||||
} else {
|
} else {
|
||||||
wprintw(win, "NULL");
|
dir_length_width[0] = scandir(path, &entry, skip_hidden_files, alphasort);
|
||||||
wmove(win, i, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *populate_dir(void *which){ // 0=left, 1=main, 2=right
|
unsigned long i = 0;
|
||||||
char wh = (char)which;
|
for (i = 0; i < dir_length_width[0]; i++ ) {
|
||||||
unsigned long longest_name = 0;
|
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||||
|
|
||||||
if (wh) {
|
|
||||||
if (wh == 1) {
|
|
||||||
free(content_m);
|
|
||||||
get_dir_size(path, &file_count_m, &longest_name);
|
|
||||||
content_m = (file_data*)calloc(file_count_m, sizeof(file_data));
|
|
||||||
get_dir_content(path, file_count_m, longest_name, content_m);
|
|
||||||
sort_dir(&file_count_m, &longest_name, content_m);
|
|
||||||
} else {
|
} else {
|
||||||
free(content_r);
|
unsigned long j = 0;
|
||||||
get_dir_size(path, &file_count_r, &longest_name);
|
for (; entry[i]->d_name[j] != '\0'; j++) {
|
||||||
content_r = calloc(file_count_r, sizeof(file_data));
|
dir_content[i * dir_length_width[1] + j] = entry[i]->d_name[j];
|
||||||
get_dir_content(path, file_count_r, longest_name, content_r);
|
|
||||||
sort_dir(&file_count_m, &longest_name, content_r);
|
|
||||||
}
|
}
|
||||||
} else {
|
dir_width[i] = j;
|
||||||
free(content_l);
|
}
|
||||||
get_dir_size(path, &file_count_l, &longest_name);
|
}
|
||||||
content_l = calloc(file_count_l, sizeof(file_data));
|
for (i = 0; i < dir_length_width[0]; i++) {
|
||||||
get_dir_content(path, file_count_l, longest_name, content_l);
|
free(entry[i]);
|
||||||
sort_dir(&file_count_m, &longest_name, content_l);
|
}
|
||||||
|
free(entry);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
void print_dir(WINDOW *win, unsigned long *dir_length_width, unsigned long *file_width, char *dir_content){
|
||||||
|
|
||||||
|
unsigned long i = 0;
|
||||||
|
for (i = 0; i<dir_length_width[0]; i++) {
|
||||||
|
unsigned long j = 0;
|
||||||
|
for (j = 0; j < file_width[i]; j++){
|
||||||
|
mvwprintw(win, i, j, "%c", dir_content[i * dir_length_width[1] + j]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
#include "backend.c"
|
#include "backend.c"
|
||||||
|
|
||||||
|
|
||||||
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name);
|
void get_dir_size(char *path, unsigned long *dir_length_width, unsigned long *dir_width);
|
||||||
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, file_data *dir_content);
|
void get_dir_content(char *path, unsigned long *dir_length_width, unsigned long *dir_width, char *dir_content);
|
||||||
void print_dir(WINDOW *win, unsigned long file_count, file_data *dir_content);
|
void print_dir(WINDOW *win, unsigned long *dir_length_width, unsigned long *width, char *dir_content);
|
||||||
//void sort_dir(unsigned long *file_count, unsigned long *longest_name, file_data *dir_content);
|
|
||||||
void *populate_dir(void *dir);
|
|
||||||
|
22
defines.h
22
defines.h
@ -1,23 +1,21 @@
|
|||||||
#define STATUS_QUIT_PROGRAM 1
|
#define STATUS_QUIT_PROGRAM 1
|
||||||
#define STATUS_RUN_BACKEND 2
|
#define STATUS_RUN_BACKEND 2
|
||||||
#define STATUS_UPDATE_SCREEN_MASK 12 // 1100
|
#define STATUS_UPDATE_SCREEN_MASK 12 /* 1100*/
|
||||||
#define STATUS_UPDATE_SCREEN_0 4
|
#define STATUS_UPDATE_SCREEN_0 4
|
||||||
#define STATUS_UPDATE_SCREEN_1 8
|
#define STATUS_UPDATE_SCREEN_RESIZE 8
|
||||||
|
#define STATUS_LOCK_MASK 496
|
||||||
|
#define STATUS_LOCK_TOP 16
|
||||||
|
#define STATUS_LOCK_BTM 32
|
||||||
|
#define STATUS_LOCK_LFT 64
|
||||||
|
#define STATUS_LOCK_MID 128
|
||||||
|
#define STATUS_LOCK_RGT 256
|
||||||
|
|
||||||
#define FILE_MODIFIERS_HIDDEN_FILES 1
|
#define FILE_MODIFIERS_HIDDEN_FILES 1
|
||||||
#define FILE_MODIFIERS_SORT_BITMASK 126 // 00000000000000000000000001111110
|
#define FILE_MODIFIERS_SORT_BITMASK 126 /* 00000000000000000000000001111110*/
|
||||||
#define FILE_MODIFIERS_SORT_ALPHABETIC 2
|
#define FILE_MODIFIERS_SORT_ALPHABETIC 2
|
||||||
#define FILE_MODIFIERS_SORT_TYPE 4
|
#define FILE_MODIFIERS_SORT_TYPE 4
|
||||||
#define FILE_MODIFIERS_SORT_EXTENSION 8
|
#define FILE_MODIFIERS_SORT_EXTENSION 8
|
||||||
#define FILE_MODIFIERS_SORT_SIZE 16
|
#define FILE_MODIFIERS_SORT_SIZE 16
|
||||||
#define FILE_MODIFIERS_SORT_RANDOM 32
|
#define FILE_MODIFIERS_SORT_RANDOM 32
|
||||||
#define FILE_MODIFIERS_SORT_REVERSE 64
|
#define FILE_MODIFIERS_SORT_REVERSE 64
|
||||||
//FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0
|
/*FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0*/
|
||||||
|
|
||||||
#ifndef HEADER_GUARD
|
|
||||||
#define HEADER_GUARD
|
|
||||||
typedef struct file_data {
|
|
||||||
char *file_name;
|
|
||||||
unsigned char file_type;
|
|
||||||
} file_data;
|
|
||||||
#endif
|
|
||||||
|
@ -13,13 +13,17 @@ void user_interactions(char *input, unsigned int *status, unsigned int *settings
|
|||||||
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||||
} else if (*input == 'a') {
|
} else if (*input == 'a') {
|
||||||
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||||
|
*status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||||
} else if (*input == 'o') {
|
} else if (*input == 'o') {
|
||||||
file_modifiers ^= FILE_MODIFIERS_SORT_BITMASK;
|
file_modifiers ^= FILE_MODIFIERS_SORT_BITMASK;
|
||||||
} else if (*input == 'e') {
|
} else if (*input == 'e') {
|
||||||
file_modifiers ^= FILE_MODIFIERS_SORT_ALPHABETIC;
|
file_modifiers ^= FILE_MODIFIERS_SORT_ALPHABETIC;
|
||||||
} else if (*input == 'u') {
|
} else if (*input == 'u') {
|
||||||
|
*status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||||
|
} else if (*input == 'h') {
|
||||||
|
chdir("..");
|
||||||
|
*status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
}
|
}
|
||||||
*status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
|
|
||||||
}
|
}
|
||||||
|
167
main.c
167
main.c
@ -1,101 +1,152 @@
|
|||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/sysinfo.h>
|
#include <sys/sysinfo.h>
|
||||||
|
#include "threading.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include "interactions.h"
|
#include "interactions.h"
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
unsigned int terminal_height;
|
unsigned int terminal_height;
|
||||||
unsigned int terminal_width;
|
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 temp_width = 0;
|
||||||
unsigned int settings;
|
unsigned int settings;
|
||||||
unsigned int file_modifiers;
|
unsigned int file_modifiers;
|
||||||
unsigned int status; //bit 0 = enable
|
unsigned int status;
|
||||||
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 = ".";
|
|
||||||
char input = 0;
|
char input = 0;
|
||||||
|
|
||||||
|
void render_pass(WINDOW *wint, WINDOW *winb, WINDOW *winl, WINDOW *winm, WINDOW *winr);
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
initscr(); //start ncurses
|
|
||||||
timeout(50); //blocking timeout of getch()
|
|
||||||
keypad(stdscr, TRUE);
|
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
getmaxyx(stdscr, terminal_height, terminal_width);
|
getmaxyx(stdscr, terminal_height, terminal_width);
|
||||||
WINDOW *winl = newwin(terminal_height, terminal_width/3, terminal_height, (terminal_width/3));
|
WINDOW *win_t = newwin(1, terminal_width, 0, 0);
|
||||||
WINDOW *winm = newwin(terminal_height, terminal_width/3, 0, 0);
|
WINDOW *win_b = newwin(1, terminal_width, terminal_height-1, 0);
|
||||||
WINDOW *winr = newwin(terminal_height, terminal_width/3, terminal_height, ((terminal_width/3)*2));
|
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)){
|
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);
|
getmaxyx(stdscr, terminal_height, terminal_width);
|
||||||
temp_heigth -= terminal_height;
|
|
||||||
temp_width -= terminal_width;
|
if (!(terminal_height == temp_heigth) || !(terminal_width == temp_width)) {
|
||||||
if (!temp_heigth || !temp_width || (status & STATUS_UPDATE_SCREEN_MASK)) { //updates screen
|
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RESIZE);
|
||||||
if (status & STATUS_UPDATE_SCREEN_0) {
|
temp_width = terminal_width;
|
||||||
window_left(winl, 0, 0, content_l);
|
temp_heigth = terminal_height;
|
||||||
window_main(winm, 0, terminal_width/3, content_m);
|
}
|
||||||
window_right(winr, 0, (terminal_width/3)*2, content_r);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
wmove(stdscr,0,0);
|
|
||||||
//status &= ~STATUS_UPDATE_SCREEN_MASK;
|
|
||||||
status = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ((input = getch())) {
|
if ((input = getch())) {
|
||||||
user_interactions(&input, &status, &settings);
|
user_interactions(&input, &status, &settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
render_pass(win_t, win_b, win_l, win_m, win_r);
|
||||||
|
|
||||||
}
|
}
|
||||||
free(content_l);
|
threading_free();
|
||||||
free(content_m);
|
|
||||||
free(content_r);
|
|
||||||
|
|
||||||
|
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();
|
endwin();
|
||||||
|
refresh();
|
||||||
return 0;
|
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() {
|
void init() {
|
||||||
|
|
||||||
cpu_cores = get_nprocs();
|
initscr(); /*start ncurses*/
|
||||||
//file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);
|
noecho(); /*hide keyboard input*/
|
||||||
file_modifiers = FILE_MODIFIERS_SORT_BITMASK;
|
timeout(50); /*blocking timeout of getch()*/
|
||||||
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
|
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));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
63
sorting.c
63
sorting.c
@ -1,71 +1,14 @@
|
|||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <string.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <strings.h>
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
extern unsigned int settings;
|
extern unsigned int settings;
|
||||||
extern unsigned int file_modifiers;
|
extern unsigned int file_modifiers;
|
||||||
extern unsigned short cpu_cores; //amount of cores/threads the host system reports to have
|
|
||||||
extern file_data *content_l;
|
|
||||||
extern file_data *content_m;
|
|
||||||
extern file_data *content_r;
|
|
||||||
extern char *path;
|
|
||||||
|
|
||||||
int type(const void *f0, const void *f1){
|
int skip_hidden_files(const struct dirent *entry){
|
||||||
const char *file0_name = ((file_data*)f0)->file_name;
|
if (entry->d_name[0] == '.') {
|
||||||
const char *file1_name = ((file_data*)f1)->file_name;
|
return 0;
|
||||||
const char file0_type = ((file_data*)f0)->file_type;
|
|
||||||
const char file1_type = ((file_data*)f1)->file_type;
|
|
||||||
if (file0_type > file1_type) {
|
|
||||||
return -1;
|
|
||||||
} else if (file0_type < file1_type) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return strcmp(file0_name, file1_name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int natural(const void *f0, const void *f1){
|
|
||||||
const char *file0_name = ((file_data*)f0)->file_name;
|
|
||||||
const char *file1_name = ((file_data*)f1)->file_name;
|
|
||||||
const char file0_type = ((file_data*)f0)->file_type;
|
|
||||||
const char file1_type = ((file_data*)f1)->file_type;
|
|
||||||
if (S_ISDIR(file0_type) || S_ISDIR(file1_type)) {
|
|
||||||
if (file0_type == file1_type) {
|
|
||||||
return strcmp(file0_name, file1_name);
|
|
||||||
} else if (S_ISDIR(file1_type)) {
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return strcmp(file0_name, file1_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int alphabetic(const void *f0, const void *f1){
|
|
||||||
const char *file0_name = ((file_data*)f0)->file_name;
|
|
||||||
const char *file1_name = ((file_data*)f1)->file_name;
|
|
||||||
return strcmp(file0_name, file1_name);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void sort_dir(unsigned long *file_count, unsigned long *longest_name, file_data *dir_content){
|
|
||||||
|
|
||||||
if ((file_modifiers & FILE_MODIFIERS_SORT_BITMASK) == ~FILE_MODIFIERS_SORT_BITMASK) {
|
|
||||||
qsort(dir_content, *file_count, sizeof(file_data), natural);
|
|
||||||
|
|
||||||
} else if (file_modifiers & FILE_MODIFIERS_SORT_ALPHABETIC) {
|
|
||||||
qsort(dir_content, *file_count, sizeof(file_data), alphabetic);
|
|
||||||
|
|
||||||
} else if (file_modifiers & FILE_MODIFIERS_SORT_TYPE) {
|
|
||||||
qsort(dir_content, *file_count, sizeof(file_data), type);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <string.h>
|
|
||||||
#include <strings.h>
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "sorting.c"
|
#include "sorting.c"
|
||||||
|
|
||||||
void sort_dir(unsigned long *file_count, unsigned long *longest_name, file_data *dir_content);
|
void sort_dir(unsigned long *dir_length_width, char *dir_content);
|
||||||
|
int skip_hidden_files(const struct dirent *entry);
|
||||||
|
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
typedef struct data {
|
|
||||||
char *file_name;
|
|
||||||
unsigned char file_type;
|
|
||||||
} file_data;
|
|
153
threading.c
Normal file
153
threading.c
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
#include <curses.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "defines.h"
|
||||||
|
#include "backend.h"
|
||||||
|
|
||||||
|
|
||||||
|
pthread_mutex_t mutex_top;
|
||||||
|
pthread_mutex_t mutex_btm;
|
||||||
|
pthread_mutex_t mutex_lft;
|
||||||
|
pthread_mutex_t mutex_mid;
|
||||||
|
pthread_mutex_t mutex_rgt;
|
||||||
|
|
||||||
|
/* contains entire directory as 2d array
|
||||||
|
* may be changed in future to only include parts of the dir (currently includes entire dir) */
|
||||||
|
char *mid_content;
|
||||||
|
char *lft_content;
|
||||||
|
char *top_content; /* current path */
|
||||||
|
/* index 0 = file_count
|
||||||
|
* index 1 = longest_name */
|
||||||
|
unsigned long *mid_length_width;
|
||||||
|
unsigned long *lft_length_width;
|
||||||
|
unsigned long *top_length_width;
|
||||||
|
/* this array exists so that a file with a 3 char long name wont spend the same amount of time printing as a file with 200 chars
|
||||||
|
* should both exist in the same directory
|
||||||
|
* currently unused */
|
||||||
|
unsigned long *mid_file_name_width;
|
||||||
|
unsigned long *lft_file_name_width;
|
||||||
|
|
||||||
|
|
||||||
|
extern unsigned int status;
|
||||||
|
|
||||||
|
void *thread_mid(void *data){
|
||||||
|
pthread_mutex_lock(&mutex_mid);
|
||||||
|
|
||||||
|
free(mid_content);
|
||||||
|
free(mid_file_name_width);
|
||||||
|
free(mid_length_width);
|
||||||
|
mid_length_width = malloc(sizeof(char)*2);
|
||||||
|
|
||||||
|
char *path;
|
||||||
|
if((path=getcwd(NULL, 0)) == NULL) {
|
||||||
|
mid_content = malloc(sizeof("cannot open directory"));
|
||||||
|
mid_length_width[0] = 1;
|
||||||
|
mid_length_width[1] = sizeof("cannot open directory");
|
||||||
|
mid_content = "cannot open directory";
|
||||||
|
} else {
|
||||||
|
|
||||||
|
|
||||||
|
get_dir_size(path, mid_length_width, mid_file_name_width);
|
||||||
|
mid_file_name_width = malloc(mid_length_width[0] * sizeof(unsigned long));
|
||||||
|
mid_content = malloc(mid_length_width[0] * mid_length_width[1] * sizeof(char));
|
||||||
|
memset(mid_content, ' ', mid_length_width[0] * mid_length_width[1] * sizeof(char));
|
||||||
|
get_dir_content(path, mid_length_width, mid_file_name_width, mid_content);
|
||||||
|
|
||||||
|
}
|
||||||
|
free(path);
|
||||||
|
pthread_mutex_unlock(&mutex_mid);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
void *thread_lft(void *data){
|
||||||
|
pthread_mutex_lock(&mutex_lft);
|
||||||
|
|
||||||
|
free(lft_content);
|
||||||
|
free(lft_file_name_width);
|
||||||
|
free(lft_length_width);
|
||||||
|
lft_length_width = malloc(sizeof(char)*2);
|
||||||
|
|
||||||
|
char *path;
|
||||||
|
if((path=getcwd(NULL, 0)) == NULL) {
|
||||||
|
lft_content = malloc(sizeof("cannot open directory"));
|
||||||
|
lft_length_width[0] = 1;
|
||||||
|
lft_length_width[1] = sizeof("cannot open directory");
|
||||||
|
lft_content = "cannot open directory";
|
||||||
|
} else {
|
||||||
|
|
||||||
|
|
||||||
|
char *parent ;
|
||||||
|
if((parent = malloc(strlen(path)+strlen("/..")+1)) != NULL){
|
||||||
|
parent[0] = '\0'; /* ensures empty string */
|
||||||
|
strcat(parent, path);
|
||||||
|
strcat(parent, "/..");
|
||||||
|
}
|
||||||
|
|
||||||
|
get_dir_size(parent, lft_length_width, lft_file_name_width);
|
||||||
|
lft_file_name_width = malloc(lft_length_width[0] * sizeof(unsigned long));
|
||||||
|
lft_content = malloc(lft_length_width[0] * lft_length_width[1] * sizeof(char));
|
||||||
|
memset(lft_content, ' ', lft_length_width[0] * lft_length_width[1] * sizeof(char));
|
||||||
|
get_dir_content(parent, lft_length_width, lft_file_name_width, lft_content);
|
||||||
|
free (parent);
|
||||||
|
}
|
||||||
|
free(path);
|
||||||
|
pthread_mutex_unlock(&mutex_lft);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
void *thread_rgt(void *data){
|
||||||
|
|
||||||
|
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
void *thread_top(void *data){
|
||||||
|
pthread_mutex_lock(&mutex_top);
|
||||||
|
free(top_content);
|
||||||
|
free(top_length_width);
|
||||||
|
top_length_width = malloc(sizeof(char)*2);
|
||||||
|
|
||||||
|
char *path;
|
||||||
|
if((path=getcwd(NULL, 0)) == NULL) {
|
||||||
|
top_content = malloc(sizeof("cannot open directory"));
|
||||||
|
top_length_width[0] = 1;
|
||||||
|
top_length_width[1] = sizeof("cannot open directory");
|
||||||
|
top_content = "cannot open directory";
|
||||||
|
} else {
|
||||||
|
top_content = getcwd(NULL, 0);
|
||||||
|
top_length_width[0] = 1;
|
||||||
|
top_length_width[1] = strlen(top_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(path);
|
||||||
|
pthread_mutex_unlock(&mutex_top);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
void *thread_btm(void *data){
|
||||||
|
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void threading_init(){
|
||||||
|
mid_content = malloc(sizeof(char));
|
||||||
|
mid_file_name_width = malloc(sizeof(char));
|
||||||
|
mid_length_width = malloc(sizeof(char));
|
||||||
|
lft_content = malloc(sizeof(char));
|
||||||
|
lft_file_name_width = malloc(sizeof(char));
|
||||||
|
lft_length_width = malloc(sizeof(char));
|
||||||
|
top_length_width = malloc(sizeof(char));
|
||||||
|
top_content = malloc(sizeof(char));
|
||||||
|
|
||||||
|
|
||||||
|
pthread_mutex_init(&mutex_top, NULL);
|
||||||
|
pthread_mutex_init(&mutex_mid, NULL);
|
||||||
|
pthread_mutex_init(&mutex_lft, NULL);
|
||||||
|
}
|
||||||
|
void threading_free(){
|
||||||
|
free(mid_content);
|
||||||
|
free(mid_file_name_width);
|
||||||
|
free(mid_length_width);
|
||||||
|
|
||||||
|
pthread_mutex_destroy(&mutex_top);
|
||||||
|
pthread_mutex_destroy(&mutex_mid);
|
||||||
|
pthread_mutex_destroy(&mutex_lft);
|
||||||
|
}
|
10
threading.h
Normal file
10
threading.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include <curses.h>
|
||||||
|
#include "threading.c"
|
||||||
|
|
||||||
|
void *thread_lft(void *data);
|
||||||
|
void *thread_mid(void *data);
|
||||||
|
void *thread_rgt(void *data);
|
||||||
|
void *thread_top(void *data);
|
||||||
|
void *thread_btm(void *data);
|
||||||
|
void threading_init();
|
||||||
|
void threading_free();
|
140
window.c
140
window.c
@ -1,72 +1,88 @@
|
|||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <dirent.h>
|
#include <pthread.h>
|
||||||
#include "backend.h"
|
#include "defines.h"
|
||||||
|
|
||||||
extern unsigned int terminal_height;
|
extern unsigned int status;
|
||||||
extern unsigned int terminal_width;
|
|
||||||
extern unsigned long file_count_l;
|
|
||||||
extern unsigned long file_count_m;
|
|
||||||
extern unsigned long file_count_r;
|
|
||||||
|
|
||||||
|
extern char *mid_content;
|
||||||
|
extern unsigned long *mid_width;
|
||||||
|
extern unsigned long *mid_length_width;
|
||||||
|
extern unsigned long *mid_file_name_width;
|
||||||
|
extern char *top_content;
|
||||||
|
extern unsigned long *top_length_width;
|
||||||
|
extern char *lft_content;
|
||||||
|
extern unsigned long *lft_width;
|
||||||
|
extern unsigned long *lft_length_width;
|
||||||
|
extern unsigned long *lft_file_name_width;
|
||||||
|
|
||||||
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content){
|
extern pthread_mutex_t mutex_top;
|
||||||
|
extern pthread_mutex_t mutex_btm;
|
||||||
|
extern pthread_mutex_t mutex_lft;
|
||||||
|
extern pthread_mutex_t mutex_mid;
|
||||||
|
extern pthread_mutex_t mutex_rgt;
|
||||||
|
|
||||||
//WINDOW *win = (window_data)window_data.win;
|
void window_top(WINDOW *win){
|
||||||
unsigned int local_width;
|
unsigned long i = 0;
|
||||||
unsigned int local_height;
|
werase(win);
|
||||||
|
|
||||||
//{{{ size & positioning
|
if (pthread_mutex_trylock(&mutex_top)) {
|
||||||
wresize(win, terminal_height, terminal_width/3);
|
wprintw(win,"loading");
|
||||||
getmaxyx(win, local_height, local_width);
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
mvwin(win, start_y, start_x);
|
|
||||||
wclear(win);
|
|
||||||
//}}}
|
|
||||||
|
|
||||||
wmove(win, 1, 1);
|
} else {
|
||||||
|
for (i = 0; i<top_length_width[1]; i++) {
|
||||||
print_dir(win, file_count_m, dir_content);
|
mvwprintw(win, 0, i, "%c", top_content[i]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
box(win,0,0);
|
|
||||||
wrefresh(win);
|
|
||||||
}
|
}
|
||||||
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content){
|
pthread_mutex_unlock(&mutex_top);
|
||||||
|
}
|
||||||
unsigned int local_width;
|
}
|
||||||
unsigned int local_height;
|
void window_btm(WINDOW *win){
|
||||||
|
werase(win);
|
||||||
//{{{ size & positioning
|
if (pthread_mutex_trylock(&mutex_btm)) {
|
||||||
wresize(win, terminal_height, terminal_width/3);
|
} else {
|
||||||
getmaxyx(win, local_height, local_width);
|
pthread_mutex_unlock(&mutex_btm);
|
||||||
mvwin(win, start_y, start_x);
|
}
|
||||||
wclear(win);
|
}
|
||||||
//}}}
|
void window_lft(WINDOW *win){
|
||||||
|
werase(win);
|
||||||
wmove(win, 1, 1);
|
box(win, 0, 0);
|
||||||
|
|
||||||
print_dir(win, file_count_l, dir_content);
|
unsigned long local_width;
|
||||||
|
unsigned long local_height;
|
||||||
|
getmaxyx(win, local_height, local_width);
|
||||||
box(win,0,0);
|
if (pthread_mutex_trylock(&mutex_lft)) {
|
||||||
wrefresh(win);
|
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||||
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", lft_length_width[0], lft_length_width[1]);
|
||||||
|
print_dir(win, lft_length_width, lft_file_name_width, lft_content);
|
||||||
|
pthread_mutex_unlock(&mutex_lft);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void window_mid(WINDOW *win){
|
||||||
|
werase(win);
|
||||||
|
box(win, 0, 0);
|
||||||
|
|
||||||
|
unsigned long local_width;
|
||||||
|
unsigned long local_height;
|
||||||
|
getmaxyx(win, local_height, local_width);
|
||||||
|
if (pthread_mutex_trylock(&mutex_mid)) {
|
||||||
|
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||||
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", mid_length_width[0], mid_length_width[1]);
|
||||||
|
print_dir(win, mid_length_width, mid_file_name_width, mid_content);
|
||||||
|
pthread_mutex_unlock(&mutex_mid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void window_rgt(WINDOW *win){
|
||||||
|
werase(win);
|
||||||
|
box(win, 0, 0);
|
||||||
|
|
||||||
|
if (pthread_mutex_trylock(&mutex_rgt)) {
|
||||||
|
} else {
|
||||||
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
}
|
}
|
||||||
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content){
|
|
||||||
|
|
||||||
wmove(win, 0, 0);
|
|
||||||
unsigned int local_width;
|
|
||||||
unsigned int local_height;
|
|
||||||
|
|
||||||
//{{{ size & positioning
|
|
||||||
wresize(win, terminal_height, terminal_width/3);
|
|
||||||
getmaxyx(win, local_height, local_width);
|
|
||||||
mvwin(win, start_y, start_x);
|
|
||||||
wclear(win);
|
|
||||||
//}}}
|
|
||||||
|
|
||||||
wmove(win, local_height/2, local_width/2);
|
|
||||||
wprintw(win, "%d,%d", local_height, local_width);
|
|
||||||
|
|
||||||
box(win,0,0);
|
|
||||||
wrefresh(win);
|
|
||||||
}
|
}
|
||||||
|
10
window.h
10
window.h
@ -1,7 +1,7 @@
|
|||||||
#include <curses.h>
|
|
||||||
#include "window.c"
|
#include "window.c"
|
||||||
|
|
||||||
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content);
|
void window_top(WINDOW *win);
|
||||||
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content);
|
void window_btm(WINDOW *win);
|
||||||
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content);
|
void window_lft(WINDOW *win);
|
||||||
|
void window_mid(WINDOW *win);
|
||||||
|
void window_rgt(WINDOW *win);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user