From fc247603ecf22eb10192a0fcde9c140e3b35872c Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Fri, 26 Jul 2024 13:17:31 -0500 Subject: Slightly refactor ncurses code & make errors not explode the terminal --- src/screen.c | 140 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 103 insertions(+), 37 deletions(-) (limited to 'src/screen.c') diff --git a/src/screen.c b/src/screen.c index 362a1a7..77b1a3b 100644 --- a/src/screen.c +++ b/src/screen.c @@ -32,6 +32,7 @@ SLOTS HERE #include #include +#include static const char *phrases[] = { // By @syxhe on telegram @@ -95,7 +96,8 @@ static int init_rgb_color(int colornum, int red, int green, int blue) { return init_color(colornum, nred, ngreen, nblue); } -int main() { +// Initialize sodium, curses, and set the proper locale. Exits on error, returns 0 otherwise +static int doinit(void) { if(sodium_init() < 0) error(1, errno, "[VX-GAMBLEGROUND] Could not initialize sodium"); @@ -105,6 +107,11 @@ int main() { if(initscr() == NULL) // Initialize curses error(1, errno, "[VX-GAMBLEGROUND] Could not init standard screen"); + return 0; +} + +// Initialize colors & define a few custom colors & color pairs. Exits on error, returns 0 otherwise +static int docolors(void) { if(has_colors() && can_change_color()) { // Init colors if available & create color pairings start_color(); @@ -126,77 +133,110 @@ int main() { init_pair(CCP_BANNER, CC_WHITE, CURSES_BLUE); init_pair(CCP_TESTING, CC_RED, CC_WHITE); + } else { + endwin(); + error(1, ENOTSUP, "[VX-GAMBLEGROUND] Colors are not supported on your terminal"); } + return 0; +} + +int main() { + doinit(); + docolors(); + cbreak(); // Disables character buffering noecho(); // Disable echoing characters - keypad(stdscr, TRUE); // Enable keypad on the standard screen - curs_set(0); + curs_set(0); // Stop the cursor from blinking + + + // Variable definitions + + struct funcholder funcholder[STATIC_ARRSIZE(menu_choices)]; // Stores the function pointers used for the menu + ITEM *items[STATIC_ARRSIZE(menu_choices) + 1]; // An array of ITEM pointers large enough to store all the menu items + int holder1 = -1, holder2 = -1; // Holders for the max yx of the menu subwindow + struct funcholder *p = NULL; // Function pointer used for callbacks in the menu driver + int row = -1, col = -1; // The rows and columns of the main screen + WINDOW *phrase = NULL; // The window for displaying the banner + WINDOW *menuholder; // The window for displaying the menu + int randomnum; // A random number for getting the banner phrase + MENU *menu; // The actual menu object + int c; // Integer for storing keys from user input + - int row = -1, col = -1; getmaxyx(stdscr, row, col); - if(row < 0 || col < 0) + if(row < 0 || col < 0) { + endwin(); error(1, 0, "[VX-GAMBLEGROUND] Couldn't get max terminal size for some reason"); + } - int randomnum = randombytes_uniform(STATIC_ARRSIZE(phrases)); - if(randomnum < 0) + randomnum = randombytes_uniform(STATIC_ARRSIZE(phrases)); + if(randomnum < 0) { + endwin(); error(1, errno, "[VX-GAMBLEGROUND] Call to randombytes_uniform failed, can't get random phrase"); + } - // Making this a subwindow of stdscr might be a good idea (maybe) - WINDOW *phrase = NULL; + // Create the banner window phrase = newwin(1, col, 0, 0); - if(phrase == NULL) + if(phrase == NULL) { + endwin(); error(1, errno, "[VX-GAMBLEGROUND] Could not create banner window"); - - if(has_colors()) - wbkgd(phrase, COLOR_PAIR(1)); - + } + wbkgd(phrase, COLOR_PAIR(1)); mvwaddstr(phrase, 0, 1, "VX-GAMBLEGROUND: "); waddstr(phrase, phrases[randomnum]); wrefresh(phrase); - /* Dummy test for fun. Shows how windows can't be overlapping or things get weird (and that row offsets matter) - WINDOW *test = newwin(row - 1, col, 1, 0); - int rand1, rand2; - - for(int c = wgetch(test);; c = wgetch(test)) { - rand1 = randombytes_uniform(row - 1); - rand2 = randombytes_uniform(col); - - mvwaddch(test, rand1, rand2, c | (A_BOLD * (randombytes_random() % 2 > 0))); - } - //*/ - - //* Menu example (fuck menus got damn) - ITEM *items[STATIC_ARRSIZE(menu_choices) + 1]; - MENU *menu; - WINDOW *menuholder; - for(long unsigned int i = 0; i < STATIC_ARRSIZE(menu_choices); i++) + // Set up menu items + for(long unsigned int i = 0; i < STATIC_ARRSIZE(menu_choices); i++) { + funcholder[i].callback = USERFUNCS[i]; items[i] = new_item(menu_choices[i], NULL); + set_item_userptr(items[i], (void*)(&funcholder[i])); + } items[STATIC_ARRSIZE(menu_choices)] = NULL; menu = new_menu(items); - if(menu == NULL) + if(menu == NULL) { + endwin(); error(1, errno, "Could not create menu"); + } + // Set menu options & format set_menu_format(menu, 1, col); menu_opts_on(menu, O_ONEVALUE | O_IGNORECASE); - menu_opts_off(menu, O_SHOWDESC); + menu_opts_off(menu, O_SHOWDESC | O_NONCYCLIC); menuholder = newwin(1, col, row - 1, 0); keypad(menuholder, TRUE); - int holder1 = -1, holder2 = -1; + getmaxyx(menuholder, holder1, holder2); + if(holder1 < 0 || holder2 < 0) { + endwin(); + error(1, errno, "[VX-GAMBLEGROUND] Could not get bounds for menu subwindow"); + } set_menu_win(menu, menuholder); set_menu_sub(menu, derwin(menuholder, holder1, holder2, 0, 0)); set_menu_mark(menu, NULL); post_menu(menu); + wbkgd(menuholder, COLOR_PAIR(CCP_BANNER)); + set_menu_back(menu, COLOR_PAIR(CCP_BANNER)); wrefresh(menuholder); - int c; + + WINDOW *slots = newwin(row - 2, col, 1, 0); + if(slots == NULL) { + endwin(); + error(1, errno, "[VX-GAMBLEGROUND] Could not create slots window"); + } + + const char *sampletext = "Lmao longer sample"; + mvwaddstr(slots, (row - 2) / 2, (col / 2) - (strlen(sampletext) / 2), sampletext); + wrefresh(slots); + + // Get user input and deal with the menu while((c = wgetch(menuholder)) != KEY_F(4)) { switch(c) { case KEY_DOWN: @@ -215,12 +255,17 @@ int main() { menu_driver(menu, REQ_RIGHT_ITEM); break; - case KEY_ENTER: + case KEY_ENTER: case FUCKED_UP_ENTER: // Enter + //wclear(menuholder); + //mvwaddstr(menuholder, 0, 0, "This shit brokey lmao"); + + p = (struct funcholder *)item_userptr(current_item(menu)); + p->callback(); + break; } } - // Clean up the menu unpost_menu(menu); free_menu(menu); @@ -230,4 +275,25 @@ int main() { endwin(); // Clean up curses return 0; +} + +// Spin the wheel +int spin(void) { + endwin(); + error(1, 0, "spin"); + return 0; +} + +// Increase number of spins +int buy(void) { + endwin(); + error(1, 0, "buy"); + return 0; +} + +// Quit out +int quit(void) { + endwin(); + error(1, 0, "quit"); + return 1; } \ No newline at end of file -- cgit v1.2.3