browser.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "../util.h"
  2. #include "../../perf.h"
  3. #include "libslang.h"
  4. #include <newt.h>
  5. #include "ui.h"
  6. #include <linux/compiler.h>
  7. #include <linux/list.h>
  8. #include <linux/rbtree.h>
  9. #include <stdlib.h>
  10. #include <sys/ttydefaults.h>
  11. #include "browser.h"
  12. #include "helpline.h"
  13. #include "../color.h"
  14. int newtGetKey(void);
  15. static int ui_browser__percent_color(double percent, bool current)
  16. {
  17. if (current)
  18. return HE_COLORSET_SELECTED;
  19. if (percent >= MIN_RED)
  20. return HE_COLORSET_TOP;
  21. if (percent >= MIN_GREEN)
  22. return HE_COLORSET_MEDIUM;
  23. return HE_COLORSET_NORMAL;
  24. }
  25. void ui_browser__set_color(struct ui_browser *self __used, int color)
  26. {
  27. SLsmg_set_color(color);
  28. }
  29. void ui_browser__set_percent_color(struct ui_browser *self,
  30. double percent, bool current)
  31. {
  32. int color = ui_browser__percent_color(percent, current);
  33. ui_browser__set_color(self, color);
  34. }
  35. void ui_browser__gotorc(struct ui_browser *self, int y, int x)
  36. {
  37. SLsmg_gotorc(self->y + y, self->x + x);
  38. }
  39. void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
  40. {
  41. struct list_head *head = self->entries;
  42. struct list_head *pos;
  43. switch (whence) {
  44. case SEEK_SET:
  45. pos = head->next;
  46. break;
  47. case SEEK_CUR:
  48. pos = self->top;
  49. break;
  50. case SEEK_END:
  51. pos = head->prev;
  52. break;
  53. default:
  54. return;
  55. }
  56. if (offset > 0) {
  57. while (offset-- != 0)
  58. pos = pos->next;
  59. } else {
  60. while (offset++ != 0)
  61. pos = pos->prev;
  62. }
  63. self->top = pos;
  64. }
  65. void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
  66. {
  67. struct rb_root *root = self->entries;
  68. struct rb_node *nd;
  69. switch (whence) {
  70. case SEEK_SET:
  71. nd = rb_first(root);
  72. break;
  73. case SEEK_CUR:
  74. nd = self->top;
  75. break;
  76. case SEEK_END:
  77. nd = rb_last(root);
  78. break;
  79. default:
  80. return;
  81. }
  82. if (offset > 0) {
  83. while (offset-- != 0)
  84. nd = rb_next(nd);
  85. } else {
  86. while (offset++ != 0)
  87. nd = rb_prev(nd);
  88. }
  89. self->top = nd;
  90. }
  91. unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
  92. {
  93. struct rb_node *nd;
  94. int row = 0;
  95. if (self->top == NULL)
  96. self->top = rb_first(self->entries);
  97. nd = self->top;
  98. while (nd != NULL) {
  99. ui_browser__gotorc(self, row, 0);
  100. self->write(self, nd, row);
  101. if (++row == self->height)
  102. break;
  103. nd = rb_next(nd);
  104. }
  105. return row;
  106. }
  107. bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
  108. {
  109. return self->top_idx + row == self->index;
  110. }
  111. void ui_browser__refresh_dimensions(struct ui_browser *self)
  112. {
  113. self->width = SLtt_Screen_Cols - 1;
  114. self->height = SLtt_Screen_Rows - 2;
  115. self->y = 1;
  116. self->x = 0;
  117. }
  118. void ui_browser__reset_index(struct ui_browser *self)
  119. {
  120. self->index = self->top_idx = 0;
  121. self->seek(self, 0, SEEK_SET);
  122. }
  123. void ui_browser__add_exit_key(struct ui_browser *browser __used, int key __used)
  124. {
  125. }
  126. void ui_browser__add_exit_keys(struct ui_browser *self, int keys[])
  127. {
  128. int i = 0;
  129. while (keys[i] && i < 64) {
  130. ui_browser__add_exit_key(self, keys[i]);
  131. ++i;
  132. }
  133. }
  134. void __ui_browser__show_title(struct ui_browser *browser, const char *title)
  135. {
  136. SLsmg_gotorc(0, 0);
  137. ui_browser__set_color(browser, NEWT_COLORSET_ROOT);
  138. slsmg_write_nstring(title, browser->width + 1);
  139. }
  140. void ui_browser__show_title(struct ui_browser *browser, const char *title)
  141. {
  142. pthread_mutex_lock(&ui__lock);
  143. __ui_browser__show_title(browser, title);
  144. pthread_mutex_unlock(&ui__lock);
  145. }
  146. int ui_browser__show(struct ui_browser *self, const char *title,
  147. const char *helpline, ...)
  148. {
  149. int err;
  150. va_list ap;
  151. int keys[] = { NEWT_KEY_UP, NEWT_KEY_DOWN, NEWT_KEY_PGUP,
  152. NEWT_KEY_PGDN, NEWT_KEY_HOME, NEWT_KEY_END, ' ',
  153. NEWT_KEY_LEFT, NEWT_KEY_ESCAPE, 'q', CTRL('c'), 0 };
  154. ui_browser__refresh_dimensions(self);
  155. pthread_mutex_lock(&ui__lock);
  156. __ui_browser__show_title(self, title);
  157. ui_browser__add_exit_keys(self, keys);
  158. self->title = title;
  159. free(self->helpline);
  160. self->helpline = NULL;
  161. va_start(ap, helpline);
  162. err = vasprintf(&self->helpline, helpline, ap);
  163. va_end(ap);
  164. if (err > 0)
  165. ui_helpline__push(self->helpline);
  166. pthread_mutex_unlock(&ui__lock);
  167. return err ? 0 : -1;
  168. }
  169. void ui_browser__hide(struct ui_browser *browser __used)
  170. {
  171. pthread_mutex_lock(&ui__lock);
  172. ui_helpline__pop();
  173. pthread_mutex_unlock(&ui__lock);
  174. }
  175. static void ui_browser__scrollbar_set(struct ui_browser *browser)
  176. {
  177. int height = browser->height, h = 0, pct = 0,
  178. col = browser->width,
  179. row = browser->y - 1;
  180. if (browser->nr_entries > 1) {
  181. pct = ((browser->index * (browser->height - 1)) /
  182. (browser->nr_entries - 1));
  183. }
  184. while (h < height) {
  185. ui_browser__gotorc(browser, row++, col);
  186. SLsmg_set_char_set(1);
  187. SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_BOARD_CHAR);
  188. SLsmg_set_char_set(0);
  189. ++h;
  190. }
  191. }
  192. static int __ui_browser__refresh(struct ui_browser *browser)
  193. {
  194. int row;
  195. row = browser->refresh(browser);
  196. ui_browser__set_color(browser, HE_COLORSET_NORMAL);
  197. SLsmg_fill_region(browser->y + row, browser->x,
  198. browser->height - row, browser->width, ' ');
  199. ui_browser__scrollbar_set(browser);
  200. return 0;
  201. }
  202. int ui_browser__refresh(struct ui_browser *browser)
  203. {
  204. pthread_mutex_lock(&ui__lock);
  205. __ui_browser__refresh(browser);
  206. pthread_mutex_unlock(&ui__lock);
  207. return 0;
  208. }
  209. /*
  210. * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
  211. * forget about any reference to any entry in the underlying data structure,
  212. * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
  213. * after an output_resort and hist decay.
  214. */
  215. void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
  216. {
  217. off_t offset = nr_entries - browser->nr_entries;
  218. browser->nr_entries = nr_entries;
  219. if (offset < 0) {
  220. if (browser->top_idx < (u64)-offset)
  221. offset = -browser->top_idx;
  222. browser->index += offset;
  223. browser->top_idx += offset;
  224. }
  225. browser->seek(browser, browser->top_idx, SEEK_SET);
  226. }
  227. int ui_browser__run(struct ui_browser *self, int delay_secs)
  228. {
  229. int err, key;
  230. struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
  231. pthread__unblock_sigwinch();
  232. while (1) {
  233. off_t offset;
  234. fd_set read_set;
  235. pthread_mutex_lock(&ui__lock);
  236. err = __ui_browser__refresh(self);
  237. SLsmg_refresh();
  238. pthread_mutex_unlock(&ui__lock);
  239. if (err < 0)
  240. break;
  241. FD_ZERO(&read_set);
  242. FD_SET(0, &read_set);
  243. if (delay_secs) {
  244. timeout.tv_sec = delay_secs;
  245. timeout.tv_usec = 0;
  246. }
  247. err = select(1, &read_set, NULL, NULL, ptimeout);
  248. if (err > 0 && FD_ISSET(0, &read_set))
  249. key = newtGetKey();
  250. else if (err == 0)
  251. break;
  252. else {
  253. pthread_mutex_lock(&ui__lock);
  254. SLtt_get_screen_size();
  255. SLsmg_reinit_smg();
  256. pthread_mutex_unlock(&ui__lock);
  257. ui_browser__refresh_dimensions(self);
  258. __ui_browser__show_title(self, self->title);
  259. ui_helpline__puts(self->helpline);
  260. continue;
  261. }
  262. switch (key) {
  263. case NEWT_KEY_DOWN:
  264. if (self->index == self->nr_entries - 1)
  265. break;
  266. ++self->index;
  267. if (self->index == self->top_idx + self->height) {
  268. ++self->top_idx;
  269. self->seek(self, +1, SEEK_CUR);
  270. }
  271. break;
  272. case NEWT_KEY_UP:
  273. if (self->index == 0)
  274. break;
  275. --self->index;
  276. if (self->index < self->top_idx) {
  277. --self->top_idx;
  278. self->seek(self, -1, SEEK_CUR);
  279. }
  280. break;
  281. case NEWT_KEY_PGDN:
  282. case ' ':
  283. if (self->top_idx + self->height > self->nr_entries - 1)
  284. break;
  285. offset = self->height;
  286. if (self->index + offset > self->nr_entries - 1)
  287. offset = self->nr_entries - 1 - self->index;
  288. self->index += offset;
  289. self->top_idx += offset;
  290. self->seek(self, +offset, SEEK_CUR);
  291. break;
  292. case NEWT_KEY_PGUP:
  293. if (self->top_idx == 0)
  294. break;
  295. if (self->top_idx < self->height)
  296. offset = self->top_idx;
  297. else
  298. offset = self->height;
  299. self->index -= offset;
  300. self->top_idx -= offset;
  301. self->seek(self, -offset, SEEK_CUR);
  302. break;
  303. case NEWT_KEY_HOME:
  304. ui_browser__reset_index(self);
  305. break;
  306. case NEWT_KEY_END:
  307. offset = self->height - 1;
  308. if (offset >= self->nr_entries)
  309. offset = self->nr_entries - 1;
  310. self->index = self->nr_entries - 1;
  311. self->top_idx = self->index - offset;
  312. self->seek(self, -offset, SEEK_END);
  313. break;
  314. default:
  315. return key;
  316. }
  317. }
  318. return -1;
  319. }
  320. unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
  321. {
  322. struct list_head *pos;
  323. struct list_head *head = self->entries;
  324. int row = 0;
  325. if (self->top == NULL || self->top == self->entries)
  326. self->top = head->next;
  327. pos = self->top;
  328. list_for_each_from(pos, head) {
  329. ui_browser__gotorc(self, row, 0);
  330. self->write(self, pos, row);
  331. if (++row == self->height)
  332. break;
  333. }
  334. return row;
  335. }
  336. static struct ui_browser__colors {
  337. const char *topColorFg, *topColorBg;
  338. const char *mediumColorFg, *mediumColorBg;
  339. const char *normalColorFg, *normalColorBg;
  340. const char *selColorFg, *selColorBg;
  341. const char *codeColorFg, *codeColorBg;
  342. } ui_browser__default_colors = {
  343. "red", "lightgray",
  344. "green", "lightgray",
  345. "black", "lightgray",
  346. "lightgray", "magenta",
  347. "blue", "lightgray",
  348. };
  349. void ui_browser__init(void)
  350. {
  351. struct ui_browser__colors *c = &ui_browser__default_colors;
  352. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  353. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  354. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  355. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  356. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  357. }