Compare commits

..
6 Commits
17 changed files with 498 additions and 48 deletions
+2
View File
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.23)
set(CACHE DEV_WIDGET BOOL OFF "Enable development widget mode")
project(ZBBSClientTUI
VERSION 1.0.0
)
+3 -1
View File
@@ -10,7 +10,9 @@ It will create all necessary file structure in order to run the program correctl
2. Build the binary running `$ make` or `$ make bbs-client`
3. Run it by `$ make run`
## Develop
## Development
### Widget development
In order to switch to widget/screen development, configure cmake with `-DDEV_WIDGET=ON`
### Name conventions:
+1 -1
View File
@@ -1,11 +1,11 @@
# TODO:
## Current:
- [ ] Rewrite cancel download. Use packages and check every time for a confirmation to continue download
- [ ] File upload multiline description (window)
- [ ] Sorting in the file list
- [ ] Resume download
- [ ] Separate common structures in the common.h
- [x] Rewrite cancel download. Use packages and check every time for a confirmation to continue download
- [x] Show download/upload progress in percentages
- [x] Break line automatically in modal widgets
- [x] Cancel upload/download
+6
View File
@@ -22,6 +22,12 @@ endif()
file(GLOB M_SOURCES "*.c")
if(DEV_WIDGET)
list(FILTER M_SOURCES EXCLUDE REGEX "main\\.c$")
else()
list(FILTER M_SOURCES EXCLUDE REGEX "main_dev_widget\\.c$")
endif()
target_sources(zbbs-client-tui
PRIVATE
${M_SOURCES}
+20 -21
View File
@@ -11,44 +11,41 @@
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <wchar.h>
void ac_file(WINDOW *win, int is_action_w);
app_t *app_init(void) {
app_t *app_init(app_t *app) {
app->top_text = NULL;
/* get values from terminal size */
int32_t y_max, x_max;
getmaxyx(stdscr, y_max, x_max);
/* allocate app struct memory */
app_t *_app = calloc(1, sizeof(app_t));
if (app == NULL) {
app = calloc(1, sizeof(app_t));
}
/* associate values for screen size */
_app->coordinates.max_x = _app->coordinates.cur_x = x_max;
_app->coordinates.max_y = _app->coordinates.cur_y = y_max;
app->coordinates.max_x = app->coordinates.cur_x = x_max;
app->coordinates.max_y = app->coordinates.cur_y = y_max;
_app->win = newwin(_app->coordinates.cur_y, _app->coordinates.cur_x, 0, 0);
app->win = newwin(app->coordinates.cur_y, app->coordinates.cur_x, 0, 0);
/* dialogue */
_app->modal.win = NULL;
_app->modal.is_initiated = 0;
/* print top and bottom bars */
_app->main_ui.b_keys_len = 0;
app_draw_bars(_app);
/* here goes box borders */
app_draw_borders(_app);
app->modal.win = NULL;
app->modal.is_initiated = 0;
/* NULL to main_ui */
MAIN_UI_RESET(_app);
MAIN_UI_RESET(app);
keypad(_app->win, TRUE);
keypad(app->win, TRUE);
/* refresh the windows */
wnoutrefresh(_app->win);
wnoutrefresh(app->win);
doupdate();
return _app;
return app;
}
void app_init_nc(void) {
@@ -106,15 +103,17 @@ void app_draw_bars(app_t *app) {
mvwprintw(app->win, app->coordinates.cur_y - 2, i, " ");
}
/* add content to the top bar */
if (app->params && app->params->is_connected) {
if (app->top_text != NULL) {
mvwaddnwstr(app->win, 1, 2, app->top_text, wcslen(app->top_text));
} else if (app->params && app->params->is_connected) {
int32_t ip_address = app->params->addr;
u_char ip_addr[4];
mempcpy(ip_addr, &ip_address, 4);
sprintf(top_text, "Connected to %u.%u.%u.%u, user: %s", ip_addr[0],
ip_addr[1], ip_addr[2], ip_addr[3], app->params->uname);
}
mvwprintw(app->win, 1, 2, "%s", top_text);
}
wattroff(app->win, A_REVERSE);
/* add content to the bottom bar */
+2 -1
View File
@@ -24,10 +24,11 @@ typedef struct {
file_args_t *file_args;
void (*active_callback)(w_cb_args_t *args);
void *active_widget;
wchar_t *top_text;
main_window_t main_ui;
} app_t;
app_t *app_init(void);
app_t *app_init(app_t *app);
void app_init_nc(void);
void app_destroy(app_t *app, int32_t exit_code);
void app_draw_borders(app_t *app);
+1 -1
View File
@@ -31,7 +31,7 @@ bool connect_to_server(app_t *app) {
return false;
}
app->query_args->state = S_WAIT_SERVER;
app->params->is_connected = TRUE;
app->params->is_connected = true;
app->query_args->sd = app->params->sd;
app_draw_bars(app);
return true;
+16 -21
View File
@@ -6,7 +6,6 @@
#include <ncursesw/ncurses.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -15,11 +14,11 @@
#include <widget.h>
#include <windows.h>
#include "main.h"
#include "../app.h"
#include "alert.h"
#include "connection.h"
#include "file_list.h"
// #include "file_list.h"
#include "main.h"
#include "query.h"
int32_t m_id = 0;
@@ -27,42 +26,38 @@ int32_t m_id = 0;
int32_t process_user_input(app_t *app, w_cb_args_t *d_args);
int main(int argc, char **argv) {
app_t *app;
app_t app;
params_t params;
query_args_t *q_args = malloc(sizeof(query_args_t));
app = calloc(1, sizeof(app_t));
query_args_t q_args;
app_init_nc();
app = app_init();
app.params = &params;
app_init(&app);
init_params(&params);
app->params = &params;
analyze_args(argc, argv, &params);
/* init alert */
w_alert_init(app);
w_alert_init(&app);
init_query_args(q_args, app->params);
q_args->buf = malloc(INBUFSIZE);
app->query_args = q_args;
main_window_set(app, mw_fl_server);
init_query_args(&q_args, app.params);
app.query_args = &q_args;
main_window_set(&app, mw_fl_server);
/* init client to connect to the server */
app->params->sd = init_client();
app.params->sd = init_client();
if (params.addr == 0) {
app->query_args->state = S_ASK_SEVER_IP;
app.query_args->state = S_ASK_SEVER_IP;
} else {
connect_to_server(app);
connect_to_server(&app);
}
app->query_args->main_ui = &(app->main_ui);
app.query_args->main_ui = &(app.main_ui);
query_loop(app);
query_loop(&app);
clear_params(&params);
app_destroy(app, 0);
app_destroy(&app, 0);
return OK;
}
+1 -1
View File
@@ -119,7 +119,7 @@ typedef struct query_args {
int32_t buf_used;
p_file_t *file;
enum state state;
char *buf;
char buf[INBUFSIZE];
bool from_server;
params_t *params;
struct {
+116
View File
@@ -0,0 +1,116 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 Oleksandr Zhylin */
#include <arpa/inet.h>
#include <modals.h>
#include <ncursesw/ncurses.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <termios.h>
#include <unistd.h>
#include <widget.h>
#include <windows.h>
#include "../app.h"
#include "alert.h"
#include "connection.h"
#include "main.h"
#include "query.h"
int32_t m_id = 0;
void mini_loop(app_t *app) {
w_cb_args_t d_args = {
.app = app, .element = NULL, .data = NULL, .resp_data.code = cbrc_none};
fd_set readfds;
while (true) {
app_draw_modal(app);
main_window_draw(app);
app_refresh(app);
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
int maxfd = STDIN_FILENO;
int sr = select(maxfd + 1, &readfds, NULL, NULL, NULL);
if (sr == -1) {
/* perror("select"); */
exit(3);
}
if (app->main_ui.cb_b_press != NULL && app->modal.is_initiated == false) {
if (ERR == app->main_ui.cb_b_press(app, &d_args)) {
w_alert("error while processing user input");
}
}
}
}
int main(void) {
app_t app;
file_args_t fargs;
query_args_t q_args;
params_t params;
p_file_t file;
init_query_args(&q_args, app.params);
app.params = &params;
app.query_args = &q_args;
app.file_args = &fargs;
app_init_nc();
init_params(&params);
w_alert_init(&app);
app_init(&app);
q_args.file = &file;
app.query_args->main_ui = &(app.main_ui);
file.name = "Test file #1";
params.is_connected = false;
app.modal.is_initiated = false;
main_window_set(&app, mw_f_desc);
mini_loop(&app);
exit(0);
}
int32_t process_user_input(app_t *app, w_cb_args_t *d_args) {
int32_t c;
w_ui_file_list_t *fui = (w_ui_file_list_t *)app->query_args->main_ui->ui;
c = wgetch(app->win);
switch (c) {
case KEY_F(9):
app_destroy(app, 0);
return OK;
case 'U':
case 'u':
if (!app->modal.is_initiated && !fui->active_search &&
app->main_ui.type != mw_fl_local) {
/* app->query_args->state = S_UPLOAD_FILE_SELECT; */
main_window_set(app, mw_fl_local);
main_window_draw(app);
d_args->element = app->main_ui.ui;
break;
}
case 's':
case 'S':
if (!app->modal.is_initiated && !fui->active_search) {
fui->active_search = true;
w_fl_draw(fui);
return OK;
}
default:
d_args->data = (void *)&c;
app->active_callback(d_args);
break;
}
return OK;
}
-1
View File
@@ -291,7 +291,6 @@ static int process_command(app_t *app, wait_server_cb *callback) {
}
void init_query_args(query_args_t *q_args, params_t *params) {
q_args->buf = NULL;
q_args->sd = -1;
q_args->state = S_N_D;
q_args->buf_used = 0;
+16
View File
@@ -13,6 +13,10 @@ void u_d_arr_ptr_init(d_array_ptr_t *arr, int32_t init_capacity) {
memset(arr->arr, 0, sizeof(void *) * init_capacity);
}
void u_d_array_append(d_array_ptr_t *arr, void *el_ptr, size_t el_len) {
u_d_arr_ptr_add(arr, el_ptr, arr->length);
}
void u_d_arr_ptr_add(d_array_ptr_t *arr, void *ptr, int32_t idx) {
if (idx > arr->capacity - 1) {
int32_t new_capacity = idx * 2;
@@ -32,3 +36,15 @@ void u_d_arr_ptr_free(d_array_ptr_t *arr) {
free(arr->arr);
arr->arr = NULL;
}
void u_d_arr_free_cb(d_array_ptr_t *arr, u_d_arr_free_callback *callback) {
for (int i = 0; i < arr->length; i++) {
if (arr->arr[i] != NULL) {
callback(arr->arr[i]);
}
}
arr->capacity = 0;
arr->length = 0;
free(arr->arr);
arr->arr = NULL;
}
+7
View File
@@ -1,7 +1,10 @@
#ifndef D_ARRAY_H
#define D_ARRAY_H
/* Dynamic array of numbers */
#include <stddef.h>
#include <stdint.h>
typedef struct d_array_ptr_t {
int32_t capacity;
int32_t length;
@@ -10,8 +13,12 @@ typedef struct d_array_ptr_t {
void *last_el;
} d_array_ptr_t;
typedef void *u_d_arr_free_callback(void *arg);
void u_d_arr_ptr_init(d_array_ptr_t *arr, int32_t init_capacity);
void u_d_arr_ptr_add(d_array_ptr_t *arr, void *ptr, int32_t idx);
void u_d_arr_ptr_free(d_array_ptr_t *arr);
void u_d_array_append(d_array_ptr_t *arr, void *el_ptr, size_t el_len);
void u_d_arr_free_cb(d_array_ptr_t *arr, u_d_arr_free_callback *callback);
#endif
+63
View File
@@ -0,0 +1,63 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef struct dlist_el_t {
struct dlist_el_t *next;
struct dlist_el_t *previous;
void *el_ptr;
} dlist_el_t;
typedef struct dlist_t {
dlist_el_t *start;
dlist_el_t *current;
size_t len;
} dlist_t;
void dlist_add(dlist_t *dlist, void *el_ptr, int el_siz, bool prepend);
dlist_t *dlist_init(void *el_ptr, int el_siz) {
dlist_t *dlist = malloc(sizeof(dlist_t));
dlist->len = 0;
dlist->current = NULL;
dlist->start = NULL;
if (el_ptr != NULL) {
dlist_add(dlist, el_ptr, el_siz, false);
}
return dlist;
}
void dlist_add(dlist_t *dlist, void *el_ptr, int el_siz, bool prepend) {
dlist_el_t *d_el = malloc(sizeof(dlist_el_t));
d_el->el_ptr = malloc(el_siz);
memcpy(d_el->el_ptr, el_ptr, el_siz);
if (dlist->start == NULL) {
d_el->previous = NULL;
d_el->next = NULL;
dlist->start = d_el;
dlist->current = d_el;
} else if (prepend) {
d_el->previous = NULL;
d_el->next = dlist->start;
dlist->start = d_el;
} else {
d_el->next = NULL;
dlist->current->next = d_el;
dlist->current = d_el;
}
dlist->len++;
}
void *dlist_get_current(dlist_t *dlist) {
return dlist->current->el_ptr;
}
void *dlist_it_prev(dlist_t *dlist) {
dlist->current = dlist->current->previous;
return dlist->current;
}
+5
View File
@@ -4,6 +4,7 @@
#include "main_window.h"
#include "file_list.h"
#include "fs_file_list.h"
#include "text_edit.h"
void main_window_set(app_t *app, enum main_window_type type) {
if (app->main_ui.ui != NULL) {
@@ -19,10 +20,12 @@ void main_window_set(app_t *app, enum main_window_type type) {
w_lfl_init_win(app);
break;
case mw_f_desc:
w_te_init_win(app);
break;
default:
break;
}
app_draw_bars(app);
}
void main_window_draw(app_t *app) {
@@ -34,6 +37,7 @@ void main_window_draw(app_t *app) {
w_lfl_draw((w_lfl_ui_t *)app->main_ui.ui);
break;
case mw_f_desc:
w_te_draw((w_te_ui_t *)app->main_ui.ui);
break;
}
app_draw_bbar(app);
@@ -48,6 +52,7 @@ void main_window_destroy(app_t *app) {
w_lfl_destroy((w_lfl_ui_t **)&(app->main_ui.ui));
break;
case mw_f_desc:
w_te_destroy((w_te_ui_t **)&(app->main_ui.ui));
break;
}
MAIN_UI_RESET(app);
+193
View File
@@ -0,0 +1,193 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 Oleksandr Zhylin */
#include <dirent.h>
#include <fcntl.h>
#include <ncursesw/ncurses.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <utils.h>
#include <wchar.h>
#include "text_edit.h"
#include "widget_core.h"
/* TODO: create dynamic array of endline positions */
/*
Массив d_array, состоящие из элементов, описывающий строки.
В строках записывать: длину, номер строки.
*/
static void w_te_cb(w_cb_args_t *args) {
app_t *app = args->app;
int32_t key = *((int32_t *)args->data);
w_te_ui_t *fui = app->query_args->main_ui->ui;
w_te_ui_line_t *new_line = NULL;
switch (key) {
case KEY_BACKSPACE:
case KEY_DL:
fui->current_line->text[fui->current_line->len--] = 0;
break;
case KEY_UP:
fui->cur_shift_pos.y--;
wchar_t *nl_ptr = NULL;
wchar_t *line_start = fui->line;
break;
case KEY_DOWN:
fui->cur_shift_pos.y++;
break;
case '\n':
new_line = malloc(sizeof(w_te_ui_line_t));
new_line->text = malloc(sizeof(wchar_t) * INBUFSIZE);
new_line->capacity = new_line->len = INBUFSIZE;
new_line->text[0] = 0;
new_line->len = 0;
u_d_array_append(&(fui->lines_arr), new_line, sizeof(w_te_ui_line_t));
fui->current_line = new_line;
break;
default:
fui->current_line->text[fui->current_line->len++] = key;
fui->current_line->text[fui->current_line->len] = 0;
}
}
static void w_te_cb_refresh(void *data) {
app_t *app = (app_t *)data;
if (app->main_ui.type != mw_f_desc) {
return;
}
w_te_ui_t *fui = app->main_ui.ui;
wnoutrefresh(fui->win);
}
static struct action_key action_keys[] = {
{.key = "Esc", .title = "Back", .code = '\33'},
{.key = "F9", .title = "Quit", .code = KEY_F(9)},
};
static int32_t process_user_input(app_t *app, w_cb_args_t *d_args) {
int32_t c;
w_te_ui_t *fui = (w_te_ui_t *)app->query_args->main_ui->ui;
c = wgetch(app->win);
switch (c) {
case KEY_F(9):
app_destroy(app, 0);
return OK;
case 'D':
case 'd':
// case '\33': /* ESC key */
// if (!app->modal.is_initiated) {
// server_send_string(app->query_args, "file list %d %d\n",
// fui->max_lines - 1, 1);
// main_window_set(app, mw_fl_server);
// d_args->element = app->main_ui.ui;
// break;
// }
default:
d_args->data = (void *)&c;
app->active_callback(d_args);
break;
}
return OK;
}
void w_te_reset_app(void *app);
w_te_ui_t *w_te_init_win(app_t *app) {
wchar_t top_text[INBUFSIZE];
int top_text_len = 0;
w_te_ui_t *fui = malloc(sizeof(w_te_ui_t));
app->main_ui.ui = fui;
app->main_ui.type = mw_f_desc;
app->main_ui.cb_ui_refresh = w_te_cb_refresh;
app->main_ui.b_keys = action_keys;
app->main_ui.b_keys_len = 2;
app->main_ui.cb_b_press = (w_cb_press_t)process_user_input;
app->main_ui.reset = w_te_reset_app;
app->main_ui.draw = (draw_f_t *)w_te_draw;
app->active_callback = w_te_cb;
fui->w.callback = w_te_cb;
w_init(&(fui->w), NULL, &(app->win), "");
fui->file_ptr = app->query_args->file;
fui->w.sz.x = getmaxx(app->win);
fui->w.sz.y = getmaxy(app->win);
fui->w.parent_win = &(app->win);
/* * INIT UI * */
fui->win = newwin(fui->w.sz.y - 4, fui->w.sz.x - 2, 2, 1);
top_text_len =
swprintf(top_text, INBUFSIZE, L"Description: %s", fui->file_ptr->name);
app->top_text = malloc(sizeof(wchar_t) * (top_text_len + 1));
wcsncpy(app->top_text, top_text, top_text_len);
app->top_text[top_text_len] = 0;
u_d_arr_ptr_init(&(fui->lines_arr), INBUFSIZE);
fui->current_line = malloc(sizeof(w_te_ui_line_t));
fui->current_line->text = malloc(INBUFSIZE * sizeof(wchar_t));
fui->current_line->len = 0;
fui->current_line->capacity = INBUFSIZE;
u_d_array_append(&(fui->lines_arr), fui->current_line, sizeof(w_te_ui_line_t));
return fui;
}
void w_te_reset(w_te_ui_t *fui) {
fui->line[0] = 0;
fui->cur_shift_pos.x = 0;
fui->cur_shift_pos.y = 0;
}
void w_te_reset_app(void *app) {
app_t *_app = app;
w_te_ui_t *fl_ui = (w_te_ui_t *)_app->main_ui.ui;
w_te_reset(fl_ui);
}
void w_te_draw(w_te_ui_t *fui) {
int32_t sz_y, sz_x;
int32_t p_y, p_x;
WINDOW *win = fui->win;
getmaxyx(win, sz_y, sz_x);
int32_t sz_y_f = sz_y - 1; // actual size (without box)
int32_t sz_x_f = sz_x - 1; // actual size (without box)
p_y = 0;
p_x = 0;
wattrset(win, COLOR_PAIR(modal_color_pair) | A_BOLD | A_REVERSE);
int i = 0;
for (i = 0; i < fui->w.sz.y; i++) {
mvwprintw(win, p_y + i, p_x, "%*s", fui->w.sz.x, "");
}
wchar_t *cur_ptr = fui->line;
wchar_t *nl_ptr = wcschr(cur_ptr, '\n');
w_te_ui_line_t *line_arr_el = NULL;
if (fui->lines_arr.length > 0) {
for (int i = 0; i < fui->lines_arr.length; i++) {
line_arr_el = (w_te_ui_line_t *)fui->lines_arr.arr[i];
mvwaddnwstr(win, p_y++, p_x, line_arr_el->text, line_arr_el->len);
}
}
fui->cur_abs_pos.y = p_y;
fui->cur_abs_pos.x = p_x;
p_x = 1;
wattroff(win, A_BOLD);
curs_set(true);
}
void w_te_destroy(w_te_ui_t **fui) {
w_te_ui_t *f = *fui;
free(*fui);
*fui = NULL;
}
+46
View File
@@ -0,0 +1,46 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 Oleksandr Zhylin */
#ifndef W_TEXT_EDIT_H /* LOCAL FILE LIST */
#define W_TEXT_EDIT_H
#include "../app.h"
#include <ncursesw/ncurses.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <wchar.h>
#include <widget_core.h>
typedef struct {
int len;
wchar_t *text; /* TODO: make dynamic */
int capacity;
} w_te_ui_line_t;
typedef struct {
w_t w;
WINDOW *win;
wchar_t line[INBUFSIZE * 10];
int line_cur_pos;
struct {
int x;
int y;
} cur_shift_pos;
struct {
int x;
int y;
} cur_abs_pos;
p_file_t *file_ptr;
w_te_ui_line_t *current_line;
d_array_ptr_t lines_arr;
} w_te_ui_t;
void w_te_draw(w_te_ui_t *fl_ui);
void w_te_destroy(w_te_ui_t **fui);
void w_te_reset(w_te_ui_t *fl_ui);
w_te_ui_t *w_te_init(WINDOW **win, w_t *w_parent);
w_te_ui_t *w_te_init_win(app_t *app);
#endif