xref: /openbsd/lib/libcurses/base/define_key.c (revision c7ef0cfc)
1 /* $OpenBSD: define_key.c,v 1.6 2023/10/17 09:52:08 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2020 Thomas E. Dickey                                          *
5  * Copyright 1998-2009,2014 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *  Author: Thomas E. Dickey                        1997-on                 *
34  *     and: Juergen Pfeifer                         2009                    *
35  ****************************************************************************/
36 
37 #include <curses.priv.h>
38 
39 MODULE_ID("$Id: define_key.c,v 1.6 2023/10/17 09:52:08 nicm Exp $")
40 
NCURSES_EXPORT(int)41 NCURSES_EXPORT(int)
42 NCURSES_SP_NAME(define_key) (NCURSES_SP_DCLx const char *str, int keycode)
43 {
44     int code = ERR;
45 
46     T((T_CALLED("define_key(%p, %s,%d)"), (void *) SP_PARM, _nc_visbuf(str), keycode));
47     if (SP_PARM == 0 || !HasTInfoTerminal(SP_PARM)) {
48 	code = ERR;
49     } else if (keycode > 0) {
50 	unsigned ukey = (unsigned) keycode;
51 
52 #ifdef USE_TERM_DRIVER
53 #define CallHasKey(keycode) CallDriver_1(SP_PARM, td_kyExist, keycode)
54 #else
55 #define CallHasKey(keycode) NCURSES_SP_NAME(has_key)(NCURSES_SP_ARGx keycode)
56 #endif
57 
58 	if (str != 0) {
59 	    NCURSES_SP_NAME(define_key) (NCURSES_SP_ARGx str, 0);
60 	} else if (CallHasKey(keycode)) {
61 	    while (_nc_remove_key(&(SP_PARM->_keytry), ukey))
62 		code = OK;
63 	}
64 	if (str != 0) {
65 	    if (NCURSES_SP_NAME(key_defined) (NCURSES_SP_ARGx str) == 0) {
66 		if (_nc_add_to_try(&(SP_PARM->_keytry), str, ukey) == OK) {
67 		    code = OK;
68 		} else {
69 		    code = ERR;
70 		}
71 	    } else {
72 		code = ERR;
73 	    }
74 	}
75     } else {
76 	while (_nc_remove_string(&(SP_PARM->_keytry), str))
77 	    code = OK;
78     }
79     returnCode(code);
80 }
81 
82 #if NCURSES_SP_FUNCS
83 NCURSES_EXPORT(int)
define_key(const char * str,int keycode)84 define_key(const char *str, int keycode)
85 {
86     return NCURSES_SP_NAME(define_key) (CURRENT_SCREEN, str, keycode);
87 }
88 #endif
89