2 * Copyright (C) 2013 IBM Corporation
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.
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.
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
18 #if defined(HAVE_CONFIG_H)
24 #include <talloc/talloc.h>
25 #include <types/types.h>
27 #include <util/util.h>
30 #include "nc-sysinfo.h"
32 struct sysinfo_screen {
39 void (*on_exit)(struct cui *);
42 static struct sysinfo_screen *sysinfo_screen_from_scr(struct nc_scr *scr)
44 struct sysinfo_screen *sysinfo_screen;
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;
53 static void sysinfo_screen_draw(struct sysinfo_screen *screen)
57 max_y = getmaxy(screen->scr.sub_ncw);
59 max_y = min(max_y, screen->scroll_y + screen->n_lines);
61 for (i = screen->scroll_y; i < max_y; i++)
62 mvwaddstr(screen->scr.sub_ncw, i, 1, screen->lines[i]);
64 wrefresh(screen->scr.sub_ncw);
67 static void sysinfo_screen_scroll(struct sysinfo_screen *screen, int key)
69 int win_lines = getmaxy(screen->scr.sub_ncw);
74 else if (key == KEY_DOWN)
79 if (screen->scroll_y + delta < 0)
81 if (screen->scroll_y + delta + win_lines > screen->n_lines - 1)
84 screen->scroll_y += delta;
85 wscrl(screen->scr.sub_ncw, delta);
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]);
95 wrefresh(screen->scr.sub_ncw);
98 static void sysinfo_clear(struct sysinfo_screen *screen)
100 talloc_free(screen->lines);
102 screen->n_alloc_lines = 16;
103 screen->lines = talloc_array(screen, char *, screen->n_alloc_lines);
106 static __attribute__((format(printf, 2, 3))) void sysinfo_screen_append_line(
107 struct sysinfo_screen *screen, const char *fmt, ...)
114 line = talloc_vasprintf(screen->lines, fmt, ap);
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);
126 screen->lines[screen->n_lines] = line;
130 static void if_info_mac_str(struct interface_info *info,
131 char *buf, unsigned int buflen)
133 return mac_str(info->hwaddr, info->hwaddr_size, buf, buflen);
136 static void sysinfo_screen_populate(struct sysinfo_screen *screen,
137 const struct system_info *sysinfo)
141 sysinfo_clear(screen);
143 #define line(...) sysinfo_screen_append_line(screen, __VA_ARGS__)
145 line("Waiting for system information...");
149 line("%-12s %s", "System type:", sysinfo->type ?: "");
150 line("%-12s %s", "System id:", sysinfo->identifier ?: "");
152 if (sysinfo->n_blockdevs) {
154 line("Storage devices");
157 for (i = 0; i < sysinfo->n_blockdevs; i++) {
158 struct blockdev_info *info = sysinfo->blockdevs[i];
160 line("%s:", info->name);
161 line(" UUID: %s", info->uuid);
162 line(" mounted at: %s", info->mountpoint);
166 if (sysinfo->n_interfaces) {
168 line("Network interfaces");
171 for (i = 0; i < sysinfo->n_interfaces; i++) {
172 struct interface_info *info = sysinfo->interfaces[i];
175 if_info_mac_str(info, macbuf, sizeof(macbuf));
177 line("%s:", info->name);
178 line(" MAC: %s", macbuf);
179 line(" link: %s", info->link ? "up" : "down");
186 static void sysinfo_screen_process_key(struct nc_scr *scr, int key)
188 struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
192 screen->on_exit(screen->cui);
196 sysinfo_screen_scroll(screen, key);
203 static void sysinfo_screen_resize(struct nc_scr *scr)
205 struct sysinfo_screen *screen = sysinfo_screen_from_scr(scr);
206 sysinfo_screen_draw(screen);
209 struct nc_scr *sysinfo_screen_scr(struct sysinfo_screen *screen)
214 void sysinfo_screen_update(struct sysinfo_screen *screen,
215 const struct system_info *sysinfo)
217 sysinfo_screen_populate(screen, sysinfo);
218 sysinfo_screen_draw(screen);
221 struct sysinfo_screen *sysinfo_screen_init(struct cui *cui,
222 const struct system_info *sysinfo,
223 void (*on_exit)(struct cui *))
225 struct sysinfo_screen *screen;
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);
233 screen->on_exit = on_exit;
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);
241 sysinfo_screen_populate(screen, sysinfo);
242 wrefresh(screen->scr.main_ncw);
243 scrollok(screen->scr.sub_ncw, true);
244 sysinfo_screen_draw(screen);