37e6fe081a58bfbfe2923920ab0e74a55fa5a9ed
[pandora-kernel.git] / tools / perf / util / ui / util.c
1 #include <newt.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <string.h>
6 #include <sys/ttydefaults.h>
7
8 #include "../cache.h"
9 #include "../debug.h"
10 #include "browser.h"
11 #include "keysyms.h"
12 #include "helpline.h"
13 #include "ui.h"
14 #include "util.h"
15
16 static void ui_browser__argv_write(struct ui_browser *browser,
17                                    void *entry, int row)
18 {
19         char **arg = entry;
20         bool current_entry = ui_browser__is_current_entry(browser, row);
21
22         ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
23                                                        HE_COLORSET_NORMAL);
24         slsmg_write_nstring(*arg, browser->width);
25 }
26
27 static int popup_menu__run(struct ui_browser *menu)
28 {
29         int key;
30
31         if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
32                 return -1;
33
34         while (1) {
35                 key = ui_browser__run(menu, 0);
36
37                 switch (key) {
38                 case K_RIGHT:
39                 case K_ENTER:
40                         key = menu->index;
41                         break;
42                 case K_LEFT:
43                 case K_ESC:
44                 case 'q':
45                 case CTRL('c'):
46                         key = -1;
47                         break;
48                 default:
49                         continue;
50                 }
51
52                 break;
53         }
54
55         ui_browser__hide(menu);
56         return key;
57 }
58
59 static void newt_form__set_exit_keys(newtComponent self)
60 {
61         newtFormAddHotKey(self, NEWT_KEY_LEFT);
62         newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
63         newtFormAddHotKey(self, 'Q');
64         newtFormAddHotKey(self, 'q');
65         newtFormAddHotKey(self, CTRL('c'));
66 }
67
68 static newtComponent newt_form__new(void)
69 {
70         newtComponent self = newtForm(NULL, NULL, 0);
71         if (self)
72                 newt_form__set_exit_keys(self);
73         return self;
74 }
75
76 int ui__popup_menu(int argc, char * const argv[])
77 {
78         struct ui_browser menu = {
79                 .entries    = (void *)argv,
80                 .refresh    = ui_browser__argv_refresh,
81                 .seek       = ui_browser__argv_seek,
82                 .write      = ui_browser__argv_write,
83                 .nr_entries = argc,
84         };
85
86         return popup_menu__run(&menu);
87 }
88
89 int ui__help_window(const char *text)
90 {
91         struct newtExitStruct es;
92         newtComponent tb, form = newt_form__new();
93         int rc = -1;
94         int max_len = 0, nr_lines = 0;
95         const char *t;
96
97         if (form == NULL)
98                 return -1;
99
100         t = text;
101         while (1) {
102                 const char *sep = strchr(t, '\n');
103                 int len;
104
105                 if (sep == NULL)
106                         sep = strchr(t, '\0');
107                 len = sep - t;
108                 if (max_len < len)
109                         max_len = len;
110                 ++nr_lines;
111                 if (*sep == '\0')
112                         break;
113                 t = sep + 1;
114         }
115
116         tb = newtTextbox(0, 0, max_len, nr_lines, 0);
117         if (tb == NULL)
118                 goto out_destroy_form;
119
120         newtTextboxSetText(tb, text);
121         newtFormAddComponent(form, tb);
122         newtCenteredWindow(max_len, nr_lines, NULL);
123         newtFormRun(form, &es);
124         newtPopWindow();
125         rc = 0;
126 out_destroy_form:
127         newtFormDestroy(form);
128         return rc;
129 }
130
131 static const char yes[] = "Yes", no[] = "No",
132                   warning_str[] = "Warning!", ok[] = "Ok";
133
134 bool ui__dialog_yesno(const char *msg)
135 {
136         /* newtWinChoice should really be accepting const char pointers... */
137         return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1;
138 }
139
140 void ui__warning(const char *format, ...)
141 {
142         va_list args;
143
144         va_start(args, format);
145         if (use_browser > 0) {
146                 pthread_mutex_lock(&ui__lock);
147                 newtWinMessagev((char *)warning_str, (char *)ok,
148                                 (char *)format, args);
149                 pthread_mutex_unlock(&ui__lock);
150         } else
151                 vfprintf(stderr, format, args);
152         va_end(args);
153 }