1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com>
4 *
5 * Derived from menuconfig.
6 */
7 #include <xalloc.h>
8 #include "nconf.h"
9 #include "lkc.h"
10
11 int attr_normal;
12 int attr_main_heading;
13 int attr_main_menu_box;
14 int attr_main_menu_fore;
15 int attr_main_menu_back;
16 int attr_main_menu_grey;
17 int attr_main_menu_heading;
18 int attr_scrollwin_text;
19 int attr_scrollwin_heading;
20 int attr_scrollwin_box;
21 int attr_dialog_text;
22 int attr_dialog_menu_fore;
23 int attr_dialog_menu_back;
24 int attr_dialog_box;
25 int attr_input_box;
26 int attr_input_heading;
27 int attr_input_text;
28 int attr_input_field;
29 int attr_function_text;
30 int attr_function_highlight;
31
32 #define COLOR_ATTR(_at, _fg, _bg, _hl) \
33 { .attr = &(_at), .has_color = true, .color_fg = _fg, .color_bg = _bg, .highlight = _hl }
34 #define NO_COLOR_ATTR(_at, _hl) \
35 { .attr = &(_at), .has_color = false, .highlight = _hl }
36 #define COLOR_DEFAULT -1
37
38 struct nconf_attr_param {
39 int *attr;
40 bool has_color;
41 int color_fg;
42 int color_bg;
43 int highlight;
44 };
45
46 static const struct nconf_attr_param color_theme_params[] = {
47 COLOR_ATTR(attr_normal, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
48 COLOR_ATTR(attr_main_heading, COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD | A_UNDERLINE),
49 COLOR_ATTR(attr_main_menu_box, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
50 COLOR_ATTR(attr_main_menu_fore, COLOR_DEFAULT, COLOR_DEFAULT, A_REVERSE),
51 COLOR_ATTR(attr_main_menu_back, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
52 COLOR_ATTR(attr_main_menu_grey, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
53 COLOR_ATTR(attr_main_menu_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
54 COLOR_ATTR(attr_scrollwin_text, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
55 COLOR_ATTR(attr_scrollwin_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
56 COLOR_ATTR(attr_scrollwin_box, COLOR_YELLOW, COLOR_DEFAULT, A_BOLD),
57 COLOR_ATTR(attr_dialog_text, COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD),
58 COLOR_ATTR(attr_dialog_menu_fore, COLOR_RED, COLOR_DEFAULT, A_STANDOUT),
59 COLOR_ATTR(attr_dialog_menu_back, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
60 COLOR_ATTR(attr_dialog_box, COLOR_YELLOW, COLOR_DEFAULT, A_BOLD),
61 COLOR_ATTR(attr_input_box, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
62 COLOR_ATTR(attr_input_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
63 COLOR_ATTR(attr_input_text, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
64 COLOR_ATTR(attr_input_field, COLOR_DEFAULT, COLOR_DEFAULT, A_UNDERLINE),
65 COLOR_ATTR(attr_function_text, COLOR_YELLOW, COLOR_DEFAULT, A_REVERSE),
66 COLOR_ATTR(attr_function_highlight, COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD),
67 { /* sentinel */ }
68 };
69
70 static const struct nconf_attr_param no_color_theme_params[] = {
71 NO_COLOR_ATTR(attr_normal, A_NORMAL),
72 NO_COLOR_ATTR(attr_main_heading, A_BOLD | A_UNDERLINE),
73 NO_COLOR_ATTR(attr_main_menu_box, A_NORMAL),
74 NO_COLOR_ATTR(attr_main_menu_fore, A_STANDOUT),
75 NO_COLOR_ATTR(attr_main_menu_back, A_NORMAL),
76 NO_COLOR_ATTR(attr_main_menu_grey, A_NORMAL),
77 NO_COLOR_ATTR(attr_main_menu_heading, A_BOLD),
78 NO_COLOR_ATTR(attr_scrollwin_text, A_NORMAL),
79 NO_COLOR_ATTR(attr_scrollwin_heading, A_BOLD),
80 NO_COLOR_ATTR(attr_scrollwin_box, A_BOLD),
81 NO_COLOR_ATTR(attr_dialog_text, A_NORMAL),
82 NO_COLOR_ATTR(attr_dialog_menu_fore, A_STANDOUT),
83 NO_COLOR_ATTR(attr_dialog_menu_back, A_NORMAL),
84 NO_COLOR_ATTR(attr_dialog_box, A_BOLD),
85 NO_COLOR_ATTR(attr_input_box, A_BOLD),
86 NO_COLOR_ATTR(attr_input_heading, A_BOLD),
87 NO_COLOR_ATTR(attr_input_text, A_NORMAL),
88 NO_COLOR_ATTR(attr_input_field, A_UNDERLINE),
89 NO_COLOR_ATTR(attr_function_text, A_REVERSE),
90 NO_COLOR_ATTR(attr_function_highlight, A_BOLD),
91 { /* sentinel */ }
92 };
93
set_colors(void)94 void set_colors(void)
95 {
96 const struct nconf_attr_param *p;
97 int pair = 0;
98
99 if (has_colors()) {
100 start_color();
101 use_default_colors();
102 p = color_theme_params;
103 } else {
104 p = no_color_theme_params;
105 }
106
107 for (; p->attr; p++) {
108 int attr = p->highlight;
109
110 if (p->has_color) {
111 pair++;
112 init_pair(pair, p->color_fg, p->color_bg);
113 attr |= COLOR_PAIR(pair);
114 }
115
116 *p->attr = attr;
117 }
118 }
119
120 /* this changes the windows attributes !!! */
print_in_middle(WINDOW * win,int y,int width,const char * str,int attrs)121 void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs)
122 {
123 wattrset(win, attrs);
124 mvwprintw(win, y, (width - strlen(str)) / 2, "%s", str);
125 }
126
get_line_no(const char * text)127 int get_line_no(const char *text)
128 {
129 int i;
130 int total = 1;
131
132 if (!text)
133 return 0;
134
135 for (i = 0; text[i] != '\0'; i++)
136 if (text[i] == '\n')
137 total++;
138 return total;
139 }
140
get_line(const char * text,int line_no)141 const char *get_line(const char *text, int line_no)
142 {
143 int i;
144 int lines = 0;
145
146 if (!text)
147 return NULL;
148
149 for (i = 0; text[i] != '\0' && lines < line_no; i++)
150 if (text[i] == '\n')
151 lines++;
152 return text+i;
153 }
154
get_line_length(const char * line)155 int get_line_length(const char *line)
156 {
157 int res = 0;
158 while (*line != '\0' && *line != '\n') {
159 line++;
160 res++;
161 }
162 return res;
163 }
164
165 /* print all lines to the window. */
fill_window(WINDOW * win,const char * text)166 void fill_window(WINDOW *win, const char *text)
167 {
168 int x, y;
169 int total_lines = get_line_no(text);
170 int i;
171
172 getmaxyx(win, y, x);
173 /* do not go over end of line */
174 total_lines = min(total_lines, y);
175 for (i = 0; i < total_lines; i++) {
176 char tmp[x+10];
177 const char *line = get_line(text, i);
178 int len = get_line_length(line);
179 strncpy(tmp, line, min(len, x));
180 tmp[len] = '\0';
181 mvwprintw(win, i, 0, "%s", tmp);
182 }
183 }
184
185 /* get the message, and buttons.
186 * each button must be a char*
187 * return the selected button
188 *
189 * this dialog is used for 2 different things:
190 * 1) show a text box, no buttons.
191 * 2) show a dialog, with horizontal buttons
192 */
btn_dialog(WINDOW * main_window,const char * msg,int btn_num,...)193 int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...)
194 {
195 va_list ap;
196 char *btn;
197 int btns_width = 0;
198 int msg_lines = 0;
199 int msg_width = 0;
200 int total_width;
201 int win_rows = 0;
202 WINDOW *win;
203 WINDOW *msg_win;
204 WINDOW *menu_win;
205 MENU *menu;
206 ITEM *btns[btn_num+1];
207 int i, x, y;
208 int res = -1;
209
210
211 va_start(ap, btn_num);
212 for (i = 0; i < btn_num; i++) {
213 btn = va_arg(ap, char *);
214 btns[i] = new_item(btn, "");
215 btns_width += strlen(btn)+1;
216 }
217 va_end(ap);
218 btns[btn_num] = NULL;
219
220 /* find the widest line of msg: */
221 msg_lines = get_line_no(msg);
222 for (i = 0; i < msg_lines; i++) {
223 const char *line = get_line(msg, i);
224 int len = get_line_length(line);
225 if (msg_width < len)
226 msg_width = len;
227 }
228
229 total_width = max(msg_width, btns_width);
230 /* place dialog in middle of screen */
231 y = (getmaxy(stdscr)-(msg_lines+4))/2;
232 x = (getmaxx(stdscr)-(total_width+4))/2;
233
234
235 /* create the windows */
236 if (btn_num > 0)
237 win_rows = msg_lines+4;
238 else
239 win_rows = msg_lines+2;
240
241 win = newwin(win_rows, total_width+4, y, x);
242 keypad(win, TRUE);
243 menu_win = derwin(win, 1, btns_width, win_rows-2,
244 1+(total_width+2-btns_width)/2);
245 menu = new_menu(btns);
246 msg_win = derwin(win, win_rows-2, msg_width, 1,
247 1+(total_width+2-msg_width)/2);
248
249 set_menu_fore(menu, attr_dialog_menu_fore);
250 set_menu_back(menu, attr_dialog_menu_back);
251
252 wattrset(win, attr_dialog_box);
253 box(win, 0, 0);
254
255 /* print message */
256 wattrset(msg_win, attr_dialog_text);
257 fill_window(msg_win, msg);
258
259 set_menu_win(menu, win);
260 set_menu_sub(menu, menu_win);
261 set_menu_format(menu, 1, btn_num);
262 menu_opts_off(menu, O_SHOWDESC);
263 menu_opts_off(menu, O_SHOWMATCH);
264 menu_opts_on(menu, O_ONEVALUE);
265 menu_opts_on(menu, O_NONCYCLIC);
266 set_menu_mark(menu, "");
267 post_menu(menu);
268
269
270 touchwin(win);
271 refresh_all_windows(main_window);
272 while ((res = wgetch(win))) {
273 switch (res) {
274 case KEY_LEFT:
275 menu_driver(menu, REQ_LEFT_ITEM);
276 break;
277 case KEY_RIGHT:
278 menu_driver(menu, REQ_RIGHT_ITEM);
279 break;
280 case 9: /* TAB */
281 if (btn_num > 1) {
282 /* cycle through buttons */
283 if (item_index(current_item(menu)) == btn_num - 1)
284 menu_driver(menu, REQ_FIRST_ITEM);
285 else
286 menu_driver(menu, REQ_NEXT_ITEM);
287 }
288 break;
289 case 10: /* ENTER */
290 case 27: /* ESCAPE */
291 case ' ':
292 case KEY_F(F_BACK):
293 case KEY_F(F_EXIT):
294 break;
295 }
296 touchwin(win);
297 refresh_all_windows(main_window);
298
299 if (res == 10 || res == ' ') {
300 res = item_index(current_item(menu));
301 break;
302 } else if (res == 27 || res == KEY_F(F_BACK) ||
303 res == KEY_F(F_EXIT)) {
304 res = KEY_EXIT;
305 break;
306 }
307 }
308
309 unpost_menu(menu);
310 free_menu(menu);
311 for (i = 0; i < btn_num; i++)
312 free_item(btns[i]);
313
314 delwin(win);
315 return res;
316 }
317
dialog_inputbox(WINDOW * main_window,const char * title,const char * prompt,const char * init,char ** resultp,int * result_len)318 int dialog_inputbox(WINDOW *main_window,
319 const char *title, const char *prompt,
320 const char *init, char **resultp, int *result_len)
321 {
322 int prompt_lines = 0;
323 int prompt_width = 0;
324 WINDOW *win;
325 WINDOW *prompt_win;
326 WINDOW *form_win;
327 PANEL *panel;
328 int i, x, y, lines, columns, win_lines, win_cols;
329 int res = -1;
330 int cursor_position = strlen(init);
331 int cursor_form_win;
332 char *result = *resultp;
333
334 getmaxyx(stdscr, lines, columns);
335
336 if (strlen(init)+1 > *result_len) {
337 *result_len = strlen(init)+1;
338 *resultp = result = xrealloc(result, *result_len);
339 }
340
341 /* find the widest line of msg: */
342 prompt_lines = get_line_no(prompt);
343 for (i = 0; i < prompt_lines; i++) {
344 const char *line = get_line(prompt, i);
345 int len = get_line_length(line);
346 prompt_width = max(prompt_width, len);
347 }
348
349 if (title)
350 prompt_width = max(prompt_width, strlen(title));
351
352 win_lines = min(prompt_lines+6, lines-2);
353 win_cols = min(prompt_width+7, columns-2);
354 prompt_lines = max(win_lines-6, 0);
355 prompt_width = max(win_cols-7, 0);
356
357 /* place dialog in middle of screen */
358 y = (lines-win_lines)/2;
359 x = (columns-win_cols)/2;
360
361 strncpy(result, init, *result_len);
362 result[*result_len - 1] = '\0';
363
364 /* create the windows */
365 win = newwin(win_lines, win_cols, y, x);
366 prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2);
367 form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2);
368 keypad(form_win, TRUE);
369
370 wattrset(form_win, attr_input_field);
371
372 wattrset(win, attr_input_box);
373 box(win, 0, 0);
374 wattrset(win, attr_input_heading);
375 if (title)
376 mvwprintw(win, 0, 3, "%s", title);
377
378 /* print message */
379 wattrset(prompt_win, attr_input_text);
380 fill_window(prompt_win, prompt);
381
382 mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
383 cursor_form_win = min(cursor_position, prompt_width-1);
384 mvwprintw(form_win, 0, 0, "%s",
385 result + cursor_position-cursor_form_win);
386
387 /* create panels */
388 panel = new_panel(win);
389
390 /* show the cursor */
391 curs_set(1);
392
393 touchwin(win);
394 refresh_all_windows(main_window);
395 while ((res = wgetch(form_win))) {
396 int len = strlen(result);
397 switch (res) {
398 case 10: /* ENTER */
399 case 27: /* ESCAPE */
400 case KEY_F(F_HELP):
401 case KEY_F(F_EXIT):
402 case KEY_F(F_BACK):
403 break;
404 case 8: /* ^H */
405 case 127: /* ^? */
406 case KEY_BACKSPACE:
407 if (cursor_position > 0) {
408 memmove(&result[cursor_position-1],
409 &result[cursor_position],
410 len-cursor_position+1);
411 cursor_position--;
412 cursor_form_win--;
413 len--;
414 }
415 break;
416 case KEY_DC:
417 if (cursor_position >= 0 && cursor_position < len) {
418 memmove(&result[cursor_position],
419 &result[cursor_position+1],
420 len-cursor_position+1);
421 len--;
422 }
423 break;
424 case KEY_UP:
425 case KEY_RIGHT:
426 if (cursor_position < len) {
427 cursor_position++;
428 cursor_form_win++;
429 }
430 break;
431 case KEY_DOWN:
432 case KEY_LEFT:
433 if (cursor_position > 0) {
434 cursor_position--;
435 cursor_form_win--;
436 }
437 break;
438 case KEY_HOME:
439 cursor_position = 0;
440 cursor_form_win = 0;
441 break;
442 case KEY_END:
443 cursor_position = len;
444 cursor_form_win = min(cursor_position, prompt_width-1);
445 break;
446 default:
447 if ((isgraph(res) || isspace(res))) {
448 /* one for new char, one for '\0' */
449 if (len+2 > *result_len) {
450 *result_len = len+2;
451 *resultp = result = realloc(result,
452 *result_len);
453 }
454 /* insert the char at the proper position */
455 memmove(&result[cursor_position+1],
456 &result[cursor_position],
457 len-cursor_position+1);
458 result[cursor_position] = res;
459 cursor_position++;
460 cursor_form_win++;
461 len++;
462 } else {
463 mvprintw(0, 0, "unknown key: %d\n", res);
464 }
465 break;
466 }
467 if (cursor_form_win < 0)
468 cursor_form_win = 0;
469 else if (cursor_form_win > prompt_width-1)
470 cursor_form_win = prompt_width-1;
471
472 wmove(form_win, 0, 0);
473 wclrtoeol(form_win);
474 mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
475 mvwprintw(form_win, 0, 0, "%s",
476 result + cursor_position-cursor_form_win);
477 wmove(form_win, 0, cursor_form_win);
478 touchwin(win);
479 refresh_all_windows(main_window);
480
481 if (res == 10) {
482 res = 0;
483 break;
484 } else if (res == 27 || res == KEY_F(F_BACK) ||
485 res == KEY_F(F_EXIT)) {
486 res = KEY_EXIT;
487 break;
488 } else if (res == KEY_F(F_HELP)) {
489 res = 1;
490 break;
491 }
492 }
493
494 /* hide the cursor */
495 curs_set(0);
496 del_panel(panel);
497 delwin(prompt_win);
498 delwin(form_win);
499 delwin(win);
500 return res;
501 }
502
503 /* refresh all windows in the correct order */
refresh_all_windows(WINDOW * main_window)504 void refresh_all_windows(WINDOW *main_window)
505 {
506 update_panels();
507 touchwin(main_window);
508 refresh();
509 }
510
show_scroll_win(WINDOW * main_window,const char * title,const char * text)511 void show_scroll_win(WINDOW *main_window,
512 const char *title,
513 const char *text)
514 {
515 (void)show_scroll_win_ext(main_window, title, (char *)text, NULL, NULL, NULL, NULL);
516 }
517
518 /* layman's scrollable window... */
show_scroll_win_ext(WINDOW * main_window,const char * title,char * text,int * vscroll,int * hscroll,extra_key_cb_fn extra_key_cb,void * data)519 int show_scroll_win_ext(WINDOW *main_window, const char *title, char *text,
520 int *vscroll, int *hscroll,
521 extra_key_cb_fn extra_key_cb, void *data)
522 {
523 int res;
524 int total_lines = get_line_no(text);
525 int x, y, lines, columns;
526 int start_x = 0, start_y = 0;
527 int text_lines = 0, text_cols = 0;
528 int total_cols = 0;
529 int win_cols = 0;
530 int win_lines = 0;
531 int i = 0;
532 WINDOW *win;
533 WINDOW *pad;
534 PANEL *panel;
535 bool done = false;
536
537 if (hscroll)
538 start_x = *hscroll;
539 if (vscroll)
540 start_y = *vscroll;
541
542 getmaxyx(stdscr, lines, columns);
543
544 /* find the widest line of msg: */
545 total_lines = get_line_no(text);
546 for (i = 0; i < total_lines; i++) {
547 const char *line = get_line(text, i);
548 int len = get_line_length(line);
549 total_cols = max(total_cols, len+2);
550 }
551
552 /* create the pad */
553 pad = newpad(total_lines+10, total_cols+10);
554 wattrset(pad, attr_scrollwin_text);
555 fill_window(pad, text);
556
557 win_lines = min(total_lines+4, lines-2);
558 win_cols = min(total_cols+2, columns-2);
559 text_lines = max(win_lines-4, 0);
560 text_cols = max(win_cols-2, 0);
561
562 /* place window in middle of screen */
563 y = (lines-win_lines)/2;
564 x = (columns-win_cols)/2;
565
566 win = newwin(win_lines, win_cols, y, x);
567 keypad(win, TRUE);
568 /* show the help in the help window, and show the help panel */
569 wattrset(win, attr_scrollwin_box);
570 box(win, 0, 0);
571 wattrset(win, attr_scrollwin_heading);
572 mvwprintw(win, 0, 3, " %s ", title);
573 panel = new_panel(win);
574
575 /* handle scrolling */
576 while (!done) {
577 copywin(pad, win, start_y, start_x, 2, 2, text_lines,
578 text_cols, 0);
579 print_in_middle(win,
580 text_lines+2,
581 text_cols,
582 "<OK>",
583 attr_dialog_menu_fore);
584 wrefresh(win);
585
586 res = wgetch(win);
587 switch (res) {
588 case KEY_NPAGE:
589 case ' ':
590 case 'd':
591 start_y += text_lines-2;
592 break;
593 case KEY_PPAGE:
594 case 'u':
595 start_y -= text_lines+2;
596 break;
597 case KEY_HOME:
598 start_y = 0;
599 break;
600 case KEY_END:
601 start_y = total_lines-text_lines;
602 break;
603 case KEY_DOWN:
604 case 'j':
605 start_y++;
606 break;
607 case KEY_UP:
608 case 'k':
609 start_y--;
610 break;
611 case KEY_LEFT:
612 case 'h':
613 start_x--;
614 break;
615 case KEY_RIGHT:
616 case 'l':
617 start_x++;
618 break;
619 default:
620 if (extra_key_cb) {
621 size_t start = (get_line(text, start_y) - text);
622 size_t end = (get_line(text, start_y + text_lines) - text);
623
624 if (extra_key_cb(res, start, end, data)) {
625 done = true;
626 break;
627 }
628 }
629 }
630 if (res == 0 || res == 10 || res == 27 || res == 'q' ||
631 res == KEY_F(F_HELP) || res == KEY_F(F_BACK) ||
632 res == KEY_F(F_EXIT))
633 break;
634 if (start_y < 0)
635 start_y = 0;
636 if (start_y >= total_lines-text_lines)
637 start_y = total_lines-text_lines;
638 if (start_x < 0)
639 start_x = 0;
640 if (start_x >= total_cols-text_cols)
641 start_x = total_cols-text_cols;
642 }
643
644 if (hscroll)
645 *hscroll = start_x;
646 if (vscroll)
647 *vscroll = start_y;
648 del_panel(panel);
649 delwin(win);
650 refresh_all_windows(main_window);
651 return res;
652 }
653