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[] = {
39 * The system has at least one "root" device representing the main display
40 * (which may be the null display). This device is part of the display_driver
41 * and generates events with no input_driver. The root device has no id
42 * as such (for the purposes of event names)
44 * The system may also have one or more auxilliary devices which each have
45 * an input_driver and an id (eg "JS0"). So the keysym "Return" is (de)coded by
46 * the root device, and the keysym "JS0: Button0" is (de)coded by the JS0 input
47 * device as "Button0".
49 * For the moment, mice are handled specially, as they behave a little
50 * differently from other devices (although this will probably change in the
55 typedef struct keymap_entry {
56 input_key_callback_t callback;
59 struct keymap_entry *next; // allow chaining
62 typedef struct mouse_entry {
64 input_mouse_callback_t callback;
66 struct mouse_entry *next;
69 typedef struct input_driver_entry {
70 input_driver_t driver;
72 struct keymap_entry *keymap[0];
73 } *input_driver_entry_t;
76 * Colour format information
78 struct colour_format colour_formats[] = {
79 { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
80 { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
81 { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
82 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
83 { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
84 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
85 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
86 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
87 { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
88 { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
93 * FIXME: make this more memory efficient
95 static struct keymap_entry *root_keymap[65535];
96 static struct keymap_entry *keyhooks = NULL;
97 static gboolean display_focused = TRUE;
98 static GList *input_drivers= NULL;
99 static display_keysym_callback_t display_keysym_hook = NULL;
100 void *display_keysym_hook_data;
102 gboolean input_register_device( input_driver_t driver, uint16_t max_keycode )
105 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
106 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
107 if( strcasecmp( entry->driver->id, driver->id ) == 0 ) {
112 input_driver_entry_t entry = g_malloc0( sizeof(struct input_driver_entry) + (sizeof(keymap_entry_t) * max_keycode) );
113 entry->driver = driver;
114 entry->entry_count = max_keycode;
115 input_drivers = g_list_append( input_drivers, entry );
119 gboolean input_has_device( const gchar *id )
122 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
123 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
124 if( strcasecmp(entry->driver->id, id) == 0 ) {
132 void input_unregister_device( input_driver_t driver )
135 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
136 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
137 if( entry->driver == driver ) {
138 if( driver->destroy != NULL ) {
139 driver->destroy(driver);
141 input_drivers = g_list_remove(input_drivers, (gpointer)entry);
149 * Resolve the keysym and return a pointer to the keymap entry pointer
150 * @return keymap pointer or NULL if the key was unresolved
152 static struct keymap_entry **input_entry_from_keysym( const gchar *keysym )
154 if( keysym == NULL || keysym[0] == 0 ) {
157 char **strv = g_strsplit(keysym,":",2);
158 if( strv[1] == NULL ) {
160 if( display_driver == NULL || display_driver->resolve_keysym == NULL) {
161 // Root device has no input handling
165 uint16_t keycode = display_driver->resolve_keysym(g_strstrip(strv[0]));
170 return &root_keymap[keycode-1];
173 char *id = g_strstrip(strv[0]);
175 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
176 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
177 if( strcasecmp( entry->driver->id, id ) == 0 ) {
178 /* we have ze device */
179 if( entry->driver->resolve_keysym == NULL ) {
183 uint16_t keycode = entry->driver->resolve_keysym(entry->driver, g_strstrip(strv[1]));
185 if( keycode == 0 || keycode > entry->entry_count ) {
188 return &entry->keymap[keycode-1];
192 return NULL; // device not found
196 static struct keymap_entry **input_entry_from_keycode( input_driver_t driver, uint16_t keycode )
204 if( driver == NULL ) {
205 return &root_keymap[keycode-1];
208 for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
209 input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
210 if( entry->driver == driver ) {
211 if( keycode > entry->entry_count ) {
214 return &entry->keymap[keycode-1];
221 gchar *input_keycode_to_keysym( input_driver_t driver, uint16_t keycode )
226 if( driver == NULL ) {
227 if( display_driver != NULL && display_driver->get_keysym_for_keycode != NULL ) {
228 return display_driver->get_keysym_for_keycode(keycode);
230 } else if( driver->get_keysym_for_keycode ) {
231 gchar *sym = driver->get_keysym_for_keycode(driver,keycode);
233 gchar *result = g_strdup_printf( "%s: %s", driver->id, sym );
242 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
243 void *data, uint32_t value )
245 if( keysym == NULL ) {
249 gchar **strv = g_strsplit(keysym, ",", 16);
251 while( *s != NULL ) {
252 keymap_entry_t *entryp = input_entry_from_keysym(*s);
253 if( entryp != NULL ) {
254 *entryp = g_malloc0(sizeof(struct keymap_entry));
255 (*entryp)->callback = callback;
256 (*entryp)->data = data;
257 (*entryp)->value = value;
266 void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
267 void *data, uint32_t value )
269 if( keysym == NULL ) {
273 gchar **strv = g_strsplit(keysym, ",", 16);
275 while( *s != NULL ) {
276 keymap_entry_t *entryp = input_entry_from_keysym(*s);
277 if( entryp != NULL && *entryp != NULL && (*entryp)->callback == callback &&
278 (*entryp)->data == data && (*entryp)->value == value ) {
287 gboolean input_register_keyboard_hook( input_key_callback_t callback,
290 keymap_entry_t key = malloc( sizeof( struct keymap_entry ) );
291 assert( key != NULL );
292 key->callback = callback;
294 key->next = keyhooks;
299 void input_unregister_keyboard_hook( input_key_callback_t callback,
302 keymap_entry_t key = keyhooks;
304 if( key->callback == callback && key->data == data ) {
305 keyhooks = keyhooks->next;
309 while( key->next != NULL ) {
310 if( key->next->callback == callback && key->next->data == data ) {
311 keymap_entry_t next = key->next;
312 key->next = next->next;
321 gboolean input_is_key_valid( const gchar *keysym )
323 keymap_entry_t *ptr = input_entry_from_keysym(keysym);
327 gboolean input_is_key_registered( const gchar *keysym )
329 keymap_entry_t *ptr = input_entry_from_keysym(keysym);
330 return ptr != NULL && *ptr != NULL;
333 void input_event_keydown( input_driver_t driver, uint16_t keycode, uint32_t pressure )
335 if( display_focused ) {
336 keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
337 if( entryp != NULL && *entryp != NULL ) {
338 (*entryp)->callback( (*entryp)->data, (*entryp)->value, pressure, TRUE );
340 if( driver == NULL ) {
341 keymap_entry_t key = keyhooks;
342 while( key != NULL ) {
343 key->callback( key->data, keycode, pressure, TRUE );
348 if( display_keysym_hook != NULL ) {
349 gchar *sym = input_keycode_to_keysym( driver, keycode );
351 display_keysym_hook(display_keysym_hook_data, sym);
357 void input_event_keyup( input_driver_t driver, uint16_t keycode, uint32_t pressure )
359 if( display_focused ) {
360 keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
361 if( entryp != NULL && *entryp != NULL ) {
362 (*entryp)->callback( (*entryp)->data, (*entryp)->value, pressure, FALSE );
365 if( driver == NULL ) {
366 keymap_entry_t key = keyhooks;
367 while( key != NULL ) {
368 key->callback( key->data, keycode, pressure, FALSE );
375 uint16_t input_keycode_to_dckeysym( uint16_t keycode )
377 return display_driver->convert_to_dckeysym(keycode);
380 void input_set_keysym_hook( display_keysym_callback_t hook, void *data )
382 display_keysym_hook = hook;
383 display_keysym_hook_data = data;
386 /***************** System mouse driver ****************/
388 static struct keymap_entry *mouse_keymap[MAX_MOUSE_BUTTONS];
389 static struct mouse_entry *mousehooks = NULL;
390 static uint32_t mouse_x = -1, mouse_y = -1, mouse_buttons = 0;
392 uint16_t mouse_resolve_keysym( struct input_driver *driver, const gchar *keysym )
394 if( strncasecmp( keysym, "Button", 6 ) == 0 ){
395 unsigned long button = strtoul( keysym+6, NULL, 10 );
396 if( button > MAX_MOUSE_BUTTONS ) {
399 return (uint16_t)button;
404 gchar *mouse_get_keysym( struct input_driver *driver, uint16_t keycode )
406 return g_strdup_printf( "Button%d", (keycode) );
409 struct input_driver system_mouse_driver = { "Mouse", mouse_resolve_keysym, NULL, mouse_get_keysym, NULL };
412 gboolean input_register_mouse_hook( gboolean relative, input_mouse_callback_t callback,
415 mouse_entry_t ent = malloc( sizeof( struct mouse_entry ) );
416 assert( ent != NULL );
417 ent->callback = callback;
419 ent->next = mousehooks;
424 void input_unregister_mouse_hook( input_mouse_callback_t callback, void *data )
426 mouse_entry_t ent = mousehooks;
428 if( ent->callback == callback && ent->data == data ) {
429 mousehooks = mousehooks->next;
433 while( ent->next != NULL ) {
434 if( ent->next->callback == callback && ent->next->data == data ) {
435 mouse_entry_t next = ent->next;
436 ent->next = next->next;
445 void input_event_run_mouse_hooks( uint32_t buttons, int32_t x, int32_t y, gboolean absolute )
447 mouse_entry_t ent = mousehooks;
448 while( ent != NULL ) {
449 ent->callback(ent->data, buttons, x, y, absolute);
454 void input_event_mousedown( uint16_t button, int32_t x, int32_t y, gboolean absolute )
460 mouse_buttons |= (1<<button);
461 input_event_keydown( &system_mouse_driver, button+1, 1 );
462 input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
465 void input_event_mouseup( uint16_t button, int32_t x, int32_t y, gboolean absolute )
471 mouse_buttons &= ~(1<<button);
472 input_event_keyup( &system_mouse_driver, button+1, 1 );
473 input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
476 void input_event_mousemove( int32_t x, int32_t y, gboolean absolute )
482 input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
485 /************************ Main display driver *************************/
487 void print_display_drivers( FILE *out )
490 fprintf( out, "Available video drivers:\n" );
491 for( i=0; display_driver_list[i] != NULL; i++ ) {
492 fprintf( out, " %-8s %s\n", display_driver_list[i]->name,
493 gettext(display_driver_list[i]->description) );
497 display_driver_t get_display_driver_by_name( const char *name )
501 return display_driver_list[0];
503 for( i=0; display_driver_list[i] != NULL; i++ ) {
504 if( strcasecmp( display_driver_list[i]->name, name ) == 0 ) {
505 return display_driver_list[i];
513 gboolean display_set_driver( display_driver_t driver )
516 if( display_driver != NULL && display_driver->shutdown_driver != NULL )
517 display_driver->shutdown_driver();
519 display_driver = driver;
520 if( driver->init_driver != NULL )
521 rv = driver->init_driver();
523 input_register_device(&system_mouse_driver, MAX_MOUSE_BUTTONS);
525 display_driver = NULL;
530 void display_set_focused( gboolean has_focus )
532 display_focused = has_focus;
.