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/* Handle the event stream, converting GGI events into SDL events */
25
26#include <sys/types.h>
27#include <sys/time.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <termios.h>
31
32#include <ggi/keyboard.h>
33
34#include "SDL_ggikeys.h"
35
36#include "SDL.h"
37#include "../SDL_sysvideo.h"
38#include "../../events/SDL_sysevents.h"
39#include "../../events/SDL_events_c.h"
40#include "SDL_ggivideo.h"
41#include "SDL_ggievents_c.h"
42
43/* The translation tables from a GGI keycode to a SDL keysym */
44static SDLKey keymap[128];
45static SDL_keysym *GGI_TranslateKey(ggi_event *ev, SDL_keysym *keysym);
46
47static int posted = 0;
48
49void GGI_PumpEvents(_THIS)
50{
51	struct timeval *tvp, tv = { 0, 0 };
52	ggi_event ev;
53
54	tvp = &tv;
55
56/*	ggiFlush(VIS); */
57
58	while (ggiEventPoll(VIS, emAll, tvp))
59/*	while (ggiEventPoll(VIS, (emKeyboard | emPointer | emCommand), tvp)) */
60	{
61		int queueevent_mouse = 0, queueevent_kbd = 0;
62		static int buttons = 0;
63		static int mouse_x = 0, mouse_y = 0, mouse_z = 0;
64		int x = 0, y = 0, z = 0, rx = 0, ry = 0, rz = 0;
65		int pressed_mouse, pressed_kbd;
66		SDL_keysym keysym;
67
68		posted = 0;
69
70		/* FIXME: We do not actually want all events, only
71		 * mouse and keyboard events.  Having to handle all
72                 * events will slow things down.  */
73
74		ggiEventRead(VIS, &ev, emAll);
75/*		ggiEventRead(VIS, &ev, (emKeyboard | emPointer | emCommand)); */
76
77		switch (ev.any.type)
78		{
79			case evPtrRelative:
80			x = ev.pmove.x;
81			y = ev.pmove.y;
82			z = ev.pmove.wheel;
83			posted += SDL_PrivateMouseMotion(0, 1, x, y);
84			break;
85			case evPtrAbsolute:
86			if (mouse_x != ev.pmove.x || mouse_y != ev.pmove.y || mouse_z != ev.pmove.wheel)
87			{
88				x = ev.pmove.x - mouse_x;
89				y = ev.pmove.y - mouse_y;
90				z = ev.pmove.wheel - mouse_z;
91				mouse_x = ev.pmove.x;
92				mouse_y = ev.pmove.y;
93				mouse_z = ev.pmove.wheel;
94				posted += SDL_PrivateMouseMotion(0, 1, x, y);
95			}
96			break;
97			case evPtrButtonPress:
98			posted += SDL_PrivateMouseButton(SDL_PRESSED, ev.pbutton.button, 0, 0);
99			break;
100			case evPtrButtonRelease:
101			posted += SDL_PrivateMouseButton(SDL_RELEASED, ev.pbutton.button, 0, 0);
102			break;
103			case evKeyPress:
104			case evKeyRepeat:
105			posted += SDL_PrivateKeyboard(SDL_PRESSED, GGI_TranslateKey(&ev, &keysym));
106			break;
107			case evKeyRelease:
108			posted += SDL_PrivateKeyboard(SDL_RELEASED, GGI_TranslateKey(&ev, &keysym));
109			break;
110			case evCommand:
111			fprintf(stderr, "Command event %x recieved\n", ev.cmd.code);
112			break;
113 			default:
114			fprintf(stderr, "Unhandled event type %d\n", ev.any.type);
115			break;
116		}
117	}
118
119}
120
121void GGI_InitOSKeymap(_THIS)
122{
123	int i;
124
125	/* Initialize the GGI key translation table */
126	for ( i=0; i<SDL_arraysize(keymap); ++i )
127		keymap[i] = SDLK_UNKNOWN;
128
129	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
130	keymap[SCANCODE_1] = SDLK_1;
131	keymap[SCANCODE_2] = SDLK_2;
132	keymap[SCANCODE_3] = SDLK_3;
133	keymap[SCANCODE_4] = SDLK_4;
134	keymap[SCANCODE_5] = SDLK_5;
135	keymap[SCANCODE_6] = SDLK_6;
136	keymap[SCANCODE_7] = SDLK_7;
137	keymap[SCANCODE_8] = SDLK_8;
138	keymap[SCANCODE_9] = SDLK_9;
139	keymap[SCANCODE_0] = SDLK_0;
140	keymap[SCANCODE_MINUS] = SDLK_MINUS;
141	keymap[SCANCODE_EQUAL] = SDLK_EQUALS;
142	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
143	keymap[SCANCODE_TAB] = SDLK_TAB;
144	keymap[SCANCODE_Q] = SDLK_q;
145	keymap[SCANCODE_W] = SDLK_w;
146	keymap[SCANCODE_E] = SDLK_e;
147	keymap[SCANCODE_R] = SDLK_r;
148	keymap[SCANCODE_T] = SDLK_t;
149	keymap[SCANCODE_Y] = SDLK_y;
150	keymap[SCANCODE_U] = SDLK_u;
151	keymap[SCANCODE_I] = SDLK_i;
152	keymap[SCANCODE_O] = SDLK_o;
153	keymap[SCANCODE_P] = SDLK_p;
154	keymap[SCANCODE_BRACKET_LEFT] = SDLK_LEFTBRACKET;
155	keymap[SCANCODE_BRACKET_RIGHT] = SDLK_RIGHTBRACKET;
156	keymap[SCANCODE_ENTER] = SDLK_RETURN;
157	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
158	keymap[SCANCODE_A] = SDLK_a;
159	keymap[SCANCODE_S] = SDLK_s;
160	keymap[SCANCODE_D] = SDLK_d;
161	keymap[SCANCODE_F] = SDLK_f;
162	keymap[SCANCODE_G] = SDLK_g;
163	keymap[SCANCODE_H] = SDLK_h;
164	keymap[SCANCODE_J] = SDLK_j;
165	keymap[SCANCODE_K] = SDLK_k;
166	keymap[SCANCODE_L] = SDLK_l;
167	keymap[SCANCODE_SEMICOLON] = SDLK_SEMICOLON;
168	keymap[SCANCODE_APOSTROPHE] = SDLK_QUOTE;
169	keymap[SCANCODE_GRAVE] = SDLK_BACKQUOTE;
170	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
171	keymap[SCANCODE_BACKSLASH] = SDLK_BACKSLASH;
172	keymap[SCANCODE_Z] = SDLK_z;
173	keymap[SCANCODE_X] = SDLK_x;
174	keymap[SCANCODE_C] = SDLK_c;
175	keymap[SCANCODE_V] = SDLK_v;
176	keymap[SCANCODE_B] = SDLK_b;
177	keymap[SCANCODE_N] = SDLK_n;
178	keymap[SCANCODE_M] = SDLK_m;
179	keymap[SCANCODE_COMMA] = SDLK_COMMA;
180	keymap[SCANCODE_PERIOD] = SDLK_PERIOD;
181	keymap[SCANCODE_SLASH] = SDLK_SLASH;
182	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
183	keymap[SCANCODE_KEYPADMULTIPLY] = SDLK_KP_MULTIPLY;
184	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
185	keymap[SCANCODE_SPACE] = SDLK_SPACE;
186	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
187	keymap[SCANCODE_F1] = SDLK_F1;
188	keymap[SCANCODE_F2] = SDLK_F2;
189	keymap[SCANCODE_F3] = SDLK_F3;
190	keymap[SCANCODE_F4] = SDLK_F4;
191	keymap[SCANCODE_F5] = SDLK_F5;
192	keymap[SCANCODE_F6] = SDLK_F6;
193	keymap[SCANCODE_F7] = SDLK_F7;
194	keymap[SCANCODE_F8] = SDLK_F8;
195	keymap[SCANCODE_F9] = SDLK_F9;
196	keymap[SCANCODE_F10] = SDLK_F10;
197	keymap[SCANCODE_NUMLOCK] = SDLK_NUMLOCK;
198	keymap[SCANCODE_SCROLLLOCK] = SDLK_SCROLLOCK;
199	keymap[SCANCODE_KEYPAD7] = SDLK_KP7;
200	keymap[SCANCODE_CURSORUPLEFT] = SDLK_KP7;
201	keymap[SCANCODE_KEYPAD8] = SDLK_KP8;
202	keymap[SCANCODE_CURSORUP] = SDLK_KP8;
203	keymap[SCANCODE_KEYPAD9] = SDLK_KP9;
204	keymap[SCANCODE_CURSORUPRIGHT] = SDLK_KP9;
205	keymap[SCANCODE_KEYPADMINUS] = SDLK_KP_MINUS;
206	keymap[SCANCODE_KEYPAD4] = SDLK_KP4;
207	keymap[SCANCODE_CURSORLEFT] = SDLK_KP4;
208	keymap[SCANCODE_KEYPAD5] = SDLK_KP5;
209	keymap[SCANCODE_KEYPAD6] = SDLK_KP6;
210	keymap[SCANCODE_CURSORRIGHT] = SDLK_KP6;
211	keymap[SCANCODE_KEYPADPLUS] = SDLK_KP_PLUS;
212	keymap[SCANCODE_KEYPAD1] = SDLK_KP1;
213	keymap[SCANCODE_CURSORDOWNLEFT] = SDLK_KP1;
214	keymap[SCANCODE_KEYPAD2] = SDLK_KP2;
215	keymap[SCANCODE_CURSORDOWN] = SDLK_KP2;
216	keymap[SCANCODE_KEYPAD3] = SDLK_KP3;
217	keymap[SCANCODE_CURSORDOWNRIGHT] = SDLK_KP3;
218	keymap[SCANCODE_KEYPAD0] = SDLK_KP0;
219	keymap[SCANCODE_KEYPADPERIOD] = SDLK_KP_PERIOD;
220	keymap[SCANCODE_LESS] = SDLK_LESS;
221	keymap[SCANCODE_F11] = SDLK_F11;
222	keymap[SCANCODE_F12] = SDLK_F12;
223	keymap[SCANCODE_KEYPADENTER] = SDLK_KP_ENTER;
224	keymap[SCANCODE_RIGHTCONTROL] = SDLK_RCTRL;
225	keymap[SCANCODE_CONTROL] = SDLK_RCTRL;
226	keymap[SCANCODE_KEYPADDIVIDE] = SDLK_KP_DIVIDE;
227	keymap[SCANCODE_PRINTSCREEN] = SDLK_PRINT;
228	keymap[SCANCODE_RIGHTALT] = SDLK_RALT;
229	keymap[SCANCODE_BREAK] = SDLK_BREAK;
230	keymap[SCANCODE_BREAK_ALTERNATIVE] = SDLK_UNKNOWN;
231	keymap[SCANCODE_HOME] = SDLK_HOME;
232	keymap[SCANCODE_CURSORBLOCKUP] = SDLK_UP;
233	keymap[SCANCODE_PAGEUP] = SDLK_PAGEUP;
234	keymap[SCANCODE_CURSORBLOCKLEFT] = SDLK_LEFT;
235	keymap[SCANCODE_CURSORBLOCKRIGHT] = SDLK_RIGHT;
236	keymap[SCANCODE_END] = SDLK_END;
237	keymap[SCANCODE_CURSORBLOCKDOWN] = SDLK_DOWN;
238	keymap[SCANCODE_PAGEDOWN] = SDLK_PAGEDOWN;
239	keymap[SCANCODE_INSERT] = SDLK_INSERT;
240	keymap[SCANCODE_REMOVE] = SDLK_DELETE;
241	keymap[119] = SDLK_PAUSE;
242	keymap[SCANCODE_RIGHTWIN] = SDLK_RSUPER;
243	keymap[SCANCODE_LEFTWIN] = SDLK_LSUPER;
244	keymap[127] = SDLK_MENU;
245}
246
247
248
249static SDL_keysym *GGI_TranslateKey(gii_event *ev, SDL_keysym *keysym)
250{
251	/* Set the keysym information */
252	keysym->scancode = ev->key.button;
253	keysym->sym = keymap[ev->key.button];
254	keysym->mod = KMOD_NONE;
255
256	/* If UNICODE is on, get the UNICODE value for the key */
257	keysym->unicode = 0;
258	if (SDL_TranslateUNICODE)
259	{
260		keysym->unicode = GII_UNICODE(ev->key.sym);
261	}
262
263	return keysym;
264}
265