util.c 5.2 KB

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