1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/* uijoystick.c: Joystick emulation (using libjsw)
Copyright (c) 2003-2004 Darren Salt, Philip Kendall
$Id: uijoystick.c 4915 2013-04-07 05:32:09Z fredm $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
Darren: linux@youmustbejoking.demon.co.uk
*/
/* Usage note: build this from within a specific UI unless that UI implements
* its own joystick support using some other library.
* Inclusion as follows:
* #if !defined USE_JOYSTICK || defined HAVE_JSW_H
* # include "../uijoystick.c"
* #else
* // UI-specific code implementing the following (exported) functions
* #endif
*/
#include <config.h>
#include "input.h"
#include "uijoystick.h"
#if defined USE_JOYSTICK && defined HAVE_JSW_H
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <jsw.h>
#include <libspectrum.h>
#include "fuse.h"
#include "keyboard.h"
#include "settings.h"
#include "spectrum.h"
#include "machine.h"
#include "ui/ui.h"
#include "utils.h"
static js_data_struct jsd[2];
static int js_button_states[2][10];
static void poll_joystick( int which );
static void do_axis( int which, double position, input_key negative,
input_key positive );
static int
init_stick( int which, const char *const device,
const char *const calibration )
{
switch( JSInit( &jsd[which], device, calibration, JSFlagNonBlocking ) ) {
case JSSuccess:
if( JSLoadCalibrationUNIX( &jsd[which] ) && errno != ENOENT ) {
ui_error( UI_ERROR_ERROR,
"failed to read calibration for joystick %i: %s", which + 1,
strerror( errno ) );
break;
}
if( jsd[which].total_axises < 2 || jsd[which].total_buttons < 1 )
{
ui_error( UI_ERROR_ERROR, "sorry, joystick %i (%s) is inadequate!",
which + 1, device );
break;
}
return 0;
case JSBadValue:
ui_error( UI_ERROR_ERROR, "failed to initialise joystick %i: %s",
which + 1, "invalid parameter/value");
break;
case JSNoAccess:
/* FIXME: why is this commented out? */
/*
ui_error (UI_ERROR_ERROR,
"failed to initialise joystick %i: %s",
which + 1, "cannot access device");
*/
break;
case JSNoBuffers:
ui_error( UI_ERROR_ERROR, "failed to initialise joystick %i: %s",
which + 1, "not enough memory" );
break;
default:
ui_error( UI_ERROR_ERROR, "failed to initialise joystick %i", which + 1 );
break;
}
JSClose( &jsd[which] );
return 1;
}
static
int open_joystick( int which, const char *device, const char *calibration )
{
char path[ PATH_MAX ];
/* If we were given an explicit device to use for this joystick, try
only that */
if( device && device[0] ) return init_stick( which, device, calibration );
/* Otherwise try /dev/input/js<n> and /dev/js<n> */
snprintf( path, PATH_MAX, "/dev/input/js%d", which );
if( !init_stick( which, path, calibration ) ) return 0;
snprintf( path, PATH_MAX, "/dev/js%d", which );
if( !init_stick( which, path, calibration ) ) return 0;
/* Couldn't find this joystick */
return 1;
}
int
ui_joystick_init( void )
{
const char *home;
char *calibration;
int error;
size_t i, j;
home = compat_get_home_path(); if( !home ) return 1;
/* Default calibration file is ~/.joystick */
calibration = malloc( strlen( home ) + strlen( JSDefaultCalibration ) + 2 );
if( !calibration ) {
ui_error( UI_ERROR_ERROR, "failed to initialise joystick: %s",
"not enough memory" );
return 0;
}
sprintf( calibration, "%s/%s", home, JSDefaultCalibration );
for( i = 0; i<2; i++ ) {
for( j = 0; j<10; j++ ) {
js_button_states[i][j] = 0;
}
}
/* If we can't init the first, don't try the second */
error = open_joystick( 0, settings_current.joystick_1, calibration );
if( error ) return 0;
error = open_joystick( 1, settings_current.joystick_2, calibration );
if( error ) return 1;
return 2;
}
void
ui_joystick_end( void )
{
int i;
for( i = 0; i < joysticks_supported; i++ ) JSClose( &jsd[i] );
}
void
ui_joystick_poll( void )
{
int i;
for( i = 0; i < joysticks_supported; i++ ) poll_joystick( i );
}
static void
poll_joystick( int which )
{
js_data_struct *joystick;
double position;
int fire, buttons;
input_event_t event;
size_t i;
joystick = &jsd[which];
if( JSUpdate( joystick ) != JSGotEvent ) return;
position = JSGetAxisCoeffNZ( joystick, 0 );
do_axis( which, position, INPUT_JOYSTICK_LEFT, INPUT_JOYSTICK_RIGHT );
position = JSGetAxisCoeffNZ( joystick, 1 );
do_axis( which, position, INPUT_JOYSTICK_UP, INPUT_JOYSTICK_DOWN );
event.types.joystick.which = which;
buttons = joystick->total_buttons;
if( buttons > 15 ) buttons = 15; /* We support 'only' 15 fire buttons */
for( i = 0; i < buttons; i++ ) {
fire = JSGetButtonState( joystick, i );
if( fire == JSButtonStateOn ) {
event.type = INPUT_EVENT_JOYSTICK_PRESS;
} else {
event.type = INPUT_EVENT_JOYSTICK_RELEASE;
}
event.types.joystick.button = INPUT_JOYSTICK_FIRE_1 + i;
if( js_button_states[which][i] != fire ) {
js_button_states[which][i] = fire;
input_event( &event );
}
}
}
static void
do_axis( int which, double position, input_key negative, input_key positive )
{
input_event_t event1, event2;
event1.types.joystick.which = event2.types.joystick.which = which;
event1.types.joystick.button = positive;
event2.types.joystick.button = negative;
if( position == 0.0 ) {
event1.type = INPUT_EVENT_JOYSTICK_RELEASE;
event2.type = INPUT_EVENT_JOYSTICK_RELEASE;
} else if( position > 0.0 ) {
event1.type = INPUT_EVENT_JOYSTICK_PRESS;
event2.type = INPUT_EVENT_JOYSTICK_RELEASE;
} else {
event1.type = INPUT_EVENT_JOYSTICK_RELEASE;
event2.type = INPUT_EVENT_JOYSTICK_PRESS;
}
input_event( &event1 );
input_event( &event2 );
}
#else /* #if defined USE_JOYSTICK && defined HAVE_JSW_H */
/* No joystick library */
int
ui_joystick_init( void )
{
return 0;
}
void
ui_joystick_end( void )
{
}
void
ui_joystick_poll( void )
{
}
#endif /* #if defined USE_JOYSTICK && defined HAVE_JSW_H */
|