Search
lxdream.org :: lxdream/src/display.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/display.c
changeset 1019:87f191f92f8f
prev1010:a506a2f66180
next1072:d82e04e6d497
author nkeynes
date Wed Jun 03 10:29:16 2009 +0000 (11 years ago)
permissions -rw-r--r--
last change Allow multiple input hooks to be registered for the same key - useful to allow
eg binding the same hotkey for run/stop so that it works as a toggle.
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@608
    59
    struct keymap_entry *next; // allow chaining
nkeynes@144
    60
} *keymap_entry_t;
nkeynes@144
    61
nkeynes@608
    62
typedef struct mouse_entry {
nkeynes@608
    63
    gboolean relative;
nkeynes@608
    64
    input_mouse_callback_t callback;
nkeynes@608
    65
    void *data;
nkeynes@608
    66
    struct mouse_entry *next;
nkeynes@608
    67
} *mouse_entry_t;
nkeynes@608
    68
nkeynes@614
    69
typedef struct input_driver_entry {
nkeynes@614
    70
    input_driver_t driver;
nkeynes@614
    71
    uint16_t entry_count;
nkeynes@614
    72
    struct keymap_entry *keymap[0];
nkeynes@614
    73
} *input_driver_entry_t;
nkeynes@614
    74
nkeynes@144
    75
/**
nkeynes@327
    76
 * Colour format information
nkeynes@327
    77
 */
nkeynes@327
    78
struct colour_format colour_formats[] = {
nkeynes@736
    79
        { GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA, GL_RGB5_A1, 2 },
nkeynes@736
    80
        { GL_UNSIGNED_SHORT_5_6_5, GL_RGB, GL_RGB5, 2 },
nkeynes@736
    81
        { GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA, GL_RGBA4, 2 },
nkeynes@736
    82
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* YUV decoded to ARGB8888 */
nkeynes@736
    83
        { GL_UNSIGNED_BYTE, GL_BGR, GL_RGB, 3 },
nkeynes@736
    84
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
nkeynes@736
    85
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index4 decoded */
nkeynes@736
    86
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 }, /* Index8 decoded */
nkeynes@736
    87
        { GL_UNSIGNED_BYTE, GL_BGRA, GL_RGBA8, 4 },
nkeynes@736
    88
        { GL_UNSIGNED_BYTE, GL_RGB, GL_RGB, 3 },
nkeynes@736
    89
nkeynes@327
    90
};
nkeynes@327
    91
nkeynes@327
    92
/**
nkeynes@144
    93
 * FIXME: make this more memory efficient
nkeynes@144
    94
 */
nkeynes@614
    95
static struct keymap_entry *root_keymap[65535];
nkeynes@614
    96
static struct keymap_entry *keyhooks = NULL;
nkeynes@614
    97
static gboolean display_focused = TRUE;
nkeynes@614
    98
static GList *input_drivers= NULL;
nkeynes@614
    99
static display_keysym_callback_t display_keysym_hook = NULL;
nkeynes@614
   100
void *display_keysym_hook_data;
nkeynes@144
   101
nkeynes@614
   102
gboolean input_register_device( input_driver_t driver, uint16_t max_keycode )
nkeynes@144
   103
{
nkeynes@614
   104
    GList *ptr;
nkeynes@614
   105
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   106
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   107
        if( strcasecmp( entry->driver->id, driver->id ) == 0 ) {
nkeynes@736
   108
            return FALSE;
nkeynes@736
   109
        }
nkeynes@144
   110
    }
nkeynes@614
   111
nkeynes@614
   112
    input_driver_entry_t entry = g_malloc0( sizeof(struct input_driver_entry) + (sizeof(keymap_entry_t) * max_keycode) );
nkeynes@614
   113
    entry->driver = driver;
nkeynes@614
   114
    entry->entry_count = max_keycode;
nkeynes@614
   115
    input_drivers = g_list_append( input_drivers, entry );
nkeynes@614
   116
    return TRUE;
nkeynes@144
   117
}
nkeynes@144
   118
nkeynes@615
   119
gboolean input_has_device( const gchar *id )
nkeynes@615
   120
{
nkeynes@615
   121
    GList *ptr;
nkeynes@615
   122
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   123
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   124
        if( strcasecmp(entry->driver->id, id) == 0 ) {
nkeynes@736
   125
            return TRUE;
nkeynes@736
   126
        }
nkeynes@615
   127
    }
nkeynes@615
   128
    return FALSE;
nkeynes@615
   129
}
nkeynes@615
   130
nkeynes@615
   131
nkeynes@614
   132
void input_unregister_device( input_driver_t driver )
nkeynes@144
   133
{
nkeynes@614
   134
    GList *ptr;
nkeynes@614
   135
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   136
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   137
        if( entry->driver == driver ) {
nkeynes@736
   138
            if( driver->destroy != NULL ) {
nkeynes@736
   139
                driver->destroy(driver);
nkeynes@736
   140
            }
nkeynes@736
   141
            input_drivers = g_list_remove(input_drivers, (gpointer)entry);
nkeynes@736
   142
            g_free( entry );
nkeynes@736
   143
            return;
nkeynes@736
   144
        }
nkeynes@144
   145
    }
nkeynes@144
   146
}
nkeynes@144
   147
nkeynes@614
   148
/**
nkeynes@614
   149
 * Resolve the keysym and return a pointer to the keymap entry pointer
nkeynes@614
   150
 * @return keymap pointer or NULL if the key was unresolved
nkeynes@614
   151
 */
nkeynes@614
   152
static struct keymap_entry **input_entry_from_keysym( const gchar *keysym )
nkeynes@144
   153
{
nkeynes@614
   154
    if( keysym == NULL || keysym[0] == 0 ) {
nkeynes@736
   155
        return NULL;
nkeynes@614
   156
    }
nkeynes@614
   157
    char **strv = g_strsplit(keysym,":",2);
nkeynes@614
   158
    if( strv[1] == NULL ) {
nkeynes@736
   159
        /* root device */
nkeynes@736
   160
        if( display_driver == NULL || display_driver->resolve_keysym == NULL) {
nkeynes@736
   161
            // Root device has no input handling
nkeynes@736
   162
            g_strfreev(strv);
nkeynes@736
   163
            return NULL;
nkeynes@736
   164
        }
nkeynes@736
   165
        uint16_t keycode = display_driver->resolve_keysym(g_strstrip(strv[0]));
nkeynes@736
   166
        g_strfreev(strv);
nkeynes@736
   167
        if( keycode == 0 ) {
nkeynes@736
   168
            return NULL;
nkeynes@736
   169
        }
nkeynes@736
   170
        return &root_keymap[keycode-1];
nkeynes@849
   171
nkeynes@614
   172
    } else {
nkeynes@736
   173
        char *id = g_strstrip(strv[0]);
nkeynes@736
   174
        GList *ptr;
nkeynes@736
   175
        for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   176
            input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   177
            if( strcasecmp( entry->driver->id, id ) == 0 ) {
nkeynes@736
   178
                /* we have ze device */
nkeynes@736
   179
                if( entry->driver->resolve_keysym == NULL ) {
nkeynes@736
   180
                    g_strfreev(strv);
nkeynes@736
   181
                    return NULL;
nkeynes@736
   182
                } 
nkeynes@736
   183
                uint16_t keycode = entry->driver->resolve_keysym(entry->driver, g_strstrip(strv[1]));
nkeynes@736
   184
                g_strfreev(strv);
nkeynes@736
   185
                if( keycode == 0 || keycode > entry->entry_count ) {
nkeynes@736
   186
                    return NULL;
nkeynes@736
   187
                }
nkeynes@736
   188
                return &entry->keymap[keycode-1];
nkeynes@736
   189
            }
nkeynes@736
   190
        }
nkeynes@736
   191
        g_strfreev(strv);
nkeynes@736
   192
        return NULL; // device not found
nkeynes@614
   193
    }
nkeynes@144
   194
}
nkeynes@144
   195
nkeynes@614
   196
static struct keymap_entry **input_entry_from_keycode( input_driver_t driver, uint16_t keycode )
nkeynes@614
   197
{
nkeynes@614
   198
    GList *ptr;
nkeynes@614
   199
nkeynes@614
   200
    if( keycode == 0 ) {
nkeynes@736
   201
        return NULL;
nkeynes@614
   202
    }
nkeynes@614
   203
nkeynes@614
   204
    if( driver == NULL ) {
nkeynes@736
   205
        return &root_keymap[keycode-1];
nkeynes@614
   206
    }
nkeynes@614
   207
nkeynes@614
   208
    for( ptr = input_drivers; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@736
   209
        input_driver_entry_t entry = (input_driver_entry_t)ptr->data;
nkeynes@736
   210
        if( entry->driver == driver ) {
nkeynes@736
   211
            if( keycode > entry->entry_count ) {
nkeynes@736
   212
                return NULL;
nkeynes@736
   213
            } else {
nkeynes@736
   214
                return &entry->keymap[keycode-1];
nkeynes@736
   215
            }
nkeynes@736
   216
        }
nkeynes@614
   217
    }
nkeynes@614
   218
    return NULL;
nkeynes@614
   219
}
nkeynes@614
   220
nkeynes@770
   221
gchar *input_keycode_to_keysym( input_driver_t driver, uint16_t keycode )
nkeynes@614
   222
{
nkeynes@614
   223
    if( keycode == 0 ) {
nkeynes@736
   224
        return NULL;
nkeynes@614
   225
    }
nkeynes@614
   226
    if( driver == NULL ) {
nkeynes@736
   227
        if( display_driver != NULL && display_driver->get_keysym_for_keycode != NULL ) {
nkeynes@736
   228
            return display_driver->get_keysym_for_keycode(keycode);
nkeynes@736
   229
        }
nkeynes@614
   230
    } else if( driver->get_keysym_for_keycode ) {
nkeynes@736
   231
        gchar *sym = driver->get_keysym_for_keycode(driver,keycode);
nkeynes@736
   232
        if( sym != NULL ) {
nkeynes@736
   233
            gchar *result = g_strdup_printf( "%s: %s", driver->id, sym );
nkeynes@736
   234
            g_free(sym);
nkeynes@736
   235
            return result;
nkeynes@736
   236
        }
nkeynes@614
   237
    }
nkeynes@614
   238
    return NULL;
nkeynes@614
   239
}
nkeynes@614
   240
nkeynes@614
   241
nkeynes@144
   242
gboolean input_register_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@736
   243
                             void *data, uint32_t value )
nkeynes@144
   244
{
nkeynes@614
   245
    if( keysym == NULL ) {
nkeynes@736
   246
        return FALSE;
nkeynes@614
   247
    }
nkeynes@614
   248
    int keys = 0;
nkeynes@271
   249
    gchar **strv = g_strsplit(keysym, ",", 16);
nkeynes@271
   250
    gchar **s = strv;
nkeynes@271
   251
    while( *s != NULL ) {
nkeynes@736
   252
        keymap_entry_t *entryp = input_entry_from_keysym(*s);
nkeynes@736
   253
        if( entryp != NULL ) {
nkeynes@1019
   254
            keymap_entry_t newentry = g_malloc0(sizeof(struct keymap_entry));
nkeynes@1019
   255
            newentry->next = *entryp;
nkeynes@1019
   256
            newentry->callback = callback;
nkeynes@1019
   257
            newentry->data = data;
nkeynes@1019
   258
            newentry->value = value;
nkeynes@1019
   259
            *entryp = newentry;
nkeynes@736
   260
            keys++;
nkeynes@736
   261
        }
nkeynes@736
   262
        s++;
nkeynes@271
   263
    }
nkeynes@271
   264
    g_strfreev(strv);
nkeynes@614
   265
    return keys != 0;
nkeynes@144
   266
}
nkeynes@144
   267
nkeynes@451
   268
void input_unregister_key( const gchar *keysym, input_key_callback_t callback,
nkeynes@736
   269
                           void *data, uint32_t value )
nkeynes@144
   270
{
nkeynes@614
   271
    if( keysym == NULL ) {
nkeynes@736
   272
        return;
nkeynes@614
   273
    }
nkeynes@614
   274
nkeynes@614
   275
    gchar **strv = g_strsplit(keysym, ",", 16);
nkeynes@614
   276
    gchar **s = strv;
nkeynes@614
   277
    while( *s != NULL ) {
nkeynes@736
   278
        keymap_entry_t *entryp = input_entry_from_keysym(*s);
nkeynes@1019
   279
        if( entryp != NULL ) {
nkeynes@1019
   280
            while( *entryp != NULL ) {
nkeynes@1019
   281
                if( (*entryp)->callback == callback &&
nkeynes@1019
   282
                    (*entryp)->data == data && (*entryp)->value == value ) {
nkeynes@1019
   283
                    keymap_entry_t next = (*entryp)->next;
nkeynes@1019
   284
                    g_free( *entryp );
nkeynes@1019
   285
                    *entryp = next;
nkeynes@1019
   286
                    break;
nkeynes@1019
   287
                }
nkeynes@1019
   288
                entryp = &(*entryp)->next; // Yes, really
nkeynes@1019
   289
            }
nkeynes@736
   290
        }
nkeynes@736
   291
        s++;
nkeynes@614
   292
    }
nkeynes@614
   293
    g_strfreev(strv);
nkeynes@144
   294
}
nkeynes@608
   295
nkeynes@849
   296
gboolean input_register_keyboard_hook( input_key_callback_t callback,
nkeynes@736
   297
                              void *data )
nkeynes@608
   298
{
nkeynes@608
   299
    keymap_entry_t key = malloc( sizeof( struct keymap_entry ) );
nkeynes@608
   300
    assert( key != NULL );
nkeynes@608
   301
    key->callback = callback;
nkeynes@608
   302
    key->data = data;
nkeynes@608
   303
    key->next = keyhooks;
nkeynes@608
   304
    keyhooks = key;
nkeynes@608
   305
    return TRUE;
nkeynes@608
   306
}
nkeynes@608
   307
nkeynes@849
   308
void input_unregister_keyboard_hook( input_key_callback_t callback,
nkeynes@736
   309
                            void *data )
nkeynes@608
   310
{
nkeynes@608
   311
    keymap_entry_t key = keyhooks;
nkeynes@608
   312
    if( key != NULL ) {
nkeynes@736
   313
        if( key->callback == callback && key->data == data ) {
nkeynes@736
   314
            keyhooks = keyhooks->next;
nkeynes@736
   315
            free(key);
nkeynes@736
   316
            return;
nkeynes@736
   317
        }
nkeynes@736
   318
        while( key->next != NULL ) {
nkeynes@736
   319
            if( key->next->callback == callback && key->next->data == data ) {
nkeynes@627
   320
                keymap_entry_t next = key->next;
nkeynes@736
   321
                key->next = next->next;
nkeynes@736
   322
                free(next);
nkeynes@736
   323
                return;
nkeynes@736
   324
            }
nkeynes@736
   325
            key = key->next;
nkeynes@736
   326
        }
nkeynes@608
   327
    }
nkeynes@608
   328
}
nkeynes@608
   329
nkeynes@849
   330
gboolean input_is_key_valid( const gchar *keysym )
nkeynes@849
   331
{
nkeynes@849
   332
    keymap_entry_t *ptr = input_entry_from_keysym(keysym);
nkeynes@849
   333
    return ptr != NULL;
nkeynes@849
   334
}
nkeynes@849
   335
nkeynes@849
   336
gboolean input_is_key_registered( const gchar *keysym )
nkeynes@849
   337
{
nkeynes@849
   338
    keymap_entry_t *ptr = input_entry_from_keysym(keysym);
nkeynes@849
   339
    return ptr != NULL && *ptr != NULL;
nkeynes@849
   340
}
nkeynes@849
   341
nkeynes@849
   342
void input_event_keydown( input_driver_t driver, uint16_t keycode, uint32_t pressure )
nkeynes@849
   343
{
nkeynes@849
   344
    if( display_focused ) {
nkeynes@849
   345
        keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
nkeynes@1019
   346
        if( entryp != NULL ) {
nkeynes@1019
   347
            keymap_entry_t key = *entryp;
nkeynes@1019
   348
            while( key != NULL ) {
nkeynes@1019
   349
                key->callback( key->data, key->value, pressure, TRUE );
nkeynes@1019
   350
                key = key->next;
nkeynes@1019
   351
            }
nkeynes@849
   352
        }
nkeynes@849
   353
        if( driver == NULL ) {
nkeynes@849
   354
            keymap_entry_t key = keyhooks;
nkeynes@849
   355
            while( key != NULL ) {
nkeynes@849
   356
                key->callback( key->data, keycode, pressure, TRUE );
nkeynes@849
   357
                key = key->next;
nkeynes@849
   358
            }
nkeynes@849
   359
        }
nkeynes@849
   360
    }
nkeynes@849
   361
    if( display_keysym_hook != NULL ) {
nkeynes@849
   362
        gchar *sym = input_keycode_to_keysym( driver, keycode );
nkeynes@849
   363
        if( sym != NULL ) {
nkeynes@849
   364
            display_keysym_hook(display_keysym_hook_data, sym);
nkeynes@849
   365
            g_free(sym);
nkeynes@849
   366
        }
nkeynes@849
   367
    }
nkeynes@849
   368
}
nkeynes@849
   369
nkeynes@1010
   370
void input_event_keyup( input_driver_t driver, uint16_t keycode )
nkeynes@849
   371
{
nkeynes@849
   372
    if( display_focused ) {
nkeynes@849
   373
        keymap_entry_t *entryp = input_entry_from_keycode(driver,keycode);
nkeynes@1019
   374
        if( entryp != NULL ) {
nkeynes@1019
   375
            keymap_entry_t key = *entryp;
nkeynes@1019
   376
            while( key != NULL ) {
nkeynes@1019
   377
                key->callback( key->data, key->value, 0, FALSE );
nkeynes@1019
   378
                key = key->next;
nkeynes@1019
   379
            }
nkeynes@849
   380
        }
nkeynes@849
   381
nkeynes@849
   382
        if( driver == NULL ) {
nkeynes@849
   383
            keymap_entry_t key = keyhooks;
nkeynes@849
   384
            while( key != NULL ) {
nkeynes@1010
   385
                key->callback( key->data, keycode, 0, FALSE );
nkeynes@849
   386
                key = key->next;
nkeynes@849
   387
            }
nkeynes@849
   388
        }
nkeynes@849
   389
    }
nkeynes@849
   390
}
nkeynes@849
   391
nkeynes@849
   392
uint16_t input_keycode_to_dckeysym( uint16_t keycode )
nkeynes@849
   393
{
nkeynes@849
   394
    return display_driver->convert_to_dckeysym(keycode);
nkeynes@849
   395
}
nkeynes@849
   396
nkeynes@849
   397
void input_set_keysym_hook( display_keysym_callback_t hook, void *data )
nkeynes@849
   398
{
nkeynes@849
   399
    display_keysym_hook = hook;
nkeynes@849
   400
    display_keysym_hook_data = data;
nkeynes@849
   401
}
nkeynes@849
   402
nkeynes@849
   403
/***************** System mouse driver ****************/
nkeynes@849
   404
nkeynes@849
   405
static struct keymap_entry *mouse_keymap[MAX_MOUSE_BUTTONS];
nkeynes@849
   406
static struct mouse_entry *mousehooks = NULL;
nkeynes@849
   407
static uint32_t mouse_x = -1, mouse_y = -1, mouse_buttons = 0;
nkeynes@849
   408
nkeynes@849
   409
uint16_t mouse_resolve_keysym( struct input_driver *driver, const gchar *keysym )
nkeynes@849
   410
{
nkeynes@849
   411
    if( strncasecmp( keysym, "Button", 6 ) == 0 ){
nkeynes@849
   412
        unsigned long button = strtoul( keysym+6, NULL, 10 );
nkeynes@849
   413
        if( button > MAX_MOUSE_BUTTONS ) {
nkeynes@849
   414
            return 0;
nkeynes@849
   415
        }
nkeynes@849
   416
        return (uint16_t)button;
nkeynes@849
   417
    }
nkeynes@849
   418
    return 0;
nkeynes@849
   419
}
nkeynes@849
   420
nkeynes@849
   421
gchar *mouse_get_keysym( struct input_driver *driver, uint16_t keycode )
nkeynes@849
   422
{
nkeynes@849
   423
    return g_strdup_printf( "Button%d", (keycode) );
nkeynes@849
   424
}
nkeynes@849
   425
nkeynes@849
   426
struct input_driver system_mouse_driver = { "Mouse", mouse_resolve_keysym, NULL, mouse_get_keysym, NULL };
nkeynes@849
   427
nkeynes@849
   428
nkeynes@608
   429
gboolean input_register_mouse_hook( gboolean relative, input_mouse_callback_t callback,
nkeynes@736
   430
                                    void *data )
nkeynes@608
   431
{
nkeynes@608
   432
    mouse_entry_t ent = malloc( sizeof( struct mouse_entry ) );
nkeynes@608
   433
    assert( ent != NULL );
nkeynes@608
   434
    ent->callback = callback;
nkeynes@608
   435
    ent->data = data;
nkeynes@608
   436
    ent->next = mousehooks;
nkeynes@608
   437
    mousehooks = ent;
nkeynes@608
   438
    return TRUE;
nkeynes@608
   439
}    
nkeynes@608
   440
nkeynes@608
   441
void input_unregister_mouse_hook( input_mouse_callback_t callback, void *data )
nkeynes@608
   442
{
nkeynes@608
   443
    mouse_entry_t ent = mousehooks;
nkeynes@608
   444
    if( ent != NULL ) {
nkeynes@736
   445
        if( ent->callback == callback && ent->data == data ) {
nkeynes@736
   446
            mousehooks = mousehooks->next;
nkeynes@736
   447
            free(ent);
nkeynes@736
   448
            return;
nkeynes@736
   449
        }
nkeynes@736
   450
        while( ent->next != NULL ) {
nkeynes@736
   451
            if( ent->next->callback == callback && ent->next->data == data ) {
nkeynes@627
   452
                mouse_entry_t next = ent->next;
nkeynes@736
   453
                ent->next = next->next;
nkeynes@736
   454
                free(next);
nkeynes@736
   455
                return;
nkeynes@736
   456
            }
nkeynes@736
   457
            ent = ent->next;
nkeynes@736
   458
        }
nkeynes@608
   459
    }
nkeynes@608
   460
}
nkeynes@608
   461
nkeynes@849
   462
void input_event_run_mouse_hooks( uint32_t buttons, int32_t x, int32_t y, gboolean absolute )
nkeynes@608
   463
{
nkeynes@608
   464
    mouse_entry_t ent = mousehooks;
nkeynes@608
   465
    while( ent != NULL ) {
nkeynes@839
   466
        ent->callback(ent->data, buttons, x, y, absolute);
nkeynes@736
   467
        ent = ent->next;
nkeynes@608
   468
    }
nkeynes@608
   469
}
nkeynes@144
   470
nkeynes@849
   471
void input_event_mousedown( uint16_t button, int32_t x, int32_t y, gboolean absolute )
nkeynes@144
   472
{
nkeynes@849
   473
    if( absolute ) {
nkeynes@849
   474
        mouse_x = x;
nkeynes@849
   475
        mouse_y = y;
nkeynes@849
   476
    }
nkeynes@849
   477
    mouse_buttons |= (1<<button);
nkeynes@1010
   478
    input_event_keydown( &system_mouse_driver, button+1, MAX_PRESSURE );
nkeynes@849
   479
    input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
nkeynes@849
   480
}    
nkeynes@849
   481
nkeynes@849
   482
void input_event_mouseup( uint16_t button, int32_t x, int32_t y, gboolean absolute )
nkeynes@849
   483
{
nkeynes@849
   484
    if( absolute ) {
nkeynes@849
   485
        mouse_x = x;
nkeynes@849
   486
        mouse_y = y;
nkeynes@849
   487
    }
nkeynes@849
   488
    mouse_buttons &= ~(1<<button);
nkeynes@1010
   489
    input_event_keyup( &system_mouse_driver, button+1 );
nkeynes@849
   490
    input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
nkeynes@144
   491
}
nkeynes@144
   492
nkeynes@849
   493
void input_event_mousemove( int32_t x, int32_t y, gboolean absolute )
nkeynes@144
   494
{
nkeynes@849
   495
    if( absolute ) {
nkeynes@849
   496
        mouse_x = x;
nkeynes@849
   497
        mouse_y = y;
nkeynes@849
   498
    }
nkeynes@849
   499
    input_event_run_mouse_hooks( mouse_buttons, x, y, absolute );
nkeynes@144
   500
}
nkeynes@144
   501
nkeynes@849
   502
/************************ Main display driver *************************/
nkeynes@144
   503
nkeynes@700
   504
void print_display_drivers( FILE *out )
nkeynes@700
   505
{
nkeynes@700
   506
    int i;
nkeynes@700
   507
    fprintf( out, "Available video drivers:\n" );
nkeynes@700
   508
    for( i=0; display_driver_list[i] != NULL; i++ ) {
nkeynes@700
   509
        fprintf( out, "  %-8s %s\n", display_driver_list[i]->name,
nkeynes@736
   510
                gettext(display_driver_list[i]->description) );
nkeynes@700
   511
    }
nkeynes@700
   512
}
nkeynes@700
   513
nkeynes@531
   514
display_driver_t get_display_driver_by_name( const char *name )
nkeynes@531
   515
{
nkeynes@531
   516
    int i;
nkeynes@531
   517
    if( name == NULL ) {
nkeynes@736
   518
        return display_driver_list[0];
nkeynes@531
   519
    }
nkeynes@531
   520
    for( i=0; display_driver_list[i] != NULL; i++ ) {
nkeynes@736
   521
        if( strcasecmp( display_driver_list[i]->name, name ) == 0 ) {
nkeynes@736
   522
            return display_driver_list[i];
nkeynes@736
   523
        }
nkeynes@531
   524
    }
nkeynes@531
   525
nkeynes@531
   526
    return NULL;
nkeynes@531
   527
}
nkeynes@144
   528
nkeynes@144
   529
nkeynes@370
   530
gboolean display_set_driver( display_driver_t driver )
nkeynes@144
   531
{
nkeynes@370
   532
    gboolean rv = TRUE;
nkeynes@370
   533
    if( display_driver != NULL && display_driver->shutdown_driver != NULL ) 
nkeynes@736
   534
        display_driver->shutdown_driver();
nkeynes@144
   535
nkeynes@144
   536
    display_driver = driver;
nkeynes@144
   537
    if( driver->init_driver != NULL )
nkeynes@736
   538
        rv = driver->init_driver();
nkeynes@849
   539
    if( rv ) {
nkeynes@849
   540
        input_register_device(&system_mouse_driver, MAX_MOUSE_BUTTONS);
nkeynes@849
   541
    } else {
nkeynes@736
   542
        display_driver = NULL;
nkeynes@370
   543
    }
nkeynes@370
   544
    return rv;
nkeynes@144
   545
}
nkeynes@614
   546
nkeynes@614
   547
void display_set_focused( gboolean has_focus )
nkeynes@614
   548
{
nkeynes@614
   549
    display_focused = has_focus;
nkeynes@614
   550
}
.