Changed signal system...

This commit is contained in:
Ognjen Milan Robovic 2023-10-29 11:20:49 -04:00
parent 7b6f33f39b
commit 5f87810f81

View File

@ -26,11 +26,15 @@ It is distributed in the hope that it will be useful or harmful, it really depen
/* Internal variable definitions. */ /* 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_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_old_terminal;
static struct termios curses_new_terminal; static struct termios curses_new_terminal;
@ -39,6 +43,8 @@ static struct termios curses_new_terminal;
static void curses_free (void) { 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 (); terminal_clear ();
@ -93,6 +99,10 @@ static void curses_idle (void) {
return; return;
} }
static void curses_exit (void) {
curses_active = 0;
}
/* External variable definitions. */ /* External variable definitions. */
int curses_realign_x = 0; int curses_realign_x = 0;
@ -108,8 +118,6 @@ int curses_active = 1;
void curses_configure (void) { void curses_configure (void) {
struct winsize screen_dimension; struct winsize screen_dimension;
char signal = 0;
atexit (curses_free); atexit (curses_free);
fatal_failure (ioctl (STDOUT_FILENO, TIOCGWINSZ, & screen_dimension) == -1, "ioctl: Failed to get terminal dimensions."); 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); 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_bind (SIGNAL_ESCAPE, curses_exit);
curses_unbind ((char) signal);
}
terminal_clear (); terminal_clear ();
@ -143,44 +149,61 @@ void curses_configure (void) {
} }
void curses_synchronize (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) { in (& signal, 4);
case '\033': curses_signal = SIGNAL_ESCAPE; break;
case '0': curses_signal = SIGNAL_0; break; if ((char) signal == '\033') {
case 'q': curses_signal = SIGNAL_Q; break; curses_signal |= SIGNAL_ESCAPE;
case 'Q': curses_signal = SIGNAL_Q | SIGNAL_SHIFT; break; } else if (character_is_digit ((char) signal) != 0) {
case 'w': curses_signal = SIGNAL_W; break; curses_signal |= SIGNAL_0 + (int) ((char) signal - '0');
case 's': curses_signal = SIGNAL_S; break; } else if (character_is_lowercase ((char) signal) != 0) {
case 'a': curses_signal = SIGNAL_A; break; curses_signal |= SIGNAL_A + (int) ((char) signal - 'a');
case 'd': curses_signal = SIGNAL_D; break; } else if (character_is_uppercase ((char) signal) != 0) {
default: curses_signal = SIGNAL_NONE; break; curses_signal |= SIGNAL_A + (int) ((char) signal - 'A');
curses_signal |= SIGNAL_SHIFT;
} else {
curses_signal = SIGNAL_NONE;
} }
if (curses_signal == SIGNAL_ESCAPE) { for (signal = 0; signal != curses_action_count; ++signal) {
curses_active = 0; if (curses_signal == curses_activator [signal]) {
return; curses_action [signal] ();
} }
if ((curses_signal > SIGNAL_ANY) && (curses_signal < SIGNAL_COUNT)) {
curses_action [curses_signal] ();
} }
} }
void curses_bind (int signal, void (* action) (void)) { 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) { 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) { 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) { void curses_render_character (char character, int colour, int effect, int x, int y) {