1 /* $Id: keyboard.h,v 1.5 2007/02/15 01:27:37 fredette Exp $ */
2 
3 /* tme/generic/keyboard.h - header file for generic keyboard support: */
4 
5 /*
6  * Copyright (c) 2003 Matt Fredette
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Matt Fredette.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _TME_GENERIC_KEYBOARD_H
37 #define _TME_GENERIC_KEYBOARD_H
38 
39 #include <tme/common.h>
40 _TME_RCSID("$Id: keyboard.h,v 1.5 2007/02/15 01:27:37 fredette Exp $");
41 
42 /* includes: */
43 #include <tme/element.h>
44 
45 /* macros: */
46 
47 /* the undefined keyval: */
48 #define TME_KEYBOARD_KEYVAL_UNDEF	(0xffffffff)
49 
50 /* modifiers: */
51 #define TME_KEYBOARD_MODIFIER_NONE		(-1)
52 #define TME_KEYBOARD_MODIFIER_SHIFT		(0)
53 #define TME_KEYBOARD_MODIFIER_LOCK		(1)
54 #define TME_KEYBOARD_MODIFIER_CONTROL		(2)
55 #define TME_KEYBOARD_MODIFIER_MOD1		(3)
56 #define TME_KEYBOARD_MODIFIER_MOD2		(4)
57 #define TME_KEYBOARD_MODIFIER_MOD3		(5)
58 #define TME_KEYBOARD_MODIFIER_MOD4		(6)
59 #define TME_KEYBOARD_MODIFIER_MOD5		(7)
60 #define TME_KEYBOARD_MODIFIER_MAX		TME_KEYBOARD_MODIFIER_MOD5
61 
62 /* keysym notes: */
63 #define TME_KEYBOARD_KEYSYM_NOTE_UNDEF		(0)
64 #define TME_KEYBOARD_KEYSYM_NOTE_CAPS_LOCK	(1)
65 #define TME_KEYBOARD_KEYSYM_NOTE_SHIFT_LOCK	(2)
66 #define TME_KEYBOARD_KEYSYM_NOTE_NUM_LOCK	(3)
67 
68 /* lookup flags: */
69 #define TME_KEYBOARD_LOOKUP_FLAG_OK_DIRECT	TME_BIT(0)
70 #define TME_KEYBOARD_LOOKUP_FLAG_OK_ALLOC	TME_BIT(1)
71 #define TME_KEYBOARD_LOOKUP_FLAG_OK_ALLOC_NOW	TME_BIT(2)
72 
73 /* event types: */
74 #define TME_KEYBOARD_EVENT_PRESS		(1)
75 #define TME_KEYBOARD_EVENT_RELEASE		(0)
76 
77 /* keyboard controls: */
78 #define TME_KEYBOARD_CTRL_OK_READ		TME_BIT(0)
79 #define TME_KEYBOARD_CTRL_ALL_UP		TME_BIT(1)
80 #define TME_KEYBOARD_CTRL_BELL			TME_BIT(2)
81 #define TME_KEYBOARD_CTRL_LED0			TME_BIT(3)
82 #define TME_KEYBOARD_CTRL_LED1			TME_BIT(4)
83 #define TME_KEYBOARD_CTRL_LED2			TME_BIT(5)
84 #define TME_KEYBOARD_CTRL_LED3			TME_BIT(6)
85 
86 /* keyboard modes: */
87 #define TME_KEYBOARD_MODE_GLOBAL		(0)
88 #define TME_KEYBOARD_MODE_UNLOCK		TME_BIT(0)
89 #define TME_KEYBOARD_MODE_LOCK			TME_BIT(1)
90 #define TME_KEYBOARD_MODE_PASSTHROUGH		TME_BIT(2)
91 #define  TME_KEYBOARD_MODE_FLAG_NO_AUTOREPEATS	TME_BIT(3)
92 #define  TME_KEYBOARD_MODE_FLAG_NO_RELEASES	TME_BIT(4)
93 #define  TME_KEYBOARD_MODE_FLAG_LOCK_SOFT	TME_BIT(5)
94 
95 /* the undefined event time: */
96 #define TME_KEYBOARD_EVENT_TIME_UNDEF		(0)
97 
98 /* this evaluates to nonzero iff a buffer is empty: */
99 #define tme_keyboard_buffer_is_empty(b)		\
100   ((b)->tme_keyboard_buffer_head		\
101    == ((b)->tme_keyboard_buffer_tail))
102 
103 /* this evaluates to nonzero iff a buffer is full: */
104 #define tme_keyboard_buffer_is_full(b)		\
105   ((((b)->tme_keyboard_buffer_head + 1)		\
106     & ((b)->tme_keyboard_buffer_size - 1))	\
107    == (b)->tme_keyboard_buffer_tail)
108 
109 /* this convert between a tme_hash_data_t and a keyval: */
110 #define tme_keyboard_hash_data_from_keyval(x)	tme_hash_data_from_uint32(x)
111 #define tme_keyboard_hash_data_to_keyval(x)	tme_hash_data_to_uint32(x)
112 
113 /* types: */
114 
115 /* a modifiers mask: */
116 typedef tme_uint8_t tme_keyboard_modifiers_t;
117 
118 /* a keyval - a keysym or a keycode: */
119 typedef tme_uint32_t tme_keyboard_keyval_t;
120 
121 /* keyboard lookup information: */
122 struct tme_keyboard_lookup {
123 
124   /* the string name being looked up: */
125   _tme_const char *tme_keyboard_lookup_string;
126 
127   /* flags for the lookup: */
128   unsigned int tme_keyboard_lookup_flags;
129 
130   /* any context that can be used to associate lookups: */
131   unsigned int tme_keyboard_lookup_context_length;
132   _tme_const tme_uint8_t *tme_keyboard_lookup_context;
133 };
134 
135 /* a keyboard keysym lookup function: */
136 typedef tme_keyboard_keyval_t (*tme_keyboard_keysym_lookup_t)
137      _TME_P((void *, _tme_const struct tme_keyboard_lookup *));
138 
139 /* one entry of a keyboard map: */
140 struct tme_keyboard_map {
141 
142   /* the keysym: */
143   tme_keyboard_keyval_t tme_keyboard_map_keysym;
144 
145   /* any special keysym note: */
146   unsigned int tme_keyboard_map_keysym_note;
147 
148   /* the keycode of the key with the keysym: */
149   tme_keyboard_keyval_t tme_keyboard_map_keycode;
150 
151   /* any single modifier that this keycode triggers.  if this is
152      nonzero then the set and clear modifiers must be zero: */
153   int tme_keyboard_map_modifier;
154 
155   /* any modifiers that must be set or clear for in order for the
156      keycode to generate this keyval: */
157   tme_keyboard_modifiers_t tme_keyboard_map_modifiers_set;
158   tme_keyboard_modifiers_t tme_keyboard_map_modifiers_clear;
159 };
160 
161 /* a keyboard event: */
162 struct tme_keyboard_event {
163 
164   /* the type of the event: */
165   int tme_keyboard_event_type;
166 
167   /* the modifier mask immediately before this event: */
168   tme_keyboard_modifiers_t tme_keyboard_event_modifiers;
169 
170   /* the keysym or keycode: */
171   tme_keyboard_keyval_t tme_keyboard_event_keyval;
172 
173   /* any keycode associated with a keysym: */
174   tme_keyboard_keyval_t tme_keyboard_event_keycode;
175 
176   /* the time of the event: */
177   tme_uint32_t tme_keyboard_event_time;
178 };
179 
180 /* a keyboard buffer.  it is assumed that the user provides locking: */
181 struct tme_keyboard_buffer {
182 
183   /* the buffer size.  this must always be a power of two: */
184   unsigned int tme_keyboard_buffer_size;
185 
186   /* the head and tail pointers: */
187   unsigned int tme_keyboard_buffer_head;
188   unsigned int tme_keyboard_buffer_tail;
189 
190   /* the buffered events: */
191   struct tme_keyboard_event *tme_keyboard_buffer_events;
192 
193   /* any log handle: */
194   struct tme_log_handle *tme_keyboard_buffer_log_handle;
195 };
196 
197 /* a keyboard connection: */
198 struct tme_keyboard_connection {
199 
200   /* the generic connection side: */
201   struct tme_connection tme_keyboard_connection;
202 
203   /* this is called when control lines change: */
204   int (*tme_keyboard_connection_ctrl) _TME_P((struct tme_keyboard_connection *,
205 					      unsigned int));
206 
207   /* this is called to read an event: */
208   int (*tme_keyboard_connection_read) _TME_P((struct tme_keyboard_connection *,
209 					      struct tme_keyboard_event *));
210 
211   /* this is called to lookup the keyval for a string: */
212   tme_keyboard_keyval_t (*tme_keyboard_connection_lookup) _TME_P((struct tme_keyboard_connection *,
213 								  _tme_const struct tme_keyboard_lookup *));
214 };
215 
216 /* prototypes: */
217 struct tme_keyboard_buffer *tme_keyboard_buffer_new _TME_P((unsigned int));
218 void tme_keyboard_buffer_destroy _TME_P((struct tme_keyboard_buffer *));
219 int tme_keyboard_buffer_in_modifier _TME_P((struct tme_keyboard_buffer *,
220 					    int, _tme_const tme_keyboard_keyval_t *));
221 int tme_keyboard_buffer_in_mode _TME_P((struct tme_keyboard_buffer *,
222 					tme_keyboard_keyval_t, int));
223 int tme_keyboard_buffer_in_macro _TME_P((struct tme_keyboard_buffer *,
224 					 _tme_const tme_keyboard_keyval_t *,
225 					 _tme_const tme_keyboard_keyval_t *));
226 int tme_keyboard_buffer_out_map _TME_P((struct tme_keyboard_buffer *,
227 					_tme_const struct tme_keyboard_map *));
228 int tme_keyboard_buffer_out_mode _TME_P((struct tme_keyboard_buffer *,
229 					 tme_keyboard_keyval_t, int));
230 tme_keyboard_modifiers_t
231 tme_keyboard_buffer_out_modifiers _TME_P((struct tme_keyboard_buffer *,
232 					  tme_keyboard_modifiers_t,
233 					  tme_keyboard_modifiers_t));
234 int tme_keyboard_buffer_copyin _TME_P((struct tme_keyboard_buffer *,
235 				       _tme_const struct tme_keyboard_event *));
236 int tme_keyboard_buffer_copyout _TME_P((struct tme_keyboard_buffer *,
237 					struct tme_keyboard_event *));
238 int tme_keyboard_parse_macro _TME_P((_tme_const char *,
239 				     tme_keyboard_keysym_lookup_t,
240 				     void *,
241 				     tme_keyboard_keyval_t **,
242 				     tme_keyboard_keyval_t **));
243 int tme_keyboard_parse_map _TME_P((_tme_const char *,
244 				   tme_keyboard_keysym_lookup_t,
245 				   void *,
246 				   struct tme_keyboard_map *));
247 
248 #endif /* !_TME_GENERIC_KEYBOARD_H */
249