Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 541:6986c644d58a
prev531:f0fee3ba71d1
next561:533f6b478071
author nkeynes
date Wed Nov 21 11:45:33 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Add config guard around GTK display device
file annotate diff log raw
nkeynes@144
     1
/**
nkeynes@477
     2
 * $Id: display.c,v 1.12 2007-10-31 09:10:23 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@431
    25
#include "pvr2/pvr2.h"
nkeynes@144
    26
nkeynes@531
    27
display_driver_t display_driver_list[] = { 
nkeynes@541
    28
#ifdef HAVE_GTK
nkeynes@531
    29
					   &display_gtk_driver,
nkeynes@541
    30
#endif
nkeynes@531
    31
					   &display_null_driver,
nkeynes@531
    32
					   NULL };
nkeynes@531
    33
nkeynes@144
    34
typedef struct keymap_entry {
nkeynes@144
    35
    uint16_t keycode;
nkeynes@144
    36
    input_key_callback_t callback;
nkeynes@144
    37
    void *data;
nkeynes@144
    38
    uint32_t value;
nkeynes@144
    39
} *keymap_entry_t;
nkeynes@144
    40
nkeynes@144
    41
/**
nkeynes@327
    42
 * Colour format information
nkeynes@327
    43
 */
nkeynes@327
    44
struct colour_format colour_formats[] = {
nkeynes@329
    45
    { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
nkeynes@327
    46
    { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
nkeynes@327
    47
    { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
nkeynes@451
    48
    { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
nkeynes@327
    49
    { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
nkeynes@451
    50
    { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
nkeynes@477
    51
    { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
nkeynes@477
    52
    { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
nkeynes@477
    53
    { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
nkeynes@477
    54
    { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
nkeynes@352
    55
    
nkeynes@327
    56
};
nkeynes@327
    57
nkeynes@327
    58
/**
nkeynes@144
    59
 * FIXME: make this more memory efficient
nkeynes@144
    60
 */
nkeynes@144
    61
struct keymap_entry *keymap[65536];
nkeynes@144
    62
nkeynes@144
    63
nkeynes@144
    64
static struct keymap_entry *input_create_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
	key = malloc( sizeof( struct keymap_entry ) );
nkeynes@144
    69
	assert( key != NULL );
nkeynes@144
    70
	keymap[ keycode ] = key;
nkeynes@144
    71
	key->keycode = keycode;
nkeynes@144
    72
    }
nkeynes@144
    73
    return key;
nkeynes@144
    74
}
nkeynes@144
    75
nkeynes@451
    76
static void input_delete_key( uint16_t keycode, input_key_callback_t callback, void *data,
nkeynes@451
    77
			      uint32_t value )
nkeynes@144
    78
{
nkeynes@144
    79
    struct keymap_entry *key = keymap[keycode];
nkeynes@451
    80
    if( key != NULL && key->callback == callback && key->data == data && key->value == value ) {
nkeynes@144
    81
	free( key );
nkeynes@144
    82
	keymap[keycode] = NULL;
nkeynes@144
    83
    }
nkeynes@144
    84
}
nkeynes@144
    85
nkeynes@144
    86
static struct keymap_entry *input_get_key( uint16_t keycode )
nkeynes@144
    87
{
nkeynes@144
    88
    return keymap[ keycode ];
nkeynes@144
    89
}
nkeynes@144
    90
nkeynes@144
    91
gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@144
    92
			     void *data, uint32_t value )
nkeynes@144
    93
{
nkeynes@356
    94
    if( display_driver == NULL || keysym == NULL || display_driver->resolve_keysym == NULL )
nkeynes@144
    95
	return FALSE; /* No display driver */
nkeynes@271
    96
    gchar **strv = g_strsplit(keysym, ",", 16);
nkeynes@271
    97
    gchar **s = strv;
nkeynes@271
    98
    while( *s != NULL ) {
nkeynes@271
    99
	uint16_t keycode = display_driver->resolve_keysym(g_strstrip(*s));
nkeynes@271
   100
	if( keycode == 0 )
nkeynes@271
   101
	    return FALSE; /* Invalid keysym */
nkeynes@271
   102
	
nkeynes@271
   103
	struct keymap_entry *key = input_create_key( keycode );
nkeynes@271
   104
	key->callback = callback;
nkeynes@271
   105
	key->data = data;
nkeynes@271
   106
	key->value = value;
nkeynes@271
   107
	s++;
nkeynes@271
   108
    }
nkeynes@271
   109
    g_strfreev(strv);
nkeynes@144
   110
    return TRUE;
nkeynes@144
   111
}
nkeynes@144
   112
nkeynes@451
   113
void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@451
   114
			   void *data, uint32_t value )
nkeynes@144
   115
{
nkeynes@144
   116
    if( display_driver == NULL || keysym == NULL )
nkeynes@144
   117
	return;
nkeynes@144
   118
    uint16_t keycode = display_driver->resolve_keysym(keysym);
nkeynes@144
   119
    if( keycode == 0 )
nkeynes@144
   120
	return;
nkeynes@451
   121
    input_delete_key( keycode, callback, data, value );
nkeynes@144
   122
}
nkeynes@144
   123
    
nkeynes@144
   124
nkeynes@144
   125
gboolean input_is_key_valid( const gchar *keysym )
nkeynes@144
   126
{
nkeynes@144
   127
    if( display_driver == NULL )
nkeynes@144
   128
	return FALSE; /* No display driver */
nkeynes@144
   129
    return display_driver->resolve_keysym(keysym) != 0;
nkeynes@144
   130
}
nkeynes@144
   131
nkeynes@144
   132
gboolean input_is_key_registered( const gchar *keysym )
nkeynes@144
   133
{
nkeynes@144
   134
    if( display_driver == NULL )
nkeynes@144
   135
	return FALSE;
nkeynes@144
   136
    uint16_t keycode = display_driver->resolve_keysym(keysym);
nkeynes@144
   137
    if( keycode == 0 )
nkeynes@144
   138
	return FALSE;
nkeynes@144
   139
    return input_get_key( keycode ) != NULL;
nkeynes@144
   140
}
nkeynes@144
   141
nkeynes@144
   142
void input_event_keydown( uint16_t keycode )
nkeynes@144
   143
{
nkeynes@144
   144
    struct keymap_entry *key = input_get_key(keycode);
nkeynes@144
   145
    if( key != NULL ) {
nkeynes@144
   146
	key->callback( key->data, key->value, TRUE );
nkeynes@144
   147
    }	
nkeynes@144
   148
}
nkeynes@144
   149
nkeynes@144
   150
void input_event_keyup( uint16_t keycode )
nkeynes@144
   151
{
nkeynes@144
   152
    struct keymap_entry *key = input_get_key(keycode);
nkeynes@144
   153
    if( key != NULL ) {
nkeynes@144
   154
	key->callback( key->data, key->value, FALSE );
nkeynes@144
   155
    }
nkeynes@144
   156
}
nkeynes@144
   157
nkeynes@531
   158
display_driver_t get_display_driver_by_name( const char *name )
nkeynes@531
   159
{
nkeynes@531
   160
    int i;
nkeynes@531
   161
    if( name == NULL ) {
nkeynes@531
   162
	return display_driver_list[0];
nkeynes@531
   163
    }
nkeynes@531
   164
    for( i=0; display_driver_list[i] != NULL; i++ ) {
nkeynes@531
   165
	if( strcasecmp( display_driver_list[i]->name, name ) == 0 ) {
nkeynes@531
   166
	    return display_driver_list[i];
nkeynes@531
   167
	}
nkeynes@531
   168
    }
nkeynes@531
   169
nkeynes@531
   170
    return NULL;
nkeynes@531
   171
}
nkeynes@144
   172
nkeynes@144
   173
nkeynes@370
   174
gboolean display_set_driver( display_driver_t driver )
nkeynes@144
   175
{
nkeynes@370
   176
    gboolean rv = TRUE;
nkeynes@370
   177
    if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
nkeynes@144
   178
	display_driver->shutdown_driver();
nkeynes@144
   179
nkeynes@144
   180
    display_driver = driver;
nkeynes@144
   181
    if( driver->init_driver != NULL )
nkeynes@370
   182
	rv = driver->init_driver();
nkeynes@531
   183
    if( !rv ) {
nkeynes@531
   184
	display_driver = NULL;
nkeynes@370
   185
    }
nkeynes@370
   186
    return rv;
nkeynes@144
   187
}
.