edbb7dd9eb2eafb45f6967c22fad90c39902e03d
[cascardo/linux.git] / tools / perf / util / ui / browser.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #undef _GNU_SOURCE
4 /*
5  * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
6  * the build if it isn't defined. Use the equivalent one that glibc
7  * has on features.h.
8  */
9 #include <features.h>
10 #ifndef HAVE_LONG_LONG
11 #define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
12 #endif
13 #include <slang.h>
14 #include <linux/list.h>
15 #include <linux/rbtree.h>
16 #include <stdlib.h>
17 #include <sys/ttydefaults.h>
18 #include "browser.h"
19 #include "../color.h"
20 #include "../util.h"
21
22 #if SLANG_VERSION < 20104
23 #define sltt_set_color(obj, name, fg, bg) \
24         SLtt_set_color(obj,(char *)name, (char *)fg, (char *)bg)
25 #else
26 #define sltt_set_color SLtt_set_color
27 #endif
28
29 newtComponent newt_form__new(void);
30
31 int ui_browser__percent_color(double percent, bool current)
32 {
33         if (current)
34                 return HE_COLORSET_SELECTED;
35         if (percent >= MIN_RED)
36                 return HE_COLORSET_TOP;
37         if (percent >= MIN_GREEN)
38                 return HE_COLORSET_MEDIUM;
39         return HE_COLORSET_NORMAL;
40 }
41
42 void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
43 {
44         struct list_head *head = self->entries;
45         struct list_head *pos;
46
47         switch (whence) {
48         case SEEK_SET:
49                 pos = head->next;
50                 break;
51         case SEEK_CUR:
52                 pos = self->top;
53                 break;
54         case SEEK_END:
55                 pos = head->prev;
56                 break;
57         default:
58                 return;
59         }
60
61         if (offset > 0) {
62                 while (offset-- != 0)
63                         pos = pos->next;
64         } else {
65                 while (offset++ != 0)
66                         pos = pos->prev;
67         }
68
69         self->top = pos;
70 }
71
72 void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
73 {
74         struct rb_root *root = self->entries;
75         struct rb_node *nd;
76
77         switch (whence) {
78         case SEEK_SET:
79                 nd = rb_first(root);
80                 break;
81         case SEEK_CUR:
82                 nd = self->top;
83                 break;
84         case SEEK_END:
85                 nd = rb_last(root);
86                 break;
87         default:
88                 return;
89         }
90
91         if (offset > 0) {
92                 while (offset-- != 0)
93                         nd = rb_next(nd);
94         } else {
95                 while (offset++ != 0)
96                         nd = rb_prev(nd);
97         }
98
99         self->top = nd;
100 }
101
102 unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
103 {
104         struct rb_node *nd;
105         int row = 0;
106
107         if (self->top == NULL)
108                 self->top = rb_first(self->entries);
109
110         nd = self->top;
111
112         while (nd != NULL) {
113                 SLsmg_gotorc(self->y + row, self->x);
114                 self->write(self, nd, row);
115                 if (++row == self->height)
116                         break;
117                 nd = rb_next(nd);
118         }
119
120         return row;
121 }
122
123 bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
124 {
125         return self->top_idx + row == self->index;
126 }
127
128 void ui_browser__refresh_dimensions(struct ui_browser *self)
129 {
130         int cols, rows;
131         newtGetScreenSize(&cols, &rows);
132
133         if (self->width > cols - 4)
134                 self->width = cols - 4;
135         self->height = rows - 5;
136         if (self->height > self->nr_entries)
137                 self->height = self->nr_entries;
138         self->y  = (rows - self->height) / 2;
139         self->x = (cols - self->width) / 2;
140 }
141
142 void ui_browser__reset_index(struct ui_browser *self)
143 {
144         self->index = self->top_idx = 0;
145         self->seek(self, 0, SEEK_SET);
146 }
147
148 int ui_browser__show(struct ui_browser *self, const char *title)
149 {
150         if (self->form != NULL) {
151                 newtFormDestroy(self->form);
152                 newtPopWindow();
153         }
154         ui_browser__refresh_dimensions(self);
155         newtCenteredWindow(self->width, self->height, title);
156         self->form = newt_form__new();
157         if (self->form == NULL)
158                 return -1;
159
160         self->sb = newtVerticalScrollbar(self->width, 0, self->height,
161                                          HE_COLORSET_NORMAL,
162                                          HE_COLORSET_SELECTED);
163         if (self->sb == NULL)
164                 return -1;
165
166         newtFormAddHotKey(self->form, NEWT_KEY_UP);
167         newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
168         newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
169         newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
170         newtFormAddHotKey(self->form, NEWT_KEY_HOME);
171         newtFormAddHotKey(self->form, NEWT_KEY_END);
172         newtFormAddComponent(self->form, self->sb);
173         return 0;
174 }
175
176 int ui_browser__refresh(struct ui_browser *self)
177 {
178         int row;
179
180         newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
181         row = self->refresh(self);
182         SLsmg_set_color(HE_COLORSET_NORMAL);
183         SLsmg_fill_region(self->y + row, self->x,
184                           self->height - row, self->width, ' ');
185
186         return 0;
187 }
188
189 int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
190 {
191         if (ui_browser__refresh(self) < 0)
192                 return -1;
193
194         while (1) {
195                 off_t offset;
196
197                 newtFormRun(self->form, es);
198
199                 if (es->reason != NEWT_EXIT_HOTKEY)
200                         break;
201                 if (is_exit_key(es->u.key))
202                         return es->u.key;
203                 switch (es->u.key) {
204                 case NEWT_KEY_DOWN:
205                         if (self->index == self->nr_entries - 1)
206                                 break;
207                         ++self->index;
208                         if (self->index == self->top_idx + self->height) {
209                                 ++self->top_idx;
210                                 self->seek(self, +1, SEEK_CUR);
211                         }
212                         break;
213                 case NEWT_KEY_UP:
214                         if (self->index == 0)
215                                 break;
216                         --self->index;
217                         if (self->index < self->top_idx) {
218                                 --self->top_idx;
219                                 self->seek(self, -1, SEEK_CUR);
220                         }
221                         break;
222                 case NEWT_KEY_PGDN:
223                 case ' ':
224                         if (self->top_idx + self->height > self->nr_entries - 1)
225                                 break;
226
227                         offset = self->height;
228                         if (self->index + offset > self->nr_entries - 1)
229                                 offset = self->nr_entries - 1 - self->index;
230                         self->index += offset;
231                         self->top_idx += offset;
232                         self->seek(self, +offset, SEEK_CUR);
233                         break;
234                 case NEWT_KEY_PGUP:
235                         if (self->top_idx == 0)
236                                 break;
237
238                         if (self->top_idx < self->height)
239                                 offset = self->top_idx;
240                         else
241                                 offset = self->height;
242
243                         self->index -= offset;
244                         self->top_idx -= offset;
245                         self->seek(self, -offset, SEEK_CUR);
246                         break;
247                 case NEWT_KEY_HOME:
248                         ui_browser__reset_index(self);
249                         break;
250                 case NEWT_KEY_END:
251                         offset = self->height - 1;
252                         if (offset >= self->nr_entries)
253                                 offset = self->nr_entries - 1;
254
255                         self->index = self->nr_entries - 1;
256                         self->top_idx = self->index - offset;
257                         self->seek(self, -offset, SEEK_END);
258                         break;
259                 default:
260                         return es->u.key;
261                 }
262                 if (ui_browser__refresh(self) < 0)
263                         return -1;
264         }
265         return 0;
266 }
267
268 unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
269 {
270         struct list_head *pos;
271         struct list_head *head = self->entries;
272         int row = 0;
273
274         if (self->top == NULL || self->top == self->entries)
275                 self->top = head->next;
276
277         pos = self->top;
278
279         list_for_each_from(pos, head) {
280                 SLsmg_gotorc(self->y + row, self->x);
281                 self->write(self, pos, row);
282                 if (++row == self->height)
283                         break;
284         }
285
286         return row;
287 }
288
289 static struct newtPercentTreeColors {
290         const char *topColorFg, *topColorBg;
291         const char *mediumColorFg, *mediumColorBg;
292         const char *normalColorFg, *normalColorBg;
293         const char *selColorFg, *selColorBg;
294         const char *codeColorFg, *codeColorBg;
295 } defaultPercentTreeColors = {
296         "red",       "lightgray",
297         "green",     "lightgray",
298         "black",     "lightgray",
299         "lightgray", "magenta",
300         "blue",      "lightgray",
301 };
302
303 void ui_browser__init(void)
304 {
305         struct newtPercentTreeColors *c = &defaultPercentTreeColors;
306
307         sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
308         sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
309         sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
310         sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
311         sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
312 }