2001-12-02 12:14:20 +03:00
|
|
|
/* $NetBSD: getch.c,v 1.35 2001/12/02 09:14:21 blymn Exp $ */
|
1997-07-22 11:36:20 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-08-18 01:51:41 +04:00
|
|
|
* Copyright (c) 1981, 1993, 1994
|
1993-11-09 06:34:01 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-22 11:36:20 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-07-22 11:36:20 +04:00
|
|
|
#if 0
|
1994-08-18 01:51:41 +04:00
|
|
|
static char sccsid[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94";
|
1997-07-22 11:36:20 +04:00
|
|
|
#else
|
2001-12-02 12:14:20 +03:00
|
|
|
__RCSID("$NetBSD: getch.c,v 1.35 2001/12/02 09:14:21 blymn Exp $");
|
1997-07-22 11:36:20 +04:00
|
|
|
#endif
|
1999-04-13 18:08:17 +04:00
|
|
|
#endif /* not lint */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
1994-08-18 01:51:41 +04:00
|
|
|
#include "curses.h"
|
2000-04-11 17:57:08 +04:00
|
|
|
#include "curses_private.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
#define DEFAULT_DELAY 2 /* default delay for timeout() */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keyboard input handler. Do this by snarfing
|
|
|
|
* all the info we can out of the termcap entry for TERM and putting it
|
|
|
|
* into a set of keymaps. A keymap is an array the size of all the possible
|
|
|
|
* single characters we can get, the contents of the array is a structure
|
|
|
|
* that contains the type of entry this character is (i.e. part/end of a
|
|
|
|
* multi-char sequence or a plain char) and either a pointer which will point
|
|
|
|
* to another keymap (in the case of a multi-char sequence) OR the data value
|
|
|
|
* that this key should return.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* private data structures for holding the key definitions */
|
|
|
|
typedef struct key_entry key_entry_t;
|
|
|
|
|
|
|
|
struct key_entry {
|
|
|
|
short type; /* type of key this is */
|
|
|
|
union {
|
|
|
|
keymap_t *next; /* next keymap is key is multi-key sequence */
|
2000-04-11 17:57:08 +04:00
|
|
|
wchar_t symbol; /* key symbol if key is a leaf entry */
|
1999-06-07 01:05:03 +04:00
|
|
|
} value;
|
1999-04-13 18:08:17 +04:00
|
|
|
};
|
|
|
|
/* Types of key structures we can have */
|
|
|
|
#define KEYMAP_MULTI 1 /* part of a multi char sequence */
|
|
|
|
#define KEYMAP_LEAF 2 /* key has a symbol associated with it, either
|
|
|
|
* it is the end of a multi-char sequence or a
|
|
|
|
* single char key that generates a symbol */
|
|
|
|
|
2000-04-17 16:25:45 +04:00
|
|
|
/* allocate this many key_entry structs at once to speed start up must
|
|
|
|
* be a power of 2.
|
|
|
|
*/
|
|
|
|
#define KEYMAP_ALLOC_CHUNK 4
|
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
/* The max number of different chars we can receive */
|
|
|
|
#define MAX_CHAR 256
|
|
|
|
|
|
|
|
struct keymap {
|
1999-06-07 01:05:03 +04:00
|
|
|
int count; /* count of number of key structs allocated */
|
|
|
|
short mapping[MAX_CHAR]; /* mapping of key to allocated structs */
|
2000-04-17 16:25:45 +04:00
|
|
|
key_entry_t **key; /* dynamic array of keys */
|
|
|
|
};
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* Key buffer */
|
|
|
|
#define INBUF_SZ 16 /* size of key buffer - must be larger than
|
|
|
|
* longest multi-key sequence */
|
2000-04-11 17:57:08 +04:00
|
|
|
static wchar_t inbuf[INBUF_SZ];
|
1999-06-15 08:50:28 +04:00
|
|
|
static int start, end, working; /* pointers for manipulating inbuf data */
|
1999-04-13 18:08:17 +04:00
|
|
|
|
1999-06-07 01:05:03 +04:00
|
|
|
#define INC_POINTER(ptr) do { \
|
|
|
|
(ptr)++; \
|
|
|
|
ptr %= INBUF_SZ; \
|
1999-04-13 18:08:17 +04:00
|
|
|
} while(/*CONSTCOND*/0)
|
|
|
|
|
1999-06-15 08:50:28 +04:00
|
|
|
static short state; /* state of the inkey function */
|
1999-04-13 18:08:17 +04:00
|
|
|
|
1999-06-07 01:05:03 +04:00
|
|
|
#define INKEY_NORM 0 /* no key backlog to process */
|
1999-04-13 18:08:17 +04:00
|
|
|
#define INKEY_ASSEMBLING 1 /* assembling a multi-key sequence */
|
1999-06-07 01:05:03 +04:00
|
|
|
#define INKEY_BACKOUT 2 /* recovering from an unrecognised key */
|
|
|
|
#define INKEY_TIMEOUT 3 /* multi-key sequence timeout */
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
/* The termcap data we are interested in and the symbols they map to */
|
|
|
|
struct tcdata {
|
2000-04-17 16:25:45 +04:00
|
|
|
const char *name; /* name of termcap entry */
|
2000-04-11 17:57:08 +04:00
|
|
|
wchar_t symbol; /* the symbol associated with it */
|
1999-04-13 18:08:17 +04:00
|
|
|
};
|
|
|
|
|
1999-06-15 08:50:28 +04:00
|
|
|
static const struct tcdata tc[] = {
|
2000-04-27 04:23:26 +04:00
|
|
|
{"!1", KEY_SSAVE},
|
|
|
|
{"!2", KEY_SSUSPEND},
|
|
|
|
{"!3", KEY_SUNDO},
|
|
|
|
{"#1", KEY_SHELP},
|
|
|
|
{"#2", KEY_SHOME},
|
|
|
|
{"#3", KEY_SIC},
|
|
|
|
{"#4", KEY_SLEFT},
|
|
|
|
{"%0", KEY_REDO},
|
|
|
|
{"%1", KEY_HELP},
|
|
|
|
{"%2", KEY_MARK},
|
|
|
|
{"%3", KEY_MESSAGE},
|
|
|
|
{"%4", KEY_MOVE},
|
|
|
|
{"%5", KEY_NEXT},
|
|
|
|
{"%6", KEY_OPEN},
|
|
|
|
{"%7", KEY_OPTIONS},
|
|
|
|
{"%8", KEY_PREVIOUS},
|
|
|
|
{"%9", KEY_PRINT},
|
|
|
|
{"%a", KEY_SMESSAGE},
|
|
|
|
{"%b", KEY_SMOVE},
|
|
|
|
{"%c", KEY_SNEXT},
|
|
|
|
{"%d", KEY_SOPTIONS},
|
|
|
|
{"%e", KEY_SPREVIOUS},
|
|
|
|
{"%f", KEY_SPRINT},
|
|
|
|
{"%g", KEY_SREDO},
|
|
|
|
{"%h", KEY_SREPLACE},
|
|
|
|
{"%i", KEY_SRIGHT},
|
|
|
|
{"%j", KEY_SRSUME},
|
|
|
|
{"&0", KEY_SCANCEL},
|
|
|
|
{"&1", KEY_REFERENCE},
|
|
|
|
{"&2", KEY_REFRESH},
|
|
|
|
{"&3", KEY_REPLACE},
|
|
|
|
{"&4", KEY_RESTART},
|
|
|
|
{"&5", KEY_RESUME},
|
|
|
|
{"&6", KEY_SAVE},
|
|
|
|
{"&7", KEY_SUSPEND},
|
|
|
|
{"&8", KEY_UNDO},
|
|
|
|
{"&9", KEY_SBEG},
|
|
|
|
{"*0", KEY_SFIND},
|
|
|
|
{"*1", KEY_SCOMMAND},
|
|
|
|
{"*2", KEY_SCOPY},
|
|
|
|
{"*3", KEY_SCREATE},
|
|
|
|
{"*4", KEY_SDC},
|
|
|
|
{"*5", KEY_SDL},
|
|
|
|
{"*6", KEY_SELECT},
|
|
|
|
{"*7", KEY_SEND},
|
|
|
|
{"*8", KEY_SEOL},
|
|
|
|
{"*9", KEY_SEXIT},
|
|
|
|
{"@0", KEY_FIND},
|
|
|
|
{"@1", KEY_BEG},
|
|
|
|
{"@2", KEY_CANCEL},
|
|
|
|
{"@3", KEY_CLOSE},
|
|
|
|
{"@4", KEY_COMMAND},
|
|
|
|
{"@5", KEY_COPY},
|
|
|
|
{"@6", KEY_CREATE},
|
|
|
|
{"@7", KEY_END},
|
|
|
|
{"@8", KEY_ENTER},
|
|
|
|
{"@9", KEY_EXIT},
|
|
|
|
{"F1", KEY_F(11)},
|
|
|
|
{"F2", KEY_F(12)},
|
|
|
|
{"F3", KEY_F(13)},
|
|
|
|
{"F4", KEY_F(14)},
|
|
|
|
{"F5", KEY_F(15)},
|
|
|
|
{"F6", KEY_F(16)},
|
|
|
|
{"F7", KEY_F(17)},
|
|
|
|
{"F8", KEY_F(18)},
|
|
|
|
{"F9", KEY_F(19)},
|
|
|
|
{"FA", KEY_F(20)},
|
|
|
|
{"FB", KEY_F(21)},
|
|
|
|
{"FC", KEY_F(22)},
|
|
|
|
{"FD", KEY_F(23)},
|
|
|
|
{"FE", KEY_F(24)},
|
|
|
|
{"FF", KEY_F(25)},
|
|
|
|
{"FG", KEY_F(26)},
|
|
|
|
{"FH", KEY_F(27)},
|
|
|
|
{"FI", KEY_F(28)},
|
|
|
|
{"FJ", KEY_F(29)},
|
|
|
|
{"FK", KEY_F(30)},
|
|
|
|
{"FL", KEY_F(31)},
|
|
|
|
{"FM", KEY_F(32)},
|
|
|
|
{"FN", KEY_F(33)},
|
|
|
|
{"FO", KEY_F(34)},
|
|
|
|
{"FP", KEY_F(35)},
|
|
|
|
{"FQ", KEY_F(36)},
|
|
|
|
{"FR", KEY_F(37)},
|
|
|
|
{"FS", KEY_F(38)},
|
|
|
|
{"FT", KEY_F(39)},
|
|
|
|
{"FU", KEY_F(40)},
|
|
|
|
{"FV", KEY_F(41)},
|
|
|
|
{"FW", KEY_F(42)},
|
|
|
|
{"FX", KEY_F(43)},
|
|
|
|
{"FY", KEY_F(44)},
|
|
|
|
{"FZ", KEY_F(45)},
|
|
|
|
{"Fa", KEY_F(46)},
|
|
|
|
{"Fb", KEY_F(47)},
|
|
|
|
{"Fc", KEY_F(48)},
|
|
|
|
{"Fd", KEY_F(49)},
|
|
|
|
{"Fe", KEY_F(50)},
|
|
|
|
{"Ff", KEY_F(51)},
|
|
|
|
{"Fg", KEY_F(52)},
|
|
|
|
{"Fh", KEY_F(53)},
|
|
|
|
{"Fi", KEY_F(54)},
|
|
|
|
{"Fj", KEY_F(55)},
|
|
|
|
{"Fk", KEY_F(56)},
|
|
|
|
{"Fl", KEY_F(57)},
|
|
|
|
{"Fm", KEY_F(58)},
|
|
|
|
{"Fn", KEY_F(59)},
|
|
|
|
{"Fo", KEY_F(60)},
|
|
|
|
{"Fp", KEY_F(61)},
|
|
|
|
{"Fq", KEY_F(62)},
|
|
|
|
{"Fr", KEY_F(63)},
|
1999-04-13 18:08:17 +04:00
|
|
|
{"K1", KEY_A1},
|
|
|
|
{"K2", KEY_B2},
|
|
|
|
{"K3", KEY_A3},
|
|
|
|
{"K4", KEY_C1},
|
|
|
|
{"K5", KEY_C3},
|
2000-04-27 04:23:26 +04:00
|
|
|
{"Km", KEY_MOUSE},
|
1999-04-13 18:08:17 +04:00
|
|
|
{"k0", KEY_F0},
|
|
|
|
{"k1", KEY_F(1)},
|
|
|
|
{"k2", KEY_F(2)},
|
|
|
|
{"k3", KEY_F(3)},
|
|
|
|
{"k4", KEY_F(4)},
|
|
|
|
{"k5", KEY_F(5)},
|
|
|
|
{"k6", KEY_F(6)},
|
|
|
|
{"k7", KEY_F(7)},
|
|
|
|
{"k8", KEY_F(8)},
|
|
|
|
{"k9", KEY_F(9)},
|
2000-04-27 04:23:26 +04:00
|
|
|
{"k;", KEY_F(10)},
|
1999-04-13 18:08:17 +04:00
|
|
|
{"kA", KEY_IL},
|
|
|
|
{"ka", KEY_CATAB},
|
2000-04-27 04:23:26 +04:00
|
|
|
{"kB", KEY_BTAB},
|
1999-04-13 18:08:17 +04:00
|
|
|
{"kb", KEY_BACKSPACE},
|
|
|
|
{"kC", KEY_CLEAR},
|
|
|
|
{"kD", KEY_DC},
|
|
|
|
{"kd", KEY_DOWN},
|
|
|
|
{"kE", KEY_EOL},
|
|
|
|
{"kF", KEY_SF},
|
|
|
|
{"kH", KEY_LL},
|
|
|
|
{"kh", KEY_HOME},
|
|
|
|
{"kI", KEY_IC},
|
|
|
|
{"kL", KEY_DL},
|
|
|
|
{"kl", KEY_LEFT},
|
2000-04-27 04:23:26 +04:00
|
|
|
{"kM", KEY_EIC},
|
1999-04-13 18:08:17 +04:00
|
|
|
{"kN", KEY_NPAGE},
|
|
|
|
{"kP", KEY_PPAGE},
|
|
|
|
{"kR", KEY_SR},
|
|
|
|
{"kr", KEY_RIGHT},
|
|
|
|
{"kS", KEY_EOS},
|
|
|
|
{"kT", KEY_STAB},
|
|
|
|
{"kt", KEY_CTAB},
|
|
|
|
{"ku", KEY_UP}
|
|
|
|
};
|
|
|
|
/* Number of TC entries .... */
|
1999-06-15 08:50:28 +04:00
|
|
|
static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
/* prototypes for private functions */
|
2000-04-17 16:25:45 +04:00
|
|
|
static key_entry_t *add_new_key(keymap_t *current, char chr, int key_type,
|
|
|
|
int symbol);
|
1999-06-15 08:50:28 +04:00
|
|
|
static keymap_t *new_keymap(void); /* create a new keymap */
|
|
|
|
static key_entry_t *new_key(void); /* create a new key entry */
|
2000-04-17 16:25:45 +04:00
|
|
|
static wchar_t inkey(int to, int delay);
|
|
|
|
|
2001-12-02 12:14:20 +03:00
|
|
|
/*
|
|
|
|
* Free the storage associated with the given keymap
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_cursesi_free_keymap(keymap_t *map)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* check for, and free, child keymaps */
|
|
|
|
for (i = 0; i < MAX_CHAR; i++) {
|
|
|
|
if (map->mapping[i] >= 0) {
|
|
|
|
if (map->key[map->mapping[i]]->type == KEYMAP_MULTI)
|
|
|
|
_cursesi_free_keymap(
|
|
|
|
map->key[map->mapping[i]]->value.next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now free any allocated keymap structs */
|
|
|
|
for (i = 0; i < map->count; i += KEYMAP_ALLOC_CHUNK) {
|
|
|
|
free(map->key[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(map->key);
|
|
|
|
free(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-17 16:25:45 +04:00
|
|
|
/*
|
|
|
|
* Add a new key entry to the keymap pointed to by current. Entry
|
|
|
|
* contains the character to add to the keymap, type is the type of
|
|
|
|
* entry to add (either multikey or leaf) and symbol is the symbolic
|
|
|
|
* value for a leaf type entry. The function returns a pointer to the
|
|
|
|
* new keymap entry.
|
|
|
|
*/
|
|
|
|
static key_entry_t *
|
|
|
|
add_new_key(keymap_t *current, char chr, int key_type, int symbol)
|
|
|
|
{
|
|
|
|
key_entry_t *the_key;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
__CTRACE("Adding character %s of type %d, symbol 0x%x\n", unctrl(chr),
|
|
|
|
key_type, symbol);
|
|
|
|
#endif
|
2001-01-10 17:02:31 +03:00
|
|
|
if (current->mapping[(unsigned char) chr] < 0) {
|
2000-04-17 16:25:45 +04:00
|
|
|
/* first time for this char */
|
2001-01-10 17:02:31 +03:00
|
|
|
current->mapping[(unsigned char) chr] = current->count; /* map new entry */
|
2000-04-17 16:25:45 +04:00
|
|
|
/* make sure we have room in the key array first */
|
|
|
|
if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
|
|
|
|
{
|
|
|
|
if ((current->key =
|
|
|
|
realloc(current->key,
|
|
|
|
(current->count) * sizeof(key_entry_t *)
|
|
|
|
+ KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Could not malloc for key entry\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
the_key = new_key();
|
|
|
|
for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
|
|
|
|
current->key[current->count + i]
|
|
|
|
= &the_key[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* point at the current key array element to use */
|
|
|
|
the_key = current->key[current->count];
|
|
|
|
|
|
|
|
the_key->type = key_type;
|
|
|
|
|
|
|
|
switch (key_type) {
|
|
|
|
case KEYMAP_MULTI:
|
|
|
|
/* need for next key */
|
|
|
|
#ifdef DEBUG
|
|
|
|
__CTRACE("Creating new keymap\n");
|
|
|
|
#endif
|
|
|
|
the_key->value.next = new_keymap();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYMAP_LEAF:
|
|
|
|
/* the associated symbol for the key */
|
|
|
|
#ifdef DEBUG
|
|
|
|
__CTRACE("Adding leaf key\n");
|
|
|
|
#endif
|
|
|
|
the_key->value.symbol = symbol;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "add_new_key: bad type passed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
current->count++;
|
|
|
|
} else {
|
|
|
|
/* the key is already known - just return the address. */
|
|
|
|
#ifdef DEBUG
|
|
|
|
__CTRACE("Keymap already known\n");
|
|
|
|
#endif
|
2001-01-10 17:02:31 +03:00
|
|
|
the_key = current->key[current->mapping[(unsigned char) chr]];
|
2000-04-17 16:25:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return the_key;
|
|
|
|
}
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Init_getch - initialise all the pointers & structures needed to make
|
|
|
|
* getch work in keypad mode.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2001-12-02 12:14:20 +03:00
|
|
|
__init_getch(SCREEN *screen)
|
1999-04-13 18:08:17 +04:00
|
|
|
{
|
2000-05-01 16:30:30 +04:00
|
|
|
char entry[1024], *p;
|
2000-04-17 16:25:45 +04:00
|
|
|
int i, j, length, key_ent;
|
|
|
|
size_t limit;
|
|
|
|
key_entry_t *tmp_key;
|
1999-04-13 18:08:17 +04:00
|
|
|
keymap_t *current;
|
2000-04-17 16:25:45 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
int k;
|
|
|
|
#endif
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
/* init the inkey state variable */
|
|
|
|
state = INKEY_NORM;
|
|
|
|
|
|
|
|
/* init the base keymap */
|
2001-12-02 12:14:20 +03:00
|
|
|
screen->base_keymap = new_keymap();
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
/* key input buffer pointers */
|
|
|
|
start = end = working = 0;
|
|
|
|
|
|
|
|
/* now do the termcap snarfing ... */
|
2001-12-02 12:14:20 +03:00
|
|
|
|
2000-05-01 16:30:30 +04:00
|
|
|
for (i = 0; i < num_tcs; i++) {
|
|
|
|
p = entry;
|
|
|
|
limit = 1023;
|
2001-12-02 12:14:20 +03:00
|
|
|
if (t_getstr(screen->cursesi_genbuf, tc[i].name,
|
|
|
|
&p, &limit) != NULL) {
|
|
|
|
current = screen->base_keymap; /* always start with
|
|
|
|
* base keymap. */
|
2000-05-01 16:30:30 +04:00
|
|
|
length = (int) strlen(entry);
|
2000-04-17 16:25:45 +04:00
|
|
|
#ifdef DEBUG
|
2000-05-01 16:30:30 +04:00
|
|
|
__CTRACE("Processing termcap entry %s, sequence ",
|
|
|
|
tc[i].name);
|
|
|
|
for (k = 0; k <= length -1; k++)
|
|
|
|
__CTRACE("%s", unctrl(entry[k]));
|
|
|
|
__CTRACE("\n");
|
2000-04-17 16:25:45 +04:00
|
|
|
#endif
|
2000-05-01 16:30:30 +04:00
|
|
|
for (j = 0; j < length - 1; j++) {
|
|
|
|
/* add the entry to the struct */
|
|
|
|
tmp_key = add_new_key(current,
|
|
|
|
entry[j],
|
|
|
|
KEYMAP_MULTI, 0);
|
2000-04-17 16:25:45 +04:00
|
|
|
|
2000-05-01 16:30:30 +04:00
|
|
|
/* index into the key array - it's
|
|
|
|
clearer if we stash this */
|
|
|
|
key_ent = current->mapping[
|
2001-01-10 17:02:31 +03:00
|
|
|
(unsigned char) entry[j]];
|
2000-05-01 16:30:30 +04:00
|
|
|
|
|
|
|
current->key[key_ent] = tmp_key;
|
|
|
|
|
|
|
|
/* next key uses this map... */
|
|
|
|
current = current->key[key_ent]->value.next;
|
|
|
|
}
|
1999-06-28 17:32:43 +04:00
|
|
|
|
2000-04-17 16:25:45 +04:00
|
|
|
/* this is the last key in the sequence (it
|
|
|
|
* may have been the only one but that does
|
|
|
|
* not matter) this means it is a leaf key and
|
|
|
|
* should have a symbol associated with it.
|
|
|
|
*/
|
2000-05-01 16:30:30 +04:00
|
|
|
tmp_key = add_new_key(current,
|
|
|
|
entry[length - 1],
|
|
|
|
KEYMAP_LEAF,
|
|
|
|
tc[i].symbol);
|
|
|
|
current->key[
|
|
|
|
current->mapping[(int)entry[length - 1]]] =
|
|
|
|
tmp_key;
|
1999-06-07 01:05:03 +04:00
|
|
|
}
|
1999-04-13 18:08:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* new_keymap - allocates & initialises a new keymap structure. This
|
|
|
|
* function returns a pointer to the new keymap.
|
|
|
|
*
|
|
|
|
*/
|
1999-06-15 08:50:28 +04:00
|
|
|
static keymap_t *
|
1999-04-13 18:08:17 +04:00
|
|
|
new_keymap(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
keymap_t *new_map;
|
|
|
|
|
|
|
|
if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
|
|
|
|
perror("Inkey: Cannot allocate new keymap");
|
|
|
|
exit(2);
|
|
|
|
}
|
1999-06-07 01:05:03 +04:00
|
|
|
|
|
|
|
/* Initialise the new map */
|
1999-04-13 18:08:17 +04:00
|
|
|
new_map->count = 0;
|
|
|
|
for (i = 0; i < MAX_CHAR; i++) {
|
|
|
|
new_map->mapping[i] = -1; /* no mapping for char */
|
|
|
|
}
|
|
|
|
|
2000-04-23 01:14:19 +04:00
|
|
|
/* key array will be allocated when first key is added */
|
|
|
|
new_map->key = NULL;
|
|
|
|
|
2000-04-17 16:25:45 +04:00
|
|
|
return new_map;
|
1999-04-13 18:08:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* new_key - allocates & initialises a new key entry. This function returns
|
|
|
|
* a pointer to the newly allocated key entry.
|
|
|
|
*
|
|
|
|
*/
|
1999-06-15 08:50:28 +04:00
|
|
|
static key_entry_t *
|
1999-04-13 18:08:17 +04:00
|
|
|
new_key(void)
|
|
|
|
{
|
|
|
|
key_entry_t *new_one;
|
2000-04-17 16:25:45 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t)))
|
|
|
|
== NULL) {
|
|
|
|
perror("inkey: Cannot allocate new key entry chunk");
|
1999-04-13 18:08:17 +04:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2000-04-17 16:25:45 +04:00
|
|
|
for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
|
|
|
|
new_one[i].type = 0;
|
|
|
|
new_one[i].value.next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_one;
|
1999-04-13 18:08:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* inkey - do the work to process keyboard input, check for multi-key
|
|
|
|
* sequences and return the appropriate symbol if we get a match.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2000-04-11 17:57:08 +04:00
|
|
|
wchar_t
|
2000-04-17 16:25:45 +04:00
|
|
|
inkey(int to, int delay)
|
1999-04-13 18:08:17 +04:00
|
|
|
{
|
2000-04-19 01:44:48 +04:00
|
|
|
wchar_t k;
|
2000-04-22 18:32:44 +04:00
|
|
|
int c;
|
2001-12-02 12:14:20 +03:00
|
|
|
keymap_t *current = _cursesi_screen->base_keymap;
|
|
|
|
FILE *infd = _cursesi_screen->infd;
|
1999-04-13 18:08:17 +04:00
|
|
|
|
2000-04-27 04:23:26 +04:00
|
|
|
k = 0; /* XXX gcc -Wuninitialized */
|
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
for (;;) { /* loop until we get a complete key sequence */
|
|
|
|
reread:
|
|
|
|
if (state == INKEY_NORM) {
|
|
|
|
if (delay && __timeout(delay) == ERR)
|
|
|
|
return ERR;
|
2000-04-22 18:32:44 +04:00
|
|
|
if ((c = getchar()) == EOF) {
|
2001-12-02 12:14:20 +03:00
|
|
|
clearerr(infd);
|
1999-04-13 18:08:17 +04:00
|
|
|
return ERR;
|
2000-04-22 18:32:44 +04:00
|
|
|
}
|
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
if (delay && (__notimeout() == ERR))
|
|
|
|
return ERR;
|
2000-04-22 18:32:44 +04:00
|
|
|
|
2000-04-11 17:57:08 +04:00
|
|
|
k = (wchar_t) c;
|
1999-04-13 18:08:17 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
__CTRACE("inkey (state normal) got '%s'\n", unctrl(k));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
working = start;
|
|
|
|
inbuf[working] = k;
|
|
|
|
INC_POINTER(working);
|
|
|
|
end = working;
|
|
|
|
state = INKEY_ASSEMBLING; /* go to the assembling
|
|
|
|
* state now */
|
1999-06-07 01:05:03 +04:00
|
|
|
} else if (state == INKEY_BACKOUT) {
|
|
|
|
k = inbuf[working];
|
|
|
|
INC_POINTER(working);
|
|
|
|
if (working == end) { /* see if we have run
|
|
|
|
* out of keys in the
|
|
|
|
* backlog */
|
1999-04-13 18:08:17 +04:00
|
|
|
|
1999-06-07 01:05:03 +04:00
|
|
|
/* if we have then switch to
|
|
|
|
assembling */
|
|
|
|
state = INKEY_ASSEMBLING;
|
|
|
|
}
|
|
|
|
} else if (state == INKEY_ASSEMBLING) {
|
|
|
|
/* assembling a key sequence */
|
|
|
|
if (delay) {
|
|
|
|
if (__timeout(to ? DEFAULT_DELAY : delay) == ERR)
|
1999-04-13 18:08:17 +04:00
|
|
|
return ERR;
|
1999-06-07 01:05:03 +04:00
|
|
|
} else {
|
|
|
|
if (to && (__timeout(DEFAULT_DELAY) == ERR))
|
1999-04-13 18:08:17 +04:00
|
|
|
return ERR;
|
1999-06-07 01:05:03 +04:00
|
|
|
}
|
2000-04-22 18:32:44 +04:00
|
|
|
|
|
|
|
c = getchar();
|
2001-12-02 12:14:20 +03:00
|
|
|
if (ferror(infd)) {
|
|
|
|
clearerr(infd);
|
1999-06-07 01:05:03 +04:00
|
|
|
return ERR;
|
2000-04-22 18:32:44 +04:00
|
|
|
}
|
|
|
|
|
1999-06-07 01:05:03 +04:00
|
|
|
if ((to || delay) && (__notimeout() == ERR))
|
1999-04-13 18:08:17 +04:00
|
|
|
return ERR;
|
1999-06-28 17:32:43 +04:00
|
|
|
|
2000-04-11 17:57:08 +04:00
|
|
|
k = (wchar_t) c;
|
1999-04-13 18:08:17 +04:00
|
|
|
#ifdef DEBUG
|
1999-06-07 01:05:03 +04:00
|
|
|
__CTRACE("inkey (state assembling) got '%s'\n", unctrl(k));
|
1999-04-13 18:08:17 +04:00
|
|
|
#endif
|
2001-12-02 12:14:20 +03:00
|
|
|
if (feof(infd)) { /* inter-char timeout,
|
1999-06-07 01:05:03 +04:00
|
|
|
* start backing out */
|
2001-12-02 12:14:20 +03:00
|
|
|
clearerr(infd);
|
1999-06-07 01:05:03 +04:00
|
|
|
if (start == end)
|
|
|
|
/* no chars in the buffer, restart */
|
|
|
|
goto reread;
|
|
|
|
|
|
|
|
k = inbuf[start];
|
|
|
|
state = INKEY_TIMEOUT;
|
1999-04-13 18:08:17 +04:00
|
|
|
} else {
|
1999-06-07 01:05:03 +04:00
|
|
|
inbuf[working] = k;
|
|
|
|
INC_POINTER(working);
|
|
|
|
end = working;
|
1999-04-13 18:08:17 +04:00
|
|
|
}
|
1999-06-07 01:05:03 +04:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Inkey state screwed - exiting!!!");
|
|
|
|
exit(2);
|
|
|
|
}
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
/* Check key has no special meaning and we have not timed out */
|
2000-04-17 16:25:45 +04:00
|
|
|
if ((state == INKEY_TIMEOUT) || (current->mapping[k] < 0)) {
|
1999-06-07 01:05:03 +04:00
|
|
|
/* return the first key we know about */
|
|
|
|
k = inbuf[start];
|
1999-04-13 18:08:17 +04:00
|
|
|
|
|
|
|
INC_POINTER(start);
|
|
|
|
working = start;
|
|
|
|
|
|
|
|
if (start == end) { /* only one char processed */
|
|
|
|
state = INKEY_NORM;
|
|
|
|
} else {/* otherwise we must have more than one char
|
|
|
|
* to backout */
|
|
|
|
state = INKEY_BACKOUT;
|
|
|
|
}
|
|
|
|
return k;
|
|
|
|
} else { /* must be part of a multikey sequence */
|
|
|
|
/* check for completed key sequence */
|
|
|
|
if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) {
|
|
|
|
start = working; /* eat the key sequence
|
|
|
|
* in inbuf */
|
|
|
|
|
1999-06-07 01:05:03 +04:00
|
|
|
/* check if inbuf empty now */
|
|
|
|
if (start == end) {
|
|
|
|
/* if it is go back to normal */
|
|
|
|
state = INKEY_NORM;
|
|
|
|
} else {
|
|
|
|
/* otherwise go to backout state */
|
1999-04-13 18:08:17 +04:00
|
|
|
state = INKEY_BACKOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the symbol */
|
|
|
|
return current->key[current->mapping[k]]->value.symbol;
|
|
|
|
|
1999-06-07 01:05:03 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Step on to next part of the multi-key
|
|
|
|
* sequence.
|
|
|
|
*/
|
1999-04-13 18:08:17 +04:00
|
|
|
current = current->key[current->mapping[k]]->value.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-15 17:17:02 +04:00
|
|
|
#ifndef _CURSES_USE_MACROS
|
|
|
|
/*
|
|
|
|
* getch --
|
|
|
|
* Read in a character from stdscr.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getch(void)
|
|
|
|
{
|
|
|
|
return wgetch(stdscr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mvgetch --
|
|
|
|
* Read in a character from stdscr at the given location.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
mvgetch(int y, int x)
|
|
|
|
{
|
|
|
|
return mvwgetch(stdscr, y, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mvwgetch --
|
|
|
|
* Read in a character from stdscr at the given location in the
|
|
|
|
* given window.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
mvwgetch(WINDOW *win, int y, int x)
|
|
|
|
{
|
|
|
|
if (wmove(win, y, x) == ERR)
|
|
|
|
return ERR;
|
|
|
|
|
|
|
|
return wgetch(win);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1993-08-07 09:48:37 +04:00
|
|
|
* wgetch --
|
|
|
|
* Read in a character from the window.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1993-08-07 09:48:37 +04:00
|
|
|
int
|
2000-04-15 17:17:02 +04:00
|
|
|
wgetch(WINDOW *win)
|
1993-08-07 09:48:37 +04:00
|
|
|
{
|
2000-07-31 20:22:46 +04:00
|
|
|
int inp, weset;
|
|
|
|
int c;
|
2001-12-02 12:14:20 +03:00
|
|
|
FILE *infd = _cursesi_screen->infd;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-11-09 06:34:01 +03:00
|
|
|
if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
|
1999-04-13 18:08:17 +04:00
|
|
|
&& win->curx == win->maxx - 1 && win->cury == win->maxy - 1
|
|
|
|
&& __echoit)
|
1993-08-07 09:48:37 +04:00
|
|
|
return (ERR);
|
2000-04-27 04:23:26 +04:00
|
|
|
|
2000-09-23 13:34:07 +04:00
|
|
|
if (is_wintouched(win))
|
|
|
|
wrefresh(win);
|
1993-08-07 09:48:37 +04:00
|
|
|
#ifdef DEBUG
|
2000-04-16 02:59:05 +04:00
|
|
|
__CTRACE("wgetch: __echoit = %d, __rawmode = %d, flags = %0.2o\n",
|
|
|
|
__echoit, __rawmode, win->flags);
|
1993-08-07 09:48:37 +04:00
|
|
|
#endif
|
|
|
|
if (__echoit && !__rawmode) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cbreak();
|
1993-08-07 09:48:37 +04:00
|
|
|
weset = 1;
|
|
|
|
} else
|
|
|
|
weset = 0;
|
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
__save_termios();
|
|
|
|
|
|
|
|
if (win->flags & __KEYPAD) {
|
|
|
|
switch (win->delay)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
|
|
|
|
break;
|
|
|
|
case 0:
|
2000-04-16 02:59:05 +04:00
|
|
|
if (__nodelay() == ERR) {
|
|
|
|
__restore_termios();
|
|
|
|
return ERR;
|
|
|
|
}
|
1999-04-13 18:08:17 +04:00
|
|
|
inp = inkey(0, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (win->delay)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
if (__nodelay() == ERR) {
|
|
|
|
__restore_termios();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (__timeout(win->delay) == ERR) {
|
|
|
|
__restore_termios();
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1999-06-07 01:05:03 +04:00
|
|
|
|
2000-04-22 18:32:44 +04:00
|
|
|
c = getchar();
|
2001-12-02 12:14:20 +03:00
|
|
|
if (feof(infd)) {
|
|
|
|
clearerr(infd);
|
2000-04-22 18:32:44 +04:00
|
|
|
__restore_termios();
|
|
|
|
return ERR; /* we have timed out */
|
|
|
|
}
|
|
|
|
|
2001-12-02 12:14:20 +03:00
|
|
|
if (ferror(infd)) {
|
|
|
|
clearerr(infd);
|
1999-04-13 18:08:17 +04:00
|
|
|
inp = ERR;
|
1999-06-07 01:05:03 +04:00
|
|
|
} else {
|
2000-07-31 20:22:46 +04:00
|
|
|
inp = c;
|
1999-04-13 18:08:17 +04:00
|
|
|
}
|
|
|
|
}
|
1993-08-07 09:48:37 +04:00
|
|
|
#ifdef DEBUG
|
1999-12-07 06:53:11 +03:00
|
|
|
if (inp > 255)
|
2000-04-17 16:25:45 +04:00
|
|
|
/* we have a key symbol - treat it differently */
|
|
|
|
/* XXXX perhaps __unctrl should be expanded to include
|
|
|
|
* XXXX the keysyms in the table....
|
|
|
|
*/
|
1999-12-07 06:53:11 +03:00
|
|
|
__CTRACE("wgetch assembled keysym 0x%x\n", inp);
|
|
|
|
else
|
|
|
|
__CTRACE("wgetch got '%s'\n", unctrl(inp));
|
1993-08-07 09:48:37 +04:00
|
|
|
#endif
|
1999-06-07 01:05:03 +04:00
|
|
|
if (win->delay > -1) {
|
1999-04-13 18:08:17 +04:00
|
|
|
if (__delay() == ERR) {
|
|
|
|
__restore_termios();
|
|
|
|
return ERR;
|
|
|
|
}
|
1999-06-07 01:05:03 +04:00
|
|
|
}
|
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
__restore_termios();
|
2000-05-01 16:30:30 +04:00
|
|
|
|
2000-04-27 21:50:01 +04:00
|
|
|
if (__echoit)
|
2000-04-11 17:57:08 +04:00
|
|
|
waddch(win, (chtype) inp);
|
2000-05-01 16:30:30 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (weset)
|
|
|
|
nocbreak();
|
1999-06-07 01:05:03 +04:00
|
|
|
|
1999-04-13 18:08:17 +04:00
|
|
|
return ((inp < 0) || (inp == ERR) ? ERR : inp);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2000-04-22 18:32:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ungetch --
|
|
|
|
* Put the character back into the input queue.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ungetch(int c)
|
|
|
|
{
|
2001-12-02 12:14:20 +03:00
|
|
|
return ((ungetc(c, _cursesi_screen->infd) == EOF) ? ERR : OK);
|
2000-04-22 18:32:44 +04:00
|
|
|
}
|