lib/util: Move mac_buf from nc code to util library
[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 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(NULL);
180         }
181
182 #undef line
183 }
184
185 static void sysinfo_screen_process_key(struct nc_scr *scr, int key)
186 {
187         struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
188
189         switch (key) {
190         case 'x':
191                 screen->on_exit(screen->cui);
192                 break;
193         case KEY_DOWN:
194         case KEY_UP:
195                 sysinfo_screen_scroll(screen, key);
196                 break;
197         default:
198                 break;
199         }
200 }
201
202 static void sysinfo_screen_resize(struct nc_scr *scr)
203 {
204         struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
205         sysinfo_screen_draw(screen);
206 }
207
208 struct nc_scr *sysinfo_screen_scr(struct sysinfo_screen *screen)
209 {
210         return &screen->scr;
211 }
212
213 void sysinfo_screen_update(struct sysinfo_screen *screen,
214                 const struct system_info *sysinfo)
215 {
216         sysinfo_screen_populate(screen, sysinfo);
217         sysinfo_screen_draw(screen);
218 }
219
220 struct sysinfo_screen *sysinfo_screen_init(struct cui *cui,
221                 const struct system_info *sysinfo,
222                 void (*on_exit)(struct cui *))
223 {
224         struct sysinfo_screen *screen;
225
226         screen = talloc_zero(cui, struct sysinfo_screen);
227         nc_scr_init(&screen->scr, pb_sysinfo_screen_sig, 0,
228                         cui, sysinfo_screen_process_key,
229                         NULL, NULL, sysinfo_screen_resize);
230
231         screen->cui = cui;
232         screen->on_exit = on_exit;
233
234         screen->scr.frame.ltitle = talloc_strdup(screen,
235                         "Petitboot System Information");
236         screen->scr.frame.rtitle = NULL;
237         screen->scr.frame.help = talloc_strdup(screen, "x=exit");
238         nc_scr_frame_draw(&screen->scr);
239
240         sysinfo_screen_populate(screen, sysinfo);
241         wrefresh(screen->scr.main_ncw);
242         scrollok(screen->scr.sub_ncw, true);
243         sysinfo_screen_draw(screen);
244
245         return screen;
246 }