kvm tools: Add ivshmem device
[linux-2.6/next.git] / tools / perf / util / ui / util.c
blobfdf1fc8f08bc83b2300311870f9b68ee559ef7c6
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>
8 #include "../cache.h"
9 #include "../debug.h"
10 #include "browser.h"
11 #include "helpline.h"
12 #include "ui.h"
13 #include "util.h"
15 static void newt_form__set_exit_keys(newtComponent self)
17 newtFormAddHotKey(self, NEWT_KEY_LEFT);
18 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
19 newtFormAddHotKey(self, 'Q');
20 newtFormAddHotKey(self, 'q');
21 newtFormAddHotKey(self, CTRL('c'));
24 static newtComponent newt_form__new(void)
26 newtComponent self = newtForm(NULL, NULL, 0);
27 if (self)
28 newt_form__set_exit_keys(self);
29 return self;
32 int ui__popup_menu(int argc, char * const argv[])
34 struct newtExitStruct es;
35 int i, rc = -1, max_len = 5;
36 newtComponent listbox, form = newt_form__new();
38 if (form == NULL)
39 return -1;
41 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
42 if (listbox == NULL)
43 goto out_destroy_form;
45 newtFormAddComponent(form, listbox);
47 for (i = 0; i < argc; ++i) {
48 int len = strlen(argv[i]);
49 if (len > max_len)
50 max_len = len;
51 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
52 goto out_destroy_form;
55 newtCenteredWindow(max_len, argc, NULL);
56 newtFormRun(form, &es);
57 rc = newtListboxGetCurrent(listbox) - NULL;
58 if (es.reason == NEWT_EXIT_HOTKEY)
59 rc = -1;
60 newtPopWindow();
61 out_destroy_form:
62 newtFormDestroy(form);
63 return rc;
66 int ui__help_window(const char *text)
68 struct newtExitStruct es;
69 newtComponent tb, form = newt_form__new();
70 int rc = -1;
71 int max_len = 0, nr_lines = 0;
72 const char *t;
74 if (form == NULL)
75 return -1;
77 t = text;
78 while (1) {
79 const char *sep = strchr(t, '\n');
80 int len;
82 if (sep == NULL)
83 sep = strchr(t, '\0');
84 len = sep - t;
85 if (max_len < len)
86 max_len = len;
87 ++nr_lines;
88 if (*sep == '\0')
89 break;
90 t = sep + 1;
93 tb = newtTextbox(0, 0, max_len, nr_lines, 0);
94 if (tb == NULL)
95 goto out_destroy_form;
97 newtTextboxSetText(tb, text);
98 newtFormAddComponent(form, tb);
99 newtCenteredWindow(max_len, nr_lines, NULL);
100 newtFormRun(form, &es);
101 newtPopWindow();
102 rc = 0;
103 out_destroy_form:
104 newtFormDestroy(form);
105 return rc;
108 static const char yes[] = "Yes", no[] = "No",
109 warning_str[] = "Warning!", ok[] = "Ok";
111 bool ui__dialog_yesno(const char *msg)
113 /* newtWinChoice should really be accepting const char pointers... */
114 return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1;
117 void ui__warning(const char *format, ...)
119 va_list args;
121 va_start(args, format);
122 if (use_browser > 0) {
123 pthread_mutex_lock(&ui__lock);
124 newtWinMessagev((char *)warning_str, (char *)ok,
125 (char *)format, args);
126 pthread_mutex_unlock(&ui__lock);
127 } else
128 vfprintf(stderr, format, args);
129 va_end(args);