Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 561:533f6b478071
prev541:6986c644d58a
next608:4f588e52bce0
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
view annotate diff log raw
     1 /**
     2  * $Id$
     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 display_driver_t display_driver_list[] = { 
    28 #ifdef HAVE_GTK
    29 					   &display_gtk_driver,
    30 #endif
    31 					   &display_null_driver,
    32 					   NULL };
    34 typedef struct keymap_entry {
    35     uint16_t keycode;
    36     input_key_callback_t callback;
    37     void *data;
    38     uint32_t value;
    39 } *keymap_entry_t;
    41 /**
    42  * Colour format information
    43  */
    44 struct colour_format colour_formats[] = {
    45     { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
    46     { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
    47     { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
    48     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
    49     { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
    50     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
    51     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
    52     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
    53     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
    54     { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
    56 };
    58 /**
    59  * FIXME: make this more memory efficient
    60  */
    61 struct keymap_entry *keymap[65536];
    64 static struct keymap_entry *input_create_key( uint16_t keycode )
    65 {
    66     struct keymap_entry *key = keymap[ keycode ];
    67     if( key == NULL ) {
    68 	key = malloc( sizeof( struct keymap_entry ) );
    69 	assert( key != NULL );
    70 	keymap[ keycode ] = key;
    71 	key->keycode = keycode;
    72     }
    73     return key;
    74 }
    76 static void input_delete_key( uint16_t keycode, input_key_callback_t callback, void *data,
    77 			      uint32_t value )
    78 {
    79     struct keymap_entry *key = keymap[keycode];
    80     if( key != NULL && key->callback == callback && key->data == data && key->value == value ) {
    81 	free( key );
    82 	keymap[keycode] = NULL;
    83     }
    84 }
    86 static struct keymap_entry *input_get_key( uint16_t keycode )
    87 {
    88     return keymap[ keycode ];
    89 }
    91 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
    92 			     void *data, uint32_t value )
    93 {
    94     if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
    95 	return FALSE; /* No display driver */
    96     gchar **strv = g_strsplit(keysym, ",", 16);
    97     gchar **s = strv;
    98     while( *s != NULL ) {
    99 	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
   100 	if( keycode == 0 )
   101 	    return FALSE; /* Invalid keysym */
   103 	struct keymap_entry *key = input_create_key( keycode );
   104 	key->callback = callback;
   105 	key->data = data;
   106 	key->value = value;
   107 	s++;
   108     }
   109     g_strfreev(strv);
   110     return TRUE;
   111 }
   113 void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
   114 			   void *data, uint32_t value )
   115 {
   116     if( display_driver == NULL || keysym == NULL )
   117 	return;
   118     uint16_t keycode = display_driver->resolve_keysym(keysym);
   119     if( keycode == 0 )
   120 	return;
   121     input_delete_key( keycode, callback, data, value );
   122 }
   125 gboolean input_is_key_valid( const gchar *keysym )
   126 {
   127     if( display_driver == NULL )
   128 	return FALSE; /* No display driver */
   129     return display_driver->resolve_keysym(keysym) != 0;
   130 }
   132 gboolean input_is_key_registered( const gchar *keysym )
   133 {
   134     if( display_driver == NULL )
   135 	return FALSE;
   136     uint16_t keycode = display_driver->resolve_keysym(keysym);
   137     if( keycode == 0 )
   138 	return FALSE;
   139     return input_get_key( keycode ) != NULL;
   140 }
   142 void input_event_keydown( uint16_t keycode )
   143 {
   144     struct keymap_entry *key = input_get_key(keycode);
   145     if( key != NULL ) {
   146 	key->callback( key->data, key->value, TRUE );
   147     }	
   148 }
   150 void input_event_keyup( uint16_t keycode )
   151 {
   152     struct keymap_entry *key = input_get_key(keycode);
   153     if( key != NULL ) {
   154 	key->callback( key->data, key->value, FALSE );
   155     }
   156 }
   158 display_driver_t get_display_driver_by_name( const char *name )
   159 {
   160     int i;
   161     if( name == NULL ) {
   162 	return display_driver_list[0];
   163     }
   164     for( i=0; display_driver_list[i] != NULL; i++ ) {
   165 	if( strcasecmp( display_driver_list[i]->name, name ) == 0 ) {
   166 	    return display_driver_list[i];
   167 	}
   168     }
   170     return NULL;
   171 }
   174 gboolean display_set_driver( display_driver_t driver )
   175 {
   176     gboolean rv = TRUE;
   177     if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
   178 	display_driver->shutdown_driver();
   180     display_driver = driver;
   181     if( driver->init_driver != NULL )
   182 	rv = driver->init_driver();
   183     if( !rv ) {
   184 	display_driver = NULL;
   185     }
   186     return rv;
   187 }
.