util.c 5.2 KB

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