diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-05-30 12:31:23 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-05-30 12:31:23 +0200 |
commit | 03862852b128748358dcb39ce50600eef0ba1a71 (patch) | |
tree | d5052d02d7bb31a866d5829eb05cfaf9b35bfa59 | |
parent | 555e5d2c0ce77dd1e031690c98ec9a1366182347 (diff) |
use ncurses instead of poopy ui library
-rw-r--r-- | client/i18n/en_us.h | 2 | ||||
-rw-r--r-- | client/main.h | 2 | ||||
-rw-r--r-- | client/makefile | 3 | ||||
-rw-r--r-- | client/serial.c | 15 | ||||
-rw-r--r-- | client/setup.c | 9 | ||||
-rw-r--r-- | client/term.h | 6 | ||||
-rw-r--r-- | client/term_linux.c | 29 | ||||
-rw-r--r-- | client/ui.c | 132 | ||||
-rw-r--r-- | client/ui.h | 50 | ||||
-rw-r--r-- | shared/bool.h | 2 |
10 files changed, 68 insertions, 182 deletions
diff --git a/client/i18n/en_us.h b/client/i18n/en_us.h index 4117ef6..80b9f09 100644 --- a/client/i18n/en_us.h +++ b/client/i18n/en_us.h @@ -4,3 +4,5 @@ #define W2_UI_CONN_STAT_DISCONNECTED "disconnected" #define W2_UI_CONN_STAT_PING "ping" #define W2_UI_BATT_STAT_BATTERY "battery" +#define W2_UI_EXPT_STAT_WARNINGS "warning(s)" +#define W2_UI_EXPT_STAT_ERRORS "error(s)" diff --git a/client/main.h b/client/main.h index 693db91..e581b3c 100644 --- a/client/main.h +++ b/client/main.h @@ -10,6 +10,8 @@ typedef struct { bool connected; uint8_t battery_level; + uint8_t mode; + w2_s_cmd_info_tx info; w2_s_cmd_sens_tx io; } w2_s_client_state; diff --git a/client/makefile b/client/makefile index f70f70f..e48eaeb 100644 --- a/client/makefile +++ b/client/makefile @@ -2,6 +2,7 @@ CC = gcc LD = gcc RM = rm -f CFLAGS = +LDFLAGS = -lncursesw EXECNAME = main include ../shared/os.mk @@ -18,7 +19,7 @@ OBJECTS := $(patsubst %.c,%.o, $(SOURCES)) $(CC) -c $(CFLAGS) $< $(EXECNAME): $(OBJECTS) - $(CC) $(OBJECTS) -o $(EXECNAME) + $(CC) $(LDFLAGS) $(OBJECTS) -o $(EXECNAME) clean:: $(RM) $(EXECNAME) *.o diff --git a/client/serial.c b/client/serial.c index 78f5631..e42bc0b 100644 --- a/client/serial.c +++ b/client/serial.c @@ -18,15 +18,16 @@ void w2_cmd_ping_tx(w2_s_bin *data) { if (g_w2_state.ping_id != cast->id) return; g_w2_state.ping = w2_timer_end(W2_TIMER_PING); g_w2_state.ping_received = true; - - printf("ping measured, %ims\n", g_w2_state.ping); } -void w2_cmd_expt_tx(w2_s_bin *data) { printf("w2_cmd_expt_tx()\n"); } -void w2_cmd_mode_tx(w2_s_bin *data) { printf("w2_cmd_mode_tx()\n"); } -void w2_cmd_cord_tx(w2_s_bin *data) { printf("w2_cmd_cord_tx()\n"); } -void w2_cmd_bomd_tx(w2_s_bin *data) { printf("w2_cmd_bomd_tx()\n"); } -void w2_cmd_sens_tx(w2_s_bin *data) { printf("w2_cmd_sens_tx()\n"); } +void w2_cmd_expt_tx(w2_s_bin *data) {} +void w2_cmd_mode_tx(w2_s_bin *data) { + W2_CAST_BIN(w2_s_cmd_mode_tx, data, cast); + g_w2_state.mode = cast->mode; +} +void w2_cmd_cord_tx(w2_s_bin *data) {} +void w2_cmd_bomd_tx(w2_s_bin *data) {} +void w2_cmd_sens_tx(w2_s_bin *data) {} void w2_cmd_info_tx(w2_s_bin *data) { memcpy(&g_w2_state.info, data->data, sizeof(w2_s_cmd_info_tx)); diff --git a/client/setup.c b/client/setup.c index 5eb6c13..59e43d1 100644 --- a/client/setup.c +++ b/client/setup.c @@ -1,12 +1,13 @@ #include <stdio.h> #include <stdlib.h> +#include <ncurses.h> #include "../shared/bin.h" #include "../shared/protocol.h" #include "commands.h" #include "serial.h" #include "setup.h" -#include "term.h" +#include "ui.h" // pointers for endianness check static const uint16_t _test = 1; @@ -24,7 +25,11 @@ void w2_client_setup(int argc, char **argv) { exit(1); } - w2_term_raw_mode(); + if ((g_w2_ui_win = initscr()) == NULL) { + printf("ncurses initscr() failed\n"); + exit(1); + } + noecho(); w2_cmd_setup_handlers(); diff --git a/client/term.h b/client/term.h deleted file mode 100644 index 8212149..0000000 --- a/client/term.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -/** set terminal to raw mode */ -void w2_term_raw_mode(); -/** set ui canvas props */ -void w2_term_props(); diff --git a/client/term_linux.c b/client/term_linux.c deleted file mode 100644 index cdab99d..0000000 --- a/client/term_linux.c +++ /dev/null @@ -1,29 +0,0 @@ -#ifdef W2_HOST_LINUX - -#include <fcntl.h> -#include <sys/ioctl.h> -#include <termios.h> -#include <unistd.h> - -#include "term.h" -#include "ui.h" - -void w2_term_raw_mode() { - fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); - struct termios term; - tcgetattr(STDIN_FILENO, &term); - term.c_lflag &= ~(ECHO | ICANON); - term.c_cc[VTIME] = 0; - term.c_cc[VMIN] = 1; - tcsetattr(STDIN_FILENO, 0, &term); -} - -void w2_term_props() { - struct winsize window; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &window); - - g_w2_ui_canvas.width = window.ws_col; - g_w2_ui_canvas.height = window.ws_row; -} - -#endif diff --git a/client/ui.c b/client/ui.c index 1f42db5..c47e1fb 100644 --- a/client/ui.c +++ b/client/ui.c @@ -1,4 +1,5 @@ #include <string.h> +#include <ncurses.h> #include "../shared/bin.h" #include "../shared/util.h" @@ -7,113 +8,54 @@ #include "term.h" #include "ui.h" -w2_s_ui_tty_canvas g_w2_ui_canvas; +WINDOW *g_w2_ui_win; +unsigned int g_w2_ui_width = 0; +unsigned int g_w2_ui_height = 0; void w2_ui_main() { - w2_ui_update(); - w2_ui_paint(); -} - -void w2_ui_update() { w2_term_props(); } - -void w2_ui_paint() { w2_ui_paint_statusbar(); } - -void w2_ui_align(char *text, w2_e_alignment align, unsigned int length) { - unsigned int padding = 0; + g_w2_ui_width = getmaxx(g_w2_ui_win); + g_w2_ui_height = getmaxy(g_w2_ui_win); - switch (align) { - case W2_UI_ALIGN_LEFT: { - break; - } - case W2_UI_ALIGN_CENTER: { - unsigned int middle = length / 2; - unsigned int offset = strlen(text) / 2; - padding = W2_MAX(0, middle - offset); - break; - } - case W2_UI_ALIGN_RIGHT: { - unsigned int right = length; - unsigned int offset = strlen(text); - padding = W2_MAX(0, right - offset); - break; - } - } - - char *temp = calloc(length, 1); - sprintf(temp, "%*s%s", padding, "", text); - memcpy(text, temp, length); - free(temp); -} - -char *w2_ui_pt_sb_con_sts() { - char *connected = - g_w2_state.connected ? W2_UI_CONN_STAT_CONNECTED : W2_UI_CONN_STAT_DISCONNECTED; - char *out = calloc(g_w2_ui_canvas.width, 1); - sprintf(out, "%s, %ims %s", connected, g_w2_state.ping, W2_UI_CONN_STAT_PING); - w2_ui_align(out, W2_UI_ALIGN_LEFT, g_w2_ui_canvas.width); - return out; -} -char *w2_ui_pt_sb_ver_num() { - char *out = calloc(g_w2_ui_canvas.width, 1); - sprintf(out, "(%s)", g_w2_state.info.build_str); - w2_ui_align(out, W2_UI_ALIGN_CENTER, g_w2_ui_canvas.width); - return out; -} -char *w2_ui_pt_sb_bat_sts() { - char *out = calloc(g_w2_ui_canvas.width, 1); - sprintf(out, "%s %i%%", W2_UI_BATT_STAT_BATTERY, g_w2_state.battery_level); - w2_ui_align(out, W2_UI_ALIGN_RIGHT, g_w2_ui_canvas.width); - return out; + w2_ui_paint(); } -void w2_ui_nullsub(char *str, unsigned int length) { - for (int i = 0; i < length; i++) - if (str[i] == 0x00) str[i] = ' '; +void w2_ui_paint() { + w2_ui_paint_statusbar(); + refresh(); } -void w2_ui_overlay(char *bottom, char *top, unsigned int length) { - w2_ui_nullsub(bottom, length); - w2_ui_nullsub(top, length); - - unsigned int start = 0; - for (int i = 0; i < length; i++) { - start = i; - if (top[start] != ' ') break; - } - unsigned int stop = 0; - for (int i = 0; i < length; i++) { - stop = length - i; - if (top[stop] != ' ') break; - } - - for (int i = 0; i < length; i++) { - if (i >= start && i <= stop) bottom[i] = top[i]; - } -} +void w2_ui_paint_statusbar() { + char temp[g_w2_ui_width + 1]; + sprintf(temp, "%s, %ims %s", + g_w2_state.connected ? W2_UI_CONN_STAT_CONNECTED : W2_UI_CONN_STAT_DISCONNECTED, + g_w2_state.ping, + W2_UI_CONN_STAT_PING); + mvaddstr(0, 0, temp); -// char* w2_ui_pt_sb_logic_mode() {} -// char* w2_ui_pt_sb_exceptions() {} -// char* w2_ui_pt_sb_tabbar() {} + sprintf(temp, "(%s)", g_w2_state.info.build_str); + mvaddstr(0, g_w2_ui_width / 2 - strlen(temp) / 2, temp); -void w2_ui_paint_statusbar() { - char top_row[g_w2_ui_canvas.width]; + sprintf(temp, "%s %i%%", W2_UI_BATT_STAT_BATTERY, g_w2_state.battery_level); + mvaddstr(0, g_w2_ui_width - strlen(temp), temp); - char *con_sts = w2_ui_pt_sb_con_sts(); - char *ver_num = w2_ui_pt_sb_ver_num(); - char *bat_sts = w2_ui_pt_sb_bat_sts(); + sprintf(temp, "[mode 0x%02x]", g_w2_state.mode); + mvaddstr(1, 0, temp); - w2_ui_overlay(top_row, con_sts, g_w2_ui_canvas.width); - w2_ui_overlay(top_row, ver_num, g_w2_ui_canvas.width); - w2_ui_overlay(top_row, bat_sts, g_w2_ui_canvas.width); + sprintf(temp, "%i %s, %i %s", + 0, W2_UI_EXPT_STAT_WARNINGS, + 0, W2_UI_EXPT_STAT_ERRORS); + mvaddstr(1, g_w2_ui_width - strlen(temp), temp); - printf("%s", top_row); + w2_ui_paint_tabbar(); - free(con_sts); - free(ver_num); - free(bat_sts); + for (unsigned int i = 0; i < g_w2_ui_width; i++) temp[i] = '-'; + temp[g_w2_ui_width] = 0; + mvaddstr(3, 0, temp); +} - // w2_ui_pt_sb_connection_status(); - // w2_ui_pt_sb_connection_status(); - // w2_ui_pt_sb_connection_status(); - printf("\n"); +void w2_ui_paint_tabbar() { + char temp[g_w2_ui_width]; + sprintf(temp, "-- tab bar here --"); + mvaddstr(2, g_w2_ui_width / 2 - strlen(temp) / 2, temp); } + diff --git a/client/ui.h b/client/ui.h index 610e3fc..06a8cea 100644 --- a/client/ui.h +++ b/client/ui.h @@ -1,35 +1,11 @@ #pragma once #include <stdint.h> +#include <ncurses.h> -#include "../shared/bool.h" - -typedef struct { - unsigned int width; - unsigned int height; - unsigned int cursor_pos; -} w2_s_ui_tty_canvas; - -typedef enum { - W2_UI_COL_BLK, - W2_UI_COL_RED, - W2_UI_COL_GRN, - W2_UI_COL_YEL, - W2_UI_COL_BLU, - W2_UI_COL_MAG, - W2_UI_COL_CYN, - W2_UI_COL_WHT, - W2_UI_COL_RST, -} w2_e_colors; - -typedef enum { - W2_UI_ALIGN_LEFT, - W2_UI_ALIGN_CENTER, - W2_UI_ALIGN_RIGHT, -} w2_e_alignment; - -extern bool g_w2_ui_enable_color; -extern w2_s_ui_tty_canvas g_w2_ui_canvas; +extern WINDOW *g_w2_ui_win; +extern unsigned int g_w2_ui_width; +extern unsigned int g_w2_ui_height; /** update terminal props */ void w2_ui_update(); @@ -37,21 +13,11 @@ void w2_ui_update(); void w2_ui_clear(); /** draw complete ui */ void w2_ui_paint(); -/** draw status bar */ -void w2_ui_paint_statusbar(); /** update and paint */ void w2_ui_main(); -/** get ansi color code for foreground color */ -char *w2_ui_set_fg(w2_e_colors color); -/** get ansi color code for background color */ -char *w2_ui_set_bg(w2_e_colors color); - -/** align `text` `align` with `text` buffer length `length` */ -void w2_ui_align(char *text, w2_e_alignment align, unsigned int length); +/** draw status bar */ +void w2_ui_paint_statusbar(); +/** draw tab bar */ +void w2_ui_paint_tabbar(); -/** - * trim spaces from `top` and overlay on top of `bottom` - * both strings should be at least `length` long - */ -void w2_ui_overlay(char *bottom, char *top, unsigned int length); diff --git a/shared/bool.h b/shared/bool.h index 9ea97f7..4ffb6b4 100644 --- a/shared/bool.h +++ b/shared/bool.h @@ -4,6 +4,8 @@ /** @file bool.h */ +#ifndef bool typedef uint8_t bool; #define false 0 /* NOLINT */ #define true 1 /* NOLINT */ +#endif |