Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 1239:be3121267597
prev1072:d82e04e6d497
next1244:6b54ef5ed413
author nkeynes
date Sat Feb 25 21:30:49 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Android support WIP
file annotate diff log raw
nkeynes@144
     1
/**
nkeynes@561
     2
 * $Id$
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@681
    27
display_driver_t display_driver_list[] = {
nkeynes@541
    28
#ifdef HAVE_GTK
nkeynes@736
    29
        &display_gtk_driver,
nkeynes@681
    30
#else
nkeynes@681
    31
#ifdef HAVE_COCOA
nkeynes@736
    32
        &display_osx_driver,
nkeynes@541
    33
#endif
nkeynes@681
    34
#endif					   
nkeynes@736
    35
        &display_null_driver,
nkeynes@736
    36
        NULL };
nkeynes@531
    37
nkeynes@614
    38
/* Some explanation:
nkeynes@614
    39
 *   The system has at least one "root" device representing the main display 
nkeynes@614
    40
 * (which may be the null display). This device is part of the display_driver
nkeynes@614
    41
 * and generates events with no input_driver. The root device has no id
nkeynes@614
    42
 * as such (for the purposes of event names)
nkeynes@614
    43
 *
nkeynes@614
    44
 *   The system may also have one or more auxilliary devices which each have
nkeynes@614
    45
 * an input_driver and an id (eg "JS0"). So the keysym "Return" is (de)coded by
nkeynes@614
    46
 * the root device, and the keysym "JS0: Button0" is (de)coded by the JS0 input
nkeynes@614
    47
 * device as "Button0".
nkeynes@614
    48
 *
nkeynes@614
    49
 *   For the moment, mice are handled specially, as they behave a little
nkeynes@614
    50
 * differently from other devices (although this will probably change in the 
nkeynes@614
    51
 * future.
nkeynes@614
    52
 */
nkeynes@614
    53
nkeynes@614
    54
nkeynes@144
    55
typedef struct keymap_entry {
nkeynes@144
    56
    input_key_callback_t callback;
nkeynes@144
    57
    void *data;
nkeynes@144
    58
    uint32_t value;
nkeynes@1072
    59
    lxdream_config_group_t group;
nkeynes@608
    60
    struct keymap_entry *next; // allow chaining
nkeynes@144
    61
} *keymap_entry_t;
nkeynes@144
    62
nkeynes@608
    63
typedef struct mouse_entry {
nkeynes@608
    64
    gboolean relative;
nkeynes@608
    65
    input_mouse_callback_t callback;
nkeynes@608
    66
    void *data;
nkeynes@1072
    67
    const lxdream_config_group_t group;
nkeynes@608
    68
    struct mouse_entry *next;
nkeynes@608
    69
} *mouse_entry_t;
nkeynes@608
    70
nkeynes@614
    71
typedef struct input_driver_entry {
nkeynes@614
    72
    input_driver_t driver;
nkeynes@614
    73
    uint16_t entry_count;
nkeynes@614
    74
    struct keymap_entry *keymap[0];
nkeynes@614
    75
} *input_driver_entry_t;
nkeynes@614
    76
nkeynes@1239
    77
nkeynes@1239
    78
#ifndef GL_RGBA8
nkeynes@1239
    79
#define GL_RGBA8 GL_RGBA8_OES
nkeynes@1239
    80
#endif
nkeynes@1239
    81
nkeynes@1239
    82
#ifndef GL_RGB5
nkeynes@1239
    83
#define GL_RGB5 GL_RGB565
nkeynes@1239
    84
#endif
nkeynes@1239
    85
nkeynes@1239
    86
#ifndef GL_BGR
nkeynes@1239
    87
#define GL_BGR GL_BGRA
nkeynes@1239
    88
#endif
nkeynes@1239
    89
nkeynes@144
    90
/**
nkeynes@327
    91
 * Colour format information
nkeynes@327
    92
 */
nkeynes@327
    93
struct colour_format colour_formats[] = {
nkeynes@736
    94
        { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
nkeynes@736
    95
        { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
nkeynes@736
    96
        { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
nkeynes@736
    97
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
nkeynes@736
    98
        { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
nkeynes@736
    99
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
nkeynes@736
   100
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
nkeynes@736
   101
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
nkeynes@736
   102
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
nkeynes@736
   103
        { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
nkeynes@736
   104
nkeynes@327
   105
};
nkeynes@327
   106
nkeynes@327
   107
/**
nkeynes@144
   108
 * FIXME: make this more memory efficient
nkeynes@144
   109
 */
nkeynes@614
   110
static struct keymap_entry *root_keymap[65535];
nkeynes@614
   111
static struct keymap_entry *keyhooks = NULL;
nkeynes@614
   112
static gboolean display_focused = TRUE;
nkeynes@614
   113
static GList *input_drivers= NULL;
nkeynes@614
   114
static display_keysym_callback_t display_keysym_hook = NULL;
nkeynes@614
   115
void *display_keysym_hook_data;
nkeynes@144
   116
nkeynes@614
   117
gboolean input_register_device( input_driver_t driver, uint16_t max_keycode )
nkeynes@144
   118
{
nkeynes@614
   119
    GList *ptr;
nkeynes@614
   120
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   121
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   122
        if( strcasecmp( entry->driver->id, driver->id ) == 0 ) {
nkeynes@736
   123
            return FALSE;
nkeynes@736
   124
        }
nkeynes@144
   125
    }
nkeynes@614
   126
nkeynes@614
   127
    input_driver_entry_t entry = g_malloc0( sizeof(struct input_driver_entry) + (sizeof(keymap_entry_t) * max_keycode) );
nkeynes@614
   128
    entry->driver = driver;
nkeynes@614
   129
    entry->entry_count = max_keycode;
nkeynes@614
   130
    input_drivers = g_list_append( input_drivers, entry );
nkeynes@614
   131
    return TRUE;
nkeynes@144
   132
}
nkeynes@144
   133
nkeynes@615
   134
gboolean input_has_device( const gchar *id )
nkeynes@615
   135
{
nkeynes@615
   136
    GList *ptr;
nkeynes@615
   137
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   138
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   139
        if( strcasecmp(entry->driver->id, id) == 0 ) {
nkeynes@736
   140
            return TRUE;
nkeynes@736
   141
        }
nkeynes@615
   142
    }
nkeynes@615
   143
    return FALSE;
nkeynes@615
   144
}
nkeynes@615
   145
nkeynes@615
   146
nkeynes@614
   147
void input_unregister_device( input_driver_t driver )
nkeynes@144
   148
{
nkeynes@614
   149
    GList *ptr;
nkeynes@614
   150
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   151
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   152
        if( entry->driver == driver ) {
nkeynes@736
   153
            if( driver->destroy != NULL ) {
nkeynes@736
   154
                driver->destroy(driver);
nkeynes@736
   155
            }
nkeynes@736
   156
            input_drivers = g_list_remove(input_drivers, (gpointer)entry);
nkeynes@736
   157
            g_free( entry );
nkeynes@736
   158
            return;
nkeynes@736
   159
        }
nkeynes@144
   160
    }
nkeynes@144
   161
}
nkeynes@144
   162
nkeynes@614
   163
/**
nkeynes@614
   164
 * Resolve the keysym and return a pointer to the keymap entry pointer
nkeynes@614
   165
 * @return keymap pointer or NULL if the key was unresolved
nkeynes@614
   166
 */
nkeynes@614
   167
static struct keymap_entry **input_entry_from_keysym( const gchar *keysym )
nkeynes@144
   168
{
nkeynes@614
   169
    if( keysym == NULL || keysym[0] == 0 ) {
nkeynes@736
   170
        return NULL;
nkeynes@614
   171
    }
nkeynes@614
   172
    char **strv = g_strsplit(keysym,":",2);
nkeynes@614
   173
    if( strv[1] == NULL ) {
nkeynes@736
   174
        /* root device */
nkeynes@736
   175
        if( display_driver == NULL || display_driver->resolve_keysym == NULL) {
nkeynes@736
   176
            // Root device has no input handling
nkeynes@736
   177
            g_strfreev(strv);
nkeynes@736
   178
            return NULL;
nkeynes@736
   179
        }
nkeynes@736
   180
        uint16_t keycode = display_driver->resolve_keysym(g_strstrip(strv[0]));
nkeynes@736
   181
        g_strfreev(strv);
nkeynes@736
   182
        if( keycode == 0 ) {
nkeynes@736
   183
            return NULL;
nkeynes@736
   184
        }
nkeynes@736
   185
        return &root_keymap[keycode-1];
nkeynes@849
   186
nkeynes@614
   187
    } else {
nkeynes@736
   188
        char *id = g_strstrip(strv[0]);
nkeynes@736
   189
        GList *ptr;
nkeynes@736
   190
        for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   191
            input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   192
            if( strcasecmp( entry->driver->id, id ) == 0 ) {
nkeynes@736
   193
                /* we have ze device */
nkeynes@736
   194
                if( entry->driver->resolve_keysym == NULL ) {
nkeynes@736
   195
                    g_strfreev(strv);
nkeynes@736
   196
                    return NULL;
nkeynes@736
   197
                } 
nkeynes@736
   198
                uint16_t keycode = entry->driver->resolve_keysym(entry->driver, g_strstrip(strv[1]));
nkeynes@736
   199
                g_strfreev(strv);
nkeynes@736
   200
                if( keycode == 0 || keycode > entry->entry_count ) {
nkeynes@736
   201
                    return NULL;
nkeynes@736
   202
                }
nkeynes@736
   203
                return &entry->keymap[keycode-1];
nkeynes@736
   204
            }
nkeynes@736
   205
        }
nkeynes@736
   206
        g_strfreev(strv);
nkeynes@736
   207
        return NULL; // device not found
nkeynes@614
   208
    }
nkeynes@144
   209
}
nkeynes@144
   210
nkeynes@614
   211
static struct keymap_entry **input_entry_from_keycode( input_driver_t driver, uint16_t keycode )
nkeynes@614
   212
{
nkeynes@614
   213
    GList *ptr;
nkeynes@614
   214
nkeynes@614
   215
    if( keycode == 0 ) {
nkeynes@736
   216
        return NULL;
nkeynes@614
   217
    }
nkeynes@614
   218
nkeynes@614
   219
    if( driver == NULL ) {
nkeynes@736
   220
        return &root_keymap[keycode-1];
nkeynes@614
   221
    }
nkeynes@614
   222
nkeynes@614
   223
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   224
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   225
        if( entry->driver == driver ) {
nkeynes@736
   226
            if( keycode > entry->entry_count ) {
nkeynes@736
   227
                return NULL;
nkeynes@736
   228
            } else {
nkeynes@736
   229
                return &entry->keymap[keycode-1];
nkeynes@736
   230
            }
nkeynes@736
   231
        }
nkeynes@614
   232
    }
nkeynes@614
   233
    return NULL;
nkeynes@614
   234
}
nkeynes@614
   235
nkeynes@770
   236
gchar *input_keycode_to_keysym( input_driver_t driver, uint16_t keycode )
nkeynes@614
   237
{
nkeynes@614
   238
    if( keycode == 0 ) {
nkeynes@736
   239
        return NULL;
nkeynes@614
   240
    }
nkeynes@614
   241
    if( driver == NULL ) {
nkeynes@736
   242
        if( display_driver != NULL && display_driver->get_keysym_for_keycode != NULL ) {
nkeynes@736
   243
            return display_driver->get_keysym_for_keycode(keycode);
nkeynes@736
   244
        }
nkeynes@614
   245
    } else if( driver->get_keysym_for_keycode ) {
nkeynes@736
   246
        gchar *sym = driver->get_keysym_for_keycode(driver,keycode);
nkeynes@736
   247
        if( sym != NULL ) {
nkeynes@736
   248
            gchar *result = g_strdup_printf( "%s: %s", driver->id, sym );
nkeynes@736
   249
            g_free(sym);
nkeynes@736
   250
            return result;
nkeynes@736
   251
        }
nkeynes@614
   252
    }
nkeynes@614
   253
    return NULL;
nkeynes@614
   254
}
nkeynes@614
   255
nkeynes@614
   256
nkeynes@144
   257
gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@736
   258
                             void *data, uint32_t value )
nkeynes@144
   259
{
nkeynes@614
   260
    if( keysym == NULL ) {
nkeynes@736
   261
        return FALSE;
nkeynes@614
   262
    }
nkeynes@614
   263
    int keys = 0;
nkeynes@271
   264
    gchar **strv = g_strsplit(keysym, ",", 16);
nkeynes@271
   265
    gchar **s = strv;
nkeynes@271
   266
    while( *s != NULL ) {
nkeynes@736
   267
        keymap_entry_t *entryp = input_entry_from_keysym(*s);
nkeynes@736
   268
        if( entryp != NULL ) {
nkeynes@1019
   269
            keymap_entry_t newentry = g_malloc0(sizeof(struct keymap_entry));
nkeynes@1019
   270
            newentry->next = *entryp;
nkeynes@1019
   271
            newentry->callback = callback;
nkeynes@1019
   272
            newentry->data = data;
nkeynes@1019
   273
            newentry->value = value;
nkeynes@1019
   274
            *entryp = newentry;
nkeynes@736
   275
            keys++;
nkeynes@736
   276
        }
nkeynes@736
   277
        s++;
nkeynes@271
   278
    }
nkeynes@271
   279
    g_strfreev(strv);
nkeynes@614
   280
    return keys != 0;
nkeynes@144
   281
}
nkeynes@144
   282
nkeynes@451
   283
void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@736
   284
                           void *data, uint32_t value )
nkeynes@144
   285
{
nkeynes@614
   286
    if( keysym == NULL ) {
nkeynes@736
   287
        return;
nkeynes@614
   288
    }
nkeynes@614
   289
nkeynes@614
   290
    gchar **strv = g_strsplit(keysym, ",", 16);
nkeynes@614
   291
    gchar **s = strv;
nkeynes@614
   292
    while( *s != NULL ) {
nkeynes@736
   293
        keymap_entry_t *entryp = input_entry_from_keysym(*s);
nkeynes@1019
   294
        if( entryp != NULL ) {
nkeynes@1019
   295
            while( *entryp != NULL ) {
nkeynes@1019
   296
                if( (*entryp)->callback == callback &&
nkeynes@1019
   297
                    (*entryp)->data == data && (*entryp)->value == value ) {
nkeynes@1019
   298
                    keymap_entry_t next = (*entryp)->next;
nkeynes@1019
   299
                    g_free( *entryp );
nkeynes@1019
   300
                    *entryp = next;
nkeynes@1019
   301
                    break;
nkeynes@1019
   302
                }
nkeynes@1019
   303
                entryp = &(*entryp)->next; // Yes, really
nkeynes@1019
   304
            }
nkeynes@736
   305
        }
nkeynes@736
   306
        s++;
nkeynes@614
   307
    }
nkeynes@614
   308
    g_strfreev(strv);
nkeynes@144
   309
}
nkeynes@608
   310
nkeynes@1072
   311
int input_register_keygroup( lxdream_config_group_t group)
nkeynes@1072
   312
{
nkeynes@1072
   313
    int i;
nkeynes@1072
   314
    int result = 0;
nkeynes@1072
   315
    for( i=0; group->params[i].key != NULL; i++ ) {
nkeynes@1072
   316
        if( group->params[i].type == CONFIG_TYPE_KEY ) {
nkeynes@1072
   317
            if( input_register_key( group->params[i].value, group->key_binding, group->data, group->params[i].tag ) ) {
nkeynes@1072
   318
                result++;
nkeynes@1072
   319
            }
nkeynes@1072
   320
        }
nkeynes@1072
   321
    }
nkeynes@1072
   322
    return result;
nkeynes@1072
   323
}
nkeynes@1072
   324
nkeynes@1072
   325
void input_unregister_keygroup( lxdream_config_group_t group )
nkeynes@1072
   326
{
nkeynes@1072
   327
    int i;
nkeynes@1072
   328
    for( i=0; group->params[i].key != NULL; i++ ) {
nkeynes@1072
   329
        if( group->params[i].type == CONFIG_TYPE_KEY ) {
nkeynes@1072
   330
            input_unregister_key( group->params[i].value, group->key_binding, group->data, group->params[i].tag );
nkeynes@1072
   331
        }
nkeynes@1072
   332
    }
nkeynes@1072
   333
}
nkeynes@1072
   334
nkeynes@1072
   335
gboolean input_keygroup_changed( void *data, lxdream_config_group_t group, unsigned key,
nkeynes@1072
   336
                                 const gchar *oldval, const gchar *newval )
nkeynes@1072
   337
{
nkeynes@1072
   338
    input_unregister_key( oldval, group->key_binding, group->data, group->params[key].tag );
nkeynes@1072
   339
    input_register_key( newval, group->key_binding, group->data, group->params[key].tag );
nkeynes@1072
   340
    return TRUE;
nkeynes@1072
   341
}
nkeynes@1072
   342
nkeynes@849
   343
gboolean input_register_keyboard_hook( input_key_callback_t callback,
nkeynes@736
   344
                              void *data )
nkeynes@608
   345
{
nkeynes@608
   346
    keymap_entry_t key = malloc( sizeof( struct keymap_entry ) );
nkeynes@608
   347
    assert( key != NULL );
nkeynes@608
   348
    key->callback = callback;
nkeynes@608
   349
    key->data = data;
nkeynes@608
   350
    key->next = keyhooks;
nkeynes@608
   351
    keyhooks = key;
nkeynes@608
   352
    return TRUE;
nkeynes@608
   353
}
nkeynes@608
   354
nkeynes@849
   355
void input_unregister_keyboard_hook( input_key_callback_t callback,
nkeynes@736
   356
                            void *data )
nkeynes@608
   357
{
nkeynes@608
   358
    keymap_entry_t key = keyhooks;
nkeynes@608
   359
    if( key != NULL ) {
nkeynes@736
   360
        if( key->callback == callback && key->data == data ) {
nkeynes@736
   361
            keyhooks = keyhooks->next;
nkeynes@736
   362
            free(key);
nkeynes@736
   363
            return;
nkeynes@736
   364
        }
nkeynes@736
   365
        while( key->next != NULL ) {
nkeynes@736
   366
            if( key->next->callback == callback && key->next->data == data ) {
nkeynes@627
   367
                keymap_entry_t next = key->next;
nkeynes@736
   368
                key->next = next->next;
nkeynes@736
   369
                free(next);
nkeynes@736
   370
                return;
nkeynes@736
   371
            }
nkeynes@736
   372
            key = key->next;
nkeynes@736
   373
        }
nkeynes@608
   374
    }
nkeynes@608
   375
}
nkeynes@608
   376
nkeynes@849
   377
gboolean input_is_key_valid( const gchar *keysym )
nkeynes@849
   378
{
nkeynes@849
   379
    keymap_entry_t *ptr = input_entry_from_keysym(keysym);
nkeynes@849
   380
    return ptr != NULL;
nkeynes@849
   381
}
nkeynes@849
   382
nkeynes@849
   383
gboolean input_is_key_registered( const gchar *keysym )
nkeynes@849
   384
{
nkeynes@849
   385
    keymap_entry_t *ptr = input_entry_from_keysym(keysym);
nkeynes@849
   386
    return ptr != NULL && *ptr != NULL;
nkeynes@849
   387
}
nkeynes@849
   388
nkeynes@849
   389
void input_event_keydown( input_driver_t driver, uint16_t keycode, uint32_t pressure )
nkeynes@849
   390
{
nkeynes@849
   391
    if( display_focused ) {
nkeynes@849
   392
        keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
nkeynes@1019
   393
        if( entryp != NULL ) {
nkeynes@1019
   394
            keymap_entry_t key = *entryp;
nkeynes@1019
   395
            while( key != NULL ) {
nkeynes@1019
   396
                key->callback( key->data, key->value, pressure, TRUE );
nkeynes@1019
   397
                key = key->next;
nkeynes@1019
   398
            }
nkeynes@849
   399
        }
nkeynes@849
   400
        if( driver == NULL ) {
nkeynes@849
   401
            keymap_entry_t key = keyhooks;
nkeynes@849
   402
            while( key != NULL ) {
nkeynes@849
   403
                key->callback( key->data, keycode, pressure, TRUE );
nkeynes@849
   404
                key = key->next;
nkeynes@849
   405
            }
nkeynes@849
   406
        }
nkeynes@849
   407
    }
nkeynes@849
   408
    if( display_keysym_hook != NULL ) {
nkeynes@849
   409
        gchar *sym = input_keycode_to_keysym( driver, keycode );
nkeynes@849
   410
        if( sym != NULL ) {
nkeynes@849
   411
            display_keysym_hook(display_keysym_hook_data, sym);
nkeynes@849
   412
            g_free(sym);
nkeynes@849
   413
        }
nkeynes@849
   414
    }
nkeynes@849
   415
}
nkeynes@849
   416
nkeynes@1010
   417
void input_event_keyup( input_driver_t driver, uint16_t keycode )
nkeynes@849
   418
{
nkeynes@849
   419
    if( display_focused ) {
nkeynes@849
   420
        keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
nkeynes@1019
   421
        if( entryp != NULL ) {
nkeynes@1019
   422
            keymap_entry_t key = *entryp;
nkeynes@1019
   423
            while( key != NULL ) {
nkeynes@1019
   424
                key->callback( key->data, key->value, 0, FALSE );
nkeynes@1019
   425
                key = key->next;
nkeynes@1019
   426
            }
nkeynes@849
   427
        }
nkeynes@849
   428
nkeynes@849
   429
        if( driver == NULL ) {
nkeynes@849
   430
            keymap_entry_t key = keyhooks;
nkeynes@849
   431
            while( key != NULL ) {
nkeynes@1010
   432
                key->callback( key->data, keycode, 0, FALSE );
nkeynes@849
   433
                key = key->next;
nkeynes@849
   434
            }
nkeynes@849
   435
        }
nkeynes@849
   436
    }
nkeynes@849
   437
}
nkeynes@849
   438
nkeynes@849
   439
uint16_t input_keycode_to_dckeysym( uint16_t keycode )
nkeynes@849
   440
{
nkeynes@849
   441
    return display_driver->convert_to_dckeysym(keycode);
nkeynes@849
   442
}
nkeynes@849
   443
nkeynes@849
   444
void input_set_keysym_hook( display_keysym_callback_t hook, void *data )
nkeynes@849
   445
{
nkeynes@849
   446
    display_keysym_hook = hook;
nkeynes@849
   447
    display_keysym_hook_data = data;
nkeynes@849
   448
}
nkeynes@849
   449
nkeynes@849
   450
/***************** System mouse driver ****************/
nkeynes@849
   451
nkeynes@849
   452
static struct keymap_entry *mouse_keymap[MAX_MOUSE_BUTTONS];
nkeynes@849
   453
static struct mouse_entry *mousehooks = NULL;
nkeynes@849
   454
static uint32_t mouse_x = -1, mouse_y = -1, mouse_buttons = 0;
nkeynes@849
   455
nkeynes@849
   456
uint16_t mouse_resolve_keysym( struct input_driver *driver, const gchar *keysym )
nkeynes@849
   457
{
nkeynes@849
   458
    if( strncasecmp( keysym, "Button", 6 ) == 0 ){
nkeynes@849
   459
        unsigned long button = strtoul( keysym+6, NULL, 10 );
nkeynes@849
   460
        if( button > MAX_MOUSE_BUTTONS ) {
nkeynes@849
   461
            return 0;
nkeynes@849
   462
        }
nkeynes@849
   463
        return (uint16_t)button;
nkeynes@849
   464
    }
nkeynes@849
   465
    return 0;
nkeynes@849
   466
}
nkeynes@849
   467
nkeynes@849
   468
gchar *mouse_get_keysym( struct input_driver *driver, uint16_t keycode )
nkeynes@849
   469
{
nkeynes@849
   470
    return g_strdup_printf( "Button%d", (keycode) );
nkeynes@849
   471
}
nkeynes@849
   472
nkeynes@849
   473
struct input_driver system_mouse_driver = { "Mouse", mouse_resolve_keysym, NULL, mouse_get_keysym, NULL };
nkeynes@849
   474
nkeynes@849
   475
nkeynes@608
   476
gboolean input_register_mouse_hook( gboolean relative, input_mouse_callback_t callback,
nkeynes@736
   477
                                    void *data )
nkeynes@608
   478
{
nkeynes@608
   479
    mouse_entry_t ent = malloc( sizeof( struct mouse_entry ) );
nkeynes@608
   480
    assert( ent != NULL );
nkeynes@608
   481
    ent->callback = callback;
nkeynes@608
   482
    ent->data = data;
nkeynes@608
   483
    ent->next = mousehooks;
nkeynes@608
   484
    mousehooks = ent;
nkeynes@608
   485
    return TRUE;
nkeynes@608
   486
}    
nkeynes@608
   487
nkeynes@608
   488
void input_unregister_mouse_hook( input_mouse_callback_t callback, void *data )
nkeynes@608
   489
{
nkeynes@608
   490
    mouse_entry_t ent = mousehooks;
nkeynes@608
   491
    if( ent != NULL ) {
nkeynes@736
   492
        if( ent->callback == callback && ent->data == data ) {
nkeynes@736
   493
            mousehooks = mousehooks->next;
nkeynes@736
   494
            free(ent);
nkeynes@736
   495
            return;
nkeynes@736
   496
        }
nkeynes@736
   497
        while( ent->next != NULL ) {
nkeynes@736
   498
            if( ent->next->callback == callback && ent->next->data == data ) {
nkeynes@627
   499
                mouse_entry_t next = ent->next;
nkeynes@736
   500
                ent->next = next->next;
nkeynes@736
   501
                free(next);
nkeynes@736
   502
                return;
nkeynes@736
   503
            }
nkeynes@736
   504
            ent = ent->next;
nkeynes@736
   505
        }
nkeynes@608
   506
    }
nkeynes@608
   507
}
nkeynes@608
   508
nkeynes@849
   509
void input_event_run_mouse_hooks( uint32_t buttons, int32_t x, int32_t y, gboolean absolute )
nkeynes@608
   510
{
nkeynes@608
   511
    mouse_entry_t ent = mousehooks;
nkeynes@608
   512
    while( ent != NULL ) {
nkeynes@839
   513
        ent->callback(ent->data, buttons, x, y, absolute);
nkeynes@736
   514
        ent = ent->next;
nkeynes@608
   515
    }
nkeynes@608
   516
}
nkeynes@144
   517
nkeynes@849
   518
void input_event_mousedown( uint16_t button, int32_t x, int32_t y, gboolean absolute )
nkeynes@144
   519
{
nkeynes@849
   520
    if( absolute ) {
nkeynes@849
   521
        mouse_x = x;
nkeynes@849
   522
        mouse_y = y;
nkeynes@849
   523
    }
nkeynes@849
   524
    mouse_buttons |= (1<<button);
nkeynes@1010
   525
    input_event_keydown( &system_mouse_driver, button+1, MAX_PRESSURE );
nkeynes@849
   526
    input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
nkeynes@849
   527
}    
nkeynes@849
   528
nkeynes@849
   529
void input_event_mouseup( uint16_t button, int32_t x, int32_t y, gboolean absolute )
nkeynes@849
   530
{
nkeynes@849
   531
    if( absolute ) {
nkeynes@849
   532
        mouse_x = x;
nkeynes@849
   533
        mouse_y = y;
nkeynes@849
   534
    }
nkeynes@849
   535
    mouse_buttons &= ~(1<<button);
nkeynes@1010
   536
    input_event_keyup( &system_mouse_driver, button+1 );
nkeynes@849
   537
    input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
nkeynes@144
   538
}
nkeynes@144
   539
nkeynes@849
   540
void input_event_mousemove( int32_t x, int32_t y, gboolean absolute )
nkeynes@144
   541
{
nkeynes@849
   542
    if( absolute ) {
nkeynes@849
   543
        mouse_x = x;
nkeynes@849
   544
        mouse_y = y;
nkeynes@849
   545
    }
nkeynes@849
   546
    input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
nkeynes@144
   547
}
nkeynes@144
   548
nkeynes@849
   549
/************************ Main display driver *************************/
nkeynes@144
   550
nkeynes@700
   551
void print_display_drivers( FILE *out )
nkeynes@700
   552
{
nkeynes@700
   553
    int i;
nkeynes@700
   554
    fprintf( out, "Available video drivers:\n" );
nkeynes@700
   555
    for( i=0; display_driver_list[i] != NULL; i++ ) {
nkeynes@700
   556
        fprintf( out, "  %-8s %s\n", display_driver_list[i]->name,
nkeynes@736
   557
                gettext(display_driver_list[i]->description) );
nkeynes@700
   558
    }
nkeynes@700
   559
}
nkeynes@700
   560
nkeynes@531
   561
display_driver_t get_display_driver_by_name( const char *name )
nkeynes@531
   562
{
nkeynes@531
   563
    int i;
nkeynes@531
   564
    if( name == NULL ) {
nkeynes@736
   565
        return display_driver_list[0];
nkeynes@531
   566
    }
nkeynes@531
   567
    for( i=0; display_driver_list[i] != NULL; i++ ) {
nkeynes@736
   568
        if( strcasecmp( display_driver_list[i]->name, name ) == 0 ) {
nkeynes@736
   569
            return display_driver_list[i];
nkeynes@736
   570
        }
nkeynes@531
   571
    }
nkeynes@531
   572
nkeynes@531
   573
    return NULL;
nkeynes@531
   574
}
nkeynes@144
   575
nkeynes@144
   576
nkeynes@370
   577
gboolean display_set_driver( display_driver_t driver )
nkeynes@144
   578
{
nkeynes@370
   579
    gboolean rv = TRUE;
nkeynes@370
   580
    if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
nkeynes@736
   581
        display_driver->shutdown_driver();
nkeynes@144
   582
nkeynes@144
   583
    display_driver = driver;
nkeynes@144
   584
    if( driver->init_driver != NULL )
nkeynes@736
   585
        rv = driver->init_driver();
nkeynes@849
   586
    if( rv ) {
nkeynes@849
   587
        input_register_device(&system_mouse_driver, MAX_MOUSE_BUTTONS);
nkeynes@849
   588
    } else {
nkeynes@736
   589
        display_driver = NULL;
nkeynes@370
   590
    }
nkeynes@370
   591
    return rv;
nkeynes@144
   592
}
nkeynes@614
   593
nkeynes@614
   594
void display_set_focused( gboolean has_focus )
nkeynes@614
   595
{
nkeynes@614
   596
    display_focused = has_focus;
nkeynes@614
   597
}
.