|
|
@@ -160,7 +160,7 @@ int curses_character = 0; |
|
|
|
int curses_signal = SIGNAL_NONE; |
|
|
|
int curses_screen_width = 0; |
|
|
|
int curses_screen_height = 0; |
|
|
|
int curses_active = 0; |
|
|
|
int curses_active = FALSE; |
|
|
|
|
|
|
|
/* |
|
|
|
I need to quickly explain how I'm structuring this subprogram. You can think of functions and variables starting with 'curses_*' as tiny standalone library if it's easier. They |
|
|
@@ -190,8 +190,12 @@ C can efficiently write good programs using those libraries, please don't misund |
|
|
|
our subprogram called 'curses'. |
|
|
|
*/ |
|
|
|
|
|
|
|
static void curses_idle (void) { return; } // If you have a lot of short functions that are intended to be in array of function pointers, you can align them like this. |
|
|
|
static void curses_exit (void) { curses_active = 0; } // And this is our main function for quitting main loop in curses. |
|
|
|
|
|
|
|
static void curses_initialize (void) { // This function will be called when 'curses_configure' is called, automatically. |
|
|
|
struct winsize screen_dimension; |
|
|
|
struct winsize screen_dimension; // We need this ugly structure for our 'ioctl' function to get the dimensions. |
|
|
|
int screen_memory; // And you can use local variables to shorten some lines of code if you want. |
|
|
|
|
|
|
|
fatal_failure (ioctl (STDOUT_FILENO, TIOCGWINSZ, & screen_dimension) == -1, // If function 'ioctl' failed, we immediately aborting the entire program. |
|
|
|
"ioctl: Failed to get terminal dimensions."); // I split those error messages, you can find your own formatting style. |
|
|
@@ -204,7 +208,7 @@ static void curses_initialize (void) { |
|
|
|
|
|
|
|
curses_new_terminal = curses_old_terminal; // Here we set our raw terminal to be the same as the non-raw one. |
|
|
|
|
|
|
|
curses_new_terminal.c_cc [VMIN] = (unsigned char) 0; // Now it's time to modify it to be raw, this essentially means no-echo. |
|
|
|
curses_new_terminal.c_cc [VMIN] = (unsigned char) 0; // Now it's time to modify it to be raw. |
|
|
|
curses_new_terminal.c_cc [VTIME] = (unsigned char) 1; |
|
|
|
|
|
|
|
curses_new_terminal.c_iflag &= (unsigned int) ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); |
|
|
@@ -214,6 +218,16 @@ static void curses_initialize (void) { |
|
|
|
|
|
|
|
fatal_failure (tcsetattr (STDIN_FILENO, TCSAFLUSH, & curses_new_terminal) == -1, // Finally, we're passing intormations to our terminal, and it becomes raw. |
|
|
|
"tcsetattr: Failed to set reverse terminal attributes."); |
|
|
|
|
|
|
|
screen_memory = CURSES_FORMAT * curses_screen_width * curses_screen_height; // This is square area of our terminal, and multiplied by 12, size of FORMAT. |
|
|
|
|
|
|
|
curses_screen = allocate (CURSES_REVERT + screen_memory + CURSES_CURSOR + 1); // We're requesting new memory for framebuffer. |
|
|
|
|
|
|
|
curses_bind (SIGNAL_ESCAPE, curses_exit); // Binding universal exit key (signal). |
|
|
|
|
|
|
|
string_copy (& curses_screen [0], "\033[H"); // ASCII black magic to always clear screen. |
|
|
|
|
|
|
|
terminal_clear (); |
|
|
|
} |
|
|
|
|
|
|
|
static void curses_deinitialize (void) { // This function will be only called once, automatically, at the program exit. |
|
|
@@ -221,6 +235,13 @@ static void curses_deinitialize (void) { |
|
|
|
curses_activator = deallocate (curses_activator); |
|
|
|
curses_action = deallocate (curses_action); |
|
|
|
|
|
|
|
curses_action_count = 0; |
|
|
|
curses_character = 0; |
|
|
|
curses_signal = SIGNAL_NONE; |
|
|
|
curses_screen_width = 0; |
|
|
|
curses_screen_height = 0; |
|
|
|
curses_active = FALSE; |
|
|
|
|
|
|
|
terminal_clear (); // This only make things look prettier, we don't have mess when we exit the program. |
|
|
|
|
|
|
|
fatal_failure (tcsetattr (STDIN_FILENO, TCSAFLUSH, & curses_old_terminal) == -1, // Again, if this fails, we're doomed, we're aborting. |
|
|
@@ -308,27 +329,16 @@ static char * curses_format_character (char character, int colour, int effect) { |
|
|
|
return (curses_format); // And we return the value (pointer to character) of formatted internal variables. |
|
|
|
} |
|
|
|
|
|
|
|
static void curses_idle (void) { return; } // If you have a lot of short functions that are intended to be in array of function pointers, you can align them like this. |
|
|
|
static void curses_exit (void) { curses_active = 0; } // And this is our main function for quitting main loop in curses. |
|
|
|
|
|
|
|
/* |
|
|
|
External function definitions, those found in "chapter_2.h" header file. |
|
|
|
*/ |
|
|
|
|
|
|
|
void curses_configure (void) { |
|
|
|
curses_active = TRUE; |
|
|
|
|
|
|
|
atexit (curses_deinitialize); // Deinitialization is automatically on exit. |
|
|
|
|
|
|
|
curses_initialize (); // Initializing curses, yaay. |
|
|
|
curses_active = TRUE; // This is important part, and something I like to do in my standalone libraries. |
|
|
|
|
|
|
|
curses_screen = allocate (CURSES_REVERT + CURSES_FORMAT * curses_screen_width * curses_screen_height + CURSES_CURSOR + 1); // We're requesting new memory for framebuffer. |
|
|
|
atexit (curses_deinitialize); // Deinitialization is automatically executed on exit point of the program, since we called 'atexit' function. |
|
|
|
|
|
|
|
curses_bind (SIGNAL_ESCAPE, curses_exit); // Binding universal exit key (signal). |
|
|
|
|
|
|
|
terminal_clear (); |
|
|
|
|
|
|
|
string_copy (& curses_screen [0], "\033[H"); // ASCII black magic to always clear screen. |
|
|
|
curses_initialize (); // Initializing curses, finally, yaay. |
|
|
|
} |
|
|
|
|
|
|
|
void curses_synchronize (void) { |
|
|
@@ -360,6 +370,10 @@ void curses_synchronize (void) { |
|
|
|
curses_action [signal] (); // We execute corresponding action (function). |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (curses_active == FALSE) { // Lastly, if we exited curses, we want to deinitialize. |
|
|
|
curses_deinitialize (); // It's no problem if we do it more than once... |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|