1 #if HAVE_CONFIG_H
2 #include "config.h"
3 #endif /* HAVE_CONFIG_H */
4
5 #if HAVE_CURSES_H
6 #include <curses.h>
7 #elif HAVE_NCURSES_CURSES_H
8 #include <ncurses/curses.h>
9 #endif
10
11 #include "cgdb_clog.h"
12 #include "sys_util.h"
13 #include "sys_win.h"
14
15 const int SWIN_A_NORMAL = A_NORMAL;
16 const int SWIN_A_STANDOUT = A_STANDOUT;
17 const int SWIN_A_UNDERLINE = A_UNDERLINE;
18 const int SWIN_A_REVERSE = A_REVERSE;
19 const int SWIN_A_BLINK = A_BLINK;
20 const int SWIN_A_DIM = A_DIM;
21 const int SWIN_A_BOLD = A_BOLD;
22
23 const int SWIN_KEY_BACKSPACE = KEY_BACKSPACE;
24
25 SWIN_CHTYPE SWIN_SYM_VLINE;
26 SWIN_CHTYPE SWIN_SYM_HLINE;
27 SWIN_CHTYPE SWIN_SYM_LTEE;
28
29 static bool initscr_intialized = false;
30
31 /**
32 * Set the ESCDELAY environment variable.
33 *
34 * This environment variable is necessary for ncurses to not delay
35 * user input when the ESCAPE key is pressed. CGDB does it's own handling
36 * of escape.
37 *
38 * @return
39 * True on success or False on failure
40 */
swin_set_escdelay(void)41 static bool swin_set_escdelay(void)
42 {
43 static char escdelay[] = "ESCDELAY=0";
44 bool success = putenv(escdelay) == 0;
45
46 if (!success)
47 clog_error(CLOG_CGDB, "putenv(\"%s\") failed", escdelay);
48
49 return success;
50 }
51
swin_start()52 bool swin_start()
53 {
54 bool success = swin_set_escdelay();
55 if (success) {
56 success = swin_initscr() != NULL;
57 if (success) {
58 if (swin_has_colors()) {
59 swin_start_color();
60 swin_use_default_colors();
61 }
62
63 swin_refresh();
64 }
65 }
66
67 return success;
68 }
69
70 /* Determines the terminal type and initializes all data structures. */
swin_initscr()71 SWINDOW *swin_initscr()
72 {
73 SWINDOW *win = (SWINDOW *)initscr();
74
75 #ifdef HAVE_CYGWIN
76 SWIN_SYM_VLINE = ':';
77 #else
78 SWIN_SYM_VLINE = ACS_VLINE;
79 #endif
80
81 SWIN_SYM_HLINE = ACS_HLINE;
82 SWIN_SYM_LTEE = ACS_LTEE;
83
84 initscr_intialized = true;
85
86 return win;
87 }
88
swin_endwin()89 int swin_endwin()
90 {
91 int result = 0;
92
93 if (initscr_intialized)
94 result = endwin();
95
96 return result;
97 }
98
swin_lines()99 int swin_lines()
100 {
101 return LINES;
102 }
103
swin_cols()104 int swin_cols()
105 {
106 return COLS;
107 }
108
swin_colors()109 int swin_colors()
110 {
111 return COLORS;
112 }
113
swin_color_pairs()114 int swin_color_pairs()
115 {
116 return COLOR_PAIRS;
117 }
118
swin_has_colors()119 int swin_has_colors()
120 {
121 return has_colors();
122 }
123
swin_start_color()124 int swin_start_color()
125 {
126 int result = start_color();
127 if (result == ERR) {
128 clog_error(CLOG_CGDB, "start_color failed");
129 }
130 return result;
131 }
132
swin_use_default_colors()133 int swin_use_default_colors()
134 {
135 int result = use_default_colors();
136 if (result == ERR) {
137 clog_error(CLOG_CGDB, "use_default_colors failed");
138 }
139 return result;
140 }
141
swin_supports_default_color_pairs_extension()142 bool swin_supports_default_color_pairs_extension()
143 {
144 int result = init_pair(65, -1, COLOR_BLACK);
145 return result != ERR;
146 }
147
swin_resizeterm(int lines,int columns)148 int swin_resizeterm(int lines, int columns)
149 {
150 return resizeterm(lines, columns);
151 }
152
swin_scrl(int n)153 int swin_scrl(int n)
154 {
155 return scrl(n);
156 }
157
swin_keypad(SWINDOW * win,int bf)158 int swin_keypad(SWINDOW *win, int bf)
159 {
160 return keypad((WINDOW *)win, bf);
161 }
162
swin_tigetstr(const char * capname)163 char *swin_tigetstr(const char *capname)
164 {
165 return tigetstr((char*)capname);
166 }
167
swin_move(int y,int x)168 int swin_move(int y, int x)
169 {
170 return move(y, x);
171 }
172
swin_wmove(SWINDOW * win,int y,int x)173 int swin_wmove(SWINDOW *win, int y, int x)
174 {
175 return wmove((WINDOW *)win, y, x);
176 }
177
swin_wattron(SWINDOW * win,int attrs)178 int swin_wattron(SWINDOW *win, int attrs)
179 {
180 return wattron((WINDOW *)win, attrs);
181 }
182
swin_wattroff(SWINDOW * win,int attrs)183 int swin_wattroff(SWINDOW *win, int attrs)
184 {
185 return wattroff((WINDOW *)win, attrs);
186 }
187
swin_newwin(int nlines,int ncols,int begin_y,int begin_x)188 SWINDOW *swin_newwin(int nlines, int ncols, int begin_y, int begin_x)
189 {
190 return (SWINDOW *)newwin(nlines, ncols, begin_y, begin_x);
191 }
192
swin_delwin(SWINDOW * win)193 int swin_delwin(SWINDOW *win)
194 {
195 return delwin((WINDOW *)win);
196 }
197
swin_curs_set(int visibility)198 int swin_curs_set(int visibility)
199 {
200 return curs_set(visibility);
201 }
202
swin_getcurx(const SWINDOW * win)203 int swin_getcurx(const SWINDOW *win)
204 {
205 return getcurx((WINDOW *)win);
206 }
207
swin_getcury(const SWINDOW * win)208 int swin_getcury(const SWINDOW *win)
209 {
210 return getcury((WINDOW *)win);
211 }
212
swin_getbegx(const SWINDOW * win)213 int swin_getbegx(const SWINDOW *win)
214 {
215 return getbegx((WINDOW *)win);
216 }
217
swin_getbegy(const SWINDOW * win)218 int swin_getbegy(const SWINDOW *win)
219 {
220 return getbegy((WINDOW *)win);
221 }
222
swin_getmaxx(const SWINDOW * win)223 int swin_getmaxx(const SWINDOW *win)
224 {
225 return getmaxx((WINDOW *)win);
226 }
227
swin_getmaxy(const SWINDOW * win)228 int swin_getmaxy(const SWINDOW *win)
229 {
230 return getmaxy((WINDOW *)win);
231 }
232
swin_werase(SWINDOW * win)233 int swin_werase(SWINDOW *win)
234 {
235 return werase((WINDOW *)win);
236 }
237
swin_wvline(SWINDOW * win,SWIN_CHTYPE ch,int n)238 int swin_wvline(SWINDOW *win, SWIN_CHTYPE ch, int n)
239 {
240 return wvline((WINDOW *)win, ch, n);
241 }
242
swin_waddch(SWINDOW * win,const SWIN_CHTYPE ch)243 int swin_waddch(SWINDOW *win, const SWIN_CHTYPE ch)
244 {
245 return waddch((WINDOW *)win, ch);
246 }
247
swin_wprintw(SWINDOW * win,const char * fmt,...)248 int swin_wprintw(SWINDOW *win, const char *fmt, ...)
249 {
250 int ret;
251 va_list ap;
252
253 va_start(ap, fmt);
254 ret = vw_printw((WINDOW *)win, fmt, ap);
255 va_end(ap);
256
257 return ret;
258 }
259
swin_waddnstr(SWINDOW * win,const char * str,int n)260 int swin_waddnstr(SWINDOW *win, const char *str, int n)
261 {
262 return waddnstr((WINDOW *)win, str, n);
263 }
264
swin_wclrtoeol(SWINDOW * win)265 int swin_wclrtoeol(SWINDOW *win)
266 {
267 return wclrtoeol((WINDOW *)win);
268 }
269
swin_mvwprintw(SWINDOW * win,int y,int x,const char * fmt,...)270 int swin_mvwprintw(SWINDOW *win, int y, int x, const char *fmt, ...)
271 {
272 int ret;
273
274 ret = wmove((WINDOW *)win, y, x);
275 if (ret != ERR)
276 {
277 va_list ap;
278
279 va_start(ap, fmt);
280 ret = vw_printw((WINDOW *)win, fmt, ap);
281 va_end(ap);
282 }
283
284 return ret;
285 }
286
swin_refresh()287 int swin_refresh()
288 {
289 return refresh();
290 }
291
swin_wnoutrefresh(SWINDOW * win)292 int swin_wnoutrefresh(SWINDOW *win)
293 {
294 return wnoutrefresh((WINDOW *)win);
295 }
296
swin_wrefresh(SWINDOW * win)297 int swin_wrefresh(SWINDOW *win)
298 {
299 return wrefresh((WINDOW *)win);
300 }
301
swin_doupdate()302 int swin_doupdate()
303 {
304 return doupdate();
305 }
306
swin_init_pair(int pair,int f,int b)307 int swin_init_pair(int pair, int f, int b)
308 {
309 int result = init_pair(pair, f, b);
310 if (result == ERR) {
311 clog_error(CLOG_CGDB, "init_pair failed pair=%d f=%d b=%d", pair, f, b);
312 }
313
314 return result;
315 }
316
swin_pair_content(int pair,int * fin,int * bin)317 int swin_pair_content(int pair, int *fin, int *bin)
318 {
319 int ret;
320 short f, b;
321
322 ret = pair_content(pair, &f, &b);
323 if (ret == ERR) {
324 clog_error(CLOG_CGDB, "pair_content failed pair=%d", pair);
325 }
326
327 *fin = f;
328 *bin = b;
329 return ret;
330 }
331
swin_color_pair(int pair)332 int swin_color_pair(int pair)
333 {
334 return COLOR_PAIR(pair);
335 }
336
swin_raw(void)337 int swin_raw(void)
338 {
339 return raw();
340 }
341