PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/perf/util/ui/util.c

https://bitbucket.org/Don2x/mod-kernel-m7-sources
C | 250 lines | 214 code | 35 blank | 1 comment | 33 complexity | 408e135b0d82758f2f5f2a90a10f8907 MD5 | raw file
  1. #include "../util.h"
  2. #include <signal.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <sys/ttydefaults.h>
  6. #include "../cache.h"
  7. #include "../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. slsmg_write_nstring(*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. max_len += 2;
  81. nr_lines += 8;
  82. y = SLtt_Screen_Rows / 2 - nr_lines / 2;
  83. x = SLtt_Screen_Cols / 2 - max_len / 2;
  84. SLsmg_set_color(0);
  85. SLsmg_draw_box(y, x++, nr_lines, max_len);
  86. if (title) {
  87. SLsmg_gotorc(y, x + 1);
  88. SLsmg_write_string((char *)title);
  89. }
  90. SLsmg_gotorc(++y, x);
  91. nr_lines -= 7;
  92. max_len -= 2;
  93. SLsmg_write_wrapped_string((unsigned char *)text, y, x,
  94. nr_lines, max_len, 1);
  95. y += nr_lines;
  96. len = 5;
  97. while (len--) {
  98. SLsmg_gotorc(y + len - 1, x);
  99. SLsmg_write_nstring((char *)" ", max_len);
  100. }
  101. SLsmg_draw_box(y++, x + 1, 3, max_len - 2);
  102. SLsmg_gotorc(y + 3, x);
  103. SLsmg_write_nstring((char *)exit_msg, max_len);
  104. SLsmg_refresh();
  105. x += 2;
  106. len = 0;
  107. key = ui__getch(delay_secs);
  108. while (key != K_TIMER && key != K_ENTER && key != K_ESC) {
  109. if (key == K_BKSPC) {
  110. if (len == 0)
  111. goto next_key;
  112. SLsmg_gotorc(y, x + --len);
  113. SLsmg_write_char(' ');
  114. } else {
  115. buf[len] = key;
  116. SLsmg_gotorc(y, x + len++);
  117. SLsmg_write_char(key);
  118. }
  119. SLsmg_refresh();
  120. /* XXX more graceful overflow handling needed */
  121. if (len == sizeof(buf) - 1) {
  122. ui_helpline__push("maximum size of symbol name reached!");
  123. key = K_ENTER;
  124. break;
  125. }
  126. next_key:
  127. key = ui__getch(delay_secs);
  128. }
  129. buf[len] = '\0';
  130. strncpy(input, buf, len+1);
  131. return key;
  132. }
  133. int ui__question_window(const char *title, const char *text,
  134. const char *exit_msg, int delay_secs)
  135. {
  136. int x, y;
  137. int max_len = 0, nr_lines = 0;
  138. const char *t;
  139. t = text;
  140. while (1) {
  141. const char *sep = strchr(t, '\n');
  142. int len;
  143. if (sep == NULL)
  144. sep = strchr(t, '\0');
  145. len = sep - t;
  146. if (max_len < len)
  147. max_len = len;
  148. ++nr_lines;
  149. if (*sep == '\0')
  150. break;
  151. t = sep + 1;
  152. }
  153. max_len += 2;
  154. nr_lines += 4;
  155. y = SLtt_Screen_Rows / 2 - nr_lines / 2,
  156. x = SLtt_Screen_Cols / 2 - max_len / 2;
  157. SLsmg_set_color(0);
  158. SLsmg_draw_box(y, x++, nr_lines, max_len);
  159. if (title) {
  160. SLsmg_gotorc(y, x + 1);
  161. SLsmg_write_string((char *)title);
  162. }
  163. SLsmg_gotorc(++y, x);
  164. nr_lines -= 2;
  165. max_len -= 2;
  166. SLsmg_write_wrapped_string((unsigned char *)text, y, x,
  167. nr_lines, max_len, 1);
  168. SLsmg_gotorc(y + nr_lines - 2, x);
  169. SLsmg_write_nstring((char *)" ", max_len);
  170. SLsmg_gotorc(y + nr_lines - 1, x);
  171. SLsmg_write_nstring((char *)exit_msg, max_len);
  172. SLsmg_refresh();
  173. return ui__getch(delay_secs);
  174. }
  175. int ui__help_window(const char *text)
  176. {
  177. return ui__question_window("Help", text, "Press any key...", 0);
  178. }
  179. int ui__dialog_yesno(const char *msg)
  180. {
  181. return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
  182. }
  183. int __ui__warning(const char *title, const char *format, va_list args)
  184. {
  185. char *s;
  186. if (use_browser > 0 && vasprintf(&s, format, args) > 0) {
  187. int key;
  188. pthread_mutex_lock(&ui__lock);
  189. key = ui__question_window(title, s, "Press any key...", 0);
  190. pthread_mutex_unlock(&ui__lock);
  191. free(s);
  192. return key;
  193. }
  194. fprintf(stderr, "%s:\n", title);
  195. vfprintf(stderr, format, args);
  196. return K_ESC;
  197. }
  198. int ui__warning(const char *format, ...)
  199. {
  200. int key;
  201. va_list args;
  202. va_start(args, format);
  203. key = __ui__warning("Warning", format, args);
  204. va_end(args);
  205. return key;
  206. }
  207. int ui__error(const char *format, ...)
  208. {
  209. int key;
  210. va_list args;
  211. va_start(args, format);
  212. key = __ui__warning("Error", format, args);
  213. va_end(args);
  214. return key;
  215. }