File: labnag.c

package info (click to toggle)
labwc 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,980 kB
  • sloc: ansic: 34,416; perl: 5,836; xml: 875; sh: 162; python: 131; makefile: 12
file content (1652 lines) | stat: -rw-r--r-- 46,024 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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Based on https://github.com/swaywm/sway/tree/master/swaynag
 *
 * Copyright (C) 2016-2017 Drew DeVault
 * Copyright (C) 2025 Johan Malm
 */
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <cairo.h>
#include <ctype.h>
#include <getopt.h>
#include <glib.h>
#include <pango/pangocairo.h>
#include <poll.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __FreeBSD__
#include <sys/event.h> /* For signalfd() */
#endif
#include <sys/signalfd.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <wayland-cursor.h>
#include <wlr/util/log.h>
#include "action-prompt-codes.h"
#include "pool-buffer.h"
#include "cursor-shape-v1-client-protocol.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"

#define LABNAG_MAX_HEIGHT 500

struct conf {
	PangoFontDescription *font_description;
	char *output;
	uint32_t anchors;
	int32_t layer; /* enum zwlr_layer_shell_v1_layer or -1 if unset */

	/* Colors */
	uint32_t button_text;
	uint32_t button_background;
	uint32_t details_background;
	uint32_t background;
	uint32_t text;
	uint32_t button_border;
	uint32_t border_bottom;

	/* Sizing */
	ssize_t bar_border_thickness;
	ssize_t message_padding;
	ssize_t details_border_thickness;
	ssize_t button_border_thickness;
	ssize_t button_gap;
	ssize_t button_gap_close;
	ssize_t button_margin_right;
	ssize_t button_padding;
};

struct pointer {
	struct wl_pointer *pointer;
	uint32_t serial;
	struct wl_cursor_theme *cursor_theme;
	struct wl_cursor_image *cursor_image;
	struct wl_surface *cursor_surface;
	int x;
	int y;
};

struct seat {
	struct wl_seat *wl_seat;
	uint32_t wl_name;
	struct nag *nag;
	struct pointer pointer;
	struct wl_list link; /* nag.seats */
};

struct output {
	char *name;
	struct wl_output *wl_output;
	uint32_t wl_name;
	uint32_t scale;
	struct nag *nag;
	struct wl_list link; /* nag.outputs */
};

struct button {
	char *text;
	char *action;
	int x;
	int y;
	int width;
	int height;
	bool expand;
	bool dismiss;
	struct wl_list link;
};

enum {
	FD_WAYLAND,
	FD_TIMER,
	FD_SIGNAL,

	NR_FDS,
};

struct nag {
	bool run_display;

	struct wl_display *display;
	struct wl_compositor *compositor;
	struct wl_seat *seat;
	struct wl_shm *shm;
	struct wl_list outputs;
	struct wl_list seats;
	struct output *output;
	struct zwlr_layer_shell_v1 *layer_shell;
	struct zwlr_layer_surface_v1 *layer_surface;
	struct wp_cursor_shape_manager_v1 *cursor_shape_manager;
	struct wl_surface *surface;

	uint32_t width;
	uint32_t height;
	int32_t scale;
	struct pool_buffer buffers[2];
	struct pool_buffer *current_buffer;

	struct conf *conf;
	char *message;
	struct wl_list buttons;
	struct pollfd pollfds[NR_FDS];

	struct {
		bool visible;
		char *message;
		char *details_text;
		int close_timeout;
		bool use_exclusive_zone;

		int x;
		int y;
		int width;
		int height;

		int offset;
		int visible_lines;
		int total_lines;
		struct button *button_details;
		struct button button_up;
		struct button button_down;
	} details;
};

static int exit_status = LAB_EXIT_FAILURE;

static void close_pollfd(struct pollfd *pollfd);

static PangoLayout *
get_pango_layout(cairo_t *cairo, const PangoFontDescription *desc,
		const char *text, double scale, bool markup)
{
	PangoLayout *layout = pango_cairo_create_layout(cairo);
	pango_context_set_round_glyph_positions(pango_layout_get_context(layout), false);

	PangoAttrList *attrs;
	if (markup) {
		char *buf;
		GError *error = NULL;
		if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) {
			pango_layout_set_text(layout, buf, -1);
			free(buf);
		} else {
			wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s",
				text, error->message);
			g_error_free(error);
			markup = false; /* fallback to plain text */
		}
	}
	if (!markup) {
		attrs = pango_attr_list_new();
		pango_layout_set_text(layout, text, -1);
	}

	pango_attr_list_insert(attrs, pango_attr_scale_new(scale));
	pango_layout_set_font_description(layout, desc);
	pango_layout_set_single_paragraph_mode(layout, 1);
	pango_layout_set_attributes(layout, attrs);
	pango_attr_list_unref(attrs);
	return layout;
}

static void
get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width, int *height,
		int *baseline, double scale, bool markup, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	gchar *buf = g_strdup_vprintf(fmt, args);
	va_end(args);
	if (!buf) {
		return;
	}

	PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
	pango_cairo_update_layout(cairo, layout);
	pango_layout_get_pixel_size(layout, width, height);
	if (baseline) {
		*baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
	}
	g_object_unref(layout);

	g_free(buf);
}

static void
render_text(cairo_t *cairo, const PangoFontDescription *desc, double scale,
		bool markup, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	gchar *buf = g_strdup_vprintf(fmt, args);
	va_end(args);
	if (!buf) {
		return;
	}

	PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
	cairo_font_options_t *fo = cairo_font_options_create();
	cairo_get_font_options(cairo, fo);
	pango_cairo_context_set_font_options(pango_layout_get_context(layout), fo);
	cairo_font_options_destroy(fo);
	pango_cairo_update_layout(cairo, layout);
	pango_cairo_show_layout(cairo, layout);
	g_object_unref(layout);

	g_free(buf);
}

static void
cairo_set_source_u32(cairo_t *cairo, uint32_t color)
{
	cairo_set_source_rgba(cairo,
			(color >> (3*8) & 0xFF) / 255.0,
			(color >> (2*8) & 0xFF) / 255.0,
			(color >> (1*8) & 0xFF) / 255.0,
			(color >> (0*8) & 0xFF) / 255.0);
}

static uint32_t
render_message(cairo_t *cairo, struct nag *nag)
{
	int text_width, text_height;
	get_text_size(cairo, nag->conf->font_description, &text_width,
		&text_height, NULL, 1, true, "%s", nag->message);

	int padding = nag->conf->message_padding;

	uint32_t ideal_height = text_height + padding * 2;
	uint32_t ideal_surface_height = ideal_height;
	if (nag->height < ideal_surface_height) {
		return ideal_surface_height;
	}

	cairo_set_source_u32(cairo, nag->conf->text);
	cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2);
	render_text(cairo, nag->conf->font_description, 1, false,
			"%s", nag->message);

	return ideal_surface_height;
}

static void
render_details_scroll_button(cairo_t *cairo, struct nag *nag,
		struct button *button)
{
	int text_width, text_height;
	get_text_size(cairo, nag->conf->font_description, &text_width,
		&text_height, NULL, 1, true, "%s", button->text);

	int border = nag->conf->button_border_thickness;
	int padding = nag->conf->button_padding;

	cairo_set_source_u32(cairo, nag->conf->details_background);
	cairo_rectangle(cairo, button->x, button->y,
			button->width, button->height);
	cairo_fill(cairo);

	cairo_set_source_u32(cairo, nag->conf->button_background);
	cairo_rectangle(cairo, button->x + border, button->y + border,
			button->width - (border * 2),
			button->height - (border * 2));
	cairo_fill(cairo);

	cairo_set_source_u32(cairo, nag->conf->button_text);
	cairo_move_to(cairo, button->x + border + padding,
			button->y + border + (button->height - text_height) / 2);
	render_text(cairo, nag->conf->font_description, 1, true,
			"%s", button->text);
}

static int
get_detailed_scroll_button_width(cairo_t *cairo, struct nag *nag)
{
	int up_width, down_width, temp_height;
	get_text_size(cairo, nag->conf->font_description, &up_width, &temp_height,
		NULL, 1, true, "%s", nag->details.button_up.text);
	get_text_size(cairo, nag->conf->font_description, &down_width, &temp_height,
		NULL, 1, true, "%s", nag->details.button_down.text);

	int text_width =  up_width > down_width ? up_width : down_width;
	int border = nag->conf->button_border_thickness;
	int padding = nag->conf->button_padding;

	return text_width + border * 2 + padding * 2;
}

static uint32_t
render_detailed(cairo_t *cairo, struct nag *nag, uint32_t y)
{
	uint32_t width = nag->width;

	int border = nag->conf->details_border_thickness;
	int padding = nag->conf->message_padding;
	int decor = padding + border;

	nag->details.x = decor;
	nag->details.y = y + decor;
	nag->details.width = width - decor * 2;

	PangoLayout *layout = get_pango_layout(cairo, nag->conf->font_description,
			nag->details.message, 1, false);
	pango_layout_set_width(layout,
			(nag->details.width - padding * 2) * PANGO_SCALE);
	pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
	pango_layout_set_single_paragraph_mode(layout, false);
	pango_cairo_update_layout(cairo, layout);
	nag->details.total_lines = pango_layout_get_line_count(layout);

	PangoLayoutLine *line;
	line = pango_layout_get_line_readonly(layout, nag->details.offset);
	gint offset = line->start_index;
	const char *text = pango_layout_get_text(layout);
	pango_layout_set_text(layout, text + offset, strlen(text) - offset);

	int text_width, text_height;
	pango_cairo_update_layout(cairo, layout);
	pango_layout_get_pixel_size(layout, &text_width, &text_height);

	bool show_buttons = nag->details.offset > 0;
	int button_width = get_detailed_scroll_button_width(cairo, nag);
	if (show_buttons) {
		nag->details.width -= button_width;
		pango_layout_set_width(layout,
				(nag->details.width - padding * 2) * PANGO_SCALE);
	}

	uint32_t ideal_height;
	do {
		ideal_height = nag->details.y + text_height + decor + padding * 2;
		if (ideal_height > LABNAG_MAX_HEIGHT) {
			ideal_height = LABNAG_MAX_HEIGHT;

			if (!show_buttons) {
				show_buttons = true;
				nag->details.width -= button_width;
				pango_layout_set_width(layout,
					(nag->details.width - padding * 2) * PANGO_SCALE);
			}
		}

		nag->details.height = ideal_height - nag->details.y - decor;
		pango_layout_set_height(layout,
				(nag->details.height - padding * 2) * PANGO_SCALE);
		pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
		pango_cairo_update_layout(cairo, layout);
		pango_layout_get_pixel_size(layout, &text_width, &text_height);
	} while (text_height != (nag->details.height - padding * 2));

	nag->details.visible_lines = pango_layout_get_line_count(layout);

	if (show_buttons) {
		nag->details.button_up.x = nag->details.x + nag->details.width;
		nag->details.button_up.y = nag->details.y;
		nag->details.button_up.width = button_width;
		nag->details.button_up.height = nag->details.height / 2;
		render_details_scroll_button(cairo, nag, &nag->details.button_up);

		nag->details.button_down.x = nag->details.x + nag->details.width;
		nag->details.button_down.y =
			nag->details.button_up.y + nag->details.button_up.height;
		nag->details.button_down.width = button_width;
		nag->details.button_down.height = nag->details.height / 2;
		render_details_scroll_button(cairo, nag, &nag->details.button_down);
	}

	cairo_set_source_u32(cairo, nag->conf->details_background);
	cairo_rectangle(cairo, nag->details.x, nag->details.y,
			nag->details.width, nag->details.height);
	cairo_fill(cairo);

	cairo_move_to(cairo, nag->details.x + padding, nag->details.y + padding);
	cairo_set_source_u32(cairo, nag->conf->text);
	pango_cairo_show_layout(cairo, layout);
	g_object_unref(layout);

	return ideal_height;
}

static uint32_t
render_button(cairo_t *cairo, struct nag *nag, struct button *button, int *x)
{
	int text_width, text_height;
	get_text_size(cairo, nag->conf->font_description, &text_width,
		&text_height, NULL, 1, true, "%s", button->text);

	int border = nag->conf->button_border_thickness;
	int padding = nag->conf->button_padding;

	uint32_t ideal_height = text_height + padding * 2 + border * 2;
	uint32_t ideal_surface_height = ideal_height;
	if (nag->height < ideal_surface_height) {
		return ideal_surface_height;
	}

	button->x = *x - border - text_width - padding * 2 + 1;
	button->y = (int)(ideal_height - text_height) / 2 - padding + 1;
	button->width = text_width + padding * 2;
	button->height = text_height + padding * 2;

	cairo_set_source_u32(cairo, nag->conf->button_border);
	cairo_rectangle(cairo, button->x - border, button->y - border,
			button->width + border * 2, button->height + border * 2);
	cairo_fill(cairo);

	cairo_set_source_u32(cairo, nag->conf->button_background);
	cairo_rectangle(cairo, button->x, button->y,
			button->width, button->height);
	cairo_fill(cairo);

	cairo_set_source_u32(cairo, nag->conf->button_text);
	cairo_move_to(cairo, button->x + padding, button->y + padding);
	render_text(cairo, nag->conf->font_description, 1, true,
			"%s", button->text);

	*x = button->x - border;

	return ideal_surface_height;
}

static uint32_t
render_to_cairo(cairo_t *cairo, struct nag *nag)
{
	uint32_t max_height = 0;

	cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
	cairo_set_source_u32(cairo, nag->conf->background);
	cairo_paint(cairo);

	uint32_t h = render_message(cairo, nag);
	max_height = h > max_height ? h : max_height;

	int x = nag->width - nag->conf->button_margin_right;
	x -= nag->conf->button_gap_close;

	struct button *button;
	wl_list_for_each(button, &nag->buttons, link) {
		h = render_button(cairo, nag, button, &x);
		max_height = h > max_height ? h : max_height;
		x -= nag->conf->button_gap;
	}

	if (nag->details.visible) {
		h = render_detailed(cairo, nag, max_height);
		max_height = h > max_height ? h : max_height;
	}

	int border = nag->conf->bar_border_thickness;
	if (max_height > nag->height) {
		max_height += border;
	}
	cairo_set_source_u32(cairo, nag->conf->border_bottom);
	cairo_rectangle(cairo, 0,
			nag->height - border,
			nag->width,
			border);
	cairo_fill(cairo);

	return max_height;
}

static void
render_frame(struct nag *nag)
{
	if (!nag->run_display) {
		return;
	}

	cairo_surface_t *recorder = cairo_recording_surface_create(
			CAIRO_CONTENT_COLOR_ALPHA, NULL);
	cairo_t *cairo = cairo_create(recorder);
	cairo_scale(cairo, nag->scale, nag->scale);
	cairo_save(cairo);
	cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
	cairo_paint(cairo);
	cairo_restore(cairo);
	uint32_t height = render_to_cairo(cairo, nag);
	if (height != nag->height) {
		zwlr_layer_surface_v1_set_size(nag->layer_surface, 0, height);
		if (nag->details.use_exclusive_zone) {
			zwlr_layer_surface_v1_set_exclusive_zone(
				nag->layer_surface, height);
		}
		wl_surface_commit(nag->surface);
		wl_display_roundtrip(nag->display);
	} else {
		nag->current_buffer = get_next_buffer(nag->shm,
				nag->buffers,
				nag->width * nag->scale,
				nag->height * nag->scale);
		if (!nag->current_buffer) {
			wlr_log(WLR_DEBUG, "Failed to get buffer. Skipping frame.");
			goto cleanup;
		}

		cairo_t *shm = nag->current_buffer->cairo;
		cairo_save(shm);
		cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR);
		cairo_paint(shm);
		cairo_restore(shm);
		cairo_set_source_surface(shm, recorder, 0.0, 0.0);
		cairo_paint(shm);

		wl_surface_set_buffer_scale(nag->surface, nag->scale);
		wl_surface_attach(nag->surface,
				nag->current_buffer->buffer, 0, 0);
		wl_surface_damage(nag->surface, 0, 0,
				nag->width, nag->height);
		wl_surface_commit(nag->surface);
		wl_display_roundtrip(nag->display);
	}

cleanup:
	cairo_surface_destroy(recorder);
	cairo_destroy(cairo);
}

static void
seat_destroy(struct seat *seat)
{
	if (seat->pointer.cursor_theme) {
		wl_cursor_theme_destroy(seat->pointer.cursor_theme);
	}
	if (seat->pointer.pointer) {
		wl_pointer_destroy(seat->pointer.pointer);
	}
	wl_seat_destroy(seat->wl_seat);
	wl_list_remove(&seat->link);
	free(seat);
}

static void
nag_destroy(struct nag *nag)
{
	nag->run_display = false;

	struct button *button, *next;
	wl_list_for_each_safe(button, next, &nag->buttons, link) {
		wl_list_remove(&button->link);
		free(button);
	}
	free(nag->details.message);

	pango_font_description_free(nag->conf->font_description);

	if (nag->layer_surface) {
		zwlr_layer_surface_v1_destroy(nag->layer_surface);
	}

	if (nag->surface) {
		wl_surface_destroy(nag->surface);
	}

	if (nag->layer_shell) {
		zwlr_layer_shell_v1_destroy(nag->layer_shell);
	}

	if (nag->cursor_shape_manager) {
		wp_cursor_shape_manager_v1_destroy(nag->cursor_shape_manager);
	}

	struct seat *seat, *tmpseat;
	wl_list_for_each_safe(seat, tmpseat, &nag->seats, link) {
		seat_destroy(seat);
	}

	destroy_buffer(&nag->buffers[0]);
	destroy_buffer(&nag->buffers[1]);

	if (nag->outputs.prev || nag->outputs.next) {
		struct output *output, *temp;
		wl_list_for_each_safe(output, temp, &nag->outputs, link) {
			wl_output_destroy(output->wl_output);
			free(output->name);
			wl_list_remove(&output->link);
			free(output);
		};
	}

	if (nag->compositor) {
		wl_compositor_destroy(nag->compositor);
	}

	if (nag->shm) {
		wl_shm_destroy(nag->shm);
	}

	if (nag->display) {
		wl_display_disconnect(nag->display);
	}
	pango_cairo_font_map_set_default(NULL);

	close_pollfd(&nag->pollfds[FD_TIMER]);
	close_pollfd(&nag->pollfds[FD_SIGNAL]);
}

static void
button_execute(struct nag *nag, struct button *button)
{
	wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action);
	if (button->expand) {
		nag->details.visible = !nag->details.visible;
		render_frame(nag);
		return;
	}
	if (button->dismiss) {
		nag->run_display = false;
	}
	if (button->action) {
		pid_t pid = fork();
		if (pid < 0) {
			wlr_log_errno(WLR_DEBUG, "Failed to fork");
			return;
		} else if (pid == 0) {
			/*
			 * Child process. Will be used to prevent zombie
			 * processes
			 */
			pid = fork();
			if (pid < 0) {
				wlr_log_errno(WLR_DEBUG, "Failed to fork");
				return;
			} else if (pid == 0) {
				/*
				 * Child of the child. Will be reparented to the
				 * init process
				 */
				execlp("sh", "sh", "-c", button->action, NULL);
				wlr_log_errno(WLR_DEBUG, "execlp failed");
				_exit(LAB_EXIT_FAILURE);
			}
			_exit(EXIT_SUCCESS);
		}

		if (waitpid(pid, NULL, 0) < 0) {
			wlr_log_errno(WLR_DEBUG, "waitpid failed");
		}
	}
}

static void
layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
		uint32_t serial, uint32_t width, uint32_t height)
{
	struct nag *nag = data;
	nag->width = width;
	nag->height = height;
	zwlr_layer_surface_v1_ack_configure(surface, serial);
	render_frame(nag);
}

static void
layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface)
{
	struct nag *nag = data;
	nag_destroy(nag);
}

static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
	.configure = layer_surface_configure,
	.closed = layer_surface_closed,
};

static void
surface_enter(void *data, struct wl_surface *surface, struct wl_output *output)
{
	struct nag *nag = data;
	struct output *nag_output;
	wl_list_for_each(nag_output, &nag->outputs, link) {
		if (nag_output->wl_output == output) {
			wlr_log(WLR_DEBUG, "Surface enter on output %s",
					nag_output->name);
			nag->output = nag_output;
			nag->scale = nag->output->scale;
			render_frame(nag);
			break;
		}
	}
}

static void
surface_leave(void *data, struct wl_surface *wl_surface, struct wl_output *output)
{
	/* nop */
}

static const struct wl_surface_listener surface_listener = {
	.enter = surface_enter,
	.leave = surface_leave,
};

static void
update_cursor(struct seat *seat)
{
	struct pointer *pointer = &seat->pointer;
	struct nag *nag = seat->nag;
	if (pointer->cursor_theme) {
		wl_cursor_theme_destroy(pointer->cursor_theme);
	}
	const char *cursor_theme = getenv("XCURSOR_THEME");
	unsigned int cursor_size = 24;
	const char *env_cursor_size = getenv("XCURSOR_SIZE");
	if (env_cursor_size && *env_cursor_size) {
		errno = 0;
		char *end;
		unsigned int size = strtoul(env_cursor_size, &end, 10);
		if (!*end && errno == 0) {
			cursor_size = size;
		}
	}
	pointer->cursor_theme = wl_cursor_theme_load(
		cursor_theme, cursor_size * nag->scale, nag->shm);
	if (!pointer->cursor_theme) {
		wlr_log(WLR_ERROR, "Failed to load cursor theme");
		return;
	}
	struct wl_cursor *cursor =
		wl_cursor_theme_get_cursor(pointer->cursor_theme, "default");
	if (!cursor) {
		wlr_log(WLR_ERROR, "Failed to get default cursor from theme");
		return;
	}
	pointer->cursor_image = cursor->images[0];
	wl_surface_set_buffer_scale(pointer->cursor_surface,
			nag->scale);
	wl_surface_attach(pointer->cursor_surface,
			wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
	wl_pointer_set_cursor(pointer->pointer, pointer->serial,
			pointer->cursor_surface,
			pointer->cursor_image->hotspot_x / nag->scale,
			pointer->cursor_image->hotspot_y / nag->scale);
	wl_surface_damage_buffer(pointer->cursor_surface, 0, 0,
			INT32_MAX, INT32_MAX);
	wl_surface_commit(pointer->cursor_surface);
}

static void
update_all_cursors(struct nag *nag)
{
	struct seat *seat;
	wl_list_for_each(seat, &nag->seats, link) {
		if (seat->pointer.pointer) {
			update_cursor(seat);
		}
	}
}

static void
wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
		struct wl_surface *surface, wl_fixed_t surface_x,
		wl_fixed_t surface_y)
{
	struct seat *seat = data;

	struct pointer *pointer = &seat->pointer;
	pointer->x = wl_fixed_to_int(surface_x);
	pointer->y = wl_fixed_to_int(surface_y);

	if (seat->nag->cursor_shape_manager) {
		struct wp_cursor_shape_device_v1 *device =
			wp_cursor_shape_manager_v1_get_pointer(
				seat->nag->cursor_shape_manager, wl_pointer);
		wp_cursor_shape_device_v1_set_shape(device, serial,
			WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
		wp_cursor_shape_device_v1_destroy(device);
	} else {
		pointer->serial = serial;
		update_cursor(seat);
	}
}

static void
wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
		uint32_t serial, struct wl_surface *surface)
{
	/* nop */
}

static void
wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, uint32_t time,
		wl_fixed_t surface_x, wl_fixed_t surface_y)
{
	struct seat *seat = data;
	seat->pointer.x = wl_fixed_to_int(surface_x);
	seat->pointer.y = wl_fixed_to_int(surface_y);
}

static void
wl_pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
		uint32_t time, uint32_t button, uint32_t state)
{
	struct seat *seat = data;
	struct nag *nag = seat->nag;

	if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
		return;
	}

	double x = seat->pointer.x;
	double y = seat->pointer.y;

	int index = 0;
	struct button *nagbutton;
	wl_list_for_each(nagbutton, &nag->buttons, link) {
		if (x >= nagbutton->x
				&& y >= nagbutton->y
				&& x < nagbutton->x + nagbutton->width
				&& y < nagbutton->y + nagbutton->height) {
			button_execute(nag, nagbutton);
			exit_status = index;
			return;
		}
		++index;
	}

	if (nag->details.visible &&
			nag->details.total_lines != nag->details.visible_lines) {
		struct button button_up = nag->details.button_up;
		if (x >= button_up.x
				&& y >= button_up.y
				&& x < button_up.x + button_up.width
				&& y < button_up.y + button_up.height
				&& nag->details.offset > 0) {
			nag->details.offset--;
			render_frame(nag);
			return;
		}

		struct button button_down = nag->details.button_down;
		int bot = nag->details.total_lines;
		bot -= nag->details.visible_lines;
		if (x >= button_down.x
				&& y >= button_down.y
				&& x < button_down.x + button_down.width
				&& y < button_down.y + button_down.height
				&& nag->details.offset < bot) {
			nag->details.offset++;
			render_frame(nag);
			return;
		}
	}
}

static void
wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time,
		uint32_t axis, wl_fixed_t value)
{
	struct seat *seat = data;
	struct nag *nag = seat->nag;
	if (!nag->details.visible
			|| seat->pointer.x < nag->details.x
			|| seat->pointer.y < nag->details.y
			|| seat->pointer.x >= nag->details.x + nag->details.width
			|| seat->pointer.y >= nag->details.y + nag->details.height
			|| nag->details.total_lines == nag->details.visible_lines) {
		return;
	}

	int direction = wl_fixed_to_int(value);
	int bot = nag->details.total_lines - nag->details.visible_lines;
	if (direction < 0 && nag->details.offset > 0) {
		nag->details.offset--;
	} else if (direction > 0 && nag->details.offset < bot) {
		nag->details.offset++;
	}

	render_frame(nag);
}

static void
wl_pointer_frame(void *data, struct wl_pointer *wl_pointer)
{
	struct seat *seat = data;
	/* pointer inputs clears timer for auto-closing */
	close_pollfd(&seat->nag->pollfds[FD_TIMER]);
}

static void
wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
		uint32_t axis_source)
{
	/* nop */
}

static void
wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
		uint32_t time, uint32_t axis)
{
	/* nop */
}

static void
wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
		uint32_t axis, int32_t discrete)
{
	/* nop */
}

static const struct wl_pointer_listener pointer_listener = {
	.enter = wl_pointer_enter,
	.leave = wl_pointer_leave,
	.motion = wl_pointer_motion,
	.button = wl_pointer_button,
	.axis = wl_pointer_axis,
	.frame = wl_pointer_frame,
	.axis_source = wl_pointer_axis_source,
	.axis_stop = wl_pointer_axis_stop,
	.axis_discrete = wl_pointer_axis_discrete,
};

static void
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
		enum wl_seat_capability caps)
{
	struct seat *seat = data;
	bool cap_pointer = caps & WL_SEAT_CAPABILITY_POINTER;
	if (cap_pointer && !seat->pointer.pointer) {
		seat->pointer.pointer = wl_seat_get_pointer(wl_seat);
		wl_pointer_add_listener(seat->pointer.pointer,
				&pointer_listener, seat);
	} else if (!cap_pointer && seat->pointer.pointer) {
		wl_pointer_destroy(seat->pointer.pointer);
		seat->pointer.pointer = NULL;
	}
}

static void
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
	/* nop */
}

static const struct wl_seat_listener seat_listener = {
	.capabilities = seat_handle_capabilities,
	.name = seat_handle_name,
};

static void
output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y,
		int32_t physical_width, int32_t physical_height,
		int32_t subpixel, const char *make, const char *model,
		int32_t transform)
{
	/* nop */
}

static void
output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
		int32_t width, int32_t height, int32_t refresh)
{
	/* nop */
}

static void
output_done(void *data, struct wl_output *output)
{
	/* nop */
}

static void
output_scale(void *data, struct wl_output *output, int32_t factor)
{
	struct output *nag_output = data;
	nag_output->scale = factor;
	if (nag_output->nag->output == nag_output) {
		nag_output->nag->scale = nag_output->scale;
		if (!nag_output->nag->cursor_shape_manager) {
			update_all_cursors(nag_output->nag);
		}
		render_frame(nag_output->nag);
	}
}

static void
output_name(void *data, struct wl_output *output, const char *name)
{
	struct output *nag_output = data;
	nag_output->name = strdup(name);

	const char *outname = nag_output->nag->conf->output;
	if (!nag_output->nag->output && outname &&
			strcmp(outname, name) == 0) {
		wlr_log(WLR_DEBUG, "Using output %s", name);
		nag_output->nag->output = nag_output;
	}
}

static void
output_description(void *data, struct wl_output *wl_output,
		const char *description)
{
	/* nop */
}

static const struct wl_output_listener output_listener = {
	.geometry = output_geometry,
	.mode = output_mode,
	.done = output_done,
	.scale = output_scale,
	.name = output_name,
	.description = output_description,
};

static void
handle_global(void *data, struct wl_registry *registry, uint32_t name,
		const char *interface, uint32_t version)
{
	struct nag *nag = data;
	if (strcmp(interface, wl_compositor_interface.name) == 0) {
		nag->compositor = wl_registry_bind(registry, name,
				&wl_compositor_interface, 4);
	} else if (strcmp(interface, wl_seat_interface.name) == 0) {
		struct seat *seat = calloc(1, sizeof(*seat));
		if (!seat) {
			perror("calloc");
			return;
		}

		seat->nag = nag;
		seat->wl_name = name;
		seat->wl_seat =
			wl_registry_bind(registry, name, &wl_seat_interface, 5);

		wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);

		wl_list_insert(&nag->seats, &seat->link);
	} else if (strcmp(interface, wl_shm_interface.name) == 0) {
		nag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
	} else if (strcmp(interface, wl_output_interface.name) == 0) {
		if (!nag->output) {
			struct output *output = calloc(1, sizeof(*output));
			if (!output) {
				perror("calloc");
				return;
			}
			output->wl_output = wl_registry_bind(registry, name,
					&wl_output_interface, 4);
			output->wl_name = name;
			output->scale = 1;
			output->nag = nag;
			wl_list_insert(&nag->outputs, &output->link);
			wl_output_add_listener(output->wl_output,
					&output_listener, output);
		}
	} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
		nag->layer_shell = wl_registry_bind(
				registry, name, &zwlr_layer_shell_v1_interface, 1);
	} else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
		nag->cursor_shape_manager = wl_registry_bind(
				registry, name, &wp_cursor_shape_manager_v1_interface, 1);
	}
}

static void
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
	struct nag *nag = data;
	if (nag->output->wl_name == name) {
		nag->run_display = false;
	}

	struct seat *seat, *tmpseat;
	wl_list_for_each_safe(seat, tmpseat, &nag->seats, link) {
		if (seat->wl_name == name) {
			seat_destroy(seat);
		}
	}
}

static const struct wl_registry_listener registry_listener = {
	.global = handle_global,
	.global_remove = handle_global_remove,
};

static void
nag_setup_cursors(struct nag *nag)
{
	struct seat *seat;

	wl_list_for_each(seat, &nag->seats, link) {
		struct pointer *p = &seat->pointer;

		p->cursor_surface =
			wl_compositor_create_surface(nag->compositor);
		assert(p->cursor_surface);
	}
}

static void
nag_setup(struct nag *nag)
{
	nag->display = wl_display_connect(NULL);
	if (!nag->display) {
		wlr_log(WLR_ERROR, "Unable to connect to the compositor. "
				"If your compositor is running, check or set the "
				"WAYLAND_DISPLAY environment variable.");
		exit(LAB_EXIT_FAILURE);
	}

	nag->scale = 1;

	struct wl_registry *registry = wl_display_get_registry(nag->display);
	wl_registry_add_listener(registry, &registry_listener, nag);
	if (wl_display_roundtrip(nag->display) < 0) {
		wlr_log(WLR_ERROR, "failed to register with the wayland display");
		exit(LAB_EXIT_FAILURE);
	}

	assert(nag->compositor && nag->layer_shell && nag->shm);

	/* Second roundtrip to get wl_output properties */
	if (wl_display_roundtrip(nag->display) < 0) {
		wlr_log(WLR_ERROR, "Error during outputs init.");
		nag_destroy(nag);
		exit(LAB_EXIT_FAILURE);
	}

	if (!nag->output && nag->conf->output) {
		wlr_log(WLR_ERROR, "Output '%s' not found", nag->conf->output);
		nag_destroy(nag);
		exit(LAB_EXIT_FAILURE);
	}

	if (!nag->cursor_shape_manager) {
		nag_setup_cursors(nag);
	}

	nag->surface = wl_compositor_create_surface(nag->compositor);
	assert(nag->surface);
	wl_surface_add_listener(nag->surface, &surface_listener, nag);

	nag->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
			nag->layer_shell, nag->surface,
			nag->output ? nag->output->wl_output : NULL,
			nag->conf->layer,
			"nag");
	assert(nag->layer_surface);
	zwlr_layer_surface_v1_add_listener(nag->layer_surface,
			&layer_surface_listener, nag);
	zwlr_layer_surface_v1_set_anchor(nag->layer_surface,
			nag->conf->anchors);

	wl_registry_destroy(registry);

	nag->pollfds[FD_WAYLAND].fd = wl_display_get_fd(nag->display);
	nag->pollfds[FD_WAYLAND].events = POLLIN;

	if (nag->details.close_timeout != 0) {
		nag->pollfds[FD_TIMER].fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
		nag->pollfds[FD_TIMER].events = POLLIN;
		struct itimerspec timeout = {
			.it_value.tv_sec = nag->details.close_timeout,
		};
		timerfd_settime(nag->pollfds[FD_TIMER].fd, 0, &timeout, NULL);
	} else {
		nag->pollfds[FD_TIMER].fd = -1;
	}

	sigset_t mask;
	sigaddset(&mask, SIGINT);
	sigaddset(&mask, SIGTERM);
	sigprocmask(SIG_BLOCK, &mask, NULL);
	nag->pollfds[FD_SIGNAL].fd = signalfd(-1, &mask, SFD_CLOEXEC | SFD_NONBLOCK);
	nag->pollfds[FD_SIGNAL].events = POLLIN;
}

static void
close_pollfd(struct pollfd *pollfd)
{
	if (pollfd->fd == -1) {
		return;
	}
	close(pollfd->fd);
	pollfd->fd = -1;
	pollfd->events = 0;
	pollfd->revents = 0;
}

static void
nag_run(struct nag *nag)
{
	nag->run_display = true;
	render_frame(nag);
	while (nag->run_display) {
		while (wl_display_prepare_read(nag->display) != 0) {
			wl_display_dispatch_pending(nag->display);
		}

		errno = 0;
		if (wl_display_flush(nag->display) == -1 && errno != EAGAIN) {
			break;
		}

		if (!nag->run_display) {
			break;
		}

		poll(nag->pollfds, NR_FDS, -1);
		if (nag->pollfds[FD_WAYLAND].revents & POLLIN) {
			wl_display_read_events(nag->display);
		} else {
			wl_display_cancel_read(nag->display);
		}
		if (nag->pollfds[FD_TIMER].revents & POLLIN) {
			exit_status = LAB_EXIT_TIMEOUT;
			break;
		}
		if (nag->pollfds[FD_SIGNAL].revents & POLLIN) {
			break;
		}
	}
}

static void
conf_init(struct conf *conf)
{
	conf->font_description = pango_font_description_from_string("pango:Sans 10");
	conf->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
		| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
		| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
	conf->layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
	conf->button_background = 0x333333FF;
	conf->details_background = 0x333333FF;
	conf->background = 0x323232FF;
	conf->text = 0xFFFFFFFF;
	conf->button_text = 0xFFFFFFFF;
	conf->button_border = 0x222222FF;
	conf->border_bottom = 0x444444FF;
	conf->bar_border_thickness = 2;
	conf->message_padding = 8;
	conf->details_border_thickness = 3;
	conf->button_border_thickness = 3;
	conf->button_gap = 20;
	conf->button_gap_close = 15;
	conf->button_margin_right = 2;
	conf->button_padding = 3;
	conf->button_background = 0x680A0AFF;
	conf->details_background = 0x680A0AFF;
	conf->background = 0x900000FF;
	conf->text = 0xFFFFFFFF;
	conf->button_text = 0xFFFFFFFF;
	conf->button_border = 0xD92424FF;
	conf->border_bottom = 0x470909FF;
}

static bool
parse_color(const char *color, uint32_t *result)
{
	if (color[0] == '#') {
		++color;
	}
	int len = strlen(color);
	if ((len != 6 && len != 8) || !isxdigit(color[0]) || !isxdigit(color[1])) {
		return false;
	}
	char *ptr;
	uint32_t parsed = strtoul(color, &ptr, 16);
	if (*ptr != '\0') {
		return false;
	}
	*result = len == 6 ? ((parsed << 8) | 0xFF) : parsed;
	return true;
}

/*
 * As labnag is slow for large "detailed messages" we curtail stdin at an
 * arbitrary size to avoid hogging the CPU.
 */
#define MAX_STDIN_LINES 200
static char *
read_and_trim_stdin(void)
{
	char *buffer = NULL, *line = NULL;
	size_t buffer_len = 0, line_size = 0;
	int line_count = 0;
	while (line_count < MAX_STDIN_LINES) {
		ssize_t nread = getline(&line, &line_size, stdin);
		if (nread == -1) {
			if (feof(stdin)) {
				break;
			} else {
				perror("getline");
				goto freeline;
			}
		}
		buffer = realloc(buffer, buffer_len + nread + 1);
		if (!buffer) {
			perror("realloc");
			return NULL;
		}
		memcpy(&buffer[buffer_len], line, nread + 1);
		buffer_len += nread;
		++line_count;
	}
	free(line);

	while (buffer_len && buffer[buffer_len - 1] == '\n') {
		buffer[--buffer_len] = '\0';
	}

	return buffer;

freeline:
	free(line);
	return NULL;
}

static int
nag_parse_options(int argc, char **argv, struct nag *nag,
		struct conf *conf, bool *debug)
{
	enum type_options {
		TO_COLOR_BACKGROUND = 256,
		TO_COLOR_BUTTON_BORDER,
		TO_COLOR_BORDER_BOTTOM,
		TO_COLOR_BUTTON_BG,
		TO_COLOR_DETAILS,
		TO_COLOR_TEXT,
		TO_COLOR_BUTTON_TEXT,
		TO_THICK_BAR_BORDER,
		TO_PADDING_MESSAGE,
		TO_THICK_DET_BORDER,
		TO_THICK_BTN_BORDER,
		TO_GAP_BTN,
		TO_GAP_BTN_DISMISS,
		TO_MARGIN_BTN_RIGHT,
		TO_PADDING_BTN,
	};

	static const struct option opts[] = {
		{"button", required_argument, NULL, 'B'},
		{"button-dismiss", required_argument, NULL, 'Z'},
		{"debug", no_argument, NULL, 'd'},
		{"edge", required_argument, NULL, 'e'},
		{"layer", required_argument, NULL, 'y'},
		{"font", required_argument, NULL, 'f'},
		{"help", no_argument, NULL, 'h'},
		{"detailed-message", no_argument, NULL, 'l'},
		{"detailed-button", required_argument, NULL, 'L'},
		{"message", required_argument, NULL, 'm'},
		{"output", required_argument, NULL, 'o'},
		{"timeout", required_argument, NULL, 't'},
		{"version", no_argument, NULL, 'v'},

		{"background-color", required_argument, NULL, TO_COLOR_BACKGROUND},
		{"button-border-color", required_argument, NULL, TO_COLOR_BUTTON_BORDER},
		{"border-bottom-color", required_argument, NULL, TO_COLOR_BORDER_BOTTOM},
		{"button-background-color", required_argument, NULL, TO_COLOR_BUTTON_BG},
		{"text-color", required_argument, NULL, TO_COLOR_TEXT},
		{"button-text-color", required_argument, NULL, TO_COLOR_BUTTON_TEXT},
		{"border-bottom-size", required_argument, NULL, TO_THICK_BAR_BORDER},
		{"message-padding", required_argument, NULL, TO_PADDING_MESSAGE},
		{"details-border-size", required_argument, NULL, TO_THICK_DET_BORDER},
		{"details-background-color", required_argument, NULL, TO_COLOR_DETAILS},
		{"button-border-size", required_argument, NULL, TO_THICK_BTN_BORDER},
		{"button-gap", required_argument, NULL, TO_GAP_BTN},
		{"button-dismiss-gap", required_argument, NULL, TO_GAP_BTN_DISMISS},
		{"button-margin-right", required_argument, NULL, TO_MARGIN_BTN_RIGHT},
		{"button-padding", required_argument, NULL, TO_PADDING_BTN},

		{0, 0, 0, 0}
	};

	const char *usage =
		"Usage: labnag [options...]\n"
		"\n"
		"  -B, --button <text> [<action>]  Create a button with text\n"
		"  -Z, --button-dismiss <text> [<action>]\n"
		"                                  Like -B but dismiss nag when pressed\n"
		"  -d, --debug                     Enable debugging.\n"
		"  -e, --edge top|bottom           Set the edge to use.\n"
		"  -y, --layer overlay|top|bottom|background\n"
		"                                  Set the layer to use.\n"
		"  -f, --font <font>               Set the font to use.\n"
		"  -h, --help                      Show help message and quit.\n"
		"  -l, --detailed-message          Read a detailed message from stdin.\n"
		"  -L, --detailed-button <text>    Set the text of the detail button.\n"
		"  -m, --message <msg>             Set the message text.\n"
		"  -o, --output <output>           Set the output to use.\n"
		"  -t, --timeout <seconds>         Set duration to close dialog.\n"
		"  -x, --exclusive-zone            Use exclusive zone.\n"
		"  -v, --version                   Show the version number and quit.\n"
		"\n"
		"The following appearance options can also be given:\n"
		"  --background-color RRGGBB[AA]    Background color.\n"
		"  --button-border-color RRGGBB[AA] Button border color.\n"
		"  --border-bottom-color RRGGBB[AA] Bottom border color.\n"
		"  --button-background-color RRGGBB[AA]\n"
		"                                   Button background color.\n"
		"  --text-color RRGGBB[AA]          Text color.\n"
		"  --button-text-color RRGGBB[AA]   Button text color.\n"
		"  --border-bottom-size size        Thickness of the bar border.\n"
		"  --message-padding padding        Padding for the message.\n"
		"  --details-border-size size       Thickness for the details border.\n"
		"  --details-background-color RRGGBB[AA]\n"
		"                                   Details background color.\n"
		"  --button-border-size size        Thickness for the button border.\n"
		"  --button-gap gap                 Size of the gap between buttons\n"
		"  --button-dismiss-gap gap         Size of the gap for dismiss button.\n"
		"  --button-margin-right margin     Margin from dismiss button to edge.\n"
		"  --button-padding padding         Padding for the button text.\n";

	optind = 1;
	while (1) {
		int c = getopt_long(argc, argv, "B:Z:c:de:y:f:hlL:m:o:s:t:vx", opts, NULL);
		if (c == -1) {
			break;
		}
		switch (c) {
		case 'B': /* Button */
		case 'Z': /* Button (Dismiss) */ {
			struct button *button = calloc(1, sizeof(*button));
			if (!button) {
				perror("calloc");
				return LAB_EXIT_FAILURE;
			}
			if (argv[optind] && argv[optind][0] != '-') {
				button->action = argv[optind];
				optind++;
			}
			button->text = optarg;
			button->dismiss = c == 'Z';
			wl_list_insert(&nag->buttons, &button->link);
			break;
		}
		case 'd': /* Debug */
			*debug = true;
			break;
		case 'e': /* Edge */
			if (strcmp(optarg, "top") == 0) {
				conf->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
					| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
					| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
			} else if (strcmp(optarg, "bottom") == 0) {
				conf->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
					| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
					| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
			} else {
				fprintf(stderr, "Invalid edge: %s\n", optarg);
				return LAB_EXIT_FAILURE;
			}
			break;
		case 'y': /* Layer */
			if (strcmp(optarg, "background") == 0) {
				conf->layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
			} else if (strcmp(optarg, "bottom") == 0) {
				conf->layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
			} else if (strcmp(optarg, "top") == 0) {
				conf->layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
			} else if (strcmp(optarg, "overlay") == 0) {
				conf->layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
			} else {
				fprintf(stderr, "Invalid layer: %s\n"
						"Usage: --layer overlay|top|bottom|background\n",
						optarg);
				return LAB_EXIT_FAILURE;
			}
			break;
		case 'f': /* Font */
			pango_font_description_free(conf->font_description);
			conf->font_description = pango_font_description_from_string(optarg);
			break;
		case 'l': /* Detailed Message */
			free(nag->details.message);
			nag->details.message = read_and_trim_stdin();
			if (!nag->details.message) {
				return LAB_EXIT_FAILURE;
			}
			nag->details.button_up.text = "▲";
			nag->details.button_down.text = "▼";
			break;
		case 'L': /* Detailed Button Text */
			nag->details.details_text = optarg;
			break;
		case 'm': /* Message */
			nag->message = optarg;
			break;
		case 'o': /* Output */
			free(conf->output);
			conf->output = optarg;
			break;
		case 't':
			nag->details.close_timeout = atoi(optarg);
			break;
		case 'x':
			nag->details.use_exclusive_zone = true;
			break;
		case 'v': /* Version */
			printf("labnag " LABWC_VERSION "\n");
			return LAB_EXIT_FAILURE;
		case TO_COLOR_BACKGROUND: /* Background color */
			if (!parse_color(optarg, &conf->background)) {
				fprintf(stderr, "Invalid background color: %s\n", optarg);
			}
			break;
		case TO_COLOR_BUTTON_BORDER: /* Border color */
			if (!parse_color(optarg, &conf->button_border)) {
				fprintf(stderr, "Invalid border color: %s\n", optarg);
			}
			break;
		case TO_COLOR_BORDER_BOTTOM: /* Bottom border color */
			if (!parse_color(optarg, &conf->border_bottom)) {
				fprintf(stderr, "Invalid border bottom color: %s\n", optarg);
			}
			break;
		case TO_COLOR_BUTTON_BG: /* Button background color */
			if (!parse_color(optarg, &conf->button_background)) {
				fprintf(stderr, "Invalid button background color: %s\n", optarg);
			}
			break;
		case TO_COLOR_DETAILS: /* Details background color */
			if (!parse_color(optarg, &conf->details_background)) {
				fprintf(stderr, "Invalid details background color: %s\n", optarg);
			}
			break;
		case TO_COLOR_TEXT: /* Text color */
			if (!parse_color(optarg, &conf->text)) {
				fprintf(stderr, "Invalid text color: %s\n", optarg);
			}
			break;
		case TO_COLOR_BUTTON_TEXT: /* Button text color */
			if (!parse_color(optarg, &conf->button_text)) {
				fprintf(stderr, "Invalid button text color: %s\n", optarg);
			}
			break;
		case TO_THICK_BAR_BORDER: /* Bottom border thickness */
			conf->bar_border_thickness = strtol(optarg, NULL, 0);
			break;
		case TO_PADDING_MESSAGE: /* Message padding */
			conf->message_padding = strtol(optarg, NULL, 0);
			break;
		case TO_THICK_DET_BORDER: /* Details border thickness */
			conf->details_border_thickness = strtol(optarg, NULL, 0);
			break;
		case TO_THICK_BTN_BORDER: /* Button border thickness */
			conf->button_border_thickness = strtol(optarg, NULL, 0);
			break;
		case TO_GAP_BTN: /* Gap between buttons */
			conf->button_gap = strtol(optarg, NULL, 0);
			break;
		case TO_GAP_BTN_DISMISS: /* Gap between dismiss button */
			conf->button_gap_close = strtol(optarg, NULL, 0);
			break;
		case TO_MARGIN_BTN_RIGHT: /* Margin on the right side of button area */
			conf->button_margin_right = strtol(optarg, NULL, 0);
			break;
		case TO_PADDING_BTN: /* Padding for the button text */
			conf->button_padding = strtol(optarg, NULL, 0);
			break;
		default: /* Help or unknown flag */
			fprintf(c == 'h' ? stdout : stderr, "%s", usage);
			return LAB_EXIT_FAILURE;
		}
	}

	return LAB_EXIT_SUCCESS;
}

int
main(int argc, char **argv)
{
	struct conf conf = { 0 };
	conf_init(&conf);

	struct nag nag = {
		.conf = &conf,
	};

	wl_list_init(&nag.buttons);
	wl_list_init(&nag.outputs);
	wl_list_init(&nag.seats);

	nag.details.details_text = "Toggle details";
	nag.details.close_timeout = 5;
	nag.details.use_exclusive_zone = false;

	bool debug = false;
	if (argc > 1) {
		exit_status = nag_parse_options(argc, argv, &nag, &conf, &debug);
		if (exit_status == LAB_EXIT_FAILURE) {
			goto cleanup;
		}
	}
	wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL);

	if (!nag.message) {
		wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m");
		exit_status = LAB_EXIT_FAILURE;
		goto cleanup;
	}

	if (nag.details.message) {
		nag.details.button_details = calloc(1, sizeof(struct button));
		assert(nag.details.button_details);
		nag.details.button_details->text = nag.details.details_text;
		assert(nag.details.button_details->text);
		nag.details.button_details->expand = true;
		wl_list_insert(nag.buttons.prev, &nag.details.button_details->link);
	}

	wlr_log(WLR_DEBUG, "Output: %s", nag.conf->output);
	wlr_log(WLR_DEBUG, "Anchors: %lu", (unsigned long)nag.conf->anchors);
	wlr_log(WLR_DEBUG, "Message: %s", nag.message);
	char *font = pango_font_description_to_string(nag.conf->font_description);
	wlr_log(WLR_DEBUG, "Font: %s", font);
	free(font);
	wlr_log(WLR_DEBUG, "Buttons");

	struct button *button;
	wl_list_for_each(button, &nag.buttons, link) {
		wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action);
	}

	nag_setup(&nag);

	nag_run(&nag);

cleanup:
	nag_destroy(&nag);
	return exit_status;
}