-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.c
571 lines (493 loc) · 12.9 KB
/
mouse.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* mouse.c - Common mouse handling
*
* Written by
* Andreas Boose <viceteam@t-online.de>
*
* NEOS & Amiga mouse and paddle support by
* Hannu Nuotio <hannu.nuotio@tut.fi>
*
* This file is part of VICE, the Versatile Commodore Emulator.
* See README for copyright notice.
*
* 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., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/
#include "vice.h"
#include "alarm.h"
#include "cmdline.h"
#include "joystick.h"
#include "machine.h"
#include "maincpu.h"
#include "mouse.h"
#include "mousedrv.h"
#include "resources.h"
#include "translate.h"
/* --------------------------------------------------------- */
/* extern variables */
int _mouse_enabled = 0;
int mouse_port = 1;
int mouse_type;
/* --------------------------------------------------------- */
/* NEOS mouse */
#define NEOS_RESET_CLK 100
struct alarm_s *neosmouse_alarm;
static int neos_and_amiga_buttons;
static int neos_prev;
static BYTE neos_x;
static BYTE neos_y;
static BYTE neos_lastx;
static BYTE neos_lasty;
enum {
NEOS_IDLE = 0,
NEOS_XH,
NEOS_XL,
NEOS_YH,
NEOS_YL,
NEOS_DONE
} neos_state = NEOS_IDLE;
static void neos_get_new_movement(void)
{
BYTE new_x, new_y;
new_x = mousedrv_get_x();
new_y = mousedrv_get_y();
neos_x = new_x - neos_lastx;
if (new_x < neos_lastx)
{
if (neos_lastx > 0x6f && new_x < 0x10)
{
neos_x += 0x80;
}
}
else if (new_x > neos_lastx)
{
if (neos_lastx < 0x10 && new_x > 0x6f)
{
neos_x += 0x80;
}
}
neos_lastx = new_x;
neos_y = new_y - neos_lasty;
if (new_y < neos_lasty)
{
if (neos_lasty > 0x6f && new_y < 0x10)
{
neos_y += 0x80;
}
}
else if (new_y > neos_lasty)
{
if (neos_lasty < 0x10 && new_y > 0x6f)
{
neos_y += 0x80;
}
}
neos_lasty = new_y;
neos_x = (neos_x & 0x80) ? (neos_x / 2) | 0x80 : neos_x / 2;
neos_y = (neos_y & 0x80) ? (neos_y / 2) | 0x80 : neos_y / 2;
neos_x = -neos_x;
}
void neos_mouse_store(BYTE val)
{
switch (neos_state) {
case NEOS_IDLE:
if (((val & 16) ^ (neos_prev & 16)) && ((val & 16)==0)) {
++neos_state;
neos_get_new_movement();
}
break;
case NEOS_XH:
if (((val & 16) ^ (neos_prev & 16)) && ((val & 16)!=0)) {
++neos_state;
}
break;
case NEOS_XL:
if (((val & 16) ^ (neos_prev & 16)) && ((val & 16)==0)) {
++neos_state;
}
break;
case NEOS_YH:
if (((val & 16) ^ (neos_prev & 16)) && ((val & 16)!=0)) {
++neos_state;
alarm_set(neosmouse_alarm, maincpu_clk + NEOS_RESET_CLK);
}
break;
case NEOS_YL:
++neos_state;
break;
case NEOS_DONE:
default:
break;
}
neos_prev = val;
}
BYTE neos_mouse_read(void)
{
switch (neos_state)
{
case NEOS_XH:
return ((neos_x >> 4) & 0xf) | 0xf0;
break;
case NEOS_XL:
return (neos_x & 0xf) | 0xf0;
break;
case NEOS_YH:
return ((neos_y >> 4) & 0xf) | 0xf0;
break;
case NEOS_YL:
return (neos_y & 0xf) | 0xf0;
break;
case NEOS_IDLE:
case NEOS_DONE:
default:
return 0xff;
break;
}
}
static void neosmouse_alarm_handler(CLOCK offset, void *data)
{
alarm_unset(neosmouse_alarm);
neos_state = NEOS_IDLE;
}
/* --------------------------------------------------------- */
/* Amiga mouse support (currently experimental) */
static const BYTE amiga_mouse_table[4] = { 0x0, 0x1, 0x5, 0x4 };
/* the method below results in alot of overflows */
#if 0
BYTE amiga_mouse_read(void)
{
BYTE new_x, new_y;
new_x = mousedrv_get_x()/2;
new_y = (-mousedrv_get_y())/2;
return (amiga_mouse_table[new_x & 3] << 1) | amiga_mouse_table[new_y &
3] | 0xf0;
}
#endif
/* the alternate method below doesn't keep track of the speed of
the mouse movements, just the direction.
*/
static BYTE old_x = 0;
static BYTE old_y = 0;
static BYTE x_count = 0;
static BYTE y_count = 0;
BYTE amiga_mouse_read(void)
{
BYTE new_x, new_y;
signed char dir_x, dir_y;
/* get the new mouse values */
new_x = mousedrv_get_x();
new_y = (-mousedrv_get_y());
/* find out the x direction */
if (new_x == old_x)
{
/* no direction, 0 */
dir_x = 0;
}
else
{
if (new_x > old_x)
{
if ((new_x - old_x) < (old_x + 256 - new_x))
{
/* right, +1 */
dir_x = 1;
}
else
{
/* left underflow, -1 */
dir_x = -1;
}
}
else
{
if ((old_x - new_x) < (new_x + 256 - old_x))
{
/* left, -1 */
dir_x = -1;
}
else
{
/* right overflow, +1 */
dir_x = 1;
}
}
}
/* find out the y direction */
if (new_y == old_y)
{
dir_y = 0;
}
else
{
if (new_y > old_y)
{
if ((new_y - old_y) < (old_y + 256 - new_y))
{
dir_y = 1;
}
else
{
dir_y = -1;
}
}
else
{
if ((old_y - new_y) < (new_y + 256 - old_y))
{
dir_y = -1;
}
else
{
dir_y = 1;
}
}
}
/* store new values as old values */
old_x = new_x;
old_y = new_y;
/* add x direction to x counter */
x_count += dir_x;
/* add y direction to y counter */
y_count += dir_y;
return (amiga_mouse_table[x_count & 3] << 1) | amiga_mouse_table[y_count & 3] | 0xf0;
}
/* --------------------------------------------------------- */
/* Paddle support */
static BYTE paddle_val[] = {
/* x y */
0x00, 0xff, /* no port */
0x00, 0xff, /* port 1 */
0x00, 0xff, /* port 2 */
0x00, 0xff /* both ports */
};
static BYTE paddle_old[] = {
0xff, 0xff,
0xff, 0xff,
0xff, 0xff,
0xff, 0xff
};
static BYTE paddle_port = 1;
static inline BYTE mouse_paddle_update(BYTE paddle_v, BYTE *old_v, BYTE new_v)
{
BYTE diff = new_v - *old_v;
BYTE new_paddle;
if (new_v < *old_v) {
if (*old_v > 0x6f && new_v < 0x10) {
diff += 0x80;
}
} else if (new_v > *old_v) {
if (*old_v < 0x10 && new_v > 0x6f) {
diff += 0x80;
}
}
new_paddle = paddle_v + diff;
*old_v = new_v;
if (((paddle_v & 0x80) ^ (new_paddle & 0x80)) && ((new_paddle & 0x80) == (diff & 0x80))) {
new_paddle = (paddle_v & 0x80)?0xff:0;
}
return new_paddle;
}
static BYTE mouse_get_paddle_x(void)
{
int i = (paddle_port << 1);
if (paddle_port == mouse_port) {
paddle_val[i] = mouse_paddle_update(paddle_val[i], &(paddle_old[i]), mousedrv_get_x());
}
return 0xff-paddle_val[i];
}
static BYTE mouse_get_paddle_y(void)
{
int i = (paddle_port << 1) + 1;
if (paddle_port == mouse_port) {
paddle_val[i] = mouse_paddle_update(paddle_val[i], &(paddle_old[i]), mousedrv_get_y());
}
return 0xff-paddle_val[i];
}
void mouse_set_paddle_port(int port)
{
paddle_port = port & 3;
}
/* --------------------------------------------------------- */
/* Resources & cmdline */
static int set_mouse_enabled(int val, void *param)
{
_mouse_enabled = val;
mousedrv_mouse_changed();
return 0;
}
static int set_mouse_port(int val, void *param)
{
if (val < 1 || val > 2)
return -1;
mouse_port = val;
return 0;
}
static int set_mouse_type(int val, void *param)
{
if (!((val >= 0) && (val <= 3))) {
return -1;
}
mouse_type = val;
return 0;
}
static const resource_int_t resources_int[] = {
{ "Mouse", 0, RES_EVENT_SAME, NULL,
&_mouse_enabled, set_mouse_enabled, NULL },
{ NULL }
};
static const resource_int_t resources_extra_int[] = {
{ "Mousetype", MOUSE_TYPE_1351, RES_EVENT_SAME, NULL,
&mouse_type, set_mouse_type, NULL },
{ "Mouseport", 1, RES_EVENT_SAME, NULL,
&mouse_port, set_mouse_port, NULL },
{ NULL }
};
int mouse_resources_init(void)
{
if (resources_register_int(resources_int) < 0)
return -1;
/* FIXME ugly kludge to remove port and type setting from xvic */
if (machine_class != VICE_MACHINE_VIC20) {
if (resources_register_int(resources_extra_int) < 0) {
return -1;
}
}
return mousedrv_resources_init();
}
static const cmdline_option_t cmdline_options[] = {
{ "-mouse", SET_RESOURCE, 0,
NULL, NULL, "Mouse", (void *)1,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_ENABLE_MOUSE_GRAB,
NULL, NULL },
{ "+mouse", SET_RESOURCE, 0,
NULL, NULL, "Mouse", (void *)0,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_DISABLE_MOUSE_GRAB,
NULL, NULL },
{ NULL }
};
static const cmdline_option_t cmdline_extra_option[] = {
{ "-mousetype", SET_RESOURCE, 1,
NULL, NULL, "Mousetype", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_VALUE, IDCLS_SELECT_MOUSE_TYPE,
NULL, NULL },
{ "-mouseport", SET_RESOURCE, 1,
NULL, NULL, "Mouseport", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_VALUE, IDCLS_SELECT_MOUSE_JOY_PORT,
NULL, NULL },
{ NULL }
};
int mouse_cmdline_options_init(void)
{
if (cmdline_register_options(cmdline_options) < 0)
return -1;
/* FIXME ugly kludge to remove port and type setting from xvic */
if (machine_class != VICE_MACHINE_VIC20) {
if (cmdline_register_options(cmdline_extra_option) < 0) {
return -1;
}
}
return mousedrv_cmdline_options_init();
}
void mouse_init(void)
{
/* FIXME ugly kludge to set the correct port and type setting for xvic */
if (machine_class == VICE_MACHINE_VIC20) {
set_mouse_port(1, NULL);
set_mouse_type(MOUSE_TYPE_PADDLE, NULL);
}
neos_and_amiga_buttons = 0;
neos_prev = 0xff;
neosmouse_alarm = alarm_new(maincpu_alarm_context, "NEOSMOUSEAlarm", neosmouse_alarm_handler, NULL);
mousedrv_init();
}
/* --------------------------------------------------------- */
/* Main API */
void mouse_button_left(int pressed)
{
BYTE joypin = ((mouse_type == MOUSE_TYPE_PADDLE) ? 4 : 16);
if (pressed) {
joystick_set_value_or(mouse_port, joypin);
} else {
joystick_set_value_and(mouse_port, (BYTE)~joypin);
}
}
void mouse_button_right(int pressed)
{
switch (mouse_type) {
case MOUSE_TYPE_1351:
if (pressed) {
joystick_set_value_or(mouse_port, 1);
} else {
joystick_set_value_and(mouse_port, ~1);
}
break;
case MOUSE_TYPE_PADDLE:
if (pressed) {
joystick_set_value_or(mouse_port, 8);
} else {
joystick_set_value_and(mouse_port, ~8);
}
break;
case MOUSE_TYPE_NEOS:
case MOUSE_TYPE_AMIGA:
if (pressed) {
neos_and_amiga_buttons |= 1;
} else {
neos_and_amiga_buttons &= ~1;
}
break;
default:
break;
}
}
BYTE mouse_get_x(void)
{
switch (mouse_type) {
case MOUSE_TYPE_1351:
return mousedrv_get_x();
break;
case MOUSE_TYPE_PADDLE:
return mouse_get_paddle_x();
break;
case MOUSE_TYPE_NEOS:
case MOUSE_TYPE_AMIGA:
return (neos_and_amiga_buttons & 1) ? 0xff : 0;
break;
default:
break;
}
return 0xff;
}
BYTE mouse_get_y(void)
{
switch (mouse_type) {
case MOUSE_TYPE_1351:
return mousedrv_get_y();
break;
case MOUSE_TYPE_PADDLE:
return mouse_get_paddle_y();
break;
case MOUSE_TYPE_NEOS:
case MOUSE_TYPE_AMIGA:
default:
break;
}
return 0xff;
}