Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 846:32ab6b592a8a
prev838:9abb2fa58934
next848:34dc33c05106
author nkeynes
date Sat Sep 06 08:16:39 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Merge controller.h into controller.c for consistency with the other maple devices
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"
    26 /* First word of controller condition */
    27 #define BUTTON_C            0x00000001 /* not on standard controller */
    28 #define BUTTON_B            0x00000002
    29 #define BUTTON_A            0x00000004
    30 #define BUTTON_START        0x00000008
    31 #define BUTTON_DPAD_UP      0x00000010
    32 #define BUTTON_DPAD_DOWN    0x00000020
    33 #define BUTTON_DPAD_LEFT    0x00000040
    34 #define BUTTON_DPAD_RIGHT   0x00000080
    35 #define BUTTON_Z            0x00000100 /* not on standard controller */
    36 #define BUTTON_Y            0x00000200
    37 #define BUTTON_X            0x00000400
    38 #define BUTTON_D            0x00000800 /* not on standard controller */
    39 #define BUTTON_LEFT_TRIGGER 0xFF000000 /* Bitmask */
    40 #define BUTTON_RIGHT_TRIGGER 0x00FF0000 /* Bitmask */
    42 /* Second word of controller condition (bitmasks) */
    43 #define JOY_X_AXIS          0x000000FF
    44 #define JOY_Y_AXIS          0x0000FF00
    45 #define JOY_X_AXIS_CENTER   0x00000080
    46 #define JOY_Y_AXIS_CENTER   0x00008000
    47 #define JOY2_X_AXIS         0x00FF0000 /* not on standard controller */
    48 #define JOY2_Y_AXIS         0xFF000000 /* not on standard controller */
    50 /* The following bits are used by the emulator for flags but don't actually
    51  * appear in the hardware
    52  */
    53 #define JOY_LEFT            0x80000001
    54 #define JOY_RIGHT           0x80000002
    55 #define JOY_UP              0x80000004
    56 #define JOY_DOWN            0x80000008
    58 /* Standard controller ID */
    59 #define CONTROLLER_IDENT {0x00, 0x00, 0x00, 0x01,  0x00, 0x0f, 0x06, 0xfe,  0x00, 0x00, 0x00, 0x00, \
    60     0x00, 0x00, 0x00, 0x00,  0xff, 0x00, 0x44, 0x72,  0x65, 0x61, 0x6d, 0x63,  0x61, 0x73, 0x74, 0x20,  \
    61     0x43, 0x6f, 0x6e, 0x74,  0x72, 0x6f, 0x6c, 0x6c,  0x65, 0x72, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20, \
    62     0x20, 0x20, 0x20, 0x20,  0x50, 0x72, 0x6f, 0x64,  0x75, 0x63, 0x65, 0x64,  0x20, 0x42, 0x79, 0x20, \
    63     0x6f, 0x72, 0x20, 0x55,  0x6e, 0x64, 0x65, 0x72,  0x20, 0x4c, 0x69, 0x63,  0x65, 0x6e, 0x73, 0x65, \
    64     0x20, 0x46, 0x72, 0x6f,  0x6d, 0x20, 0x53, 0x45,  0x47, 0x41, 0x20, 0x45,  0x4e, 0x54, 0x45, 0x52, \
    65     0x50, 0x52, 0x49, 0x53,  0x45, 0x53, 0x2c, 0x4c,  0x54, 0x44, 0x2e, 0x20,  0x20, 0x20, 0x20, 0x20, \
    66     0xae, 0x01, 0xf4, 0x01}
    67 #define CONTROLLER_VERSION {0x56, 0x65, 0x72, 0x73,  0x69, 0x6f, 0x6e, 0x20,  0x31, 0x2e, 0x30, 0x31, \
    68     0x30, 0x2c, 0x31, 0x39,  0x39, 0x38, 0x2f, 0x30,  0x39, 0x2f, 0x32, 0x38,  0x2c, 0x33, 0x31, 0x35, \
    69     0x2d, 0x36, 0x32, 0x31,  0x31, 0x2d, 0x41, 0x42,  0x20, 0x20, 0x20, 0x2c,  0x41, 0x6e, 0x61, 0x6c, \
    70     0x6f, 0x67, 0x20, 0x4d,  0x6f, 0x64, 0x75, 0x6c,  0x65, 0x20, 0x3a, 0x20,  0x54, 0x68, 0x65, 0x20, \
    71     0x34, 0x74, 0x68, 0x20,  0x45, 0x64, 0x69, 0x74,  0x69, 0x6f, 0x6e, 0x2e,  0x35, 0x2f, 0x38, 0x20, \
    72     0x20, 0x2b, 0x44, 0x46 }
    74 #define CONTROLLER_CONFIG_ENTRIES 15
    76 static void controller_attach( maple_device_t dev );
    77 static void controller_detach( maple_device_t dev );
    78 static void controller_destroy( maple_device_t dev );
    79 static maple_device_t controller_clone( maple_device_t dev );
    80 static maple_device_t controller_new();
    81 static lxdream_config_entry_t controller_get_config( maple_device_t dev );
    82 static void controller_set_config_value( maple_device_t dev, unsigned int key, const gchar *value );
    83 static int controller_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
    84                          unsigned int *outlen );
    86 typedef struct controller_device {
    87     struct maple_device dev;
    88     uint32_t condition[2];
    89     struct lxdream_config_entry config[CONTROLLER_CONFIG_ENTRIES+1];
    90 } *controller_device_t;
    92 struct maple_device_class controller_class = { "Sega Controller", controller_new };
    94 static struct controller_device base_controller = {
    95         { MAPLE_DEVICE_TAG, &controller_class, MAPLE_GRAB_DONTCARE,
    96           CONTROLLER_IDENT, CONTROLLER_VERSION, 
    97           controller_get_config, controller_set_config_value, 
    98           controller_attach, controller_detach, controller_destroy,
    99           controller_clone, NULL, NULL, controller_get_cond, NULL, NULL, NULL },
   100           {0x0000FFFF, 0x80808080}, 
   101           {{ "dpad left", N_("Dpad left"), CONFIG_TYPE_KEY },
   102            { "dpad right", N_("Dpad right"), CONFIG_TYPE_KEY },
   103            { "dpad up", N_("Dpad up"), CONFIG_TYPE_KEY },
   104            { "dpad down", N_("Dpad down"), CONFIG_TYPE_KEY },
   105            { "analog left", N_("Analog left"), CONFIG_TYPE_KEY },
   106            { "analog right", N_("Analog right"), CONFIG_TYPE_KEY },
   107            { "analog up", N_("Analog up"), CONFIG_TYPE_KEY },
   108            { "analog down", N_("Analog down"), CONFIG_TYPE_KEY },
   109            { "button X", N_("Button X"), CONFIG_TYPE_KEY },
   110            { "button Y", N_("Button Y"), CONFIG_TYPE_KEY },
   111            { "button A", N_("Button A"), CONFIG_TYPE_KEY },
   112            { "button B", N_("Button B"), CONFIG_TYPE_KEY },
   113            { "trigger left", N_("Trigger left"), CONFIG_TYPE_KEY },
   114            { "trigger right", N_("Trigger right"), CONFIG_TYPE_KEY },
   115            { "start", N_("Start button"), CONFIG_TYPE_KEY },
   116            { NULL, CONFIG_TYPE_NONE }} };
   118 static int config_button_map[] = { 
   119         BUTTON_DPAD_LEFT, BUTTON_DPAD_RIGHT, BUTTON_DPAD_UP, BUTTON_DPAD_DOWN,
   120         JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN, BUTTON_X, BUTTON_Y, BUTTON_A, 
   121         BUTTON_B, BUTTON_LEFT_TRIGGER, BUTTON_RIGHT_TRIGGER, BUTTON_START };
   123 #define CONTROLLER(x) ((controller_device_t)(x))
   125 static maple_device_t controller_new( )
   126 {
   127     controller_device_t dev = malloc( sizeof(struct controller_device) );
   128     memcpy( dev, &base_controller, sizeof(base_controller) );
   129     return MAPLE_DEVICE(dev);
   130 }
   132 static maple_device_t controller_clone( maple_device_t srcdevice )
   133 {
   134     controller_device_t src = (controller_device_t)srcdevice;
   135     controller_device_t dev = (controller_device_t)controller_new();
   136     lxdream_copy_config_list( dev->config, src->config );
   137     memcpy( dev->condition, src->condition, sizeof(src->condition) );
   138     return MAPLE_DEVICE(dev);
   139 }
   141 /**
   142  * Input callback 
   143  */
   144 static void controller_key_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   145 {
   146     controller_device_t dev = (controller_device_t)mdev;
   147     if( isKeyDown ) {
   148         switch( value ) {
   149         case JOY_LEFT:
   150             dev->condition[1] &= ~JOY_X_AXIS;
   151             break;
   152         case JOY_RIGHT:
   153             dev->condition[1] |= JOY_X_AXIS;
   154             break;
   155         case JOY_UP:
   156             dev->condition[1] &= ~JOY_Y_AXIS;
   157             break;
   158         case JOY_DOWN:
   159             dev->condition[1] |= JOY_Y_AXIS;
   160             break;
   161         case BUTTON_LEFT_TRIGGER:
   162         case BUTTON_RIGHT_TRIGGER:
   163             dev->condition[0] |= value;
   164             break;
   165         default:
   166             dev->condition[0] &= ~value;
   167         }
   168     } else {
   169         switch(value ) {
   170         case JOY_LEFT:
   171         case JOY_RIGHT:
   172             dev->condition[1] = (dev->condition[1] & ~JOY_X_AXIS)| JOY_X_AXIS_CENTER;
   173             break;
   174         case JOY_UP:
   175         case JOY_DOWN:
   176             dev->condition[1] = (dev->condition[1] & ~JOY_Y_AXIS)| JOY_Y_AXIS_CENTER;
   177             break;
   178         case BUTTON_LEFT_TRIGGER:
   179         case BUTTON_RIGHT_TRIGGER:
   180             dev->condition[0] &= ~value;
   181             break;
   182         default:
   183             dev->condition[0] |= value;
   184         }
   185     }
   186 }
   188 static lxdream_config_entry_t controller_get_config( maple_device_t mdev )
   189 {
   190     controller_device_t dev = (controller_device_t)mdev;
   191     return dev->config;
   192 }
   194 static void controller_set_config_value( maple_device_t mdev, unsigned int key, const gchar *value )
   195 {
   196     controller_device_t dev = (controller_device_t)mdev;
   197     assert( key < CONTROLLER_CONFIG_ENTRIES );
   199     input_unregister_key( dev->config[key].value, controller_key_callback, dev, config_button_map[key] );
   200     lxdream_set_config_value( &dev->config[key], value );
   201     input_register_key( dev->config[key].value, controller_key_callback, dev, config_button_map[key] );
   202 }
   204 static void controller_destroy( maple_device_t mdev )
   205 {
   206     free( mdev );
   207 }
   209 /**
   210  * Device is being attached to the bus. Go through the config and reserve the
   211  * keys we need.
   212  */
   213 static void controller_attach( maple_device_t mdev )
   214 {
   215     controller_device_t dev = (controller_device_t)mdev;
   216     int i;
   217     for( i=0; i<CONTROLLER_CONFIG_ENTRIES; i++ ) {
   218         input_register_key( dev->config[i].value, controller_key_callback, dev, config_button_map[i] );
   219     }
   220 }
   222 static void controller_detach( maple_device_t mdev )
   223 {
   224     controller_device_t dev = (controller_device_t)mdev;
   225     int i;
   226     for( i=0; i<CONTROLLER_CONFIG_ENTRIES; i++ ) {
   227         input_unregister_key( dev->config[i].value, controller_key_callback, dev, config_button_map[i] );
   228     }
   230 }
   233 static int controller_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
   234                          unsigned int *outlen )
   235 {
   236     controller_device_t dev = (controller_device_t)mdev;
   237     if( function == MAPLE_FUNC_CONTROLLER ) {
   238         *outlen = 2;
   239         memcpy( outbuf, dev->condition, 8 );
   240         return 0;
   241     } else {
   242         return MAPLE_ERR_FUNC_UNSUP;
   243     }
   244 }
.