~ubuntu-branches/ubuntu/trusty/lifelines/trusty

« back to all changes in this revision

Viewing changes to src/liflines/interact.c

  • Committer: Bazaar Package Importer
  • Author(s): Felipe Augusto van de Wiel (faw)
  • Date: 2007-08-14 00:02:04 UTC
  • mfrom: (1.1.4 upstream) (3.1.4 gutsy)
  • Revision ID: james.westby@ubuntu.com-20070814000204-mpv5faygl0dgq3qi
Tags: 3.0.61-1
* New upstream release. (Closes: #387856).
* Fixing documentation build problems also fixes FTBFS if built twice in a
  row. (Closes: #424543).
* Adding lynx as a build dependency. This is necessary to generate txt files
  from html, discovered while fixing #424543.
* Upstream fix: charset for gedcom file in unicode. (Closes: #396206).
* Upstream fim: updating documentation about desc-tex2. (Closes: #405501).
* Bumping Standards-Version to 3.7.2 without package changes.
* Dropping local debian patches added upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   Copyright (c) 2006 Perry Rapp
 
3
 
 
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:
 
11
 
 
12
   The above copyright notice and this permission notice shall be
 
13
   included in all copies or substantial portions of the Software.
 
14
 
 
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
 
22
   SOFTWARE.
 
23
*/
 
24
/*=============================================================
 
25
 * interact.c -- code to wait for and return a single letter or keypress
 
26
 * Copyright(c) 2006
 
27
 *===========================================================*/
 
28
 
 
29
#include <time.h>
 
30
#include "llstdlib.h"
 
31
#include "liflines.h"
 
32
#include "llinesi.h"
 
33
#include "menuitem.h"
 
34
#include "screen.h"
 
35
#include "screeni.h"
 
36
 
 
37
/*********************************************
 
38
 * external/imported variables
 
39
 *********************************************/
 
40
 
 
41
extern STRING qSmn_unkcmd;
 
42
 
 
43
/*********************************************
 
44
 * local function prototypes
 
45
 *********************************************/
 
46
 
 
47
/* alphabetical */
 
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);
 
51
 
 
52
/*********************************************
 
53
 * local variables
 
54
 *********************************************/
 
55
 
 
56
static int ui_time_elapsed = 0; /* total time waiting for user input */
 
57
 
 
58
/*********************************************
 
59
 * local & exported function definitions
 
60
 * body of module
 
61
 *********************************************/
 
62
 
 
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
 *=============================*/
 
68
INT
 
69
interact_choice_string (UIWINDOW uiwin, STRING str)
 
70
{
 
71
        return interact_worker(uiwin, str, -1);
 
72
}
 
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
 *=============================*/
 
79
INT
 
80
interact_screen_menu (UIWINDOW uiwin, INT screen)
 
81
{
 
82
        return interact_worker(uiwin, NULL, screen);
 
83
}
 
84
/*===============================
 
85
 * interact_worker -- Interact with user
 
86
 * This is just for browse screens (witness argument "screen")
 
87
 *=============================*/
 
88
static INT
 
89
interact_worker (UIWINDOW uiwin, STRING str, INT screen)
 
90
{
 
91
        char buffer[4]; /* 3 char cmds max */
 
92
        INT offset=0;
 
93
        INT cmdnum;
 
94
        INT c, i, n = str ? strlen(str) : 0;
 
95
 
 
96
        /* Menu Loop */
 
97
        while (TRUE)
 
98
        {
 
99
                INT time_start=time(NULL);
 
100
                crmode();
 
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';
 
105
                nocrmode();
 
106
                /* after they chose off the menu, we wipe any
 
107
                status message still lingering from before they chose */
 
108
                clear_status_display();
 
109
                /*
 
110
                return hardware and control keys
 
111
                in case caller handles them
 
112
                */
 
113
                if (c<0x20) {
 
114
                        return translate_control_key(c);
 
115
                }
 
116
                if (has_key(c)) {
 
117
                        return translate_hdware_key(c);
 
118
                }
 
119
                if (str) { /* traditional: list of choice letters */
 
120
                        for (i = 0; i < n; i++) {
 
121
                                if (c == (uchar)str[i]) return c;
 
122
                        }
 
123
                } else { /* new menus (in menuitem.c) */
 
124
                        if (offset < (INT)sizeof(buffer)-1) {
 
125
                                buffer[offset] = c;
 
126
                                buffer[offset+1] = 0;
 
127
                                offset++;
 
128
                        } else {
 
129
                                buffer[0] = c;
 
130
                                buffer[1] = 0;
 
131
                                offset = 1;
 
132
                        }
 
133
 
 
134
                        /* Get Menu Command */
 
135
                        cmdnum = menuset_check_cmd(get_screen_menuset(screen), buffer);
 
136
 
 
137
                        /* Act On Menu Command */
 
138
                        if (cmdnum != CMD_NONE && cmdnum != CMD_PARTIAL) {
 
139
                                return cmdnum;
 
140
                        }
 
141
                        if (cmdnum != CMD_PARTIAL) {
 
142
                                msg_error(_(qSmn_unkcmd));
 
143
                                offset = 0;
 
144
                        }
 
145
                }
 
146
                /* choice was no good, we loop & wait for another choice */
 
147
        }
 
148
}
 
149
/*===============================
 
150
 * translate_hdware_key -- 
 
151
 *  translate curses keycode into menuitem.h constant
 
152
 *=============================*/
 
153
struct hdkeycvt { int key; int cmd; };
 
154
static INT
 
155
translate_hdware_key (INT c)
 
156
{
 
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 }
 
168
        };
 
169
        int i;
 
170
        for (i=0; i<ARRSIZE(hdkey); ++i) {
 
171
                if (c == hdkey[i].key)
 
172
                        return hdkey[i].cmd;
 
173
        }
 
174
        return CMD_NONE;
 
175
}
 
176
/*===============================
 
177
 * translate_control_key -- 
 
178
 *  translate control keys into menuitem.h constant
 
179
 *=============================*/
 
180
static INT
 
181
translate_control_key (INT c)
 
182
{
 
183
        static struct hdkeycvt hdkey[] = {
 
184
                { '\r', CMD_KY_ENTER } /* Win32 */
 
185
                , { '\n', CMD_KY_ENTER } /* UNIX */
 
186
        };
 
187
        int i;
 
188
        for (i=0; i<ARRSIZE(hdkey); ++i) {
 
189
                if (c == hdkey[i].key)
 
190
                        return hdkey[i].cmd;
 
191
        }
 
192
        return CMD_NONE;
 
193
}
 
194
/*============================
 
195
 * get_uitime -- return cumulative elapsed time waiting 
 
196
 *  for user input (since start of program)
 
197
 *==========================*/
 
198
int
 
199
get_uitime (void)
 
200
{
 
201
        return ui_time_elapsed;
 
202
}