2017-01-18 15:51:10 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Brian Barto
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the MIT License. See LICENSE for more details.
|
|
|
|
*/
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* The nmstermio_ncurses modules implements the terminal interface, with
|
|
|
|
* ncurses support, used by the nmseffect module. All terminal IO
|
|
|
|
* functionality is defined and implemented here.
|
|
|
|
*/
|
|
|
|
|
2017-01-18 16:31:59 -08:00
|
|
|
#include <string.h>
|
2017-01-18 15:51:10 -08:00
|
|
|
#include <ncurses.h>
|
|
|
|
|
2017-01-18 16:19:33 -08:00
|
|
|
// Static settings
|
|
|
|
static int clearScr = 1; // clearScr flag
|
|
|
|
static int foregroundColor = COLOR_BLUE; // Foreground color setting
|
2017-01-18 15:51:10 -08:00
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Initialize and configure the terminal for output. This usually means
|
|
|
|
* just turning off terminal echo, line buffering, and clearing the screen.
|
|
|
|
* Note that ncurses always clears the screen, so there is no option to do
|
|
|
|
* otherwise.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_init_terminal(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
curs_set(0);
|
|
|
|
move(0, 0);
|
|
|
|
if (has_colors()) {
|
|
|
|
start_color();
|
|
|
|
init_pair(1, foregroundColor, COLOR_BLACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Restore the state of the terminal to the state prior to executing
|
|
|
|
* nmstermio_init_terminal(). This usually means turning on line buffering,
|
|
|
|
* echo, and terminal contents.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_restore_terminal(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
endwin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-01-20 11:35:00 -08:00
|
|
|
* Get and return the number of rows in the current terminal window.
|
2017-01-18 15:51:10 -08:00
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
int nmstermio_get_rows(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
return getmaxy(stdscr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-01-20 11:35:00 -08:00
|
|
|
* Get and return the number of cols in the current terminal window.
|
2017-01-18 15:51:10 -08:00
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
int nmstermio_get_cols(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
return getmaxx(stdscr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-01-20 11:35:00 -08:00
|
|
|
* Move terminal cursor to the given x/y coordinates.
|
2017-01-18 15:51:10 -08:00
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_move_cursor(int y, int x) {
|
2017-01-18 15:51:10 -08:00
|
|
|
move(y, x);
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Print the given character string to the terminal output.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_print_string(char *s) {
|
2017-01-18 15:51:10 -08:00
|
|
|
addstr(s);
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Refresh the screen. Display all pending output that has not been
|
|
|
|
* displayed yet.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_refresh(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Clear all input pending in STDIN. This is used prior to getting user
|
|
|
|
* input to clear the input queue in case of any prior errant keystrokes.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_clear_input(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
flushinp();
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Get a single character of input from the user.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_get_char(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
getch();
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Print the character string that represents the decrypted data. We
|
|
|
|
* apply the bold and color attributes (if the terminal supports color)
|
|
|
|
* to this string.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_print_reveal_string(char *s, int colorOn) {
|
2017-01-18 15:51:10 -08:00
|
|
|
(void) colorOn;
|
|
|
|
(void) foregroundColor;
|
|
|
|
|
|
|
|
// Set bold and foreground color for character reveal
|
|
|
|
attron(A_BOLD);
|
|
|
|
if (has_colors())
|
|
|
|
attron(COLOR_PAIR(1));
|
|
|
|
|
|
|
|
// print source character
|
|
|
|
addstr(s);
|
|
|
|
|
|
|
|
// Unset foreground color
|
|
|
|
if (has_colors())
|
|
|
|
attroff(COLOR_PAIR(1));
|
|
|
|
attroff(A_BOLD);
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Display the cursor that have been turned off by nmstermio_init_terminal().
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_show_cursor(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
curs_set(1);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Make the terminal beep. Used when the returnOpts setting is set in the
|
|
|
|
* nmseffect module.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_beep(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
beep();
|
|
|
|
}
|
|
|
|
|
2017-01-20 11:35:00 -08:00
|
|
|
/*
|
|
|
|
* Return the status of the clearScr flag. This is used by the nmseffect
|
|
|
|
* module to make certain decisions based on its value.
|
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
int nmstermio_get_clearscr(void) {
|
2017-01-18 15:51:10 -08:00
|
|
|
return clearScr;
|
|
|
|
}
|
2017-01-18 16:19:33 -08:00
|
|
|
|
|
|
|
/*
|
2017-01-20 11:35:00 -08:00
|
|
|
* Sets the clearScr flag according to the true/false value of the 's' argument.
|
2017-01-18 16:19:33 -08:00
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_set_clearscr(int s) {
|
2017-01-18 16:19:33 -08:00
|
|
|
if (s)
|
|
|
|
clearScr = 1;
|
|
|
|
else
|
|
|
|
clearScr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-01-20 11:35:00 -08:00
|
|
|
* Set the desired foreground color of the unencrypted characters as they
|
|
|
|
* are revealed by nmstermio_print_reveal_string(). Valid arguments are
|
|
|
|
* "white", "yellow", "magenta", "blue", "green", "red", and "cyan".
|
2017-01-18 16:19:33 -08:00
|
|
|
*/
|
2017-01-19 08:42:29 -08:00
|
|
|
void nmstermio_set_foregroundcolor(char *c) {
|
2017-01-18 16:19:33 -08:00
|
|
|
|
|
|
|
if(strcmp("white", c) == 0)
|
|
|
|
foregroundColor = COLOR_WHITE;
|
|
|
|
else if(strcmp("yellow", c) == 0)
|
|
|
|
foregroundColor = COLOR_YELLOW;
|
|
|
|
else if(strcmp("black", c) == 0)
|
|
|
|
foregroundColor = COLOR_BLACK;
|
|
|
|
else if(strcmp("magenta", c) == 0)
|
|
|
|
foregroundColor = COLOR_MAGENTA;
|
|
|
|
else if(strcmp("blue", c) == 0)
|
|
|
|
foregroundColor = COLOR_BLUE;
|
|
|
|
else if(strcmp("green", c) == 0)
|
|
|
|
foregroundColor = COLOR_GREEN;
|
|
|
|
else if(strcmp("red", c) == 0)
|
|
|
|
foregroundColor = COLOR_RED;
|
|
|
|
else if(strcmp("cyan", c) == 0)
|
|
|
|
foregroundColor = COLOR_CYAN;
|
|
|
|
else
|
|
|
|
foregroundColor = COLOR_BLUE;
|
|
|
|
}
|
2017-01-20 11:35:00 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get and returns the current row position of the cursor.
|
|
|
|
*/
|
|
|
|
int nmstermio_get_cursor_row(void) {
|
|
|
|
return getcury(stdscr);
|
|
|
|
}
|