util.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <signal.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/ttydefaults.h>
  7. #include "../browser.h"
  8. #include "../keysyms.h"
  9. #include "../helpline.h"
  10. #include "../ui.h"
  11. #include "../util.h"
  12. #include "../libslang.h"
  13. static void ui_browser__argv_write(struct ui_browser *browser,
  14. void *entry, int row)
  15. {
  16. char **arg = entry;
  17. bool current_entry = ui_browser__is_current_entry(browser, row);
  18. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  19. HE_COLORSET_NORMAL);
  20. ui_browser__write_nstring(browser, *arg, browser->width);
  21. }
  22. static int popup_menu__run(struct ui_browser *menu, int *keyp)
  23. {
  24. int key;
  25. if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
  26. return -1;
  27. while (1) {
  28. key = ui_browser__run(menu, 0);
  29. switch (key) {
  30. case K_RIGHT:
  31. case K_ENTER:
  32. key = menu->index;
  33. break;
  34. case K_LEFT:
  35. case K_ESC:
  36. case 'q':
  37. case CTRL('c'):
  38. key = -1;
  39. break;
  40. default:
  41. if (keyp) {
  42. *keyp = key;
  43. key = menu->nr_entries;
  44. break;
  45. }
  46. continue;
  47. }
  48. break;
  49. }
  50. ui_browser__hide(menu);
  51. return key;
  52. }
  53. int ui__popup_menu(int argc, char * const argv[], int *keyp)
  54. {
  55. struct ui_browser menu = {
  56. .entries = (void *)argv,
  57. .refresh = ui_browser__argv_refresh,
  58. .seek = ui_browser__argv_seek,
  59. .write = ui_browser__argv_write,
  60. .nr_entries = argc,
  61. };
  62. return popup_menu__run(&menu, keyp);
  63. }
  64. int ui_browser__input_window(const char *title, const char *text, char *input,
  65. const char *exit_msg, int delay_secs)
  66. {
  67. int x, y, len, key;
  68. int max_len = 60, nr_lines = 0;
  69. static char buf[50];
  70. const char *t;
  71. t = text;
  72. while (1) {
  73. const char *sep = strchr(t, '\n');
  74. if (sep == NULL)
  75. sep = strchr(t, '\0');
  76. len = sep - t;
  77. if (max_len < len)
  78. max_len = len;
  79. ++nr_lines;
  80. if (*sep == '\0')
  81. break;
  82. t = sep + 1;
  83. }
  84. mutex_lock(&ui__lock);
  85. max_len += 2;
  86. nr_lines += 8;
  87. y = SLtt_Screen_Rows / 2 - nr_lines / 2;
  88. x = SLtt_Screen_Cols / 2 - max_len / 2;
  89. SLsmg_set_color(0);
  90. SLsmg_draw_box(y, x++, nr_lines, max_len);
  91. if (title) {
  92. SLsmg_gotorc(y, x + 1);
  93. SLsmg_write_string(title);
  94. }
  95. SLsmg_gotorc(++y, x);
  96. nr_lines -= 7;
  97. max_len -= 2;
  98. SLsmg_write_wrapped_string((unsigned char *)text, y, x,
  99. nr_lines, max_len, 1);
  100. y += nr_lines;
  101. len = 5;
  102. while (len--) {
  103. SLsmg_gotorc(y + len - 1, x);
  104. SLsmg_write_nstring(" ", max_len);
  105. }
  106. SLsmg_draw_box(y++, x + 1, 3, max_len - 2);
  107. SLsmg_gotorc(y + 3, x);
  108. SLsmg_write_nstring(exit_msg, max_len);
  109. SLsmg_refresh();
  110. mutex_unlock(&ui__lock);
  111. x += 2;
  112. len = 0;
  113. key = ui__getch(delay_secs);
  114. while (key != K_TIMER && key != K_ENTER && key != K_ESC) {
  115. mutex_lock(&ui__lock);
  116. if (key == K_BKSPC) {
  117. if (len == 0) {
  118. mutex_unlock(&ui__lock);
  119. goto next_key;
  120. }
  121. SLsmg_gotorc(y, x + --len);
  122. SLsmg_write_char(' ');
  123. } else {
  124. buf[len] = key;
  125. SLsmg_gotorc(y, x + len++);
  126. SLsmg_write_char(key);
  127. }
  128. SLsmg_refresh();
  129. mutex_unlock(&ui__lock);
  130. /* XXX more graceful overflow handling needed */
  131. if (len == sizeof(buf) - 1) {
  132. ui_helpline__push("maximum size of symbol name reached!");
  133. key = K_ENTER;
  134. break;
  135. }
  136. next_key:
  137. key = ui__getch(delay_secs);
  138. }
  139. buf[len] = '\0';
  140. strncpy(input, buf, len+1);
  141. return key;
  142. }
  143. void __ui__info_window(const char *title, const char *text, const char *exit_msg)
  144. {
  145. int x, y;
  146. int max_len = 0, nr_lines = 0;
  147. const char *t;
  148. t = text;
  149. while (1) {
  150. const char *sep = strchr(t, '\n');
  151. int len;
  152. if (sep == NULL)
  153. sep = strchr(t, '\0');
  154. len = sep - t;
  155. if (max_len < len)
  156. max_len = len;
  157. ++nr_lines;
  158. if (*sep == '\0')
  159. break;
  160. t = sep + 1;
  161. }
  162. max_len += 2;
  163. nr_lines += 2;
  164. if (exit_msg)
  165. nr_lines += 2;
  166. y = SLtt_Screen_Rows / 2 - nr_lines / 2,
  167. x = SLtt_Screen_Cols / 2 - max_len / 2;
  168. SLsmg_set_color(0);
  169. SLsmg_draw_box(y, x++, nr_lines, max_len);
  170. if (title) {
  171. SLsmg_gotorc(y, x + 1);
  172. SLsmg_write_string(title);
  173. }
  174. SLsmg_gotorc(++y, x);
  175. if (exit_msg)
  176. nr_lines -= 2;
  177. max_len -= 2;
  178. SLsmg_write_wrapped_string((unsigned char *)text, y, x,
  179. nr_lines, max_len, 1);
  180. if (exit_msg) {
  181. SLsmg_gotorc(y + nr_lines - 2, x);
  182. SLsmg_write_nstring(" ", max_len);
  183. SLsmg_gotorc(y + nr_lines - 1, x);
  184. SLsmg_write_nstring(exit_msg, max_len);
  185. }
  186. }
  187. void ui__info_window(const char *title, const char *text)
  188. {
  189. mutex_lock(&ui__lock);
  190. __ui__info_window(title, text, NULL);
  191. SLsmg_refresh();
  192. mutex_unlock(&ui__lock);
  193. }
  194. int ui__question_window(const char *title, const char *text,
  195. const char *exit_msg, int delay_secs)
  196. {
  197. mutex_lock(&ui__lock);
  198. __ui__info_window(title, text, exit_msg);
  199. SLsmg_refresh();
  200. mutex_unlock(&ui__lock);
  201. return ui__getch(delay_secs);
  202. }
  203. int ui__help_window(const char *text)
  204. {
  205. return ui__question_window("Help", text, "Press any key...", 0);
  206. }
  207. int ui__dialog_yesno(const char *msg)
  208. {
  209. return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
  210. }
  211. static int __ui__warning(const char *title, const char *format, va_list args)
  212. {
  213. char *s;
  214. if (vasprintf(&s, format, args) > 0) {
  215. int key;
  216. key = ui__question_window(title, s, "Press any key...", 0);
  217. free(s);
  218. return key;
  219. }
  220. fprintf(stderr, "%s\n", title);
  221. vfprintf(stderr, format, args);
  222. return K_ESC;
  223. }
  224. static int perf_tui__error(const char *format, va_list args)
  225. {
  226. return __ui__warning("Error:", format, args);
  227. }
  228. static int perf_tui__warning(const char *format, va_list args)
  229. {
  230. return __ui__warning("Warning:", format, args);
  231. }
  232. struct perf_error_ops perf_tui_eops = {
  233. .error = perf_tui__error,
  234. .warning = perf_tui__warning,
  235. };