aboutsummaryrefslogtreecommitdiff
path: root/backends/PalmOS/Src/palmevt.cpp
blob: eabf5f39bcb838e84c81a5bda6bec3209a140571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001-2004 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program 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 General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "stdafx.h"
#include "palm.h"
#include "common/scaler.h"

#include <PalmNavigator.h>

#ifndef DISABLE_TAPWAVE
// Tapwave code will come here
#endif

#define EXITDELAY (500) // delay to exit : calc button : double tap 1/500 sec

void OSystem_PALMOS::SimulateArrowKeys(Event *event, Int8 iHoriz, Int8 iVert, Boolean repeat) {
	Int16 x = _mouseCurState.x;
	Int16 y = _mouseCurState.y;

	if (_lastKeyPressed != kLastKeyNone || repeat) {
		_lastKeyRepeat += 2;

		if (_lastKeyRepeat > 32)
			_lastKeyRepeat = 32;
	}
	else
		_lastKeyRepeat = 0;

	x = x + iHoriz * (_lastKeyRepeat >> 1);
	y = y + iVert * (_lastKeyRepeat >> 1);

	x = (x < 0				) ? 0					: x;
	x = (x >= _screenWidth	) ? _screenWidth - 1	: x;
	y = (y < 0				) ? 0					: y;
	y = (y >= _screenHeight	) ? _screenHeight - 1	: y;

	event->event_code = EVENT_MOUSEMOVE;
	event->mouse.x = x;
	event->mouse.y = y;
	set_mouse_pos(x, y);
}

void OSystem_PALMOS::getCoordinates(EventPtr event, Coord *x, Coord *y) {
	if (OPTIONS_TST(kOptModeHiDensity)) {
		Boolean dummy;
		EvtGetPenNative(WinGetDisplayWindow(), &event->screenX, &event->screenY, &dummy);
	} else {
		event->screenX <<= 1;
		event->screenY <<= 1;
	}

	if (_mode == GFX_WIDE) {
		// wide landscape
		if (OPTIONS_TST(kOptModeLandscape)) {

			// zodiac have mutliple ratio 
			if (OPTIONS_TST(kOptDeviceZodiac)) {
				Int32 w,h;

				h = (_adjustAspectRatio ? ((_screenHeight == 200) ? 300 : 360) : 320);
				w = gVars->screenFullWidth;

				*x = ((event->screenX - _screenOffset.x) << 8) / ((w << 8) / _screenWidth);
				*y = ((event->screenY - _screenOffset.y) << 8) / ((h << 8) / _screenHeight);

			// default (NR, NZ, NX, UX50, TT3, ...), wide only for 320x200
			} else {
				*x = ((event->screenX - _screenOffset.x) << 1) / 3;
				*y = ((event->screenY - _screenOffset.y) << 1) / 3;
			}

		// wide portrait, only for 320x200
		} else {
			*y =       ((event->screenX - _screenOffset.y) << 1) / 3;
			*x = 320 - ((event->screenY - _screenOffset.x) << 1) / 3 - 1;	
		}

	// normal coord
	} else {
		*x = (event->screenX - _screenOffset.x);
		*y = (event->screenY - _screenOffset.y);
	}
}

bool OSystem_PALMOS::poll_event(Event *event) {
	EventType ev;
	Boolean handled;
	UInt32 keyCurrentState;
	Coord x, y;

	if(_quitCount) {
		if (_quitCount >= 10)
			SysReset();
		else
			_quitCount++;
	
		event->event_code = EVENT_QUIT;
		exit(0);	// resend an exit event
		return false;
	}

	// battery status
	battery_handler();
	
	// sound handler
	sound_handler();

	// timer handler
	timer_handler(get_msecs());

	if (_selfQuit)
		quit();

	for(;;) {
		EvtGetEvent(&ev, evtNoWait);

		// check for hardkey repeat for mouse emulation (no 5way only)
		keyCurrentState = KeyCurrentState();
		if (_lastKeyPressed != kLastKeyNone && _lastKeyPressed != kLastKeyCalc &&
			!(	(keyCurrentState & keyBitHard2) ||		// left
				(keyCurrentState & keyBitPageUp) ||		// up
				(keyCurrentState & keyBitPageDown) ||	// down
				(keyCurrentState & keyBitHard3)			// right
				)
			) {
					_lastKeyPressed = kLastKeyNone;
		}

		if (ev.eType == nilEvent)
			return false;

		if (ev.eType == keyDownEvent) {
			switch (ev.data.keyDown.chr) {
			
				// ESC key
				case vchrLaunch:
					_lastKeyPressed = kLastKeyNone;
					event->event_code = EVENT_KEYDOWN;
					event->kbd.keycode = 27;
					event->kbd.ascii = 27;
					event->kbd.flags = 0;
					return true;
				
				// F5 = menu
				case vchrJogPushRepeat:
				case vchrMenu:
					_lastKeyPressed = kLastKeyNone;
					event->event_code = EVENT_KEYDOWN;
					event->kbd.keycode = 319;
					event->kbd.ascii = 319;
					event->kbd.flags = 0;
					return true;

				case vchrCalc:
					if (_lastKeyPressed & kLastKeyCalc)
						if ((get_msecs() - _exit_delay) <= (EXITDELAY)) {
							event->event_code = EVENT_QUIT;
							_selfQuit = true;
						}

					_exit_delay = get_msecs();
					_lastKeyPressed = kLastKeyCalc;
					return true;

				// mouse button
				case vchrJogBack:
				case vchrHard4: // right button
					event->event_code = EVENT_RBUTTONDOWN;
					event->mouse.x = _mouseCurState.x;
					event->mouse.y = _mouseCurState.y;
					_lastKeyPressed = kLastKeyNone;
					return true;

				case vchrJogPushedUp:
				case vchrJogPushedDown: // hot swap gfx mode
					if (_initMode == GFX_WIDE)
						hotswap_gfx_mode(_mode == GFX_WIDE ? GFX_NORMAL: GFX_WIDE);
					else
						setFeatureState(kFeatureFullscreenMode, !_fullscreen);
					return true;

				// wheel
				case vchrJogUp:
					event->event_code = EVENT_WHEELUP;
					return true;

				case vchrJogDown:
					event->event_code = EVENT_WHEELDOWN;
					return true;


				// if hotsync pressed
				case vchrHardCradle:
				case vchrHardCradle2:
					_selfQuit = true;
					break;

				case vchrLowBattery:
				case vchrFind:
				case vchrBrightness:
				case vchrContrast:
					// do nothing
					return true;

				// trun off
				case vchrAutoOff:
				case vchrPowerOff:
					// pause the sound thread if any
					if (OPTIONS_TST(kOptPalmSoundAPI) && _sound.active)
						SndStreamPause(*((SndStreamRef *)_sound.handle), true);
					break;
				
				case vchrLateWakeup:
					// resume the sound thread if any
					if (OPTIONS_TST(kOptPalmSoundAPI) && _sound.active)
						SndStreamPause(*((SndStreamRef *)_sound.handle), false);
					break;
			}
			
			if (OPTIONS_TST(kOpt5WayNavigator)) {
				// mouse emulation for device with 5-Way navigator
				switch (ev.data.keyDown.chr) {
					// hot swap gfx
					case vchrHard1:
						if (_initMode == GFX_WIDE)
							hotswap_gfx_mode(_mode == GFX_WIDE ? GFX_NORMAL: GFX_WIDE);
						else
							setFeatureState(kFeatureFullscreenMode, !_fullscreen);
						return true;

					// ESC key
					case vchrHard2:
						_lastKeyPressed = kLastKeyNone;
						event->event_code = EVENT_KEYDOWN;
						event->kbd.keycode = 27;
						event->kbd.ascii = 27;
						event->kbd.flags = 0;
						return true;
					
					// F5 = menu
					case vchrHard3:
						_lastKeyPressed = kLastKeyNone;
						event->event_code = EVENT_KEYDOWN;
						event->kbd.keycode = 319;
						event->kbd.ascii = 319;
						event->kbd.flags = 0;
						return true;
					
					case vchrPageUp:
					case vchrPageDown:
					case vchrNavChange:
						// left mouse button
						if (ev.data.keyDown.keyCode & (navBitSelect|navChangeSelect)) {
							event->event_code = EVENT_LBUTTONDOWN;
							event->mouse.x = _mouseCurState.x;
							event->mouse.y = _mouseCurState.y;
							_lastKeyPressed = kLastKeyNone;
							return true;						

						// mouse move
						} else {
							Boolean kUp		= (ev.data.keyDown.keyCode & navBitUp);
							Boolean kDown	= (ev.data.keyDown.keyCode & navBitDown);
							Boolean kLeft	= (ev.data.keyDown.keyCode & navBitLeft);
							Boolean kRight	= (ev.data.keyDown.keyCode & navBitRight);
							Boolean kRepeat	= (ev.data.keyDown.modifiers & autoRepeatKeyMask);
							Boolean process	= (kUp || kDown || kLeft || kRight);
							
							if (process) {
								Int8 stepX = 0;
								Int8 stepY = 0;

								if (kLeft)
									stepX = -1;
								else if (kRight)
									stepX = +1;
									
								if (kUp)
									stepY = -1;
								else if (kDown)
									stepY = +1;
								
								SimulateArrowKeys(event, stepX, stepY, kRepeat);
								_lastKeyPressed = kLastKeyNone;	// use only repeat flag
								return true;
							}
						}
						break;
				}

			} else {
				// mouse emulation for device without 5-Way navigator
				switch (ev.data.keyDown.chr) {
					case vchrHard1: // left button
						event->event_code = EVENT_LBUTTONDOWN;
						event->mouse.x = _mouseCurState.x;
						event->mouse.y = _mouseCurState.y;
						_lastKeyPressed = kLastKeyNone;
						return true;

					case vchrHard2:	// move left
						SimulateArrowKeys(event, -1, 0);
						_lastKeyPressed = kLastKeyMouseLeft;
						return true;
						
					case vchrPageUp: // move up
						SimulateArrowKeys(event, 0, -1);
						_lastKeyPressed = kLastKeyMouseUp;
						return true;

					case vchrPageDown: // move down
						SimulateArrowKeys(event, 0, 1);
						_lastKeyPressed = kLastKeyMouseDown;
						return true;

					case vchrHard3: // move right
						SimulateArrowKeys(event, 1, 0);
						_lastKeyPressed = kLastKeyMouseRight;
						return true;
				}
			}
		}

		// prevent crash when alarm is raised
		handled = ((ev.eType == keyDownEvent) && 
						(ev.data.keyDown.modifiers & commandKeyMask) && 
						((ev.data.keyDown.chr == vchrAttnStateChanged) || 
						(ev.data.keyDown.chr == vchrAttnUnsnooze))); 

		// graffiti strokes, auto-off, etc...
		if (!handled)
			if (SysHandleEvent(&ev))
				continue;

		// others events
		switch(ev.eType) {

		case keyDownEvent: {
				_lastEvent = keyDownEvent;
				_lastKeyPressed = kLastKeyNone;

				if (ev.data.keyDown.chr == vchrCommand && (ev.data.keyDown.modifiers & commandKeyMask)) {
					_lastKeyModifier++;
					_lastKeyModifier %= 4;
					
					if (_lastKeyModifier)
						draw1BitGfx((kDrawKeyState + _lastKeyModifier - 1), 2, _screenHeight + 2, true);
					else
						draw1BitGfx(kDrawKeyState, 2, _screenHeight + 2, false);

				} else {
					int keycode;
					byte b = 0;

					if (_lastKeyModifier == MD_CMD)  b = KBD_CTRL|KBD_ALT;
					if (_lastKeyModifier == MD_ALT)  b = KBD_ALT;
					if (_lastKeyModifier == MD_CTRL) b = KBD_CTRL;
					
					keycode = ev.data.keyDown.chr;
					
					// F1 -> F10 key
					if  (keycode >= '0' && keycode <= '9' && b == (KBD_CTRL|KBD_ALT)) {
						keycode = keycode == '0' ? 324 : (315 + keycode - '1');
						b = 0;
					
					} else if  ((keycode == 'z' && b == KBD_CTRL) || (b == KBD_ALT && keycode == 'x')) {
						event->event_code = EVENT_QUIT;
						_selfQuit = true;
						return true;

					} else if (keycode == 'n' && b == KBD_CTRL) {
						UInt8 *scr = _screenP + _screenWidth * (_screenHeight + 2);
						_useNumPad = !_useNumPad;
						draw1BitGfx(kDrawNumPad, (_screenWidth >> 1) - 32, _screenHeight + 2, _useNumPad);
						return true;

#ifndef DISABLE_TAPWAVE
// Tapwave code will come here
#endif
					}
					
					event->event_code = EVENT_KEYDOWN;
					event->kbd.keycode = keycode;
					event->kbd.ascii = keycode; //(keycode>='a' && keycode<='z' && (event->kbd.flags & KBD_SHIFT) ? keycode &~ 0x20 : keycode);
					event->kbd.flags = b;
					
					if (_lastKeyModifier) {
						_lastKeyModifier = MD_NONE;
						draw1BitGfx(kDrawKeyState, 2, getHeight() + 2, false);
					}
				}
				return true;
			}

		case penMoveEvent:
			getCoordinates(&ev, &x, &y);

			if (y > _screenHeight || y < 0 || x > _screenWidth || x < 0)
				return true;

			if (_lastEvent != penMoveEvent && (abs(y - event->mouse.y) <= 2 || abs(x - event->mouse.x) <= 2)) // move only if
				return true;

			_lastEvent = penMoveEvent;
			event->event_code = EVENT_MOUSEMOVE;
			event->mouse.x = x;
			event->mouse.y = y;
			set_mouse_pos(event->mouse.x, event->mouse.y);
			return true;

		case penDownEvent:
			getCoordinates(&ev, &x, &y);

			if (_useNumPad) {
				Coord x2 = (_screenWidth >> 1) - 20; // - 64 / 2 + 12
				Coord y2 = _screenHeight + 2;

				if (y >= y2 && y < (y2 + 34) && x >= x2 && x < (x2 + 40)) {	// numpad location
					UInt8 key = '1';
					key += 9 - ( (3 - ((x - x2) / 13)) + (3 * ((y - y2) / 11)) );
					
					_lastEvent = keyDownEvent;
					_lastKeyPressed = kLastKeyNone;
					
					event->event_code = EVENT_KEYDOWN;
					event->kbd.keycode = key;
					event->kbd.ascii = key;
					event->kbd.flags = 0;
					return true;
				}
			}

			_lastEvent = penDownEvent;
			if (y > _screenHeight || y < 0 || x > _screenWidth || x < 0)
				return true;

			event->event_code = EVENT_LBUTTONDOWN;
			event->mouse.x = x;
			event->mouse.y = y;
			set_mouse_pos(event->mouse.x, event->mouse.y);
			return true;

		case penUpEvent:
			getCoordinates(&ev, &x, &y);
			event->event_code = EVENT_LBUTTONUP;

			if (y > _screenHeight || y < 0 || x > _screenWidth || x < 0)
				return true;

			event->mouse.x = x;
			event->mouse.y = y;
			set_mouse_pos(event->mouse.x, event->mouse.y);
			return true;

		default:
			return false;
		}
	}
}