Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 144:7f0714e89aaa
next166:8aa70cf503a2
author nkeynes
date Mon May 15 08:28:52 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Rename video_driver to display_driver
Add input source to display
Implement configuration file support
Hook controllers up to configuration
file annotate diff log raw
nkeynes@144
     1
/**
nkeynes@144
     2
 * $Id: display.c,v 1.1 2006-05-15 08:28:48 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@144
    34
 * FIXME: make this more memory efficient
nkeynes@144
    35
 */
nkeynes@144
    36
struct keymap_entry *keymap[65536];
nkeynes@144
    37
nkeynes@144
    38
nkeynes@144
    39
static struct keymap_entry *input_create_key( uint16_t keycode )
nkeynes@144
    40
{
nkeynes@144
    41
    struct keymap_entry *key = keymap[ keycode ];
nkeynes@144
    42
    if( key == NULL ) {
nkeynes@144
    43
	key = malloc( sizeof( struct keymap_entry ) );
nkeynes@144
    44
	assert( key != NULL );
nkeynes@144
    45
	keymap[ keycode ] = key;
nkeynes@144
    46
	key->keycode = keycode;
nkeynes@144
    47
    }
nkeynes@144
    48
    return key;
nkeynes@144
    49
}
nkeynes@144
    50
nkeynes@144
    51
static void input_delete_key( uint16_t keycode )
nkeynes@144
    52
{
nkeynes@144
    53
    struct keymap_entry *key = keymap[keycode];
nkeynes@144
    54
    if( key != NULL ) {
nkeynes@144
    55
	free( key );
nkeynes@144
    56
	keymap[keycode] = NULL;
nkeynes@144
    57
    }
nkeynes@144
    58
}
nkeynes@144
    59
nkeynes@144
    60
static struct keymap_entry *input_get_key( uint16_t keycode )
nkeynes@144
    61
{
nkeynes@144
    62
    return keymap[ keycode ];
nkeynes@144
    63
}
nkeynes@144
    64
nkeynes@144
    65
gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@144
    66
			     void *data, uint32_t value )
nkeynes@144
    67
{
nkeynes@144
    68
    if( display_driver == NULL || keysym == NULL )
nkeynes@144
    69
	return FALSE; /* No display driver */
nkeynes@144
    70
    uint16_t keycode = display_driver->resolve_keysym(keysym);
nkeynes@144
    71
    if( keycode == 0 )
nkeynes@144
    72
	return FALSE; /* Invalid keysym */
nkeynes@144
    73
nkeynes@144
    74
    struct keymap_entry *key = input_create_key( keycode );
nkeynes@144
    75
    key->callback = callback;
nkeynes@144
    76
    key->data = data;
nkeynes@144
    77
    key->value = value;
nkeynes@144
    78
    INFO( "Registered key '%s' (%4x)", keysym, keycode );
nkeynes@144
    79
    return TRUE;
nkeynes@144
    80
}
nkeynes@144
    81
nkeynes@144
    82
void input_unregister_key( const gchar *keysym )
nkeynes@144
    83
{
nkeynes@144
    84
    if( display_driver == NULL || keysym == NULL )
nkeynes@144
    85
	return;
nkeynes@144
    86
    uint16_t keycode = display_driver->resolve_keysym(keysym);
nkeynes@144
    87
    if( keycode == 0 )
nkeynes@144
    88
	return;
nkeynes@144
    89
    input_delete_key( keycode );
nkeynes@144
    90
}
nkeynes@144
    91
    
nkeynes@144
    92
nkeynes@144
    93
gboolean input_is_key_valid( const gchar *keysym )
nkeynes@144
    94
{
nkeynes@144
    95
    if( display_driver == NULL )
nkeynes@144
    96
	return FALSE; /* No display driver */
nkeynes@144
    97
    return display_driver->resolve_keysym(keysym) != 0;
nkeynes@144
    98
}
nkeynes@144
    99
nkeynes@144
   100
gboolean input_is_key_registered( const gchar *keysym )
nkeynes@144
   101
{
nkeynes@144
   102
    if( display_driver == NULL )
nkeynes@144
   103
	return FALSE;
nkeynes@144
   104
    uint16_t keycode = display_driver->resolve_keysym(keysym);
nkeynes@144
   105
    if( keycode == 0 )
nkeynes@144
   106
	return FALSE;
nkeynes@144
   107
    return input_get_key( keycode ) != NULL;
nkeynes@144
   108
}
nkeynes@144
   109
nkeynes@144
   110
void input_event_keydown( uint16_t keycode )
nkeynes@144
   111
{
nkeynes@144
   112
    struct keymap_entry *key = input_get_key(keycode);
nkeynes@144
   113
    if( key != NULL ) {
nkeynes@144
   114
	key->callback( key->data, key->value, TRUE );
nkeynes@144
   115
    }	
nkeynes@144
   116
}
nkeynes@144
   117
nkeynes@144
   118
void input_event_keyup( uint16_t keycode )
nkeynes@144
   119
{
nkeynes@144
   120
    struct keymap_entry *key = input_get_key(keycode);
nkeynes@144
   121
    if( key != NULL ) {
nkeynes@144
   122
	key->callback( key->data, key->value, FALSE );
nkeynes@144
   123
    }
nkeynes@144
   124
}
nkeynes@144
   125
nkeynes@144
   126
nkeynes@144
   127
nkeynes@144
   128
void display_set_driver( display_driver_t driver )
nkeynes@144
   129
{
nkeynes@144
   130
    if( display_driver != NULL && display_driver->shutdown_driver != NULL )
nkeynes@144
   131
	display_driver->shutdown_driver();
nkeynes@144
   132
nkeynes@144
   133
    display_driver = driver;
nkeynes@144
   134
    if( driver->init_driver != NULL )
nkeynes@144
   135
	driver->init_driver();
nkeynes@144
   136
    driver->set_display_format( 640, 480, COLFMT_ARGB8888 );
nkeynes@144
   137
    driver->set_render_format( 640, 480, COLFMT_ARGB8888, FALSE );
nkeynes@144
   138
    texcache_gl_init();
nkeynes@144
   139
}
.