File: libunix.cc

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (1889 lines) | stat: -rw-r--r-- 54,414 bytes parent folder | download | duplicates (2)
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
/**
 * @file
 * @brief Functions for unix and curses support
**/

/* Emulation of ancient Borland conio.
   Some of these are inspired by/stolen from the Linux-conio package
   by Mental EXPlotion. Hope you guys don't mind.
   The colour exchange system is perhaps a little overkill, but I wanted
   it to be general to support future changes.. The only thing not
   supported properly is black on black (used by curses for "normal" mode)
   and white on white (used by me for "bright black" (darkgrey) on black

   Jan 1998 Svante Gerhard <svante@algonet.se>                          */

#include "AppHdr.h"

#define _LIBUNIX_IMPLEMENTATION
#include "libunix.h"

#include <cassert>
#include <cctype>
#include <clocale>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <langinfo.h>
#include <term.h>
#include <termios.h>
#include <unistd.h>

#include "colour.h"
#include "cio.h"
#include "crash.h"
#include "libutil.h"
#include "state.h"
#include "tiles-build-specific.h"
#include "unicode.h"
#include "version.h"
#include "view.h"
#include "ui.h"

static struct termios def_term;
static struct termios game_term;

#ifdef USE_UNIX_SIGNALS
#include <signal.h>
#endif

#include <time.h>

// replace definitions from curses.h; not needed outside this file
#define HEADLESS_LINES 24
#define HEADLESS_COLS 80

// for some reason we use 1 indexing internally
static int headless_x = 1;
static int headless_y = 1;

// Its best if curses comes at the end (name conflicts with Solaris). -- bwr
#ifndef CURSES_INCLUDE_FILE
    #ifndef _XOPEN_SOURCE_EXTENDED
    #define _XOPEN_SOURCE_EXTENDED
    #endif

    #include <curses.h>
#else
    #include CURSES_INCLUDE_FILE
#endif

static bool _headless_mode = false;
bool in_headless_mode() { return _headless_mode; }
void enter_headless_mode() { _headless_mode = true; }

// Globals holding current text/backg. colours
// Note that these are internal colours, *not* curses colors.
/** @brief The current foreground @em colour. */
static COLOURS FG_COL = LIGHTGREY;

/** @brief The default foreground @em colour. */
static COLOURS FG_COL_DEFAULT = LIGHTGREY;

/** @brief The current background @em colour. */
static COLOURS BG_COL = BLACK;

/** @brief The default background @em colour. */
static COLOURS BG_COL_DEFAULT = BLACK;

struct curses_style
{
    attr_t attr;
    short color_pair;
};

/**
 * @brief Get curses attributes for the current internal color combination.
 *
 * @param fg
 *  The internal colour for the foreground.
 * @param bg
 *  The internal colour for the background.
 * @param adjust_background
 *  Determines which color in the pair is adjusted if adjustment is necessary.
 *  True chooses the background, while false chooses the foreground.
 *
 *  @return
 *   Returns the character attributes and colour pair index.
 */
static curses_style curs_attr(COLOURS fg, COLOURS bg, bool adjust_background = false);

/**
 * @brief Change the foreground colour and returns the resulting curses info.
 *
 * @param col
 *  An internal color with attached highlight.
 *
 *  @return
 *   Returns the character attributes and colour pair index.
 */
static curses_style curs_attr_bg(int col);

/**
 * @brief Change the background colour and returns the resulting curses info.
 *
 * @param col
 *  An internal color with attached highlight.
 *
 *  @return
 *   Returns the character attributes and colour pair index.
 */
static curses_style curs_attr_fg(int col);

/**
 * @brief Get curses attributes for the passed internal color combination.
 *
 * Performs colour mapping for both default colours as well as the color map.
 * Additionally, this function takes into consideration a passed highlight.
 *
 * @param fg
 *  The internal colour for the foreground.
 * @param bg
 *  The internal colour for the background.
 * @param highlight
 *  Internal color highlighting information.
 *
 *  @return
 *   Returns the character attributes and colour pair index.
 */
static curses_style curs_attr_mapped(COLOURS fg, COLOURS bg, int highlight);

/**
 * @brief Returns a curses color pair index for the passed fg/bg combo.
 *
 * Strips brightening flags from the passed colors according to the game
 * options. The current FG_COL_DEFAULT and BG_COL_DEFAULT values are used for
 * determining the default pair.
 *
 * @param fg
 *  The flagged curses color index for the foreground.
 * @param bg
 *  The flagged curses color index for the background.
 *
 * @return
 *  Returns the pair index associated with the combination, or the default
 *  pair, 0, if there is no associated pair.
 */
static short curs_calc_pair_safe(short fg, short bg);

/**
 * @brief Returns a curses color pair index for the passed fg/bg combo.
 *
 * Unlike curs_calc_pair_safe(short, short), the passed colors are treated
 * as-is and default colors are passed explicitly.
 *
 * @param fg
 *  The curses color index for the foreground.
 * @param bg
 *  The curses color index for the background.
 * @param fg_default
 *  The explicit curses color to consider the default foreground.
 * @param bg_default
 *  The explicit curses color to consider the default background.
 *
 * @return
 *  Returns the pair index associated with the combination, or the default
 *  pair, 0, if there is no associated pair.
 */
static short curs_calc_pair_safe(short fg, short bg, short fg_default,
    short bg_default);

/**
 * @brief Can extended colors be used directly instead of character attributes?
 *
 * @return
 *  True if extended colors can be used in lieu of attributes, false otherwise.
 */
static bool curs_can_use_extended_colors();

/**
 * @brief Is there a curses pair for the passed color combination?
 *
 * @param fg
 *  The raw curses foreground color.
 * @param bg
 *  The raw curses background color.
 *
 * @return
 *  True if there is a valid curses color pair for the combo, false otherwise.
 */
static bool curs_color_combo_has_pair(short fg, short bg);

/**
 * @brief Adjust a curses color pair to not be visually identical.
 *
 * Colors which are not equal in value may still be considered identical
 * in appearance depending on the current rendering assumption options.
 *
 * If the pair is not considered visually identical, no change is performed.
 *
 * @param fg
 *  The pair's brightness-flagged foreground color.
 * @param bg
 *  The pair's brightness-flagged background color.
 * @param adjust_background
 *  Determines which color in the pair is adjusted if adjustment is necessary.
 *  True chooses the background, while false chooses the foreground.
 */
static void curs_adjust_color_pair_to_non_identical(short &fg, short &bg,
    bool adjust_background = false);

/**
 * @brief Get the @em maximum palette size currently supported by the program.
 *
 * This value is limited by both the internal color palette and reported
 * terminal color capabilities.
 *
 * This value is @em not affected by the rendering assumption options.
 *
 * @return The maximum palette size currently supported by the program.
 */
static short curs_palette_size();

/**
 * @brief Sets the default colors for the terminal.
 *
 * Attempt to set the default colors for the terminal based on the current
 * game options. If a default color cannot be set, the curses default will be
 * used (COLOR_WHITE for foreground, COLOR_BLACK for background).
 */
static void curs_set_default_colors();

/**
 * @brief Translates a flagged curses color to an internal COLOUR.
 *
 * @param col
 *  The flagged curses color to convert.
 *
 * @return
 *  The equivalent internal colour.
 */
static COLOURS curses_color_to_internal_colour(short col);

/**
 * @brief Flip the foreground and background color of the @p ch.
 *
 * @see flip_colour(short &, attr_t &, short, attr_t)
 *
 * @param ch
 *  The curses character whose colors will be flipped.
 */
static void flip_colour(cchar_t &ch);

/**
 * @brief Flip the foreground and background color of the @p ch.
 *
 * This flip respects the current rendering assumptions. Therefore, the
 * resulting color combination may not be a strict color swap, but one that
 * is guaranteed to be visible.
 *
 * @param attr
 *  The character attributes to flip.
 * @param color
 *  The color pair to flip.
 *
 *  @return
 *   Returns the flipped character attributes and colour pair index.
 */
static curses_style flip_colour(curses_style style);

/**
 * @brief Translate internal colours to flagged curses colors.
 *
 * @param col
 *  The internal colour to translate.
 *
 * @return
 *  The equivalent flagged curses colour.
 */
static short translate_colour(COLOURS col);

/**
 * @brief Write a complex curses character to specified screen location.
 *
 * There are two additional effects of this function:
 *  - The screen's character attributes are set to those contained in @p ch.
 *  - The cursor is moved to the passed coordinate.
 *
 * @param y
 *  The y-component of the location to draw @p ch.
 * @param x
 *  The x-component of the location to draw @p ch.
 * @param ch
 *  The complex character to render.
 */
static void write_char_at(int y, int x, const cchar_t &ch);

/**
 * @brief Terminal default aware version of pair_safe.
 *
 * @param pair
 *   Pair identifier
 * @param f
 *   Foreground colour
 * @param b
 *   Background colour
 */
static void init_pair_safe(short pair, short f, short b);

static bool cursor_is_enabled = true;

static unsigned int convert_to_curses_style(int chattr)
{
    switch (chattr & CHATTR_ATTRMASK)
    {
    case CHATTR_STANDOUT:       return WA_STANDOUT;
    case CHATTR_BOLD:           return WA_BOLD;
    case CHATTR_BLINK:          return WA_BLINK;
    case CHATTR_UNDERLINE:      return WA_UNDERLINE;
    case CHATTR_DIM:            return WA_DIM;
    default:                    return WA_NORMAL;
    }
}

// see declaration
static short translate_colour(COLOURS col)
{
    switch (col)
    {
    case BLACK:
        return COLOR_BLACK;
    case BLUE:
        return COLOR_BLUE;
    case GREEN:
        return COLOR_GREEN;
    case CYAN:
        return COLOR_CYAN;
    case RED:
        return COLOR_RED;
    case MAGENTA:
        return COLOR_MAGENTA;
    case BROWN:
        return COLOR_YELLOW;
    case LIGHTGREY:
        return COLOR_WHITE;
    case DARKGREY:
        return COLOR_BLACK | COLFLAG_CURSES_BRIGHTEN;
    case LIGHTBLUE:
        return COLOR_BLUE | COLFLAG_CURSES_BRIGHTEN;
    case LIGHTGREEN:
        return COLOR_GREEN | COLFLAG_CURSES_BRIGHTEN;
    case LIGHTCYAN:
        return COLOR_CYAN | COLFLAG_CURSES_BRIGHTEN;
    case LIGHTRED:
        return COLOR_RED | COLFLAG_CURSES_BRIGHTEN;
    case LIGHTMAGENTA:
        return COLOR_MAGENTA | COLFLAG_CURSES_BRIGHTEN;
    case YELLOW:
        return COLOR_YELLOW | COLFLAG_CURSES_BRIGHTEN;
    case WHITE:
        return COLOR_WHITE | COLFLAG_CURSES_BRIGHTEN;
    default:
        return COLOR_GREEN;
    }
}

/**
 * @internal
 * Attempt to exhaustively allocate color pairs, relying on
 * curs_calc_pair_safe() to order pairs.
 *
 * 256-color considerations:
 *  - For ncurses stable, need ncurses 6.0 or later for >256 pairs.
 *  - Pairs use a short type.
 *    We will likely run out of pairs in a 256-color terminal.
 *
 *    For short = int16, highest pair = 2^15 - 1:
 *    Assuming an implicit default pair:
 *      (bg_max * num_colors) - 1 = 2^15 - 1; num_colors = 2^8
 *       bg_max = 2^15/2^8 = 2^7 = 128
 *    So, 128 maximum background colors for 256 foreground colors.
 *
 *    Alternatively, limit to a 181-color palette; sqrt(2^15) = ~181.02
 *
 * @todo
 *  If having access to all possible 256-color pairs (non-simultaneously) is
 *  important for the project, implement paging for color-pairs.
 */
static void setup_colour_pairs()
{
    // The init_pair routine accepts negative values of foreground and
    // background color to support the use_default_colors extension, but only
    // if that routine has been first invoked.
    if (Options.use_terminal_default_colours)
        use_default_colors();

    // Only generate pairs which we may need.
    short num_colors = curs_palette_size();

    for (short i = 0; i < num_colors; i++)
    {
        for (short j = 0; j < num_colors; j++)
        {
            short pair = curs_calc_pair_safe(j, i, COLOR_WHITE, COLOR_BLACK);
            if (pair > 0)
                init_pair_safe(pair, j, i);
        }
    }
}

static void unix_handle_terminal_resize();

static void termio_init()
{
    tcgetattr(0, &def_term);
    memcpy(&game_term, &def_term, sizeof(struct termios));

    def_term.c_cc[VINTR] = (char) 3;        // ctrl-C
    game_term.c_cc[VINTR] = (char) 3;       // ctrl-C

    // Let's recover some control sequences
    game_term.c_cc[VSTART] = (char) -1;     // ctrl-Q
    game_term.c_cc[VSTOP] = (char) -1;      // ctrl-S
    game_term.c_cc[VSUSP] = (char) -1;      // ctrl-Y
#ifdef VDSUSP
    game_term.c_cc[VDSUSP] = (char) -1;     // ctrl-Y
#endif

    tcsetattr(0, TCSAFLUSH, &game_term);

    crawl_state.terminal_resize_handler = unix_handle_terminal_resize;
}

void set_mouse_enabled(bool enabled)
{
#ifdef NCURSES_MOUSE_VERSION
    if (_headless_mode)
        return;
    const int mask = enabled ? ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION : 0;
    mmask_t oldmask = 0;
    mousemask(mask, &oldmask);
#endif
}

#ifdef NCURSES_MOUSE_VERSION
static int proc_mouse_event(int c, const MEVENT *me)
{
    UNUSED(c);

    crawl_view.mousep.x = me->x + 1;
    crawl_view.mousep.y = me->y + 1;

    if (!crawl_state.mouse_enabled)
        return CK_MOUSE_MOVE;

    c_mouse_event cme(crawl_view.mousep);
    if (me->bstate & BUTTON1_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON1;
    else if (me->bstate & BUTTON1_DOUBLE_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON1_DBL;
    else if (me->bstate & BUTTON2_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON2;
    else if (me->bstate & BUTTON2_DOUBLE_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON2_DBL;
    else if (me->bstate & BUTTON3_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON3;
    else if (me->bstate & BUTTON3_DOUBLE_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON3_DBL;
    else if (me->bstate & BUTTON4_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON4;
    else if (me->bstate & BUTTON4_DOUBLE_CLICKED)
        cme.bstate |= c_mouse_event::BUTTON4_DBL;

    if (cme)
    {
        new_mouse_event(cme);
        return CK_MOUSE_CLICK;
    }

    return CK_MOUSE_MOVE;
}
#endif

static int curses_pending_key = 0;
#ifdef USE_TILE_WEB
static int tiles_pending_key = 0;
#endif

static bool _curses_fetch_pending_key(bool blocking)
{
    if (curses_pending_key)
        return true;

    wint_t c;
    int i;
    if (blocking)
        i = get_wch(&c);
    else
    {
        nodelay(stdscr, TRUE);
        // apparently some need this to guarantee non-blocking -- bwr
        timeout(0);
        i = get_wch(&c);
        nodelay(stdscr, FALSE);
    }

    switch (i)
    {
    case ERR:
        // getch() returns -1 on EOF, convert that into an Escape. Evil hack,
        // but the alternative is to explicitly check for -1 everywhere where
        // we might otherwise spin in a tight keyboard input loop.
        // XXX: this doesn't work with `blocking` false, because we can't tell
        // a timeout from an error...
        if (!blocking)
            return false;
        curses_pending_key = ESCAPE;
        return true;
    case OK:
        // a normal (printable) key
        curses_pending_key = c;
        return true;
    case KEY_CODE_YES:
    default:
        curses_pending_key = -c;
        return true;
    }
}

static bool _curses_has_key()
{
    return _curses_fetch_pending_key(false);
}

static int _get_key_from_curses()
{
#ifdef WATCHDOG
    // If we have (or wait for) actual keyboard input, it's not an infinite
    // loop.
    watchdog();
#endif

#ifdef USE_TILE_WEB
    refresh();
    tiles.redraw();

    if (tiles_pending_key)
    {
        wint_t c = tiles_pending_key;
        tiles_pending_key = 0;
        return c;
    }

    wint_t c = tiles.await_input(&_curses_has_key);
    if (c != 0)
        return c;
#endif

    _curses_fetch_pending_key(true);
    int result = curses_pending_key;
    curses_pending_key = 0;
    return result;
}

#if defined(KEY_RESIZE) || defined(USE_UNIX_SIGNALS)
static void unix_handle_resize_event()
{
    crawl_state.last_winch = time(0);
    if (crawl_state.waiting_for_command || crawl_state.waiting_for_ui)
        handle_terminal_resize();
    else
        crawl_state.terminal_resized = true;
}
#endif

static int _headless_getchk()
{
#ifdef WATCHDOG
    // If we have (or wait for) actual keyboard input, it's not an infinite
    // loop.
    watchdog();
#endif

#ifdef USE_TILE_WEB
    tiles.redraw();

    if (tiles_pending_key)
    {
        wint_t c = tiles_pending_key;
        tiles_pending_key = 0;
        return c;
    }

    wint_t c = tiles.await_input([]() { return false; });
    if (c != 0)
        return c;
#endif

    return ESCAPE; // TODO: ??
}

static int _headless_getch_ck()
{
    int c;
    do
    {
        c = _headless_getchk();
    }
    while ((c == CK_MOUSE_MOVE || c == CK_MOUSE_CLICK)
               && !crawl_state.mouse_enabled);

    return c;
}

int getch_ck()
{
    if (_headless_mode)
        return _headless_getch_ck();

    while (true)
    {
        int c = _get_key_from_curses();

#ifdef NCURSES_MOUSE_VERSION
        if (c == -KEY_MOUSE)
        {
            MEVENT me;
            getmouse(&me);
            c = proc_mouse_event(c, &me);

            if (!crawl_state.mouse_enabled)
                continue;
        }
#endif

#ifdef KEY_RESIZE
        if (c == -KEY_RESIZE)
        {
            unix_handle_resize_event();

            // XXX: Before ncurses get_wch() returns KEY_RESIZE, it
            // updates LINES and COLS to the new window size. The resize
            // handler will only redraw the whole screen if the main view
            // is being shown, for slightly insane reasons, which results
            // in crawl_view.termsz being out of sync.
            //
            // This causes crashiness: e.g. in a menu, make the window taller,
            // then scroll down one line. To fix this, we always sync termsz:
            crawl_view.init_geometry();

            continue;
        }
#endif

        switch (-c)
        {
        case 127:
        // 127 is ASCII DEL, which some terminals (all mac, some linux) use for
        // the backspace key. ncurses does not typically map this to
        // KEY_BACKSPACE (though this may depend on TERM settings?). '\b' (^H)
        // in contrast should be handled automatically. Note that ASCII DEL
        // is distinct from the standard esc code for del, esc[3~, which
        // reliably does get mapped to KEY_DC by ncurses. Some background:
        //     https://invisible-island.net/xterm/xterm.faq.html#xterm_erase
        // (I've never found documentation for the mac situation.)
        case KEY_BACKSPACE: return CK_BKSP;
        case KEY_IC:        return CK_INSERT;
        case KEY_DC:        return CK_DELETE;
        case KEY_HOME:      return CK_HOME;
        case KEY_END:       return CK_END;
        case KEY_PPAGE:     return CK_PGUP;
        case KEY_NPAGE:     return CK_PGDN;
        case KEY_UP:        return CK_UP;
        case KEY_DOWN:      return CK_DOWN;
        case KEY_LEFT:      return CK_LEFT;
        case KEY_RIGHT:     return CK_RIGHT;
        case KEY_BEG:       return CK_CLEAR;

        case KEY_BTAB:      return CK_SHIFT_TAB;
        case KEY_SDC:       return CK_SHIFT_DELETE;
        case KEY_SHOME:     return CK_SHIFT_HOME;
        case KEY_SEND:      return CK_SHIFT_END;
        case KEY_SPREVIOUS: return CK_SHIFT_PGUP;
        case KEY_SNEXT:     return CK_SHIFT_PGDN;
        case KEY_SR:        return CK_SHIFT_UP;
        case KEY_SF:        return CK_SHIFT_DOWN;
        case KEY_SLEFT:     return CK_SHIFT_LEFT;
        case KEY_SRIGHT:    return CK_SHIFT_RIGHT;

        case KEY_A1:        return CK_NUMPAD_7;
        case KEY_A3:        return CK_NUMPAD_9;
        case KEY_B2:        return CK_NUMPAD_5;
        case KEY_C1:        return CK_NUMPAD_1;
        case KEY_C3:        return CK_NUMPAD_3;

        // Undocumented ncurses control keycodes, here be dragons!!!
        case 515:           return CK_CTRL_DELETE; // Mac
        case 526:           return CK_CTRL_DELETE; // Linux
        case 542:           return CK_CTRL_HOME;
        case 537:           return CK_CTRL_END;
        case 562:           return CK_CTRL_PGUP;
        case 557:           return CK_CTRL_PGDN;
        case 573:           return CK_CTRL_UP;
        case 532:           return CK_CTRL_DOWN;
        case 552:           return CK_CTRL_LEFT;
        case 567:           return CK_CTRL_RIGHT;

        default:            return c;
        }
    }
}

static void unix_handle_terminal_resize()
{
    console_shutdown();
    console_startup();
}

static void unixcurses_defkeys()
{
#ifdef NCURSES_VERSION
    // To debug these on a specific terminal, you can use `cat -v` to see what
    // escape codes are being printed. To some degree it's better to let ncurses
    // do what it can rather than hard-coding things, but that doesn't always
    // work.
    // cool trick: `printf '\033[?1061h\033='; cat -v` initializes application
    // mode if the terminal supports it. (For some terminals, it may need to
    // be explicitly allowed, or enable via numlock.)

    // keypad 0-9 (only if the "application mode" was successfully initialised)
    define_key("\033Op", 1000);
    define_key("\033Oq", 1001);
    define_key("\033Or", 1002);
    define_key("\033Os", 1003);
    define_key("\033Ot", 1004);
    define_key("\033Ou", 1005);
    define_key("\033Ov", 1006);
    define_key("\033Ow", 1007);
    define_key("\033Ox", 1008);
    define_key("\033Oy", 1009);

    // non-arrow keypad keys (for macros)
    define_key("\033OM", 1010); // keypad enter

    // TODO: I don't know under what context these four are mapped to numpad
    // keys, but they are *much* more commonly used for F1-F4. So don't
    // unconditionally define these. But these mappings have been around for
    // a while, so I'm hesitant to remove them...
#define check_define_key(s, n) if (!key_defined(s)) define_key(s, n)
    check_define_key("\033OP", 1011); // NumLock
    check_define_key("\033OQ", 1012); // /
    check_define_key("\033OR", 1013); // *
    check_define_key("\033OS", 1014); // -

    // TODO: these could probably use further verification on linux
    // notes:
    // * this code doesn't like to map multiple esc sequences to the same
    //   keycode. However, doing so works fine on my testing on mac, on
    //   current ncurses. Why would this be bad?
    // * mac Terminal.app even in application mode does not shift =/*
    // * historically, several comments here were wrong on my testing, but
    //   they could be right somewhere. The current key descriptions are
    //   accurate as far as I can tell.
    define_key("\033Oj", 1015); // *
    define_key("\033Ok", 1016); // + (probably everything else except mac terminal)
    define_key("\033Ol", 1017); // + (mac terminal application mode)
    define_key("\033Om", 1018); // -
    define_key("\033On", 1019); // .
    define_key("\033Oo", 1012); // / (may conflict with the above define?)
    define_key("\033OX", 1021); // =, at least on mac console

# ifdef TARGET_OS_MACOSX
    // force some mappings for function keys that work on mac Terminal.app with
    // the default TERM value.

    // The following seem to be the rxvt escape codes, even
    // though Terminal.app defaults to xterm-256color.
    // TODO: would it be harmful to force this unconditionally?
    check_define_key("\033[25~", 277); // F13
    check_define_key("\033[26~", 278); // F14
    check_define_key("\033[28~", 279); // F15
    check_define_key("\033[29~", 280); // F16
    check_define_key("\033[31~", 281); // F17
    check_define_key("\033[32~", 282); // F18
    check_define_key("\033[33~", 283); // F19, highest key on a magic keyboard

    // not sure exactly what's up with these, but they exist by default:
    // ctrl bindings do too, but they are intercepted by macos
    check_define_key("\033b", -(CK_LEFT + CK_ALT_BASE));
    check_define_key("\033f", -(CK_RIGHT + CK_ALT_BASE));
    // (sadly, only left and right have modifiers by default on Terminal.app)
# endif
#undef check_define_key

#endif
}

// Certain terminals support vt100 keypad application mode only after some
// extra goading.
#define KPADAPP "\033[?1051l\033[?1052l\033[?1060l\033[?1061h"
#define KPADCUR "\033[?1051l\033[?1052l\033[?1060l\033[?1061l"

static void _headless_startup()
{
    // override the default behavior for SIGINT set in libutil.cc:init_signals.
    // TODO: windows ctrl-c? should be able to add a handler on top of
    // libutil.cc:console_handler
#if defined(USE_UNIX_SIGNALS) && defined(SIGINT)
    signal(SIGINT, handle_hangup);
#endif

#ifdef USE_TILE_WEB
    tiles.resize();
#endif
}

void console_startup()
{
    if (_headless_mode)
    {
        _headless_startup();
        return;
    }
    termio_init();

#ifdef CURSES_USE_KEYPAD
    // If hardening is enabled (default on recent distributions), glibc
    // declares write() with __attribute__((warn_unused_result)) which not
    // only spams when not relevant, but cannot even be selectively hushed
    // by (void) casts like all other such warnings.
    // "if ();" is an unsightly hack...
    if (write(1, KPADAPP, strlen(KPADAPP))) {};
#endif

#ifdef USE_UNIX_SIGNALS
# ifndef KEY_RESIZE
    signal(SIGWINCH, unix_handle_resize_event);
# endif
#endif

    initscr();
    raw();
    noecho();

    nonl();
    intrflush(stdscr, FALSE);
#ifdef CURSES_USE_KEYPAD
    keypad(stdscr, TRUE);

# ifdef CURSES_SET_ESCDELAY
#  ifdef NCURSES_REENTRANT
    set_escdelay(CURSES_SET_ESCDELAY);
#  else
    ESCDELAY = CURSES_SET_ESCDELAY;
#  endif
# endif
#endif

    meta(stdscr, TRUE);
    unixcurses_defkeys();
    start_color();

    setup_colour_pairs();
    // Since it may swap pairs, set default colors *after* setting up all pairs.
    curs_set_default_colors();

    scrollok(stdscr, FALSE);

    // Must call refresh() for ncurses to update COLS and LINES.
    refresh();
    crawl_view.init_geometry();

    // TODO: how does this relate to what tiles.resize does?
    ui::resize(crawl_view.termsz.x, crawl_view.termsz.y);

#ifdef USE_TILE_WEB
    tiles.resize();
#endif
}

void console_shutdown()
{
    if (_headless_mode)
        return;

    // resetty();
    endwin();

    tcsetattr(0, TCSAFLUSH, &def_term);
#ifdef CURSES_USE_KEYPAD
    // "if ();" to avoid undisableable spurious warning.
    if (write(1, KPADCUR, strlen(KPADCUR))) {};
#endif

#ifdef USE_UNIX_SIGNALS
# ifndef KEY_RESIZE
    signal(SIGWINCH, SIG_DFL);
# endif
#endif
}

void cprintf(const char *format, ...)
{
    char buffer[2048];          // One full screen if no control seq...

    va_list argp;

    va_start(argp, format);
    vsnprintf(buffer, sizeof(buffer), format, argp);
    va_end(argp);

    char32_t c;
    char *bp = buffer;
    while (int s = utf8towc(&c, bp))
    {
        bp += s;
        // headless check handled in putwch
        putwch(c);
    }
}

void putwch(char32_t chr)
{
    wchar_t c = chr; // ??
    if (_headless_mode)
    {
        // simulate cursor movement and wrapping
        headless_x += c ? wcwidth(chr) : 0;
        if (headless_x >= HEADLESS_COLS && headless_y >= HEADLESS_LINES)
        {
            headless_x = HEADLESS_COLS;
            headless_y = HEADLESS_LINES;
        }
        else if (headless_x > HEADLESS_COLS)
        {
            headless_y++;
            headless_x = headless_x - HEADLESS_COLS;
        }
    }
    else
    {
        if (!c)
            c = ' ';
        // TODO: recognize unsupported characters and try to transliterate
        addnwstr(&c, 1);
    }

#ifdef USE_TILE_WEB
    char32_t buf[2];
    buf[0] = chr;
    buf[1] = 0;
    tiles.put_ucs_string(buf);
#endif
}

void puttext(int x1, int y1, const crawl_view_buffer &vbuf)
{
    const screen_cell_t *cell = vbuf;
    const coord_def size = vbuf.size();
    for (int y = 0; y < size.y; ++y)
    {
        cgotoxy(x1, y1 + y);
        for (int x = 0; x < size.x; ++x)
        {
            // headless check handled in putwch, which this calls
            put_colour_ch(cell->colour, cell->glyph);
            cell++;
        }
    }
}

// These next four are front functions so that we can reduce
// the amount of curses special code that occurs outside this
// this file. This is good, since there are some issues with
// name space collisions between curses macros and the standard
// C++ string class.  -- bwr
void update_screen()
{
    // In objstat, headless, and similar modes, there might not be a screen to update.
    if (stdscr)
    {
        // Refreshing the default colors helps keep colors synced in ttyrecs.
        curs_set_default_colors();
        refresh();
    }

#ifdef USE_TILE_WEB
    tiles.set_need_redraw();
#endif
}

void clear_to_end_of_line()
{
    if (!_headless_mode)
    {
        textcolour(LIGHTGREY);
        textbackground(BLACK);
        clrtoeol(); // shouldn't move cursor pos
    }

#ifdef USE_TILE_WEB
    tiles.clear_to_end_of_line();
#endif
}

int get_number_of_lines()
{
    if (_headless_mode)
        return HEADLESS_LINES;
    else
        return LINES;
}

int get_number_of_cols()
{
    if (_headless_mode)
        return HEADLESS_COLS;
    else
        return COLS;
}

int num_to_lines(int num)
{
    return num;
}

#ifdef DGAMELAUNCH
static bool _suppress_dgl_clrscr = false;

// TODO: this is not an ideal way to solve this problem. An alternative might
// be to queue dgl clrscr and only send them at the same time as an actual
// refresh?
suppress_dgl_clrscr::suppress_dgl_clrscr()
    : prev(_suppress_dgl_clrscr)
{
    _suppress_dgl_clrscr = true;
}

suppress_dgl_clrscr::~suppress_dgl_clrscr()
{
    _suppress_dgl_clrscr = prev;
}
#endif

void clrscr_sys()
{
    if (_headless_mode)
    {
        headless_x = 1;
        headless_y = 1;
        return;
    }

    textcolour(LIGHTGREY);
    textbackground(BLACK);
    clear();
#ifdef DGAMELAUNCH
    if (!_suppress_dgl_clrscr)
    {
        printf("%s", DGL_CLEAR_SCREEN);
        fflush(stdout);
    }
#endif

}

void set_cursor_enabled(bool enabled)
{
    curs_set(cursor_is_enabled = enabled);
#ifdef USE_TILE_WEB
    tiles.set_text_cursor(enabled);
#endif
}

bool is_cursor_enabled()
{
    return cursor_is_enabled;
}

static inline unsigned get_highlight(int col)
{
    return ((col & COLFLAG_UNUSUAL_MASK) == COLFLAG_UNUSUAL_MASK) ?
                                              Options.unusual_highlight :
           (col & COLFLAG_FRIENDLY_MONSTER) ? Options.friend_highlight :
           (col & COLFLAG_NEUTRAL_MONSTER)  ? Options.neutral_highlight :
           (col & COLFLAG_ITEM_HEAP)        ? Options.heap_highlight :
           (col & COLFLAG_WILLSTAB)         ? Options.stab_highlight :
           (col & COLFLAG_MAYSTAB)          ? Options.may_stab_highlight :
           (col & COLFLAG_FEATURE_ITEM)     ? Options.feature_item_highlight :
           (col & COLFLAG_TRAP_ITEM)        ? Options.trap_item_highlight :
           (col & COLFLAG_REVERSE)          ? unsigned{CHATTR_REVERSE}
                                            : unsigned{CHATTR_NORMAL};
}

// see declaration
static curses_style curs_attr(COLOURS fg, COLOURS bg, bool adjust_background)
{
    curses_style style;
    style.attr = 0;
    style.color_pair = 0;
    bool monochrome_output_requested = curs_palette_size() == 0;

    // Convert over to curses colours.
    short fg_curses = translate_colour(fg);
    short bg_curses = translate_colour(bg);

    // Resolve fg/bg colour conflicts.
    curs_adjust_color_pair_to_non_identical(fg_curses, bg_curses,
        adjust_background);

    if (!monochrome_output_requested)
    {
        // Grab the colour pair.
        style.color_pair = curs_calc_pair_safe(fg_curses, bg_curses);

        // Request decolourise if the pair doesn't actually exist.
        if (style.color_pair == 0
            && !curs_color_combo_has_pair(fg_curses, bg_curses))
        {
            monochrome_output_requested = true;
        }
    }

    if (!curs_can_use_extended_colors())
    {
        // curses typically uses WA_BOLD to give bright foreground colour,
        // but various termcaps may disagree
        if ((fg_curses & COLFLAG_CURSES_BRIGHTEN)
            && (Options.bold_brightens_foreground != false
                || Options.best_effort_brighten_foreground))
        {
            style.attr |= WA_BOLD;
        }

        // curses typically uses WA_BLINK to give bright background colour,
        // but various termcaps may disagree (in whole or in part)
        if ((bg_curses & COLFLAG_CURSES_BRIGHTEN)
            && (Options.blink_brightens_background
                || Options.best_effort_brighten_background))
        {
            style.attr |= WA_BLINK;
        }
    }
    else if (bool(Options.bold_brightens_foreground)
                && (fg_curses & COLFLAG_CURSES_BRIGHTEN))
    {
        style.attr |= WA_BOLD;
    }

    if (monochrome_output_requested)
    {
        // Decolourise the output if necessary.
        if (curs_palette_size() != 0)
            style.color_pair = curs_calc_pair_safe(COLOR_WHITE, COLOR_BLACK);

        // Do the best we can for backgrounds with monochrome output.
        if (bg_curses != COLOR_BLACK)
            style.attr |= WA_REVERSE;
    }

    return style;
}

// see declaration
static curses_style curs_attr_bg(int col)
{
    BG_COL = static_cast<COLOURS>(col & 0x00ff);
    return curs_attr_mapped(FG_COL, BG_COL, get_highlight(col));
}

// see declaration
static curses_style curs_attr_fg(int col)
{
    FG_COL = static_cast<COLOURS>(col & 0x00ff);
    return curs_attr_mapped(FG_COL, BG_COL, get_highlight(col));
}

// see declaration
static curses_style curs_attr_mapped(COLOURS fg, COLOURS bg, int highlight)
{
    COLOURS fg_mod = fg;
    COLOURS bg_mod = bg;
    attr_t flags = 0;

    // calculate which curses flags we need...
    if (highlight != CHATTR_NORMAL)
    {
        flags |= convert_to_curses_style(highlight);

        // Allow highlights to override the current background color.
        if ((highlight & CHATTR_ATTRMASK) == CHATTR_HILITE)
            bg_mod = static_cast<COLOURS>((highlight & CHATTR_COLMASK) >> 8);
    }

    // Respect color remapping.
    fg_mod = static_cast<COLOURS>(macro_colour(static_cast<short>(fg_mod)));
    bg_mod = static_cast<COLOURS>(macro_colour(static_cast<short>(bg_mod)));

    // Use the default foreground for lightgrey, with reverse mapping.
    if (fg_mod == LIGHTGREY)
        fg_mod = FG_COL_DEFAULT;
    else if (fg_mod == FG_COL_DEFAULT)
        fg_mod = LIGHTGREY;

    // Use the default background for black, with reverse mapping.
    if (bg_mod == BLACK)
        bg_mod = BG_COL_DEFAULT;
    else if (bg_mod == BG_COL_DEFAULT)
        bg_mod = BLACK;

    // Done with color mapping; get the resulting attributes.
    curses_style ret = curs_attr(fg_mod, bg_mod);
    ret.attr |= flags;

    // Reverse color manually to ensure correct brightening attrs.
    if ((highlight & CHATTR_ATTRMASK) == CHATTR_REVERSE)
        return flip_colour(ret);

    return ret;
}

// see declaration
static short curs_calc_pair_safe(short fg, short bg)
{
    short fg_stripped = fg;
    short bg_stripped = bg;
    short fg_col_default_curses = curses_color_to_internal_colour(
        FG_COL_DEFAULT);
    short bg_col_default_curses = curses_color_to_internal_colour(
        BG_COL_DEFAULT);

    if (!curs_can_use_extended_colors())
    {
        // Strip out all the bits above the raw 3-bit colour definition
        fg_stripped &= 0x07;
        bg_stripped &= 0x07;
    }

    // Pass along to the more generic function for the heavy lifting.
    short pair = curs_calc_pair_safe(fg_stripped, bg_stripped,
        fg_col_default_curses, bg_col_default_curses);

    return pair;
}

/**
 * @internal
 *  Always use the default pair, 0, for the current default (fg, bg) combo.
 *  For the pair (fg=white, bg=black):
 *    Use a pair index which depends on the current default color combination.
 *    - If the default combo is this special combo, simply use pair 0.
 *    - Otherwise, use the color pair that would correspond to the default
 *      combo if the default combo was not the default.
 */
static short curs_calc_pair_safe(short fg, short bg, short fg_default,
    short bg_default)
{
    // Use something wider than short for the calculation in case of overflow.
    long pair = 0;

    if (fg == fg_default && bg == bg_default)
        pair = 0;
    else
    {
        // Remap the *default* default curses pair as necessary.
        short fg_mapped = fg;
        short bg_mapped = bg;

        if (fg == COLOR_WHITE && bg == COLOR_BLACK)
        {
            fg_mapped = fg_default;
            bg_mapped = bg_default;
        }

        // Work around the *default* default combination hole.
        const short num_colors = curs_palette_size();
        const short default_color_hole = 1 + COLOR_BLACK * num_colors
            + COLOR_WHITE;

        // Calculate the pair index, starting from 1.
        pair = 1 + bg_mapped * num_colors + fg_mapped;
        if (pair > default_color_hole)
            pair--;
    }

    // Last, validate the pair. Guard against overflow and bogus input.
    if (pair > COLOR_PAIRS || pair < 0)
        pair = 0;

    return static_cast<short>(pair);
}

// see declaration
static bool curs_can_use_extended_colors()
{
    return Options.allow_extended_colours && COLORS >= NUM_TERM_COLOURS;
}

lib_display_info::lib_display_info()
    : type(CRAWL_BUILD_NAME),
    term(termname()),
    fg_colors(
        (curs_can_use_extended_colors()
                || Options.bold_brightens_foreground != false)
        ? 16 : 8),
    bg_colors(
        (curs_can_use_extended_colors() || Options.blink_brightens_background)
        ? 16 : 8)
{
}

// see declaration
static bool curs_color_combo_has_pair(short fg, short bg)
{
    short fg_col_curses = translate_colour(FG_COL_DEFAULT);
    short bg_col_curses = translate_colour(BG_COL_DEFAULT);

    bool has_pair = false;

    if (curs_palette_size() > 0)
    {
        if (fg == fg_col_curses && bg == bg_col_curses)
            has_pair = true;
        else
            has_pair = curs_calc_pair_safe(fg, bg, COLOR_WHITE, COLOR_BLACK);
    }

    return has_pair;
}

// see declaration
static void curs_adjust_color_pair_to_non_identical(short &fg, short &bg,
    bool adjust_background)
{
    // The default colors.
    short fg_default = translate_colour(FG_COL_DEFAULT);
    short bg_default = translate_colour(BG_COL_DEFAULT);

    // The expected output colors.
    short fg_to_compare = fg;
    short bg_to_compare = bg;
    short fg_default_to_compare = fg_default;
    short bg_default_to_compare = bg_default;

    // Adjust the brighten bits of the expected output color depending on the
    // game options, so we are doing an apples to apples comparison. If we
    // aren't using extended colors, *and* we aren't applying a bold to
    // brighten, then brightened colors are guaranteed not to be different
    // than unbrightened colors. With just a `best_effort..` option set, don't
    // undo brightening as it isn't assumed to be safe. It is this
    // transformation that can result in black on black if a player incorrectly
    // sets one of these options.
    if (!curs_can_use_extended_colors())
    {
        if (!Options.bold_brightens_foreground)
        {
            fg_to_compare = fg & ~COLFLAG_CURSES_BRIGHTEN;
            fg_default_to_compare = fg_default & ~COLFLAG_CURSES_BRIGHTEN;
        }

        if (!Options.blink_brightens_background)
        {
            bg_to_compare = bg & ~COLFLAG_CURSES_BRIGHTEN;
            bg_default_to_compare = bg_default & ~COLFLAG_CURSES_BRIGHTEN;
        }
    }

    if (fg_to_compare != bg_to_compare)
        return;  // colours look different; no need to adjust

    // Choose terminal's current default colors as the default failsafe.
    short failsafe_col = adjust_background ? fg_default : bg_default;

    if (!adjust_background && fg_to_compare == bg_default_to_compare)
    {
        /*
            * Replacing the *foreground* color with a secondary failsafe.
            *
            * In general, use black as a failsafe for non-black backgrounds
            * and white as a failsafe for black backgrounds. Black tends to
            * look good on any visible background.
            *
            * However, for black and white *default* background colours,
            * mitigate information contrast issues with bright black
            * foregrounds by using blue as a special-case failsafe color.
            */
        switch (bg_default_to_compare)
        {
        case COLOR_BLACK:
            if (fg_default_to_compare == COLOR_WHITE
                || fg_default_to_compare == COLOR_BLACK)
            {
                failsafe_col = COLOR_BLUE;
            }
            else
                failsafe_col = COLOR_WHITE;
            break;
        case COLOR_WHITE:
            if (fg_default_to_compare == COLOR_WHITE
                || fg_default_to_compare == COLOR_BLACK)
            {
                failsafe_col = COLOR_BLUE;
            }
            else
                failsafe_col = COLOR_BLACK;
            break;
        default:
            failsafe_col = COLOR_BLACK;
            break;
        }
    }
    else if (adjust_background && bg_to_compare == fg_default_to_compare)
    {
        /*
            * Replacing the *background* color with a secondary failsafe.
            *
            * Don't bother special-casing bright black:
            *  - The information contrast issue is not as prevalent for
            *    backgrounds as foregrounds.
            *  - A visible bright-black-on-black glyph actively changing into
            *    black-on-blue when reversed looks much worse than a change to
            *    black-on-white.
            */
        if (fg_default_to_compare == COLOR_BLACK)
            failsafe_col = COLOR_WHITE;
        else
            failsafe_col = COLOR_BLACK;
    }

    // Update the appropriate color in the pair.
    if (adjust_background)
        bg = failsafe_col;
    else
        fg = failsafe_col;
}

// see declaration
static short curs_palette_size()
{
    int palette_size = std::min(COLORS, static_cast<int>(NUM_TERM_COLOURS));

    if (palette_size > SHRT_MAX)
        palette_size = SHRT_MAX;
    else if (palette_size < 8)
        palette_size = 0;

    return palette_size;
}

/**
 * @internal
 * There are ncurses-specific extensions to change the default color pair
 * from COLOR_WHITE on COLOR_BLACK.
 *
 * The use_default_colors() function is dangerous to use for the program,
 * unless it is running on a monochrome terminal.
 *
 * The color_content() function cannot be used on a default color, and
 * use_default_colors() may or may not be matched with an existing color in the
 * game's palette. There's a very likely chance of a color collision which
 * cannot be avoided programmatically.
 *
 * So, use the safer assume_default_colors() function with manually-defined
 * default colors for color terminals.
 */
static void curs_set_default_colors()
{
    // The *default* default curses colors.
    const COLOURS failsafe_fg = curses_color_to_internal_colour(COLOR_WHITE);
    const COLOURS failsafe_bg = curses_color_to_internal_colour(COLOR_BLACK);

    int default_colors_loaded = ERR;
    COLOURS default_fg_prev = FG_COL_DEFAULT;
    COLOURS default_bg_prev = BG_COL_DEFAULT;
    COLOURS default_fg = static_cast<COLOURS>(Options.foreground_colour);
    COLOURS default_bg = static_cast<COLOURS>(Options.background_colour);

#ifdef NCURSES_VERSION
    if (!curs_can_use_extended_colors())
    {
        // Deny colours outside the standard 8-color palette.
        if (is_high_colour(default_fg))
            default_fg = failsafe_fg;
        if (is_high_colour(default_bg))
            default_bg = failsafe_bg;
    }

    // Deny default background colours which can't pair with all foregrounds.
    if (!curs_color_combo_has_pair(curs_palette_size() - 1,
        translate_colour(default_bg)))
    {
        default_bg = failsafe_bg;
    }

    // Assume new default colors.
    if (Options.use_terminal_default_colours)
        default_colors_loaded = OK;
    else if (curs_palette_size() == 0)
        default_colors_loaded = use_default_colors();
    else
    {
        default_colors_loaded = assume_default_colors(
            translate_colour(default_fg), translate_colour(default_bg));
    }
#endif

    // Check if a failsafe is needed.
    if (default_colors_loaded == ERR)
    {
        default_fg = failsafe_fg;
        default_bg = failsafe_bg;
    }

    // Store the validated default colors.
    // The new default color pair is now in pair 0.
    FG_COL_DEFAULT = default_fg;
    BG_COL_DEFAULT = default_bg;

    // Restore the previous default color pair.
    short default_fg_prev_curses = translate_colour(default_fg_prev);
    short default_bg_prev_curses = translate_colour(default_bg_prev);
    short prev_default_pair = curs_calc_pair_safe(default_fg_prev_curses,
        default_bg_prev_curses, COLOR_WHITE, COLOR_BLACK);
    if (prev_default_pair != 0)
    {
        init_pair_safe(prev_default_pair, default_fg_prev_curses,
            default_bg_prev_curses);
    }

    // Make sure the *default* default color pair has a home.
    short new_default_default_pair = curs_calc_pair_safe(COLOR_WHITE,
        COLOR_BLACK, translate_colour(default_fg),
        translate_colour(default_bg));
    if (new_default_default_pair != 0)
        init_pair_safe(new_default_default_pair, COLOR_WHITE, COLOR_BLACK);
}

// see declaration
static COLOURS curses_color_to_internal_colour(short col)
{
    switch (col)
    {
    case COLOR_BLACK:
        return BLACK;
    case COLOR_BLUE:
        return BLUE;
    case COLOR_GREEN:
        return GREEN;
    case COLOR_CYAN:
        return CYAN;
    case COLOR_RED:
        return RED;
    case COLOR_MAGENTA:
        return MAGENTA;
    case COLOR_YELLOW:
        return BROWN;
    case COLOR_WHITE:
        return LIGHTGREY;
    case (COLOR_BLACK | COLFLAG_CURSES_BRIGHTEN):
        return DARKGREY;
    case (COLOR_BLUE | COLFLAG_CURSES_BRIGHTEN):
        return LIGHTBLUE;
    case (COLOR_GREEN | COLFLAG_CURSES_BRIGHTEN):
        return LIGHTGREEN;
    case (COLOR_CYAN | COLFLAG_CURSES_BRIGHTEN):
        return LIGHTCYAN;
    case (COLOR_RED | COLFLAG_CURSES_BRIGHTEN):
        return LIGHTRED;
    case (COLOR_MAGENTA | COLFLAG_CURSES_BRIGHTEN):
        return LIGHTMAGENTA;
    case (COLOR_YELLOW | COLFLAG_CURSES_BRIGHTEN):
        return YELLOW;
    case (COLOR_WHITE | COLFLAG_CURSES_BRIGHTEN):
        return WHITE;
    default:
        return GREEN;
    }
}

void textcolour(int col)
{
    if (!_headless_mode)
    {
        const auto style = curs_attr_fg(col);
        attr_set(style.attr, style.color_pair, nullptr);
    }

#ifdef USE_TILE_WEB
    tiles.textcolour(col);
#endif
}

COLOURS default_hover_colour()
{
    // DARKGREY backgrounds go to black with 8 colors. I think this is
    // generally what we want, rather than applying a workaround (like the
    // DARKGREY -> BLUE foreground trick), but this means that using a
    // DARKGREY hover, which arguably looks better in 16colors, won't work.
    // DARKGREY is also not safe under bold_brightens_foreground, since a
    // terminal that supports this won't necessarily handle a bold background.

    // n.b. if your menu uses just one color, and you set that as the hover,
    // you will get automatic color inversion. That is generally the safest
    // option where possible.
    return (curs_can_use_extended_colors() || Options.blink_brightens_background)
                 ? DARKGREY : BLUE;
}

void textbackground(int col)
{
    if (!_headless_mode)
    {
        const auto style = curs_attr_bg(col);
        attr_set(style.attr, style.color_pair, nullptr);
    }

#ifdef USE_TILE_WEB
    tiles.textbackground(col);
#endif
}

void gotoxy_sys(int x, int y)
{
    if (_headless_mode)
    {
        headless_x = x;
        headless_y = y;
    }
    else
        move(y - 1, x - 1);
}

static inline cchar_t character_at(int y, int x)
{
    cchar_t c;
    // (void) is to hush an incorrect clang warning.
    (void)mvin_wch(y, x, &c);
    return c;
}

/**
 * @internal
 * Writing out a cchar_t to the screen using one of the add_wch functions does
 * not necessarily set the character attributes contained within.
 *
 * So, explicitly set the screen attributes to those contained within the passed
 * cchar_t before writing it to the screen. This *should* guarantee that that
 * all attributes within the cchar_t (and only those within the cchar_t) are
 * actually set.
 */
static void write_char_at(int y, int x, const cchar_t &ch)
{
    attr_t attr = 0;
    short color_pair = 0;
    wchar_t *wch = nullptr;

    // Make sure to allocate enough space for the characters.
    int chars_to_allocate = getcchar(&ch, nullptr, &attr, &color_pair, nullptr);
    if (chars_to_allocate > 0)
        wch = new wchar_t[chars_to_allocate];

    // Good to go. Grab the color / attr info.
    getcchar(&ch, wch, &attr, &color_pair, nullptr);

    // Clean up.
    if (chars_to_allocate > 0)
        delete [] wch;

    attr_set(attr, color_pair, nullptr);
    mvadd_wchnstr(y, x, &ch, 1);
}

static void init_pair_safe(short pair, short f, short b)
{
    if (Options.use_terminal_default_colours)
    {
        short _f = (f == COLOR_WHITE) ? -1 : f;
        short _b = (b == COLOR_BLACK) ? -1 : b;
        init_pair(pair, _f, _b);
    }
    else
        init_pair(pair, f, b);
}

// see declaration
static void flip_colour(cchar_t &ch)
{
    attr_t attr = 0;
    short color_pair = 0;
    wchar_t *wch = nullptr;

    // Make sure to allocate enough space for the characters.
    int chars_to_allocate = getcchar(&ch, nullptr, &attr,
        &color_pair, nullptr);
    if (chars_to_allocate > 0)
        wch = new wchar_t[chars_to_allocate];

    // Good to go. Grab the color / attr info.
    curses_style style;
    getcchar(&ch, wch, &style.attr, &style.color_pair, nullptr);
    style = flip_colour(style);

    // Assign the new, reversed info and clean up.
    setcchar(&ch, wch, style.attr, style.color_pair, nullptr);
    if (chars_to_allocate > 0)
        delete [] wch;
}

// see declaration
static curses_style flip_colour(curses_style style)
{
    short fg = COLOR_WHITE;
    short bg = COLOR_BLACK;

    if (style.color_pair != 0)
        pair_content(style.color_pair, &fg, &bg);
    else
    {
        // Default pair; use the current default colors.
        fg = translate_colour(FG_COL_DEFAULT);
        bg = translate_colour(BG_COL_DEFAULT);
    }

    bool need_attribute_only_flip = !curs_color_combo_has_pair(bg, fg);

    // Adjust brightness flags for low-color modes.
    if (!curs_can_use_extended_colors())
    {
        // Check if these were brightened colours.
        if (style.attr & WA_BOLD)
            fg |= COLFLAG_CURSES_BRIGHTEN;
        if (style.attr & WA_BLINK)
            bg |= COLFLAG_CURSES_BRIGHTEN;
        style.attr &= ~(WA_BOLD | WA_BLINK);
    }

    if (!need_attribute_only_flip)
    {
        // Perform a flip using the inverse color pair, preferring to adjust
        // the background color in case of a conflict.
        const auto out = curs_attr(curses_color_to_internal_colour(bg),
                                   curses_color_to_internal_colour(fg), true);
        style.color_pair = out.color_pair;
        style.attr |= out.attr;
    }
    else
    {
        // The best we can do is a purely attribute-based flip.

        // Swap potential brightness flags.
        if (!curs_can_use_extended_colors())
        {
            if ((fg & COLFLAG_CURSES_BRIGHTEN)
                && (Options.blink_brightens_background
                    || Options.best_effort_brighten_background))
            {
                style.attr |= WA_BLINK;
            }
            // XX I don't *think* this logic should apply for
            // bold_brightens_foreground = force...
            if ((bg & COLFLAG_CURSES_BRIGHTEN)
                && (Options.bold_brightens_foreground != false
                    || Options.best_effort_brighten_foreground))
            {
                style.attr |= WA_BOLD;
            }
        }

        style.attr ^= WA_REVERSE;
    }

    return style;
}

void fakecursorxy(int x, int y)
{
    if (_headless_mode)
    {
        gotoxy_sys(x, y);
        set_cursor_region(GOTO_CRT);
        return;
    }

    int x_curses = x - 1;
    int y_curses = y - 1;

    cchar_t c = character_at(y_curses, x_curses);
    flip_colour(c);
    write_char_at(y_curses, x_curses, c);
    // the above still results in changes to the return values for wherex and
    // wherey, so set the cursor region to ensure that the cursor position is
    // valid after this call. (This also matches the behavior of real cursorxy.)
    set_cursor_region(GOTO_CRT);
}

int wherex()
{
    if (_headless_mode)
        return headless_x;
    else
        return getcurx(stdscr) + 1;
}

int wherey()
{
    if (_headless_mode)
        return headless_y;
    else
        return getcury(stdscr) + 1;
}

void delay(unsigned int time)
{
    if (crawl_state.disables[DIS_DELAY])
        return;

#ifdef USE_TILE_WEB
    tiles.redraw();
    if (time)
    {
        tiles.send_message("{\"msg\":\"delay\",\"t\":%d}", time);
        tiles.flush_messages();
    }
#endif

    refresh();
    if (time)
        usleep(time * 1000);
}

static bool _headless_kbhit()
{
#ifdef USE_TILE_WEB
    if (tiles_pending_key)
        return true;
    wint_t c = tiles.try_await_input();
    if (c != 0)
    {
        tiles_pending_key = c;
        return true;
    }
#endif

    return false;
}

/* This is Juho Snellman's modified kbhit, to work with macros */
bool kbhit()
{
    if (_headless_mode)
        return _headless_kbhit();

    if (_curses_has_key())
        return true;

#ifdef USE_TILE_WEB
    if (tiles_pending_key)
        return true;
    wint_t c = tiles.try_await_input();
    if (c != 0)
    {
        tiles_pending_key = c;
        return true;
    }
#endif

    return false;
}