header.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "util/cache.h"
  3. #include "util/debug.h"
  4. #include "ui/browser.h"
  5. #include "ui/keysyms.h"
  6. #include "ui/ui.h"
  7. #include "ui/util.h"
  8. #include "ui/libslang.h"
  9. #include "util/header.h"
  10. #include "util/session.h"
  11. #include <sys/ttydefaults.h>
  12. static void ui_browser__argv_write(struct ui_browser *browser,
  13. void *entry, int row)
  14. {
  15. char **arg = entry;
  16. char *str = *arg;
  17. char empty[] = " ";
  18. bool current_entry = ui_browser__is_current_entry(browser, row);
  19. unsigned long offset = (unsigned long)browser->priv;
  20. if (offset >= strlen(str))
  21. str = empty;
  22. else
  23. str = str + offset;
  24. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  25. HE_COLORSET_NORMAL);
  26. ui_browser__write_nstring(browser, str, browser->width);
  27. }
  28. static int list_menu__run(struct ui_browser *menu)
  29. {
  30. int key;
  31. unsigned long offset;
  32. const char help[] =
  33. "h/?/F1 Show this window\n"
  34. "UP/DOWN/PGUP\n"
  35. "PGDN/SPACE\n"
  36. "LEFT/RIGHT Navigate\n"
  37. "q/ESC/CTRL+C Exit browser";
  38. if (ui_browser__show(menu, "Header information", "Press 'q' to exit") < 0)
  39. return -1;
  40. while (1) {
  41. key = ui_browser__run(menu, 0);
  42. switch (key) {
  43. case K_RIGHT:
  44. offset = (unsigned long)menu->priv;
  45. offset += 10;
  46. menu->priv = (void *)offset;
  47. continue;
  48. case K_LEFT:
  49. offset = (unsigned long)menu->priv;
  50. if (offset >= 10)
  51. offset -= 10;
  52. menu->priv = (void *)offset;
  53. continue;
  54. case K_F1:
  55. case 'h':
  56. case '?':
  57. ui_browser__help_window(menu, help);
  58. continue;
  59. case K_ESC:
  60. case 'q':
  61. case CTRL('c'):
  62. key = -1;
  63. break;
  64. default:
  65. continue;
  66. }
  67. break;
  68. }
  69. ui_browser__hide(menu);
  70. return key;
  71. }
  72. static int ui__list_menu(int argc, char * const argv[])
  73. {
  74. struct ui_browser menu = {
  75. .entries = (void *)argv,
  76. .refresh = ui_browser__argv_refresh,
  77. .seek = ui_browser__argv_seek,
  78. .write = ui_browser__argv_write,
  79. .nr_entries = argc,
  80. };
  81. return list_menu__run(&menu);
  82. }
  83. int tui__header_window(struct perf_env *env)
  84. {
  85. int i, argc = 0;
  86. char **argv;
  87. struct perf_session *session;
  88. char *ptr, *pos;
  89. size_t size;
  90. FILE *fp = open_memstream(&ptr, &size);
  91. session = container_of(env, struct perf_session, header.env);
  92. perf_header__fprintf_info(session, fp, true);
  93. fclose(fp);
  94. for (pos = ptr, argc = 0; (pos = strchr(pos, '\n')) != NULL; pos++)
  95. argc++;
  96. argv = calloc(argc + 1, sizeof(*argv));
  97. if (argv == NULL)
  98. goto out;
  99. argv[0] = pos = ptr;
  100. for (i = 1; (pos = strchr(pos, '\n')) != NULL; i++) {
  101. *pos++ = '\0';
  102. argv[i] = pos;
  103. }
  104. BUG_ON(i != argc + 1);
  105. ui__list_menu(argc, argv);
  106. out:
  107. free(argv);
  108. free(ptr);
  109. return 0;
  110. }