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 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
|
/* x.c
* (c) 2002 Petr 'Brain' Kulhavy
* This file is a part of the Links program, released under GPL.
*/
/* Takovej mensi problemek se scrollovanim:
*
* Mikulas a Xy zpusobili, ze scrollovani a prekreslovani je asynchronni. To znamena, ze
* je v tom peknej bordylek. Kdyz BFU scrollne s oknem, tak se zavola funkce scroll. Ta
* posle Xum XCopyArea (prekopiruje prislusny kus okna) a vygeneruje eventu
* (GraphicsExpose) na postizenou (odkrytou) oblast. Funkce XCopyArea pripadne vygeneruje
* dalsi GraphicsExpose eventu na postizenou oblast, ktera se muze objevit, kdyz je
* linksove okno prekryto jinym oknem.
*
* Funkce scroll skonci. V event handleru se nekdy v budoucnosti (treba za tyden)
* zpracuji eventy od Xu, mezi nimi i GraphicsExpose - tedy prekreslovani postizenych
* oblasti.
*
* Problem je v tom, ze v okamziku, kdy scroll skonci, neni obrazovka prekreslena do
* konzistentniho stavu (misty je garbaz) a navic se muze volat dalsi scroll. Tedy
* XCopyArea muze posunout garbaz nekam do cudu a az se dostane na radu prekreslovani
* postizenych oblasti, garbaz se uz nikdy neprekresli.
*
* Ja jsem navrhoval udelat scrollovani synchronni, to znamena, ze v okamziku, kdy scroll
* skonci, bude okno v konzistentnim stavu. To by znamenalo volat ze scrollu zpracovavani
* eventu (alespon GraphicsExpose). To by ovsem nepomohlo, protoze prekreslovaci funkce
* neprekresluje, ale registruje si bottom halfy a podobny ptakoviny a prekresluje az
* nekdy v budoucnosti. Navic Mikulas rikal, ze prekreslovaci funkce muze generovat dalsi
* prekreslovani (sice jsem nepochopil jak, ale hlavne, ze je vecirek), takze by to
* neslo.
*
* Proto Mikulas vymyslel genialni tah - takzvany "genitah". Ve funkci scroll se projede
* fronta eventu od Xserveru a vyberou se GraphicsExp(l)ose eventy a ulozi se do zvlastni
* fronty. Ve funkci na zpracovani Xovych eventu se zpracuji eventy z teto fronty. Na
* zacatku scrollovaci funkce se projedou vsechny eventy ve zvlastni fronte a updatuji se
* jim souradnice podle prislusneho scrollu.
*
* Na to jsem ja vymyslel uzasnou vymluvu: co kdyz 1. scroll vyrobi nejake postizene
* oblasti a 2. scroll bude mit jinou clipovaci oblast, ktera bude tu postizenou oblast
* zasahovat z casti. Tak to se bude jako ta postizena oblast stipat na casti a ty casti
* se posunou podle toho, jestli jsou zasazene tim 2. scrollem? Tim jsem ho utrel, jak
* zpoceny celo.
*
* Takze se to nakonec udela tak, ze ze scrollu vratim hromadu rectanglu, ktere se maji
* prekreslit, a Mikulas si s tim udela, co bude chtit. Podobne jako ve svgalib, kde se
* vrati 1 a Mikulas si prislusnou odkrytou oblast prekresli sam. Doufam jen, ze to je
* posledni verze a ze nevzniknou dalsi problemy.
*
* Ve funkci scroll tedy pribude argument struct rect_set **.
*/
/* Data od XImage se alokujou pomoci malloc. get_links_icon musi alokovat taky
* pomoci malloc.
*/
/* Pozor: po kazdem XSync nebo XFlush se musi dat
* X_SCHEDULE_PROCESS_EVENTS
* jinak to bude cekat na filedescriptoru, i kdyz to ma eventy uz ve fronte.
* -- mikulas
*/
#include "cfg.h"
#ifdef GRDRV_X
/* #define X_DEBUG */
/* #define SC_DEBUG */
#if defined(X_DEBUG) || defined(SC_DEBUG)
#define MESSAGE(a) fprintf(stderr,"%s",a);
#endif
#include "links.h"
/* Mikulas je PRASE: definuje makro "format" a navrch to jeste nechce vopravit */
#ifdef format
#undef format
#endif
#if defined(HAVE_XOPENIM) && defined(HAVE_XCLOSEIM) && defined(HAVE_XCREATEIC) && defined(HAVE_XDESTROYIC) && (defined(HAVE_XWCLOOKUPSTRING) || defined(HAVE_XUTF8LOOKUPSTRING))
#define X_INPUT_METHOD
#endif
#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#ifdef HAVE_X11_XLOCALE_H
#include <X11/Xlocale.h>
#endif
#ifndef XK_MISCELLANY
#define XK_MISCELLANY
#endif
#ifndef XK_LATIN1
#define XK_LATIN1
#endif
#include <X11/keysymdef.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#define X_BORDER_WIDTH 4
#define X_HASH_TABLE_SIZE 64
#define X_MAX_CLIPBOARD_SIZE (15*1024*1024)
#define XPIXMAPP(a) ((struct x_pixmapa*)(a))
static int x_default_window_width;
static int x_default_window_height;
static long (*x_get_color_function)(int);
static void selection_request(XEvent *event);
static int x_fd; /* x socket */
static Display *x_display = NULL; /* display */
static int x_screen; /* screen */
static int x_display_height,x_display_width; /* screen dimensions */
static int x_black_pixel,x_white_pixel; /* white and black pixel */
static int x_depth,x_bitmap_bpp; /* bits per pixel and bytes per pixel */
static int x_bitmap_scanline_pad; /* bitmap scanline_padding in bytes */
static int x_colors; /* colors in the palette (undefined when there's no palette) */
static int x_have_palette;
static int x_input_encoding; /* locales encoding */
static int x_bitmap_bit_order;
static Window x_root_window, fake_window;
static int fake_window_initialized = 0;
static GC x_normal_gc = 0, x_copy_gc = 0, x_drawbitmap_gc = 0, x_scroll_gc = 0;
static Colormap x_colormap;
static Atom x_delete_window_atom, x_wm_protocols_atom, x_sel_atom, x_targets_atom, x_utf8_string_atom;
static Visual* x_default_visual;
static Pixmap x_icon = 0;
#ifdef X_INPUT_METHOD
static XIM xim = NULL;
#endif
extern struct graphics_driver x_driver;
static unsigned char *x_driver_param=NULL;
static int n_wins; /* number of windows */
#define X_TYPE_PIXMAP 1
#define X_TYPE_IMAGE 2
struct x_pixmapa
{
unsigned char type;
union
{
XImage *image;
Pixmap *pixmap;
}data;
};
static struct
{
unsigned char count;
struct graphics_device **pointer;
}
x_hash_table[X_HASH_TABLE_SIZE];
/* string in clipboard is in UTF-8 */
static unsigned char * x_my_clipboard=NULL;
struct window_info {
#ifdef X_INPUT_METHOD
XIC xic;
#endif
Window window;
};
static inline struct window_info *get_window_info(struct graphics_device *gd)
{
return gd->driver_data;
}
/*----------------------------------------------------------------------*/
/* Tyhle opicarny tu jsou pro zvyseni rychlosti. Flush se nedela pri kazde operaci, ale
* rekne se, ze je potreba udelat flush, a zaregistruje se bottom-half, ktery flush
* provede. Takze jakmile se vrati rizeni do select smycky, tak se provede flush.
*/
static void x_wait_for_event(void)
{
fd_set rfds;
int rs;
if (x_fd >= (int)FD_SETSIZE) {
fatal_exit("too big handle %d", x_fd);
}
FD_ZERO(&rfds);
FD_SET(x_fd, &rfds);
EINTRLOOP(rs, select(x_fd+1, &rfds, NULL, NULL, NULL));
}
static void x_process_events(void *data);
static unsigned char flush_in_progress=0;
static unsigned char process_events_in_progress=0;
static inline void X_SCHEDULE_PROCESS_EVENTS(void)
{
if (!process_events_in_progress)
{
register_bottom_half(x_process_events,NULL);
process_events_in_progress = 1;
}
}
static void x_do_flush(void *ignore)
{
/* kdyz budu mit zaregistrovanej bottom-half na tuhle funkci a nekdo mi
* tipne Xy, tak se nic nedeje, maximalne se zavola XFlush na blbej
* display, ale Xy se nepodelaj */
flush_in_progress=0;
XFlush(x_display);
X_SCHEDULE_PROCESS_EVENTS();
}
static inline void X_FLUSH(void)
{
#ifdef INTERIX
/*
* Interix has some bug, it locks up in _XWaitForWritable.
* As a workaround, do synchronous flushes.
*/
x_do_flush(NULL);
#else
if (!flush_in_progress)
{
register_bottom_half(x_do_flush,NULL);
flush_in_progress=1;
}
#endif
}
static int (*old_error_handler)(Display *, XErrorEvent *) = NULL;
static int failure_happened;
static int failure_handler(Display *d, XErrorEvent *e)
{
failure_happened = 1;
return 0;
}
static void x_prepare_for_failure(void)
{
if (old_error_handler)
internal("x_prepare_for_failure: double call");
failure_happened = 0;
old_error_handler = XSetErrorHandler(failure_handler);
}
static int x_test_for_failure(void)
{
XSync(x_display,False);
X_SCHEDULE_PROCESS_EVENTS();
XSetErrorHandler(old_error_handler);
old_error_handler = NULL;
return failure_happened;
}
/* suppose l<h */
static void x_clip_number(int *n,int l,int h)
{
if ((*n)<l)*n=l;
if ((*n)>h)*n=h;
}
static unsigned char * x_set_palette(void)
{
XColor color;
int a,r,g,b;
int tbl0[4]={0,21845,43690,65535};
int tbl1[8]={0,9362,18724,28086,37449,46811,56173,65535};
x_colormap=XCreateColormap(x_display,x_root_window,x_default_visual,AllocAll);
XInstallColormap(x_display,x_colormap);
switch(x_depth)
{
case 4:
for (a=0;a<16;a++)
{
color.red=(a&8)?65535:0;
color.green=tbl0[(a>>1)&3];
color.blue=(a&1)?65535:0;
color.pixel=a;
color.flags=DoRed|DoGreen|DoBlue;
XStoreColor(x_display,x_colormap,&color);
}
break;
case 8:
for (a=0;a<256;a++)
{
color.red=tbl1[(a>>5)&7];
color.green=tbl1[(a>>2)&7];
color.blue=tbl0[a&3];
color.pixel=a;
color.flags=DoRed|DoGreen|DoBlue;
XStoreColor(x_display,x_colormap,&color);
}
break;
case 15:
for (a=0;a<32768;a++){
color.red=((a>>10)&31)*(65535/31);
color.green=((a>>5)&31)*(65535/31);
color.blue=(a&31)*(65535/31);
color.pixel=a;
color.flags=DoRed|DoGreen|DoBlue;
XStoreColor(x_display,x_colormap,&color);
}
break;
case 16:
for (a=0;a<65536;a++){
color.red=((a>>11)&31)*(65535/31);
color.green=((a>>5)&63)*(65535/63);
color.blue=(a&31)*(65535/31);
color.pixel=a;
color.flags=DoRed|DoGreen|DoBlue;
XStoreColor(x_display,x_colormap,&color);
}
break;
case 24:
for (r=0;r<256;r++)
for (g=0;g<256;g++)
for (b=0;b<256;b++)
{
color.red=r<<8;
color.green=g<<8;
color.blue=b<<8;
color.pixel=(r<<16)+(g<<8)+(b);
color.flags=DoRed|DoGreen|DoBlue;
XStoreColor(x_display,x_colormap,&color);
}
break;
}
X_FLUSH();
return NULL;
}
static inline int trans_key(unsigned char * str, int table)
{
if (table==utf8_table){int a; GET_UTF_8(str,a);return a;}
if (*str<128)return *str;
return cp2u(*str,table);
}
/* translates X keys to links representation */
/* return value: 1=valid key, 0=nothing */
static int x_translate_key(struct graphics_device *gd, XKeyEvent *e,int *key,int *flag)
{
KeySym ks = 0;
static XComposeStatus comp = { NULL, 0 };
static unsigned char str[16];
#define str_size ((int)(sizeof(str) - 1))
int table=x_input_encoding<0?drv->codepage:x_input_encoding;
int len;
#ifdef X_INPUT_METHOD
if (get_window_info(gd)->xic) {
Status status;
#ifndef HAVE_XUTF8LOOKUPSTRING
{
wchar_t wc;
len = XwcLookupString(get_window_info(gd)->xic, e, &wc, 1, &ks, &status);
if (len == 1) {
strcpy(cast_char str, cast_const_char encode_utf_8(wc));
len = strlen(cast_const_char str);
} else
len = 0;
}
#else
{
len = Xutf8LookupString(get_window_info(gd)->xic, e, cast_char str, str_size, &ks, &status);
}
#endif
table = utf8_table;
/*fprintf(stderr, "len: %d, ks %ld, status %d\n", len, ks, status);*/
} else
#endif
{
len = XLookupString(e,cast_char str,str_size,&ks,&comp);
}
str[len>str_size?str_size:len]=0;
if (!len) str[0]=ks, str[1]=0;
*flag=0;
*key=0;
/* alt, control, shift ... */
if (e->state&ShiftMask)*flag|=KBD_SHIFT;
if (e->state&ControlMask)*flag|=KBD_CTRL;
if (e->state&Mod1Mask)*flag|=KBD_ALT;
/* alt-f4 */
if (((*flag)&KBD_ALT)&&(ks==XK_F4)){*key=KBD_CTRL_C;*flag=0;return 1;}
/* ctrl-c */
if (((*flag)&KBD_CTRL)&&(ks==XK_c||ks==XK_C)){*key=KBD_CTRL_C;*flag=0;return 1;}
switch (ks)
{
case NoSymbol: return 0;
case XK_Return: *key=KBD_ENTER;break;
case XK_BackSpace: *key=KBD_BS;break;
#ifdef XK_KP_Tab
case XK_KP_Tab:
#endif
case XK_Tab: *key=KBD_TAB;break;
case XK_Escape: *key=KBD_ESC;break;
#ifdef XK_KP_Left
case XK_KP_Left:
#endif
case XK_Left: *key=KBD_LEFT;break;
#ifdef XK_KP_Right
case XK_KP_Right:
#endif
case XK_Right: *key=KBD_RIGHT;break;
#ifdef XK_KP_Up
case XK_KP_Up:
#endif
case XK_Up: *key=KBD_UP;break;
#ifdef XK_KP_Down
case XK_KP_Down:
#endif
case XK_Down: *key=KBD_DOWN;break;
#ifdef XK_KP_Insert
case XK_KP_Insert:
#endif
case XK_Insert: *key=KBD_INS;break;
#ifdef XK_KP_Delete
case XK_KP_Delete:
#endif
case XK_Delete: *key=KBD_DEL;break;
#ifdef XK_KP_Home
case XK_KP_Home:
#endif
case XK_Home: *key=KBD_HOME;break;
#ifdef XK_KP_End
case XK_KP_End:
#endif
case XK_End: *key=KBD_END;break;
#ifdef XK_KP_Page_Up
case XK_KP_Page_Up:
#endif
#ifdef XK_Page_Up
case XK_Page_Up:
#endif
*key=KBD_PAGE_UP;break;
#ifdef XK_KP_Page_Down
case XK_KP_Page_Down:
#endif
#ifdef XK_Page_Down
case XK_Page_Down:
#endif
*key=KBD_PAGE_DOWN;break;
#ifdef XK_KP_F1
case XK_KP_F1:
#endif
case XK_F1: *key=KBD_F1;break;
#ifdef XK_KP_F2
case XK_KP_F2:
#endif
case XK_F2: *key=KBD_F2;break;
#ifdef XK_KP_F3
case XK_KP_F3:
#endif
case XK_F3: *key=KBD_F3;break;
#ifdef XK_KP_F4
case XK_KP_F4:
#endif
case XK_F4: *key=KBD_F4;break;
case XK_F5: *key=KBD_F5;break;
case XK_F6: *key=KBD_F6;break;
case XK_F7: *key=KBD_F7;break;
case XK_F8: *key=KBD_F8;break;
case XK_F9: *key=KBD_F9;break;
case XK_F10: *key=KBD_F10;break;
case XK_F11: *key=KBD_F11;break;
case XK_F12: *key=KBD_F12;break;
case XK_KP_Subtract: *key='-';break;
case XK_KP_Decimal: *key='.';break;
case XK_KP_Divide: *key='/';break;
case XK_KP_Space: *key=' ';break;
case XK_KP_Enter: *key=KBD_ENTER;break;
case XK_KP_Equal: *key='=';break;
case XK_KP_Multiply: *key='*';break;
case XK_KP_Add: *key='+';break;
case XK_KP_0: *key='0';break;
case XK_KP_1: *key='1';break;
case XK_KP_2: *key='2';break;
case XK_KP_3: *key='3';break;
case XK_KP_4: *key='4';break;
case XK_KP_5: *key='5';break;
case XK_KP_6: *key='6';break;
case XK_KP_7: *key='7';break;
case XK_KP_8: *key='8';break;
case XK_KP_9: *key='9';break;
default:
if (ks&0x8000)return 0;
*key=((*flag)&KBD_CTRL)?(int)ks&255:trans_key(str,table);
break;
/*
default: *key=((*flag)&KBD_CTRL)?(int)ks&255:trans_key(str,table);(*flag)&=~KBD_SHIFT;break;
*/
}
return 1;
}
static void x_hash_table_init(void)
{
int a;
for (a=0;a<X_HASH_TABLE_SIZE;a++)
{
x_hash_table[a].count=0;
x_hash_table[a].pointer=NULL;
}
}
static void x_clear_clipboard(void);
static void x_free_hash_table(void)
{
int a,b;
unregister_bottom_half(x_process_events,NULL);
unregister_bottom_half(x_do_flush,NULL);
for (a=0;a<X_HASH_TABLE_SIZE;a++)
{
for (b=0;b<x_hash_table[a].count;b++)
mem_free(x_hash_table[a].pointer[b]);
if (x_hash_table[a].pointer)
mem_free(x_hash_table[a].pointer);
}
x_clear_clipboard();
if (x_display) {
if (x_icon) XFreePixmap(x_display, x_icon), x_icon = 0;
if (fake_window_initialized) XDestroyWindow(x_display,fake_window), fake_window_initialized = 0;
if (x_normal_gc) XFreeGC(x_display,x_normal_gc), x_normal_gc = 0;
if (x_copy_gc) XFreeGC(x_display,x_copy_gc), x_copy_gc = 0;
if (x_drawbitmap_gc) XFreeGC(x_display,x_drawbitmap_gc), x_drawbitmap_gc = 0;
if (x_scroll_gc) XFreeGC(x_display,x_scroll_gc), x_scroll_gc = 0;
#ifdef X_INPUT_METHOD
if (xim) XCloseIM(xim), xim = NULL;
#endif
XCloseDisplay(x_display), x_display = NULL;
}
if (x_driver_param) mem_free(x_driver_param), x_driver_param = NULL;
}
/* returns graphics device structure which belonging to the window */
static struct graphics_device *x_find_gd(Window *win)
{
int a,b;
a=(*win)&(X_HASH_TABLE_SIZE-1);
if (!x_hash_table[a].count)return 0;
for (b=0;b<x_hash_table[a].count;b++)
{
if (get_window_info(x_hash_table[a].pointer[b])->window == *win)
return x_hash_table[a].pointer[b];
}
return NULL;
}
static void x_update_driver_param(int w, int h)
{
int l=0;
if (n_wins!=1)return;
x_default_window_width=w;
x_default_window_height=h;
if (x_driver_param)mem_free(x_driver_param);
x_driver_param=init_str();
add_num_to_str(&x_driver_param,&l,x_default_window_width);
add_to_str(&x_driver_param,&l,cast_uchar "x");
add_num_to_str(&x_driver_param,&l,x_default_window_height);
}
/* adds graphics device to hash table */
static void x_add_to_table(struct graphics_device* gd)
{
int a=get_window_info(gd)->window & (X_HASH_TABLE_SIZE-1);
int c=x_hash_table[a].count;
if (!c) {
x_hash_table[a].pointer=mem_alloc(sizeof(struct graphics_device *));
} else {
if ((unsigned)c > MAXINT / sizeof(struct graphics_device *) - 1) overalloc();
x_hash_table[a].pointer=mem_realloc(x_hash_table[a].pointer,(c+1)*sizeof(struct graphics_device *));
}
x_hash_table[a].pointer[c]=gd;
x_hash_table[a].count++;
}
/* removes graphics device from table */
static void x_remove_from_table(Window *win)
{
int a=(*win)&(X_HASH_TABLE_SIZE-1);
int b;
for (b=0;b<x_hash_table[a].count;b++)
if (get_window_info(x_hash_table[a].pointer[b])->window == *win)
{
memmove(x_hash_table[a].pointer+b,x_hash_table[a].pointer+b+1,(x_hash_table[a].count-b-1)*sizeof(struct graphics_device *));
x_hash_table[a].count--;
x_hash_table[a].pointer=mem_realloc(x_hash_table[a].pointer,x_hash_table[a].count*sizeof(struct graphics_device*));
}
}
static void x_clear_clipboard(void)
{
if(x_my_clipboard)
{
mem_free(x_my_clipboard);
x_my_clipboard=NULL;
}
}
static void x_process_events(void *data)
{
XEvent event;
XEvent last_event;
struct graphics_device *gd;
int last_was_mouse;
int replay_event = 0;
process_events_in_progress = 0;
#ifdef SC_DEBUG
MESSAGE("x_process_event\n");
#endif
memset(&last_event, 0, sizeof last_event); /* against warning */
last_was_mouse=0;
while (XPending(x_display) || replay_event)
{
if (replay_event) replay_event = 0;
else XNextEvent(x_display,&event);
if (last_was_mouse&&(event.type==ButtonPress||event.type==ButtonRelease)) /* this is end of mouse move block --- call mouse handler */
{
int a,b;
last_was_mouse=0;
#ifdef X_DEBUG
MESSAGE("(MotionNotify event)\n");
{
unsigned char txt[256];
sprintf(txt,"x=%d y=%d\n",last_event.xmotion.x,last_event.xmotion.y);
MESSAGE(txt);
}
#endif
gd=x_find_gd(&(last_event.xmotion.window));
if (!gd)break;
a=B_LEFT;
b=B_MOVE;
if ((last_event.xmotion.state)&Button1Mask)
{
a=B_LEFT;
b=B_DRAG;
#ifdef X_DEBUG
MESSAGE("left button/drag\n");
#endif
}
if ((last_event.xmotion.state)&Button2Mask)
{
a=B_MIDDLE;
b=B_DRAG;
#ifdef X_DEBUG
MESSAGE("middle button/drag\n");
#endif
}
if ((last_event.xmotion.state)&Button3Mask)
{
a=B_RIGHT;
b=B_DRAG;
#ifdef X_DEBUG
MESSAGE("right button/drag\n");
#endif
}
x_clip_number(&(last_event.xmotion.x),gd->size.x1,gd->size.x2);
x_clip_number(&(last_event.xmotion.y),gd->size.y1,gd->size.y2);
gd->mouse_handler(gd,last_event.xmotion.x,last_event.xmotion.y,a|b);
}
switch(event.type)
{
case GraphicsExpose: /* redraw uncovered area during scroll */
{
struct rect r;
#ifdef X_DEBUG
MESSAGE("(GraphicsExpose event)\n");
#endif
gd=x_find_gd(&(event.xgraphicsexpose.drawable));
if (!gd)break;
r.x1=event.xgraphicsexpose.x;
r.y1=event.xgraphicsexpose.y;
r.x2=event.xgraphicsexpose.x+event.xgraphicsexpose.width;
r.y2=event.xgraphicsexpose.y+event.xgraphicsexpose.height;
gd->redraw_handler(gd,&r);
}
break;
case Expose: /* redraw part of the window */
{
struct rect r;
#ifdef X_DEBUG
MESSAGE("(Expose event)\n");
#endif
gd=x_find_gd(&(event.xexpose.window));
if (!gd)break;
r.x1=event.xexpose.x;
r.y1=event.xexpose.y;
r.x2=event.xexpose.x+event.xexpose.width;
r.y2=event.xexpose.y+event.xexpose.height;
gd->redraw_handler(gd,&r);
}
break;
case ConfigureNotify: /* resize window */
#ifdef X_DEBUG
MESSAGE("(ConfigureNotify event)\n");
{
unsigned char txt[256];
sprintf(txt,"width=%d height=%d\n",event.xconfigure.width,event.xconfigure.height);
MESSAGE(txt);
}
#endif
gd=x_find_gd(&(event.xconfigure.window));
if (!gd)break;
/* when window only moved and size is the same, do nothing */
if (gd->size.x2==event.xconfigure.width&&gd->size.y2==event.xconfigure.height)break;
configure_notify_again:
gd->size.x2=event.xconfigure.width;
gd->size.y2=event.xconfigure.height;
x_update_driver_param(event.xconfigure.width, event.xconfigure.height);
while (XCheckWindowEvent(x_display,get_window_info(gd)->window,ExposureMask,&event)==True)
;
if (XCheckWindowEvent(x_display,get_window_info(gd)->window,StructureNotifyMask,&event)==True) {
if (event.type==ConfigureNotify) goto configure_notify_again;
replay_event=1;
}
gd->resize_handler(gd);
break;
case KeyPress: /* a key was pressed */
{
int f,k;
#ifdef X_DEBUG
MESSAGE("(KeyPress event)\n");
{
unsigned char txt[256];
sprintf(txt,"keycode=%d state=%d\n",event.xkey.keycode,event.xkey.state);
MESSAGE(txt);
}
#endif
#ifdef X_INPUT_METHOD
if (XFilterEvent(&event, None))
break;
#endif
gd=x_find_gd(&(event.xkey.window));
if (!gd)break;
if (x_translate_key(gd, (XKeyEvent*)(&event),&k,&f))
gd->keyboard_handler(gd,k,f);
}
break;
case ClientMessage:
if (
event.xclient.format!=32||
event.xclient.message_type!=x_wm_protocols_atom||
(Atom)event.xclient.data.l[0]!=x_delete_window_atom
)break;
/* This event is destroy window event from window manager */
case DestroyNotify:
#ifdef X_DEBUG
MESSAGE("(DestroyNotify event)\n");
#endif
gd=x_find_gd(&(event.xkey.window));
if (!gd)break;
gd->keyboard_handler(gd,KBD_CLOSE,0);
break;
case ButtonRelease: /* mouse button was released */
{
int a;
#ifdef X_DEBUG
MESSAGE("(ButtonRelease event)\n");
{
unsigned char txt[256];
sprintf(txt,"x=%d y=%d buttons=%d mask=%d\n",event.xbutton.x,event.xbutton.y,event.xbutton.button,event.xbutton.state);
MESSAGE(txt);
}
#endif
gd=x_find_gd(&(event.xbutton.window));
if (!gd)break;
last_was_mouse=0;
switch(event.xbutton.button)
{
case 1:
a=B_LEFT;
break;
case 3:
a=B_RIGHT;
break;
case 2:
a=B_MIDDLE;
break;
case 8:
a=B_FOURTH;
break;
case 9:
a=B_FIFTH;
break;
default:
goto r_xx;
}
x_clip_number(&(event.xmotion.x),gd->size.x1,gd->size.x2);
x_clip_number(&(event.xmotion.y),gd->size.y1,gd->size.y2);
gd->mouse_handler(gd,event.xbutton.x,event.xbutton.y,a|B_UP);
r_xx:;
}
break;
case ButtonPress: /* mouse button was pressed */
{
int a;
#ifdef X_DEBUG
MESSAGE("(ButtonPress event)\n");
{
unsigned char txt[256];
sprintf(txt,"x=%d y=%d buttons=%d mask=%d\n",event.xbutton.x,event.xbutton.y,event.xbutton.button,event.xbutton.state);
MESSAGE(txt);
}
#endif
gd=x_find_gd(&(event.xbutton.window));
if (!gd)break;
last_was_mouse=0;
switch(event.xbutton.button)
{
case 1:
a=B_LEFT;
break;
case 3:
a=B_RIGHT;
break;
case 2:
a=B_MIDDLE;
break;
case 4:
a=B_WHEELUP;
break;
case 5:
a=B_WHEELDOWN;
break;
case 6:
a=B_WHEELLEFT;
break;
case 7:
a=B_WHEELRIGHT;
break;
case 8:
a=B_FOURTH;
break;
case 9:
a=B_FIFTH;
break;
default:
goto p_xx;
}
x_clip_number(&(event.xmotion.x),gd->size.x1,gd->size.x2);
x_clip_number(&(event.xmotion.y),gd->size.y1,gd->size.y2);
gd->mouse_handler(gd,event.xbutton.x,event.xbutton.y,a|(a != B_WHEELDOWN && a != B_WHEELUP && a != B_WHEELLEFT && a != B_WHEELRIGHT ? B_DOWN : B_MOVE));
p_xx:;
}
break;
case MotionNotify: /* pointer moved */
{
#ifdef X_DEBUG
MESSAGE("(MotionNotify event)\n");
{
unsigned char txt[256];
sprintf(txt,"x=%d y=%d\n",event.xmotion.x,event.xmotion.y);
MESSAGE(txt);
}
#endif
/* just sign, that this was mouse event */
last_was_mouse=1;
last_event=event;
}
break;
/* read clipboard */
case SelectionNotify:
#ifdef X_DEBUG
MESSAGE("xselectionnotify\n");
#endif
/* handled in x_get_clipboard_text */
break;
/* This long code must be here in order to implement copying of stuff into the clipboard */
case SelectionRequest:
{
selection_request(&event);
}
break;
case MapNotify:
XFlush (x_display);
break;
default:
#ifdef X_DEBUG
{
unsigned char txt[256];
sprintf(txt,"event=%d\n",event.type);
MESSAGE(txt);
}
#endif
break;
}
}
if (last_was_mouse) /* that was end of mouse move block --- call mouse handler */
{
int a,b;
last_was_mouse=0;
#ifdef X_DEBUG
MESSAGE("(MotionNotify event)\n");
/*
{
unsigned char txt[256];
sprintf(txt,"x=%d y=%d\n",last_event.xmotion.x,last_event.xmotion.y);
MESSAGE(txt);
}
*/
#endif
gd=x_find_gd(&(last_event.xmotion.window));
if (!gd)goto ret;
a=B_LEFT;
b=B_MOVE;
if ((last_event.xmotion.state)&Button1Mask)
{
a=B_LEFT;
b=B_DRAG;
#ifdef X_DEBUG
MESSAGE("left button/drag\n");
#endif
}
if ((last_event.xmotion.state)&Button2Mask)
{
a=B_MIDDLE;
b=B_DRAG;
#ifdef X_DEBUG
MESSAGE("middle button/drag\n");
#endif
}
if ((last_event.xmotion.state)&Button3Mask)
{
a=B_RIGHT;
b=B_DRAG;
#ifdef X_DEBUG
MESSAGE("right button/drag\n");
#endif
}
x_clip_number(&(last_event.xmotion.x),gd->size.x1,gd->size.x2);
x_clip_number(&(last_event.xmotion.y),gd->size.y1,gd->size.y2);
gd->mouse_handler(gd,last_event.xmotion.x,last_event.xmotion.y,a|b);
}
ret:;
#ifdef SC_DEBUG
MESSAGE("x_process_event end\n");
#endif
}
/* returns pointer to string with driver parameter or NULL */
static unsigned char * x_get_driver_param(void)
{
return x_driver_param;
}
#ifdef X_INPUT_METHOD
static XIC x_open_xic(Window w);
#endif
/* initiate connection with X server */
static unsigned char * x_init_driver(unsigned char *param, unsigned char *display)
{
XGCValues gcv;
XSetWindowAttributes win_attr;
XVisualInfo vinfo;
int misordered=-1;
x_hash_table_init();
n_wins=0;
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
setlocale(LC_CTYPE, "");
#endif
#ifdef X_DEBUG
{
unsigned char txt[256];
sprintf(txt,"x_init_driver(%s, %s)\n",param, display);
MESSAGE(txt);
}
#endif
x_input_encoding=-1;
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_LANGINFO_H) && defined(CODESET) && !defined(WIN32) && !defined(INTERIX)
{
unsigned char *cp;
cp = cast_uchar nl_langinfo(CODESET);
x_input_encoding=get_cp_index(cp);
}
#endif
if (!display||!(*display))display=NULL;
/*
X documentation says on XOpenDisplay(display_name) :
display_name
Specifies the hardware display name, which determines the dis-
play and communications domain to be used. On a POSIX-confor-
mant system, if the display_name is NULL, it defaults to the
value of the DISPLAY environment variable.
But OS/2 has problems when display_name is NULL ...
*/
if (!display) display = cast_uchar getenv("DISPLAY");
#ifndef __linux__
/* on Linux, do not assume XWINDOW present if $DISPLAY is not set
--- rather open links on svgalib or framebuffer console */
if (!display) display = cast_uchar ":0.0"; /* needed for MacOS X */
#endif
x_display = XOpenDisplay(cast_char display);
if (!x_display)
{
unsigned char *err=init_str();
int l=0;
add_to_str(&err,&l,cast_uchar "Can't open display \"");
add_to_str(&err,&l,display?display:(unsigned char *)"(null)");
add_to_str(&err,&l,cast_uchar "\"\n");
x_free_hash_table();
return err;
}
x_bitmap_bit_order=BitmapBitOrder(x_display);
x_fd=XConnectionNumber(x_display);
x_screen=DefaultScreen(x_display);
x_display_height=DisplayHeight(x_display,x_screen);
x_display_width=DisplayWidth(x_display,x_screen);
x_root_window=RootWindow(x_display,x_screen);
x_default_window_width=x_display_width-50;
x_default_window_height=x_display_height-50;
x_driver_param=NULL;
if (param)
{
unsigned char *p, *e, *f;
int w,h;
x_driver_param=stracpy(param);
for (p=x_driver_param;(*p)&&(*p)!='x'&&(*p)!='X';p++)
;
if (!(*p))goto done;
*p=0;
w=strtoul(cast_const_char x_driver_param,(char **)(void *)&e,10);
h=strtoul(cast_const_char(p+1),(char **)(void *)&f,10);
if (!(*e)&&!(*f)&&w&&h){x_default_window_width=w;x_default_window_height=h;}
*p='x';
done:;
}
/* find best visual */
{
#define DEPTHS 5
#define CLASSES 2
int depths[DEPTHS]={24, 16, 15, 8, 4};
int classes[CLASSES]={TrueColor, PseudoColor}; /* FIXME: dodelat DirectColor */
int a,b;
for (a=0;a<DEPTHS;a++)
for (b=0;b<CLASSES;b++)
{
if (XMatchVisualInfo(x_display, x_screen,depths[a],classes[b], &vinfo))
{
x_default_visual=vinfo.visual;
x_depth=vinfo.depth;
/* determine bytes per pixel */
{
XPixmapFormatValues *pfm;
int n,i;
pfm=XListPixmapFormats(x_display,&n);
for (i=0;i<n;i++)
if (pfm[i].depth==x_depth)
{
x_bitmap_bpp=pfm[i].bits_per_pixel<8?1:((pfm[i].bits_per_pixel)>>3);
x_bitmap_scanline_pad=(pfm[i].scanline_pad)>>3;
XFree(pfm);
goto bytes_per_pixel_found;
}
if(n) XFree(pfm);
continue;
}
bytes_per_pixel_found:
/* test misordered flag */
switch(x_depth)
{
case 4:
case 8:
if (x_bitmap_bpp!=1)break;
if (vinfo.red_mask>=vinfo.green_mask&&vinfo.green_mask>=vinfo.blue_mask)
{
misordered=0;
goto visual_found;
}
break;
case 15:
case 16:
if (x_bitmap_bpp!=2)break;
if (x_bitmap_bit_order==MSBFirst&&vinfo.red_mask>vinfo.green_mask&&vinfo.green_mask>vinfo.blue_mask)
{
misordered=256;
goto visual_found;
}
if (x_bitmap_bit_order==MSBFirst)break;
if (vinfo.red_mask>vinfo.green_mask&&vinfo.green_mask>vinfo.blue_mask)
{
misordered=0;
goto visual_found;
}
break;
case 24:
if (x_bitmap_bpp!=3&&x_bitmap_bpp!=4) break;
if (vinfo.red_mask<vinfo.green_mask&&vinfo.green_mask<vinfo.blue_mask)
{
misordered=256;
goto visual_found;
}
if (x_bitmap_bit_order==MSBFirst&&vinfo.red_mask>vinfo.green_mask&&vinfo.green_mask>vinfo.blue_mask)
{
misordered=512;
goto visual_found;
}
if (vinfo.red_mask>vinfo.green_mask&&vinfo.green_mask>vinfo.blue_mask)
{
misordered=0;
goto visual_found;
}
break;
}
}
}
x_free_hash_table();
return stracpy(cast_uchar "No supported color depth found.\n");
visual_found:;
}
x_driver.depth=0;
x_driver.depth|=x_bitmap_bpp;
x_driver.depth|=x_depth<<3;
x_driver.depth|=misordered;
/* check if depth is sane */
if (x_driver.depth == 707) x_driver.depth = 195;
switch (x_driver.depth)
{
case 33:
case 65:
case 122:
case 130:
case 451:
case 195:
case 196:
case 378:
case 386:
case 452:
case 708:
/* printf("depth=%d visualid=%x\n",x_driver.depth, vinfo.visualid); */
break;
default:
{
unsigned char nevidim_te_ani_te_neslysim_ale_smrdis_jako_lejno[MAX_STR_LEN];
snprintf(cast_char nevidim_te_ani_te_neslysim_ale_smrdis_jako_lejno,MAX_STR_LEN,
"Unsupported graphics mode: x_depth=%d, bits_per_pixel=%d, bytes_per_pixel=%d\n",x_driver.depth, x_depth, x_bitmap_bpp);
x_free_hash_table();
return stracpy(nevidim_te_ani_te_neslysim_ale_smrdis_jako_lejno);
}
}
x_get_color_function=get_color_fn(x_driver.depth);
if (!x_get_color_function) internal("Unknown bit depth: %d", x_driver.depth);
#ifdef X_DEBUG
{
unsigned char txt[256];
sprintf(txt,"x_driver.depth=%d\n",x_driver.depth);
MESSAGE(txt);
}
#endif
x_colors=1<<x_depth;
x_have_palette=0;
if (vinfo.class==DirectColor||vinfo.class==PseudoColor)
{
unsigned char *t;
x_have_palette=1;
if((t=x_set_palette())){x_free_hash_table(); return t;}
}
x_black_pixel=BlackPixel(x_display,x_screen);
x_white_pixel=WhitePixel(x_display,x_screen);
gcv.function=GXcopy;
gcv.graphics_exposures=True; /* we want to receive GraphicsExpose events when uninitialized area is discovered during scroll */
gcv.fill_style=FillSolid;
gcv.background=x_black_pixel;
set_handlers(x_fd,x_process_events,0,0,0);
x_delete_window_atom = XInternAtom(x_display,"WM_DELETE_WINDOW", False);
x_wm_protocols_atom = XInternAtom(x_display,"WM_PROTOCOLS", False);
x_sel_atom = XInternAtom(x_display, "SEL_PROP", False);
x_targets_atom = XInternAtom(x_display, "TARGETS", False);
x_utf8_string_atom = XInternAtom(x_display, "UTF8_STRING", False);
if (x_have_palette) win_attr.colormap=x_colormap;
else win_attr.colormap=XCreateColormap(x_display, x_root_window, x_default_visual, AllocNone);
win_attr.border_pixel=x_black_pixel;
fake_window=XCreateWindow(
x_display,
x_root_window,
0,
0,
10,
10,
0,
x_depth,
CopyFromParent,
x_default_visual,
CWColormap|CWBorderPixel,
&win_attr
);
fake_window_initialized = 1;
x_normal_gc=XCreateGC(x_display,fake_window,GCFillStyle|GCBackground,&gcv);
if (!x_normal_gc) {x_free_hash_table(); return stracpy(cast_uchar "Cannot create graphic context.\n");}
x_copy_gc=XCreateGC(x_display,fake_window,GCFunction,&gcv);
if (!x_copy_gc) {x_free_hash_table(); return stracpy(cast_uchar "Cannot create graphic context.\n");}
x_drawbitmap_gc=XCreateGC(x_display,fake_window,GCFunction,&gcv);
if (!x_drawbitmap_gc) {x_free_hash_table(); return stracpy(cast_uchar "Cannot create graphic context.\n");}
x_scroll_gc=XCreateGC(x_display,fake_window,GCGraphicsExposures|GCBackground,&gcv);
if (!x_scroll_gc) {x_free_hash_table(); return stracpy(cast_uchar "Cannot create graphic context.\n");}
XSetLineAttributes(x_display,x_normal_gc,1,LineSolid,CapRound,JoinRound);
#ifdef X_INPUT_METHOD
{
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
/*
* Unfortunatelly, dead keys are translated according to
* current locale, even if we use Xutf8LookupString.
* So, try to set locale to utf8 for the input method.
*/
unsigned char *l, *m, *d;
l = cast_uchar setlocale(LC_CTYPE, "");
if (l) {
m = stracpy(l);
d = cast_uchar strchr(cast_const_char m, '.');
if (d) *d = 0;
add_to_strn(&m, cast_uchar ".UTF-8");
l = cast_uchar setlocale(LC_CTYPE, cast_const_char m);
mem_free(m);
}
if (!l) {
l = cast_uchar setlocale(LC_CTYPE, "en_US.UTF-8");
}
#endif
xim = XOpenIM(x_display, NULL, NULL, NULL);
if (xim) {
XIC xic = x_open_xic(fake_window);
if (xic) {
XDestroyIC(xic);
} else {
XCloseIM(xim), xim = NULL;
}
}
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
setlocale(LC_CTYPE, "");
#endif
}
#endif
if (x_input_encoding<0
#ifdef X_INPUT_METHOD
&& !xim
#endif
) x_driver.flags|=GD_NEED_CODEPAGE;
XSync(x_display,False);
X_SCHEDULE_PROCESS_EVENTS();
return NULL;
}
/* close connection with the X server */
static void x_shutdown_driver(void)
{
#ifdef X_DEBUG
MESSAGE("x_shutdown_driver\n");
#endif
x_free_hash_table();
}
#ifdef X_INPUT_METHOD
static XIC x_open_xic(Window w)
{
return XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, w, XNFocusWindow, w, NULL);
}
#endif
/* create new window */
static struct graphics_device* x_init_device(void)
{
struct graphics_device *gd;
XWMHints wm_hints;
XClassHint class_hints;
XTextProperty windowName;
unsigned char *links_name=cast_uchar "Links";
XSetWindowAttributes win_attr;
struct window_info *wi;
#ifdef X_DEBUG
MESSAGE("x_init_device\n");
#endif
gd=mem_alloc(sizeof(struct graphics_device));
wi=mem_calloc(sizeof(struct window_info));
gd->size.x1=0;
gd->size.y1=0;
gd->size.x2=x_default_window_width;
gd->size.y2=x_default_window_height;
if (x_have_palette) win_attr.colormap=x_colormap;
else win_attr.colormap=XCreateColormap(x_display, x_root_window, x_default_visual, AllocNone);
win_attr.border_pixel=x_black_pixel;
wi->window=XCreateWindow(
x_display,
x_root_window,
gd->size.x1,
gd->size.y1,
gd->size.x2,
gd->size.y2,
X_BORDER_WIDTH,
x_depth,
InputOutput,
x_default_visual,
CWColormap|CWBorderPixel,
&win_attr
);
if (!x_icon)
{
XImage *img;
unsigned char *data;
int w,h,skip;
get_links_icon(&data,&w,&h,&skip,x_bitmap_scanline_pad);
img=XCreateImage(x_display,x_default_visual,x_depth,ZPixmap,0,0,w,h,x_bitmap_scanline_pad<<3,w*((x_driver.depth)&7));
if (!img){x_icon=0;goto nic_nebude_bobankove;}
img->data=cast_char data;
x_icon=XCreatePixmap(x_display,wi->window,w,h,x_depth);
if (!x_icon){XDestroyImage(img);x_icon=0;goto nic_nebude_bobankove;}
XPutImage(x_display,x_icon,x_copy_gc,img,0,0,0,0,w,h);
XDestroyImage(img);
nic_nebude_bobankove:;
}
wm_hints.flags=InputHint;
wm_hints.input=True;
if (x_icon)
{
wm_hints.flags=InputHint|IconPixmapHint;
wm_hints.icon_pixmap=x_icon;
}
XSetWMHints(x_display, wi->window, &wm_hints);
class_hints.res_name = cast_char links_name;
class_hints.res_class = cast_char links_name;
XSetClassHint(x_display, wi->window, &class_hints);
XStringListToTextProperty((char **)(void *)&links_name, 1, &windowName);
XSetWMName(x_display, wi->window, &windowName);
XStoreName(x_display,wi->window,cast_char links_name);
XSetWMIconName(x_display, wi->window, &windowName);
XMapWindow(x_display,wi->window);
gd->clip.x1=gd->size.x1;
gd->clip.y1=gd->size.y1;
gd->clip.x2=gd->size.x2;
gd->clip.y2=gd->size.y2;
gd->driver_data=wi;
gd->user_data=0;
XSetWindowBackgroundPixmap(x_display, wi->window, None);
if (x_have_palette) XSetWindowColormap(x_display,wi->window,x_colormap);
x_add_to_table(gd);
XSetWMProtocols(x_display,wi->window,&x_delete_window_atom,1);
XSelectInput(x_display,wi->window,
ExposureMask|
KeyPressMask|
ButtonPressMask|
ButtonReleaseMask|
PointerMotionMask|
ButtonMotionMask|
StructureNotifyMask|
0
);
#ifdef X_INPUT_METHOD
if (xim) {
wi->xic = x_open_xic(wi->window);
}
#endif
XSync(x_display,False);
X_SCHEDULE_PROCESS_EVENTS();
n_wins++;
return gd;
}
/* close window */
static void x_shutdown_device(struct graphics_device *gd)
{
struct window_info *wi = get_window_info(gd);
#ifdef X_DEBUG
MESSAGE("x_shutdown_device\n");
#endif
if (!gd)return;
n_wins--;
XDestroyWindow(x_display,wi->window);
#ifdef X_INPUT_METHOD
if (wi->xic) {
XDestroyIC(wi->xic);
}
#endif
XSync(x_display,False);
X_SCHEDULE_PROCESS_EVENTS();
x_remove_from_table(&wi->window);
mem_free(wi);
mem_free(gd);
}
static int x_get_empty_bitmap(struct bitmap *bmp)
{
int pad;
#ifdef X_DEBUG
MESSAGE("x_get_empty_bitmap\n");
#endif
pad=x_bitmap_scanline_pad-((bmp->x*x_bitmap_bpp)%x_bitmap_scanline_pad);
if (pad==x_bitmap_scanline_pad)pad=0;
bmp->skip=bmp->x*x_bitmap_bpp+pad;
bmp->flags=NULL;
retry:
if (!(bmp->data=malloc(bmp->skip*bmp->y))) {
if (out_of_memory(0, NULL, 0))
goto retry;
return -1;
}
/* on error bmp->data should point to NULL */
return 0;
}
static void x_register_bitmap(struct bitmap *bmp)
{
struct x_pixmapa *p;
XImage *image;
Pixmap *pixmap;
int can_create_pixmap;
#ifdef X_DEBUG
MESSAGE("x_register_bitmap\n");
#endif
X_FLUSH();
if (!bmp->data||!bmp->x||!bmp->y) goto cant_create;
/* alloc struct x_bitmapa */
p=mem_alloc(sizeof(struct x_pixmapa));
/* alloc XImage in client's memory */
retry:
image=XCreateImage(x_display,x_default_visual,x_depth,ZPixmap,0,0,bmp->x,bmp->y,x_bitmap_scanline_pad<<3,bmp->skip);
if (!image){
if (out_of_memory(0, NULL, 0))
goto retry;
mem_free(p);
goto cant_create;
}
image->data=bmp->data;
/* try to alloc XPixmap in server's memory */
can_create_pixmap=1;
x_prepare_for_failure();
pixmap=mem_alloc(sizeof(Pixmap));
(*pixmap)=XCreatePixmap(x_display,fake_window,bmp->x,bmp->y,x_depth);
if (x_test_for_failure()) {
if (*pixmap) {
x_prepare_for_failure();
XFreePixmap(x_display,*pixmap);
x_test_for_failure();
*pixmap=0;
}
}
if (!(*pixmap)){mem_free(pixmap);can_create_pixmap=0;}
if (can_create_pixmap)
{
#ifdef X_DEBUG
MESSAGE("x_register_bitmap: creating pixmap\n");
#endif
XPutImage(x_display,*pixmap,x_copy_gc,image,0,0,0,0,bmp->x,bmp->y);
XDestroyImage(image);
p->type=X_TYPE_PIXMAP;
p->data.pixmap=pixmap;
}
else
{
#ifdef X_DEBUG
MESSAGE("x_register_bitmap: creating image\n");
#endif
p->type=X_TYPE_IMAGE;
p->data.image=image;
}
bmp->flags=p;
bmp->data=NULL;
return;
cant_create:
if (bmp->data) free(bmp->data), bmp->data = NULL;
bmp->flags=NULL;
return;
}
static void x_unregister_bitmap(struct bitmap *bmp)
{
#ifdef X_DEBUG
MESSAGE("x_unregister_bitmap\n");
#endif
if (!bmp->flags) return;
switch(XPIXMAPP(bmp->flags)->type)
{
case X_TYPE_PIXMAP:
XFreePixmap(x_display,*(XPIXMAPP(bmp->flags)->data.pixmap)); /* free XPixmap from server's memory */
mem_free(XPIXMAPP(bmp->flags)->data.pixmap); /* XPixmap */
break;
case X_TYPE_IMAGE:
XDestroyImage(XPIXMAPP(bmp->flags)->data.image); /* free XImage from client's memory */
break;
}
mem_free(bmp->flags); /* struct x_pixmap */
}
static long x_get_color(int rgb)
{
long block;
unsigned char *b;
#ifdef X_DEBUG
MESSAGE("x_get_color\n");
#endif
block=x_get_color_function(rgb);
b = (unsigned char *)█
/*fprintf(stderr, "bitmap bpp %d\n", x_bitmap_bpp);*/
switch (x_bitmap_bpp) {
case 1: return b[0];
case 2: if (x_bitmap_bit_order == LSBFirst)
return b[0] | (b[1] << 8);
else
return b[1] | (b[0] << 8);
case 3: return b[0] | (b[1] << 8) | (b[2] << 16);
default: if (x_bitmap_bit_order == LSBFirst)
return b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
else
return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
}
}
static void x_fill_area(struct graphics_device *gd, int x1, int y1, int x2, int y2, long color)
{
/*int a;*/
#ifdef X_DEBUG
{
unsigned char txt[256];
sprintf(txt,"x_fill_area (x1=%d y1=%d x2=%d y2=%d)\n",x1,y1,x2,y2);
MESSAGE(txt);
}
#endif
/* Mikulas: v takovem pripade radsi neplnit nic ... */
/*
if (x1>x2){a=x2;x2=x1;x1=a;}
if (y1>y2){a=y2;y2=y1;y1=a;}
*/
if (x1 < gd->clip.x1) x1 = gd->clip.x1;
if (x2 > gd->clip.x2) x2 = gd->clip.x2;
if (y1 < gd->clip.y1) y1 = gd->clip.y1;
if (y2 > gd->clip.y2) y2 = gd->clip.y2;
if (x1>=x2) return;
if (y1>=y2) return;
XSetForeground(x_display,x_normal_gc,color);
XFillRectangle(
x_display,
get_window_info(gd)->window,
x_normal_gc,
x1,
y1,
x2-x1,
y2-y1
);
X_FLUSH();
}
static void x_draw_hline(struct graphics_device *gd, int left, int y, int right, long color)
{
#ifdef X_DEBUG
MESSAGE("x_draw_hline\n");
#endif
if (left>=right)return;
if ((y>=gd->clip.y2)||(y<gd->clip.y1)) return;
if (right<=gd->clip.x1||left>=gd->clip.x2)return;
XSetForeground(x_display,x_normal_gc,color);
XDrawLine(
x_display,
get_window_info(gd)->window,
x_normal_gc,
left,y,right-1,y
);
X_FLUSH();
}
static void x_draw_vline(struct graphics_device *gd, int x, int top, int bottom, long color)
{
#ifdef X_DEBUG
MESSAGE("x_draw_vline\n");
#endif
if (top>=bottom)return;
if ((x>=gd->clip.x2)||(x<gd->clip.x1)) return;
if (bottom<=gd->clip.y1||top>=gd->clip.y2)return;
XSetForeground(x_display,x_normal_gc,color);
XDrawLine(
x_display,
get_window_info(gd)->window,
x_normal_gc,
x,top,x,bottom-1
);
X_FLUSH();
}
static void x_set_clip_area(struct graphics_device *gd, struct rect *r)
{
XRectangle xr;
#ifdef X_DEBUG
{
unsigned char txt[512];
snprintf(txt,512,"x_set_clip_area(x1=%d, y1=%d, x2=%d, y2=%d\n",r->x1,r->y1,r->x2,r->y2);
MESSAGE(txt);
}
#endif
gd->clip.x1=r->x1;
gd->clip.x2=r->x2;
gd->clip.y1=r->y1;
gd->clip.y2=r->y2;
xr.x=r->x1;
xr.y=r->y1;
if (r->x2<r->x1)xr.width=0;
else xr.width=(r->x2)-(r->x1);
if (r->y2<r->y1)xr.height=0;
else xr.height=(r->y2)-(r->y1);
XSetClipRectangles(x_display,x_normal_gc,0,0,&xr,1,Unsorted);
XSetClipRectangles(x_display,x_scroll_gc,0,0,&xr,1,Unsorted);
XSetClipRectangles(x_display,x_drawbitmap_gc,0,0,&xr,1,Unsorted);
X_FLUSH();
}
static void x_draw_bitmap(struct graphics_device *gd, struct bitmap *bmp, int x, int y)
{
int bmp_off_x, bmp_off_y, bmp_size_x, bmp_size_y;
#ifdef X_DEBUG
MESSAGE("x_draw_bitmap\n");
#endif
if (!(bmp->flags)||!bmp->x||!bmp->y) {
return;
}
if ((x>=gd->clip.x2)||(y>=gd->clip.y2)) return;
if ((x+(bmp->x)<=gd->clip.x1)||(y+(bmp->y)<=gd->clip.y1)) return;
bmp_off_x = 0;
bmp_off_y = 0;
bmp_size_x = bmp->x;
bmp_size_y = bmp->y;
if (x < gd->clip.x1) {
bmp_off_x = gd->clip.x1 - x;
bmp_size_x -= gd->clip.x1 - x;
x = gd->clip.x1;
}
if (x + bmp_size_x > gd->clip.x2) {
bmp_size_x = gd->clip.x2 - x;
}
if (y < gd->clip.y1) {
bmp_off_y = gd->clip.y1 - y;
bmp_size_y -= gd->clip.y1 - y;
y = gd->clip.y1;
}
if (y + bmp_size_y > gd->clip.y2) {
bmp_size_y = gd->clip.y2 - y;
}
switch(XPIXMAPP(bmp->flags)->type)
{
case X_TYPE_PIXMAP:
XCopyArea(x_display,*(XPIXMAPP(bmp->flags)->data.pixmap),get_window_info(gd)->window,x_drawbitmap_gc,bmp_off_x,bmp_off_y,bmp_size_x,bmp_size_y,x,y);
break;
case X_TYPE_IMAGE:
XPutImage(x_display,get_window_info(gd)->window,x_drawbitmap_gc,XPIXMAPP(bmp->flags)->data.image,bmp_off_x,bmp_off_y,x,y,bmp_size_x,bmp_size_y);
break;
}
X_FLUSH();
}
#if 0
static void x_draw_bitmaps(struct graphics_device *gd, struct bitmap **bmps, int n, int x, int y)
{
int a;
#ifdef X_DEBUG
MESSAGE("x_draw_bitmaps\n");
#endif
if (!bmps)return;
for (a=0;a<n;a++)
{
x_draw_bitmap(gd,bmps[a],x,y);
x+=(bmps[a])->x;
}
}
#endif
static int x_hscroll(struct graphics_device *gd, struct rect_set **set, int sc)
{
XEvent ev;
struct rect r;
*set=NULL;
if (!sc)return 0;
*set=init_rect_set();
if (!(*set))internal("Cannot allocate memory for rect set in scroll function.\n");
XCopyArea(
x_display,
get_window_info(gd)->window,
get_window_info(gd)->window,
x_scroll_gc,
gd->clip.x1,gd->clip.y1,
gd->clip.x2-gd->clip.x1,gd->clip.y2-gd->clip.y1,
gd->clip.x1+sc,gd->clip.y1
);
XSync(x_display,False);
/* ten sync tady musi byt, protoze potrebuju zarucit, aby vsechny
* graphics-expose vyvolane timto scrollem byly vraceny v rect-set */
/* take all graphics expose events for this window and put them into the rect set */
while (XCheckWindowEvent(x_display,get_window_info(gd)->window,ExposureMask,&ev)==True)
{
switch(ev.type)
{
case GraphicsExpose:
r.x1=ev.xgraphicsexpose.x;
r.y1=ev.xgraphicsexpose.y;
r.x2=ev.xgraphicsexpose.x+ev.xgraphicsexpose.width;
r.y2=ev.xgraphicsexpose.y+ev.xgraphicsexpose.height;
break;
case Expose:
r.x1=ev.xexpose.x;
r.y1=ev.xexpose.y;
r.x2=ev.xexpose.x+ev.xexpose.width;
r.y2=ev.xexpose.y+ev.xexpose.height;
break;
default:
continue;
}
if (r.x1 < gd->clip.x1 || r.x2 > gd->clip.x2 ||
r.y1 < gd->clip.y1 || r.y2 > gd->clip.y2) {
switch(ev.type)
{
case GraphicsExpose:
ev.xgraphicsexpose.x = 0;
ev.xgraphicsexpose.y = 0;
ev.xgraphicsexpose.width = gd->size.x2;
ev.xgraphicsexpose.height = gd->size.y2;
break;
case Expose:
ev.xexpose.x = 0;
ev.xexpose.y = 0;
ev.xexpose.width = gd->size.x2;
ev.xexpose.height = gd->size.y2;
break;
}
XPutBackEvent(x_display, &ev);
mem_free(*set);
*set = NULL;
break;
}
add_to_rect_set(set,&r);
}
X_SCHEDULE_PROCESS_EVENTS();
#ifdef SC_DEBUG
MESSAGE("hscroll\n");
#endif
return 1;
}
static int x_vscroll(struct graphics_device *gd, struct rect_set **set, int sc)
{
XEvent ev;
struct rect r;
*set=NULL;
if (!sc)return 0;
*set=init_rect_set();
if (!(*set))internal("Cannot allocate memory for rect set in scroll function.\n");
XCopyArea(
x_display,
get_window_info(gd)->window,
get_window_info(gd)->window,
x_scroll_gc,
gd->clip.x1,gd->clip.y1,
gd->clip.x2-gd->clip.x1,gd->clip.y2-gd->clip.y1,
gd->clip.x1,gd->clip.y1+sc
);
XSync(x_display,False);
/* ten sync tady musi byt, protoze potrebuju zarucit, aby vsechny
* graphics-expose vyvolane timto scrollem byly vraceny v rect-set */
/* take all graphics expose events for this window and put them into the rect set */
while (XCheckWindowEvent(x_display,get_window_info(gd)->window,ExposureMask,&ev)==True)
{
switch(ev.type)
{
case GraphicsExpose:
r.x1=ev.xgraphicsexpose.x;
r.y1=ev.xgraphicsexpose.y;
r.x2=ev.xgraphicsexpose.x+ev.xgraphicsexpose.width;
r.y2=ev.xgraphicsexpose.y+ev.xgraphicsexpose.height;
break;
case Expose:
r.x1=ev.xexpose.x;
r.y1=ev.xexpose.y;
r.x2=ev.xexpose.x+ev.xexpose.width;
r.y2=ev.xexpose.y+ev.xexpose.height;
break;
default:
continue;
}
if (r.x1 < gd->clip.x1 || r.x2 > gd->clip.x2 ||
r.y1 < gd->clip.y1 || r.y2 > gd->clip.y2) {
switch(ev.type)
{
case GraphicsExpose:
ev.xgraphicsexpose.x = 0;
ev.xgraphicsexpose.y = 0;
ev.xgraphicsexpose.width = gd->size.x2;
ev.xgraphicsexpose.height = gd->size.y2;
break;
case Expose:
ev.xexpose.x = 0;
ev.xexpose.y = 0;
ev.xexpose.width = gd->size.x2;
ev.xexpose.height = gd->size.y2;
break;
}
XPutBackEvent(x_display, &ev);
mem_free(*set);
*set = NULL;
break;
}
add_to_rect_set(set,&r);
}
X_SCHEDULE_PROCESS_EVENTS();
#ifdef SC_DEBUG
MESSAGE("vscroll\n");
#endif
return 1;
}
static void *x_prepare_strip(struct bitmap *bmp, int top, int lines)
{
struct x_pixmapa *p=(struct x_pixmapa *)bmp->flags;
XImage *image;
void *x_data;
if (!p) return NULL;
#ifdef DEBUG
if (lines <= 0) internal("x_prepare_strip: %d lines",lines);
#endif
#ifdef X_DEBUG
MESSAGE("x_prepare_strip\n");
#endif
bmp->data = NULL;
switch (p->type)
{
case X_TYPE_PIXMAP:
retry:
x_data=malloc(bmp->skip*lines);
if (!x_data) {
if (out_of_memory(0, NULL, 0))
goto retry;
return NULL;
}
retry2:
image=XCreateImage(x_display,x_default_visual,x_depth,ZPixmap,0,0,bmp->x,lines,x_bitmap_scanline_pad<<3,bmp->skip);
if (!image) {
if (out_of_memory(0, NULL, 0))
goto retry2;
free(x_data);
return NULL;
}
image->data = x_data;
bmp->data=image;
return image->data;
case X_TYPE_IMAGE:
return p->data.image->data+(bmp->skip*top);
}
internal("Unknown pixmap type found in x_prepare_strip. SOMETHING IS REALLY STRANGE!!!!\n");
return NULL;
}
static void x_commit_strip(struct bitmap *bmp, int top, int lines)
{
struct x_pixmapa *p=(struct x_pixmapa *)bmp->flags;
if (!p) return;
#ifdef X_DEBUG
MESSAGE("x_commit_strip\n");
#endif
switch(p->type)
{
/* send image to pixmap in xserver */
case X_TYPE_PIXMAP:
if (!bmp->data) return;
XPutImage(x_display,*(XPIXMAPP(bmp->flags)->data.pixmap),x_copy_gc,(XImage*)bmp->data,0,0,0,top,bmp->x,lines);
XDestroyImage((XImage *)bmp->data);
return;
case X_TYPE_IMAGE:
/* everything has been done by user */
return;
}
}
static void x_set_window_title(struct graphics_device *gd, unsigned char *title)
{
struct conv_table *ct;
unsigned char *t;
XTextProperty windowName;
int output_encoding;
Status ret;
#if defined(HAVE_XSUPPORTSLOCALE) && defined(HAVE_XMBTEXTLISTTOTEXTPROPERTY)
if (XSupportsLocale()) {
output_encoding = x_input_encoding >= 0 ? x_input_encoding : 0;
} else
retry_encode_ascii:
#endif
{
output_encoding = 0;
}
ct = get_translation_table(utf8_table,output_encoding);
if (!gd)internal("x_set_window_title called with NULL graphics_device pointer.\n");
t = convert_string(ct, title, strlen(cast_const_char title), NULL);
clr_white(t);
/*XStoreName(x_display,get_window_info(gd)->window,"blabla");*/
#if defined(HAVE_XSUPPORTSLOCALE) && defined(HAVE_XMBTEXTLISTTOTEXTPROPERTY)
if (XSupportsLocale()) {
ret = XmbTextListToTextProperty(x_display, (char**)(void *)(&t), 1, XStdICCTextStyle, &windowName);
#ifdef X_HAVE_UTF8_STRING
if (ret > 0) {
XFree(windowName.value);
ret = XmbTextListToTextProperty(x_display, (char**)(void *)(&t), 1, XUTF8StringStyle, &windowName);
if (ret < 0) {
ret = XmbTextListToTextProperty(x_display, (char**)(void *)(&t), 1, XStdICCTextStyle, &windowName);
}
}
#endif
if (ret < 0) {
if (output_encoding) {
mem_free(t);
goto retry_encode_ascii;
} else {
goto retry_print_ascii;
}
}
} else
retry_print_ascii:
#endif
{
ret = XStringListToTextProperty((char**)(void *)(&t), 1, &windowName);
if (!ret) {
mem_free(t);
return;
}
}
mem_free(t);
XSetWMName(x_display, get_window_info(gd)->window, &windowName);
XSetWMIconName(x_display, get_window_info(gd)->window, &windowName);
XFree(windowName.value);
X_FLUSH();
}
/* gets string in UTF8 */
static void x_set_clipboard_text(struct graphics_device *gd, unsigned char * text)
{
x_clear_clipboard();
if (text) {
x_my_clipboard = stracpy(text);
XSetSelectionOwner (x_display, XA_PRIMARY, get_window_info(gd)->window, CurrentTime);
XFlush (x_display);
X_SCHEDULE_PROCESS_EVENTS();
}
}
static void selection_request(XEvent *event)
{
XSelectionRequestEvent *req;
XSelectionEvent sel;
size_t l;
req = &(event->xselectionrequest);
sel.type = SelectionNotify;
sel.requestor = req->requestor;
sel.selection = XA_PRIMARY;
sel.target = req->target;
sel.property = req->property;
sel.time = req->time;
sel.display = req->display;
#ifdef X_DEBUG
{
unsigned char txt[256];
sprintf (txt,"xselectionrequest from %i\n",(int)event.xselection.requestor);
MESSAGE(txt);
sprintf (txt,"property:%i target:%i selection:%i\n", req->property,req->target, req->selection);
MESSAGE(txt);
}
#endif
if (req->target == XA_STRING) {
unsigned char *str, *p;
struct conv_table *ct = NULL;
int iso1 = get_cp_index(cast_uchar "iso-8859-1");
if (iso1 >= 0) ct = get_translation_table(utf8_table, iso1);
if (!x_my_clipboard) str = stracpy(cast_uchar "");
else if (!ct) str = stracpy(x_my_clipboard);
else str = convert_string(ct, x_my_clipboard, strlen(cast_const_char x_my_clipboard), NULL);
for (p = cast_uchar strchr(cast_const_char str, 1); p; p = cast_uchar strchr(cast_const_char(str + 1), 1)) *p = 0xa0;
l = strlen(cast_const_char str);
if (l > X_MAX_CLIPBOARD_SIZE) l = X_MAX_CLIPBOARD_SIZE;
XChangeProperty (x_display,
sel.requestor,
sel.property,
XA_STRING,
8,
PropModeReplace,
str,
l
);
mem_free(str);
} else if (req->target == x_utf8_string_atom) {
l = x_my_clipboard ? strlen(cast_const_char x_my_clipboard) : 0;
if (l > X_MAX_CLIPBOARD_SIZE) l = X_MAX_CLIPBOARD_SIZE;
XChangeProperty (x_display,
sel.requestor,
sel.property,
x_utf8_string_atom,
8,
PropModeReplace,
x_my_clipboard,
l
);
} else if (req->target == x_targets_atom) {
unsigned tgt_atoms[3];
tgt_atoms[0] = x_targets_atom;
tgt_atoms[1] = XA_STRING;
tgt_atoms[2] = x_utf8_string_atom;
XChangeProperty (x_display,
sel.requestor,
sel.property,
XA_ATOM,
32,
PropModeReplace,
(unsigned char*)&tgt_atoms,
3
);
} else {
#ifdef X_DEBUG
{
unsigned char txt[256];
sprintf (txt,"Non-String wanted: %i\n",(int)req->target);
MESSAGE(txt);
}
#endif
sel.property = None;
}
XSendEvent(x_display, sel.requestor, 0, 0, (XEvent*)&sel);
XFlush(x_display);
X_SCHEDULE_PROCESS_EVENTS();
}
static unsigned char *x_get_clipboard_text(void)
{
XEvent event;
Atom type_atom = x_utf8_string_atom;
retry:
XConvertSelection(x_display, XA_PRIMARY, type_atom, x_sel_atom, fake_window, CurrentTime);
while (1) {
XSync(x_display, False);
if (XCheckTypedEvent(x_display,SelectionRequest, &event)) {
selection_request(&event);
continue;
}
if (XCheckTypedEvent(x_display,SelectionNotify, &event)) break;
x_wait_for_event();
}
if (event.xselection.property) {
unsigned char *buffer;
unsigned long pty_size, pty_items;
int pty_format, ret;
Atom pty_type;
if (event.xselection.target != type_atom) goto no_new_sel;
if (event.xselection.property != x_sel_atom) goto no_new_sel;
/* Get size and type of property */
ret = XGetWindowProperty(
x_display,
fake_window,
event.xselection.property,
0,
0,
False,
AnyPropertyType,
&pty_type,
&pty_format,
&pty_items,
&pty_size,
&buffer);
if (ret != Success) goto no_new_sel;
XFree(buffer);
ret = XGetWindowProperty(
x_display,
fake_window,
event.xselection.property,
0,
(long)pty_size,
True,
AnyPropertyType,
&pty_type,
&pty_format,
&pty_items,
&pty_size,
&buffer
);
if (ret != Success) goto no_new_sel;
pty_size = (pty_format>>3) * pty_items;
x_clear_clipboard();
if (type_atom == x_utf8_string_atom) {
x_my_clipboard = stracpy(buffer);
} else {
struct conv_table *ct = NULL;
int iso1 = get_cp_index(cast_uchar "iso-8859-1");
if (iso1 >= 0) ct = get_translation_table(iso1, utf8_table);
if (!ct) {
x_my_clipboard = stracpy(buffer);
} else {
x_my_clipboard = convert_string(ct, buffer, strlen(cast_const_char buffer), NULL);
}
}
XFree(buffer);
} else {
if (type_atom == x_utf8_string_atom) {
type_atom = XA_STRING;
goto retry;
}
}
no_new_sel:
X_SCHEDULE_PROCESS_EVENTS();
if (!x_my_clipboard) return NULL;
return stracpy(x_my_clipboard);
}
static int x_exec(unsigned char *command, int fg)
{
unsigned char *run;
int retval;
if (!fg) {
errno = 0;
EINTRLOOP(retval, system(cast_const_char command));
return retval;
}
run=subst_file(*x_driver.shell?x_driver.shell:(unsigned char *)"x-terminal-emulator -e %",command, 0);
errno = 0;
EINTRLOOP(retval, system(cast_const_char run));
mem_free(run);
return retval;
}
struct graphics_driver x_driver={
cast_uchar "x",
x_init_driver,
x_init_device,
x_shutdown_device,
x_shutdown_driver,
x_get_driver_param,
x_get_empty_bitmap,
/*x_get_filled_bitmap,*/
x_register_bitmap,
x_prepare_strip,
x_commit_strip,
x_unregister_bitmap,
x_draw_bitmap,
/*x_draw_bitmaps,*/
x_get_color,
x_fill_area,
x_draw_hline,
x_draw_vline,
x_hscroll,
x_vscroll,
x_set_clip_area,
dummy_block,
dummy_unblock,
x_set_window_title,
x_exec,
x_set_clipboard_text,
x_get_clipboard_text,
0, /* depth (filled in x_init_driver function) */
0, 0, /* size (in X is empty) */
0, /* flags */
0, /* codepage */
NULL, /* shell */
};
#endif /* GRDRV_X */
|