browser.c 9.7 KB

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