27 static int move_dx, move_dy, nibble_counter;
29 static int wheel_dx, wheel_dy;
31 static int mouse_button1, mouse_button2;
32 static int rts_old_level = -1;
33 static int mouse_pulse = 0;
36 static Uint32 watchdog_mouse;
37 static Uint32 watchdog_xep128;
40 static int control_port_emu_mode = -1;
43 { SDL_SCANCODE_1, 0x31,
"1" },
44 { SDL_SCANCODE_2, 0x36,
"2" },
45 { SDL_SCANCODE_3, 0x35,
"3" },
46 { SDL_SCANCODE_4, 0x33,
"4" },
47 { SDL_SCANCODE_5, 0x34,
"5" },
48 { SDL_SCANCODE_6, 0x32,
"6" },
49 { SDL_SCANCODE_7, 0x30,
"7" },
50 { SDL_SCANCODE_8, 0x50,
"8" },
51 { SDL_SCANCODE_9, 0x52,
"9" },
52 { SDL_SCANCODE_0, 0x54,
"0" },
53 { SDL_SCANCODE_Q, 0x21,
"Q" },
54 { SDL_SCANCODE_W, 0x26,
"W" },
55 { SDL_SCANCODE_E, 0x25,
"E" },
56 { SDL_SCANCODE_R, 0x23,
"R" },
57 { SDL_SCANCODE_T, 0x24,
"T" },
58 { SDL_SCANCODE_Y, 0x22,
"Y" },
59 { SDL_SCANCODE_U, 0x20,
"U" },
60 { SDL_SCANCODE_I, 0x90,
"I" },
61 { SDL_SCANCODE_O, 0x92,
"O" },
62 { SDL_SCANCODE_P, 0x94,
"P" },
63 { SDL_SCANCODE_A, 0x16,
"A" },
64 { SDL_SCANCODE_S, 0x15,
"S" },
65 { SDL_SCANCODE_D, 0x13,
"D" },
66 { SDL_SCANCODE_F, 0x14,
"F" },
67 { SDL_SCANCODE_G, 0x12,
"G" },
68 { SDL_SCANCODE_H, 0x10,
"H" },
69 { SDL_SCANCODE_J, 0x60,
"J" },
70 { SDL_SCANCODE_K, 0x62,
"K" },
71 { SDL_SCANCODE_L, 0x64,
"L" },
72 { SDL_SCANCODE_RETURN, 0x76,
"ENTER" },
73 { SDL_SCANCODE_LSHIFT, 0x07,
"L-SHIFT" },
74 { SDL_SCANCODE_RSHIFT, 0x85,
"R-SHIFT" },
75 { SDL_SCANCODE_CAPSLOCK, 0x11,
"CAPS" },
76 { SDL_SCANCODE_Z, 0x06,
"Z" },
77 { SDL_SCANCODE_X, 0x05,
"X" },
78 { SDL_SCANCODE_C, 0x03,
"C" },
79 { SDL_SCANCODE_V, 0x04,
"V" },
80 { SDL_SCANCODE_B, 0x02,
"B" },
81 { SDL_SCANCODE_N, 0x00,
"N" },
82 { SDL_SCANCODE_M, 0x80,
"M" },
83 { SDL_SCANCODE_LCTRL, 0x17,
"CTRL" },
84 { SDL_SCANCODE_SPACE, 0x86,
"SPACE" },
85 { SDL_SCANCODE_SEMICOLON, 0x63,
";" },
86 { SDL_SCANCODE_LEFTBRACKET, 0x95,
"[" },
87 { SDL_SCANCODE_RIGHTBRACKET, 0x66,
"]" },
88 { SDL_SCANCODE_APOSTROPHE, 0x65,
":" },
89 { SDL_SCANCODE_MINUS, 0x53,
"-" },
90 { SDL_SCANCODE_BACKSLASH, 0x01,
"\\" },
91 { SDL_SCANCODE_TAB, 0x27,
"TAB" },
92 { SDL_SCANCODE_ESCAPE, 0x37,
"ESC" },
93 { SDL_SCANCODE_INSERT, 0x87,
"INS" },
94 { SDL_SCANCODE_BACKSPACE, 0x56,
"ERASE" },
95 { SDL_SCANCODE_DELETE, 0x81,
"DEL" },
96 { SDL_SCANCODE_LEFT, 0x75,
"LEFT" },
97 { SDL_SCANCODE_RIGHT, 0x72,
"RIGHT" },
98 { SDL_SCANCODE_UP, 0x73,
"UP" },
99 { SDL_SCANCODE_DOWN, 0x71,
"DOWN" },
100 { SDL_SCANCODE_SLASH, 0x83,
"/" },
101 { SDL_SCANCODE_PERIOD, 0x84,
"." },
102 { SDL_SCANCODE_COMMA, 0x82,
"," },
103 { SDL_SCANCODE_EQUALS, 0x93,
"@" },
104 { SDL_SCANCODE_F1, 0x47,
"F1" },
105 { SDL_SCANCODE_F2, 0x46,
"F2" },
106 { SDL_SCANCODE_F3, 0x42,
"F3" },
107 { SDL_SCANCODE_F4, 0x40,
"F4" },
108 { SDL_SCANCODE_F5, 0x44,
"F5" },
109 { SDL_SCANCODE_F6, 0x43,
"F6" },
110 { SDL_SCANCODE_F7, 0x45,
"F7" },
111 { SDL_SCANCODE_F8, 0x41,
"F8" },
113 { SDL_SCANCODE_HOME, 0x74,
"HOLD" },
114 { SDL_SCANCODE_END, 0x70,
"STOP" },
116 { SDL_SCANCODE_KP_5, 0xA0,
"ExtJoy FIRE" },
117 { SDL_SCANCODE_KP_8, 0xA1,
"ExtJoy UP" },
118 { SDL_SCANCODE_KP_2, 0xA2,
"ExtJoy DOWN" },
119 { SDL_SCANCODE_KP_4, 0xA3,
"ExtJoy LEFT" },
120 { SDL_SCANCODE_KP_6, 0xA4,
"ExtJoy RIGHT" },
138 static Uint8 mouse_buffer[] = {
149 #define WATCHDOG_USEC(n) (n / 64)
155 #define BUTTON_MAIN 1
156 #define BUTTON_OTHER 2
178 .
name =
"BoxSoft J-col",
185 .name =
"ExtMSX J-col",
192 .name =
"EnterMice J-col",
199 .name =
"BoxSoft K-col",
206 .name =
"ExtMSX K-col",
213 .name =
"EnterMice K-col",
222 #define LAST_MOUSE_MODE ((sizeof(mouse_modes) / sizeof(const struct mouse_modes_st)) - 1)
227 #define JOYSTICK_SCAN(num, dir) joystick_scan(num, dir)
236 sprintf(buffer,
"#%d *** Invalid mouse mode ***", cfg);
241 "#%d (%s) nibbles=%d wrap=%d watchdog=%d mask=%d",
243 mouse_modes[cfg].
name,
245 mouse_modes[cfg].
wrap,
259 mouse_buffer[2] &= 0xF0;
264 static inline void set_button (
Uint8 *storage,
int mask,
int pressed )
269 *storage &= 255 -
mask;
278 switch (sdl_button) {
279 case SDL_BUTTON_LEFT:
283 case SDL_BUTTON_MIDDLE:
287 case SDL_BUTTON_RIGHT:
304 DEBUG(
"MOUSE: BUTTON: event: SDL#%d XEP#%d (%s) %s" NL, sdl_button,
id,
name, press ?
"pressed" :
"released");
306 DEBUG(
"MOUSE: BUTTON: unknown button on SDL level (see previous MOUSE: line)!!" NL);
309 if (sdl_button == SDL_BUTTON_LEFT && press &&
mouse_grab == 0) {
316 DEBUG(
"MOUSE: BUTTON: not in grab mode, do not forward event" NL);
319 switch (
mode->buttons[
id]) {
321 mouse_button1 = press;
324 mouse_button2 = press;
327 set_button(&mouse_buffer[2], 1, press);
330 set_button(&mouse_buffer[2], 2, press);
333 set_button(&mouse_buffer[2], 4, press);
336 DEBUG(
"MOUSE: used mouse button cannot be mapped for the given mouse mode (map=%d), ignored" NL,
mode->buttons[
id]);
345 DEBUG(
"MOUSE: MOTION: event: dx = %d, dy = %d" NL, dx, dy);
347 DEBUG(
"MOUSE: MOTION: not in grab mode, do not forward event" NL);
351 if (move_dx > 127) move_dx = 127;
352 else if (move_dx < -128) move_dx = -128;
354 if (move_dy > 127) move_dy = 127;
355 else if (move_dy < -128) move_dy = -128;
362 DEBUG(
"MOUSE: WHEEL: event: x = %d, y = %d, flipped = %d" NL,
x,
y, flipped);
364 DEBUG(
"MOUSE: WHEEL: not in grab mode, do not forward event" NL);
367 flipped = flipped ? -1 : 1;
368 wheel_dx -=
x * flipped;
369 if (wheel_dx > 127) wheel_dx = 127;
370 else if (wheel_dx < -128) wheel_dx = -128;
371 wheel_dy -=
y * flipped;
372 if (wheel_dy > 127) wheel_dy = 127;
373 else if (wheel_dy < -128) wheel_dy = -128;
392 mouse_buffer[0] = mouse_buffer[1] = mouse_buffer[3] = 0;
397 static inline void check_mouse_watchdog (
void )
401 if (
mode->watchdog >= 0 && (time >
mode->watchdog || time < 0))
408 if (num != 0 && num != 1)
418 int mouse_ok, joy1_ok;
426 }
else if (mouse_pulse && (time <
WATCHDOG_USEC(100000) || time < 0)) {
434 if (control_port_emu_mode != mouse_ok + joy1_ok) {
435 static const char *m[] = {
"joystick",
"Mouse",
"dual (K-col)" };
436 control_port_emu_mode = mouse_ok + joy1_ok;
437 OSD(-1, -1,
"Control port: %s mode", m[control_port_emu_mode - 1 ]);
443 (mouse_ok ? ((mouse_button1 ? 0 :
mode->data_mask) | (7 -
mode->data_mask - 4) | (mouse_button2 ? 0 : 4)) : 7) &
465 if ((val & 2) == rts_old_level)
467 rts_old_level = val & 2;
470 check_mouse_watchdog();
471 if (nibble_counter >=
mode->nibbles &&
mode->wrap)
475 if (nibble_counter == 0) {
478 mouse_buffer[0] = move_dx;
480 }
else if (nibble_counter == 2) {
483 mouse_buffer[1] = move_dy;
485 }
else if (nibble_counter == 6) {
486 mouse_buffer[3] = wheel_dy;
489 if (nibble_counter < mode->
nibbles) {
492 nibble = ((nibble_counter & 1) ? (mouse_buffer[nibble_counter >> 1] & 15) : (mouse_buffer[nibble_counter >> 1] >> 4));
508 mode = &mouse_modes[cfg];
510 DEBUG(
"MOUSE: SETUP: %s" NL, buffer);
521 if (!pressed && pos == -2 && key == 0 && handled == SDL_BUTTON_RIGHT)