Home | History | Annotate | Line # | Download | only in libcurses
      1 /*   $NetBSD: keymap.h,v 1.5 2022/04/19 20:32:16 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2005 The NetBSD Foundation Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from code donated to the NetBSD Foundation
      8  * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
      9  *
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef _KEYMAP_H_
     38 #define _KEYMAP_H_
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 __RCSID("$NetBSD: keymap.h,v 1.5 2022/04/19 20:32:16 rillig Exp $");
     43 #endif                          /* not lint */
     44 
     45 /* keymap related stuff */
     46 /*
     47  * moved here by Ruibiao Qiu
     48  * because it is needed by both getch() and get_wch()
     49  *
     50  * Keyboard input handler.  Do this by snarfing
     51  * all the info we can out of the terminfo entry for TERM and putting it
     52  * into a set of keymaps.  A keymap is an array the size of all the possible
     53  * single characters we can get, the contents of the array is a structure
     54  * that contains the type of entry this character is (i.e. part/end of a
     55  * multi-char sequence or a plain char) and either a pointer which will point
     56  * to another keymap (in the case of a multi-char sequence) OR the data value
     57  * that this key should return.
     58  *
     59  */
     60 
     61 /* private data structures for holding the key definitions */
     62 typedef struct key_entry key_entry_t;
     63 
     64 struct key_entry {
     65 	short   type;		/* type of key this is */
     66 	bool    enable;         /* true if the key is active */
     67 	union {
     68 		keymap_t *next;	/* next keymap is key is multi-key sequence */
     69 		wchar_t   symbol;	/* key symbol if key is a leaf entry */
     70 	} value;
     71 };
     72 /* Types of key structures we can have */
     73 #define KEYMAP_MULTI  1		/* part of a multi char sequence */
     74 #define KEYMAP_LEAF   2		/* key has a symbol associated with it, either
     75 				 * it is the end of a multi-char sequence or a
     76 				 * single char key that generates a symbol */
     77 
     78 /* allocate this many key_entry structs at once to speed start up must
     79  * be a power of 2.
     80  */
     81 #define KEYMAP_ALLOC_CHUNK 4
     82 
     83 /* The max number of different chars we can receive */
     84 #define MAX_CHAR 256
     85 
     86 /*
     87  * Unused mapping flag.
     88  */
     89 #define MAPPING_UNUSED (0 - MAX_CHAR) /* never been used */
     90 
     91 struct keymap {
     92 	int	count;	       /* count of number of key structs allocated */
     93 	short	mapping[MAX_CHAR]; /* mapping of key to allocated structs */
     94 	key_entry_t **key;     /* dynamic array of keys */
     95 };
     96 
     97 #define INC_POINTER(ptr)  do {	\
     98 	(ptr)++;		\
     99 	(ptr) %= INBUF_SZ;	\
    100 } while (0)
    101 
    102 #define INKEY_NORM	   0 /* no key backlog to process */
    103 #define INKEY_ASSEMBLING   1 /* assembling a multi-key sequence */
    104 #define INKEY_BACKOUT	   2 /* recovering from an unrecognised key */
    105 #define INKEY_TIMEOUT	   3 /* multi-key sequence timeout */
    106 #ifdef HAVE_WCHAR
    107 #define INKEY_WCASSEMBLING 4 /* assembling a wide char sequence */
    108 #endif /* HAVE_WCHAR */
    109 
    110 /* The terminfo data we are interested in and the symbols they map to */
    111 struct tcdata {
    112 	int code;		/* code of the terminfo entry */
    113 	wchar_t	symbol;		/* the symbol associated with it */
    114 };
    115 
    116 #endif /* _KEYMAP_H_ */
    117