1 /****************************************************************************
2  * Copyright (c) 1999-2006,2008 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies 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 included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 #include <curses.priv.h>
30 
31 #include <term.h>
32 /* keypad_xmit, keypad_local, meta_on, meta_off */
33 /* cursor_visible,cursor_normal,cursor_invisible */
34 
35 #include <tic.h>		/* struct tinfo_fkeys */
36 
37 #include <term_entry.h>
38 
39 MODULE_ID("$Id: init_keytry.c,v 1.12 2008/05/24 21:44:51 tom Exp $")
40 
41 /*
42 **      _nc_init_keytry()
43 **
44 **      Construct the try for the current terminal's keypad keys.
45 **
46 */
47 
48 /*
49  * Internal entrypoints use SCREEN* parameter to obtain capabilities rather
50  * than cur_term.
51  */
52 #undef CUR
53 #define CUR (sp->_term)->type.
54 
55 #if	BROKEN_LINKER
56 #undef	_nc_tinfo_fkeys
57 #endif
58 
59 /* LINT_PREPRO
60 #if 0*/
61 #include <init_keytry.h>
62 /* LINT_PREPRO
63 #endif*/
64 
65 #if	BROKEN_LINKER
66 const struct tinfo_fkeys *
67 _nc_tinfo_fkeysf(void)
68 {
69     return _nc_tinfo_fkeys;
70 }
71 #endif
72 
73 NCURSES_EXPORT(void)
74 _nc_init_keytry(SCREEN *sp)
75 {
76     size_t n;
77 
78     /* The sp->_keytry value is initialized in newterm(), where the sp
79      * structure is created, because we can not tell where keypad() or
80      * mouse_activate() (which will call keyok()) are first called.
81      */
82 
83     if (sp != 0) {
84 	for (n = 0; _nc_tinfo_fkeys[n].code; n++) {
85 	    if (_nc_tinfo_fkeys[n].offset < STRCOUNT) {
86 		(void) _nc_add_to_try(&(sp->_keytry),
87 				      CUR Strings[_nc_tinfo_fkeys[n].offset],
88 				      _nc_tinfo_fkeys[n].code);
89 	    }
90 	}
91 #if NCURSES_XNAMES
92 	/*
93 	 * Add any of the extended strings to the tries if their name begins
94 	 * with 'k', i.e., they follow the convention of other terminfo key
95 	 * names.
96 	 */
97 	{
98 	    TERMTYPE *tp = &(sp->_term->type);
99 	    for (n = STRCOUNT; n < NUM_STRINGS(tp); ++n) {
100 		const char *name = ExtStrname(tp, n, strnames);
101 		char *value = tp->Strings[n];
102 		if (name != 0
103 		    && *name == 'k'
104 		    && value != 0
105 		    && key_defined(value) == 0) {
106 		    (void) _nc_add_to_try(&(sp->_keytry),
107 					  value,
108 					  n - STRCOUNT + KEY_MAX);
109 		}
110 	    }
111 	}
112 #endif
113 #ifdef TRACE
114 	_nc_trace_tries(sp->_keytry);
115 #endif
116     }
117 }
118