]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-scr.c
ui/ncurses: Move general nc init code to cui module
[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 #include "config.h"
20
21 #define _GNU_SOURCE
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         mvwaddstr(scr->main_ncw, LINES - nc_scr_pos_status, 1,
40                 scr->frame.status);
41 }
42
43 void nc_scr_frame_draw(struct nc_scr *scr)
44 {
45         int ltitle_len, rtitle_len;
46
47         DBGS("ltitle '%s'\n", scr->frame.ltitle);
48         DBGS("rtitle '%s'\n", scr->frame.rtitle);
49         DBGS("help '%s'\n", scr->frame.help);
50         DBGS("status '%s'\n", scr->frame.status);
51
52         ltitle_len = strlen(scr->frame.ltitle);
53         rtitle_len = scr->frame.rtitle ? strlen(scr->frame.rtitle) : 0;
54
55         /* if both ltitle and rtitle don't fit, trim rtitle */
56         if (ltitle_len + rtitle_len + nc_scr_pos_lrtitle_space > COLS - 2)
57                 rtitle_len = COLS - 2 - ltitle_len - nc_scr_pos_lrtitle_space;
58
59         mvwaddstr(scr->main_ncw, nc_scr_pos_title, 1, scr->frame.ltitle);
60         mvwaddnstr(scr->main_ncw, nc_scr_pos_title, COLS - rtitle_len - 1,
61                         scr->frame.rtitle, rtitle_len);
62         mvwhline(scr->main_ncw, nc_scr_pos_title_sep, 1, ACS_HLINE, COLS - 2);
63
64         mvwhline(scr->main_ncw, LINES - nc_scr_pos_help_sep, 1, ACS_HLINE,
65                 COLS - 2);
66         mvwaddstr(scr->main_ncw, LINES - nc_scr_pos_help, 1, scr->frame.help);
67         nc_scr_status_draw(scr);
68 }
69
70 void nc_scr_status_free(struct nc_scr *scr)
71 {
72         talloc_free(scr->frame.status);
73         scr->frame.status = NULL;
74         nc_scr_status_clear(scr);
75 }
76
77 /**
78  * nc_scr_status_printf - Set the text of the scr status using sprintf.
79  * @scr: The scr to opperate on.
80  * @text: The status text.
81  *
82  * The caller is reponsible for calling scr_draw() to update the display.
83  */
84
85 void nc_scr_status_printf(struct nc_scr *scr, const char *format, ...)
86 {
87         va_list ap;
88
89         nc_scr_status_free(scr);
90
91         va_start(ap, format);
92         scr->frame.status = talloc_vasprintf(scr, format, ap);
93         va_end(ap);
94
95         nc_scr_status_draw(scr);
96         wrefresh(scr->main_ncw);
97 }
98
99 int nc_scr_init(struct nc_scr *scr, enum pb_nc_sig sig, int begin_x,
100         void *ui_ctx,
101         void (*process_key)(struct nc_scr *, int),
102         int (*post)(struct nc_scr *),
103         int (*unpost)(struct nc_scr *),
104         void (*resize)(struct nc_scr *))
105 {
106         scr->sig = sig;
107         scr->ui_ctx = ui_ctx;
108         scr->process_key = process_key;
109         scr->post = post;
110         scr->unpost = unpost;
111         scr->resize = resize;
112
113         scr->main_ncw = newwin(LINES, COLS, 0, 0);
114
115         scr->sub_ncw = derwin(scr->main_ncw,
116                 LINES - nc_scr_frame_lines,
117                 COLS - 1 - begin_x,
118                 nc_scr_pos_sub,
119                 begin_x);
120
121         assert(scr->main_ncw);
122         assert(scr->sub_ncw);
123
124         return scr->main_ncw && scr->sub_ncw;
125 }