1*b618a1eeSThomas Cort /* $NetBSD: driver.c,v 1.9 2003/03/09 01:08:48 lukem Exp $ */
2*b618a1eeSThomas Cort
3*b618a1eeSThomas Cort /*-
4*b618a1eeSThomas Cort * Copyright (c) 1998-1999 Brett Lymn (blymn@baea.com.au, brett_lymn@yahoo.com.au)
5*b618a1eeSThomas Cort * All rights reserved.
6*b618a1eeSThomas Cort *
7*b618a1eeSThomas Cort * Redistribution and use in source and binary forms, with or without
8*b618a1eeSThomas Cort * modification, are permitted provided that the following conditions
9*b618a1eeSThomas Cort * are met:
10*b618a1eeSThomas Cort * 1. Redistributions of source code must retain the above copyright
11*b618a1eeSThomas Cort * notice, this list of conditions and the following disclaimer.
12*b618a1eeSThomas Cort * 2. The name of the author may not be used to endorse or promote products
13*b618a1eeSThomas Cort * derived from this software without specific prior written permission
14*b618a1eeSThomas Cort *
15*b618a1eeSThomas Cort * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*b618a1eeSThomas Cort * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*b618a1eeSThomas Cort * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*b618a1eeSThomas Cort * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*b618a1eeSThomas Cort * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*b618a1eeSThomas Cort * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*b618a1eeSThomas Cort * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*b618a1eeSThomas Cort * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*b618a1eeSThomas Cort * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*b618a1eeSThomas Cort * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*b618a1eeSThomas Cort *
26*b618a1eeSThomas Cort *
27*b618a1eeSThomas Cort */
28*b618a1eeSThomas Cort
29*b618a1eeSThomas Cort #include <sys/cdefs.h>
30*b618a1eeSThomas Cort __RCSID("$NetBSD: driver.c,v 1.9 2003/03/09 01:08:48 lukem Exp $");
31*b618a1eeSThomas Cort
32*b618a1eeSThomas Cort #include <menu.h>
33*b618a1eeSThomas Cort #include <ctype.h>
34*b618a1eeSThomas Cort #include <stdlib.h>
35*b618a1eeSThomas Cort #include "internals.h"
36*b618a1eeSThomas Cort
37*b618a1eeSThomas Cort /*
38*b618a1eeSThomas Cort * The guts of the menu library. This function processes the character
39*b618a1eeSThomas Cort * in c and performs actions based on the value of the character. If the
40*b618a1eeSThomas Cort * character is a normal one then the driver attempts to match the character
41*b618a1eeSThomas Cort * against the items. If the character is a recognised request then the
42*b618a1eeSThomas Cort * request is processed by the driver, if the character is not a recognised
43*b618a1eeSThomas Cort * request and is not printable then it assumed to be a user defined command.
44*b618a1eeSThomas Cort */
45*b618a1eeSThomas Cort int
menu_driver(MENU * menu,int c)46*b618a1eeSThomas Cort menu_driver(MENU *menu, int c)
47*b618a1eeSThomas Cort {
48*b618a1eeSThomas Cort int drv_top_row, drv_scroll, i, it, status = E_OK;
49*b618a1eeSThomas Cort ITEM *drv_new_item;
50*b618a1eeSThomas Cort
51*b618a1eeSThomas Cort i = 0;
52*b618a1eeSThomas Cort
53*b618a1eeSThomas Cort if (menu == NULL)
54*b618a1eeSThomas Cort return E_BAD_ARGUMENT;
55*b618a1eeSThomas Cort if (menu->posted == 0)
56*b618a1eeSThomas Cort return E_NOT_POSTED;
57*b618a1eeSThomas Cort if (menu->items == NULL)
58*b618a1eeSThomas Cort return E_NOT_CONNECTED;
59*b618a1eeSThomas Cort if (*menu->items == NULL)
60*b618a1eeSThomas Cort return E_NOT_CONNECTED;
61*b618a1eeSThomas Cort if (menu->in_init == 1)
62*b618a1eeSThomas Cort return E_BAD_STATE;
63*b618a1eeSThomas Cort
64*b618a1eeSThomas Cort /* this one should never happen but just in case.... */
65*b618a1eeSThomas Cort if (menu->items[menu->cur_item] == NULL)
66*b618a1eeSThomas Cort return E_SYSTEM_ERROR;
67*b618a1eeSThomas Cort
68*b618a1eeSThomas Cort drv_new_item = menu->items[menu->cur_item];
69*b618a1eeSThomas Cort it = menu->cur_item;
70*b618a1eeSThomas Cort drv_top_row = menu->top_row;
71*b618a1eeSThomas Cort
72*b618a1eeSThomas Cort if ((c > REQ_BASE_NUM) && (c <= MAX_COMMAND)) {
73*b618a1eeSThomas Cort /* is a known driver request - first check if the pattern
74*b618a1eeSThomas Cort * buffer needs to be cleared, we do this on non-search
75*b618a1eeSThomas Cort * type requests.
76*b618a1eeSThomas Cort */
77*b618a1eeSThomas Cort if (! ((c == REQ_BACK_PATTERN) || (c == REQ_NEXT_MATCH) ||
78*b618a1eeSThomas Cort (c == REQ_PREV_MATCH))) {
79*b618a1eeSThomas Cort if ((c == REQ_CLEAR_PATTERN)
80*b618a1eeSThomas Cort && (menu->pattern == NULL))
81*b618a1eeSThomas Cort return E_REQUEST_DENIED;
82*b618a1eeSThomas Cort free(menu->pattern);
83*b618a1eeSThomas Cort menu->pattern = NULL;
84*b618a1eeSThomas Cort menu->plen = 0;
85*b618a1eeSThomas Cort menu->match_len = 0;
86*b618a1eeSThomas Cort }
87*b618a1eeSThomas Cort
88*b618a1eeSThomas Cort switch (c) {
89*b618a1eeSThomas Cort case REQ_LEFT_ITEM:
90*b618a1eeSThomas Cort drv_new_item = drv_new_item->left;
91*b618a1eeSThomas Cort break;
92*b618a1eeSThomas Cort case REQ_RIGHT_ITEM:
93*b618a1eeSThomas Cort drv_new_item = drv_new_item->right;
94*b618a1eeSThomas Cort break;
95*b618a1eeSThomas Cort case REQ_UP_ITEM:
96*b618a1eeSThomas Cort drv_new_item = drv_new_item->up;
97*b618a1eeSThomas Cort break;
98*b618a1eeSThomas Cort case REQ_DOWN_ITEM:
99*b618a1eeSThomas Cort drv_new_item = drv_new_item->down;
100*b618a1eeSThomas Cort break;
101*b618a1eeSThomas Cort case REQ_SCR_ULINE:
102*b618a1eeSThomas Cort if (drv_top_row == 0)
103*b618a1eeSThomas Cort return E_REQUEST_DENIED;
104*b618a1eeSThomas Cort drv_top_row--;
105*b618a1eeSThomas Cort drv_new_item = drv_new_item->up;
106*b618a1eeSThomas Cort break;
107*b618a1eeSThomas Cort case REQ_SCR_DLINE:
108*b618a1eeSThomas Cort drv_top_row++;
109*b618a1eeSThomas Cort if ((drv_top_row + menu->rows - 1)> menu->item_rows)
110*b618a1eeSThomas Cort return E_REQUEST_DENIED;
111*b618a1eeSThomas Cort drv_new_item = drv_new_item->down;
112*b618a1eeSThomas Cort break;
113*b618a1eeSThomas Cort case REQ_SCR_DPAGE:
114*b618a1eeSThomas Cort drv_scroll = menu->item_rows - menu->rows
115*b618a1eeSThomas Cort - menu->top_row;
116*b618a1eeSThomas Cort if (drv_scroll > menu->rows) {
117*b618a1eeSThomas Cort drv_scroll = menu->rows;
118*b618a1eeSThomas Cort }
119*b618a1eeSThomas Cort
120*b618a1eeSThomas Cort if (drv_scroll <= 0) {
121*b618a1eeSThomas Cort return E_REQUEST_DENIED;
122*b618a1eeSThomas Cort } else {
123*b618a1eeSThomas Cort drv_top_row += drv_scroll;
124*b618a1eeSThomas Cort while (drv_scroll-- > 0)
125*b618a1eeSThomas Cort drv_new_item = drv_new_item->down;
126*b618a1eeSThomas Cort }
127*b618a1eeSThomas Cort break;
128*b618a1eeSThomas Cort case REQ_SCR_UPAGE:
129*b618a1eeSThomas Cort if (menu->rows < menu->top_row) {
130*b618a1eeSThomas Cort drv_scroll = menu->rows;
131*b618a1eeSThomas Cort } else {
132*b618a1eeSThomas Cort drv_scroll = menu->top_row;
133*b618a1eeSThomas Cort }
134*b618a1eeSThomas Cort if (drv_scroll == 0)
135*b618a1eeSThomas Cort return E_REQUEST_DENIED;
136*b618a1eeSThomas Cort
137*b618a1eeSThomas Cort drv_top_row -= drv_scroll;
138*b618a1eeSThomas Cort while (drv_scroll-- > 0)
139*b618a1eeSThomas Cort drv_new_item = drv_new_item->up;
140*b618a1eeSThomas Cort break;
141*b618a1eeSThomas Cort case REQ_FIRST_ITEM:
142*b618a1eeSThomas Cort drv_new_item = menu->items[0];
143*b618a1eeSThomas Cort break;
144*b618a1eeSThomas Cort case REQ_LAST_ITEM:
145*b618a1eeSThomas Cort drv_new_item = menu->items[menu->item_count - 1];
146*b618a1eeSThomas Cort break;
147*b618a1eeSThomas Cort case REQ_NEXT_ITEM:
148*b618a1eeSThomas Cort if ((menu->cur_item + 1) >= menu->item_count) {
149*b618a1eeSThomas Cort if ((menu->opts & O_NONCYCLIC)
150*b618a1eeSThomas Cort == O_NONCYCLIC) {
151*b618a1eeSThomas Cort return E_REQUEST_DENIED;
152*b618a1eeSThomas Cort } else {
153*b618a1eeSThomas Cort drv_new_item = menu->items[0];
154*b618a1eeSThomas Cort }
155*b618a1eeSThomas Cort } else {
156*b618a1eeSThomas Cort drv_new_item =
157*b618a1eeSThomas Cort menu->items[menu->cur_item + 1];
158*b618a1eeSThomas Cort }
159*b618a1eeSThomas Cort break;
160*b618a1eeSThomas Cort case REQ_PREV_ITEM:
161*b618a1eeSThomas Cort if (menu->cur_item == 0) {
162*b618a1eeSThomas Cort if ((menu->opts & O_NONCYCLIC)
163*b618a1eeSThomas Cort == O_NONCYCLIC) {
164*b618a1eeSThomas Cort return E_REQUEST_DENIED;
165*b618a1eeSThomas Cort } else {
166*b618a1eeSThomas Cort drv_new_item = menu->items[
167*b618a1eeSThomas Cort menu->item_count - 1];
168*b618a1eeSThomas Cort }
169*b618a1eeSThomas Cort } else {
170*b618a1eeSThomas Cort drv_new_item =
171*b618a1eeSThomas Cort menu->items[menu->cur_item - 1];
172*b618a1eeSThomas Cort }
173*b618a1eeSThomas Cort break;
174*b618a1eeSThomas Cort case REQ_TOGGLE_ITEM:
175*b618a1eeSThomas Cort if ((menu->opts & (O_RADIO | O_ONEVALUE)) != 0) {
176*b618a1eeSThomas Cort if ((menu->opts & O_RADIO) == O_RADIO) {
177*b618a1eeSThomas Cort if ((drv_new_item->opts & O_SELECTABLE)
178*b618a1eeSThomas Cort != O_SELECTABLE)
179*b618a1eeSThomas Cort return E_NOT_SELECTABLE;
180*b618a1eeSThomas Cort
181*b618a1eeSThomas Cort /* don't deselect selected item */
182*b618a1eeSThomas Cort if (drv_new_item->selected == 1)
183*b618a1eeSThomas Cort return E_REQUEST_DENIED;
184*b618a1eeSThomas Cort
185*b618a1eeSThomas Cort /* deselect all items */
186*b618a1eeSThomas Cort for (i = 0; i < menu->item_count; i++) {
187*b618a1eeSThomas Cort if ((menu->items[i]->selected) &&
188*b618a1eeSThomas Cort (drv_new_item->index != i)) {
189*b618a1eeSThomas Cort menu->items[i]->selected ^= 1;
190*b618a1eeSThomas Cort _menui_draw_item(menu,
191*b618a1eeSThomas Cort menu->items[i]->index);
192*b618a1eeSThomas Cort }
193*b618a1eeSThomas Cort }
194*b618a1eeSThomas Cort
195*b618a1eeSThomas Cort /* turn on selected item */
196*b618a1eeSThomas Cort drv_new_item->selected ^= 1;
197*b618a1eeSThomas Cort _menui_draw_item(menu, drv_new_item->index);
198*b618a1eeSThomas Cort } else {
199*b618a1eeSThomas Cort return E_REQUEST_DENIED;
200*b618a1eeSThomas Cort }
201*b618a1eeSThomas Cort } else {
202*b618a1eeSThomas Cort if ((drv_new_item->opts
203*b618a1eeSThomas Cort & O_SELECTABLE) == O_SELECTABLE) {
204*b618a1eeSThomas Cort /* toggle select flag */
205*b618a1eeSThomas Cort drv_new_item->selected ^= 1;
206*b618a1eeSThomas Cort /* update item in menu */
207*b618a1eeSThomas Cort _menui_draw_item(menu,
208*b618a1eeSThomas Cort drv_new_item->index);
209*b618a1eeSThomas Cort } else {
210*b618a1eeSThomas Cort return E_NOT_SELECTABLE;
211*b618a1eeSThomas Cort }
212*b618a1eeSThomas Cort }
213*b618a1eeSThomas Cort break;
214*b618a1eeSThomas Cort case REQ_CLEAR_PATTERN:
215*b618a1eeSThomas Cort /* this action is taken before the
216*b618a1eeSThomas Cort case statement */
217*b618a1eeSThomas Cort break;
218*b618a1eeSThomas Cort case REQ_BACK_PATTERN:
219*b618a1eeSThomas Cort if (menu->pattern == NULL)
220*b618a1eeSThomas Cort return E_REQUEST_DENIED;
221*b618a1eeSThomas Cort
222*b618a1eeSThomas Cort if (menu->plen == 0)
223*b618a1eeSThomas Cort return E_REQUEST_DENIED;
224*b618a1eeSThomas Cort menu->pattern[menu->plen--] = '\0';
225*b618a1eeSThomas Cort break;
226*b618a1eeSThomas Cort case REQ_NEXT_MATCH:
227*b618a1eeSThomas Cort if (menu->pattern == NULL)
228*b618a1eeSThomas Cort return E_REQUEST_DENIED;
229*b618a1eeSThomas Cort
230*b618a1eeSThomas Cort status = _menui_match_pattern(menu, 0,
231*b618a1eeSThomas Cort MATCH_NEXT_FORWARD,
232*b618a1eeSThomas Cort &it);
233*b618a1eeSThomas Cort drv_new_item = menu->items[it];
234*b618a1eeSThomas Cort break;
235*b618a1eeSThomas Cort case REQ_PREV_MATCH:
236*b618a1eeSThomas Cort if (menu->pattern == NULL)
237*b618a1eeSThomas Cort return E_REQUEST_DENIED;
238*b618a1eeSThomas Cort
239*b618a1eeSThomas Cort status = _menui_match_pattern(menu, 0,
240*b618a1eeSThomas Cort MATCH_NEXT_REVERSE,
241*b618a1eeSThomas Cort &it);
242*b618a1eeSThomas Cort drv_new_item = menu->items[it];
243*b618a1eeSThomas Cort break;
244*b618a1eeSThomas Cort }
245*b618a1eeSThomas Cort } else if (c > MAX_COMMAND) {
246*b618a1eeSThomas Cort /* must be a user command */
247*b618a1eeSThomas Cort return E_UNKNOWN_COMMAND;
248*b618a1eeSThomas Cort } else if (isprint((unsigned char) c)) {
249*b618a1eeSThomas Cort /* otherwise search items for the character. */
250*b618a1eeSThomas Cort status = _menui_match_pattern(menu, (unsigned char) c,
251*b618a1eeSThomas Cort MATCH_FORWARD, &it);
252*b618a1eeSThomas Cort drv_new_item = menu->items[it];
253*b618a1eeSThomas Cort
254*b618a1eeSThomas Cort /* update the position of the cursor if we are doing
255*b618a1eeSThomas Cort * show match and the current item has not changed. If
256*b618a1eeSThomas Cort * we don't do this here it won't get done since the
257*b618a1eeSThomas Cort * display will not be updated due to the current item
258*b618a1eeSThomas Cort * not changing.
259*b618a1eeSThomas Cort */
260*b618a1eeSThomas Cort if ((drv_new_item->index == menu->cur_item)
261*b618a1eeSThomas Cort && ((menu->opts & O_SHOWMATCH) == O_SHOWMATCH)) {
262*b618a1eeSThomas Cort pos_menu_cursor(menu);
263*b618a1eeSThomas Cort }
264*b618a1eeSThomas Cort
265*b618a1eeSThomas Cort
266*b618a1eeSThomas Cort } else {
267*b618a1eeSThomas Cort /* bad character */
268*b618a1eeSThomas Cort return E_BAD_ARGUMENT;
269*b618a1eeSThomas Cort }
270*b618a1eeSThomas Cort
271*b618a1eeSThomas Cort if (drv_new_item == NULL)
272*b618a1eeSThomas Cort return E_REQUEST_DENIED;
273*b618a1eeSThomas Cort
274*b618a1eeSThomas Cort if (drv_new_item->row < drv_top_row) drv_top_row = drv_new_item->row;
275*b618a1eeSThomas Cort if (drv_new_item->row >= (drv_top_row + menu->rows))
276*b618a1eeSThomas Cort drv_top_row = drv_new_item->row - menu->rows + 1;
277*b618a1eeSThomas Cort
278*b618a1eeSThomas Cort if ((drv_new_item->index != menu->cur_item)
279*b618a1eeSThomas Cort || (drv_top_row != menu->top_row))
280*b618a1eeSThomas Cort _menui_goto_item(menu, drv_new_item, drv_top_row);
281*b618a1eeSThomas Cort
282*b618a1eeSThomas Cort return status;
283*b618a1eeSThomas Cort }
284*b618a1eeSThomas Cort
285*b618a1eeSThomas Cort
286*b618a1eeSThomas Cort
287*b618a1eeSThomas Cort
288*b618a1eeSThomas Cort
289