blob: 0d312ca322c04258169fb8272262c97d482b65dc [file] [log] [blame]
Uwe Hermann7eb845e2008-11-02 17:01:06 +00001/*
2 * This file is part of the bayou 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 version 2 as
8 * published by the Free Software Foundation.
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.
Uwe Hermann7eb845e2008-11-02 17:01:06 +000014 */
15
16#include <libpayload.h>
17#include <curses.h>
18#include "bayou.h"
19
20#define SCREEN_X 80
21#define SCREEN_Y 25
22
23static int menu_width = 0;
24static struct payload *mpayloads[BAYOU_MAX_ENTRIES];
25static int m_entries = 0;
26static unsigned int selected = 0;
27static WINDOW *menuwin, *status;
28
29void create_menu(void)
30{
31 int i;
32
33 for (i = 0; i < bayoucfg.n_entries; i++) {
34 struct payload *p = &(bayoucfg.entries[i]);
35 char *name;
36
37 if ((p->pentry.parent != 0) ||
38 (p->pentry.flags & BPT_FLAG_NOSHOW))
39 continue;
40
41 mpayloads[m_entries++] = p;
42
43 name = payload_get_name(p);
44
45 if (strlen(name) > menu_width)
46 menu_width = strlen(name);
47 }
48
49 menu_width += 4;
50
51 if (menu_width < 30)
52 menu_width = 30;
53
54 menuwin = newwin(m_entries + 3, menu_width,
55 (SCREEN_Y - (m_entries + 3)) / 2,
56 (SCREEN_X - menu_width) / 2);
57}
58
59void draw_menu(void)
60{
61 struct payload *s;
62 int i;
63
64 wattrset(menuwin, COLOR_PAIR(3));
65 wclear(menuwin);
Ulf Jordan8c953382008-11-08 00:58:16 +000066 wborder(menuwin, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE,
67 ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER);
Uwe Hermann7eb845e2008-11-02 17:01:06 +000068
69 wattrset(menuwin, COLOR_PAIR(4) | A_BOLD);
70 mvwprintw(menuwin, 0, (menu_width - 17) / 2, " Payload Chooser ");
71
72 wattrset(menuwin, COLOR_PAIR(3));
73
74 for (i = 0; i < m_entries; i++) {
75 char *name = payload_get_name(mpayloads[i]);
76 int col = (menu_width - (2 + strlen(name))) / 2;
77
78 if (i == selected)
79 wattrset(menuwin, COLOR_PAIR(5) | A_BOLD);
80 else
81 wattrset(menuwin, COLOR_PAIR(3));
82
83 mvwprintw(menuwin, 2 + i, col, name);
84 }
85
86 s = mpayloads[selected];
87
88 wclear(status);
89
90 if (s->params[BAYOU_PARAM_DESC] != NULL) {
91 char buf[66];
92 int len = strnlen(s->params[BAYOU_PARAM_DESC], 65);
93
94 snprintf(buf, 65, s->params[BAYOU_PARAM_DESC]);
95 buf[65] = 0;
96
97 mvwprintw(status, 0, (80 - len) / 2, buf);
98 }
99
100 wrefresh(menuwin);
101 wrefresh(status);
102}
103
104void loop(void)
105{
106 int key;
107
108 while (1) {
109 key = getch();
110
111 if (key == ERR)
112 continue;
113
114 if (key == KEY_DOWN)
115 selected = (selected + 1) % m_entries;
116 else if (key == KEY_UP)
117 selected = (selected - 1) % m_entries;
118 else if (key == KEY_ENTER) {
119 run_payload(mpayloads[selected]);
Ulf Jordan142cad12009-02-17 16:23:26 +0000120 clear();
Uwe Hermann7eb845e2008-11-02 17:01:06 +0000121 refresh();
122 } else
123 continue;
124
125 draw_menu();
126 }
127}
128
129void menu(void)
130{
131 initscr();
132
133 init_pair(1, COLOR_WHITE, COLOR_RED);
134 init_pair(2, COLOR_BLACK, COLOR_WHITE);
135 init_pair(3, COLOR_BLACK, COLOR_WHITE);
136 init_pair(4, COLOR_CYAN, COLOR_WHITE);
137 init_pair(5, COLOR_WHITE, COLOR_RED);
138
139 wattrset(stdscr, COLOR_PAIR(1));
140 wclear(stdscr);
141
142 status = newwin(1, 80, 24, 0);
143 wattrset(status, COLOR_PAIR(2));
144 wclear(status);
145
146 refresh();
147
148 create_menu();
149 draw_menu();
150
151 loop();
152}