From 5f87810f81601efc9fa16e2e183cd5749dcff632 Mon Sep 17 00:00:00 2001 From: xolatile Date: Sun, 29 Oct 2023 11:20:49 -0400 Subject: [PATCH] Changed signal system... --- xurses.c | 85 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/xurses.c b/xurses.c index 470c2c6..659dce4 100644 --- a/xurses.c +++ b/xurses.c @@ -26,11 +26,15 @@ It is distributed in the hope that it will be useful or harmful, it really depen /* Internal variable definitions. */ +static char curses_format [CURSES_FORMAT + 1] = "\033[-;3-m-\033[0m"; +static char curses_cursor [CURSES_CURSOR + 1] = "\033[---;---H"; + static char * curses_screen = NULL; -static char curses_format [CURSES_FORMAT + 1] = "\033[-;3-m-\033[0m"; +static int curses_action_count = 0; +static int * curses_activator = NULL; -static void (* curses_action [SIGNAL_COUNT]) (void) = { 0 }; +static void (* * curses_action) (void) = NULL; static struct termios curses_old_terminal; static struct termios curses_new_terminal; @@ -38,7 +42,9 @@ static struct termios curses_new_terminal; /* Internal function definitions. */ static void curses_free (void) { - curses_screen = deallocate (curses_screen); + curses_screen = deallocate (curses_screen); + curses_activator = deallocate (curses_activator); + curses_action = deallocate (curses_action); terminal_clear (); @@ -93,6 +99,10 @@ static void curses_idle (void) { return; } +static void curses_exit (void) { + curses_active = 0; +} + /* External variable definitions. */ int curses_realign_x = 0; @@ -108,8 +118,6 @@ int curses_active = 1; void curses_configure (void) { struct winsize screen_dimension; - char signal = 0; - atexit (curses_free); fatal_failure (ioctl (STDOUT_FILENO, TIOCGWINSZ, & screen_dimension) == -1, "ioctl: Failed to get terminal dimensions."); @@ -133,9 +141,7 @@ void curses_configure (void) { curses_screen = allocate (CURSES_REVERT + CURSES_FORMAT * curses_screen_width * curses_screen_height + CURSES_CURSOR + 1); - for (signal = SIGNAL_NONE; signal != SIGNAL_COUNT; ++signal) { - curses_unbind ((char) signal); - } + curses_bind (SIGNAL_ESCAPE, curses_exit); terminal_clear (); @@ -143,44 +149,61 @@ void curses_configure (void) { } void curses_synchronize (void) { - curses_signal = '\0'; + int signal; - out (curses_screen, CURSES_REVERT + CURSES_FORMAT * curses_screen_width * curses_screen_height); + curses_signal = signal = 0; - in (& curses_signal, 4); + out (curses_screen, CURSES_REVERT + CURSES_FORMAT * curses_screen_width * curses_screen_height + CURSES_CURSOR); - switch (curses_signal) { - case '\033': curses_signal = SIGNAL_ESCAPE; break; - case '0': curses_signal = SIGNAL_0; break; - case 'q': curses_signal = SIGNAL_Q; break; - case 'Q': curses_signal = SIGNAL_Q | SIGNAL_SHIFT; break; - case 'w': curses_signal = SIGNAL_W; break; - case 's': curses_signal = SIGNAL_S; break; - case 'a': curses_signal = SIGNAL_A; break; - case 'd': curses_signal = SIGNAL_D; break; - default: curses_signal = SIGNAL_NONE; break; + in (& signal, 4); + + if ((char) signal == '\033') { + curses_signal |= SIGNAL_ESCAPE; + } else if (character_is_digit ((char) signal) != 0) { + curses_signal |= SIGNAL_0 + (int) ((char) signal - '0'); + } else if (character_is_lowercase ((char) signal) != 0) { + curses_signal |= SIGNAL_A + (int) ((char) signal - 'a'); + } else if (character_is_uppercase ((char) signal) != 0) { + curses_signal |= SIGNAL_A + (int) ((char) signal - 'A'); + curses_signal |= SIGNAL_SHIFT; + } else { + curses_signal = SIGNAL_NONE; } - if (curses_signal == SIGNAL_ESCAPE) { - curses_active = 0; - return; - } - - if ((curses_signal > SIGNAL_ANY) && (curses_signal < SIGNAL_COUNT)) { - curses_action [curses_signal] (); + for (signal = 0; signal != curses_action_count; ++signal) { + if (curses_signal == curses_activator [signal]) { + curses_action [signal] (); + } } } void curses_bind (int signal, void (* action) (void)) { - curses_action [signal] = action; + ++curses_action_count; + + curses_activator = reallocate (curses_activator, curses_action_count * (int) sizeof (* curses_activator)); + curses_action = reallocate (curses_action, curses_action_count * (int) sizeof (* curses_action)); + + curses_activator [curses_action_count - 1] = signal; + curses_action [curses_action_count - 1] = action; } void curses_unbind (int signal) { - curses_action [signal] = curses_idle; + (void) signal; + + curses_activator [curses_action_count - 1] = SIGNAL_NONE; + curses_action [curses_action_count - 1] = curses_idle; + + --curses_action_count; } void curses_render_cursor (int x, int y) { - terminal_show_cursor (x + y); + x %= 1000; + y %= 1000; + + string_copy_limit (curses_cursor + 2, string_realign (number_to_string (x), 3, '0'), 3); + string_copy_limit (curses_cursor + 6, string_realign (number_to_string (y), 3, '0'), 3); + + string_copy_limit (& curses_screen [CURSES_REVERT + CURSES_FORMAT * curses_screen_width * curses_screen_height], curses_cursor, CURSES_CURSOR); } void curses_render_character (char character, int colour, int effect, int x, int y) {