4 * Generic support for keyboard and other input sources. The active display
5 * driver is expected to deliver events here, where they're translated and
6 * passed to the appropriate dreamcast controllers (if any).
8 * Copyright (c) 2005 Nathan Keynes.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
25 #include "pvr2/pvr2.h"
27 display_driver_t display_driver_list[] = {
35 * The system has at least one "root" device representing the main display
36 * (which may be the null display). This device is part of the display_driver
37 * and generates events with no input_driver. The root device has no id
38 * as such (for the purposes of event names)
40 * The system may also have one or more auxilliary devices which each have
41 * an input_driver and an id (eg "JS0"). So the keysym "Return" is (de)coded by
42 * the root device, and the keysym "JS0: Button0" is (de)coded by the JS0 input
43 * device as "Button0".
45 * For the moment, mice are handled specially, as they behave a little
46 * differently from other devices (although this will probably change in the
51 typedef struct keymap_entry {
52 input_key_callback_t callback;
55 struct keymap_entry *next; // allow chaining
58 typedef struct mouse_entry {
60 input_mouse_callback_t callback;
62 struct mouse_entry *next;
65 typedef struct input_driver_entry {
66 input_driver_t driver;
68 struct keymap_entry *keymap[0];
69 } *input_driver_entry_t;
72 * Colour format information
74 struct colour_format colour_formats[] = {
75 { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
76 { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
77 { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
78 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
79 { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
80 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
81 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
82 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
83 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
84 { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
89 * FIXME: make this more memory efficient
91 static struct keymap_entry *root_keymap[65535];
92 static struct keymap_entry *keyhooks = NULL;
93 static struct mouse_entry *mousehooks = NULL;
94 static gboolean display_focused = TRUE;
95 static GList *input_drivers= NULL;
96 static display_keysym_callback_t display_keysym_hook = NULL;
97 void *display_keysym_hook_data;
99 gboolean input_register_device( input_driver_t driver, uint16_t max_keycode )
102 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
103 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
104 if( strcasecmp( entry->driver->id, driver->id ) == 0 ) {
109 input_driver_entry_t entry = g_malloc0( sizeof(struct input_driver_entry) + (sizeof(keymap_entry_t) * max_keycode) );
110 entry->driver = driver;
111 entry->entry_count = max_keycode;
112 input_drivers = g_list_append( input_drivers, entry );
116 gboolean input_has_device( const gchar *id )
119 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
120 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
121 if( strcasecmp(entry->driver->id, id) == 0 ) {
129 void input_unregister_device( input_driver_t driver )
132 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
133 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
134 if( entry->driver == driver ) {
135 if( driver->destroy != NULL ) {
136 driver->destroy(driver);
138 input_drivers = g_list_remove(input_drivers, (gpointer)entry);
146 * Resolve the keysym and return a pointer to the keymap entry pointer
147 * @return keymap pointer or NULL if the key was unresolved
149 static struct keymap_entry **input_entry_from_keysym( const gchar *keysym )
151 if( keysym == NULL || keysym[0] == 0 ) {
154 char **strv = g_strsplit(keysym,":",2);
155 if( strv[1] == NULL ) {
157 if( display_driver == NULL || display_driver->resolve_keysym == NULL) {
158 // Root device has no input handling
162 uint16_t keycode = display_driver->resolve_keysym(g_strstrip(strv[0]));
167 return &root_keymap[keycode-1];
169 char *id = g_strstrip(strv[0]);
171 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
172 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
173 if( strcasecmp( entry->driver->id, id ) == 0 ) {
174 /* we have ze device */
175 if( entry->driver->resolve_keysym == NULL ) {
179 uint16_t keycode = entry->driver->resolve_keysym(entry->driver, g_strstrip(strv[1]));
181 if( keycode == 0 || keycode > entry->entry_count ) {
184 return &entry->keymap[keycode-1];
188 return NULL; // device not found
192 static struct keymap_entry **input_entry_from_keycode( input_driver_t driver, uint16_t keycode )
200 if( driver == NULL ) {
201 return &root_keymap[keycode-1];
204 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
205 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
206 if( entry->driver == driver ) {
207 if( keycode > entry->entry_count ) {
210 return &entry->keymap[keycode-1];
217 static gchar *input_keysym_for_keycode( input_driver_t driver, uint16_t keycode )
222 if( driver == NULL ) {
223 if( display_driver != NULL && display_driver->get_keysym_for_keycode != NULL ) {
224 return display_driver->get_keysym_for_keycode(keycode);
226 } else if( driver->get_keysym_for_keycode ) {
227 gchar *sym = driver->get_keysym_for_keycode(driver,keycode);
229 gchar *result = g_strdup_printf( "%s: %s", driver->id, sym );
238 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
239 void *data, uint32_t value )
241 if( keysym == NULL ) {
245 gchar **strv = g_strsplit(keysym, ",", 16);
247 while( *s != NULL ) {
248 keymap_entry_t *entryp = input_entry_from_keysym(*s);
249 if( entryp != NULL ) {
250 *entryp = g_malloc0(sizeof(struct keymap_entry));
251 (*entryp)->callback = callback;
252 (*entryp)->data = data;
253 (*entryp)->value = value;
262 void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
263 void *data, uint32_t value )
265 if( keysym == NULL ) {
269 gchar **strv = g_strsplit(keysym, ",", 16);
271 while( *s != NULL ) {
272 keymap_entry_t *entryp = input_entry_from_keysym(*s);
273 if( entryp != NULL && *entryp != NULL && (*entryp)->callback == callback &&
274 (*entryp)->data == data && (*entryp)->value == value ) {
283 gboolean input_register_hook( input_key_callback_t callback,
286 keymap_entry_t key = malloc( sizeof( struct keymap_entry ) );
287 assert( key != NULL );
288 key->callback = callback;
290 key->next = keyhooks;
295 void input_unregister_hook( input_key_callback_t callback,
298 keymap_entry_t key = keyhooks;
300 keymap_entry_t next = key->next;
301 if( key->callback == callback && key->data == data ) {
306 while( next != NULL ) {
307 if( next->callback == callback && next->data == data ) {
308 key->next = next->next;
315 gboolean input_register_mouse_hook( gboolean relative, input_mouse_callback_t callback,
318 mouse_entry_t ent = malloc( sizeof( struct mouse_entry ) );
319 assert( ent != NULL );
320 ent->callback = callback;
322 ent->next = mousehooks;
327 void input_unregister_mouse_hook( input_mouse_callback_t callback, void *data )
329 mouse_entry_t ent = mousehooks;
331 mouse_entry_t next = ent->next;
332 if( ent->callback == callback && ent->data == data ) {
337 while( next != NULL ) {
338 if( next->callback == callback && next->data == data ) {
339 ent->next = next->next;
346 void input_event_mouse( uint32_t buttons, int32_t x, int32_t y )
348 mouse_entry_t ent = mousehooks;
349 while( ent != NULL ) {
350 ent->callback(ent->data, buttons, x, y);
355 gboolean input_is_key_valid( const gchar *keysym )
357 keymap_entry_t *ptr = input_entry_from_keysym(keysym);
361 gboolean input_is_key_registered( const gchar *keysym )
363 keymap_entry_t *ptr = input_entry_from_keysym(keysym);
364 return ptr != NULL && *ptr != NULL;
367 void input_event_keydown( input_driver_t driver, uint16_t keycode, uint32_t pressure )
369 if( display_focused ) {
370 keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
371 if( entryp != NULL && *entryp != NULL ) {
372 (*entryp)->callback( (*entryp)->data, (*entryp)->value, pressure, TRUE );
374 keymap_entry_t key = keyhooks;
375 while( key != NULL ) {
376 key->callback( key->data, keycode, pressure, TRUE );
380 if( display_keysym_hook != NULL ) {
381 gchar *sym = input_keysym_for_keycode( driver, keycode );
383 display_keysym_hook(display_keysym_hook_data, sym);
389 void input_event_keyup( input_driver_t driver, uint16_t keycode, uint32_t pressure )
391 if( display_focused ) {
392 keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
393 if( entryp != NULL && *entryp != NULL ) {
394 (*entryp)->callback( (*entryp)->data, (*entryp)->value, pressure, FALSE );
397 keymap_entry_t key = keyhooks;
398 while( key != NULL ) {
399 key->callback( key->data, keycode, pressure, FALSE );
405 uint16_t input_keycode_to_dckeysym( uint16_t keycode )
407 return display_driver->convert_to_dckeysym(keycode);
410 display_driver_t get_display_driver_by_name( const char *name )
414 return display_driver_list[0];
416 for( i=0; display_driver_list[i] != NULL; i++ ) {
417 if( strcasecmp( display_driver_list[i]->name, name ) == 0 ) {
418 return display_driver_list[i];
426 gboolean display_set_driver( display_driver_t driver )
429 if( display_driver != NULL && display_driver->shutdown_driver != NULL )
430 display_driver->shutdown_driver();
432 display_driver = driver;
433 if( driver->init_driver != NULL )
434 rv = driver->init_driver();
436 display_driver = NULL;
441 void display_set_focused( gboolean has_focus )
443 display_focused = has_focus;
446 void input_set_keysym_hook( display_keysym_callback_t hook, void *data )
448 display_keysym_hook = hook;
449 display_keysym_hook_data = data;
.