[go: up one dir, main page]

File: libdecor.c

package info (click to toggle)
libdecor-0 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 492 kB
  • sloc: ansic: 8,377; cpp: 288; sh: 89; makefile: 23
file content (1738 lines) | stat: -rw-r--r-- 44,249 bytes parent folder | download
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
/*
 * Copyright © 2017-2018 Red Hat Inc.
 * Copyright © 2018 Jonas Ådahl
 * Copyright © 2019 Christian Rauch
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <errno.h>
#include <dirent.h>
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "libdecor.h"
#include "libdecor-fallback.h"
#include "libdecor-plugin.h"
#include "utils.h"

#include "xdg-shell-client-protocol.h"
#include "xdg-decoration-client-protocol.h"

#ifdef HAVE_XDG_SHELL_V6
#define XDG_WM_BASE_VER 6
#else
#define XDG_WM_BASE_VER 2
#endif

struct libdecor {
	int ref_count;

	struct libdecor_interface *iface;

	struct libdecor_plugin *plugin;
	bool plugin_ready;

	struct wl_display *wl_display;
	struct wl_registry *wl_registry;
	struct xdg_wm_base *xdg_wm_base;
	struct zxdg_decoration_manager_v1 *decoration_manager;

	struct wl_callback *init_callback;
	bool init_done;
	bool has_error;

	struct wl_list frames;
};

struct libdecor_state {
	enum libdecor_window_state window_state;

	int content_width;
	int content_height;
};

struct libdecor_limits {
	int min_width;
	int min_height;
	int max_width;
	int max_height;
};

struct libdecor_configuration {
	uint32_t serial;

	bool has_window_state;
	enum libdecor_window_state window_state;

	bool has_size;
	int window_width;
	int window_height;
};

struct libdecor_frame_private {
	int ref_count;

	struct libdecor *context;

	struct wl_surface *wl_surface;

	struct libdecor_frame_interface *iface;
	void *user_data;

	struct xdg_surface *xdg_surface;
	struct xdg_toplevel *xdg_toplevel;
	struct zxdg_toplevel_decoration_v1 *toplevel_decoration;

	bool pending_map;

	struct {
		char *app_id;
		char *title;
		struct libdecor_limits content_limits;
		struct xdg_toplevel *parent;
	} state;

	struct libdecor_configuration *pending_configuration;

	int content_width;
	int content_height;

	enum libdecor_window_state window_state;

	bool has_decoration_mode;
	enum zxdg_toplevel_decoration_v1_mode decoration_mode;

	enum libdecor_capabilities capabilities;

	/* original limits for interactive resize */
	struct libdecor_limits interactive_limits;

	bool visible;
};

struct libdecor_plugin_private {
	struct libdecor_plugin_interface *iface;
};

/* gather all states at which a window is non-floating */
static const enum libdecor_window_state states_non_floating =
	LIBDECOR_WINDOW_STATE_MAXIMIZED | LIBDECOR_WINDOW_STATE_FULLSCREEN |
	LIBDECOR_WINDOW_STATE_TILED_LEFT | LIBDECOR_WINDOW_STATE_TILED_RIGHT |
	LIBDECOR_WINDOW_STATE_TILED_TOP | LIBDECOR_WINDOW_STATE_TILED_BOTTOM;

static bool
streql(const char *str1, const char *str2)
{
	return (str1 && str2) && (strcmp(str1, str2) == 0);
}


static void
do_map(struct libdecor_frame *frame);

static bool
state_is_floating(enum libdecor_window_state window_state)
{
	return !(window_state & states_non_floating);
}

static void
constrain_content_size(const struct libdecor_frame *frame,
		       int *width,
		       int *height)
{
	const struct libdecor_limits lim = frame->priv->state.content_limits;

	if (lim.min_width > 0)
		*width = MAX(lim.min_width, *width);
	if (lim.max_width > 0)
		*width = MIN(*width, lim.max_width);

	if (lim.min_height > 0)
		*height = MAX(lim.min_height, *height);
	if (lim.max_height > 0)
		*height = MIN(*height, lim.max_height);
}

static bool
frame_has_visible_client_side_decoration(struct libdecor_frame *frame)
{
	/* visibility by client configuration */
	const bool vis_client = frame->priv->visible;
	/* visibility by compositor configuration */
	const bool vis_server = (frame->priv->decoration_mode ==
				 ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE);

	return vis_client && vis_server;
}

LIBDECOR_EXPORT int
libdecor_state_get_content_width(struct libdecor_state *state)
{
	return state->content_width;
}

LIBDECOR_EXPORT int
libdecor_state_get_content_height(struct libdecor_state *state)
{
	return state->content_height;
}

LIBDECOR_EXPORT enum libdecor_window_state
libdecor_state_get_window_state(struct libdecor_state *state)
{
	return state->window_state;
}

LIBDECOR_EXPORT struct libdecor_state *
libdecor_state_new(int width,
		   int height)
{
	struct libdecor_state *state;

	state = zalloc(sizeof *state);
	state->content_width = width;
	state->content_height = height;

	return state;
}

LIBDECOR_EXPORT void
libdecor_state_free(struct libdecor_state *state)
{
	free(state);
}

static struct libdecor_configuration *
libdecor_configuration_new(void)
{
	struct libdecor_configuration *configuration;

	configuration = zalloc(sizeof *configuration);

	return configuration;
}

static void
libdecor_configuration_free(struct libdecor_configuration *configuration)
{
	free(configuration);
}

static bool
frame_get_window_size_for(struct libdecor_frame *frame,
			  struct libdecor_state *state,
			  int *window_width,
			  int *window_height)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor *context = frame_priv->context;
	struct libdecor_plugin *plugin = context->plugin;

	*window_width = state->content_width;
	*window_height = state->content_height;

	if (frame_has_visible_client_side_decoration(frame) &&
	    plugin->priv->iface->frame_get_border_size) {
		int left, right, top, bottom;
		if (!plugin->priv->iface->frame_get_border_size(
			plugin, frame, NULL, &left, &right, &top, &bottom))
			return false;
		*window_width += left + right;
		*window_height += top + bottom;
	}

	return true;
}

static void
frame_set_window_geometry(struct libdecor_frame *frame,
			  int32_t content_width, int32_t content_height)
{
	struct libdecor_plugin *plugin = frame->priv->context->plugin;
	int x, y, width, height;
	int left, right, top, bottom;

	plugin->priv->iface->frame_get_border_size(plugin, frame, NULL,
						   &left, &right, &top, &bottom);
	x = -left;
	y = -top;
	width = content_width + left + right;
	height = content_height + top + bottom;
	xdg_surface_set_window_geometry(frame->priv->xdg_surface, x, y, width, height);
}

LIBDECOR_EXPORT bool
libdecor_configuration_get_content_size(struct libdecor_configuration *configuration,
					struct libdecor_frame *frame,
					int *width,
					int *height)
{
	struct libdecor_plugin *plugin = frame->priv->context->plugin;

	/* get configured toplevel dimensions */
	if (!configuration->has_size)
		return false;

	if (configuration->window_width == 0 || configuration->window_height == 0)
		return false;

	*width = configuration->window_width;
	*height = configuration->window_height;

	/* remove plugin-specific border size */
	if (frame_has_visible_client_side_decoration(frame) &&
	    plugin->priv->iface->frame_get_border_size) {
		int left, right, top, bottom;

		/* Update window state for correct border size calculation */
		frame->priv->window_state = configuration->window_state;
		if (!plugin->priv->iface->frame_get_border_size(
			plugin, frame, configuration, &left, &right, &top, &bottom))
			return false;

		*width -= (left + right);
		*height -= (top + bottom);
	}

	/* constrain content dimensions manually */
	if (state_is_floating(configuration->window_state)) {
		constrain_content_size(frame, width, height);
	}

	return true;
}

LIBDECOR_EXPORT bool
libdecor_configuration_get_window_state(struct libdecor_configuration *configuration,
					enum libdecor_window_state *window_state)
{
	if (!configuration->has_window_state)
		return false;

	*window_state = configuration->window_state;
	return true;
}

static void
xdg_surface_configure(void *user_data,
		      struct xdg_surface *xdg_surface,
		      uint32_t serial)
{
	struct libdecor_frame *frame = user_data;
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor_configuration *configuration;

	configuration = frame_priv->pending_configuration;
	frame_priv->pending_configuration = NULL;

	if (!configuration)
		configuration = libdecor_configuration_new();

	configuration->serial = serial;

	frame_priv->iface->configure(frame,
				     configuration,
				     frame_priv->user_data);

	libdecor_configuration_free(configuration);
}

static const struct xdg_surface_listener xdg_surface_listener = {
	xdg_surface_configure,
};

static enum libdecor_window_state
parse_states(struct wl_array *states)
{
	enum libdecor_window_state pending_state = LIBDECOR_WINDOW_STATE_NONE;
	uint32_t *p;

	wl_array_for_each(p, states) {
		enum xdg_toplevel_state state = *p;

		switch (state) {
		case XDG_TOPLEVEL_STATE_FULLSCREEN:
			pending_state |= LIBDECOR_WINDOW_STATE_FULLSCREEN;
			break;
		case XDG_TOPLEVEL_STATE_MAXIMIZED:
			pending_state |= LIBDECOR_WINDOW_STATE_MAXIMIZED;
			break;
		case XDG_TOPLEVEL_STATE_ACTIVATED:
			pending_state |= LIBDECOR_WINDOW_STATE_ACTIVE;
			break;
		case XDG_TOPLEVEL_STATE_TILED_LEFT:
			pending_state |= LIBDECOR_WINDOW_STATE_TILED_LEFT;
			break;
		case XDG_TOPLEVEL_STATE_TILED_RIGHT:
			pending_state |= LIBDECOR_WINDOW_STATE_TILED_RIGHT;
			break;
		case XDG_TOPLEVEL_STATE_TILED_TOP:
			pending_state |= LIBDECOR_WINDOW_STATE_TILED_TOP;
			break;
		case XDG_TOPLEVEL_STATE_TILED_BOTTOM:
			pending_state |= LIBDECOR_WINDOW_STATE_TILED_BOTTOM;
			break;
#ifdef HAVE_XDG_SHELL_V6
		case XDG_TOPLEVEL_STATE_SUSPENDED:
			pending_state |= LIBDECOR_WINDOW_STATE_SUSPENDED;
			break;
#endif
		default:
			break;
		}
	}

	return pending_state;
}

static void
xdg_toplevel_configure(void *user_data,
		       struct xdg_toplevel *xdg_toplevel,
		       int32_t width,
		       int32_t height,
		       struct wl_array *states)
{
	struct libdecor_frame *frame = user_data;
	struct libdecor_frame_private *frame_priv = frame->priv;
	enum libdecor_window_state window_state;

	window_state = parse_states(states);

	frame_priv->pending_configuration = libdecor_configuration_new();

	frame_priv->pending_configuration->has_size = true;
	frame_priv->pending_configuration->window_width = width;
	frame_priv->pending_configuration->window_height = height;

	frame_priv->pending_configuration->has_window_state = true;
	frame_priv->pending_configuration->window_state = window_state;
}

static void
xdg_toplevel_close(void *user_data,
		   struct xdg_toplevel *xdg_toplevel)
{
	struct libdecor_frame *frame = user_data;
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->iface->close(frame, frame_priv->user_data);
}

#ifdef HAVE_XDG_SHELL_V6
static void
xdg_toplevel_configure_bounds(void *data,
			      struct xdg_toplevel *xdg_toplevel,
			      int32_t width,
			      int32_t height)
{
}

static void
xdg_toplevel_wm_capabilities(void *data,
			     struct xdg_toplevel *xdg_toplevel,
			     struct wl_array *capabilities)
{
}
#endif

static const struct xdg_toplevel_listener xdg_toplevel_listener = {
	xdg_toplevel_configure,
	xdg_toplevel_close,
#ifdef HAVE_XDG_SHELL_V6
	xdg_toplevel_configure_bounds,
	xdg_toplevel_wm_capabilities,
#endif
};

static void
toplevel_decoration_configure(
		void *data,
		struct zxdg_toplevel_decoration_v1 *zxdg_toplevel_decoration_v1,
		uint32_t mode)
{
	struct libdecor_frame_private *frame_priv = (struct libdecor_frame_private *)data;
	/* Ignore any _configure calls after the first, they will be
	 * from our set_mode call. */
	if (!frame_priv->has_decoration_mode) {
		frame_priv->has_decoration_mode = true;
		frame_priv->decoration_mode = mode;
	}
}

static const struct zxdg_toplevel_decoration_v1_listener
		xdg_toplevel_decoration_listener = {
	toplevel_decoration_configure,
};

void
libdecor_frame_create_xdg_decoration(struct libdecor_frame_private *frame_priv)
{
	if (!frame_priv->context->decoration_manager)
		return;

	frame_priv->toplevel_decoration =
		zxdg_decoration_manager_v1_get_toplevel_decoration(
				frame_priv->context->decoration_manager,
				frame_priv->xdg_toplevel);

	zxdg_toplevel_decoration_v1_add_listener(
				frame_priv->toplevel_decoration,
				&xdg_toplevel_decoration_listener,
				frame_priv);
}

static void
init_shell_surface(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor *context = frame_priv->context;

	if (frame_priv->xdg_surface)
		return;

	frame_priv->xdg_surface =
		xdg_wm_base_get_xdg_surface(context->xdg_wm_base,
					    frame_priv->wl_surface);
	xdg_surface_add_listener(frame_priv->xdg_surface,
				 &xdg_surface_listener,
				 frame);

	frame_priv->xdg_toplevel =
		xdg_surface_get_toplevel(frame_priv->xdg_surface);
	xdg_toplevel_add_listener(frame_priv->xdg_toplevel,
				  &xdg_toplevel_listener,
				  frame);

	frame_priv->decoration_mode =
			ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
	frame_priv->toplevel_decoration = NULL;
	libdecor_frame_create_xdg_decoration(frame_priv);

	if (frame_priv->state.parent) {
		xdg_toplevel_set_parent(frame_priv->xdg_toplevel,
					frame_priv->state.parent);
	}
	if (frame_priv->state.title) {
		xdg_toplevel_set_title(frame_priv->xdg_toplevel,
				       frame_priv->state.title);
	}
	if (frame_priv->state.app_id) {
		xdg_toplevel_set_app_id(frame_priv->xdg_toplevel,
					frame_priv->state.app_id);
	}

	if (frame_priv->pending_map)
		do_map(frame);
}

LIBDECOR_EXPORT struct libdecor_frame *
libdecor_decorate(struct libdecor *context,
		  struct wl_surface *wl_surface,
		  struct libdecor_frame_interface *iface,
		  void *user_data)
{
	struct libdecor_plugin *plugin = context->plugin;
	struct libdecor_frame *frame;
	struct libdecor_frame_private *frame_priv;

	if (context->has_error)
		return NULL;

	frame = plugin->priv->iface->frame_new(plugin);
	if (!frame)
		return NULL;

	frame_priv = zalloc(sizeof *frame_priv);
	frame->priv = frame_priv;

	frame_priv->ref_count = 1;
	frame_priv->context = context;

	frame_priv->wl_surface = wl_surface;
	frame_priv->iface = iface;
	frame_priv->user_data = user_data;

	wl_list_insert(&context->frames, &frame->link);

	libdecor_frame_set_capabilities(frame,
					LIBDECOR_ACTION_MOVE |
					LIBDECOR_ACTION_RESIZE |
					LIBDECOR_ACTION_MINIMIZE |
					LIBDECOR_ACTION_FULLSCREEN |
					LIBDECOR_ACTION_CLOSE);

	frame_priv->visible = true;

	if (context->init_done)
		init_shell_surface(frame);

	return frame;
}

LIBDECOR_EXPORT void
libdecor_frame_ref(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->ref_count++;
}

LIBDECOR_EXPORT void
libdecor_frame_unref(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->ref_count--;
	if (frame_priv->ref_count == 0) {
		struct libdecor *context = frame_priv->context;
		struct libdecor_plugin *plugin = context->plugin;

		if (context->decoration_manager && frame_priv->toplevel_decoration) {
			zxdg_toplevel_decoration_v1_destroy(frame_priv->toplevel_decoration);
			frame_priv->toplevel_decoration = NULL;
		}

		wl_list_remove(&frame->link);

		if (frame_priv->xdg_toplevel)
			xdg_toplevel_destroy(frame_priv->xdg_toplevel);
		if (frame_priv->xdg_surface)
			xdg_surface_destroy(frame_priv->xdg_surface);

		plugin->priv->iface->frame_free(plugin, frame);

		free(frame_priv->state.title);
		free(frame_priv->state.app_id);

		free(frame_priv);

		free(frame);
	}
}

LIBDECOR_EXPORT void
libdecor_frame_set_visibility(struct libdecor_frame *frame,
			      bool visible)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor *context = frame_priv->context;
	struct libdecor_plugin *plugin = context->plugin;

	frame_priv->visible = visible;

	/* enable/disable decorations that are managed by the compositor.
	 * Note that, as of xdg_decoration v1, this is just a hint and there is
	 * no reliable way of disabling all decorations. In practice this should
	 * work but per spec this is not guaranteed.
	 *
	 * See also: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/17
	 */
	if (context->decoration_manager &&
	    frame_priv->toplevel_decoration &&
	    frame_priv->has_decoration_mode &&
	    frame_priv->decoration_mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE) {
		zxdg_toplevel_decoration_v1_set_mode(frame_priv->toplevel_decoration,
						     frame->priv->visible
						     ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE
						     : ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE);
	}

	if (frame_priv->content_width <= 0 ||
	    frame_priv->content_height <= 0)
		return;

	/* enable/disable decorations that are managed by a plugin */
	if (frame_has_visible_client_side_decoration(frame)) {
		/* show client-side decorations */
		plugin->priv->iface->frame_commit(plugin, frame, NULL, NULL);
	} else {
		/* destroy client-side decorations */
		plugin->priv->iface->frame_free(plugin, frame);
	}

	frame_set_window_geometry(frame,
				  frame_priv->content_width,
				  frame_priv->content_height);

	libdecor_frame_toplevel_commit(frame);
}

LIBDECOR_EXPORT bool
libdecor_frame_is_visible(struct libdecor_frame *frame)
{
	return frame->priv->visible;
}

LIBDECOR_EXPORT void
libdecor_frame_set_parent(struct libdecor_frame *frame,
			  struct libdecor_frame *parent)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	if (!frame_priv->xdg_toplevel)
		return;

	frame_priv->state.parent = parent ? parent->priv->xdg_toplevel : NULL;

	xdg_toplevel_set_parent(frame_priv->xdg_toplevel,
				frame_priv->state.parent);
}

LIBDECOR_EXPORT void
libdecor_frame_set_title(struct libdecor_frame *frame,
			 const char *title)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor_plugin *plugin = frame_priv->context->plugin;

	if (!streql(frame_priv->state.title, title)) {
		free(frame_priv->state.title);
		frame_priv->state.title = strdup(title);

		if (!frame_priv->xdg_toplevel)
			return;

		xdg_toplevel_set_title(frame_priv->xdg_toplevel, title);

		plugin->priv->iface->frame_property_changed(plugin, frame);
	}
}

LIBDECOR_EXPORT const char *
libdecor_frame_get_title(struct libdecor_frame *frame)
{
	return frame->priv->state.title;
}

LIBDECOR_EXPORT void
libdecor_frame_set_app_id(struct libdecor_frame *frame,
			  const char *app_id)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	free(frame_priv->state.app_id);
	frame_priv->state.app_id = strdup(app_id);

	if (!frame_priv->xdg_toplevel)
		return;

	xdg_toplevel_set_app_id(frame_priv->xdg_toplevel, app_id);
}

static void
notify_on_capability_change(struct libdecor_frame *frame,
			    const enum libdecor_capabilities old_capabilities)
{
	struct libdecor_plugin *plugin = frame->priv->context->plugin;
	struct libdecor_state *state;

	if (frame->priv->capabilities == old_capabilities)
		return;

	if (frame->priv->content_width == 0 ||
	    frame->priv->content_height == 0)
		return;

	plugin->priv->iface->frame_property_changed(plugin, frame);

	if (!libdecor_frame_has_capability(frame, LIBDECOR_ACTION_RESIZE)) {
		frame->priv->interactive_limits = frame->priv->state.content_limits;
		/* set fixed window size */
		libdecor_frame_set_min_content_size(frame,
						    frame->priv->content_width,
						    frame->priv->content_height);
		libdecor_frame_set_max_content_size(frame,
						    frame->priv->content_width,
						    frame->priv->content_height);
	} else {
		/* restore old limits */
		frame->priv->state.content_limits = frame->priv->interactive_limits;
	}

	state = libdecor_state_new(frame->priv->content_width,
				   frame->priv->content_height);
	libdecor_frame_commit(frame, state, NULL);
	libdecor_state_free(state);

	libdecor_frame_toplevel_commit(frame);
}

LIBDECOR_EXPORT void
libdecor_frame_set_capabilities(struct libdecor_frame *frame,
				enum libdecor_capabilities capabilities)
{
	const enum libdecor_capabilities old_capabilities =
			frame->priv->capabilities;

	frame->priv->capabilities |= capabilities;

	notify_on_capability_change(frame, old_capabilities);
}

LIBDECOR_EXPORT void
libdecor_frame_unset_capabilities(struct libdecor_frame *frame,
				  enum libdecor_capabilities capabilities)
{
	const enum libdecor_capabilities old_capabilities =
			frame->priv->capabilities;

	frame->priv->capabilities &= ~capabilities;

	notify_on_capability_change(frame, old_capabilities);
}

LIBDECOR_EXPORT bool
libdecor_frame_has_capability(struct libdecor_frame *frame,
			      enum libdecor_capabilities capability)
{
	return frame->priv->capabilities & capability;
}

LIBDECOR_EXPORT void
libdecor_frame_popup_grab(struct libdecor_frame *frame,
			  const char *seat_name)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor *context = frame_priv->context;
	struct libdecor_plugin *plugin = context->plugin;

	plugin->priv->iface->frame_popup_grab(plugin, frame, seat_name);
}

LIBDECOR_EXPORT void
libdecor_frame_popup_ungrab(struct libdecor_frame *frame,
			    const char *seat_name)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor *context = frame_priv->context;
	struct libdecor_plugin *plugin = context->plugin;

	plugin->priv->iface->frame_popup_ungrab(plugin, frame, seat_name);
}

LIBDECOR_EXPORT void
libdecor_frame_dismiss_popup(struct libdecor_frame *frame,
			     const char *seat_name)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->iface->dismiss_popup(frame, seat_name, frame_priv->user_data);
}

LIBDECOR_EXPORT void
libdecor_frame_show_window_menu(struct libdecor_frame *frame,
				struct wl_seat *wl_seat,
				uint32_t serial,
				int x,
				int y)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	if (!frame_priv->xdg_toplevel) {
		fprintf(stderr, "Can't show window menu before being mapped\n");
		return;
	}

	xdg_toplevel_show_window_menu(frame_priv->xdg_toplevel,
				      wl_seat, serial,
				      x, y);
}

LIBDECOR_EXPORT void
libdecor_frame_translate_coordinate(struct libdecor_frame *frame,
				    int content_x,
				    int content_y,
				    int *frame_x,
				    int *frame_y)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor *context = frame_priv->context;
	struct libdecor_plugin *plugin = context->plugin;

	*frame_x = content_x;
	*frame_y = content_y;

	if (frame_has_visible_client_side_decoration(frame) &&
	    plugin->priv->iface->frame_get_border_size) {
		int left, top;
		plugin->priv->iface->frame_get_border_size(plugin, frame, NULL,
							   &left, NULL, &top, NULL);
		*frame_x += left;
		*frame_y += top;
	}
}

LIBDECOR_EXPORT void
libdecor_frame_set_min_content_size(struct libdecor_frame *frame,
				    int content_width,
				    int content_height)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->state.content_limits.min_width = content_width;
	frame_priv->state.content_limits.min_height = content_height;
}

LIBDECOR_EXPORT void
libdecor_frame_set_max_content_size(struct libdecor_frame *frame,
				    int content_width,
				    int content_height)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->state.content_limits.max_width = content_width;
	frame_priv->state.content_limits.max_height = content_height;
}

LIBDECOR_EXPORT void
libdecor_frame_get_min_content_size(const struct libdecor_frame *frame,
				    int *content_width,
				    int *content_height)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	*content_width = frame_priv->state.content_limits.min_width;
	*content_height = frame_priv->state.content_limits.min_height;
}

LIBDECOR_EXPORT void
libdecor_frame_get_max_content_size(const struct libdecor_frame *frame,
				    int *content_width,
				    int *content_height)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	*content_width = frame_priv->state.content_limits.max_width;
	*content_height = frame_priv->state.content_limits.max_height;
}

LIBDECOR_EXPORT enum libdecor_capabilities
libdecor_frame_get_capabilities(const struct libdecor_frame *frame)
{
	return frame->priv->capabilities;
}

enum xdg_toplevel_resize_edge
edge_to_xdg_edge(enum libdecor_resize_edge edge)
{
	switch (edge) {
	case LIBDECOR_RESIZE_EDGE_NONE:
		return XDG_TOPLEVEL_RESIZE_EDGE_NONE;
	case LIBDECOR_RESIZE_EDGE_TOP:
		return XDG_TOPLEVEL_RESIZE_EDGE_TOP;
	case LIBDECOR_RESIZE_EDGE_BOTTOM:
		return XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM;
	case LIBDECOR_RESIZE_EDGE_LEFT:
		return XDG_TOPLEVEL_RESIZE_EDGE_LEFT;
	case LIBDECOR_RESIZE_EDGE_TOP_LEFT:
		return XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT;
	case LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT:
		return XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT;
	case LIBDECOR_RESIZE_EDGE_RIGHT:
		return XDG_TOPLEVEL_RESIZE_EDGE_RIGHT;
	case LIBDECOR_RESIZE_EDGE_TOP_RIGHT:
		return XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT;
	case LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT:
		return XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT;
	}

	abort();
}

LIBDECOR_EXPORT void
libdecor_frame_resize(struct libdecor_frame *frame,
		      struct wl_seat *wl_seat,
		      uint32_t serial,
		      enum libdecor_resize_edge edge)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	enum xdg_toplevel_resize_edge xdg_edge;

	xdg_edge = edge_to_xdg_edge(edge);
	xdg_toplevel_resize(frame_priv->xdg_toplevel,
			    wl_seat, serial, xdg_edge);
}

LIBDECOR_EXPORT void
libdecor_frame_move(struct libdecor_frame *frame,
		    struct wl_seat *wl_seat,
		    uint32_t serial)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	xdg_toplevel_move(frame_priv->xdg_toplevel, wl_seat, serial);
}

LIBDECOR_EXPORT void
libdecor_frame_set_minimized(struct libdecor_frame *frame)
{
	xdg_toplevel_set_minimized(frame->priv->xdg_toplevel);
}

LIBDECOR_EXPORT void
libdecor_frame_set_maximized(struct libdecor_frame *frame)
{
	xdg_toplevel_set_maximized(frame->priv->xdg_toplevel);
}

LIBDECOR_EXPORT void
libdecor_frame_unset_maximized(struct libdecor_frame *frame)
{
	xdg_toplevel_unset_maximized(frame->priv->xdg_toplevel);
}

LIBDECOR_EXPORT void
libdecor_frame_set_fullscreen(struct libdecor_frame *frame,
			      struct wl_output *output)
{
	xdg_toplevel_set_fullscreen(frame->priv->xdg_toplevel, output);
}

LIBDECOR_EXPORT void
libdecor_frame_unset_fullscreen(struct libdecor_frame *frame)
{
	xdg_toplevel_unset_fullscreen(frame->priv->xdg_toplevel);
}

LIBDECOR_EXPORT bool
libdecor_frame_is_floating(struct libdecor_frame *frame)
{
	return state_is_floating(frame->priv->window_state);
}

LIBDECOR_EXPORT void
libdecor_frame_close(struct libdecor_frame *frame)
{
	xdg_toplevel_close(frame, frame->priv->xdg_toplevel);
}

bool
valid_limits(struct libdecor_frame_private *frame_priv)
{
	if (frame_priv->state.content_limits.min_width > 0 &&
	    frame_priv->state.content_limits.max_width > 0 &&
	    frame_priv->state.content_limits.min_width >
	    frame_priv->state.content_limits.max_width)
		return false;

	if (frame_priv->state.content_limits.min_height > 0 &&
	    frame_priv->state.content_limits.max_height > 0 &&
	    frame_priv->state.content_limits.min_height >
	    frame_priv->state.content_limits.max_height)
		return false;

	return true;
}

static void
libdecor_frame_apply_limits(struct libdecor_frame *frame,
			    enum libdecor_window_state window_state)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	if (!valid_limits(frame_priv)) {
		libdecor_notify_plugin_error(
			frame_priv->context,
			LIBDECOR_ERROR_INVALID_FRAME_CONFIGURATION,
			"minimum size (%i,%i) must be smaller than maximum size (%i,%i)",
			frame_priv->state.content_limits.min_width,
			frame_priv->state.content_limits.min_height,
			frame_priv->state.content_limits.max_width,
			frame_priv->state.content_limits.max_height);
	}

	/* If the frame is configured as non-resizable before the first
	 * configure event is received, we have to manually set the min/max
	 * limits with the configured content size afterwards. */
	if (!libdecor_frame_has_capability(frame, LIBDECOR_ACTION_RESIZE)) {
		frame_priv->state.content_limits.min_width =
				frame_priv->content_width;
		frame_priv->state.content_limits.max_width =
				frame_priv->content_width;

		frame_priv->state.content_limits.min_height =
				frame_priv->content_height;
		frame_priv->state.content_limits.max_height =
				frame_priv->content_height;
	}

	if (frame_priv->state.content_limits.min_width > 0 &&
	    frame_priv->state.content_limits.min_height > 0) {
		struct libdecor_state state_min;
		int win_min_width, win_min_height;

		state_min.content_width = frame_priv->state.content_limits.min_width;
		state_min.content_height = frame_priv->state.content_limits.min_height;
		state_min.window_state = window_state;

		frame_get_window_size_for(frame, &state_min,
					  &win_min_width, &win_min_height);
		xdg_toplevel_set_min_size(frame_priv->xdg_toplevel,
					  win_min_width, win_min_height);
	} else {
		xdg_toplevel_set_min_size(frame_priv->xdg_toplevel, 0, 0);
	}

	if (frame_priv->state.content_limits.max_width > 0 &&
	    frame_priv->state.content_limits.max_height > 0) {
		struct libdecor_state state_max;
		int win_max_width, win_max_height;

		state_max.content_width = frame_priv->state.content_limits.max_width;
		state_max.content_height = frame_priv->state.content_limits.max_height;
		state_max.window_state = window_state;

		frame_get_window_size_for(frame, &state_max,
					  &win_max_width, &win_max_height);
		xdg_toplevel_set_max_size(frame_priv->xdg_toplevel,
					  win_max_width, win_max_height);
	} else {
		xdg_toplevel_set_max_size(frame_priv->xdg_toplevel, 0, 0);
	}
}

static void
libdecor_frame_apply_state(struct libdecor_frame *frame,
			   struct libdecor_state *state)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->content_width = state->content_width;
	frame_priv->content_height = state->content_height;

	libdecor_frame_apply_limits(frame, state->window_state);
}

LIBDECOR_EXPORT void
libdecor_frame_toplevel_commit(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->iface->commit(frame, frame_priv->user_data);
}

LIBDECOR_EXPORT void
libdecor_frame_commit(struct libdecor_frame *frame,
		      struct libdecor_state *state,
		      struct libdecor_configuration *configuration)
{
	struct libdecor_frame_private *frame_priv = frame->priv;
	struct libdecor *context = frame_priv->context;
	struct libdecor_plugin *plugin = context->plugin;

	if (configuration && configuration->has_window_state) {
		frame_priv->window_state = configuration->window_state;
		state->window_state = configuration->window_state;
	} else {
		state->window_state = frame_priv->window_state;
	}

	libdecor_frame_apply_state(frame, state);

	/* switch between decoration modes */
	if (frame_has_visible_client_side_decoration(frame)) {
		plugin->priv->iface->frame_commit(plugin, frame, state,
						  configuration);
	} else {
		plugin->priv->iface->frame_free(plugin, frame);
	}

	frame_set_window_geometry(frame,
				  frame_priv->content_width,
				  frame_priv->content_height);

	if (configuration) {
		xdg_surface_ack_configure(frame_priv->xdg_surface,
					  configuration->serial);
	}
}

static void
do_map(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	frame_priv->pending_map = false;
	wl_surface_commit(frame_priv->wl_surface);
}

LIBDECOR_EXPORT void
libdecor_frame_map(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	if (!frame_priv->xdg_surface) {
		frame_priv->pending_map = true;
		return;
	}

	do_map(frame);
}

LIBDECOR_EXPORT struct wl_surface *
libdecor_frame_get_wl_surface(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	return frame_priv->wl_surface;
}

LIBDECOR_EXPORT struct xdg_surface *
libdecor_frame_get_xdg_surface(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	return frame_priv->xdg_surface;
}

LIBDECOR_EXPORT struct xdg_toplevel *
libdecor_frame_get_xdg_toplevel(struct libdecor_frame *frame)
{
	return frame->priv->xdg_toplevel;
}

LIBDECOR_EXPORT int
libdecor_frame_get_content_width(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	return frame_priv->content_width;
}

LIBDECOR_EXPORT int
libdecor_frame_get_content_height(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	return frame_priv->content_height;
}

LIBDECOR_EXPORT enum libdecor_window_state
libdecor_frame_get_window_state(struct libdecor_frame *frame)
{
	struct libdecor_frame_private *frame_priv = frame->priv;

	return frame_priv->window_state;
}

LIBDECOR_EXPORT int
libdecor_plugin_init(struct libdecor_plugin *plugin,
		     struct libdecor *context,
		     struct libdecor_plugin_interface *iface)
{
	plugin->priv = zalloc(sizeof (struct libdecor_plugin_private));
	if (!plugin->priv)
		return -1;

	plugin->priv->iface = iface;

	return 0;
}

LIBDECOR_EXPORT void
libdecor_plugin_release(struct libdecor_plugin *plugin)
{
	free(plugin->priv);
}

static void
xdg_wm_base_ping(void *user_data,
		 struct xdg_wm_base *xdg_wm_base,
		 uint32_t serial)
{
	xdg_wm_base_pong(xdg_wm_base, serial);
}

static const struct xdg_wm_base_listener xdg_wm_base_listener = {
	xdg_wm_base_ping,
};

static void
init_xdg_wm_base(struct libdecor *context,
		 uint32_t id,
		 uint32_t version)
{
	context->xdg_wm_base = wl_registry_bind(context->wl_registry,
						id,
						&xdg_wm_base_interface,
						MIN(version,XDG_WM_BASE_VER));
	xdg_wm_base_add_listener(context->xdg_wm_base,
				 &xdg_wm_base_listener,
				 context);
}

static void
registry_handle_global(void *user_data,
		       struct wl_registry *wl_registry,
		       uint32_t id,
		       const char *interface,
		       uint32_t version)
{
	struct libdecor *context = user_data;

	if (!strcmp(interface, xdg_wm_base_interface.name)) {
		init_xdg_wm_base(context, id, version);
	} else if (!strcmp(interface, zxdg_decoration_manager_v1_interface.name)) {
		const char *force_csd = getenv("LIBDECOR_FORCE_CSD");

		if (force_csd && atoi(force_csd)) {
			return;
		}

		context->decoration_manager = wl_registry_bind(
				context->wl_registry, id,
				&zxdg_decoration_manager_v1_interface,
				MIN(version,2));
	}
}

static void
registry_handle_global_remove(void *user_data,
			      struct wl_registry *wl_registry,
			      uint32_t name)
{
}

static const struct wl_registry_listener registry_listener = {
	registry_handle_global,
	registry_handle_global_remove
};

static bool
is_compositor_compatible(struct libdecor *context)
{
	if (!context->xdg_wm_base)
		return false;

	return true;
}

static void
notify_error(struct libdecor *context,
	     enum libdecor_error error,
	     const char *message)
{
	context->has_error = true;
	context->iface->error(context, error, message);
	context->plugin->priv->iface->destroy(context->plugin);
}

static void
finish_init(struct libdecor *context)
{
	struct libdecor_frame *frame;

	wl_list_for_each(frame, &context->frames, link)
		init_shell_surface(frame);
}

static void
init_wl_display_callback(void *user_data,
			 struct wl_callback *callback,
			 uint32_t time)
{
	struct libdecor *context = user_data;

	context->init_done = true;

	wl_callback_destroy(callback);
	context->init_callback = NULL;

	if (!is_compositor_compatible(context)) {
		notify_error(context,
			     LIBDECOR_ERROR_COMPOSITOR_INCOMPATIBLE,
			     "Compositor is missing required interfaces");
	}

	if (context->plugin_ready) {
		finish_init(context);
	}
}

static const struct wl_callback_listener init_wl_display_callback_listener = {
	init_wl_display_callback
};

struct plugin_loader {
	struct wl_list link;
	void *lib;
	const struct libdecor_plugin_description *description;
	int priority;
	char *name;
};

static int
calculate_priority(const struct libdecor_plugin_description *plugin_description)
{
	const char *current_desktop;
	int i;

	if (!plugin_description->priorities)
		return -1;

	current_desktop = getenv("XDG_CURRENT_DESKTOP");

	i = 0;
	while (true) {
		struct libdecor_plugin_priority priority =
			plugin_description->priorities[i];

		i++;

		if (priority.desktop) {
			char *tokens;
			char *saveptr;
			char *token;

			if (!current_desktop)
				continue;

			tokens = strdup(current_desktop);
			token = strtok_r(tokens, ":", &saveptr);
			while (token) {
				if (strcmp(priority.desktop, token) == 0) {
					free(tokens);
					return priority.priority;
				}
				token = strtok_r(NULL, ":", &saveptr);
			}
			free(tokens);
		} else {
			return priority.priority;
		}
	}

	return -1;
}

static bool
check_symbol_conflicts(const struct libdecor_plugin_description *plugin_description, void *lib)
{
	char * const *symbol;

	symbol = plugin_description->conflicting_symbols;
	while (*symbol) {
		dlerror();
		void *sym = dlsym(RTLD_DEFAULT, *symbol);
		if (!dlerror()) {
			void *libsym = dlsym(lib, *symbol);
			if (!dlerror() && libsym != sym) {
				fprintf(stderr, "Plugin \"%s\" uses conflicting symbol \"%s\".\n",
					plugin_description->description, *symbol);
				return false;
			}
		}

		symbol++;
	}

	return true;
}

static struct plugin_loader *
load_plugin_loader(struct libdecor *context,
		   const char *path,
		   const char *name)
{
	char *ext;
	char *filename;
	void *lib;
	const struct libdecor_plugin_description *plugin_description;
	int priority;
	struct plugin_loader *plugin_loader;

	ext = strrchr(name, '.');
	if (ext == NULL || strcmp(ext, ".so") != 0)
		return NULL;

	if (asprintf(&filename, "%s/%s", path, name) == -1)
		return NULL;

	lib = dlopen(filename, RTLD_NOW | RTLD_LAZY);
	free(filename);
	if (!lib) {
		fprintf(stderr, "Failed to load plugin: '%s'\n", dlerror());
		return NULL;
	}

	plugin_description = dlsym(lib, "libdecor_plugin_description");
	if (!plugin_description) {
		fprintf(stderr,
			"Failed to load plugin '%s': no plugin description symbol\n",
			name);
		dlclose(lib);
		return NULL;
	}

	if (plugin_description->api_version != LIBDECOR_PLUGIN_API_VERSION) {
		fprintf(stderr,
			"Plugin '%s' found, but it's incompatible "
			"(expected API version %d, but got %d)\n",
			name,
			LIBDECOR_PLUGIN_API_VERSION,
			plugin_description->api_version);
		dlclose(lib);
		return NULL;
	}

	if (!(plugin_description->capabilities & LIBDECOR_PLUGIN_CAPABILITY_BASE)) {
		dlclose(lib);
		return NULL;
	}

	if (!check_symbol_conflicts(plugin_description, lib)) {
		dlclose(lib);
		return NULL;
	}

	priority = calculate_priority(plugin_description);
	if (priority == -1) {
		fprintf(stderr,
			"Plugin '%s' found, but has an invalid description\n",
			name);
		dlclose(lib);
		return NULL;
	}

	plugin_loader = zalloc(sizeof *plugin_loader);
	plugin_loader->description = plugin_description;
	plugin_loader->lib = lib;
	plugin_loader->priority = priority;
	plugin_loader->name = strdup(name);

	return plugin_loader;
}

static bool
plugin_loader_higher_priority(struct plugin_loader *plugin_loader,
			      struct plugin_loader *best_plugin_loader)
{
	return plugin_loader->priority > best_plugin_loader->priority;
}

static int
init_plugins(struct libdecor *context)
{
	const char *plugin_dir_env;
	char *all_plugin_dirs;
	char *plugin_dir;
	char *saveptr;
	DIR *dir;
	struct wl_list plugin_loaders;
	struct plugin_loader *plugin_loader, *tmp;
	struct plugin_loader *best_plugin_loader;
	struct libdecor_plugin *plugin;

	plugin_dir_env = getenv("LIBDECOR_PLUGIN_DIR");
	if (!plugin_dir_env) {
		plugin_dir_env = LIBDECOR_PLUGIN_DIR;
	}
	all_plugin_dirs = strdup(plugin_dir_env);

	wl_list_init(&plugin_loaders);
	plugin_dir = strtok_r(all_plugin_dirs, ":", &saveptr);
	while (plugin_dir) {
		dir = opendir(plugin_dir);
		if (!dir) {
			fprintf(stderr, "Couldn't open plugin directory: %s\n",
				strerror(errno));
		} else {

			while (true) {
				struct dirent *de;

				de = readdir(dir);
				if (!de)
					break;

				plugin_loader = load_plugin_loader(context,
								   plugin_dir,
								   de->d_name);
				if (!plugin_loader)
					continue;

				wl_list_insert(plugin_loaders.prev, &plugin_loader->link);
			}

			closedir(dir);
		}
		plugin_dir = strtok_r(NULL, ":", &saveptr);
	}
	free(all_plugin_dirs);

retry_next:
	best_plugin_loader = NULL;
	wl_list_for_each(plugin_loader, &plugin_loaders, link) {
		if (!best_plugin_loader) {
			best_plugin_loader = plugin_loader;
			continue;
		}

		if (plugin_loader_higher_priority(plugin_loader,
						  best_plugin_loader))
			best_plugin_loader = plugin_loader;
	}

	if (!best_plugin_loader)
		return -1;

	plugin_loader = best_plugin_loader;
	plugin = plugin_loader->description->constructor(context);
	if (!plugin) {
		fprintf(stderr,
			"Failed to load plugin '%s': failed to init\n",
			plugin_loader->name);
		dlclose(plugin_loader->lib);
		wl_list_remove(&plugin_loader->link);
		free(plugin_loader->name);
		free(plugin_loader);
		goto retry_next;
	}

	context->plugin = plugin;

	wl_list_remove(&plugin_loader->link);
	free(plugin_loader->name);
	free(plugin_loader);

	wl_list_for_each_safe(plugin_loader, tmp, &plugin_loaders, link) {
		dlclose(plugin_loader->lib);
		free(plugin_loader->name);
		free(plugin_loader);
	}

	return 0;
}

LIBDECOR_EXPORT int
libdecor_get_fd(struct libdecor *context)
{
	struct libdecor_plugin *plugin = context->plugin;

	return plugin->priv->iface->get_fd(plugin);
}

LIBDECOR_EXPORT int
libdecor_dispatch(struct libdecor *context,
		  int timeout)
{
	struct libdecor_plugin *plugin = context->plugin;

	return plugin->priv->iface->dispatch(plugin, timeout);
}

LIBDECOR_EXPORT struct wl_display *
libdecor_get_wl_display(struct libdecor *context)
{
	return context->wl_display;
}

LIBDECOR_EXPORT void
libdecor_notify_plugin_ready(struct libdecor *context)
{
	context->plugin_ready = true;

	if (context->init_done)
		finish_init(context);
}

LIBDECOR_EXPORT void
libdecor_notify_plugin_error(struct libdecor *context,
			     enum libdecor_error error,
			     const char *__restrict fmt,
			     ...)
{
	char *msg = NULL;
	int nbytes = 0;
	va_list argp;

	if (context->has_error)
		return;

	va_start(argp, fmt);
	nbytes = vasprintf(&msg, fmt, argp);
	va_end(argp);

	if (nbytes>0)
		notify_error(context, error, msg);

	if (msg)
		free(msg);
}

LIBDECOR_EXPORT void
libdecor_unref(struct libdecor *context)
{
	context->ref_count--;
	if (context->ref_count == 0) {
		if (context->plugin)
			context->plugin->priv->iface->destroy(context->plugin);
		if (context->init_callback)
			wl_callback_destroy(context->init_callback);
		wl_registry_destroy(context->wl_registry);
		if (context->xdg_wm_base)
			xdg_wm_base_destroy(context->xdg_wm_base);
		if (context->decoration_manager)
			zxdg_decoration_manager_v1_destroy(
						context->decoration_manager);
		free(context);
	}
}

LIBDECOR_EXPORT struct libdecor *
libdecor_new(struct wl_display *wl_display,
	     struct libdecor_interface *iface)
{
	struct libdecor *context;

	context = zalloc(sizeof *context);

	context->ref_count = 1;
	context->iface = iface;
	context->wl_display = wl_display;
	context->wl_registry = wl_display_get_registry(wl_display);
	wl_registry_add_listener(context->wl_registry,
				 &registry_listener,
				 context);
	context->init_callback = wl_display_sync(context->wl_display);
	wl_callback_add_listener(context->init_callback,
				 &init_wl_display_callback_listener,
				 context);

	wl_list_init(&context->frames);

	if (init_plugins(context) != 0) {
		fprintf(stderr,
			"No plugins found, falling back on no decorations\n");
		context->plugin = libdecor_fallback_plugin_new(context);
	}

	wl_display_flush(wl_display);

	return context;
}