From 9b4826f9cbfbc56d8cbf1a7c3650d0541d1130a2 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Fri, 28 Jun 2019 16:25:52 +0200 Subject: [PATCH] begin framework for command handling --- ncdc/CMakeLists.txt | 2 ++ ncdc/include/ncdc/cmds.h | 19 +++++++++++++++++++ ncdc/include/ncdc/ncdc.h | 2 ++ ncdc/src/cmds.c | 11 +++++++++++ ncdc/src/mainwindow.c | 25 +++++++++++++++++++++++++ ncdc/src/util.c | 14 ++++++++++++++ 6 files changed, 73 insertions(+) create mode 100644 ncdc/include/ncdc/cmds.h create mode 100644 ncdc/src/cmds.c diff --git a/ncdc/CMakeLists.txt b/ncdc/CMakeLists.txt index 2f7e921..f9c22b1 100644 --- a/ncdc/CMakeLists.txt +++ b/ncdc/CMakeLists.txt @@ -7,9 +7,11 @@ PKG_CHECK_MODULES(EDIT REQUIRED libedit) SET(TARGET "ncdc") SET(SOURCES + "include/ncdc/cmds.h" "include/ncdc/input.h" "include/ncdc/mainwindow.h" "include/ncdc/ncdc.h" + "src/cmds.c" "src/emacs.c" "src/input.c" "src/mainwindow.c" diff --git a/ncdc/include/ncdc/cmds.h b/ncdc/include/ncdc/cmds.h new file mode 100644 index 0000000..5c5f6de --- /dev/null +++ b/ncdc/include/ncdc/cmds.h @@ -0,0 +1,19 @@ +#ifndef NCDC_CMDS_H +#define NCDC_CMDS_H + +#include +#include + +typedef bool (*ncdc_command_t)(ncdc_mainwindow_t n, + wchar_t const *s, size_t len); + +typedef struct { + wchar_t const *name; + ncdc_command_t handler; +} ncdc_commands_t; + +extern ncdc_commands_t cmds[]; + +bool ncdc_cmd_quit(ncdc_mainwindow_t n, wchar_t const *s, size_t len); + +#endif diff --git a/ncdc/include/ncdc/ncdc.h b/ncdc/include/ncdc/ncdc.h index eb656ae..f52777d 100644 --- a/ncdc/include/ncdc/ncdc.h +++ b/ncdc/include/ncdc/ncdc.h @@ -39,4 +39,6 @@ extern char *ncdc_private_dir; int strwidth(char const *string); char *read_char(FILE *stream); +wchar_t const *w_next_word(wchar_t const *w, ssize_t len); + #endif diff --git a/ncdc/src/cmds.c b/ncdc/src/cmds.c new file mode 100644 index 0000000..7402a58 --- /dev/null +++ b/ncdc/src/cmds.c @@ -0,0 +1,11 @@ +#include + +ncdc_commands_t cmds[] = { + { L"quit", ncdc_cmd_quit }, + { NULL, NULL } +}; + +bool ncdc_cmd_quit(ncdc_mainwindow_t n, wchar_t const *s, size_t len) +{ + exit(0); +} diff --git a/ncdc/src/mainwindow.c b/ncdc/src/mainwindow.c index 8a6edb3..58c302e 100644 --- a/ncdc/src/mainwindow.c +++ b/ncdc/src/mainwindow.c @@ -1,5 +1,6 @@ #include #include +#include #include typedef enum { @@ -41,6 +42,8 @@ struct ncdc_mainwindow_ static void ncdc_mainwindow_resize(ncdc_mainwindow_t n); static void ncdc_mainwindow_update_focus(ncdc_mainwindow_t n); +static bool ncdc_mainwindow_callback(ncdc_input_t i, wchar_t const *s, + size_t len, void *arg); static void ncdc_mainwindow_free(ncdc_mainwindow_t n) { @@ -66,6 +69,7 @@ ncdc_mainwindow_t ncdc_mainwindow_new(void) ptr->ref.cleanup = (dc_cleanup_t)ncdc_mainwindow_free; ptr->in = ncdc_input_new(); + ncdc_input_set_callback(ptr->in, ncdc_mainwindow_callback, ptr); ptr->guilds = newwin(5, 5, 1, 1); ptr->chat = newwin(5, 5, 4, 4); @@ -84,6 +88,27 @@ ncdc_mainwindow_t ncdc_mainwindow_new(void) return ptr; } +static bool +ncdc_mainwindow_callback(ncdc_input_t i, wchar_t const *s, + size_t len, void *arg) +{ + ncdc_mainwindow_t mainwin = (ncdc_mainwindow_t)arg; + + if (s[0] == '/') { + size_t i = 0; + wchar_t const *n = w_next_word(s, len); + + for (; cmds[i].name != NULL; i++) { + if (wcsncmp(s+1, cmds[i].name, (n-s-1)) == 0) { + cmds[i].handler(mainwin, s, len); + return true; + } + } + } + + return false; +} + static void ncdc_mainwindow_resize(ncdc_mainwindow_t n) { n->guilds_h = LINES - 2; diff --git a/ncdc/src/util.c b/ncdc/src/util.c index 0b836b7..836cf37 100644 --- a/ncdc/src/util.c +++ b/ncdc/src/util.c @@ -1,5 +1,19 @@ #include +wchar_t const *w_next_word(wchar_t const *w, ssize_t len) +{ + size_t i = 0; + + if (len < 0) { + len = wcslen(w); + } + + for (; !iswspace(w[i]) && i < len; i++) + ; + + return w+i; +} + char *read_char(FILE *stream) { uint8_t str[7] = {0};