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 (13 years ago)
permissions -rw-r--r--
last change Handle video driver init failure cleanly (fallback to headless)
Hookup shutdown for the GTK driver
file annotate diff log raw
nkeynes@144
     1
/**
nkeynes@370
     2
 * $Id: display.c,v 1.8 2007-09-08 04:05:35 nkeynes Exp $
nkeynes@144
     3
 *
nkeynes@144
     4
 * Generic support for keyboard and other input sources. The active display
nkeynes@144
     5
 * driver is expected to deliver events here, where they're translated and
nkeynes@144
     6
 * passed to the appropriate dreamcast controllers (if any).
nkeynes@144
     7
 *
nkeynes@144
     8
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@144
     9
 *
nkeynes@144
    10
 * This program is free software; you can redistribute it and/or modify
nkeynes@144
    11
 * it under the terms of the GNU General Public License as published by
nkeynes@144
    12
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@144
    13
 * (at your option) any later version.
nkeynes@144
    14
 *
nkeynes@144
    15
 * This program is distributed in the hope that it will be useful,
nkeynes@144
    16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@144
    17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@144
    18
 * GNU General Public License for more details.
nkeynes@144
    19
 */
nkeynes@144
    20
nkeynes@144
    21
#include <stdint.h>
nkeynes@144
    22
#include <assert.h>
nkeynes@144
    23
#include "dream.h"
nkeynes@144
    24
#include "display.h"
nkeynes@144
    25
nkeynes@144
    26
typedef struct keymap_entry {
nkeynes@144
    27
    uint16_t keycode;
nkeynes@144
    28
    input_key_callback_t callback;
nkeynes@144
    29
    void *data;
nkeynes@144
    30
    uint32_t value;
nkeynes@144
    31
} *keymap_entry_t;
nkeynes@144
    32
nkeynes@144
    33
/**
nkeynes@327
    34
 * Colour format information
nkeynes@327
    35
 */
nkeynes@327
    36
struct colour_format colour_formats[] = {
nkeynes@329
    37
    { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
nkeynes@327
    38
    { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
nkeynes@327
    39
    { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
nkeynes@327
    40
    { GL_UNSIGNED_INT_8_8_8_8_REV, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
nkeynes@327
    41
    { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
nkeynes@352
    42
    { GL_UNSIGNED_INT_8_8_8_8_REV, GL_BGRA, GL_RGBA8, 4 },
nkeynes@352
    43
    
nkeynes@327
    44
};
nkeynes@327
    45
nkeynes@327
    46
/**
nkeynes@144
    47
 * FIXME: make this more memory efficient
nkeynes@144
    48
 */
nkeynes@144
    49
struct keymap_entry *keymap[65536];
nkeynes@144
    50
nkeynes@144
    51
nkeynes@144
    52
static struct keymap_entry *input_create_key( uint16_t keycode )
nkeynes@144
    53
{
nkeynes@144
    54
    struct keymap_entry *key = keymap[ keycode ];
nkeynes@144
    55
    if( key == NULL ) {
nkeynes@144
    56
	key = malloc( sizeof( struct keymap_entry ) );
nkeynes@144
    57
	assert( key != NULL );
nkeynes@144
    58
	keymap[ keycode ] = key;
nkeynes@144
    59
	key->keycode = keycode;
nkeynes@144
    60
    }
nkeynes@144
    61
    return key;
nkeynes@144
    62
}
nkeynes@144
    63
nkeynes@144
    64
static void input_delete_key( uint16_t keycode )
nkeynes@144
    65
{
nkeynes@144
    66
    struct keymap_entry *key = keymap[keycode];
nkeynes@144
    67
    if( key != NULL ) {
nkeynes@144
    68
	free( key );
nkeynes@144
    69
	keymap[keycode] = NULL;
nkeynes@144
    70
    }
nkeynes@144
    71
}
nkeynes@144
    72
nkeynes@144
    73
static struct keymap_entry *input_get_key( uint16_t keycode )
nkeynes@144
    74
{
nkeynes@144
    75
    return keymap[ keycode ];
nkeynes@144
    76
}
nkeynes@144
    77
nkeynes@144
    78
gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@144
    79
			     void *data, uint32_t value )
nkeynes@144
    80
{
nkeynes@271
    81
    int i;
nkeynes@356
    82
    if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
nkeynes@144
    83
	return FALSE; /* No display driver */
nkeynes@271
    84
    gchar **strv = g_strsplit(keysym, ",", 16);
nkeynes@271
    85
    gchar **s = strv;
nkeynes@271
    86
    while( *s != NULL ) {
nkeynes@271
    87
	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
nkeynes@271
    88
	if( keycode == 0 )
nkeynes@271
    89
	    return FALSE; /* Invalid keysym */
nkeynes@271
    90
	
nkeynes@271
    91
	struct keymap_entry *key = input_create_key( keycode );
nkeynes@271
    92
	key->callback = callback;
nkeynes@271
    93
	key->data = data;
nkeynes@271
    94
	key->value = value;
nkeynes@271
    95
	s++;
nkeynes@271
    96
    }
nkeynes@271
    97
    g_strfreev(strv);
nkeynes@144
    98
    return TRUE;
nkeynes@144
    99
}
nkeynes@144
   100
nkeynes@144
   101
void input_unregister_key( const gchar *keysym )
nkeynes@144
   102
{
nkeynes@144
   103
    if( display_driver == NULL || keysym == NULL )
nkeynes@144
   104
	return;
nkeynes@144
   105
    uint16_t keycode = display_driver->resolve_keysym(keysym);
nkeynes@144
   106
    if( keycode == 0 )
nkeynes@144
   107
	return;
nkeynes@144
   108
    input_delete_key( keycode );
nkeynes@144
   109
}
nkeynes@144
   110
    
nkeynes@144
   111
nkeynes@144
   112
gboolean input_is_key_valid( const gchar *keysym )
nkeynes@144
   113
{
nkeynes@144
   114
    if( display_driver == NULL )
nkeynes@144
   115
	return FALSE; /* No display driver */
nkeynes@144
   116
    return display_driver->resolve_keysym(keysym) != 0;
nkeynes@144
   117
}
nkeynes@144
   118
nkeynes@144
   119
gboolean input_is_key_registered( const gchar *keysym )
nkeynes@144
   120
{
nkeynes@144
   121
    if( display_driver == NULL )
nkeynes@144
   122
	return FALSE;
nkeynes@144
   123
    uint16_t keycode = display_driver->resolve_keysym(keysym);
nkeynes@144
   124
    if( keycode == 0 )
nkeynes@144
   125
	return FALSE;
nkeynes@144
   126
    return input_get_key( keycode ) != NULL;
nkeynes@144
   127
}
nkeynes@144
   128
nkeynes@144
   129
void input_event_keydown( uint16_t keycode )
nkeynes@144
   130
{
nkeynes@144
   131
    struct keymap_entry *key = input_get_key(keycode);
nkeynes@144
   132
    if( key != NULL ) {
nkeynes@144
   133
	key->callback( key->data, key->value, TRUE );
nkeynes@144
   134
    }	
nkeynes@144
   135
}
nkeynes@144
   136
nkeynes@144
   137
void input_event_keyup( uint16_t keycode )
nkeynes@144
   138
{
nkeynes@144
   139
    struct keymap_entry *key = input_get_key(keycode);
nkeynes@144
   140
    if( key != NULL ) {
nkeynes@144
   141
	key->callback( key->data, key->value, FALSE );
nkeynes@144
   142
    }
nkeynes@144
   143
}
nkeynes@144
   144
nkeynes@144
   145
nkeynes@144
   146
nkeynes@370
   147
gboolean display_set_driver( display_driver_t driver )
nkeynes@144
   148
{
nkeynes@370
   149
    gboolean rv = TRUE;
nkeynes@370
   150
    if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
nkeynes@144
   151
	display_driver->shutdown_driver();
nkeynes@144
   152
nkeynes@144
   153
    display_driver = driver;
nkeynes@144
   154
    if( driver->init_driver != NULL )
nkeynes@370
   155
	rv = driver->init_driver();
nkeynes@370
   156
    if( rv ) {
nkeynes@370
   157
	texcache_gl_init();
nkeynes@370
   158
    }
nkeynes@370
   159
    return rv;
nkeynes@144
   160
}
.