function for creating windows with borders and title

This commit is contained in:
Max 2024-03-01 01:04:07 +01:00
parent e6e43fc9f0
commit 11226d1495
14 changed files with 112 additions and 36 deletions

View File

@ -16,3 +16,4 @@ target_include_directories(netex PRIVATE ${CMAKE_SOURCE_DIR}/src/net/include)
add_subdirectory(include) add_subdirectory(include)
add_subdirectory(str) add_subdirectory(str)
add_subdirectory(tui)

View File

@ -4,5 +4,6 @@ target_sources(netex PUBLIC
"config.h" "config.h"
"net.h" "net.h"
"netex.h" "netex.h"
"nx_tui.h"
"sockets.h" "sockets.h"
"strutil.h") "strutil.h")

18
src/net/include/nx_tui.h Normal file
View File

@ -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

View File

@ -5,25 +5,24 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
#include <signal.h>
#include "netex.h" #include "netex.h"
#include "config.h" #include "config.h"
#include "nx_tui.h"
#define PORT_NUM_LENGHT 6 #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) void netex_init(void)
{ {
// Curses signal(SIGINT, handle_exit);
initscr(); signal(SIGWINCH, handle_term_resize);
noecho(); // Stop printing key presses
if (has_colors() && can_change_color()) setup_curses();
{
start_color();
PRINT_LINE("%i colors available, %i color pairs available.", COLORS, COLOR_PAIRS);
//TODO: Make color pairs
}
// OpenSSL // OpenSSL
SSL_load_error_strings(); SSL_load_error_strings();
SSL_library_init(); SSL_library_init();
@ -32,11 +31,10 @@ void netex_init(void)
void netex_shutdown(void) void netex_shutdown(void)
{ {
shutdown_curses();
// OpenSSL // OpenSSL
ERR_free_strings(); ERR_free_strings();
EVP_cleanup(); EVP_cleanup();
// Curses
endwin();
} }
int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv_type) int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv_type)
@ -194,3 +192,13 @@ void free_host_info(HOST_INFO* host_info)
free(host_info->ip); free(host_info->ip);
free(host_info); 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.
}

View File

@ -0,0 +1,2 @@
target_sources(netex PRIVATE
"nx_curses.c")

33
src/net/tui/nx_curses.c Normal file
View File

@ -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();
}

View File

@ -2,7 +2,10 @@ add_executable(netex_svr
"main.c" "main.c"
"server.c" "server.c"
"srv_tui.c" "srv_tui.c"
"srv_tui.h"
"server.h") "server.h")
target_include_directories(netex_svr PRIVATE ${CMAKE_SOURCE_DIR}/src/net/include) target_include_directories(netex_svr PRIVATE ${CMAKE_SOURCE_DIR}/src/net/include)
target_link_libraries(netex_svr netex) target_link_libraries(netex_svr netex)
add_subdirectory(tui)

View File

@ -21,7 +21,6 @@ CONFIGURATION* srv_configuration;
int server_sockfd; int server_sockfd;
struct sockaddr_in server_sockaddr; struct sockaddr_in server_sockaddr;
pthread_t listen_thread; pthread_t listen_thread;
bool b_listen;
void server_listen(); void server_listen();
@ -38,10 +37,8 @@ int initialize_server(int argc, char* argv[])
config_set_string(srv_configuration, "Connection.IPv", "Double"); config_set_string(srv_configuration, "Connection.IPv", "Double");
config_set_string(srv_configuration, "Connection.Crypto.CertPath", "cert.pem"); config_set_string(srv_configuration, "Connection.Crypto.CertPath", "cert.pem");
if (config_save_to_path(srv_configuration, CONFIG_PATH) != 0) if (config_save_to_path(srv_configuration, CONFIG_PATH) != 0)
{
WARN("Failed to save configuration to disk, default config is only available in memory!"); WARN("Failed to save configuration to disk, default config is only available in memory!");
} }
}
// Socket // Socket
const int64_t portnum = config_get_int(srv_configuration, "Connection.Port"); const int64_t portnum = config_get_int(srv_configuration, "Connection.Port");
char* hostname = config_get_string(srv_configuration, "Connection.BindIP"); char* hostname = config_get_string(srv_configuration, "Connection.BindIP");

View File

@ -1,19 +1,13 @@
//
// Created by max on 28-1-24.
//
#include <ncurses.h> #include <ncurses.h>
#include "netex.h" #include "netex.h"
#include "server.h" #include "server.h"
#include "srv_tui.h"
// INFO #include "nx_tui.h"
// https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
//
#define PAIR_BG_WIN_COLOR 2
WINDOW *info_win, *log_win; WINDOW *info_win, *log_win, *log_pad;
int setup_windows(void) int setup_windows(void)
{ {
@ -23,23 +17,19 @@ int setup_windows(void)
// Windows // Windows
int ymax,xmax; int ymax,xmax;
getmaxyx(stdscr, ymax, xmax); getmaxyx(stdscr, ymax, xmax);
info_win = newwin(GET_PERC(ymax, 20), xmax-1, 1, 0); info_win = create_base_window_box(" Info ",GET_PERC(ymax, 20), xmax-1, 1, 0);
log_win = newwin(ymax-16, GET_PERC(xmax, 50), GET_PERC(ymax, 20) + 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(); 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++) for (int i = 0; i != 15; i++)
{ {
mvwprintw(log_win, i + 1, 1, "Test!"); waddstr(log_pad, "Test!");
} }
wrefresh(info_win); wrefresh(info_win);
wrefresh(log_win); wrefresh(log_win);
touchwin(log_win);
prefresh(log_pad, 0, 0, 10, 10, 10, 10);
return 0; return 0;
} }

14
src/server/srv_tui.h Normal file
View File

@ -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

View File

@ -0,0 +1,3 @@
# server/tui
target_sources(netex_svr PRIVATE
"info_win.c")

View File

@ -0,0 +1,6 @@
//
// Created by max on 10-2-24.
//
#include <ncurses.h>