diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt index c0f36d0..a21ebdf 100644 --- a/src/net/CMakeLists.txt +++ b/src/net/CMakeLists.txt @@ -16,3 +16,4 @@ target_include_directories(netex PRIVATE ${CMAKE_SOURCE_DIR}/src/net/include) add_subdirectory(include) add_subdirectory(str) +add_subdirectory(tui) diff --git a/src/net/include/CMakeLists.txt b/src/net/include/CMakeLists.txt index 40773f1..858b6ff 100644 --- a/src/net/include/CMakeLists.txt +++ b/src/net/include/CMakeLists.txt @@ -4,5 +4,6 @@ target_sources(netex PUBLIC "config.h" "net.h" "netex.h" + "nx_tui.h" "sockets.h" "strutil.h") \ No newline at end of file diff --git a/src/net/include/nx_tui.h b/src/net/include/nx_tui.h new file mode 100644 index 0000000..e819bb6 --- /dev/null +++ b/src/net/include/nx_tui.h @@ -0,0 +1,18 @@ +#ifndef NX_TUI_H +#define NX_TUI_H + +/// Color pairs + +#define DEF_COLOR_PAIR 1 +#define DEF_WIN_COLOR 2 +#define LOG_PREFIX_COLOR 3 +#define CHAT_PREFIX_COLOR 4 + + +int setup_curses(void); + +WINDOW* create_base_window_box(const char* title, const int nlines, const int ncols, const int begin_y, const int begin_x); + +void shutdown_curses(void); + +#endif //NX_TUI_H diff --git a/src/net/netex.c b/src/net/netex.c index 3772020..dd9c7fe 100644 --- a/src/net/netex.c +++ b/src/net/netex.c @@ -5,25 +5,24 @@ #include #include #include +#include #include "netex.h" #include "config.h" +#include "nx_tui.h" #define PORT_NUM_LENGHT 6 -// https://invisible-island.net/ncurses/ +void handle_exit(int c); +void handle_term_resize(int c); void netex_init(void) { - // Curses - initscr(); - noecho(); // Stop printing key presses - if (has_colors() && can_change_color()) - { - start_color(); - PRINT_LINE("%i colors available, %i color pairs available.", COLORS, COLOR_PAIRS); - //TODO: Make color pairs - } + signal(SIGINT, handle_exit); + signal(SIGWINCH, handle_term_resize); + + setup_curses(); + // OpenSSL SSL_load_error_strings(); SSL_library_init(); @@ -32,11 +31,10 @@ void netex_init(void) void netex_shutdown(void) { + shutdown_curses(); // OpenSSL ERR_free_strings(); EVP_cleanup(); - // Curses - endwin(); } int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv_type) @@ -193,4 +191,14 @@ void free_host_info(HOST_INFO* host_info) if (host_info->ip != NULL) free(host_info->ip); free(host_info); +} + +void handle_exit(int c) +{ + netex_shutdown(); + exit(OK); +} +void handle_term_resize(int c) +{ + //TODO: Set parameter for ncurses code to check if window is resized. } \ No newline at end of file diff --git a/src/list.h b/src/net/old/list.h similarity index 100% rename from src/list.h rename to src/net/old/list.h diff --git a/src/main.c b/src/net/old/main.c similarity index 100% rename from src/main.c rename to src/net/old/main.c diff --git a/src/net/tui/CMakeLists.txt b/src/net/tui/CMakeLists.txt new file mode 100644 index 0000000..2e09d6c --- /dev/null +++ b/src/net/tui/CMakeLists.txt @@ -0,0 +1,2 @@ +target_sources(netex PRIVATE + "nx_curses.c") \ No newline at end of file diff --git a/src/net/tui/nx_curses.c b/src/net/tui/nx_curses.c new file mode 100644 index 0000000..7cc90a2 --- /dev/null +++ b/src/net/tui/nx_curses.c @@ -0,0 +1,33 @@ +#include "netex.h" +#include "nx_tui.h" + +int setup_curses(void) +{ + initscr(); + noecho(); // Stop printing key presses + if (has_colors() && can_change_color()) + { + start_color(); + PRINT_LINE("%i colors available, %i color pairs available.", COLORS, COLOR_PAIRS); + + init_pair(DEF_COLOR_PAIR, COLOR_WHITE, COLOR_BLACK); + init_pair(DEF_WIN_COLOR, COLOR_GREEN, COLOR_BLACK); + + } + return 0; +} + +WINDOW* create_base_window_box(const char* title, const int nlines, const int ncols, const int begin_y, const int begin_x) +{ + WINDOW* win = newwin(nlines, ncols, begin_y, begin_x); + if (win == NULL) + return NULL; + box(win, 0, 0); + mvwprintw(win, 0, 1, title); + return win; +} + +void shutdown_curses(void) +{ + endwin(); +} \ No newline at end of file diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt index 576cbcf..1e88d05 100644 --- a/src/server/CMakeLists.txt +++ b/src/server/CMakeLists.txt @@ -2,7 +2,10 @@ add_executable(netex_svr "main.c" "server.c" "srv_tui.c" + "srv_tui.h" "server.h") target_include_directories(netex_svr PRIVATE ${CMAKE_SOURCE_DIR}/src/net/include) -target_link_libraries(netex_svr netex) \ No newline at end of file +target_link_libraries(netex_svr netex) + +add_subdirectory(tui) \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 15de8ed..3b7a842 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -21,7 +21,6 @@ CONFIGURATION* srv_configuration; int server_sockfd; struct sockaddr_in server_sockaddr; pthread_t listen_thread; -bool b_listen; void server_listen(); @@ -38,9 +37,7 @@ int initialize_server(int argc, char* argv[]) config_set_string(srv_configuration, "Connection.IPv", "Double"); config_set_string(srv_configuration, "Connection.Crypto.CertPath", "cert.pem"); if (config_save_to_path(srv_configuration, CONFIG_PATH) != 0) - { WARN("Failed to save configuration to disk, default config is only available in memory!"); - } } // Socket const int64_t portnum = config_get_int(srv_configuration, "Connection.Port"); diff --git a/src/server/srv_tui.c b/src/server/srv_tui.c index 66e0cb7..dcfd73d 100644 --- a/src/server/srv_tui.c +++ b/src/server/srv_tui.c @@ -1,19 +1,13 @@ -// -// Created by max on 28-1-24. -// - #include #include "netex.h" #include "server.h" +#include "srv_tui.h" -// INFO -// https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ -// +#include "nx_tui.h" -#define PAIR_BG_WIN_COLOR 2 -WINDOW *info_win, *log_win; +WINDOW *info_win, *log_win, *log_pad; int setup_windows(void) { @@ -23,23 +17,19 @@ int setup_windows(void) // Windows int ymax,xmax; getmaxyx(stdscr, ymax, xmax); - info_win = newwin(GET_PERC(ymax, 20), xmax-1, 1, 0); - log_win = newwin(ymax-16, GET_PERC(xmax, 50), GET_PERC(ymax, 20) + 1, 0); + info_win = create_base_window_box(" Info ",GET_PERC(ymax, 20), xmax-1, 1, 0); + log_win = create_base_window_box(" Log window ", ymax-16, GET_PERC(xmax, 50), GET_PERC(ymax, 20) + 1, 0); + log_pad = subpad(log_win, ymax-15, GET_PERC(xmax, 50) - 1, 1, 1); refresh(); - wattron(info_win, COLOR_PAIR(PAIR_BG_WIN_COLOR)); - box(info_win, 0, 0); - wattroff(info_win, COLOR_PAIR(PAIR_BG_WIN_COLOR)); - wattron(log_win, COLOR_PAIR(PAIR_BG_WIN_COLOR)); - box(log_win, 0, 0); - wattroff(log_win, COLOR_PAIR(PAIR_BG_WIN_COLOR)); - mvwprintw(info_win, 0, 1, " Info window "); - mvwprintw(log_win, 0, 1, " Log window "); + for (int i = 0; i != 15; i++) { - mvwprintw(log_win, i + 1, 1, "Test!"); + waddstr(log_pad, "Test!"); } wrefresh(info_win); wrefresh(log_win); + touchwin(log_win); + prefresh(log_pad, 0, 0, 10, 10, 10, 10); return 0; } diff --git a/src/server/srv_tui.h b/src/server/srv_tui.h new file mode 100644 index 0000000..8c8e3f8 --- /dev/null +++ b/src/server/srv_tui.h @@ -0,0 +1,14 @@ +// +// Created by max on 10-2-24. +// +// https://invisible-island.net/ncurses/ +// https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ + +#ifndef SRV_TUI_H +#define SRV_TUI_H + +// Color pairs + +#define PAIR_BG_WIN_COLOR 1 + +#endif //SRV_TUI_H diff --git a/src/server/tui/CMakeLists.txt b/src/server/tui/CMakeLists.txt new file mode 100644 index 0000000..83e5321 --- /dev/null +++ b/src/server/tui/CMakeLists.txt @@ -0,0 +1,3 @@ +# server/tui +target_sources(netex_svr PRIVATE + "info_win.c") \ No newline at end of file diff --git a/src/server/tui/info_win.c b/src/server/tui/info_win.c new file mode 100644 index 0000000..db07b3d --- /dev/null +++ b/src/server/tui/info_win.c @@ -0,0 +1,6 @@ +// +// Created by max on 10-2-24. +// + +#include +