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

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <stdarg.h>
#include <limits.h>
#include <string.h>

#include <glib.h>
#include <glib-unix.h>
#include <gio/gio.h>
#include <pango/pango.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <signal.h>
#include <pwd.h>

#include <termo.h> // input
#include <ncurses.h> // output

#include "config.h"
#include "stardict.h"
#include "utils.h"

#ifdef WITH_GTK
#include <gtk/gtk.h>
#endif  // WITH_GTK

#define CTRL_KEY(x)  ((x) - 'A' + 1)

#define TOP_BAR_CUTOFF  2               ///< How many lines are reserved on top
#define APP_TITLE  PROJECT_NAME " "     ///< Left top corner

// --- Utilities ---------------------------------------------------------------

static size_t
unichar_width (gunichar ch)
{
	if (g_unichar_iszerowidth (ch))
		return 0;
	return 1 + g_unichar_iswide (ch);
}

// At times, GLib even with its sheer size is surprisingly useless,
// and I need to port some code over from "liberty".

static gchar **
get_xdg_config_dirs (void)
{
	GPtrArray *paths = g_ptr_array_new ();
	g_ptr_array_add (paths, (gpointer) g_get_user_config_dir ());
	for (const gchar *const *system = g_get_system_config_dirs ();
		*system; system++)
		g_ptr_array_add (paths, (gpointer) *system);
	g_ptr_array_add (paths, NULL);
	return (gchar **) g_ptr_array_free (paths, FALSE);
}

static gchar *
resolve_relative_filename_generic
	(gchar **paths, const gchar *tail, const gchar *filename)
{
	for (; *paths; paths++)
	{
		// As per XDG spec, relative paths are ignored
		if (**paths != '/')
			continue;

		gchar *file = g_build_filename (*paths, tail, filename, NULL);
		GStatBuf st;
		if (!g_stat (file, &st))
			return file;
		g_free (file);
	}
	return NULL;
}

static gchar *
resolve_relative_config_filename (const gchar *filename)
{
	gchar **paths = get_xdg_config_dirs ();
	gchar *result = resolve_relative_filename_generic
		(paths, PROJECT_NAME, filename);
	g_strfreev (paths);
	return result;
}

static gchar *
try_expand_tilde (const gchar *filename)
{
	size_t until_slash = strcspn (filename, "/");
	if (!until_slash)
		return g_build_filename (g_get_home_dir () ?: "", filename, NULL);

	long buf_len = sysconf (_SC_GETPW_R_SIZE_MAX);
	if (buf_len < 0)
		buf_len = 1024;
	struct passwd pwd, *success = NULL;

	gchar *user = g_strndup (filename, until_slash);
	gchar *buf = g_malloc (buf_len);
	while (getpwnam_r (user, &pwd, buf, buf_len, &success) == ERANGE)
		buf = g_realloc (buf, buf_len <<= 1);
	g_free (user);

	gchar *result = NULL;
	if (success)
		result = g_strdup_printf ("%s%s", pwd.pw_dir, filename + until_slash);
	g_free (buf);
	return result;
}

static gchar *
resolve_filename (const gchar *filename, gchar *(*relative_cb) (const char *))
{
	// Absolute path is absolute
	if (*filename == '/')
		return g_strdup (filename);

	// We don't want to use wordexp() for this as it may execute /bin/sh
	if (*filename == '~')
	{
		// Paths to home directories ought to be absolute
		char *expanded = try_expand_tilde (filename + 1);
		if (expanded)
			return expanded;
		g_debug ("failed to expand the home directory in `%s'", filename);
	}
	return relative_cb (filename);
}

// --- Application -------------------------------------------------------------

#define ATTRIBUTE_TABLE(XX)                            \
	XX( HEADER, "header",        -1, -1, A_REVERSE   ) \
	XX( ACTIVE, "header-active", -1, -1, A_UNDERLINE ) \
	XX( SEARCH, "search",        -1, -1, A_UNDERLINE ) \
	XX( EVEN,   "even",          -1, -1, 0           ) \
	XX( ODD,    "odd",           -1, -1, 0           )

enum
{
#define XX(name, config, fg_, bg_, attrs_) ATTRIBUTE_ ## name,
	ATTRIBUTE_TABLE (XX)
#undef XX
	ATTRIBUTE_COUNT
};

struct attrs
{
	short fg;                           ///< Foreground color index
	short bg;                           ///< Background color index
	chtype attrs;                       ///< Other attributes
};

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/// Data relating to one entry within the dictionary.
typedef struct view_entry               ViewEntry;
/// Data relating to a dictionary file.
typedef struct dictionary               Dictionary;
/// Encloses application data.
typedef struct application              Application;

struct view_entry
{
	gchar   * word;                     ///< Word
	gchar  ** definitions;              ///< Word definition entries
	gsize     definitions_length;       ///< Length of the @a definitions array
};

struct dictionary
{
	gchar         * name;               ///< Visible identifier
	gsize           name_width;         ///< Visible width of the name
	gchar         * filename;           ///< Path to the dictionary
	StardictDict  * dict;               ///< Dictionary
};

struct application
{
	GMainLoop     * loop;               ///< Main loop
	termo_t       * tk;                 ///< termo handle
	guint           tk_timer;           ///< termo timeout timer
	GIConv          ucs4_to_locale;     ///< UTF-32 -> locale conversion
	gboolean        locale_is_utf8;     ///< The locale is Unicode

	GArray        * dictionaries;       ///< All loaded dictionaries

	StardictDict  * dict;               ///< The current dictionary
	guint           show_help : 1;      ///< Whether help can be shown
	guint           center_search : 1;  ///< Whether to center the search
	guint           underline_last : 1; ///< Underline the last definition
	guint           hl_prefix : 1;      ///< Highlight the common prefix

	guint32         top_position;       ///< Index of the topmost dict. entry
	guint           top_offset;         ///< Offset into the top entry
	guint           selected;           ///< Offset to the selected definition
	GPtrArray     * entries;            ///< ViewEntry's within the view

	gchar         * search_label;       ///< Text of the "Search" label
	GArray        * input;              ///< The current search input
	guint           input_pos;          ///< Cursor position within input
	gboolean        input_confirmed;    ///< Input has been confirmed

	gfloat          division;           ///< Position of the division column

	guint           selection_timer;    ///< Selection watcher timeout timer
	gint            selection_interval; ///< Selection watcher timer interval
	gchar         * selection_contents; ///< Selection contents

	struct attrs    attrs[ATTRIBUTE_COUNT];
};

/// Shortcut to retrieve named terminal attributes
#define APP_ATTR(name) self->attrs[ATTRIBUTE_ ## name].attrs

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/// Splits the entry and adds it to a pointer array.
static void
view_entry_split_add (GPtrArray *out, const gchar *text)
{
	gchar **it, **tmp = g_strsplit (text, "\n", -1);
	for (it = tmp; *it; it++)
		if (**it)
			g_ptr_array_add (out, g_strdup (*it));
	g_strfreev (tmp);
}

/// Decomposes a dictionary entry into the format we want.
static ViewEntry *
view_entry_new (StardictIterator *iterator)
{
	g_return_val_if_fail (stardict_iterator_is_valid (iterator), NULL);

	ViewEntry *ve = g_slice_alloc (sizeof *ve);
	GString *word = g_string_new (stardict_iterator_get_word (iterator));

	StardictEntry *entry = stardict_iterator_get_entry (iterator);
	g_return_val_if_fail (entry != NULL, NULL);

	GPtrArray *definitions = g_ptr_array_new ();
	const GList *fields = stardict_entry_get_fields (entry);
	gboolean found_anything_displayable = FALSE;
	while (fields)
	{
		const StardictEntryField *field = fields->data;
		switch (field->type)
		{
		case STARDICT_FIELD_MEANING:
			view_entry_split_add (definitions, field->data);
			found_anything_displayable = TRUE;
			break;
		case STARDICT_FIELD_PANGO:
		{
			char *text;
			if (!pango_parse_markup (field->data, -1,
				0, NULL, &text, NULL, NULL))
				text = g_strdup_printf ("<%s>", _("error in entry"));
			view_entry_split_add (definitions, text);
			found_anything_displayable = TRUE;
			g_free (text);
			break;
		}
		case STARDICT_FIELD_PHONETIC:
			g_string_append_printf (word, " /%s/", (const gchar *) field->data);
			break;
		default:
			// TODO support more of them
			break;
		}
		fields = fields->next;
	}
	g_object_unref (entry);

	if (!found_anything_displayable)
		g_ptr_array_add (definitions,
			g_strdup_printf ("<%s>", _("no usable field found")));

	ve->word = g_string_free (word, FALSE);
	ve->definitions_length = definitions->len;
	g_ptr_array_add (definitions, NULL);
	ve->definitions = (gchar **) g_ptr_array_free (definitions, FALSE);
	return ve;
}

/// Release resources associated with the view entry.
static void
view_entry_free (ViewEntry *ve)
{
	g_free (ve->word);
	g_strfreev (ve->definitions);
	g_slice_free1 (sizeof *ve, ve);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static gboolean
dictionary_load (Dictionary *self, GError **e)
{
	if (!(self->dict = stardict_dict_new (self->filename, e)))
		return FALSE;

	if (!self->name)
	{
		self->name = g_strdup (stardict_info_get_book_name
			(stardict_dict_get_info (self->dict)));
	}

	// Add some padding for decorative purposes
	gchar *tmp = g_strdup_printf (" %s ", self->name);
	g_free (self->name);
	self->name = tmp;

	gunichar *ucs4 = g_utf8_to_ucs4_fast (self->name, -1, NULL);
	for (gunichar *it = ucs4; *it; it++)
		self->name_width += unichar_width (*it);
	g_free (ucs4);
	return TRUE;
}

static void
dictionary_free (Dictionary *self)
{
	g_free (self->name);
	g_free (self->filename);

	if (self->dict)
		g_object_unref (self->dict);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/// Reload view items.
static void
app_reload_view (Application *self)
{
	if (self->entries->len != 0)
		g_ptr_array_remove_range (self->entries, 0, self->entries->len);

	gint remains = LINES - TOP_BAR_CUTOFF + self->top_offset;
	StardictIterator *iterator =
		stardict_iterator_new (self->dict, self->top_position);
	while (remains > 0 && stardict_iterator_is_valid (iterator))
	{
		ViewEntry *entry = view_entry_new (iterator);
		remains -= entry->definitions_length;
		g_ptr_array_add (self->entries, entry);
		stardict_iterator_next (iterator);
	}
	g_object_unref (iterator);
}

#ifdef WITH_GTK
static gboolean on_selection_timer (gpointer data);

static void
rearm_selection_watcher (Application *self)
{
	if (self->selection_interval > 0)
		self->selection_timer = g_timeout_add
			(self->selection_interval, on_selection_timer, self);
}
#endif  // WITH_GTK

/// Load configuration for a color using a subset of git config colors.
static void
app_load_color (Application *self, GKeyFile *kf, const gchar *name, int id)
{
	gchar *value = g_key_file_get_string (kf, "Colors", name, NULL);
	if (!value)
		return;

	struct attrs attrs = { -1, -1, 0 };
	gchar **values = g_strsplit (value, " ", 0);
	gint colors = 0;
	for (gchar **it = values; *it; it++)
	{
		gchar *end = NULL;
		gint64 n = g_ascii_strtoll (*it, &end, 10);
		if (*it != end && !*end && n >= G_MINSHORT && n <= G_MAXSHORT)
		{
			if (colors == 0) attrs.fg = n;
			if (colors == 1) attrs.bg = n;
			colors++;
		}
		else if (!strcmp (*it, "bold"))    attrs.attrs |= A_BOLD;
		else if (!strcmp (*it, "dim"))     attrs.attrs |= A_DIM;
		else if (!strcmp (*it, "ul"))      attrs.attrs |= A_UNDERLINE;
		else if (!strcmp (*it, "blink"))   attrs.attrs |= A_BLINK;
		else if (!strcmp (*it, "reverse")) attrs.attrs |= A_REVERSE;
#ifdef A_ITALIC
		else if (!strcmp (*it, "italic"))  attrs.attrs |= A_ITALIC;
#endif  // A_ITALIC
	}
	g_strfreev (values);

	g_free (value);
	self->attrs[id] = attrs;
}

static gboolean
app_load_bool (GKeyFile *kf, const gchar *name, gboolean def)
{
	GError *e = NULL;
	bool value = g_key_file_get_boolean (kf, "Settings", name, &e);
	if (e)
	{
		g_error_free (e);
		return def;
	}
	return value;
}

static void
app_load_config_values (Application *self, GKeyFile *kf)
{
	self->center_search =
		app_load_bool (kf, "center-search", self->center_search);
	self->underline_last =
		app_load_bool (kf, "underline-last", self->underline_last);
	self->hl_prefix =
		app_load_bool (kf, "hl-common-prefix", self->hl_prefix);

	guint64 timer;
	const gchar *watch_selection = "watch-selection";
	if (app_load_bool (kf, watch_selection, FALSE))
		self->selection_interval = 500;
	else if ((timer = g_key_file_get_uint64
		(kf, "Settings", watch_selection, NULL)) && timer <= G_MAXINT)
		self->selection_interval = timer;

#define XX(name, config, fg_, bg_, attrs_) \
	app_load_color (self, kf, config, ATTRIBUTE_ ## name);
	ATTRIBUTE_TABLE (XX)
#undef XX

	const gchar *dictionaries = "Dictionaries";
	gchar **names = g_key_file_get_keys (kf, dictionaries, NULL, NULL);
	if (!names)
		return;

	for (gchar **it = names; *it; it++)
	{
		gchar *path = g_key_file_get_string (kf, dictionaries, *it, NULL);
		if (!path)
			continue;

		// Try to resolve relative paths and expand tildes
		gchar *resolved = resolve_filename
			(path, resolve_relative_config_filename);
		if (resolved)
			g_free (path);
		else
			resolved = path;

		Dictionary dict = { .name = g_strdup (*it), .filename = resolved };
		g_array_append_val (self->dictionaries, dict);
	}
	g_strfreev (names);
}

static void
app_load_config (Application *self, GError **e)
{
	GKeyFile *kf = g_key_file_new ();
	gchar **paths = get_xdg_config_dirs ();

	// XXX: if there are dashes in the final path component,
	//   the function tries to replace them with directory separators,
	//   which is completely undocumented
	GError *error = NULL;
	g_key_file_load_from_dirs (kf,
		PROJECT_NAME G_DIR_SEPARATOR_S PROJECT_NAME ".conf",
		(const gchar **) paths, NULL, 0, &error);
	g_strfreev (paths);

	// TODO: proper error handling showing all relevant information;
	//   we can afford that here since the terminal hasn't been initialized yet
	if (!error)
		app_load_config_values (self, kf);
	else if (error->code == G_KEY_FILE_ERROR_NOT_FOUND)
		g_error_free (error);
	else
		g_propagate_error (e, error);

	g_key_file_free (kf);
}

static void
app_init_attrs (Application *self)
{
#define XX(name, config, fg_, bg_, attrs_)          \
	self->attrs[ATTRIBUTE_ ## name].fg    = fg_;    \
	self->attrs[ATTRIBUTE_ ## name].bg    = bg_;    \
	self->attrs[ATTRIBUTE_ ## name].attrs = attrs_;
	ATTRIBUTE_TABLE (XX)
#undef XX
}

/// Initialize the application core.
static void
app_init (Application *self, char **filenames)
{
	self->loop = NULL;
	self->selection_interval = -1;
	self->selection_timer = 0;
	self->selection_contents = NULL;

	self->tk = NULL;
	self->tk_timer = 0;

	self->show_help = TRUE;
	self->center_search = TRUE;
	self->underline_last = TRUE;
	self->hl_prefix = TRUE;

	self->top_position = 0;
	self->top_offset = 0;
	self->selected = 0;
	self->entries = g_ptr_array_new_with_free_func
		((GDestroyNotify) view_entry_free);

	self->search_label = g_strdup_printf ("%s: ", _("Search"));

	self->input = g_array_new (TRUE, FALSE, sizeof (gunichar));
	self->input_pos = 0;
	self->input_confirmed = FALSE;

	self->division = 0.5;

	const char *charset;
	self->locale_is_utf8 = g_get_charset (&charset);
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
	self->ucs4_to_locale = g_iconv_open (charset, "UTF-32LE");
#else // G_BYTE_ORDER != G_LITTLE_ENDIAN
	self->ucs4_to_locale = g_iconv_open (charset, "UTF-32BE");
#endif // G_BYTE_ORDER != G_LITTLE_ENDIAN

	app_init_attrs (self);
	self->dictionaries = g_array_new (FALSE, TRUE, sizeof (Dictionary));
	g_array_set_clear_func
		(self->dictionaries, (GDestroyNotify) dictionary_free);

	GError *error = NULL;
	app_load_config (self, &error);
	if (error)
	{
		g_printerr ("%s: %s\n", _("Cannot load configuration"), error->message);
		exit (EXIT_FAILURE);
	}

	// Now we have settings for the clipboard watcher, we can arm the timer
#ifdef WITH_GTK
	if (gtk_init_check (0, NULL))
	{
		// So that we set the input only when it actually changes
		GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
		self->selection_contents = gtk_clipboard_wait_for_text (clipboard);
		rearm_selection_watcher (self);
	}
	else
#endif  // WITH_GTK
		self->loop = g_main_loop_new (NULL, FALSE);

	// Dictionaries given on the command line override the configuration
	if (*filenames)
	{
		g_array_set_size (self->dictionaries, 0);
		while (*filenames)
		{
			Dictionary dict = { .filename = g_strdup (*filenames++) };
			g_array_append_val (self->dictionaries, dict);
		}
	}

	for (guint i = 0; i < self->dictionaries->len && dictionary_load
		(&g_array_index (self->dictionaries, Dictionary, i), &error); i++)
		;
	if (error)
	{
		g_printerr ("%s: %s\n", _("Error loading dictionary"), error->message);
		exit (EXIT_FAILURE);
	}

	if (!self->dictionaries->len)
	{
		g_printerr ("%s\n", _("No dictionaries found either in "
			"the configuration or on the command line"));
		exit (EXIT_FAILURE);
	}
	self->dict = g_array_index (self->dictionaries, Dictionary, 0).dict;
	app_reload_view (self);
}

static void
app_init_terminal (Application *self)
{
	TERMO_CHECK_VERSION;
	if (!(self->tk = termo_new (STDIN_FILENO, NULL, 0)))
		abort ();
	if (!initscr () || nonl () == ERR)
		abort ();

	// By default we don't use any colors so they're not required...
	if (start_color () == ERR
	 || use_default_colors () == ERR
	 || COLOR_PAIRS <= ATTRIBUTE_COUNT)
		return;

	gboolean failed = FALSE;
	for (int a = 0; a < ATTRIBUTE_COUNT; a++)
	{
		if (self->attrs[a].fg >= COLORS || self->attrs[a].fg < -1
		 || self->attrs[a].bg >= COLORS || self->attrs[a].bg < -1)
		{
			failed = TRUE;
			continue;
		}

		init_pair (a + 1, self->attrs[a].fg, self->attrs[a].bg);
		self->attrs[a].attrs |= COLOR_PAIR (a + 1);
	}

	// ...thus we can reset back to defaults even after initializing some
	if (failed)
		app_init_attrs (self);
}

/// Free any resources used by the application.
static void
app_destroy (Application *self)
{
	if (self->loop)
		g_main_loop_unref (self->loop);
	if (self->tk)
		termo_destroy (self->tk);
	if (self->tk_timer)
		g_source_remove (self->tk_timer);

	if (self->selection_timer)
		g_source_remove (self->selection_timer);
	g_free (self->selection_contents);

	g_ptr_array_free (self->entries, TRUE);
	g_free (self->search_label);
	g_array_free (self->input, TRUE);
	g_array_free (self->dictionaries, TRUE);

	g_iconv_close (self->ucs4_to_locale);
}

/// Run the main event dispatch loop.
static void
app_run (Application *self)
{
	if (self->loop)
		g_main_loop_run (self->loop);
#ifdef WITH_GTK
	else
		gtk_main ();
#endif  // WITH_GTK
}

/// Quit the main event dispatch loop.
static void
app_quit (Application *self)
{
	if (self->loop)
		g_main_loop_quit (self->loop);
#ifdef WITH_GTK
	else
		gtk_main_quit ();
#endif  // WITH_GTK
}

/// Returns if the Unicode character is representable in the current locale.
static gboolean
app_is_character_in_locale (Application *self, gunichar ch)
{
	// Avoid the overhead joined with calling iconv() for all characters
	if (self->locale_is_utf8)
		return TRUE;

	gchar *tmp = g_convert_with_iconv ((const gchar *) &ch, sizeof ch,
		self->ucs4_to_locale, NULL, NULL, NULL);
	if (!tmp)
		return FALSE;
	g_free (tmp);
	return TRUE;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// Necessary abstraction to simplify aligned, formatted character output

typedef struct row_char                 RowChar;
typedef struct row_buffer               RowBuffer;

struct row_char
{
	gunichar c;                         ///< Unicode codepoint
	chtype attrs;                       ///< Special attributes
	int width;                          ///< How many cells this takes
};

struct row_buffer
{
	Application *app;                   ///< Reference to Application
	GArray *chars;                      ///< Characters
	int total_width;                    ///< Total width of all characters
};

static void
row_buffer_init (RowBuffer *self, Application *app)
{
	self->app = app;
	self->chars = g_array_new (FALSE, TRUE, sizeof (RowChar));
	self->total_width = 0;
}

#define row_buffer_free(self) g_array_unref ((self)->chars)

/// Replace invalid chars and push all codepoints to the array w/ attributes.
static void
row_buffer_append (RowBuffer *self, const gchar *str, chtype attrs)
{
	glong ucs4_len;
	gunichar *ucs4 = g_utf8_to_ucs4_fast (str, -1, &ucs4_len);
	for (glong i = 0; i < ucs4_len; i++)
	{
		// XXX: this is very crude as it disrespects combining marks
		gunichar c =
			app_is_character_in_locale (self->app, ucs4[i]) ? ucs4[i] : '?';
		struct row_char rc = { c, attrs, unichar_width (c) };
		g_array_append_val (self->chars, rc);
		self->total_width += rc.width;
	}
	g_free (ucs4);
}

/// Pop as many codepoints as needed to free up "space" character cells.
/// Given the suffix nature of combining marks, this should work pretty fine.
static gint
row_buffer_pop_cells (RowBuffer *self, gint space)
{
	int made = 0;
	while (self->chars->len && made < space)
	{
		guint last = self->chars->len - 1;
		made += g_array_index (self->chars, RowChar, last).width;
		g_array_remove_index (self->chars, last);
	}
	self->total_width -= made;
	return made;
}

static void
row_buffer_ellipsis (RowBuffer *self, int target, chtype attrs)
{
	row_buffer_pop_cells (self, self->total_width - target);

	gunichar ellipsis = L'…';
	if (app_is_character_in_locale (self->app, ellipsis))
	{
		if (self->total_width >= target)
			row_buffer_pop_cells (self, 1);
		if (self->total_width + 1 <= target)
			row_buffer_append (self, "…", attrs);
	}
	else if (target >= 3)
	{
		if (self->total_width >= target)
			row_buffer_pop_cells (self, 3);
		if (self->total_width + 3 <= target)
			row_buffer_append (self, "...", attrs);
	}
}

static void
row_buffer_print (RowBuffer *self, gunichar *ucs4, size_t len, chtype attrs)
{
	gsize locale_str_len;
	guchar *str = (guchar *) g_convert_with_iconv
		((const gchar *) ucs4, len * sizeof *ucs4,
		self->app->ucs4_to_locale, NULL, &locale_str_len, NULL);
	g_return_if_fail (str != NULL);

	for (gsize i = 0; i < locale_str_len; i++)
		addch (str[i] | attrs);
	g_free (str);
}

static void
row_buffer_flush (RowBuffer *self)
{
	if (!self->chars->len)
		return;

	gunichar ucs4[self->chars->len];
	for (guint i = 0; i < self->chars->len; i++)
		ucs4[i] = g_array_index (self->chars, RowChar, i).c;

	guint mark = 0;
	for (guint i = 1; i < self->chars->len; i++)
	{
		chtype attrs = g_array_index (self->chars, RowChar, i - 1).attrs;
		if (attrs != g_array_index (self->chars, RowChar, i).attrs)
		{
			row_buffer_print (self, ucs4 + mark, i - mark, attrs);
			mark = i;
		}
	}
	row_buffer_print (self, ucs4 + mark, self->chars->len - mark,
		g_array_index (self->chars, RowChar, self->chars->len - 1).attrs);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/// Write the given UTF-8 string padded with spaces.
/// @param[in] n  The number of characters to write, or -1 for the whole string.
/// @param[in] attrs  Text attributes for the text, without padding.
///                   To change the attributes of all output, use attrset().
/// @return The number of characters output.
static gsize
app_add_utf8_string (Application *self, const gchar *str, chtype attrs, int n)
{
	if (!n)
		return 0;

	RowBuffer buf;
	row_buffer_init (&buf, self);
	row_buffer_append (&buf, str, attrs);

	if (n < 0)
		n = buf.total_width;
	if (buf.total_width > n)
		row_buffer_ellipsis (&buf, n, attrs);

	row_buffer_flush (&buf);
	for (int i = buf.total_width; i < n; i++)
		addch (' ');

	row_buffer_free (&buf);
	return n;
}

/// Render the top bar.
static void
app_redraw_top (Application *self)
{
	attrset (APP_ATTR (HEADER));
	mvwhline (stdscr, 0, 0, APP_ATTR (HEADER), COLS);
	gsize indent = app_add_utf8_string (self, APP_TITLE, A_BOLD, -1);

	attrset (0);
	for (guint i = 0; i < self->dictionaries->len; i++)
	{
		Dictionary *dict = &g_array_index (self->dictionaries, Dictionary, i);
		indent += app_add_utf8_string (self, dict->name,
			(self->dictionaries->len > 1 && self->dict == dict->dict)
				? APP_ATTR (ACTIVE) : APP_ATTR (HEADER),
			MIN (COLS - indent, dict->name_width));
	}

	attrset (APP_ATTR (SEARCH));
	mvwhline (stdscr, 1, 0, APP_ATTR (SEARCH), COLS);
	indent = app_add_utf8_string (self, self->search_label, 0, -1);

	gchar *input_utf8 = g_ucs4_to_utf8
		((gunichar *) self->input->data, -1, NULL, NULL, NULL);
	g_return_if_fail (input_utf8 != NULL);

	int word_attrs = 0;
	if (self->input_confirmed)
		word_attrs |= A_BOLD;
	app_add_utf8_string (self, input_utf8, word_attrs, COLS - indent - 1);
	g_free (input_utf8);

	guint offset, i;
	for (offset = i = 0; i < self->input_pos; i++)
		// This may be inconsistent with the output of app_add_utf8_string()
		offset += unichar_width (g_array_index (self->input, gunichar, i));

	move (1, MIN ((gint) (indent + offset), COLS - 1));
	refresh ();
}

/// Computes width for the left column.
static guint
app_get_left_column_width (Application *self)
{
	gint width = COLS * self->division + 0.5;
	if (width < 1)
		width = 1;
	else if (width > COLS - 2)
		width = COLS - 2;
	return width;
}

/// Display a message in the view area.
static void
app_show_message (Application *self, const gchar *lines[], gsize len)
{
	gint top = (LINES - TOP_BAR_CUTOFF - len) / 2;

	gint i;
	for (i = 0; i < top; i++)
	{
		move (TOP_BAR_CUTOFF + i, 0);
		clrtoeol ();
	}

	attrset (0);
	while (len-- && i < LINES - TOP_BAR_CUTOFF)
	{
		move (TOP_BAR_CUTOFF + i, 0);
		clrtoeol ();

		gint x = (COLS - g_utf8_strlen (*lines, -1)) / 2;
		if (x < 0)
			x = 0;

		move (TOP_BAR_CUTOFF + i, x);
		app_add_utf8_string (self, *lines, 0, COLS - x);

		lines++;
		i++;
	}

	clrtobot ();
	refresh ();
}

/// Show some information about the program.
static void
app_show_help (Application *self)
{
	const gchar *lines[] =
	{
		PROJECT_NAME " " PROJECT_VERSION,
		_("Terminal UI for StarDict dictionaries"),
		"Copyright (c) 2013 - 2016, Přemysl Janouch",
		"",
		_("Type to search")
	};

	app_show_message (self, lines, G_N_ELEMENTS (lines));
}

/// Redraw the dictionary view.
static void
app_redraw_view (Application *self)
{
	if (self->show_help)
	{
		app_show_help (self);
		return;
	}

	move (TOP_BAR_CUTOFF, 0);
	clrtobot ();

	// TODO: clean this stuff up a bit, it's all rather ugly
	gchar *input_utf8 = g_ucs4_to_utf8
		((gunichar *) self->input->data, -1, NULL, NULL, NULL);

	guint i, k = self->top_offset, shown = 0;
	for (i = 0; i < self->entries->len; i++)
	{
		ViewEntry *ve = g_ptr_array_index (self->entries, i);
		for (; k < ve->definitions_length; k++)
		{
			int attrs = ((self->top_position + i) & 1)
				? APP_ATTR (ODD) : APP_ATTR (EVEN);
			if (shown == self->selected)      attrs |= A_REVERSE;
			gboolean last = k + 1 == ve->definitions_length;
			if (last && self->underline_last) attrs |= A_UNDERLINE;

			attrset (attrs);

			RowBuffer buf;
			row_buffer_init (&buf, self);

			size_t common = 0;
			if (self->hl_prefix)
			{
				common = stardict_longest_common_collation_prefix
					(self->dict, ve->word, input_utf8);

				gchar *prefix = g_strndup (ve->word, common);
				row_buffer_append (&buf, prefix, A_BOLD);
				g_free (prefix);
			}
			row_buffer_append (&buf, ve->word + common, 0);

			gint left_width = app_get_left_column_width (self);
			if (buf.total_width > left_width)
				row_buffer_ellipsis (&buf, left_width, attrs);

			row_buffer_flush (&buf);
			for (int i = buf.total_width; i < left_width + 1; i++)
				addch (' ');
			row_buffer_free (&buf);

			app_add_utf8_string (self,
				ve->definitions[k], 0, COLS - left_width - 1);

			if ((gint) ++shown == LINES - TOP_BAR_CUTOFF)
				goto done;
		}

		k = 0;
	}

done:
	free (input_utf8);
	attrset (0);
	refresh ();
}

/// Just prepends a new view entry into the entries array.
static ViewEntry *
prepend_entry (Application *self, guint32 position)
{
	StardictIterator *iterator = stardict_iterator_new (self->dict, position);
	ViewEntry *ve = view_entry_new (iterator);
	g_object_unref (iterator);

	g_ptr_array_add (self->entries, NULL);
	memmove (self->entries->pdata + 1, self->entries->pdata,
		sizeof ve * (self->entries->len - 1));
	return g_ptr_array_index (self->entries, 0) = ve;
}

/// Just appends a new view entry to the entries array.
static ViewEntry *
append_entry (Application *self, guint32 position)
{
	ViewEntry *ve = NULL;
	StardictIterator *iterator = stardict_iterator_new
		(self->dict, position);
	if (stardict_iterator_is_valid (iterator))
	{
		ve = view_entry_new (iterator);
		g_ptr_array_add (self->entries, ve);
	}
	g_object_unref (iterator);
	return ve;
}

/// Counts the number of definitions available for seeing.
static guint
app_count_view_items (Application *self)
{
	guint i, n_definitions = 0;
	for (i = 0; i < self->entries->len; i++)
	{
		ViewEntry *entry = g_ptr_array_index (self->entries, i);
		n_definitions += entry->definitions_length;
	}
	return n_definitions;
}

/// Scroll up @a n entries.  Doesn't redraw.
static gboolean
app_scroll_up (Application *self, guint n)
{
	gboolean success = TRUE;
	guint n_definitions = app_count_view_items (self);
	while (n--)
	{
		if (self->top_offset > 0)
		{
			self->top_offset--;
			continue;
		}

		// We've reached the top
		if (self->top_position == 0)
		{
			success = FALSE;
			break;
		}

		ViewEntry *ve = prepend_entry (self, --self->top_position);
		self->top_offset = ve->definitions_length - 1;
		n_definitions += ve->definitions_length;

		// Remove the last entry if not shown
		ViewEntry *last_entry =
			g_ptr_array_index (self->entries, self->entries->len - 1);
		if ((gint) (n_definitions - self->top_offset
			- last_entry->definitions_length) >= LINES - TOP_BAR_CUTOFF)
		{
			n_definitions -= last_entry->definitions_length;
			g_ptr_array_remove_index_fast
				(self->entries, self->entries->len - 1);
		}
	}
	return success;
}

/// Scroll down @a n entries.  Doesn't redraw.
static gboolean
app_scroll_down (Application *self, guint n)
{
	gboolean success = TRUE;
	guint n_definitions = app_count_view_items (self);
	while (n--)
	{
		if (self->entries->len == 0)
		{
			success = FALSE;
			break;
		}

		ViewEntry *first_entry = g_ptr_array_index (self->entries, 0);
		if (self->top_offset < first_entry->definitions_length - 1)
			self->top_offset++;
		else
		{
			n_definitions -= first_entry->definitions_length;
			g_ptr_array_remove_index (self->entries, 0);
			self->top_position++;
			self->top_offset = 0;
		}

		if ((gint) (n_definitions - self->top_offset) < LINES - TOP_BAR_CUTOFF)
		{
			ViewEntry *ve = append_entry (self,
				self->top_position + self->entries->len);
			if (ve != NULL)
				n_definitions += ve->definitions_length;
		}
	}

	// Fix cursor to not point below the view items
	if (self->selected >= n_definitions - self->top_offset)
		self->selected  = n_definitions - self->top_offset - 1;
	return success;
}

/// Moves the selection one entry up.
static gboolean
app_one_entry_up (Application *self)
{
	if (self->selected == 0 && self->top_offset == 0)
	{
		if (self->top_position == 0)
			return FALSE;
		prepend_entry (self, --self->top_position);
	}

	// Find the last entry that starts above the selection
	gint first = -self->top_offset;
	guint i;
	for (i = 0; i < self->entries->len; i++)
	{
		ViewEntry *ve = g_ptr_array_index (self->entries, i);
		gint new_first = first + ve->definitions_length;
		if (new_first >= (gint) self->selected)
			break;
		first = new_first;
	}

	if (first < 0)
	{
		self->selected = 0;
		app_scroll_up (self, -first);
	}
	else
		self->selected = first;

	app_redraw_view (self);
	return TRUE;
}

/// Moves the selection one entry down.
static void
app_one_entry_down (Application *self)
{
	// Find the first entry that starts below the selection
	gint first = -self->top_offset;
	guint i;
	for (i = 0; i < self->entries->len; i++)
	{
		ViewEntry *ve = g_ptr_array_index (self->entries, i);
		first += ve->definitions_length;
		if (first > (gint) self->selected)
			break;
	}

	if (first > LINES - TOP_BAR_CUTOFF - 1)
	{
		self->selected = LINES - TOP_BAR_CUTOFF - 1;
		app_scroll_down (self, first - (LINES - TOP_BAR_CUTOFF - 1));
	}
	else
		self->selected = first;

	app_redraw_view (self);
}

/// Redraw everything.
static void
app_redraw (Application *self)
{
	app_redraw_view (self);
	app_redraw_top (self);
}

/// Search for the current entry.
static void
app_search_for_entry (Application *self)
{
	gchar *input_utf8 = g_ucs4_to_utf8
		((gunichar *) self->input->data, -1, NULL, NULL, NULL);
	g_return_if_fail (input_utf8 != NULL);

	StardictIterator *iterator =
		stardict_dict_search (self->dict, input_utf8, NULL);
	g_free (input_utf8);

	self->top_position = stardict_iterator_get_offset (iterator);
	self->top_offset = 0;
	self->selected = 0;
	g_object_unref (iterator);

	self->show_help = FALSE;
	app_reload_view (self);

	// If the user wants it centered, just move the view up half a screen;
	// actually, one third seems to be a better guess
	if (self->center_search)
	{
		for (int half = (LINES - TOP_BAR_CUTOFF) / 3; half > 0; half--)
			if (app_scroll_up (self, 1))
				self->selected++;
			else
				break;
	}

	app_redraw_view (self);
}

/// Switch to a different dictionary by number.
static gboolean
app_goto_dictionary (Application *self, guint n)
{
	if (n >= self->dictionaries->len)
		return FALSE;

	Dictionary *dict = &g_array_index (self->dictionaries, Dictionary, n);
	self->dict = dict->dict;
	app_search_for_entry (self);
	app_redraw_top (self);
	return TRUE;
}

/// The terminal has been resized, make appropriate changes.
static gboolean
app_process_resize (Application *self)
{
	app_reload_view (self);

	guint n_visible = app_count_view_items (self) - self->top_offset;
	if ((gint) n_visible > LINES - TOP_BAR_CUTOFF)
		n_visible = LINES - TOP_BAR_CUTOFF;

	if (self->selected >= n_visible)
	{
		app_scroll_down (self, self->selected - n_visible + 1);
		self->selected = n_visible - 1;
	}

	app_redraw (self);
	return TRUE;
}

// --- User input handling -----------------------------------------------------

/// All the actions that can be performed by the user.
typedef enum user_action                UserAction;

enum user_action
{
	USER_ACTION_NONE,

	USER_ACTION_QUIT,
	USER_ACTION_REDRAW,

	USER_ACTION_MOVE_SPLITTER_LEFT,
	USER_ACTION_MOVE_SPLITTER_RIGHT,

	USER_ACTION_GOTO_ENTRY_PREVIOUS,
	USER_ACTION_GOTO_ENTRY_NEXT,
	USER_ACTION_GOTO_DEFINITION_PREVIOUS,
	USER_ACTION_GOTO_DEFINITION_NEXT,
	USER_ACTION_GOTO_PAGE_PREVIOUS,
	USER_ACTION_GOTO_PAGE_NEXT,

	USER_ACTION_INPUT_CONFIRM,
	USER_ACTION_INPUT_HOME,
	USER_ACTION_INPUT_END,
	USER_ACTION_INPUT_LEFT,
	USER_ACTION_INPUT_RIGHT,
	USER_ACTION_INPUT_DELETE_PREVIOUS,
	USER_ACTION_INPUT_DELETE_NEXT,
	USER_ACTION_INPUT_DELETE_TO_HOME,
	USER_ACTION_INPUT_DELETE_TO_END,
	USER_ACTION_INPUT_DELETE_PREVIOUS_WORD,
	USER_ACTION_INPUT_TRANSPOSE,

	USER_ACTION_COUNT
};

#define SAVE_CURSOR                 \
	int last_x, last_y;             \
	getyx (stdscr, last_y, last_x);

#define RESTORE_CURSOR              \
	move (last_y, last_x);          \
	refresh ();

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static gboolean
app_process_user_action (Application *self, UserAction action)
{
	SAVE_CURSOR
	switch (action)
	{
	case USER_ACTION_QUIT:
		return FALSE;
	case USER_ACTION_REDRAW:
		clear ();
		app_redraw (self);
		return TRUE;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	case USER_ACTION_MOVE_SPLITTER_LEFT:
		self->division = (app_get_left_column_width (self) - 1.) / COLS;
		app_redraw_view (self);
		RESTORE_CURSOR
		return TRUE;
	case USER_ACTION_MOVE_SPLITTER_RIGHT:
		self->division = (app_get_left_column_width (self) + 1.) / COLS;
		app_redraw_view (self);
		RESTORE_CURSOR
		return TRUE;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	case USER_ACTION_GOTO_DEFINITION_PREVIOUS:
		if (self->selected > 0)
			self->selected--;
		else
			app_scroll_up (self, 1);
		app_redraw_view (self);
		RESTORE_CURSOR
		return TRUE;
	case USER_ACTION_GOTO_DEFINITION_NEXT:
		if ((gint) self->selected < LINES - TOP_BAR_CUTOFF - 1 &&
			self->selected < app_count_view_items (self) - self->top_offset - 1)
			self->selected++;
		else
			app_scroll_down (self, 1);
		app_redraw_view (self);
		RESTORE_CURSOR
		return TRUE;

	case USER_ACTION_GOTO_ENTRY_PREVIOUS:
		app_one_entry_up (self);
		RESTORE_CURSOR
		return TRUE;
	case USER_ACTION_GOTO_ENTRY_NEXT:
		app_one_entry_down (self);
		RESTORE_CURSOR
		return TRUE;

	case USER_ACTION_GOTO_PAGE_PREVIOUS:
		app_scroll_up (self, LINES - TOP_BAR_CUTOFF);
		// FIXME: selection
		app_redraw_view (self);
		RESTORE_CURSOR
		return TRUE;
	case USER_ACTION_GOTO_PAGE_NEXT:
		app_scroll_down (self, LINES - TOP_BAR_CUTOFF);
		// FIXME: selection
		app_redraw_view (self);
		RESTORE_CURSOR
		return TRUE;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	case USER_ACTION_INPUT_HOME:
		self->input_pos = 0;
		app_redraw_top (self);
		return TRUE;
	case USER_ACTION_INPUT_END:
		self->input_pos = self->input->len;
		app_redraw_top (self);
		return TRUE;
	case USER_ACTION_INPUT_LEFT:
		if (self->input_pos > 0)
		{
			self->input_pos--;
			app_redraw_top (self);
		}
		return TRUE;
	case USER_ACTION_INPUT_RIGHT:
		if (self->input_pos < self->input->len)
		{
			self->input_pos++;
			app_redraw_top (self);
		}
		return TRUE;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	case USER_ACTION_INPUT_CONFIRM:
		self->input_confirmed = TRUE;
		app_redraw_top (self);
		return TRUE;

	case USER_ACTION_INPUT_TRANSPOSE:
	{
		if (!self->input_pos || self->input->len < 2)
			break;

		guint start = self->input_pos - 1;
		if (self->input_pos >= self->input->len)
			start--;

		gunichar tmp = g_array_index (self->input, gunichar, start);
		g_array_index (self->input, gunichar, start)
			= g_array_index (self->input, gunichar, start + 1);
		g_array_index (self->input, gunichar, start + 1) = tmp;

		if (self->input_pos < self->input->len)
			self->input_pos++;

		app_search_for_entry (self);
		app_redraw_top (self);
		return TRUE;
	}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	case USER_ACTION_INPUT_DELETE_PREVIOUS:
		if (self->input_pos > 0)
		{
			g_array_remove_index (self->input, --self->input_pos);
			app_search_for_entry (self);
			app_redraw_top (self);
		}
		return TRUE;
	case USER_ACTION_INPUT_DELETE_NEXT:
		if (self->input_pos < self->input->len)
		{
			g_array_remove_index (self->input, self->input_pos);
			app_search_for_entry (self);
			app_redraw_top (self);
		}
		return TRUE;
	case USER_ACTION_INPUT_DELETE_TO_HOME:
		if (self->input->len != 0)
		{
			g_array_remove_range (self->input, 0, self->input_pos);
			self->input_pos = 0;

			app_search_for_entry (self);
			app_redraw_top (self);
		}
		return TRUE;
	case USER_ACTION_INPUT_DELETE_TO_END:
		if (self->input_pos < self->input->len)
		{
			g_array_remove_range (self->input,
				self->input_pos, self->input->len - self->input_pos);
			app_search_for_entry (self);
			app_redraw_top (self);
		}
		return TRUE;
	case USER_ACTION_INPUT_DELETE_PREVIOUS_WORD:
	{
		if (self->input_pos == 0)
			return TRUE;

		gint i = self->input_pos;
		while (i)
			if (g_array_index (self->input, gunichar, --i) != L' ')
				break;
		while (i--)
			if (g_array_index (self->input, gunichar,   i) == L' ')
				break;

		i++;
		g_array_remove_range (self->input, i, self->input_pos - i);
		self->input_pos = i;

		app_search_for_entry (self);
		app_redraw_top (self);
		return TRUE;
	}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	case USER_ACTION_NONE:
		return TRUE;
	default:
		g_assert_not_reached ();
	}
	return TRUE;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static gboolean
app_process_keysym (Application *self, termo_key_t *event)
{
	UserAction action = USER_ACTION_NONE;
	typedef const UserAction ActionMap[TERMO_N_SYMS];

	static ActionMap actions =
	{
		[TERMO_SYM_ESCAPE]    = USER_ACTION_QUIT,

		[TERMO_SYM_UP]        = USER_ACTION_GOTO_DEFINITION_PREVIOUS,
		[TERMO_SYM_DOWN]      = USER_ACTION_GOTO_DEFINITION_NEXT,
		[TERMO_SYM_PAGEUP]    = USER_ACTION_GOTO_PAGE_PREVIOUS,
		[TERMO_SYM_PAGEDOWN]  = USER_ACTION_GOTO_PAGE_NEXT,

		[TERMO_SYM_ENTER]     = USER_ACTION_INPUT_CONFIRM,

		[TERMO_SYM_HOME]      = USER_ACTION_INPUT_HOME,
		[TERMO_SYM_END]       = USER_ACTION_INPUT_END,
		[TERMO_SYM_LEFT]      = USER_ACTION_INPUT_LEFT,
		[TERMO_SYM_RIGHT]     = USER_ACTION_INPUT_RIGHT,

		[TERMO_SYM_BACKSPACE] = USER_ACTION_INPUT_DELETE_PREVIOUS,
		[TERMO_SYM_DEL]       = USER_ACTION_INPUT_DELETE_PREVIOUS,
		[TERMO_SYM_DELETE]    = USER_ACTION_INPUT_DELETE_NEXT,
	};
	static ActionMap actions_alt =
	{
		[TERMO_SYM_LEFT]      = USER_ACTION_MOVE_SPLITTER_LEFT,
		[TERMO_SYM_RIGHT]     = USER_ACTION_MOVE_SPLITTER_RIGHT,
	};
	static ActionMap actions_ctrl =
	{
		[TERMO_SYM_UP]        = USER_ACTION_GOTO_ENTRY_PREVIOUS,
		[TERMO_SYM_DOWN]      = USER_ACTION_GOTO_ENTRY_NEXT,
	};

	if (!event->modifiers)
		action = actions[event->code.sym];
	else if (event->modifiers == TERMO_KEYMOD_ALT)
		action = actions_alt[event->code.sym];
	else if (event->modifiers == TERMO_KEYMOD_CTRL)
		action = actions_ctrl[event->code.sym];

	return app_process_user_action (self, action);
}

static gboolean
app_process_ctrl_key (Application *self, termo_key_t *event)
{
	static const UserAction actions[32] =
	{
		[CTRL_KEY ('L')]      = USER_ACTION_REDRAW,

		[CTRL_KEY ('P')]      = USER_ACTION_GOTO_DEFINITION_PREVIOUS,
		[CTRL_KEY ('N')]      = USER_ACTION_GOTO_DEFINITION_NEXT,
		[CTRL_KEY ('B')]      = USER_ACTION_GOTO_PAGE_PREVIOUS,
		[CTRL_KEY ('F')]      = USER_ACTION_GOTO_PAGE_NEXT,

		[CTRL_KEY ('A')]      = USER_ACTION_INPUT_HOME,
		[CTRL_KEY ('E')]      = USER_ACTION_INPUT_END,

		[CTRL_KEY ('H')]      = USER_ACTION_INPUT_DELETE_PREVIOUS,
		[CTRL_KEY ('K')]      = USER_ACTION_INPUT_DELETE_TO_END,
		[CTRL_KEY ('W')]      = USER_ACTION_INPUT_DELETE_PREVIOUS_WORD,
		[CTRL_KEY ('U')]      = USER_ACTION_INPUT_DELETE_TO_HOME,
		[CTRL_KEY ('T')]      = USER_ACTION_INPUT_TRANSPOSE,
	};

	gint64 i = (gint64) event->code.codepoint - 'a' + 1;
	if (i > 0 && i < (gint64) G_N_ELEMENTS (actions))
		return app_process_user_action (self, actions[i]);

	return TRUE;
}

static gboolean
app_process_alt_key (Application *self, termo_key_t *event)
{
	if (event->code.codepoint == 'c')
		self->center_search = !self->center_search;

	if (event->code.codepoint >= '0'
	 && event->code.codepoint <= '9')
	{
		gint n = event->code.codepoint - '0';
		if (!app_goto_dictionary (self, (n == 0 ? 10 : n) - 1))
			beep ();
	}
	return TRUE;
}

static gboolean
app_process_key (Application *self, termo_key_t *event)
{
	if (event->modifiers == TERMO_KEYMOD_CTRL)
		return app_process_ctrl_key (self, event);
	if (event->modifiers == TERMO_KEYMOD_ALT)
		return app_process_alt_key (self, event);
	if (event->modifiers)
		return TRUE;

	gunichar c = event->code.codepoint;
	if (!g_unichar_isprint (c))
	{
		beep ();
		return TRUE;
	}

	if (self->input_confirmed)
	{
		if (self->input->len != 0)
			g_array_remove_range (self->input, 0, self->input->len);
		self->input_pos = 0;
		self->input_confirmed = FALSE;
	}

	g_array_insert_val (self->input, self->input_pos++, c);
	app_search_for_entry (self);
	app_redraw_top (self);
	return TRUE;
}

static void
app_process_left_mouse_click (Application *self, int line, int column)
{
	SAVE_CURSOR
	if (line == 0)
	{
		int indent = strlen (APP_TITLE);
		if (column < indent)
			return;

		Dictionary *dicts = (Dictionary *) self->dictionaries->data;
		for (guint i = 0; i < self->dictionaries->len; i++)
		{
			if (column < (indent += dicts[i].name_width))
			{
				app_goto_dictionary (self, i);
				return;
			}
		}
	}
	else if (line == 1)
	{
		// FIXME: this is only an approximation
		gsize label_len = g_utf8_strlen (self->search_label, -1);
		gint pos = column - label_len;
		if (pos >= 0)
		{
			self->input_pos = MIN ((guint) pos, self->input->len);
			move (1, label_len + self->input_pos);
			refresh ();
		}
	}
	else if (line <= (int) (app_count_view_items (self) - self->top_offset))
	{
		self->selected = line - TOP_BAR_CUTOFF;
		app_redraw_view (self);
		RESTORE_CURSOR
	}
}

/// Process mouse input.
static gboolean
app_process_mouse (Application *self, termo_key_t *event)
{
	int line, column, button;
	termo_mouse_event_t type;
	termo_interpret_mouse (self->tk, event, &type, &button, &line, &column);

	if (type != TERMO_MOUSE_PRESS)
		return TRUE;

	if (button == 1)
		app_process_left_mouse_click (self, line, column);
	else if (button == 4)
		app_process_user_action (self, USER_ACTION_GOTO_DEFINITION_PREVIOUS);
	else if (button == 5)
		app_process_user_action (self, USER_ACTION_GOTO_DEFINITION_NEXT);

	return TRUE;
}

/// Process input events from the terminal.
static gboolean
app_process_termo_event (Application *self, termo_key_t *event)
{
	switch (event->type)
	{
	case TERMO_TYPE_MOUSE:
		return app_process_mouse (self, event);
	case TERMO_TYPE_KEY:
		return app_process_key (self, event);
	case TERMO_TYPE_KEYSYM:
		return app_process_keysym (self, event);
	default:
		return TRUE;
	}
}

// --- SIGWINCH ----------------------------------------------------------------

static int g_winch_pipe[2];            ///< SIGWINCH signalling pipe.

static void
winch_handler (int signum)
{
	(void) signum;
	write (g_winch_pipe[1], "x", 1);
}

static void
install_winch_handler (void)
{
	struct sigaction act, oldact;

	if (pipe (g_winch_pipe) == -1)
		abort ();

	act.sa_handler = winch_handler;
	act.sa_flags = SA_RESTART;
	sigemptyset (&act.sa_mask);
	sigaction (SIGWINCH, &act, &oldact);
}

// --- Initialisation, event handling ------------------------------------------

static gboolean on_stdin_input_timeout (gpointer data);

static gboolean
process_stdin_input (G_GNUC_UNUSED GIOChannel *source,
	G_GNUC_UNUSED GIOCondition condition, gpointer data)
{
	Application *app = data;
	if (app->tk_timer)
	{
		g_source_remove (app->tk_timer);
		app->tk_timer = 0;
	}

	termo_advisereadable (app->tk);

	termo_key_t event;
	termo_result_t res;
	while ((res = termo_getkey (app->tk, &event)) == TERMO_RES_KEY)
		if (!app_process_termo_event (app, &event))
			goto quit;

	if (res == TERMO_RES_AGAIN)
		app->tk_timer = g_timeout_add (termo_get_waittime (app->tk),
			on_stdin_input_timeout, app);
	else if (res == TERMO_RES_ERROR || res == TERMO_RES_EOF)
		goto quit;

	return TRUE;

quit:
	app_quit (app);
	return FALSE;
}

static gboolean
on_stdin_input_timeout (gpointer data)
{
	Application *app = data;
	termo_key_t event;
	if (termo_getkey_force (app->tk, &event) == TERMO_RES_KEY)
		if (!app_process_termo_event (app, &event))
			app_quit (app);

	app->tk_timer = 0;
	return FALSE;
}

static gboolean
process_winch_input (GIOChannel *source,
	G_GNUC_UNUSED GIOCondition condition, gpointer data)
 {
	char c;
	(void) read (g_io_channel_unix_get_fd (source), &c, 1);

	update_curses_terminal_size ();
	app_process_resize (data);
	return TRUE;
}

static gboolean
on_terminated (gpointer user_data)
{
	app_quit (user_data);
	return TRUE;
}

#ifdef WITH_GTK
static void
app_set_input (Application *self, const gchar *input)
{
	glong size;
	gunichar *output = g_utf8_to_ucs4 (input, -1, NULL, &size, NULL);

	// XXX: signal invalid data?
	if (!output)
		return;

	g_array_free (self->input, TRUE);
	self->input = g_array_new (TRUE, FALSE, sizeof (gunichar));
	self->input_pos = 0;

	gunichar *p = output;
	while (size--)
	{
		// XXX: skip?
		if (!g_unichar_isprint (*p))
			break;

		g_array_insert_val (self->input, self->input_pos++, *p++);
	}
	g_free (output);

	self->input_confirmed = FALSE;
	app_search_for_entry (self);
	app_redraw_top (self);
}

static void
on_selection_text_received (G_GNUC_UNUSED GtkClipboard *clipboard,
	const gchar *text, gpointer data)
{
	Application *app = data;
	rearm_selection_watcher (app);

	if (text)
	{
		if (app->selection_contents && !strcmp (app->selection_contents, text))
			return;

		g_free (app->selection_contents);
		app->selection_contents = g_strdup (text);
		app_set_input (app, text);
	}
	else if (app->selection_contents)
	{
		g_free (app->selection_contents);
		app->selection_contents = NULL;
	}
}

static gboolean
on_selection_timer (gpointer data)
{
	Application *app = data;
	GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
	gtk_clipboard_request_text (clipboard, on_selection_text_received, app);

	app->selection_timer = 0;
	return FALSE;
}
#endif  // WITH_GTK

static void
log_handler_curses (Application *self, const gchar *message)
{
	// Beep, beep, I'm a jeep; let the user know
	beep ();

	// We certainly don't want to end up in a possibly infinite recursion
	static gboolean in_processing;
	if (in_processing)
		return;

	in_processing = TRUE;
	SAVE_CURSOR

	attrset (A_REVERSE);
	mvwhline (stdscr, 0, 0, A_REVERSE, COLS);
	app_add_utf8_string (self, message, 0, COLS);

	RESTORE_CURSOR
	in_processing = FALSE;
}

static void
log_handler (const gchar *domain, GLogLevelFlags level,
	const gchar *message, gpointer data)
{
	// There's probably no point in trying to display a fatal message nicely
	if (level & G_LOG_FLAG_FATAL)
		g_log_default_handler (domain, level, message, NULL);

	const gchar *prefix;
	switch (level & G_LOG_LEVEL_MASK)
	{
	case G_LOG_LEVEL_ERROR:    prefix = "E"; break;
	case G_LOG_LEVEL_CRITICAL: prefix = "C"; break;
	case G_LOG_LEVEL_WARNING:  prefix = "W"; break;
	case G_LOG_LEVEL_MESSAGE:  prefix = "M"; break;
	case G_LOG_LEVEL_INFO:     prefix = "I"; break;
	case G_LOG_LEVEL_DEBUG:    prefix = "D"; break;
	default:                   prefix = "?";
	}

	gchar *out;
	if (domain)
		out = g_strdup_printf ("%s: %s: %s", prefix, domain, message);
	else
		out = g_strdup_printf ("%s: %s", prefix, message);

	// If the standard error output isn't redirected, try our best at showing
	// the message to the user; it will probably get overdrawn soon
	if (isatty (STDERR_FILENO))
		log_handler_curses (data, out);
	else
		fprintf (stderr, "%s\n", out);

	g_free (out);
}

int
main (int argc, char *argv[])
{
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
	if (glib_check_version (2, 36, 0))
		g_type_init ();
G_GNUC_END_IGNORE_DEPRECATIONS

	gboolean show_version = FALSE;
	GOptionEntry entries[] =
	{
		{ "version", 0, G_OPTION_FLAG_IN_MAIN,
		  G_OPTION_ARG_NONE, &show_version,
		  N_("Output version information and exit"), NULL },
		{ NULL }
	};

	if (!setlocale (LC_ALL, ""))
		g_printerr ("%s: %s\n", _("Warning"), _("failed to set the locale"));

	bindtextdomain (GETTEXT_PACKAGE, GETTEXT_DIRNAME);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	GError *error = NULL;
	GOptionContext *ctx = g_option_context_new
		(N_("[dictionary.ifo...] - StarDict terminal UI"));
	GOptionGroup *group = g_option_group_new ("", "", "", NULL, NULL);
	g_option_group_add_entries (group, entries);
	g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
	g_option_context_add_group (ctx, group);
	g_option_context_set_translation_domain (ctx, GETTEXT_PACKAGE);
	if (!g_option_context_parse (ctx, &argc, &argv, &error))
	{
		g_printerr ("%s: %s: %s\n", _("Error"), _("option parsing failed"),
			error->message);
		exit (EXIT_FAILURE);
	}
	g_option_context_free (ctx);

	if (show_version)
	{
		g_print (PROJECT_NAME " " PROJECT_VERSION "\n");
		exit (EXIT_SUCCESS);
	}

	Application app;
	app_init (&app, argv + 1);
	app_init_terminal (&app);
	app_redraw (&app);

	// g_unix_signal_add() cannot handle SIGWINCH
	install_winch_handler ();

	// GtkClipboard can internally issue some rather disruptive warnings
	g_log_set_default_handler (log_handler, &app);

	// Message loop
	guint watch_term  = g_unix_signal_add (SIGTERM, on_terminated, &app);
	guint watch_int   = g_unix_signal_add (SIGINT,  on_terminated, &app);
	guint watch_stdin = g_io_add_watch (g_io_channel_unix_new (STDIN_FILENO),
		G_IO_IN, process_stdin_input, &app);
	guint watch_winch = g_io_add_watch (g_io_channel_unix_new (g_winch_pipe[0]),
		G_IO_IN, process_winch_input, &app);

	app_run (&app);

	g_source_remove (watch_term);
	g_source_remove (watch_int);
	g_source_remove (watch_stdin);
	g_source_remove (watch_winch);

	endwin ();
	g_log_set_default_handler (g_log_default_handler, NULL);
	app_destroy (&app);

	if (close (g_winch_pipe[0]) == -1
	 || close (g_winch_pipe[1]) == -1)
		abort ();

	return 0;
}