Search
lxdream.org :: lxdream/src/maple/controller.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/controller.c
changeset 451:50622730f226
prev450:207461e79f21
next460:a0c865b74c63
author nkeynes
date Sun Oct 21 05:15:56 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Unregister keys on controller detach
view annotate diff log raw
     1 /**
     2  * $Id: controller.c,v 1.8 2007-10-21 05:15:56 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 "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_new();
    32 lxdream_config_entry_t controller_get_config( maple_device_t dev );
    33 int controller_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
    34                          int *outlen );
    36 typedef struct controller_device {
    37     struct maple_device dev;
    38     uint32_t condition[2];
    39     struct lxdream_config_entry config[CONTROLLER_CONFIG_ENTRIES];
    40 } *controller_device_t;
    42 struct maple_device_class controller_class = { "Sega Controller", controller_new };
    44 static struct controller_device base_controller = {
    45     { MAPLE_DEVICE_TAG, &controller_class, CONTROLLER_IDENT, CONTROLLER_VERSION, 
    46       controller_get_config, controller_attach, controller_detach, controller_destroy,
    47       NULL, NULL, controller_get_cond, NULL, NULL, NULL },
    48     {0x0000FFFF, 0x80808080}, 
    49     {{ "dpad left", CONFIG_TYPE_KEY },
    50      { "dpad right", CONFIG_TYPE_KEY },
    51      { "dpad up", CONFIG_TYPE_KEY },
    52      { "dpad down", CONFIG_TYPE_KEY },
    53      { "analog left", CONFIG_TYPE_KEY },
    54      { "analog right", CONFIG_TYPE_KEY },
    55      { "analog up", CONFIG_TYPE_KEY },
    56      { "analog down", CONFIG_TYPE_KEY },
    57      { "button X", CONFIG_TYPE_KEY },
    58      { "button Y", CONFIG_TYPE_KEY },
    59      { "button A", CONFIG_TYPE_KEY },
    60      { "button B", CONFIG_TYPE_KEY },
    61      { "trigger left", CONFIG_TYPE_KEY },
    62      { "trigger right", CONFIG_TYPE_KEY },
    63      { "start", CONFIG_TYPE_KEY },
    64      { NULL, CONFIG_TYPE_NONE }} };
    66 #define CONTROLLER(x) ((controller_device_t)(x))
    68 maple_device_t controller_new( )
    69 {
    70     controller_device_t dev = malloc( sizeof(struct controller_device) );
    71     memcpy( dev, &base_controller, sizeof(base_controller) );
    72     return MAPLE_DEVICE(dev);
    73 }
    75 /**
    76  * Input callback 
    77  */
    78 void controller_key_callback( void *mdev, uint32_t value, gboolean isKeyDown )
    79 {
    80     controller_device_t dev = (controller_device_t)mdev;
    81     if( isKeyDown ) {
    82 	switch( value ) {
    83 	case JOY_LEFT:
    84 	    dev->condition[1] &= ~JOY_X_AXIS;
    85 	    break;
    86 	case JOY_RIGHT:
    87 	    dev->condition[1] |= JOY_X_AXIS;
    88 	    break;
    89 	case JOY_UP:
    90 	    dev->condition[1] &= ~JOY_Y_AXIS;
    91 	    break;
    92 	case JOY_DOWN:
    93 	    dev->condition[1] |= JOY_Y_AXIS;
    94 	    break;
    95 	case BUTTON_LEFT_TRIGGER:
    96 	case BUTTON_RIGHT_TRIGGER:
    97 	    dev->condition[0] |= value;
    98 	    break;
    99 	default:
   100 	    dev->condition[0] &= ~value;
   101 	}
   102     } else {
   103 	switch(value ) {
   104 	case JOY_LEFT:
   105 	case JOY_RIGHT:
   106 	    dev->condition[1] = (dev->condition[1] & ~JOY_X_AXIS)| JOY_X_AXIS_CENTER;
   107 	    break;
   108 	case JOY_UP:
   109 	case JOY_DOWN:
   110 	    dev->condition[1] = (dev->condition[1] & ~JOY_Y_AXIS)| JOY_Y_AXIS_CENTER;
   111 	    break;
   112 	case BUTTON_LEFT_TRIGGER:
   113 	case BUTTON_RIGHT_TRIGGER:
   114 	    dev->condition[0] &= ~value;
   115 	    break;
   116 	default:
   117 	    dev->condition[0] |= value;
   118 	}
   119     }
   120 }
   122 lxdream_config_entry_t controller_get_config( maple_device_t mdev )
   123 {
   124     controller_device_t dev = (controller_device_t)mdev;
   125     return dev->config;
   126 }
   128 void controller_destroy( maple_device_t mdev )
   129 {
   130     free( mdev );
   131 }
   133 /**
   134  * Device is being attached to the bus. Go through the config and reserve the
   135  * keys we need.
   136  */
   137 void controller_attach( maple_device_t mdev )
   138 {
   139     controller_device_t dev = (controller_device_t)mdev;
   140     input_register_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
   141     input_register_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
   142     input_register_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
   143     input_register_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
   144     input_register_key( dev->config[4].value, controller_key_callback, dev, JOY_LEFT );
   145     input_register_key( dev->config[5].value, controller_key_callback, dev, JOY_RIGHT );
   146     input_register_key( dev->config[6].value, controller_key_callback, dev, JOY_UP );
   147     input_register_key( dev->config[7].value, controller_key_callback, dev, JOY_DOWN );
   148     input_register_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
   149     input_register_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
   150     input_register_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
   151     input_register_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
   152     input_register_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
   153     input_register_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
   154     input_register_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
   155 }
   157 void controller_detach( maple_device_t mdev )
   158 {
   159     controller_device_t dev = (controller_device_t)mdev;
   160     input_unregister_key( dev->config[0].value, controller_key_callback, dev, BUTTON_DPAD_LEFT );
   161     input_unregister_key( dev->config[1].value, controller_key_callback, dev, BUTTON_DPAD_RIGHT );
   162     input_unregister_key( dev->config[2].value, controller_key_callback, dev, BUTTON_DPAD_UP );
   163     input_unregister_key( dev->config[3].value, controller_key_callback, dev, BUTTON_DPAD_DOWN );
   164     input_unregister_key( dev->config[4].value, controller_key_callback, dev, JOY_LEFT );
   165     input_unregister_key( dev->config[5].value, controller_key_callback, dev, JOY_RIGHT );
   166     input_unregister_key( dev->config[6].value, controller_key_callback, dev, JOY_UP );
   167     input_unregister_key( dev->config[7].value, controller_key_callback, dev, JOY_DOWN );
   168     input_unregister_key( dev->config[8].value, controller_key_callback, dev, BUTTON_X );
   169     input_unregister_key( dev->config[9].value, controller_key_callback, dev, BUTTON_Y );
   170     input_unregister_key( dev->config[10].value, controller_key_callback, dev, BUTTON_A );
   171     input_unregister_key( dev->config[11].value, controller_key_callback, dev, BUTTON_B );
   172     input_unregister_key( dev->config[12].value, controller_key_callback, dev, BUTTON_LEFT_TRIGGER );
   173     input_unregister_key( dev->config[13].value, controller_key_callback, dev, BUTTON_RIGHT_TRIGGER );
   174     input_unregister_key( dev->config[14].value, controller_key_callback, dev, BUTTON_START );
   176 }
   179 int controller_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
   180                          int *outlen )
   181 {
   182     controller_device_t dev = (controller_device_t)mdev;
   183     if( function == MAPLE_FUNC_CONTROLLER ) {
   184         *outlen = 2;
   185         memcpy( outbuf, dev->condition, 8 );
   186         return 0;
   187     } else {
   188         return MAPLE_ERR_FUNC_UNSUP;
   189     }
   190 }
.