]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-scr.c
test/lib: Use talloc in list test
[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
26 #include "log/log.h"
27 #include "talloc/talloc.h"
28
29 #include "nc-scr.h"
30
31 void nc_start(void)
32 {
33         initscr();                      /* Initialize ncurses. */
34         cbreak();                       /* Disable line buffering. */
35         noecho();                       /* Disable getch() echo. */
36         keypad(stdscr, TRUE);           /* Enable num keypad keys. */
37         nonl();                         /* Disable new-line translation. */
38         intrflush(stdscr, FALSE);       /* Disable interrupt flush. */
39         curs_set(0);                    /* Make cursor invisible */
40         nodelay(stdscr, TRUE);          /* Enable non-blocking getch() */
41         while (getch() != ERR)          /* flush stdin */
42                 (void)0;
43 }
44
45 void nc_atexit(void)
46 {
47         clear();
48         refresh();
49         endwin();
50 }
51
52 static void nc_scr_status_clear(struct nc_scr *scr)
53 {
54         mvwhline(scr->main_ncw, LINES - nc_scr_pos_status, 0, ' ', COLS);
55 }
56
57 static void nc_scr_status_draw(struct nc_scr *scr)
58 {
59         mvwaddstr(scr->main_ncw, LINES - nc_scr_pos_status, 1,
60                 scr->frame.status);
61 }
62
63 void nc_scr_frame_draw(struct nc_scr *scr)
64 {
65         DBGS("title '%s'\n", scr->frame.title);
66         DBGS("help '%s'\n", scr->frame.help);
67         DBGS("status '%s'\n", scr->frame.status);
68
69         mvwaddstr(scr->main_ncw, nc_scr_pos_title, 1, scr->frame.title);
70         mvwhline(scr->main_ncw, nc_scr_pos_title_sep, 1, ACS_HLINE, COLS - 2);
71
72         mvwhline(scr->main_ncw, LINES - nc_scr_pos_help_sep, 1, ACS_HLINE,
73                 COLS - 2);
74         mvwaddstr(scr->main_ncw, LINES - nc_scr_pos_help, 1, scr->frame.help);
75         nc_scr_status_draw(scr);
76 }
77
78 void nc_scr_status_free(struct nc_scr *scr)
79 {
80         talloc_free(scr->frame.status);
81         scr->frame.status = NULL;
82         nc_scr_status_clear(scr);
83 }
84
85 /**
86  * nc_scr_status_printf - Set the text of the scr status using sprintf.
87  * @scr: The scr to opperate on.
88  * @text: The status text.
89  *
90  * The caller is reponsible for calling scr_draw() to update the display.
91  */
92
93 void nc_scr_status_printf(struct nc_scr *scr, const char *format, ...)
94 {
95         va_list ap;
96
97         nc_scr_status_free(scr);
98
99         va_start(ap, format);
100         scr->frame.status = talloc_vasprintf(scr, format, ap);
101         va_end(ap);
102
103         nc_scr_status_draw(scr);
104         wrefresh(scr->main_ncw);
105 }
106
107 int nc_scr_init(struct nc_scr *scr, enum pb_nc_sig sig, int begin_x,
108         void *ui_ctx,
109         void (*process_key)(struct nc_scr *, int),
110         int (*post)(struct nc_scr *),
111         int (*unpost)(struct nc_scr *),
112         void (*resize)(struct nc_scr *))
113 {
114         scr->sig = sig;
115         scr->ui_ctx = ui_ctx;
116         scr->process_key = process_key;
117         scr->post = post;
118         scr->unpost = unpost;
119         scr->resize = resize;
120
121         scr->main_ncw = newwin(LINES, COLS, 0, 0);
122
123         scr->sub_ncw = derwin(scr->main_ncw,
124                 LINES - nc_scr_frame_lines,
125                 COLS - 1 - begin_x,
126                 nc_scr_pos_sub,
127                 begin_x);
128
129         assert(scr->main_ncw);
130         assert(scr->sub_ncw);
131
132         return scr->main_ncw && scr->sub_ncw;
133 }