xref: /titanic_51/usr/src/lib/libtecla/common/keytab.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #ifndef keytab_h
2*7c478bd9Sstevel@tonic-gate #define keytab_h
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate /*
5*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * All rights reserved.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
10*7c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
11*7c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
12*7c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
13*7c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
14*7c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
15*7c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
16*7c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
17*7c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20*7c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21*7c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
22*7c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23*7c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
24*7c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
25*7c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
26*7c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
27*7c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28*7c478bd9Sstevel@tonic-gate  *
29*7c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
30*7c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
31*7c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
32*7c478bd9Sstevel@tonic-gate  * of the copyright holder.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "libtecla.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*-----------------------------------------------------------------------*
40*7c478bd9Sstevel@tonic-gate  * This module defines a binary-search symbol table of key-bindings.     *
41*7c478bd9Sstevel@tonic-gate  *-----------------------------------------------------------------------*/
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * All key-binding functions are defined as follows.
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * Input:
47*7c478bd9Sstevel@tonic-gate  *  gl    GetLine *  The resource object of this library.
48*7c478bd9Sstevel@tonic-gate  *  count     int    A positive repeat count specified by the user,
49*7c478bd9Sstevel@tonic-gate  *                   or 1. Action functions should ignore this if
50*7c478bd9Sstevel@tonic-gate  *                   repeating the action multiple times isn't
51*7c478bd9Sstevel@tonic-gate  *                   appropriate.
52*7c478bd9Sstevel@tonic-gate  *  data     void *  A pointer to action-specific data,
53*7c478bd9Sstevel@tonic-gate  *                   cast to (void *).
54*7c478bd9Sstevel@tonic-gate  * Output:
55*7c478bd9Sstevel@tonic-gate  *  return    int    0 - OK.
56*7c478bd9Sstevel@tonic-gate  *                   1 - Error.
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate #define KT_KEY_FN(fn) int (fn)(GetLine *gl, int count, void *data)
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate typedef KT_KEY_FN(KtKeyFn);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * Allow the association of arbitrary callback data with each action
64*7c478bd9Sstevel@tonic-gate  * function.
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate typedef struct {
67*7c478bd9Sstevel@tonic-gate   KtKeyFn *fn;          /* The acion function */
68*7c478bd9Sstevel@tonic-gate   void *data;           /* A pointer to arbitrary data to be passed to */
69*7c478bd9Sstevel@tonic-gate                         /*  fn() whenever it is called. */
70*7c478bd9Sstevel@tonic-gate } KtAction;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * Enumerate the possible sources of key-bindings in order of decreasing
74*7c478bd9Sstevel@tonic-gate  * priority.
75*7c478bd9Sstevel@tonic-gate  */
76*7c478bd9Sstevel@tonic-gate typedef enum {
77*7c478bd9Sstevel@tonic-gate   KTB_USER,         /* This is a binding being set by the user */
78*7c478bd9Sstevel@tonic-gate   KTB_NORM,         /* This is the default binding set by the library */
79*7c478bd9Sstevel@tonic-gate   KTB_TERM,         /* This is a binding taken from the terminal settings */
80*7c478bd9Sstevel@tonic-gate /* The following entry must always be last */
81*7c478bd9Sstevel@tonic-gate   KTB_NBIND         /* The number of binding sources listed above */
82*7c478bd9Sstevel@tonic-gate } KtBinder;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * Define an entry of a key-binding binary symbol table.
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate typedef struct {
88*7c478bd9Sstevel@tonic-gate   char *keyseq;                /* The key sequence that triggers the macro */
89*7c478bd9Sstevel@tonic-gate   int nc;                      /* The number of characters in keyseq[] */
90*7c478bd9Sstevel@tonic-gate   KtAction actions[KTB_NBIND]; /* Bindings from different sources */
91*7c478bd9Sstevel@tonic-gate   int binder;                  /* The index of the highest priority element */
92*7c478bd9Sstevel@tonic-gate                                /*  of actions[] that has been assigned an */
93*7c478bd9Sstevel@tonic-gate                                /*  action function, or -1 if none have. */
94*7c478bd9Sstevel@tonic-gate } KeySym;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate /*
97*7c478bd9Sstevel@tonic-gate  * Provide an opaque type alias to the symbol table container.
98*7c478bd9Sstevel@tonic-gate  */
99*7c478bd9Sstevel@tonic-gate typedef struct KeyTab KeyTab;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * Create a new symbol table.
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate KeyTab *_new_KeyTab(void);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * Delete the symbol table.
108*7c478bd9Sstevel@tonic-gate  */
109*7c478bd9Sstevel@tonic-gate KeyTab *_del_KeyTab(KeyTab *kt);
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate int _kt_set_keybinding(KeyTab *kt, KtBinder binder,
112*7c478bd9Sstevel@tonic-gate 		       const char *keyseq, const char *action);
113*7c478bd9Sstevel@tonic-gate int _kt_set_keyfn(KeyTab *kt, KtBinder binder, const char *keyseq,
114*7c478bd9Sstevel@tonic-gate 		  KtKeyFn *fn, void *data);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate int _kt_set_action(KeyTab *kt, const char *action, KtKeyFn *fn, void *data);
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /*
119*7c478bd9Sstevel@tonic-gate  * Lookup the function that implements a given action.
120*7c478bd9Sstevel@tonic-gate  */
121*7c478bd9Sstevel@tonic-gate int _kt_lookup_action(KeyTab *kt, const char *action,
122*7c478bd9Sstevel@tonic-gate 		      KtKeyFn **fn, void **data);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate typedef enum {
125*7c478bd9Sstevel@tonic-gate   KT_EXACT_MATCH,   /* An exact match was found */
126*7c478bd9Sstevel@tonic-gate   KT_AMBIG_MATCH,   /* An ambiguous match was found */
127*7c478bd9Sstevel@tonic-gate   KT_NO_MATCH,      /* No match was found */
128*7c478bd9Sstevel@tonic-gate   KT_BAD_MATCH      /* An error occurred while searching */
129*7c478bd9Sstevel@tonic-gate } KtKeyMatch;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate KtKeyMatch _kt_lookup_keybinding(KeyTab *kt, const char *binary_keyseq,
132*7c478bd9Sstevel@tonic-gate 				 int nc, KeySym **matches, int *nmatch);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate  * Remove all key bindings that came from a specified source.
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate void _kt_clear_bindings(KeyTab *kt, KtBinder binder);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  * When installing an array of keybings each binding is defined by
141*7c478bd9Sstevel@tonic-gate  * an element of the following type:
142*7c478bd9Sstevel@tonic-gate  */
143*7c478bd9Sstevel@tonic-gate typedef struct {
144*7c478bd9Sstevel@tonic-gate   const char *keyseq;   /* The sequence of keys that trigger this binding */
145*7c478bd9Sstevel@tonic-gate   const char *action;   /* The name of the action function that is triggered */
146*7c478bd9Sstevel@tonic-gate } KtKeyBinding;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate /*
149*7c478bd9Sstevel@tonic-gate  * Merge an array of bindings with existing bindings.
150*7c478bd9Sstevel@tonic-gate  */
151*7c478bd9Sstevel@tonic-gate int _kt_add_bindings(KeyTab *kt, KtBinder binder, const KtKeyBinding *bindings,
152*7c478bd9Sstevel@tonic-gate 		     unsigned n);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * Get information about the last error in this module.
156*7c478bd9Sstevel@tonic-gate  */
157*7c478bd9Sstevel@tonic-gate const char *_kt_last_error(KeyTab *kt);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate #endif
160