Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 148:3f31c2d9b783
prev144:7f0714e89aaa
next411:5ae249d63c44
author nkeynes
date Sat May 20 02:40:51 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Implement analog joystick (and make sure it defaults to centered)
view annotate diff log raw
     1 /**
     2  * $Id: controller.c,v 1.4 2006-05-20 02:40:51 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, 0x80808080}, 
    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     return MAPLE_DEVICE(dev);
    72 }
    74 /**
    75  * Input callback 
    76  */
    77 void controller_key_callback( void *mdev, uint32_t value, gboolean isKeyDown )
    78 {
    79     controller_device_t dev = (controller_device_t)mdev;
    80     if( isKeyDown ) {
    81 	switch( value ) {
    82 	case JOY_LEFT:
    83 	    dev->condition[1] &= ~JOY_X_AXIS;
    84 	    break;
    85 	case JOY_RIGHT:
    86 	    dev->condition[1] |= JOY_X_AXIS;
    87 	    break;
    88 	case JOY_UP:
    89 	    dev->condition[1] &= ~JOY_Y_AXIS;
    90 	    break;
    91 	case JOY_DOWN:
    92 	    dev->condition[1] |= JOY_Y_AXIS;
    93 	    break;
    94 	default:
    95 	    dev->condition[0] &= ~value;
    96 	}
    97     } else {
    98 	switch(value ) {
    99 	case JOY_LEFT:
   100 	case JOY_RIGHT:
   101 	    dev->condition[1] = (dev->condition[1] & ~JOY_X_AXIS)| JOY_X_AXIS_CENTER;
   102 	    break;
   103 	case JOY_UP:
   104 	case JOY_DOWN:
   105 	    dev->condition[1] = (dev->condition[1] & ~JOY_Y_AXIS)| JOY_Y_AXIS_CENTER;
   106 	    break;
   107 	default:
   108 	    dev->condition[0] |= value;
   109 	}
   110     }
   111 }
   113 dreamcast_config_entry_t controller_get_config( maple_device_t mdev )
   114 {
   115     controller_device_t dev = (controller_device_t)mdev;
   116     return dev->config;
   117 }
   119 void controller_destroy( maple_device_t mdev )
   120 {
   121     free( mdev );
   122 }
   124 /**
   125  * Device is being attached to the bus. Go through the config and reserve the
   126  * keys we need.
   127  */
   128 void controller_attach( maple_device_t mdev )
   129 {
   130     controller_device_t dev = (controller_device_t)mdev;
   131     input_register_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
   132     input_register_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
   133     input_register_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
   134     input_register_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
   135     input_register_key( dev->config[4].value, controller_key_callback, dev, JOY_LEFT );
   136     input_register_key( dev->config[5].value, controller_key_callback, dev, JOY_RIGHT );
   137     input_register_key( dev->config[6].value, controller_key_callback, dev, JOY_UP );
   138     input_register_key( dev->config[7].value, controller_key_callback, dev, JOY_DOWN );
   139     input_register_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
   140     input_register_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
   141     input_register_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
   142     input_register_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
   143     input_register_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
   144     input_register_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
   145     input_register_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
   146 }
   148 void controller_detach( maple_device_t dev )
   149 {
   151 }
   154 int controller_get_cond( maple_device_t mdev, int function, char *outbuf,
   155                          int *outlen )
   156 {
   157     controller_device_t dev = (controller_device_t)mdev;
   158     if( function == MAPLE_FUNC_CONTROLLER ) {
   159         *outlen = 2;
   160         memcpy( outbuf, dev->condition, 8 );
   161         return 0;
   162     } else {
   163         return MAPLE_ERR_FUNC_UNSUP;
   164     }
   165 }
.