/
SDL_gemevents.c
457 lines (387 loc) · 11.2 KB
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
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
26
/*
* GEM SDL video driver implementation
* inspired from the Dummy SDL driver
27
*
28
29
30
31
32
33
34
* Patrice Mandin
* and work from
* Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
*/
#include <gem.h>
35
#include "SDL_timer.h"
36
37
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"
38
39
#include "SDL_gemvideo.h"
#include "SDL_gemevents_c.h"
40
#include "SDL_gemmouse_c.h"
41
42
43
#include "../ataricommon/SDL_atarikeys.h" /* for keyboard scancodes */
#include "../ataricommon/SDL_atarievents_c.h"
#include "../ataricommon/SDL_xbiosevents_c.h"
44
#include "../ataricommon/SDL_ataridevmouse_c.h"
45
46
47
48
49
/* Duration after which we consider key released */
#define KEY_PRESS_DURATION 100
50
51
#define MSG_SDL_ID (('S'<<8)|'D')
52
53
54
55
/* Variables */
static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
56
static Uint32 keyboard_ticks[ATARIBIOS_MAXKEYS];
57
58
static short prevmx=0,prevmy=0,prevmb=0;
59
static short dummy_msgbuf[8] = {MSG_SDL_ID,0,0,0, 0,0,0,0};
60
61
62
/* Functions prototypes */
63
static int do_messages(_THIS, short *message, short latest_msg_id);
64
65
static void do_keyboard(short kc, Uint32 tick);
static void do_keyboard_special(short ks, Uint32 tick);
66
67
static void do_mouse_motion(_THIS, short mx, short my);
static void do_mouse_buttons(_THIS, short mb);
68
static int mouse_in_work_area(int winhandle, short mx, short my);
69
static void clearKeyboardState(Uint32 tick);
70
71
72
73
74
/* Functions */
void GEM_InitOSKeymap(_THIS)
{
75
76
SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
77
SDL_memset(keyboard_ticks, 0, sizeof(keyboard_ticks));
78
79
80
/* Mouse init */
GEM_mouse_relative = SDL_FALSE;
81
82
SDL_Atari_InitInternalKeymap(this);
83
84
85
86
}
void GEM_PumpEvents(_THIS)
{
87
short prevkc=0, mousex, mousey, mouseb, kstate;
88
int i, quit = 0;
89
SDL_keysym keysym;
90
Uint32 cur_tick;
91
92
static Uint32 prev_now = 0, prev_msg = 0;
static short latest_msg_id = 0;
93
94
cur_tick = SDL_GetTicks();
95
96
97
98
99
100
if (prev_now == cur_tick) {
return;
}
prev_now = cur_tick;
SDL_AtariMint_BackgroundTasks();
101
clearKeyboardState(cur_tick);
102
103
104
105
106
107
108
109
if (prev_msg) {
/* Wait at least 20ms before each event processing loop */
if (cur_tick-prev_msg < 20) {
return;
}
}
prev_msg = cur_tick;
110
111
112
113
114
115
dummy_msgbuf[1] = ++latest_msg_id;
if (appl_write(GEM_ap_id, sizeof(dummy_msgbuf), dummy_msgbuf) == 0) {
/* If it fails, wait for previous id */
--latest_msg_id;
}
116
117
118
119
while (!quit) {
int resultat;
short buffer[8], kc, dummy;
120
121
resultat = evnt_multi(
122
123
124
MU_MESAG|MU_TIMER|MU_KEYBD,
0,0,0,
0,0,0,0,0,
125
0,0,0,0,0,
126
buffer,
127
1000,
128
&dummy,&dummy,&dummy,&kstate,&kc,&dummy
129
130
131
132
);
/* Message event ? */
if (resultat & MU_MESAG)
133
quit = do_messages(this, buffer, latest_msg_id);
134
135
/* Keyboard event ? */
136
if (resultat & MU_KEYBD) {
137
do_keyboard_special(kstate, cur_tick);
138
if (prevkc != kc) {
139
do_keyboard(kc, cur_tick);
140
prevkc = kc;
141
142
}
}
143
144
145
146
147
/* Timer event ? Just used as a safeguard */
if (resultat & MU_TIMER) {
quit = 1;
}
148
149
}
150
151
GEM_CheckMouseMode(this);
152
153
/* Update mouse state */
graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
154
do_keyboard_special(kstate, cur_tick);
155
156
157
do_mouse_motion(this, mousex, mousey);
do_mouse_buttons(this, mouseb);
158
/* Now generate keyboard events */
159
160
161
for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
/* Key pressed ? */
if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
162
SDL_PrivateKeyboard(SDL_PRESSED,
163
SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
164
165
166
/* Key unpressed ? */
if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
167
SDL_PrivateKeyboard(SDL_RELEASED,
168
SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
169
170
}
171
SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
172
173
174
/* Refresh window name ? */
if (GEM_refresh_name) {
175
176
177
178
179
180
181
182
const char *window_name =
(SDL_GetAppState() & SDL_APPACTIVE)
? GEM_title_name : GEM_icon_name;
if (window_name) {
wind_set(GEM_handle,WF_NAME,
(short)(((unsigned long)window_name)>>16),
(short)(((unsigned long)window_name) & 0xffff),
0,0);
183
184
185
}
GEM_refresh_name = SDL_FALSE;
}
186
187
}
188
static int do_messages(_THIS, short *message, short latest_msg_id)
189
{
190
int quit, update_work_area, iconified, sdl_resize;
191
192
quit = update_work_area = iconified = sdl_resize = 0;
193
switch (message[0]) {
194
195
196
case MSG_SDL_ID:
quit=(message[1] == latest_msg_id);
break;
197
case WM_CLOSED:
198
case AP_TERM:
199
SDL_PrivateQuit();
200
201
202
203
quit=1;
break;
case WM_MOVED:
wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
204
update_work_area = 1;
205
206
207
break;
case WM_TOPPED:
wind_set(message[3],WF_TOP,message[4],0,0,0);
208
209
/* Continue with TOP event processing */
case WM_ONTOP:
210
SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
211
212
213
if (VDI_setpalette) {
VDI_setpalette(this, VDI_curpalette);
}
214
215
break;
case WM_REDRAW:
216
217
218
if (!GEM_lock_redraw) {
GEM_wind_redraw(this, message[3],&message[4]);
}
219
220
221
break;
case WM_ICONIFY:
case WM_ALLICONIFY:
222
wind_set (message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
223
224
225
/* If we're active, make ourselves inactive */
if ( SDL_GetAppState() & SDL_APPACTIVE ) {
/* Send an internal deactivate event */
226
SDL_PrivateAppActive(0, SDL_APPACTIVE);
227
}
228
229
/* Update window title */
if (GEM_refresh_name && GEM_icon_name) {
230
231
232
233
wind_set(GEM_handle,WF_NAME,
(short)(((unsigned long)GEM_icon_name)>>16),
(short)(((unsigned long)GEM_icon_name) & 0xffff),
0,0);
234
235
GEM_refresh_name = SDL_FALSE;
}
236
237
GEM_iconified = SDL_TRUE;
update_work_area = 1;
238
239
break;
case WM_UNICONIFY:
240
wind_set (message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
241
242
243
244
245
/* If we're not active, make ourselves active */
if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
/* Send an internal activate event */
SDL_PrivateAppActive(1, SDL_APPACTIVE);
}
246
if (GEM_refresh_name && GEM_title_name) {
247
248
249
250
wind_set(GEM_handle,WF_NAME,
(short)(((unsigned long)GEM_title_name)>>16),
(short)(((unsigned long)GEM_title_name) & 0xffff),
0,0);
251
252
GEM_refresh_name = SDL_FALSE;
}
253
GEM_iconified = SDL_FALSE;
254
update_work_area = 1;
255
256
257
break;
case WM_SIZED:
wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
258
update_work_area = sdl_resize = 1;
259
260
GEM_win_fulled = SDL_FALSE; /* Cancel maximized flag */
GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers resized */
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
break;
case WM_FULLED:
{
short x,y,w,h;
if (GEM_win_fulled) {
wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
GEM_win_fulled = SDL_FALSE;
} else {
x = GEM_desk_x;
y = GEM_desk_y;
w = GEM_desk_w;
h = GEM_desk_h;
GEM_win_fulled = SDL_TRUE;
}
wind_set (message[3], WF_CURRXYWH, x, y, w, h);
277
update_work_area = sdl_resize = 1;
278
GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers resized */
279
280
281
}
break;
case WM_BOTTOMED:
282
283
wind_set(message[3],WF_BOTTOM,0,0,0,0);
/* Continue with BOTTOM event processing */
284
285
case WM_UNTOPPED:
SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
286
287
288
if (VDI_setpalette) {
VDI_setpalette(this, VDI_oldpalette);
}
289
290
break;
}
291
292
if (update_work_area) {
293
GEM_align_work_area(this, message[3], 1);
294
295
296
297
298
299
if (sdl_resize) {
SDL_PrivateResize(GEM_work_w, GEM_work_h);
}
}
300
301
302
return quit;
}
303
static void do_keyboard(short kc, Uint32 tick)
304
{
305
int scancode;
306
307
if (kc) {
308
scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
309
gem_currentkeyboard[scancode]=0xFF;
310
keyboard_ticks[scancode]=tick;
311
}
312
}
313
314
static void do_keyboard_special(short ks, Uint32 tick)
315
{
316
317
int scancode=0;
318
319
/* Read special keys */
if (ks & K_RSHIFT)
320
scancode=SCANCODE_RIGHTSHIFT;
321
if (ks & K_LSHIFT)
322
scancode=SCANCODE_LEFTSHIFT;
323
if (ks & K_CTRL)
324
scancode=SCANCODE_LEFTCONTROL;
325
if (ks & K_ALT)
326
327
328
329
330
331
scancode=SCANCODE_LEFTALT;
if (scancode) {
gem_currentkeyboard[scancode]=0xFF;
keyboard_ticks[scancode]=tick;
}
332
333
}
334
static void do_mouse_motion(_THIS, short mx, short my)
335
{
336
337
short x2, y2, w2, h2;
338
339
340
341
342
343
344
345
346
347
if (this->input_grab == SDL_GRAB_OFF) {
/* Switch mouse focus state */
if (!GEM_fullscreen && (GEM_handle>=0)) {
SDL_PrivateAppActive(
mouse_in_work_area(GEM_handle, mx,my),
SDL_APPMOUSEFOCUS);
}
}
GEM_CheckMouseMode(this);
348
349
350
351
352
/* Don't return mouse events if out of window */
if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
return;
}
353
354
355
356
357
358
359
360
361
362
/* Relative mouse motion ? */
if (GEM_mouse_relative) {
if (GEM_usedevmouse) {
SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
} else {
SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
}
return;
}
363
364
/* Retrieve window coords, and generate mouse events accordingly */
x2 = y2 = 0;
365
366
w2 = VDI_w;
h2 = VDI_h;
367
if ((!GEM_fullscreen) && (GEM_handle>=0)) {
368
369
370
371
x2 = GEM_work_x;
y2 = GEM_work_y;
w2 = GEM_work_w;
h2 = GEM_work_h;
372
}
373
374
if ((prevmx!=mx) || (prevmy!=my)) {
375
376
377
378
379
380
381
382
383
384
385
int posx, posy;
/* Give mouse position relative to window position */
posx = mx - x2;
if (posx<0) posx = 0;
if (posx>w2) posx = w2-1;
posy = my - y2;
if (posy<0) posy = 0;
if (posy>h2) posy = h2-1;
SDL_PrivateMouseMotion(0, 0, posx, posy);
386
}
387
388
389
prevmx = mx;
prevmy = my;
390
}
391
392
393
394
static void do_mouse_buttons(_THIS, short mb)
{
int i;
395
396
397
398
399
/* Don't return mouse events if out of window */
if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
return;
400
if (prevmb==mb)
401
402
403
404
return;
for (i=0;i<3;i++) {
int curbutton, prevbutton;
405
406
curbutton = mb & (1<<i);
407
prevbutton = prevmb & (1<<i);
408
409
410
411
412
413
if (curbutton && !prevbutton) {
SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
}
if (!curbutton && prevbutton) {
SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
414
415
}
}
416
417
prevmb = mb;
418
}
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* Check if mouse in visible area of the window */
static int mouse_in_work_area(int winhandle, short mx, short my)
{
short todo[4];
short inside[4] = {mx,my,1,1};
/* Browse the rectangle list */
if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
while (todo[2] && todo[3]) {
if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
return 1;
}
if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
break;
}
}
}
return 0;
}
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* Clear key state for which we did not receive events for a while */
static void clearKeyboardState(Uint32 tick)
{
int i;
for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
if (keyboard_ticks[i]) {
if (tick-keyboard_ticks[i] > KEY_PRESS_DURATION) {
gem_currentkeyboard[i]=0;
keyboard_ticks[i]=0;
}
}
}
}