Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libcurses Added the ncurses extensions define_key and ke...



details:   https://anonhg.NetBSD.org/src/rev/a65bb981317c
branches:  trunk
changeset: 538537:a65bb981317c
user:      blymn <blymn%NetBSD.org@localhost>
date:      Tue Oct 22 12:07:20 2002 +0000

description:
Added the ncurses extensions define_key and keyok.

diffstat:

 lib/libcurses/curses.3       |    4 +-
 lib/libcurses/curses.h       |    4 +-
 lib/libcurses/curses_input.3 |   65 +++++++++-
 lib/libcurses/getch.c        |  277 ++++++++++++++++++++++++++++++++----------
 lib/libcurses/shlib_version  |    4 +-
 5 files changed, 280 insertions(+), 74 deletions(-)

diffs (truncated from 570 to 300 lines):

diff -r a020ed10cfd9 -r a65bb981317c lib/libcurses/curses.3
--- a/lib/libcurses/curses.3    Tue Oct 22 11:55:10 2002 +0000
+++ b/lib/libcurses/curses.3    Tue Oct 22 12:07:20 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: curses.3,v 1.41 2002/10/21 13:55:07 blymn Exp $
+.\"    $NetBSD: curses.3,v 1.42 2002/10/22 12:07:20 blymn Exp $
 .\"
 .\" Copyright (c) 1985, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -105,6 +105,7 @@
 .It delay_output Ta Xr curses_tty 3
 .It def_prog_mode Ta Xr curses_tty 3
 .It def_shell_mode Ta Xr curses_tty 3
+.It define_key Ta Xr curses_input 3
 .It delch Ta Xr curses_delch 3
 .It deleteln Ta Xr curses_deleteln 3
 .It delscreen Ta Xr curses_screen 3
@@ -159,6 +160,7 @@
 .It is_wintouched Ta Xr curses_touch 3
 .It isendwin Ta Xr curses_screen 3
 .It keypad Ta Xr curses_input 3
+.It keyok Ta Xr curses_input 3
 .It killchar Ta Xr curses_tty 3
 .It leaveok Ta Xr curses_tty 3
 .It longname Ta Xr curses_termcap 3
diff -r a020ed10cfd9 -r a65bb981317c lib/libcurses/curses.h
--- a/lib/libcurses/curses.h    Tue Oct 22 11:55:10 2002 +0000
+++ b/lib/libcurses/curses.h    Tue Oct 22 12:07:20 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: curses.h,v 1.68 2002/10/14 16:25:52 jdc Exp $  */
+/*     $NetBSD: curses.h,v 1.69 2002/10/22 12:07:20 blymn Exp $        */
 
 /*
  * Copyright (c) 1981, 1993, 1994
@@ -496,6 +496,7 @@
 int     curs_set(int);
 int     def_prog_mode(void);
 int     def_shell_mode(void);
+int      define_key(char *, int);
 int     delay_output(int);
 void     delscreen(SCREEN *);
 int     delwin(WINDOW *);
@@ -534,6 +535,7 @@
 bool    isendwin(void);
 bool    is_linetouched(WINDOW *, int);
 bool    is_wintouched(WINDOW *);
+int      keyok(int, bool);
 void    keypad(WINDOW *, bool);
 char     killchar(void);
 int     leaveok(WINDOW *, bool);
diff -r a020ed10cfd9 -r a65bb981317c lib/libcurses/curses_input.3
--- a/lib/libcurses/curses_input.3      Tue Oct 22 11:55:10 2002 +0000
+++ b/lib/libcurses/curses_input.3      Tue Oct 22 12:07:20 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: curses_input.3,v 1.4 2002/10/21 14:31:36 wiz Exp $
+.\"    $NetBSD: curses_input.3,v 1.5 2002/10/22 12:07:20 blymn Exp $
 .\"
 .\" Copyright (c) 2002
 .\"    Brett Lymn (blymn%netbsd.org@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -37,6 +37,8 @@
 .Nm curses_input ,
 .Nm getch ,
 .Nm wgetch ,
+.Nm define_key ,
+.Nm keyok ,
 .Nm getnstr ,
 .Nm wgetnstr ,
 .Nm mvgetnstr ,
@@ -61,6 +63,10 @@
 .Ft int
 .Fn wgetch "WINDOW *win"
 .Ft int
+.Fn keyok "int key_symbol" "bool flag"
+.Ft int
+.Fn define_key "char *sequence" "int key_symbol"
+.Ft int
 .Fn getnstr "char *str" "int limit"
 .Ft int
 .Fn wgetnstr "WINDOW *win" "char *str" "int limit"
@@ -119,6 +125,55 @@
 with the window specified by
 .Fa win .
 .Pp
+If the 
+.Fn keypad
+flag is
+.Dv TRUE
+then the assembly of specific key symbols can be disabled by using the 
+.Fn keyok
+function.
+If the 
+.Fa flag
+is set to 
+.Dv FALSE
+on a key symbol then
+.Fn getch
+will behave as if the character sequence associated with that key symbol
+was not recognised and will return the component characters one at a time to
+the caller.
+.Pp
+Custom associations between sequences of characters and a key symbol can
+be made by using the
+.Fn define_key
+function.
+Normally, these associations are made by the information in the
+.Xr termcap 5
+database but the
+.Fn define_key
+function gives the capability to remove or add more associations.
+If
+.Fn define_key
+is passed a non-NULL string in
+.Fa sequence
+it will associate that sequence with the key symbol passed in
+.Fa key_symbol .
+The key symbol may be one of the ones listed below or a custom value that
+is application defined.
+It is valid to have multiple character sequences map to the same key
+symbol and there are no constraints on the length of the sequence allowed.
+The assembly of custom sequences follow the same rules for inter-character
+timing and so forth as the
+.Xr termcap 5
+derived ones.
+If 
+.Fn define_key
+is passed a NULL in
+.Fa sequence
+then all associations for the key symbol in
+.Fa key_symbol
+will be deleted, this includes any associations that were derived from
+.Xr termcap 5 .
+.Pp
 The
 .Fn mvgetch
 and
@@ -482,6 +537,14 @@
 .Nx
 Curses library complies with the X/Open Curses specification, part of the
 Single Unix Specification.
+.Sh NOTES
+The
+.Fn keyok
+and
+.Fn define_key
+functions are implementations of extensions made by the NCurses library
+to the Curses standard.
+Portable implementations should avoid the use of these functions.
 .Sh HISTORY
 The Curses package appeared in
 .Bx 4.0 .
diff -r a020ed10cfd9 -r a65bb981317c lib/libcurses/getch.c
--- a/lib/libcurses/getch.c     Tue Oct 22 11:55:10 2002 +0000
+++ b/lib/libcurses/getch.c     Tue Oct 22 12:07:20 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getch.c,v 1.36 2002/01/02 10:38:27 blymn Exp $ */
+/*     $NetBSD: getch.c,v 1.37 2002/10/22 12:07:20 blymn Exp $ */
 
 /*
  * Copyright (c) 1981, 1993, 1994
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)getch.c    8.2 (Berkeley) 5/4/94";
 #else
-__RCSID("$NetBSD: getch.c,v 1.36 2002/01/02 10:38:27 blymn Exp $");
+__RCSID("$NetBSD: getch.c,v 1.37 2002/10/22 12:07:20 blymn Exp $");
 #endif
 #endif                                 /* not lint */
 
@@ -68,6 +68,7 @@
 
 struct key_entry {
        short   type;           /* type of key this is */
+       bool    enable;         /* true if the key is active */
        union {
                keymap_t *next; /* next keymap is key is multi-key sequence */
                wchar_t   symbol;       /* key symbol if key is a leaf entry */
@@ -87,10 +88,15 @@
 /* The max number of different chars we can receive */
 #define MAX_CHAR 256
 
+/*
+ * Unused mapping flag.
+ */
+#define MAPPING_UNUSED (0 - MAX_CHAR) /* never been used */
+
 struct keymap {
-       int     count;          /* count of number of key structs allocated */
+       int     count;         /* count of number of key structs allocated */
        short   mapping[MAX_CHAR]; /* mapping of key to allocated structs */
-       key_entry_t **key;      /* dynamic array of keys */
+       key_entry_t **key;     /* dynamic array of keys */
 };
 
 
@@ -274,8 +280,11 @@
 static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
 
 /* prototypes for private functions */
+static void add_key_sequence(SCREEN *screen, char *sequence, int key_type);
 static key_entry_t *add_new_key(keymap_t *current, char chr, int key_type,
                                int symbol);
+static void delete_key_sequence(keymap_t *current, int key_type);
+static void do_keyok(keymap_t *current, int key_type, bool flag, int *retval);
 static keymap_t                *new_keymap(void);      /* create a new keymap */
 static key_entry_t     *new_key(void);         /* create a new key entry */
 static wchar_t         inkey(int to, int delay);
@@ -318,36 +327,46 @@
 add_new_key(keymap_t *current, char chr, int key_type, int symbol)
 {
        key_entry_t *the_key;
-        int i;
+        int i, ki;
 
 #ifdef DEBUG
        __CTRACE("Adding character %s of type %d, symbol 0x%x\n", unctrl(chr),
                 key_type, symbol);
 #endif
        if (current->mapping[(unsigned char) chr] < 0) {
-                 /* first time for this char */
-               current->mapping[(unsigned char) chr] = current->count; /* map new entry */
-                 /* 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);
+               if (current->mapping[(unsigned char) chr] == MAPPING_UNUSED) {
+                         /* first time for this char */
+                       current->mapping[(unsigned char) chr] =
+                               current->count; /* map new entry */
+                       ki = current->count;
+                       
+                         /* make sure we have room in the key array first */
+                       if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
+                       {
+                               if ((current->key =
+                                    realloc(current->key,
+                                            ki * 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[ki + i] = &the_key[i];
+                               }
                        }
-                       
-                       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];
+                } else {
+                         /* the mapping was used but freed, reuse it */
+                       ki = - current->mapping[(unsigned char) chr];
+                       current->mapping[(unsigned char) chr] = ki;
+               }
+               
+               current->count++;
+               
+                 /* point at the current key array element to use */
+               the_key = current->key[ki];
                                                 
                the_key->type = key_type;
 
@@ -358,6 +377,7 @@
                          __CTRACE("Creating new keymap\n");
 #endif
                          the_key->value.next = new_keymap();
+                         the_key->enable = TRUE;
                          break;
 
                  case KEYMAP_LEAF:
@@ -366,14 +386,13 @@
                          __CTRACE("Adding leaf key\n");
 #endif
                          the_key->value.symbol = symbol;
+                         the_key->enable = TRUE;
                          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



Home | Main Index | Thread Index | Old Index