Merge branch 'stable/swiotlb-0.9' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / tools / perf / util / ui / browser.c
1 #include <slang.h>
2 #include "libslang.h"
3 #include <linux/compiler.h>
4 #include <linux/list.h>
5 #include <linux/rbtree.h>
6 #include <stdlib.h>
7 #include <sys/ttydefaults.h>
8 #include "browser.h"
9 #include "helpline.h"
10 #include "../color.h"
11 #include "../util.h"
12 #include <stdio.h>
13
14 static int ui_browser__percent_color(double percent, bool current)
15 {
16         if (current)
17                 return HE_COLORSET_SELECTED;
18         if (percent >= MIN_RED)
19                 return HE_COLORSET_TOP;
20         if (percent >= MIN_GREEN)
21                 return HE_COLORSET_MEDIUM;
22         return HE_COLORSET_NORMAL;
23 }
24
25 void ui_browser__set_color(struct ui_browser *self __used, int color)
26 {
27         SLsmg_set_color(color);
28 }
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(percent, current);
34          ui_browser__set_color(self, color);
35 }
36
37 void ui_browser__gotorc(struct ui_browser *self, int y, int x)
38 {
39         SLsmg_gotorc(self->y + y, self->x + x);
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                 ui_browser__gotorc(self, row, 0);
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         self->width = cols - 1;
134         self->height = rows - 2;
135         self->y = 1;
136         self->x = 0;
137 }
138
139 void ui_browser__reset_index(struct ui_browser *self)
140 {
141         self->index = self->top_idx = 0;
142         self->seek(self, 0, SEEK_SET);
143 }
144
145 void ui_browser__add_exit_key(struct ui_browser *self, int key)
146 {
147         newtFormAddHotKey(self->form, key);
148 }
149
150 void ui_browser__add_exit_keys(struct ui_browser *self, int keys[])
151 {
152         int i = 0;
153
154         while (keys[i] && i < 64) {
155                 ui_browser__add_exit_key(self, keys[i]);
156                 ++i;
157         }
158 }
159
160 int ui_browser__show(struct ui_browser *self, const char *title,
161                      const char *helpline, ...)
162 {
163         va_list ap;
164         int keys[] = { NEWT_KEY_UP, NEWT_KEY_DOWN, NEWT_KEY_PGUP,
165                        NEWT_KEY_PGDN, NEWT_KEY_HOME, NEWT_KEY_END, ' ',
166                        NEWT_KEY_LEFT, NEWT_KEY_ESCAPE, 'q', CTRL('c'), 0 };
167
168         if (self->form != NULL)
169                 newtFormDestroy(self->form);
170
171         ui_browser__refresh_dimensions(self);
172         self->form = newtForm(NULL, NULL, 0);
173         if (self->form == NULL)
174                 return -1;
175
176         self->sb = newtVerticalScrollbar(self->width, 1, self->height,
177                                          HE_COLORSET_NORMAL,
178                                          HE_COLORSET_SELECTED);
179         if (self->sb == NULL)
180                 return -1;
181
182         SLsmg_gotorc(0, 0);
183         ui_browser__set_color(self, NEWT_COLORSET_ROOT);
184         slsmg_write_nstring(title, self->width);
185
186         ui_browser__add_exit_keys(self, keys);
187         newtFormAddComponent(self->form, self->sb);
188
189         va_start(ap, helpline);
190         ui_helpline__vpush(helpline, ap);
191         va_end(ap);
192         return 0;
193 }
194
195 void ui_browser__hide(struct ui_browser *self)
196 {
197         newtFormDestroy(self->form);
198         self->form = NULL;
199         ui_helpline__pop();
200 }
201
202 int ui_browser__refresh(struct ui_browser *self)
203 {
204         int row;
205
206         newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
207         row = self->refresh(self);
208         ui_browser__set_color(self, HE_COLORSET_NORMAL);
209         SLsmg_fill_region(self->y + row, self->x,
210                           self->height - row, self->width, ' ');
211
212         return 0;
213 }
214
215 int ui_browser__run(struct ui_browser *self)
216 {
217         struct newtExitStruct es;
218
219         if (ui_browser__refresh(self) < 0)
220                 return -1;
221
222         while (1) {
223                 off_t offset;
224
225                 newtFormRun(self->form, &es);
226
227                 if (es.reason != NEWT_EXIT_HOTKEY)
228                         break;
229                 switch (es.u.key) {
230                 case NEWT_KEY_DOWN:
231                         if (self->index == self->nr_entries - 1)
232                                 break;
233                         ++self->index;
234                         if (self->index == self->top_idx + self->height) {
235                                 ++self->top_idx;
236                                 self->seek(self, +1, SEEK_CUR);
237                         }
238                         break;
239                 case NEWT_KEY_UP:
240                         if (self->index == 0)
241                                 break;
242                         --self->index;
243                         if (self->index < self->top_idx) {
244                                 --self->top_idx;
245                                 self->seek(self, -1, SEEK_CUR);
246                         }
247                         break;
248                 case NEWT_KEY_PGDN:
249                 case ' ':
250                         if (self->top_idx + self->height > self->nr_entries - 1)
251                                 break;
252
253                         offset = self->height;
254                         if (self->index + offset > self->nr_entries - 1)
255                                 offset = self->nr_entries - 1 - self->index;
256                         self->index += offset;
257                         self->top_idx += offset;
258                         self->seek(self, +offset, SEEK_CUR);
259                         break;
260                 case NEWT_KEY_PGUP:
261                         if (self->top_idx == 0)
262                                 break;
263
264                         if (self->top_idx < self->height)
265                                 offset = self->top_idx;
266                         else
267                                 offset = self->height;
268
269                         self->index -= offset;
270                         self->top_idx -= offset;
271                         self->seek(self, -offset, SEEK_CUR);
272                         break;
273                 case NEWT_KEY_HOME:
274                         ui_browser__reset_index(self);
275                         break;
276                 case NEWT_KEY_END:
277                         offset = self->height - 1;
278                         if (offset >= self->nr_entries)
279                                 offset = self->nr_entries - 1;
280
281                         self->index = self->nr_entries - 1;
282                         self->top_idx = self->index - offset;
283                         self->seek(self, -offset, SEEK_END);
284                         break;
285                 default:
286                         return es.u.key;
287                 }
288                 if (ui_browser__refresh(self) < 0)
289                         return -1;
290         }
291         return -1;
292 }
293
294 unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
295 {
296         struct list_head *pos;
297         struct list_head *head = self->entries;
298         int row = 0;
299
300         if (self->top == NULL || self->top == self->entries)
301                 self->top = head->next;
302
303         pos = self->top;
304
305         list_for_each_from(pos, head) {
306                 ui_browser__gotorc(self, row, 0);
307                 self->write(self, pos, row);
308                 if (++row == self->height)
309                         break;
310         }
311
312         return row;
313 }
314
315 static struct newtPercentTreeColors {
316         const char *topColorFg, *topColorBg;
317         const char *mediumColorFg, *mediumColorBg;
318         const char *normalColorFg, *normalColorBg;
319         const char *selColorFg, *selColorBg;
320         const char *codeColorFg, *codeColorBg;
321 } defaultPercentTreeColors = {
322         "red",       "lightgray",
323         "green",     "lightgray",
324         "black",     "lightgray",
325         "lightgray", "magenta",
326         "blue",      "lightgray",
327 };
328
329 void ui_browser__init(void)
330 {
331         struct newtPercentTreeColors *c = &defaultPercentTreeColors;
332
333         sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
334         sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
335         sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
336         sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
337         sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
338 }