-
Notifications
You must be signed in to change notification settings - Fork 25
/
joystick.c
497 lines (436 loc) · 14.8 KB
/
joystick.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** 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
** of the License.
**
** 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.
**
** Joystick emulation
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "gui.h"
#include "disk.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#include "joystick.h"
#include "plugins/twilighte_board/oric_twilighte_board_plugin.h"
struct keyjoydef
{
char *id;
SDL_COMPAT_KEY sym;
};
static struct keyjoydef keyjoytab[] = { { "BACKSPACE", SDLK_BACKSPACE },
{ "TAB", SDLK_TAB },
{ "CLEAR", SDLK_CLEAR },
{ "RETURN", SDLK_RETURN },
{ "ENTER", SDLK_RETURN },
{ "PAUSE", SDLK_PAUSE },
{ "ESCAPE", SDLK_ESCAPE },
{ "SPACE", SDLK_SPACE },
{ "HELP", SDLK_HELP },
{ "DELETE", SDLK_DELETE },
{ "DEL", SDLK_DELETE },
{ "LSHIFT", SDLK_LSHIFT },
{ "RSHIFT", SDLK_RSHIFT },
{ "RCTRL", SDLK_RCTRL },
{ "LCTRL", SDLK_LCTRL },
{ "RALT", SDLK_RALT },
{ "LALT", SDLK_LALT },
{ "KP0", SDLK_KP0 },
{ "KP1", SDLK_KP1 },
{ "KP2", SDLK_KP2 },
{ "KP3", SDLK_KP3 },
{ "KP4", SDLK_KP4 },
{ "KP5", SDLK_KP5 },
{ "KP6", SDLK_KP6 },
{ "KP7", SDLK_KP7 },
{ "KP8", SDLK_KP8 },
{ "KP9", SDLK_KP9 },
{ "KP_PERIOD", SDLK_KP_PERIOD },
{ "KP_FULLSTOP", SDLK_KP_PERIOD },
{ "KP_DIVIDE", SDLK_KP_DIVIDE },
{ "KP_MULTIPLY", SDLK_KP_MULTIPLY },
{ "KP_MINUS", SDLK_KP_MINUS },
{ "KP_PLUS", SDLK_KP_PLUS },
{ "KP_ENTER", SDLK_KP_ENTER },
{ "KP_EQUALS", SDLK_KP_EQUALS },
{ "UP", SDLK_UP },
{ "DOWN", SDLK_DOWN },
{ "LEFT", SDLK_LEFT },
{ "RIGHT", SDLK_RIGHT },
{ "INSERT", SDLK_INSERT },
{ "HOME", SDLK_HOME },
{ "END", SDLK_END },
{ "PAGEUP", SDLK_PAGEUP },
{ "PAGEDOWN", SDLK_PAGEDOWN },
{ NULL, 0 } };
static SDL_bool joysubinited = SDL_FALSE;
Uint8 joystate_a[7], joystate_b[7];
static SDL_bool is_real_joystick( Sint16 joymode )
{
if( ( joymode >= JOYMODE_SDL0 ) && ( joymode <= JOYMODE_SDL9 ) )
return SDL_TRUE;
return SDL_FALSE;
}
static void close_joysticks( struct machine *oric )
{
if( joysubinited )
{
if( oric->sdljoy_a )
{
SDL_JoystickClose( oric->sdljoy_a );
if( oric->sdljoy_b == oric->sdljoy_a )
oric->sdljoy_b = NULL;
oric->sdljoy_a = NULL;
}
if( oric->sdljoy_b )
{
SDL_JoystickClose( oric->sdljoy_b );
oric->sdljoy_b = NULL;
}
SDL_JoystickEventState( SDL_FALSE );
}
}
// Return an SDL keysym corresponding to the specified name (or 0)
SDL_COMPAT_KEY joy_keyname_to_sym( char *name )
{
Sint32 i;
char c;
// Just a single char?
if( name[1] == 0 )
{
c = tolower( name[0] );
if( ( c >= 32 ) && ( c < 127 ) ) return c;
return 0;
}
// Look for a matching named key
for( i=0; keyjoytab[i].id; i++ )
{
if( strcasecmp( keyjoytab[i].id, name ) == 0 )
{
// dbg_printf("%s => %.8X\n", name, keyjoytab[i].sym);
return keyjoytab[i].sym;
}
}
return 0;
}
static SDL_bool dojoyevent( SDL_Event *ev, struct machine *oric, Sint16 mode, Uint8 *joystate, SDL_Joystick *sjoy )
{
SDL_bool swallowit = SDL_FALSE;
switch( mode )
{
case JOYMODE_NONE:
case JOYMODE_MOUSE: // Telestrat only
return SDL_FALSE;
case JOYMODE_KB1:
case JOYMODE_KB2:
{
SDL_COMPAT_KEY *kbtab = mode == JOYMODE_KB1? oric->kbjoy1 : oric->kbjoy2;
int st = ev->type == SDL_KEYDOWN? 1 : 0;
int i;
for( i=0; i<7; i++ )
{
if( ev->key.keysym.sym == kbtab[i] )
{
// dbg_printf("%d: %.8X <> %.8X => %d\n", i, kbtab[i], ev->key.keysym.sym, st);
joystate[i] = st;
swallowit = SDL_TRUE;
}
}
}
break;
case JOYMODE_SDL0:
case JOYMODE_SDL1:
case JOYMODE_SDL2:
case JOYMODE_SDL3:
case JOYMODE_SDL4:
case JOYMODE_SDL5:
case JOYMODE_SDL6:
case JOYMODE_SDL7:
case JOYMODE_SDL8:
case JOYMODE_SDL9:
if( !sjoy ) return SDL_FALSE;
switch( ev->type )
{
case SDL_JOYAXISMOTION:
if( ev->jaxis.which != (mode-JOYMODE_SDL0) ) return SDL_FALSE;
swallowit = SDL_TRUE;
switch( ev->jaxis.axis )
{
case 0: // left/right
if( ev->jaxis.value < -3200 )
{
joystate[2] = 1; // left
joystate[3] = 0;
} else if( ev->jaxis.value > 3200 ) {
joystate[2] = 0; // right
joystate[3] = 1;
} else {
joystate[2] = 0;
joystate[3] = 0;
}
break;
case 1: // up/down
if( ev->jaxis.value < -3200 )
{
joystate[0] = 1; // up
joystate[1] = 0;
} else if( ev->jaxis.value > 3200 ) {
joystate[0] = 0; // down
joystate[1] = 1;
} else {
joystate[0] = 0;
joystate[1] = 0;
}
break;
}
break;
case SDL_JOYBUTTONDOWN:
if( ev->jbutton.which != (mode-JOYMODE_SDL0) ) return SDL_FALSE;
joystate[4+(ev->jbutton.button % 3)] = 1;
swallowit = SDL_TRUE;
break;
case SDL_JOYBUTTONUP:
if( ev->jbutton.which != (mode-JOYMODE_SDL0) ) return SDL_FALSE;
joystate[4+(ev->jbutton.button % 3)] = 0;
swallowit = SDL_TRUE;
break;
}
break;
}
return swallowit;
}
void joy_buildmask( struct machine *oric )
{
Uint8 mkmask = 0xff;
Uint8 mkmask_f = 0xff;
Uint8 joysel = oric->via.read_port_a( &oric->via );
Uint8 telestrat_joysel = oric->tele_via.read_port_b( &oric->tele_via );
Uint8 twilighteboard_joysel = 0;
SDL_bool gimme_port_a = SDL_FALSE;
SDL_bool gimme_port_b = SDL_FALSE;
if (oric->twilighteboard_activated)
twilighteboard_joysel= twilighteboard_oric_read(oric->twilighte,0x320);
if (oric->type == MACH_ATMOS && oric->twilighteboard_activated)
{
if ( twilighteboard_joysel & 0x80 )
{
// Right Port
if( joystate_a[0] ) mkmask &= 0xef;
if( joystate_a[1] ) mkmask &= 0xf7;
if( joystate_a[2] ) mkmask &= 0xfd;
if( joystate_a[3] ) mkmask &= 0xfe;
if( joystate_a[4] ) mkmask &= 0xfb;
if( joystate_a[5] ) mkmask_f &= 0x7f; // PA7 - fire 2
if( joystate_a[6] ) mkmask_f &= 0xdf; // PA5 - fire 3
gimme_port_a = SDL_TRUE;
gimme_port_b = SDL_TRUE;
}
if ( twilighteboard_joysel & 0x40 )
{
// Left Port
if( joystate_b[0] ) mkmask &= 0xef; // 0x10 - up
if( joystate_b[1] ) mkmask &= 0xf7; // 0x08 - down
if( joystate_b[2] ) mkmask &= 0xfd; // 0x02 - left
if( joystate_b[3] ) mkmask &= 0xfe; // 0x01 - right
if( joystate_b[4] ) mkmask &= 0xfb; // 0x04 - fire
gimme_port_b = SDL_TRUE;
}
if( gimme_port_a )
{
twilighteboard_oric_write(oric->twilighte, 0x321, 0xff, mkmask_f);
}
if( gimme_port_b )
{
// port B
twilighteboard_oric_write(oric->twilighte, 0x320, 0xff, mkmask);
}
}
else if (oric->type == MACH_TELESTRAT)
{
if ( telestrat_joysel & 0x80 )
{
// Right Port
if( joystate_a[0] ) mkmask &= 0xef;
if( joystate_a[1] ) mkmask &= 0xf7;
if( joystate_a[2] ) mkmask &= 0xfd;
if( joystate_a[3] ) mkmask &= 0xfe;
if( joystate_a[4] ) mkmask &= 0xfb;
if( joystate_a[5] ) mkmask_f &= 0x7f; // PA7 - fire 2
if( joystate_a[6] ) mkmask_f &= 0xdf; // PA5 - fire 3
gimme_port_a = SDL_TRUE;
gimme_port_b = SDL_TRUE;
}
if ( telestrat_joysel & 0x40 )
{
// Left Port
if( joystate_b[0] ) mkmask &= 0xef; // 0x10 - up
if( joystate_b[1] ) mkmask &= 0xf7; // 0x08 - down
if( joystate_b[2] ) mkmask &= 0xfd; // 0x02 - left
if( joystate_b[3] ) mkmask &= 0xfe; // 0x01 - right
if( joystate_b[4] ) mkmask &= 0xfb; // 0x04 - fire
gimme_port_b = SDL_TRUE;
}
if( gimme_port_a )
{
oric->tele_via.write_port_a( &oric->tele_via, 0xff, mkmask_f );
}
if( gimme_port_b )
{
oric->tele_via.write_port_b( &oric->tele_via, 0xff, mkmask );
}
}
else
{
switch( oric->joy_iface )
{
case JOYIFACE_ALTAI:
if( joysel & 0x80 )
{
if( joystate_a[0] ) mkmask &= 0xef;
if( joystate_a[1] ) mkmask &= 0xf7;
if( joystate_a[2] ) mkmask &= 0xfe;
if( joystate_a[3] ) mkmask &= 0xfd;
if( joystate_a[4] ) mkmask &= 0xdf;
gimme_port_a = SDL_TRUE;
}
if( joysel & 0x40 )
{
if( joystate_b[0] ) mkmask &= 0xef; // 0x10 - up
if( joystate_b[1] ) mkmask &= 0xf7; // 0x08 - down
if( joystate_b[2] ) mkmask &= 0xfe; // 0x01 - left
if( joystate_b[3] ) mkmask &= 0xfd; // 0x02 - right
if( joystate_b[4] ) mkmask &= 0xdf; // 0x20 - fire
gimme_port_a = SDL_TRUE;
}
break;
case JOYIFACE_IJK:
mkmask &= 0xdf;
if( ((oric->via.ddrb & 0x10)==0) ||
((oric->via.read_port_b( &oric->via )&0x10)!=0) )
break;
gimme_port_a = SDL_TRUE;
if( ( joysel & 0xc0 ) == 0xc0 ) break;
if( joysel & 0x40 )
{
if( joystate_a[0] ) mkmask &= 0xef;
if( joystate_a[1] ) mkmask &= 0xf7;
if( joystate_a[2] ) mkmask &= 0xfd;
if( joystate_a[3] ) mkmask &= 0xfe;
if( joystate_a[4] ) mkmask &= 0xfb;
}
if( joysel & 0x80 )
{
if( joystate_b[0] ) mkmask &= 0xef; // 0x10 - up
if( joystate_b[1] ) mkmask &= 0xf7; // 0x08 - down
if( joystate_b[2] ) mkmask &= 0xfd; // 0x02 - left
if( joystate_b[3] ) mkmask &= 0xfe; // 0x01 - right
if( joystate_b[4] ) mkmask &= 0xfb; // 0x04 - fire
}
break;
}
oric->porta_joy = mkmask;
if( gimme_port_a )
{
oric->via.write_port_a( &oric->via, 0xff, mkmask );
oric->porta_is_ay = SDL_FALSE;
} else {
if( !oric->porta_is_ay )
{
oric->via.write_port_a( &oric->via, 0xff, oric->porta_ay );
oric->porta_is_ay = SDL_TRUE;
}
}
}
}
SDL_bool joy_filter_event( SDL_Event *ev, struct machine *oric )
{
SDL_bool swallow_event;
SDL_bool mode = (oric->type==MACH_TELESTRAT || (oric->twilighteboard_activated && oric->type==MACH_ATMOS))? SDL_TRUE : SDL_FALSE;
swallow_event = dojoyevent( ev, oric, mode ? oric->telejoymode_a : oric->joymode_a, joystate_a, oric->sdljoy_a );
swallow_event |= dojoyevent( ev, oric, mode ? oric->telejoymode_b : oric->joymode_b, joystate_b, oric->sdljoy_b );
if( swallow_event )
{
joy_buildmask( oric );
// {
// char testytesttest[64];
// sprintf( testytesttest, "A: %d%d%d%d-%d%d B: %d%d%d%d-%d%d",
// joystate_a[0], joystate_a[1], joystate_a[2], joystate_a[3],
// joystate_a[4], joystate_a[5],
// joystate_b[0], joystate_b[1], joystate_b[2], joystate_b[3],
// joystate_b[4], joystate_b[5] );
// SDL_WM_SetCaption( testytesttest, testytesttest );
// }
}
return swallow_event;
}
static void dojoysetup( struct machine *oric, Sint16 mode_a, Sint16 mode_b )
{
Sint32 i;
close_joysticks( oric );
for( i=0; i<6; i++ )
{
joystate_a[i] = 0;
joystate_b[i] = 0;
}
if( (!is_real_joystick( mode_a )) && (!is_real_joystick( mode_b )) )
return;
if( !joysubinited )
{
//dbg_printf( "Initialising joysubsystem" );
if( SDL_InitSubSystem( SDL_INIT_JOYSTICK ) != 0 )
return;
//dbg_printf( "Success!" );
joysubinited = SDL_TRUE;
}
if( is_real_joystick( mode_a ) )
{
oric->sdljoy_a = SDL_JoystickOpen( mode_a - JOYMODE_SDL0 );
//dbg_printf( "Joy0 = %p", oric->sdljoy_a );
SDL_JoystickEventState( SDL_TRUE );
}
if( is_real_joystick( mode_b ) )
{
if( mode_b == mode_a )
{
oric->sdljoy_b = oric->sdljoy_a;
} else {
oric->sdljoy_b = SDL_JoystickOpen( mode_b - JOYMODE_SDL0 );
SDL_JoystickEventState( SDL_TRUE );
}
}
}
void joy_setup( struct machine *oric )
{
if( oric->type == MACH_TELESTRAT || ( oric->type == MACH_ATMOS && oric->twilighteboard_activated) )
dojoysetup( oric, oric->telejoymode_a, oric->telejoymode_b );
else
dojoysetup( oric, oric->joymode_a, oric->joymode_b );
}
SDL_bool init_joy( struct machine *oric )
{
return SDL_TRUE;
}
void shut_joy( struct machine *oric )
{
close_joysticks( oric );
}