Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 370:3131ba1440fc
prev356:b281cad966b7
next425:17f019f4ed8d
author nkeynes
date Sat Sep 08 04:05:35 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Handle video driver init failure cleanly (fallback to headless)
Hookup shutdown for the GTK driver
view annotate diff log raw
     1 /**
     2  * $Id: display.c,v 1.8 2007-09-08 04:05:35 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"
    26 typedef struct keymap_entry {
    27     uint16_t keycode;
    28     input_key_callback_t callback;
    29     void *data;
    30     uint32_t value;
    31 } *keymap_entry_t;
    33 /**
    34  * Colour format information
    35  */
    36 struct colour_format colour_formats[] = {
    37     { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
    38     { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
    39     { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
    40     { GL_UNSIGNED_INT_8_8_8_8_REV, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
    41     { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
    42     { GL_UNSIGNED_INT_8_8_8_8_REV, GL_BGRA, GL_RGBA8, 4 },
    44 };
    46 /**
    47  * FIXME: make this more memory efficient
    48  */
    49 struct keymap_entry *keymap[65536];
    52 static struct keymap_entry *input_create_key( uint16_t keycode )
    53 {
    54     struct keymap_entry *key = keymap[ keycode ];
    55     if( key == NULL ) {
    56 	key = malloc( sizeof( struct keymap_entry ) );
    57 	assert( key != NULL );
    58 	keymap[ keycode ] = key;
    59 	key->keycode = keycode;
    60     }
    61     return key;
    62 }
    64 static void input_delete_key( uint16_t keycode )
    65 {
    66     struct keymap_entry *key = keymap[keycode];
    67     if( key != NULL ) {
    68 	free( key );
    69 	keymap[keycode] = NULL;
    70     }
    71 }
    73 static struct keymap_entry *input_get_key( uint16_t keycode )
    74 {
    75     return keymap[ keycode ];
    76 }
    78 gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
    79 			     void *data, uint32_t value )
    80 {
    81     int i;
    82     if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
    83 	return FALSE; /* No display driver */
    84     gchar **strv = g_strsplit(keysym, ",", 16);
    85     gchar **s = strv;
    86     while( *s != NULL ) {
    87 	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
    88 	if( keycode == 0 )
    89 	    return FALSE; /* Invalid keysym */
    91 	struct keymap_entry *key = input_create_key( keycode );
    92 	key->callback = callback;
    93 	key->data = data;
    94 	key->value = value;
    95 	s++;
    96     }
    97     g_strfreev(strv);
    98     return TRUE;
    99 }
   101 void input_unregister_key( const gchar *keysym )
   102 {
   103     if( display_driver == NULL || keysym == NULL )
   104 	return;
   105     uint16_t keycode = display_driver->resolve_keysym(keysym);
   106     if( keycode == 0 )
   107 	return;
   108     input_delete_key( keycode );
   109 }
   112 gboolean input_is_key_valid( const gchar *keysym )
   113 {
   114     if( display_driver == NULL )
   115 	return FALSE; /* No display driver */
   116     return display_driver->resolve_keysym(keysym) != 0;
   117 }
   119 gboolean input_is_key_registered( const gchar *keysym )
   120 {
   121     if( display_driver == NULL )
   122 	return FALSE;
   123     uint16_t keycode = display_driver->resolve_keysym(keysym);
   124     if( keycode == 0 )
   125 	return FALSE;
   126     return input_get_key( keycode ) != NULL;
   127 }
   129 void input_event_keydown( uint16_t keycode )
   130 {
   131     struct keymap_entry *key = input_get_key(keycode);
   132     if( key != NULL ) {
   133 	key->callback( key->data, key->value, TRUE );
   134     }	
   135 }
   137 void input_event_keyup( uint16_t keycode )
   138 {
   139     struct keymap_entry *key = input_get_key(keycode);
   140     if( key != NULL ) {
   141 	key->callback( key->data, key->value, FALSE );
   142     }
   143 }
   147 gboolean display_set_driver( display_driver_t driver )
   148 {
   149     gboolean rv = TRUE;
   150     if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
   151 	display_driver->shutdown_driver();
   153     display_driver = driver;
   154     if( driver->init_driver != NULL )
   155 	rv = driver->init_driver();
   156     if( rv ) {
   157 	texcache_gl_init();
   158     }
   159     return rv;
   160 }
.