xref: /linux/scripts/kconfig/lxdialog/util.c (revision f898c16a0624e7f2dcb0b1cda6916c9be6489197)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  util.c
4  *
5  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
7  */
8 
9 #include <stdarg.h>
10 
11 #include "dialog.h"
12 
13 /* Needed in signal handler in mconf.c */
14 int saved_x, saved_y;
15 
16 struct dialog_info dlg;
17 
18 static void set_mono_theme(void)
19 {
20 	dlg.title.atr = A_BOLD;
21 	dlg.button_active.atr = A_REVERSE;
22 	dlg.button_inactive.atr = A_DIM;
23 	dlg.button_key_active.atr = A_REVERSE;
24 	dlg.button_key_inactive.atr = A_BOLD;
25 	dlg.button_label_active.atr = A_REVERSE;
26 	dlg.position_indicator.atr = A_BOLD;
27 	dlg.item_selected.atr = A_REVERSE;
28 	dlg.tag.atr = A_BOLD;
29 	dlg.tag_selected.atr = A_REVERSE;
30 	dlg.tag_key.atr = A_BOLD;
31 	dlg.tag_key_selected.atr = A_REVERSE;
32 	dlg.check.atr = A_BOLD;
33 	dlg.check_selected.atr = A_REVERSE;
34 	dlg.uarrow.atr = A_BOLD;
35 	dlg.darrow.atr = A_BOLD;
36 }
37 
38 #define DLG_COLOR(dialog, f, b, h) \
39 do {                               \
40 	dlg.dialog.fg = (f);       \
41 	dlg.dialog.bg = (b);       \
42 	dlg.dialog.hl = (h);       \
43 } while (0)
44 
45 static void set_classic_theme(void)
46 {
47 	DLG_COLOR(screen,                COLOR_CYAN,   COLOR_BLUE,   true);
48 	DLG_COLOR(shadow,                COLOR_BLACK,  COLOR_BLACK,  true);
49 	DLG_COLOR(dialog,                COLOR_BLACK,  COLOR_WHITE,  false);
50 	DLG_COLOR(title,                 COLOR_YELLOW, COLOR_WHITE,  true);
51 	DLG_COLOR(border,                COLOR_WHITE,  COLOR_WHITE,  true);
52 	DLG_COLOR(button_active,         COLOR_WHITE,  COLOR_BLUE,   true);
53 	DLG_COLOR(button_inactive,       COLOR_BLACK,  COLOR_WHITE,  false);
54 	DLG_COLOR(button_key_active,     COLOR_WHITE,  COLOR_BLUE,   true);
55 	DLG_COLOR(button_key_inactive,   COLOR_RED,    COLOR_WHITE,  false);
56 	DLG_COLOR(button_label_active,   COLOR_YELLOW, COLOR_BLUE,   true);
57 	DLG_COLOR(button_label_inactive, COLOR_BLACK,  COLOR_WHITE,  true);
58 	DLG_COLOR(inputbox,              COLOR_BLACK,  COLOR_WHITE,  false);
59 	DLG_COLOR(position_indicator,    COLOR_YELLOW, COLOR_WHITE,  true);
60 	DLG_COLOR(menubox,               COLOR_BLACK,  COLOR_WHITE,  false);
61 	DLG_COLOR(menubox_border,        COLOR_WHITE,  COLOR_WHITE,  true);
62 	DLG_COLOR(item,                  COLOR_BLACK,  COLOR_WHITE,  false);
63 	DLG_COLOR(item_selected,         COLOR_WHITE,  COLOR_BLUE,   true);
64 	DLG_COLOR(tag,                   COLOR_YELLOW, COLOR_WHITE,  true);
65 	DLG_COLOR(tag_selected,          COLOR_YELLOW, COLOR_BLUE,   true);
66 	DLG_COLOR(tag_key,               COLOR_YELLOW, COLOR_WHITE,  true);
67 	DLG_COLOR(tag_key_selected,      COLOR_YELLOW, COLOR_BLUE,   true);
68 	DLG_COLOR(check,                 COLOR_BLACK,  COLOR_WHITE,  false);
69 	DLG_COLOR(check_selected,        COLOR_WHITE,  COLOR_BLUE,   true);
70 	DLG_COLOR(uarrow,                COLOR_GREEN,  COLOR_WHITE,  true);
71 	DLG_COLOR(darrow,                COLOR_GREEN,  COLOR_WHITE,  true);
72 }
73 
74 static void set_blackbg_theme(void)
75 {
76 	DLG_COLOR(screen, COLOR_RED,   COLOR_BLACK, true);
77 	DLG_COLOR(shadow, COLOR_BLACK, COLOR_BLACK, false);
78 	DLG_COLOR(dialog, COLOR_WHITE, COLOR_BLACK, false);
79 	DLG_COLOR(title,  COLOR_RED,   COLOR_BLACK, false);
80 	DLG_COLOR(border, COLOR_BLACK, COLOR_BLACK, true);
81 
82 	DLG_COLOR(button_active,         COLOR_YELLOW, COLOR_RED,   false);
83 	DLG_COLOR(button_inactive,       COLOR_YELLOW, COLOR_BLACK, false);
84 	DLG_COLOR(button_key_active,     COLOR_YELLOW, COLOR_RED,   true);
85 	DLG_COLOR(button_key_inactive,   COLOR_RED,    COLOR_BLACK, false);
86 	DLG_COLOR(button_label_active,   COLOR_WHITE,  COLOR_RED,   false);
87 	DLG_COLOR(button_label_inactive, COLOR_WHITE,  COLOR_BLACK, false);
88 
89 	DLG_COLOR(inputbox,         COLOR_YELLOW, COLOR_BLACK, false);
90 
91 	DLG_COLOR(position_indicator, COLOR_RED, COLOR_BLACK,  false);
92 
93 	DLG_COLOR(menubox,          COLOR_YELLOW, COLOR_BLACK, false);
94 	DLG_COLOR(menubox_border,   COLOR_BLACK,  COLOR_BLACK, true);
95 
96 	DLG_COLOR(item,             COLOR_WHITE, COLOR_BLACK, false);
97 	DLG_COLOR(item_selected,    COLOR_WHITE, COLOR_RED,   false);
98 
99 	DLG_COLOR(tag,              COLOR_RED,    COLOR_BLACK, false);
100 	DLG_COLOR(tag_selected,     COLOR_YELLOW, COLOR_RED,   true);
101 	DLG_COLOR(tag_key,          COLOR_RED,    COLOR_BLACK, false);
102 	DLG_COLOR(tag_key_selected, COLOR_YELLOW, COLOR_RED,   true);
103 
104 	DLG_COLOR(check,            COLOR_YELLOW, COLOR_BLACK, false);
105 	DLG_COLOR(check_selected,   COLOR_YELLOW, COLOR_RED,   true);
106 
107 	DLG_COLOR(uarrow, COLOR_RED, COLOR_BLACK, false);
108 	DLG_COLOR(darrow, COLOR_RED, COLOR_BLACK, false);
109 }
110 
111 static void set_bluetitle_theme(void)
112 {
113 	set_classic_theme();
114 	DLG_COLOR(title,               COLOR_BLUE,   COLOR_WHITE, true);
115 	DLG_COLOR(button_key_active,   COLOR_YELLOW, COLOR_BLUE,  true);
116 	DLG_COLOR(button_label_active, COLOR_WHITE,  COLOR_BLUE,  true);
117 	DLG_COLOR(position_indicator,  COLOR_BLUE,   COLOR_WHITE, true);
118 	DLG_COLOR(tag,                 COLOR_BLUE,   COLOR_WHITE, true);
119 	DLG_COLOR(tag_key,             COLOR_BLUE,   COLOR_WHITE, true);
120 
121 }
122 
123 /*
124  * Select color theme
125  */
126 static int set_theme(const char *theme)
127 {
128 	int use_color = 1;
129 	if (!theme)
130 		set_bluetitle_theme();
131 	else if (strcmp(theme, "classic") == 0)
132 		set_classic_theme();
133 	else if (strcmp(theme, "bluetitle") == 0)
134 		set_bluetitle_theme();
135 	else if (strcmp(theme, "blackbg") == 0)
136 		set_blackbg_theme();
137 	else if (strcmp(theme, "mono") == 0)
138 		use_color = 0;
139 
140 	return use_color;
141 }
142 
143 static void init_one_color(struct dialog_color *color)
144 {
145 	static int pair = 0;
146 
147 	pair++;
148 	init_pair(pair, color->fg, color->bg);
149 	if (color->hl)
150 		color->atr = A_BOLD | COLOR_PAIR(pair);
151 	else
152 		color->atr = COLOR_PAIR(pair);
153 }
154 
155 static void init_dialog_colors(void)
156 {
157 	init_one_color(&dlg.screen);
158 	init_one_color(&dlg.shadow);
159 	init_one_color(&dlg.dialog);
160 	init_one_color(&dlg.title);
161 	init_one_color(&dlg.border);
162 	init_one_color(&dlg.button_active);
163 	init_one_color(&dlg.button_inactive);
164 	init_one_color(&dlg.button_key_active);
165 	init_one_color(&dlg.button_key_inactive);
166 	init_one_color(&dlg.button_label_active);
167 	init_one_color(&dlg.button_label_inactive);
168 	init_one_color(&dlg.inputbox);
169 	init_one_color(&dlg.position_indicator);
170 	init_one_color(&dlg.menubox);
171 	init_one_color(&dlg.menubox_border);
172 	init_one_color(&dlg.item);
173 	init_one_color(&dlg.item_selected);
174 	init_one_color(&dlg.tag);
175 	init_one_color(&dlg.tag_selected);
176 	init_one_color(&dlg.tag_key);
177 	init_one_color(&dlg.tag_key_selected);
178 	init_one_color(&dlg.check);
179 	init_one_color(&dlg.check_selected);
180 	init_one_color(&dlg.uarrow);
181 	init_one_color(&dlg.darrow);
182 }
183 
184 /*
185  * Setup for color display
186  */
187 static void color_setup(const char *theme)
188 {
189 	int use_color;
190 
191 	use_color = set_theme(theme);
192 	if (use_color && has_colors()) {
193 		start_color();
194 		init_dialog_colors();
195 	} else
196 		set_mono_theme();
197 }
198 
199 /*
200  * Set window to attribute 'attr'
201  */
202 void attr_clear(WINDOW * win, int height, int width, chtype attr)
203 {
204 	int i, j;
205 
206 	wattrset(win, attr);
207 	for (i = 0; i < height; i++) {
208 		wmove(win, i, 0);
209 		for (j = 0; j < width; j++)
210 			waddch(win, ' ');
211 	}
212 	touchwin(win);
213 }
214 
215 void dialog_clear(void)
216 {
217 	int lines, columns;
218 
219 	lines = getmaxy(stdscr);
220 	columns = getmaxx(stdscr);
221 
222 	attr_clear(stdscr, lines, columns, dlg.screen.atr);
223 	/* Display background title if it exists ... - SLH */
224 	if (dlg.backtitle != NULL) {
225 		int i, len = 0, skip = 0;
226 		struct subtitle_list *pos;
227 
228 		wattrset(stdscr, dlg.screen.atr);
229 		mvwaddstr(stdscr, 0, 1, (char *)dlg.backtitle);
230 
231 		for (pos = dlg.subtitles; pos != NULL; pos = pos->next) {
232 			/* 3 is for the arrow and spaces */
233 			len += strlen(pos->text) + 3;
234 		}
235 
236 		wmove(stdscr, 1, 1);
237 		if (len > columns - 2) {
238 			const char *ellipsis = "[...] ";
239 			waddstr(stdscr, ellipsis);
240 			skip = len - (columns - 2 - strlen(ellipsis));
241 		}
242 
243 		for (pos = dlg.subtitles; pos != NULL; pos = pos->next) {
244 			if (skip == 0)
245 				waddch(stdscr, ACS_RARROW);
246 			else
247 				skip--;
248 
249 			if (skip == 0)
250 				waddch(stdscr, ' ');
251 			else
252 				skip--;
253 
254 			if (skip < strlen(pos->text)) {
255 				waddstr(stdscr, pos->text + skip);
256 				skip = 0;
257 			} else
258 				skip -= strlen(pos->text);
259 
260 			if (skip == 0)
261 				waddch(stdscr, ' ');
262 			else
263 				skip--;
264 		}
265 
266 		for (i = len + 1; i < columns - 1; i++)
267 			waddch(stdscr, ACS_HLINE);
268 	}
269 	wnoutrefresh(stdscr);
270 }
271 
272 /*
273  * Do some initialization for dialog
274  */
275 int init_dialog(const char *backtitle)
276 {
277 	int height, width;
278 
279 	initscr();		/* Init curses */
280 
281 	/* Get current cursor position for signal handler in mconf.c */
282 	getyx(stdscr, saved_y, saved_x);
283 
284 	getmaxyx(stdscr, height, width);
285 	if (height < WINDOW_HEIGHT_MIN || width < WINDOW_WIDTH_MIN) {
286 		endwin();
287 		return -ERRDISPLAYTOOSMALL;
288 	}
289 
290 	dlg.backtitle = backtitle;
291 	color_setup(getenv("MENUCONFIG_COLOR"));
292 
293 	keypad(stdscr, TRUE);
294 	cbreak();
295 	noecho();
296 	dialog_clear();
297 
298 	return 0;
299 }
300 
301 void set_dialog_backtitle(const char *backtitle)
302 {
303 	dlg.backtitle = backtitle;
304 }
305 
306 void set_dialog_subtitles(struct subtitle_list *subtitles)
307 {
308 	dlg.subtitles = subtitles;
309 }
310 
311 /*
312  * End using dialog functions.
313  */
314 void end_dialog(int x, int y)
315 {
316 	/* move cursor back to original position */
317 	move(y, x);
318 	refresh();
319 	endwin();
320 }
321 
322 /* Print the title of the dialog. Center the title and truncate
323  * tile if wider than dialog (- 2 chars).
324  **/
325 void print_title(WINDOW *dialog, const char *title, int width)
326 {
327 	if (title) {
328 		int tlen = MIN(width - 2, strlen(title));
329 		wattrset(dialog, dlg.title.atr);
330 		mvwaddch(dialog, 0, (width - tlen) / 2 - 1, ' ');
331 		mvwaddnstr(dialog, 0, (width - tlen)/2, title, tlen);
332 		waddch(dialog, ' ');
333 	}
334 }
335 
336 /*
337  * Print a string of text in a window, automatically wrap around to the
338  * next line if the string is too long to fit on one line. Newline
339  * characters '\n' are properly processed.  We start on a new line
340  * if there is no room for at least 4 nonblanks following a double-space.
341  */
342 void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x)
343 {
344 	int newl, cur_x, cur_y;
345 	int prompt_len, room, wlen;
346 	char tempstr[MAX_LEN + 1], *word, *sp, *sp2, *newline_separator = 0;
347 
348 	strcpy(tempstr, prompt);
349 
350 	prompt_len = strlen(tempstr);
351 
352 	if (prompt_len <= width - x * 2) {	/* If prompt is short */
353 		wmove(win, y, (width - prompt_len) / 2);
354 		waddstr(win, tempstr);
355 	} else {
356 		cur_x = x;
357 		cur_y = y;
358 		newl = 1;
359 		word = tempstr;
360 		while (word && *word) {
361 			sp = strpbrk(word, "\n ");
362 			if (sp && *sp == '\n')
363 				newline_separator = sp;
364 
365 			if (sp)
366 				*sp++ = 0;
367 
368 			/* Wrap to next line if either the word does not fit,
369 			   or it is the first word of a new sentence, and it is
370 			   short, and the next word does not fit. */
371 			room = width - cur_x;
372 			wlen = strlen(word);
373 			if (wlen > room ||
374 			    (newl && wlen < 4 && sp
375 			     && wlen + 1 + strlen(sp) > room
376 			     && (!(sp2 = strpbrk(sp, "\n "))
377 				 || wlen + 1 + (sp2 - sp) > room))) {
378 				cur_y++;
379 				cur_x = x;
380 			}
381 			wmove(win, cur_y, cur_x);
382 			waddstr(win, word);
383 			getyx(win, cur_y, cur_x);
384 
385 			/* Move to the next line if the word separator was a newline */
386 			if (newline_separator) {
387 				cur_y++;
388 				cur_x = x;
389 				newline_separator = 0;
390 			} else
391 				cur_x++;
392 
393 			if (sp && *sp == ' ') {
394 				cur_x++;	/* double space */
395 				while (*++sp == ' ') ;
396 				newl = 1;
397 			} else
398 				newl = 0;
399 			word = sp;
400 		}
401 	}
402 }
403 
404 /*
405  * Print a button
406  */
407 void print_button(WINDOW * win, const char *label, int y, int x, int selected)
408 {
409 	int i, temp;
410 
411 	wmove(win, y, x);
412 	wattrset(win, selected ? dlg.button_active.atr
413 		 : dlg.button_inactive.atr);
414 	waddstr(win, "<");
415 	temp = strspn(label, " ");
416 	label += temp;
417 	wattrset(win, selected ? dlg.button_label_active.atr
418 		 : dlg.button_label_inactive.atr);
419 	for (i = 0; i < temp; i++)
420 		waddch(win, ' ');
421 	wattrset(win, selected ? dlg.button_key_active.atr
422 		 : dlg.button_key_inactive.atr);
423 	waddch(win, label[0]);
424 	wattrset(win, selected ? dlg.button_label_active.atr
425 		 : dlg.button_label_inactive.atr);
426 	waddstr(win, (char *)label + 1);
427 	wattrset(win, selected ? dlg.button_active.atr
428 		 : dlg.button_inactive.atr);
429 	waddstr(win, ">");
430 	wmove(win, y, x + temp + 1);
431 }
432 
433 /*
434  * Draw a rectangular box with line drawing characters
435  */
436 void
437 draw_box(WINDOW * win, int y, int x, int height, int width,
438 	 chtype box, chtype border)
439 {
440 	int i, j;
441 
442 	wattrset(win, 0);
443 	for (i = 0; i < height; i++) {
444 		wmove(win, y + i, x);
445 		for (j = 0; j < width; j++)
446 			if (!i && !j)
447 				waddch(win, border | ACS_ULCORNER);
448 			else if (i == height - 1 && !j)
449 				waddch(win, border | ACS_LLCORNER);
450 			else if (!i && j == width - 1)
451 				waddch(win, box | ACS_URCORNER);
452 			else if (i == height - 1 && j == width - 1)
453 				waddch(win, box | ACS_LRCORNER);
454 			else if (!i)
455 				waddch(win, border | ACS_HLINE);
456 			else if (i == height - 1)
457 				waddch(win, box | ACS_HLINE);
458 			else if (!j)
459 				waddch(win, border | ACS_VLINE);
460 			else if (j == width - 1)
461 				waddch(win, box | ACS_VLINE);
462 			else
463 				waddch(win, box | ' ');
464 	}
465 }
466 
467 /*
468  * Draw shadows along the right and bottom edge to give a more 3D look
469  * to the boxes
470  */
471 void draw_shadow(WINDOW * win, int y, int x, int height, int width)
472 {
473 	int i;
474 
475 	if (has_colors()) {	/* Whether terminal supports color? */
476 		wattrset(win, dlg.shadow.atr);
477 		wmove(win, y + height, x + 2);
478 		for (i = 0; i < width; i++)
479 			waddch(win, winch(win) & A_CHARTEXT);
480 		for (i = y + 1; i < y + height + 1; i++) {
481 			wmove(win, i, x + width);
482 			waddch(win, winch(win) & A_CHARTEXT);
483 			waddch(win, winch(win) & A_CHARTEXT);
484 		}
485 		wnoutrefresh(win);
486 	}
487 }
488 
489 /*
490  *  Return the position of the first alphabetic character in a string.
491  */
492 int first_alpha(const char *string, const char *exempt)
493 {
494 	int i, in_paren = 0, c;
495 
496 	for (i = 0; i < strlen(string); i++) {
497 		c = tolower(string[i]);
498 
499 		if (strchr("<[(", c))
500 			++in_paren;
501 		if (strchr(">])", c) && in_paren > 0)
502 			--in_paren;
503 
504 		if ((!in_paren) && isalpha(c) && strchr(exempt, c) == 0)
505 			return i;
506 	}
507 
508 	return 0;
509 }
510 
511 /*
512  * ncurses uses ESC to detect escaped char sequences. This resutl in
513  * a small timeout before ESC is actually delivered to the application.
514  * lxdialog suggest <ESC> <ESC> which is correctly translated to two
515  * times esc. But then we need to ignore the second esc to avoid stepping
516  * out one menu too much. Filter away all escaped key sequences since
517  * keypad(FALSE) turn off ncurses support for escape sequences - and that's
518  * needed to make notimeout() do as expected.
519  */
520 int on_key_esc(WINDOW *win)
521 {
522 	int key;
523 	int key2;
524 	int key3;
525 
526 	nodelay(win, TRUE);
527 	keypad(win, FALSE);
528 	key = wgetch(win);
529 	key2 = wgetch(win);
530 	do {
531 		key3 = wgetch(win);
532 	} while (key3 != ERR);
533 	nodelay(win, FALSE);
534 	keypad(win, TRUE);
535 	if (key == KEY_ESC && key2 == ERR)
536 		return KEY_ESC;
537 	else if (key != ERR && key != KEY_ESC && key2 == ERR)
538 		ungetch(key);
539 
540 	return -1;
541 }
542 
543 /* redraw screen in new size */
544 int on_key_resize(void)
545 {
546 	dialog_clear();
547 	return KEY_RESIZE;
548 }
549 
550 struct dialog_list *item_cur;
551 struct dialog_list item_nil;
552 struct dialog_list *item_head;
553 
554 void item_reset(void)
555 {
556 	struct dialog_list *p, *next;
557 
558 	for (p = item_head; p; p = next) {
559 		next = p->next;
560 		free(p);
561 	}
562 	item_head = NULL;
563 	item_cur = &item_nil;
564 }
565 
566 void item_make(const char *fmt, ...)
567 {
568 	va_list ap;
569 	struct dialog_list *p = malloc(sizeof(*p));
570 
571 	if (item_head)
572 		item_cur->next = p;
573 	else
574 		item_head = p;
575 	item_cur = p;
576 	memset(p, 0, sizeof(*p));
577 
578 	va_start(ap, fmt);
579 	vsnprintf(item_cur->node.str, sizeof(item_cur->node.str), fmt, ap);
580 	va_end(ap);
581 }
582 
583 void item_add_str(const char *fmt, ...)
584 {
585 	va_list ap;
586 	size_t avail;
587 
588 	avail = sizeof(item_cur->node.str) - strlen(item_cur->node.str);
589 
590 	va_start(ap, fmt);
591 	vsnprintf(item_cur->node.str + strlen(item_cur->node.str),
592 		  avail, fmt, ap);
593 	item_cur->node.str[sizeof(item_cur->node.str) - 1] = '\0';
594 	va_end(ap);
595 }
596 
597 void item_set_tag(char tag)
598 {
599 	item_cur->node.tag = tag;
600 }
601 void item_set_data(void *ptr)
602 {
603 	item_cur->node.data = ptr;
604 }
605 
606 void item_set_selected(int val)
607 {
608 	item_cur->node.selected = val;
609 }
610 
611 int item_activate_selected(void)
612 {
613 	item_foreach()
614 		if (item_is_selected())
615 			return 1;
616 	return 0;
617 }
618 
619 void *item_data(void)
620 {
621 	return item_cur->node.data;
622 }
623 
624 char item_tag(void)
625 {
626 	return item_cur->node.tag;
627 }
628 
629 int item_count(void)
630 {
631 	int n = 0;
632 	struct dialog_list *p;
633 
634 	for (p = item_head; p; p = p->next)
635 		n++;
636 	return n;
637 }
638 
639 void item_set(int n)
640 {
641 	int i = 0;
642 	item_foreach()
643 		if (i++ == n)
644 			return;
645 }
646 
647 int item_n(void)
648 {
649 	int n = 0;
650 	struct dialog_list *p;
651 
652 	for (p = item_head; p; p = p->next) {
653 		if (p == item_cur)
654 			return n;
655 		n++;
656 	}
657 	return 0;
658 }
659 
660 const char *item_str(void)
661 {
662 	return item_cur->node.str;
663 }
664 
665 int item_is_selected(void)
666 {
667 	return (item_cur->node.selected != 0);
668 }
669 
670 int item_is_tag(char tag)
671 {
672 	return (item_cur->node.tag == tag);
673 }
674