Smaller fixes to allow using -Wall (trivial).
[coreboot.git] / payloads / coreinfo / coreinfo.c
1 /*
2  * This file is part of the coreinfo project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include "coreinfo.h"
21
22 #define SCREEN_Y 24
23 #define SCREEN_X 80
24
25 extern struct coreinfo_module cpuinfo_module;
26 extern struct coreinfo_module pci_module;
27 extern struct coreinfo_module coreboot_module;
28
29 #define MODULE_COUNT 3
30
31 struct coreinfo_module *modules[MODULE_COUNT] = {
32         &cpuinfo_module,
33         &pci_module,
34         &coreboot_module
35 };
36
37 static WINDOW *modwin;
38 static int curwin;
39
40 void print_module_title(WINDOW *win, const char *title)
41 {
42         int i;
43
44         wattrset(win, COLOR_PAIR(2));
45         mvwprintw(win, 0, 1, title);
46
47         wmove(win, 1, 1);
48
49         for (i = 0; i < 78; i++)
50                 waddch(win, '\304');
51 }
52
53 void print_menu(void)
54 {
55         int i, j;
56         char menu[80];
57         char *ptr = menu;
58
59         wmove(stdscr, 23, 0);
60
61         for (j = 0; j < SCREEN_X; j++)
62                 waddch(stdscr, ' ');
63
64         for (i = 0; i < MODULE_COUNT; i++)
65                 ptr += sprintf(ptr, "F%d: %s ", i + 1, modules[i]->name);
66
67         mvprintw(23, 0, menu);
68 }
69
70 void center(int row, const char *str)
71 {
72         int len = strlen(str);
73         int j;
74
75         wmove(stdscr, row, 0);
76
77         for (j = 0; j < SCREEN_X; j++)
78                 waddch(stdscr, ' ');
79
80         mvprintw(row, (SCREEN_X - len) / 2, str);
81 }
82
83 void header(int row, const char *str)
84 {
85         char buf[SCREEN_X];
86         char *ptr = buf;
87         int i;
88         int len = strlen(str) + 4;
89
90         for (i = 0; i < (SCREEN_X - len) / 2; i++)
91                 ptr += sprintf(ptr, "=");
92
93         ptr += sprintf(ptr, "[ %s ]", str);
94
95         for (i = ((SCREEN_X - len) / 2) + len; i < SCREEN_X; i++)
96                 ptr += sprintf(ptr, "=");
97
98         mvprintw(row, 0, buf);
99 }
100
101 static void redraw_module(void)
102 {
103         wclear(modwin);
104         modules[curwin]->redraw(modwin);
105         refresh();
106 }
107
108 void loop(void)
109 {
110         int key;
111
112         center(0, "coreinfo v0.1");
113
114         print_menu();
115         modules[curwin]->redraw(modwin);
116         refresh();
117
118         while (1) {
119                 key = getch();
120
121                 if (key == ERR)
122                         continue;
123
124                 if (key >= KEY_F(1) && key <= KEY_F(9)) {
125                         unsigned char ch = key - KEY_F(1);
126
127                         if (ch < MODULE_COUNT) {
128                                 curwin = ch;
129                                 redraw_module();
130                                 continue;
131                         }
132                 }
133
134                 if (modules[curwin]->handle)
135                         if (modules[curwin]->handle(key))
136                                 redraw_module();
137         }
138 }
139
140 int main(void)
141 {
142         int i, j;
143
144         curses_enable_serial(0);
145         curses_enable_vga(1);
146
147         initscr();
148
149         init_pair(1, COLOR_WHITE, COLOR_GREEN);
150         init_pair(2, COLOR_BLACK, COLOR_WHITE);
151         init_pair(3, COLOR_WHITE, COLOR_WHITE);
152
153         modwin = newwin(23, 80, 1, 0);
154
155         wattrset(stdscr, COLOR_PAIR(1) | A_BOLD);
156         wattrset(modwin, COLOR_PAIR(2));
157
158         for (i = 0; i < 23; i++) {
159                 wmove(modwin, i - 1, 0);
160
161                 for (j = 0; j < SCREEN_X; j++)
162                         waddch(modwin, ' ');
163         }
164
165         refresh();
166
167         for (i = 0; i < MODULE_COUNT; i++)
168                 modules[i]->init();
169
170         loop();
171
172         return 0;
173 }