]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-textscreen.c
ui/ncurses: Don't modify config when clearing IPMI override
[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, max_x, i;
45
46         max_y = getmaxy(screen->scr.sub_ncw);
47         max_x = getmaxx(screen->scr.sub_ncw) - 1;
48
49         max_y = min(max_y, screen->scroll_y + screen->n_lines);
50
51         for (i = screen->scroll_y; i < max_y; i++)
52                 mvwaddnstr(screen->scr.sub_ncw, i, 1, screen->lines[i], max_x);
53
54         wrefresh(screen->scr.sub_ncw);
55 }
56
57 static void text_screen_scroll(struct text_screen *screen, int key)
58 {
59         int win_lines = getmaxy(screen->scr.sub_ncw);
60         int win_cols = getmaxx(screen->scr.sub_ncw) - 1;
61         int delta;
62
63         if (key == KEY_UP)
64                 delta = -1;
65         else if (key == KEY_DOWN)
66                 delta = 1;
67         else
68                 return;
69
70         if (screen->scroll_y + delta < 0)
71                 return;
72         if (screen->scroll_y + delta + win_lines > screen->n_lines)
73                 return;
74
75         screen->scroll_y += delta;
76         wscrl(screen->scr.sub_ncw, delta);
77
78         if (delta > 0) {
79                 mvwaddnstr(screen->scr.sub_ncw, win_lines - 1, 1,
80                                 screen->lines[screen->scroll_y+win_lines-1],
81                                 win_cols);
82         } else if (delta < 0) {
83                 mvwaddnstr(screen->scr.sub_ncw, 0, 1,
84                                 screen->lines[screen->scroll_y], win_cols);
85         }
86
87         wrefresh(screen->scr.sub_ncw);
88 }
89
90 void text_screen_clear(struct text_screen *screen)
91 {
92         talloc_free(screen->lines);
93         screen->n_lines = 0;
94         screen->n_alloc_lines = 16;
95         screen->lines = talloc_array(screen, const char *,
96                         screen->n_alloc_lines);
97 }
98
99 static void __text_screen_append_line(struct text_screen *screen,
100                 const char *line)
101 {
102         if (screen->n_lines == screen->n_alloc_lines) {
103                 screen->n_alloc_lines *= 2;
104                 screen->lines = talloc_realloc(screen, screen->lines,
105                                                 const char *,
106                                                 screen->n_alloc_lines);
107         }
108
109         screen->lines[screen->n_lines] = line;
110         screen->n_lines++;
111 }
112
113 void text_screen_append_line(struct text_screen *screen, const char *fmt, ...)
114 {
115         char *line;
116         va_list ap;
117
118         if (fmt) {
119                 va_start(ap, fmt);
120                 line = talloc_vasprintf(screen->lines, fmt, ap);
121                 va_end(ap);
122         } else {
123                 line = "";
124         }
125
126         __text_screen_append_line(screen, line);
127 }
128
129 static int text_screen_fold_cb(void *arg, const char *buf, int len)
130 {
131         struct text_screen *screen = arg;
132
133         buf = len ? talloc_strndup(screen->lines, buf, len) : "";
134         __text_screen_append_line(screen, buf);
135
136         return 0;
137 }
138
139 void text_screen_set_text(struct text_screen *screen, const char *text)
140 {
141         fold_text(text, getmaxx(screen->scr.sub_ncw) - 1, text_screen_fold_cb,
142                         screen);
143 }
144
145 void text_screen_process_key(struct nc_scr *scr, int key)
146 {
147         struct text_screen *screen = text_screen_from_scr(scr);
148
149         switch (key) {
150         case 'x':
151         case 27: /* esc */
152                 screen->on_exit(screen->cui);
153                 break;
154         case KEY_DOWN:
155         case KEY_UP:
156                 text_screen_scroll(screen, key);
157                 break;
158         case 'h':
159                 if (screen->help_text)
160                         cui_show_help(screen->cui, screen->help_title,
161                                         screen->help_text);
162                 break;
163         default:
164                 break;
165         }
166 }
167
168 static void text_screen_resize(struct nc_scr *scr)
169 {
170         struct text_screen *screen = text_screen_from_scr(scr);
171         text_screen_draw(screen);
172 }
173
174 struct nc_scr *text_screen_scr(struct text_screen *screen)
175 {
176         return &screen->scr;
177 }
178
179 void text_screen_set_help(struct text_screen *screen, const char *title,
180                 const struct help_text *text)
181 {
182         screen->help_title = title;
183         screen->help_text = text;
184         screen->scr.frame.help = _("x=exit, h=help");
185 }
186
187 static int text_screen_post(struct nc_scr *scr)
188 {
189         struct text_screen *screen = text_screen_from_scr(scr);
190
191         if (screen->need_update) {
192                 text_screen_draw(screen);
193                 screen->need_update = false;
194         }
195
196         nc_scr_frame_draw(scr);
197         redrawwin(scr->main_ncw);
198         wrefresh(scr->main_ncw);
199         return 0;
200 }
201
202 void text_screen_init(struct text_screen *screen, struct cui *cui,
203                 const char *title, void (*on_exit)(struct cui *))
204 {
205         nc_scr_init(&screen->scr, pb_text_screen_sig, 0,
206                         cui, text_screen_process_key,
207                         text_screen_post, NULL, text_screen_resize);
208
209         /* this will establish our array of lines */
210         screen->lines = NULL;
211         text_screen_clear(screen);
212
213         screen->cui = cui;
214         screen->on_exit = on_exit;
215         screen->need_update = false;
216
217         screen->scr.frame.ltitle = talloc_strdup(screen, title);
218         screen->scr.frame.rtitle = NULL;
219         screen->scr.frame.help = _("x=exit");
220         scrollok(screen->scr.sub_ncw, true);
221 }