types: Remove detail and progress from struct status
[petitboot] / ui / ncurses / nc-textscreen.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <string.h>
23
24 #include <talloc/talloc.h>
25 #include <types/types.h>
26 #include <log/log.h>
27 #include <fold/fold.h>
28 #include <util/util.h>
29 #include <i18n/i18n.h>
30
31 #include "nc-cui.h"
32 #include "nc-textscreen.h"
33
34 struct text_screen *text_screen_from_scr(struct nc_scr *scr)
35 {
36         struct text_screen *text_screen;
37         assert(scr->sig == pb_text_screen_sig);
38         text_screen = container_of(scr, struct text_screen, scr);
39         return text_screen;
40 }
41
42 void text_screen_draw(struct text_screen *screen)
43 {
44         int max_y, i;
45
46         max_y = getmaxy(screen->scr.sub_ncw);
47
48         max_y = min(max_y, screen->scroll_y + screen->n_lines);
49
50         for (i = screen->scroll_y; i < max_y; i++)
51                 mvwaddstr(screen->scr.sub_ncw, i, 1, screen->lines[i]);
52
53         wrefresh(screen->scr.sub_ncw);
54 }
55
56 static void text_screen_scroll(struct text_screen *screen, int key)
57 {
58         int win_lines = getmaxy(screen->scr.sub_ncw);
59         int delta;
60
61         if (key == KEY_UP)
62                 delta = -1;
63         else if (key == KEY_DOWN)
64                 delta = 1;
65         else
66                 return;
67
68         if (screen->scroll_y + delta < 0)
69                 return;
70         if (screen->scroll_y + delta + win_lines > screen->n_lines)
71                 return;
72
73         screen->scroll_y += delta;
74         wscrl(screen->scr.sub_ncw, delta);
75
76         if (delta > 0) {
77                 mvwaddstr(screen->scr.sub_ncw, win_lines - 1, 1,
78                                 screen->lines[screen->scroll_y+win_lines-1]);
79         } else if (delta < 0) {
80                 mvwaddstr(screen->scr.sub_ncw, 0, 1,
81                                 screen->lines[screen->scroll_y]);
82         }
83
84         wrefresh(screen->scr.sub_ncw);
85 }
86
87 void text_screen_clear(struct text_screen *screen)
88 {
89         talloc_free(screen->lines);
90         screen->n_lines = 0;
91         screen->n_alloc_lines = 16;
92         screen->lines = talloc_array(screen, const char *,
93                         screen->n_alloc_lines);
94 }
95
96 static void __text_screen_append_line(struct text_screen *screen,
97                 const char *line)
98 {
99         if (screen->n_lines == screen->n_alloc_lines) {
100                 screen->n_alloc_lines *= 2;
101                 screen->lines = talloc_realloc(screen, screen->lines,
102                                                 const char *,
103                                                 screen->n_alloc_lines);
104         }
105
106         screen->lines[screen->n_lines] = line;
107         screen->n_lines++;
108 }
109
110 void text_screen_append_line(struct text_screen *screen, const char *fmt, ...)
111 {
112         char *line;
113         va_list ap;
114
115         if (fmt) {
116                 va_start(ap, fmt);
117                 line = talloc_vasprintf(screen->lines, fmt, ap);
118                 va_end(ap);
119         } else {
120                 line = "";
121         }
122
123         __text_screen_append_line(screen, line);
124 }
125
126 static int text_screen_fold_cb(void *arg, const char *buf, int len)
127 {
128         struct text_screen *screen = arg;
129
130         buf = len ? talloc_strndup(screen->lines, buf, len) : "";
131         __text_screen_append_line(screen, buf);
132
133         return 0;
134 }
135
136 void text_screen_set_text(struct text_screen *screen, const char *text)
137 {
138         fold_text(text, getmaxx(screen->scr.sub_ncw), text_screen_fold_cb,
139                         screen);
140 }
141
142 void text_screen_process_key(struct nc_scr *scr, int key)
143 {
144         struct text_screen *screen = text_screen_from_scr(scr);
145
146         switch (key) {
147         case 'x':
148         case 27: /* esc */
149                 screen->on_exit(screen->cui);
150                 break;
151         case KEY_DOWN:
152         case KEY_UP:
153                 text_screen_scroll(screen, key);
154                 break;
155         case 'h':
156                 if (screen->help_text)
157                         cui_show_help(screen->cui, screen->help_title,
158                                         screen->help_text);
159                 break;
160         default:
161                 break;
162         }
163 }
164
165 static void text_screen_resize(struct nc_scr *scr)
166 {
167         struct text_screen *screen = text_screen_from_scr(scr);
168         text_screen_draw(screen);
169 }
170
171 struct nc_scr *text_screen_scr(struct text_screen *screen)
172 {
173         return &screen->scr;
174 }
175
176 void text_screen_set_help(struct text_screen *screen, const char *title,
177                 const struct help_text *text)
178 {
179         screen->help_title = title;
180         screen->help_text = text;
181         screen->scr.frame.help = _("x=exit, h=help");
182 }
183
184 static int text_screen_post(struct nc_scr *scr)
185 {
186         struct text_screen *screen = text_screen_from_scr(scr);
187
188         if (screen->need_update) {
189                 text_screen_draw(screen);
190                 screen->need_update = false;
191         }
192
193         nc_scr_frame_draw(scr);
194         redrawwin(scr->main_ncw);
195         wrefresh(scr->main_ncw);
196         return 0;
197 }
198
199 void text_screen_init(struct text_screen *screen, struct cui *cui,
200                 const char *title, void (*on_exit)(struct cui *))
201 {
202         nc_scr_init(&screen->scr, pb_text_screen_sig, 0,
203                         cui, text_screen_process_key,
204                         text_screen_post, NULL, text_screen_resize);
205
206         /* this will establish our array of lines */
207         screen->lines = NULL;
208         text_screen_clear(screen);
209
210         screen->cui = cui;
211         screen->on_exit = on_exit;
212         screen->need_update = false;
213
214         screen->scr.frame.ltitle = talloc_strdup(screen, title);
215         screen->scr.frame.rtitle = NULL;
216         screen->scr.frame.help = _("x=exit");
217         scrollok(screen->scr.sub_ncw, true);
218 }