Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 531:f0fee3ba71d1
prev477:9a373f2ff009
next541:6986c644d58a
author nkeynes
date Mon Nov 19 08:47:39 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Move name-to-driver mappings to display.c and audio.c respectively
Move responsibility for invoking texcache_gl_init() to the driver
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 display_driver_t display_driver_list[] = { 
    28 					   &display_gtk_driver,
    29 					   &display_null_driver,
    30 					   NULL };
    32 typedef struct keymap_entry {
    33     uint16_t keycode;
    34     input_key_callback_t callback;
    35     void *data;
    36     uint32_t value;
    37 } *keymap_entry_t;
    39 /**
    40  * Colour format information
    41  */
    42 struct colour_format colour_formats[] = {
    43     { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
    44     { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
    45     { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
    46     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
    47     { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
    48     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
    49     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
    50     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
    51     { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
    52     { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
    54 };
    56 /**
    57  * FIXME: make this more memory efficient
    58  */
    59 struct keymap_entry *keymap[65536];
    62 static struct keymap_entry *input_create_key( uint16_t keycode )
    63 {
    64     struct keymap_entry *key = keymap[ keycode ];
    65     if( key == NULL ) {
    66 	key = malloc( sizeof( struct keymap_entry ) );
    67 	assert( key != NULL );
    68 	keymap[ keycode ] = key;
    69 	key->keycode = keycode;
    70     }
    71     return key;
    72 }
    74 static void input_delete_key( uint16_t keycode, input_key_callback_t callback, void *data,
    75 			      uint32_t value )
    76 {
    77     struct keymap_entry *key = keymap[keycode];
    78     if( key != NULL && key->callback == callback && key->data == data && key->value == value ) {
    79 	free( key );
    80 	keymap[keycode] = NULL;
    81     }
    82 }
    84 static struct keymap_entry *input_get_key( uint16_t keycode )
    85 {
    86     return keymap[ keycode ];
    87 }
    89 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
    90 			     void *data, uint32_t value )
    91 {
    92     if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
    93 	return FALSE; /* No display driver */
    94     gchar **strv = g_strsplit(keysym, ",", 16);
    95     gchar **s = strv;
    96     while( *s != NULL ) {
    97 	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
    98 	if( keycode == 0 )
    99 	    return FALSE; /* Invalid keysym */
   101 	struct keymap_entry *key = input_create_key( keycode );
   102 	key->callback = callback;
   103 	key->data = data;
   104 	key->value = value;
   105 	s++;
   106     }
   107     g_strfreev(strv);
   108     return TRUE;
   109 }
   111 void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
   112 			   void *data, uint32_t value )
   113 {
   114     if( display_driver == NULL || keysym == NULL )
   115 	return;
   116     uint16_t keycode = display_driver->resolve_keysym(keysym);
   117     if( keycode == 0 )
   118 	return;
   119     input_delete_key( keycode, callback, data, value );
   120 }
   123 gboolean input_is_key_valid( const gchar *keysym )
   124 {
   125     if( display_driver == NULL )
   126 	return FALSE; /* No display driver */
   127     return display_driver->resolve_keysym(keysym) != 0;
   128 }
   130 gboolean input_is_key_registered( const gchar *keysym )
   131 {
   132     if( display_driver == NULL )
   133 	return FALSE;
   134     uint16_t keycode = display_driver->resolve_keysym(keysym);
   135     if( keycode == 0 )
   136 	return FALSE;
   137     return input_get_key( keycode ) != NULL;
   138 }
   140 void input_event_keydown( uint16_t keycode )
   141 {
   142     struct keymap_entry *key = input_get_key(keycode);
   143     if( key != NULL ) {
   144 	key->callback( key->data, key->value, TRUE );
   145     }	
   146 }
   148 void input_event_keyup( uint16_t keycode )
   149 {
   150     struct keymap_entry *key = input_get_key(keycode);
   151     if( key != NULL ) {
   152 	key->callback( key->data, key->value, FALSE );
   153     }
   154 }
   156 display_driver_t get_display_driver_by_name( const char *name )
   157 {
   158     int i;
   159     if( name == NULL ) {
   160 	return display_driver_list[0];
   161     }
   162     for( i=0; display_driver_list[i] != NULL; i++ ) {
   163 	if( strcasecmp( display_driver_list[i]->name, name ) == 0 ) {
   164 	    return display_driver_list[i];
   165 	}
   166     }
   168     return NULL;
   169 }
   172 gboolean display_set_driver( display_driver_t driver )
   173 {
   174     gboolean rv = TRUE;
   175     if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
   176 	display_driver->shutdown_driver();
   178     display_driver = driver;
   179     if( driver->init_driver != NULL )
   180 	rv = driver->init_driver();
   181     if( !rv ) {
   182 	display_driver = NULL;
   183     }
   184     return rv;
   185 }
.