1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
|
/*
* nvidia-installer: A tool for installing NVIDIA software packages on
* Unix and Linux systems.
*
* Copyright (C) 2003 NVIDIA Corporation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the:
*
* Free Software Foundation, Inc.
* 59 Temple Place - Suite 330
* Boston, MA 02111-1307, USA
*
*
* ncurses_ui.c - implementation of the nvidia-installer ui using ncurses.
*/
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include "nvidia-installer.h"
#include "nvidia-installer-ui.h"
/* structures */
/*
* RegionStruct - a region is sort of like an ncurses window, but
* implemented completely in this source file (ie: doesn't use ncurses
* windows). I found enough bugs with ncurses windows that I felt it
* better to avoid their use altogether.
*/
typedef struct {
int x, y, w, h; /* position and dimensions relative to stdscr */
int attr; /* attributes (to be passed to setattr()) */
char *line; /* a string filled with spaces; its length is the
width of the region. This is just a
convenience for use when clearing the region */
} RegionStruct;
/*
* PagerStruct - Pager implements the functionality of `less`
*/
typedef struct {
TextRows *t; /* rows of text to be displayed */
RegionStruct *region; /* region in which the text will be displayed */
const char *label; /* label for the left side of the footer */
int cur; /* current position in the pager */
int page; /* height of a page (for use with pgup/pgdn) */
} PagerStruct;
/*
* DataStruct - private data structure that gets plugged into
* Options->ui_priv.
*/
typedef struct {
RegionStruct *header; /* header region */
RegionStruct *footer; /* footer region */
RegionStruct *message; /* message region */
FormatTextRows format_text_rows; /* XXX function pointer from installer */
bool use_color; /* should the ncurses ui use color? */
int width; /* cached copy of the terminal dimensions */
int height;
char *title; /* cached strings for the header and footer */
char *footer_left;
char *footer_right;
char *progress_title; /* cached string for the title of the
progress messages */
} DataStruct;
/* constants */
/* default strings for the header and footer */
#define NV_NCURSES_DEFAULT_TITLE "NVIDIA Software Installer for Unix/Linux"
#define NV_NCURSES_DEFAULT_FOOTER_LEFT NV_NCURSES_DEFAULT_TITLE
#define NV_NCURSES_DEFAULT_FOOTER_RIGHT "www.nvidia.com"
/* indices for color pairs */
#define NV_NCURSES_HEADER_COLOR_IDX 1
#define NV_NCURSES_MESSAGE_COLOR_IDX 2
#define NV_NCURSES_BUTTON_COLOR_IDX 3
#define NV_NCURSES_INPUT_COLOR_IDX 4
#define NV_NCURSES_HEADER_COLOR (COLOR_PAIR(NV_NCURSES_HEADER_COLOR_IDX))
#define NV_NCURSES_FOOTER_COLOR A_REVERSE
#define NV_NCURSES_MESSAGE_COLOR (COLOR_PAIR(NV_NCURSES_MESSAGE_COLOR_IDX))
#define NV_NCURSES_BUTTON_COLOR (COLOR_PAIR(NV_NCURSES_BUTTON_COLOR_IDX))
#define NV_NCURSES_INPUT_COLOR (COLOR_PAIR(NV_NCURSES_INPUT_COLOR_IDX))
#define NV_NCURSES_HEADER_NO_COLOR A_REVERSE
#define NV_NCURSES_FOOTER_NO_COLOR A_REVERSE
#define NV_NCURSES_MESSAGE_NO_COLOR A_NORMAL
/* use when animating button presses */
#define NV_NCURSES_BUTTON_PRESS_TIME 125000
#define NV_NCURSES_TAB 9
#define NV_NCURSES_ENTER 10
#define NV_NCURSES_BACKSPACE 8
#define NV_NCURSES_CTRL(x) ((x) & 0x1f)
/*
* somewhat arbitrary minimum values: if the current window size is
* smaller than this, don't attempt to use the ncurses ui.
*/
#define NV_NCURSES_MIN_WIDTH 40
#define NV_NCURSES_MIN_HEIGHT 10
#define NV_NCURSES_HLINE '_'
/* prototypes for ui entry points */
static int nv_ncurses_detect (Options*);
static int nv_ncurses_init (Options*,
FormatTextRows format_text_rows);
static void nv_ncurses_set_title (Options*, const char*);
static char *nv_ncurses_get_input (Options*, const char*,
const char*);
static int nv_ncurses_display_license (Options*, const char*);
static void nv_ncurses_message (Options*, const int level,
const char*);
static void nv_ncurses_command_output (Options*, const char*);
static int nv_ncurses_approve_command_list(Options*, CommandList*,
const char*);
static int nv_ncurses_yes_no (Options*, const int, const char*);
static void nv_ncurses_status_begin (Options*, const char*,
const char*);
static void nv_ncurses_status_update (Options*, const float,
const char*);
static void nv_ncurses_status_end (Options*, const char*);
static void nv_ncurses_close (Options*);
/* helper functions for manipulating the header and footer */
static void nv_ncurses_set_header(DataStruct *, const char *);
static void nv_ncurses_set_footer(DataStruct *, const char *, const char *);
/* helper functions for manipulating RegionStructs */
static RegionStruct *nv_ncurses_create_region(DataStruct *, int, int,
int, int, int, int);
static void nv_ncurses_clear_region(RegionStruct *);
static void nv_ncurses_destroy_region(RegionStruct *);
/* helper functions for drawing buttons */
static void nv_ncurses_draw_button(DataStruct *, RegionStruct *,
int, int, int, int, char *, bool, bool);
static void nv_ncurses_erase_button(RegionStruct *, int, int, int, int);
/* helper functions for drawing regions and stuff */
static void nv_ncurses_do_message_region(DataStruct *, const char *,
const char *, int, int);
static void nv_ncurses_do_progress_bar_region(DataStruct *);
static void nv_ncurses_do_progress_bar_message(DataStruct *, const char *,
int, int);
/* pager functions */
static PagerStruct *nv_ncurses_create_pager(DataStruct *, int, int, int, int,
TextRows *, const char *, int);
static void nv_ncurses_pager_update(DataStruct *, PagerStruct *);
static void nv_ncurses_pager_handle_events(DataStruct *, PagerStruct *, int);
static void nv_ncurses_destroy_pager(PagerStruct *);
/* progress bar helper functions */
static int choose_char(int i, int p[4], char v[4], char def);
static void init_percentage_string(char v[4], int n);
static void init_position(int p[4], int w);
/* misc helper functions */
static TextRows *nv_ncurses_create_command_list_textrows(DataStruct *,
CommandList *, int);
static char *nv_ncurses_mode_to_permission_string(mode_t);
static void nv_ncurses_concat_text_rows(TextRows *, TextRows *);
static void nv_ncurses_free_text_rows(TextRows *);
static int nv_ncurses_format_print(DataStruct *, RegionStruct *,
int, int, int, int, TextRows *t);
static int nv_ncurses_check_resize(DataStruct *, bool);
/* dispatch table that gets dlsym()'ed by ui_init() */
InstallerUI ui_dispatch_table = {
nv_ncurses_detect,
nv_ncurses_init,
nv_ncurses_set_title,
nv_ncurses_get_input,
nv_ncurses_display_license,
nv_ncurses_message,
nv_ncurses_command_output,
nv_ncurses_approve_command_list,
nv_ncurses_yes_no,
nv_ncurses_status_begin,
nv_ncurses_status_update,
nv_ncurses_status_end,
nv_ncurses_close
};
/*
* nv_ncurses_detect() - initialize ncurses; return FALSE if
* initialization fails
*/
static int nv_ncurses_detect(Options *op)
{
int x, y;
if (!initscr()) return FALSE;
/*
* query the current size of the window, and don't try to use the
* ncurses ui if it's too small.
*/
getmaxyx(stdscr, y, x);
if ((x < NV_NCURSES_MIN_WIDTH) || (y < NV_NCURSES_MIN_HEIGHT)) {
endwin();
return FALSE;
}
return TRUE;
} /* nv_ncurses_detect() */
/*
* nv_ncurses_init() - initialize the ncurses interface.
*/
static int nv_ncurses_init(Options *op, FormatTextRows format_text_rows)
{
DataStruct *d;
d = (DataStruct *) malloc(sizeof(DataStruct));
memset(d, 0, sizeof(DataStruct));
d->format_text_rows = format_text_rows;
/* initialize color */
d->use_color = !op->no_ncurses_color;
if (d->use_color) {
if (!has_colors()) {
d->use_color = FALSE;
}
}
if (d->use_color) {
if (start_color() == ERR) {
d->use_color = FALSE;
} else {
/* foreground background */
init_pair(NV_NCURSES_HEADER_COLOR_IDX, COLOR_BLACK, COLOR_GREEN);
init_pair(NV_NCURSES_MESSAGE_COLOR_IDX, COLOR_WHITE, COLOR_BLUE);
init_pair(NV_NCURSES_BUTTON_COLOR_IDX, COLOR_WHITE, COLOR_RED);
init_pair(NV_NCURSES_INPUT_COLOR_IDX, COLOR_GREEN, COLOR_BLACK);
}
}
clear(); /* clear the screen */
noecho(); /* don't echo input to the screen */
cbreak(); /* disable line buffering and control characters */
curs_set(0); /* make the cursor invisible */
keypad(stdscr, TRUE); /* enable keypad, function keys, arrow keys, etc */
getmaxyx(stdscr, d->height, d->width); /* get current window dimensions */
/* create the regions */
d->header = nv_ncurses_create_region(d, 1, 0, d->width - 2, 1,
NV_NCURSES_HEADER_COLOR,
NV_NCURSES_HEADER_NO_COLOR);
d->footer = nv_ncurses_create_region(d, 1, d->height - 2, d->width - 2, 1,
NV_NCURSES_FOOTER_COLOR,
NV_NCURSES_FOOTER_NO_COLOR);
/* plug the DataStruct struct into the ui_priv pointer */
op->ui_priv = (void *) d;
/* set the initial strings in the header and footer */
nv_ncurses_set_header(d, NV_NCURSES_DEFAULT_TITLE);
nv_ncurses_set_footer(d, NV_NCURSES_DEFAULT_FOOTER_LEFT,
NV_NCURSES_DEFAULT_FOOTER_RIGHT);
refresh();
return TRUE;
} /* nv_ncurses_init() */
/*
* nv_ncurses_set_title() - update the string in the header region
*/
static void nv_ncurses_set_title(Options *op, const char *title)
{
DataStruct *d = (DataStruct *) op->ui_priv;
nv_ncurses_set_header(d, title);
refresh();
} /* nv_ncurses_set_title() */
/*
* nv_ncurses_get_input - prompt for user input with the given msg;
* returns the user inputed string.
*/
#define MIN_INPUT_LEN 32
#define MAX_BUF_LEN 1024
#define BUF_CHAR(c) ((c) ? (c) : ' ')
static char *nv_ncurses_get_input(Options *op,
const char *def, const char *msg)
{
DataStruct *d = (DataStruct *) op->ui_priv;
int msg_len, width, input_len, buf_len, def_len;
int input_x, input_y, i, w, h, x, y, c, lines, ch, color, redraw;
char *tmp, buf[MAX_BUF_LEN];
TextRows *t;
if (!msg) return NULL;
nv_ncurses_check_resize(d, FALSE);
color = d->use_color ? NV_NCURSES_INPUT_COLOR : A_REVERSE;
/* concatenate ": " to the end of the message */
msg_len = strlen(msg) + 2;
tmp = (char *) malloc(msg_len + 1);
snprintf(tmp, msg_len, "%s: ", msg);
/* copy the default response into the buffer */
memset(buf, 0, MAX_BUF_LEN);
if (def) strncpy(buf, def, MAX_BUF_LEN);
draw_get_input:
/* free any existing message region */
if (d->message) {
nv_ncurses_destroy_region(d->message);
d->message = NULL;
}
/*
* compute the size of the input box: the input box is twice the
* length of the default string, clamped to MIN_INPUT_LEN
*/
def_len = def ? strlen(def) : 0;
input_len = NV_MAX(def_len * 2, MIN_INPUT_LEN);
width = d->width - 4;
/* convert the message to text rows */
t = d->format_text_rows(NULL, tmp, width, TRUE);
/*
* if the message and input box will fit on one line, do that;
* otherwise, place them on separate lines
*/
if ((msg_len + input_len + 1) < width) {
input_x = 1 + msg_len;
lines = 1;
} else {
input_x = 2;
lines = t->n + 1;
}
input_y = lines;
/*
* compute the width, height, and starting position of the message
* region
*/
w = d->width - 2;
h = lines + 2;
x = 1;
y = ((d->height - (3 + h)) / 3) + 1;
/* create the message region */
d->message = nv_ncurses_create_region(d, x, y, w, h,
NV_NCURSES_MESSAGE_COLOR,
NV_NCURSES_MESSAGE_NO_COLOR);
nv_ncurses_format_print(d, d->message, 1, 1, d->message->w - 2, lines, t);
/* free the text rows */
nv_ncurses_free_text_rows(t);
/* clamp the input box to the width of the region */
input_len = NV_MIN(input_len, width - 2);
curs_set(1); /* make the cursor visible */
c = buf_len = strlen(buf);
redraw = TRUE; /* force a redraw the first time through */
/* offset input_x and input_y by the region offset */
input_x += d->message->x;
input_y += d->message->y;
do {
x = NV_MAX(c - (input_len - 1), 0);
/* redraw the input box */
if (redraw) {
for (i = 0; i < input_len; i++) {
mvaddch(input_y, input_x + i, BUF_CHAR(buf[i + x]) | color);
}
/* if we're scrolling, display an arrow */
if (x > 0) {
mvaddch(input_y, input_x - 1, '<' | d->message->attr);
} else {
mvaddch(input_y, input_x - 1, ' ' | d->message->attr);
}
if (buf_len > (input_len - 1 + x)) {
mvaddch(input_y, input_x + input_len, '>' | d->message->attr);
} else {
mvaddch(input_y, input_x + input_len, ' ' | d->message->attr);
}
redraw = FALSE;
}
/* position the cursor */
move(input_y, input_x - x + c);
refresh();
/* wait for input */
if (nv_ncurses_check_resize(d, FALSE)) goto draw_get_input;
ch = getch();
switch (ch) {
case NV_NCURSES_BACKSPACE:
case KEY_BACKSPACE:
/*
* If there is a character to be deleted, move everything
* after the character forward, and decrement c and len.
*/
if (c <= 0) break;
for (i = c; i <= buf_len; i++) buf[i-1] = buf[i];
c--;
buf_len--;
redraw = TRUE;
break;
case KEY_DC:
/*
* If there is a character to be deleted, move everything
* after the character forward, and decrement len.
*/
if (c == buf_len) break;
for (i = c; i < buf_len; i++) buf[i] = buf[i+1];
buf_len--;
redraw = TRUE;
break;
case KEY_LEFT:
if (c > 0) {
c--;
redraw = TRUE;
}
break;
case KEY_RIGHT:
if (c < buf_len) {
c++;
redraw = TRUE;
}
break;
case NV_NCURSES_CTRL('L'):
nv_ncurses_check_resize(d, TRUE);
goto draw_get_input;
break;
}
/*
* If we have a printable character, then move everything
* after the current location back, and insert the character.
*/
if (isprint(ch)) {
if (buf_len < (MAX_BUF_LEN - 1)) {
for (i = buf_len; i > c; i--) buf[i] = buf[i-1];
buf[c] = (char) ch;
buf_len++;
c++;
redraw = TRUE;
}
}
if (op->debug) {
mvprintw(d->message->y, d->message->x,
"c: %3d ch: %04o (%d)", c, ch, ch);
clrtoeol();
redraw = TRUE;
}
} while (ch != NV_NCURSES_ENTER);
/* free the message region */
nv_ncurses_destroy_region(d->message);
d->message = NULL;
curs_set(0); /* make the cursor invisible */
refresh();
free(tmp);
tmp = strdup(buf);
return tmp;
} /* nv_ncurses_get_input() */
/*
* nv_ncurses_display_license() - print the text from the license file,
* prompt for acceptance from the user, and return whether or not the
* user accepted.
*/
static int nv_ncurses_display_license(Options *op, const char *license)
{
DataStruct *d = (DataStruct *) op->ui_priv;
TextRows *t_pager = NULL;
int ch, x, cur = 0;
int button_width, button_y, accept_x, no_accept_x, accepted;
PagerStruct *p = NULL;
char *str;
static const char *descr = "Please read the following LICENSE "
"and then select either \"Accept\" to accept the license "
"and continue with the installation, or select "
"\"Do Not Accept\" to abort the installation.";
nv_ncurses_check_resize(d, FALSE);
accepted = FALSE;
draw_license:
/* free the pager, and pager's text rows */
if (d->message) nv_ncurses_destroy_region(d->message);
if (p) nv_ncurses_destroy_pager(p);
if (t_pager) nv_ncurses_free_text_rows(t_pager);
/* create the message region and print descr in it */
nv_ncurses_do_message_region(d, NULL, descr, TRUE, 2);
/* draw the accept buttons */
button_width = 15;
button_y = d->message->h - 2;
accept_x = (d->message->w - (button_width * 3)) / 2;
no_accept_x = accept_x + (button_width * 2);
nv_ncurses_draw_button(d, d->message, accept_x, button_y, button_width,
1, "Accept", accepted, FALSE);
nv_ncurses_draw_button(d, d->message, no_accept_x, button_y, button_width,
1, "Do Not Accept", !accepted, FALSE);
/* init the pager to display the license */
t_pager = d->format_text_rows(NULL, license, d->message->w, TRUE);
p = nv_ncurses_create_pager(d, 1, d->message->h + 2, d->message->w,
d->height - d->message->h - 4,
t_pager, "NVIDIA Software License", cur);
refresh();
/* process key strokes */
do {
/* if a resize occurred, jump back to the top and redraw */
if (nv_ncurses_check_resize(d, FALSE)) {
cur = p->cur;
goto draw_license;
}
ch = getch();
switch (ch) {
case NV_NCURSES_TAB:
case KEY_LEFT:
case KEY_RIGHT:
/*
* any of left, right, and tab will toggle which button is
* selected
*/
accepted ^= 1;
nv_ncurses_draw_button(d, d->message, accept_x, button_y,
button_width, 1, "Accept",
accepted, FALSE);
nv_ncurses_draw_button(d, d->message, no_accept_x, button_y,
button_width, 1, "Do Not Accept",
!accepted, FALSE);
refresh();
break;
case NV_NCURSES_CTRL('L'):
nv_ncurses_check_resize(d, TRUE);
cur = p->cur;
goto draw_license;
break;
default:
break;
}
nv_ncurses_pager_handle_events(d, p, ch);
} while (ch != NV_NCURSES_ENTER);
/* animate the button being pushed down */
x = accepted ? accept_x : no_accept_x;
str = accepted ? "Accept" : "Do Not Accept";
nv_ncurses_erase_button(d->message, x, button_y, button_width, 1);
nv_ncurses_draw_button(d, d->message, x, button_y, button_width, 1,
str, TRUE, TRUE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
nv_ncurses_erase_button(d->message, x, button_y, button_width, 1);
nv_ncurses_draw_button(d, d->message, x, button_y, button_width, 1,
str, TRUE, FALSE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
/* free the text rows used by the pager */
nv_ncurses_free_text_rows(t_pager);
/* free the pager */
nv_ncurses_destroy_pager(p);
/* restore the footer */
nv_ncurses_set_footer(d, NV_NCURSES_DEFAULT_FOOTER_LEFT,
NV_NCURSES_DEFAULT_FOOTER_RIGHT);
/* free the message region */
nv_ncurses_destroy_region(d->message);
d->message = NULL;
refresh();
return accepted;
} /* nv_ncurses_display_license() */
/*
* nv_ncurses_message() - print a message
*/
static void nv_ncurses_message(Options *op, int level, const char *msg)
{
DataStruct *d = (DataStruct *) op->ui_priv;
int w, h, x, y, ch;
char *prefix;
if (!msg) return;
/* XXX for now, log messages are ignored by the ncurses ui */
if (level == NV_MSG_LEVEL_LOG) return;
/* determine the prefix for the message from the message level */
switch(level) {
case NV_MSG_LEVEL_MESSAGE:
prefix = NULL;
break;
case NV_MSG_LEVEL_WARNING:
prefix = "WARNING: ";
break;
case NV_MSG_LEVEL_ERROR:
prefix = "ERROR: ";
break;
default:
return;
}
draw_message:
if (d->message) {
/*
* XXX we may already have a message region allocated when we
* enter nv_ncurses_message(): we may have been in the middle
* of displaying a progress bar when we encountered an error
* that we need to report. To deal with this situation, throw
* out the existing message region; nv_ncurses_status_update()
* and nv_ncurses_status_end() will have to recreate their
* message region.
*/
nv_ncurses_destroy_region(d->message);
d->message = NULL;
}
/* create the message region and print msg in it */
nv_ncurses_do_message_region(d, prefix, msg, FALSE, 2);
/* init the dimensions of the button */
w = 6;
h = 1;
x = (d->message->w - w) / 2;
y = d->message->h - 2;
/* draw the OK button */
nv_ncurses_draw_button(d, d->message, x, y, w, h, "OK",
TRUE, FALSE);
refresh();
/* wait for enter */
do {
/* if a resize occurred, jump back to the top and redraw */
if (nv_ncurses_check_resize(d, FALSE)) goto draw_message;
ch = getch();
switch (ch) {
case NV_NCURSES_CTRL('L'):
nv_ncurses_check_resize(d, TRUE);
goto draw_message;
break;
}
} while (ch != NV_NCURSES_ENTER);
/* animate the button being pushed down */
nv_ncurses_erase_button(d->message, x, y, w, h);
nv_ncurses_draw_button(d, d->message, x, y, w, h, "OK", TRUE, TRUE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
nv_ncurses_erase_button(d->message, x, y, w, h);
nv_ncurses_draw_button(d, d->message, x, y, w, h, "OK", TRUE, FALSE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
/* free the message region */
nv_ncurses_destroy_region(d->message);
d->message = NULL;
refresh();
} /* nv_ncurses_message() */
/*
* nv_ncurses_command_output() - drop this on the floor, for now.
*/
static void nv_ncurses_command_output(Options *op, const char *msg)
{
/*
* XXX we don't currently display command output in the ncurses ui
*/
return;
} /* nv_ncurses_command_output() */
/*
* nv_ncurses_approve_command_list() - list all the commands that will be
* executed, and ask for approval.
*/
static int nv_ncurses_approve_command_list(Options *op, CommandList *cl,
const char *descr)
{
DataStruct *d = (DataStruct *) op->ui_priv;
TextRows *t_pager = NULL;
PagerStruct *p = NULL;
char *str, *question;
int button_w, button_y, yes_x, no_x;
int len, ch, x, yes, cur = 0;
/* initialize the question string */
len = strlen(descr) + 256;
question = (char *) malloc(len + 1);
snprintf(question, len, "The following operations will be performed to "
"install the %s. Is this acceptable?", descr);
/* check if the window was resized */
nv_ncurses_check_resize(d, FALSE);
yes = 1;
draw_command_list:
/* free any existing message region, pager, and pager textrows */
if (d->message) {
nv_ncurses_destroy_region(d->message);
d->message = NULL;
}
if (p) nv_ncurses_destroy_pager(p);
if (t_pager) nv_ncurses_free_text_rows(t_pager);
/* create the message region and print descr in it */
nv_ncurses_do_message_region(d, NULL, question, TRUE, 2);
/* draw the yes/no buttons */
button_w = 7;
button_y = d->message->h - 2;
yes_x = (d->message->w - button_w*3)/2 + 0;
no_x = (d->message->w - button_w*3)/2 + 2*button_w;
nv_ncurses_draw_button(d, d->message, yes_x, button_y,
button_w, 1, "Yes", yes, FALSE);
nv_ncurses_draw_button(d, d->message, no_x, button_y,
button_w, 1, "No", !yes, FALSE);
/* draw the command list */
t_pager = nv_ncurses_create_command_list_textrows(d, cl, d->message->w);
p = nv_ncurses_create_pager(d, 1, d->message->h + 2, d->message->w,
d->height - d->message->h - 4,
t_pager, "Proposed Commandlist", cur);
refresh();
/* process key strokes */
do {
/* if a resize occurred, jump back to the top and redraw */
if (nv_ncurses_check_resize(d, FALSE)) {
cur = p->cur;
goto draw_command_list;
}
ch = getch();
switch (ch) {
case NV_NCURSES_TAB:
case KEY_LEFT:
case KEY_RIGHT:
/*
* any of left, right, and tab will toggle which button is
* selected
*/
yes ^= 1;
nv_ncurses_draw_button(d, d->message, yes_x, button_y,
button_w, 1, "Yes", yes, FALSE);
nv_ncurses_draw_button(d, d->message, no_x, button_y,
button_w, 1, "No", !yes, FALSE);
refresh();
break;
case NV_NCURSES_CTRL('L'):
nv_ncurses_check_resize(d, TRUE);
cur = p->cur;
goto draw_command_list;
break;
default:
break;
}
nv_ncurses_pager_handle_events(d, p, ch);
} while (ch != NV_NCURSES_ENTER);
/* animate the button being pushed down */
x = yes ? yes_x : no_x;
str = yes ? "Yes" : "No";
nv_ncurses_erase_button(d->message, x, button_y, button_w, 1);
nv_ncurses_draw_button(d, d->message, x, button_y,
button_w, 1, str, TRUE, TRUE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
nv_ncurses_erase_button(d->message, x, button_y, button_w, 1);
nv_ncurses_draw_button(d, d->message, x, button_y,
button_w, 1, str, TRUE, FALSE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
/* free the text rows used by the pager */
nv_ncurses_free_text_rows(t_pager);
/* free the pager */
nv_ncurses_destroy_pager(p);
/* restore the footer */
nv_ncurses_set_footer(d, NV_NCURSES_DEFAULT_FOOTER_LEFT,
NV_NCURSES_DEFAULT_FOOTER_RIGHT);
/* free the message region */
nv_ncurses_destroy_region(d->message);
d->message = NULL;
refresh();
free(question);
return yes;
} /* nv_ncurses_approve_command_list() */
/*
* nv_ncurses_yes_no() - ask the yes/no question 'msg' and return TRUE for
* yes, and FALSE for no.
*/
static int nv_ncurses_yes_no(Options *op, const int def, const char *msg)
{
DataStruct *d = (DataStruct *) op->ui_priv;
int yes_x, no_x, x, y, w, h, yes, ch;
char *str;
if (!msg) return FALSE;
/* check if the window was resized */
nv_ncurses_check_resize(d, FALSE);
yes = def;
draw_yes_no:
if (d->message) {
/*
* XXX we may already have a message region allocated when we
* enter nv_ncurses_yes_no(): we may have been in the middle
* of displaying a progress bar when we encountered an error
* that we need to report. To deal with this situation, throw
* out the existing message region; nv_ncurses_status_update()
* and nv_ncurses_status_end() will have to recreate their
* message region.
*/
nv_ncurses_destroy_region(d->message);
d->message = NULL;
}
/* create the message region and print the message in it */
nv_ncurses_do_message_region(d, NULL, msg, FALSE, 2);
/* draw the yes/no buttons */
w = 7;
h = 1;
y = d->message->h - 2;
yes_x = (d->message->w - w*3)/2 + 0;
no_x = (d->message->w - w*3)/2 + 2*w;
nv_ncurses_draw_button(d, d->message, yes_x, y, w, h, "Yes", yes,FALSE);
nv_ncurses_draw_button(d, d->message, no_x, y, w, h, "No", !yes, FALSE);
refresh();
/* process key strokes */
do {
if (nv_ncurses_check_resize(d, FALSE)) goto draw_yes_no;
ch = getch();
switch (ch) {
case NV_NCURSES_TAB:
case KEY_LEFT:
case KEY_RIGHT:
/*
* any of left, right, and tab will toggle which button is
* selected
*/
yes ^= 1;
nv_ncurses_draw_button(d, d->message, yes_x, y, w, h,
"Yes", yes, FALSE);
nv_ncurses_draw_button(d, d->message, no_x, y, w, h,
"No", !yes, FALSE);
refresh();
break;
case NV_NCURSES_CTRL('L'):
nv_ncurses_check_resize(d, TRUE);
goto draw_yes_no;
break;
default:
break;
}
} while (ch != NV_NCURSES_ENTER);
/* animate the button being pushed down */
x = yes ? yes_x : no_x;
str = yes ? "Yes" : "No";
nv_ncurses_erase_button(d->message, x, y, w, h);
nv_ncurses_draw_button(d, d->message, x, y, w, 1, str, TRUE, TRUE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
nv_ncurses_erase_button(d->message, x, y, w, h);
nv_ncurses_draw_button(d, d->message, x, y, w, h, str, TRUE, FALSE);
refresh();
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
/* free the message region */
nv_ncurses_destroy_region(d->message);
d->message = NULL;
refresh();
return yes;
} /* nv_ncurses_yes_no() */
/*
* nv_ncurses_status_begin() - initialize a progress bar
*/
static void nv_ncurses_status_begin(Options *op,
const char *title, const char *msg)
{
DataStruct *d = (DataStruct *) op->ui_priv;
nv_ncurses_check_resize(d, FALSE);
/* cache the progress bar title */
d->progress_title = strdup(title);
/* create the message region for use by the progress bar */
nv_ncurses_do_progress_bar_region(d);
/* write the one line msg (truncating it, if need be) */
nv_ncurses_do_progress_bar_message(d, msg, d->message->h - 3,
d->message->w);
refresh();
} /* nv_ncurses_status_begin() */
/*
* nv_ncurses_status_update() - update the progress bar
*/
static void nv_ncurses_status_update(Options *op, const float percent,
const char *msg)
{
int i, n, h, ch;
int p[4];
char v[4];
DataStruct *d = (DataStruct *) op->ui_priv;
/*
* if the message region was deleted or if the window was resized,
* redraw the entire progress bar region.
*/
if (nv_ncurses_check_resize(d, FALSE) || !d->message) {
if (d->message) nv_ncurses_destroy_region(d->message);
nv_ncurses_do_progress_bar_region(d);
}
/* temporarily set getch() to non-blocking mode */
nodelay(stdscr, TRUE);
while ((ch = getch()) != ERR) {
/*
* if the user explicitely requested that the screen be
* redrawn by pressing CTRL-L, then also redraw the entire
* progress bar egion.
*/
if (ch == NV_NCURSES_CTRL('L')) {
nv_ncurses_check_resize(d, TRUE);
if (d->message) nv_ncurses_destroy_region(d->message);
nv_ncurses_do_progress_bar_region(d);
}
}
/* set getch() back to blocking mode */
nodelay(stdscr, FALSE);
/* compute the percentage */
n = ((int) (percent * (float) (d->message->w - 2)));
n = NV_MAX(n, 2);
init_position(p, d->message->w);
init_percentage_string(v, (int) (100.0 * percent));
h = d->message->h;
/* write the one line msg (truncating it, if need be) */
nv_ncurses_do_progress_bar_message(d, msg, h - 3, d->message->w - 2);
/* draw the progress bar */
if (d->use_color) {
for (i = 1; i <= n; i++) {
mvaddch(d->message->y + h - 2, d->message->x + i,
choose_char(i, p, v,' ') |
A_REVERSE | NV_NCURSES_INPUT_COLOR);
}
for (i = 0; i < 4; i++) {
if (p[i] >= (n+1)) {
mvaddch(d->message->y + h - 2, d->message->x + p[i],
(v[i] ? v[i] : ' ') | NV_NCURSES_INPUT_COLOR);
}
}
} else {
for (i = 2; i < n; i++) {
mvaddch(d->message->y + h - 2, d->message->x + i,
choose_char(i, p, v, ' ') | A_REVERSE);
}
for (i = 0; i < 4; i++) {
if (p[i] >= n) {
mvaddch(d->message->y + h - 2, d->message->x + p[i],
v[i] ? v[i] : '-');
}
}
}
refresh();
} /* nv_ncurses_status_update() */
/*
* nv_ncurses_status_end() - draw the progress bar at 100%
*/
static void nv_ncurses_status_end(Options *op, const char *msg)
{
int i, n, h;
int p[4];
char v[4];
DataStruct *d = (DataStruct *) op->ui_priv;
/*
* if the message region was deleted or if the window was resized,
* redraw the entire progress bar region.
*/
if (nv_ncurses_check_resize(d, FALSE) || !d->message) {
if (d->message) nv_ncurses_destroy_region(d->message);
nv_ncurses_do_progress_bar_region(d);
}
n = d->message->w - 2;
init_position(p, d->message->w);
init_percentage_string(v, 100.0);
h = d->message->h;
/* write the one line msg (truncating it, if need be) */
nv_ncurses_do_progress_bar_message(d, msg, h - 3, d->message->w - 2);
/* draw the complete progress bar */
if (d->use_color) {
for (i = 1; i < (n+1); i++) {
mvaddch(d->message->y + h - 2, d->message->x + i,
choose_char(i, p, v, ' ') |
A_REVERSE | NV_NCURSES_INPUT_COLOR);
}
} else {
for (i = 2; i < (n); i++) {
mvaddch(d->message->y + h - 2, d->message->x + i,
choose_char(i, p, v, ' ') | A_REVERSE);
}
}
refresh();
free(d->progress_title);
d->progress_title = NULL;
/* XXX don't free the message window, yet... */
} /* nv_ncurses_status_end() */
/*
* nv_ncurses_close() - close the ui: free any memory we allocated,
* and end curses mode.
*/
static void nv_ncurses_close(Options *op)
{
DataStruct *d = NULL;
if (op) {
/* XXX op may be NULL if we get called from a signal handler */
d = (DataStruct *) op->ui_priv;
nv_ncurses_destroy_region(d->header);
nv_ncurses_destroy_region(d->footer);
free(d);
}
clear();
refresh();
endwin(); /* End curses mode */
return;
} /* nv_ncurses_close() */
/****************************************************************************/
/*
* internal helper functions for manipulating the header and footer
*/
/*
* nv_ncurses_set_header() - write the title to the header region;
* note that this function does not call refresh()
*/
static void nv_ncurses_set_header(DataStruct *d, const char *title)
{
int x, y;
char *tmp;
tmp = strdup(title);
if (d->title) free(d->title);
d->title = tmp;
x = (d->header->w - strlen(d->title)) / 2;
y = 0;
attrset(d->header->attr);
nv_ncurses_clear_region(d->header);
mvaddstr(d->header->y + y, d->header->x + x, (char *) d->title);
attrset(A_NORMAL);
} /* nv_ncurses_set_header() */
/*
* nv_ncurses_set_footer() - write the left and right text to the
* footer; note that this function does not call refresh()
*/
static void nv_ncurses_set_footer(DataStruct *d, const char *left,
const char *right)
{
int x, y;
char *tmp0, *tmp1;
tmp0 = strdup(left);
tmp1 = strdup(right);
if (d->footer_left) free(d->footer_left);
if (d->footer_right) free(d->footer_right);
d->footer_left = tmp0;
d->footer_right = tmp1;
attrset(d->footer->attr);
nv_ncurses_clear_region(d->footer);
if (d->footer_left) {
y = 0;
x = 1;
mvaddstr(d->footer->y + y, d->footer->x + x, d->footer_left);
}
if (d->footer_right) {
y = 0;
x = d->footer->w - strlen(d->footer_right) - 1;
mvaddstr(d->footer->y + y, d->footer->x + x, d->footer_right);
}
attrset(A_NORMAL);
} /* nv_ncurses_set_footer() */
/****************************************************************************/
/*
* internal helper functions for manipulating RegionStructs
*/
/*
* nv_ncurses_create_region() - create a new region at the specified
* location with the specified dimensions; note that this function
* does not call refresh()
*/
static RegionStruct *nv_ncurses_create_region(DataStruct *d,
int x, int y, int w, int h,
int color,
int no_color_attr)
{
RegionStruct *region =
(RegionStruct *) malloc(sizeof(RegionStruct));
region->x = x;
region->y = y;
region->w = w;
region->h = h;
if (d->use_color) region->attr = color;
else region->attr = no_color_attr;
/* create a single line for use in clearing the region */
region->line = (char *) malloc(w + 1);
memset(region->line, ' ', w);
region->line[w] = '\0';
/* clear the region */
attrset(region->attr);
nv_ncurses_clear_region(region);
attrset(A_NORMAL);
return region;
} /* nv_ncurses_create_region() */
/*
* nv_ncurses_clear_region() - clear each line in the region; note
* that this function does not call refresh(), nor does it explicitly
* set any attributes.
*/
static void nv_ncurses_clear_region(RegionStruct *region)
{
int i;
for (i = region->y; i < (region->y + region->h); i++) {
mvaddstr(i, region->x, region->line);
}
} /* nv_ncurses_clear_region() */
/*
* nv_ncurses_destroy_region() - clear and free the RegionStruct; note
* that this function does not call refresh()
*/
static void nv_ncurses_destroy_region(RegionStruct *region)
{
if (!region) return;
attrset(A_NORMAL);
nv_ncurses_clear_region(region);
free(region->line);
free(region);
} /* nv_ncurses_destroy_region() */
/****************************************************************************/
/*
* internal helper functions for drawing buttons
*/
/*
* nv_ncurses_draw_button() - draw a button on the specified region,
* at location x,y with dimensions w,h. Give the button the label
* str.
*
* The hilite parameter, when TRUE, causes the label to be printed in
* reverse colors.
*
* The down parameter, when TRUE, causes the button to be drawn as if
* it had been pressed down (ie: shifted down and right).
*/
static void nv_ncurses_draw_button(DataStruct *d, RegionStruct *region,
int x, int y, int w, int h,
char *str, bool hilite, bool down)
{
int i, j, n, attr = 0;
if (down) x++, y++;
n = strlen(str);
n = (n > w) ? 0 : ((w - n) / 2);
if (d->use_color) {
attr = NV_NCURSES_BUTTON_COLOR;
} else if (hilite) {
attr = A_REVERSE;
} else {
attr = 0;
}
for (j = y; j < (y + h); j++) {
for (i = x; i < (x + w); i++) {
mvaddch(region->y + j, region->x + i, ' ' | attr);
}
}
if (hilite) attr |= A_REVERSE;
attron(attr);
mvaddstr(region->y + y + h/2, region->x + x + n, str);
attroff(attr);
} /* nv_ncurses_draw_button() */
/*
* nv_ncurses_erase_button() - erase the button on the specified
* region at location x,y with dimensions w,h.
*/
static void nv_ncurses_erase_button(RegionStruct *region,
int x, int y, int w, int h)
{
int i, j;
for (j = y; j <= (y + h); j++) {
for (i = x; i <= (x + w); i++) {
mvaddch(region->y + j, region->x + i, ' ' | region->attr);
}
}
} /* nv_ncurses_erase_button() */
/*****************************************************************************/
/*
* nv_ncurses_do_message_region(),
* nv_ncurses_do_progress_bar_region(),
* nv_ncurses_do_progress_bar_message() - helper functions for drawing
* regions and stuff.
*/
/*
* nv_ncurses_do_message_region() - create a new message region
* containing the string msg. The "top" argument indicates whether
* the region should be vertically positioned immediately below the
* header, or should be positioned 1/3 of the way down the screen.
* The num_extra_lines argument is used to request extra lines in the
* message region below the string (to leave room for buttons, for
* example).
*/
static void nv_ncurses_do_message_region(DataStruct *d, const char *prefix,
const char *msg, int top,
int num_extra_lines)
{
int w, h, x, y;
TextRows *t;
/*
* compute the width and height that we need (taking into account
* num_extra_lines that the caller may need for buttons
*/
w = d->width - 2;
t = d->format_text_rows(prefix, msg, w - 2, TRUE);
h = t->n + num_extra_lines + 2;
/*
* compute the starting position of the message region: either
* immediately below the header or 1/3 of the way down the screen.
*/
x = 1;
if (top) y = 2;
else y = ((d->height - (3 + h)) / 3) + 1;
/* create the message region */
d->message = nv_ncurses_create_region(d, x, y, w, h,
NV_NCURSES_MESSAGE_COLOR,
NV_NCURSES_MESSAGE_NO_COLOR);
nv_ncurses_format_print(d, d->message, 1, 1, d->message->w - 2,
d->message->h - (1 + num_extra_lines), t);
/* free the text rows */
nv_ncurses_free_text_rows(t);
} /* nv_ncurses_do_message_region() */
/*
* nv_ncurses_do_progress_bar_region() - create a message region, draw
* the progress bar's title, separator, and the empty progress bar.
*/
static void nv_ncurses_do_progress_bar_region(DataStruct *d)
{
int n, h, i, p[4];
char v[4];
/* create the message region and print the title in it */
nv_ncurses_do_message_region(d, NULL, d->progress_title, FALSE, 3);
n = d->message->w - 2;
h = d->message->h;
attrset(d->message->attr);
/* draw the horizontal separator */
for (i = 1; i <= n; i++) {
mvaddch(d->message->y + h - 4, d->message->x + i,
NV_NCURSES_HLINE | d->message->attr);
}
/* draw an empty progress bar */
init_position(p, n + 2);
init_percentage_string(v, 0);
v[2] = '0';
if (d->use_color) {
for (i = 1; i <= n; i++) {
mvaddch(d->message->y + h - 2, d->message->x + i,
choose_char(i, p, v, ' ') | NV_NCURSES_INPUT_COLOR);
}
} else {
mvaddch(d->message->y + h - 2, d->message->x + 1, '[');
for (i = 2; i < n; i++)
mvaddch(d->message->y + h - 2, d->message->x + i,
choose_char(i, p, v, '-'));
mvaddch(d->message->y + h - 2, d->message->x + n, ']');
}
} /* nv_ncurses_do_progress_bar_region() */
/*
* nv_ncurses_do_progress_bar_message() - write the one line progress
* bar message to the message region, truncating the string, if need
* be.
*/
static void nv_ncurses_do_progress_bar_message(DataStruct *d, const char *str,
int y, int w)
{
char *tmp;
/* clear the message line */
attrset(d->message->attr);
mvaddstr(d->message->y + y, d->message->x, d->message->line);
/* write the message string */
if (str) {
tmp = malloc(w + 1);
strncpy(tmp, str, w);
tmp[w] = '\0';
mvaddstr(d->message->y + y, d->message->x + 1, tmp);
free(tmp);
}
} /* nv_ncurses_do_progress_bar_message() */
/***************************************************************************/
/*
* pager functions
*/
/*
* pager functions -- these functions provide the basic behavior of a
* text viewer... used to display TextRows.
*
* d : DataStruct struct
* x : starting x coordinate of the pager
* y : starting y coordinate of the pager
* w : width of the pager
* h : height of the pager
* t : TextRows to be displayed
* label : string to be displayed in the status bar
* cur : initial current line of the pager
*/
static PagerStruct *nv_ncurses_create_pager(DataStruct *d,
int x, int y, int w, int h,
TextRows *t, const char *label,
int cur)
{
PagerStruct *p = (PagerStruct *) malloc(sizeof(PagerStruct));
p->t = t;
p->region = nv_ncurses_create_region(d, x, y, w, h, A_NORMAL, A_NORMAL);
p->label = label;
p->cur = cur;
p->page = h - 2;
nv_ncurses_pager_update(d, p);
return p;
} /* nv_ncurses_create_pager() */
/*
* nv_ncurses_destroy_pager() - free resources associated with the
* pager
*/
static void nv_ncurses_destroy_pager(PagerStruct *p)
{
nv_ncurses_destroy_region(p->region);
free(p);
} /* nv_ncurses_destroy_pager () */
/*
* nv_ncurses_pager_update() - redraw the text in the pager, and
* update the information about the pager in the footer. Note that
* this function does not call refresh().
*/
static void nv_ncurses_pager_update(DataStruct *d, PagerStruct *p)
{
int i, maxy, percent, denom;
char tmp[10];
if (!p) return;
/* determine the maximum y value for the text */
maxy = (p->cur + (p->region->h - 1));
if (maxy > p->t->n) maxy = p->t->n;
/* draw the text */
attrset(p->region->attr);
for (i = p->cur; i < maxy; i++) {
mvaddstr(p->region->y + i - p->cur, p->region->x, p->region->line);
if (p->t->t[i]) {
mvaddstr(p->region->y + i - p->cur, p->region->x, p->t->t[i]);
}
}
/* compute the percentage */
denom = p->t->n - (p->region->h - 1);
if (denom < 1) percent = 100;
else percent = ((100.0 * (float) (p->cur)) / (float) denom);
/* create the percentage string */
if (p->t->n <= (p->region->h - 1)) snprintf(tmp, 10, "All");
else if (percent <= 0) snprintf(tmp, 10, "Top");
else if (percent >= 100) snprintf(tmp, 10, "Bot");
else snprintf(tmp, 10, "%3d%%", percent);
/* update the status in the footer */
nv_ncurses_set_footer(d, p->label, tmp);
} /* nv_ncurses_pager_update() */
/*
* nv_ncurses_pager_handle_events() - process any keys that affect the
* pager.
*/
static void nv_ncurses_pager_handle_events(DataStruct *d,
PagerStruct *p, int ch)
{
int n;
if (!p) return;
n = p->t->n - (p->region->h - 1);
switch (ch) {
case KEY_UP:
if (p->cur > 0) {
p->cur--;
nv_ncurses_pager_update(d, p);
refresh();
}
break;
case KEY_DOWN:
if (p->cur < n) {
p->cur++;
nv_ncurses_pager_update(d, p);
refresh();
}
break;
case KEY_PPAGE:
if (p->cur > 0) {
p->cur -= p->page;
if (p->cur < 0) p->cur = 0;
nv_ncurses_pager_update(d, p);
refresh();
}
break;
case KEY_NPAGE:
if (p->cur < n) {
p->cur += p->page;
if (p->cur > n) p->cur = n;
nv_ncurses_pager_update(d, p);
refresh();
}
break;
}
} /* nv_ncurses_pager_handle_events() */
/*****************************************************************************/
/*
* helper functions for the progress bar
*/
static int choose_char(int i, int p[4], char v[4], char def)
{
if (p[0] == i) return v[0] ? v[0] : def;
if (p[1] == i) return v[1] ? v[1] : def;
if (p[2] == i) return v[2] ? v[2] : def;
if (p[3] == i) return v[3] ? v[3] : def;
return def;
}
static void init_percentage_string(char v[4], int n)
{
int j;
n = NV_MAX(n, 1);
v[0] = (n/100);
v[1] = (n/10) - (v[0]*10);
v[2] = (n - (v[0]*100) - (v[1]*10));
v[0] += '0';
v[1] += '0';
v[2] += '0';
v[3] = '%';
for (j = 0; j < 3; j++) {
if (v[j] == '0') v[j] = 0;
else break;
}
}
static void init_position(int p[4], int w)
{
p[0] = 0 + (w - 4)/2;
p[1] = 1 + (w - 4)/2;
p[2] = 2 + (w - 4)/2;
p[3] = 3 + (w - 4)/2;
}
/*****************************************************************************/
/*
* misc helper functions
*/
/*
* nv_ncurses_create_command_list_textrows() - build TextRows to
* describe the command list
*/
static TextRows *nv_ncurses_create_command_list_textrows(DataStruct *d,
CommandList *cl,
int w)
{
int i, len;
Command *c;
char *str, *perms;
TextRows *t0, *t1;
t0 = t1 = NULL;
for (i = 0; i < cl->num; i++) {
c = &cl->cmds[i];
str = NULL;
switch (c->cmd) {
case INSTALL_CMD:
perms = nv_ncurses_mode_to_permission_string(c->mode);
len = strlen(c->s0) + strlen(c->s1) + strlen(perms) + 64;
str = (char *) malloc(len + 1);
snprintf(str, len, "Install the file '%s' as '%s' with "
"permissions '%s'", c->s0, c->s1, perms);
free(perms);
break;
case RUN_CMD:
len = strlen(c->s0) + 64;
str = (char *) malloc(len + 1);
snprintf(str, len, "Execute the command `%s`", c->s0);
break;
case SYMLINK_CMD:
len = strlen(c->s0) + strlen(c->s1) + 64;
str = (char *) malloc(len + 1);
snprintf(str, len, "Create a symbolic link '%s' to '%s'",
c->s0, c->s1);
break;
case BACKUP_CMD:
len = strlen(c->s0) + 64;
str = (char *) malloc(len + 1);
snprintf(str, len, "Backup the file '%s'", c->s0);
break;
case DELETE_CMD:
len = strlen(c->s0) + 64;
str = (char *) malloc(len + 1);
snprintf(str, len, "Delete the file '%s'", c->s0);
break;
default:
/* XXX should not get here */
break;
}
if (str) {
t1 = d->format_text_rows(NV_BULLET_STR, str, w, TRUE);
if (t0) {
nv_ncurses_concat_text_rows(t0, t1);
nv_ncurses_free_text_rows(t1);
} else {
t0 = t1;
}
t1 = NULL;
free(str);
}
}
return t0;
} /* nv_ncurses_create_command_list_textrows() */
/*
* mode_to_permission_string() - given a mode bitmask, allocate and
* write a permission string.
*/
static char *nv_ncurses_mode_to_permission_string(mode_t mode)
{
char *s = (char *) malloc(10);
memset (s, '-', 9);
if (mode & (1 << 8)) s[0] = 'r';
if (mode & (1 << 7)) s[1] = 'w';
if (mode & (1 << 6)) s[2] = 'x';
if (mode & (1 << 5)) s[3] = 'r';
if (mode & (1 << 4)) s[4] = 'w';
if (mode & (1 << 3)) s[5] = 'x';
if (mode & (1 << 2)) s[6] = 'r';
if (mode & (1 << 1)) s[7] = 'w';
if (mode & (1 << 0)) s[8] = 'x';
s[9] = '\0';
return s;
} /* mode_to_permission_string() */
/*
* nv_ncurses_concat_text_rows() - concatenate two text rows
*/
static void nv_ncurses_concat_text_rows(TextRows *t0, TextRows *t1)
{
int n, i;
n = t0->n + t1->n;
t0->t = (char **) realloc(t0->t, sizeof(char *) * n);
for (i = 0; i < t1->n; i++) {
t0->t[i + t0->n] = strdup(t1->t[i]);
}
t0->m = NV_MAX(t0->m, t1->m);
t0->n = n;
} /* nv_ncurses_concat_text_rows() */
/*
* nv_free_text_rows() - free the TextRows data structure allocated by
* nv_format_text_rows()
*/
static void nv_ncurses_free_text_rows(TextRows *t)
{
int i;
if (!t) return;
for (i = 0; i < t->n; i++) free(t->t[i]);
if (t->t) free(t->t);
free(t);
} /* nv_free_text_rows() */
/*
* nv_ncurses_formatted_printw() - this function formats the string
* str on the region region, wrapping to the next line as necessary,
* using the bounding box specified by x, y, w, and h.
*
* The number of lines printed are returned.
*/
static int nv_ncurses_format_print(DataStruct *d, RegionStruct *region,
int x, int y, int w, int h,
TextRows *t)
{
int i, n;
n = NV_MIN(t->n, h);
attrset(region->attr);
for (i = 0; i < n; i++) {
mvaddstr(region->y + y + i, region->x + x, t->t[i]);
}
attrset(A_NORMAL);
return n;
} /* nv_ncurses_format_printw() */
/*
* nv_ncurses_check_resize() - check if the dimensions of the screen
* have changed; if so, update the old header and footer regions,
* clear everything, update our cached dimensions, and recreate the
* header and footer.
*
* XXX we could catch the SIGWINCH (window resize) signal, but that's
* asynchronous... it's safer to only attempt to handle a resize when
* we know we can.
*/
static int nv_ncurses_check_resize(DataStruct *d, bool force)
{
int x, y;
getmaxyx(stdscr, y, x);
if (!force) {
if ((x == d->width) && (y == d->height)) {
/* no resize detected... just return */
return FALSE;
}
}
/* we have been resized */
/* destroy the old header and footer */
nv_ncurses_destroy_region(d->header);
nv_ncurses_destroy_region(d->footer);
clear();
/* update our cached copy of the dimensions */
d->height = y;
d->width = x;
/* recreate the header and footer */
d->header = nv_ncurses_create_region(d, 1, 0, d->width - 2, 1,
NV_NCURSES_HEADER_COLOR,
NV_NCURSES_HEADER_NO_COLOR);
d->footer = nv_ncurses_create_region(d, 1, d->height - 2, d->width - 2, 1,
NV_NCURSES_FOOTER_COLOR,
NV_NCURSES_FOOTER_NO_COLOR);
nv_ncurses_set_header(d, d->title);
nv_ncurses_set_footer(d, d->footer_left, d->footer_right);
return TRUE;
} /* nv_ncurses_check_resize() */
|