summaryrefslogtreecommitdiff
path: root/schewm.c
blob: ab6993c25fcbf8981e43d71ae9a8bb3e7423a0ab (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
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
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <xcb/randr.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_keysyms.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>

// Workspace values which are special for us
const uint32_t ALL_WORKSPACES = 0xffffffff;
const uint32_t NULL_WORKSPACE = 0xfffffffe;

// Glyphs (mouse cursor types)
const uint16_t MOVE_GLYPH = 52;
const uint16_t RESIZE_GLYPH = 120;

// Key modifiers
const uint16_t MOD_ALT = XCB_MOD_MASK_1;
const uint16_t MOD_META = XCB_MOD_MASK_4;
const uint16_t MOD_SHIFT = XCB_MOD_MASK_SHIFT;

uint32_t
keysym_from_str(const char *s) {
    return (uint32_t) XStringToKeysym(s);
}

// General utility functions
uint32_t
parse_color(const char *s) {
    unsigned int r, g, b;
    if (s == NULL
        || strlen(s) != 7
        || sscanf(s, "#%2x%2x%2x", &r, &g, &b) != 3) {
        // Parse error
        return 0;
    }
    return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
}

uint64_t
make_key_chord(uint16_t mod, xcb_keysym_t keysym) {
    uint64_t chord = mod;
    chord = (chord << 32) | keysym;
    return chord;
}

uint32_t
make_button_chord(uint16_t mod, xcb_button_t button) {
    uint32_t chord = mod;
    chord = (chord << 16) | button;
    return chord;
}

void
parse_key_chord(uint64_t chord, uint16_t *mod, xcb_keysym_t *keysym) {
    *mod = chord >> 32;
    *keysym = chord & 0xffff;
}

void
parse_button_chord(uint32_t chord, uint16_t *mod, xcb_button_t *button) {
    *mod = chord >> 16;
    *button = chord & 0xf;
}

struct Point {
    int16_t x, y;
};

struct Rect {
    int16_t x, y;
    uint16_t width, height;
};

int16_t
rect_center_x(const struct Rect *rect) {
    return rect->x + rect->width / 2;
}

int16_t
rect_center_y(const struct Rect *rect) {
    return rect->y + rect->height / 2;
}

void
rect_center(const struct Rect *rect, struct Point *p) {
    p->x = rect_center_x(rect);
    p->y = rect_center_y(rect);
}

int16_t
max(int16_t a, int16_t b) {
    return a > b ? a : b;
}

int16_t
min(int16_t a, int16_t b) {
    return a < b ? a : b;
}

uint32_t
rect_intersect_area(const struct Rect *r1, const struct Rect *r2) {
    int32_t x1 = max(r1->x, r2->x);
    int32_t y1 = max(r1->y, r2->y);
    int32_t x2 = min(r1->x + r1->width, r2->x + r2->width);
    int32_t y2 = min(r1->y + r1->height, r2->y + r2->height);
    return (x1 > x2 || y1 > y2) ? 0 : (x2 - x1) * (y2 - y1);
}

enum WindowState {
    WS_NORMAL = 0,
    WS_ICONIFIED,
    WS_MAXIMIZED,
    WS_FULLSCREEN,
};

struct Client {
    xcb_window_t id;
    int16_t x, y;
    uint16_t width, height;
    uint16_t min_width, min_height, max_width, max_height;
    uint16_t width_inc, height_inc, base_width, base_height;
    uint32_t workspace;
    bool user_coord;
    enum WindowState state, prev_state;
    bool is_unkillable;
    bool should_ignore_borders;
    bool has_old_size;
    int16_t old_x, old_y;
    uint16_t old_width, old_height;
    struct Monitor *monitor;
    struct Client *prev, *next;
};

struct Client *
client_new(xcb_window_t id) {
    struct Client *client = calloc(1, sizeof(struct Client));
    if (client == NULL) {
        return NULL;
    }

    client->id = id;
    client->width_inc = 1;
    client->height_inc = 1;
    client->workspace = NULL_WORKSPACE;
    /*
     * Supposed to be 0 anyway:
     *
     * client->state = WS_NORMAL;
     * client->prev_state = WS_NORMAL;
     */
    return client;
}

/*
 * Adds a new client to the ring, adjusting the necessary next/prev
 * pointers.
 */
struct Client *
client_ring_add(struct Client *ring, struct Client *client) {
    if (ring == NULL || ring->next == NULL || ring->prev == NULL) {
        ring = client;
        client->next = client;
    }

    client->prev = ring;
    client->next = ring->next;
    ring->next->prev = client;
    ring->next = client;
    return client;
}

/*
 * Removes a client from the ring. We do not manage memory here, so
 * the caller must free the memory of the client struct if that's
 * needed.
 */
struct Client *
client_ring_erase(struct Client *ring, struct Client *client) {
    if (ring == NULL) {
        return NULL;
    }
    if (ring == client && client->next == client) {
        // client_free(ring);
        return NULL;
    }

    client->next->prev = client->prev;
    client->prev->next = client->next;
    if (ring == client) {
        ring = client->next;
        // client_free(client);
    }
    return ring;
}

void
client_store_size(struct Client *client) {
    client->old_x = client->x;
    client->old_y = client->y;
    client->old_width = client->width;
    client->old_height = client->height;
    client->has_old_size = true;
}

void
client_restore_size(struct Client *client) {
    if (!client->has_old_size) {
        return;
    }

    client->x = client->old_x;
    client->y = client->old_y;
    client->width = client->old_width;
    client->height = client->old_height;
    client->has_old_size = false;
}

struct ClientList {
    struct Client *client;
    struct ClientList *next;
};

/*
 * Prepends a new client to list of clients.
 */
struct ClientList *
client_list_add(struct ClientList *list, struct Client *client) {
    struct ClientList *node = calloc(1, sizeof(struct ClientList));
    if (node == NULL) {
        // Cannot alloc node, just abort. If list was NULL, stays so.
        return list;
    }
    node->client = client;
    // Also works when list is NULL
    node->next = list;
    list = node;
    return list;
}

/*
 * `deep' indicates whether we should also free the clients referenced
 * by the list elements.
 */
void
client_list_free(struct ClientList *list, bool deep) {
    struct ClientList *next;
    while (list != NULL) {
        next = list->next;
        if (deep) {
            free(list->client);
        }
        free(list);
        list = next;
    }
}

/*
 * The following hash functions come from [1].
 *
 * Apparently supposed to be overall decent, no independent tests were
 * made here. We have 32-bit and 64-bit versions, as we need hash
 * tables for both data types.
 *
 * [1] https://stackoverflow.com/a/12996028
 */

uint32_t
u32_hash(uint32_t x) {
    x = ((x >> 16) ^ x) * 0x45d9f3b;
    x = ((x >> 16) ^ x) * 0x45d9f3b;
    x = (x >> 16) ^ x;
    return x;
}

uint32_t
u32_unhash(uint32_t x) {
    x = ((x >> 16) ^ x) * 0x119de1f3;
    x = ((x >> 16) ^ x) * 0x119de1f3;
    x = (x >> 16) ^ x;
    return x;
}

// We assume this works as well as for 32 bits, but who knows?
uint64_t
u64_hash(uint64_t x) {
    x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
    x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
    x = x ^ (x >> 31);
    return x;
}

uint64_t
u64_unhash(uint64_t x) {
    x = (x ^ (x >> 31) ^ (x >> 62)) * UINT64_C(0x319642b2d24d8ec3);
    x = (x ^ (x >> 27) ^ (x >> 54)) * UINT64_C(0x96de1b173f119089);
    x = x ^ (x >> 30) ^ (x >> 60);
    return x;
}

struct ClientsMap {
    struct ClientList **buckets;
    size_t num_buckets;
    size_t size;
};

struct ClientsMap *
clients_map_new(size_t num_buckets) {
    struct ClientsMap *map = calloc(1, sizeof(struct ClientsMap));
    if (!map) {
        return NULL;
    }

    map->buckets = calloc(num_buckets, sizeof(struct ClientList *));
    if (!map->buckets) {
        free(map);
        return NULL;
    }

    map->num_buckets = num_buckets;
    map->size = 0;
    return map;
}

void
clients_map_add(struct ClientsMap *map, struct Client *client) {
    uint32_t hash = u32_hash(client->id);
    uint32_t i = hash % map->num_buckets;
    // collision if (map->buckets[i] != NULL)
    map->buckets[i] = client_list_add(map->buckets[i], client);
    map->size++;
}

struct Client *
clients_map_erase(struct ClientsMap *map, struct Client *client) {
    uint32_t hash = u32_hash(client->id);
    uint32_t i = hash % map->num_buckets;
    struct ClientList *prev = NULL;
    for (struct ClientList *list = map->buckets[i];
         list != NULL;
         list = list->next) {
        if (list->client == client) {
            if (prev == NULL) {
                // It matched the first, update the starting node of
                // the list
                map->buckets[i] = list->next;
            }
            if (prev != NULL) {
                // We are not at the first, make prev skip the node we
                // just found
                prev->next = list->next;
            }
            free(list);
            map->size--;
            return client;
        }
        prev = list;
    }
    // Not found
    return NULL;
}

struct Client *
clients_map_find(const struct ClientsMap *map, xcb_window_t id) {
    uint32_t hash = u32_hash(id);
    uint32_t i = hash % map->num_buckets;
    for (struct ClientList *list = map->buckets[i];
         list != NULL;
         list = list->next) {
        if (list->client->id == id) {
            return list->client;
        }
    }
    // Not found
    return NULL;
}

/*
 * This function frees the memory of clients as well.
 */
void
clients_map_free(struct ClientsMap *map) {
    for (size_t i = 0; i < map->num_buckets; i++) {
        client_list_free(map->buckets[i], true);
    }
    free(map->buckets);
    free(map);
}

struct Monitor {
    xcb_randr_output_t id;
    const char *name;
    int16_t x, y;
    uint16_t width, height;
    struct Monitor *prev, *next;
};

/*
 * Add a new monitor to the ring, keeping it circular and adjusting
 * the necessary next/prev pointers.
 */
struct Monitor *
monitor_ring_add(struct Monitor *ring, struct Monitor *monitor) {
    if (ring == NULL || ring->next == NULL || ring->prev == NULL) {
        ring = monitor;
        monitor->next = monitor;
    }

    monitor->prev = ring;
    monitor->next = ring->next;
    ring->next->prev = monitor;
    ring->next = monitor;
    return monitor;
}

/*
 * Same as for clients, we also don't manage memory here, just the
 * structure of the ring. The caller should free the monitor struct if
 * needed.
 */
struct Monitor *
monitor_ring_erase(struct Monitor *ring, struct Monitor *monitor) {
    if (ring == NULL) {
        return NULL;
    }
    if (ring == monitor && monitor->next == monitor) {
        // free(ring);
        return NULL;
    }

    monitor->next->prev = monitor->prev;
    monitor->prev->next = monitor->next;
    if (ring == monitor) {
        ring = monitor->next;
        // free(monitor);
    }
    return ring;
}

void
monitor_rect(const struct Monitor *monitor, struct Rect *rect) {
    rect->x = monitor->x;
    rect->y = monitor->y;
    rect->width = monitor->width;
    rect->height = monitor->height;
}

struct Workspace {
    struct Client *ring;
};

typedef void (*Callback)();

struct CallbackList {
    uint64_t key;
    Callback callback;
    struct CallbackList *next;
};

/*
 * Prepends a new callback to list of callbacks.
 */
struct CallbackList *
callback_list_add(struct CallbackList *list, uint64_t key, Callback callback) {
    struct CallbackList *node = calloc(1, sizeof(struct CallbackList));
    if (node == NULL) {
        // Cannot alloc node, just abort. If list was NULL, stays so.
        return list;
    }
    node->key = key;
    node->callback = callback;
    // Also works when list is NULL
    node->next = list;
    list = node;
    return list;
}

void
callback_list_free(struct CallbackList *list) {
    struct CallbackList *next;
    while (list != NULL) {
        next = list->next;
        free(list);
        list = next;
    }
}

struct CallbacksMap {
    struct CallbackList **buckets;
    size_t num_buckets;
    size_t size;
};

struct CallbacksMap *
callbacks_map_new(size_t num_buckets) {
    struct CallbacksMap *map = calloc(1, sizeof(struct CallbacksMap));
    if (!map) {
        return NULL;
    }

    map->buckets = calloc(num_buckets, sizeof(struct CallbackList *));
    if (!map->buckets) {
        free(map);
        return NULL;
    }

    map->num_buckets = num_buckets;
    map->size = 0;
    return map;
}

/*
 * For internal use only. Call callbacks_map_get() for regular usage.
 *
 * Returns a pointer to the callback so we can modify stuff and reuse
 * the search in multiple other functions.
 */
Callback *
callbacks_map_find(const struct CallbacksMap *map, uint64_t key) {
    uint64_t hash = u64_hash(key);
    uint64_t i = hash % map->num_buckets;
    for (struct CallbackList *list = map->buckets[i];
         list != NULL;
         list = list->next) {
        if (list->key == key) {
            return &list->callback;
        }
    }
    // Not found
    return NULL;
}

Callback
callbacks_map_get(struct CallbacksMap *map, uint64_t key) {
    Callback *callback_ptr = callbacks_map_find(map, key);
    if (callback_ptr == NULL) {
        return NULL;
    }
    return *callback_ptr;
}

/*
 * The semantics with set/unset is that we simply replace whatever
 * callback was there, if something already exists with that key.
 *
 * This function also increases the size of the map if a new callback
 * is added and not simply replaced.
 */
void
callbacks_map_set(struct CallbacksMap *map, uint64_t key, Callback callback) {
    Callback *callback_ptr = callbacks_map_find(map, key);
    if (callback_ptr != NULL) {
        if (*callback_ptr == NULL) {
            // Increase size since there was no callback set, even
            // though the node exists
            map->size++;
        }
        *callback_ptr = callback;
    } else {
        uint64_t hash = u64_hash(key);
        uint64_t i = hash % map->num_buckets;
        map->buckets[i] = callback_list_add(map->buckets[i], key, callback);
        map->size++;
    }
}

/*
 * This function replaces the existing stored callback with NULL,
 * returning the old value, if found. It also reduces the size of the
 * map.
 *
 * That means that once the map grows, it is only truly using less
 * memory when callbacks_map_free() is called.
 */
Callback
callbacks_map_unset(struct CallbacksMap *map, uint64_t key) {
    Callback *callback_ptr = callbacks_map_find(map, key);
    if (callback_ptr != NULL) {
        Callback callback = *callback_ptr;
        *callback_ptr = NULL;
        map->size--;
        return callback;
    }
    // Not found
    return NULL;
}

void
callbacks_map_free(struct CallbacksMap *map) {
    for (size_t i = 0; i < map->num_buckets; i++) {
        callback_list_free(map->buckets[i]);
    }
    free(map->buckets);
    free(map);
}

static struct Config {
    uint16_t border_width, outer_border_width, magnet_border_width;
    int16_t offset_x, offset_y;
    uint16_t offset_width, offset_height;
    struct {
        uint32_t focused, unfocused, unkillable, empty, outer;
    } colors;
} cfg;

/*
 * Display server-specific information.
 */
static struct {
    xcb_connection_t *conn;
    xcb_ewmh_connection_t *ewmh;
    bool has_error;
    xcb_atom_t delete_window_atom, change_state_atom;
    xcb_key_symbols_t *keysyms;
    uint16_t num_lock, caps_lock, scroll_lock;
    xcb_font_t cursor_font;
    xcb_cursor_t fleur_cursor, sizing_cursor;
    xcb_screen_t *screen;
    bool has_randr;
    int randr_base, screen_num;
    struct Monitor *monitors;
} dpy;

/*
 * Function pointer type for event handlers that take a generic event
 * and know what to do with it.
 */
typedef void (*EventHandler)(xcb_generic_event_t *);

static struct {
    uint32_t cur_workspace, num_workspaces;
    bool is_running;
    uint16_t mod_key;
    struct Client *focus;
    struct ClientsMap *clients;
    struct Workspace *workspaces;
    struct CallbacksMap *keys;
    struct CallbacksMap *buttons;
    EventHandler events[XCB_NO_OPERATION];
} wm;

static xcb_screen_t *get_screen();
static bool request_has_error(xcb_void_cookie_t);
static bool dpy_init();
static void dpy_flush();

static xcb_screen_t *
get_screen() {
    const xcb_setup_t* setup = xcb_get_setup(dpy.conn);
    xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
    for (int i = dpy.screen_num; iter.rem; --i) {
        if (i == 0) {
            return iter.data;
        }
        xcb_screen_next(&iter);
    }
    return NULL;
}

static bool
request_has_error(xcb_void_cookie_t cookie) {
    xcb_generic_error_t *error = xcb_request_check(dpy.conn, cookie);
    if (error) {
        free(error);
        return true;
    }
    return false;
}

struct WindowsQueryReply {
    xcb_window_t *data;
    int length;
};

void
windows_query(xcb_window_t root, struct WindowsQueryReply *reply) {
    xcb_query_tree_cookie_t cookie = xcb_query_tree(dpy.conn, root);
    xcb_query_tree_reply_t *data = xcb_query_tree_reply(dpy.conn, cookie, NULL);
    if (!data) {
        return;
    }
    reply->length = 0;
    int length = xcb_query_tree_children_length(data);
    // Assume length is OK, but reply->length holds the length of
    // clients we are really interested in
    reply->data = calloc(length, sizeof(xcb_window_t));
    xcb_window_t *children = xcb_query_tree_children(data);
    for (int i = 0; i < length; i++) {
        xcb_get_window_attributes_cookie_t attr_cookie = xcb_get_window_attributes(
            dpy.conn,
            children[i]);
        xcb_get_window_attributes_reply_t *attr_reply = xcb_get_window_attributes_reply(
            dpy.conn,
            attr_cookie,
            NULL);
        /*
         * From the 2bwm source:
         * Ignore windows in override redirect mode. This mode means
         * they wouldn't have been reported to us with a
         * XCB_MAP_REQUEST if we had been running, so in the normal
         * case we wouldn't have seen them. Only handle visible
         * windows.
         */
        if (attr_reply
            && !attr_reply->override_redirect
            && attr_reply->map_state == XCB_MAP_STATE_VIEWABLE) {
            reply->data[reply->length++] = children[i];
        }
        free(attr_reply);
    }
}

void
windows_query_reply_wipe(struct WindowsQueryReply *reply) {
    if (reply->length > 0) {
        free(reply->data);
    }
}

static void
dpy_set_window_state(xcb_window_t window, enum WindowState state) {
    if (state == WS_NORMAL) {
        long data[] = { XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE };
        xcb_change_property(
            dpy.conn,
            XCB_PROP_MODE_REPLACE,
            window,
            dpy.ewmh->_NET_WM_STATE,
            dpy.ewmh->_NET_WM_STATE,
            32,
            2,
            data);
    } else if (state == WS_ICONIFIED) {
        xcb_atom_t data[] = { dpy.ewmh->_NET_WM_STATE_HIDDEN };
        xcb_ewmh_set_wm_state(dpy.ewmh, window, 1, data);
    } else if (state == WS_MAXIMIZED) {
        xcb_atom_t data[] = {
            dpy.ewmh->_NET_WM_STATE_MAXIMIZED_VERT,
            dpy.ewmh->_NET_WM_STATE_MAXIMIZED_HORZ,
        };
        xcb_ewmh_set_wm_state(dpy.ewmh, window, 2, data);
    } else if (state == WS_FULLSCREEN) {
        xcb_atom_t data[] = { dpy.ewmh->_NET_WM_STATE_FULLSCREEN };
        xcb_ewmh_set_wm_state(dpy.ewmh, window, 1, data);
    }
}

static void
dpy_set_focus(xcb_window_t window) {
    xcb_window_t focus_window = window;
    uint8_t revert_to = XCB_INPUT_FOCUS_POINTER_ROOT;
    if (window == XCB_NONE) {
        focus_window = XCB_INPUT_FOCUS_POINTER_ROOT;
        revert_to = XCB_INPUT_FOCUS_NONE;
    } else {
        dpy_set_window_state(window, WS_NORMAL);
    }
    xcb_set_input_focus(dpy.conn, revert_to, focus_window, XCB_CURRENT_TIME);
    xcb_ewmh_set_active_window(dpy.ewmh, dpy.screen_num, window);
}

bool
dpy_has_error() {
    return dpy.has_error || xcb_connection_has_error(dpy.conn) > 0;
}

bool
dpy_is_protocol_supported(xcb_window_t window, xcb_atom_t atom) {
    xcb_get_property_cookie_t cookie =
        xcb_icccm_get_wm_protocols_unchecked(
            dpy.conn,
            window,
            dpy.ewmh->WM_PROTOCOLS);
    xcb_icccm_get_wm_protocols_reply_t protocols;
    uint8_t reply = xcb_icccm_get_wm_protocols_reply(
        dpy.conn,
        cookie,
        &protocols,
        NULL);
    if (!reply) {
        return false;
    }

    bool is_supported = false;
    for (uint32_t i = 0; i < protocols.atoms_len; i++) {
        if (protocols.atoms[i] == atom) {
            is_supported = true;
            break;
        }
    }
    xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
    return is_supported;
}

struct Monitor *
dpy_find_monitor(xcb_randr_output_t id) {
    if (dpy.monitors == NULL) {
        return NULL;
    }

    struct Monitor *monitor = dpy.monitors;
    for (;;) {
        if (monitor->id == id) {
            return monitor;
        }
        monitor = monitor->next;
        if (monitor == dpy.monitors) {
            break;
        }
    }
    return NULL;
}

bool
dpy_should_update_outputs(const xcb_generic_event_t *event) {
    return dpy.has_randr
        && event->response_type == dpy.randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY;
}

static bool
dpy_update_output(xcb_randr_output_t output,
                  xcb_timestamp_t timestamp,
                  xcb_randr_get_output_info_reply_t *o_reply) {
    xcb_randr_get_crtc_info_cookie_t c_cookie =
        xcb_randr_get_crtc_info(dpy.conn, o_reply->crtc, timestamp);
    xcb_randr_get_crtc_info_reply_t *c_reply =
        xcb_randr_get_crtc_info_reply(dpy.conn, c_cookie, NULL);
    if (!c_reply) {
        return false;
    }

    bool any_change = false;
    struct Monitor *monitor = dpy_find_monitor(output);
    if (monitor != NULL) {
        // Monitor already known, update info
        if (c_reply->x != monitor->x) {
            monitor->x = c_reply->x;
            any_change = true;
        }
        if (c_reply->y != monitor->y) {
            monitor->y = c_reply->y;
            any_change = true;
        }
        if (c_reply->width != monitor->width) {
            monitor->width = c_reply->width;
            any_change = true;
        }
        if (c_reply->height != monitor->height) {
            monitor->height = c_reply->height;
            any_change = true;
        }
        goto dpy_update_output_end;
    }

    // Check if we have a clone of an existing monitor, since we did
    // not find a monitor with same id
    if (dpy.monitors != NULL) {
        monitor = dpy.monitors;
        for (;;) {
            if (monitor->x == c_reply->x
                && monitor->y == c_reply->y
                && monitor->width == c_reply->width
                && monitor->height == c_reply->height) {
                // Found a clone, can be ignored
                goto dpy_update_output_end;
            }
            monitor = monitor->next;
            if (monitor == dpy.monitors) {
                break;
            }
        }
    }

    // It is a new monitor
    // XXX: should we care about name length?
    // int name_len = min(16, xcb_randr_get_output_info_name_length(o_reply));
    const char *name = (const char *) xcb_randr_get_output_info_name(o_reply);
    monitor = calloc(1, sizeof(struct Monitor));
    if (monitor == NULL) {
        goto dpy_update_output_end;
    }
    monitor->id = output;
    monitor->name = name;
    monitor->x = c_reply->x;
    monitor->y = c_reply->y;
    monitor->width = c_reply->width;
    monitor->height = c_reply->height;
    dpy.monitors = monitor_ring_add(dpy.monitors, monitor);
    any_change = true;

dpy_update_output_end:
    free(c_reply);
    return any_change;
}

/*
 * Attempts to update monitor info and returns whether there were any
 * changes.
 */
static bool
dpy_update_outputs() {
    if (!dpy.has_randr) {
        return false;
    }

    xcb_randr_get_screen_resources_current_cookie_t s_cookie =
        xcb_randr_get_screen_resources_current(dpy.conn, dpy.screen->root);
    xcb_randr_get_screen_resources_current_reply_t *s_reply =
        xcb_randr_get_screen_resources_current_reply(dpy.conn, s_cookie, NULL);
    if (s_reply == NULL) {
        return false;
    }

    bool any_change = false;
    xcb_timestamp_t timestamp = s_reply->config_timestamp;
    int length = xcb_randr_get_screen_resources_current_outputs_length(s_reply);
    xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(s_reply);
    for (int i = 0; i < length; i++) {
        xcb_randr_get_output_info_cookie_t o_cookie =
            xcb_randr_get_output_info(dpy.conn, outputs[i], timestamp);
        xcb_randr_get_output_info_reply_t *o_reply =
            xcb_randr_get_output_info_reply(dpy.conn, o_cookie, NULL);
        if (o_reply == NULL) {
            continue;
        }

        if (o_reply->crtc == XCB_NONE) {
            // Output no longer active, delete if we had any info about it
            struct Monitor *monitor = dpy_find_monitor(outputs[i]);
            if (monitor != NULL) {
                dpy.monitors = monitor_ring_erase(dpy.monitors, monitor);
                free(monitor);
                any_change = true;
            }
        } else {
            // New monitor or existing one changed its info
            any_change = any_change || dpy_update_output(outputs[i], timestamp, o_reply);
        }

        free(o_reply);
    }
    free(s_reply);
    return any_change;
}

static void
dpy_flush() {
    xcb_flush(dpy.conn);
}

static uint16_t
mod_from_keycode(xcb_keycode_t keycode) {
    uint16_t mod = 0;
    xcb_get_modifier_mapping_cookie_t cookie =
        xcb_get_modifier_mapping(dpy.conn);
    xcb_get_modifier_mapping_reply_t *reply =
        xcb_get_modifier_mapping_reply(dpy.conn, cookie, NULL);
    if (reply == NULL || reply->keycodes_per_modifier == 0) {
        goto mod_from_keycode_end;
    }

    xcb_keycode_t *mod_keycodes = xcb_get_modifier_mapping_keycodes(reply);
    if (mod_keycodes == NULL) {
        goto mod_from_keycode_end;
    }

    int length = xcb_get_modifier_mapping_keycodes_length(reply) / reply->keycodes_per_modifier;
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < reply->keycodes_per_modifier; j++) {
            xcb_keycode_t mod_keycode = mod_keycodes[i * reply->keycodes_per_modifier + j];
            if (mod_keycode != XCB_NO_SYMBOL && mod_keycode == keycode) {
                mod |= 1 << i;
            }
        }
    }

mod_from_keycode_end:
    free(reply);
    return mod;
}

static uint16_t
mod_from_keysym(xcb_keysym_t keysym) {
    uint16_t mod = 0;
    const xcb_setup_t *setup = xcb_get_setup(dpy.conn);
    if (!setup) {
        return mod;
    }

    /*
     * We go through every keycode in the setup, looking for the ones
     * matching our keysym request. Then, we transform those into a
     * mod we can use and return.
     *
     * Also, don't use xcb_keycode_t for kc. It overflows when
     * max_keycode coincides with the maximum value of xcb_keycode_t.
     */
    unsigned int kc;
    for (kc = setup->min_keycode; kc <= setup->max_keycode; kc++) {
        for (unsigned int col = 0; col < 4 /* KEYSYMS_PER_KEYCODE */; col++) {
            xcb_keysym_t ks = xcb_key_symbols_get_keysym(dpy.keysyms, kc, col);
            if (ks == keysym) {
                mod |= mod_from_keycode(kc);
            }
        }
    }
    return mod;
}

static xcb_atom_t
dpy_get_atom(const char *name) {
    xcb_intern_atom_cookie_t cookie = xcb_intern_atom(dpy.conn, 0, strlen(name), name);
    xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy.conn, cookie, NULL);
    if (!reply) {
        return 0;
    }

    xcb_atom_t atom = reply->atom;
    free(reply);
    return atom;
}

static xcb_cursor_t
dpy_create_cursor(uint16_t glyph) {
    xcb_cursor_t cursor = xcb_generate_id(dpy.conn);
    xcb_create_glyph_cursor(
        dpy.conn,
        cursor,
        dpy.cursor_font,
        dpy.cursor_font,
        glyph,
        glyph + 1,
        0x3232,
        0x3232,
        0x3232,
        0xeeee,
        0xeeee,
        0xeeec);
    return cursor;
}

static bool
dpy_init(const char *wm_name) {
    // This is initially NULL on purpose: we always try to dealloc
    // later and it might happen that we dealloc before we even tried
    // to setup the keysym stuff. This way we ensure we know whether
    // keysyms is a valid ptr or not.
    dpy.keysyms = NULL;
    dpy.has_error = false;
    dpy.has_randr = false;
    dpy.randr_base = -1;
    dpy.monitors = NULL;

    dpy.conn = xcb_connect(NULL, &dpy.screen_num);
    if (dpy_has_error()) {
        return false;
    }

    dpy.screen = get_screen();
    if (!dpy.screen) {
        dpy.has_error = true;
        return false;
    }

    dpy.ewmh = calloc(1, sizeof(xcb_ewmh_connection_t));
    if (!dpy.ewmh) {
        dpy.has_error = true;
        return false;
    }

    xcb_intern_atom_cookie_t *ewmh_cookie = xcb_ewmh_init_atoms(
        dpy.conn,
        dpy.ewmh);
    if (xcb_ewmh_init_atoms_replies(dpy.ewmh, ewmh_cookie, NULL) == 0) {
        dpy.has_error = true;
        return false;
    }

    xcb_atom_t net_atoms[] = {
        dpy.ewmh->_NET_SUPPORTED,
        dpy.ewmh->_NET_WM_DESKTOP,
        dpy.ewmh->_NET_NUMBER_OF_DESKTOPS,
        dpy.ewmh->_NET_CURRENT_DESKTOP,
        dpy.ewmh->_NET_ACTIVE_WINDOW,
        dpy.ewmh->_NET_WM_ICON,
        dpy.ewmh->_NET_WM_STATE,
        dpy.ewmh->_NET_WM_NAME,
        dpy.ewmh->_NET_SUPPORTING_WM_CHECK,
        dpy.ewmh->_NET_WM_STATE_HIDDEN,
        dpy.ewmh->_NET_WM_ICON_NAME,
        dpy.ewmh->_NET_WM_WINDOW_TYPE,
        dpy.ewmh->_NET_WM_WINDOW_TYPE_DOCK,
        dpy.ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
        dpy.ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR,
        dpy.ewmh->_NET_WM_PID,
        dpy.ewmh->_NET_CLIENT_LIST,
        dpy.ewmh->WM_PROTOCOLS,
        dpy.ewmh->_NET_WM_STATE,
        dpy.ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
        dpy.ewmh->_NET_WM_STATE_FULLSCREEN,
    };
    xcb_ewmh_set_supported(
        dpy.ewmh,
        dpy.screen_num,
        sizeof(net_atoms) / sizeof(net_atoms[0]),
        net_atoms);

    // Setup RandR
    const struct xcb_query_extension_reply_t *reply =
        xcb_get_extension_data(dpy.conn, &xcb_randr_id);
    if (!reply || !reply->present) {
        dpy.has_randr = false;
    } else {
        dpy.has_randr = true;
        dpy_update_outputs();
        dpy.randr_base = reply->first_event;
        xcb_randr_select_input(
            dpy.conn,
            dpy.screen->root,
            XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
            | XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
            | XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE
            | XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
    }

    // Ask display server for window manager capabilities
    dpy.delete_window_atom = dpy_get_atom("WM_DELETE_WINDOW");
    dpy.change_state_atom = dpy_get_atom("WM_CHANGE_STATE");

    // Setup keys
    xcb_ungrab_key(dpy.conn, XCB_GRAB_ANY, dpy.screen->root, XCB_MOD_MASK_ANY);
    dpy.keysyms = xcb_key_symbols_alloc(dpy.conn);
    if (!dpy.keysyms) {
        dpy.has_error = true;
        return false;
    }
    dpy.num_lock = mod_from_keysym(XK_Num_Lock);
    dpy.caps_lock = mod_from_keysym(XK_Caps_Lock);
    dpy.scroll_lock = mod_from_keysym(XK_Scroll_Lock);

    // Setup cursors
    const char *cursor_font_name = "cursor";
    dpy.cursor_font = xcb_generate_id(dpy.conn);
    xcb_open_font(dpy.conn, dpy.cursor_font, strlen(cursor_font_name), cursor_font_name);
    dpy.fleur_cursor = dpy_create_cursor(MOVE_GLYPH);
    dpy.sizing_cursor = dpy_create_cursor(RESIZE_GLYPH);

    xcb_event_mask_t event_mask = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
        | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
        | XCB_EVENT_MASK_PROPERTY_CHANGE
        | XCB_EVENT_MASK_BUTTON_PRESS;
    xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(
        dpy.conn,
        dpy.screen->root,
        XCB_CW_EVENT_MASK,
        &event_mask);
    if (request_has_error(cookie)) {
        dpy.has_error = true;
        return false;
    }

    xcb_ewmh_set_wm_name(dpy.ewmh, dpy.screen->root, strlen(wm_name), wm_name);
    xcb_ewmh_set_wm_pid(dpy.ewmh, dpy.screen->root, getpid());
    dpy_flush();
    return !dpy.has_error;
}

static bool
dpy_grab_keycode(uint16_t mod, xcb_keycode_t keycode) {
    xcb_void_cookie_t cookie = xcb_grab_key_checked(
        dpy.conn,
        true,
        dpy.screen->root,
        mod,
        keycode,
        XCB_GRAB_MODE_ASYNC,
        XCB_GRAB_MODE_ASYNC);
    xcb_generic_error_t *error = xcb_request_check(dpy.conn, cookie);
    return !error;
}

static void
dpy_grab_keycode_with_lock_mods(uint16_t mod, xcb_keycode_t keycode) {
    dpy_grab_keycode(mod, keycode);
    if (mod == XCB_MOD_MASK_ANY) {
        return;
    }

    if (dpy.num_lock != 0) {
        dpy_grab_keycode(mod | dpy.num_lock, keycode);
    }
    if (dpy.caps_lock != 0) {
        dpy_grab_keycode(mod | dpy.caps_lock, keycode);
    }
    if (dpy.scroll_lock != 0) {
        dpy_grab_keycode(mod | dpy.scroll_lock, keycode);
    }
    if (dpy.num_lock != 0 && dpy.caps_lock != 0) {
        dpy_grab_keycode(mod | dpy.num_lock | dpy.caps_lock, keycode);
    }
    if (dpy.caps_lock != 0 && dpy.scroll_lock != 0) {
        dpy_grab_keycode(mod | dpy.caps_lock | dpy.scroll_lock, keycode);
    }
    if (dpy.num_lock != 0 && dpy.scroll_lock != 0) {
        dpy_grab_keycode(mod | dpy.num_lock | dpy.scroll_lock, keycode);
    }
    if (dpy.num_lock != 0 && dpy.caps_lock != 0 && dpy.scroll_lock != 0) {
        dpy_grab_keycode(mod | dpy.num_lock | dpy.caps_lock | dpy.scroll_lock, keycode);
    }
}

static void
dpy_grab_key(uint16_t mod, xcb_keysym_t keysym) {
    const xcb_setup_t *setup = xcb_get_setup(dpy.conn);
    if (!setup) {
        assert(setup);
        return;
    }

    /*
     * We go through every keycode in the setup, looking for the ones
     * matching our keysym request.
     */
    unsigned int kc;
    for (kc = setup->min_keycode; kc <= setup->max_keycode; kc++) {
        for (unsigned int col = 0; col < 4 /* KEYSYMS_PER_KEYCODE */; col++) {
            xcb_keysym_t ks = xcb_key_symbols_get_keysym(dpy.keysyms, kc, col);
            if (ks == keysym) {
                dpy_grab_keycode_with_lock_mods(mod, kc);
            }
        }
    }
}

static uint16_t
dpy_clean_mod(uint16_t mod) {
    return mod & ~(dpy.num_lock | dpy.caps_lock | dpy.scroll_lock);
}

static xcb_keysym_t
dpy_keysym_from_keycode(xcb_keycode_t keycode) {
    return xcb_key_symbols_get_keysym(dpy.keysyms, keycode, 0);
}

static void
dpy_set_workspace(uint32_t workspace) {
    xcb_ewmh_set_current_desktop(dpy.ewmh, dpy.screen_num, workspace);
}

static void
dpy_set_num_workspaces(uint32_t num_workspaces) {
    xcb_ewmh_set_number_of_desktops(dpy.ewmh, dpy.screen_num, num_workspaces);
}

static void
dpy_map_window(xcb_window_t window) {
    xcb_map_window(dpy.conn, window);
}

static void
dpy_unmap_window(xcb_window_t window) {
    xcb_unmap_window(dpy.conn, window);
}

static void
dpy_raise_window(xcb_window_t window) {
    if (window == dpy.screen->root || window == XCB_NONE) {
        return;
    }

    uint32_t mode = XCB_STACK_MODE_ABOVE;
    xcb_configure_window(dpy.conn, window, XCB_CONFIG_WINDOW_STACK_MODE, &mode);
}

void
dpy_set_window_workspace(xcb_window_t window, uint32_t workspace) {
    xcb_ewmh_set_wm_desktop(dpy.ewmh, window, workspace);
}

uint32_t
dpy_get_window_workspace(xcb_window_t window) {
    xcb_get_property_cookie_t cookie = xcb_ewmh_get_wm_desktop(dpy.ewmh, window);
    uint32_t workspace = NULL_WORKSPACE;
    // We don't check the return of the call below because, if anything goes
    // wrong, we'd just assume o3::NULL_WORKSPACE anyway
    xcb_ewmh_get_wm_desktop_reply(dpy.ewmh, cookie, &workspace, NULL);
    return workspace;
}

void
dpy_update_window_geometry(const struct Client *client) {
    xcb_window_t window = client->id;
    if (dpy.screen->root == window || window == XCB_NONE) {
        return;
    }

    uint32_t data[] = {
        (uint32_t) client->x,
        (uint32_t) client->y,
        (uint32_t) client->width,
        (uint32_t) client->height,
    };
    uint16_t mask = XCB_CONFIG_WINDOW_X
        | XCB_CONFIG_WINDOW_Y
        | XCB_CONFIG_WINDOW_WIDTH
        | XCB_CONFIG_WINDOW_HEIGHT;
    xcb_configure_window(dpy.conn, window, mask, data);
}

void
dpy_update_window_position(const struct Client *client) {
    xcb_window_t window = client->id;
    if (dpy.screen->root == window || window == XCB_NONE) {
        return;
    }

    uint32_t data[] = {
        (uint32_t) client->x,
        (uint32_t) client->y,
    };
    uint16_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
    xcb_configure_window(dpy.conn, window, mask, data);
}

void
dpy_update_window_size(const struct Client *client) {
    xcb_window_t window = client->id;
    if (dpy.screen->root == window || window == XCB_NONE) {
        return;
    }

    uint32_t data[] = {
        (uint32_t) client->width,
        (uint32_t) client->height,
    };
    uint16_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
    xcb_configure_window(dpy.conn, window, mask, data);
}

void
dpy_set_window_border_width(xcb_window_t window) {
    xcb_configure_window(
        dpy.conn,
        window,
        XCB_CONFIG_WINDOW_BORDER_WIDTH,
        &cfg.border_width);
}

#define RECT_INIT(x, y, w, h) { \
        (int16_t) (x), \
        (int16_t) (y), \
        (uint16_t) (w), \
        (uint16_t) (h), \
}

static void
dpy_draw_borders(struct Client *client, bool is_focused) {
    if (client->state == WS_ICONIFIED || client->should_ignore_borders) {
        return;
    }

    dpy_set_window_border_width(client->id);

    xcb_rectangle_t inner_rect[] = {
        RECT_INIT(
            client->width,
            0,
            cfg.border_width - cfg.outer_border_width,
            client->height + cfg.border_width - cfg.outer_border_width
        ),
        RECT_INIT(
            client->width + cfg.border_width + cfg.outer_border_width,
            0,
            cfg.border_width - cfg.outer_border_width,
            client->height + cfg.border_width - cfg.outer_border_width
        ),
        RECT_INIT(
            0,
            client->height,
            client->width + cfg.border_width - cfg.outer_border_width,
            cfg.border_width - cfg.outer_border_width
        ),
        RECT_INIT(
            0,
            client->height + cfg.border_width + cfg.outer_border_width,
            client->width + cfg.border_width - cfg.outer_border_width,
            cfg.border_width - cfg.outer_border_width
        ),
        RECT_INIT(
            client->width + cfg.border_width + cfg.outer_border_width,
            client->height + cfg.border_width + cfg.outer_border_width,
            cfg.border_width,
            cfg.border_width
        ),
    };
    xcb_rectangle_t outer_rect[] = {
        RECT_INIT(
            client->width + cfg.border_width - cfg.outer_border_width,
            0,
            cfg.outer_border_width,
            client->height + cfg.border_width * 2
        ),
        RECT_INIT(
            client->width + cfg.border_width,
            0,
            cfg.outer_border_width,
            client->height + cfg.border_width * 2
        ),
        RECT_INIT(
            0,
            client->height + cfg.border_width - cfg.outer_border_width,
            client->width + cfg.border_width * 2,
            cfg.outer_border_width
        ),
        RECT_INIT(
            0,
            client->height + cfg.border_width,
            client->width + cfg.border_width * 2,
            cfg.outer_border_width
        ),
        RECT_INIT(
            1,
            1,
            1,
            1
        ),
    };

    xcb_pixmap_t pixmap = xcb_generate_id(dpy.conn);
    xcb_create_pixmap(
        dpy.conn,
        dpy.screen->root_depth,
        pixmap,
        dpy.screen->root,
        client->width + cfg.border_width * 2,
        client->height + cfg.border_width * 2);

    uint32_t color = client->is_unkillable
                   ? cfg.colors.unkillable : cfg.colors.outer;
    xcb_gcontext_t gc = xcb_generate_id(dpy.conn);
    xcb_create_gc(dpy.conn, gc, pixmap, 0, NULL);
    xcb_change_gc(dpy.conn, gc, XCB_GC_FOREGROUND, &color);
    xcb_poly_fill_rectangle(dpy.conn, pixmap, gc, 5, outer_rect);

    color = is_focused ? cfg.colors.focused : cfg.colors.unfocused;
    xcb_change_gc(dpy.conn, gc, XCB_GC_FOREGROUND, &color);
    xcb_poly_fill_rectangle(dpy.conn, pixmap, gc, 5, inner_rect);
    xcb_change_window_attributes(dpy.conn, client->id, XCB_CW_BORDER_PIXMAP, &pixmap);
    xcb_free_gc(dpy.conn, gc);
    xcb_free_pixmap(dpy.conn, pixmap);
}

#undef RECT_INIT

void
dpy_draw_focused_borders(struct Client *client) {
    dpy_draw_borders(client, true);
}

void
dpy_draw_unfocused_borders(struct Client *client) {
    dpy_draw_borders(client, false);
}

xcb_window_t
dpy_window_under_cursor() {
    xcb_query_pointer_cookie_t cookie =
        xcb_query_pointer(dpy.conn, dpy.screen->root);
    xcb_query_pointer_reply_t *reply =
        xcb_query_pointer_reply(dpy.conn, cookie, NULL);
    if (!reply) {
        return XCB_NONE;
    }

    xcb_window_t window = reply->child;
    free(reply);
    return window;
}

void
dpy_center_cursor_client(const struct Client *client) {
    int16_t x = client->width / 2;
    int16_t y = client->height / 2;
    xcb_warp_pointer(dpy.conn, XCB_NONE, client->id, 0, 0, 0, 0, x, y);
}

void
dpy_grab_buttons(struct Client *client) {
}

bool
dpy_fetch_geometry(struct Client *client) {
    xcb_get_geometry_cookie_t cookie = xcb_get_geometry(dpy.conn, client->id);
    xcb_get_geometry_reply_t *reply =
        xcb_get_geometry_reply(dpy.conn, cookie, NULL);
    if (!reply) {
        return false;
    }

    client->x = reply->x;
    client->y = reply->y;
    client->width = reply->width;
    client->height = reply->height;
    free(reply);
    return true;
}

bool
dpy_fetch_hints(struct Client *client) {
    xcb_size_hints_t hints;
    xcb_get_property_cookie_t cookie =
        xcb_icccm_get_wm_normal_hints_unchecked(dpy.conn, client->id);
    uint8_t reply = xcb_icccm_get_wm_normal_hints_reply(dpy.conn, cookie, &hints, NULL);
    if (!reply) {
        return false;
    }

    if (hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION) {
        client->user_coord = true;
        client->x = hints.x;
        client->y = hints.y;
    }
    if (hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
        client->min_width = hints.min_width;
        client->min_height = hints.min_height;
    }
    if (hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE) {
        client->max_width = hints.max_width;
        client->max_height = hints.max_height;
    }
    if (hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
        client->base_width = hints.base_width;
        client->base_height = hints.base_height;
    }
    return true;
}

bool
dpy_fetch_cursor_position(struct Point *p) {
    xcb_query_pointer_cookie_t cookie =
        xcb_query_pointer(dpy.conn, dpy.screen->root);
    xcb_query_pointer_reply_t *reply =
        xcb_query_pointer_reply(dpy.conn, cookie, NULL);
    if (!reply) {
        return false;
    }

    p->x = reply->win_x;
    p->y = reply->win_y;
    free(reply);
    return true;
}

static bool
dpy_should_ignore_window(xcb_window_t window) {
    xcb_ewmh_get_atoms_reply_t window_type;
    xcb_get_property_cookie_t cookie =
        xcb_ewmh_get_wm_window_type(dpy.ewmh, window);
    uint8_t reply = xcb_ewmh_get_wm_window_type_reply(
        dpy.ewmh,
        cookie,
        &window_type,
        NULL);
    if (!reply) {
        return false;
    }

    // Figure out from window type if we should ignore it
    bool should_ignore = false;
    for (uint32_t i = 0; i < window_type.atoms_len; i++) {
        xcb_atom_t atom = window_type.atoms[i];
        if (atom == dpy.ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
            || atom == dpy.ewmh->_NET_WM_WINDOW_TYPE_DESKTOP
            || atom == dpy.ewmh->_NET_WM_WINDOW_TYPE_DOCK) {
            // Ignore those windows
            should_ignore = true;
            break;
        }
    }
    // TODO: test/check if this is necessary only if reply is valid
    xcb_ewmh_get_atoms_reply_wipe(&window_type);
    return should_ignore;
}

struct Client *
dpy_make_client(xcb_window_t window) {
    if (dpy_should_ignore_window(window)) {
        return NULL;
    }

    uint32_t event_mask[] = { XCB_EVENT_MASK_ENTER_WINDOW, XCB_NONE };
    xcb_change_window_attributes(dpy.conn, window, XCB_CW_EVENT_MASK, event_mask);
    xcb_change_window_attributes(dpy.conn, window, XCB_CW_BACK_PIXEL, &cfg.colors.empty);
    xcb_change_save_set(dpy.conn, XCB_SET_MODE_INSERT, window);

    struct Client *client = client_new(window);
    if (client) {
        dpy_fetch_geometry(client);
        // Assume a reasonable max_width/max_height, then fetch hints
        client->max_width = dpy.screen->width_in_pixels;
        client->max_height = dpy.screen->height_in_pixels;
        dpy_fetch_hints(client);
    }
    return client;
}

/*
 * Finds the monitor which contains most of the client's rect.
 */
struct Monitor *
find_monitor(const struct Client *client) {
    if (dpy.monitors == NULL) {
        return NULL;
    }
    if (dpy.monitors->next == dpy.monitors) {
        // We only have one monitor
        return dpy.monitors;
    }

    struct Rect c_rect;
    c_rect.x = client->x;
    c_rect.y = client->y;
    c_rect.width = client->width;
    c_rect.height = client->height;

    uint32_t max_area = 0;
    struct Monitor *iter = dpy.monitors;
    struct Monitor *monitor = iter;
    for (;;) {
        struct Rect mon_rect;
        monitor_rect(iter, &mon_rect);
        uint32_t area = rect_intersect_area(&c_rect, &mon_rect);
        if (area > max_area) {
            max_area = area;
            monitor = iter;
        }
        iter = iter->next;
        if (iter == dpy.monitors) {
            // We reached the first monitor in the ring again
            break;
        }
    }
    return monitor;
}

void
dpy_update_window_list(const struct ClientsMap *clients) {
    xcb_window_t *windows = NULL;
    size_t num_windows = 0;
    if (clients->size != 0) {
        windows = malloc(clients->size * sizeof(xcb_window_t));
        for (size_t i = 0; i < clients->num_buckets; i++) {
            for (struct ClientList *list = clients->buckets[i];
                 list != NULL;
                 list = list->next) {
                windows[num_windows++] = list->client->id;
            }
        }
    }

    xcb_ewmh_set_client_list(dpy.ewmh, dpy.screen_num, num_windows, windows);
    free(windows);
}

void
dpy_send_window_message(xcb_window_t window, xcb_atom_t atom) {
    xcb_client_message_event_t event;
    event.response_type = XCB_CLIENT_MESSAGE;
    event.format = 32;
    event.sequence = 0;
    event.window = window;
    event.type = dpy.ewmh->WM_PROTOCOLS;
    event.data.data32[0] = atom;
    event.data.data32[1] = XCB_CURRENT_TIME;
    xcb_send_event(
        dpy.conn,
        false,
        window,
        XCB_EVENT_MASK_NO_EVENT,
        (char *) &event);
}

void
dpy_kill_window(xcb_window_t window) {
    xcb_kill_client(dpy.conn, window);
}

void
dpy_destroy() {
    if (dpy.ewmh) {
        xcb_ewmh_connection_wipe(dpy.ewmh);
        free(dpy.ewmh);
    }

    if (dpy.keysyms) {
        xcb_key_symbols_free(dpy.keysyms);
    }

    struct Monitor *monitor = dpy.monitors;
    while (dpy.monitors != NULL) {
        dpy.monitors = monitor_ring_erase(dpy.monitors, monitor);
        free(monitor);
        monitor = dpy.monitors;
    }

    if (!dpy_has_error()) {
        xcb_free_cursor(dpy.conn, dpy.fleur_cursor);
        xcb_free_cursor(dpy.conn, dpy.sizing_cursor);
        xcb_close_font(dpy.conn, dpy.cursor_font);
        xcb_set_input_focus(
            dpy.conn,
            XCB_NONE,
            XCB_INPUT_FOCUS_POINTER_ROOT,
            XCB_CURRENT_TIME);
        dpy_flush();
    }
    xcb_disconnect(dpy.conn);
}

/*
 * Window Manager API.
 */
struct Client *wm_find_client(xcb_window_t id);
void wm_erase_client(xcb_window_t id);
bool wm_update_outputs();
bool wm_has_error();
bool wm_init();
void wm_quit();
void wm_grab_key_with_mod(xcb_keysym_t keysym, Callback callback);
void wm_grab_key_with_mod_shift(xcb_keysym_t keysym, Callback callback);
void wm_begin_move_client();
void wm_begin_resize_client();
void wm_focus_close();
void wm_focus_prev();
void wm_focus_next();
void wm_toggle_maximize();
void wm_toggle_half_left();
void wm_toggle_half_right();
void wm_toggle_half_top();
void wm_toggle_half_bottom();
void wm_toggle_top_left();
void wm_toggle_top_right();
void wm_toggle_bottom_left();
void wm_toggle_bottom_right();
void wm_pack_left();
void wm_pack_right();
void wm_pack_top();
void wm_pack_bottom();
void wm_set_workspace(uint32_t workspace);
void wm_set_focused_client_workspace(uint32_t workspace);
void wm_client_monitor_prev();
void wm_client_monitor_next();
void wm_run();

struct Client *
wm_find_client(xcb_window_t id) {
    return clients_map_find(wm.clients, id);
}

void
wm_erase_client(xcb_window_t id) {
    struct Client *client = wm_find_client(id);
    if (client == NULL) {
        return;
    }

    struct Workspace *workspace = &wm.workspaces[client->workspace];
    // Workspace being NULL is a bug!
    workspace->ring = client_ring_erase(workspace->ring, client);
    clients_map_erase(wm.clients, client);
    free(client);
    if (!wm.focus) {
        dpy_set_focus(XCB_NONE);
    }
}

bool
wm_apply_size_hints(struct Client *client) {
    if (client->state != WS_NORMAL) {
        return false;
    }

    bool any_change = false;
    if (client->min_width > 0 && client->width < client->min_width) {
        client->width = client->min_width;
        any_change = true;
    }
    if (client->max_width > 0 && client->width > client->max_width) {
        client->width = client->max_width;
        any_change = true;
    }
    if (client->min_height > 0 && client->height < client->min_height) {
        client->height = client->min_height;
        any_change = true;
    }
    if (client->max_height > 0 && client->height > client->max_height) {
        client->height = client->max_height;
        any_change = true;
    }
    return any_change;
}

void
wm_set_focus(struct Client *client) {
    if (wm.focus != NULL) {
        if (wm.focus == client) {
            return;
        }
        dpy_draw_unfocused_borders(wm.focus);
    }

    wm.focus = client;
    dpy_set_focus(client->id);
    dpy_grab_buttons(client);
    dpy_draw_focused_borders(client);
    dpy_flush();
}

void
wm_set_workspace(uint32_t workspace) {
    struct Client *client;
    if (workspace > wm.num_workspaces) {
        return;
    }

    if (workspace != wm.cur_workspace) {
        // Unmap all clients in current workspace
        struct Workspace *ws_ptr = &wm.workspaces[wm.cur_workspace];
        client = ws_ptr->ring;
        while (client != NULL) {
            if (client->state != WS_ICONIFIED) {
                dpy_unmap_window(client->id);
            }
            client = client->next;

            if (client == ws_ptr->ring) {
                break;
            }
        }

        wm.cur_workspace = workspace;
    }

    // Update current workspace and map all clients at new current workspace
    dpy_set_workspace(wm.cur_workspace);
    struct Workspace *ws_ptr = &wm.workspaces[wm.cur_workspace];
    client = ws_ptr->ring;
    while (client != NULL) {
        if (client->state != WS_ICONIFIED) {
            dpy_map_window(client->id);
        }
        client = client->next;

        if (client == ws_ptr->ring) {
            break;
        }
    }

    xcb_window_t window = dpy_window_under_cursor();
    client = clients_map_find(wm.clients, window);
    if (client == NULL) {
        wm.focus = NULL;
        dpy_set_focus(XCB_NONE);
        dpy_flush();
    } else {
        wm_set_focus(client);
    }
}

void
wm_set_client_workspace(struct Client *client, uint32_t workspace) {
    uint32_t old_workspace = client->workspace;
    bool is_known = clients_map_find(wm.clients, client->id) != NULL;
    if (old_workspace == workspace && is_known) {
        return;
    }

    // assert(workspace != NULL_WORKSPACE && workspace != ALL_WORKSPACES);
    client->workspace = workspace;
    dpy_set_window_workspace(client->id, workspace);
    if (is_known && old_workspace != NULL_WORKSPACE) {
        wm.workspaces[old_workspace].ring = client_ring_erase(wm.workspaces[old_workspace].ring, client);
        dpy_unmap_window(client->id);
    }
    wm.workspaces[workspace].ring = client_ring_add(wm.workspaces[workspace].ring, client);
}

void
wm_client_monitor_size(const struct Client *client,
                       bool with_offsets,
                       struct Rect *rect) {
    if (client->monitor == NULL) {
        // No monitor means that the default screen is our monitor
        rect->x = rect->y = 0;
        rect->width = dpy.screen->width_in_pixels;
        rect->height = dpy.screen->height_in_pixels;
    } else {
        rect->x = client->monitor->x;
        rect->y = client->monitor->y;
        rect->width = client->monitor->width;
        rect->height = client->monitor->height;
    }

    if (with_offsets) {
        rect->x += cfg.offset_x;
        rect->y += cfg.offset_y;
        rect->width -= cfg.offset_width;
        rect->height -= cfg.offset_height;
    }
}

void
wm_set_client_state(struct Client *client, enum WindowState state) {
    if (state == client->state) {
        return;
    }

    switch (state) {
    case WS_NORMAL:
        if (client->state == WS_ICONIFIED) {
            dpy_map_window(client->id);
            // TODO: check size restoration
        }
        client_restore_size(client);
        dpy_update_window_geometry(client);
        dpy_draw_borders(client, wm.focus == client);
        break;
    case WS_ICONIFIED:
        dpy_unmap_window(client->id);
        break;
    case WS_MAXIMIZED:
        if (!client->has_old_size) {
            client_store_size(client);
        }
        {
            struct Rect mon_rect;
            wm_client_monitor_size(client, true, &mon_rect);
            client->x = mon_rect.x;
            client->y = mon_rect.y;
            client->width = mon_rect.width - cfg.border_width * 2;
            client->height = mon_rect.height - cfg.border_width * 2;
        }
        dpy_update_window_geometry(client);
        dpy_draw_borders(client, wm.focus == client);
        break;
    case WS_FULLSCREEN:
        break;
    }
    client->prev_state = client->state;
    client->state = state;
    dpy_set_window_state(client->id, client->state);
}

void
wm_move_client(struct Client *client, int16_t dx, int16_t dy, bool in_monitor) {
    client->x += dx;
    client->y += dy;
    if (in_monitor) {
        struct Rect mon_rect;
        wm_client_monitor_size(client, true, &mon_rect);
        // Complete movement by checking for magnets
        if (client->x < cfg.magnet_border_width + mon_rect.x) {
            client->x = mon_rect.x;
        } else if (client->x + client->width + cfg.border_width >
                   mon_rect.x + mon_rect.width - cfg.magnet_border_width) {
            client->x = mon_rect.x + mon_rect.width - client->width - cfg.border_width * 2;
        }
        if (client->y < cfg.magnet_border_width + mon_rect.y) {
            client->y = mon_rect.y;
        } else if (client->y + client->height + cfg.border_width >
                   mon_rect.y + mon_rect.height - cfg.magnet_border_width) {
            client->y = mon_rect.y + mon_rect.height - client->height - cfg.border_width * 2;
        }
    }
    dpy_update_window_position(client);
    // dpy_draw_focused_borders(client);
    dpy_flush();
}

void
wm_resize_client(struct Client *client,
                 uint16_t width_inc,
                 uint16_t height_inc,
                 bool in_monitor) {
    // TODO: honor inc hints
    client->width += width_inc;
    client->height += height_inc;
    wm_apply_size_hints(client);

    if (in_monitor) {
        struct Rect mon_rect;
        wm_client_monitor_size(client, true, &mon_rect);
        if (client->x + client->width + cfg.border_width >
            mon_rect.x + mon_rect.width) {
            client->width = mon_rect.width - ((client->x - mon_rect.x) + cfg.border_width * 2);
        }
        if (client->y + client->height + cfg.border_width >
            mon_rect.y + mon_rect.height) {
            client->height = mon_rect.height - ((client->y - mon_rect.y) + cfg.border_width * 2);
        }
    }
    dpy_update_window_size(client);
    dpy_draw_focused_borders(client);
    dpy_flush();
}

void
wm_move_resize_client(struct Client *client,
                      const struct Rect *rect,
                      bool in_monitor) {
    if (in_monitor) {
    } else {
        client->x = rect->x;
        client->y = rect->y;
        client->width = rect->width;
        client->height = rect->height;
    }
    wm_apply_size_hints(client);
    dpy_update_window_geometry(client);
    dpy_draw_focused_borders(client);
    dpy_flush();
}

bool
wm_fit_client(struct Client *client) {
    if (client->state == WS_ICONIFIED) {
        return false;
    }

    struct Rect mon_rect;
    wm_client_monitor_size(client, true, &mon_rect);

    // Check if client *looks* maximized, event though it is not
    if (client->state == WS_NORMAL
        && client->width >= mon_rect.width
        && client->height >= mon_rect.height) {
        wm_set_client_state(client, WS_MAXIMIZED);
        return true;
    }

    bool should_move = false;
    // Check if client is located outside monitor coords
    if (client->x > mon_rect.x + mon_rect.width) {
        client->x = mon_rect.x + mon_rect.width - client->width - cfg.border_width * 2;
        should_move = true;
    }
    if (client->y > mon_rect.y + mon_rect.height) {
        client->y = mon_rect.y + mon_rect.height - client->height - cfg.border_width * 2;
        should_move = true;
    }
    if (client->x < mon_rect.x) {
        client->x = mon_rect.x;
        should_move = true;
    }
    if (client->y < mon_rect.y) {
        client->y = mon_rect.y;
        should_move = true;
    }

    // Check if client is larger than monitor, if so move to upper corner and
    // resize to fill
    bool should_resize = wm_apply_size_hints(client);
    uint16_t border_width = client->should_ignore_borders ? 0 : cfg.border_width;
    if (client->width + border_width * 2 > mon_rect.width) {
        client->x = mon_rect.x;
        client->width = mon_rect.width - border_width * 2;
        should_move = true;
        should_resize = true;
    } else if (client->x + client->width + border_width * 2 > mon_rect.x + mon_rect.width) {
        client->x = mon_rect.x + mon_rect.width - (client->width + border_width * 2);
        should_move = true;
    }
    if (client->height + border_width * 2 > mon_rect.height) {
        client->y = mon_rect.y;
        client->height = mon_rect.height - border_width * 2;
        should_move = true;
        should_resize = true;
    } else if (client->y + client->height + border_width * 2 > mon_rect.y + mon_rect.height) {
        client->y = mon_rect.y + mon_rect.height - (client->height + border_width * 2);
        should_move = true;
    }

    if (should_move && should_resize) {
        dpy_update_window_geometry(client);
    } else if (should_move) {
        dpy_update_window_position(client);
    } else if (should_resize) {
        dpy_update_window_size(client);
    }

    return should_move || should_resize;
}

void
wm_update_client_monitor(struct Client *client) {
    client->monitor = find_monitor(client);
    wm_fit_client(client);
}

void
wm_handle_state(struct Client *client, xcb_atom_t atom, uint32_t action) {
    // `state' holds what the atom wants to tell us about what to do
    enum WindowState state = WS_NORMAL;
    if (atom == dpy.ewmh->_NET_WM_STATE_FULLSCREEN) {
        state = WS_FULLSCREEN;
    } else if (atom == dpy.ewmh->_NET_WM_STATE_HIDDEN) {
        state = WS_ICONIFIED;
    } else {
        fprintf(stderr,
                "handle_state(): Unknown state=%u, action=%u\n",
                state,
                action);
    }

    // `new_state' is what the new state of our window should be
    enum WindowState new_state = state;
    switch (action) {
    case XCB_EWMH_WM_STATE_ADD:
        // This case is assumed to be the default as well
        new_state = state;
        break;
    case XCB_EWMH_WM_STATE_REMOVE:
        new_state = client->prev_state;
        break;
    case XCB_EWMH_WM_STATE_TOGGLE:
        new_state = client->state != state ? state : client->prev_state;
        break;
    default:
        fprintf(stderr, "handle_state(): Unknown action=%u\n", action);
    }
    wm_set_client_state(client, new_state);
}

void
wm_set_mod_key(uint16_t mod) {
    wm.mod_key = mod;
}

uint16_t
wm_get_mod_key(bool with_shift) {
    if (with_shift) {
        return wm.mod_key | MOD_SHIFT;
    }
    return wm.mod_key;
}

void
wm_grab_key(xcb_keysym_t keysym, bool with_shift, Callback callback) {
    /*
     * Key grabbing is done in two stages:
     *
     * 1. Register a function to be called when that key combo is
     * pressed
     */
    uint16_t mod = wm_get_mod_key(with_shift);
    uint64_t map_key = make_key_chord(mod, keysym);
    callbacks_map_set(wm.keys, map_key, callback);

    /*
     * 2. Tell the display server we are interested in a certain
     * mod/key combo
     */
    dpy_grab_key(mod, keysym);
}

void
wm_grab_key_with_mod(xcb_keysym_t keysym, Callback callback) {
    wm_grab_key(keysym, false, callback);
}

void
wm_grab_key_with_mod_shift(xcb_keysym_t keysym, Callback callback) {
    wm_grab_key(keysym, true, callback);
}

/*
 * Event handlers that know what to cast the generic event into.
 */
static void ev_configure_request(xcb_generic_event_t *);
static void ev_destroy_notify(xcb_generic_event_t *);
static void ev_enter_notify(xcb_generic_event_t *);
static void ev_key_press(xcb_generic_event_t *);
static void ev_map_request(xcb_generic_event_t *);
static void ev_unmap_notify(xcb_generic_event_t *);
static void ev_mapping_notify(xcb_generic_event_t *);
static void ev_configure_notify(xcb_generic_event_t *);
static void ev_circulate_request(xcb_generic_event_t *);
static void ev_button_press(xcb_generic_event_t *);
static void ev_client_message(xcb_generic_event_t *);

static struct Client *
manage_client_maybe_reposition(xcb_window_t window, bool set_position) {
    struct Client *client = dpy_make_client(window);
    if (!client) {
        return NULL;
    }
    // TODO: check if unkillable
    uint32_t workspace = dpy_get_window_workspace(client->id);
    if (workspace == ALL_WORKSPACES) {
        fprintf(stderr, "client was supposed to be sticky;"
                        " mapping to current workspace\n");
        workspace = wm.cur_workspace;
    } else if (workspace == NULL_WORKSPACE || workspace >= wm.num_workspaces) {
        workspace = wm.cur_workspace;
    }
    wm_set_client_workspace(client, workspace);
    if (set_position && !client->user_coord) {
        // No predefined position, map at cursor position if possible
        struct Point p;
        if (!dpy_fetch_cursor_position(&p)) {
            p.x = p.y = 0;
        }
        client->x = p.x - client->width / 2;
        client->y = p.y - client->height / 2;
        dpy_update_window_position(client);
    }
    wm_update_client_monitor(client);
    clients_map_add(wm.clients, client);
    if (client->state == WS_NORMAL || client->state == WS_MAXIMIZED) {
        dpy_draw_unfocused_borders(client);
    }
    return client;
}

static struct Client *
manage_client(xcb_window_t window) {
    return manage_client_maybe_reposition(window, false);
}

bool
wm_init() {
    if (!dpy_init("schewm")) {
        return false;
    }

    wm.is_running = true;
    wm.focus = NULL;
    wm.clients = clients_map_new(128);
    if (wm.clients == NULL) {
        return false;
    }
    wm.keys = callbacks_map_new(64);
    if (wm.keys == NULL) {
        return false;
    }
    wm.buttons = callbacks_map_new(16);
    if (wm.buttons == NULL) {
        return false;
    }

    wm.cur_workspace = 0;
    wm.num_workspaces = 9;
    wm.workspaces = calloc(wm.num_workspaces, sizeof(struct Workspace));
    if (wm.workspaces == NULL) {
        return false;
    }
    dpy_set_num_workspaces(wm.num_workspaces);

    // Look into all existing windows and set them up
    struct WindowsQueryReply reply;
    windows_query(dpy.screen->root, &reply);
    for (int i = 0; i < reply.length; i++) {
        xcb_window_t window = reply.data[i];
        if (!manage_client(window)) {
            /*
             * Could not be managed, just map it on screen because we
             * probably should not own it.
             */
            fprintf(stderr, "wm_init(): could not manage: %x\n", window);
            dpy_map_window(window);
        } else {
            /*
             * Unmap everything we grab initially because they may be
             * mapped currently, but belong to another workspace; this
             * way, we can map again only the correct windows.
             */
            dpy_unmap_window(window);
        }
    }
    windows_query_reply_wipe(&reply);

    dpy_update_window_list(wm.clients);
    wm_set_workspace(wm.cur_workspace);
    wm.mod_key = MOD_ALT;
    dpy_flush();

    memset(wm.events, 0, sizeof(wm.events));
    wm.events[XCB_CONFIGURE_REQUEST]   = ev_configure_request;
    wm.events[XCB_DESTROY_NOTIFY]      = ev_destroy_notify;
    wm.events[XCB_ENTER_NOTIFY]        = ev_enter_notify;
    wm.events[XCB_KEY_PRESS]           = ev_key_press;
    wm.events[XCB_MAP_REQUEST]         = ev_map_request;
    wm.events[XCB_UNMAP_NOTIFY]        = ev_unmap_notify;
    wm.events[XCB_MAPPING_NOTIFY]      = ev_mapping_notify;
    wm.events[XCB_CONFIGURE_NOTIFY]    = ev_configure_notify;
    wm.events[XCB_CIRCULATE_REQUEST]   = ev_circulate_request;
    wm.events[XCB_BUTTON_PRESS]        = ev_button_press;
    wm.events[XCB_CLIENT_MESSAGE]      = ev_client_message;

    return !dpy.has_error;
}

void
wm_quit() {
    wm.is_running = false;
}

void
wm_destroy() {
    free(wm.workspaces);
    clients_map_free(wm.clients);
    callbacks_map_free(wm.keys);
    callbacks_map_free(wm.buttons);
    dpy_destroy();
}

void
wm_reconfigure(uint16_t border_width,
               uint16_t outer_border_width,
               uint16_t magnet_border_width,
               int16_t offset_x,
               int16_t offset_y,
               uint16_t offset_width,
               uint16_t offset_height,
               uint32_t focused,
               uint32_t unfocused,
               uint32_t unkillable,
               uint32_t empty,
               uint32_t outer) {
    cfg.border_width = border_width;
    cfg.outer_border_width = outer_border_width;
    cfg.magnet_border_width = magnet_border_width;
    cfg.offset_x = offset_x;
    cfg.offset_width = offset_width;
    cfg.offset_height = offset_height;
    cfg.colors.focused = focused;
    cfg.colors.unfocused = unfocused;
    cfg.colors.unkillable = unkillable;
    cfg.colors.empty = empty;
    cfg.colors.outer = outer;

    // Set new window border of ALL clients
    for (uint32_t workspace = 0; workspace < wm.num_workspaces; workspace++) {
        struct Client *client = wm.workspaces[workspace].ring;
        if (client == NULL) {
            continue;
        }

        for (;;) {
            dpy_set_window_border_width(client->id);
            wm_fit_client(client);

            client = client->next;
            if (client == wm.workspaces[workspace].ring) {
                break;
            }
        }
    }

    // Redraw borders of clients from current workspace only
    struct Client *client = wm.workspaces[wm.cur_workspace].ring;
    if (client == NULL) {
        return;
    }

    for (;;) {
        dpy_draw_borders(client, wm.focus == client);
        client = client->next;
        if (client == wm.workspaces[wm.cur_workspace].ring) {
            break;
        }
    }
}

void
wm_close(struct Client *client) {
    if (client->is_unkillable) {
        return;
    }

    if (dpy_is_protocol_supported(client->id, dpy.delete_window_atom)) {
        dpy_send_window_message(client->id, dpy.delete_window_atom);
    } else {
        dpy_kill_window(client->id);
    }
}

void
wm_focus_close() {
    if (wm.focus == NULL) {
        return;
    }

    wm_close(wm.focus);
}

void
wm_raise_and_center_cursor(struct Client *client) {
    dpy_raise_window(client->id);
    dpy_center_cursor_client(client);
}

void
wm_focus_other(bool is_next) {
    struct Client *focused = wm.focus;
    if (focused == NULL) {
        // No current focus, pick first client from the ring
        focused = wm.workspaces[wm.cur_workspace].ring;
    } else {
        // We have focus, pick next/prev
        focused = is_next ? focused->next : focused->prev;
    }
    if (focused == NULL) {
        return;
    }

    wm_raise_and_center_cursor(focused);
    wm_set_focus(focused);
}

void
wm_focus_prev() {
    wm_focus_other(false);
}

void
wm_focus_next() {
    wm_focus_other(true);
}

void
wm_toggle_maximize() {
    if (wm.focus == NULL) {
      return;
    }

    if (wm.focus->state == WS_MAXIMIZED) {
        wm_set_client_state(wm.focus, wm.focus->prev_state);
    } else {
        wm_set_client_state(wm.focus, WS_MAXIMIZED);
    }
}

static void
wm_client_ensure_state(struct Client *client, enum WindowState state) {
    if (client->state != state) {
        wm_set_client_state(client, state);
    }
}

enum TileType {
    TILE_HALF_LEFT,
    TILE_HALF_RIGHT,
    TILE_HALF_TOP,
    TILE_HALF_BOTTOM,
    TILE_TOP_LEFT,
    TILE_TOP_RIGHT,
    TILE_BOTTOM_LEFT,
    TILE_BOTTOM_RIGHT,
};

static void
wm_toggle_tile(enum TileType tile_type) {
    if (wm.focus == NULL) {
        return;
    }

    wm_client_ensure_state(wm.focus, WS_NORMAL);
    if (wm.focus->has_old_size) {
        client_restore_size(wm.focus);
        dpy_update_window_geometry(wm.focus);
        dpy_draw_focused_borders(wm.focus);
   } else {
        struct Rect rect;
        // Start with monitor size, then reduce to appropriate target size
        wm_client_monitor_size(wm.focus, true, &rect);
        switch (tile_type) {
        case TILE_HALF_LEFT:
            rect.width = rect.width / 2;
            break;
        case TILE_HALF_RIGHT:
            rect.x += rect.width / 2;
            rect.width = rect.width / 2;
            break;
        case TILE_HALF_TOP:
            rect.height = rect.height / 2;
            break;
        case TILE_HALF_BOTTOM:
            rect.y += rect.height / 2;
            rect.height = rect.height / 2;
            break;
        case TILE_TOP_LEFT:
            rect.width = rect.width / 2;
            rect.height = rect.height / 2;
            break;
        case TILE_TOP_RIGHT:
            rect.x += rect.width / 2;
            rect.width = rect.width / 2;
            rect.height = rect.height / 2;
            break;
        case TILE_BOTTOM_LEFT:
            rect.y += rect.height / 2;
            rect.width = rect.width / 2;
            rect.height = rect.height / 2;
            break;
        case TILE_BOTTOM_RIGHT:
            rect.x += rect.width / 2;
            rect.y += rect.height / 2;
            rect.width = rect.width / 2;
            rect.height = rect.height / 2;
            break;
        }
        // Correct width/height according to borders
        rect.width -= cfg.border_width * 2;
        rect.height -= cfg.border_width * 2;
        // Stores current size so we can toggle it later
        client_store_size(wm.focus);
        wm_move_resize_client(wm.focus, &rect, false);
    }
    wm_raise_and_center_cursor(wm.focus);
}

void
wm_toggle_half_left() {
    wm_toggle_tile(TILE_HALF_LEFT);
}

void
wm_toggle_half_right() {
    wm_toggle_tile(TILE_HALF_RIGHT);
}

void
wm_toggle_half_top() {
    wm_toggle_tile(TILE_HALF_TOP);
}

void
wm_toggle_half_bottom() {
    wm_toggle_tile(TILE_HALF_BOTTOM);
}

void
wm_toggle_top_left() {
    wm_toggle_tile(TILE_TOP_LEFT);
}

void
wm_toggle_top_right() {
    wm_toggle_tile(TILE_TOP_RIGHT);
}

void
wm_toggle_bottom_left() {
    wm_toggle_tile(TILE_BOTTOM_LEFT);
}

void
wm_toggle_bottom_right() {
    wm_toggle_tile(TILE_BOTTOM_RIGHT);
}

enum PackType {
    PACK_LEFT,
    PACK_RIGHT,
    PACK_TOP,
    PACK_BOTTOM,
};

void
wm_pack_client(enum PackType pack_type) {
    if (wm.focus == NULL || wm.focus->state != WS_NORMAL) {
        return;
    }

    struct Rect rect;
    wm_client_monitor_size(wm.focus, true, &rect);
    int16_t dx = 0, dy = 0;
    switch (pack_type) {
    case PACK_LEFT:
        dx = rect.x - wm.focus->x;
        break;
    case PACK_RIGHT:
        dx = rect.width - (wm.focus->x - rect.x + wm.focus->width) - cfg.border_width * 2;
        break;
    case PACK_TOP:
        dy = rect.y - wm.focus->y;
        break;
    case PACK_BOTTOM:
        dy = rect.height - (wm.focus->y - rect.y + wm.focus->height) - cfg.border_width * 2;
        break;
    }
    wm_move_client(wm.focus, dx, dy, false);
    wm_raise_and_center_cursor(wm.focus);
}

void
wm_pack_left() {
    wm_pack_client(PACK_LEFT);
}

void
wm_pack_right() {
    wm_pack_client(PACK_RIGHT);
}

void
wm_pack_top() {
    wm_pack_client(PACK_TOP);
}

void
wm_pack_bottom() {
    wm_pack_client(PACK_BOTTOM);
}

void
wm_set_focused_client_workspace(uint32_t workspace) {
    if (wm.focus == NULL) {
        return;
    }

    wm_set_client_workspace(wm.focus, workspace);
}

static void
relative_pos(const struct Client *client,
             const struct Monitor *m1,
             const struct Monitor *m2,
             struct Point *p) {
    float scale_x = ((float) (client->x - m1->x)) / m1->width;
    float scale_y = ((float) (client->y - m1->y)) / m1->height;
    p->x = (int16_t) (m2->x + m2->width * scale_x + 0.5f);
    p->y = (int16_t) (m2->y + m2->height * scale_y + 0.5f);
}

void
wm_client_monitor_other(struct Client *client, bool is_next) {
    if (client->monitor == NULL || client->monitor->next == client->monitor) {
        return;
    }

    struct Monitor *other_monitor = is_next
        ? client->monitor->next : client->monitor->prev;
    struct Point new_pos;
    relative_pos(client, client->monitor, other_monitor, &new_pos);
    client->monitor = other_monitor;
    if (client->state == WS_NORMAL) {
        client->x = new_pos.x;
        client->y = new_pos.y;
        if (!wm_fit_client(client)) {
            // wm_fit_client() did not update position, force it
            dpy_update_window_position(client);
        }
    } else {
        // We restore size and ensure it is located within the new
        // monitor before maximizing, otherwise it will move between
        // monitors again when restored. We also fool the function
        // into believing we were not maximized before so that it
        // properly maximizes to the new monitor size.
        client_restore_size(client);
        client->x = new_pos.x;
        client->y = new_pos.y;
        client->state = WS_NORMAL;
        wm_set_client_state(client, WS_MAXIMIZED);
    }
    wm_raise_and_center_cursor(client);
    dpy_flush();
}

void
wm_client_monitor_prev() {
    if (wm.focus == NULL) {
        return;
    }

    wm_client_monitor_other(wm.focus, false);
}

void
wm_client_monitor_next() {
    if (wm.focus == NULL) {
        return;
    }

    wm_client_monitor_other(wm.focus, true);
}

static uint8_t
ev_type(const xcb_generic_event_t *ev) {
    return ev->response_type & ~0x80;
}

void
wm_run() {
    while (wm.is_running) {
        /*
         * Flush everything and check connection health before trying
         * to process further events
         */
        dpy_flush();
        if (dpy_has_error()) {
            fprintf(stderr, "display has an error\n");
            break;
        }

        // We usually get ev == NULL when the display server is killed
        xcb_generic_event_t *ev = xcb_wait_for_event(dpy.conn);
        if (ev == NULL) {
            fprintf(stderr, "cannot get display events, aborting\n");
            break;
        }

        EventHandler handler = wm.events[ev_type(ev)];
        if (handler) {
            handler(ev);
        }
        free(ev);
    }
    wm_destroy();
}

/*
 * NOTE:
 * Don't do the thing below. Create hooks for relevant stuff the user
 * might care about, call them at the appropriate places. Design should be:
 * - Event handlers: the WMs job
 * - Hooks: user customization
 *
 * Current sketch is too complex and we don't need to customize
 * behavior that much anyway.
 */

// XCB_CONFIGURE_REQUEST
typedef void (*configure_request_handler_t)();
static configure_request_handler_t configure_request_handler = NULL;

void
wm_set_configure_request_handler(configure_request_handler_t handler) {
    configure_request_handler = handler;
}

static void
ev_configure_request(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_configure_request\n");
    xcb_configure_request_event_t *ev = (xcb_configure_request_event_t *) generic_ev;
    if (configure_request_handler) {
        configure_request_handler();
    }

    struct Client *client = wm_find_client(ev->window);
    if (client == NULL) {
        uint16_t mask = 0;
        uint32_t data[7];
        uint8_t i = 0;
        if (ev->value_mask & XCB_CONFIG_WINDOW_X) {
            mask |= XCB_CONFIG_WINDOW_X;
            data[i++] = ev->x;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_Y) {
            mask |= XCB_CONFIG_WINDOW_Y;
            data[i++] = ev->y;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
            mask |= XCB_CONFIG_WINDOW_WIDTH;
            data[i++] = ev->width;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
            mask |= XCB_CONFIG_WINDOW_HEIGHT;
            data[i++] = ev->height;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
            mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
            data[i++] = ev->border_width;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
            mask |= XCB_CONFIG_WINDOW_SIBLING;
            data[i++] = ev->sibling;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
            mask |= XCB_CONFIG_WINDOW_STACK_MODE;
            data[i++] = ev->stack_mode;
        }
        if (i > 0) {
            // TODO: should be a dpy_something() function
            xcb_configure_window(dpy.conn, ev->window, mask, data);
        }
    } else if (client->state == WS_NORMAL) {
        // TODO: do we need to handle XCB_CONFIG_WINDOW_SIBLING and
        // XCB_CONFIG_WINDOW_STACK_MODE?
        if (ev->value_mask & XCB_CONFIG_WINDOW_X) {
            client->x = ev->x;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_Y) {
            client->y = ev->y;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
            client->width = ev->width;
        }
        if (ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
            client->height = ev->height;
        }
        wm_apply_size_hints(client);
        dpy_update_window_geometry(client);
        dpy_draw_borders(client, wm.focus == client);
        wm_update_client_monitor(client);
    }
}

// XCB_DESTROY_NOTIFY
typedef void (*destroy_notify_handler_t)();
static destroy_notify_handler_t destroy_notify_handler = NULL;

void
wm_set_destroy_notify_handler(destroy_notify_handler_t handler) {
    destroy_notify_handler = handler;
}

static void
ev_destroy_notify(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_destroy_notify\n");
    xcb_destroy_notify_event_t *ev = (xcb_destroy_notify_event_t *) generic_ev;
    if (destroy_notify_handler) {
        destroy_notify_handler();
    }

    struct Client *client = wm_find_client(ev->window);
    if (client == NULL) {
        return;
    }

    wm_erase_client(client->id);
    dpy_update_window_list(wm.clients);
    dpy_flush();
}

// XCB_ENTER_NOTIFY
typedef void (*enter_notify_handler_t)();
static enter_notify_handler_t enter_notify_handler = NULL;

void
wm_set_enter_notify_handler(enter_notify_handler_t handler) {
    enter_notify_handler = handler;
}

static void
ev_enter_notify(xcb_generic_event_t *generic_ev) {
    xcb_enter_notify_event_t *ev = (xcb_enter_notify_event_t *) generic_ev;
    if (enter_notify_handler) {
        enter_notify_handler();
    }

    if ((ev->mode != XCB_NOTIFY_MODE_NORMAL && ev->mode != XCB_NOTIFY_MODE_UNGRAB)
        || (wm.focus != NULL && wm.focus->id == ev->event)) {
        return;
    }

    struct Client *client = wm_find_client(ev->event);
    if (client != NULL) {
        wm_set_focus(client);
    }
}

// XCB_KEY_PRESS
typedef void (*key_press_handler_t)(uint16_t, xcb_keysym_t);
static key_press_handler_t key_press_handler = NULL;

void
wm_set_key_press_handler(key_press_handler_t handler) {
    key_press_handler = handler;
}

static void
ev_key_press(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_key_press\n");
    xcb_key_press_event_t *ev = (xcb_key_press_event_t *) generic_ev;
    uint16_t mod = dpy_clean_mod(ev->state);
    xcb_keysym_t keysym = dpy_keysym_from_keycode(ev->detail);

    if (key_press_handler) {
        key_press_handler(mod, keysym);
    }

    uint64_t map_key = make_key_chord(mod, keysym);
    Callback callback = callbacks_map_get(wm.keys, map_key);
    if (callback != NULL) {
        callback();
    }
}

// XCB_MAP_REQUEST
typedef void (*map_request_handler_t)();
static map_request_handler_t map_request_handler = NULL;

void
wm_set_map_request_handler(map_request_handler_t handler) {
    map_request_handler = handler;
}

static void
ev_map_request(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_map_request\n");
    xcb_map_request_event_t *ev = (xcb_map_request_event_t *) generic_ev;
    if (map_request_handler) {
        map_request_handler();
    }

    struct Client *client = wm_find_client(ev->window);
    if (client != NULL) {
        // Client already known, ignore
        return;
    }

    client = manage_client_maybe_reposition(ev->window, true);
    if (client != NULL) {
        dpy_set_window_state(client->id, WS_NORMAL);
        dpy_center_cursor_client(client);
        dpy_update_window_list(wm.clients);
    }

    // Map as requested, it does not matter if we can manage or not
    dpy_map_window(ev->window);
    dpy_flush();
}

// XCB_UNMAP_NOTIFY
typedef void (*unmap_notify_handler_t)();
static unmap_notify_handler_t unmap_notify_handler = NULL;

void
wm_set_unmap_notify_handler(unmap_notify_handler_t handler) {
    unmap_notify_handler = handler;
}

static void
ev_unmap_notify(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_unmap_notify\n");
    xcb_unmap_notify_event_t *ev = (xcb_unmap_notify_event_t *) generic_ev;
    if (unmap_notify_handler) {
        unmap_notify_handler();
    }
    return;

    struct Client *client = wm_find_client(ev->window);
    if (client == NULL
        || wm.workspaces[wm.cur_workspace].ring == NULL
        || client->state == WS_ICONIFIED) {
        /*
         * For unknown clients, clients not in our current workspace,
         * or when a client was unmmaped and state is already iconified,
         * we don't have to do anything
         */
        return;
    }

    bool should_ignore = true;
    struct Client *ring_client = wm.workspaces[wm.cur_workspace].ring;
    for (;;) {
        if (ring_client->id == ev->window) {
            /*
             * We found the client in the current workspace, now we
             * must handle the unmapping
             */
            should_ignore = false;
            break;
        }
        ring_client = ring_client->next;
        if (ring_client == wm.workspaces[wm.cur_workspace].ring) {
            break;
        }
    }
    if (should_ignore) {
        return;
    }

    dpy_flush();
}

// XXX: do we need this?
// XCB_MAPPING_NOTIFY
typedef void (*mapping_notify_handler_t)();
static mapping_notify_handler_t mapping_notify_handler = NULL;

void
wm_set_mapping_notify_handler(mapping_notify_handler_t handler) {
    mapping_notify_handler = handler;
}

static void
ev_mapping_notify(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_mapping_notify\n");
    if (mapping_notify_handler) {
        xcb_mapping_notify_event_t *ev = (xcb_mapping_notify_event_t *) generic_ev;
        mapping_notify_handler();
    }
}

// XCB_CONFIGURE_NOTIFY
typedef void (*configure_notify_handler_t)(int16_t, int16_t, uint16_t, uint16_t);
static configure_notify_handler_t configure_notify_handler = NULL;

void
wm_set_configure_notify_handler(configure_notify_handler_t handler) {
    configure_notify_handler = handler;
}

static void
ev_configure_notify(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_configure_notify\n");
    xcb_configure_notify_event_t *ev = (xcb_configure_notify_event_t *) generic_ev;
    if (configure_notify_handler) {
        configure_notify_handler(ev->x, ev->y, ev->width, ev->height);
    }

    if (ev->window != dpy.screen->root) {
        // Currently ignoring regular windows being reconfigured
        return;
    }

    if (ev->width != dpy.screen->width_in_pixels ||
        ev->height != dpy.screen->height_in_pixels) {
        dpy.screen->width_in_pixels = ev->width;
        dpy.screen->height_in_pixels = ev->height;

        if (!dpy.has_randr) {
            // TODO: loop over all clients and call fit_client()
            dpy_flush();
        }
    }
}

// XCB_CIRCULATE_REQUEST
typedef void (*circulate_request_handler_t)();
static circulate_request_handler_t circulate_request_handler = NULL;

void
wm_set_circulate_request_handler(circulate_request_handler_t handler) {
    circulate_request_handler = handler;
}

static void
ev_circulate_request(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_circulate_request\n");
    xcb_circulate_request_event_t *ev = (xcb_circulate_request_event_t *) generic_ev;
    if (circulate_request_handler) {
        circulate_request_handler();
    }

    /*
     * XXX:
     * For proper abstraction, we should be calling
     *
     * dpy_circulate_window(ev->window, ev->place);
     *
     * instead. But this direct call below makes our code smaller.
     */
    xcb_circulate_window(dpy.conn, ev->window, ev->place);
}

// XCB_BUTTON_PRESS
typedef void (*button_press_handler_t)();
static button_press_handler_t button_press_handler = NULL;

void
wm_set_button_press_handler(button_press_handler_t handler) {
    button_press_handler = handler;
}

static void
ev_button_press(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_button_press\n");
    if (button_press_handler) {
        xcb_button_press_event_t *ev = (xcb_button_press_event_t *) generic_ev;
        button_press_handler();
    }
}

// XCB_CLIENT_MESSAGE
typedef void (*client_message_handler_t)();
static client_message_handler_t client_message_handler = NULL;

void
wm_set_client_message_handler(client_message_handler_t handler) {
    client_message_handler = handler;
}

static void
ev_client_message(xcb_generic_event_t *generic_ev) {
    fprintf(stderr, "ev_client_message\n");
    xcb_client_message_event_t *ev = (xcb_client_message_event_t *) generic_ev;
    if (client_message_handler) {
        client_message_handler();
    }

    if (ev->type == dpy.ewmh->_NET_CURRENT_DESKTOP) {
        wm_set_workspace(ev->data.data32[0]);
        return;
    }

    struct Client *client = wm_find_client(ev->window);
    if (client == NULL) {
        return;
    }

    if (ev->type == dpy.ewmh->_NET_ACTIVE_WINDOW) {
        wm_set_focus(client);
    } else if (ev->type == dpy.ewmh->_NET_WM_DESKTOP) {
        wm_set_client_workspace(client, ev->data.data32[0]);
    } else if (ev->type == dpy.ewmh->_NET_CLOSE_WINDOW) {
        wm_close(client);
    } else if (ev->type == dpy.ewmh->_NET_WM_STATE) {
        wm_handle_state(client, ev->data.data32[1], ev->data.data32[0]);
        wm_handle_state(client, ev->data.data32[2], ev->data.data32[0]);
    } else if (ev->type == dpy.change_state_atom
               && ev->format == 32
               && ev->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC) {
        wm_handle_state(
            client,
            dpy.ewmh->_NET_WM_STATE_HIDDEN,
            XCB_EWMH_WM_STATE_TOGGLE);
    }
}