browser.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. #include "../util.h"
  2. #include "../config.h"
  3. #include "../../perf.h"
  4. #include "libslang.h"
  5. #include "ui.h"
  6. #include "util.h"
  7. #include <linux/compiler.h>
  8. #include <linux/list.h>
  9. #include <linux/rbtree.h>
  10. #include <stdlib.h>
  11. #include <sys/ttydefaults.h>
  12. #include "browser.h"
  13. #include "helpline.h"
  14. #include "keysyms.h"
  15. #include "../color.h"
  16. static int ui_browser__percent_color(struct ui_browser *browser,
  17. double percent, bool current)
  18. {
  19. if (current && (!browser->use_navkeypressed || browser->navkeypressed))
  20. return HE_COLORSET_SELECTED;
  21. if (percent >= MIN_RED)
  22. return HE_COLORSET_TOP;
  23. if (percent >= MIN_GREEN)
  24. return HE_COLORSET_MEDIUM;
  25. return HE_COLORSET_NORMAL;
  26. }
  27. int ui_browser__set_color(struct ui_browser *browser, int color)
  28. {
  29. int ret = browser->current_color;
  30. browser->current_color = color;
  31. SLsmg_set_color(color);
  32. return ret;
  33. }
  34. void ui_browser__set_percent_color(struct ui_browser *browser,
  35. double percent, bool current)
  36. {
  37. int color = ui_browser__percent_color(browser, percent, current);
  38. ui_browser__set_color(browser, color);
  39. }
  40. void ui_browser__gotorc(struct ui_browser *browser, int y, int x)
  41. {
  42. SLsmg_gotorc(browser->y + y, browser->x + x);
  43. }
  44. void ui_browser__write_nstring(struct ui_browser *browser __maybe_unused, const char *msg,
  45. unsigned int width)
  46. {
  47. slsmg_write_nstring(msg, width);
  48. }
  49. void ui_browser__printf(struct ui_browser *browser __maybe_unused, const char *fmt, ...)
  50. {
  51. va_list args;
  52. va_start(args, fmt);
  53. slsmg_vprintf(fmt, args);
  54. va_end(args);
  55. }
  56. static struct list_head *
  57. ui_browser__list_head_filter_entries(struct ui_browser *browser,
  58. struct list_head *pos)
  59. {
  60. do {
  61. if (!browser->filter || !browser->filter(browser, pos))
  62. return pos;
  63. pos = pos->next;
  64. } while (pos != browser->entries);
  65. return NULL;
  66. }
  67. static struct list_head *
  68. ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
  69. struct list_head *pos)
  70. {
  71. do {
  72. if (!browser->filter || !browser->filter(browser, pos))
  73. return pos;
  74. pos = pos->prev;
  75. } while (pos != browser->entries);
  76. return NULL;
  77. }
  78. void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
  79. {
  80. struct list_head *head = browser->entries;
  81. struct list_head *pos;
  82. if (browser->nr_entries == 0)
  83. return;
  84. switch (whence) {
  85. case SEEK_SET:
  86. pos = ui_browser__list_head_filter_entries(browser, head->next);
  87. break;
  88. case SEEK_CUR:
  89. pos = browser->top;
  90. break;
  91. case SEEK_END:
  92. pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
  93. break;
  94. default:
  95. return;
  96. }
  97. assert(pos != NULL);
  98. if (offset > 0) {
  99. while (offset-- != 0)
  100. pos = ui_browser__list_head_filter_entries(browser, pos->next);
  101. } else {
  102. while (offset++ != 0)
  103. pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
  104. }
  105. browser->top = pos;
  106. }
  107. void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
  108. {
  109. struct rb_root *root = browser->entries;
  110. struct rb_node *nd;
  111. switch (whence) {
  112. case SEEK_SET:
  113. nd = rb_first(root);
  114. break;
  115. case SEEK_CUR:
  116. nd = browser->top;
  117. break;
  118. case SEEK_END:
  119. nd = rb_last(root);
  120. break;
  121. default:
  122. return;
  123. }
  124. if (offset > 0) {
  125. while (offset-- != 0)
  126. nd = rb_next(nd);
  127. } else {
  128. while (offset++ != 0)
  129. nd = rb_prev(nd);
  130. }
  131. browser->top = nd;
  132. }
  133. unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
  134. {
  135. struct rb_node *nd;
  136. int row = 0;
  137. if (browser->top == NULL)
  138. browser->top = rb_first(browser->entries);
  139. nd = browser->top;
  140. while (nd != NULL) {
  141. ui_browser__gotorc(browser, row, 0);
  142. browser->write(browser, nd, row);
  143. if (++row == browser->rows)
  144. break;
  145. nd = rb_next(nd);
  146. }
  147. return row;
  148. }
  149. bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
  150. {
  151. return browser->top_idx + row == browser->index;
  152. }
  153. void ui_browser__refresh_dimensions(struct ui_browser *browser)
  154. {
  155. browser->width = SLtt_Screen_Cols - 1;
  156. browser->height = browser->rows = SLtt_Screen_Rows - 2;
  157. browser->y = 1;
  158. browser->x = 0;
  159. }
  160. void ui_browser__handle_resize(struct ui_browser *browser)
  161. {
  162. ui__refresh_dimensions(false);
  163. ui_browser__show(browser, browser->title, ui_helpline__current);
  164. ui_browser__refresh(browser);
  165. }
  166. int ui_browser__warning(struct ui_browser *browser, int timeout,
  167. const char *format, ...)
  168. {
  169. va_list args;
  170. char *text;
  171. int key = 0, err;
  172. va_start(args, format);
  173. err = vasprintf(&text, format, args);
  174. va_end(args);
  175. if (err < 0) {
  176. va_start(args, format);
  177. ui_helpline__vpush(format, args);
  178. va_end(args);
  179. } else {
  180. while ((key = ui__question_window("Warning!", text,
  181. "Press any key...",
  182. timeout)) == K_RESIZE)
  183. ui_browser__handle_resize(browser);
  184. free(text);
  185. }
  186. return key;
  187. }
  188. int ui_browser__help_window(struct ui_browser *browser, const char *text)
  189. {
  190. int key;
  191. while ((key = ui__help_window(text)) == K_RESIZE)
  192. ui_browser__handle_resize(browser);
  193. return key;
  194. }
  195. bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
  196. {
  197. int key;
  198. while ((key = ui__dialog_yesno(text)) == K_RESIZE)
  199. ui_browser__handle_resize(browser);
  200. return key == K_ENTER || toupper(key) == 'Y';
  201. }
  202. void ui_browser__reset_index(struct ui_browser *browser)
  203. {
  204. browser->index = browser->top_idx = 0;
  205. browser->seek(browser, 0, SEEK_SET);
  206. }
  207. void __ui_browser__show_title(struct ui_browser *browser, const char *title)
  208. {
  209. SLsmg_gotorc(0, 0);
  210. ui_browser__set_color(browser, HE_COLORSET_ROOT);
  211. ui_browser__write_nstring(browser, title, browser->width + 1);
  212. }
  213. void ui_browser__show_title(struct ui_browser *browser, const char *title)
  214. {
  215. pthread_mutex_lock(&ui__lock);
  216. __ui_browser__show_title(browser, title);
  217. pthread_mutex_unlock(&ui__lock);
  218. }
  219. int ui_browser__show(struct ui_browser *browser, const char *title,
  220. const char *helpline, ...)
  221. {
  222. int err;
  223. va_list ap;
  224. if (browser->refresh_dimensions == NULL)
  225. browser->refresh_dimensions = ui_browser__refresh_dimensions;
  226. browser->refresh_dimensions(browser);
  227. pthread_mutex_lock(&ui__lock);
  228. __ui_browser__show_title(browser, title);
  229. browser->title = title;
  230. zfree(&browser->helpline);
  231. va_start(ap, helpline);
  232. err = vasprintf(&browser->helpline, helpline, ap);
  233. va_end(ap);
  234. if (err > 0)
  235. ui_helpline__push(browser->helpline);
  236. pthread_mutex_unlock(&ui__lock);
  237. return err ? 0 : -1;
  238. }
  239. void ui_browser__hide(struct ui_browser *browser)
  240. {
  241. pthread_mutex_lock(&ui__lock);
  242. ui_helpline__pop();
  243. zfree(&browser->helpline);
  244. pthread_mutex_unlock(&ui__lock);
  245. }
  246. static void ui_browser__scrollbar_set(struct ui_browser *browser)
  247. {
  248. int height = browser->height, h = 0, pct = 0,
  249. col = browser->width,
  250. row = 0;
  251. if (browser->nr_entries > 1) {
  252. pct = ((browser->index * (browser->height - 1)) /
  253. (browser->nr_entries - 1));
  254. }
  255. SLsmg_set_char_set(1);
  256. while (h < height) {
  257. ui_browser__gotorc(browser, row++, col);
  258. SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
  259. ++h;
  260. }
  261. SLsmg_set_char_set(0);
  262. }
  263. static int __ui_browser__refresh(struct ui_browser *browser)
  264. {
  265. int row;
  266. int width = browser->width;
  267. row = browser->refresh(browser);
  268. ui_browser__set_color(browser, HE_COLORSET_NORMAL);
  269. if (!browser->use_navkeypressed || browser->navkeypressed)
  270. ui_browser__scrollbar_set(browser);
  271. else
  272. width += 1;
  273. SLsmg_fill_region(browser->y + row, browser->x,
  274. browser->height - row, width, ' ');
  275. return 0;
  276. }
  277. int ui_browser__refresh(struct ui_browser *browser)
  278. {
  279. pthread_mutex_lock(&ui__lock);
  280. __ui_browser__refresh(browser);
  281. pthread_mutex_unlock(&ui__lock);
  282. return 0;
  283. }
  284. /*
  285. * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
  286. * forget about any reference to any entry in the underlying data structure,
  287. * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
  288. * after an output_resort and hist decay.
  289. */
  290. void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
  291. {
  292. off_t offset = nr_entries - browser->nr_entries;
  293. browser->nr_entries = nr_entries;
  294. if (offset < 0) {
  295. if (browser->top_idx < (u64)-offset)
  296. offset = -browser->top_idx;
  297. browser->index += offset;
  298. browser->top_idx += offset;
  299. }
  300. browser->top = NULL;
  301. browser->seek(browser, browser->top_idx, SEEK_SET);
  302. }
  303. int ui_browser__run(struct ui_browser *browser, int delay_secs)
  304. {
  305. int err, key;
  306. while (1) {
  307. off_t offset;
  308. pthread_mutex_lock(&ui__lock);
  309. err = __ui_browser__refresh(browser);
  310. SLsmg_refresh();
  311. pthread_mutex_unlock(&ui__lock);
  312. if (err < 0)
  313. break;
  314. key = ui__getch(delay_secs);
  315. if (key == K_RESIZE) {
  316. ui__refresh_dimensions(false);
  317. browser->refresh_dimensions(browser);
  318. __ui_browser__show_title(browser, browser->title);
  319. ui_helpline__puts(browser->helpline);
  320. continue;
  321. }
  322. if (browser->use_navkeypressed && !browser->navkeypressed) {
  323. if (key == K_DOWN || key == K_UP ||
  324. (browser->columns && (key == K_LEFT || key == K_RIGHT)) ||
  325. key == K_PGDN || key == K_PGUP ||
  326. key == K_HOME || key == K_END ||
  327. key == ' ') {
  328. browser->navkeypressed = true;
  329. continue;
  330. } else
  331. return key;
  332. }
  333. switch (key) {
  334. case K_DOWN:
  335. if (browser->index == browser->nr_entries - 1)
  336. break;
  337. ++browser->index;
  338. if (browser->index == browser->top_idx + browser->rows) {
  339. ++browser->top_idx;
  340. browser->seek(browser, +1, SEEK_CUR);
  341. }
  342. break;
  343. case K_UP:
  344. if (browser->index == 0)
  345. break;
  346. --browser->index;
  347. if (browser->index < browser->top_idx) {
  348. --browser->top_idx;
  349. browser->seek(browser, -1, SEEK_CUR);
  350. }
  351. break;
  352. case K_RIGHT:
  353. if (!browser->columns)
  354. goto out;
  355. if (browser->horiz_scroll < browser->columns - 1)
  356. ++browser->horiz_scroll;
  357. break;
  358. case K_LEFT:
  359. if (!browser->columns)
  360. goto out;
  361. if (browser->horiz_scroll != 0)
  362. --browser->horiz_scroll;
  363. break;
  364. case K_PGDN:
  365. case ' ':
  366. if (browser->top_idx + browser->rows > browser->nr_entries - 1)
  367. break;
  368. offset = browser->rows;
  369. if (browser->index + offset > browser->nr_entries - 1)
  370. offset = browser->nr_entries - 1 - browser->index;
  371. browser->index += offset;
  372. browser->top_idx += offset;
  373. browser->seek(browser, +offset, SEEK_CUR);
  374. break;
  375. case K_PGUP:
  376. if (browser->top_idx == 0)
  377. break;
  378. if (browser->top_idx < browser->rows)
  379. offset = browser->top_idx;
  380. else
  381. offset = browser->rows;
  382. browser->index -= offset;
  383. browser->top_idx -= offset;
  384. browser->seek(browser, -offset, SEEK_CUR);
  385. break;
  386. case K_HOME:
  387. ui_browser__reset_index(browser);
  388. break;
  389. case K_END:
  390. offset = browser->rows - 1;
  391. if (offset >= browser->nr_entries)
  392. offset = browser->nr_entries - 1;
  393. browser->index = browser->nr_entries - 1;
  394. browser->top_idx = browser->index - offset;
  395. browser->seek(browser, -offset, SEEK_END);
  396. break;
  397. default:
  398. out:
  399. return key;
  400. }
  401. }
  402. return -1;
  403. }
  404. unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
  405. {
  406. struct list_head *pos;
  407. struct list_head *head = browser->entries;
  408. int row = 0;
  409. if (browser->top == NULL || browser->top == browser->entries)
  410. browser->top = ui_browser__list_head_filter_entries(browser, head->next);
  411. pos = browser->top;
  412. list_for_each_from(pos, head) {
  413. if (!browser->filter || !browser->filter(browser, pos)) {
  414. ui_browser__gotorc(browser, row, 0);
  415. browser->write(browser, pos, row);
  416. if (++row == browser->rows)
  417. break;
  418. }
  419. }
  420. return row;
  421. }
  422. static struct ui_browser_colorset {
  423. const char *name, *fg, *bg;
  424. int colorset;
  425. } ui_browser__colorsets[] = {
  426. {
  427. .colorset = HE_COLORSET_TOP,
  428. .name = "top",
  429. .fg = "red",
  430. .bg = "default",
  431. },
  432. {
  433. .colorset = HE_COLORSET_MEDIUM,
  434. .name = "medium",
  435. .fg = "green",
  436. .bg = "default",
  437. },
  438. {
  439. .colorset = HE_COLORSET_NORMAL,
  440. .name = "normal",
  441. .fg = "default",
  442. .bg = "default",
  443. },
  444. {
  445. .colorset = HE_COLORSET_SELECTED,
  446. .name = "selected",
  447. .fg = "black",
  448. .bg = "yellow",
  449. },
  450. {
  451. .colorset = HE_COLORSET_JUMP_ARROWS,
  452. .name = "jump_arrows",
  453. .fg = "blue",
  454. .bg = "default",
  455. },
  456. {
  457. .colorset = HE_COLORSET_ADDR,
  458. .name = "addr",
  459. .fg = "magenta",
  460. .bg = "default",
  461. },
  462. {
  463. .colorset = HE_COLORSET_ROOT,
  464. .name = "root",
  465. .fg = "white",
  466. .bg = "blue",
  467. },
  468. {
  469. .name = NULL,
  470. }
  471. };
  472. static int ui_browser__color_config(const char *var, const char *value,
  473. void *data __maybe_unused)
  474. {
  475. char *fg = NULL, *bg;
  476. int i;
  477. /* same dir for all commands */
  478. if (prefixcmp(var, "colors.") != 0)
  479. return 0;
  480. for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
  481. const char *name = var + 7;
  482. if (strcmp(ui_browser__colorsets[i].name, name) != 0)
  483. continue;
  484. fg = strdup(value);
  485. if (fg == NULL)
  486. break;
  487. bg = strchr(fg, ',');
  488. if (bg == NULL)
  489. break;
  490. *bg = '\0';
  491. while (isspace(*++bg));
  492. ui_browser__colorsets[i].bg = bg;
  493. ui_browser__colorsets[i].fg = fg;
  494. return 0;
  495. }
  496. free(fg);
  497. return -1;
  498. }
  499. void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
  500. {
  501. switch (whence) {
  502. case SEEK_SET:
  503. browser->top = browser->entries;
  504. break;
  505. case SEEK_CUR:
  506. browser->top = browser->top + browser->top_idx + offset;
  507. break;
  508. case SEEK_END:
  509. browser->top = browser->top + browser->nr_entries - 1 + offset;
  510. break;
  511. default:
  512. return;
  513. }
  514. }
  515. unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
  516. {
  517. unsigned int row = 0, idx = browser->top_idx;
  518. char **pos;
  519. if (browser->top == NULL)
  520. browser->top = browser->entries;
  521. pos = (char **)browser->top;
  522. while (idx < browser->nr_entries) {
  523. if (!browser->filter || !browser->filter(browser, *pos)) {
  524. ui_browser__gotorc(browser, row, 0);
  525. browser->write(browser, pos, row);
  526. if (++row == browser->rows)
  527. break;
  528. }
  529. ++idx;
  530. ++pos;
  531. }
  532. return row;
  533. }
  534. void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
  535. u16 start, u16 end)
  536. {
  537. SLsmg_set_char_set(1);
  538. ui_browser__gotorc(browser, start, column);
  539. SLsmg_draw_vline(end - start + 1);
  540. SLsmg_set_char_set(0);
  541. }
  542. void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
  543. int graph)
  544. {
  545. SLsmg_set_char_set(1);
  546. SLsmg_write_char(graph);
  547. SLsmg_set_char_set(0);
  548. }
  549. static void __ui_browser__line_arrow_up(struct ui_browser *browser,
  550. unsigned int column,
  551. u64 start, u64 end)
  552. {
  553. unsigned int row, end_row;
  554. SLsmg_set_char_set(1);
  555. if (start < browser->top_idx + browser->rows) {
  556. row = start - browser->top_idx;
  557. ui_browser__gotorc(browser, row, column);
  558. SLsmg_write_char(SLSMG_LLCORN_CHAR);
  559. ui_browser__gotorc(browser, row, column + 1);
  560. SLsmg_draw_hline(2);
  561. if (row-- == 0)
  562. goto out;
  563. } else
  564. row = browser->rows - 1;
  565. if (end > browser->top_idx)
  566. end_row = end - browser->top_idx;
  567. else
  568. end_row = 0;
  569. ui_browser__gotorc(browser, end_row, column);
  570. SLsmg_draw_vline(row - end_row + 1);
  571. ui_browser__gotorc(browser, end_row, column);
  572. if (end >= browser->top_idx) {
  573. SLsmg_write_char(SLSMG_ULCORN_CHAR);
  574. ui_browser__gotorc(browser, end_row, column + 1);
  575. SLsmg_write_char(SLSMG_HLINE_CHAR);
  576. ui_browser__gotorc(browser, end_row, column + 2);
  577. SLsmg_write_char(SLSMG_RARROW_CHAR);
  578. }
  579. out:
  580. SLsmg_set_char_set(0);
  581. }
  582. static void __ui_browser__line_arrow_down(struct ui_browser *browser,
  583. unsigned int column,
  584. u64 start, u64 end)
  585. {
  586. unsigned int row, end_row;
  587. SLsmg_set_char_set(1);
  588. if (start >= browser->top_idx) {
  589. row = start - browser->top_idx;
  590. ui_browser__gotorc(browser, row, column);
  591. SLsmg_write_char(SLSMG_ULCORN_CHAR);
  592. ui_browser__gotorc(browser, row, column + 1);
  593. SLsmg_draw_hline(2);
  594. if (++row == 0)
  595. goto out;
  596. } else
  597. row = 0;
  598. if (end >= browser->top_idx + browser->rows)
  599. end_row = browser->rows - 1;
  600. else
  601. end_row = end - browser->top_idx;
  602. ui_browser__gotorc(browser, row, column);
  603. SLsmg_draw_vline(end_row - row + 1);
  604. ui_browser__gotorc(browser, end_row, column);
  605. if (end < browser->top_idx + browser->rows) {
  606. SLsmg_write_char(SLSMG_LLCORN_CHAR);
  607. ui_browser__gotorc(browser, end_row, column + 1);
  608. SLsmg_write_char(SLSMG_HLINE_CHAR);
  609. ui_browser__gotorc(browser, end_row, column + 2);
  610. SLsmg_write_char(SLSMG_RARROW_CHAR);
  611. }
  612. out:
  613. SLsmg_set_char_set(0);
  614. }
  615. void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
  616. u64 start, u64 end)
  617. {
  618. if (start > end)
  619. __ui_browser__line_arrow_up(browser, column, start, end);
  620. else
  621. __ui_browser__line_arrow_down(browser, column, start, end);
  622. }
  623. void ui_browser__init(void)
  624. {
  625. int i = 0;
  626. perf_config(ui_browser__color_config, NULL);
  627. while (ui_browser__colorsets[i].name) {
  628. struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
  629. sltt_set_color(c->colorset, c->name, c->fg, c->bg);
  630. }
  631. annotate_browser__init();
  632. }