browser.c 10 KB

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