-
Notifications
You must be signed in to change notification settings - Fork 28
/
ncurses-ui.c
1994 lines (1405 loc) · 52.1 KB
/
ncurses-ui.c
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
/*
* 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 and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses>.
*
*
* 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 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 int nv_ncurses_multiple_choice (Options *, const char*,
const char * const *, int, int);
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_update_indeterminate(Options*, 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, const char *, bool, bool);
static void nv_ncurses_erase_button(RegionStruct *, int, int, int, int);
static void draw_buttons(DataStruct *d, const char * const *buttons,
int num_buttons, int button, int button_w,
const int *buttons_x, int button_y);
/* 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 *);
static int nv_ncurses_paged_prompt(Options *, const char*, const char*,
const char*, const char * const *, int, int);
/* 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 char *nv_ncurses_create_command_list_text(DataStruct *, CommandList *);
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_message,
nv_ncurses_command_output,
nv_ncurses_approve_command_list,
nv_ncurses_yes_no,
nv_ncurses_multiple_choice,
nv_ncurses_paged_prompt,
nv_ncurses_status_begin,
nv_ncurses_status_update,
nv_ncurses_status_end,
nv_ncurses_update_indeterminate,
nv_ncurses_close
};
/* internal variables */
/* Save the return value of initscr() here instead of using stdscr directly.
* This avoids problems if libncurses.so doesn't expose stdscr, as was the
* case in http://bugzilla.suse.com/show_bug.cgi?id=1132282 */
static WINDOW *nv_stdscr;
/*
* nv_ncurses_detect() - initialize ncurses; return FALSE if
* initialization fails
*/
static int nv_ncurses_detect(Options *op)
{
int x, y;
if (!(nv_stdscr = 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(nv_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);
}
}
wclear(nv_stdscr); /* 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(nv_stdscr, TRUE); /* enable keypad, function keys, arrow keys, etc */
getmaxyx(nv_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);
wrefresh(nv_stdscr);
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);
wrefresh(nv_stdscr);
} /* nv_ncurses_set_title() */
/*
* nv_ncurses_get_input - prompt for user input with the given msg;
* returns the user inputted 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 - 1);
buf[MAX_BUF_LEN - 1] = '\0';
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++) {
mvwaddch(nv_stdscr, input_y, input_x + i,
BUF_CHAR(buf[i + x]) | color);
}
/* if we're scrolling, display an arrow */
if (x > 0) {
mvwaddch(nv_stdscr, input_y, input_x - 1,
'<' | d->message->attr);
} else {
mvwaddch(nv_stdscr, input_y, input_x - 1,
' ' | d->message->attr);
}
if (buf_len > (input_len - 1 + x)) {
mvwaddch(nv_stdscr, input_y, input_x + input_len,
'>' | d->message->attr);
} else {
mvwaddch(nv_stdscr, input_y, input_x + input_len,
' ' | d->message->attr);
}
redraw = FALSE;
}
/* position the cursor */
wmove(nv_stdscr, input_y, input_x - x + c);
wrefresh(nv_stdscr);
/* wait for input */
if (nv_ncurses_check_resize(d, FALSE)) goto draw_get_input;
ch = wgetch(nv_stdscr);
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);
wclrtoeol(nv_stdscr);
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 */
wrefresh(nv_stdscr);
free(tmp);
tmp = strdup(buf);
return tmp;
} /* nv_ncurses_get_input() */
/*
* 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);
wrefresh(nv_stdscr);
/* 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 = wgetch(nv_stdscr);
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);
wrefresh(nv_stdscr);
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);
wrefresh(nv_stdscr);
usleep(NV_NCURSES_BUTTON_PRESS_TIME);
/* free the message region */
nv_ncurses_destroy_region(d->message);
d->message = NULL;
wrefresh(nv_stdscr);
} /* 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;
char *commandlist, *question;
int ret, len;
const char *buttons[2] = {"Yes", "No"};
/* 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);
commandlist = nv_ncurses_create_command_list_text(d, cl);
ret = nv_ncurses_paged_prompt(op, question, "Proposed CommandList",
commandlist, buttons, 2, 0);
free(question);
free(commandlist);
return ret == 0;
} /* 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)
{
const char *buttons[2] = { "Yes", "No" };
return (nv_ncurses_multiple_choice(op, msg, buttons, 2,
(def) ? 0 : 1) == 0);
} /* nv_ncurses_yes_no() */
/*
* nv_ncurses_multiple_choice() - display a question with multiple-choice
* buttons allowing the user to select a button corresponding to the desired
* response. Returns the index of the button selected from the passed-in
* buttons array.
*/
static int nv_ncurses_multiple_choice(Options *op, const char *question,
const char * const *buttons, int num_buttons,
int default_button)
{
return nv_ncurses_paged_prompt(op, question, NULL, NULL, buttons,
num_buttons, default_button);
} /* nv_ncurses_multiple_choice() */
/*
* 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);
wrefresh(nv_stdscr);
} /* 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, w;
int p[4];
char v[4];
DataStruct *d = (DataStruct *) op->ui.priv;
int color_offset, color_flag;
char status_char;
/*
* 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(nv_stdscr, TRUE);
while ((ch = wgetch(nv_stdscr)) != ERR) {
/*
* if the user explicitly 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);
nv_ncurses_destroy_region(d->message);
nv_ncurses_do_progress_bar_region(d);
}
}
/* set getch() back to blocking mode */
nodelay(nv_stdscr, FALSE);
/* compute the percentage */
w = d->message->w - 2;
n = ((int) (percent * (float) w));
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) {
color_offset = 0;
color_flag = NV_NCURSES_INPUT_COLOR;
status_char = ' ';
} else {
color_offset = 1;
color_flag = 0;
status_char = '-';
}
for (i = 1 + color_offset; i <= w - color_offset; i++) {
int reverse_flag = i > n - color_offset ? 0 : A_REVERSE;
mvwaddch(nv_stdscr, d->message->y + h - 2, d->message->x + i,
choose_char(i, p, v, ' ') | reverse_flag | color_flag);
}
for (i = 0; i < 4; i++) {
if (p[i] >= n + 1 - color_offset) {
mvwaddch(nv_stdscr, d->message->y + h - 2, d->message->x + p[i],
(v[i] ? v[i] : status_char) | color_flag);
}
}
wrefresh(nv_stdscr);
} /* nv_ncurses_status_update() */
static void nv_ncurses_update_indeterminate(Options *op, const char *msg)
{
DataStruct *d = op->ui.priv;
static uint32_t pattern = 0x7ff;
int x;
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);
}
nv_ncurses_do_progress_bar_message(d, msg, d->message->h - 3,
d->message->w - 2);
for (x = 1; x < d->message->w - 1; x++) {
int color_flag;
if (d->use_color) {
color_flag = NV_NCURSES_INPUT_COLOR;
} else {
color_flag = 0;
}
if (pattern & (1 << (x % 32))) {
color_flag |= A_REVERSE;
}
mvwaddch(nv_stdscr, d->message->y + d->message->h - 2,
d->message->x + x, ' ' | color_flag);
}
wrefresh(nv_stdscr);
pattern = pattern << 1 | (pattern >> 31 & 1);
usleep(100000);
}
/*
* 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,