1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include <ncurses.h>
// http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/printw.html
#include <stdio.h>
#include <ncurses.h>
#define WIDTH 50
#define HEIGHT 20
int startx = 0;
int starty = 0;
/*
COLOR_BLACK 0
COLOR_RED 1
COLOR_GREEN 2
COLOR_YELLOW 3
COLOR_BLUE 4
COLOR_MAGENTA 5
COLOR_CYAN 6
COLOR_WHITE 7
*/
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
int main()
{ WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
initscr();
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
start_color(); /* Start color */
init_pair(1, COLOR_BLACK, COLOR_WHITE);
init_pair(2, COLOR_WHITE, COLOR_BLUE);
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
wbkgd(menu_win, COLOR_PAIR(2));
keypad(menu_win, TRUE);
/*
nodelay(menu_win,FALSE);
notimeout(menu_win,FALSE);
wtimeout(menu_win,-1);
*/
mvprintw(0, 0, "Use W/S/A/D to go up and down, Press enter to select a choice");
refresh();
print_menu(menu_win, highlight);
while(1)
{
choice=0;
int y=0;
while(1)
{ c = wgetch(menu_win);
mvprintw(y++,0, "%3d / '%c'", c, c);
y%=24;
refresh();
switch(c)
{ case 'w':
case KEY_UP:
if(highlight == 1)
highlight = n_choices;
else
--highlight;
break;
case 's':
case KEY_DOWN:
if(highlight == n_choices)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
}
print_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
char *argv1[]={"xterm","/bin/fsh",0};
char *env1[]={"HOME=/home/miguel","PS1=\033[34m$\033[37m","PWD=/home/miguel","PATH=/bin","TERM=fool-term",0};
int pid=_fork();
if(pid==0)
{
_execve("/bin/xterm",argv1,env1);
}
}
clrtoeol();
refresh();
endwin();
return 0;
}
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, COLOR_PAIR(1));
mvwprintw(menu_win, y, x, ">> %40s <<", choices[i]);
wattroff(menu_win, COLOR_PAIR(1));
}
else
mvwprintw(menu_win, y, x, " %40s ", choices[i]);
++y;
}
wrefresh(menu_win);
}
|