]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-scr.c
ui/ncurses: Update nc-config help text for Disk R/W
[petitboot] / ui / ncurses / nc-scr.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <string.h>
26
27 #include "log/log.h"
28 #include "talloc/talloc.h"
29
30 #include "nc-scr.h"
31
32 static void nc_scr_status_clear(struct nc_scr *scr)
33 {
34         mvwhline(scr->main_ncw, LINES - nc_scr_pos_status, 0, ' ', COLS);
35 }
36
37 static void nc_scr_status_draw(struct nc_scr *scr)
38 {
39         mvwaddnstr(scr->main_ncw, LINES - nc_scr_pos_status, 1,
40                 scr->frame.status, COLS);
41 }
42
43 int nc_scr_post(struct nc_scr *scr)
44 {
45         if (scr->post)
46                 return scr->post(scr);
47         return 0;
48 }
49
50 int nc_scr_unpost(struct nc_scr *scr)
51 {
52         if (scr->unpost)
53                 return scr->unpost(scr);
54         return 0;
55 }
56
57 void nc_scr_frame_draw(struct nc_scr *scr)
58 {
59         int ltitle_len, rtitle_len;
60
61         DBGS("ltitle '%s'\n", scr->frame.ltitle);
62         DBGS("rtitle '%s'\n", scr->frame.rtitle);
63         DBGS("help '%s'\n", scr->frame.help);
64         DBGS("status '%s'\n", scr->frame.status);
65
66         ltitle_len = strlen(scr->frame.ltitle);
67         rtitle_len = scr->frame.rtitle ? strlen(scr->frame.rtitle) : 0;
68
69         /* if both ltitle and rtitle don't fit, trim rtitle */
70         if (ltitle_len + rtitle_len + nc_scr_pos_lrtitle_space > COLS - 2)
71                 rtitle_len = COLS - 2 - ltitle_len - nc_scr_pos_lrtitle_space;
72
73         mvwaddstr(scr->main_ncw, nc_scr_pos_title, 1, scr->frame.ltitle);
74         mvwaddnstr(scr->main_ncw, nc_scr_pos_title, COLS - rtitle_len - 1,
75                         scr->frame.rtitle, rtitle_len);
76         mvwhline(scr->main_ncw, nc_scr_pos_title_sep, 1, ACS_HLINE, COLS - 2);
77
78         mvwhline(scr->main_ncw, LINES - nc_scr_pos_help_sep, 1, ACS_HLINE,
79                 COLS - 2);
80         mvwaddstr(scr->main_ncw, LINES - nc_scr_pos_help, 1, scr->frame.help);
81         nc_scr_status_draw(scr);
82 }
83
84 void nc_scr_status_free(struct nc_scr *scr)
85 {
86         talloc_free(scr->frame.status);
87         scr->frame.status = NULL;
88         nc_scr_status_clear(scr);
89 }
90
91 /**
92  * nc_scr_status_printf - Set the text of the scr status using sprintf.
93  * @scr: The scr to opperate on.
94  * @text: The status text.
95  *
96  * The caller is reponsible for calling scr_draw() to update the display.
97  */
98
99 void nc_scr_status_printf(struct nc_scr *scr, const char *format, ...)
100 {
101         va_list ap;
102
103         nc_scr_status_free(scr);
104
105         va_start(ap, format);
106         scr->frame.status = talloc_vasprintf(scr, format, ap);
107         va_end(ap);
108
109         nc_scr_status_draw(scr);
110         wrefresh(scr->main_ncw);
111 }
112
113 int nc_scr_init(struct nc_scr *scr, enum pb_nc_sig sig, int begin_x,
114         void *ui_ctx,
115         void (*process_key)(struct nc_scr *, int),
116         int (*post)(struct nc_scr *),
117         int (*unpost)(struct nc_scr *),
118         void (*resize)(struct nc_scr *))
119 {
120         scr->sig = sig;
121         scr->ui_ctx = ui_ctx;
122         scr->process_key = process_key;
123         scr->post = post;
124         scr->unpost = unpost;
125         scr->resize = resize;
126
127         scr->main_ncw = newwin(LINES, COLS, 0, 0);
128
129         scr->sub_ncw = derwin(scr->main_ncw,
130                 LINES - nc_scr_frame_lines,
131                 COLS - nc_scr_frame_cols - begin_x,
132                 nc_scr_pos_sub,
133                 begin_x);
134
135         assert(scr->main_ncw);
136         assert(scr->sub_ncw);
137
138         return scr->main_ncw && scr->sub_ncw;
139 }