-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.c
311 lines (256 loc) · 8.92 KB
/
input.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* input.c: generalised input events layer for Fuse
Copyright (c) 2004 Philip Kendall
$Id: input.c 3751 2008-08-19 15:46:09Z specu $
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
*/
#include <config.h>
#include "fuse.h"
#include "input.h"
#include "joystick.h"
#include "keyboard.h"
#include "menu.h"
#include "settings.h"
#include "snapshot.h"
#include "tape.h"
#include "ui/ui.h"
#include "utils.h"
#ifdef USE_WIDGET
#include "ui/widget/widget.h"
#endif /* #ifdef USE_WIDGET */
static int keypress( const input_event_key_t *event );
static int keyrelease( const input_event_key_t *event );
static int do_joystick( const input_event_joystick_t *joystick_event,
int press );
int
input_event( const input_event_t *event )
{
switch( event->type ) {
case INPUT_EVENT_KEYPRESS: return keypress( &( event->types.key ) );
case INPUT_EVENT_KEYRELEASE: return keyrelease( &( event->types.key ) );
case INPUT_EVENT_JOYSTICK_PRESS:
return do_joystick( &( event->types.joystick ), 1 );
case INPUT_EVENT_JOYSTICK_RELEASE:
return do_joystick( &( event->types.joystick ), 0 );
}
ui_error( UI_ERROR_ERROR, "unknown input event type %d", event->type );
return 1;
}
static int
keypress( const input_event_key_t *event )
{
int swallow;
const keyboard_spectrum_keys_t *ptr;
#ifdef USE_WIDGET
if( widget_level >= 0 ) {
widget_keyhandler( event->native_key );
return 0;
}
#endif /* #ifdef USE_WIDGET */
/* Escape => ask UI to end mouse grab, return if grab ended */
if( event->native_key == INPUT_KEY_Escape && ui_mouse_grabbed ) {
ui_mouse_grabbed = ui_mouse_release( 0 );
if( !ui_mouse_grabbed ) return 0;
}
swallow = 0;
/* Joystick emulation via keyboard keys */
if ( event->spectrum_key == settings_current.joystick_keyboard_up ) {
swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_UP , 1 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_down ) {
swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_DOWN , 1 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_left ) {
swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_LEFT , 1 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_right ) {
swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_RIGHT, 1 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_fire ) {
swallow = joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_FIRE , 1 );
}
if( swallow ) return 0;
ptr = keyboard_get_spectrum_keys( event->spectrum_key );
if( ptr ) {
keyboard_press( ptr->key1 );
keyboard_press( ptr->key2 );
}
#ifdef USE_WIDGET
switch( event->native_key ) {
case INPUT_KEY_F1:
fuse_emulation_pause();
widget_do( WIDGET_TYPE_MENU, &widget_menu );
fuse_emulation_unpause();
break;
case INPUT_KEY_F2:
fuse_emulation_pause();
menu_file_savesnapshot( 0 );
fuse_emulation_unpause();
break;
case INPUT_KEY_F3:
fuse_emulation_pause();
menu_file_open( 0 );
fuse_emulation_unpause();
break;
case INPUT_KEY_F4:
fuse_emulation_pause();
menu_options_general( 0 );
fuse_emulation_unpause();
break;
case INPUT_KEY_F5:
fuse_emulation_pause();
menu_machine_reset( 0 );
fuse_emulation_unpause();
break;
case INPUT_KEY_F6:
fuse_emulation_pause();
menu_media_tape_write( 0 );
fuse_emulation_unpause();
break;
case INPUT_KEY_F7:
fuse_emulation_pause();
menu_media_tape_open( 0 );
fuse_emulation_unpause();
break;
case INPUT_KEY_F8:
menu_media_tape_play( 0 );
break;
case INPUT_KEY_F9:
fuse_emulation_pause();
menu_machine_select( 0 );
fuse_emulation_unpause();
break;
case INPUT_KEY_F10:
fuse_emulation_pause();
menu_file_exit( 0 );
fuse_emulation_unpause();
break;
default: break; /* Remove gcc warning */
}
#endif /* #ifdef USE_WIDGET */
return 0;
}
static int
keyrelease( const input_event_key_t *event )
{
const keyboard_spectrum_keys_t *ptr;
ptr = keyboard_get_spectrum_keys( event->spectrum_key );
if( ptr ) {
keyboard_release( ptr->key1 );
keyboard_release( ptr->key2 );
}
/* Joystick emulation via keyboard keys */
if( event->spectrum_key == settings_current.joystick_keyboard_up ) {
joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_UP , 0 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_down ) {
joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_DOWN , 0 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_left ) {
joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_LEFT , 0 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_right ) {
joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_RIGHT, 0 );
}
else if( event->spectrum_key == settings_current.joystick_keyboard_fire ) {
joystick_press( JOYSTICK_KEYBOARD, JOYSTICK_BUTTON_FIRE , 0 );
}
return 0;
}
static keyboard_key_name
get_fire_button_key( int which, input_key button )
{
switch( which ) {
case 0:
switch( button ) {
case INPUT_JOYSTICK_FIRE_1 : return settings_current.joystick_1_fire_1;
case INPUT_JOYSTICK_FIRE_2 : return settings_current.joystick_1_fire_2;
case INPUT_JOYSTICK_FIRE_3 : return settings_current.joystick_1_fire_3;
case INPUT_JOYSTICK_FIRE_4 : return settings_current.joystick_1_fire_4;
case INPUT_JOYSTICK_FIRE_5 : return settings_current.joystick_1_fire_5;
case INPUT_JOYSTICK_FIRE_6 : return settings_current.joystick_1_fire_6;
case INPUT_JOYSTICK_FIRE_7 : return settings_current.joystick_1_fire_7;
case INPUT_JOYSTICK_FIRE_8 : return settings_current.joystick_1_fire_8;
case INPUT_JOYSTICK_FIRE_9 : return settings_current.joystick_1_fire_9;
case INPUT_JOYSTICK_FIRE_10: return settings_current.joystick_1_fire_10;
default: break;
}
break;
case 1:
switch( button ) {
case INPUT_JOYSTICK_FIRE_1 : return settings_current.joystick_2_fire_1;
case INPUT_JOYSTICK_FIRE_2 : return settings_current.joystick_2_fire_2;
case INPUT_JOYSTICK_FIRE_3 : return settings_current.joystick_2_fire_3;
case INPUT_JOYSTICK_FIRE_4 : return settings_current.joystick_2_fire_4;
case INPUT_JOYSTICK_FIRE_5 : return settings_current.joystick_2_fire_5;
case INPUT_JOYSTICK_FIRE_6 : return settings_current.joystick_2_fire_6;
case INPUT_JOYSTICK_FIRE_7 : return settings_current.joystick_2_fire_7;
case INPUT_JOYSTICK_FIRE_8 : return settings_current.joystick_2_fire_8;
case INPUT_JOYSTICK_FIRE_9 : return settings_current.joystick_2_fire_9;
case INPUT_JOYSTICK_FIRE_10: return settings_current.joystick_2_fire_10;
default: break;
}
break;
}
ui_error( UI_ERROR_ERROR, "get_fire_button_key: which = %d, button = %d",
which, button );
fuse_abort();
}
static int
do_joystick( const input_event_joystick_t *joystick_event, int press )
{
int which;
#ifdef USE_WIDGET
if( widget_level >= 0 ) {
if( press ) widget_keyhandler( joystick_event->button );
return 0;
}
switch( joystick_event->button ) {
case INPUT_JOYSTICK_FIRE_2:
fuse_emulation_pause();
widget_do( WIDGET_TYPE_MENU, &widget_menu );
fuse_emulation_unpause();
break;
default: break; /* Remove gcc warning */
}
#endif /* #ifdef USE_WIDGET */
which = joystick_event->which;
if( joystick_event->button < INPUT_JOYSTICK_FIRE_1 ) {
joystick_button button;
switch( joystick_event->button ) {
case INPUT_JOYSTICK_UP : button = JOYSTICK_BUTTON_UP; break;
case INPUT_JOYSTICK_DOWN : button = JOYSTICK_BUTTON_DOWN; break;
case INPUT_JOYSTICK_LEFT : button = JOYSTICK_BUTTON_LEFT; break;
case INPUT_JOYSTICK_RIGHT: button = JOYSTICK_BUTTON_RIGHT; break;
default:
ui_error( UI_ERROR_ERROR, "do_joystick: unknown button %d",
joystick_event->button );
fuse_abort();
}
joystick_press( which, button, press );
} else {
keyboard_key_name key;
key = get_fire_button_key( which, joystick_event->button );
if( key == KEYBOARD_JOYSTICK_FIRE ) {
joystick_press( which, JOYSTICK_BUTTON_FIRE, press );
} else {
if( press ) {
keyboard_press( key );
} else {
keyboard_release( key );
}
}
}
return 0;
}