Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 411:5ae249d63c44
prev148:3f31c2d9b783
next429:e581b90c3fb3
author nkeynes
date Sun Sep 30 21:19:43 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix trigger buttons (sense was inverted)
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@411
     2
 * $Id: controller.c,v 1.5 2007-09-30 21:19:43 nkeynes Exp $
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@2
    22
#include "maple.h"
nkeynes@2
    23
#include "maple/controller.h"
nkeynes@2
    24
nkeynes@144
    25
#define CONTROLLER_CONFIG_ENTRIES 16
nkeynes@144
    26
nkeynes@2
    27
void controller_attach( maple_device_t dev );
nkeynes@2
    28
void controller_detach( maple_device_t dev );
nkeynes@144
    29
void controller_destroy( maple_device_t dev );
nkeynes@144
    30
maple_device_t controller_new();
nkeynes@144
    31
dreamcast_config_entry_t controller_get_config( maple_device_t dev );
nkeynes@2
    32
int controller_get_cond( maple_device_t dev, int function, char *outbuf,
nkeynes@2
    33
                         int *outlen );
nkeynes@2
    34
nkeynes@2
    35
typedef struct controller_device {
nkeynes@2
    36
    struct maple_device dev;
nkeynes@2
    37
    uint32_t condition[2];
nkeynes@144
    38
    struct dreamcast_config_entry config[CONTROLLER_CONFIG_ENTRIES];
nkeynes@2
    39
} *controller_device_t;
nkeynes@2
    40
nkeynes@144
    41
struct maple_device_class controller_class = { "Sega Controller", controller_new };
nkeynes@2
    42
nkeynes@144
    43
static struct controller_device base_controller = {
nkeynes@144
    44
    { MAPLE_DEVICE_TAG, &controller_class, CONTROLLER_IDENT, CONTROLLER_VERSION, 
nkeynes@144
    45
      controller_get_config, controller_attach, controller_detach, controller_destroy,
nkeynes@144
    46
      NULL, NULL, controller_get_cond, NULL, NULL, NULL },
nkeynes@148
    47
    {0x0000FFFF, 0x80808080}, 
nkeynes@144
    48
    {{ "dpad left", CONFIG_TYPE_KEY },
nkeynes@144
    49
     { "dpad right", CONFIG_TYPE_KEY },
nkeynes@144
    50
     { "dpad up", CONFIG_TYPE_KEY },
nkeynes@144
    51
     { "dpad down", CONFIG_TYPE_KEY },
nkeynes@144
    52
     { "analog left", CONFIG_TYPE_KEY },
nkeynes@144
    53
     { "analog right", CONFIG_TYPE_KEY },
nkeynes@144
    54
     { "analog up", CONFIG_TYPE_KEY },
nkeynes@144
    55
     { "analog down", CONFIG_TYPE_KEY },
nkeynes@144
    56
     { "button X", CONFIG_TYPE_KEY },
nkeynes@144
    57
     { "button Y", CONFIG_TYPE_KEY },
nkeynes@144
    58
     { "button A", CONFIG_TYPE_KEY },
nkeynes@144
    59
     { "button B", CONFIG_TYPE_KEY },
nkeynes@144
    60
     { "trigger left", CONFIG_TYPE_KEY },
nkeynes@144
    61
     { "trigger right", CONFIG_TYPE_KEY },
nkeynes@144
    62
     { "start", CONFIG_TYPE_KEY },
nkeynes@144
    63
     { NULL, CONFIG_TYPE_NONE }} };
nkeynes@144
    64
nkeynes@144
    65
#define CONTROLLER(x) ((controller_device_t)(x))
nkeynes@2
    66
nkeynes@2
    67
maple_device_t controller_new( )
nkeynes@2
    68
{
nkeynes@2
    69
    controller_device_t dev = malloc( sizeof(struct controller_device) );
nkeynes@2
    70
    memcpy( dev, &base_controller, sizeof(base_controller) );
nkeynes@2
    71
    return MAPLE_DEVICE(dev);
nkeynes@2
    72
}
nkeynes@2
    73
nkeynes@144
    74
/**
nkeynes@144
    75
 * Input callback 
nkeynes@144
    76
 */
nkeynes@144
    77
void controller_key_callback( void *mdev, uint32_t value, gboolean isKeyDown )
nkeynes@144
    78
{
nkeynes@144
    79
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
    80
    if( isKeyDown ) {
nkeynes@148
    81
	switch( value ) {
nkeynes@148
    82
	case JOY_LEFT:
nkeynes@148
    83
	    dev->condition[1] &= ~JOY_X_AXIS;
nkeynes@148
    84
	    break;
nkeynes@148
    85
	case JOY_RIGHT:
nkeynes@148
    86
	    dev->condition[1] |= JOY_X_AXIS;
nkeynes@148
    87
	    break;
nkeynes@148
    88
	case JOY_UP:
nkeynes@148
    89
	    dev->condition[1] &= ~JOY_Y_AXIS;
nkeynes@148
    90
	    break;
nkeynes@148
    91
	case JOY_DOWN:
nkeynes@148
    92
	    dev->condition[1] |= JOY_Y_AXIS;
nkeynes@148
    93
	    break;
nkeynes@411
    94
	case BUTTON_LEFT_TRIGGER:
nkeynes@411
    95
	case BUTTON_RIGHT_TRIGGER:
nkeynes@411
    96
	    dev->condition[0] |= value;
nkeynes@411
    97
	    break;
nkeynes@148
    98
	default:
nkeynes@148
    99
	    dev->condition[0] &= ~value;
nkeynes@148
   100
	}
nkeynes@144
   101
    } else {
nkeynes@148
   102
	switch(value ) {
nkeynes@148
   103
	case JOY_LEFT:
nkeynes@148
   104
	case JOY_RIGHT:
nkeynes@148
   105
	    dev->condition[1] = (dev->condition[1] & ~JOY_X_AXIS)| JOY_X_AXIS_CENTER;
nkeynes@148
   106
	    break;
nkeynes@148
   107
	case JOY_UP:
nkeynes@148
   108
	case JOY_DOWN:
nkeynes@148
   109
	    dev->condition[1] = (dev->condition[1] & ~JOY_Y_AXIS)| JOY_Y_AXIS_CENTER;
nkeynes@148
   110
	    break;
nkeynes@411
   111
	case BUTTON_LEFT_TRIGGER:
nkeynes@411
   112
	case BUTTON_RIGHT_TRIGGER:
nkeynes@411
   113
	    dev->condition[0] &= ~value;
nkeynes@411
   114
	    break;
nkeynes@148
   115
	default:
nkeynes@148
   116
	    dev->condition[0] |= value;
nkeynes@148
   117
	}
nkeynes@144
   118
    }
nkeynes@144
   119
}
nkeynes@2
   120
nkeynes@144
   121
dreamcast_config_entry_t controller_get_config( maple_device_t mdev )
nkeynes@2
   122
{
nkeynes@144
   123
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
   124
    return dev->config;
nkeynes@144
   125
}
nkeynes@2
   126
nkeynes@144
   127
void controller_destroy( maple_device_t mdev )
nkeynes@144
   128
{
nkeynes@144
   129
    free( mdev );
nkeynes@144
   130
}
nkeynes@144
   131
nkeynes@144
   132
/**
nkeynes@144
   133
 * Device is being attached to the bus. Go through the config and reserve the
nkeynes@144
   134
 * keys we need.
nkeynes@144
   135
 */
nkeynes@144
   136
void controller_attach( maple_device_t mdev )
nkeynes@144
   137
{
nkeynes@144
   138
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
   139
    input_register_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
nkeynes@144
   140
    input_register_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
nkeynes@144
   141
    input_register_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
nkeynes@144
   142
    input_register_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
nkeynes@148
   143
    input_register_key( dev->config[4].value, controller_key_callback, dev, JOY_LEFT );
nkeynes@148
   144
    input_register_key( dev->config[5].value, controller_key_callback, dev, JOY_RIGHT );
nkeynes@148
   145
    input_register_key( dev->config[6].value, controller_key_callback, dev, JOY_UP );
nkeynes@148
   146
    input_register_key( dev->config[7].value, controller_key_callback, dev, JOY_DOWN );
nkeynes@144
   147
    input_register_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
nkeynes@144
   148
    input_register_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
nkeynes@144
   149
    input_register_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
nkeynes@144
   150
    input_register_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
nkeynes@144
   151
    input_register_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
nkeynes@144
   152
    input_register_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
nkeynes@144
   153
    input_register_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
nkeynes@2
   154
}
nkeynes@2
   155
nkeynes@2
   156
void controller_detach( maple_device_t dev )
nkeynes@2
   157
{
nkeynes@2
   158
nkeynes@2
   159
}
nkeynes@2
   160
nkeynes@2
   161
nkeynes@2
   162
int controller_get_cond( maple_device_t mdev, int function, char *outbuf,
nkeynes@2
   163
                         int *outlen )
nkeynes@2
   164
{
nkeynes@2
   165
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@2
   166
    if( function == MAPLE_FUNC_CONTROLLER ) {
nkeynes@2
   167
        *outlen = 2;
nkeynes@2
   168
        memcpy( outbuf, dev->condition, 8 );
nkeynes@2
   169
        return 0;
nkeynes@2
   170
    } else {
nkeynes@2
   171
        return MAPLE_ERR_FUNC_UNSUP;
nkeynes@2
   172
    }
nkeynes@2
   173
}
nkeynes@2
   174
.