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