Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 846:32ab6b592a8a
prev838:9abb2fa58934
next848:34dc33c05106
author nkeynes
date Sat Sep 06 08:16:39 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Merge controller.h into controller.c for consistency with the other maple devices
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@31
     3
 *
nkeynes@31
     4
 * Implements the standard dreamcast controller
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@31
    17
 */
nkeynes@31
    18
nkeynes@2
    19
#include <stdlib.h>
nkeynes@770
    20
#include <assert.h>
nkeynes@2
    21
#include "dream.h"
nkeynes@144
    22
#include "dreamcast.h"
nkeynes@429
    23
#include "display.h"
nkeynes@2
    24
#include "maple.h"
nkeynes@846
    25
nkeynes@846
    26
/* First word of controller condition */
nkeynes@846
    27
#define BUTTON_C            0x00000001 /* not on standard controller */
nkeynes@846
    28
#define BUTTON_B            0x00000002
nkeynes@846
    29
#define BUTTON_A            0x00000004
nkeynes@846
    30
#define BUTTON_START        0x00000008
nkeynes@846
    31
#define BUTTON_DPAD_UP      0x00000010
nkeynes@846
    32
#define BUTTON_DPAD_DOWN    0x00000020
nkeynes@846
    33
#define BUTTON_DPAD_LEFT    0x00000040
nkeynes@846
    34
#define BUTTON_DPAD_RIGHT   0x00000080
nkeynes@846
    35
#define BUTTON_Z            0x00000100 /* not on standard controller */
nkeynes@846
    36
#define BUTTON_Y            0x00000200
nkeynes@846
    37
#define BUTTON_X            0x00000400
nkeynes@846
    38
#define BUTTON_D            0x00000800 /* not on standard controller */
nkeynes@846
    39
#define BUTTON_LEFT_TRIGGER 0xFF000000 /* Bitmask */
nkeynes@846
    40
#define BUTTON_RIGHT_TRIGGER 0x00FF0000 /* Bitmask */
nkeynes@846
    41
nkeynes@846
    42
/* Second word of controller condition (bitmasks) */
nkeynes@846
    43
#define JOY_X_AXIS          0x000000FF
nkeynes@846
    44
#define JOY_Y_AXIS          0x0000FF00
nkeynes@846
    45
#define JOY_X_AXIS_CENTER   0x00000080
nkeynes@846
    46
#define JOY_Y_AXIS_CENTER   0x00008000
nkeynes@846
    47
#define JOY2_X_AXIS         0x00FF0000 /* not on standard controller */
nkeynes@846
    48
#define JOY2_Y_AXIS         0xFF000000 /* not on standard controller */
nkeynes@846
    49
nkeynes@846
    50
/* The following bits are used by the emulator for flags but don't actually
nkeynes@846
    51
 * appear in the hardware
nkeynes@846
    52
 */
nkeynes@846
    53
#define JOY_LEFT            0x80000001
nkeynes@846
    54
#define JOY_RIGHT           0x80000002
nkeynes@846
    55
#define JOY_UP              0x80000004
nkeynes@846
    56
#define JOY_DOWN            0x80000008
nkeynes@846
    57
nkeynes@846
    58
/* Standard controller ID */
nkeynes@846
    59
#define CONTROLLER_IDENT {0x00, 0x00, 0x00, 0x01,  0x00, 0x0f, 0x06, 0xfe,  0x00, 0x00, 0x00, 0x00, \
nkeynes@846
    60
    0x00, 0x00, 0x00, 0x00,  0xff, 0x00, 0x44, 0x72,  0x65, 0x61, 0x6d, 0x63,  0x61, 0x73, 0x74, 0x20,  \
nkeynes@846
    61
    0x43, 0x6f, 0x6e, 0x74,  0x72, 0x6f, 0x6c, 0x6c,  0x65, 0x72, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20, \
nkeynes@846
    62
    0x20, 0x20, 0x20, 0x20,  0x50, 0x72, 0x6f, 0x64,  0x75, 0x63, 0x65, 0x64,  0x20, 0x42, 0x79, 0x20, \
nkeynes@846
    63
    0x6f, 0x72, 0x20, 0x55,  0x6e, 0x64, 0x65, 0x72,  0x20, 0x4c, 0x69, 0x63,  0x65, 0x6e, 0x73, 0x65, \
nkeynes@846
    64
    0x20, 0x46, 0x72, 0x6f,  0x6d, 0x20, 0x53, 0x45,  0x47, 0x41, 0x20, 0x45,  0x4e, 0x54, 0x45, 0x52, \
nkeynes@846
    65
    0x50, 0x52, 0x49, 0x53,  0x45, 0x53, 0x2c, 0x4c,  0x54, 0x44, 0x2e, 0x20,  0x20, 0x20, 0x20, 0x20, \
nkeynes@846
    66
    0xae, 0x01, 0xf4, 0x01}
nkeynes@846
    67
#define CONTROLLER_VERSION {0x56, 0x65, 0x72, 0x73,  0x69, 0x6f, 0x6e, 0x20,  0x31, 0x2e, 0x30, 0x31, \
nkeynes@846
    68
    0x30, 0x2c, 0x31, 0x39,  0x39, 0x38, 0x2f, 0x30,  0x39, 0x2f, 0x32, 0x38,  0x2c, 0x33, 0x31, 0x35, \
nkeynes@846
    69
    0x2d, 0x36, 0x32, 0x31,  0x31, 0x2d, 0x41, 0x42,  0x20, 0x20, 0x20, 0x2c,  0x41, 0x6e, 0x61, 0x6c, \
nkeynes@846
    70
    0x6f, 0x67, 0x20, 0x4d,  0x6f, 0x64, 0x75, 0x6c,  0x65, 0x20, 0x3a, 0x20,  0x54, 0x68, 0x65, 0x20, \
nkeynes@846
    71
    0x34, 0x74, 0x68, 0x20,  0x45, 0x64, 0x69, 0x74,  0x69, 0x6f, 0x6e, 0x2e,  0x35, 0x2f, 0x38, 0x20, \
nkeynes@846
    72
    0x20, 0x2b, 0x44, 0x46 }
nkeynes@2
    73
nkeynes@770
    74
#define CONTROLLER_CONFIG_ENTRIES 15
nkeynes@144
    75
nkeynes@770
    76
static void controller_attach( maple_device_t dev );
nkeynes@770
    77
static void controller_detach( maple_device_t dev );
nkeynes@770
    78
static void controller_destroy( maple_device_t dev );
nkeynes@770
    79
static maple_device_t controller_clone( maple_device_t dev );
nkeynes@770
    80
static maple_device_t controller_new();
nkeynes@770
    81
static lxdream_config_entry_t controller_get_config( maple_device_t dev );
nkeynes@770
    82
static void controller_set_config_value( maple_device_t dev, unsigned int key, const gchar *value );
nkeynes@770
    83
static int controller_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
nkeynes@502
    84
                         unsigned int *outlen );
nkeynes@2
    85
nkeynes@2
    86
typedef struct controller_device {
nkeynes@2
    87
    struct maple_device dev;
nkeynes@2
    88
    uint32_t condition[2];
nkeynes@770
    89
    struct lxdream_config_entry config[CONTROLLER_CONFIG_ENTRIES+1];
nkeynes@2
    90
} *controller_device_t;
nkeynes@2
    91
nkeynes@144
    92
struct maple_device_class controller_class = { "Sega Controller", controller_new };
nkeynes@2
    93
nkeynes@144
    94
static struct controller_device base_controller = {
nkeynes@838
    95
        { MAPLE_DEVICE_TAG, &controller_class, MAPLE_GRAB_DONTCARE,
nkeynes@838
    96
          CONTROLLER_IDENT, CONTROLLER_VERSION, 
nkeynes@770
    97
          controller_get_config, controller_set_config_value, 
nkeynes@770
    98
          controller_attach, controller_detach, controller_destroy,
nkeynes@736
    99
          controller_clone, NULL, NULL, controller_get_cond, NULL, NULL, NULL },
nkeynes@736
   100
          {0x0000FFFF, 0x80808080}, 
nkeynes@736
   101
          {{ "dpad left", N_("Dpad left"), CONFIG_TYPE_KEY },
nkeynes@736
   102
           { "dpad right", N_("Dpad right"), CONFIG_TYPE_KEY },
nkeynes@736
   103
           { "dpad up", N_("Dpad up"), CONFIG_TYPE_KEY },
nkeynes@736
   104
           { "dpad down", N_("Dpad down"), CONFIG_TYPE_KEY },
nkeynes@736
   105
           { "analog left", N_("Analog left"), CONFIG_TYPE_KEY },
nkeynes@736
   106
           { "analog right", N_("Analog right"), CONFIG_TYPE_KEY },
nkeynes@736
   107
           { "analog up", N_("Analog up"), CONFIG_TYPE_KEY },
nkeynes@736
   108
           { "analog down", N_("Analog down"), CONFIG_TYPE_KEY },
nkeynes@736
   109
           { "button X", N_("Button X"), CONFIG_TYPE_KEY },
nkeynes@736
   110
           { "button Y", N_("Button Y"), CONFIG_TYPE_KEY },
nkeynes@736
   111
           { "button A", N_("Button A"), CONFIG_TYPE_KEY },
nkeynes@736
   112
           { "button B", N_("Button B"), CONFIG_TYPE_KEY },
nkeynes@736
   113
           { "trigger left", N_("Trigger left"), CONFIG_TYPE_KEY },
nkeynes@736
   114
           { "trigger right", N_("Trigger right"), CONFIG_TYPE_KEY },
nkeynes@736
   115
           { "start", N_("Start button"), CONFIG_TYPE_KEY },
nkeynes@736
   116
           { NULL, CONFIG_TYPE_NONE }} };
nkeynes@144
   117
nkeynes@770
   118
static int config_button_map[] = { 
nkeynes@770
   119
        BUTTON_DPAD_LEFT, BUTTON_DPAD_RIGHT, BUTTON_DPAD_UP, BUTTON_DPAD_DOWN,
nkeynes@770
   120
        JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN, BUTTON_X, BUTTON_Y, BUTTON_A, 
nkeynes@770
   121
        BUTTON_B, BUTTON_LEFT_TRIGGER, BUTTON_RIGHT_TRIGGER, BUTTON_START };
nkeynes@770
   122
        
nkeynes@144
   123
#define CONTROLLER(x) ((controller_device_t)(x))
nkeynes@2
   124
nkeynes@770
   125
static maple_device_t controller_new( )
nkeynes@2
   126
{
nkeynes@2
   127
    controller_device_t dev = malloc( sizeof(struct controller_device) );
nkeynes@2
   128
    memcpy( dev, &base_controller, sizeof(base_controller) );
nkeynes@2
   129
    return MAPLE_DEVICE(dev);
nkeynes@2
   130
}
nkeynes@2
   131
nkeynes@770
   132
static maple_device_t controller_clone( maple_device_t srcdevice )
nkeynes@460
   133
{
nkeynes@460
   134
    controller_device_t src = (controller_device_t)srcdevice;
nkeynes@460
   135
    controller_device_t dev = (controller_device_t)controller_new();
nkeynes@460
   136
    lxdream_copy_config_list( dev->config, src->config );
nkeynes@460
   137
    memcpy( dev->condition, src->condition, sizeof(src->condition) );
nkeynes@460
   138
    return MAPLE_DEVICE(dev);
nkeynes@460
   139
}
nkeynes@460
   140
nkeynes@144
   141
/**
nkeynes@144
   142
 * Input callback 
nkeynes@144
   143
 */
nkeynes@770
   144
static void controller_key_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@144
   145
{
nkeynes@144
   146
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
   147
    if( isKeyDown ) {
nkeynes@736
   148
        switch( value ) {
nkeynes@736
   149
        case JOY_LEFT:
nkeynes@736
   150
            dev->condition[1] &= ~JOY_X_AXIS;
nkeynes@736
   151
            break;
nkeynes@736
   152
        case JOY_RIGHT:
nkeynes@736
   153
            dev->condition[1] |= JOY_X_AXIS;
nkeynes@736
   154
            break;
nkeynes@736
   155
        case JOY_UP:
nkeynes@736
   156
            dev->condition[1] &= ~JOY_Y_AXIS;
nkeynes@736
   157
            break;
nkeynes@736
   158
        case JOY_DOWN:
nkeynes@736
   159
            dev->condition[1] |= JOY_Y_AXIS;
nkeynes@736
   160
            break;
nkeynes@736
   161
        case BUTTON_LEFT_TRIGGER:
nkeynes@736
   162
        case BUTTON_RIGHT_TRIGGER:
nkeynes@736
   163
            dev->condition[0] |= value;
nkeynes@736
   164
            break;
nkeynes@736
   165
        default:
nkeynes@736
   166
            dev->condition[0] &= ~value;
nkeynes@736
   167
        }
nkeynes@144
   168
    } else {
nkeynes@736
   169
        switch(value ) {
nkeynes@736
   170
        case JOY_LEFT:
nkeynes@736
   171
        case JOY_RIGHT:
nkeynes@736
   172
            dev->condition[1] = (dev->condition[1] & ~JOY_X_AXIS)| JOY_X_AXIS_CENTER;
nkeynes@736
   173
            break;
nkeynes@736
   174
        case JOY_UP:
nkeynes@736
   175
        case JOY_DOWN:
nkeynes@736
   176
            dev->condition[1] = (dev->condition[1] & ~JOY_Y_AXIS)| JOY_Y_AXIS_CENTER;
nkeynes@736
   177
            break;
nkeynes@736
   178
        case BUTTON_LEFT_TRIGGER:
nkeynes@736
   179
        case BUTTON_RIGHT_TRIGGER:
nkeynes@736
   180
            dev->condition[0] &= ~value;
nkeynes@736
   181
            break;
nkeynes@736
   182
        default:
nkeynes@736
   183
            dev->condition[0] |= value;
nkeynes@736
   184
        }
nkeynes@144
   185
    }
nkeynes@144
   186
}
nkeynes@2
   187
nkeynes@770
   188
static lxdream_config_entry_t controller_get_config( maple_device_t mdev )
nkeynes@2
   189
{
nkeynes@144
   190
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
   191
    return dev->config;
nkeynes@144
   192
}
nkeynes@2
   193
nkeynes@770
   194
static void controller_set_config_value( maple_device_t mdev, unsigned int key, const gchar *value )
nkeynes@770
   195
{
nkeynes@770
   196
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@770
   197
    assert( key < CONTROLLER_CONFIG_ENTRIES );
nkeynes@770
   198
    
nkeynes@770
   199
    input_unregister_key( dev->config[key].value, controller_key_callback, dev, config_button_map[key] );
nkeynes@770
   200
    lxdream_set_config_value( &dev->config[key], value );
nkeynes@770
   201
    input_register_key( dev->config[key].value, controller_key_callback, dev, config_button_map[key] );
nkeynes@770
   202
}
nkeynes@770
   203
nkeynes@770
   204
static void controller_destroy( maple_device_t mdev )
nkeynes@144
   205
{
nkeynes@144
   206
    free( mdev );
nkeynes@144
   207
}
nkeynes@144
   208
nkeynes@144
   209
/**
nkeynes@144
   210
 * Device is being attached to the bus. Go through the config and reserve the
nkeynes@144
   211
 * keys we need.
nkeynes@144
   212
 */
nkeynes@770
   213
static void controller_attach( maple_device_t mdev )
nkeynes@144
   214
{
nkeynes@144
   215
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@770
   216
    int i;
nkeynes@770
   217
    for( i=0; i<CONTROLLER_CONFIG_ENTRIES; i++ ) {
nkeynes@770
   218
        input_register_key( dev->config[i].value, controller_key_callback, dev, config_button_map[i] );
nkeynes@770
   219
    }
nkeynes@2
   220
}
nkeynes@2
   221
nkeynes@770
   222
static void controller_detach( maple_device_t mdev )
nkeynes@2
   223
{
nkeynes@451
   224
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@770
   225
    int i;
nkeynes@770
   226
    for( i=0; i<CONTROLLER_CONFIG_ENTRIES; i++ ) {
nkeynes@770
   227
        input_unregister_key( dev->config[i].value, controller_key_callback, dev, config_button_map[i] );
nkeynes@770
   228
    }
nkeynes@2
   229
nkeynes@2
   230
}
nkeynes@2
   231
nkeynes@2
   232
nkeynes@770
   233
static int controller_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
nkeynes@502
   234
                         unsigned int *outlen )
nkeynes@2
   235
{
nkeynes@2
   236
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@2
   237
    if( function == MAPLE_FUNC_CONTROLLER ) {
nkeynes@2
   238
        *outlen = 2;
nkeynes@2
   239
        memcpy( outbuf, dev->condition, 8 );
nkeynes@2
   240
        return 0;
nkeynes@2
   241
    } else {
nkeynes@2
   242
        return MAPLE_ERR_FUNC_UNSUP;
nkeynes@2
   243
    }
nkeynes@2
   244
}
nkeynes@2
   245
.