summaryrefslogtreecommitdiff
path: root/apps/plugins/jewels.c
blob: 922f32dd8bb3dbd92f00f7f3fddcee85caf10ddd (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
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* Copyright (C) 2005 Adam Boot
*
* Color graphics from Gweled (http://sebdelestaing.free.fr/gweled/)
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/

#include "plugin.h"
#include "lib/playback_control.h"

#ifdef HAVE_LCD_BITMAP

PLUGIN_HEADER

/* button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_PLAY
#define JEWELS_CANCEL BUTTON_OFF

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_OFF

#elif CONFIG_KEYPAD == ONDIO_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_MENU
#define JEWELS_CANCEL BUTTON_OFF

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_MENU   BUTTON_MODE
#define JEWELS_CANCEL BUTTON_OFF
#define JEWELS_RC_CANCEL BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
      (CONFIG_KEYPAD == IPOD_1G2G_PAD)
#define JEWELS_SCROLLWHEEL
#define JEWELS_UP     BUTTON_MENU
#define JEWELS_DOWN   BUTTON_PLAY
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_PREV   BUTTON_SCROLL_BACK
#define JEWELS_NEXT   BUTTON_SCROLL_FWD
#define JEWELS_SELECT BUTTON_SELECT

#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_PLAY

#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == GIGABEAT_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == SANSA_E200_PAD
#define JEWELS_SCROLLWHEEL
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_PREV   BUTTON_SCROLL_BACK
#define JEWELS_NEXT   BUTTON_SCROLL_FWD
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_POWER

#elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
#define JEWELS_SCROLLWHEEL
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_PREV   BUTTON_SCROLL_BACK
#define JEWELS_NEXT   BUTTON_SCROLL_FWD
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL (BUTTON_HOME|BUTTON_REPEAT)

#elif CONFIG_KEYPAD == SANSA_C200_PAD || \
CONFIG_KEYPAD == SANSA_CLIP_PAD || \
CONFIG_KEYPAD == SANSA_M200_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == IRIVER_H10_PAD
#define JEWELS_UP     BUTTON_SCROLL_UP
#define JEWELS_DOWN   BUTTON_SCROLL_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_PLAY
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_MENU   BUTTON_MENU
#define JEWELS_CANCEL BUTTON_BACK

#elif CONFIG_KEYPAD == MROBE100_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define JEWELS_UP     BUTTON_RC_VOL_UP
#define JEWELS_DOWN   BUTTON_RC_VOL_DOWN
#define JEWELS_LEFT   BUTTON_RC_REW
#define JEWELS_RIGHT  BUTTON_RC_FF
#define JEWELS_SELECT BUTTON_RC_PLAY
#define JEWELS_CANCEL BUTTON_RC_REC

#define JEWELS_RC_CANCEL BUTTON_REC

#elif CONFIG_KEYPAD == COWOND2_PAD
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == IAUDIO67_PAD
#define JEWELS_UP     BUTTON_STOP
#define JEWELS_DOWN   BUTTON_PLAY
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_MENU
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_BACK

#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
#define JEWELS_UP     BUTTON_UP
#define JEWELS_DOWN   BUTTON_DOWN
#define JEWELS_LEFT   BUTTON_LEFT
#define JEWELS_RIGHT  BUTTON_RIGHT
#define JEWELS_SELECT BUTTON_SELECT
#define JEWELS_CANCEL BUTTON_POWER

#elif CONFIG_KEYPAD == ONDAVX747_PAD
#define JEWELS_CANCEL BUTTON_POWER

#else
#error No keymap defined!
#endif

#ifdef HAVE_TOUCHSCREEN
#ifndef JEWELS_UP
#define JEWELS_UP     BUTTON_TOPMIDDLE
#endif
#ifndef JEWELS_DOWN
#define JEWELS_DOWN   BUTTON_BOTTOMMIDDLE
#endif
#ifndef JEWELS_LEFT
#define JEWELS_LEFT   BUTTON_MIDLEFT
#endif
#ifndef JEWELS_RIGHT
#define JEWELS_RIGHT  BUTTON_MIDRIGHT
#endif
#ifndef JEWELS_SELECT
#define JEWELS_SELECT BUTTON_CENTER
#endif
#ifndef JEWELS_CANCEL
#define JEWELS_CANCEL BUTTON_TOPLEFT
#endif
#endif

/* use 30x30 tiles (iPod Video, Gigabeat, Onda VX747) */
#if (LCD_HEIGHT == 240) && (LCD_WIDTH == 320) || \
      ((LCD_HEIGHT == 320) && (LCD_WIDTH == 240)) || \
      ((LCD_HEIGHT == 400) && (LCD_WIDTH == 240))
#define TILE_WIDTH  30
#define TILE_HEIGHT 30
#define YOFS 0
#define NUM_SCORES 10

/* use 22x22 tiles (H300, iPod Color) */
#elif ((LCD_HEIGHT == 176) && (LCD_WIDTH == 220)) || \
      ((LCD_HEIGHT == 220) && (LCD_WIDTH == 176))
#define TILE_WIDTH  22
#define TILE_HEIGHT 22
#define YOFS 0
#define NUM_SCORES 10

/* use 16x16 tiles (iPod Nano) */
#elif (LCD_HEIGHT == 132) && (LCD_WIDTH == 176)
#define TILE_WIDTH  16
#define TILE_HEIGHT 16
#define YOFS 4
#define NUM_SCORES 10

/* use 16x16 tiles (H100, iAudio X5, iPod 3G, iPod 4G grayscale) */
#elif (LCD_HEIGHT == 128) && (LCD_WIDTH == 160)
#define TILE_WIDTH  16
#define TILE_HEIGHT 16
#define YOFS 0
#define NUM_SCORES 10

/* use 14x14 tiles (H10 5/6 GB) */
#elif (LCD_HEIGHT == 128) && (LCD_WIDTH == 128)
#define TILE_WIDTH  14
#define TILE_HEIGHT 14
#define YOFS 0
#define NUM_SCORES 10

/* use 13x13 tiles (iPod Mini) */
#elif (LCD_HEIGHT == 110) && (LCD_WIDTH == 138)
#define TILE_WIDTH  13
#define TILE_HEIGHT 13
#define YOFS 6
#define NUM_SCORES 10

/* use 12x12 tiles (iAudio M3) */
#elif (LCD_HEIGHT == 96) && (LCD_WIDTH == 128)
#define TILE_WIDTH  12
#define TILE_HEIGHT 12
#define YOFS 0
#define NUM_SCORES 9

/* use 10x10 tiles (Sansa c200) */
#elif (LCD_HEIGHT == 80) && (LCD_WIDTH == 132)
#define TILE_WIDTH 10
#define TILE_HEIGHT 10
#define YOFS 0
#define NUM_SCORES 8

/* use 10x8 tiles (iFP 700) */
#elif (LCD_HEIGHT == 64) && (LCD_WIDTH == 128)
#define TILE_WIDTH 10
#define TILE_HEIGHT 8
#define YOFS 0
#define NUM_SCORES 8

/* use 10x8 tiles (Recorder, Ondio) */
#elif (LCD_HEIGHT == 64) && (LCD_WIDTH == 112)
#define TILE_WIDTH 10
#define TILE_HEIGHT 8
#define YOFS 0
#define NUM_SCORES 8

#else
  #error JEWELS: Unsupported LCD
#endif

/* save files */
#define SCORE_FILE PLUGIN_GAMES_DIR "/jewels.score"
#define SAVE_FILE  PLUGIN_GAMES_DIR "/jewels.save"

/* final game return status */
#define BJ_QUIT_FROM_GAME 4
#define BJ_END            3
#define BJ_USB            2
#define BJ_QUIT           1
#define BJ_LOSE           0

/* swap directions */
#define SWAP_UP    0
#define SWAP_RIGHT 1
#define SWAP_DOWN  2
#define SWAP_LEFT  3

/* play board dimension */
#define BJ_HEIGHT 9
#define BJ_WIDTH  8

/* next level threshold */
#define LEVEL_PTS 100

/* animation frame rate */
#define MAX_FPS 20

/* Game types */
enum game_type {
    GAME_TYPE_NORMAL,
    GAME_TYPE_PUZZLE
};

/* menu values */
#define FONT_HEIGHT 8
#define MAX_MITEMS  6
#define MENU_WIDTH  100

/* menu results */
enum menu_result {
    MRES_NONE,
    MRES_NEW,
    MRES_PUZZLE,
    MRES_SAVE,
    MRES_RESUME,
    MRES_SCORES,
    MRES_HELP,
    MRES_QUIT,
    MRES_PLAYBACK,
    MRES_EXIT
};

/* menu commands */
enum menu_cmd {
    MCMD_NONE,
    MCMD_NEXT,
    MCMD_PREV,
    MCMD_SELECT
};

/* menus */
struct jewels_menu {
    char *title;
    bool hasframe;
    int selected;
    int itemcnt;
    struct jewels_menuitem {
        char *text;
        enum menu_result res;
    } items[MAX_MITEMS];
} bjmenu[] = {
    {"Jewels", false, 0, 6,
        {{"New Game",    MRES_NEW},
         {"Puzzle",      MRES_PUZZLE},
         {"Resume Saved Game", MRES_RESUME},
         {"High Scores", MRES_SCORES},
         {"Help",        MRES_HELP},
         {"Quit",        MRES_QUIT}}},
    {"Menu", true, 0, 5,
        {{"Audio Playback", MRES_PLAYBACK },
         {"Resume Game", MRES_RESUME},
         {"Save Game",   MRES_SAVE},
         {"End Game",    MRES_QUIT},
         {"Exit Jewels", MRES_EXIT}}}
};

/* external bitmaps */
extern const fb_data jewels[];

/* tile background colors */
#ifdef HAVE_LCD_COLOR
static const unsigned jewels_bkgd[2] = {
    LCD_RGBPACK(104, 63, 63),
    LCD_RGBPACK(83, 44, 44)
};
#endif

/* the tile struct
 * type is the jewel number 0-7
 * falling if the jewel is falling
 * delete marks the jewel for deletion
 */
struct tile {
    int type;
    bool falling;
    bool delete;
};

/* the game context struct
 * score is the current level score
 * segments is the number of cleared segments in the current run
 * level is the current level
 * type is the game type (normal or puzzle)
 * highscores is the list of high scores
 * resume denotes whether to resume the currently loaded game
 * dirty denotes whether the high scores are out of sync with the saved file
 * playboard is the game playing board (first row is hidden)
 * num_jewels is the number of different jewels to use
 */
struct game_context {
    unsigned int score;
    unsigned int segments;
    unsigned int level;
    unsigned int type;
    unsigned int highscores[NUM_SCORES];
    bool resume;
    bool dirty;
    struct tile playboard[BJ_HEIGHT][BJ_WIDTH];
    unsigned int num_jewels;
};

#define MAX_NUM_JEWELS 7

#define MAX_PUZZLE_TILES 4
#define NUM_PUZZLE_LEVELS 10

struct puzzle_tile {
    int x;
    int y;
    int tile_type;
};

struct puzzle_level {
    unsigned int num_jewels;
    unsigned int num_tiles;
    struct puzzle_tile tiles[MAX_PUZZLE_TILES];
};

#define PUZZLE_TILE_UP      1
#define PUZZLE_TILE_DOWN    2
#define PUZZLE_TILE_LEFT    4
#define PUZZLE_TILE_RIGHT   8

struct puzzle_level puzzle_levels[NUM_PUZZLE_LEVELS] = {
    { 5, 2, { {3, 3, PUZZLE_TILE_RIGHT},
              {4, 2, PUZZLE_TILE_LEFT} } },
    { 5, 2, { {3, 2, PUZZLE_TILE_DOWN},
              {3, 4, PUZZLE_TILE_UP} } },
    { 6, 3, { {3, 2, PUZZLE_TILE_DOWN},
              {3, 4, PUZZLE_TILE_UP|PUZZLE_TILE_DOWN},
              {3, 6, PUZZLE_TILE_UP} } },
    { 6, 3, { {3, 2, PUZZLE_TILE_RIGHT},
              {4, 3, PUZZLE_TILE_LEFT|PUZZLE_TILE_RIGHT},
              {5, 4, PUZZLE_TILE_LEFT} } },
    { 6, 2, { {3, 4, PUZZLE_TILE_RIGHT},
              {4, 2, PUZZLE_TILE_LEFT} } },
    { 6, 2, { {3, 2, PUZZLE_TILE_DOWN},
              {4, 4, PUZZLE_TILE_UP} } },
    { 7, 4, { {3, 2, PUZZLE_TILE_RIGHT|PUZZLE_TILE_DOWN},
              {4, 3, PUZZLE_TILE_LEFT|PUZZLE_TILE_DOWN},
              {3, 4, PUZZLE_TILE_RIGHT|PUZZLE_TILE_UP},
              {4, 4, PUZZLE_TILE_LEFT|PUZZLE_TILE_UP} } },
    { 6, 3, { {3, 2, PUZZLE_TILE_DOWN},
              {4, 4, PUZZLE_TILE_UP|PUZZLE_TILE_DOWN},
              {3, 6, PUZZLE_TILE_UP} } },
    { 7, 3, { {2, 2, PUZZLE_TILE_RIGHT},
              {4, 1, PUZZLE_TILE_LEFT|PUZZLE_TILE_RIGHT},
              {5, 4, PUZZLE_TILE_LEFT} } },
    { 7, 4, { {3, 0, PUZZLE_TILE_RIGHT|PUZZLE_TILE_DOWN},
              {5, 0, PUZZLE_TILE_LEFT|PUZZLE_TILE_DOWN},
              {2, 7, PUZZLE_TILE_RIGHT|PUZZLE_TILE_UP},
              {4, 7, PUZZLE_TILE_LEFT|PUZZLE_TILE_UP} } },
};

/*****************************************************************************
* jewels_init() initializes jewels data structures.
******************************************************************************/
static void jewels_init(struct game_context* bj) {
    /* seed the rand generator */
    rb->srand(*rb->current_tick);

    /* check for resumed game */
    if(bj->resume) {
        bj->resume = false;
        return;
    }

    /* reset scoring */
    bj->level = 1;
    bj->score = 0;
    bj->segments = 0;

    /* clear playing board */
    rb->memset(bj->playboard, 0, sizeof(bj->playboard));
}

/*****************************************************************************
* jewels_setcolors() set the foreground and background colors.
******************************************************************************/
static inline void jewels_setcolors(void) {
#ifdef HAVE_LCD_COLOR
    rb->lcd_set_background(LCD_RGBPACK(49, 26, 26));
    rb->lcd_set_foreground(LCD_RGBPACK(210, 181, 181));
#endif
}

/*****************************************************************************
* jewels_drawboard() redraws the entire game board.
******************************************************************************/
static void jewels_drawboard(struct game_context* bj) {
    int i, j;
    int w, h;
    unsigned int tempscore;
    char *title = "Level";
    char str[10];

    tempscore = (bj->score>LEVEL_PTS ? LEVEL_PTS : bj->score);

    /* clear screen */
    rb->lcd_clear_display();

    /* dispay playing board */
    for(i=0; i<BJ_HEIGHT-1; i++){
        for(j=0; j<BJ_WIDTH; j++){
#ifdef HAVE_LCD_COLOR
            rb->lcd_set_foreground(jewels_bkgd[(i+j)%2]);
            rb->lcd_fillrect(j*TILE_WIDTH, i*TILE_HEIGHT+YOFS,
                                TILE_WIDTH, TILE_HEIGHT);
            rb->lcd_bitmap_transparent_part(jewels,
                           0, TILE_HEIGHT*(bj->playboard[i+1][j].type),
                           TILE_WIDTH, j*TILE_WIDTH, i*TILE_HEIGHT+YOFS,
                           TILE_WIDTH, TILE_HEIGHT);
#else
            rb->lcd_bitmap_part(jewels,
                           0, TILE_HEIGHT*(bj->playboard[i+1][j].type),
                           TILE_WIDTH, j*TILE_WIDTH, i*TILE_HEIGHT+YOFS,
                           TILE_WIDTH, TILE_HEIGHT);
#endif
        }
    }

#if LCD_WIDTH > LCD_HEIGHT /* horizontal layout */

    /* draw separator lines */
    jewels_setcolors();
    rb->lcd_vline(BJ_WIDTH*TILE_WIDTH, 0, LCD_HEIGHT-1);
    rb->lcd_hline(BJ_WIDTH*TILE_WIDTH, LCD_WIDTH-1, 18);
    rb->lcd_hline(BJ_WIDTH*TILE_WIDTH, LCD_WIDTH-1, LCD_HEIGHT-10);

    /* draw progress bar */
#ifdef HAVE_LCD_COLOR
    rb->lcd_set_foreground(LCD_RGBPACK(104, 63, 63));
#endif
    rb->lcd_fillrect(BJ_WIDTH*TILE_WIDTH+(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/4,
                     (LCD_HEIGHT-10)-(((LCD_HEIGHT-10)-18)*
                         tempscore/LEVEL_PTS),
                     (LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2,
                     ((LCD_HEIGHT-10)-18)*tempscore/LEVEL_PTS);
#ifdef HAVE_LCD_COLOR
    rb->lcd_set_foreground(LCD_RGBPACK(83, 44, 44));
    rb->lcd_drawrect(BJ_WIDTH*TILE_WIDTH+(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/4+1,
                     (LCD_HEIGHT-10)-(((LCD_HEIGHT-10)-18)*
                         tempscore/LEVEL_PTS)+1,
                     (LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2-2,
                     ((LCD_HEIGHT-10)-18)*tempscore/LEVEL_PTS-1);
    jewels_setcolors();
    rb->lcd_drawrect(BJ_WIDTH*TILE_WIDTH+(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/4,
                     (LCD_HEIGHT-10)-(((LCD_HEIGHT-10)-18)*
                         tempscore/LEVEL_PTS),
                     (LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2,
                     ((LCD_HEIGHT-10)-18)*tempscore/LEVEL_PTS+1);
#endif

    /* print text */
    rb->lcd_getstringsize(title, &w, &h);
    rb->lcd_putsxy(LCD_WIDTH-(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2-w/2, 1, title);

    rb->snprintf(str, 4, "%d", bj->level);
    rb->lcd_getstringsize(str, &w, &h);
    rb->lcd_putsxy(LCD_WIDTH-(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2-w/2, 10, str);

    rb->snprintf(str, 6, "%d", (bj->level-1)*LEVEL_PTS+bj->score);
    rb->lcd_getstringsize(str, &w, &h);
    rb->lcd_putsxy(LCD_WIDTH-(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2-w/2,
                   LCD_HEIGHT-8, str);

#elif LCD_WIDTH < LCD_HEIGHT /* vertical layout */

    /* draw separator lines */
    jewels_setcolors();
    rb->lcd_hline(0, LCD_WIDTH-1, 8*TILE_HEIGHT+YOFS);
    rb->lcd_hline(0, LCD_WIDTH-1, LCD_HEIGHT-14);
    rb->lcd_vline(LCD_WIDTH/2, LCD_HEIGHT-14, LCD_HEIGHT-1);

    /* draw progress bar */
#ifdef HAVE_LCD_COLOR
    rb->lcd_set_foreground(LCD_RGBPACK(104, 63, 63));
#endif
    rb->lcd_fillrect(0, (8*TILE_HEIGHT+YOFS)
                           +(LCD_HEIGHT-14-(8*TILE_HEIGHT+YOFS))/4,
                     LCD_WIDTH*tempscore/LEVEL_PTS,
                     (LCD_HEIGHT-14-(8*TILE_HEIGHT+YOFS))/2);
#ifdef HAVE_LCD_COLOR
    rb->lcd_set_foreground(LCD_RGBPACK(83, 44, 44));
    rb->lcd_drawrect(1, (8*TILE_HEIGHT+YOFS)
                           +(LCD_HEIGHT-14-(8*TILE_HEIGHT+YOFS))/4+1,
                     LCD_WIDTH*tempscore/LEVEL_PTS-1,
                     (LCD_HEIGHT-14-(8*TILE_HEIGHT+YOFS))/2-2);
    jewels_setcolors();
    rb->lcd_drawrect(0, (8*TILE_HEIGHT+YOFS)
                           +(LCD_HEIGHT-14-(8*TILE_HEIGHT+YOFS))/4,
                     LCD_WIDTH*tempscore/LEVEL_PTS+1,
                     (LCD_HEIGHT-14-(8*TILE_HEIGHT+YOFS))/2);
#endif

    /* print text */
    rb->snprintf(str, 10, "%s %d", title, bj->level);
    rb->lcd_putsxy(1, LCD_HEIGHT-10, str);

    rb->snprintf(str, 6, "%d", (bj->level-1)*LEVEL_PTS+bj->score);
    rb->lcd_getstringsize(str, &w, &h);
    rb->lcd_putsxy((LCD_WIDTH-2)-w, LCD_HEIGHT-10, str);

#else /* square layout */

    /* draw separator lines */
    jewels_setcolors();
    rb->lcd_hline(0, LCD_WIDTH-1, 8*TILE_HEIGHT+YOFS);
    rb->lcd_vline(BJ_WIDTH*TILE_WIDTH, 0, 8*TILE_HEIGHT+YOFS);
    rb->lcd_vline(LCD_WIDTH/2, 8*TILE_HEIGHT+YOFS, LCD_HEIGHT-1);

    /* draw progress bar */
#ifdef HAVE_LCD_COLOR
    rb->lcd_set_foreground(LCD_RGBPACK(104, 63, 63));
#endif
    rb->lcd_fillrect(BJ_WIDTH*TILE_WIDTH+(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/4,
                     (8*TILE_HEIGHT+YOFS)-(8*TILE_HEIGHT+YOFS)
                        *tempscore/LEVEL_PTS,
                     (LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2,
                     (8*TILE_HEIGHT+YOFS)*tempscore/LEVEL_PTS);
#ifdef HAVE_LCD_COLOR
    rb->lcd_set_foreground(LCD_RGBPACK(83, 44, 44));
    rb->lcd_drawrect(BJ_WIDTH*TILE_WIDTH+(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/4+1,
                     (8*TILE_HEIGHT+YOFS)-(8*TILE_HEIGHT+YOFS)
                        *tempscore/LEVEL_PTS+1,
                     (LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2-2,
                     (8*TILE_HEIGHT+YOFS)*tempscore/LEVEL_PTS-1);
    jewels_setcolors();
    rb->lcd_drawrect(BJ_WIDTH*TILE_WIDTH+(LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/4,
                     (8*TILE_HEIGHT+YOFS)-(8*TILE_HEIGHT+YOFS)
                        *tempscore/LEVEL_PTS,
                     (LCD_WIDTH-BJ_WIDTH*TILE_WIDTH)/2,
                     (8*TILE_HEIGHT+YOFS)*tempscore/LEVEL_PTS+1);
#endif

    /* print text */
    rb->snprintf(str, 10, "%s %d", title, bj->level);
    rb->lcd_putsxy(1, LCD_HEIGHT-(LCD_HEIGHT-(8*TILE_HEIGHT+YOFS))/2-3, str);

    rb->snprintf(str, 6, "%d", (bj->level-1)*LEVEL_PTS+bj->score);
    rb->lcd_getstringsize(str, &w, &h);
    rb->lcd_putsxy((LCD_WIDTH-2)-w,
                   LCD_HEIGHT-(LCD_HEIGHT-(8*TILE_HEIGHT+YOFS))/2-3, str);

#endif /* layout */

    rb->lcd_update();
}

/*****************************************************************************
* jewels_showmenu() displays the chosen menu after performing the chosen
*     menu command.
******************************************************************************/
static enum menu_result jewels_showmenu(struct jewels_menu* menu,
                                           enum menu_cmd cmd) {
    int i;
    int w, h;
    int firstline;
    int adj;
    int extraline = LCD_HEIGHT <= ((menu->itemcnt+2)*FONT_HEIGHT) ? 0 : 1;

    /* handle menu command */
    switch(cmd) {
        case MCMD_NEXT:
            menu->selected = (menu->selected+1)%menu->itemcnt;
            break;

        case MCMD_PREV:
            menu->selected = (menu->selected-1+menu->itemcnt)%menu->itemcnt;
            break;

        case MCMD_SELECT:
            return menu->items[menu->selected].res;

        default:
            break;
    }

    /* clear menu area */
    firstline = (LCD_HEIGHT/FONT_HEIGHT-(menu->itemcnt+3))/2;

    rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
    rb->lcd_fillrect((LCD_WIDTH-MENU_WIDTH)/2, firstline*FONT_HEIGHT,
                     MENU_WIDTH, (menu->itemcnt+3)*FONT_HEIGHT);
    rb->lcd_set_drawmode(DRMODE_SOLID);

    if(menu->hasframe) {
        rb->lcd_drawrect((LCD_WIDTH-MENU_WIDTH)/2-1, firstline*FONT_HEIGHT-1,
                         MENU_WIDTH+2, (menu->itemcnt+3)*FONT_HEIGHT+2);
        rb->lcd_hline((LCD_WIDTH-MENU_WIDTH)/2-1,
                      (LCD_WIDTH-MENU_WIDTH)/2-1+MENU_WIDTH+2,
                      (firstline+1)*FONT_HEIGHT);
    }

    /* draw menu items */
    rb->lcd_getstringsize(menu->title, &w, &h);
    rb->lcd_putsxy((LCD_WIDTH-w)/2, firstline*FONT_HEIGHT, menu->title);

    for(i=0; i<menu->itemcnt; i++) {
        if(i == menu->selected) {
            rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
        }
        rb->lcd_putsxy((LCD_WIDTH-MENU_WIDTH)/2,
                       (firstline+i+1+extraline)*FONT_HEIGHT,
                       menu->items[i].text);
        if(i == menu->selected) {
            rb->lcd_set_drawmode(DRMODE_SOLID);
        }
    }

    adj = (firstline == 0 ? 0 : 1);
    rb->lcd_update_rect((LCD_WIDTH-MENU_WIDTH)/2-1, firstline*FONT_HEIGHT-adj,
                        MENU_WIDTH+2, (menu->itemcnt+3)*FONT_HEIGHT+2*adj);
    return MRES_NONE;
}

/*****************************************************************************
* jewels_putjewels() makes the jewels fall to fill empty spots and adds
* new random jewels at the empty spots at the top of each row.
******************************************************************************/
static void jewels_putjewels(struct game_context* bj){
    int i, j, k;
    bool mark, done;
    long lasttick, currenttick;

    /* loop to make all the jewels fall */
    while(true) {
        /* mark falling jewels and add new jewels to hidden top row*/
        mark = false;
        done = true;
        for(j=0; j<BJ_WIDTH; j++) {
            if(bj->playboard[1][j].type == 0) {
                bj->playboard[0][j].type = rb->rand()%bj->num_jewels+1;
            }
            for(i=BJ_HEIGHT-2; i>=0; i--) {
                if(!mark && bj->playboard[i+1][j].type == 0) {
                    mark = true;
                    done = false;
                }
                if(mark) bj->playboard[i][j].falling = true;
            }
            /*if(bj->playboard[1][j].falling) {
                bj->playboard[0][j].type = rb->rand()%bj->num_jewels+1;
                bj->playboard[0][j].falling = true;
            }*/
            mark = false;
        }

        /* break if there are no falling jewels */
        if(done) break;

        /* animate falling jewels */
        lasttick = *rb->current_tick;

        for(k=1; k<=8; k++) {
            for(i=BJ_HEIGHT-2; i>=0; i--) {
                for(j=0; j<BJ_WIDTH; j++) {
                    if(bj->playboard[i][j].falling &&
                       bj->playboard[i][j].type != 0) {
                        /* clear old position */
#ifdef HAVE_LCD_COLOR
                        if(i == 0 && YOFS) {
                            rb->lcd_set_foreground(rb->lcd_get_background());
                        } else {
                            rb->lcd_set_foreground(jewels_bkgd[(i-1+j)%2]);
                        }
                        rb->lcd_fillrect(j*TILE_WIDTH, (i-1)*TILE_HEIGHT+YOFS,
                                            TILE_WIDTH, TILE_HEIGHT);
                        if(bj->playboard[i+1][j].type == 0) {
                            rb->lcd_set_foreground(jewels_bkgd[(i+j)%2]);
                            rb->lcd_fillrect(j*TILE_WIDTH, i*TILE_HEIGHT+YOFS,
                                            TILE_WIDTH, TILE_HEIGHT);
                        }
#else
                        rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
                        rb->lcd_fillrect(j*TILE_WIDTH, (i-1)*TILE_HEIGHT+YOFS,
                                            TILE_WIDTH, TILE_HEIGHT);
                        if(bj->playboard[i+1][j].type == 0) {
                            rb->lcd_fillrect(j*TILE_WIDTH, i*TILE_HEIGHT+YOFS,
                                            TILE_WIDTH, TILE_HEIGHT);
                        }
                        rb->lcd_set_drawmode(DRMODE_SOLID);
#endif

                        /* draw new position */
#ifdef HAVE_LCD_COLOR
                        rb->lcd_bitmap_transparent_part(jewels, 0,
                                       TILE_HEIGHT*(bj->playboard[i][j].type),
                                       TILE_WIDTH, j*TILE_WIDTH,
                                       (i-1)*TILE_HEIGHT+YOFS+
                                           ((((TILE_HEIGHT<<10)*k)/8)>>10),
                                       TILE_WIDTH, TILE_HEIGHT);
#else
                        rb->lcd_bitmap_part(jewels, 0,
                                       TILE_HEIGHT*(bj->playboard[i][j].type),
                                       TILE_WIDTH, j*TILE_WIDTH,
                                       (i-1)*TILE_HEIGHT+YOFS+
                                           ((((TILE_HEIGHT<<10)*k)/8)>>10),
                                       TILE_WIDTH, TILE_HEIGHT);
#endif
                    }
                }
            }

            rb->lcd_update_rect(0, 0, TILE_WIDTH*8, LCD_HEIGHT);
            jewels_setcolors();

            /* framerate limiting */
            currenttick = *rb->current_tick;
            if(currenttick-lasttick < HZ/MAX_FPS) {
                rb->sleep((HZ/MAX_FPS)-(currenttick-lasttick));
            } else {
                rb->yield();
            }
            lasttick = currenttick;
        }

        /* shift jewels down */
        for(j=0; j<BJ_WIDTH; j++) {
            for(i=BJ_HEIGHT-1; i>=1; i--) {
                if(bj->playboard[i-1][j].falling) {
                    bj->playboard[i][j].type = bj->playboard[i-1][j].type;
                }
            }
        }

        /* clear out top row */
        for(j=0; j<BJ_WIDTH; j++) {
            bj->playboard[0][j].type = 0;
        }

        /* mark everything not falling */
        for(i=0; i<BJ_HEIGHT; i++) {
            for(j=0; j<BJ_WIDTH; j++) {
                bj->playboard[i][j].falling = false;
            }
        }
    }
}

/*****************************************************************************
* jewels_clearjewels() finds all the connected rows and columns and
*     calculates and returns the points earned.
******************************************************************************/
static unsigned int jewels_clearjewels(struct game_context* bj) {
    int i, j;
    int last, run;
    unsigned int points = 0;

    /* check for connected rows */
    for(i=1; i<BJ_HEIGHT; i++) {
        last = 0;
        run = 1;
        for(j=0; j<BJ_WIDTH; j++) {
            if(bj->playboard[i][j].type == last &&
               bj->playboard[i][j].type != 0 &&
               bj->playboard[i][j].type <= MAX_NUM_JEWELS) {
                run++;

                if(run == 3) {
                    bj->segments++;
                    points += bj->segments;
                    bj->playboard[i][j].delete = true;
                    bj->playboard[i][j-1].delete = true;
                    bj->playboard[i][j-2].delete = true;
                } else if(run > 3) {
                    points++;
                    bj->playboard[i][j].delete = true;
                }
            } else {
                run = 1;
                last = bj->playboard[i][j].type;
            }
        }
    }

    /* check for connected columns */
    for(j=0; j<BJ_WIDTH; j++) {
        last = 0;
        run = 1;
        for(i=1; i<BJ_HEIGHT; i++) {
            if(bj->playboard[i][j].type != 0 &&
               bj->playboard[i][j].type == last &&
               bj->playboard[i][j].type <= MAX_NUM_JEWELS) {
                run++;

                if(run == 3) {
                    bj->segments++;
                    points += bj->segments;
                    bj->playboard[i][j].delete = true;
                    bj->playboard[i-1][j].delete = true;
                    bj->playboard[i-2][j].delete = true;
                } else if(run > 3) {
                    points++;
                    bj->playboard[i][j].delete = true;
                }
            } else {
                run = 1;
                last = bj->playboard[i][j].type;
            }
        }
    }

    /* clear deleted jewels */
    for(i=1; i<BJ_HEIGHT; i++) {
        for(j=0; j<BJ_WIDTH; j++) {
            if(bj->playboard[i][j].delete) {
                bj->playboard[i][j].delete = false;
                bj->playboard[i][j].type = 0;
            }
        }
    }

    return points;
}

/*****************************************************************************
* jewels_runboard() runs the board until it settles in a fixed state and
*     returns points earned.
******************************************************************************/
static unsigned int jewels_runboard(struct game_context* bj) {
    unsigned int points = 0;
    unsigned int ret;

    bj->segments = 0;

    while((ret = jewels_clearjewels(bj)) > 0) {
        points += ret;
        jewels_drawboard(bj);
        jewels_putjewels(bj);
    }

    return points;
}

/*****************************************************************************
* jewels_swapjewels() swaps two jewels as long as it results in points and
*     returns points earned.
******************************************************************************/
static unsigned int jewels_swapjewels(struct game_context* bj,
                                         int x, int y, int direc) {
    int k;
    int horzmod, vertmod;
    int movelen = 0;
    bool undo = false;
    unsigned int points = 0;
    long lasttick, currenttick;

    /* check for invalid parameters */
    if(x < 0 || x >= BJ_WIDTH || y < 0 || y >= BJ_HEIGHT-1 ||
       direc < SWAP_UP || direc > SWAP_LEFT) {
        return 0;
    }

    /* check for invalid directions */
    if((x == 0 && direc == SWAP_LEFT) ||
       (x == BJ_WIDTH-1 && direc == SWAP_RIGHT) ||
       (y == 0 && direc == SWAP_UP) ||
       (y == BJ_HEIGHT-2 && direc == SWAP_DOWN)) {
        return 0;
    }

    /* set direction variables */
    horzmod = 0;
    vertmod = 0;
    switch(direc) {
        case SWAP_UP:
            vertmod = -1;
            movelen = TILE_HEIGHT;
            break;
        case SWAP_RIGHT:
            horzmod = 1;
            movelen = TILE_WIDTH;
            break;
        case SWAP_DOWN:
            vertmod = 1;
            movelen = TILE_HEIGHT;
            break;
        case SWAP_LEFT:
            horzmod = -1;
            movelen = TILE_WIDTH;
            break;
    }

    while(true) {
        lasttick = *rb->current_tick;

        /* animate swapping jewels */
        for(k=0; k<=8; k++) {
            /* clear old position */
#ifdef HAVE_LCD_COLOR
            rb->lcd_set_foreground(jewels_bkgd[(x+y)%2]);
            rb->lcd_fillrect(x*TILE_WIDTH,
                             y*TILE_HEIGHT+YOFS,
                             TILE_WIDTH, TILE_HEIGHT);
            rb->lcd_set_foreground(jewels_bkgd[(x+horzmod+y+vertmod)%2]);
            rb->lcd_fillrect((x+horzmod)*TILE_WIDTH,
                             (y+vertmod)*TILE_HEIGHT+YOFS,
                             TILE_WIDTH, TILE_HEIGHT);
#else
            rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
            rb->lcd_fillrect(x*TILE_WIDTH,
                             y*TILE_HEIGHT+YOFS,
                             TILE_WIDTH, TILE_HEIGHT);
            rb->lcd_fillrect((x+horzmod)*TILE_WIDTH,
                             (y+vertmod)*TILE_HEIGHT+YOFS,
                             TILE_WIDTH, TILE_HEIGHT);
            rb->lcd_set_drawmode(DRMODE_SOLID);
#endif
            /* draw new position */
#ifdef HAVE_LCD_COLOR
            rb->lcd_bitmap_transparent_part(jewels,
                           0, TILE_HEIGHT*(bj->playboard
                               [y+1+vertmod][x+horzmod].type), TILE_WIDTH,
                           (x+horzmod)*TILE_WIDTH-horzmod*
                               ((((movelen<<10)*k)/8)>>10),
                           (y+vertmod)*TILE_HEIGHT-vertmod*
                               ((((movelen<<10)*k)/8)>>10)+YOFS,
                           TILE_WIDTH, TILE_HEIGHT);
            rb->lcd_bitmap_transparent_part(jewels,
                           0, TILE_HEIGHT*(bj->playboard[y+1][x].type),
                           TILE_WIDTH, x*TILE_WIDTH+horzmod*
                               ((((movelen<<10)*k)/8)>>10),
                           y*TILE_HEIGHT+vertmod*
                               ((((movelen<<10)*k)/8)>>10)+YOFS,
                           TILE_WIDTH, TILE_HEIGHT);
#else
            rb->lcd_bitmap_part(jewels,
                           0, TILE_HEIGHT*(bj->playboard
                               [y+1+vertmod][x+horzmod].type), TILE_WIDTH,
                           (x+horzmod)*TILE_WIDTH-horzmod*
                               ((((movelen<<10)*k)/8)>>10),
                           (y+vertmod)*TILE_HEIGHT-vertmod*
                               ((((movelen<<10)*k)/8)>>10)+YOFS,
                           TILE_WIDTH, TILE_HEIGHT);
            rb->lcd_set_drawmode(DRMODE_FG);
            rb->lcd_bitmap_part(jewels,
                           0, TILE_HEIGHT*(bj->playboard[y+1][x].type),
                           TILE_WIDTH, x*TILE_WIDTH+horzmod*
                               ((((movelen<<10)*k)/8)>>10),
                           y*TILE_HEIGHT+vertmod*
                               ((((movelen<<10)*k)/8)>>10)+YOFS,
                           TILE_WIDTH, TILE_HEIGHT);
            rb->lcd_set_drawmode(DRMODE_SOLID);
#endif

            rb->lcd_update_rect(0, 0, TILE_WIDTH*8, LCD_HEIGHT);
            jewels_setcolors();

            /* framerate limiting */
            currenttick = *rb->current_tick;
            if(currenttick-lasttick < HZ/MAX_FPS) {
                rb->sleep((HZ/MAX_FPS)-(currenttick-lasttick));
            } else {
                rb->yield();
            }
            lasttick = currenttick;
        }

        /* swap jewels */
        int temp = bj->playboard[y+1][x].type;
        bj->playboard[y+1][x].type =
            bj->playboard[y+1+vertmod][x+horzmod].type;
        bj->playboard[y+1+vertmod][x+horzmod].type = temp;

        if(undo) break;

        points = jewels_runboard(bj);
        if(points == 0) {
            undo = true;
        } else {
            break;
        }
    }

    return points;
}

/*****************************************************************************
* jewels_movesavail() uses pattern matching to see if there are any
*     available move left.
******************************************************************************/
static bool jewels_movesavail(struct game_context* bj) {
    int i, j;
    bool moves = false;
    int mytype;

    for(i=1; i<BJ_HEIGHT; i++) {
        for(j=0; j<BJ_WIDTH; j++) {
            mytype = bj->playboard[i][j].type;
            if(mytype == 0 || mytype > MAX_NUM_JEWELS) continue;

            /* check horizontal patterns */
            if(j <= BJ_WIDTH-3) {
                if(i > 1) {
                    if(bj->playboard[i-1][j+1].type == mytype) {
                        if(bj->playboard[i-1][j+2].type == mytype)
                            {moves = true; break;}
                        if(bj->playboard[i][j+2].type == mytype)
                            {moves = true; break;}
                    }
                    if(bj->playboard[i][j+1].type == mytype) {
                        if(bj->playboard[i-1][j+2].type == mytype)
                            {moves = true; break;}
                    }
                }

                if(j <= BJ_WIDTH-4) {
                    if(bj->playboard[i][j+3].type == mytype) {
                        if(bj->playboard[i][j+1].type == mytype)
                            {moves = true; break;}
                        if(bj->playboard[i][j+2].type == mytype)
                            {moves = true; break;}
                    }
                }

                if(i < BJ_HEIGHT-1) {
                    if(bj->playboard[i][j+1].type == mytype) {
                        if(bj->playboard[i+1][j+2].type == mytype)
                            {moves = true; break;}
                    }
                    if(bj->playboard[i+1][j+1].type == mytype) {
                        if(bj->playboard[i][j+2].type == mytype)
                            {moves = true; break;}
                        if(bj->playboard[i+1][j+2].type == mytype)
                            {moves = true; break;}
                    }
                }
            }

            /* check vertical patterns */
            if(i <= BJ_HEIGHT-3) {
                if(j > 0) {
                    if(bj->playboard[i+1][j-1].type == mytype) {
                        if(bj->playboard[i+2][j-1].type == mytype)
                            {moves = true; break;}
                        if(bj->playboard[i+2][j].type == mytype)
                            {moves = true; break;}
                    }
                    if(bj->playboard[i+1][j].type == mytype) {
                        if(bj->playboard[i+2][j-1].type == mytype)
                            {moves = true; break;}
                    }
                }

                if(i <= BJ_HEIGHT-4) {
                    if(bj->playboard[i+3][j].type == mytype) {
                        if(bj->playboard[i+1][j].type == mytype)
                            {moves = true; break;}
                        if(bj->playboard[i+2][j].type == mytype)
                            {moves = true; break;}
                    }
                }

                if(j < BJ_WIDTH-1) {
                    if(bj->playboard[i+1][j].type == mytype) {
                        if(bj->playboard[i+2][j+1].type == mytype)
                            {moves = true; break;}
                    }
                    if(bj->playboard[i+1][j+1].type == mytype) {
                        if(bj->playboard[i+2][j].type == mytype)
                            {moves = true; break;}
                        if (bj->playboard[i+2][j+1].type == mytype)
                            {moves = true; break;}
                    }
                }
            }
        }

        if(moves) break;
    }

    return moves;
}

/*****************************************************************************
* jewels_puzzle_is_finished(bj) checks if the puzzle is finished.
******************************************************************************/
static int jewels_puzzle_is_finished(struct game_context* bj) {
    unsigned int i, j;
    for(i=0; i<BJ_HEIGHT; i++) {
        for(j=0; j<BJ_WIDTH; j++) {
            int mytype = bj->playboard[i][j].type;
            if(mytype>MAX_NUM_JEWELS) {
                mytype -= MAX_NUM_JEWELS;
                if(mytype&PUZZLE_TILE_UP) {
                    if(i==0 || bj->playboard[i-1][j].type<=MAX_NUM_JEWELS ||
                       !((bj->playboard[i-1][j].type-MAX_NUM_JEWELS)
                         &PUZZLE_TILE_DOWN))
                        return 0;
                }
                if(mytype&PUZZLE_TILE_DOWN) {
                    if(i==BJ_HEIGHT-1 ||
                       bj->playboard[i+1][j].type<=MAX_NUM_JEWELS ||
                       !((bj->playboard[i+1][j].type-MAX_NUM_JEWELS)
                         &PUZZLE_TILE_UP))
                        return 0;
                }
                if(mytype&PUZZLE_TILE_LEFT) {
                    if(j==0 || bj->playboard[i][j-1].type<=MAX_NUM_JEWELS ||
                       !((bj->playboard[i][j-1].type-MAX_NUM_JEWELS)
                         &PUZZLE_TILE_RIGHT))
                        return 0;
                }
                if(mytype&PUZZLE_TILE_RIGHT) {
                    if(j==BJ_WIDTH-1 ||
                       bj->playboard[i][j+1].type<=MAX_NUM_JEWELS ||
                       !((bj->playboard[i][j+1].type-MAX_NUM_JEWELS)
                         &PUZZLE_TILE_LEFT))
                        return 0;
                }
            }
        }
    }
    return 1;
}

/*****************************************************************************
* jewels_initlevel() initialises a level.
******************************************************************************/
static unsigned int jewels_initlevel(struct game_context* bj) {
    unsigned int points = 0;

    switch(bj->type) {
        case GAME_TYPE_NORMAL:
            bj->num_jewels = MAX_NUM_JEWELS;
            break;

        case GAME_TYPE_PUZZLE:
        {
            unsigned int i, j;
            struct puzzle_tile *tile;

            bj->num_jewels = puzzle_levels[bj->level-1].num_jewels;

            for(i=0; i<BJ_HEIGHT; i++) {
                for(j=0; j<BJ_WIDTH; j++) {
                    bj->playboard[i][j].type = (rb->rand()%bj->num_jewels)+1;
                    bj->playboard[i][j].falling = false;
                    bj->playboard[i][j].delete = false;
                }
            }
            jewels_runboard(bj);
            tile = puzzle_levels[bj->level-1].tiles;
            for(i=0; i<puzzle_levels[bj->level-1].num_tiles; i++, tile++) {
                bj->playboard[tile->y+1][tile->x].type = MAX_NUM_JEWELS
                                                         +tile->tile_type;
            }
        }
        break;
    }

    jewels_drawboard(bj);

    /* run the play board */
    jewels_putjewels(bj);
    points += jewels_runboard(bj);
    return points;
}

/*****************************************************************************
* jewels_nextlevel() advances the game to the next level and returns
*     points earned.
******************************************************************************/
static void jewels_nextlevel(struct game_context* bj) {
    int i, x, y;
    unsigned int points = 0;

    switch(bj->type) {
        case GAME_TYPE_NORMAL:
            /* roll over score, change and display level */
            while(bj->score >= LEVEL_PTS) {
                bj->score -= LEVEL_PTS;
                bj->level++;
                rb->splashf(HZ*2, "Level %d", bj->level);
                jewels_drawboard(bj);
            }

            /* randomly clear some jewels */
            for(i=0; i<16; i++) {
                x = rb->rand()%8;
                y = rb->rand()%8;

                if(bj->playboard[y][x].type != 0) {
                    points++;
                    bj->playboard[y][x].type = 0;
                }
            }
            break;

        case GAME_TYPE_PUZZLE:
            bj->level++;
            if(bj->level>NUM_PUZZLE_LEVELS) {
                rb->splash(HZ*2, "You win!");
                bj->level = 1;
            } else {
                rb->splashf(HZ*2, "Level %d", bj->level);
            }
            break;
    }

    points += jewels_initlevel(bj);
    bj->score += points;
}

/*****************************************************************************
* jewels_recordscore() inserts a high score into the high scores list and
*     returns the high score position.
******************************************************************************/
static int jewels_recordscore(struct game_context* bj) {
    int i;
    int position = 0;
    unsigned int current, temp;

    /* calculate total score */
    current = (bj->level-1)*LEVEL_PTS+bj->score;
    if(current <= 0) return 0;

    /* insert the current score into the high scores */
    for(i=0; i<NUM_SCORES; i++) {
        if(current >= bj->highscores[i]) {
            if(!position) {
                position = i+1;
                bj->dirty = true;
            }
            temp = bj->highscores[i];
            bj->highscores[i] = current;
            current = temp;
        }
    }

    return position;
 }

/*****************************************************************************
* jewels_loadscores() loads the high scores saved file.
******************************************************************************/
static void jewels_loadscores(struct game_context* bj) {
    int fd;

    bj->dirty = false;

    /* clear high scores */
    rb->memset(bj->highscores, 0, sizeof(bj->highscores));

    /* open scores file */
    fd = rb->open(SCORE_FILE, O_RDONLY);
    if(fd < 0) return;

    /* read in high scores */
    if(rb->read(fd, bj->highscores, sizeof(bj->highscores)) <= 0) {
        /* scores are bad, reset */
        rb->memset(bj->highscores, 0, sizeof(bj->highscores));
    }

    rb->close(fd);
}

/*****************************************************************************
* jewels_savescores() saves the high scores saved file.
******************************************************************************/
static void jewels_savescores(struct game_context* bj) {
    int fd;

    /* write out the high scores to the save file */
    fd = rb->open(SCORE_FILE, O_WRONLY|O_CREAT);
    rb->write(fd, bj->highscores, sizeof(bj->highscores));
    rb->close(fd);
    bj->dirty = false;
}

/*****************************************************************************
* jewels_loadgame() loads the saved game and returns load success.
******************************************************************************/
static bool jewels_loadgame(struct game_context* bj) {
    int fd;
    bool loaded = false;

    /* open game file */
    fd = rb->open(SAVE_FILE, O_RDONLY);
    if(fd < 0) return loaded;

    /* read in saved game */
    while(true) {
        if(rb->read(fd, &bj->score, sizeof(bj->score)) <= 0) break;
        if(rb->read(fd, &bj->level, sizeof(bj->level)) <= 0) break;
        if(rb->read(fd, &bj->type, sizeof(bj->type)) <= 0) break;
        if(rb->read(fd, bj->playboard, sizeof(bj->playboard)) <= 0) break;
        bj->resume = true;
        loaded = true;
        break;
    }

    rb->close(fd);

    /* delete saved file */
    rb->remove(SAVE_FILE);
    return loaded;
}

/*****************************************************************************
* jewels_savegame() saves the current game state.
******************************************************************************/
static void jewels_savegame(struct game_context* bj) {
    int fd;

    /* write out the game state to the save file */
    fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT);
    rb->write(fd, &bj->score, sizeof(bj->score));
    rb->write(fd, &bj->level, sizeof(bj->level));
    rb->write(fd, &bj->type, sizeof(bj->type));
    rb->write(fd, bj->playboard, sizeof(bj->playboard));
    rb->close(fd);

    bj->resume = true;
}

/*****************************************************************************
* jewels_callback() is the default event handler callback which is called
*     on usb connect and shutdown.
******************************************************************************/
static void jewels_callback(void* param) {
    struct game_context* bj = (struct game_context*) param;
    if(bj->dirty) {
        rb->splash(HZ, "Saving high scores...");
        jewels_savescores(bj);
    }
}

/*****************************************************************************
* jewels_main() is the main game subroutine, it returns the final game status.
******************************************************************************/
static int jewels_main(struct game_context* bj) {
    int i, j;
    int w, h;
    int button;
    char str[18];
    bool startgame = false;
    bool inmenu = false;
    bool selected = false;
    enum menu_cmd cmd = MCMD_NONE;
    enum menu_result res;

    /* the cursor coordinates */
    int x=0, y=0;

    /* don't resume by default */
    bj->resume = false;

    /********************
    *       menu        *
    ********************/
    rb->lcd_clear_display();

    while(!startgame) {
        res = jewels_showmenu(&bjmenu[0], cmd);
        cmd = MCMD_NONE;

        rb->snprintf(str, 18, "High Score: %d", bj->highscores[0]);
        rb->lcd_getstringsize(str, &w, &h);
        rb->lcd_putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT-8, str);
        rb->lcd_update();

        rb->yield();

        switch(res) {
            case MRES_NEW:
                startgame = true;
                bj->type = GAME_TYPE_NORMAL;
                continue;

            case MRES_PUZZLE:
                startgame = true;
                bj->type = GAME_TYPE_PUZZLE;
                continue;

            case MRES_RESUME:
                if(!jewels_loadgame(bj)) {
                    rb->splash(HZ*2, "Nothing to resume");
                    rb->lcd_clear_display();
                } else {
                    startgame = true;
                }
                continue;

            case MRES_SCORES:
                rb->lcd_clear_display();

                /* room for a title? */
                j = 0;
                if(LCD_HEIGHT-NUM_SCORES*8 >= 8) {
                    rb->snprintf(str, 12, "%s", "High Scores");
                    rb->lcd_getstringsize(str, &w, &h);
                    rb->lcd_putsxy((LCD_WIDTH-w)/2, 0, str);
                    j = 2;
                }

                /* print high scores */
                for(i=0; i<NUM_SCORES; i++) {
                    rb->snprintf(str, 11, "#%02d: %d", i+1, bj->highscores[i]);
                    rb->lcd_puts(0, i+j, str);
                }

                rb->lcd_update();
                while(true) {
                    button = rb->button_get(true);
                    if(button != BUTTON_NONE && !(button&BUTTON_REL)) break;
                    rb->yield();
                }
                rb->lcd_clear_display();
                continue;

            case MRES_HELP:
                /* welcome screen to display key bindings */
                rb->lcd_clear_display();
                rb->snprintf(str, 5, "%s", "Help");
                rb->lcd_getstringsize(str, &w, &h);
                rb->lcd_putsxy((LCD_WIDTH-w)/2, 0, str);
#if CONFIG_KEYPAD == RECORDER_PAD
                rb->lcd_puts(0, 2, "Controls:");
                rb->lcd_puts(0, 3, "Directions = move");
                rb->lcd_puts(0, 4, "PLAY = select");
                rb->lcd_puts(0, 5, "Long PLAY = menu");
                rb->lcd_puts(0, 6, "OFF = cancel");
#elif CONFIG_KEYPAD == ONDIO_PAD
                rb->lcd_puts(0, 2, "Controls:");
                rb->lcd_puts(0, 3, "Directions = move");
                rb->lcd_puts(0, 4, "MENU = select");
                rb->lcd_puts(0, 5, "Long MENU = menu");
                rb->lcd_puts(0, 6, "OFF = cancel");
#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
                rb->lcd_puts(0, 2, "Controls:");
                rb->lcd_puts(0, 3, "Directions = move");
                rb->lcd_puts(0, 4, "SELECT = select");
                rb->lcd_puts(0, 5, "Long SELECT = menu");
                rb->lcd_puts(0, 6, "PLAY = cancel");
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "SELECT to select");
                rb->lcd_puts(0, 10, "Long SELECT to show menu");
                rb->lcd_puts(0, 11, "OFF to cancel");
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
      (CONFIG_KEYPAD == IPOD_1G2G_PAD)
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions or scroll to move");
                rb->lcd_puts(0, 9, "SELECT to select");
                rb->lcd_puts(0, 10, "Long SELECT to show menu");
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "SELECT to select");
                rb->lcd_puts(0, 10, "Long SELECT to show menu");
                rb->lcd_puts(0, 11, "PLAY to cancel");
#elif CONFIG_KEYPAD == GIGABEAT_PAD \
   || CONFIG_KEYPAD == MROBE100_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "SELECT to select");
                rb->lcd_puts(0, 10, "Long SELECT to show menu");
                rb->lcd_puts(0, 11, "POWER to cancel");
#elif CONFIG_KEYPAD == SANSA_E200_PAD \
   || CONFIG_KEYPAD == SANSA_C200_PAD \
   || CONFIG_KEYPAD == SANSA_CLIP_PAD \
   || CONFIG_KEYPAD == SANSA_FUZE_PAD \
   || CONFIG_KEYPAD == SANSA_M200_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "SELECT to select");
                rb->lcd_puts(0, 10, "Long SELECT to show menu");
                rb->lcd_puts(0, 11, "POWER to cancel");
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels");
                rb->lcd_puts(0, 3, "to form connected");
                rb->lcd_puts(0, 4, "segments of three or ");
                rb->lcd_puts(0, 5, "more of the");
                rb->lcd_puts(0, 6, "same type.");
                rb->lcd_puts(0, 8, "Controls:");
                rb->lcd_puts(0, 9, "Directions or scroll to move");
                rb->lcd_puts(0, 10, "PLAY to select");
                rb->lcd_puts(0, 11, "Long PLAY for menu");
                rb->lcd_puts(0, 12, "POWER to cancel");
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels");
                rb->lcd_puts(0, 3, "to form connected");
                rb->lcd_puts(0, 4, "segments of three or ");
                rb->lcd_puts(0, 5, "more of the");
                rb->lcd_puts(0, 6, "same type.");
                rb->lcd_puts(0, 8, "Controls:");
                rb->lcd_puts(0, 9, "Directions or scroll to move");
                rb->lcd_puts(0, 10, "PLAY to select");
                rb->lcd_puts(0, 11, "Long PLAY for menu");
                rb->lcd_puts(0, 12, "REC to cancel");
#elif CONFIG_KEYPAD == COWOND2_PAD
                rb->lcd_puts(0, 11, "POWER to cancel");
#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "SELECT to select");
                rb->lcd_puts(0, 10, "Long SELECT to show menu");
                rb->lcd_puts(0, 11, "BACK to cancel");
#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "MIDDLE to select");
                rb->lcd_puts(0, 10, "Long MIDDLE to show menu");
                rb->lcd_puts(0, 11, "BACK to cancel");
#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "SELECT/PLAY to select");
                rb->lcd_puts(0, 10, "Long SELECT to show menu");
                rb->lcd_puts(0, 11, "POWER to cancel");
#elif CONFIG_KEYPAD == ONDAVX747_PAD
                rb->lcd_puts(0, 11, "POWER to cancel");
#else
    #warning: missing help text.
#endif

#ifdef HAVE_TOUCHSCREEN
                rb->lcd_puts(0, 2, "Swap pairs of jewels to");
                rb->lcd_puts(0, 3, "form connected segments");
                rb->lcd_puts(0, 4, "of three or more of the");
                rb->lcd_puts(0, 5, "same type.");
                rb->lcd_puts(0, 7, "Controls:");
                rb->lcd_puts(0, 8, "Directions to move");
                rb->lcd_puts(0, 9, "CENTER to select");
                rb->lcd_puts(0, 10, "Long CENTER to show menu");
#endif
                rb->lcd_update();
                while(true) {
                    button = rb->button_get(true);
                    if(button != BUTTON_NONE && !(button&BUTTON_REL)) break;
                }
                rb->lcd_clear_display();
                continue;

            case MRES_QUIT:
                return BJ_QUIT;

            default:
                break;
        }

        /* handle menu button presses */
        button = rb->button_get(true);
        switch(button){
#ifdef JEWELS_SCROLLWHEEL
            case JEWELS_PREV:
            case (JEWELS_PREV|BUTTON_REPEAT):
#endif
            case JEWELS_UP:
            case (JEWELS_UP|BUTTON_REPEAT):
                cmd = MCMD_PREV;
                break;

#ifdef JEWELS_SCROLLWHEEL
            case JEWELS_NEXT:
            case (JEWELS_NEXT|BUTTON_REPEAT):
#endif
            case JEWELS_DOWN:
            case (JEWELS_DOWN|BUTTON_REPEAT):
                cmd = MCMD_NEXT;
                break;

            case JEWELS_SELECT:
            case JEWELS_RIGHT:
                cmd = MCMD_SELECT;
                break;

#ifdef JEWELS_CANCEL
#ifdef JEWELS_RC_CANCEL
            case JEWELS_RC_CANCEL:
#endif
            case JEWELS_CANCEL:
                return BJ_QUIT;
#endif

            default:
                if(rb->default_event_handler_ex(button, jewels_callback,
                   (void*) bj) == SYS_USB_CONNECTED)
                    return BJ_USB;
                break;
        }
    }

    /********************
    *       init        *
    ********************/
    jewels_init(bj);

    /********************
    *  setup the board  *
    ********************/
    bj->score += jewels_initlevel(bj);
    if (!jewels_movesavail(bj)) {
        switch(bj->type) {
            case GAME_TYPE_NORMAL:
                return BJ_LOSE;

            case GAME_TYPE_PUZZLE:
                do {
                    rb->splash(2*HZ, "No more moves!");
                    bj->score += jewels_initlevel(bj);
                } while(!jewels_movesavail(bj));
                break;
        }
    }

    /**********************
    *        play         *
    **********************/
    while(true) {
        int no_movesavail = false;

        if(!inmenu) {
            /* refresh the board */
            jewels_drawboard(bj);

            /* display the cursor */
            if(selected) {
                rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
                rb->lcd_fillrect(x*TILE_WIDTH, y*TILE_HEIGHT+YOFS,
                                 TILE_WIDTH, TILE_HEIGHT);
                rb->lcd_set_drawmode(DRMODE_SOLID);
            } else {
                rb->lcd_drawrect(x*TILE_WIDTH, y*TILE_HEIGHT+YOFS,
                                 TILE_WIDTH, TILE_HEIGHT);
            }
            rb->lcd_update_rect(x*TILE_WIDTH, y*TILE_HEIGHT+YOFS,
                                TILE_WIDTH, TILE_HEIGHT);
        } else {
            res = jewels_showmenu(&bjmenu[1], cmd);
            cmd = MCMD_NONE;
            switch(res) {
                case MRES_RESUME:
                    inmenu = false;
                    selected = false;
                    continue;

                case MRES_PLAYBACK:
                    playback_control(NULL);
                    rb->lcd_setfont(FONT_SYSFIXED);
                    inmenu = false;
                    selected = false;
                    break;

                case MRES_SAVE:
                    rb->splash(HZ, "Saving game...");
                    jewels_savegame(bj);
                    return BJ_END;

                case MRES_QUIT:
                    return BJ_END;

                case MRES_EXIT:
                    return BJ_QUIT_FROM_GAME;

                default:
                    break;
            }
        }

        /* handle game button presses */
        rb->yield();
        button = rb->button_get(true);
        switch(button){
            case JEWELS_LEFT:             /* move cursor left */
            case (JEWELS_LEFT|BUTTON_REPEAT):
                if(!inmenu) {
                    if(selected) {
                        bj->score += jewels_swapjewels(bj, x, y, SWAP_LEFT);
                        selected = false;
                        if (!jewels_movesavail(bj)) no_movesavail = true;
                    } else {
                        x = (x+BJ_WIDTH-1)%BJ_WIDTH;
                    }
                }
                break;

            case JEWELS_RIGHT:            /* move cursor right */
            case (JEWELS_RIGHT|BUTTON_REPEAT):
                if(!inmenu) {
                    if(selected) {
                        bj->score += jewels_swapjewels(bj, x, y, SWAP_RIGHT);
                        selected = false;
                        if (!jewels_movesavail(bj)) no_movesavail = true;
                    } else {
                        x = (x+1)%BJ_WIDTH;
                    }
                } else {
                    cmd = MCMD_SELECT;
                }
                break;

            case JEWELS_DOWN:             /* move cursor down */
            case (JEWELS_DOWN|BUTTON_REPEAT):
                if(!inmenu) {
                    if(selected) {
                        bj->score += jewels_swapjewels(bj, x, y, SWAP_DOWN);
                        selected = false;
                        if (!jewels_movesavail(bj)) no_movesavail = true;
                    } else {
                        y = (y+1)%(BJ_HEIGHT-1);
                    }
                } else {
                    cmd = MCMD_NEXT;
                }
                break;

            case JEWELS_UP:               /* move cursor up */
            case (JEWELS_UP|BUTTON_REPEAT):
                if(!inmenu) {
                    if(selected) {
                        bj->score += jewels_swapjewels(bj, x, y, SWAP_UP);
                        selected = false;
                        if (!jewels_movesavail(bj)) no_movesavail = true;
                    } else {
                        y = (y+(BJ_HEIGHT-1)-1)%(BJ_HEIGHT-1);
                    }
                } else {
                    cmd = MCMD_PREV;
                }
                break;

#ifdef JEWELS_SCROLLWHEEL
            case JEWELS_PREV:             /* scroll backwards */
            case (JEWELS_PREV|BUTTON_REPEAT):
                if(!inmenu) {
                    if(!selected) {
                        if(x == 0) {
                            y = (y+(BJ_HEIGHT-1)-1)%(BJ_HEIGHT-1);
                        }
                        x = (x+BJ_WIDTH-1)%BJ_WIDTH;
                    }
                } else {
                    cmd = MCMD_PREV;
                }
                break;

            case JEWELS_NEXT:             /* scroll forwards */
            case (JEWELS_NEXT|BUTTON_REPEAT):
                if(!inmenu) {
                    if(!selected) {
                        if(x == BJ_WIDTH-1) {
                            y = (y+1)%(BJ_HEIGHT-1);
                        }
                        x = (x+1)%BJ_WIDTH;
                    }
                } else {
                    cmd = MCMD_NEXT;
                }
                break;
#endif

            case JEWELS_SELECT:           /* toggle selected */
                if(!inmenu) {
                    selected = !selected;
                } else {
                    cmd = MCMD_SELECT;
                }
                break;

#ifdef JEWELS_MENU
            case JEWELS_MENU:
#endif
            case (JEWELS_SELECT|BUTTON_REPEAT): /* show menu */
                if(!inmenu) inmenu = true;
                break;

#ifdef JEWELS_CANCEL
#ifdef JEWELS_RC_CANCEL
            case JEWELS_RC_CANCEL:
#endif
            case JEWELS_CANCEL:           /* end game */
                return BJ_END;
                break;
#endif

            default:
                if(rb->default_event_handler_ex(button, jewels_callback,
                   (void*) bj) == SYS_USB_CONNECTED)
                    return BJ_USB;
                break;
        }

        if (no_movesavail) {
            switch(bj->type) {
                case GAME_TYPE_NORMAL:
                    return BJ_LOSE;

                case GAME_TYPE_PUZZLE:
                    do {
                        rb->splash(2*HZ, "No more moves!");
                        bj->score += jewels_initlevel(bj);
                    } while(!jewels_movesavail(bj));
                    break;
            }
        }

        switch(bj->type) {
            case GAME_TYPE_NORMAL:
                if(bj->score >= LEVEL_PTS)
                    jewels_nextlevel(bj);
                break;

            case GAME_TYPE_PUZZLE:
                if(jewels_puzzle_is_finished(bj))
                    jewels_nextlevel(bj);
                break;
        }
    }
}

/*****************************************************************************
* plugin entry point.
******************************************************************************/
enum plugin_status plugin_start(const void* parameter) {
    struct game_context bj;
    bool exit = false;
    int position;
    char str[19];

    /* plugin init */
    (void)parameter;
    /* end of plugin init */

    /* load high scores */
    jewels_loadscores(&bj);

    rb->lcd_setfont(FONT_SYSFIXED);
#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif
    jewels_setcolors();

    while(!exit) {
        switch(jewels_main(&bj)){
            case BJ_LOSE:
                rb->splash(HZ*2, "No more moves!");
                /* fall through to BJ_END */

            case BJ_END:
                if(!bj.resume) {
                    if((position = jewels_recordscore(&bj))) {
                        rb->snprintf(str, 19, "New high score #%d!", position);
                        rb->splash(HZ*2, str);
                    }
                }
                break;

            case BJ_USB:
                rb->lcd_setfont(FONT_UI);
                return PLUGIN_USB_CONNECTED;

            case BJ_QUIT:
                if(bj.dirty) {
                    rb->splash(HZ, "Saving high scores...");
                    jewels_savescores(&bj);
                }
                exit = true;
                break;

            case BJ_QUIT_FROM_GAME:
                if(!bj.resume) {
                    if((position = jewels_recordscore(&bj))) {
                        rb->snprintf(str, 19, "New high score #%d!", position);
                        rb->splash(HZ*2, str);
                    }
                }
                if(bj.dirty) {
                    rb->splash(HZ, "Saving high scores...");
                    jewels_savescores(&bj);
                }
                exit = true;
                break;

            default:
                break;
        }
    }

    rb->lcd_setfont(FONT_UI);
    return PLUGIN_OK;
}

#endif /* HAVE_LCD_BITMAP */