aboutsummaryrefslogtreecommitdiff
path: root/backends/PalmOS/Src/palm.cpp
blob: c2fe4c7f12c7a9cb96c6c1e45ec9731ad8c34277 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001-2003 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 "scumm.h"
#include "mididrv.h"
#include "gameDetector.h"
#include "common/scaler.h"
//##############################################################################
#include "palm.h"
#include "starterrsc.h"
#include "pa1lib.h"
#include "sonychars.h"
//extern UInt8 _indicatorColorOn,_indicatorColorOff;

#define SAVEDELAY	(5 * 60 * 1000) // five minutes
//#define EXITDELAY	(2 * 1000)		// delay to exit : calc button
#define EXITDELAY	(100)		// delay to exit : calc button : double tap 1/500 sec

//OSystem_PALMOS *g_palm = NULL;
//##############################################################################
//##############################################################################
//-- 02-12-17 --////////////////////////////////////////////////////////////////
/*void OSystem_PALMOS::autosave()
{
	g_scumm->_doAutosave = true;
}*/
//-- 02-12-17 --////////////////////////////////////////////////////////////////
OSystem *OSystem_PALMOS::create(UInt16 gfx_mode) {

	OSystem_PALMOS *syst = new OSystem_PALMOS();
	syst->_mode = gfx_mode;
	syst->_vibrate = gVars->vibrator;
//	g_palm = syst;
	return syst;
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
OSystem *OSystem_PALMOS_create(int gfx_mode) {
	return OSystem_PALMOS::create(gfx_mode);
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::set_palette(const byte *colors, uint start, uint num) {
	const byte *b = colors;
	uint i;
	RGBColorType *base = _currentPalette + start;
	for(i=0; i < num; i++) {
		base[i].r = b[0];
		base[i].g = b[1];
		base[i].b = b[2];
		b += 4;
	}

	if (start < _paletteDirtyStart)
		_paletteDirtyStart = start;

	if (start + num > _paletteDirtyEnd)
		_paletteDirtyEnd = start + num;
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::load_gfx_mode() {
	Err e;
	const byte startupPalette[] = {
		0	,0	,0	,0,
		0	,0	,171,0,
		0	,171, 0	,0,
		0	,171,171,0,
		171	,0	,0	,0, 
		171	,0	,171,0,
		171	,87	,0	,0,
		171	,171,171,0,
		87	,87	,87	,0,
		87	,87	,255,0,
		87	,255,87	,0,
		87	,255,255,0,
		255	,87	,87	,0,
		255	,87	,255,0,
		255	,255,87	,0,
		255	,255,255,0
	};
	
	// palette for preload dialog
	set_palette(startupPalette,0,16);

	switch(_mode)
	{
		case GFX_FLIPPING:
			palm_offscreen	= WinScreenLock(winLockErase) + _screeny;
			palm_screen		= palm_offscreen;
			gVars->screenLocked = true;
			_renderer_proc = &update_screen__flipping;
			break;
		case GFX_DOUBLEBUFFER:
			h_palm_screen	= WinGetDisplayWindow();
			palm_screen		= (byte *)(BmpGetBits(WinGetBitmap(h_palm_screen))) + _screeny;
			h_palm_offscreen= WinCreateOffscreenWindow(SCREEN_WIDTH, SCREEN_HEIGHT, screenFormat, &e);
			palm_offscreen	= (byte *)(BmpGetBits(WinGetBitmap(h_palm_offscreen)));
			_renderer_proc = &update_screen__dbuffer;
			break;
		case GFX_NORMAL:
		default:
			h_palm_offscreen= WinGetDisplayWindow();
			palm_offscreen	= (byte *)(BmpGetBits(WinGetBitmap(h_palm_offscreen))) + _screeny;
			palm_screen		= palm_offscreen;
			_renderer_proc = &update_screen__direct;
			break;
	}

	h_palm_tmpscreen= WinCreateOffscreenWindow(SCREEN_WIDTH, SCREEN_HEIGHT, screenFormat, &e);
	palm_tmpscreen	= (byte *)(BmpGetBits(WinGetBitmap(h_palm_tmpscreen)));
	_overlaySaved = false;

}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::unload_gfx_mode() {
	switch (_mode)
	{
		case GFX_FLIPPING:
			WinScreenUnlock();
			break;
		case GFX_DOUBLEBUFFER:
			WinDeleteWindow(h_palm_offscreen,false);
			break;
	}

	WinDeleteWindow(h_palm_tmpscreen,false);
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::init_size(uint w, uint h) {

	SCREEN_WIDTH = w;
	SCREEN_HEIGHT = h;

	_overlay_visible = false;
	_quit = false;

	_decaly = (320-h)/2;
	_screeny= _decaly * 320;

	set_mouse_pos(200,150);

	_currentPalette = (RGBColorType*)calloc(sizeof(RGBColorType), 256);
	_mouse_backup = (byte*)malloc(MAX_MOUSE_W * MAX_MOUSE_H);
	
	load_gfx_mode();
}

//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::copy_rect(const byte *buf, int pitch, int x, int y, int w, int h) {

	byte *dst;

	/* FIXME: undraw mouse only if the draw rect intersects with the mouse rect */
	if (_mouse_drawn)
		undraw_mouse();

	dst = palm_offscreen + y * SCREEN_WIDTH + x;

	do {
		memcpy(dst, buf, w);
		dst += SCREEN_WIDTH;
		buf += pitch;
	} while (--h);
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::update_screen__flipping()
{
	RectangleType r;
	UInt8 *screen;
	UInt32 size = SCREEN_WIDTH*SCREEN_HEIGHT + 6400; // 10 pix top and bottom border
	Boolean shaked = false;
	UInt32 move = 0;

	// shake screen
	if (_current_shake_pos != _new_shake_pos) {
		if (gVars->HRrefNum) {
			RctSetRectangle(&r, 0, _decaly - _new_shake_pos, SCREEN_WIDTH, SCREEN_HEIGHT + (_new_shake_pos << 2));
			HRWinScrollRectangle(gVars->HRrefNum, &r, winDown, _new_shake_pos, NULL);
		} else {
			move = (_new_shake_pos * SCREEN_WIDTH);
			screen = palm_offscreen - 3200;
			MemMove(screen + move, screen, size);
		}

		if (_vibrate) {
			Boolean active = (_new_shake_pos >= 3);
			HwrVibrateAttributes(1, kHwrVibrateActive, &active);
		}

		_current_shake_pos = _new_shake_pos;
		shaked = true;
	}

	// update screen
	WinScreenUnlock();
	palm_offscreen = WinScreenLock(winLockCopy) + _screeny;
	palm_screen = palm_offscreen;
	if (shaked) {
		if (gVars->HRrefNum) {
			HRWinScrollRectangle(gVars->HRrefNum, &r, winUp, _new_shake_pos, NULL);
		} else {
			screen = palm_offscreen - 3200;
			MemMove(screen, screen + move, size);
		}
	}

}

void OSystem_PALMOS::update_screen__dbuffer()
{
	UInt32 move = 0;
	UInt32 size = SCREEN_WIDTH*SCREEN_HEIGHT;

	// shake screen
	if (_current_shake_pos != _new_shake_pos) {
		move = (_new_shake_pos * SCREEN_WIDTH);
		// copy clear bottom of the screen to top to cover shaking image
		MemMove(palm_screen, palm_screen + size , move);

		if (_vibrate) {
			Boolean active = (_new_shake_pos >= 3);
			HwrVibrateAttributes(1, kHwrVibrateActive, &active);
		}

		_current_shake_pos = _new_shake_pos;
	}
	// update screen
	MemMove(palm_screen + move, palm_offscreen, size - move);
}

void OSystem_PALMOS::update_screen__direct()
{
	if (_current_shake_pos != _new_shake_pos) {
		if (_vibrate) {
			Boolean active = (_new_shake_pos >= 3);
			HwrVibrateAttributes(1, kHwrVibrateActive, &active);
		}	
		_current_shake_pos = _new_shake_pos;
	}
}

void OSystem_PALMOS::update_screen() {
	if(_quit)
		return;

	// Make sure the mouse is drawn, if it should be drawn.
	draw_mouse();

	// Check whether the palette was changed in the meantime and update the
	// screen surface accordingly. 
	if (_paletteDirtyEnd != 0) {
			if (gVars->stdPalette) {
				WinSetDrawWindow(WinGetDisplayWindow());	// hack by Doug
				WinPalette(winPaletteSet, _paletteDirtyStart, _paletteDirtyEnd - _paletteDirtyStart,_currentPalette + _paletteDirtyStart);
			} else {
				HwrDisplayPalette(winPaletteSet, _paletteDirtyStart, _paletteDirtyEnd - _paletteDirtyStart,_currentPalette + _paletteDirtyStart);
			}

		_paletteDirtyEnd = 0;

//		_msg.color = RGBToColor(255,255,255);
		gVars->indicator.on = RGBToColor(0,255,0);
//		gVars->indicator.off= 0; //RGBToColor(0,0,0);
		if (lastKeyModifier)
			drawKeyState();
	}

	((this)->*(_renderer_proc))();
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
bool OSystem_PALMOS::show_mouse(bool visible) {
	if (_mouse_visible == visible)
		return visible;
	
	bool last = _mouse_visible;
	_mouse_visible = visible;

	if (visible)
		draw_mouse();
	else
		undraw_mouse();

	return last;
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::warp_mouse(int x, int y) {
}

void OSystem_PALMOS::set_mouse_pos(int x, int y) {
	if (x != _mouse_cur_state.x || y != _mouse_cur_state.y) {
		_mouse_cur_state.x = x;
		_mouse_cur_state.y = y;
		undraw_mouse();
	}
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::set_mouse_cursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y) {
	_mouse_cur_state.w = w;
	_mouse_cur_state.h = h;

	_mouse_hotspot_x = hotspot_x;
	_mouse_hotspot_y = hotspot_y;

	_mouse_data = (byte*)buf;

	undraw_mouse();
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::set_shake_pos(int shake_pos) {
	_new_shake_pos = shake_pos;
	
	if (shake_pos == 0 && _vibrate)
	{
		Boolean active = false;
		HwrVibrateAttributes(1, kHwrVibrateActive, &active);
	}
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
uint32 OSystem_PALMOS::get_msecs() {
	uint32 ticks = TimGetTicks();
	ticks *= (1000/SysTicksPerSecond());
	return ticks;

}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::delay_msecs(uint msecs) {
	SysTaskDelay((SysTicksPerSecond()*msecs)/1000);
}
//-- 02-12-17 --////////////////////////////////////////////////////////////////
void *OSystem_PALMOS::create_thread(ThreadProc *proc, void *param) {
	_thread.active	= true;
	_thread.proc	= proc;
	_thread.param	= param;
	
	return 0;
}
//-- 03-01-20 --////////////////////////////////////////////////////////////////
void OSystem_PALMOS::set_timer(int timer, int (*callback)(int))
{
	if (callback != NULL) {
		_timer.duration = timer;
		_timer.next_expiry = get_msecs() + timer;
		_timer.callback = callback;
		_timer.active = true;
	} else {
		_timer.active = false;
	}
}

/* Mutex handling */
void *OSystem_PALMOS::create_mutex(void)
{
  return NULL;
}

void OSystem_PALMOS::lock_mutex(void *mutex)
{
}
 
void OSystem_PALMOS::unlock_mutex(void *mutex)
{
}

void OSystem_PALMOS::delete_mutex(void *mutex)
{
}
////////////////////////////////////////////////////////////////////////////////
/*
static UInt32 GetOSFreeMem( UInt32* totalMemoryP, UInt32* dynamicMemoryP )
{
	#define		memoryBlockSize		(1024L)
	UInt32		heapFree;
	UInt32		max;
	Int16		i;
	Int16		nCards;
	UInt16		cardNo;
	UInt16		heapID;
	UInt32		freeMemory = 0;
	UInt32		totalMemory = 0;
	UInt32		dynamicMemory = 0;

	// Iterate through each card to support devices with multiple cards.
	nCards = MemNumCards();		
	for (cardNo = 0; cardNo < nCards; cardNo++)
	{
		// Iterate through the RAM heaps on a card (excludes ROM).
		for (i=0; i< MemNumRAMHeaps(cardNo); i++)
		{
			// Obtain the ID of the heap.
			heapID = MemHeapID(cardNo, i);
			
			// If the heap is dynamic, increment the dynamic memory total.
			if (MemHeapDynamic(heapID))
			{
				dynamicMemory += MemHeapSize(heapID);
			}
			
			// The heap is nondynamic.
			else
			{
				// Calculate the total memory and free memory of the heap.
				totalMemory += MemHeapSize(heapID);
				MemHeapFreeBytes(heapID, &heapFree, &max);
				freeMemory += heapFree;
			}
		}
	}
	
	// Reduce the stats to KB.  Round the results.
	freeMemory  = freeMemory / memoryBlockSize;
	totalMemory = totalMemory  / memoryBlockSize;
	dynamicMemory = dynamicMemory / memoryBlockSize;

	if (totalMemoryP)	*totalMemoryP = totalMemory;
	if (dynamicMemoryP)	*dynamicMemoryP = dynamicMemory;

	return (freeMemory);
}	// GetOSFreeMem

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

	if (repeat) {
		lastKeyRepeat += 100;
		
		if (lastKeyRepeat > 3200)
			lastKeyRepeat = 3200;
	}
	else
		lastKeyRepeat = 100;

	x = x + iHoriz * (lastKeyRepeat/100);
	y = y + iVert * (lastKeyRepeat/100);

	x = (x < 0				) ? 0				: x;
	x = (x >= SCREEN_WIDTH	) ? SCREEN_WIDTH-1	: x;
	y = (y < 0				) ? 0				: y;
	y = (y >= SCREEN_HEIGHT	) ? SCREEN_HEIGHT-1	: y;


	event->event_code = EVENT_MOUSEMOVE;
	event->mouse.x = x;
	event->mouse.y = y;
}

#define MD_NONE 0
#define MD_CTRL 1
#define MD_ALT	2

void OSystem_PALMOS::drawKeyState() {
	UInt8 i,j;
	UInt16 bmpID = 3000 + lastKeyModifier - 1;
	
	MemHandle hTemp;
	BitmapType *bmTemp;
	UInt32 *bmData;
	UInt8 *scr = palm_screen + SCREEN_WIDTH * (SCREEN_HEIGHT + 2) + 2;

	hTemp	= DmGetResource(bitmapRsc,bmpID);
	
	if (hTemp) {
		bmTemp	= (BitmapType *)MemHandleLock(hTemp);
		bmData	= (UInt32 *)BmpGetBits(bmTemp);

		for (i = 0; i < 7; i++) {
			for (j = 0; j < 32; j++) {
				if (*bmData & (1 << (31-j)))
					*scr++ = gVars->indicator.on;
				else
					*scr++ = gVars->indicator.off;
			}
			scr += SCREEN_WIDTH - 32;
			bmData++;
		}

		MemPtrUnlock(bmTemp);
		DmReleaseResource(hTemp);
	} else {
		for (i = 0; i < 7; i++) {
			for (j = 0; j < 32; j++) {
				*scr++ = gVars->indicator.off;
			}
			scr += SCREEN_WIDTH - 32;
		}
	}
}

bool OSystem_PALMOS::poll_event(Event *event) {
	EventType ev;
	Boolean handled;
//	UInt32 button = 0;
	uint32 current_msecs;
	UInt32 keyCurrentState = 0;
	Boolean funcButton = false;

	/* First, handle timers */
	for(;;) {
		EvtGetEvent(&ev, 0);
		
		keyCurrentState = KeyCurrentState();
		current_msecs = get_msecs();
	
		//thread handler
		if (_thread.active)
			_thread.proc(_thread.param);

		// sound handler
//		if(_sound.active)
//			check_sound();
		
//		if (_msg.state != 0)
//			drawMessage();

		// timer handler
		if (_timer.active && (current_msecs >= _timer.next_expiry)) {
			_timer.duration = _timer.callback(_timer.duration);
			_timer.next_expiry = current_msecs + _timer.duration;
		}

		if (ev.eType == keyDownEvent) {
			switch (ev.data.keyDown.chr) {
				case vchrLaunch:
					lastKeyPressed = -1;
					funcButton = true;
					event->event_code = EVENT_KEYDOWN;
					event->kbd.keycode = 27;
					event->kbd.ascii = 27;
					event->kbd.flags = 0;
					return true;
				
				case vchrMenu:
					lastKeyPressed = -1;
					funcButton = true;
					event->event_code = EVENT_KEYDOWN;
					event->kbd.keycode = 319;
					event->kbd.ascii = 319;
					event->kbd.flags = 0;
					return true;

				case vchrBrightness:
				case vchrContrast:
				case vchrFind:
					WinPalette(winPaletteSet, 0, 256, _currentPalette);
					break;

				case vchrCalc:
					if (lastKeyPressed == vchrCalc)
						if ((get_msecs() - _exit_delay) <= (EXITDELAY))
							quit();

					_exit_delay = get_msecs();
					funcButton = true;
					lastKeyPressed = vchrCalc;
					return true;

				// mouse emulation
				case vchrHard1: // left button
					event->event_code = EVENT_LBUTTONDOWN;
					event->mouse.x = _mouse_cur_state.x;
					event->mouse.y = _mouse_cur_state.y;
					lastKeyPressed = -1;
					return true;

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

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

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

				case vchrHard4: // right button
					event->event_code = EVENT_RBUTTONDOWN;
					event->mouse.x = _mouse_cur_state.x;
					event->mouse.y = _mouse_cur_state.y;
					lastKeyPressed = -1;
					return true;


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

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

				case vchrHardCradle:
					quit();
			}
		}
		// check for hardkey repeat
		if (lastKeyPressed != -1 && lastKeyPressed != vchrCalc &&
			!(	(keyCurrentState & keyBitHard2) ||
				(keyCurrentState & keyBitPageUp) ||
				(keyCurrentState & keyBitPageDown) ||
				(keyCurrentState & keyBitHard3)
				)
			) {
					lastKeyPressed = -1;
		}
		// 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, autooff, etc...
		if (!funcButton && !handled)
			if (SysHandleEvent(&ev))
				continue;

		// others events
		switch(ev.eType) {

		case keyDownEvent: {
				lastEvent = keyDownEvent;
				lastKeyPressed = -1;
				//if (ev.data.keyDown.modifiers & shiftKeyMask) b |= KBD_SHIFT;

				if (ev.data.keyDown.chr == 262 && (ev.data.keyDown.modifiers & commandKeyMask)) {
					lastKeyModifier++;
					lastKeyModifier %= 3;
					drawKeyState();				
				} else {
					byte b = 0;
					if (lastKeyModifier == MD_CTRL)	b = KBD_CTRL;
					if (lastKeyModifier == MD_ALT)	b = KBD_ALT;

					event->event_code = EVENT_KEYDOWN;
					event->kbd.keycode = ev.data.keyDown.chr;
					event->kbd.ascii = (ev.data.keyDown.chr>='a' && ev.data.keyDown.chr<='z' && (event->kbd.flags & KBD_SHIFT)?ev.data.keyDown.chr &~ 0x20 : ev.data.keyDown.chr);
					event->kbd.flags = b;
					lastKeyModifier = MD_NONE;
					drawKeyState();				
				}
				return true;
			}

		case penMoveEvent:
			if (ev.screenY*2-_decaly > SCREEN_HEIGHT || ev.screenY*2-_decaly < 0)
				return true;

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

			lastEvent = penMoveEvent;
			event->event_code = EVENT_MOUSEMOVE;
			event->mouse.x = ev.screenX*2;
			event->mouse.y = ev.screenY*2 - _decaly;
			return true;

		case penDownEvent:
			lastEvent = penDownEvent;

			if (ev.screenY*2-_decaly > SCREEN_HEIGHT || ev.screenY*2-_decaly < 0)
				return true;

			event->event_code = EVENT_LBUTTONDOWN;
			event->mouse.x = ev.screenX*2;
			event->mouse.y = ev.screenY*2 - _decaly;
			set_mouse_pos(event->mouse.x, event->mouse.y);
			return true;

		case penUpEvent:
			event->event_code = EVENT_LBUTTONUP;

			if (ev.screenY*2-_decaly > SCREEN_HEIGHT || ev.screenY*2-_decaly < 0)
				return true;

			event->mouse.x = ev.screenX*2;
			event->mouse.y = ev.screenY*2 - _decaly;
			set_mouse_pos(event->mouse.x, event->mouse.y);
			return true;

		default:
			return false;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
uint32 OSystem_PALMOS::property(int param, Property *value) {
	switch(param) {
/*
	case PROP_TOGGLE_FULLSCREEN:
		_full_screen ^= true;
		g_scumm->_fullScreen = _full_screen;

		if (!SDL_WM_ToggleFullScreen(sdl_hwscreen)) {
			// if ToggleFullScreen fails, achieve the same effect with hotswap gfx mode //
			hotswap_gfx_mode();
		}
		return 1;
*/
	case PROP_SET_WINDOW_CAPTION:
/*		RectangleType r;
		
		RctSetRectangle(&r, 45, 60, 70 ,25);
		WinEraseRectangle (&r,0);
		RctSetRectangle(&r, 45 - 2, 60 - 2, 70 + 4 ,25 + 4);

		WinSetForeColor (UIColorGetTableEntryIndex (UIFormFrame));
		WinDrawRectangleFrame(dialogFrame, &r);
		FntSetFont(boldFont);
		WinSetForeColor (UIColorGetTableEntryIndex (UIObjectForeground));
		WinDrawChars("Loading...", 10, 45 + 11, 60 + 7);
*/		
		if (StrCaselessCompare(value->caption,"ScummVM") == 0)
			return 1;
		
		UInt16 w1 = FntCharsWidth(value->caption,StrLen(value->caption));

		if (gVars->HRrefNum != sysInvalidRefNum) {
			Char *caption = "Loading...\0";
			UInt16 h0 = FntLineHeight() + 2;
			UInt16 w1;

			WinSetTextColor(255);
			HRFntSetFont(gVars->HRrefNum,hrTinyBoldFont);
			w1 = FntCharsWidth(caption,StrLen(caption)) * 1;
			w1 = (320 - w1) / 2;
			HRWinDrawChars(gVars->HRrefNum,caption,StrLen(caption),w1,80);

			HRFntSetFont(gVars->HRrefNum,hrTinyFont);
			w1 = FntCharsWidth(value->caption,StrLen(value->caption)) * 1;
			w1 = (320 - w1) / 2;
			HRWinDrawChars(gVars->HRrefNum,value->caption,StrLen(value->caption),w1,80 + h0);
		} else {
			Char *caption = "Loading...\0";
			UInt16 w1;

			WinSetTextColor(255);
			FntSetFont(boldFont);
			w1 = FntCharsWidth(caption,StrLen(caption));
			w1 = (160 - w1) / 2;
			WinDrawChars(caption,StrLen(caption),w1,40);
//			WinSetScalingMode(kTextScalingOff);
//			FntSetFont(stdFont);
//			WinDrawChars(value->caption,StrLen(value->caption),0,0);
		}
/*
		IndexedColorType newColor, oldColor;
		
		RectangleType r;
		HRFntSetFont(gVars->HRrefNum,hrTinyFont);
		UInt16 w0 = FntCharsWidth("Detected game :",StrLen("Detected game :"));
		UInt16 w1 = FntCharsWidth(value->caption,StrLen(value->caption));
		UInt16 h0 = FntLineHeight();
		
		if (w0>w1) w1 = w0;
		w0 = (w1 + 20) / 2;

		oldColor = WinSetBackColor (256);
		RctSetRectangle(&r,(160-w0)/2-1, (100)/2-1,w0+2,25+2);
		WinEraseRectangle (&r,0);
		newColor = UIColorGetTableEntryIndex (UIFormFrame);
		oldColor = WinSetForeColor (newColor);
		RctSetRectangle(&r,(160-w0)/2, (100)/2,w0,25);
		WinDrawRectangleFrame(dialogFrame, &r);

		HRWinDrawChars(gVars->HRrefNum,"Detected game :",15,(320-w1)/2,105);
		HRWinDrawChars(gVars->HRrefNum,value->caption,StrLen(value->caption),(320-w1)/2,105+h0);

		HRFntSetFont(gVars->HRrefNum,hrTinyBoldFont);
		HRWinDrawChars(gVars->HRrefNum,"Initializing...",15,(320-w1)/2,110+h0*2);
*/
		return 1;

	case PROP_OPEN_CD:
		break;
/*		if (SDL_InitSubSystem(SDL_INIT_CDROM) == -1)
			cdrom = NULL;
		else {
			cdrom = SDL_CDOpen(value->cd_num);
			// Did if open? Check if cdrom is NULL //
			if (!cdrom) {
				warning("Couldn't open drive: %s\n", SDL_GetError());
			}
		}
		break;

	case PROP_SET_GFX_MODE:
		if (value->gfx_mode >= 7)
			return 0;

		_mode = value->gfx_mode;
		hotswap_gfx_mode();

		return 1;

	case PROP_SHOW_DEFAULT_CURSOR:
		SDL_ShowCursor(value->show_cursor ? SDL_ENABLE : SDL_DISABLE);		
		break;
*/
	case PROP_GET_SAMPLE_RATE:
		return SAMPLES_PER_SEC;
	}

	return 0;
}
///////////////////////////////////////////////////////////////////////////////
void OSystem_PALMOS::quit() {
	exit(1);

	if (_quit)
		return;
	if (g_scumm)
		g_scumm->_quit = true;
	if (_currentPalette)
		free(_currentPalette);
	if (_mouse_backup)
		free(_mouse_backup);
	if (_sndData)
		MemPtrFree(_sndData);

	unload_gfx_mode();

	_quit = true;
	_currentPalette = NULL;
	_mouse_backup = NULL;
}
///////////////////////////////////////////////////////////////////////////////
void OSystem_PALMOS::draw_mouse() {
	if (_mouse_drawn || !_mouse_visible || _quit)
		return;

	_mouse_cur_state.y = _mouse_cur_state.y>=SCREEN_HEIGHT ? SCREEN_HEIGHT-1 : _mouse_cur_state.y;

	int x = _mouse_cur_state.x - _mouse_hotspot_x;
	int y = _mouse_cur_state.y - _mouse_hotspot_y;
	int w = _mouse_cur_state.w;
	int h = _mouse_cur_state.h;
	byte color;
	byte *src = _mouse_data;		// Image representing the mouse
	byte *bak = _mouse_backup;		// Surface used to backup the area obscured by the mouse
	byte *dst;					// Surface we are drawing into


	// clip the mouse rect, and addjust the src pointer accordingly
	if (x < 0) {
		w += x;
		src -= x;
		x = 0;
	}
	if (y < 0) {
		h += y;
		src -= y * _mouse_cur_state.w;
		y = 0;
	}
	if (w > SCREEN_WIDTH - x)
		w = SCREEN_WIDTH - x;
	if (h > SCREEN_HEIGHT - y)
		h = SCREEN_HEIGHT - y;

	// Store the bounding box so that undraw mouse can restore the area the
	// mouse currently covers to its original content.
	_mouse_old_state.x = x;
	_mouse_old_state.y = y;
	_mouse_old_state.w = w;
	_mouse_old_state.h = h;

	// Quick check to see if anything has to be drawn at all
	if (w <= 0 || h <= 0)
		return;

	// Draw the mouse cursor; backup the covered area in "bak"
	dst = palm_offscreen + y * SCREEN_WIDTH + x;
	while (h > 0) {
		int width = w;
		while (width > 0) {
			*bak++ = *dst;
			color = *src++;
			if (color != 0xFF)	// 0xFF = transparent, don't draw
				*dst = color;
			dst++;
			width--;
		}
		src += _mouse_cur_state.w - w;
		bak += MAX_MOUSE_W - w;
		dst += SCREEN_WIDTH - w;
		h--;
	}

	// Finally, set the flag to indicate the mouse has been drawn
	_mouse_drawn = true;
}
///////////////////////////////////////////////////////////////////////////////
void OSystem_PALMOS::undraw_mouse() {
	if (!_mouse_drawn || _quit)
		return;

	_mouse_drawn = false;

	byte *dst, *bak = _mouse_backup;
	const int old_mouse_x = _mouse_old_state.x;
	const int old_mouse_y = _mouse_old_state.y;
	const int old_mouse_w = _mouse_old_state.w;
	const int old_mouse_h = _mouse_old_state.h;
	int x,y;

	// No need to do clipping here, since draw_mouse() did that already

	dst = palm_offscreen + old_mouse_y * SCREEN_WIDTH + old_mouse_x;
	for (y = 0; y < old_mouse_h; ++y, bak += MAX_MOUSE_W, dst += SCREEN_WIDTH) {
		for (x = 0; x < old_mouse_w; ++x) {
			dst[x] = bak[x];
		}
	}
}
///////////////////////////////////////////////////////////////////////////////
void OSystem_PALMOS::stop_cdrom() {
	return;
}
///////////////////////////////////////////////////////////////////////////////
void OSystem_PALMOS::play_cdrom(int track, int num_loops, int start_frame, int end_frame) {
	return;
}
///////////////////////////////////////////////////////////////////////////////
bool OSystem_PALMOS::poll_cdrom() {
	return false;
}
///////////////////////////////////////////////////////////////////////////////
void OSystem_PALMOS::update_cdrom() {
	return;
}
///////////////////////////////////////////////////////////////////////////////
OSystem_PALMOS::OSystem_PALMOS()
{
	// cannot use memset beacuse of virtual ?

	_quit = false;
	_current_shake_pos = 0;
	_new_shake_pos = 0;

	memset(&_mouse_old_state,0,sizeof(MousePos));
	memset(&_mouse_cur_state,0,sizeof(MousePos));
	
	_msg.state = 0;

	_paletteDirtyStart = 0;
	_paletteDirtyEnd = 0;
	
	_timer.active = false;
	_thread.active = false;
	_sound.active = false;
	
	_currentPalette = NULL;
	_mouse_backup = NULL;

	lastKeyPressed = -1;
	lastKeyRepeat = 100;
	lastKeyModifier = MD_NONE;
	
	_isPlaying = false;

	_sndData = (UInt8 *)MemPtrNew(512);
}
///////////////////////////////////////////////////////////////////////////////
void OSystem_PALMOS::move_screen(int dx, int dy, int height) {

	if ((dx == 0) && (dy == 0))
		return;

	if (dx == 0) {
		// vertical movement
		if (dy > 0) {
			// move down
			// copy from bottom to top
			for (int y = height - 1; y >= dy; y--)
				copy_rect((byte *)palm_offscreen + SCREEN_WIDTH * (y - dy), SCREEN_WIDTH, 0, y, SCREEN_WIDTH, 1);
		} else {
			// move up
			// copy from top to bottom
			for (int y = 0; y < height + dx; y++)
				copy_rect((byte *)palm_offscreen + SCREEN_WIDTH * (y - dy), SCREEN_WIDTH, 0, y, SCREEN_WIDTH, 1);
		}
	} else if (dy == 0) {
		// horizontal movement
		if (dx > 0) {
			// move right
			// copy from right to left
			for (int x = SCREEN_WIDTH - 1; x >= dx; x--)
				copy_rect((byte *)palm_offscreen + x - dx, SCREEN_WIDTH, x, 0, 1, height);
		} else {
			// move left
			// copy from left to right
			for (int x = 0; x < SCREEN_WIDTH; x++)
				copy_rect((byte *)palm_offscreen + x - dx, SCREEN_WIDTH, x, 0, 1, height);
		}
	} else {
		// free movement
		// not neccessary for now
	}
}

void OSystem_PALMOS::drawMessage() {
	UInt32 msecs = get_msecs();
	
	if ((msecs - _msg.time) >= _msg.wait) {
		Int16 y = _msg.position * _msg.state + (_msg.state == -1 ? 320 : 308);
		 _msg.time = msecs;

		WinSetDrawMode(winPaint);
		WinSetBackColor(0);
		WinSetTextColor(_msg.color);
		HRFntSetFont(gVars->HRrefNum,hrTinyFont);
		HRWinDrawChars(gVars->HRrefNum, _msg.text, StrLen(_msg.text), 2, y);
		
		_msg.position += 2;
		if (_msg.position > 12) {
			_msg.position = 0;
			_msg.state *= -1;
			_msg.wait = 5000;
			_msg.state = (_msg.state == -1 ? 0 : _msg.state);
		} else {
			_msg.wait = 100;
		}
	}
}

void OSystem_PALMOS::deleteMessage() {
	if (_msg.state != 0) {
		Int16 y = _msg.position * _msg.state + (_msg.state == -1 ? 320 : 308);
		WinSetDrawMode(winPaint);
		WinSetBackColor(0);
		WinSetTextColor(0);
		HRFntSetFont(gVars->HRrefNum,hrTinyFont);
		HRWinDrawChars(gVars->HRrefNum, _msg.text, StrLen(_msg.text), 2, y);
	}
}

void OSystem_PALMOS::addMessage(const Char *msg) {

	if (_msg.state != 0)
		deleteMessage();

	_msg.state = -1;
	_msg.position = 0;
	StrCopy(_msg.text,msg);
	_msg.time = get_msecs();
	_msg.wait = 100;
	_msg.color = RGBToColor(255,255,255);
}

///////////////////////////////////////////////////////////////////////////////
#define FRAG_SIZE 256
///////////////////////////////////////////////////////////////////////////////
/*
void callback(UInt32 data) {
	
	Pa1Lib_adpcmStop(g_palm->_sndHandle);
	Pa1Lib_adpcmClose(g_palm->_sndHandle);
	g_palm->_isPlaying = false;
	
}
*/
////////////////////////////////////////////////////////////////////////////////
/*

UInt16 snd;

Err SonySoundLibx(UInt16 *refNumP)
{
	SonySysFtrSysInfoP sonySysFtrSysInfoP;
	Err error = errNone;

	if ((error = FtrGet(sonySysFtrCreator, sonySysFtrNumSysInfoP, (UInt32*)&sonySysFtrSysInfoP))) {
		// Not CLIE: maybe not available //
	} else {
		if (sonySysFtrSysInfoP->libr & sonySysFtrSysInfoLibrFm) {
			// Sound-Lib available //
			if ((error = SysLibFind(sonySysLibNameSound, refNumP))) {
				if (error == sysErrLibNotFound) {
				// couldn't find lib //
					error = SysLibLoad( 'libr', sonySysFileCSoundLib, refNumP );
				}
			}

			if ( error ) {
				// Now we can use Sound-Lib //
				FrmCustomAlert(FrmWarnAlert,"Sound Lib not found.",0,0);
			}
		}
	}
	
	return error;
}
*/
/*
typedef struct t_sound {
		bool active;	
		OSystem::SoundProc *proc;
		void *param;
} t_sound;


Err sound(void *userData, SndStreamRef stream, void *buffer, UInt32 frameCount) {
	OSystem_PALMOS *s = (OSystem_PALMOS *)userData;

	s->check_sound();
	return 0;
}
*/
bool OSystem_PALMOS::set_sound_proc(void *param, SoundProc *proc, byte format) {
/*
	_snd_format.formatTag = 0x0020;
	_snd_format.numOfChan = 1;
	_snd_format.samplePerSec = 8000;
	_snd_format.bitPerSample = 4;
	_snd_format.dataSize = FRAG_SIZE;

	_snd_options.amplitude = 256;
	_snd_options.dwStartMilliSec = 0;
	_snd_options.dwEndMilliSec = 500;
	_snd_options.interruptible = true;

	_sound_proc_param = param;
	_sound_proc = proc;

	_sound = false;*/
//	Pa1Lib_Open();
return false;

	_sound.active = true;
	_sound.proc = proc;
	_sound.param = param;
/*
	SndStreamRef sndPref;

	Err e = SndStreamCreate(	&sndPref,
						sndOutput, 22050,
						sndInt16Big, sndMono,
						&sound,
						this, 512,
						false);

	SndStreamStart(sndPref);*/
	return true;
}

/*
UInt32 ADPCM_encoder16bit(UInt16 *dataP, UInt8 *saveP, UInt32 dataLength);*/
void OSystem_PALMOS::check_sound() {

//	if (!Pa1Lib_sndEventProc()) {
//if (!_isPlaying) {

//		CallbackInfoType	callbackInfo;
//		UInt8 buf[512];
//		UInt8 *copy = _sndData;
//		callbackInfo.funcP = callback;
//		callbackInfo.dwUserData = 0;
//		UInt16 buf[512];
//		UInt16 *a = (UInt16 *)buf;

//		MemSet(_sndData,4096,0);
		_sound.proc(_sound.param, _sndData, 512);
/*		int a = ADPCM_encoder16bit((UInt16 *)buf,_sndData, 512);

//		_sound.proc(_sound.param, (UInt8 *)buf, 512);

		MemSet(_sndData,4096,0);

		for (int n=0;n<256;n++) {
			*copy = (UInt8)((*a++ >> 12) | ((*a++ >> 12) << 4));
			copy++;
			//a++;
		}
		

		if (_isPlaying) {
			//Pa1Lib_adpcmStop(_sndHandle);
			//Pa1Lib_adpcmClose(_sndHandle);
			Pa1Lib_sndAdpcmStop();
		}

		//Pa1Lib_adpcmOpen(1, _sndData, 256, NULL, &_sndHandle);
		//Pa1Lib_adpcmStart(_sndHandle,1);
		Pa1Lib_sndAdpcmStart(1, _sndData, a, NULL);
		_isPlaying = true;
	}*/
/*
	e = SndPlayPcm(SndRefNum, NULL, cmd, sound_buffer, &_snd_format, &_snd_options, NULL, true);


	switch (e) {
	case errNone:
		HRWinDrawChars(HRrefNum,"errNone",StrLen("errNone"),0,220);
		break;
	case sndErrBadParam:
		HRWinDrawChars(HRrefNum,"sndErrBadParam",StrLen("sndErrBadParam"),0,220);
		break;
	case sndErrFormat:
		HRWinDrawChars(HRrefNum,"sndErrFormat",StrLen("sndErrFormat"),0,220);
		break;
	case sndErrInterrupted:
		HRWinDrawChars(HRrefNum,"sndErrInterrupted",StrLen("sndErrInterrupted"),0,220);
		break;
	default:
		HRWinDrawChars(HRrefNum,"unknow",StrLen("unknow"),0,220);
		break;
	}*/
}

void OSystem_PALMOS::show_overlay()
{
	// hide the mouse
	undraw_mouse();

	_overlay_visible = true;
	clear_overlay();
}

void OSystem_PALMOS::hide_overlay()
{
	// hide the mouse
	undraw_mouse();

	_overlay_visible = false;
	_overlaySaved = false;
	memmove(palm_offscreen, palm_tmpscreen, SCREEN_WIDTH*SCREEN_HEIGHT);
}

void OSystem_PALMOS::clear_overlay()
{
	if (!_overlay_visible)
		return;
	
	// hide the mouse
	undraw_mouse();
	if (!_overlaySaved)
	{	memmove(palm_tmpscreen, palm_offscreen, SCREEN_WIDTH*SCREEN_HEIGHT);
		_overlaySaved = true;
	}
}

void OSystem_PALMOS::grab_overlay(byte *buf, int pitch)
{
	if (!_overlay_visible)
		return;

	// hide the mouse
	undraw_mouse();

	byte *src = palm_tmpscreen;
	int h = SCREEN_HEIGHT;

	do {
		memcpy(buf, src, SCREEN_WIDTH);
		src += SCREEN_WIDTH;
		buf += pitch;
	} while (--h);
}

void OSystem_PALMOS::copy_rect_overlay(const byte *buf, int pitch, int x, int y, int w, int h)
{
	if (!_overlay_visible)
		return;

	undraw_mouse();

	byte *dst = palm_offscreen + y * SCREEN_WIDTH + x;

	do {
		memcpy(dst, buf, w);
		dst += SCREEN_WIDTH;
		buf += pitch;
	} while (--h);
}


int16 OSystem_PALMOS::get_height() {
	return SCREEN_HEIGHT;
}

int16 OSystem_PALMOS::get_width() {
	return SCREEN_WIDTH;
}

byte OSystem_PALMOS::RGBToColor(uint8 r, uint8 g, uint8 b)
{
	NewGuiColor color = 255;
	byte nearest = 255;
	byte check;
	byte r2,g2,b2;

	for (int i=0; i<256; i++)
	{
		r2 = _currentPalette[i].r;
		g2 = _currentPalette[i].g;
		b2 = _currentPalette[i].b;

		check = (ABS(r2 - r) + ABS(g2 - g) + ABS(b2 - b))/3;

		if (check == 0)				// perfect match
			return i;
		else if (check<nearest)		// else save and continue
		{	
			color = i;
			nearest = check;
		}
	}

	return color;
}

void OSystem_PALMOS::ColorToRGB(byte color, uint8 &r, uint8 &g, uint8 &b)
{
	r = _currentPalette[color].r;
	g = _currentPalette[color].g;
	b = _currentPalette[color].b;
}