Search
lxdream.org :: lxdream/src/maple/lightgun.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/lightgun.c
changeset 850:28782ebbd01d
next1021:848db285a184
author nkeynes
date Tue Oct 14 08:44:37 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix a few more subtle flag problems
view annotate diff log raw
     1 /**
     2  * $Id: lightgun.c 858 2008-09-02 03:34:00Z nkeynes $
     3  *
     4  * Implementation of the SEGA lightgun device
     5  * Part No. HKT-7800
     6  *
     7  * Copyright (c) 2008 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <stdlib.h>
    21 #include <stdio.h>
    22 #include <string.h>
    23 #include <assert.h>
    24 #include "display.h"
    25 #include "eventq.h"
    26 #include "pvr2/pvr2.h"
    27 #include "maple/maple.h"
    29 #define BUTTON_B            0x00000002
    30 #define BUTTON_A            0x00000004
    31 #define BUTTON_START        0x00000008
    32 #define BUTTON_DPAD_UP      0x00000010
    33 #define BUTTON_DPAD_DOWN    0x00000020
    34 #define BUTTON_DPAD_LEFT    0x00000040
    35 #define BUTTON_DPAD_RIGHT   0x00000080
    37 #define LIGHTGUN_IDENT {     0x00, 0x00, 0x00, 0x81,  0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0xFE, \
    38 	0x00, 0x00, 0x00, 0x00,  0xFF, 0x01, 0x44, 0x72,  0x65, 0x61, 0x6D, 0x63,  0x61, 0x73, 0x74, 0x20, \
    39 	0x47, 0x75, 0x6E, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20, \
    40 	0x20, 0x20, 0x20, 0x20,  0x50, 0x72, 0x6F, 0x64,  0x75, 0x63, 0x65, 0x64,  0x20, 0x42, 0x79, 0x20, \
    41 	0x6F, 0x72, 0x20, 0x55,  0x6E, 0x64, 0x65, 0x72,  0x20, 0x4C, 0x69, 0x63,  0x65, 0x6E, 0x73, 0x65, \
    42 	0x20, 0x46, 0x72, 0x6F,  0x6D, 0x20, 0x53, 0x45,  0x47, 0x41, 0x20, 0x45,  0x4E, 0x54, 0x45, 0x52, \
    43 	0x50, 0x52, 0x49, 0x53,  0x45, 0x53, 0x2C, 0x4C,  0x54, 0x44, 0x2E, 0x20,  0x20, 0x20, 0x20, 0x20, \
    44 	0xDC, 0x00, 0x2C, 0x01 }
    45 #define LIGHTGUN_VERSION {   0x56, 0x65, 0x72, 0x73,  0x69, 0x6F, 0x6E, 0x20,  0x31, 0x2E, 0x30, 0x30, \
    46 	0x30, 0x2C, 0x31, 0x39,  0x39, 0x38, 0x2F, 0x30,  0x39, 0x2F, 0x31, 0x36,  0x2C, 0x33, 0x31, 0x35, \
    47 	0x2D, 0x36, 0x31, 0x32,  0x35, 0x2D, 0x41, 0x47,  0x20, 0x20, 0x20, 0x2C,  0x55, 0x2C, 0x44, 0x2C, \
    48 	0x4C, 0x2C, 0x52, 0x2C,  0x53, 0x2C, 0x41, 0x2C,  0x42, 0x20, 0x4B, 0x65,  0x79, 0x20, 0x26, 0x20, \
    49 	0x53, 0x63, 0x61, 0x6E,  0x6E, 0x69, 0x6E, 0x67,  0x20, 0x4C, 0x69, 0x6E,  0x65, 0x20, 0x41, 0x6D, \
    50 	0x70, 0x2E, 0x20, 0x20 }                                       
    53 #define LIGHTGUN_CONFIG_ENTRIES 7
    55 static void lightgun_attach( maple_device_t dev );
    56 static void lightgun_detach( maple_device_t dev );
    57 static void lightgun_destroy( maple_device_t dev );
    58 static maple_device_t lightgun_clone( maple_device_t dev );
    59 static maple_device_t lightgun_new();
    60 static lxdream_config_entry_t lightgun_get_config( maple_device_t dev );
    61 static void lightgun_set_config_value( maple_device_t dev, unsigned int key, const gchar *value );
    62 static int lightgun_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
    63                          unsigned int *outlen );
    64 static void lightgun_start_gun( maple_device_t dev );
    65 static void lightgun_stop_gun( maple_device_t dev );
    67 typedef struct lightgun_device {
    68     struct maple_device dev;
    69     uint32_t condition[2];
    70     int gun_active;
    71     int mouse_x, mouse_y;
    72     struct lxdream_config_entry config[LIGHTGUN_CONFIG_ENTRIES+1];
    73 } *lightgun_device_t;
    75 struct maple_device_class lightgun_class = { "Sega Lightgun", lightgun_new };
    77 static struct lightgun_device base_lightgun = {
    78         { MAPLE_DEVICE_TAG, &lightgun_class, MAPLE_GRAB_NO,
    79           LIGHTGUN_IDENT, LIGHTGUN_VERSION, 
    80           lightgun_get_config, lightgun_set_config_value, 
    81           lightgun_attach, lightgun_detach, lightgun_destroy,
    82           lightgun_clone, NULL, NULL, lightgun_get_cond, NULL, NULL, NULL,
    83           lightgun_start_gun, lightgun_stop_gun},
    84           {0x0000FFFF, 0x80808080}, 0, -1, -1, 
    85           {{ "dpad left", N_("Dpad left"), CONFIG_TYPE_KEY },
    86            { "dpad right", N_("Dpad right"), CONFIG_TYPE_KEY },
    87            { "dpad up", N_("Dpad up"), CONFIG_TYPE_KEY },
    88            { "dpad down", N_("Dpad down"), CONFIG_TYPE_KEY },
    89            { "button A", N_("Button A"), CONFIG_TYPE_KEY },
    90            { "button B", N_("Button B"), CONFIG_TYPE_KEY },
    91            { "start", N_("Start button"), CONFIG_TYPE_KEY },
    92            { NULL, CONFIG_TYPE_NONE }} };
    94 static int config_button_map[] = { 
    95         BUTTON_DPAD_LEFT, BUTTON_DPAD_RIGHT, BUTTON_DPAD_UP, BUTTON_DPAD_DOWN,
    96         BUTTON_A, BUTTON_B, BUTTON_START };
    98 #define lightgun(x) ((lightgun_device_t)(x))
   100 static maple_device_t lightgun_new( )
   101 {
   102     lightgun_device_t dev = malloc( sizeof(struct lightgun_device) );
   103     memcpy( dev, &base_lightgun, sizeof(base_lightgun) );
   104     return MAPLE_DEVICE(dev);
   105 }
   107 static maple_device_t lightgun_clone( maple_device_t srcdevice )
   108 {
   109     lightgun_device_t src = (lightgun_device_t)srcdevice;
   110     lightgun_device_t dev = (lightgun_device_t)lightgun_new();
   111     lxdream_copy_config_list( dev->config, src->config );
   112     memcpy( dev->condition, src->condition, sizeof(src->condition) );
   113     return MAPLE_DEVICE(dev);
   114 }
   116 /**
   117  * Input callback 
   118  */
   119 static void lightgun_key_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   120 {
   121     lightgun_device_t dev = (lightgun_device_t)mdev;
   122     if( isKeyDown ) {
   123         dev->condition[0] &= ~value;
   124     } else {
   125         dev->condition[0] |= value;
   126     }
   127 }
   129 static lxdream_config_entry_t lightgun_get_config( maple_device_t mdev )
   130 {
   131     lightgun_device_t dev = (lightgun_device_t)mdev;
   132     return dev->config;
   133 }
   135 static void lightgun_set_config_value( maple_device_t mdev, unsigned int key, const gchar *value )
   136 {
   137     lightgun_device_t dev = (lightgun_device_t)mdev;
   138     assert( key < LIGHTGUN_CONFIG_ENTRIES );
   140     input_unregister_key( dev->config[key].value, lightgun_key_callback, dev, config_button_map[key] );
   141     lxdream_set_config_value( &dev->config[key], value );
   142     input_register_key( dev->config[key].value, lightgun_key_callback, dev, config_button_map[key] );
   143 }
   145 static void lightgun_destroy( maple_device_t mdev )
   146 {
   147     free( mdev );
   148 }
   151 static void lightgun_mouse_callback( void *mdev, uint32_t buttons, int32_t x, int32_t y, gboolean absolute )
   152 {
   153     lightgun_device_t dev = (lightgun_device_t)mdev;
   154     if( absolute ) {
   155         dev->mouse_x = x;
   156         dev->mouse_y = y;
   157         if( dev->gun_active ) {
   158             pvr2_queue_gun_event( x, y );
   159             dev->gun_active = FALSE;
   160         }
   161     }
   162 }
   164 /**
   165  * Device is being attached to the bus. Go through the config and reserve the
   166  * keys we need.
   167  */
   168 static void lightgun_attach( maple_device_t mdev )
   169 {
   170     lightgun_device_t dev = (lightgun_device_t)mdev;
   171     int i;
   172     for( i=0; i<LIGHTGUN_CONFIG_ENTRIES; i++ ) {
   173         input_register_key( dev->config[i].value, lightgun_key_callback, dev, config_button_map[i] );
   174     }
   175     input_register_mouse_hook( TRUE, lightgun_mouse_callback, dev );
   177 }
   179 static void lightgun_detach( maple_device_t mdev )
   180 {
   181     lightgun_device_t dev = (lightgun_device_t)mdev;
   182     int i;
   183     for( i=0; i<LIGHTGUN_CONFIG_ENTRIES; i++ ) {
   184         input_unregister_key( dev->config[i].value, lightgun_key_callback, dev, config_button_map[i] );
   185     }
   186     input_unregister_mouse_hook( lightgun_mouse_callback, dev );
   188 }
   191 static int lightgun_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
   192                          unsigned int *outlen )
   193 {
   194     lightgun_device_t dev = (lightgun_device_t)mdev;
   195     if( function == MAPLE_FUNC_CONTROLLER ) {
   196         *outlen = 2;
   197         memcpy( outbuf, dev->condition, 8 );
   198         return 0;
   199     } else {
   200         return MAPLE_ERR_FUNC_UNSUP;
   201     }
   202 }
   204 static void lightgun_start_gun( maple_device_t mdev )
   205 {
   206     lightgun_device_t dev = (lightgun_device_t)mdev;
   207     if( dev->mouse_x != -1 && dev->mouse_y != -1 ) {
   208         pvr2_queue_gun_event( dev->mouse_x, dev->mouse_y );
   209     } else {
   210         // Wait for a mouse event
   211         dev->gun_active = 1;
   212     }
   213 }
   215 static void lightgun_stop_gun( maple_device_t mdev )
   216 {
   217     lightgun_device_t dev = (lightgun_device_t)mdev;
   218     dev->gun_active = 0;
   219     event_cancel( EVENT_GUNPOS );
   220 }
.