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/*
25 * GEM SDL video driver implementation
26 * inspired from the Dummy SDL driver
27 *
28 * Patrice Mandin
29 * and work from
30 * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
31 */
32
33#include <gem.h>
34
35#include "../../events/SDL_sysevents.h"
36#include "../../events/SDL_events_c.h"
37#include "SDL_gemvideo.h"
38#include "SDL_gemevents_c.h"
39#include "SDL_gemmouse_c.h"
40#include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
41#include "../ataricommon/SDL_atarievents_c.h"
42#include "../ataricommon/SDL_xbiosevents_c.h"
43#include "../ataricommon/SDL_ataridevmouse_c.h"
44
45/* Variables */
46
47static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
48static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
49
50/* Functions prototypes */
51
52static int do_messages(_THIS, short *message);
53static void do_keyboard(short kc, short ks);
54static void do_mouse(_THIS, short mx, short my, short mb, short ks);
55
56/* Functions */
57
58void GEM_InitOSKeymap(_THIS)
59{
60	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
61	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
62
63	/* Mouse init */
64	GEM_mouse_relative = SDL_FALSE;
65
66	SDL_Atari_InitInternalKeymap(this);
67}
68
69void GEM_PumpEvents(_THIS)
70{
71	short prevkc, prevks;
72	static short maskmouseb=0;
73	int i;
74	SDL_keysym	keysym;
75
76	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
77	prevkc = prevks = 0;
78
79	for (;;)
80	{
81		int quit, resultat, event_mask, mouse_event;
82		short buffer[8], kc;
83		short x2,y2,w2,h2;
84		short mousex, mousey, mouseb, dummy;
85		short kstate;
86
87		quit =
88			mouse_event =
89			x2=y2=w2=h2 = 0;
90
91		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD|MU_BUTTON;
92		if (!GEM_fullscreen && (GEM_handle>=0)) {
93			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
94			event_mask |= MU_M1;
95			mouse_event = ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
96				== SDL_APPMOUSEFOCUS) ? MO_LEAVE : MO_ENTER;
97		}
98
99		resultat = evnt_multi(
100			event_mask,
101			0x101,7,maskmouseb,
102			mouse_event,x2,y2,w2,h2,
103			0,0,0,0,0,
104			buffer,
105			10,
106			&mousex,&mousey,&mouseb,&kstate,&kc,&dummy
107		);
108
109		/* Message event ? */
110		if (resultat & MU_MESAG)
111			quit = do_messages(this, buffer);
112
113		/* Keyboard event ? */
114		if (resultat & MU_KEYBD) {
115			if ((prevkc != kc) || (prevks != kstate)) {
116				do_keyboard(kc,kstate);
117			} else {
118				/* Avoid looping, if repeating same key */
119				break;
120			}
121		}
122
123		/* Mouse entering/leaving window */
124		if (resultat & MU_M1) {
125			if (this->input_grab == SDL_GRAB_OFF) {
126				/* Switch mouse focus state */
127				SDL_PrivateAppActive((mouse_event == MO_ENTER),
128					SDL_APPMOUSEFOCUS);
129			}
130			GEM_CheckMouseMode(this);
131		}
132
133		/* Mouse button event ? */
134		if (resultat & MU_BUTTON) {
135			do_mouse(this, mousex, mousey, mouseb, kstate);
136			maskmouseb = mouseb & 7;
137		}
138
139		/* Timer event ? */
140		if ((resultat & MU_TIMER) || quit)
141			break;
142	}
143
144	/* Now generate keyboard events */
145	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
146		/* Key pressed ? */
147		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
148			SDL_PrivateKeyboard(SDL_PRESSED,
149				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
150
151		/* Key unpressed ? */
152		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
153			SDL_PrivateKeyboard(SDL_RELEASED,
154				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
155	}
156
157	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
158
159	/* Refresh window name ? */
160	if (GEM_refresh_name) {
161		const char *window_name =
162			(SDL_GetAppState() & SDL_APPACTIVE)
163			? GEM_title_name : GEM_icon_name;
164		if (window_name) {
165			wind_set(GEM_handle,WF_NAME,
166				(short)(((unsigned long)window_name)>>16),
167				(short)(((unsigned long)window_name) & 0xffff),
168				0,0);
169		}
170		GEM_refresh_name = SDL_FALSE;
171	}
172}
173
174static int do_messages(_THIS, short *message)
175{
176	int quit, check_mouse_mode;
177	short x2,y2,w2,h2;
178
179	quit = check_mouse_mode = 0;
180	switch (message[0]) {
181		case WM_CLOSED:
182		case AP_TERM:
183			SDL_PrivateQuit();
184			quit=1;
185			break;
186		case WM_MOVED:
187			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
188			break;
189		case WM_TOPPED:
190			wind_set(message[3],WF_TOP,message[4],0,0,0);
191			/* Continue with TOP event processing */
192		case WM_ONTOP:
193			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
194			if (VDI_setpalette) {
195				VDI_setpalette(this, VDI_curpalette);
196			}
197			check_mouse_mode = 1;
198			break;
199		case WM_REDRAW:
200			if (!GEM_lock_redraw) {
201				GEM_wind_redraw(this, message[3],&message[4]);
202			}
203			break;
204		case WM_ICONIFY:
205		case WM_ALLICONIFY:
206			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
207			/* If we're active, make ourselves inactive */
208			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
209				/* Send an internal deactivate event */
210				SDL_PrivateAppActive(0, SDL_APPACTIVE);
211			}
212			/* Update window title */
213			if (GEM_refresh_name && GEM_icon_name) {
214				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
215				GEM_refresh_name = SDL_FALSE;
216			}
217			check_mouse_mode = 1;
218			break;
219		case WM_UNICONIFY:
220			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
221			/* If we're not active, make ourselves active */
222			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
223				/* Send an internal activate event */
224				SDL_PrivateAppActive(1, SDL_APPACTIVE);
225			}
226			if (GEM_refresh_name && GEM_title_name) {
227				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
228				GEM_refresh_name = SDL_FALSE;
229			}
230			check_mouse_mode = 1;
231			break;
232		case WM_SIZED:
233			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
234			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
235			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
236			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
237			SDL_PrivateResize(w2, h2);
238			break;
239		case WM_FULLED:
240			{
241				short x,y,w,h;
242
243				if (GEM_win_fulled) {
244					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
245					GEM_win_fulled = SDL_FALSE;
246				} else {
247					x = GEM_desk_x;
248					y = GEM_desk_y;
249					w = GEM_desk_w;
250					h = GEM_desk_h;
251					GEM_win_fulled = SDL_TRUE;
252				}
253				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
254				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
255				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
256				SDL_PrivateResize(w2, h2);
257			}
258			break;
259		case WM_BOTTOMED:
260			wind_set(message[3],WF_BOTTOM,0,0,0,0);
261			/* Continue with BOTTOM event processing */
262		case WM_UNTOPPED:
263			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
264			if (VDI_setpalette) {
265				VDI_setpalette(this, VDI_oldpalette);
266			}
267			check_mouse_mode = 1;
268			break;
269	}
270
271	if (check_mouse_mode) {
272		GEM_CheckMouseMode(this);
273	}
274
275	return quit;
276}
277
278static void do_keyboard(short kc, short ks)
279{
280	int scancode;
281
282	if (kc) {
283		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
284		gem_currentkeyboard[scancode]=0xFF;
285	}
286
287	/* Read special keys */
288	if (ks & K_RSHIFT)
289		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
290	if (ks & K_LSHIFT)
291		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
292	if (ks & K_CTRL)
293		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
294	if (ks & K_ALT)
295		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
296}
297
298static void do_mouse(_THIS, short mx, short my, short mb, short ks)
299{
300	static short prevmousex=0, prevmousey=0, prevmouseb=0;
301	short x2, y2, w2, h2;
302
303	/* Don't return mouse events if out of window */
304	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
305		return;
306	}
307
308	/* Retrieve window coords, and generate mouse events accordingly */
309	x2 = y2 = 0;
310	w2 = VDI_w;
311	h2 = VDI_h;
312	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
313		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
314
315		/* Do not generate mouse button event if out of window working area */
316		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
317			mb=prevmouseb;
318		}
319	}
320
321	/* Mouse motion ? */
322	if (GEM_mouse_relative) {
323		if (GEM_usedevmouse) {
324			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
325		} else {
326			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
327		}
328	} else {
329		if ((prevmousex!=mx) || (prevmousey!=my)) {
330			int posx, posy;
331
332			/* Give mouse position relative to window position */
333			posx = mx - x2;
334			if (posx<0) posx = 0;
335			if (posx>w2) posx = w2-1;
336			posy = my - y2;
337			if (posy<0) posy = 0;
338			if (posy>h2) posy = h2-1;
339
340			SDL_PrivateMouseMotion(0, 0, posx, posy);
341		}
342		prevmousex = mx;
343		prevmousey = my;
344	}
345
346	/* Mouse button ? */
347	if (prevmouseb!=mb) {
348		int i;
349
350		for (i=0;i<3;i++) {
351			int curbutton, prevbutton;
352
353			curbutton = mb & (1<<i);
354			prevbutton = prevmouseb & (1<<i);
355
356			if (curbutton && !prevbutton) {
357				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
358			}
359			if (!curbutton && prevbutton) {
360				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
361			}
362		}
363		prevmouseb = mb;
364	}
365
366	/* Read special keys */
367	if (ks & K_RSHIFT)
368		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
369	if (ks & K_LSHIFT)
370		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
371	if (ks & K_CTRL)
372		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
373	if (ks & K_ALT)
374		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
375}
376