Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 838:9abb2fa58934
prev770:429ff505c450
next846:32ab6b592a8a
author nkeynes
date Tue Sep 02 00:42:43 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Add flag to maple devices indicating whether they need a mouse grab to operate
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 <assert.h>
    21 #include "dream.h"
    22 #include "dreamcast.h"
    23 #include "display.h"
    24 #include "maple.h"
    25 #include "maple/controller.h"
    27 #define CONTROLLER_CONFIG_ENTRIES 15
    29 static void controller_attach( maple_device_t dev );
    30 static void controller_detach( maple_device_t dev );
    31 static void controller_destroy( maple_device_t dev );
    32 static maple_device_t controller_clone( maple_device_t dev );
    33 static maple_device_t controller_new();
    34 static lxdream_config_entry_t controller_get_config( maple_device_t dev );
    35 static void controller_set_config_value( maple_device_t dev, unsigned int key, const gchar *value );
    36 static int controller_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
    37                          unsigned int *outlen );
    39 typedef struct controller_device {
    40     struct maple_device dev;
    41     uint32_t condition[2];
    42     struct lxdream_config_entry config[CONTROLLER_CONFIG_ENTRIES+1];
    43 } *controller_device_t;
    45 struct maple_device_class controller_class = { "Sega Controller", controller_new };
    47 static struct controller_device base_controller = {
    48         { MAPLE_DEVICE_TAG, &controller_class, MAPLE_GRAB_DONTCARE,
    49           CONTROLLER_IDENT, CONTROLLER_VERSION, 
    50           controller_get_config, controller_set_config_value, 
    51           controller_attach, controller_detach, controller_destroy,
    52           controller_clone, NULL, NULL, controller_get_cond, NULL, NULL, NULL },
    53           {0x0000FFFF, 0x80808080}, 
    54           {{ "dpad left", N_("Dpad left"), CONFIG_TYPE_KEY },
    55            { "dpad right", N_("Dpad right"), CONFIG_TYPE_KEY },
    56            { "dpad up", N_("Dpad up"), CONFIG_TYPE_KEY },
    57            { "dpad down", N_("Dpad down"), CONFIG_TYPE_KEY },
    58            { "analog left", N_("Analog left"), CONFIG_TYPE_KEY },
    59            { "analog right", N_("Analog right"), CONFIG_TYPE_KEY },
    60            { "analog up", N_("Analog up"), CONFIG_TYPE_KEY },
    61            { "analog down", N_("Analog down"), CONFIG_TYPE_KEY },
    62            { "button X", N_("Button X"), CONFIG_TYPE_KEY },
    63            { "button Y", N_("Button Y"), CONFIG_TYPE_KEY },
    64            { "button A", N_("Button A"), CONFIG_TYPE_KEY },
    65            { "button B", N_("Button B"), CONFIG_TYPE_KEY },
    66            { "trigger left", N_("Trigger left"), CONFIG_TYPE_KEY },
    67            { "trigger right", N_("Trigger right"), CONFIG_TYPE_KEY },
    68            { "start", N_("Start button"), CONFIG_TYPE_KEY },
    69            { NULL, CONFIG_TYPE_NONE }} };
    71 static int config_button_map[] = { 
    72         BUTTON_DPAD_LEFT, BUTTON_DPAD_RIGHT, BUTTON_DPAD_UP, BUTTON_DPAD_DOWN,
    73         JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN, BUTTON_X, BUTTON_Y, BUTTON_A, 
    74         BUTTON_B, BUTTON_LEFT_TRIGGER, BUTTON_RIGHT_TRIGGER, BUTTON_START };
    76 #define CONTROLLER(x) ((controller_device_t)(x))
    78 static maple_device_t controller_new( )
    79 {
    80     controller_device_t dev = malloc( sizeof(struct controller_device) );
    81     memcpy( dev, &base_controller, sizeof(base_controller) );
    82     return MAPLE_DEVICE(dev);
    83 }
    85 static maple_device_t controller_clone( maple_device_t srcdevice )
    86 {
    87     controller_device_t src = (controller_device_t)srcdevice;
    88     controller_device_t dev = (controller_device_t)controller_new();
    89     lxdream_copy_config_list( dev->config, src->config );
    90     memcpy( dev->condition, src->condition, sizeof(src->condition) );
    91     return MAPLE_DEVICE(dev);
    92 }
    94 /**
    95  * Input callback 
    96  */
    97 static void controller_key_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
    98 {
    99     controller_device_t dev = (controller_device_t)mdev;
   100     if( isKeyDown ) {
   101         switch( value ) {
   102         case JOY_LEFT:
   103             dev->condition[1] &= ~JOY_X_AXIS;
   104             break;
   105         case JOY_RIGHT:
   106             dev->condition[1] |= JOY_X_AXIS;
   107             break;
   108         case JOY_UP:
   109             dev->condition[1] &= ~JOY_Y_AXIS;
   110             break;
   111         case JOY_DOWN:
   112             dev->condition[1] |= JOY_Y_AXIS;
   113             break;
   114         case BUTTON_LEFT_TRIGGER:
   115         case BUTTON_RIGHT_TRIGGER:
   116             dev->condition[0] |= value;
   117             break;
   118         default:
   119             dev->condition[0] &= ~value;
   120         }
   121     } else {
   122         switch(value ) {
   123         case JOY_LEFT:
   124         case JOY_RIGHT:
   125             dev->condition[1] = (dev->condition[1] & ~JOY_X_AXIS)| JOY_X_AXIS_CENTER;
   126             break;
   127         case JOY_UP:
   128         case JOY_DOWN:
   129             dev->condition[1] = (dev->condition[1] & ~JOY_Y_AXIS)| JOY_Y_AXIS_CENTER;
   130             break;
   131         case BUTTON_LEFT_TRIGGER:
   132         case BUTTON_RIGHT_TRIGGER:
   133             dev->condition[0] &= ~value;
   134             break;
   135         default:
   136             dev->condition[0] |= value;
   137         }
   138     }
   139 }
   141 static lxdream_config_entry_t controller_get_config( maple_device_t mdev )
   142 {
   143     controller_device_t dev = (controller_device_t)mdev;
   144     return dev->config;
   145 }
   147 static void controller_set_config_value( maple_device_t mdev, unsigned int key, const gchar *value )
   148 {
   149     controller_device_t dev = (controller_device_t)mdev;
   150     assert( key < CONTROLLER_CONFIG_ENTRIES );
   152     input_unregister_key( dev->config[key].value, controller_key_callback, dev, config_button_map[key] );
   153     lxdream_set_config_value( &dev->config[key], value );
   154     input_register_key( dev->config[key].value, controller_key_callback, dev, config_button_map[key] );
   155 }
   157 static void controller_destroy( maple_device_t mdev )
   158 {
   159     free( mdev );
   160 }
   162 /**
   163  * Device is being attached to the bus. Go through the config and reserve the
   164  * keys we need.
   165  */
   166 static void controller_attach( maple_device_t mdev )
   167 {
   168     controller_device_t dev = (controller_device_t)mdev;
   169     int i;
   170     for( i=0; i<CONTROLLER_CONFIG_ENTRIES; i++ ) {
   171         input_register_key( dev->config[i].value, controller_key_callback, dev, config_button_map[i] );
   172     }
   173 }
   175 static void controller_detach( maple_device_t mdev )
   176 {
   177     controller_device_t dev = (controller_device_t)mdev;
   178     int i;
   179     for( i=0; i<CONTROLLER_CONFIG_ENTRIES; i++ ) {
   180         input_unregister_key( dev->config[i].value, controller_key_callback, dev, config_button_map[i] );
   181     }
   183 }
   186 static int controller_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
   187                          unsigned int *outlen )
   188 {
   189     controller_device_t dev = (controller_device_t)mdev;
   190     if( function == MAPLE_FUNC_CONTROLLER ) {
   191         *outlen = 2;
   192         memcpy( outbuf, dev->condition, 8 );
   193         return 0;
   194     } else {
   195         return MAPLE_ERR_FUNC_UNSUP;
   196     }
   197 }
.