aboutsummaryrefslogtreecommitdiff
path: root/backends/PalmOS/Src/palmevt.cpp
blob: 98020cba35614b39c0865ffcf7ed6551e47971a8 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001-2005 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 "common/stdafx.h"
#include "palm.h"
#include "common/scaler.h"

#include <PalmNavigator.h>

#ifndef DISABLE_TAPWAVE
#include <TwChars.h>
#endif

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

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

	if (_lastKeyPressed != kLastKeyNone) {
		_lastKeyRepeat++;

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

	slow = (iHoriz && iVert) ? 2 : 1;

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

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

	event.type = 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::pollEvent(Event &event) {
	::EventType ev;
	Boolean handled;
	UInt32 keyCurrentState;
	Coord x, y;

	battery_handler();
	sound_handler();
	timer_handler(getMillis());

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

		// check for hardkey repeat for mouse emulation
		keyCurrentState = KeyCurrentState();

		// arrow keys emulation
		if ((keyCurrentState & _keyMask)) {
			Int8 sx = 0;
			Int8 sy = 0;

			if (keyCurrentState & _keyMouse.bitButLeft) {
				event.type = EVENT_LBUTTONDOWN;
				event.mouse.x = _mouseCurState.x;
				event.mouse.y = _mouseCurState.y;
				_lastKeyPressed = kLastKeyNone;
				return true;
			}

			if (keyCurrentState & _keyMouse.bitUp)
				sy = -1;
			else if (keyCurrentState & _keyMouse.bitDown)
				sy = +1;

			if (keyCurrentState & _keyMouse.bitLeft)
				sx = -1;
			else if (keyCurrentState & _keyMouse.bitRight)
				sx = +1;

			SimulateArrowKeys(event, sx, sy);
			updateScreen();
			updateCD();
			_lastKeyPressed = kLastKeyMouse;
			return true;

		} else if (_lastKeyPressed != kLastKeyCalc) {
			_lastKeyPressed = kLastKeyNone;
		}

		if (ev.eType == nilEvent) {
			// force CD update, useful when the game is paused in some cases
			updateCD();
			return false;
		}

		if (ev.eType == keyDownEvent) {
			switch (ev.data.keyDown.chr) {

				// ESC key
				case vchrLaunch:
					_lastKeyPressed = kLastKeyNone;
					event.type = EVENT_KEYDOWN;
					event.kbd.keycode = 27;
					event.kbd.ascii = 27;
					event.kbd.flags = 0;
					return true;

				// F5 = menu
				case vchrJogPushRepeat:
				case vchrMenu:
				case vchrThumbWheelBack:	// Tapwave back button
					_lastKeyPressed = kLastKeyNone;
					event.type = EVENT_KEYDOWN;
					event.kbd.keycode = 319;
					event.kbd.ascii = 319;
					event.kbd.flags = 0;
					return true;

				case vchrCalc:
					if (_lastKeyPressed & kLastKeyCalc)
						if ((getMillis() - _exit_delay) <= (EXITDELAY))
							event.type = EVENT_QUIT;

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

				// mouse button
				case vchrJogBack:
				case vchrHard4: // right button
#ifndef DISABLE_TAPWAVE
				case vchrActionRight:
#endif
					event.type = 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.type = EVENT_WHEELUP;
					return true;

				case vchrJogDown:
					event.type = EVENT_WHEELDOWN;
					return true;


				// if hotsync pressed, etc...
				case vchrHardCradle:
				case vchrHardCradle2:
				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.type = EVENT_KEYDOWN;
						event.kbd.keycode = 27;
						event.kbd.ascii = 27;
						event.kbd.flags = 0;
						return true;

					// F5 = menu
					case vchrHard3:
						_lastKeyPressed = kLastKeyNone;
						event.type = EVENT_KEYDOWN;
						event.kbd.keycode = 319;
						event.kbd.ascii = 319;
						event.kbd.flags = 0;
						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.type = EVENT_QUIT;
						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
					// Zodiac only keys
					} else if (keycode == vchrTriggerRight) {
						setFeatureState(kFeatureAspectRatioCorrection, 0);
						return true;

					} else if (keycode == vchrTriggerLeft && _screenWidth != 640) {
						if (_initMode == GFX_WIDE)
							hotswap_gfx_mode(_mode == GFX_WIDE ? GFX_NORMAL: GFX_WIDE);
						else
							setFeatureState(kFeatureFullscreenMode, !_fullscreen);
#endif
					}

					event.type = 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.type = 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;
				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.type = 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.type = 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.type = 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;
		}
	}
}