]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-scr.c
ff20d637781289572d962430b0a094e7195447ce
[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
42         /* We may be operating with an incorrect $TERM type; in this case
43          * the keymappings will be slightly broken. We want at least
44          * backspace to work though, so we'll define both DEL and ^H to
45          * map to backspace */
46         define_key("\x7f", KEY_BACKSPACE);
47         define_key("\x08", KEY_BACKSPACE);
48
49         while (getch() != ERR)          /* flush stdin */
50                 (void)0;
51 }
52
53 void nc_atexit(void)
54 {
55         clear();
56         refresh();
57         endwin();
58 }
59
60 static void nc_scr_status_clear(struct nc_scr *scr)
61 {
62         mvwhline(scr->main_ncw, LINES - nc_scr_pos_status, 0, ' ', COLS);
63 }
64
65 static void nc_scr_status_draw(struct nc_scr *scr)
66 {
67         mvwaddstr(scr->main_ncw, LINES - nc_scr_pos_status, 1,
68                 scr->frame.status);
69 }
70
71 void nc_scr_frame_draw(struct nc_scr *scr)
72 {
73         DBGS("title '%s'\n", scr->frame.title);
74         DBGS("help '%s'\n", scr->frame.help);
75         DBGS("status '%s'\n", scr->frame.status);
76
77         mvwaddstr(scr->main_ncw, nc_scr_pos_title, 1, scr->frame.title);
78         mvwhline(scr->main_ncw, nc_scr_pos_title_sep, 1, ACS_HLINE, COLS - 2);
79
80         mvwhline(scr->main_ncw, LINES - nc_scr_pos_help_sep, 1, ACS_HLINE,
81                 COLS - 2);
82         mvwaddstr(scr->main_ncw, LINES - nc_scr_pos_help, 1, scr->frame.help);
83         nc_scr_status_draw(scr);
84 }
85
86 void nc_scr_status_free(struct nc_scr *scr)
87 {
88         talloc_free(scr->frame.status);
89         scr->frame.status = NULL;
90         nc_scr_status_clear(scr);
91 }
92
93 /**
94  * nc_scr_status_printf - Set the text of the scr status using sprintf.
95  * @scr: The scr to opperate on.
96  * @text: The status text.
97  *
98  * The caller is reponsible for calling scr_draw() to update the display.
99  */
100
101 void nc_scr_status_printf(struct nc_scr *scr, const char *format, ...)
102 {
103         va_list ap;
104
105         nc_scr_status_free(scr);
106
107         va_start(ap, format);
108         scr->frame.status = talloc_vasprintf(scr, format, ap);
109         va_end(ap);
110
111         nc_scr_status_draw(scr);
112         wrefresh(scr->main_ncw);
113 }
114
115 int nc_scr_init(struct nc_scr *scr, enum pb_nc_sig sig, int begin_x,
116         void *ui_ctx,
117         void (*process_key)(struct nc_scr *, int),
118         int (*post)(struct nc_scr *),
119         int (*unpost)(struct nc_scr *),
120         void (*resize)(struct nc_scr *))
121 {
122         scr->sig = sig;
123         scr->ui_ctx = ui_ctx;
124         scr->process_key = process_key;
125         scr->post = post;
126         scr->unpost = unpost;
127         scr->resize = resize;
128
129         scr->main_ncw = newwin(LINES, COLS, 0, 0);
130
131         scr->sub_ncw = derwin(scr->main_ncw,
132                 LINES - nc_scr_frame_lines,
133                 COLS - 1 - begin_x,
134                 nc_scr_pos_sub,
135                 begin_x);
136
137         assert(scr->main_ncw);
138         assert(scr->sub_ncw);
139
140         return scr->main_ncw && scr->sub_ncw;
141 }