1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 Sam Lantinga
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19    Sam Lantinga
20    slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24#include "SDL.h"
25#include "../../events/SDL_sysevents.h"
26#include "../../events/SDL_events_c.h"
27#include "SDL_dcvideo.h"
28#include "SDL_dcevents_c.h"
29
30#include <dc/maple.h>
31#include <dc/maple/mouse.h>
32#include <dc/maple/keyboard.h>
33
34const static unsigned short sdl_key[]= {
35	/*0*/	0, 0, 0, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
36		'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
37		'u', 'v', 'w', 'x', 'y', 'z',
38	/*1e*/	'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
39	/*28*/	SDLK_RETURN, SDLK_ESCAPE, SDLK_BACKSPACE, SDLK_TAB, SDLK_SPACE, SDLK_MINUS, SDLK_PLUS, SDLK_LEFTBRACKET,
40	SDLK_RIGHTBRACKET, SDLK_BACKSLASH , 0, SDLK_SEMICOLON, SDLK_QUOTE,
41	/*35*/	'~', SDLK_COMMA, SDLK_PERIOD, SDLK_SLASH, SDLK_CAPSLOCK,
42	SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_F9, SDLK_F10, SDLK_F11, SDLK_F12,
43	/*46*/	SDLK_PRINT, SDLK_SCROLLOCK, SDLK_PAUSE, SDLK_INSERT, SDLK_HOME, SDLK_PAGEUP, SDLK_DELETE, SDLK_END, SDLK_PAGEDOWN, SDLK_RIGHT, SDLK_LEFT, SDLK_DOWN, SDLK_UP,
44	/*53*/	SDLK_NUMLOCK, SDLK_KP_DIVIDE, SDLK_KP_MULTIPLY, SDLK_KP_MINUS, SDLK_KP_PLUS, SDLK_KP_ENTER,
45	SDLK_KP1, SDLK_KP2, SDLK_KP3, SDLK_KP4, SDLK_KP5, SDLK_KP6,
46	/*5f*/	SDLK_KP7, SDLK_KP8, SDLK_KP9, SDLK_KP0, SDLK_KP_PERIOD, 0 /* S3 */
47};
48
49const static unsigned short sdl_shift[] = {
50	SDLK_LCTRL,SDLK_LSHIFT,SDLK_LALT,0 /* S1 */,
51	SDLK_RCTRL,SDLK_RSHIFT,SDLK_RALT,0 /* S2 */,
52};
53
54#define	MOUSE_WHEELUP 	(1<<4)
55#define	MOUSE_WHEELDOWN	(1<<5)
56
57static void mouse_update(void)
58{
59const	static char sdl_mousebtn[] = {
60	MOUSE_LEFTBUTTON,
61	MOUSE_RIGHTBUTTON,
62	MOUSE_SIDEBUTTON,
63	MOUSE_WHEELUP,
64	MOUSE_WHEELDOWN
65};
66
67	uint8 addr;
68	mouse_cond_t	cond;
69
70	static int prev_buttons;
71	int buttons,changed;
72	int i;
73
74	if ((addr = maple_first_mouse())==0 || mouse_get_cond(addr, &cond)<0) return;
75
76	buttons = cond.buttons^0xff;
77	if (cond.dz<0) buttons|=MOUSE_WHEELUP;
78	if (cond.dz>0) buttons|=MOUSE_WHEELDOWN;
79
80	if (cond.dx||cond.dy) SDL_PrivateMouseMotion(0,1,cond.dx,cond.dy);
81
82	changed = buttons^prev_buttons;
83	for(i=0;i<sizeof(sdl_mousebtn);i++) {
84		if (changed & sdl_mousebtn[i]) {
85			SDL_PrivateMouseButton((buttons & sdl_mousebtn[i])?SDL_PRESSED:SDL_RELEASED,i,0,0);
86		}
87	}
88	prev_buttons = buttons;
89}
90
91static void keyboard_update(void)
92{
93	static kbd_state_t	old_state;
94	static uint8 old_addr;
95
96	kbd_state_t	*state;
97	uint8	addr;
98	int	port,unit;
99
100	int shiftkeys;
101	SDL_keysym keysym;
102
103	int i;
104
105	addr = maple_first_kb();
106
107	if (addr==0) return;
108
109	if (addr!=old_addr) {
110		old_addr = addr;
111		SDL_memset(&old_state,0,sizeof(old_state));
112	}
113
114	maple_raddr(addr,&port,&unit);
115
116	state = maple_dev_state(port,unit);
117	if (!state) return;
118
119	shiftkeys = state->shift_keys ^ old_state.shift_keys;
120	for(i=0;i<sizeof(sdl_shift);i++) {
121		if ((shiftkeys>>i)&1) {
122			keysym.sym = sdl_shift[i];
123			SDL_PrivateKeyboard(((state->shift_keys>>i)&1)?SDL_PRESSED:SDL_RELEASED,&keysym);
124		}
125	}
126
127	for(i=0;i<sizeof(sdl_key);i++) {
128		if (state->matrix[i]!=old_state.matrix[i]) {
129			int key = sdl_key[i];
130			if (key) {
131				keysym.sym = key;
132				SDL_PrivateKeyboard(state->matrix[i]?SDL_PRESSED:SDL_RELEASED,&keysym);
133			}
134		}
135	}
136
137	old_state = *state;
138}
139
140void DC_PumpEvents(_THIS)
141{
142	keyboard_update();
143	mouse_update();
144}
145
146void DC_InitOSKeymap(_THIS)
147{
148	/* do nothing. */
149}
150
151/* end of SDL_dcevents.c ... */
152
153