2
Copyright (c) 2006 Perry Rapp
4
Permission is hereby granted, free of charge, to any person
5
obtaining a copy of this software and associated documentation
6
files (the "Software"), to deal in the Software without
7
restriction, including without limitation the rights to use, copy,
8
modify, merge, publish, distribute, sublicense, and/or sell copies
9
of the Software, and to permit persons to whom the Software is
10
furnished to do so, subject to the following conditions:
12
The above copyright notice and this permission notice shall be
13
included in all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
/*=============================================================
25
* interact.c -- code to wait for and return a single letter or keypress
27
*===========================================================*/
37
/*********************************************
38
* external/imported variables
39
*********************************************/
41
extern STRING qSmn_unkcmd;
43
/*********************************************
44
* local function prototypes
45
*********************************************/
48
static INT interact_worker(UIWINDOW uiwin, STRING str, INT screen);
49
static INT translate_control_key(INT c);
50
static INT translate_hdware_key(INT c);
52
/*********************************************
54
*********************************************/
56
static int ui_time_elapsed = 0; /* total time waiting for user input */
58
/*********************************************
59
* local & exported function definitions
61
*********************************************/
63
/*===============================
64
* interact_choice_string -- Interact with user
65
* Handle string of choices as passed
66
* also returns hardware and control keys, so caller must check & loop
67
*=============================*/
69
interact_choice_string (UIWINDOW uiwin, STRING str)
71
return interact_worker(uiwin, str, -1);
73
/*===============================
74
* interact_screen_menu -- Interact with user
75
* This is just for browse screens (witness argument "screen")
76
* and uses the preconfigured menu for that screen
77
* also returns hardware and control keys, so caller must check & loop
78
*=============================*/
80
interact_screen_menu (UIWINDOW uiwin, INT screen)
82
return interact_worker(uiwin, NULL, screen);
84
/*===============================
85
* interact_worker -- Interact with user
86
* This is just for browse screens (witness argument "screen")
87
*=============================*/
89
interact_worker (UIWINDOW uiwin, STRING str, INT screen)
91
char buffer[4]; /* 3 char cmds max */
94
INT c, i, n = str ? strlen(str) : 0;
99
INT time_start=time(NULL);
101
keypad(uiw_win(uiwin),1);
102
c = wgetch(uiw_win(uiwin));
103
ui_time_elapsed += time(NULL) - time_start;
104
if (c == EOF) c = 'q';
106
/* after they chose off the menu, we wipe any
107
status message still lingering from before they chose */
108
clear_status_display();
110
return hardware and control keys
111
in case caller handles them
114
return translate_control_key(c);
117
return translate_hdware_key(c);
119
if (str) { /* traditional: list of choice letters */
120
for (i = 0; i < n; i++) {
121
if (c == (uchar)str[i]) return c;
123
} else { /* new menus (in menuitem.c) */
124
if (offset < (INT)sizeof(buffer)-1) {
126
buffer[offset+1] = 0;
134
/* Get Menu Command */
135
cmdnum = menuset_check_cmd(get_screen_menuset(screen), buffer);
137
/* Act On Menu Command */
138
if (cmdnum != CMD_NONE && cmdnum != CMD_PARTIAL) {
141
if (cmdnum != CMD_PARTIAL) {
142
msg_error(_(qSmn_unkcmd));
146
/* choice was no good, we loop & wait for another choice */
149
/*===============================
150
* translate_hdware_key --
151
* translate curses keycode into menuitem.h constant
152
*=============================*/
153
struct hdkeycvt { int key; int cmd; };
155
translate_hdware_key (INT c)
157
/* curses constant, menuitem constant */
158
static struct hdkeycvt hdkey[] = {
159
{ KEY_UP, CMD_KY_UP }
160
, { KEY_DOWN, CMD_KY_DN }
161
, { KEY_NPAGE, CMD_KY_PGDN }
162
, { KEY_PPAGE, CMD_KY_PGUP }
163
, { KEY_SNEXT, CMD_KY_SHPGDN }
164
, { KEY_SPREVIOUS, CMD_KY_SHPGUP }
165
, { KEY_HOME, CMD_KY_HOME }
166
, { KEY_END, CMD_KY_END }
167
, { KEY_ENTER, CMD_KY_ENTER }
170
for (i=0; i<ARRSIZE(hdkey); ++i) {
171
if (c == hdkey[i].key)
176
/*===============================
177
* translate_control_key --
178
* translate control keys into menuitem.h constant
179
*=============================*/
181
translate_control_key (INT c)
183
static struct hdkeycvt hdkey[] = {
184
{ '\r', CMD_KY_ENTER } /* Win32 */
185
, { '\n', CMD_KY_ENTER } /* UNIX */
188
for (i=0; i<ARRSIZE(hdkey); ++i) {
189
if (c == hdkey[i].key)
194
/*============================
195
* get_uitime -- return cumulative elapsed time waiting
196
* for user input (since start of program)
197
*==========================*/
201
return ui_time_elapsed;