Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 144:7f0714e89aaa
prev31:495e480360d7
next148:3f31c2d9b783
author nkeynes
date Mon May 15 08:28:52 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Rename video_driver to display_driver
Add input source to display
Implement configuration file support
Hook controllers up to configuration
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@144
     2
 * $Id: controller.c,v 1.3 2006-05-15 08:28:52 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@144
    47
    {0x0000FFFF, 0}, 
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
    memset( dev->condition, 0, 8 );
nkeynes@2
    72
    dev->condition[0] = 0x0000FFFF;
nkeynes@2
    73
    return MAPLE_DEVICE(dev);
nkeynes@2
    74
}
nkeynes@2
    75
nkeynes@144
    76
/**
nkeynes@144
    77
 * Input callback 
nkeynes@144
    78
 */
nkeynes@144
    79
void controller_key_callback( void *mdev, uint32_t value, gboolean isKeyDown )
nkeynes@144
    80
{
nkeynes@144
    81
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
    82
    if( isKeyDown ) {
nkeynes@144
    83
	dev->condition[0] |= value;
nkeynes@144
    84
	fprintf( stderr, "Key %08X DOWN\n", value );
nkeynes@144
    85
    } else {
nkeynes@144
    86
	dev->condition[0] &= ~value;
nkeynes@144
    87
	fprintf( stderr, "Key %08X UP\n", value );
nkeynes@144
    88
    }
nkeynes@144
    89
}
nkeynes@2
    90
nkeynes@144
    91
dreamcast_config_entry_t controller_get_config( maple_device_t mdev )
nkeynes@2
    92
{
nkeynes@144
    93
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
    94
    return dev->config;
nkeynes@144
    95
}
nkeynes@2
    96
nkeynes@144
    97
void controller_destroy( maple_device_t mdev )
nkeynes@144
    98
{
nkeynes@144
    99
    free( mdev );
nkeynes@144
   100
}
nkeynes@144
   101
nkeynes@144
   102
/**
nkeynes@144
   103
 * Device is being attached to the bus. Go through the config and reserve the
nkeynes@144
   104
 * keys we need.
nkeynes@144
   105
 */
nkeynes@144
   106
void controller_attach( maple_device_t mdev )
nkeynes@144
   107
{
nkeynes@144
   108
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@144
   109
    input_register_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
nkeynes@144
   110
    input_register_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
nkeynes@144
   111
    input_register_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
nkeynes@144
   112
    input_register_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
nkeynes@144
   113
    input_register_key( dev->config[4].value, controller_key_callback, dev, 0 );
nkeynes@144
   114
    input_register_key( dev->config[5].value, controller_key_callback, dev, 0 );
nkeynes@144
   115
    input_register_key( dev->config[6].value, controller_key_callback, dev, 0 );
nkeynes@144
   116
    input_register_key( dev->config[7].value, controller_key_callback, dev, 0 );
nkeynes@144
   117
    input_register_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
nkeynes@144
   118
    input_register_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
nkeynes@144
   119
    input_register_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
nkeynes@144
   120
    input_register_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
nkeynes@144
   121
    input_register_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
nkeynes@144
   122
    input_register_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
nkeynes@144
   123
    input_register_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
nkeynes@2
   124
}
nkeynes@2
   125
nkeynes@2
   126
void controller_detach( maple_device_t dev )
nkeynes@2
   127
{
nkeynes@2
   128
nkeynes@2
   129
}
nkeynes@2
   130
nkeynes@2
   131
nkeynes@2
   132
int controller_get_cond( maple_device_t mdev, int function, char *outbuf,
nkeynes@2
   133
                         int *outlen )
nkeynes@2
   134
{
nkeynes@2
   135
    controller_device_t dev = (controller_device_t)mdev;
nkeynes@2
   136
    if( function == MAPLE_FUNC_CONTROLLER ) {
nkeynes@2
   137
        *outlen = 2;
nkeynes@2
   138
        memcpy( outbuf, dev->condition, 8 );
nkeynes@2
   139
        return 0;
nkeynes@2
   140
    } else {
nkeynes@2
   141
        return MAPLE_ERR_FUNC_UNSUP;
nkeynes@2
   142
    }
nkeynes@2
   143
}
nkeynes@2
   144
.