[mucurses] Attempt to fix resource leaks
[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 /* I think we should blank the region covered by the window -
22 ncurses doesn't do this, but they have a buffer, so they
23 may just be deleting from an offscreen context whereas we
24 are guaranteed to be deleting something onscreen */
25 wmove( win, 0, 0 );
26 chtype killch = (chtype)' ';
27 do {
28 _wputch( win, killch, WRAP );
29 } while ( win->curs_x + win->curs_y );
30
31 free( win );
32
33 wmove ( stdscr, 0, 0 );
34
35 return OK;
36 }
37
38 /**
39 * Create a new derived window
40 *
41 * @v parent parent window
42 * @v nlines window height
43 * @v ncols window width
44 * @v begin_y window y origin (relative to parent)
45 * @v begin_x window x origin (relative to parent)
46 * @ret ptr return pointer to child window
47 */
48 WINDOW *derwin ( WINDOW *parent, int nlines, int ncols,
49 int begin_y, int begin_x ) {
50 WINDOW *child;
51 if ( ( (unsigned)ncols > parent->width ) ||
52 ( (unsigned)nlines > parent->height ) )
53 return NULL;
54 if ( ( child = malloc( sizeof( WINDOW ) ) ) == NULL )
55 return NULL;
56 child->ori_y = parent->ori_y + begin_y;
57 child->ori_x = parent->ori_x + begin_x;
58 child->height = nlines;
59 child->width = ncols;
60 child->parent = parent;
61 child->scr = parent->scr;
62 return child;
63 }
64
65 /**
66 * Create a duplicate of the specified window
67 *
68 * @v orig original window
69 * @ret ptr pointer to duplicate window
70 */
71 WINDOW *dupwin ( WINDOW *orig ) {
72 WINDOW *copy;
73 if ( ( copy = malloc( sizeof( WINDOW ) ) ) == NULL )
74 return NULL;
75 copy->scr = orig->scr;
76 copy->attrs = orig->attrs;
77 copy->ori_y = orig->ori_y;
78 copy->ori_x = orig->ori_x;
79 copy->curs_y = orig->curs_y;
80 copy->curs_x = orig->curs_x;
81 copy->height = orig->height;
82 copy->width = orig->width;
83 return copy;
84 }
85
86 /**
87 * Move window origin to specified coordinates
88 *
89 * @v *win window to move
90 * @v y Y position
91 * @v x X position
92 * @ret rc return status code
93 */
94 int mvwin ( WINDOW *win, int y, int x ) {
95 if ( ( ( (unsigned)y + win->height ) > LINES ) ||
96 ( ( (unsigned)x + win->width ) > COLS ) )
97 return ERR;
98
99 win->ori_y = y;
100 win->ori_x = x;
101
102 return OK;
103 }
104
105 /**
106 * Create new WINDOW
107 *
108 * @v nlines number of lines
109 * @v ncols number of columns
110 * @v begin_y column origin
111 * @v begin_x line origin
112 * @ret *win return pointer to new window
113 */
114 WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
115 WINDOW *win;
116 if ( ( (unsigned)( begin_y + nlines ) > stdscr->height ) &&
117 ( (unsigned)( begin_x + ncols ) > stdscr->width ) )
118 return NULL;
119 if ( ( win = malloc( sizeof(WINDOW) ) ) == NULL )
120 return NULL;
121 win->ori_y = begin_y;
122 win->ori_x = begin_x;
123 win->height = nlines;
124 win->width = ncols;
125 win->scr = stdscr->scr;
126 win->parent = stdscr;
127 return win;
128 }
129
130 /**
131 * Create a new sub-window
132 *
133 * @v orig parent window
134 * @v nlines window height
135 * @v ncols window width
136 * @v begin_y window y origin (absolute)
137 * @v begin_x window x origin (absolute)
138 * @ret ptr return pointer to child window
139 */
140 WINDOW *subwin ( WINDOW *parent, int nlines, int ncols,
141 int begin_y, int begin_x ) {
142 WINDOW *child;
143 child = newwin( nlines, ncols, begin_y, begin_x );
144 child->parent = parent;
145 child->scr = parent->scr;
146 return child;
147 }