[prefix] Use garbage-collectable section names
[ipxe.git] / src / hci / mucurses / windows.c
1 #include <curses.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include "mucurses.h"
5
6 /** @file
7 *
8 * MuCurses windows instance functions
9 *
10 */
11
12 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
13
14 /**
15 * Delete a window
16 *
17 * @v *win pointer to window being deleted
18 * @ret rc return status code
19 */
20 int delwin ( WINDOW *win ) {
21 if ( win == NULL )
22 return ERR;
23
24 /* I think we should blank the region covered by the window -
25 ncurses doesn't do this, but they have a buffer, so they
26 may just be deleting from an offscreen context whereas we
27 are guaranteed to be deleting something onscreen */
28 wmove( win, 0, 0 );
29 chtype killch = (chtype)' ';
30 do {
31 _wputch( win, killch, WRAP );
32 } while ( win->curs_x + win->curs_y );
33
34 free( win );
35
36 wmove ( stdscr, 0, 0 );
37
38 return OK;
39 }
40
41 /**
42 * Create a new derived window
43 *
44 * @v parent parent window
45 * @v nlines window height
46 * @v ncols window width
47 * @v begin_y window y origin (relative to parent)
48 * @v begin_x window x origin (relative to parent)
49 * @ret ptr return pointer to child window
50 */
51 WINDOW *derwin ( WINDOW *parent, int nlines, int ncols,
52 int begin_y, int begin_x ) {
53 WINDOW *child;
54 if ( parent == NULL )
55 return NULL;
56 if ( ( child = malloc( sizeof( WINDOW ) ) ) == NULL )
57 return NULL;
58 if ( ( (unsigned)ncols > parent->width ) ||
59 ( (unsigned)nlines > parent->height ) )
60 return NULL;
61 child->ori_y = parent->ori_y + begin_y;
62 child->ori_x = parent->ori_x + begin_x;
63 child->height = nlines;
64 child->width = ncols;
65 child->parent = parent;
66 child->scr = parent->scr;
67 return child;
68 }
69
70 /**
71 * Create a duplicate of the specified window
72 *
73 * @v orig original window
74 * @ret ptr pointer to duplicate window
75 */
76 WINDOW *dupwin ( WINDOW *orig ) {
77 WINDOW *copy;
78 if ( orig == NULL )
79 return NULL;
80 if ( ( copy = malloc( sizeof( WINDOW ) ) ) == NULL )
81 return NULL;
82 copy->scr = orig->scr;
83 copy->attrs = orig->attrs;
84 copy->ori_y = orig->ori_y;
85 copy->ori_x = orig->ori_x;
86 copy->curs_y = orig->curs_y;
87 copy->curs_x = orig->curs_x;
88 copy->height = orig->height;
89 copy->width = orig->width;
90 return copy;
91 }
92
93 /**
94 * Move window origin to specified coordinates
95 *
96 * @v *win window to move
97 * @v y Y position
98 * @v x X position
99 * @ret rc return status code
100 */
101 int mvwin ( WINDOW *win, int y, int x ) {
102 if ( win == NULL )
103 return ERR;
104 if ( ( ( (unsigned)y + win->height ) > LINES ) ||
105 ( ( (unsigned)x + win->width ) > COLS ) )
106 return ERR;
107
108 win->ori_y = y;
109 win->ori_x = x;
110
111 return OK;
112 }
113
114 /**
115 * Create new WINDOW
116 *
117 * @v nlines number of lines
118 * @v ncols number of columns
119 * @v begin_y column origin
120 * @v begin_x line origin
121 * @ret *win return pointer to new window
122 */
123 WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
124 WINDOW *win;
125 if ( ( win = malloc( sizeof(WINDOW) ) ) == NULL )
126 return NULL;
127 if ( ( (unsigned)( begin_y + nlines ) > stdscr->height ) &&
128 ( (unsigned)( begin_x + ncols ) > stdscr->width ) )
129 return NULL;
130 win->ori_y = begin_y;
131 win->ori_x = begin_x;
132 win->height = nlines;
133 win->width = ncols;
134 win->scr = stdscr->scr;
135 win->parent = stdscr;
136 return win;
137 }
138
139 /**
140 * Create a new sub-window
141 *
142 * @v orig parent window
143 * @v nlines window height
144 * @v ncols window width
145 * @v begin_y window y origin (absolute)
146 * @v begin_x window x origin (absolute)
147 * @ret ptr return pointer to child window
148 */
149 WINDOW *subwin ( WINDOW *parent, int nlines, int ncols,
150 int begin_y, int begin_x ) {
151 WINDOW *child;
152 if ( parent == NULL )
153 return NULL;
154 if ( ( child = malloc( sizeof( WINDOW ) ) ) == NULL )
155 return NULL;
156 child = newwin( nlines, ncols, begin_y, begin_x );
157 child->parent = parent;
158 child->scr = parent->scr;
159 return child;
160 }