1 // SPDX-License-Identifier: GPL-2.0
2 #include "../../util/util.h"
6 #include <sys/ttydefaults.h>
8 #include "../../util/cache.h"
9 #include "../../util/debug.h"
10 #include "../browser.h"
11 #include "../keysyms.h"
12 #include "../helpline.h"
15 #include "../libslang.h"
17 static void ui_browser__argv_write(struct ui_browser *browser,
21 bool current_entry = ui_browser__is_current_entry(browser, row);
23 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
25 ui_browser__write_nstring(browser, *arg, browser->width);
28 static int popup_menu__run(struct ui_browser *menu)
32 if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
36 key = ui_browser__run(menu, 0);
56 ui_browser__hide(menu);
60 int ui__popup_menu(int argc, char * const argv[])
62 struct ui_browser menu = {
63 .entries = (void *)argv,
64 .refresh = ui_browser__argv_refresh,
65 .seek = ui_browser__argv_seek,
66 .write = ui_browser__argv_write,
70 return popup_menu__run(&menu);
73 int ui_browser__input_window(const char *title, const char *text, char *input,
74 const char *exit_msg, int delay_secs)
77 int max_len = 60, nr_lines = 0;
83 const char *sep = strchr(t, '\n');
86 sep = strchr(t, '\0');
96 pthread_mutex_lock(&ui__lock);
100 y = SLtt_Screen_Rows / 2 - nr_lines / 2;
101 x = SLtt_Screen_Cols / 2 - max_len / 2;
104 SLsmg_draw_box(y, x++, nr_lines, max_len);
106 SLsmg_gotorc(y, x + 1);
107 SLsmg_write_string((char *)title);
109 SLsmg_gotorc(++y, x);
112 SLsmg_write_wrapped_string((unsigned char *)text, y, x,
113 nr_lines, max_len, 1);
117 SLsmg_gotorc(y + len - 1, x);
118 SLsmg_write_nstring((char *)" ", max_len);
120 SLsmg_draw_box(y++, x + 1, 3, max_len - 2);
122 SLsmg_gotorc(y + 3, x);
123 SLsmg_write_nstring((char *)exit_msg, max_len);
126 pthread_mutex_unlock(&ui__lock);
130 key = ui__getch(delay_secs);
131 while (key != K_TIMER && key != K_ENTER && key != K_ESC) {
132 pthread_mutex_lock(&ui__lock);
134 if (key == K_BKSPC) {
136 pthread_mutex_unlock(&ui__lock);
139 SLsmg_gotorc(y, x + --len);
140 SLsmg_write_char(' ');
143 SLsmg_gotorc(y, x + len++);
144 SLsmg_write_char(key);
148 pthread_mutex_unlock(&ui__lock);
150 /* XXX more graceful overflow handling needed */
151 if (len == sizeof(buf) - 1) {
152 ui_helpline__push("maximum size of symbol name reached!");
157 key = ui__getch(delay_secs);
161 strncpy(input, buf, len+1);
165 int ui__question_window(const char *title, const char *text,
166 const char *exit_msg, int delay_secs)
169 int max_len = 0, nr_lines = 0;
174 const char *sep = strchr(t, '\n');
178 sep = strchr(t, '\0');
188 pthread_mutex_lock(&ui__lock);
192 y = SLtt_Screen_Rows / 2 - nr_lines / 2,
193 x = SLtt_Screen_Cols / 2 - max_len / 2;
196 SLsmg_draw_box(y, x++, nr_lines, max_len);
198 SLsmg_gotorc(y, x + 1);
199 SLsmg_write_string((char *)title);
201 SLsmg_gotorc(++y, x);
204 SLsmg_write_wrapped_string((unsigned char *)text, y, x,
205 nr_lines, max_len, 1);
206 SLsmg_gotorc(y + nr_lines - 2, x);
207 SLsmg_write_nstring((char *)" ", max_len);
208 SLsmg_gotorc(y + nr_lines - 1, x);
209 SLsmg_write_nstring((char *)exit_msg, max_len);
212 pthread_mutex_unlock(&ui__lock);
214 return ui__getch(delay_secs);
217 int ui__help_window(const char *text)
219 return ui__question_window("Help", text, "Press any key...", 0);
222 int ui__dialog_yesno(const char *msg)
224 return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
227 static int __ui__warning(const char *title, const char *format, va_list args)
231 if (vasprintf(&s, format, args) > 0) {
234 key = ui__question_window(title, s, "Press any key...", 0);
239 fprintf(stderr, "%s\n", title);
240 vfprintf(stderr, format, args);
244 static int perf_tui__error(const char *format, va_list args)
246 return __ui__warning("Error:", format, args);
249 static int perf_tui__warning(const char *format, va_list args)
251 return __ui__warning("Warning:", format, args);
254 struct perf_error_ops perf_tui_eops = {
255 .error = perf_tui__error,
256 .warning = perf_tui__warning,