Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 451:50622730f226
prev431:248dd77a9e44
next477:9a373f2ff009
author nkeynes
date Sun Oct 21 05:15:56 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Unregister keys on controller detach
view annotate diff log raw
     1 /**
     2  * $Id: display.c,v 1.11 2007-10-21 05:15:56 nkeynes Exp $
     3  *
     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).
     7  *
     8  * Copyright (c) 2005 Nathan Keynes.
     9  *
    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.
    14  *
    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.
    19  */
    21 #include <stdint.h>
    22 #include <assert.h>
    23 #include "dream.h"
    24 #include "display.h"
    25 #include "pvr2/pvr2.h"
    27 typedef struct keymap_entry {
    28     uint16_t keycode;
    29     input_key_callback_t callback;
    30     void *data;
    31     uint32_t value;
    32 } *keymap_entry_t;
    34 /**
    35  * Colour format information
    36  */
    37 struct colour_format colour_formats[] = {
    38     { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
    39     { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
    40     { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
    41     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
    42     { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
    43     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
    45 };
    47 /**
    48  * FIXME: make this more memory efficient
    49  */
    50 struct keymap_entry *keymap[65536];
    53 static struct keymap_entry *input_create_key( uint16_t keycode )
    54 {
    55     struct keymap_entry *key = keymap[ keycode ];
    56     if( key == NULL ) {
    57 	key = malloc( sizeof( struct keymap_entry ) );
    58 	assert( key != NULL );
    59 	keymap[ keycode ] = key;
    60 	key->keycode = keycode;
    61     }
    62     return key;
    63 }
    65 static void input_delete_key( uint16_t keycode, input_key_callback_t callback, void *data,
    66 			      uint32_t value )
    67 {
    68     struct keymap_entry *key = keymap[keycode];
    69     if( key != NULL && key->callback == callback && key->data == data && key->value == value ) {
    70 	free( key );
    71 	keymap[keycode] = NULL;
    72     }
    73 }
    75 static struct keymap_entry *input_get_key( uint16_t keycode )
    76 {
    77     return keymap[ keycode ];
    78 }
    80 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
    81 			     void *data, uint32_t value )
    82 {
    83     if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
    84 	return FALSE; /* No display driver */
    85     gchar **strv = g_strsplit(keysym, ",", 16);
    86     gchar **s = strv;
    87     while( *s != NULL ) {
    88 	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
    89 	if( keycode == 0 )
    90 	    return FALSE; /* Invalid keysym */
    92 	struct keymap_entry *key = input_create_key( keycode );
    93 	key->callback = callback;
    94 	key->data = data;
    95 	key->value = value;
    96 	s++;
    97     }
    98     g_strfreev(strv);
    99     return TRUE;
   100 }
   102 void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
   103 			   void *data, uint32_t value )
   104 {
   105     if( display_driver == NULL || keysym == NULL )
   106 	return;
   107     uint16_t keycode = display_driver->resolve_keysym(keysym);
   108     if( keycode == 0 )
   109 	return;
   110     input_delete_key( keycode, callback, data, value );
   111 }
   114 gboolean input_is_key_valid( const gchar *keysym )
   115 {
   116     if( display_driver == NULL )
   117 	return FALSE; /* No display driver */
   118     return display_driver->resolve_keysym(keysym) != 0;
   119 }
   121 gboolean input_is_key_registered( const gchar *keysym )
   122 {
   123     if( display_driver == NULL )
   124 	return FALSE;
   125     uint16_t keycode = display_driver->resolve_keysym(keysym);
   126     if( keycode == 0 )
   127 	return FALSE;
   128     return input_get_key( keycode ) != NULL;
   129 }
   131 void input_event_keydown( uint16_t keycode )
   132 {
   133     struct keymap_entry *key = input_get_key(keycode);
   134     if( key != NULL ) {
   135 	key->callback( key->data, key->value, TRUE );
   136     }	
   137 }
   139 void input_event_keyup( uint16_t keycode )
   140 {
   141     struct keymap_entry *key = input_get_key(keycode);
   142     if( key != NULL ) {
   143 	key->callback( key->data, key->value, FALSE );
   144     }
   145 }
   149 gboolean display_set_driver( display_driver_t driver )
   150 {
   151     gboolean rv = TRUE;
   152     if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
   153 	display_driver->shutdown_driver();
   155     display_driver = driver;
   156     if( driver->init_driver != NULL )
   157 	rv = driver->init_driver();
   158     if( rv ) {
   159 	texcache_gl_init();
   160     }
   161     return rv;
   162 }
.