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