Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 477:9a373f2ff009
prev451:50622730f226
next531:f0fee3ba71d1
author nkeynes
date Wed Nov 14 10:18:21 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Add portugese and german translations (thanks to GT and TK)
view annotate diff log raw
     1 /**
     2  * $Id: display.c,v 1.12 2007-10-31 09:10:23 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 },
    44     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
    45     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
    46     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
    47     { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
    49 };
    51 /**
    52  * FIXME: make this more memory efficient
    53  */
    54 struct keymap_entry *keymap[65536];
    57 static struct keymap_entry *input_create_key( uint16_t keycode )
    58 {
    59     struct keymap_entry *key = keymap[ keycode ];
    60     if( key == NULL ) {
    61 	key = malloc( sizeof( struct keymap_entry ) );
    62 	assert( key != NULL );
    63 	keymap[ keycode ] = key;
    64 	key->keycode = keycode;
    65     }
    66     return key;
    67 }
    69 static void input_delete_key( uint16_t keycode, input_key_callback_t callback, void *data,
    70 			      uint32_t value )
    71 {
    72     struct keymap_entry *key = keymap[keycode];
    73     if( key != NULL && key->callback == callback && key->data == data && key->value == value ) {
    74 	free( key );
    75 	keymap[keycode] = NULL;
    76     }
    77 }
    79 static struct keymap_entry *input_get_key( uint16_t keycode )
    80 {
    81     return keymap[ keycode ];
    82 }
    84 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
    85 			     void *data, uint32_t value )
    86 {
    87     if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
    88 	return FALSE; /* No display driver */
    89     gchar **strv = g_strsplit(keysym, ",", 16);
    90     gchar **s = strv;
    91     while( *s != NULL ) {
    92 	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
    93 	if( keycode == 0 )
    94 	    return FALSE; /* Invalid keysym */
    96 	struct keymap_entry *key = input_create_key( keycode );
    97 	key->callback = callback;
    98 	key->data = data;
    99 	key->value = value;
   100 	s++;
   101     }
   102     g_strfreev(strv);
   103     return TRUE;
   104 }
   106 void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
   107 			   void *data, uint32_t value )
   108 {
   109     if( display_driver == NULL || keysym == NULL )
   110 	return;
   111     uint16_t keycode = display_driver->resolve_keysym(keysym);
   112     if( keycode == 0 )
   113 	return;
   114     input_delete_key( keycode, callback, data, value );
   115 }
   118 gboolean input_is_key_valid( const gchar *keysym )
   119 {
   120     if( display_driver == NULL )
   121 	return FALSE; /* No display driver */
   122     return display_driver->resolve_keysym(keysym) != 0;
   123 }
   125 gboolean input_is_key_registered( const gchar *keysym )
   126 {
   127     if( display_driver == NULL )
   128 	return FALSE;
   129     uint16_t keycode = display_driver->resolve_keysym(keysym);
   130     if( keycode == 0 )
   131 	return FALSE;
   132     return input_get_key( keycode ) != NULL;
   133 }
   135 void input_event_keydown( uint16_t keycode )
   136 {
   137     struct keymap_entry *key = input_get_key(keycode);
   138     if( key != NULL ) {
   139 	key->callback( key->data, key->value, TRUE );
   140     }	
   141 }
   143 void input_event_keyup( uint16_t keycode )
   144 {
   145     struct keymap_entry *key = input_get_key(keycode);
   146     if( key != NULL ) {
   147 	key->callback( key->data, key->value, FALSE );
   148     }
   149 }
   153 gboolean display_set_driver( display_driver_t driver )
   154 {
   155     gboolean rv = TRUE;
   156     if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
   157 	display_driver->shutdown_driver();
   159     display_driver = driver;
   160     if( driver->init_driver != NULL )
   161 	rv = driver->init_driver();
   162     if( rv ) {
   163 	texcache_gl_init();
   164     }
   165     return rv;
   166 }
.