Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 561:533f6b478071
prev502:c4ecae2b1b5e
next614:a2d239d4438a
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
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@2
    20
#include "dream.h"
nkeynes@144
    21
#include "dreamcast.h"
nkeynes@429
    22
#include "display.h"
nkeynes@2
    23
#include "maple.h"
nkeynes@2
    24
#include "maple/controller.h"
nkeynes@2
    25
nkeynes@144
    26
#define CONTROLLER_CONFIG_ENTRIES 16
nkeynes@144
    27
nkeynes@2
    28
void controller_attach( maple_device_t dev );
nkeynes@2
    29
void controller_detach( maple_device_t dev );
nkeynes@144
    30
void controller_destroy( maple_device_t dev );
nkeynes@460
    31
maple_device_t controller_clone( maple_device_t dev );
nkeynes@144
    32
maple_device_t controller_new();
nkeynes@450
    33
lxdream_config_entry_t controller_get_config( maple_device_t dev );
nkeynes@429
    34
int controller_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
nkeynes@502
    35
                         unsigned int *outlen );
nkeynes@2
    36
nkeynes@2
    37
typedef struct controller_device {
nkeynes@2
    38
    struct maple_device dev;
nkeynes@2
    39
    uint32_t condition[2];
nkeynes@450
    40
    struct lxdream_config_entry config[CONTROLLER_CONFIG_ENTRIES];
nkeynes@2
    41
} *controller_device_t;
nkeynes@2
    42
nkeynes@144
    43
struct maple_device_class controller_class = { "Sega Controller", controller_new };
nkeynes@2
    44
nkeynes@144
    45
static struct controller_device base_controller = {
nkeynes@144
    46
    { MAPLE_DEVICE_TAG, &controller_class, CONTROLLER_IDENT, CONTROLLER_VERSION, 
nkeynes@144
    47
      controller_get_config, controller_attach, controller_detach, controller_destroy,
nkeynes@460
    48
      controller_clone, NULL, NULL, controller_get_cond, NULL, NULL, NULL },
nkeynes@148
    49
    {0x0000FFFF, 0x80808080}, 
nkeynes@144
    50
    {{ "dpad left", CONFIG_TYPE_KEY },
nkeynes@144
    51
     { "dpad right", CONFIG_TYPE_KEY },
nkeynes@144
    52
     { "dpad up", CONFIG_TYPE_KEY },
nkeynes@144
    53
     { "dpad down", CONFIG_TYPE_KEY },
nkeynes@144
    54
     { "analog left", CONFIG_TYPE_KEY },
nkeynes@144
    55
     { "analog right", CONFIG_TYPE_KEY },
nkeynes@144
    56
     { "analog up", CONFIG_TYPE_KEY },
nkeynes@144
    57
     { "analog down", CONFIG_TYPE_KEY },
nkeynes@144
    58
     { "button X", CONFIG_TYPE_KEY },
nkeynes@144
    59
     { "button Y", CONFIG_TYPE_KEY },
nkeynes@144
    60
     { "button A", CONFIG_TYPE_KEY },
nkeynes@144
    61
     { "button B", CONFIG_TYPE_KEY },
nkeynes@144
    62
     { "trigger left", CONFIG_TYPE_KEY },
nkeynes@144
    63
     { "trigger right", CONFIG_TYPE_KEY },
nkeynes@144
    64
     { "start", CONFIG_TYPE_KEY },
nkeynes@144
    65
     { NULL, CONFIG_TYPE_NONE }} };
nkeynes@144
    66
nkeynes@144
    67
#define CONTROLLER(x) ((controller_device_t)(x))
nkeynes@2
    68
nkeynes@2
    69
maple_device_t controller_new( )
nkeynes@2
    70
{
nkeynes@2
    71
    controller_device_t dev = malloc( sizeof(struct controller_device) );
nkeynes@2
    72
    memcpy( dev, &base_controller, sizeof(base_controller) );
nkeynes@2
    73
    return MAPLE_DEVICE(dev);
nkeynes@2
    74
}
nkeynes@2
    75
nkeynes@460
    76
maple_device_t controller_clone( maple_device_t srcdevice )
nkeynes@460
    77
{
nkeynes@460
    78
    controller_device_t src = (controller_device_t)srcdevice;
nkeynes@460
    79
    controller_device_t dev = (controller_device_t)controller_new();
nkeynes@460
    80
    lxdream_copy_config_list( dev->config, src->config );
nkeynes@460
    81
    memcpy( dev->condition, src->condition, sizeof(src->condition) );
nkeynes@460
    82
    return MAPLE_DEVICE(dev);
nkeynes@460
    83
}
nkeynes@460
    84
nkeynes@144
    85
/**
nkeynes@144
    86
 * Input callback 
nkeynes@144
    87
 */
nkeynes@144
    88
void controller_key_callback( void *mdev, uint32_t value, gboolean isKeyDown )
nkeynes@144
    89
{
nkeynes@144
    90
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
    91
    if( isKeyDown ) {
nkeynes@148
    92
	switch( value ) {
nkeynes@148
    93
	case JOY_LEFT:
nkeynes@148
    94
	    dev->condition[1] &= ~JOY_X_AXIS;
nkeynes@148
    95
	    break;
nkeynes@148
    96
	case JOY_RIGHT:
nkeynes@148
    97
	    dev->condition[1] |= JOY_X_AXIS;
nkeynes@148
    98
	    break;
nkeynes@148
    99
	case JOY_UP:
nkeynes@148
   100
	    dev->condition[1] &= ~JOY_Y_AXIS;
nkeynes@148
   101
	    break;
nkeynes@148
   102
	case JOY_DOWN:
nkeynes@148
   103
	    dev->condition[1] |= JOY_Y_AXIS;
nkeynes@148
   104
	    break;
nkeynes@411
   105
	case BUTTON_LEFT_TRIGGER:
nkeynes@411
   106
	case BUTTON_RIGHT_TRIGGER:
nkeynes@411
   107
	    dev->condition[0] |= value;
nkeynes@411
   108
	    break;
nkeynes@148
   109
	default:
nkeynes@148
   110
	    dev->condition[0] &= ~value;
nkeynes@148
   111
	}
nkeynes@144
   112
    } else {
nkeynes@148
   113
	switch(value ) {
nkeynes@148
   114
	case JOY_LEFT:
nkeynes@148
   115
	case JOY_RIGHT:
nkeynes@148
   116
	    dev->condition[1] = (dev->condition[1] & ~JOY_X_AXIS)| JOY_X_AXIS_CENTER;
nkeynes@148
   117
	    break;
nkeynes@148
   118
	case JOY_UP:
nkeynes@148
   119
	case JOY_DOWN:
nkeynes@148
   120
	    dev->condition[1] = (dev->condition[1] & ~JOY_Y_AXIS)| JOY_Y_AXIS_CENTER;
nkeynes@148
   121
	    break;
nkeynes@411
   122
	case BUTTON_LEFT_TRIGGER:
nkeynes@411
   123
	case BUTTON_RIGHT_TRIGGER:
nkeynes@411
   124
	    dev->condition[0] &= ~value;
nkeynes@411
   125
	    break;
nkeynes@148
   126
	default:
nkeynes@148
   127
	    dev->condition[0] |= value;
nkeynes@148
   128
	}
nkeynes@144
   129
    }
nkeynes@144
   130
}
nkeynes@2
   131
nkeynes@450
   132
lxdream_config_entry_t controller_get_config( maple_device_t mdev )
nkeynes@2
   133
{
nkeynes@144
   134
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
   135
    return dev->config;
nkeynes@144
   136
}
nkeynes@2
   137
nkeynes@144
   138
void controller_destroy( maple_device_t mdev )
nkeynes@144
   139
{
nkeynes@144
   140
    free( mdev );
nkeynes@144
   141
}
nkeynes@144
   142
nkeynes@144
   143
/**
nkeynes@144
   144
 * Device is being attached to the bus. Go through the config and reserve the
nkeynes@144
   145
 * keys we need.
nkeynes@144
   146
 */
nkeynes@144
   147
void controller_attach( maple_device_t mdev )
nkeynes@144
   148
{
nkeynes@144
   149
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
   150
    input_register_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
nkeynes@144
   151
    input_register_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
nkeynes@144
   152
    input_register_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
nkeynes@144
   153
    input_register_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
nkeynes@148
   154
    input_register_key( dev->config[4].value, controller_key_callback, dev, JOY_LEFT );
nkeynes@148
   155
    input_register_key( dev->config[5].value, controller_key_callback, dev, JOY_RIGHT );
nkeynes@148
   156
    input_register_key( dev->config[6].value, controller_key_callback, dev, JOY_UP );
nkeynes@148
   157
    input_register_key( dev->config[7].value, controller_key_callback, dev, JOY_DOWN );
nkeynes@144
   158
    input_register_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
nkeynes@144
   159
    input_register_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
nkeynes@144
   160
    input_register_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
nkeynes@144
   161
    input_register_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
nkeynes@144
   162
    input_register_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
nkeynes@144
   163
    input_register_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
nkeynes@144
   164
    input_register_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
nkeynes@2
   165
}
nkeynes@2
   166
nkeynes@451
   167
void controller_detach( maple_device_t mdev )
nkeynes@2
   168
{
nkeynes@451
   169
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@451
   170
    input_unregister_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
nkeynes@451
   171
    input_unregister_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
nkeynes@451
   172
    input_unregister_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
nkeynes@451
   173
    input_unregister_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
nkeynes@451
   174
    input_unregister_key( dev->config[4].value, controller_key_callback, dev, JOY_LEFT );
nkeynes@451
   175
    input_unregister_key( dev->config[5].value, controller_key_callback, dev, JOY_RIGHT );
nkeynes@451
   176
    input_unregister_key( dev->config[6].value, controller_key_callback, dev, JOY_UP );
nkeynes@451
   177
    input_unregister_key( dev->config[7].value, controller_key_callback, dev, JOY_DOWN );
nkeynes@451
   178
    input_unregister_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
nkeynes@451
   179
    input_unregister_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
nkeynes@451
   180
    input_unregister_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
nkeynes@451
   181
    input_unregister_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
nkeynes@451
   182
    input_unregister_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
nkeynes@451
   183
    input_unregister_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
nkeynes@451
   184
    input_unregister_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
nkeynes@2
   185
nkeynes@2
   186
}
nkeynes@2
   187
nkeynes@2
   188
nkeynes@429
   189
int controller_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
nkeynes@502
   190
                         unsigned int *outlen )
nkeynes@2
   191
{
nkeynes@2
   192
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@2
   193
    if( function == MAPLE_FUNC_CONTROLLER ) {
nkeynes@2
   194
        *outlen = 2;
nkeynes@2
   195
        memcpy( outbuf, dev->condition, 8 );
nkeynes@2
   196
        return 0;
nkeynes@2
   197
    } else {
nkeynes@2
   198
        return MAPLE_ERR_FUNC_UNSUP;
nkeynes@2
   199
    }
nkeynes@2
   200
}
nkeynes@2
   201
.