xref: /onnv-gate/usr/src/lib/libcurses/screen/setkeymap.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*LINTLIBRARY*/
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include	<sys/types.h>
45*0Sstevel@tonic-gate #include	<stdlib.h>
46*0Sstevel@tonic-gate #include	<string.h>
47*0Sstevel@tonic-gate #include	"curses_inc.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate static	short	keycodes[] = {
50*0Sstevel@tonic-gate 		    KEY_BACKSPACE,
51*0Sstevel@tonic-gate 		    KEY_CATAB,
52*0Sstevel@tonic-gate 		    KEY_CLEAR,
53*0Sstevel@tonic-gate 		    KEY_CTAB,
54*0Sstevel@tonic-gate 		    KEY_DC,
55*0Sstevel@tonic-gate 		    KEY_DL,
56*0Sstevel@tonic-gate 		    KEY_DOWN,
57*0Sstevel@tonic-gate 		    KEY_EIC,
58*0Sstevel@tonic-gate 		    KEY_EOL,
59*0Sstevel@tonic-gate 		    KEY_EOS,
60*0Sstevel@tonic-gate 		    KEY_F(0),
61*0Sstevel@tonic-gate 		    KEY_F(1),
62*0Sstevel@tonic-gate 		    KEY_F(10),
63*0Sstevel@tonic-gate 		    KEY_F(2),
64*0Sstevel@tonic-gate 		    KEY_F(3),
65*0Sstevel@tonic-gate 		    KEY_F(4),
66*0Sstevel@tonic-gate 		    KEY_F(5),
67*0Sstevel@tonic-gate 		    KEY_F(6),
68*0Sstevel@tonic-gate 		    KEY_F(7),
69*0Sstevel@tonic-gate 		    KEY_F(8),
70*0Sstevel@tonic-gate 		    KEY_F(9),
71*0Sstevel@tonic-gate 		    KEY_HOME,
72*0Sstevel@tonic-gate 		    KEY_IC,
73*0Sstevel@tonic-gate 		    KEY_IL,
74*0Sstevel@tonic-gate 		    KEY_LEFT,
75*0Sstevel@tonic-gate 		    KEY_LL,
76*0Sstevel@tonic-gate 		    KEY_NPAGE,
77*0Sstevel@tonic-gate 		    KEY_PPAGE,
78*0Sstevel@tonic-gate 		    KEY_RIGHT,
79*0Sstevel@tonic-gate 		    KEY_SF,
80*0Sstevel@tonic-gate 		    KEY_SR,
81*0Sstevel@tonic-gate 		    KEY_STAB,
82*0Sstevel@tonic-gate 		    KEY_UP,
83*0Sstevel@tonic-gate 		    KEY_A1,
84*0Sstevel@tonic-gate 		    KEY_A3,
85*0Sstevel@tonic-gate 		    KEY_B2,
86*0Sstevel@tonic-gate 		    KEY_C1,
87*0Sstevel@tonic-gate 		    KEY_C3,
88*0Sstevel@tonic-gate 		    KEY_BTAB,
89*0Sstevel@tonic-gate 		    KEY_BEG,
90*0Sstevel@tonic-gate 		    KEY_CANCEL,
91*0Sstevel@tonic-gate 		    KEY_CLOSE,
92*0Sstevel@tonic-gate 		    KEY_COMMAND,
93*0Sstevel@tonic-gate 		    KEY_COPY,
94*0Sstevel@tonic-gate 		    KEY_CREATE,
95*0Sstevel@tonic-gate 		    KEY_END,
96*0Sstevel@tonic-gate 		    KEY_ENTER,
97*0Sstevel@tonic-gate 		    KEY_EXIT,
98*0Sstevel@tonic-gate 		    KEY_FIND,
99*0Sstevel@tonic-gate 		    KEY_HELP,
100*0Sstevel@tonic-gate 		    KEY_MARK,
101*0Sstevel@tonic-gate 		    KEY_MESSAGE,
102*0Sstevel@tonic-gate 		    KEY_MOVE,
103*0Sstevel@tonic-gate 		    KEY_NEXT,
104*0Sstevel@tonic-gate 		    KEY_OPEN,
105*0Sstevel@tonic-gate 		    KEY_OPTIONS,
106*0Sstevel@tonic-gate 		    KEY_PREVIOUS,
107*0Sstevel@tonic-gate 		    KEY_PRINT,
108*0Sstevel@tonic-gate 		    KEY_REDO,
109*0Sstevel@tonic-gate 		    KEY_REFERENCE,
110*0Sstevel@tonic-gate 		    KEY_REFRESH,
111*0Sstevel@tonic-gate 		    KEY_REPLACE,
112*0Sstevel@tonic-gate 		    KEY_RESTART,
113*0Sstevel@tonic-gate 		    KEY_RESUME,
114*0Sstevel@tonic-gate 		    KEY_SAVE,
115*0Sstevel@tonic-gate 		    KEY_SUSPEND,
116*0Sstevel@tonic-gate 		    KEY_UNDO,
117*0Sstevel@tonic-gate 		    KEY_SBEG,
118*0Sstevel@tonic-gate 		    KEY_SCANCEL,
119*0Sstevel@tonic-gate 		    KEY_SCOMMAND,
120*0Sstevel@tonic-gate 		    KEY_SCOPY,
121*0Sstevel@tonic-gate 		    KEY_SCREATE,
122*0Sstevel@tonic-gate 		    KEY_SDC,
123*0Sstevel@tonic-gate 		    KEY_SDL,
124*0Sstevel@tonic-gate 		    KEY_SELECT,
125*0Sstevel@tonic-gate 		    KEY_SEND,
126*0Sstevel@tonic-gate 		    KEY_SEOL,
127*0Sstevel@tonic-gate 		    KEY_SEXIT,
128*0Sstevel@tonic-gate 		    KEY_SFIND,
129*0Sstevel@tonic-gate 		    KEY_SHELP,
130*0Sstevel@tonic-gate 		    KEY_SHOME,
131*0Sstevel@tonic-gate 		    KEY_SIC,
132*0Sstevel@tonic-gate 		    KEY_SLEFT,
133*0Sstevel@tonic-gate 		    KEY_SMESSAGE,
134*0Sstevel@tonic-gate 		    KEY_SMOVE,
135*0Sstevel@tonic-gate 		    KEY_SNEXT,
136*0Sstevel@tonic-gate 		    KEY_SOPTIONS,
137*0Sstevel@tonic-gate 		    KEY_SPREVIOUS,
138*0Sstevel@tonic-gate 		    KEY_SPRINT,
139*0Sstevel@tonic-gate 		    KEY_SREDO,
140*0Sstevel@tonic-gate 		    KEY_SREPLACE,
141*0Sstevel@tonic-gate 		    KEY_SRIGHT,
142*0Sstevel@tonic-gate 		    KEY_SRSUME,
143*0Sstevel@tonic-gate 		    KEY_SSAVE,
144*0Sstevel@tonic-gate 		    KEY_SSUSPEND,
145*0Sstevel@tonic-gate 		    KEY_SUNDO,
146*0Sstevel@tonic-gate 		    KEY_MOUSE
147*0Sstevel@tonic-gate 		};
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate static	_KEY_MAP	*p;
150*0Sstevel@tonic-gate static	bool		*funckey;
151*0Sstevel@tonic-gate static	short		*codeptr;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate static	void
_laddone(char * txt)154*0Sstevel@tonic-gate _laddone(char *txt)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	p->_sends = (txt);
157*0Sstevel@tonic-gate 	p->_keyval = *codeptr;
158*0Sstevel@tonic-gate 	funckey[(unsigned char)(txt)[0]] |= _KEY;
159*0Sstevel@tonic-gate 	p++;
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /* Map text into num, updating the map structure p. */
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate static	void
_keyfunc(char ** keyptr,char ** lastkey)165*0Sstevel@tonic-gate _keyfunc(char **keyptr, char **lastkey)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	for (; keyptr <= lastkey; keyptr++, codeptr++)
168*0Sstevel@tonic-gate 		if (*keyptr) {
169*0Sstevel@tonic-gate 			p->_sends = (*keyptr);
170*0Sstevel@tonic-gate 			p->_keyval = *codeptr;
171*0Sstevel@tonic-gate 			funckey[(unsigned char)(*keyptr)[0]] |= _KEY;
172*0Sstevel@tonic-gate 			p++;
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate /* Map text into num, updating the map structure p. */
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate static	void
_keyfunc2(char ** keyptr,char ** lastkey)179*0Sstevel@tonic-gate _keyfunc2(char **keyptr, char **lastkey)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate 	short code_value = KEY_F(11);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	for (; *keyptr && keyptr <= lastkey; keyptr++, code_value++) {
184*0Sstevel@tonic-gate 		p->_sends = *keyptr;
185*0Sstevel@tonic-gate 		p->_keyval = (short) code_value;
186*0Sstevel@tonic-gate 		funckey[(unsigned char)*keyptr[0]] |= _KEY;
187*0Sstevel@tonic-gate 		p++;
188*0Sstevel@tonic-gate 	}
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate int
setkeymap(void)192*0Sstevel@tonic-gate setkeymap(void)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate 	_KEY_MAP	keymap[((sizeof (keycodes) / sizeof (short)) +
195*0Sstevel@tonic-gate 			    ((KEY_F(63) - KEY_F(11)) + 1))], **key_ptrs;
196*0Sstevel@tonic-gate 	short		numkeys;
197*0Sstevel@tonic-gate 	int		numbytes, key_size = cur_term->_ksz;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (cur_term->internal_keys != NULL)
200*0Sstevel@tonic-gate 		return (ERR);
201*0Sstevel@tonic-gate 	p = keymap;
202*0Sstevel@tonic-gate 	codeptr = keycodes;
203*0Sstevel@tonic-gate 	funckey = cur_term->funckeystarter;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	/* If backspace key sends \b, don't map it. */
206*0Sstevel@tonic-gate 	if (key_backspace && strcmp(key_backspace, "\b"))
207*0Sstevel@tonic-gate 		_laddone(key_backspace);
208*0Sstevel@tonic-gate 	codeptr++;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	_keyfunc(&key_catab, &key_dl);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	/* If down arrow key sends \n, don't map it. */
213*0Sstevel@tonic-gate 	if (key_down && strcmp(key_down, "\n"))
214*0Sstevel@tonic-gate 		_laddone(key_down);
215*0Sstevel@tonic-gate 	codeptr++;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	_keyfunc(&key_eic, &key_il);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	/* If left arrow key sends \b, don't map it. */
220*0Sstevel@tonic-gate 	if (key_left && strcmp(key_left, "\b"))
221*0Sstevel@tonic-gate 		_laddone(key_left);
222*0Sstevel@tonic-gate 	codeptr++;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	_keyfunc(&key_ll, &key_up);
225*0Sstevel@tonic-gate 	_keyfunc(&key_a1, &key_c3);
226*0Sstevel@tonic-gate 	_keyfunc(&key_btab, &key_btab);
227*0Sstevel@tonic-gate 	_keyfunc(&key_beg, &key_sundo);
228*0Sstevel@tonic-gate 	_keyfunc2(&key_f11, &key_f63);
229*0Sstevel@tonic-gate 	_keyfunc(&key_mouse, &key_mouse);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	/*
232*0Sstevel@tonic-gate 	 * malloc returns the address of a list of pointers to
233*0Sstevel@tonic-gate 	 * (_KEY_MAP *) structures
234*0Sstevel@tonic-gate 	 */
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if ((key_ptrs = (_KEY_MAP **)
237*0Sstevel@tonic-gate 	    /* LINTED */
238*0Sstevel@tonic-gate 	    malloc((key_size + (numkeys = (short)(p - keymap))) *
239*0Sstevel@tonic-gate 	    sizeof (_KEY_MAP *))) == NULL) {
240*0Sstevel@tonic-gate 		goto out;
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/*
244*0Sstevel@tonic-gate 	 * Number of bytes needed is the number of structures times their size
245*0Sstevel@tonic-gate 	 * malloc room for our array of _KEY_MAP structures
246*0Sstevel@tonic-gate 	 */
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	if ((p = (_KEY_MAP *) malloc((unsigned)
249*0Sstevel@tonic-gate 	    /* LINTED */
250*0Sstevel@tonic-gate 	    (numbytes = (int)(sizeof (_KEY_MAP) * numkeys)))) == NULL) {
251*0Sstevel@tonic-gate 		/* Can't do it, free list of pointers, indicate */
252*0Sstevel@tonic-gate 		/* error upon return. */
253*0Sstevel@tonic-gate 		free((char *) key_ptrs);
254*0Sstevel@tonic-gate out:
255*0Sstevel@tonic-gate 		term_errno = TERM_BAD_MALLOC;
256*0Sstevel@tonic-gate #ifdef	DEBUG
257*0Sstevel@tonic-gate 		strcpy(term_parm_err, "setkeymap");
258*0Sstevel@tonic-gate 		termerr();
259*0Sstevel@tonic-gate #endif	/* DEBUG */
260*0Sstevel@tonic-gate 		return (ERR);
261*0Sstevel@tonic-gate 	}
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	if (key_size != 0) {
264*0Sstevel@tonic-gate 		(void) memcpy((char *) &(key_ptrs[numkeys]),
265*0Sstevel@tonic-gate 		    (char *) cur_term->_keys, (key_size *
266*0Sstevel@tonic-gate 		    sizeof (_KEY_MAP *)));
267*0Sstevel@tonic-gate 		free(cur_term->_keys);
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 	(void) memcpy((char *) (cur_term->internal_keys = p),
270*0Sstevel@tonic-gate 	    (char *) keymap, numbytes);
271*0Sstevel@tonic-gate 	cur_term->_keys = key_ptrs;
272*0Sstevel@tonic-gate 	cur_term->_ksz += numkeys;
273*0Sstevel@tonic-gate 	/*
274*0Sstevel@tonic-gate 	 * Reset _lastkey_ordered to -1 since we put the keys read in
275*0Sstevel@tonic-gate 	 * from terminfo at the beginning of the keys table.
276*0Sstevel@tonic-gate 	 */
277*0Sstevel@tonic-gate 	cur_term->_lastkey_ordered = -1;
278*0Sstevel@tonic-gate 	cur_term->_lastmacro_ordered += numkeys;
279*0Sstevel@tonic-gate 	cur_term->_first_macro += numkeys;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	/* Initialize our pointers to the structures */
282*0Sstevel@tonic-gate 	while (numkeys--)
283*0Sstevel@tonic-gate 		*key_ptrs++ = p++;
284*0Sstevel@tonic-gate #ifdef	DEBUG
285*0Sstevel@tonic-gate 	if (outf)
286*0Sstevel@tonic-gate 		fprintf(outf, "return key structure %x, ending at %x\n",
287*0Sstevel@tonic-gate 		    keymap, p);
288*0Sstevel@tonic-gate #endif	/* DEBUG */
289*0Sstevel@tonic-gate 	return (OK);
290*0Sstevel@tonic-gate }
291