File: x_sherry.c

package info (click to toggle)
timidity 2.14.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 11,364 kB
  • sloc: ansic: 168,735; sh: 3,729; makefile: 1,405; tcl: 1,048; perl: 285; ruby: 126
file content (2328 lines) | stat: -rw-r--r-- 53,918 bytes parent folder | download | duplicates (5)
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
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
/*
    TiMidity++ -- MIDI to WAVE converter and player
    Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
    Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>

    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

    x_sherry.c - Sherry WRD for X Window written by Masanao Izumo
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#ifdef ENABLE_SHERRY
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <stdlib.h>
#include <png.h>

#include "timidity.h"
#include "common.h"
#include "controls.h"
#include "wrd.h"
#include "aq.h"
#include "x_sherry.h"

#ifndef XSHM_SUPPORT
#if defined(HAVE_XSHMCREATEPIXMAP) && \
    defined(HAVE_X11_EXTENSIONS_XSHM_H) && \
    defined(HAVE_SYS_IPC_H) && \
    defined(HAVE_SYS_SHM_H)
#define XSHM_SUPPORT 1
#else
#define XSHM_SUPPORT 0
#endif
#endif /* XSHM_SUPPORT */

#if XSHM_SUPPORT
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif

#define MAX_PLANES		8
#define MAX_COLORS		(1<<MAX_PLANES)
#define MAX_VIRTUAL_SCREENS	65536
#define MAX_PALETTES		256
#define MAX_VIRTUAL_PALETTES	65536
#define REAL_SCREEN_SIZE_X	640
#define REAL_SCREEN_SIZE_Y	480
#define CHAR_WIDTH1		8
#define CHAR_WIDTH2		16
#define CHAR_HEIGHT		16
#define SHERRY_MAX_VALUE	65536
#define JISX0201 "-*-fixed-*-r-normal--16-*-*-*-*-*-jisx0201.1976-*"
#define JISX0208 "-*-fixed-*-r-normal--16-*-*-*-*-*-jisx0208.1983-*"

/* (mask & src) | (~mask & dst) equals as Windows API BitBlt ROP 0x00CA0749 */
/* #define ROP3_CA0749(ptn, src, dst) (((ptn) & (src)) | (~(ptn) & (dst))) */
#define ROP3_CA0749(ptn, src, dst) ((dst) ^ ((ptn) & ((src) ^ (dst))))

typedef struct _ImagePixmap
{
    Pixmap		pm;
    XImage		*im;
    GC			gc;
    int			depth;
#if XSHM_SUPPORT
    XShmSegmentInfo	shminfo;
#endif /* XSHM_SUPPORT */
} ImagePixmap;

typedef struct _VirtualScreen
{
    uint16 width, height;
    uint8 transParent;
    uint8 data[1]; /* Pseudo 256 color, size = width * height */
} VirtualScreen;

typedef struct _SherryPaletteEntry
{
    uint8 r;
    uint8 g;
    uint8 b;
} SherryPaletteEntry;

typedef struct _SherryPalette
{
    SherryPaletteEntry entry[MAX_PALETTES];
} SherryPalette;


static Display	*theDisplay;
static int	theScreen;

/* real screen */
static Window	theWindow;
static ImagePixmap *theRealScreen;
static Visual	*theVisual;	/* Visual of main window */
static Colormap	theColormap;	/* Colormap of main window */
static int	theDepth;	/* Depth of main window */
static int	theClass;	/* Visual class */
static XFontStruct *theFont8;	/* 8bit Font */
static XFontStruct *theFont16;	/* 16bit Font */
static int lbearing8, lbearing16, ascent8, ascent16;
static GC theGC;

/* For memory draw string */
#define IMAGEBITMAPLEN 40
static ImagePixmap *imageBitmap;
static int bitmap_drawimage(ImagePixmap *ip, char *sjis_str, int nbytes);


static VirtualScreen **virtualScreen; /* MAX_VIRTUAL_SCREENS */
static VirtualScreen  *tmpScreen;
static VirtualScreen  *alloc_vscreen(int width, int height, int transParent);
static void	       free_vscreen(VirtualScreen *scr);

#define VSCREEN_PIXEL(scr, x, y) ((scr)->data[(scr)->width * (y) + (x)])

static unsigned long basePixel;	/* base pixel */
static unsigned long planePixel[MAX_PLANES]; /* plane pixel */
static unsigned long palette2Pixel[MAX_COLORS];
static int currentPalette;

static SherryPalette **virtualPalette; /* MAX_VIRTUAL_PALETTES, ۥѥå */
static SherryPaletteEntry realPalette[MAX_PALETTES];
static uint8 *pseudoImage = NULL; /* For TrueColor */

static int draw_ctl_flag = True;
static int err_to_stop = 0;


/* Image Pixmap */
static ImagePixmap *create_image_pixmap(int width, int height, int depth);
static void clear_image_pixmap(ImagePixmap *ip, unsigned long pixel);
static void free_image_pixmap(ImagePixmap *ip);

#if XSHM_SUPPORT
static ImagePixmap *create_shm_image_pixmap(int width, int height, int depth);
#endif /* XSHM_SUPPORT */

static int isRealPaletteChanged, isRealScreenChanged;
static int updateClipX1, updateClipY1, updateClipX2, updateClipY2;

static int check_range(VirtualScreen *scr, int x1, int y1, int x2, int y2);


static Window try_create_window(Display *disp, int width, int height,
				int want_class, int want_depth,
				Visual **newVisual,
				Colormap *newColormap)
{
    Visual *defVisual;
    int defScreen;
    int defDepth;

    XSetWindowAttributes xswa;
    unsigned long xswamask;
    int numvis;

    defScreen = DefaultScreen(disp);
    defVisual = DefaultVisual(disp, defScreen);
    defDepth  = DefaultDepth(disp, defScreen);

    if(want_class == -1)
	want_class = defVisual->class;
    if(want_depth == -1)
	want_depth = defDepth;

    if(defVisual->class == want_class && defDepth == want_depth)
	*newVisual = defVisual;
    else
    {
	XVisualInfo *vinfo, rvinfo;

	rvinfo.class  = want_class;
	rvinfo.screen = defScreen;
	rvinfo.depth = want_depth;
	vinfo = XGetVisualInfo(disp, 
			       VisualClassMask |
			       VisualScreenMask |
			       VisualDepthMask,
			       &rvinfo, &numvis);
	if(vinfo == NULL)
	    return None;
	if(numvis == 0)
	{
	    XFree((char *)vinfo);
	    return None;
	}

	*newVisual = vinfo[0].visual;
    }
    *newColormap = XCreateColormap(disp,
				   RootWindow(disp, defScreen),
				   *newVisual, AllocNone);

    xswa.background_pixel = 0;
    xswa.border_pixel     = 0;
    xswa.colormap         = *newColormap;
    xswamask = CWBackPixel | CWBorderPixel | CWColormap;
    return XCreateWindow(disp, RootWindow(disp, defScreen),
			 0, 0, width, height, 0, want_depth,
			 InputOutput, *newVisual, xswamask, &xswa);
}


/* Create real-screen for Sherry WRD Window
 * sutable theVisual, theColormap, theDepth, theClass is selected.
 */
static Window create_main_window(int want_class, int want_depth)
{
    Window win;
    int i;
    struct {
	int want_class, want_depth;
    } target_visuals[] = {
    {TrueColor, 24},
    {TrueColor, 32},
    {TrueColor, 16},
    {PseudoColor, 8},
    {-1, -1}};

    if(want_class != -1)
    {
	win = try_create_window(theDisplay,
				REAL_SCREEN_SIZE_X, REAL_SCREEN_SIZE_Y,
				want_class, want_depth,
				&theVisual, &theColormap);
	theClass = want_class;
	theDepth = want_depth;
	return win;
    }

    for(i = 0; target_visuals[i].want_class != -1; i++)
	if((win = try_create_window(theDisplay,
				    REAL_SCREEN_SIZE_X, REAL_SCREEN_SIZE_Y,
				    target_visuals[i].want_class,
				    target_visuals[i].want_depth,
				    &theVisual, &theColormap)) != None)
	{
	    theClass = target_visuals[i].want_class;
	    theDepth = target_visuals[i].want_depth;
	    return win;
	}

    /* Use default visual */
    if((win = try_create_window(theDisplay,
				REAL_SCREEN_SIZE_X, REAL_SCREEN_SIZE_Y,
				-1, -1, &theVisual, &theColormap)) != None)
    {
	theClass = theVisual->class;
	theDepth = DefaultDepth(theDisplay, theScreen);
	return win;
    }
    return None;
}

static GC createGC(Display *disp, Drawable draw)
{
    XGCValues gv;
    gv.graphics_exposures = False;
    return XCreateGC(disp, draw, GCGraphicsExposures, &gv);
}

static int highbit(unsigned long ul)
{
    int i;  unsigned long hb;
    hb = 0x80000000UL;
    for(i = 31; ((ul & hb) == 0) && i >= 0;  i--, ul<<=1)
	;
    return i;
}

static inline unsigned long trueColorPixel(unsigned long r, /* 0..255 */
					   unsigned long g, /* 0..255 */
					   unsigned long b) /* 0..255 */
{
    static int rs, gs, bs;

    if(r == 0xffffffff) /* for initialize */
    {
	rs = 15 - highbit(theVisual->red_mask);
	gs = 15 - highbit(theVisual->green_mask);
	bs = 15 - highbit(theVisual->blue_mask);
	return 0;
    }

    r *= 257; /* 0..65535 */
    g *= 257; /* 0..65535 */
    b *= 257; /* 0..65535 */
    if(rs < 0)	r <<= -rs;
    else	r >>=  rs;
    if(gs < 0)	g <<= -gs;
    else	g >>=  gs;
    if(bs < 0)	b <<= -bs;
    else	b >>=  bs;
    r &= theVisual->red_mask;
    g &= theVisual->green_mask;
    b &= theVisual->blue_mask;

    return r | g | b;
}

static Bool init_palette(void)
{
    static int first_flag = 1;
    int i, j;
    XColor xcolors[MAX_COLORS];

    if(first_flag)
    {
	first_flag = 0;

	if(theClass == TrueColor)
	{
	    basePixel = 0;
	    trueColorPixel(0xffffffff, 0, 0);
	    return True;
	}

	if(!XAllocColorCells(theDisplay, theColormap, False,
			     planePixel, MAX_PLANES,
			     &basePixel, 1))
	    return False;
    }

    if(theClass == TrueColor)
	return True;

    for(i = 0; i < MAX_COLORS; i++)
    {
	palette2Pixel[i] = basePixel;
	for(j = 0; j < MAX_PLANES; j++)
	    if((i & (1 << j)) != 0) palette2Pixel[i] |= planePixel[j];
    }

    for(i = 0; i < MAX_COLORS; i++)
    {
	xcolors[i].pixel = palette2Pixel[i];
	xcolors[i].red   = 0;
	xcolors[i].green = 0;
	xcolors[i].blue  = 0;
	xcolors[i].flags = DoRed | DoGreen | DoBlue;
    }
    XStoreColors(theDisplay, theColormap, xcolors, MAX_COLORS);
    return True;
}

static Bool init_font(void)
{
    if((theFont8 = XLoadQueryFont(theDisplay, JISX0201)) == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Can't load font", JISX0201);
	return False;
    }
    if((theFont16 = XLoadQueryFont(theDisplay, JISX0208)) == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Can't load font", JISX0208);
	XFreeFont(theDisplay, theFont8);
	return False;
    }

    lbearing8 = theFont8->min_bounds.lbearing;
    ascent8   = theFont8->max_bounds.ascent;
    lbearing16 = theFont16->min_bounds.lbearing;
    ascent16   = theFont16->max_bounds.ascent;

    return True;
}

static void free_font(void)
{
    XFreeFont(theDisplay, theFont8);
    XFreeFont(theDisplay, theFont16);
}

/* open */
/* return:
 * -1: error
 *  0: success
 *  1: already initialized
 */
static int x_sry_open(char *opts)
{
    static Bool error_flag = False;
    int i;
    Bool try_pseudo;

    if(error_flag)
    {
	if(error_flag == -1)
	    ctl->cmsg(CMSG_ERROR, VERB_NOISY,
		      "Sherry WRD: Can't work because of error");
	else if(error_flag == 1) /* Already initialized */
	{
	    XMapWindow(theDisplay, theWindow);
	    clear_image_pixmap(theRealScreen, basePixel);
	    XFlush(theDisplay);
	    return 0;
	}
	return error_flag;
    }

    try_pseudo = 0;
    if(opts)
    {
	if(strchr(opts, 'p'))
	    try_pseudo = 1;
    }

    if((theDisplay = XOpenDisplay(NULL)) == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: Can't open display: %s", XDisplayName(NULL));
	error_flag = -1;
	return -1;
    }

    if(!init_font())
    {
	XCloseDisplay(theDisplay);
	theDisplay = NULL;
	error_flag = -1;
	return -1;
    }

    theScreen = DefaultScreen(theDisplay);
    if(try_pseudo)
	theWindow = create_main_window(PseudoColor, 8);
    else
	theWindow = create_main_window(-1, -1);
    if(theWindow == None)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: Can't create Sherry WRD Window");
	free_font();
	XCloseDisplay(theDisplay);
	theDisplay = NULL;
	error_flag = -1;
	return -1;
    }
    XSelectInput(theDisplay, theWindow, ExposureMask);

#if XSHM_SUPPORT
    theRealScreen = create_shm_image_pixmap(REAL_SCREEN_SIZE_X,
					    REAL_SCREEN_SIZE_Y,
					    theDepth);
    imageBitmap = create_shm_image_pixmap(IMAGEBITMAPLEN * CHAR_WIDTH1,
					  CHAR_HEIGHT,
					  theDepth);
#else
    theRealScreen = create_image_pixmap(REAL_SCREEN_SIZE_X,
					REAL_SCREEN_SIZE_Y,
					theDepth);
    imageBitmap = create_image_pixmap(IMAGEBITMAPLEN * CHAR_WIDTH1,
				      CHAR_HEIGHT,
				      1);
#endif

    init_palette();
    XSetWindowBackground(theDisplay, theWindow, basePixel);
    XSetForeground(theDisplay, imageBitmap->gc, 1);
    XSetBackground(theDisplay, imageBitmap->gc, 0);

    theGC = createGC(theDisplay, theWindow);

    virtualScreen = (VirtualScreen **)safe_malloc(MAX_VIRTUAL_SCREENS *
						  sizeof(VirtualScreen *));
    for(i = 0; i < MAX_VIRTUAL_SCREENS; i++)
	virtualScreen[i] = NULL;
    tmpScreen = NULL;

    virtualPalette = (SherryPalette **)safe_malloc(MAX_VIRTUAL_PALETTES *
						   sizeof(SherryPalette *));
    for(i = 0; i < MAX_VIRTUAL_PALETTES; i++)
	virtualPalette[i] = NULL;
    memset(realPalette, 0, sizeof(realPalette));

    if(theClass == TrueColor)
    {
	pseudoImage = (uint8 *)safe_malloc(REAL_SCREEN_SIZE_X *
					   REAL_SCREEN_SIZE_Y);
	memset(pseudoImage, 0, REAL_SCREEN_SIZE_X * REAL_SCREEN_SIZE_Y);
    }

    XMapWindow(theDisplay, theWindow);
    XFlush(theDisplay);
    error_flag = 1;

    return 0;
}

void CloseSryWindow(void)
{
    XUnmapWindow(theDisplay, theWindow);
    XFlush(theDisplay);
}

int OpenSryWindow(char *opts)
{
    return x_sry_open(opts);
}

void x_sry_close(void)
{
    int i;

    if(theDisplay == NULL)
	return;
    free_image_pixmap(theRealScreen);
    free_image_pixmap(imageBitmap);
    for(i = 0; i < MAX_VIRTUAL_SCREENS; i++)
	if(virtualScreen[i] != NULL)
	{
	    free_vscreen(virtualScreen[i]);
	    virtualScreen[i] = NULL;
	}
    free(virtualScreen);
    if(tmpScreen != NULL)
    {
	free(tmpScreen);
	tmpScreen = NULL;
    }
    for(i = 0; i < MAX_VIRTUAL_PALETTES; i++)
	if(virtualPalette[i] != NULL)
	{
	    free(virtualPalette[i]);
	    virtualPalette[i] = NULL;
	}
    free(virtualPalette);
    if(pseudoImage)
	free(pseudoImage);
    XFreeFont(theDisplay, theFont8);
    XFreeFont(theDisplay, theFont16);
    XCloseDisplay(theDisplay);
    theDisplay = NULL;
}

static void sry_new_vpal(uint8 *data, int len)
{
    int i, n;

    for(i = 0; i < len-1; i += 2)
    {
	n = SRY_GET_SHORT(data + i) & 0xffff;
#ifdef SRY_DEBUG
	printf("NEW palette %d\n", n);
#endif /* SRY_DEBUG */
	if(virtualPalette[n] == NULL)
	    virtualPalette[n] =
		(SherryPalette *)safe_malloc(sizeof(SherryPalette));
	memset(virtualPalette[n], 0, sizeof(SherryPalette));
    }
}

static void sry_free_vpal(uint8 *data, int len)
{
    int i, n;

    for(i = 0; i < len-1; i += 2)
    {
	n = SRY_GET_SHORT(data + i) & 0xffff;
	if(virtualPalette[n] != NULL)
	{
	    free(virtualPalette[n]);
	    virtualPalette[n] = NULL;
	}
    }
}

static void sry_new_vram(uint8 *data, int len)
{
    int i, n, width, height, c;

    for(i = 0; i < len-6; i += 7)
    {
	n      = SRY_GET_SHORT(data + i) & 0xffff;
	width  = SRY_GET_SHORT(data + i + 2) & 0xffff;
	height = SRY_GET_SHORT(data + i + 4) & 0xffff;
	c      = data[6];
#ifdef SRY_DEBUG
	printf("sherry: new vram[%d] %dx%d (t=%d)\n",
	       n, width, height, c);
#endif /* SRY_DEBUG */

	if(virtualScreen[n] != NULL)
	    free_vscreen(virtualScreen[n]);
	virtualScreen[n] = alloc_vscreen(width, height, c);
    }
}

static void sry_free_vram(uint8 *data, int len)
{
    int i, n;
    for(i = 0; i < len-1; i += 2)
    {
	n = SRY_GET_SHORT(data + i) & 0xffff;
	if(virtualScreen[n] != NULL)
	{
	    free_vscreen(virtualScreen[n]);
	    virtualScreen[n] = NULL;
	}
    }
}

static void sry_pal_set(uint8 *data, int len)
{
    int pg, i, p;
    SherryPaletteEntry *entry;

    pg = SRY_GET_SHORT(data) & 0xffff;
    if(virtualPalette[pg] == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "WRD Sherry: virtual palette %d is not allocated", pg);
	err_to_stop = 1;
	return;
    }
    entry = virtualPalette[pg]->entry;
    data += 2;
    len -= 2;

#ifdef SRY_DEBUG
    printf("set palette color of %d (n=%d)\n", pg, len / 4);
#endif /* SRY_DEBUG */

    for(i = 0; i < len - 3; i += 4)
    {
	p = data[i];
	entry[p].r = data[i + 1];
	entry[p].g = data[i + 2];
	entry[p].b = data[i + 3];
    }
}

static void update_real_screen(int x, int y, int width, int height)
{
  if(!draw_ctl_flag)
    return;
#if XSHM_SUPPORT
    if(theRealScreen->shminfo.shmid != -1)
    {
	XShmPutImage(theDisplay, theWindow, theGC, theRealScreen->im,
		     x, y, x, y, width, height, False);
	return;
    }
#endif

    XPutImage(theDisplay, theWindow, theGC, theRealScreen->im,
	      x, y, x, y, width, height);
}

static void updateScreen(int sx, int sy, int width, int height)
{
    if(theClass == TrueColor)
    {
	XImage *im;
	int x, y, c;
	int units_per_line;
	unsigned long pixels[MAX_COLORS];

	for(c = 0; c < MAX_COLORS; c++)
	    pixels[c] = trueColorPixel(realPalette[c].r,
				       realPalette[c].g,
				       realPalette[c].b);

	im = theRealScreen->im;
	units_per_line = im->bytes_per_line / (im->bits_per_pixel / 8);

	switch(im->bits_per_pixel)
	{
#if 1
	  case 8:
	    for(y = 0; y < height; y++)
		for(x = 0; x < width; x++)
		{
		    c = pseudoImage[(sy + y) * REAL_SCREEN_SIZE_X + sx + x];
		    im->data[(sy + y) * units_per_line + sx + x] = pixels[c];
		}
	    break;
	  case 16:
	    for(y = 0; y < height; y++)
		for(x = 0; x < width; x++)
		{
		    c = pseudoImage[(sy + y) * REAL_SCREEN_SIZE_X + sx + x];
		    ((uint16 *)im->data)[(sy + y) * units_per_line + sx + x]
			= pixels[c];
		}
	    break;
	  case 32:
	    for(y = 0; y < height; y++)
		for(x = 0; x < width; x++)
		{
		    c = pseudoImage[(sy + y) * REAL_SCREEN_SIZE_X + sx + x];
		    ((uint32 *)im->data)[(sy + y) * units_per_line + sx + x]
			= pixels[c];
		}
	    break;
#endif
	  default: /* Generic routine */
	    for(y = 0; y < height; y++)
		for(x = 0; x < width; x++)
		{
		    c = pseudoImage[(sy + y) * REAL_SCREEN_SIZE_X + sx + x];
		    XPutPixel(theRealScreen->im, sx + x, sy + y, pixels[c]);
		}
	    break;
	}
    }

    update_real_screen(sx, sy, width, height);
    isRealScreenChanged = 0;
}

static void updatePalette(void)
{
    int i;
    XColor xc[256];

    if(theClass != TrueColor)
    {
	for(i = 0; i < MAX_COLORS; i++)
	{
	    xc[i].pixel = palette2Pixel[i];
	    xc[i].red   = realPalette[i].r * 257;
	    xc[i].green = realPalette[i].g * 257;
	    xc[i].blue  = realPalette[i].b * 257;
	    xc[i].flags = DoRed | DoGreen | DoBlue;
	}
	XStoreColors(theDisplay, theColormap, xc, MAX_COLORS);
    }
    else /* True color */
    {
	updateScreen(0, 0, REAL_SCREEN_SIZE_X, REAL_SCREEN_SIZE_Y);
	isRealScreenChanged = 0;
    }
    isRealPaletteChanged = 0;
}


static void sry_pal_v2r(uint8 *data)
{
    int n;
    n = SRY_GET_SHORT(data) & 0xffff;

#ifdef SRY_DEBUG
    printf("Transfer palette %d\n", n);
#endif /* SRY_DEBUG */

    if(virtualPalette[n] == NULL)
    {
	virtualPalette[n] =
	    (SherryPalette *)safe_malloc(sizeof(SherryPalette));
	memset(virtualPalette[n], 0, sizeof(SherryPalette));
    }

    memcpy(realPalette, virtualPalette[n]->entry, sizeof(realPalette));
    isRealPaletteChanged = 1;
    currentPalette = n;
}

static void png_read_func(png_structp png_ptr, char *buff, size_t n)
{
    struct timidity_file *tf = png_get_io_ptr(png_ptr);
    tf_read(buff, 1, n, tf);
}

#define PNG_BYTES_TO_CHECK	8
#define MAX_SCREEN_COLORS	256
static void sry_load_png(uint8 *data)
{
    int i;
    int x, y;
    int screen, vpalette;
    png_structp pngPtr;
    png_infop infoPtr;
    png_infop endInfo;
    VirtualScreen *scr;
    SherryPaletteEntry *entry;

    char *filename;
    struct timidity_file *tf;
    char sig[PNG_BYTES_TO_CHECK];

    png_uint_32 width, height;
    int bitDepth, colorType, interlaceType, compressionType, filterType;

    int numPalette;
    png_colorp palette;
    png_uint_16p hist;
    png_bytep *rowPointers;
    png_uint_32 rowbytes;
    png_bytep trans;
    int transParent;
    int numTrans;
    png_color_16p transValues;

    numPalette = -1;
    memset(&palette, 0, sizeof(palette));
    hist = NULL;
    rowbytes = 0;
    rowPointers = NULL;
    trans = NULL;
    numTrans = 0;
    transValues = NULL;

    screen = SRY_GET_SHORT(data) & 0xffff;
    vpalette = SRY_GET_SHORT(data + 2) & 0xffff;
    filename = (char *)data + 4;

#ifdef SRY_DEBUG
    printf("Load png: %s: scr=%d pal=%d\n", filename, screen, vpalette);
#endif /* SRY_DEBUG */

    if((tf = wrd_open_file(filename)) == NULL)
    {
	err_to_stop = 1;
	return;
    }
    if(tf_read(sig, 1, sizeof(sig), tf) != sizeof(sig))
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "WRD Sherry: %s is too short to be png format.", filename);
	err_to_stop = 1;
	return;
    }
    if(png_sig_cmp((unsigned char*)sig, 0, sizeof(sig)))
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Not a png file", filename);
	err_to_stop = 1;
	return;
    }


    pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
				    NULL, NULL, NULL);
    infoPtr = png_create_info_struct(pngPtr);
    endInfo = png_create_info_struct(pngPtr);

    png_set_read_fn(pngPtr, (void *)tf, (png_rw_ptr)png_read_func);
    png_set_sig_bytes(pngPtr, sizeof(sig));
    png_read_info(pngPtr, infoPtr);

    /* get info */
    png_get_IHDR(pngPtr, infoPtr, &width, &height,
		 &bitDepth, &colorType,
		 &interlaceType, &compressionType, &filterType);
    
    /* transformation */
    /* contert to 256 palette */
    if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)
	png_set_expand(pngPtr);
    if(bitDepth == 16)
	png_set_strip_16(pngPtr);
    if(bitDepth < 8)
	png_set_packing(pngPtr);
    if(colorType == PNG_COLOR_TYPE_GRAY ||
       colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
	png_set_gray_to_rgb(pngPtr);

#if 0
  double screenGamma;
  const char* const gammaStr = getenv ("SCREEN_GAMMA");
  if (gammaStr != NULL)
    {
      screenGamma = atof (gammaStr);
    }
  else
    {
      screenGamma = 2.2;
/*
      screenGamms = 2.0;
      screenGamms = 1.7;
      screenGamms = 1.0;
*/
    }

  if (png_get_gAMA (pngPtr, infoPtr, &gamma))
    png_set_gamma (pngPtr, screenGamma, gamma);
  else
    png_set_gamma (pngPtr, screenGamma, 0.50);
#endif

    if(png_get_valid(pngPtr, infoPtr, PNG_INFO_PLTE))
    {
	png_get_PLTE(pngPtr, infoPtr, &palette, &numPalette);
	if(numPalette > MAX_SCREEN_COLORS)
	{
	    if(png_get_valid(pngPtr, infoPtr, PNG_INFO_hIST))
		png_get_hIST(pngPtr, infoPtr, &hist);
#if PNG_LIBPNG_VER >= 10402
	    png_set_quantize(pngPtr, palette,
#else
	    png_set_dither(pngPtr, palette,
#endif
			   numPalette, MAX_SCREEN_COLORS, hist, 1);
	}
    }
    else
    {
	/* XXX */
	/* NOTE 6*7*6 = 252 */
	/* 6*7*6 = 5*7*6 + 6*6 + 6 */
	png_color stdColorCube[6*7*6];
	png_byte r, g, b;

	for(r = 0; r < 6; r++)
	{
	    for(g = 0; g < 7; g++)
	    {
		for(b = 0; b < 6; b++)
		{
		    png_byte index = r*7*6+g*6+b;
		    stdColorCube[index].red   = r;
		    stdColorCube[index].green = g;
		    stdColorCube[index].blue  = b;
		}
	    }
	}
#if PNG_LIBPNG_VER >= 10402
	png_set_quantize(pngPtr, stdColorCube,
#else
	png_set_dither(pngPtr, stdColorCube,
#endif
		       6*7*6, MAX_SCREEN_COLORS,
		       NULL, 1);
	png_get_PLTE(pngPtr, infoPtr, &palette, &numPalette);
    }

    if(png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS))
	png_get_tRNS(pngPtr, infoPtr, &trans, &numTrans, &transValues);

    png_read_update_info(pngPtr, infoPtr);

    rowbytes = png_get_rowbytes(pngPtr, infoPtr);
    /* rowbytes == width */
    rowPointers = (png_bytep *)safe_malloc(height * sizeof(png_bytep));
    rowPointers[0] = (png_byte *)safe_malloc(rowbytes * height *
					     sizeof(png_byte));
    for(i = 1; i < height; i++)
	rowPointers[i] = rowPointers[0] + i * rowbytes;

    png_read_image(pngPtr, rowPointers);
    png_read_end(pngPtr, endInfo);

    if(trans == NULL || numTrans == 0)
	transParent = 0;
    else
	transParent = trans[0] & 0xff;

    scr = virtualScreen[screen];
    if(scr != NULL && (scr->width != width || scr->height != height))
    {
	free_vscreen(scr);
	scr = NULL;
    }
    if(scr == NULL)
	scr = virtualScreen[screen] =
	    alloc_vscreen(width, height*2, transParent);
    else
	scr->transParent = transParent;

    if(virtualPalette[vpalette] == NULL)
    {
	virtualPalette[vpalette] =
	    (SherryPalette *)safe_malloc(sizeof(SherryPalette));
	memset(virtualPalette[vpalette], 0, sizeof(SherryPalette));
    }

    entry = virtualPalette[vpalette]->entry;
    for(i = 0; i < numPalette; i++)
    {
	entry[i].r = palette[i].red;
	entry[i].g = palette[i].green;
	entry[i].b = palette[i].blue;
    }

    for(y = 0; y < height; y++)
	for(x = 0; x < width; x++)
	    VSCREEN_PIXEL(scr, x, y) = rowPointers[y][x];

    free(rowPointers[0]);
    free(rowPointers);
    png_destroy_read_struct(&pngPtr, &infoPtr, &endInfo);
    close_file(tf);
#ifdef SRY_DEBUG
    printf("Load png ok: width=%d height=%d\n", width, height);
#endif /* SRY_DEBUG */
}

static void sry_trans_all(uint8 *data)
{
    int screen, x, y;
    int sx, sy;
    VirtualScreen *scr;
    XImage *im;

    screen = SRY_GET_SHORT(data + 0) & 0xffff;
    sx     = SRY_GET_SHORT(data + 2) & 0xffff;
    sy     = SRY_GET_SHORT(data + 4) & 0xffff;
    im = theRealScreen->im;
    scr = virtualScreen[screen];

#ifdef SRY_DEBUG
    printf("Trans all: %d:(%d,%d)\n", screen, sx, sy);
#endif /* SRY_DEBUG */

    if(!check_range(scr, sx, sy,
		    sx + REAL_SCREEN_SIZE_X - 1, sy + REAL_SCREEN_SIZE_Y - 1))
	return;

    if(theClass != TrueColor)
    {
	for(y = 0; y < REAL_SCREEN_SIZE_Y; y++)
	    for(x = 0; x < REAL_SCREEN_SIZE_X; x++)
		XPutPixel(im, x, y, VSCREEN_PIXEL(scr, sx + x, sy + y));
    }
    else /* TrueColor */
    {
	for(y = 0; y < REAL_SCREEN_SIZE_Y; y++)
	    memcpy(pseudoImage + y * REAL_SCREEN_SIZE_X,
		   scr->data + (sy + y) * scr->width + sx,
		   REAL_SCREEN_SIZE_X);
    }

    isRealScreenChanged = 1;
    updateClipX1 = 0;
    updateClipY1 = 0;
    updateClipX2 = REAL_SCREEN_SIZE_X-1;
    updateClipY2 = REAL_SCREEN_SIZE_Y-1;
}

static void sry_trans_partial_real(uint8 *data)
{
    int screen, sx, sy, tx, ty, w, h, x, y;
    int x2, y2;
    VirtualScreen *scr;
    XImage *im;

    screen = SRY_GET_SHORT(data + 0) & 0xffff;
    sx     = SRY_GET_SHORT(data + 2) & 0xffff;
    sy     = SRY_GET_SHORT(data + 4) & 0xffff;
    tx     = SRY_GET_SHORT(data + 6) & 0xffff;
    ty     = SRY_GET_SHORT(data + 8) & 0xffff;
    w      = SRY_GET_SHORT(data +10) & 0xffff;
    h      = SRY_GET_SHORT(data +12) & 0xffff;
    x2     = tx + w - 1;
    if(x2 >= REAL_SCREEN_SIZE_X)
    {
	x2 = REAL_SCREEN_SIZE_X - 1;
	w = x2 - tx + 1;
    }
    y2     = ty + h - 1;
    if(y2 >= REAL_SCREEN_SIZE_Y)
    {
	y2 = REAL_SCREEN_SIZE_Y - 1;
	h = y2 - ty + 1;
    }

#ifdef SRY_DEBUG
    printf("Trans partial: %d:(%d,%d) (%d,%d) %dx%d\n",
	   screen, sx, sy, tx, ty, w, h);
#endif /* SRY_DEBUG */

    im = theRealScreen->im;
    scr = virtualScreen[screen];

    if(theClass != TrueColor)
    {
	for(y = 0; y < h; y++)
	    for(x = 0; x < w; x++)
		XPutPixel(im, tx + x, ty + y,
			  VSCREEN_PIXEL(scr, sx + x, sy + y));
    }
    else
    {
	for(y = 0; y < h; y++)
	{
	    memcpy(pseudoImage + (ty + y) * REAL_SCREEN_SIZE_X + tx,
		   scr->data   + (sy + y) * scr->width + sx,
		   w);
	}
    }

    if(isRealScreenChanged)
    {
	/*???*/
	x_sry_update();
    }
    updateClipX1 = tx;
    updateClipY1 = ty;
    updateClipX2 = x2;
    updateClipY2 = y2;
    isRealScreenChanged = 1;
}

static VirtualScreen *get_tmp_screen(int width_require,
				     int height_require)
{
    if(tmpScreen == NULL)
    {
	if(width_require < REAL_SCREEN_SIZE_X)
	    width_require = REAL_SCREEN_SIZE_X;
	if(height_require < REAL_SCREEN_SIZE_Y)
	    height_require = REAL_SCREEN_SIZE_Y;
	return tmpScreen = alloc_vscreen(width_require, height_require, 0);
    }
    if(tmpScreen->width * tmpScreen->height >= width_require * height_require)
    {
	tmpScreen->width = width_require;
	tmpScreen->height = height_require;
	return tmpScreen;
    }
    if(tmpScreen)
	free_vscreen(tmpScreen);
    return tmpScreen = alloc_vscreen(width_require, height_require, 0);
}

static void normalize_rect(int *x1, int *y1, int *x2, int *y2)
{
    int w, h, x0, y0;

    w = *x2 - *x1;
    if(w >= 0)
	x0 = *x1;
    else
    {
	w = -w;
	x0 = *x2;
    }

    h = *y2 - *y1;
    if(h >= 0)
	y0 = *y1;
    else
    {
	h = -h;
	y0 = *y2;
    }

    *x1 = x0;
    *y1 = y0;
    *x2 = x0 + w;
    *y2 = y0 + h;
}

static int check_range(VirtualScreen *scr,
		       int x1, int y1, int x2, int y2)
{
    if(x1 < 0 ||
       y1 < 0 ||
       x2 >= scr->width ||
       y2 >= scr->height ||
       x1 > x2 ||
       y1 > y2)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: Out of range: (%d,%d)-(%d,%d)"
		  " > %dx%d, stop sherring",
		  x1, y1, x2, y2, scr->width, scr->height);
	err_to_stop = 1;
	return 0;
    }
    return 1;
}

static VirtualScreen *virtual_screen(int scr)
{
    if(virtualScreen[scr] == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: screen %d is not allocated, stop sherring",
		  scr);
	err_to_stop = 1;
	return NULL;
    }
    return virtualScreen[scr];
}

static void copy_vscreen_area(VirtualScreen *src,
			      VirtualScreen *dest,
			      int src_x1,
			      int src_y1,
			      int src_x2,
			      int src_y2,
			      int dest_x,
			      int dest_y,
			      int mask,
			      int trans)
{
    int x, y, sw, dw;
    uint8 *source, *target;
    int ww, hh;

    sw = src->width;
    dw = dest->width;
    source = src->data;
    target = dest->data;
    ww = src_x2 - src_x1 + 1;
    hh = src_y2 - src_y1 + 1;

    if(mask == 0xff && !trans)
    {
	for(y = 0; y < hh; y++)
	{
	    memcpy(target + (dest_y + y) * dw + dest_x,
		   source + (src_y1 + y) * sw + src_x1, ww);
	}
    }
    else
    {
	int p, q, i;
	int skip_color;

	skip_color = trans? src->transParent : -1;
	for(y = 0; y < hh; y++)
	{
	    for(x = 0; x < ww; x++)
	    {
		p = source[(src_y1 + y) * sw + src_x1 + x];
		if(p == skip_color)
		    continue;
		i = (dest_y + y) * dw + dest_x + x;
		q = target[i];
		target[i] = ROP3_CA0749(mask, p, q);
	    }
	}
    }
}

static void sry_trans_partial(uint8 *data)
{
    VirtualScreen *src, *dest;
    int from, to;
    int x1, y1, x2, y2, tx, ty;
    int mask, trans;

    from = SRY_GET_SHORT(data + 0) & 0xffff;
    to   = SRY_GET_SHORT(data + 2) & 0xffff;
    mask = data[4];
    trans = data[5];
    x1   = SRY_GET_SHORT(data + 6) & 0xffff;
    y1   = SRY_GET_SHORT(data + 8) & 0xffff;
    x2   = SRY_GET_SHORT(data +10) & 0xffff;
    y2   = SRY_GET_SHORT(data +12) & 0xffff;
    tx   = SRY_GET_SHORT(data +14) & 0xffff;
    ty   = SRY_GET_SHORT(data +16) & 0xffff;

    if((src = virtual_screen(from)) == NULL)
	return;
    if((dest = virtual_screen(to)) == NULL)
	return;

#ifdef SRY_DEBUG
    printf("Screen copy: %d:(%d,%d)-(%d,%d) -> %d:(%d,%d)  mask=0x%02x trans=%d\n",
	   from, x1, y1, x2, y2, to, tx, ty,
	   mask, trans);
#endif /* SRY_DEBUG */

    normalize_rect(&x1, &y1, &x2, &y2);
    if(!check_range(src, x1, y1, x2, y2))
	return;
    if(!check_range(dest, tx, ty, tx + x2 - x1, ty + y2 - y1))
	return;

    if(src == dest)
    {
	VirtualScreen *tmp;
	tmp = get_tmp_screen(x2 + 1, y2 + 1);
	tmp->transParent = src->transParent;
	copy_vscreen_area(src, tmp, x1, y1, x2, y2, x1, y1, 0xff, 0);
	src = tmp;
    }

    copy_vscreen_area(src, dest, x1, y1, x2, y2, tx, ty, mask, trans);
}

static void sry_trans_partial_mask(uint8 *data)
{
    int from, to;
    int planeMask, transparencyFlag;
    int x1, y1, x2, y2, toX, toY;
    int maskX, maskY;
    uint8 *maskData;
    VirtualScreen *src, *dest;
    int x, y, width, height;
    int mx, my;
    int bytesPerLine;
    int srcP, dstP;

    from = SRY_GET_SHORT(data + 0) & 0xffff;
    to = SRY_GET_SHORT(data + 2) & 0xffff;
    planeMask = data[4];
    transparencyFlag = data[5];
    x1 = SRY_GET_SHORT(data + 6) & 0xffff;
    y1 = SRY_GET_SHORT(data + 8) & 0xffff;
    x2 = SRY_GET_SHORT(data +10) & 0xffff;
    y2 = SRY_GET_SHORT(data +12) & 0xffff;
    toX = SRY_GET_SHORT(data +14) & 0xffff;
    toY = SRY_GET_SHORT(data +16) & 0xffff;
    maskX = data[18];
    maskY = data[19];
    maskData = data + 20;

    width = x2 - x1 + 1;
    height = y2 - y1 + 1;

    if((src = virtual_screen(from)) == NULL)
	return;
    if((dest = virtual_screen(to)) == NULL)
	return;
    normalize_rect(&x1, &y1, &x2, &y2);
    if(!check_range(src, x1, y1, x2, y2))
	return;
    if(!check_range(dest, toX, toY, toX + x2 - x1, toY + y2 - y1))
	return;

    if(src == dest)
    {
	VirtualScreen *tmp;
	tmp = get_tmp_screen(x2 + 1, y2 + 1);
	tmp->transParent = src->transParent;
	copy_vscreen_area(src, tmp, x1, y1, x2, y2, x1, y1, 0xff, 0);
	src = tmp;
    }

    bytesPerLine = maskX;
    bytesPerLine = ((bytesPerLine + 7) & ~7); /* round up to 8 */
    bytesPerLine /= 8;

    my = y1 % maskY;
    for(y = 0; y < height; y++, my++)
    {
	if(my == maskY)
	    my = 0;
	mx = x1 % maskX;
	for(x = 0; x < width; x++, mx++)
	{
	    if(mx == maskX)
		mx = 0;
	    if(maskData[my * bytesPerLine + mx / 8] & (0x80 >> (mx%8)))
	    {
		srcP = VSCREEN_PIXEL(src, x1 + x, y1 + y);
		if(!transparencyFlag || srcP != src->transParent)
		{
		    dstP = VSCREEN_PIXEL(dest, toX + x, toY + y);
		    VSCREEN_PIXEL(dest, toX + x, toY + y) =
			ROP3_CA0749(planeMask, srcP, dstP);
		}
	    }
	}
    }
}


static void sry_draw_box(uint8 *data)
{
    int screen;
    int mask;
    int x1, y1, x2, y2, x, y;
    int color;
    VirtualScreen *v;

    screen = SRY_GET_SHORT(data + 0) & 0xffff;
    mask   = data[2];
    x1     = SRY_GET_SHORT(data + 3) & 0xffff;
    y1     = SRY_GET_SHORT(data + 5) & 0xffff;
    x2     = SRY_GET_SHORT(data + 7) & 0xffff;
    y2     = SRY_GET_SHORT(data + 9) & 0xffff;
    color  = data[11];

#ifdef SRY_DEBUG
    printf("Box %d 0x%02x (%d,%d)-(%d,%d)\n", screen, mask, x1, y1, x2, y2);
#endif /* SRY_DEBUG */

    if((v = virtual_screen(screen)) == NULL)
	return;
    normalize_rect(&x1, &y1, &x2, &y2);
    if(!check_range(v, x1, y1, x2, y2))
	return;

    if(mask == 0xff)
    {
	for(y = y1; y <= y2; y++)
	    memset(v->data + v->width * y + x1, color, x2 - x1 + 1);
    }
    else
    {
	int p;
	for(y = y1; y <= y2; y++)
	    for(x = x1; x <= x2; x++)
	    {
		int i;
		i = v->width * y + x;
		p = v->data[i];
		v->data[i] = ROP3_CA0749(mask, color, p);
	    }
    }
}

static void vscreen_drawline(VirtualScreen* scr,
			     int x1, int y1, int x2, int y2,
			     int pixel, int mask)
{
    int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag, idx;

    if(scr == NULL)
	return;

    if(x1 < 0 ||
       y1 < 0 ||
       x2 >= scr->width ||
       y2 >= scr->height)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: Out of range: (%d,%d)-(%d,%d)"
		  " > %dx%d, stop sherring",
		  x1, y1, x2, y2, scr->width, scr->height);
	err_to_stop = 1;
	return;
    }

    dx = x2 - x1; if(dx < 0) dx = -dx; /* dx = |x2-x1| */
    dy = y2 - y1; if(dy < 0) dy = -dy; /* dy = |y2-y1| */

    if(dy <= dx)
    {
	d = 2 * dy - dx;
	incr1 = 2 * dy;
	incr2 = 2 * (dy - dx);
	if(x1 > x2)
	{
	    x = x2;
	    y = y2;
	    ydirflag = -1;
	    xend = x1;
	}
	else
	{
	    x = x1;
	    y = y1;
	    ydirflag = 1;
	    xend = x2;
	}

	idx = scr->width * y + x;
	scr->data[idx] = ROP3_CA0749(mask, pixel, scr->data[idx]);
	if((y2 - y1) * ydirflag > 0)
	{
	    while(x < xend)
	    {
		x++;
		if(d < 0)
		    d += incr1;
		else
		{
		    y++;
		    d += incr2;
		}
		idx = scr->width * y + x;
		scr->data[idx] = ROP3_CA0749(mask, pixel, scr->data[idx]);
	    }
	}
	else
	{
	    while(x < xend)
	    {
		x++;
		if(d < 0)
		    d += incr1;
		else
		{
		    y--;
		    d += incr2;
		}
		idx = scr->width * y + x;
		scr->data[idx] = ROP3_CA0749(mask, pixel, scr->data[idx]);
	    }
	}		
    }
    else
    {
	d = 2 * dx - dy;
	incr1 = 2 * dx;
	incr2 = 2 * (dx - dy);
	if(y1 > y2)
	{
	    y = y2;
	    x = x2;
	    yend = y1;
	    xdirflag = -1;
	}
	else
	{
	    y = y1;
	    x = x1;
	    yend = y2;
	    xdirflag = 1;
	}
	idx = scr->width * y + x;
	scr->data[idx] = ROP3_CA0749(mask, pixel, scr->data[idx]);
	if((x2 - x1) * xdirflag > 0)
	{
	    while(y < yend)
	    {
		y++;
		if(d <0)
		    d += incr1;
		else
		{
		    x++;
		    d += incr2;
		}
		idx = scr->width * y + x;
		scr->data[idx] = ROP3_CA0749(mask, pixel, scr->data[idx]);
	    }
	}
	else
	{
	    while(y < yend)
	    {
		y++;
		if(d < 0)
		    d += incr1;
		else
		{
		    x--;
		    d += incr2;
		}
		idx = scr->width * y + x;
		scr->data[idx] = ROP3_CA0749(mask, pixel, scr->data[idx]);
	    }
	}
    }
}

static void sry_draw_vline(uint8 *data)
{
    int screen = SRY_GET_SHORT(data + 0) & 0xffff;
    int mask   = data[2];
    int x1     = SRY_GET_SHORT(data + 3) & 0xffff;
    int y1     = SRY_GET_SHORT(data + 5) & 0xffff;
    int y2     = SRY_GET_SHORT(data + 7) & 0xffff;
    int color  = data[9];

    vscreen_drawline(virtual_screen(screen),
		     x1, y1, x1, y2, color, mask);
}

static void sry_draw_hline(uint8 *data)
{
    int screen = SRY_GET_SHORT(data + 0) & 0xffff;
    int mask   = data[2];
    int x1     = SRY_GET_SHORT(data + 3) & 0xffff;
    int x2     = SRY_GET_SHORT(data + 5) & 0xffff;
    int y1     = SRY_GET_SHORT(data + 7) & 0xffff;
    int color  = data[9];

    vscreen_drawline(virtual_screen(screen),
		     x1, y1, x2, y1, color, mask);
}

static void sry_draw_line(uint8 *data)
{
    int screen = SRY_GET_SHORT(data + 0) & 0xffff;
    int mask   = data[2];
    int x1     = SRY_GET_SHORT(data + 3) & 0xffff;
    int y1     = SRY_GET_SHORT(data + 5) & 0xffff;
    int x2     = SRY_GET_SHORT(data + 7) & 0xffff;
    int y2     = SRY_GET_SHORT(data + 9) & 0xffff;
    int color  = data[11];

    vscreen_drawline(virtual_screen(screen),
		     x1, y1, x2, y2, color, mask);
}

static void sry_pal_merge(uint8 *data)
{
    int Pal1, Pal2, PalResult;
    int Pal1in, Pal1bit, Pal1out;
    int Pal2in, Pal2bit, Pal2out;
    int Per1, Per2;
    int Pal1Mask, Pal2Mask, PalMask;
    int i;
    int i1, i2;
    SherryPaletteEntry *pal_in1, *pal_in2, *pal_res;

    Pal1 = SRY_GET_SHORT(data + 0) & 0xffff;
    Pal2 = SRY_GET_SHORT(data + 2) & 0xffff;
    PalResult = SRY_GET_SHORT(data + 4) & 0xffff;
    Pal1in = data[6];
    Pal1bit = data[7];
    Pal1out = data[8];
    Pal2in = data[9];
    Pal2bit = data[10];
    Pal2out = data[11];
    Per1 = data[12];
    Per2 = data[13];

    Pal1Mask = 0xff >> ( 8 - Pal1bit );
    Pal1Mask <<= Pal1out;
    Pal2Mask = 0xff >> ( 8 - Pal2bit );
    Pal2Mask <<= Pal2out;
    PalMask  = Pal1Mask | Pal2Mask;

#ifdef SRY_DEBUG
    printf("palette merge %d(%d%%) %d(%d%%) => %d (mask=0x%x)\n",
	   Pal1, Per1,
	   Pal2, Per2,
	   PalResult,
	   PalMask);
#endif /* SRY_DEBUG */

    if(virtualPalette[Pal1] == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: palette %d is not allocated, stop sherring",
		  Pal1);
	err_to_stop = 1;
	return;
    }

    if(virtualPalette[Pal2] == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: palette %d is not allocated, stop sherring",
		  Pal2);
	err_to_stop = 1;
	return;
    }

    if(virtualPalette[PalResult] == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: palette %d is not allocated, stop sherring",
		  PalResult);
	err_to_stop = 1;
	return;
    }

    pal_in1 = virtualPalette[Pal1]->entry;
    pal_in2 = virtualPalette[Pal2]->entry;
    pal_res = virtualPalette[PalResult]->entry;

    for(i = 0; i < MAX_COLORS; i++)
    {
	if((i & PalMask) == i)
	{
	    int p;

	    i1 = (i & Pal1Mask) >> Pal1in;
	    i2 = (i & Pal2Mask) >> Pal2in;

	    p = pal_in1[i1].r * Per1 / 100 + pal_in2[i2].r * Per2 / 100;
	    if(p > 0xff) p = 0xff;
	    pal_res[i].r = p;

	    p = pal_in1[i1].g * Per1 / 100 + pal_in2[i2].g * Per2 / 100;
	    if(p > 0xff) p = 0xff;
	    pal_res[i].g = p;

	    p = pal_in1[i1].b * Per1 / 100 + pal_in2[i2].b * Per2 / 100;
	    if(p > 0xff) p = 0xff;
	    pal_res[i].b = p;
	}
    }
}

static void sry_pal_copy(uint8 *data)
{
    int pal1, pal2;

    pal1 = SRY_GET_SHORT(data + 0) & 0xffff;
    pal2 = SRY_GET_SHORT(data + 2) & 0xffff;

#ifdef SRY_DEBUG
    printf("Copy palette %d->%d\n", pal1, pal2);
#endif /* SRY_DEBUG */

    if(virtualPalette[pal1] == NULL)
    {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD: palette %d is not allocated, stop sherring",
		  pal1);
	err_to_stop = 1;
	return;
    }

    if(virtualPalette[pal2] == NULL)
	virtualPalette[pal2] =
	    (SherryPalette *)safe_malloc(sizeof(SherryPalette));
    memcpy(virtualPalette[pal2], virtualPalette[pal1], sizeof(SherryPalette));
}

static void sry_text(uint8 *data)
{
    int screen, mask, mode, bg, fg, tx, ty;
    char *str;
    int len, advance;
    VirtualScreen *scr;

    screen = SRY_GET_SHORT(data + 0);
    mask = data[2];
    mode = data[3];
    fg   = data[4];
    bg   = data[5];
    tx    = SRY_GET_SHORT(data + 6);
    ty    = SRY_GET_SHORT(data + 8);
    str  = (char *)data + 10;
    len  = strlen(str);

    if(len == 0)
	return;

#ifdef SRY_DEBUG
    printf("%d 0x%02x %d %d %d (%d,%d)\n%s\n",
	   screen, mask, mode, fg, bg, tx, ty, str);
#endif /* SRY_DEBUG */

    if((scr = virtual_screen(screen)) == NULL)
	return;

    if(!check_range(scr,
		    tx, ty, tx + CHAR_WIDTH1 * len - 1, ty + CHAR_HEIGHT - 1))
	return;

    while((advance = bitmap_drawimage(imageBitmap, str, len)) > 0)
    {
	int width, bx, by, p;
	width = advance * CHAR_WIDTH1;

	for(by = 0; by < CHAR_HEIGHT; by++)
	{
	    for(bx = 0; bx < width; bx++)
	    {
		if(XGetPixel(imageBitmap->im, bx, by))
		{
		    if(mode & 1)
		    {
			p = VSCREEN_PIXEL(scr, tx + bx, ty + by);
			VSCREEN_PIXEL(scr, tx + bx, ty + by) =
			    ROP3_CA0749(mask, fg, p);
		    }
		}
		else
		{
		    if(mode & 2)
		    {
			p = VSCREEN_PIXEL(scr, tx + bx, ty + by);
			VSCREEN_PIXEL(scr, tx + bx, ty + by) =
			    ROP3_CA0749(mask, bg, p);
		    }
		}
	    }
	}

	str += advance;
	len -= advance;
	tx  += advance * CHAR_WIDTH1;
    }
}

void x_sry_clear(void)
{
    int i;

    if(theDisplay == NULL)
	return;

    isRealPaletteChanged = 0;
    isRealScreenChanged = 0;
    err_to_stop = 0;

    init_palette();

    XSetWindowBackground(theDisplay, theWindow, basePixel);
    XClearWindow(theDisplay, theWindow);

    clear_image_pixmap(theRealScreen, basePixel);

    for(i = 0; i < MAX_VIRTUAL_SCREENS; i++)
	if(virtualScreen[i] != NULL)
	{
	    free_vscreen(virtualScreen[i]);
	    virtualScreen[i] = NULL;
	}
    if(tmpScreen)
    {
	free(tmpScreen);
	tmpScreen = NULL;
    }

    for(i = 0; i < MAX_VIRTUAL_PALETTES; i++)
	if(virtualPalette[i] != NULL)
	{
	    free(virtualPalette[i]);
	    virtualPalette[i] = NULL;
	}
    memset(realPalette, 0, sizeof(realPalette));
    if(theClass == TrueColor)
	memset(pseudoImage, 0, REAL_SCREEN_SIZE_X * REAL_SCREEN_SIZE_Y);
}


#ifdef SRY_DEBUG
static void print_command(uint8 *data, int len)
{
    int i;
    printf("sherry:");
    for(i = 0; i < len; i++)
    {
	printf(" 0x%02x", data[i]);
	if(i >= 28)
	{
	    printf("...");
	    break;
	}
    }
    printf("\n");
    fflush(stdout);
}
#endif /* SRY_DEBUG */

void x_sry_wrdt_apply(uint8 *data, int len)
{
    int op, skip_bit;

    if(err_to_stop || theDisplay == NULL)
	return;

#ifdef SRY_DEBUG
    print_command(data, len);
#endif /* SRY_DEBUG */

    op = *data++;
    len--;

    skip_bit = op & 0x80;
    if(skip_bit && aq_filled_ratio() < 0.2)
	return;
    op &= 0x7F;

    switch(op)
    {
      case 0x00: /* DataEnd */
	break;
      case 0x01:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "sherry start");
	wrd_init_path();
	x_sry_clear();
	break;
      case 0x21:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "new pal 0x21");
	sry_new_vpal(data, len);
	break;
      case 0x22:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "free pal 0x22");
	sry_free_vpal(data, len);
	break;
      case 0x25:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "new vram 0x25");
	sry_new_vram(data, len);
	break;
      case 0x26:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "free vram 0x26");
	sry_free_vram(data, len);
	break;
      case 0x27:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "load PNG 0x27");
	sry_load_png(data);
	break;
      case 0x31:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG,
		  "palette trans 0x31 (%d)", SRY_GET_SHORT(data));
	sry_pal_v2r(data);
	break;
      case 0x35:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG,
		  "image copy 0x35 (%d)", SRY_GET_SHORT(data));
	sry_trans_all(data);
	break;
      case 0x36:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG,
		  "image copy 0x36 (%d)", SRY_GET_SHORT(data));
	sry_trans_partial_real(data);
	break;
      case 0x41:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "set pal 0x41");
	sry_pal_set(data, len);
	break;
      case 0x42:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "pal merge 0x42");
	sry_pal_merge(data);
	break;
      case 0x43:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "pal copy 0x43");
	sry_pal_copy(data);
	break;
      case 0x51:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "text 0x51");
	sry_text(data);
	break;
      case 0x52:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "draw box 0x52");
	sry_draw_box(data);
	break;
      case 0x53:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "draw line 0x53");
	sry_draw_vline(data);
	break;
      case 0x54:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "draw line 0x54");
	sry_draw_hline(data);
	break;
      case 0x55:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "draw line 0x55");
	sry_draw_line(data);
	break;
      case 0x61:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "image copy 0x61");
	sry_trans_partial(data);
	break;
      case 0x62:
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "image copy 0x62");
	sry_trans_partial_mask(data);
	break;
      case 0x10:
      case 0x11:
      case 0x12:
      case 0x13:
      case 0x14:
      case 0x15:
      case 0x16:
      case 0x17:
      case 0x18:
      case 0x19:
      case 0x1a:
      case 0x1b:
      case 0x1c:
      case 0x1d:
      case 0x1e:
      case 0x1f:
      case 0x20:
      case 0x71:
      case 0x72:
      case 0x7f:
	ctl->cmsg(CMSG_WARNING, VERB_DEBUG,
		  "Sherry WRD 0x%x: not supported, ignore", op);
	break;

      default:
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
		  "Sherry WRD 0x%x: not defined, stop sherring", op);
	err_to_stop = 1;
	break;
    }
}

void x_sry_redraw_ctl(int flag)
{
    draw_ctl_flag = flag;
    if(draw_ctl_flag)
	update_real_screen(0, 0, REAL_SCREEN_SIZE_X, REAL_SCREEN_SIZE_Y);
}

void x_sry_update(void)
{
    if(err_to_stop)
	return;

#ifdef SRY_DEBUG
    puts("Update pallete & screen");
#endif /* SRY_DEBUG */

    if(!isRealPaletteChanged && !isRealScreenChanged)
	return;
    if(isRealPaletteChanged)
	updatePalette();
    if(isRealScreenChanged)
	updateScreen(updateClipX1, updateClipY1,
		     updateClipX2 - updateClipX1 + 1,
		     updateClipY2 - updateClipY1 + 1);
    XSync(theDisplay, False);
}


/**** ImagePixmap interfaces ****/
static ImagePixmap *create_image_pixmap(int width, int height, int depth)
{
    ImagePixmap *ip;

    ip = (ImagePixmap *)safe_malloc(sizeof(ImagePixmap));
    ip->pm = XCreatePixmap(theDisplay, theWindow, width, height, depth);
    ip->im = XGetImage(theDisplay, ip->pm, 0, 0, width, height,
		       AllPlanes, ZPixmap);
    ip->gc = createGC(theDisplay, ip->pm);
    ip->depth = depth;
#if XSHM_SUPPORT
    ip->shminfo.shmid = -1;
#endif

    return ip;
}

#if XSHM_SUPPORT
static int shm_error;
static int my_err_handler(Display* dpy, XErrorEvent* e)
{
    shm_error = e->error_code;
    ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
	      "Warning: X Sherry Warning: Can't create SHM Pixmap. error-code=%d",
	      shm_error);
    return shm_error;
}
static ImagePixmap *create_shm_image_pixmap(int width, int height, int depth)
{
    XErrorHandler origh;
    ImagePixmap *ip;
    int shm_depth;

    shm_depth = depth;
    ip = (ImagePixmap *)safe_malloc(sizeof(ImagePixmap));

    shm_error = 0;
    origh = XSetErrorHandler(my_err_handler);

    /* There is no need to initialize XShmSegmentInfo structure
     * before the call to XShmCreateImage.
     */
    ip->im = XShmCreateImage(theDisplay, theVisual, shm_depth,
			     ZPixmap, NULL,
			     &ip->shminfo, width, height);
    if(ip->im == NULL)
    {
	if(shm_error == 0)
	    shm_error = -1;
	goto done;
    }

    /* The next step is to create the shared memory segment.
     * The return value of shmat() should be stored both
     * the XImage structure and the shminfo structure.
     */
    ip->shminfo.shmid = shmget(IPC_PRIVATE,
			       ip->im->bytes_per_line * ip->im->height,
			       IPC_CREAT | 0777);
    if(ip->shminfo.shmid == -1)
    {
	ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
		  "X Sherry Warning: Can't create SHM Pixmap.\n"
		  "shmget: %s", strerror(errno));
	XDestroyImage(ip->im);
	ip->im = NULL;
	shm_error = -1;
	goto done;
    }
    ip->shminfo.shmaddr = ip->im->data =
	(char *)shmat(ip->shminfo.shmid, NULL, 0);
    if(ip->shminfo.shmaddr == (void *)-1)
    {
	ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
		  "X Sherry Warning: Can't create SHM Pixmap.\n"
		  "shmget: %s", strerror(errno));
	shmctl(ip->shminfo.shmid, IPC_RMID, NULL);
	XDestroyImage(ip->im);
	ip->im = NULL;
	shm_error = -1;
	goto done;
    }


    /* If readOnly is True, XShmGetImage calls will fail. */
    ip->shminfo.readOnly = False;


    /* Tell the server to attach to your shared memory segment. */
    if(XShmAttach(theDisplay, &ip->shminfo) == 0)
    {
	if(shm_error == 0)
	{
	    ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
		      "X Sherry Warning: Can't create SHM Pixmap.\n"
		      "Can't attach to the shared memory segment.");
	    shm_error = -1;
	}
	shmdt(ip->shminfo.shmaddr);
	shmctl(ip->shminfo.shmid, IPC_RMID, NULL);
	XDestroyImage(ip->im);
	ip->im = NULL;
	goto done;
    }

    XSync(theDisplay, False);		/* Wait until ready. */

    ip->pm = XShmCreatePixmap(theDisplay, theWindow, ip->im->data,
			      &ip->shminfo, width, height, shm_depth);
    if(ip->pm == None)
    {
	if(shm_error == 0)
	{
	    ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
		      "X Sherry Warning: Can't create SHM Pixmap.\n"
		      "XShmCreatePixmap() is failed");
	    shm_error = -1;
	}
	shmdt(ip->shminfo.shmaddr);
	shmctl(ip->shminfo.shmid, IPC_RMID, NULL);
	XDestroyImage(ip->im);
	ip->im = NULL;
	goto done;
    }

  done:
    XSetErrorHandler(origh);

    if(ip->im != NULL)
    {
	ip->gc = createGC(theDisplay, ip->pm);
	ip->depth = shm_depth;
    }
    else
    {
	free(ip);
	ip = create_image_pixmap(width, height, depth);
    }
    return ip;
}
#endif

static void clear_image_pixmap(ImagePixmap *ip, unsigned long pixel)
{
    int x, y, w, h;

    w = ip->im->width;
    h = ip->im->height;

    XSetForeground(theDisplay, ip->gc, pixel);
    XFillRectangle(theDisplay, ip->pm, ip->gc,
		   0, 0, ip->im->width, ip->im->height);
#if XSHM_SUPPORT
    if(ip->shminfo.shmid != -1)
	return;
#endif /* XSHM_SUPPORT */
    for(y = 0; y < h; y++)
	for(x = 0; x < w; x++){
	    XPutPixel(ip->im, x, y, pixel);
	}
}

static void free_image_pixmap(ImagePixmap *ip)
{
    XFreePixmap(theDisplay, ip->pm);

#if XSHM_SUPPORT
    if(ip->shminfo.shmid != -1)
    {
	/* To destory a shard memory XImage, you should call XShmDetach()
	 * first.
	 */
	XShmDetach(theDisplay, &ip->shminfo);

	/* Unmap shared memory segment */
	shmdt(ip->shminfo.shmaddr);

	/* Remove a shared memory ID from the system */
	shmctl(ip->shminfo.shmid, IPC_RMID, NULL);
    }
#endif /* XSHM_SUPPORT */
    XDestroyImage(ip->im);
    free(ip);
}


#define IS_MULTI_BYTE(c) (((c)&0x80) && \
			  ((0x1 <= ((c)&0x7F) && ((c)&0x7F) <= 0x1f) || \
			   (0x60 <= ((c)&0x7F) && ((c)&0x7F) <= 0x7c)))

/* bitmap_drawimage() Draws string into image
 * It returns number of bytes drawn
 */
static int bitmap_drawimage(ImagePixmap *ip, char *sjis_str, int nbytes)
{
    int write_len;
    int sjis_c1, sjis_c2;
    int x, width, height;

    x = write_len = 0;
    while(*sjis_str && write_len < IMAGEBITMAPLEN)
    {
	sjis_c1 = *sjis_str & 0xff;
	sjis_str++;
	
	if(IS_MULTI_BYTE(sjis_c1))
	{
	    int e1, e2;
	    XChar2b b;

	    if(write_len+1 == IMAGEBITMAPLEN)
		break;
	    sjis_c2 = *sjis_str & 0xff;
	    if(sjis_c2 == 0)
		break;
	    sjis_str++;

	    /* SJIS to EUC */
	    if(sjis_c2 >= 0x9f)
	    {
		if(sjis_c1 >= 0xe0)
		    e1 = sjis_c1 * 2 - 0xe0;
		else
		    e1 = sjis_c1 * 2 - 0x60;
		e2 = sjis_c2 + 2;
	    }
	    else
	    {
		if(sjis_c1 >= 0xe0)
		    e1 = sjis_c1 * 2 - 0xe1;
		else
		    e1 = sjis_c1 * 2 - 0x61;
		if(sjis_c2 >= 0x7f)
		    e2 = sjis_c2 + 0x60;
		else
		    e2 = sjis_c2 +  0x61;
	    }

	    b.byte1 = e1 & 0x7f;
	    b.byte2 = e2 & 0x7f;
	    XSetFont(theDisplay, ip->gc, theFont16->fid);
	    XDrawImageString16(theDisplay, ip->pm, ip->gc,
			       x - lbearing16, ascent16, &b, 1);
	    x += CHAR_WIDTH2;
	    write_len += 2;
	}
	else
	{
	    char c = sjis_c1;
	    XSetFont(theDisplay, ip->gc, theFont8->fid);
	    XDrawImageString(theDisplay, ip->pm, ip->gc,
			     x - lbearing8, ascent8, &c, 1);
	    x += CHAR_WIDTH1;
	    write_len++;
	}
    }

    if(write_len == 0)
	return 0; /* Terminate repeating call */

#if XSHM_SUPPORT
    if(ip->shminfo.shmid != -1)
    {
        XSync(theDisplay, 0); /* Wait until ready */
	return write_len;
    }
#endif /* XSHM_SUPPORT */

    /* XSHM is not supported.
     * Now, re-allocate XImage structure from the pixmap.
     */
    width = ip->im->width;
    height = ip->im->height;
    XDestroyImage(ip->im);
    ip->im = XGetImage(theDisplay, ip->pm, 0, 0, width, height,
		       AllPlanes, ZPixmap);

    return write_len;
}


/**** VirtualScreen intarfaces ****/
static VirtualScreen *alloc_vscreen(int width, int height, int transParent)
{
    VirtualScreen *scr;
    int size = width * height;

    /* Shared the allocated memory for data and the structure */
    scr = (VirtualScreen *)safe_malloc(sizeof(VirtualScreen) + size);
    scr->width = width;
    scr->height = height;
    scr->transParent = transParent;
    memset(scr->data, transParent, size);
    return scr;
}

static void free_vscreen(VirtualScreen *scr)
{
    free(scr);
}

void x_sry_event(void)
{
    if(QLength(theDisplay) == 0)
	XSync(theDisplay, False);
    while(QLength(theDisplay) > 0)
    {
	XEvent e;
	XNextEvent(theDisplay, &e);
	switch(e.type)
	{
	  case Expose:
	    update_real_screen(e.xexpose.x, e.xexpose.y,
			       e.xexpose.width, e.xexpose.height);
	    break;
	}
	if(QLength(theDisplay) == 0)
	    XSync(theDisplay, False);
    }
}
#endif /* ENABLE_SHERRY */