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