Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 425:17f019f4ed8d
prev370:3131ba1440fc
next431:248dd77a9e44
author nkeynes
date Sun Oct 07 06:03:22 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
view annotate diff log raw
     1 /**
     2  * $Id: display.c,v 1.9 2007-10-07 06:03:22 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     if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
    82 	return FALSE; /* No display driver */
    83     gchar **strv = g_strsplit(keysym, ",", 16);
    84     gchar **s = strv;
    85     while( *s != NULL ) {
    86 	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
    87 	if( keycode == 0 )
    88 	    return FALSE; /* Invalid keysym */
    90 	struct keymap_entry *key = input_create_key( keycode );
    91 	key->callback = callback;
    92 	key->data = data;
    93 	key->value = value;
    94 	s++;
    95     }
    96     g_strfreev(strv);
    97     return TRUE;
    98 }
   100 void input_unregister_key( const gchar *keysym )
   101 {
   102     if( display_driver == NULL || keysym == NULL )
   103 	return;
   104     uint16_t keycode = display_driver->resolve_keysym(keysym);
   105     if( keycode == 0 )
   106 	return;
   107     input_delete_key( keycode );
   108 }
   111 gboolean input_is_key_valid( const gchar *keysym )
   112 {
   113     if( display_driver == NULL )
   114 	return FALSE; /* No display driver */
   115     return display_driver->resolve_keysym(keysym) != 0;
   116 }
   118 gboolean input_is_key_registered( const gchar *keysym )
   119 {
   120     if( display_driver == NULL )
   121 	return FALSE;
   122     uint16_t keycode = display_driver->resolve_keysym(keysym);
   123     if( keycode == 0 )
   124 	return FALSE;
   125     return input_get_key( keycode ) != NULL;
   126 }
   128 void input_event_keydown( uint16_t keycode )
   129 {
   130     struct keymap_entry *key = input_get_key(keycode);
   131     if( key != NULL ) {
   132 	key->callback( key->data, key->value, TRUE );
   133     }	
   134 }
   136 void input_event_keyup( uint16_t keycode )
   137 {
   138     struct keymap_entry *key = input_get_key(keycode);
   139     if( key != NULL ) {
   140 	key->callback( key->data, key->value, FALSE );
   141     }
   142 }
   146 gboolean display_set_driver( display_driver_t driver )
   147 {
   148     gboolean rv = TRUE;
   149     if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
   150 	display_driver->shutdown_driver();
   152     display_driver = driver;
   153     if( driver->init_driver != NULL )
   154 	rv = driver->init_driver();
   155     if( rv ) {
   156 	texcache_gl_init();
   157     }
   158     return rv;
   159 }
.