Newer
Older
Kevin P. Fleming
committed
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2005 - 2006, Russell Bryant
*
* Russell Bryant <russell@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*
* \file
*
* \author Russell Bryant <russell@digium.com>
*
* \brief curses frontend for Asterisk module selection
*/
#include "asterisk/autoconfig.h"
Kevin P. Fleming
committed
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <curses.h>
#include "menuselect.h"
#define MENU_TITLE1 "*************************************"
#define MENU_TITLE2 "* Asterisk Module Selection *"
#define MENU_TITLE3 "*************************************"
Tilghman Lesher
committed
#define MENU_HELP "Press 'h' for help."
Kevin P. Fleming
committed
Tilghman Lesher
committed
#define TITLE_HEIGHT 7
Kevin P. Fleming
committed
#define MIN_X 80
#define MIN_Y 20
#define PAGE_OFFSET 10
/*! Maximum number of characters horizontally */
int max_x = 0;
/*! Maximum number of characters vertically */
int max_y = 0;
const char * const help_info[] = {
"scroll => up/down arrows",
"(de)select => Enter",
"select all => F8",
"deselect all => F7",
"back => left arrow",
"quit => q",
"save and quit => x",
"",
"XXX means dependencies have not been met"
};
void winch_handler(int sig);
void show_help(WINDOW *win);
void draw_main_menu(WINDOW *menu, int curopt);
Russell Bryant
committed
void draw_category_menu(WINDOW *menu, struct category *cat, int start, int end, int curopt, int changed);
Kevin P. Fleming
committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
int run_category_menu(WINDOW *menu, int cat_num);
int run_category_menu(WINDOW *menu, int cat_num);
void draw_title_window(WINDOW *title);
/*! \brief Handle a window resize in xterm */
void winch_handler(int sig)
{
getmaxyx(stdscr, max_y, max_x);
if (max_x < MIN_X - 1 || max_y < MIN_Y - 1) {
fprintf(stderr, "Terminal must be at least 80 x 25.\n");
max_x = MIN_X - 1;
max_y = MIN_Y - 1;
}
}
/*! \brief Display help information */
void show_help(WINDOW *win)
{
int i;
wclear(win);
for (i = 0; i < (sizeof(help_info) / sizeof(help_info[0])); i++) {
wmove(win, i, max_x / 2 - 15);
waddstr(win, help_info[i]);
}
wrefresh(win);
getch(); /* display the help until the user hits a key */
}
void draw_main_menu(WINDOW *menu, int curopt)
{
struct category *cat;
char buf[64];
int i = 0;
wclear(menu);
AST_LIST_TRAVERSE(&categories, cat, list) {
wmove(menu, i++, max_x / 2 - 10);
if (!strlen_zero(cat->displayname))
snprintf(buf, sizeof(buf), "%d.%s %s", i, i < 10 ? " " : "", cat->displayname);
else
snprintf(buf, sizeof(buf), "%d.%s %s", i, i < 10 ? " " : "", cat->name);
waddstr(menu, buf);
}
wmove(menu, curopt, (max_x / 2) - 15);
waddstr(menu, "--->");
wmove(menu, 0, 0);
wrefresh(menu);
}
Russell Bryant
committed
void display_mem_info(WINDOW *menu, struct member *mem, int start, int end)
Kevin P. Fleming
committed
{
Russell Bryant
committed
char buf[64];
struct depend *dep;
struct conflict *con;
Russell Bryant
committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
wmove(menu, end - start + 2, max_x / 2 - 16);
wclrtoeol(menu);
wmove(menu, end - start + 3, max_x / 2 - 16);
wclrtoeol(menu);
wmove(menu, end - start + 4, max_x / 2 - 16);
wclrtoeol(menu);
if (mem->displayname) {
wmove(menu, end - start + 2, max_x / 2 - 16);
waddstr(menu, mem->displayname);
}
if (!AST_LIST_EMPTY(&mem->deps)) {
wmove(menu, end - start + 3, max_x / 2 - 16);
strcpy(buf, "Depends on: ");
AST_LIST_TRAVERSE(&mem->deps, dep, list) {
strncat(buf, dep->name, sizeof(buf) - strlen(buf) - 1);
if (AST_LIST_NEXT(dep, list))
strncat(buf, ", ", sizeof(buf) - strlen(buf) - 1);
}
waddstr(menu, buf);
}
if (!AST_LIST_EMPTY(&mem->conflicts)) {
wmove(menu, end - start + 4, max_x / 2 - 16);
strcpy(buf, "Conflicts with: ");
AST_LIST_TRAVERSE(&mem->conflicts, con, list) {
strncat(buf, con->name, sizeof(buf) - strlen(buf) - 1);
if (AST_LIST_NEXT(con, list))
strncat(buf, ", ", sizeof(buf) - strlen(buf) - 1);
}
waddstr(menu, buf);
}
}
void draw_category_menu(WINDOW *menu, struct category *cat, int start, int end, int curopt, int changed)
{
int i = 0;
int j = 0;
struct member *mem;
Kevin P. Fleming
committed
char buf[64];
Kevin P. Fleming
committed
Russell Bryant
committed
if (!changed) {
/* If all we have to do is move the cursor,
* then don't clear the screen and start over */
AST_LIST_TRAVERSE(&cat->members, mem, list) {
i++;
if (curopt + 1 == i) {
display_mem_info(menu, mem, start, end);
break;
}
}
wmove(menu, curopt - start, max_x / 2 - 9);
wrefresh(menu);
return;
}
Kevin P. Fleming
committed
wclear(menu);
Russell Bryant
committed
i = 0;
Kevin P. Fleming
committed
AST_LIST_TRAVERSE(&cat->members, mem, list) {
if (i < start) {
i++;
continue;
}
wmove(menu, j++, max_x / 2 - 10);
i++;
if (mem->depsfailed)
snprintf(buf, sizeof(buf), "XXX %d.%s %s", i, i < 10 ? " " : "", mem->name);
else
snprintf(buf, sizeof(buf), "[%s] %d.%s %s", mem->enabled ? "*" : " ", i, i < 10 ? " " : "", mem->name);
waddstr(menu, buf);
if (curopt + 1 == i)
Russell Bryant
committed
display_mem_info(menu, mem, start, end);
Kevin P. Fleming
committed
if (i == end)
break;
}
wmove(menu, curopt - start, max_x / 2 - 9);
wrefresh(menu);
}
int run_category_menu(WINDOW *menu, int cat_num)
{
struct category *cat;
int i = 0;
int start = 0;
int end = max_y - TITLE_HEIGHT - 6;
Kevin P. Fleming
committed
int c;
int curopt = 0;
int maxopt;
Russell Bryant
committed
int changed = 1;
Kevin P. Fleming
committed
AST_LIST_TRAVERSE(&categories, cat, list) {
if (i++ == cat_num)
break;
}
if (!cat)
return -1;
maxopt = count_members(cat) - 1;
Russell Bryant
committed
draw_category_menu(menu, cat, start, end, curopt, changed);
Kevin P. Fleming
committed
while ((c = getch())) {
Russell Bryant
committed
changed = 0;
Kevin P. Fleming
committed
switch (c) {
case KEY_UP:
if (curopt > 0) {
curopt--;
if (curopt < start) {
start--;
end--;
Russell Bryant
committed
changed = 1;
Kevin P. Fleming
committed
}
}
break;
case KEY_DOWN:
if (curopt < maxopt) {
curopt++;
if (curopt > end - 1) {
start++;
end++;
Russell Bryant
committed
changed = 1;
Kevin P. Fleming
committed
}
}
break;
case KEY_NPAGE:
/* XXX Move down the list by PAGE_OFFSET */
break;
case KEY_PPAGE:
/* XXX Move up the list by PAGE_OFFSET */
break;
case KEY_LEFT:
Tilghman Lesher
committed
case 27: /* Esc key */
Kevin P. Fleming
committed
return 0;
case KEY_RIGHT:
case KEY_ENTER:
case '\n':
case ' ':
toggle_enabled(cat, curopt);
Russell Bryant
committed
changed = 1;
Kevin P. Fleming
committed
break;
case 'h':
case 'H':
show_help(menu);
Kevin P. Fleming
committed
break;
case KEY_F(7):
set_all(cat, 0);
Russell Bryant
committed
changed = 1;
Kevin P. Fleming
committed
break;
case KEY_F(8):
set_all(cat, 1);
Russell Bryant
committed
changed = 1;
Kevin P. Fleming
committed
default:
break;
}
if (c == 'x' || c == 'X' || c == 'Q' || c == 'q')
Kevin P. Fleming
committed
break;
Russell Bryant
committed
draw_category_menu(menu, cat, start, end, curopt, changed);
Kevin P. Fleming
committed
}
wrefresh(menu);
return c;
}
void draw_title_window(WINDOW *title)
{
wmove(title, 1, (max_x / 2) - (strlen(MENU_TITLE1) / 2));
waddstr(title, MENU_TITLE1);
wmove(title, 2, (max_x / 2) - (strlen(MENU_TITLE2) / 2));
waddstr(title, MENU_TITLE2);
wmove(title, 3, (max_x / 2) - (strlen(MENU_TITLE3) / 2));
waddstr(title, MENU_TITLE3);
Tilghman Lesher
committed
wmove(title, 5, (max_x / 2) - (strlen(MENU_HELP) / 2));
waddstr(title, MENU_HELP);
Kevin P. Fleming
committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
wrefresh(title);
}
int run_menu(void)
{
WINDOW *title;
WINDOW *menu;
int maxopt;
int curopt = 0;
int c;
int res = 0;
initscr();
getmaxyx(stdscr, max_y, max_x);
signal(SIGWINCH, winch_handler); /* handle window resizing in xterm */
if (max_x < MIN_X - 1 || max_y < MIN_Y - 1) {
fprintf(stderr, "Terminal must be at least %d x %d.\n", MIN_X, MIN_Y);
endwin();
return -1;
}
cbreak(); /* don't buffer input until the enter key is pressed */
noecho(); /* don't echo user input to the screen */
keypad(stdscr, TRUE); /* allow the use of arrow keys */
clear();
refresh();
maxopt = count_categories() - 1;
/* We have two windows - the title window at the top, and the menu window gets the rest */
title = newwin(TITLE_HEIGHT, max_x, 0, 0);
menu = newwin(max_y - TITLE_HEIGHT, max_x, TITLE_HEIGHT, 0);
draw_title_window(title);
draw_main_menu(menu, curopt);
while ((c = getch())) {
switch (c) {
case KEY_UP:
if (curopt > 0)
curopt--;
break;
case KEY_DOWN:
if (curopt < maxopt)
curopt++;
break;
case KEY_RIGHT:
case KEY_ENTER:
case '\n':
case ' ':
c = run_category_menu(menu, curopt);
break;
case 'h':
case 'H':
show_help(menu);
default:
break;
}
Tilghman Lesher
committed
if (c == 'q' || c == 'Q' || c == 27) {
Kevin P. Fleming
committed
res = -1;
break;
}
Tilghman Lesher
committed
if (c == 'x' || c == 'X' || c == 's' || c == 'S')
Kevin P. Fleming
committed
break;
draw_main_menu(menu, curopt);
}
endwin();
return res;
}