/
Input.cpp
242 lines (209 loc) · 5.78 KB
/
Input.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
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
/*
priiloader - A tool which allows to change the default boot up sequence on the Wii console
Copyright (C) 2008-2019 DacoTaco
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 version 2.
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.
*/
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <wiikeyboard/usbkeyboard.h>
#include <sys/time.h>
#include <sys/unistd.h>
#include "Input.h"
#include "gecko.h"
s8 _input_init = 0;
u32 _STM_input_pressed = 0;
u32 _kbd_pressed = 0;
struct timeval _last_press;
struct timeval _time_init;
static lwp_t kbd_handle = LWP_THREAD_NULL;
static volatile bool kbd_should_quit = false;
void KBEventHandler(USBKeyboard_event event)
{
//gprintf("KB event %d", event.type);
if(event.type != USBKEYBOARD_PRESSED && event.type != USBKEYBOARD_RELEASED)
return;
//gprintf("keyCode 0x%X", event.keyCode);
u32 button = 0;
switch (event.keyCode) {
case 0x52: //Up
button = INPUT_BUTTON_UP;
break;
case 0x51: //Down
button = INPUT_BUTTON_DOWN;
break;
case 0x50: //Left
button = INPUT_BUTTON_LEFT;
break;
case 0x4F: //Right
button = INPUT_BUTTON_RIGHT;
break;
case 0x28: //Enter
button = INPUT_BUTTON_A;
break;
case 0x29: //Esc
button = INPUT_BUTTON_B;
break;
case 0x1B: //X
button = INPUT_BUTTON_X;
break;
case 0x1C: //Y
button = INPUT_BUTTON_Y;
break;
case 0x2C: //Space
button = INPUT_BUTTON_START;
break;
default:
break;
}
if(event.type == USBKEYBOARD_PRESSED)
_kbd_pressed |= button;
else
_kbd_pressed &= ~button;
}
void *kbd_thread (void *arg)
{
while (!kbd_should_quit)
{
if(!USBKeyboard_IsConnected() && USBKeyboard_Open(KBEventHandler))
{
//wake up the keyboard by sending it a command.
//im looking at you, you bastard LINQ keyboard.
USBKeyboard_SetLed(USBKEYBOARD_LEDCAPS, false);
}
USBKeyboard_Scan();
usleep(400);
}
return NULL;
}
//the STM event is triggered when the buttons are pressed, or released.
void HandleSTMEvent(u32 event)
{
struct timeval press;
gettimeofday(&press, NULL);
//after the 1 second of being init, we wont accept input
if(_time_init.tv_sec+1 >= press.tv_sec)
return;
//only accept input every +/- 200ms
//this stops it from detecting one press as multiple fires
if(press.tv_sec == _last_press.tv_sec && (press.tv_usec - _last_press.tv_usec < 200000))
return;
gettimeofday(&_last_press, NULL);
switch(event)
{
case STM_EVENT_POWER:
_STM_input_pressed = INPUT_BUTTON_DOWN;
break;
case STM_EVENT_RESET:
//only send press when the button is pressed, not unpressed
_STM_input_pressed = (RESET_UNPRESSED == 0)?INPUT_BUTTON_A:0;
break;
default:
_STM_input_pressed = 0;
break;
}
return;
}
s8 Input_Init( void )
{
if(_input_init)
return 1;
s8 r = PAD_Init();
r |= WPAD_Init();
gettimeofday(&_time_init, NULL);
STM_RegisterEventHandler(HandleSTMEvent);
r |= USB_Initialize();
r |= USBKeyboard_Initialize();
// Even if the thread fails to start,
kbd_should_quit = false;
LWP_CreateThread( &kbd_handle,
kbd_thread,
NULL,
NULL,
16*1024,
50);
_input_init = 1;
return r;
}
void Input_Shutdown( void )
{
if (!_input_init)
return;
for (int i = 0;i < WPAD_MAX_WIIMOTES ;i++)
{
if(WPAD_Probe(i,0) < 0)
continue;
WPAD_Flush(i);
WPAD_Disconnect(i);
}
WPAD_Shutdown();
//signal thread to exit, if it would hang on a USBRead the close will cancel it.
kbd_should_quit = true;
USBKeyboard_Close();
USBKeyboard_Deinitialize();
if (kbd_handle != LWP_THREAD_NULL) {
LWP_JoinThread(kbd_handle, NULL);
kbd_handle = LWP_THREAD_NULL;
}
_input_init = 0;
}
u32 Input_ScanPads( void )
{
WPAD_ScanPads();
PAD_ScanPads();
usleep(800); // Give the keyboard thread a chance to run
return 1;
}
u32 Input_ButtonsDown( bool _overrideSTM )
{
u32 pressed = 0;
if(_STM_input_pressed != 0)
{
if(_overrideSTM)
pressed = INPUT_BUTTON_B;
else
pressed = _STM_input_pressed | INPUT_BUTTON_STM;
_STM_input_pressed = 0;
}
else if (_kbd_pressed != 0)
{
pressed |= _kbd_pressed;
_kbd_pressed = 0;
}
else
{
pressed |= (PAD_ButtonsDown(0) | PAD_ButtonsDown(1) | PAD_ButtonsDown(2) | PAD_ButtonsDown(3));
u32 WPAD_Pressed = (WPAD_ButtonsDown(0) | WPAD_ButtonsDown(1) | WPAD_ButtonsDown(2) | WPAD_ButtonsDown(3));
if( WPAD_Pressed & WPAD_BUTTON_DOWN || WPAD_Pressed & WPAD_CLASSIC_BUTTON_DOWN )
pressed |= INPUT_BUTTON_DOWN;
if( WPAD_Pressed & WPAD_BUTTON_UP || WPAD_Pressed & WPAD_CLASSIC_BUTTON_UP )
pressed |= INPUT_BUTTON_UP;
if( WPAD_Pressed & WPAD_BUTTON_LEFT || WPAD_Pressed & WPAD_CLASSIC_BUTTON_LEFT )
pressed |= INPUT_BUTTON_LEFT;
if( WPAD_Pressed & WPAD_BUTTON_RIGHT || WPAD_Pressed & WPAD_CLASSIC_BUTTON_RIGHT )
pressed |= INPUT_BUTTON_RIGHT;
if(WPAD_Pressed & WPAD_BUTTON_A || WPAD_Pressed & WPAD_CLASSIC_BUTTON_A)
pressed |= INPUT_BUTTON_A;
if(WPAD_Pressed & WPAD_BUTTON_B || WPAD_Pressed & WPAD_CLASSIC_BUTTON_B)
pressed |= INPUT_BUTTON_B;
if(WPAD_Pressed & WPAD_BUTTON_PLUS || WPAD_Pressed & WPAD_CLASSIC_BUTTON_PLUS)
pressed |= INPUT_BUTTON_START;
if(WPAD_Pressed & WPAD_BUTTON_1 || WPAD_Pressed & WPAD_CLASSIC_BUTTON_Y)
pressed |= INPUT_BUTTON_Y;
if(WPAD_Pressed & WPAD_BUTTON_2 || WPAD_Pressed & WPAD_CLASSIC_BUTTON_X)
pressed |= INPUT_BUTTON_X;
}
return pressed;
}
u32 Input_ButtonsDown( void )
{
return Input_ButtonsDown(false);
}