C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
menu.c
Go to the documentation of this file.
1/** @file menu.c
2 @brief Command line start-up for C-Menu components
3 @author Bill Waller
4 Copyright (c) 2025
5 MIT License
6 billxwaller@gmail.com
7 @date 2026-02-09
8 */
9#define _GNU_SOURCE
10#include "common.h"
11#include <string.h>
12#ifdef NCURSES_UI
13#include "../ui/ui_ncurses_internal.h"
14#include "ui_backend.h"
15#include <ncursesw/ncurses.h>
16#include <ncursesw/panel.h>
17#endif
18
19/** @brief This function is responsible for cleaning up the terminal state and
20 exiting the program.
21 @details This function is typically registered to be called when the program
22 exits, ensuring that the terminal is properly restored to its original state,
23 even if the program encounters an error or is terminated unexpectedly. The
24 end_pgm function performs the following actions:
25 1. It calls win_del() to delete any windows that may have been created
26 during the program's execution.
27 2. It calls destroy_curses() to clean up the ncurses library and restore the
28 terminal to its normal state.
29 3. It calls restore_shell_tioctl() to restore the terminal's input/output
30 settings to their original state.
31 4. Finally, it calls exit(EXIT_FAILURE) to terminate the program with a
32 failure status, indicating that the program has encountered an error or is
33 exiting due to an unexpected condition. By defining this function and
34 registering it to be called on program exit, you can help ensure that the
35 terminal is properly cleaned up and restored, preventing any issues with the
36 terminal state after the program has exited. */
37static void end_pgm(void) {
38 close(cmenu_log_fd);
39 exit(EXIT_SUCCESS);
40}
41
42int main(int argc, char **argv) {
43 int rc;
44 char pgm_name[MAXLEN];
46 Init *init = new_init(argc, argv);
47 mapp_initialization(init, argc, argv);
48 SIO *sio = init->sio;
49 UiConfig *ui_config = calloc(1, sizeof(UiConfig));
50 ui_config->enable_mouse = true;
51 ui_config->enable_alt_screen = false;
52 ui_config->cursor_visible = true;
53 ui_config->border_style = sio->border;
54 ui_init(ui_config);
56 rc = atexit(end_pgm);
57 if (rc != 0) {
58 fprintf(stderr, "\nCannot set exit function\n");
59 exit(EXIT_FAILURE);
60 }
63 base_name(pgm_name, argv[0]);
64 if (!strcmp(pgm_name, "menu")) {
65 new_menu(init, init->argc, init->argv, LINES / 14, COLS / 14);
66 menu_engine(init);
67 } else if (!strcmp(pgm_name, "form")) {
68 init_form(init, init->argc, init->argv, LINES / 14, COLS / 14);
69 } else if (!strcmp(pgm_name, "pick")) {
70 init_pick(init, init->argc, init->argv, 0, 0);
71 } else if (!strcmp(pgm_name, "view")) {
72 View *view = new_view(init);
73 if (init->lines > 0 || init->cols > 0) {
74 if (init->h_shift == 0)
75 init->h_shift = view->cols / 3;
76 if (!init_view_boxwin(init)) {
77 view_file(init);
78 }
79 } else if (!init_view_full_screen(init)) {
80 view_file(init);
81 }
82 } else if (!strcmp(pgm_name, "ckeys"))
87 exit(EXIT_SUCCESS);
88}
int cmenu_log_fd
Definition futil.c:47
UiRuntime * ui_init(const UiConfig *config)
Definition ui_ncurses.c:261
uint COLS
Definition ui_backend.h:266
uint LINES
int popup_ckeys()
Definition curskeys.c:31
#define MAXLEN
Definition curskeys.c:15
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
Definition amort.c:30
void initialize_cells(SIO *sio)
Initialize local color variables and color pairs based on SIO settings.
Definition dwin.c:177
void destroy_curses()
Gracefully shut down NCurses and restore terminal settings.
Definition dwin.c:384
int init_form(Init *, int, char **, uint, uint)
Initialize form data structure and parse description file.
Definition form_engine.c:61
bool base_name(char *, char *)
Returns the base name of a file specification.
Definition futil.c:1106
void mapp_initialization(Init *, int, char **)
Main initialization function for MAPP - Menu Application.
Definition init.c:474
int init_view_full_screen(Init *init)
Initialize C-Menu View in full screen mode.
Definition init_view.c:47
int init_view_boxwin(Init *init)
Initialize the C-Menu View in box window mode.
Definition init_view.c:147
View * destroy_view(Init *init)
Destroy View structure.
Definition mem.c:345
Menu * new_menu(Init *init, int argc, char **argv, uint begy, uint begx)
Create and initialize Menu structure.
Definition mem.c:134
Init * new_init(int argc, char **argv)
Create and initialize Init structure.
Definition mem.c:60
View * new_view(Init *init)
Create and initialize View structure.
Definition mem.c:299
Init * destroy_init(Init *init)
Destroy Init structure.
Definition mem.c:95
unsigned int menu_engine(Init *)
The main loop of the menu system.
Definition menu_engine.c:36
int init_pick(Init *, int, char **, uint, uint)
Initializes pick structure and opens pick input file or pipe.
Definition pick_engine.c:66
bool capture_shell_tioctl()
capture_shell_tioctl() - capture shell terminal settings
Definition scriou.c:43
bool capture_curses_tioctl()
capture_curses_tioctl() - capture curses terminal settings
Definition scriou.c:70
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition sig.c:62
int view_file(Init *)
Start view.
bool cursor_visible
Definition ui_backend.h:145
bool enable_mouse
Definition ui_backend.h:143
char border_style
Definition ui_backend.h:146
bool enable_alt_screen
Definition ui_backend.h:144
char border
Definition cm.h:752
SIO * sio
Definition common.h:114
int argc
Definition common.h:130
int cols
Definition common.h:116
int h_shift
Definition common.h:182
char ** argv
Definition common.h:131
int lines
Definition common.h:115
uint cols
Definition view.h:73