1 // SPDX-License-Identifier: GPL-2.0
6 #include <sys/ttydefaults.h>
8 #include "../browser.h"
9 #include "../keysyms.h"
10 #include "../helpline.h"
13 #include "../libslang.h"
15 static void ui_browser__argv_write(struct ui_browser *browser,
19 bool current_entry = ui_browser__is_current_entry(browser, row);
21 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
23 ui_browser__write_nstring(browser, *arg, browser->width);
26 static int popup_menu__run(struct ui_browser *menu, int *keyp)
30 if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
34 key = ui_browser__run(menu, 0);
50 key = menu->nr_entries;
59 ui_browser__hide(menu);
63 int ui__popup_menu(int argc, char * const argv[], int *keyp)
65 struct ui_browser menu = {
66 .entries = (void *)argv,
67 .refresh = ui_browser__argv_refresh,
68 .seek = ui_browser__argv_seek,
69 .write = ui_browser__argv_write,
72 return popup_menu__run(&menu, keyp);
75 int ui_browser__input_window(const char *title, const char *text, char *input,
76 const char *exit_msg, int delay_secs)
79 int max_len = 60, nr_lines = 0;
85 const char *sep = strchr(t, '\n');
88 sep = strchr(t, '\0');
98 pthread_mutex_lock(&ui__lock);
102 y = SLtt_Screen_Rows / 2 - nr_lines / 2;
103 x = SLtt_Screen_Cols / 2 - max_len / 2;
106 SLsmg_draw_box(y, x++, nr_lines, max_len);
108 SLsmg_gotorc(y, x + 1);
109 SLsmg_write_string((char *)title);
111 SLsmg_gotorc(++y, x);
114 SLsmg_write_wrapped_string((unsigned char *)text, y, x,
115 nr_lines, max_len, 1);
119 SLsmg_gotorc(y + len - 1, x);
120 SLsmg_write_nstring((char *)" ", max_len);
122 SLsmg_draw_box(y++, x + 1, 3, max_len - 2);
124 SLsmg_gotorc(y + 3, x);
125 SLsmg_write_nstring((char *)exit_msg, max_len);
128 pthread_mutex_unlock(&ui__lock);
132 key = ui__getch(delay_secs);
133 while (key != K_TIMER && key != K_ENTER && key != K_ESC) {
134 pthread_mutex_lock(&ui__lock);
136 if (key == K_BKSPC) {
138 pthread_mutex_unlock(&ui__lock);
141 SLsmg_gotorc(y, x + --len);
142 SLsmg_write_char(' ');
145 SLsmg_gotorc(y, x + len++);
146 SLsmg_write_char(key);
150 pthread_mutex_unlock(&ui__lock);
152 /* XXX more graceful overflow handling needed */
153 if (len == sizeof(buf) - 1) {
154 ui_helpline__push("maximum size of symbol name reached!");
159 key = ui__getch(delay_secs);
163 strncpy(input, buf, len+1);
167 void __ui__info_window(const char *title, const char *text, const char *exit_msg)
170 int max_len = 0, nr_lines = 0;
175 const char *sep = strchr(t, '\n');
179 sep = strchr(t, '\0');
193 y = SLtt_Screen_Rows / 2 - nr_lines / 2,
194 x = SLtt_Screen_Cols / 2 - max_len / 2;
197 SLsmg_draw_box(y, x++, nr_lines, max_len);
199 SLsmg_gotorc(y, x + 1);
200 SLsmg_write_string((char *)title);
202 SLsmg_gotorc(++y, x);
206 SLsmg_write_wrapped_string((unsigned char *)text, y, x,
207 nr_lines, max_len, 1);
209 SLsmg_gotorc(y + nr_lines - 2, x);
210 SLsmg_write_nstring((char *)" ", max_len);
211 SLsmg_gotorc(y + nr_lines - 1, x);
212 SLsmg_write_nstring((char *)exit_msg, max_len);
216 void ui__info_window(const char *title, const char *text)
218 pthread_mutex_lock(&ui__lock);
219 __ui__info_window(title, text, NULL);
221 pthread_mutex_unlock(&ui__lock);
224 int ui__question_window(const char *title, const char *text,
225 const char *exit_msg, int delay_secs)
227 pthread_mutex_lock(&ui__lock);
228 __ui__info_window(title, text, exit_msg);
230 pthread_mutex_unlock(&ui__lock);
231 return ui__getch(delay_secs);
234 int ui__help_window(const char *text)
236 return ui__question_window("Help", text, "Press any key...", 0);
239 int ui__dialog_yesno(const char *msg)
241 return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
244 static int __ui__warning(const char *title, const char *format, va_list args)
248 if (vasprintf(&s, format, args) > 0) {
251 key = ui__question_window(title, s, "Press any key...", 0);
256 fprintf(stderr, "%s\n", title);
257 vfprintf(stderr, format, args);
261 static int perf_tui__error(const char *format, va_list args)
263 return __ui__warning("Error:", format, args);
266 static int perf_tui__warning(const char *format, va_list args)
268 return __ui__warning("Warning:", format, args);
271 struct perf_error_ops perf_tui_eops = {
272 .error = perf_tui__error,
273 .warning = perf_tui__warning,