18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * dialog.h -- common declarations for all dialog modules 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * AUTHOR: Savio Lam (lam836@cs.cuhk.hk) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <sys/types.h> 98c2ecf20Sopenharmony_ci#include <fcntl.h> 108c2ecf20Sopenharmony_ci#include <unistd.h> 118c2ecf20Sopenharmony_ci#include <ctype.h> 128c2ecf20Sopenharmony_ci#include <stdlib.h> 138c2ecf20Sopenharmony_ci#include <string.h> 148c2ecf20Sopenharmony_ci#include <stdbool.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#ifdef __sun__ 178c2ecf20Sopenharmony_ci#define CURS_MACROS 188c2ecf20Sopenharmony_ci#endif 198c2ecf20Sopenharmony_ci#include <ncurses.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * Colors in ncurses 1.9.9e do not work properly since foreground and 238c2ecf20Sopenharmony_ci * background colors are OR'd rather than separately masked. This version 248c2ecf20Sopenharmony_ci * of dialog was hacked to work with ncurses 1.9.9e, making it incompatible 258c2ecf20Sopenharmony_ci * with standard curses. The simplest fix (to make this work with standard 268c2ecf20Sopenharmony_ci * curses) uses the wbkgdset() function, not used in the original hack. 278c2ecf20Sopenharmony_ci * Turn it off if we're building with 1.9.9e, since it just confuses things. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci#if defined(NCURSES_VERSION) && defined(_NEED_WRAP) && !defined(GCC_PRINTFLIKE) 308c2ecf20Sopenharmony_ci#define OLD_NCURSES 1 318c2ecf20Sopenharmony_ci#undef wbkgdset 328c2ecf20Sopenharmony_ci#define wbkgdset(w,p) /*nothing */ 338c2ecf20Sopenharmony_ci#else 348c2ecf20Sopenharmony_ci#define OLD_NCURSES 0 358c2ecf20Sopenharmony_ci#endif 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define TR(params) _tracef params 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define KEY_ESC 27 408c2ecf20Sopenharmony_ci#define TAB 9 418c2ecf20Sopenharmony_ci#define MAX_LEN 2048 428c2ecf20Sopenharmony_ci#define BUF_SIZE (10*1024) 438c2ecf20Sopenharmony_ci#define MIN(x,y) (x < y ? x : y) 448c2ecf20Sopenharmony_ci#define MAX(x,y) (x > y ? x : y) 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#ifndef ACS_ULCORNER 478c2ecf20Sopenharmony_ci#define ACS_ULCORNER '+' 488c2ecf20Sopenharmony_ci#endif 498c2ecf20Sopenharmony_ci#ifndef ACS_LLCORNER 508c2ecf20Sopenharmony_ci#define ACS_LLCORNER '+' 518c2ecf20Sopenharmony_ci#endif 528c2ecf20Sopenharmony_ci#ifndef ACS_URCORNER 538c2ecf20Sopenharmony_ci#define ACS_URCORNER '+' 548c2ecf20Sopenharmony_ci#endif 558c2ecf20Sopenharmony_ci#ifndef ACS_LRCORNER 568c2ecf20Sopenharmony_ci#define ACS_LRCORNER '+' 578c2ecf20Sopenharmony_ci#endif 588c2ecf20Sopenharmony_ci#ifndef ACS_HLINE 598c2ecf20Sopenharmony_ci#define ACS_HLINE '-' 608c2ecf20Sopenharmony_ci#endif 618c2ecf20Sopenharmony_ci#ifndef ACS_VLINE 628c2ecf20Sopenharmony_ci#define ACS_VLINE '|' 638c2ecf20Sopenharmony_ci#endif 648c2ecf20Sopenharmony_ci#ifndef ACS_LTEE 658c2ecf20Sopenharmony_ci#define ACS_LTEE '+' 668c2ecf20Sopenharmony_ci#endif 678c2ecf20Sopenharmony_ci#ifndef ACS_RTEE 688c2ecf20Sopenharmony_ci#define ACS_RTEE '+' 698c2ecf20Sopenharmony_ci#endif 708c2ecf20Sopenharmony_ci#ifndef ACS_UARROW 718c2ecf20Sopenharmony_ci#define ACS_UARROW '^' 728c2ecf20Sopenharmony_ci#endif 738c2ecf20Sopenharmony_ci#ifndef ACS_DARROW 748c2ecf20Sopenharmony_ci#define ACS_DARROW 'v' 758c2ecf20Sopenharmony_ci#endif 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* error return codes */ 788c2ecf20Sopenharmony_ci#define ERRDISPLAYTOOSMALL (KEY_MAX + 1) 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/* 818c2ecf20Sopenharmony_ci * Color definitions 828c2ecf20Sopenharmony_ci */ 838c2ecf20Sopenharmony_cistruct dialog_color { 848c2ecf20Sopenharmony_ci chtype atr; /* Color attribute */ 858c2ecf20Sopenharmony_ci int fg; /* foreground */ 868c2ecf20Sopenharmony_ci int bg; /* background */ 878c2ecf20Sopenharmony_ci int hl; /* highlight this item */ 888c2ecf20Sopenharmony_ci}; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistruct subtitle_list { 918c2ecf20Sopenharmony_ci struct subtitle_list *next; 928c2ecf20Sopenharmony_ci const char *text; 938c2ecf20Sopenharmony_ci}; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistruct dialog_info { 968c2ecf20Sopenharmony_ci const char *backtitle; 978c2ecf20Sopenharmony_ci struct subtitle_list *subtitles; 988c2ecf20Sopenharmony_ci struct dialog_color screen; 998c2ecf20Sopenharmony_ci struct dialog_color shadow; 1008c2ecf20Sopenharmony_ci struct dialog_color dialog; 1018c2ecf20Sopenharmony_ci struct dialog_color title; 1028c2ecf20Sopenharmony_ci struct dialog_color border; 1038c2ecf20Sopenharmony_ci struct dialog_color button_active; 1048c2ecf20Sopenharmony_ci struct dialog_color button_inactive; 1058c2ecf20Sopenharmony_ci struct dialog_color button_key_active; 1068c2ecf20Sopenharmony_ci struct dialog_color button_key_inactive; 1078c2ecf20Sopenharmony_ci struct dialog_color button_label_active; 1088c2ecf20Sopenharmony_ci struct dialog_color button_label_inactive; 1098c2ecf20Sopenharmony_ci struct dialog_color inputbox; 1108c2ecf20Sopenharmony_ci struct dialog_color inputbox_border; 1118c2ecf20Sopenharmony_ci struct dialog_color searchbox; 1128c2ecf20Sopenharmony_ci struct dialog_color searchbox_title; 1138c2ecf20Sopenharmony_ci struct dialog_color searchbox_border; 1148c2ecf20Sopenharmony_ci struct dialog_color position_indicator; 1158c2ecf20Sopenharmony_ci struct dialog_color menubox; 1168c2ecf20Sopenharmony_ci struct dialog_color menubox_border; 1178c2ecf20Sopenharmony_ci struct dialog_color item; 1188c2ecf20Sopenharmony_ci struct dialog_color item_selected; 1198c2ecf20Sopenharmony_ci struct dialog_color tag; 1208c2ecf20Sopenharmony_ci struct dialog_color tag_selected; 1218c2ecf20Sopenharmony_ci struct dialog_color tag_key; 1228c2ecf20Sopenharmony_ci struct dialog_color tag_key_selected; 1238c2ecf20Sopenharmony_ci struct dialog_color check; 1248c2ecf20Sopenharmony_ci struct dialog_color check_selected; 1258c2ecf20Sopenharmony_ci struct dialog_color uarrow; 1268c2ecf20Sopenharmony_ci struct dialog_color darrow; 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* 1308c2ecf20Sopenharmony_ci * Global variables 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_ciextern struct dialog_info dlg; 1338c2ecf20Sopenharmony_ciextern char dialog_input_result[]; 1348c2ecf20Sopenharmony_ciextern int saved_x, saved_y; /* Needed in signal handler in mconf.c */ 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci/* 1378c2ecf20Sopenharmony_ci * Function prototypes 1388c2ecf20Sopenharmony_ci */ 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/* item list as used by checklist and menubox */ 1418c2ecf20Sopenharmony_civoid item_reset(void); 1428c2ecf20Sopenharmony_civoid item_make(const char *fmt, ...); 1438c2ecf20Sopenharmony_civoid item_add_str(const char *fmt, ...); 1448c2ecf20Sopenharmony_civoid item_set_tag(char tag); 1458c2ecf20Sopenharmony_civoid item_set_data(void *p); 1468c2ecf20Sopenharmony_civoid item_set_selected(int val); 1478c2ecf20Sopenharmony_ciint item_activate_selected(void); 1488c2ecf20Sopenharmony_civoid *item_data(void); 1498c2ecf20Sopenharmony_cichar item_tag(void); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/* item list manipulation for lxdialog use */ 1528c2ecf20Sopenharmony_ci#define MAXITEMSTR 200 1538c2ecf20Sopenharmony_cistruct dialog_item { 1548c2ecf20Sopenharmony_ci char str[MAXITEMSTR]; /* prompt displayed */ 1558c2ecf20Sopenharmony_ci char tag; 1568c2ecf20Sopenharmony_ci void *data; /* pointer to menu item - used by menubox+checklist */ 1578c2ecf20Sopenharmony_ci int selected; /* Set to 1 by dialog_*() function if selected. */ 1588c2ecf20Sopenharmony_ci}; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci/* list of lialog_items */ 1618c2ecf20Sopenharmony_cistruct dialog_list { 1628c2ecf20Sopenharmony_ci struct dialog_item node; 1638c2ecf20Sopenharmony_ci struct dialog_list *next; 1648c2ecf20Sopenharmony_ci}; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ciextern struct dialog_list *item_cur; 1678c2ecf20Sopenharmony_ciextern struct dialog_list item_nil; 1688c2ecf20Sopenharmony_ciextern struct dialog_list *item_head; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciint item_count(void); 1718c2ecf20Sopenharmony_civoid item_set(int n); 1728c2ecf20Sopenharmony_ciint item_n(void); 1738c2ecf20Sopenharmony_ciconst char *item_str(void); 1748c2ecf20Sopenharmony_ciint item_is_selected(void); 1758c2ecf20Sopenharmony_ciint item_is_tag(char tag); 1768c2ecf20Sopenharmony_ci#define item_foreach() \ 1778c2ecf20Sopenharmony_ci for (item_cur = item_head ? item_head: item_cur; \ 1788c2ecf20Sopenharmony_ci item_cur && (item_cur != &item_nil); item_cur = item_cur->next) 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci/* generic key handlers */ 1818c2ecf20Sopenharmony_ciint on_key_esc(WINDOW *win); 1828c2ecf20Sopenharmony_ciint on_key_resize(void); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci/* minimum (re)size values */ 1858c2ecf20Sopenharmony_ci#define CHECKLIST_HEIGTH_MIN 6 /* For dialog_checklist() */ 1868c2ecf20Sopenharmony_ci#define CHECKLIST_WIDTH_MIN 6 1878c2ecf20Sopenharmony_ci#define INPUTBOX_HEIGTH_MIN 2 /* For dialog_inputbox() */ 1888c2ecf20Sopenharmony_ci#define INPUTBOX_WIDTH_MIN 2 1898c2ecf20Sopenharmony_ci#define MENUBOX_HEIGTH_MIN 15 /* For dialog_menu() */ 1908c2ecf20Sopenharmony_ci#define MENUBOX_WIDTH_MIN 65 1918c2ecf20Sopenharmony_ci#define TEXTBOX_HEIGTH_MIN 8 /* For dialog_textbox() */ 1928c2ecf20Sopenharmony_ci#define TEXTBOX_WIDTH_MIN 8 1938c2ecf20Sopenharmony_ci#define YESNO_HEIGTH_MIN 4 /* For dialog_yesno() */ 1948c2ecf20Sopenharmony_ci#define YESNO_WIDTH_MIN 4 1958c2ecf20Sopenharmony_ci#define WINDOW_HEIGTH_MIN 19 /* For init_dialog() */ 1968c2ecf20Sopenharmony_ci#define WINDOW_WIDTH_MIN 80 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ciint init_dialog(const char *backtitle); 1998c2ecf20Sopenharmony_civoid set_dialog_backtitle(const char *backtitle); 2008c2ecf20Sopenharmony_civoid set_dialog_subtitles(struct subtitle_list *subtitles); 2018c2ecf20Sopenharmony_civoid end_dialog(int x, int y); 2028c2ecf20Sopenharmony_civoid attr_clear(WINDOW * win, int height, int width, chtype attr); 2038c2ecf20Sopenharmony_civoid dialog_clear(void); 2048c2ecf20Sopenharmony_civoid print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x); 2058c2ecf20Sopenharmony_civoid print_button(WINDOW * win, const char *label, int y, int x, int selected); 2068c2ecf20Sopenharmony_civoid print_title(WINDOW *dialog, const char *title, int width); 2078c2ecf20Sopenharmony_civoid draw_box(WINDOW * win, int y, int x, int height, int width, chtype box, 2088c2ecf20Sopenharmony_ci chtype border); 2098c2ecf20Sopenharmony_civoid draw_shadow(WINDOW * win, int y, int x, int height, int width); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ciint first_alpha(const char *string, const char *exempt); 2128c2ecf20Sopenharmony_ciint dialog_yesno(const char *title, const char *prompt, int height, int width); 2138c2ecf20Sopenharmony_ciint dialog_msgbox(const char *title, const char *prompt, int height, 2148c2ecf20Sopenharmony_ci int width, int pause); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_citypedef void (*update_text_fn)(char *buf, size_t start, size_t end, void 2188c2ecf20Sopenharmony_ci *_data); 2198c2ecf20Sopenharmony_ciint dialog_textbox(const char *title, char *tbuf, int initial_height, 2208c2ecf20Sopenharmony_ci int initial_width, int *keys, int *_vscroll, int *_hscroll, 2218c2ecf20Sopenharmony_ci update_text_fn update_text, void *data); 2228c2ecf20Sopenharmony_ciint dialog_menu(const char *title, const char *prompt, 2238c2ecf20Sopenharmony_ci const void *selected, int *s_scroll); 2248c2ecf20Sopenharmony_ciint dialog_checklist(const char *title, const char *prompt, int height, 2258c2ecf20Sopenharmony_ci int width, int list_height); 2268c2ecf20Sopenharmony_ciint dialog_inputbox(const char *title, const char *prompt, int height, 2278c2ecf20Sopenharmony_ci int width, const char *init); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci/* 2308c2ecf20Sopenharmony_ci * This is the base for fictitious keys, which activate 2318c2ecf20Sopenharmony_ci * the buttons. 2328c2ecf20Sopenharmony_ci * 2338c2ecf20Sopenharmony_ci * Mouse-generated keys are the following: 2348c2ecf20Sopenharmony_ci * -- the first 32 are used as numbers, in addition to '0'-'9' 2358c2ecf20Sopenharmony_ci * -- the lowercase are used to signal mouse-enter events (M_EVENT + 'o') 2368c2ecf20Sopenharmony_ci * -- uppercase chars are used to invoke the button (M_EVENT + 'O') 2378c2ecf20Sopenharmony_ci */ 2388c2ecf20Sopenharmony_ci#define M_EVENT (KEY_MAX+1) 239