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 (13 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
view annotate diff log raw
     1 /**
     2  * $Id: controller.c,v 1.3 2006-05-15 08:28:52 nkeynes Exp $
     3  *
     4  * Implements the standard dreamcast controller
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <stdlib.h>
    20 #include "dream.h"
    21 #include "dreamcast.h"
    22 #include "maple.h"
    23 #include "maple/controller.h"
    25 #define CONTROLLER_CONFIG_ENTRIES 16
    27 void controller_attach( maple_device_t dev );
    28 void controller_detach( maple_device_t dev );
    29 void controller_destroy( maple_device_t dev );
    30 maple_device_t controller_new();
    31 dreamcast_config_entry_t controller_get_config( maple_device_t dev );
    32 int controller_get_cond( maple_device_t dev, int function, char *outbuf,
    33                          int *outlen );
    35 typedef struct controller_device {
    36     struct maple_device dev;
    37     uint32_t condition[2];
    38     struct dreamcast_config_entry config[CONTROLLER_CONFIG_ENTRIES];
    39 } *controller_device_t;
    41 struct maple_device_class controller_class = { "Sega Controller", controller_new };
    43 static struct controller_device base_controller = {
    44     { MAPLE_DEVICE_TAG, &controller_class, CONTROLLER_IDENT, CONTROLLER_VERSION, 
    45       controller_get_config, controller_attach, controller_detach, controller_destroy,
    46       NULL, NULL, controller_get_cond, NULL, NULL, NULL },
    47     {0x0000FFFF, 0}, 
    48     {{ "dpad left", CONFIG_TYPE_KEY },
    49      { "dpad right", CONFIG_TYPE_KEY },
    50      { "dpad up", CONFIG_TYPE_KEY },
    51      { "dpad down", CONFIG_TYPE_KEY },
    52      { "analog left", CONFIG_TYPE_KEY },
    53      { "analog right", CONFIG_TYPE_KEY },
    54      { "analog up", CONFIG_TYPE_KEY },
    55      { "analog down", CONFIG_TYPE_KEY },
    56      { "button X", CONFIG_TYPE_KEY },
    57      { "button Y", CONFIG_TYPE_KEY },
    58      { "button A", CONFIG_TYPE_KEY },
    59      { "button B", CONFIG_TYPE_KEY },
    60      { "trigger left", CONFIG_TYPE_KEY },
    61      { "trigger right", CONFIG_TYPE_KEY },
    62      { "start", CONFIG_TYPE_KEY },
    63      { NULL, CONFIG_TYPE_NONE }} };
    65 #define CONTROLLER(x) ((controller_device_t)(x))
    67 maple_device_t controller_new( )
    68 {
    69     controller_device_t dev = malloc( sizeof(struct controller_device) );
    70     memcpy( dev, &base_controller, sizeof(base_controller) );
    71     memset( dev->condition, 0, 8 );
    72     dev->condition[0] = 0x0000FFFF;
    73     return MAPLE_DEVICE(dev);
    74 }
    76 /**
    77  * Input callback 
    78  */
    79 void controller_key_callback( void *mdev, uint32_t value, gboolean isKeyDown )
    80 {
    81     controller_device_t dev = (controller_device_t)mdev;
    82     if( isKeyDown ) {
    83 	dev->condition[0] |= value;
    84 	fprintf( stderr, "Key %08X DOWN\n", value );
    85     } else {
    86 	dev->condition[0] &= ~value;
    87 	fprintf( stderr, "Key %08X UP\n", value );
    88     }
    89 }
    91 dreamcast_config_entry_t controller_get_config( maple_device_t mdev )
    92 {
    93     controller_device_t dev = (controller_device_t)mdev;
    94     return dev->config;
    95 }
    97 void controller_destroy( maple_device_t mdev )
    98 {
    99     free( mdev );
   100 }
   102 /**
   103  * Device is being attached to the bus. Go through the config and reserve the
   104  * keys we need.
   105  */
   106 void controller_attach( maple_device_t mdev )
   107 {
   108     controller_device_t dev = (controller_device_t)mdev;
   109     input_register_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
   110     input_register_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
   111     input_register_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
   112     input_register_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
   113     input_register_key( dev->config[4].value, controller_key_callback, dev, 0 );
   114     input_register_key( dev->config[5].value, controller_key_callback, dev, 0 );
   115     input_register_key( dev->config[6].value, controller_key_callback, dev, 0 );
   116     input_register_key( dev->config[7].value, controller_key_callback, dev, 0 );
   117     input_register_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
   118     input_register_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
   119     input_register_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
   120     input_register_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
   121     input_register_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
   122     input_register_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
   123     input_register_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
   124 }
   126 void controller_detach( maple_device_t dev )
   127 {
   129 }
   132 int controller_get_cond( maple_device_t mdev, int function, char *outbuf,
   133                          int *outlen )
   134 {
   135     controller_device_t dev = (controller_device_t)mdev;
   136     if( function == MAPLE_FUNC_CONTROLLER ) {
   137         *outlen = 2;
   138         memcpy( outbuf, dev->condition, 8 );
   139         return 0;
   140     } else {
   141         return MAPLE_ERR_FUNC_UNSUP;
   142     }
   143 }
.