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