Uwe Hermann | 7eb845e | 2008-11-02 17:01:06 +0000 | [diff] [blame] | 1 | /* |
| 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 Hermann | 7eb845e | 2008-11-02 17:01:06 +0000 | [diff] [blame] | 14 | */ |
| 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 | |
| 23 | static int menu_width = 0; |
| 24 | static struct payload *mpayloads[BAYOU_MAX_ENTRIES]; |
| 25 | static int m_entries = 0; |
| 26 | static unsigned int selected = 0; |
| 27 | static WINDOW *menuwin, *status; |
| 28 | |
| 29 | void 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 | |
| 59 | void draw_menu(void) |
| 60 | { |
| 61 | struct payload *s; |
| 62 | int i; |
| 63 | |
| 64 | wattrset(menuwin, COLOR_PAIR(3)); |
| 65 | wclear(menuwin); |
Ulf Jordan | 8c95338 | 2008-11-08 00:58:16 +0000 | [diff] [blame] | 66 | wborder(menuwin, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE, |
| 67 | ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER); |
Uwe Hermann | 7eb845e | 2008-11-02 17:01:06 +0000 | [diff] [blame] | 68 | |
| 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 | |
| 104 | void 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 Jordan | 142cad1 | 2009-02-17 16:23:26 +0000 | [diff] [blame] | 120 | clear(); |
Uwe Hermann | 7eb845e | 2008-11-02 17:01:06 +0000 | [diff] [blame] | 121 | refresh(); |
| 122 | } else |
| 123 | continue; |
| 124 | |
| 125 | draw_menu(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void 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 | } |