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