ui/ncurses: Add sysinfo screen
[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 #define _GNU_SOURCE
19
20
21 #include <string.h>
22
23 #include <talloc/talloc.h>
24 #include <types/types.h>
25 #include <log/log.h>
26 #include <util/util.h>
27
28 #include "config.h"
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 mac_str(struct interface_info *info, char *buf, unsigned int buflen)
131 {
132         unsigned int i;
133         char *pos;
134
135         assert(buflen > sizeof("unknown"));
136
137         if (!info->hwaddr_size || info->hwaddr_size * 3 + 1 > buflen) {
138                 strcpy(buf, "unknown");
139                 return;
140         }
141
142         pos = buf;
143
144         for (i = 0; i < info->hwaddr_size; i++) {
145                 snprintf(pos, 4, "%02x:", info->hwaddr[i]);
146                 pos += 3;
147         }
148
149         *(pos - 1) = '\0';
150
151         return;
152 }
153
154 static void sysinfo_screen_populate(struct sysinfo_screen *screen,
155                 const struct system_info *sysinfo)
156 {
157         unsigned int i;
158
159         sysinfo_clear(screen);
160
161 #define line(...) sysinfo_screen_append_line(screen, __VA_ARGS__)
162         if (!sysinfo) {
163                 line("Waiting for system information...");
164                 return;
165         }
166
167         line("%-12s %s", "System type:", sysinfo->type ?: "");
168         line("%-12s %s", "System id:",   sysinfo->identifier ?: "");
169
170         line(NULL);
171         if (sysinfo->n_interfaces)
172                 line("Network interfaces");
173
174         for (i = 0; i < sysinfo->n_interfaces; i++) {
175                 struct interface_info *info = sysinfo->interfaces[i];
176                 char macbuf[32];
177
178                 mac_str(info, macbuf, sizeof(macbuf));
179
180                 line("%s:", info->name);
181                 line(" MAC: %s", macbuf);
182                 line(NULL);
183         }
184
185 #undef line
186 }
187
188 static void sysinfo_screen_process_key(struct nc_scr *scr, int key)
189 {
190         struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
191
192         switch (key) {
193         case 'x':
194                 screen->on_exit(screen->cui);
195                 break;
196         case KEY_DOWN:
197         case KEY_UP:
198                 sysinfo_screen_scroll(screen, key);
199                 break;
200         default:
201                 break;
202         }
203 }
204
205 static void sysinfo_screen_resize(struct nc_scr *scr)
206 {
207         struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
208         sysinfo_screen_draw(screen);
209 }
210
211 struct nc_scr *sysinfo_screen_scr(struct sysinfo_screen *screen)
212 {
213         return &screen->scr;
214 }
215
216 void sysinfo_screen_update(struct sysinfo_screen *screen,
217                 const struct system_info *sysinfo)
218 {
219         sysinfo_screen_populate(screen, sysinfo);
220         sysinfo_screen_draw(screen);
221 }
222
223 struct sysinfo_screen *sysinfo_screen_init(struct cui *cui,
224                 const struct system_info *sysinfo,
225                 void (*on_exit)(struct cui *))
226 {
227         struct sysinfo_screen *screen;
228
229         screen = talloc_zero(cui, struct sysinfo_screen);
230         nc_scr_init(&screen->scr, pb_sysinfo_screen_sig, 0,
231                         cui, sysinfo_screen_process_key,
232                         NULL, NULL, sysinfo_screen_resize);
233
234         screen->cui = cui;
235         screen->on_exit = on_exit;
236
237         screen->scr.frame.ltitle = talloc_strdup(screen,
238                         "Petitboot System Information");
239         screen->scr.frame.rtitle = NULL;
240         screen->scr.frame.help = talloc_strdup(screen, "x=exit");
241         nc_scr_frame_draw(&screen->scr);
242
243         sysinfo_screen_populate(screen, sysinfo);
244         wrefresh(screen->scr.main_ncw);
245         scrollok(screen->scr.sub_ncw, true);
246         sysinfo_screen_draw(screen);
247
248         return screen;
249 }