4 * Implementation of the SEGA lightgun device
7 * Copyright (c) 2008 Nathan Keynes.
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.
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.
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 void lightgun_key_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
61 static lxdream_config_group_t lightgun_get_config( maple_device_t dev );
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];
72 struct lxdream_config_group config;
75 struct maple_device_class lightgun_class = { "Sega Lightgun",
76 MAPLE_TYPE_PRIMARY|MAPLE_GRAB_NO|MAPLE_SLOTS_2, lightgun_new };
78 static struct lightgun_device base_lightgun = {
79 { MAPLE_DEVICE_TAG, &lightgun_class,
80 LIGHTGUN_IDENT, LIGHTGUN_VERSION,
82 lightgun_attach, lightgun_detach, lightgun_destroy,
83 lightgun_clone, NULL, NULL, lightgun_get_cond, NULL, NULL, NULL, NULL,
84 lightgun_start_gun, lightgun_stop_gun},
85 {0x0000FFFF, 0x80808080}, 0, -1, -1,
86 {"Sega Lightgun", NULL, lightgun_key_callback, NULL,
87 {{ "dpad left", N_("Dpad left"), CONFIG_TYPE_KEY, NULL, BUTTON_DPAD_LEFT },
88 { "dpad right", N_("Dpad right"), CONFIG_TYPE_KEY, NULL, BUTTON_DPAD_RIGHT },
89 { "dpad up", N_("Dpad up"), CONFIG_TYPE_KEY, NULL, BUTTON_DPAD_UP },
90 { "dpad down", N_("Dpad down"), CONFIG_TYPE_KEY, NULL, BUTTON_DPAD_DOWN },
91 { "button A", N_("Button A"), CONFIG_TYPE_KEY, NULL, BUTTON_A },
92 { "button B", N_("Button B"), CONFIG_TYPE_KEY, NULL, BUTTON_B },
93 { "start", N_("Start button"), CONFIG_TYPE_KEY, NULL, BUTTON_START },
94 { NULL, CONFIG_TYPE_NONE }}} };
97 #define lightgun(x) ((lightgun_device_t)(x))
99 static maple_device_t lightgun_new( )
101 lightgun_device_t dev = malloc( sizeof(struct lightgun_device) );
102 memcpy( dev, &base_lightgun, sizeof(base_lightgun) );
103 dev->config.data = dev;
104 return MAPLE_DEVICE(dev);
107 static maple_device_t lightgun_clone( maple_device_t srcdevice )
109 lightgun_device_t src = (lightgun_device_t)srcdevice;
110 lightgun_device_t dev = (lightgun_device_t)lightgun_new();
111 lxdream_copy_config_group( &dev->config, &src->config );
112 memcpy( dev->condition, src->condition, sizeof(src->condition) );
113 return MAPLE_DEVICE(dev);
116 static lxdream_config_group_t lightgun_get_config( maple_device_t mdev )
118 lightgun_device_t dev = (lightgun_device_t)mdev;
122 static void lightgun_destroy( maple_device_t mdev )
130 static void lightgun_key_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
132 lightgun_device_t dev = (lightgun_device_t)mdev;
134 dev->condition[0] &= ~value;
136 dev->condition[0] |= value;
140 static void lightgun_mouse_callback( void *mdev, uint32_t buttons, int32_t x, int32_t y, gboolean absolute )
142 lightgun_device_t dev = (lightgun_device_t)mdev;
146 if( dev->gun_active ) {
147 pvr2_queue_gun_event( x, y );
148 dev->gun_active = FALSE;
154 * Device is being attached to the bus. Go through the config and reserve the
157 static void lightgun_attach( maple_device_t mdev )
159 lightgun_device_t dev = (lightgun_device_t)mdev;
160 dev->config.on_change = input_keygroup_changed;
161 input_register_keygroup( &dev->config );
162 input_register_mouse_hook( TRUE, lightgun_mouse_callback, dev );
165 static void lightgun_detach( maple_device_t mdev )
167 lightgun_device_t dev = (lightgun_device_t)mdev;
168 input_unregister_keygroup( &dev->config );
169 dev->config.on_change = NULL;
170 input_unregister_mouse_hook( lightgun_mouse_callback, dev );
175 static int lightgun_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
176 unsigned int *outlen )
178 lightgun_device_t dev = (lightgun_device_t)mdev;
179 if( function == MAPLE_FUNC_CONTROLLER ) {
181 memcpy( outbuf, dev->condition, 8 );
184 return MAPLE_ERR_FUNC_UNSUP;
188 static void lightgun_start_gun( maple_device_t mdev )
190 lightgun_device_t dev = (lightgun_device_t)mdev;
191 if( dev->mouse_x != -1 && dev->mouse_y != -1 ) {
192 pvr2_queue_gun_event( dev->mouse_x, dev->mouse_y );
194 // Wait for a mouse event
199 static void lightgun_stop_gun( maple_device_t mdev )
201 lightgun_device_t dev = (lightgun_device_t)mdev;
203 event_cancel( EVENT_GUNPOS );
.