browser.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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__show_title(struct ui_browser *browser, const char *title)
  124. {
  125. SLsmg_gotorc(0, 0);
  126. ui_browser__set_color(browser, NEWT_COLORSET_ROOT);
  127. slsmg_write_nstring(title, browser->width + 1);
  128. }
  129. void ui_browser__show_title(struct ui_browser *browser, const char *title)
  130. {
  131. pthread_mutex_lock(&ui__lock);
  132. __ui_browser__show_title(browser, title);
  133. pthread_mutex_unlock(&ui__lock);
  134. }
  135. int ui_browser__show(struct ui_browser *self, const char *title,
  136. const char *helpline, ...)
  137. {
  138. int err;
  139. va_list ap;
  140. ui_browser__refresh_dimensions(self);
  141. pthread_mutex_lock(&ui__lock);
  142. __ui_browser__show_title(self, title);
  143. self->title = title;
  144. free(self->helpline);
  145. self->helpline = NULL;
  146. va_start(ap, helpline);
  147. err = vasprintf(&self->helpline, helpline, ap);
  148. va_end(ap);
  149. if (err > 0)
  150. ui_helpline__push(self->helpline);
  151. pthread_mutex_unlock(&ui__lock);
  152. return err ? 0 : -1;
  153. }
  154. void ui_browser__hide(struct ui_browser *browser __used)
  155. {
  156. pthread_mutex_lock(&ui__lock);
  157. ui_helpline__pop();
  158. pthread_mutex_unlock(&ui__lock);
  159. }
  160. static void ui_browser__scrollbar_set(struct ui_browser *browser)
  161. {
  162. int height = browser->height, h = 0, pct = 0,
  163. col = browser->width,
  164. row = browser->y - 1;
  165. if (browser->nr_entries > 1) {
  166. pct = ((browser->index * (browser->height - 1)) /
  167. (browser->nr_entries - 1));
  168. }
  169. while (h < height) {
  170. ui_browser__gotorc(browser, row++, col);
  171. SLsmg_set_char_set(1);
  172. SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_BOARD_CHAR);
  173. SLsmg_set_char_set(0);
  174. ++h;
  175. }
  176. }
  177. static int __ui_browser__refresh(struct ui_browser *browser)
  178. {
  179. int row;
  180. row = browser->refresh(browser);
  181. ui_browser__set_color(browser, HE_COLORSET_NORMAL);
  182. SLsmg_fill_region(browser->y + row, browser->x,
  183. browser->height - row, browser->width, ' ');
  184. ui_browser__scrollbar_set(browser);
  185. return 0;
  186. }
  187. int ui_browser__refresh(struct ui_browser *browser)
  188. {
  189. pthread_mutex_lock(&ui__lock);
  190. __ui_browser__refresh(browser);
  191. pthread_mutex_unlock(&ui__lock);
  192. return 0;
  193. }
  194. /*
  195. * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
  196. * forget about any reference to any entry in the underlying data structure,
  197. * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
  198. * after an output_resort and hist decay.
  199. */
  200. void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
  201. {
  202. off_t offset = nr_entries - browser->nr_entries;
  203. browser->nr_entries = nr_entries;
  204. if (offset < 0) {
  205. if (browser->top_idx < (u64)-offset)
  206. offset = -browser->top_idx;
  207. browser->index += offset;
  208. browser->top_idx += offset;
  209. }
  210. browser->seek(browser, browser->top_idx, SEEK_SET);
  211. }
  212. int ui_browser__run(struct ui_browser *self, int delay_secs)
  213. {
  214. int err, key;
  215. struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
  216. pthread__unblock_sigwinch();
  217. while (1) {
  218. off_t offset;
  219. fd_set read_set;
  220. pthread_mutex_lock(&ui__lock);
  221. err = __ui_browser__refresh(self);
  222. SLsmg_refresh();
  223. pthread_mutex_unlock(&ui__lock);
  224. if (err < 0)
  225. break;
  226. FD_ZERO(&read_set);
  227. FD_SET(0, &read_set);
  228. if (delay_secs) {
  229. timeout.tv_sec = delay_secs;
  230. timeout.tv_usec = 0;
  231. }
  232. err = select(1, &read_set, NULL, NULL, ptimeout);
  233. if (err > 0 && FD_ISSET(0, &read_set))
  234. key = newtGetKey();
  235. else if (err == 0)
  236. break;
  237. else {
  238. pthread_mutex_lock(&ui__lock);
  239. SLtt_get_screen_size();
  240. SLsmg_reinit_smg();
  241. pthread_mutex_unlock(&ui__lock);
  242. ui_browser__refresh_dimensions(self);
  243. __ui_browser__show_title(self, self->title);
  244. ui_helpline__puts(self->helpline);
  245. continue;
  246. }
  247. switch (key) {
  248. case NEWT_KEY_DOWN:
  249. if (self->index == self->nr_entries - 1)
  250. break;
  251. ++self->index;
  252. if (self->index == self->top_idx + self->height) {
  253. ++self->top_idx;
  254. self->seek(self, +1, SEEK_CUR);
  255. }
  256. break;
  257. case NEWT_KEY_UP:
  258. if (self->index == 0)
  259. break;
  260. --self->index;
  261. if (self->index < self->top_idx) {
  262. --self->top_idx;
  263. self->seek(self, -1, SEEK_CUR);
  264. }
  265. break;
  266. case NEWT_KEY_PGDN:
  267. case ' ':
  268. if (self->top_idx + self->height > self->nr_entries - 1)
  269. break;
  270. offset = self->height;
  271. if (self->index + offset > self->nr_entries - 1)
  272. offset = self->nr_entries - 1 - self->index;
  273. self->index += offset;
  274. self->top_idx += offset;
  275. self->seek(self, +offset, SEEK_CUR);
  276. break;
  277. case NEWT_KEY_PGUP:
  278. if (self->top_idx == 0)
  279. break;
  280. if (self->top_idx < self->height)
  281. offset = self->top_idx;
  282. else
  283. offset = self->height;
  284. self->index -= offset;
  285. self->top_idx -= offset;
  286. self->seek(self, -offset, SEEK_CUR);
  287. break;
  288. case NEWT_KEY_HOME:
  289. ui_browser__reset_index(self);
  290. break;
  291. case NEWT_KEY_END:
  292. offset = self->height - 1;
  293. if (offset >= self->nr_entries)
  294. offset = self->nr_entries - 1;
  295. self->index = self->nr_entries - 1;
  296. self->top_idx = self->index - offset;
  297. self->seek(self, -offset, SEEK_END);
  298. break;
  299. default:
  300. return key;
  301. }
  302. }
  303. return -1;
  304. }
  305. unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
  306. {
  307. struct list_head *pos;
  308. struct list_head *head = self->entries;
  309. int row = 0;
  310. if (self->top == NULL || self->top == self->entries)
  311. self->top = head->next;
  312. pos = self->top;
  313. list_for_each_from(pos, head) {
  314. ui_browser__gotorc(self, row, 0);
  315. self->write(self, pos, row);
  316. if (++row == self->height)
  317. break;
  318. }
  319. return row;
  320. }
  321. static struct ui_browser__colors {
  322. const char *topColorFg, *topColorBg;
  323. const char *mediumColorFg, *mediumColorBg;
  324. const char *normalColorFg, *normalColorBg;
  325. const char *selColorFg, *selColorBg;
  326. const char *codeColorFg, *codeColorBg;
  327. } ui_browser__default_colors = {
  328. "red", "lightgray",
  329. "green", "lightgray",
  330. "black", "lightgray",
  331. "lightgray", "magenta",
  332. "blue", "lightgray",
  333. };
  334. void ui_browser__init(void)
  335. {
  336. struct ui_browser__colors *c = &ui_browser__default_colors;
  337. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  338. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  339. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  340. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  341. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  342. }