forked from badman12345/osmose-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoystickInputDevice.cpp
165 lines (150 loc) · 4.36 KB
/
JoystickInputDevice.cpp
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
/*****************************************************************************
*
* File: JoystickInputDevice.cpp
*
* Project: Osmose emulator.
*
* Description: This class implements input device Pad.
*
* Author: Vedder Bruno
* Date: 14/11/2006, 15h50
*
* URL: http://bcz.emu-france.com/
*****************************************************************************/
#include "JoystickInputDevice.h"
extern EmulatorOptions emu_opt;
/*--------------------------------------------------------------------*/
/* Class constructor. */
/*--------------------------------------------------------------------*/
JoystickInputDevice::JoystickInputDevice(IOMapper *iomapper, OsmoseConfiguration *o)
{
int joy_num = 0;
iom = iomapper;
oc = o;
joy_num = SDL_NumJoysticks();
if (joy_num == 0)
{
cerr << "No Joystick found. Try to connect or configure it and then restart Osmose." << endl;
exit(-1);
}
cout << "Joystick: " << SDL_JoystickName(0) <<endl;
SDL_JoystickEventState(SDL_ENABLE);
j = SDL_JoystickOpen(0);
}
/*---------------------------------------------------------------------*/
/* Keyboard events are used to emulate DEFAULT PAD */
//*--------------------------------------------------------------------*/
void JoystickInputDevice::handleDeviceChange(SDL_Event &e)
{
int type = e.type;
/* Handle Joystick Motion */
if (type == SDL_JOYAXISMOTION)
{
if ( e.jaxis.axis == 0)
{
/* Left/Right Axis is in central position. */
if (e.jaxis.value > -3200 && e.jaxis.value < 3200)
{
iom->portPAD1 |= BIT3;
iom->portPAD1 |= BIT2;
}
if (e.jaxis.value < -3200)
{
/* Left movement avoid left and right pad flag to be set together :) */
iom->portPAD1 &= BIT2_MASK;
iom->portPAD1 |= BIT3;
return;
}
if (e.jaxis.value > 3200)
{
/* Right movement avoid left and right pad flag to be set together :) */
iom->portPAD1 &= BIT3_MASK;
iom->portPAD1 |= BIT2;
return;
}
return;
}
/* Up/Down Axis is in central position. */
if( e.jaxis.axis == 1)
{
if (e.jaxis.value > -3200 && e.jaxis.value < 3200)
{
iom->portPAD1 |= BIT0;
iom->portPAD1 |= BIT1;
}
if (e.jaxis.value < -3200)
{
/* Down movement avoid up and down pad flag to be set together :) */
iom->portPAD1 &= BIT0_MASK;
iom->portPAD1 |= BIT1;
return;
}
if (e.jaxis.value > 3200)
{
/* Down movement avoid up and down pad flag to be set together :) */
iom->portPAD1 &= BIT1_MASK;
iom->portPAD1 |= BIT0;
return;
}
return;
}
}
/* Handle Buttons events.*/
if (type == SDL_JOYBUTTONDOWN)
{
if ( (int)e.jbutton.button == emu_opt.joy1 )
{
iom->portPAD1 &= BIT4_MASK;
return;
}
if ( (int)e.jbutton.button == emu_opt.joy2 )
{
iom->portPAD1 &= BIT5_MASK;
return;
}
if ( (int)e.jbutton.button == emu_opt.joystart )
{
iom->port0x0 &= BIT7_MASK;
return;
}
return;
}
if (type == SDL_JOYBUTTONUP)
{
if ( (int)e.jbutton.button == emu_opt.joy1 )
{
iom->portPAD1 |= BIT4;
return;
}
if ( (int)e.jbutton.button == emu_opt.joy2 )
{
iom->portPAD1 |= BIT5;
return;
}
if ( (int)e.jbutton.button == emu_opt.joystart )
{
iom->port0x0 |= BIT7;
return;
}
return;
}
}
/*----------------------------------------------------------------------*/
/* Unused in this device. Use when emulating analogic thing on keyboard.*/
/*----------------------------------------------------------------------*/
void JoystickInputDevice::updateDevice()
{
}
/*----------------------------------------------------------------------*/
/* Reset the device. */
/*----------------------------------------------------------------------*/
void JoystickInputDevice::reset()
{
}
/*----------------------------------------------------------------------*/
/* Return ID string. */
/*----------------------------------------------------------------------*/
string JoystickInputDevice::getInputDeviceName()
{
return string("Joystick");
}