-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_get_str.c
234 lines (207 loc) · 5.78 KB
/
test_get_str.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/***
* Example code for setting flags indicating signal arrival within the context
* of a curses application.
*/
//test
#include <ctype.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ncurses.h>
#include "charcell-utils.h"
int sigusr1_received = 0;
int sighup_received = 0;
void signal_catcher( int signum )
{
switch( signum ) {
case SIGHUP : sighup_received = 1; break;
case SIGUSR1 : sigusr1_received = 1; break;
}
/***
* The "kick in the pants" trick. We send ourselves a SIGWINCH
* to make sure that getch() in our primary process thread has a "key"
* to return.
*
* Otherwise we would have to wait around until the window *really* changed
* or a key press was sent to the terminal. Either way would result in a clunky user
* interface experience.
*
* Note that raise(2) IS async signal safe (see signal(7)).
*/
raise( SIGWINCH );
}
void install_handler( int signum )
{
/***
* setup a signal handler --- always use sigaction, avoid signal(2)
*
* another equally valid approach is to setup an independent handler for
* each signal. one handler seems easier to explain in lecture.
*/
struct sigaction act;
memset( &act, 0, sizeof(act) );
act.sa_handler = signal_catcher; // set the function to call on signal
// prevent reentrant signals --- wise to do whether you are using one or
// multiple signal handler functions
sigfillset( &act.sa_mask );
/***
* NOTE: in order for the SIGWINCH "kick in the pants" trick to work, we
* don't want the ncurses read(2) command to restart. So DON'T set SA_RESTART
* for these signal handlers!
*/
if( sigaction( signum, &act, NULL )) {
perror("sigaction" );
exit(1);
}
}
char *choices[] = { "Choice 1", "Choice 2", "Choice 3", "Exit" };
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
//box(menu_win, 0, 0);
mvwprintw(menu_win, 1, 2, "Use arrow keys to go up and down, Press enter to select a choice");
for(i = 0; i < n_choices; ++i)
{
clear_line(menu_win, y);
if(highlight == i + 1) /* High light the present choice */
{
wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}
#define BUFLEN 1024
int main(int argc, char *argv[])
{
initscr();
noecho();
cbreak();
keypad(stdscr,TRUE); // For arrows and enter/backspace for menu navigation and input
curs_set(FALSE);
install_handler( SIGHUP );
install_handler( SIGUSR1 );
// set up initial windows
WINDOW* display = newwin(1, 1, 0, 0 );
WINDOW* edit = newwin(1,1, 0, 0 );
int dispheight = size_display( display, edit );
int d = 0;
char buf[BUFLEN];
char inputstring[BUFLEN];
int ch;
int highlight = 1;
int choice = 0;
//mvwprintw(display, 1, 2, "Use arrow keys to go up and down, Press enter to select a choice");
print_menu( display, highlight );
// wrefresh(display);
while((ch = getch()) != KEY_F(10)) {
switch (ch) {
case KEY_RESIZE:
dispheight = size_display( display, edit );
d = 0;
strncpy( buf, "KEY_RESIZE", BUFLEN );
mvwprintw( display, d++ + 2, 2, buf );
d = d % dispheight;
if( sighup_received ) {
snprintf( buf, BUFLEN, "Received SIGHUP %lu", (unsigned long)time(NULL) );
mvwprintw( display, d++ + 2, 2, buf );
d = d % dispheight;
sighup_received = 0;
}
if( sigusr1_received ) {
snprintf( buf, BUFLEN, "Received SIGUSR1 %lu", (unsigned long)time(NULL) );
mvwprintw( display, d++ + 2, 2, buf );
d = d % dispheight;
sigusr1_received = 0;
}
wrefresh(display);
break;
case KEY_UP:
if(highlight == 1)
highlight = n_choices;
else
--highlight;
strncpy( buf, "KEY_UP", BUFLEN );
mvwprintw( edit, 1, 2, buf );
wrefresh(edit);
break;
// mvwprintw( display, d++ + 2, 2, buf );
// d = d % dispheight;
break;
case KEY_DOWN:
if(highlight == n_choices)
highlight = 1;
else
++highlight;
strncpy( buf, "KEY_DOWN", BUFLEN );
mvwprintw( edit, 1, 2, buf );
wrefresh(edit);
break;
// mvwprintw( display, d++ + 2, 2, buf );
// d = d % dispheight;
// wrefresh(display);
// break;
case 10:
choice = highlight;
strncpy( buf, "KEY_ENTER", BUFLEN );
mvwprintw( edit, 1, 2, buf );
wrefresh(edit);
break;
// mvwprintw( display, d++ + 2, 2, buf );
// d = d % dispheight;
// wrefresh(display);
// break;
case KEY_BACKSPACE:
// choice = highlight;
strncpy( buf, "KEY_BACKSPACE", BUFLEN );
mvwprintw( edit, 1, 2, buf );
wrefresh(edit);
break;
default :
if ( isprint(ch) ) {
snprintf( buf, BUFLEN, "%c", ch );
clear_line( edit, 1 );
mvwprintw( edit, 1, 2, buf );
wrefresh(edit);
} else {
snprintf( buf, BUFLEN, "Unprintable %04x", ch );
mvwprintw( display, d++ + 2, 2, buf );
d = d % dispheight;
wrefresh(display);
}
break;
}
//print_menu( display, highlight );
if( choice != 0 && choice != n_choices )
{
dispheight = size_display( display, edit );
//clear_line( display, 8 );
//snprintf( buf, BUFLEN, "You chose choice %d with choice string %s", choice, choices[choice-1] );
strncpy( buf, "Enter a string", BUFLEN );
mvwprintw( display, 2, 2, buf );
wrefresh( display );
get_string_input( edit, inputstring );
if( strlen( inputstring ) == 0 )
strncpy( buf, "You didn't type anything", BUFLEN );
else
snprintf( buf, BUFLEN, "You typed string: %s", inputstring );
mvwprintw( display, 3, 2, buf );
wrefresh( display );
getch();
choice = 0;
}
print_menu( display, highlight );
}
// close curses lib, reset terminal
endwin();
return 0;
}