ui/ncurses/nc-widgets: Add initial textbox validation functions
[petitboot] / ui / ncurses / nc-sysinfo.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <string.h>
23
24 #include <talloc/talloc.h>
25 #include <types/types.h>
26 #include <log/log.h>
27 #include <util/util.h>
28
29 #include "nc-cui.h"
30 #include "nc-sysinfo.h"
31
32 struct sysinfo_screen {
33         struct nc_scr   scr;
34         struct cui      *cui;
35         char            **lines;
36         int             n_lines;
37         int             n_alloc_lines;
38         int             scroll_y;
39         void            (*on_exit)(struct cui *);
40 };
41
42 static struct sysinfo_screen *sysinfo_screen_from_scr(struct nc_scr *scr)
43 {
44         struct sysinfo_screen *sysinfo_screen;
45
46         assert(scr->sig == pb_sysinfo_screen_sig);
47         sysinfo_screen = (struct sysinfo_screen *)
48                 ((char *)scr - (size_t)&((struct sysinfo_screen *)0)->scr);
49         assert(sysinfo_screen->scr.sig == pb_sysinfo_screen_sig);
50         return sysinfo_screen;
51 }
52
53 static void sysinfo_screen_draw(struct sysinfo_screen *screen)
54 {
55         int max_y, i;
56
57         max_y = getmaxy(screen->scr.sub_ncw);
58
59         max_y = min(max_y, screen->scroll_y + screen->n_lines);
60
61         for (i = screen->scroll_y; i < max_y; i++)
62                 mvwaddstr(screen->scr.sub_ncw, i, 1, screen->lines[i]);
63
64         wrefresh(screen->scr.sub_ncw);
65 }
66
67 static void sysinfo_screen_scroll(struct sysinfo_screen *screen, int key)
68 {
69         int win_lines = getmaxy(screen->scr.sub_ncw);
70         int delta;
71
72         if (key == KEY_UP)
73                 delta = -1;
74         else if (key == KEY_DOWN)
75                 delta = 1;
76         else
77                 return;
78
79         if (screen->scroll_y + delta < 0)
80                 return;
81         if (screen->scroll_y + delta + win_lines > screen->n_lines - 1)
82                 return;
83
84         screen->scroll_y += delta;
85         wscrl(screen->scr.sub_ncw, delta);
86
87         if (delta > 0) {
88                 mvwaddstr(screen->scr.sub_ncw, win_lines - 1, 1,
89                                 screen->lines[screen->scroll_y+win_lines-1]);
90         } else if (delta < 0) {
91                 mvwaddstr(screen->scr.sub_ncw, 0, 1,
92                                 screen->lines[screen->scroll_y]);
93         }
94
95         wrefresh(screen->scr.sub_ncw);
96 }
97
98 static void sysinfo_clear(struct sysinfo_screen *screen)
99 {
100         talloc_free(screen->lines);
101         screen->n_lines = 0;
102         screen->n_alloc_lines = 16;
103         screen->lines = talloc_array(screen, char *, screen->n_alloc_lines);
104 }
105
106 static __attribute__((format(printf, 2, 3))) void sysinfo_screen_append_line(
107                 struct sysinfo_screen *screen, const char *fmt, ...)
108 {
109         char *line;
110         va_list ap;
111
112         if (fmt) {
113                 va_start(ap, fmt);
114                 line = talloc_vasprintf(screen->lines, fmt, ap);
115                 va_end(ap);
116         } else {
117                 line = "";
118         }
119
120         if (screen->n_lines == screen->n_alloc_lines) {
121                 screen->n_alloc_lines *= 2;
122                 screen->lines = talloc_realloc(screen, screen->lines,
123                                                 char *, screen->n_alloc_lines);
124         }
125
126         screen->lines[screen->n_lines] = line;
127         screen->n_lines++;
128 }
129
130 static void if_info_mac_str(struct interface_info *info,
131                 char *buf, unsigned int buflen)
132 {
133         return mac_str(info->hwaddr, info->hwaddr_size, buf, buflen);
134 }
135
136 static void sysinfo_screen_populate(struct sysinfo_screen *screen,
137                 const struct system_info *sysinfo)
138 {
139         unsigned int i;
140
141         sysinfo_clear(screen);
142
143 #define line(...) sysinfo_screen_append_line(screen, __VA_ARGS__)
144         if (!sysinfo) {
145                 line("Waiting for system information...");
146                 return;
147         }
148
149         line("%-12s %s", "System type:", sysinfo->type ?: "");
150         line("%-12s %s", "System id:",   sysinfo->identifier ?: "");
151
152         if (sysinfo->n_blockdevs) {
153                 line(NULL);
154                 line("Storage devices");
155         }
156
157         for (i = 0; i < sysinfo->n_blockdevs; i++) {
158                 struct blockdev_info *info = sysinfo->blockdevs[i];
159
160                 line("%s:", info->name);
161                 line(" UUID:       %s", info->uuid);
162                 line(" mounted at: %s", info->mountpoint);
163                 line(NULL);
164         }
165
166         if (sysinfo->n_interfaces) {
167                 line(NULL);
168                 line("Network interfaces");
169         }
170
171         for (i = 0; i < sysinfo->n_interfaces; i++) {
172                 struct interface_info *info = sysinfo->interfaces[i];
173                 char macbuf[32];
174
175                 if_info_mac_str(info, macbuf, sizeof(macbuf));
176
177                 line("%s:", info->name);
178                 line(" MAC:  %s", macbuf);
179                 line(" link: %s", info->link ? "up" : "down");
180                 line(NULL);
181         }
182
183 #undef line
184 }
185
186 static void sysinfo_screen_process_key(struct nc_scr *scr, int key)
187 {
188         struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
189
190         switch (key) {
191         case 'x':
192                 screen->on_exit(screen->cui);
193                 break;
194         case KEY_DOWN:
195         case KEY_UP:
196                 sysinfo_screen_scroll(screen, key);
197                 break;
198         default:
199                 break;
200         }
201 }
202
203 static void sysinfo_screen_resize(struct nc_scr *scr)
204 {
205         struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
206         sysinfo_screen_draw(screen);
207 }
208
209 struct nc_scr *sysinfo_screen_scr(struct sysinfo_screen *screen)
210 {
211         return &screen->scr;
212 }
213
214 void sysinfo_screen_update(struct sysinfo_screen *screen,
215                 const struct system_info *sysinfo)
216 {
217         sysinfo_screen_populate(screen, sysinfo);
218         sysinfo_screen_draw(screen);
219 }
220
221 struct sysinfo_screen *sysinfo_screen_init(struct cui *cui,
222                 const struct system_info *sysinfo,
223                 void (*on_exit)(struct cui *))
224 {
225         struct sysinfo_screen *screen;
226
227         screen = talloc_zero(cui, struct sysinfo_screen);
228         nc_scr_init(&screen->scr, pb_sysinfo_screen_sig, 0,
229                         cui, sysinfo_screen_process_key,
230                         NULL, NULL, sysinfo_screen_resize);
231
232         screen->cui = cui;
233         screen->on_exit = on_exit;
234
235         screen->scr.frame.ltitle = talloc_strdup(screen,
236                         "Petitboot System Information");
237         screen->scr.frame.rtitle = NULL;
238         screen->scr.frame.help = talloc_strdup(screen, "x=exit");
239         nc_scr_frame_draw(&screen->scr);
240
241         sysinfo_screen_populate(screen, sysinfo);
242         wrefresh(screen->scr.main_ncw);
243         scrollok(screen->scr.sub_ncw, true);
244         sysinfo_screen_draw(screen);
245
246         return screen;
247 }