xaizek / euclid-wm (License: BSD 3-Clause) (since 2018-12-07)
A minimalist, tiling window manager for X11 that seeks to allow easy management of numerous windows entirely from the keyboard.
<root> / euclid-wm.c (fe0e8011c2b5f7fba55f0a7ccb32481c84610962) (93KiB) (mode 100644) [raw]
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 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403
/*

Copyright (c) 2010-14, William M. Diem <wmdiem at gmail dot com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
* The names of the contributor(s) may not be used to endorse or promote products
  derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

scrotwm < http://www.scrotwm.org/ > and dwm < http://dwm.suckless.org/ > 
were used as a model for some parts.
Thus the one or more of the following notices may apply to some sections:

* scrotwm: 
* Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
* Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
* Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
* Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* dwm:
* 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
* 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
* 2006-2007 Jukka Salmi <jukka at salmi dot ch>
* 2007 Premysl Hruby <dfenze at gmail dot com>
* 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
* 2007 Christof Musik <christof at sendfax dot de>
* 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
* 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
* 2008 Martin Hurton <martin dot hurton at gmail dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.

*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <errno.h>
#ifndef NOXINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#include <signal.h>


//this is a hack
FILE *popen(char *, char *);
int pclose (FILE *);
char *tempnam(char *,char*);

//determines size of a static array (won't work with pointers)
#define ARRAY_LEN(x) (sizeof(x)/sizeof((x)[0]))

//number of builtin commands
#define BCMDS 56
//maximum number of supported custom commands
#define CCMDS 99
//total maximum number of commands
#define BINDINGS (BCMDS + CCMDS)

/*BASIC VARIABLE TYPES*/

/*
 * Overall structure
 * v =		 views
 * 		  |		\
 * t = 		tracks		stack
 * 		  |
 * c = 		containers
 * 		  |
 * w = 		 wins
 * 
 */


struct screen {
	struct screen *next;
	struct view *v;
	Window stackid;
	int x;
	int y;
	unsigned int w;
	unsigned int h;
};

struct view {
	struct view *next; //the views of a screen are in a doubly linked list
	struct view *prev;
	struct track *ft; //first track
	struct stack_item *stack;
	struct cont *mfocus; //container for focused window  
	struct stack_item *sfocus; //stackfocus
	int idx; //index
	bool showstack; //is it visible
	bool orientv; //tracks run verically?
	bool fs; //fullscreen?
};

struct track {
	struct view *view; //what view does this track belong to
	struct track *next; //tracks on a view are in a doubly linked list
	struct track *prev;
	struct cont *c; //first container in the track
	int size;
};

struct cont {
	struct track *track; //what track is the container in?
	struct cont *next; //doubly linked list of conts in a track
	struct cont *prev; 
	struct win *win;  //pointer to window that the cont holds
	int size; //size represents h or w depending on the orientation of the layout

};

struct win {
	Window id; //X11 window id
	struct win *next; //linked list of ALL windows everywhere that euclid cares about
	struct cont *cont; //holds a pointer to the cont if the win is in a layout
	bool del_win; 
	bool take_focus; 
	bool fullscreen; 
	bool req_fullscreen; //did this window originate the fullscreen state of the view?
	bool last_only_chld; //last time this window was mapped, was it in its own track?
	int last_tpos; // we will store the last track and cont coords for the CENTER of the window here to allow semi-persistence
	int last_cpos;
	struct cont *prev_focus; // container of previously focused window (needs validity check before use)
};

struct stack_item { //items in the stack are doubly linked per view
	struct win *win; 
	struct stack_item *next;
	struct stack_item *prev;
};

struct binding { // keep track of our keybindings
	unsigned int *mask;
	unsigned int keycode;
};


/*
 *GLOBAL VARIABLES
 */

struct view *fv = NULL; 			//first view
struct screen *cs = NULL; 			//currently active screen
struct screen *firstscreen = NULL; 		//start of a list of screens
struct win *first_win = NULL; 			//start of our linked list of all windows
unsigned int mod = Mod1Mask; 			//modifyer key
unsigned int mods = Mod1Mask | ShiftMask; 	//modifyer key + shift These are for sending keybindings to X
bool sloppy_focus = true; 			//focus follows mouse
struct binding bindings[BINDINGS];		//all the info on our keybindings
Display *dpy;					// need this to talk to X
Window root;					// root window struct
unsigned long focus_pix; 			//these are X colors
unsigned long unfocus_pix;
unsigned long stack_background_pix;
unsigned long stack_focus_pix;
unsigned long stack_unfocus_pix;
GC focus_gc = NULL;				//who even knows? X Needs graphical contexts
GC unfocus_gc = NULL;
bool gxerror = false;				//so Xlib can tell us something went wrong, again, Xlib needs this
Atom wm_del_win;				//atoms for getting info on windows
Atom wm_take_focus;
Atom wm_prot;
Atom wm_change_state;
Atom wm_fullscreen;
Atom wm_dock;
Atom current_desktop;
Atom utf8;
char *dcmd = NULL;				//string that gets passed to /bin/sh when we invoke the menu
char *tcmd = NULL;				//string that gets passed to /bin/sh when we invoke the terminal
char *ccmds[CCMDS];				//array of strings that can be set by the user to pass to /bin/sh
unsigned short res_top = 0;			//reserved space top, bottom, left, right
unsigned short res_bot = 0;
unsigned short res_left = 0;
unsigned short res_right = 0;
unsigned short resize_inc = 15;			//resize increment, sorta
unsigned short empty_stack_height = 8;		//what it says, we show an empty stack (if visible) so the user knows that (1) the stack is empty and (2) the stack is toggled on
unsigned short offscreen = 0;			//we'll set this later so we know where to move windows we don't want to be onscreen (i.e., a hidden stack)
struct timeval last_redraw;			//we use this to keep track of whether events are triggered by euclid moving things around (e.g., if we move a window under the cursor we get an enter notify even, even though the pointer never moved) or whether we need to pay attention to them
bool default_orientation = true; 		//which way do we initialize views with their tracks running?
bool autobalance = false;			//is smart layout balancing enabled?
bool win_menu = false; 				//if false use dmenu for to search windows, if true use euclid-menu, eventually we may phase dmenu out altogether
int last_view_idx = 1; //index of the last seen view

//records the keycode in appropriate array
void bind_key(char s[12], unsigned int *m, struct binding *b) {
	unsigned int code;
	code = XKeysymToKeycode(dpy,XStringToKeysym(s));
	b->keycode = code;
	b->mask = m;
}


//handles binding the default keys
void load_defaults() {
	//note that the array index is significant
	//it binds the binding to the relevant code
	//these would be more readable if we #defined them

	//resize up down left right		4	0-3
	bind_key("y",&mod,&bindings[0]);
	bind_key("u",&mod,&bindings[1]);
	bind_key("i",&mod,&bindings[2]);
	bind_key("o",&mod,&bindings[3]);
	
	//move win to view next prev 1-0	12	4-15
	bind_key("1",&mods,&bindings[4]);
	bind_key("2",&mods,&bindings[5]);
	bind_key("3",&mods,&bindings[6]);
	bind_key("4",&mods,&bindings[7]);
	bind_key("5",&mods,&bindings[8]);
	bind_key("6",&mods,&bindings[9]);
	bind_key("7",&mods,&bindings[10]);
	bind_key("8",&mods,&bindings[11]);
	bind_key("9",&mods,&bindings[12]);
	bind_key("0",&mods,&bindings[13]);
	bind_key("n",&mods,&bindings[14]);
	bind_key("m",&mods,&bindings[15]);

	//change view next prev 1-0		12	16-27
	bind_key("1",&mod,&bindings[16]);
	bind_key("2",&mod,&bindings[17]);
	bind_key("3",&mod,&bindings[18]);
	bind_key("4",&mod,&bindings[19]);
	bind_key("5",&mod,&bindings[20]);
	bind_key("6",&mod,&bindings[21]);
	bind_key("7",&mod,&bindings[22]);
	bind_key("8",&mod,&bindings[23]);
	bind_key("9",&mod,&bindings[24]);
	bind_key("0",&mod,&bindings[25]);
	bind_key("n",&mod,&bindings[26]);
	bind_key("m",&mod,&bindings[27]);

	//shift window u d r l  		4 	28-31
	bind_key("h",&mods,&bindings[28]);
	bind_key("j",&mods,&bindings[29]);
	bind_key("k",&mods,&bindings[30]);
	bind_key("l",&mods,&bindings[31]);

	//toggle stack				1	32
	bind_key("space",&mod,&bindings[32]);

	//move to stack				1	33
	bind_key("period",&mod,&bindings[33]);

	//move to main				1	34
	bind_key("comma",&mod,&bindings[34]);

	//swap stack and main			1	35
	bind_key("slash",&mod,&bindings[35]);

	//swap stack up down			2	36-37
	bind_key("semicolon",&mods,&bindings[36]);
	bind_key("apostrophe",&mods,&bindings[37]);

	//shift main focus up down left right	4	38-41
	bind_key("h",&mod,&bindings[38]);
	bind_key("j",&mod,&bindings[39]);
	bind_key("k",&mod,&bindings[40]);
	bind_key("l",&mod,&bindings[41]);

	//shift stack focus up down		2	42-43
	bind_key("semicolon",&mod,&bindings[42]);
	bind_key("apostrophe",&mod,&bindings[43]);

	//close win				2	44-45
	bind_key("Escape",&mod,&bindings[44]);
	bind_key("Escape",&mods,&bindings[45]);

	//run menu term				2	46-47
	bind_key("Return",&mod,&bindings[46]);
	bind_key("Return",&mods,&bindings[47]);

	//fullscreen				1	48
	bind_key("space",&mods,&bindings[48]);

	//quit					1	49
	bind_key("Delete",&mods,&bindings[49]);
	
	//toggle orientation
	bind_key("Tab",&mod,&bindings[50]);
	
	bind_key("r",&mod,&bindings[51]);

	//prev/next view
	bind_key("Prior", &mod,&bindings[52]);
	bind_key("Next", &mod,  &bindings[53]);

	//bind search
	bind_key("slash",&mods, &bindings[54]);

	//go to previous view
	bind_key("backslash",&mod, &bindings[55]);

	// user defined
}

void spawn(char *cmd) {
	if (cmd == NULL || cmd[0] == '\0') {
		return;
	};
	if (fork() == 0) {
		if (dpy != NULL) {
			close(ConnectionNumber(dpy));
		};

		// restore default handling of SIGCHLD signal to allow shell function
		// properly
		signal(SIGCHLD, SIG_DFL);

		setsid();
		char cmd2[264];
		strcpy (&cmd2[0],"exec ");
		strcpy (&cmd2[5],cmd);
		int end = strlen (cmd2);
		cmd2[end++] = '\0';
		execl("/bin/sh","/bin/sh","-c",cmd2,NULL);
		fprintf(stderr,"error number %d  spawning %s\n",errno,cmd);
		exit(1);
	} else {
		return;
	};
}

void split(char *in, char *out1, char *out2, char delim) {
	int i = 0;
	while (*in != '\0' && *in != delim) {
		out1[i] = *in;
		in++;
		i++;
	};
	out1[i] = '\0';
	if (*in == delim) {
		in++;
		strcpy (out2,in);
	} else {
		out2[0] = '\0';
	};
}

char *str_dup(char *in) {
	char * out = (char *) malloc(strlen(in) + 1);
	strcpy(out,in);
	return out;
}

void load_conf( bool first_call) {
	FILE *conf;
	char confdir[512];
	char conffile[512];
	char rcfile[512];
	memset(confdir, '\0', sizeof(confdir));
	memset(conffile, '\0',sizeof(conffile));
	memset(rcfile, '\0',sizeof(rcfile));
	confdir[0] = '\0';
	char *xdgconf = getenv("XDG_CONFIG_HOME");
	if (xdgconf == NULL) {
		char *home = getenv("HOME");
		if (home != NULL) { 
			strcpy(confdir, home);
			strcat(confdir,"/.config");
		};
	} else { 
		strcat(confdir,xdgconf);
	};
	//what happens here if both xdgconf and home were NULL?

	strcat(confdir,"/euclid-wm");

	//run rc file
	//but only when euclid loads
	if (first_call) {

		strcpy(rcfile,confdir);
		strcat(rcfile,"/euclidrc");
		spawn(rcfile);
	};

	//open and parse config file
	strcpy(conffile,confdir);
	strcat(conffile,"/euclid-wm.conf");
        conf = fopen(conffile,"r");
	if (conf == NULL) {
		fprintf(stderr,"euclid-wm ERROR: could not open conf file, falling back to internal defaults\n");
		load_defaults();
		return;
	};
	printf("euclid-wm: conf file opened successfully: %s\n",conffile);
	char line[256];
	load_defaults();
	while (fgets(line, 256, conf) != NULL) {
		//parse line
		if (line[0] != '#' && line[0] != '\n') {
			char key[64];
			char val[256];
			char *v = NULL;
			memset(key,'\0',sizeof(key));
			memset(val,'\0',sizeof(val));
			split(line,key,val,'=');
			if (val[0] == ' ') {
				v = &val[1];
			} else {
				v = &val[0];
			};
			if (val[strlen(val)-1] == '\n') {
				val[strlen(val) - 1] = '\0';
			};
			if (key[strlen(key) - 1] == ' ') {
				key[strlen(key) - 1] = '\0';
			};
		
			if (strcmp(key,"dmenu") == 0) {
				dcmd = str_dup(v);
			} else if (strcmp(key,"term") == 0) {
				tcmd = str_dup(v);
			} else if (strcmp(key,"autobalance") == 0) {
				if (strcmp(v,"true") == 0) {
					autobalance = true;
				} else {
					autobalance = false;
				};
			} else if (strcmp(key,"sloppy_focus") == 0) {
				sloppy_focus = (strcmp(v,"true") == 0);
			} else if (strcmp(key,"resize_increment") == 0) { 
				resize_inc = atoi(v);
			} else if (strcmp(key,"empty_stack_height") == 0) {
				empty_stack_height = atoi(v);
			} else if (strcmp(key,"reserved_top") == 0) {
				res_top = atoi(v);
			} else if (strcmp(key,"reserved_bottom") == 0) {
				res_bot = atoi(v);
			} else if (strcmp(key,"reserved_left") == 0) {
				res_left = atoi(v);
			} else if (strcmp(key,"reserved_right") == 0) {
				res_right = atoi(v);
			} else if (strcmp(key,"window_menu") == 0) {
				win_menu = atoi(v);
			} else if (strcmp(key,"modkey") == 0) {
				if (strcmp(v,"1") == 0) {
					mod = Mod1Mask;
					mods = Mod1Mask | ShiftMask;
				} else if (strcmp(v,"2") == 0) {
					mod = Mod2Mask;
					mods = Mod2Mask | ShiftMask;
				} else if (strcmp(v,"3") == 0) {
					mod = Mod3Mask;
					mods = Mod3Mask | ShiftMask;
				} else if (strcmp(v,"4") == 0) {
					mod = Mod4Mask;
					mods = Mod4Mask | ShiftMask;
				} else if (strcmp(v,"5") == 0) {
					mod = Mod5Mask;
					mods = Mod5Mask | ShiftMask;
				};

			} else if (key[0] == 'c' && key[2] == 'l' && key[4] == 'r') { 
			/* here we set colors:
			 *color_main_focus
			 *color_stack_focus
			 *color_stack_unfocus
			 *color_main_unfocus
			 *color_stack_background
			 */
				XColor color;
				if (XParseColor(dpy,DefaultColormap(dpy,0),v,&color) != 0) {
					XAllocColor(dpy,DefaultColormap(dpy,0),&color);
					//main_focus
					if (key[6] == 'm' && key[11] == 'f') {
						focus_pix = color.pixel;
					//main_unfocus
					} else if (key[6] == 'm' && key[11] == 'u') {
						unfocus_pix = color.pixel;
					//stack_focus
					} else if (key[6] == 's' && key[12] == 'f') {
						stack_focus_pix = color.pixel;
					//stack_unfocus 
					} else if (key[6] == 's' && key[12] == 'u') {
						stack_unfocus_pix = color.pixel;
					//stack_background
					} else if (key[6] == 's' && key[12] == 'b') {
						stack_background_pix = color.pixel;
					} else {
						fprintf(stderr,"euclid-wm ERROR: unknown color key in config: %s\n",key);
					};
				} else {
					fprintf(stderr,"euclid-wm ERROR: unparsable color: %s\n",v);
				};
			//custom commands
			//custom_command_01 = cmd arg1 arg2
			} else if (key[0] == 'c' && key[5] == 'm' && key[8] == 'o' && key[13] == 'd') {
				const int ccmd_index = atoi(&key[15]) - 1;
				if (ccmd_index >= 0 && ccmd_index < ARRAY_LEN(ccmds)) {
					ccmds[ccmd_index] = str_dup(v);
				} else {
					fprintf(stderr,"euclid-wm ERROR: wrong ccmd_index: %d; max is %d\n",ccmd_index,CCMDS);
				};
				
			/*
			 *Actual bindings, format
			 *bind_$ACT = mod keyname
			 *mod can be M or MS
			 *keyname is the exact name passed to X
			 */
			} else if (key[0] == 'b' && key[1] == 'i' && key[3] == 'd') {
			bool known = true;
			//select the appropriate index:
				int bindx = 0;
				if (strcmp(key,"bind_resize_left") == 0) {
					bindx = 0;
				} else if (strcmp(key,"bind_resize_down") == 0) {
					bindx = 1;
				} else if (strcmp(key,"bind_resize_up") == 0) {
					bindx = 2;
				} else if (strcmp(key,"bind_resize_right") == 0) {
					bindx = 3;
				} else if (strcmp(key,"bind_move_to_previous_view") == 0) {
					bindx = 14;
				} else if (strcmp(key,"bind_move_to_next_view") == 0) {
					bindx = 15;
				} else if (strcmp(key,"bind_goto_previous_view") == 0) {
					bindx = 26;
				} else if (strcmp(key,"bind_goto_next_view") == 0) {
					bindx = 27;
				} else if (strcmp(key,"bind_shift_win_left") == 0) {
					bindx = 28;
				} else if (strcmp(key,"bind_shift_win_down") == 0) {
					bindx = 29;
				} else if (strcmp(key,"bind_shift_win_up") == 0) {
					bindx = 30;
				} else if (strcmp(key,"bind_shift_win_right") == 0) {
					bindx = 31;
				} else if (strcmp(key,"bind_toggle_stack") == 0) {
					bindx = 32;
				} else if (strcmp(key,"bind_move_to_stack") == 0) {
					bindx = 33;
				} else if (strcmp(key,"bind_move_to_main") == 0) {
					bindx = 34;
				} else if (strcmp(key,"bind_swap_stack_and_main") == 0) {
					bindx = 35;
				} else if (strcmp(key,"bind_swap_stack_up") == 0) {
					bindx = 36;
				} else if (strcmp(key,"bind_swap_stack_down") == 0) {
					bindx = 37;
				} else if (strcmp(key,"bind_focus_left") == 0) {
					bindx = 38;
				} else if (strcmp(key,"bind_focus_down") == 0) {
					bindx = 39;
				} else if (strcmp(key,"bind_focus_up") == 0) {
					bindx = 40;
				} else if (strcmp(key,"bind_focus_right") == 0) {
					bindx = 41;
				} else if (strcmp(key,"bind_stack_focus_up") == 0) {
					bindx = 42;
				} else if (strcmp(key,"bind_stack_focus_down") == 0) {
					bindx = 43;
				} else if (strcmp(key,"bind_close_win") == 0) {
					bindx = 44;
				} else if (strcmp(key,"bind_kill_win") == 0) {
					bindx = 45;
				} else if (strcmp(key,"bind_spawn_menu") == 0) {
					bindx = 46;
				} else if (strcmp(key,"bind_spawn_term") == 0) {
					bindx = 47;
				} else if (strcmp(key,"bind_toggle_fullscreen") == 0) {
					bindx = 48;
				} else if (strcmp(key,"bind_quit") == 0) {
					bindx = 49;
				} else if (strcmp(key,"bind_toggle_orientation") == 0) {
					bindx = 50;
				} else if (strcmp(key,"bind_reload_config") == 0) {
					bindx = 51;
				} else if (strcmp(key,"bind_goto_previous_screen") == 0) {
					bindx = 52;
				} else if (strcmp(key,"bind_goto_next_screen") == 0) {
					bindx = 53;
				}else if (strcmp(key,"bind_search") == 0) {
					bindx = 54;
				} else if (strcmp(key,"bind_move_to_last_view") == 0) {
					bindx = 55;
				} else if (strncmp(key,"bind_custom_", 12) == 0) {
					const int ccmd_index = atoi(&key[12]) - 1;
					if (ccmd_index >= 0 && ccmd_index < ARRAY_LEN(ccmds)) {
						bindx = BCMDS + ccmd_index;
					} else {
						fprintf(stderr,"euclid-wm ERROR: uknown binding in config: \"%s\"\n",key);
						known = false;
					};
				} else {
					fprintf(stderr,"euclid-wm ERROR: uknown binding in config: \"%s\"\n",key),
					known = false;
				};

				if (known == true) {
				
					//get the name of the key, and the modifier
					char m[3];
					char xkey[24];
					split(v,m,xkey,' ');
					if (m[0] == 'M') {
						if (m[1] == 'S') {
							bind_key(xkey,&mods,&bindings[bindx]);
						} else {
							bind_key(xkey,&mod,&bindings[bindx]);
						};
					} else if (m[0] == 'N') {
						static unsigned int modn = 0U;
						bind_key(xkey,&modn,&bindings[bindx]);
					} else {
						m[2] = '\0';
						fprintf(stderr,"euclid-wm ERROR: unknown keybinding modifier in config: \"%s\"\n",m);
					};
				};
			};	 
		};
	};
	fclose(conf);
}

void commit_bindings() {
	int i;
	for (i = 0; i < BINDINGS; i++) {
		//need to check whether the binding has been set before sending garbage to X
		if (bindings[i].keycode != 0) {
			//BIND *ALSO* WITH The LOCKMASK set so we will get keypresses when caplock or numlock is on
			//mod2mask == numlock
			XGrabKey(dpy,bindings[i].keycode,*(bindings[i].mask),root,True,GrabModeAsync,GrabModeAsync);
			XGrabKey(dpy,bindings[i].keycode,*(bindings[i].mask) ^ LockMask,root,True,GrabModeAsync,GrabModeAsync);
			XGrabKey(dpy,bindings[i].keycode,*(bindings[i].mask) ^ LockMask ^ Mod2Mask,root,True,GrabModeAsync,GrabModeAsync);
			XGrabKey(dpy,bindings[i].keycode,*(bindings[i].mask) ^ Mod2Mask,root,True,GrabModeAsync,GrabModeAsync);
			if (gxerror == true) {
				fprintf(stderr,"euclid-wm ERROR: error grabbing key %d\n",bindings[i].keycode);
				gxerror = false;
			};
		};
	};
}

/*The wmname bit fixes bugs in dumb programs that assume a reparenting wm
 taken from scrotwm, which took it from wmname
 now it also does general atom stuff
 */
void set_atoms() {
	
	wm_del_win = XInternAtom(dpy,"WM_DELETE_WINDOW",False);
	wm_take_focus = XInternAtom(dpy,"WM_TAKE_FOCUS",False);
	wm_prot = XInternAtom(dpy, "WM_PROTOCOLS", False);
	wm_change_state = XInternAtom(dpy,"_NET_WM_STATE",False);
	wm_fullscreen = XInternAtom(dpy,"_NET_WM_STATE_FULLSCREEN",False);
	wm_dock = XInternAtom(dpy,"_NET_WM_WINDOW_TYPE_DOCK",False);
	current_desktop = XInternAtom(dpy,"_NET_CURRENT_DESKTOP",False);
	utf8 = XInternAtom(dpy,"UTF8_STRING",False);
	Atom wm_win_type = XInternAtom(dpy,"_NET_WM_WINDOW_TYPE",False);
	Atom wm_supported = XInternAtom(dpy,"_NET_SUPPORTED",False);
	Atom wm_check = XInternAtom(dpy,"_NET_SUPPORTING_WM_CHECK",False);
	Atom wm_name = XInternAtom(dpy,"_NET_WM_NAME",False);
	Atom supported[] = {wm_supported, wm_name, wm_change_state, wm_fullscreen, wm_win_type, current_desktop};
	XChangeProperty(dpy,root,wm_check,XA_WINDOW,32,PropModeReplace,(unsigned char *)&root,1);
	XChangeProperty(dpy,root,wm_name,utf8,8,PropModeReplace,(unsigned char *) "LG3D",strlen("LG3D"));
	XChangeProperty(dpy,root,wm_supported,XA_ATOM,32,PropModeReplace,(unsigned char *) supported,ARRAY_LEN(supported));
	XSync(dpy,False);
}

struct view * make_view() {
	struct view *ptr = (struct view *) malloc(sizeof(struct view));
	ptr->next = NULL;
	ptr->prev = NULL;
	ptr->mfocus = NULL;
	ptr->sfocus = NULL;
	ptr->idx = 0;
	ptr->ft = (struct track *) malloc(sizeof(struct track)); 
	ptr->ft->view = ptr;
	ptr->ft->next = NULL;
	ptr->ft->prev = NULL;
	ptr->ft->c = NULL;
	ptr->ft->size = cs->w;
	ptr->orientv = default_orientation;
	ptr->stack = NULL;
	ptr->showstack = true;
	ptr->fs = false;
	return ptr;
 }

struct view * find_view(int i);

void addscreen(short h, short w, short x, short y, short n) {

	struct screen *new = (struct screen * ) malloc (sizeof(struct screen));
	//set the pointers
	
	
	printf("adding screen %d: \n\tH %d \n\tW %d \n\tX %d \n\tY %d\n",n,h,w,x,y);
	new->next = NULL;
	if (firstscreen == NULL) {
		//set it as first screen
		firstscreen = new;
		cs = new;
		new->v = make_view();
		new->v->idx = 1;
		fv = new->v;
	} else {
		struct screen *s = firstscreen;
		while (s->next != NULL) {
			s = s->next;
		};
		s->next = new;
		new->v = find_view(n + 1);
	}
	//define its geomentry
	new->h = h;
	new->w = w;
	new->x = x;
	new->y = y;
	//make it a stack window
	new->stackid = XCreateSimpleWindow(dpy,root,0,((h - 15) + y),(w + x),15,1,stack_unfocus_pix,stack_background_pix);
	XSetWindowAttributes att;
	att.override_redirect = true;
	XChangeWindowAttributes(dpy,cs->stackid,CWOverrideRedirect,&att);
	XMapWindow(dpy,new->stackid);
	XSync(dpy,False);
}

//checks whether cont is part of the specified view
bool view_contains_win(struct view *v, struct cont *cont) {
	for (struct track *t = v->ft; t != NULL; t = t->next) {
		for (struct cont *c = t->c; c != NULL; c = c->next) {
			if (c == cont) {
				return true;
			};
		};
	};
	return false;
}

void remove_cont(struct cont *c) {
	struct win *w = c->win;
	//reset focus if necessary (prefer previously focused container if it's in the same view)
	if (w->prev_focus != NULL && view_contains_win(c->track->view, w->prev_focus)) {
		c->track->view->mfocus = w->prev_focus;
	} else if (c->prev != NULL) {
		c->track->view->mfocus = c->prev;
	} else if (c->next != NULL) {
		c->track->view->mfocus = c->next;
	} else if (c->track->prev != NULL) {
		c->track->view->mfocus = c->track->prev->c;
		while (c->track->view->mfocus->next != NULL) {
			c->track->view->mfocus = c->track->view->mfocus->next;
		};
	} else if (c->track->next != NULL) {
		c->track->view->mfocus = c->track->next->c;
	} else {
		c->track->view->mfocus = NULL;
	};
	//inorder to enable semi-peristent placement, we are going to record the last position of the window now
	//get the track offset, then add half of track size
	// also get cont offset and add half of cont size
	//but not if it is the only window on its view, as that is useless:
	if (c->track->next || c->track->prev) {
		int tcoord = 0;
		struct track *tp = c->track->view->ft;
		while (tp->next != NULL && tp != c->track) {
			tcoord += tp->size;
			tp = tp->next;
		};
		tcoord += tp->size / 2;
		c->win->last_tpos = tcoord;
	};
 
		
	if (c->next || c->prev) {
		int ccoord = 0;
		struct cont *cp = c->track->c;
		cp = c->track->c;
		ccoord = 0;
		while (cp->next != NULL && cp != c) {
			ccoord += cp->size;
			cp = cp->next;
		};
		ccoord += cp->size / 2;
		c->win->last_cpos = ccoord;
		c->win->last_only_chld = false;
	} else if (c->track->next || c->track->prev) {
		c->win->last_only_chld = true;
	};
	
	w->cont = NULL;

	
	//remove c and clean up
	//is c first in the track?
	if (c->track->c == c) {
		//is it only in track?
		if (c->next == NULL) {
			//is track first?
			if (c->track == c->track->view->ft) {
				//is track only?
				if (c->track->next == NULL) {
					c->track->c = NULL;
					//if we are in fs we need to show the stack now
					c->track->view->fs = false;
					c->track->view->showstack = true;
					free(c);
					//set focus to root so we still get keys!
					XSetInputFocus(dpy,root,None,CurrentTime);
				} else {
					//its the first track, but there is a next
					c->track->view->ft = c->track->next;
					c->track->next->prev = NULL;
					free (c->track);
					free (c);
				};
			} else { //not first track 
				//delete it 
				c->track->prev->next = c->track->next;
				if (c->track->next != NULL) {
					c->track->next->prev = c->track->prev;
				};
				free (c->track);
				free(c);
			}; 
		} else {//its just first
			c->track->c = c->next;
			c->next->prev = NULL;
			free(c);
		};
	} else { // is there a prev 
		c->prev->next = c->next;
		if (c->next != NULL) {
			c->next->prev = c->prev;
		};
		free(c);
	};
}


struct win * add_win(Window  id) {
	//PropertyChangeMask might be important for future features
	if (sloppy_focus == true) {
		XSelectInput(dpy,id,PointerMotionMask | PointerMotionHintMask | EnterWindowMask);
	};
	XSetWindowBorderWidth(dpy,id,1);
	struct win *p = (struct win *) malloc(sizeof(struct win));
	p->take_focus = false;
	p->del_win = false;
	Atom *prot = NULL;
	Atom *pp = NULL;
	int n, j;
	if (XGetWMProtocols(dpy, id, &prot, &n)) {
		for (j = 0, pp = prot; j < n; j++, pp++) {
			if (*pp == wm_del_win) {
				p->del_win = true;
			} else if (*pp == wm_take_focus) {
				p->take_focus = true;
			};
		};
 	};
	if (prot) {
		XFree(prot);
	};
	p->next = first_win;
	first_win = p;
	p->id = id;
	p->last_tpos = 0;
	p->last_cpos = 0;
	p->last_only_chld = false;
	p->cont = NULL;
	p->prev_focus = NULL;
	return p;
}

// finds window in layout and removes it (possibly removing its parent track)
void erase_win (struct win * w) {
	struct view *v = fv;
	struct track *t;
	struct cont *c;
	//TODO, now that wins point to their cont we can remove these nested loops
	//HOWEVER: WE MUST ENSURE THAT WE NEVER call add_client_to_view until we have first removed it from any view it is on. 
	while (v != NULL) {
		t = v->ft;
		while (t != NULL) {
			c = t->c;
			while (c != NULL) {
				if (c->win == w) {
					//if it is the main focus on a fullscreen desktop
					//take the desktop out of fs
					//c->track->view->fs = false;
					//reset focus if necessary
					if (v->mfocus == c) {
						if (c->next != NULL) {
							v->mfocus = c->next;
						} else if (c->prev != NULL) {
							v->mfocus = c->prev;
						} else if (c->track->next != NULL) {
							v->mfocus = c->track->next->c;
						} else if (c->track->prev != NULL) {
							v->mfocus = c->track->prev->c;
						} else {
							v->mfocus = NULL;
						};
					};
					//remove c and clean up
					//is c first in the track?
					if (c->track->c == c) {
						//is it only in track?
						if (c->next == NULL) {//remove track
							//is track first?
							if (c->track == c->track->view->ft) {
								//is track only?
								if (c->track->next == NULL) {
									c->track->c = NULL;
									free(c);
								} else {
									//its the first track, but there is a next
									cs->v->ft = c->track->next;
									c->track->next->prev = NULL;
									free (c->track);
									free (c);
								};
							} else { //not first track 
								//delete it 
								c->track->prev->next = c->track->next;
								if (c->track->next != NULL) {
									c->track->next->prev = c->track->prev;
								};
								free (c->track);
								free(c);
							}; 
						} else {//its just first
							c->track->c = c->next;
							c->next->prev = NULL;
							free(c);
						};
					} else { //there's a prev 
						c->prev->next = c->next;
						if (c->next != NULL) {
							c->next->prev = c->prev;
						};
						free(c);
					};

                    return;
				};
				c = c->next;
			};
			t = t->next;
		};
		v = v->next;
	};
}

void forget_win (Window id) {
	//first see whether we have a record of it
	if (first_win == NULL) {
		fprintf(stderr,"euclid-wm: cannot remove window %6.0lx, internal data structure is corrpupt or there are not windows being managed (no first_win defined)\n",id);
		return;
	};
	struct win * w = first_win;
	struct win * w2 = first_win;
	if (w->id != id) {
		while (w->next != NULL && w->next->id != id) {
			w = w->next;
		};
		w2 = w; //this should be the win struct before the one we are deleting
		w = w->next;
	}; 
	
	if (w == NULL) { 
		return;
	};

    erase_win(w);
	//we also need to check the stacks:
	struct view *v = fv;
	struct stack_item *s = NULL;
	while (v != NULL) {
		s = v->stack;
		while (s != NULL) {
			//storing this in case the entry will get free()'d
			struct stack_item *next_s = s->next;

			if (s->win == w) {
				if (s == v->stack) {
					v->stack = s->next;
				};
				if (s == v->sfocus) {
					if (s->next != NULL) {
						v->sfocus = s->next;
					} else {
						v->sfocus = s->prev;
					};
				};
				if (s->next != NULL) {
					s->next->prev = s->prev;
				};
				if (s->prev != NULL) {
					s->prev->next = s->next;
				};
				if (s == v->stack) {
					v->stack = s->next;
				};
				free(s);
			};

			s = next_s;
		};
		v = v->next;
	};
	//remove w
	//is it first?
	if (first_win == w) {
		first_win = w->next;
	};
	w2->next = w->next;
	free(w);
}

void add_client_to_view (struct win *p, struct view *v) {
	//first make sure it is not already in the target view:
	//while doing this we are also going to count the tracks, to help with finding a good place for the new container later
	//printf("adding client to view\n");
	struct track *tmpt = v->ft;
	struct cont *tmpc = NULL;
	int trks = 0;
	int cnts = 0;
	while (tmpt != NULL) {
		trks++;
		tmpc = tmpt->c;
		while (tmpc != NULL) {
			if (tmpc->win->id == p->id) {
				return;
			};
			tmpc = tmpc->next;
		};
		tmpt = tmpt->next;
	};


	//make a cont for it
	struct cont *c = (struct cont *) malloc  (sizeof(struct cont));
	c->win = p;
	p->cont = c;
	
	//starting with the current track, walk thorugh the tracks until certain conditions are met (we find a track with enough room, or we get back to where we started), storing useful information as we go
	//preferences are
	//if current track has fewer conts than total tracks in the view, put it in current track
	//if current track as many conts as tracks, but another track has fewer conts than track, put it in the first track that meets that condition
	//if current track has tracks + 1 cont, put it in current track
	//if current track has tracks + 1 cont, but another track has fewer conts (i.e., conts <= tracks) put it in the first track that meets that condition
	//otherwise make a new track and put the window there
	if (autobalance) { 
		if (p->last_tpos && v->mfocus != NULL) { 
			//if we have a record try to put it where it last was. 
			//walk the layout, looking for the track and cont that overlays last_tpos and last_cpos
			struct track *tp = v->ft;
			struct cont *cp;
			int tcoord = 0;
			int ccoord = 0;
			while (tp->next != NULL && tcoord + tp->size < p->last_tpos) {
				tcoord += tp->size;
				tp = tp->next;
			};
			//does the window go in the this track or did it have its own? 
			//only way to decide is to keep a bool of last state. let's call it: last_only_chld
			if (p->last_only_chld) {
				//we have to make a new track for it
				struct track *nt = (struct track *) malloc(sizeof(struct track));
				nt->view = v; 
				nt->c = c;
				nt->size = tp->size;
				c->prev = NULL;
				c->next = NULL;
				c->track = nt;
				//decide where the new track goes:
				if (tcoord + tp->size / 2 >= p->last_tpos) {
					//put it before tp
					nt->next = tp;
					nt->prev = tp->prev;
					tp->prev = nt;
					if (nt->prev == NULL) { 
						v->ft = nt;
					} else {
						nt->prev->next = nt;
					};
				} else {
					//put it after
					nt->prev = tp;
					nt->next = tp->next;
					if (tp->next) {
						tp->next->prev = nt;
					};
					tp->next = nt;
				};
				
			} else { 
				//shove it into an existing track, ie, tp
				//decide whether to put p above or below cp:
				cp = tp->c;
				while (cp->next !=NULL && ccoord + cp->size < p->last_cpos) {
					ccoord += cp->size;
					cp= cp->next;
				};
				c->track = tp;
				c->size = cp->size;
				if (ccoord + cp->size / 2 >= p->last_cpos) {
					//put it in front of tc;
					c->next = cp;
					c->prev = cp->prev;
					cp->prev = c;
					if (c->prev == NULL) {
						c->track->c = c;
					} else {
						c->prev->next = c;
					};
				} else {
					//put it after
					c->prev = cp;
					c->next = cp->next;
					cp->next = c;
					if (c->next) {
						c->next->prev = c;
					};
				};
	
			};	
	
			
		} else if (v->mfocus != NULL) { //we get here if it is a window with no previous position but is being add to a view with other windows
		
			tmpt = v->mfocus->track;
			struct cont *tmpc = tmpt->c;
			struct cont *bestp = NULL; //we set this to whatever track has the fewest conts in it but it has fewer conts than tracks
			int btcnts = 0; //number of conts in best track
			int ctcnts = 0; //current track's number of conts;
			do {
				cnts = 1;
					while ( cnts < trks + 1 &&  tmpc->next != NULL){ //we stop if we realize there are too many contrs in this track already, or if we run out of conts to count. 
						cnts++;
						tmpc = tmpc->next;
					}; 			
				if (tmpt == v->mfocus->track) {
					ctcnts = cnts;
				} else if (cnts <= trks && bestp == NULL) { 
					bestp = tmpc;
					btcnts = cnts;
				} else if (cnts < btcnts) {
					bestp = tmpc;
					btcnts = cnts;
				};
				if (tmpt->next != NULL) {
					tmpt = tmpt->next;
				} else {
					tmpt = v->ft;
				};
				tmpc = tmpt->c;
			} while (tmpt != v->mfocus->track); 
			if (btcnts < ctcnts && btcnts < trks + 1 && bestp) {
				//put it in bestp
				c->next = bestp->next;
				c->prev = bestp;
				bestp->next = c;
				c->track = bestp->track;
				c->size = bestp->size;
			} else if (ctcnts < trks + 1) {
				//put it after mfocus
				c->next = v->mfocus->next;
				c->prev = v->mfocus;
				if (v->mfocus->next) {
					v->mfocus->next->prev = c;
				};
				v->mfocus->next = c;
				c->track = v->mfocus->track;
				c->size = v->mfocus->size;
	
	
			} else {
				//make a new  track for it
				while (tmpt->next != NULL) {tmpt = tmpt->next;};
				//make track, 
				struct track *nt = (struct track *) malloc(sizeof(struct track));
				tmpt->next = nt;
				nt->next = NULL;
				nt->prev = tmpt;
				nt->view = v;
				nt->c = c;
				nt->size = tmpt->size;
				//set cont in it
				c->track = nt;
				c->next = NULL;
				c->prev = NULL;
				c->size = 100; //doesn't matter; layout will figure it out
			
			}; 
		} else { //first client on view 
			c->next = NULL;
			c->prev = NULL;
			c->track = v->ft;
			v->ft->c = c;
			if (v->orientv == true) {
				c->size = cs->h;
			} else {
				c->size = cs->w;
			};
	
		};
	
// Old version: just put window below window with focus
	} else { 
		if (v->mfocus != NULL) {
			c->next = v->mfocus->next;
			c->prev = v->mfocus;
			if (v->mfocus->next != NULL) {
				v->mfocus->next->prev = c;
			};
			v->mfocus->next = c;
			c->track = v->mfocus->track;
			c->size = v->mfocus->size;
		} else {
			//first client in view
			c->next = NULL;
			c->prev = NULL;
			c->track = v->ft;
			v->ft->c = c;
			if (v->orientv == true) {
				c->size = cs->h;
			} else {
				c->size = cs->w;
			};
		};
		c->win = p;
	};
//
	v->mfocus = c;
}

void move_to_stack(struct cont *c) {
	struct stack_item *s = (struct stack_item *) malloc (sizeof(struct stack_item));
	struct stack_item *p = cs->v->sfocus;
	s->win = c->win;
	if (p != NULL) {
		s->next = p->next;
		if (p->next != NULL) {
			p->next->prev = s;
		};
		p->next = s;
	} else {
		cs->v->stack = s;
		s->next = NULL;
	};
	s->prev = p;
	cs->v->sfocus = s;
	remove_cont(c);
}

void move_to_main() {
	//just add whatever has stack focus to the layout
	if (cs->v->sfocus == NULL) {return;};
	add_client_to_view(cs->v->sfocus->win, cs->v);
	//remove it from the stack:
	struct stack_item *p = cs->v->sfocus;
	//reset sfocus
	cs->v->sfocus = NULL;
	if (p->prev != NULL) {
		cs->v->sfocus = p->prev;
		p->prev->next = p->next;
	};
	if (p->next != NULL) {
		p->next->prev = p->prev;
		if (cs->v->sfocus == NULL) {
			cs->v->sfocus = p->next;
			cs->v->stack = p->next;
		};
	};
	if (cs->v->sfocus == NULL) {
		cs->v->stack = NULL;
	};
	free(p);
}

void shift_stack_focus (bool dir) {
	//true up, false, down
	if (cs->v->sfocus == NULL) {return;}; //there is not stack
	if (dir && cs->v->sfocus->prev != NULL) {
		cs->v->sfocus = cs->v->sfocus->prev;
	} else if (dir && cs->v->sfocus->prev == NULL) {
		//wrap to last, follow next till we run out
		while (cs->v->sfocus->next != NULL) {
			cs->v->sfocus = cs->v->sfocus->next;
		};
	} else if (!dir && cs->v->sfocus->next != NULL) {
		cs->v->sfocus = cs->v->sfocus->next;
		//wrap, jump to first
	} else if (!dir && cs->v->sfocus->prev != NULL) {
		cs->v->sfocus = cs->v->stack;
	};
}

bool is_top_level(Window id) {
	Window r; //root return;
	Window p; //parent return;
	Window *c; //children;
	unsigned int nc; //number of children 
	//this is a hack, hopefully temproary: for some reason in a multiscreen setup the stack for the second screen is showing up as a normal top level window:

	struct screen *s = firstscreen;
	while (s != NULL) {
		if (s->stackid == id) {
			return false;
		};
		
		s = s->next;
	};

	gxerror = false;
	XQueryTree(dpy,root,&r,&p,&c,&nc);
	if (gxerror) {
		gxerror = false;
		return(false);
	};
	int i;
	//what we are doing here is seeing if it is a child of the root window or not. 
	for (i = 0; i < nc; i++) {
		if (c[i] == id) {
			//this is a child of the root window, so we are potentially interested. 
			XFree(c);
			XWindowAttributes wa;
			XGetWindowAttributes(dpy,id,&wa);
			if (gxerror == true) {
				gxerror = false;
				return(false);
			};
			
			if (wa.override_redirect == true) {
				return(false);
			}; 
			//here check if it has declared itself a dock--in which case, let it manage itself. 
			Atom actual_type;
   			int actual_format;
			unsigned long nitems, bytes_after;
   			unsigned char *prop;
			Atom net_wm_type = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
			if (XGetWindowProperty(dpy, id, net_wm_type, 0L, 1L, False,
                              XA_ATOM, &actual_type, &actual_format, &nitems,
                              &bytes_after, &prop) == Success && prop) {
				Atom *atoms = (Atom*)prop;
		            	Atom atom = atoms[0];
				if (atom == wm_dock) {
					printf("New window presenting as a dock--not managing\n");
					XFree(prop);
					return (false);
				} else {
					XFree(prop);
					return(true);
				}	
			
			} else {
				//atom was not set, so treat as top level.
				return(true);
			};
		};
	};
	XFree(c);
	return(false);
}

short int convert_to_internal_dir(short int dir) {/*four possibilities: 
	 *  1) we are moving down in the track
	 *  2) we are movign up in the track
	 *  3) we are moving down accross tracks
	 *  4) we are moving up accross tracks
	 */
	if (cs->v->orientv == true) {
		short swp[] = {0,2,3,1,4};
		dir = swp[dir];
	} else {
		short swp[] = {0,4,1,3,2};
		dir = swp[dir];
	};
	return dir;
}
void shift_window(short int dir) {
	if (cs->v->mfocus == NULL) {
		return;
	};
	dir = convert_to_internal_dir(dir);
	if (dir == 1 && cs->v->mfocus->next != NULL) { //down in the track;
		//get a direct reference to all four nodes
		//make sure that we are also updating track->c if necessary 
		struct cont *tmpa;
		struct cont *tmpb;
		struct cont *tmpc;
		struct cont *tmpd;
		tmpa = cs->v->mfocus->prev;
		tmpb = cs->v->mfocus;
		tmpc = cs->v->mfocus->next;
		if (cs->v->mfocus->next != NULL) {
			tmpd = cs->v->mfocus->next->next;
		} else {tmpd = NULL;};
		if (tmpa != NULL) {
			tmpa->next = tmpc;
		} else { //a is null, b is track->c
			cs->v->mfocus->track->c = tmpc;
		};
		if (tmpd != NULL) {
			tmpd->prev = tmpb;
		};
		if (tmpc != NULL) {
			tmpc->prev = tmpa;
			tmpc->next = tmpb;
		};
		tmpb->prev = tmpc;
		tmpb->next = tmpd;
	} else if (dir == 2 && cs->v->mfocus->prev != NULL) {
		struct cont *tmpa;
		struct cont *tmpb;
		struct cont *tmpc;
		struct cont *tmpd;
		if (cs->v->mfocus->prev != NULL) {
			tmpa = cs->v->mfocus->prev->prev;
		} else {
			tmpa = NULL;
		};
		tmpb = cs->v->mfocus->prev;
		tmpc = cs->v->mfocus;
		tmpd = cs->v->mfocus->next;
		if (tmpa != NULL) {
			tmpa->next = tmpc;
		};
		if (cs->v->mfocus->track->c == tmpb) {
			cs->v->mfocus->track->c = tmpc;
		};
		if (tmpb != NULL) {
			tmpb->prev = tmpc;
			tmpb->next = tmpd;
		};
		if (tmpd != NULL) {
			tmpd->prev = tmpb;
		};
		tmpc->prev = tmpa;
		tmpc->next = tmpb;
	} else if (dir == 3) {
		//check for track->next
		if (cs->v->mfocus->track->next != NULL) {
			//find position in it
			//find the midpoint of the current cont
			struct cont *p;
			p = cs->v->mfocus->prev;
			int s = 0;
			int t = 0;
			while (p != NULL) {
				s += p->size;
				p = p->prev;
			};
			s += (cs->v->mfocus->size / 2);
			p = cs->v->mfocus->track->next->c;
			while (p->next != NULL) {
				t += p->size;
				if (t >= s) {break;};
				p = p->next;
			};
			if (p != NULL) {//it never should
				if (cs->v->mfocus->prev != NULL) {
					cs->v->mfocus->prev->next = cs->v->mfocus->next;
				} else { //it's first 
					if (cs->v->mfocus->next != NULL) {
						cs->v->mfocus->track->c = cs->v->mfocus->next;
					} else {
						//only one in the track
						//remove the old track
						if (cs->v->ft == cs->v->mfocus->track) {
							cs->v->ft = p->track;
						} else {
							cs->v->mfocus->track->prev->next = p->track;
						}; 
						p->track->prev = cs->v->mfocus->track->prev;
						free(cs->v->mfocus->track);
					};
				};
				if (cs->v->mfocus->next != NULL) {
					cs->v->mfocus->next->prev = cs->v->mfocus->prev;
				};
				//put it behind p
				struct cont *b = p->next;
				if (b != NULL) {
					b->prev = cs->v->mfocus;
				};
				p->next = cs->v->mfocus;
				cs->v->mfocus->prev = p;
				cs->v->mfocus->next = b;
				cs->v->mfocus->track = p->track;
			};
		}else{ //make a track for it
			if (cs->v->mfocus->track->c == cs->v->mfocus) { 
				if (cs->v->mfocus->next != NULL) {
						cs->v->mfocus->track->c = cs->v->mfocus->next;
					} else if (cs->v->mfocus->prev != NULL) {
						cs->v->mfocus->track->c = cs->v->mfocus->prev;
					} else {
						return; //premature return, because movign the window that direction doesn't make any sense
					};
				};
			struct track *ptr = (struct track *) malloc (sizeof(struct track));
			cs->v->mfocus->track->next = ptr;
			//update all the other links
			ptr->view = cs->v;
			ptr->next = NULL;
			ptr->prev = cs->v->mfocus->track;
			ptr->c = cs->v->mfocus;
			ptr->size = cs->v->mfocus->track->size;
			cs->v->mfocus->track = ptr;
			//patch up the hole in mfocus->track
			if (cs->v->mfocus->prev != NULL) 
				cs->v->mfocus->prev->next = cs->v->mfocus->next;
			if (cs->v->mfocus->next != NULL)
				cs->v->mfocus->next->prev = cs->v->mfocus->prev;
			cs->v->mfocus->next = NULL;
			cs->v->mfocus->prev = NULL;
		};
	} else if (dir == 4) { //4
		if (cs->v->mfocus->track->prev != NULL) {
			//find position in it
			//find the midpoint of the current cont
			struct cont *p;
			p = cs->v->mfocus->prev;
			int s = 0;
			int t = 0;
			while (p != NULL) {
				s += p->size;
				p = p->prev;
			};
			s += (cs->v->mfocus->size / 2);
			p = cs->v->mfocus->track->prev->c;
			while (p->next != NULL) {
				t += p->size;
				if (t >= s) {break;};
				p = p->next;
			};
			if (p != NULL) {//it never should
				if (cs->v->mfocus->prev != NULL) {
					cs->v->mfocus->prev->next = cs->v->mfocus->next;
				} else { //it's first in the track
					if (cs->v->mfocus->next != NULL) {
						cs->v->mfocus->track->c = cs->v->mfocus->next;
					} else {
						//only one in the track
						//remove the old track
						if (cs->v->mfocus->track->next != NULL) {
							cs->v->mfocus->track->next->prev = p->track;
						}; 
						p->track->next = cs->v->mfocus->track->next;
						free(cs->v->mfocus->track);
					};
				};
				if (cs->v->mfocus->next != NULL) {
					cs->v->mfocus->next->prev = cs->v->mfocus->prev;
				};
				//put it behind p
				struct cont *b = p->next;
				if (b != NULL) {
					b->prev = cs->v->mfocus;
				};
				p->next = cs->v->mfocus;
				cs->v->mfocus->prev = p;
				cs->v->mfocus->next = b;
				cs->v->mfocus->track = p->track;
			};
		}else{ //make a track for it
			if (cs->v->mfocus->track->c == cs->v->mfocus) { 
				if (cs->v->mfocus->next != NULL) {
						cs->v->mfocus->track->c = cs->v->mfocus->next;
					} else if (cs->v->mfocus->prev != NULL) {
						cs->v->mfocus->track->c = cs->v->mfocus->prev;
					} else {
						return; //premature return, because movign the window that direction doesn't make any sense
					};
				};
			struct track *ptr = (struct track *) malloc (sizeof(struct track));
			cs->v->mfocus->track->prev = ptr;
			cs->v->ft = ptr;
			//update all the other links
			ptr->view = cs->v;
			ptr->next = cs->v->mfocus->track;
			ptr->prev = NULL;
			ptr->c = cs->v->mfocus;
			ptr->size = cs->v->mfocus->track->size;
			cs->v->mfocus->track = ptr;
			//patch up the hole in mfocus->track
			if (cs->v->mfocus->prev != NULL) 
				cs->v->mfocus->prev->next = cs->v->mfocus->next;
			if (cs->v->mfocus->next != NULL)
				cs->v->mfocus->next->prev = cs->v->mfocus->prev;
			cs->v->mfocus->prev = NULL;
			cs->v->mfocus->next = NULL;
		};
	};
	if (cs->v->mfocus->prev != NULL) {
		cs->v->mfocus->size = cs->v->mfocus->prev->size;
	} else if (cs->v->mfocus->next != NULL) {
		cs->v->mfocus->size = cs->v->mfocus->next->size;
	};
}

void shift_main_focus(short int dir) {
	if (cs->v->mfocus == NULL) {return;};
	dir = convert_to_internal_dir(dir);
	if (dir == 1) {
		if (cs->v->fs == false) {
			if (cs->v->mfocus->next != NULL) {
				cs->v->mfocus =  cs->v->mfocus->next;
			};
		} else {
			if (cs->v->mfocus->next != NULL) {
				cs->v->mfocus = cs->v->mfocus->next;
			} else if (cs->v->mfocus->track->next != NULL) {
				cs->v->mfocus = cs->v->mfocus->track->next->c;
			};
		};
	} else if (dir == 2) { 
		if (cs->v->fs == false) {
			if (cs->v->mfocus->prev != NULL) {
				cs->v->mfocus = cs->v->mfocus->prev;
			};
		} else {
			if (cs->v->mfocus->prev != NULL) {
				cs->v->mfocus = cs->v->mfocus->prev;
			} else if (cs->v->mfocus->track->prev != NULL ) {
				struct cont *tmpc = cs->v->mfocus->track->prev->c;
				while (tmpc->next != NULL) {
					tmpc = tmpc->next;
				};
				cs->v->mfocus = tmpc;
			};
		};
	} else if (dir == 3 && cs->v->mfocus->track->next != NULL) {
		if (cs->v->fs == false) {
			struct cont *p;
				p = cs->v->mfocus->prev;
				int s = 0;
				int t = 0;
				while (p != NULL) {
					s += p->size;
					p = p->prev;
				};
				s += (cs->v->mfocus->size / 2);
				p = cs->v->mfocus->track->next->c;
				while (p->next != NULL) {
					t += p->size;
					if (t >= s) {break;};
					p = p->next;
				};
				cs->v->mfocus = p;
		};
	} else if (dir == 4 && cs->v->mfocus->track->prev != NULL) {
		if (cs->v->fs == false) {
			struct cont *p;
				p = cs->v->mfocus->prev;
				int s = 0;
				int t = 0;
				while (p != NULL) {
					s += p->size;
					p = p->prev;
				};
				s += (cs->v->mfocus->size / 2);
				p = cs->v->mfocus->track->prev->c;
				while (p->next != NULL) {
					t += p->size;
					if (t >= s) {break;};
					p = p->next;
				};
				cs->v->mfocus = p;
		};
	};
}

struct view * find_view(int i) {
	//this will return a pointer to a given view
	//even if it must first create it
	//some views get a numbered index
	//i -2 means move forward -3 means backward
	//-1 means last
	struct view *v = NULL;
	if (i == -2) {
		//move forward
		if (cs->v->next != NULL && cs->v->next->idx == cs->v->idx + 1) {
			return(cs->v->next);
		} else {
			//make cs->v->next
			v = make_view();
			if (cs->v->next != NULL) {
				cs->v->next->prev = v;
			};
			v->next = cs->v->next;
			cs->v->next = v;
			v->prev = cs->v;
			v->idx = (cs->v->idx + 1);
		};
	} else if (i == -3) {
		//move backward
		if (cs->v->prev != NULL && cs->v->prev->idx == cs->v->idx - 1) {
			return(cs->v->prev);
		} else {
			//make cs->v->prev
			v = make_view();
			v->next = cs->v;
			//v next, v prev, cs prev next, cs prev
			v->prev = cs->v->prev;
			if (cs->v->prev != NULL) {
				cs->v->prev->next = v; 
			};
			cs->v->prev = v;
			v->idx = (cs->v->idx - 1);
			if (fv == v->next) {
				fv = v;
			};
		};
	} else if (i == -1) {
		v = cs->v;
		while (v->next != NULL) {
			v = v->next;
		};
	} else if (i <= 9 && i >= 1) {
			v = fv;
			while (v->next != NULL && v->next->idx < i) {
				v = v->next;
			};
			if (fv->idx > i) { 
				v = make_view();
				v->idx = i;
				v->next = fv;
				v->prev = NULL;
				v->next->prev = v;
				fv = v;
			} else if (v->next == NULL) {
				//we overshot, and ran out of views: make the view
				//or we are looking for fv
				if (i == v->idx) {
					return(v);
				};
				v->next = make_view();
				v->next->idx = i;
				v->next->prev = v;
				v = v->next;
			} else if (v->next->idx > i && v->idx < i) {
				//we overshot, but there are higher indexed views
				//make a view in front of v
				//store v->next
				struct view *v2 = v->next;
				v->next = make_view();
				v->next->idx = i;
				v->next->prev = v;
				v->next->next = v2;
				v2->prev = v->next;
				v = v->next;
			} else if (v->next->idx == i) {
				v = v->next;
			};
	};
	return(v);
}

void set_desktop_name(int i) {
	char desktop_name[128];
	snprintf(desktop_name, sizeof(desktop_name), "%d", i);
	XChangeProperty(dpy,root,current_desktop,utf8,8,PropModeReplace,(unsigned char *) desktop_name,strlen(desktop_name));
}

void goto_view(struct view *v) {
	//this just unmaps the windows of the current view
	//sets cs->v
	//and maps the windows of the new cs->v
	//it also deletes empty views
	if (v == NULL) {return;};
	struct screen *s = firstscreen;
	while (s != NULL) {
		if (s->v == v) {return;};
		s = s->next;
	};

	last_view_idx = cs->v->idx;

	struct track *t;
	struct cont *c;

	t = v->ft;
	while (t != NULL) {
		c = t->c;
		while (c != NULL) {
			XMapWindow(dpy,c->win->id);
			c = c->next;
		};
		t = t->next;
	};

	t = cs->v->ft;
	while (t != NULL) {
		c = t->c;
		while (c != NULL) {
			XUnmapWindow(dpy,c->win->id);
			c = c->next;
		};
		t = t->next;
	};
	if (cs->v->mfocus == NULL && cs->v->sfocus == NULL) {
		if (cs->v->prev != NULL) {
			cs->v->prev->next = cs->v->next;
		};
		if (cs->v->next != NULL) {
			cs->v->next->prev = cs->v->prev;
		};
		if (cs->v == fv) {
			fv = cs->v->next;
		};
		free(cs->v);
	};

	cs->v = v;

	gettimeofday(&last_redraw,0);

	set_desktop_name(v->idx);
}


void move_to_view(struct view *v) {
	//move currenlty focused item
	if (v == NULL || v == cs->v || cs->v->mfocus == NULL) {return;};
	struct win *w = cs->v->mfocus->win;
	//remove it from the current view
	//we might want to reset its state information.
	//i think we do, at least if it is getting sent to a view with more than one other window
	if (v->ft->next || (v->ft->c && v->ft->c->next)) {
		 //clear position information
		w->last_only_chld = false;
		w->last_tpos = 0;
		w->last_cpos = 0;
	};

	//check whether the view we are moving it to is displayed before unmapping
	struct screen *s = firstscreen;
	while (s != NULL && s->v != v) {
		s = s->next;
	};
	if (s == NULL) {
		XUnmapWindow(dpy,w->id);
	};
	remove_cont(cs->v->mfocus);
	//add it to the new view
	add_client_to_view(w, v);
}

struct cont * id_to_cont(Window w) {
	//struct track *t;
	//struct cont *c;
	//struct screen *s = firstscreen;
	/*while (s != NULL) {
	//TODO: First, I thought views were screen independed, so we should be looping through all views,
	//not just those that are on a screen
	//Second: this is expensive and gets called a lot: We should have the win struct point to the cont the window is displayed in
	//we should be able to just update add_client to view and remove_cont
		t = s->v->ft;
		while (t != NULL) {
			c = t->c;
			while (c!= NULL) {
				if (c->win->id == w) {
					return (c);
				};
				c = c->next;
			};
			t = t->next;
		};
	s = s->next;
	};
	return (NULL);
	*/
	struct win *ws = first_win;
	while (ws != NULL && ws->next != NULL && ws->id!=w){
		ws = ws->next;
	};
	if (ws != NULL && ws->id == w) {
		return (ws->cont);
	} else {
		return (NULL);
	};
}

void resize (int dir) {
	if (cs->v->mfocus == NULL) {
		return;
	};
	if (cs->v->orientv == true) {
		switch (dir) {
			case 1:
				cs->v->mfocus->size -= resize_inc;
			break; 
			case 2:
				cs->v->mfocus->track->size += resize_inc;
			break;
			case 3:
				cs->v->mfocus->size += resize_inc;
			break;
			case 4:
				cs->v->mfocus->track->size -= resize_inc;
			break;
		};
	} else {
		switch (dir) {
			case 1:
				cs->v->mfocus->track->size += resize_inc;
			break; 
			case 2:
				cs->v->mfocus->size += resize_inc;
			break;
			case 3:
				cs->v->mfocus->track->size -= resize_inc;
			break;
			case 4:
				cs->v->mfocus->size -= resize_inc;
			break;
		};
	};
}

void search_wins() {

	char *fname = (char *) tempnam(NULL,"eucld");
	FILE *list = fopen(fname,"w");
	struct view *v = fv;
	while (v != NULL) {
		struct track *t = v->ft;
		while (t != NULL) {
			struct cont *c = t->c;
			while (c != NULL) {
				//print	
				XTextProperty wmname;
				XGetWMName(dpy,c->win->id,&wmname);
				char ent[128];
				int i = 0;
				int limit = 128 < (wmname.nitems) ? 128 : (wmname.nitems);
				while (i < limit) {
					ent[i] = wmname.value[i];
					i++;
				};
				ent[i] = '\0';
				fprintf(list,"%s [%d]\n",ent, (int) c->win->id);
				c = c->next;
			};
			t = t->next;
		};
		struct stack_item *s = v->stack;
		while (s != NULL) {
			XTextProperty wmname;
			XGetWMName(dpy,s->win->id,&wmname);
			char ent[128];
			int i = 0;
			int limit = 128 < (wmname.nitems) ? 128 : (wmname.nitems);
			while (i < limit) {
				ent[i] = wmname.value[i];
				i++;
			};
			ent[i] = '\0';
			fprintf(list,"%s [%d]\n",ent, (int) s->win->id);
	
			s = s->next;
		};

		v = v->next;
	};	

		
		fclose(list);
		FILE *ret;
		char *com;
		if (win_menu == false) { 
			com = malloc(strlen(fname) + 12);
			strcpy(com,"dmenu -i < ");
			strcat(com,fname);

		} else {

			com = malloc(strlen(fname) + 35);
			strcpy(com,"euclid-menu -l \".echo_file.sh ");
			strcat(com,fname);
			strcat(com," \" -r");

		};
		ret = (FILE *) popen(com,"r");
		free(com);
		
		char buff[256];
		if (!ret) {
			printf("ERROR opening dmenu\n");
		} else {
			fgets(buff,256,ret);
		};
		unlink(fname);	
		free(fname);
		
		if (ret) {
			pclose(ret);
		} else {
			return;
		};
		
		// parse the return, buff
		int last = strlen(buff);
		int pos = last;
		while (buff[pos] != '[' && pos != 0) {
			pos--;
		};
		if (pos == 0) {
			return;
		};
		pos++;
		char winnum[32];
		int pos2 = 0; 
		while (buff[pos] != ']') {
			winnum[pos2] = buff[pos];
			pos++;
			pos2++;
		};
		winnum[pos2] = '\0';
		int id = atoi(winnum);
	v = fv;
	while (v != NULL) { //we can speed this up by using win->c
		//search main area
		struct track *t = v->ft;
		while (t != NULL) {
			struct cont *c = t->c;
			while (c != NULL) {
				if (c->win->id == id) {
					//goto it and place focus on it
					goto_view(v);
					v->mfocus = c;
					return;
					
				};
				c = c->next;
			};
			t = t->next;
		};
		//search stack;
		struct stack_item *s = v->stack;
		while (s != NULL) {
			if (s->win->id == id) {
				//goto it
				goto_view(v);
				v->sfocus = s;
				move_to_main();
				XMapWindow(dpy,id);
				return;
			};
			s = s->next;
		};
		v = v->next;
	};
	
}

void layout() {
	struct screen *s = firstscreen;
	while (s != NULL) {
		int xo = s->x;
		int yo = s->y;
		int stackheight;
		if (s->v->showstack == false || s->v->fs == true) {
			stackheight = 0;
		} else {
			struct stack_item *si = s->v->stack;
			int i = 0;
			while (si != NULL) {
				i++;
				si = si->next;
			};
			stackheight = (i * 20); 
			if (i == 0) {
				stackheight = empty_stack_height;
			};
		};
		//draw the stack 
		XClearWindow(dpy,s->stackid);
		if (stackheight != 0) {
			XMoveResizeWindow(dpy,s->stackid,(res_left + xo),(((s->h - res_bot) - stackheight) + yo),(s->w - (res_left + res_right + 2)),(stackheight - 2));
			XRaiseWindow(dpy,s->stackid);
			XSync(dpy,false);//important!
			struct stack_item *si = s->v->stack;
			int i = 15;
			while (si != NULL) {
				GC gc;
				if (si == s->v->sfocus) {
					gc = focus_gc;
				} else {
					gc = unfocus_gc;
				};
				XTextProperty wmname;
				XGetWMName(dpy,si->win->id,&wmname);
				XDrawString(dpy,s->stackid,gc,3,i, (char *) wmname.value,wmname.nitems);
				si = si->next;
				i += 20;
			}; 
		} else { //hide stack
			XMoveResizeWindow(dpy,s->stackid,0,offscreen,s->w,10);
		};
		XSync(dpy,false);
		if (s->v->mfocus == NULL && cs == s) {
			XSetInputFocus(dpy,root,None,CurrentTime);
		};
		if (s->v->fs == true && s->v->mfocus != NULL && s->v->mfocus->win != NULL) {
			//draw mf fullscreen
			XSetWindowBorderWidth(dpy,s->v->mfocus->win->id,0);
			int w = s->w; //this overflows onto adjacent screens. 
			int h = s->h;
			h -= stackheight;
			XMoveResizeWindow(dpy,s->v->mfocus->win->id,(xo),(yo),(w),(h));
			XRaiseWindow(dpy,s->v->mfocus->win->id);
			if (cs == s) {
				if (s->v->mfocus->win->take_focus == true) {
					XClientMessageEvent cm;
					memset (&cm,'\0', sizeof(cm));
					cm.type = ClientMessage;
					cm.window = s->v->mfocus->win->id;
					cm.message_type = wm_prot;
					cm.format = 32;
					cm.data.l[0] = wm_take_focus;
					cm.data.l[1] = CurrentTime;
				};
				XSetInputFocus(dpy,s->v->mfocus->win->id,None,CurrentTime);
			};
			XSync(dpy,false); //could we postpone this till we are done?
		} else {
			//first check that the tracks layout:
			struct track *curt = s->v->ft;
			struct cont *curc = NULL;
			int target;
			int tot = 0;
			if (s->v->orientv == true) {
				target = s->w - (res_left + res_right);
			} else {
				target = (s->h - (res_top + res_bot)) - stackheight;
			};
			int nooftracks = 0;
			while (curt != NULL) {
				//check if the size is negligably small, requireing a reseize
				if (curt->size <= 5) {
					curt->size += 40;
				};
				tot += curt->size;
				curt = curt->next;
				nooftracks ++;
			};
			//compare tot to target, if they match go on 
			if (tot != target) {
				signed int delta = target - tot;
				delta = delta/nooftracks;
				curt = s->v->ft;
				//while (curt->next != NULL) {
				while (curt) {
					curt->size += delta;
					curt = curt->next;
				};
				//curt->size += difference - (delta*(nooftracks-1));
			};
			//else calculate the difference, and adjust all tracks
			//second check that within each track the containers fit
			curt = s->v->ft;
			if (s->v->orientv != true) {
				target = s->w - (res_left + res_right);
			} else {
				target = (s->h - (res_top + res_bot)) - stackheight;
			}; 
			while (curt != NULL) {
				curc = curt->c;
				int tot = 0;
				int noofconts = 0;
				while (curc != NULL) {
					//check for cont with negligable size and resize if necessary
					if (curc->size <= 5) {
						curc->size += 40;
					};
					noofconts ++;
					tot += curc->size;
					curc = curc->next;
				};
				//if tot == target, do nothing
				//else calculate and distribute difference
				if (tot != target) {
					signed int delta;
				 	if (noofconts != 0) {
						delta = target - tot;
						delta = delta/noofconts;
					} else {
						delta = 0;
					};
					curc = curt->c;
					while (curc != NULL) {
						curc->size += delta;
						//if (curc->next == NULL) {
						//	//ensure that the last the track is fully filled
						//	curc->size += difference - delta*noofconts;
					//	};
						curc = curc->next;
					};
				};
				curt = curt->next;
			};
			//walk and draw
			curt = s->v->ft; //first track of view
			int x;
			int y;
			int h;
			int w;
			int offsett;
			int offsetc;
			if (s->v->orientv) {
				offsett = res_left;
			} else {
				offsett = res_top;
			};
			while (curt != NULL) {
				//offsetc = 0;
				if (s->v->orientv) {
					offsetc = res_top;
				} else {
					offsetc = res_left;
				};
				curc = curt->c;
				while (curc != NULL) {
					//make sure we tell windows that think they are fs that they aren't
					if (curc->win->fullscreen == true) {
						curc->win->fullscreen = false;
						curc->win->req_fullscreen = false;
						XChangeProperty(dpy,curc->win->id,wm_change_state,XA_ATOM,32,PropModeReplace,(unsigned char *)0,0);
					};

					XSetWindowBorderWidth(dpy,curc->win->id,1);
					if (s == cs && curc == s->v->mfocus) {
					//set border
						XSetWindowBorder(dpy,curc->win->id,focus_pix);
						if (s->v->mfocus->win->take_focus == true) {
							XClientMessageEvent cm;
							memset (&cm,'\0', sizeof(cm));
							cm.type = ClientMessage;
							cm.window = s->v->mfocus->win->id;
							cm.message_type = wm_prot;
							cm.format = 32;
							cm.data.l[0] = wm_take_focus;
							cm.data.l[1] = CurrentTime;
						}; 
						//we intentionally do this; even if the event was sent, the
						//event alone does not suffice to get focus on the window
						XSetInputFocus(dpy,curc->win->id,None,CurrentTime);
					} else {
						XSetWindowBorder(dpy,curc->win->id,unfocus_pix);
					};
					//place window
					if (s->v->orientv == true) {
						x = offsett + xo;
						y = offsetc + yo;
						if (!curt->next) { //adjust for rounding error
							w = s->w - (offsett + 2 + res_right);
						} else { 
							w = curt->size - 2;
						};
						if (!curc->next) {
							h = s->h - (offsetc + 2 + stackheight + res_bot);
						} else {
							h = curc->size - 2;
						};
					} else {
						x = offsetc + xo;
						y = offsett + yo;
						if (!curc->next) {
							w = s->w - (offsetc + 2 + res_right);
						} else {
							w = curc->size - 2;
						};
						if (!curt->next) {
							h = s->h - (offsett + 2 + stackheight + res_bot);
						} else {
							h = curt->size - 2;
						};
					};
					XMoveResizeWindow(dpy,curc->win->id,(x),(y),(w),(h));
					offsetc += curc->size;
					curc = curc->next;
				};
				offsett += curt->size;
				curt = curt->next;
			};
		};
		s = s->next;
		XSync(dpy,false);
	};
}

int xerror(Display *d, XErrorEvent *e) {
	//get and print the error description for diagnostics:
	char buff[256];
	XGetErrorText(dpy, e->error_code, buff, 256);
	fprintf(stderr,"euclid-wm ERROR: X error: %s\n",buff);
	if (e->error_code == BadWindow) {
		forget_win((Window) e->resourceid);
	};
	gxerror = true;
	return(0);
}

int event_loop() {
	bool redraw;
	last_redraw.tv_sec = 0;
	last_redraw.tv_usec = 0;
	layout();
	XEvent ev;
	for (;;) {
		redraw = false; //this will get set to true if something gets changed onscreen
		do {
			XNextEvent(dpy, &ev);
			
			//Debugging, print all events:
			//char *events[] = {NULL, NULL, "KeyPress","KeyRelease","ButtonPress","ButtonRelease","MotionNotify","EnterNotify","LeaveNotify","FocusIn","FocusOut","KeymapNotify","Expose","GraphicsExpose","NoExpose","VisibilityNotify","CreateNotify","DestroyNotify","UnmapNotify","MapNotify","MapRequest","ReparentNotify","ConfigureNotify","ConfigureRequest","GravityNotify","ResizeRequest","CirculateNotify","CirculateRequest","PropertyNotify","SelectionClear","SelectionRequest","SelectionNotify","ColormapNotify","ClientMessage","MappingNotify","GenericEvent"};
			//printf ("eventtype: %d %s\n",ev.type,events[ev.type]);
		
			switch (ev.type){ //using a switch, event types are defined in /usr/include/X11/X.h; they range from 2-36
			case KeyPress:
			{
			//first find the keypress index from bindings[]
			//set the lockmask to 0 first
			//LockMask ^ ev.xkey.state
				int i = 0;
				//printf("\n%x -- %x\n",ev.xkey.keycode,ev.xkey.state);
				while (i < BINDINGS && (bindings[i].keycode != ev.xkey.keycode || *(bindings[i].mask) != (ev.xkey.state & ~LockMask &~Mod2Mask))) {
					i++;
				};
  
				switch (i) { 
					//resize
					case 0:
						resize(4);
						redraw = true;
						break;
					case 1:
						resize(3);
						redraw = true;
						break;
					case 2:
						resize(1);
						redraw = true;
						break;
					case 3:
						resize(2);
						redraw = true;
						break;
					//move win to view 1-0 n m
					case 4:
						move_to_view(find_view(1));
						redraw = true;
						break;
					case 5:
						move_to_view(find_view(2));
						redraw = true;
						break;
					case 6:
						move_to_view(find_view(3));
						redraw = true;
						break;
					case 7:
						move_to_view(find_view(4));
						redraw = true;
						break;
					case 8:
						move_to_view(find_view(5));
						redraw = true;
						break;
					case 9:
						move_to_view(find_view(6));
						redraw = true;
						break;
					case 10:
						move_to_view(find_view(7));
						redraw = true;
						break;
					case 11:
						move_to_view(find_view(8));
						redraw = true;
						break;
					case 12:
						move_to_view(find_view(9));
						redraw = true;
						break;
					case 13:
						move_to_view(find_view(-1));
						redraw = true;
						break;
					case 14:
						move_to_view(find_view(-3));
						redraw = true;
						break;
					case 15:
						move_to_view(find_view(-2));
						redraw = true;
						break;
					//change view
					//internally views are 0 index
					//userside 1 indexed, w/0 pointing to the last
					case 16: //1 pressed, view 0
						goto_view(find_view(1));
						redraw = true;
						break;
					case 17:
						goto_view(find_view(2));
						redraw = true;
						break;
					case 18:
						goto_view(find_view(3));
						redraw = true;
						break;
					case 19:
						goto_view(find_view(4));
						redraw = true;
						break;
					case 20:
						goto_view(find_view(5));
						redraw = true;
						break;
					case 21:
						goto_view(find_view(6));
						redraw = true;
						break;
					case 22:
						goto_view(find_view(7));
						redraw = true;
						break;
					case 23:
						goto_view(find_view(8));
						redraw = true;
						break;
					case 24:
						goto_view(find_view(9));
						redraw = true;
						break;
					//goto last:
					case 25:
						goto_view(find_view(-1));
						redraw = true;
						break;
					//goto next/prev
					case 26:
						goto_view(find_view(-3));
						redraw = true;
						break;
					case 27:
						goto_view(find_view(-2));
						redraw = true;
						break;
					//shift window
					case 28:
						shift_window(4);
						redraw = true;
						break;
					case 29:
						shift_window(3);
						redraw = true;
						break;
					case 30:
						shift_window(1);
						redraw = true;
						break;
					case 31:
						shift_window(2);
						redraw = true;
						break;
					//toggle stack
					case 32:
						if (cs->v->fs == false) {
							if (cs->v->showstack == true) {
								cs->v->showstack = false;
							} else {
								cs->v->showstack = true;
							};
							redraw = true;
						};
						break;
					//move to stack
					case 33:
						if (cs->v->mfocus == NULL) {break;};
						if (cs->v->fs == true) {break;};
						XUnmapWindow(dpy,cs->v->mfocus->win->id);
						move_to_stack(cs->v->mfocus);
						redraw = true;
						break;
					//move to main
					case 34:
						if (cs->v->fs == true) {break;};
						move_to_main();
						if (cs->v->mfocus != NULL && cs->v->mfocus->win != NULL) {
							XMapWindow(dpy,cs->v->mfocus->win->id);
						};
						redraw = true;
						break;
					//swap stack and main	
					case 35:
						if (cs->v->fs == true) {break;};
						if (cs->v->sfocus != NULL && cs->v->mfocus != NULL) {
							struct win *m = cs->v->mfocus->win;
							struct win *s = cs->v->sfocus->win;
							cs->v->sfocus->win->cont = cs->v->mfocus->win->cont;
							cs->v->mfocus->win->cont = NULL;
							cs->v->mfocus->win = s;
							cs->v->sfocus->win = m;
							XMapWindow(dpy,cs->v->mfocus->win->id);
							XUnmapWindow(dpy,cs->v->sfocus->win->id);
							XSync(dpy,False);
							redraw = true;
						};
						break;
					//swap stack up/down
					case 36:
						if (cs->v->sfocus != NULL && cs->v->sfocus->prev != NULL) {
							struct stack_item *tmpa, *tmpb, *tmpc, *tmpd;
							tmpa = cs->v->sfocus->prev->prev;
							tmpb = cs->v->sfocus->prev;
							tmpc = cs->v->sfocus;
							tmpd =cs->v->sfocus->next;
							if (tmpa != NULL) {
								tmpa->next = tmpc;
							} else {
								cs->v->stack = tmpc;
							};
							if (tmpd != NULL) {
								tmpd->prev = tmpb;
							};
							tmpc->prev = tmpa;
							tmpc->next = tmpb;
							tmpb->prev = tmpc;
							tmpb->next = tmpd;
						};
						redraw = true;
						break;
					case 37:
						if (cs->v->sfocus != NULL && cs->v->sfocus->next != NULL) {
							struct stack_item *tmpa, *tmpb, *tmpc, *tmpd;
							tmpa = cs->v->sfocus->prev;
							tmpb = cs->v->sfocus;
							tmpc = cs->v->sfocus->next;
							tmpd =cs->v->sfocus->next->next;
							if (tmpa != NULL) {
								tmpa->next = tmpc;
							} else {
								cs->v->stack = tmpc;
							};
							if (tmpd != NULL) {
								tmpd->prev = tmpb;
							};
							tmpc->prev = tmpa;
							tmpc->next = tmpb;
							tmpb->prev = tmpc;
							tmpb->next = tmpd;
						};
						redraw = true;
						break;
					//shift main focus udrl
					case 38:
						shift_main_focus(4);
						redraw = true;
						break;
					case 39:
						shift_main_focus(3);
						redraw = true;
						break;
					case 40:
						shift_main_focus(1);
						redraw = true;
						break;
					case 41:
						shift_main_focus(2);
						redraw = true;
						break;
					//shift stack focus up down
					case 42:
						shift_stack_focus(true);
						redraw = true;
						break;
					case 43:
						shift_stack_focus(false);
						redraw = true;
						break;
					//close win soft or hard
					case 44:
						if (cs->v->mfocus == NULL || cs->v->mfocus->win == NULL) {
							break;
						};
						if (cs->v->mfocus->win->del_win == true) {
							XClientMessageEvent	cm;
							memset(&cm,'\0', sizeof cm);
							cm.type = ClientMessage;
							cm.window = cs->v->mfocus->win->id;
							cm.message_type = wm_prot;
							cm.format = 32;
							cm.data.l[0] = wm_del_win;
							cm.data.l[1] = CurrentTime;
							XSendEvent(dpy, cs->v->mfocus->win->id, False, 0L, (XEvent *)&cm);
						} else {
							XDestroyWindow(dpy,cs->v->mfocus->win->id);
						};
						break;
					case 45:
						if (cs->v->mfocus != NULL && cs->v->mfocus->win != NULL) {
							XKillClient(dpy,cs->v->mfocus->win->id);
						};
						break;
					//run menu/xterm
					case 46:
						spawn(dcmd);
						break;
					case 47:
						//spawn xterm
						spawn(tcmd);
						break;
					//fullscreen:
					case 48:
						if (cs->v->fs == true) {
							cs->v->fs = false;

						} else { 
							cs->v->fs = true;
						};
						redraw = true;
						break;
					//quit
					case 49:
						return(0);
						break;
					case 50:
						if (cs->v->orientv == true) {
							cs->v->orientv = false;
						} else {
							cs->v->orientv = true;
						};
						redraw = true;
						break;

					case 51:
						//reload configs:
						if (true) {
							free(tcmd);
							tcmd = NULL;
							free(dcmd);
							dcmd = NULL;
							for (int i = 0; i < ARRAY_LEN(ccmds); i++) {
								free(ccmds[i]);
								ccmds[i] = NULL;
							};

							//Unbind keys
							int i = 0;
							while (i < BINDINGS ) {
								if (bindings[i].mask != NULL) {
									//Also ungrab with the LockMask set
									XUngrabKey(dpy,bindings[i].keycode,*(bindings[i].mask),root);
									XUngrabKey(dpy,bindings[i].keycode,*(bindings[i].mask) ^ LockMask,root);
									XUngrabKey(dpy,bindings[i].keycode,*(bindings[i].mask) ^ LockMask ^ Mod2Mask,root);
									XUngrabKey(dpy,bindings[i].keycode,*(bindings[i].mask) ^ Mod2Mask,root);

								};
								i++;
							};
							//call bind keys
							load_defaults();
							//call load_config
							//don't rerun euclidrc
							//can we check whether there are more or fewer screens?
							load_conf(false);
							commit_bindings();
							XFreeGC(dpy,focus_gc);
							XFreeGC(dpy,unfocus_gc);
							XGCValues xgcv;
							xgcv.foreground = stack_focus_pix;
							focus_gc = XCreateGC(dpy,cs->stackid,GCForeground,&xgcv);
							xgcv.foreground = stack_unfocus_pix;
							unfocus_gc = XCreateGC(dpy,cs->stackid,GCForeground,&xgcv);
						};
						break;
					case 52:
						//move to previous screen
						if (cs != firstscreen) {
							struct screen *s = firstscreen;
							while (s->next != cs) {
								s = s->next;
							};
							cs = s;
							redraw = true;
						};
						break;
					case 53:
						//move to next screen
						if (cs->next != NULL) {
							cs = cs->next;
							redraw = true;
						};
						break;
					case 54:
						search_wins();
						redraw = true;
						break;
					case 55:
						goto_view(find_view(last_view_idx));
						redraw = true;
						break;

					default:
						{
							const int ccmd_index = i - BCMDS;
							if (ccmd_index >= 0 && ccmd_index < ARRAY_LEN(ccmds)) {
								spawn(ccmds[ccmd_index]);
							} else {
								fprintf(stderr,"euclid-wm ERROR: unknown key id: %d",i);
							};
							break;
						}
				};
	
			}
			break;

			case KeyRelease:
			break;
			
			case ButtonPress:
			break;
			
			case ButtonRelease:
			break;

			case MotionNotify:
				if (cs->v->mfocus == NULL || cs->v->mfocus->win->id != ev.xmotion.window) {
					struct cont *f = id_to_cont(ev.xmotion.window);
					if (f != NULL) {
							struct screen *s = firstscreen;
							while (s != NULL && s->v != f->track->view) {
								s = s->next;
							};
							cs = s;
							cs->v->mfocus = f;

						redraw = true;
					};
				}; 
			break;
			
			case EnterNotify:
				if (!(ev.xcrossing.focus == false && sloppy_focus == true)) {break;};
				struct timeval ctime;
				gettimeofday(&ctime,0);
				signed long usec = ctime.tv_usec - last_redraw.tv_usec;
				signed long sec = ctime.tv_sec - last_redraw.tv_sec;
				if ( sec > 1 || (sec == 0 && usec >= 10000) || (sec == 1 && usec >= -990000)) { 
					if (cs->v->mfocus != NULL && cs->v->mfocus->win->id != ev.xcrossing.window) {
						struct cont *f = id_to_cont(ev.xmotion.window);
						if (f != NULL) {
							struct screen *s = firstscreen;
							while (s != NULL && s->v != f->track->view) {
								s = s->next;
							};
							if (s != NULL) {
								cs = s;
								cs->v->mfocus = f;
							};
							redraw = true;
						};
					};
				};
			break;
			
			case LeaveNotify:
			break;

			case FocusIn:
			break;

			case FocusOut:
			break;
			
			case KeymapNotify:
			break;
		
			case Expose:
			break;
		
			case GraphicsExpose:
			break;

			case NoExpose:
			break;
	
			case VisibilityNotify:
			break;

			case CreateNotify:
			break;

			case DestroyNotify:
				forget_win(ev.xdestroywindow.window);
			break;

			case UnmapNotify:
			{	struct cont *c;
				c = id_to_cont(ev.xunmap.window);
				if (c != NULL ) {
					
					if (c->win->req_fullscreen == true && c->track->view->mfocus == c) {
						cs->v->fs = false;
						c->win->req_fullscreen = false;
					};
					//hold everything: What if the unmapnotify is a result of us moving the window to a new view?
					//why was this not a problem before changing the id_to_cont function?
					//so check that the view is displayed before removeing the cont:
					struct screen *s = firstscreen;
					while (s !=NULL && s->v != c->track->view) {
						s = s->next;
					};
					if (s) { //win was on screen
						remove_cont(c);
					 
						redraw = true;
					};
				};
			//} else if (ev.type == CreateNotify && is_top_level(ev.xcreatewindow.window) ==true) {
			}
			break;

			case MapNotify:
				if (!is_top_level(ev.xmap.window)){
					break;
				} else {

				//check for its win struct:
				struct win *w = first_win;
				while (w!=NULL && w->id != ev.xmap.window) {
					w = w->next;
				};

				if (w == NULL) { //window was unknown, add it
					w = add_win(ev.xmap.window);
					w->prev_focus = cs->v->mfocus;
					//need to get the win struct to pass to 
					//add_client_to_view;
				} else { //window is known
					//reset pointer to previously focused container on window move
					w->prev_focus = NULL;
					//remove from where it previously was, unless where it previously was is already on a screen (Which shouldn't happen, since then it would already have been mapped)
					if (w->cont) { //it is in a layout somewhere
						struct cont *c = w->cont;
						//struct win *w = c->win;
						struct screen *s = firstscreen;
						while (s!=NULL && c->track->view != s->v) {	
							s = s->next;
						};
					
						//remove the cont from the prior view
						if (!s) { //remember: we map windows immediately after switching views internally, we don't want to remove the windows as we map them!
							remove_cont(c);
						//	add_client_to_view(w,cs->v);	
						//	redraw = true;
						} else { //client is already onscreen
							break;
						};

					} else {
						struct stack_item *s = cs->v->stack;
						while (s != NULL) {
							if (s->win->id == ev.xmap.window) {break;};
							s = s->next;
						};
						if (s != NULL && s->win->id == ev.xmap.window) { // remove it from the stack
							 if (s == cs->v->stack) {
			                                        cs->v->stack = s->next;
                        			        };
			                                if (s == cs->v->sfocus) {
                        			                if (s->next != NULL) {
                                               				 cs->v->sfocus = s->next;
                                       				 } else {
                                              				  cs->v->sfocus = s->prev;
                                       				 };
                               				 };
                               				 if (s->next != NULL) {
                                       				 s->next->prev = s->prev;
                              				  };
			                                if (s->prev != NULL) {
                        		                	s->prev->next = s->next;
                               				 };
                               				 if (s == cs->v->stack) {
                                      				  cs->v->stack = s->next;
                               				 };
                              				 free(s);

						}; //finished with stack check
						//finally add to layout
					};

				};
				add_client_to_view(w,cs->v);
						//but we aren't done yet. Flash maps the window already configured to be fullscreen, we ignore the first configure, because the window isn't mapped and isn't in our data structure.
						//so we need to check NOW to see if it is trying to force fullscreen

                               	if (cs->v->fs != true) { 
					XWindowAttributes wa;
					XGetWindowAttributes(dpy,ev.xmap.window,&wa);
					if (gxerror == true) {gxerror = false;};
					if (wa.height == cs->h && wa.width == cs->w) {
						cs->v->fs = true;
						w->fullscreen = true;
						w->req_fullscreen = true;
					};
				};

				redraw = true;


			};
					//first see if it is in a layout, second, go therough the stacks to find it
					
//		};
	/*
				//check whether it's in the layout, if not add it
				if (id_to_cont(ev.xmap.window) == NULL) {
					//see whether we know about the window
					if (is_top_level(ev.xmap.window)) { //calling this twice in a row seems unncessecary but it might just be me. 
						//In fact this whole routine seems to run in circles
						struct win *w;
						w = first_win;
						while (w != NULL && w->id != ev.xmap.window) {
							w = w->next;
						};
						if (w == NULL) { //we don't have a record of it
							w = add_win(ev.xmap.window);
						};
						//check to see whether it was hiding in the stack
						struct stack_item *s = cs->v->stack;
						while (s != NULL) {
							if (s->win->id == ev.xmap.window) {break;};
							s = s->next;
						};
						if (s != NULL && s->win->id == ev.xmap.window) { // remove it from the stack
							 if (s == cs->v->stack) {
			                                        cs->v->stack = s->next;
                        			        };
			                                if (s == cs->v->sfocus) {
                        			                if (s->next != NULL) {
                                               				 cs->v->sfocus = s->next;
                                       				 } else {
                                              				  cs->v->sfocus = s->prev;
                                       				 };
                               				 };
                               				 if (s->next != NULL) {
                                       				 s->next->prev = s->prev;
                              				  };
			                                if (s->prev != NULL) {
                        		                	s->prev->next = s->next;
                               				 };
                               				 if (s == cs->v->stack) {
                                      				  cs->v->stack = s->next;
                               				 };
                              				 free(s);

						}; //finished with stack check
						//finally add to layout
						add_client_to_view(w,cs->v);
						//but we aren't done yet. Flash maps the window already configured to be fullscreen, we ignore the first configure, because the window isn't mapped and isn't in our data structure.
						//so we need to check NOW to see if it is trying to force fullscreen

                               			if (cs->v->fs != true) { 
							XWindowAttributes wa;
							XGetWindowAttributes(dpy,ev.xmap.window,&wa);
							if (gxerror == true) {gxerror = false;};
							if (wa.height == cs->h && wa.width == cs->w) {
								cs->v->fs = true;
								w->fullscreen = true;
								w->req_fullscreen = true;
							};
						};

						redraw = true;

					};
				} else { //we know this window, and it is in a view somewhere
					struct cont *c = id_to_cont(ev.xmap.window);
					struct win *w = c->win;
					struct screen *s = firstscreen;
					while (s!=NULL && c->track->view != s->v) {	
						s = s->next;
					};
					
					//remove the cont from the prior view
					if (!s) { //remember: we map windows immediately after switching views internally, we don't want to remove the windows as we map them!
						remove_cont(c);
						add_client_to_view(w,cs->v);	
						//do we need to set focus? shouldn't
						redraw = true;
					};
				};
	*/
			break;
	
		case MapRequest:
		break;

		case ReparentNotify:
				if (ev.xreparent.parent == root) {
					if (is_top_level(ev.xreparent.window) == true) {
						XWindowAttributes att;
						XGetWindowAttributes (dpy,ev.xreparent.window,&att);
						if (att.map_state != IsUnmapped) {
							struct win *t;
							t = add_win(ev.xreparent.window);
							add_client_to_view(t,cs->v);
							redraw = true;
						};
					};
				} else {
					forget_win(ev.xreparent.window);
				};
		
		break;

		case ConfigureNotify:
			{
				//if a window tries to manage itself we are going to play rough, unless it is putting itself in or out of fullscreen
				struct cont *wc = id_to_cont(ev.xconfigure.window);
				if (wc) {
					struct screen *tmps = firstscreen;
					//we will see if adding this line, is good or not: the problem is that in fs, if a window tries to bring itself to the front euclid won't let it
					//this behavior might be good, as it could keep things from stealing focus, try and see. 
					if (wc->track->view->fs == true) {
						wc->track->view->mfocus = wc;
					};

					while (tmps->next != NULL && tmps->v != wc->track->view) {
						tmps = tmps->next;
					};
					if (wc->track->view == tmps->v) {
						if (ev.xconfigure.width >= tmps->w  && ev.xconfigure.height >= tmps->h) {
							if (tmps->v->fs == false) { //if we get this request when we are already in fullscreen just ignore it, do NOT fall through to one of the last two elses
								tmps->v->fs = true;
								tmps->v->mfocus = wc;
								tmps->v->mfocus->win->req_fullscreen = true;
								redraw = true;
							};
						} else if (tmps->v->fs == true && wc == tmps->v->mfocus && (ev.xconfigure.height < tmps->h || ev.xconfigure.width < tmps->w)) {
							if (wc->win->req_fullscreen == true) {
								tmps->v->fs = false;
							};
							wc->win->req_fullscreen = false;
							redraw = true; 
						
						} else if (wc->track->view->orientv == true) {
							//we are going to be niave and just check the w and h
							//w =track size
							if (wc->track->size != ev.xconfigure.width || wc->size != ev.xconfigure.height) {
								redraw = true;
							} else {
								//calculate the x
								//tp is a pointer to the track
								struct track *tp = wc->track->view->ft;
								int tmpx = 0;
								while (tp != NULL && tp != wc->track) {
									tmpx += tp->size;
									tp = tp->next;
								};
								if (ev.xconfigure.x != tmpx) {
									redraw = true;
								} else {
									//check y, tc is a temp container pointer
									struct cont *tc = wc->track->c;
									int tmpy = 0;
									while (tc != NULL && tc != wc) {
										tmpy += tc->size;
										tc = tc->next;
									};
									if (tmpy != ev.xconfigure.y) {
										redraw = true;
									};
								};

							};
						} else {
							if (wc->track->size != ev.xconfigure.height || wc->size != ev.xconfigure.width) {
								redraw = true;
							} else {
								//calculate the y
								//tp is a pointer to the track
								struct track *tp = wc->track->view->ft;
								int tmpy = 0;
								while (tp != NULL && tp != wc->track) {
									tmpy += tp->size;
									tp = tp->next;
								};
								if (ev.xconfigure.y != tmpy) {
									redraw = true;
								} else {
									//check x, tc is a temp container pointer
									struct cont *tc = wc->track->c;
									int tmpx = 0;
									while (tc != NULL && tc != wc) {
										tmpx += tc->size;
										tc = tc->next;
									};
									if (tmpx != ev.xconfigure.x) {
										redraw = true;
									};
								};

							};
	
						};
					};
				};
			}
			
			
			break;

			case ConfigureRequest:
			break;

			case GravityNotify:
			break;

			case ResizeRequest:
			break;

			case CirculateNotify:
			break;
	
			case CirculateRequest:
			break;

			case PropertyNotify:
			break;
			
			case SelectionClear:
			break;

			case SelectionRequest:
			break;

			case SelectionNotify:
			break;

			case ColormapNotify:
			break;

			case ClientMessage:
				if (ev.xclient.message_type == wm_change_state) {
					if (ev.xclient.data.l[1] == wm_fullscreen) {
						struct cont *wc = id_to_cont(ev.xclient.window);
						if (wc != NULL) {
							if (ev.xclient.data.l[0] == 1) { //go into full screen
								wc->track->view->fs = true;
								redraw = true;
								wc->win->fullscreen = true;
								wc->win->req_fullscreen = true;
								XChangeProperty(dpy,ev.xclient.window,wm_change_state,XA_ATOM,32,PropModeReplace,(unsigned char *)&wm_fullscreen,1);
							} else { // exit fullscreen
								if (wc->win->req_fullscreen == true) {
									wc->track->view->fs = false;
								};
								redraw = true;
								wc->win->fullscreen = false;
								wc->win->req_fullscreen = false;
								XChangeProperty(dpy,ev.xclient.window,wm_change_state,XA_ATOM,32,PropModeReplace,(unsigned char *)0,0);
							};
						};
					};
				};
			break;
			//case CreateNotify:
			//	if (!is_top_level(ev.xcreatewindow.window)) {break;};
			//	add_win(ev.xcreatewindow.window);
			//break;
			
			case MappingNotify:
			break;

			case GenericEvent:
			break;

			default:
			break;

			};	
		} while (XPending(dpy)); 
	
		if (redraw == true) {
			layout();
			gettimeofday(&last_redraw,0);
		};
	}; //end infinite loop
}

int main() {
	printf("\neuclid-wm: running\n");
	//this is to avoid leaving zombies
	//sighandler_t is a gnu extention
	signal (SIGCHLD, SIG_IGN);

	dpy = XOpenDisplay(0);
	XSetErrorHandler(xerror);
	//	cs->h = DisplayHeight(dpy,DefaultScreen(dpy));
//	cs->w = DisplayWidth(dpy,DefaultScreen(dpy));
//	printf("euclid-wm: sreen dimensions: %d %d\n",cs->h, cs->w);
	//set some stuff
	if ((root = DefaultRootWindow(dpy))) {
		printf("euclid-wm: root is %6.0lx\n",root);
	} else {
		fprintf(stderr,"euclid-wm ERROR: faild to find root window\n");
		return(0);
	};
	//set colors, these get overridden by the config file
	XColor color;
	//these values are shorts, 0 - 65535
	//focus color:
	color.red = 0;
	color.green = 0;
	color.blue = 65500;
	color.flags = DoRed | DoGreen | DoBlue;
	XAllocColor(dpy,DefaultColormap(dpy,DefaultScreen(dpy)),&color);
	focus_pix = color.pixel;
	//unfocus color
	color.red = 5000;
	color.green = 5000;
	color.blue = 5000;
	color.flags = DoRed | DoGreen | DoBlue;
	XAllocColor(dpy,DefaultColormap(dpy,DefaultScreen(dpy)),&color);
	unfocus_pix = color.pixel;
	//stack background:
	color.red = 100;
	color.green = 100;
	color.blue = 200;
	color.flags = DoRed | DoGreen | DoBlue;
	XAllocColor(dpy,DefaultColormap(dpy,DefaultScreen(dpy)),&color);
	stack_background_pix = color.pixel;
	//stack unfocus text:
	color.red = 60000;
	color.green = 60000;
	color.blue = 60000;
	color.flags = DoRed | DoGreen | DoBlue;
	XAllocColor(dpy,DefaultColormap(dpy,DefaultScreen(dpy)),&color);
	stack_unfocus_pix = color.pixel;
	//stack focus text:
	color.red = 0;
	color.green = 0;
	color.blue = 65500;
	color.flags = DoRed | DoGreen | DoBlue;
	XAllocColor(dpy,DefaultColormap(dpy,DefaultScreen(dpy)),&color);
	stack_focus_pix = color.pixel;
	
	//we have to do this after we get root
	memset(bindings, '\0', sizeof(bindings));
	load_conf(true);
	commit_bindings();

	
	set_atoms();
	
#ifndef NOXINERAMA 

	int screens;
	XineramaScreenInfo *scrn_info = NULL; 
	scrn_info = XineramaQueryScreens(dpy,&screens);

	printf("screens %d\n",screens);
	unsigned short sn = 0;
	if (screens == 0) {
		printf("Xinerama diabled\n");
		addscreen( DisplayHeight(dpy,DefaultScreen(dpy)),DisplayWidth(dpy,DefaultScreen(dpy)),0,0,0);

	} else {
		printf("Xinerama enabled: %d screens\n",screens);
		while (sn < screens) {
			addscreen(scrn_info[sn].height,scrn_info[sn].width,scrn_info[sn].x_org,scrn_info[sn].y_org,sn);
			sn ++;
		};
	};

	XFree(scrn_info);
#else
	/* Old testing definitions
	XineramaScreenInfo scrn_info[3];
	scrn_info[0].height = 400;
	scrn_info[0].width = 450;
	scrn_info[0].x_org = 5;
	scrn_info[0].y_org = 5;
	scrn_info[1].height = 300;
	scrn_info[1].width = 250;
	scrn_info[1].x_org = 200;
	scrn_info[1].y_org = 410;
	scrn_info[2].height = 500;
	scrn_info[2].width = 400;
	scrn_info[2].x_org = 650;
	scrn_info[2].y_org = 200;
	screens = 3;
	*/
	printf("Compiled without Xinerama support.\n");
	addscreen(DisplayHeight(dpy,DefaultScreen(dpy)),DisplayWidth(dpy,DefaultScreen(dpy)),0,0,0);

#endif
	
	offscreen = DisplayHeight(dpy,DefaultScreen(dpy));

//	cs->v = make_view();
//	fv = cs->v;
//	cs->v->idx = 1;

	//now we also need to get all already exisiting windows
	Window d1, d2, *wins = NULL;
	unsigned int no;
	int i;
	XQueryTree(dpy,root,&d1,&d2,&wins,&no);
	struct win *t;
	printf("euclid-wm: %d windows\n",no);
	for (i = 0 ; i<no; i++) {
		if (is_top_level(wins[i])) {
			t = add_win(wins[i]);
			XWindowAttributes att;
			XGetWindowAttributes(dpy,wins[i],&att);
			if (att.map_state != IsUnmapped) {
				add_client_to_view(t,cs->v);
			};
			//to see how to check for iconified windows, look at scrotwm getstate(
		};
	};
	
	//and set an event on root to get new ones
	if  (XSelectInput(dpy,root,SubstructureNotifyMask )) {
		printf("euclid-wm: now controlling root\n");
	} else {
		fprintf(stderr,"euclid-wm ERROR: fail to control root, is there already a WM?");
		return(1);
	};
	XSync(dpy,False);
	
	
	XGCValues xgcv;
	xgcv.foreground = stack_focus_pix;
	focus_gc = XCreateGC(dpy,cs->stackid,GCForeground,&xgcv);
	xgcv.foreground = stack_unfocus_pix;
	unfocus_gc = XCreateGC(dpy,cs->stackid,GCForeground,&xgcv);

	layout();
	
	set_desktop_name(1);

	return (event_loop());
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/euclid-wm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/euclid-wm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master