Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmpdcurses/pdcurses/initscr.c
3153 views
1
/* PDCurses */
2
3
#include <curspriv.h>
4
5
/*man-start**************************************************************
6
7
initscr
8
-------
9
10
### Synopsis
11
12
WINDOW *initscr(void);
13
WINDOW *Xinitscr(int argc, char **argv);
14
int endwin(void);
15
bool isendwin(void);
16
SCREEN *newterm(const char *type, FILE *outfd, FILE *infd);
17
SCREEN *set_term(SCREEN *new);
18
void delscreen(SCREEN *sp);
19
20
int resize_term(int nlines, int ncols);
21
bool is_termresized(void);
22
const char *curses_version(void);
23
void PDC_get_version(PDC_VERSION *ver);
24
25
int set_tabsize(int tabsize);
26
27
### Description
28
29
initscr() should be the first curses routine called. It will
30
initialize all curses data structures, and arrange that the first
31
call to refresh() will clear the screen. In case of error, initscr()
32
will write a message to standard error and end the program.
33
34
endwin() should be called before exiting or escaping from curses mode
35
temporarily. It will restore tty modes, move the cursor to the lower
36
left corner of the screen and reset the terminal into the proper
37
non-visual mode. To resume curses after a temporary escape, call
38
refresh() or doupdate().
39
40
isendwin() returns TRUE if endwin() has been called without a
41
subsequent refresh, unless SP is NULL.
42
43
In some implementations of curses, newterm() allows the use of
44
multiple terminals. Here, it's just an alternative interface for
45
initscr(). It always returns SP, or NULL.
46
47
delscreen() frees the memory allocated by newterm() or initscr(),
48
since it's not freed by endwin(). This function is usually not
49
needed. In PDCurses, the parameter must be the value of SP, and
50
delscreen() sets SP to NULL.
51
52
set_term() does nothing meaningful in PDCurses, but is included for
53
compatibility with other curses implementations.
54
55
resize_term() is effectively two functions: When called with nonzero
56
values for nlines and ncols, it attempts to resize the screen to the
57
given size. When called with (0, 0), it merely adjusts the internal
58
structures to match the current size after the screen is resized by
59
the user. On the currently supported platforms, SDL, Windows console,
60
and X11 allow user resizing, while DOS, OS/2, SDL and Windows console
61
allow programmatic resizing. If you want to support user resizing,
62
you should check for getch() returning KEY_RESIZE, and/or call
63
is_termresized() at appropriate times; if either condition occurs,
64
call resize_term(0, 0). Then, with either user or programmatic
65
resizing, you'll have to resize any windows you've created, as
66
appropriate; resize_term() only handles stdscr and curscr.
67
68
is_termresized() returns TRUE if the curses screen has been resized
69
by the user, and a call to resize_term() is needed. Checking for
70
KEY_RESIZE is generally preferable, unless you're not handling the
71
keyboard.
72
73
curses_version() returns a string describing the version of PDCurses.
74
75
PDC_get_version() fills a PDC_VERSION structure provided by the user
76
with more detailed version info (see curses.h).
77
78
set_tabsize() sets the tab interval, stored in TABSIZE.
79
80
### Return Value
81
82
All functions return NULL on error, except endwin(), which always
83
returns OK, and resize_term(), which returns either OK or ERR.
84
85
### Portability
86
X/Open ncurses NetBSD
87
initscr Y Y Y
88
endwin Y Y Y
89
isendwin Y Y Y
90
newterm Y Y Y
91
set_term Y Y Y
92
delscreen Y Y Y
93
resize_term - Y Y
94
set_tabsize - Y Y
95
curses_version - Y -
96
is_termresized - - -
97
98
**man-end****************************************************************/
99
100
#include <stdlib.h>
101
102
char ttytype[128];
103
104
const char *_curses_notice = "PDCurses " PDC_VERDOT " - " __DATE__;
105
106
SCREEN *SP = (SCREEN*)NULL; /* curses variables */
107
WINDOW *curscr = (WINDOW *)NULL; /* the current screen image */
108
WINDOW *stdscr = (WINDOW *)NULL; /* the default screen window */
109
110
int LINES = 0; /* current terminal height */
111
int COLS = 0; /* current terminal width */
112
int TABSIZE = 8;
113
114
MOUSE_STATUS Mouse_status;
115
116
extern RIPPEDOFFLINE linesripped[5];
117
extern char linesrippedoff;
118
119
WINDOW *initscr(void)
120
{
121
int i;
122
123
PDC_LOG(("initscr() - called\n"));
124
125
if (SP && SP->alive)
126
return NULL;
127
128
SP = calloc(1, sizeof(SCREEN));
129
if (!SP)
130
return NULL;
131
132
if (PDC_scr_open() == ERR)
133
{
134
fprintf(stderr, "initscr(): Unable to create SP\n");
135
exit(8);
136
}
137
138
SP->autocr = TRUE; /* cr -> lf by default */
139
SP->raw_out = FALSE; /* tty I/O modes */
140
SP->raw_inp = FALSE; /* tty I/O modes */
141
SP->cbreak = TRUE;
142
SP->key_modifiers = 0L;
143
SP->return_key_modifiers = FALSE;
144
SP->echo = TRUE;
145
SP->visibility = 1;
146
SP->resized = FALSE;
147
SP->_trap_mbe = 0L;
148
SP->linesrippedoff = 0;
149
SP->linesrippedoffontop = 0;
150
SP->delaytenths = 0;
151
SP->line_color = -1;
152
SP->lastscr = (WINDOW *)NULL;
153
SP->dbfp = NULL;
154
SP->color_started = FALSE;
155
SP->dirty = FALSE;
156
SP->sel_start = -1;
157
SP->sel_end = -1;
158
159
SP->orig_cursor = PDC_get_cursor_mode();
160
161
LINES = SP->lines = PDC_get_rows();
162
COLS = SP->cols = PDC_get_columns();
163
164
if (LINES < 2 || COLS < 2)
165
{
166
fprintf(stderr, "initscr(): LINES=%d COLS=%d: too small.\n",
167
LINES, COLS);
168
exit(4);
169
}
170
171
curscr = newwin(LINES, COLS, 0, 0);
172
if (!curscr)
173
{
174
fprintf(stderr, "initscr(): Unable to create curscr.\n");
175
exit(2);
176
}
177
178
SP->lastscr = newwin(LINES, COLS, 0, 0);
179
if (!SP->lastscr)
180
{
181
fprintf(stderr, "initscr(): Unable to create SP->lastscr.\n");
182
exit(2);
183
}
184
185
wattrset(SP->lastscr, (chtype)(-1));
186
werase(SP->lastscr);
187
188
PDC_slk_initialize();
189
LINES -= SP->slklines;
190
191
/* We have to sort out ripped off lines here, and reduce the height
192
of stdscr by the number of lines ripped off */
193
194
for (i = 0; i < linesrippedoff; i++)
195
{
196
if (linesripped[i].line < 0)
197
(*linesripped[i].init)(newwin(1, COLS, LINES - 1, 0), COLS);
198
else
199
(*linesripped[i].init)(newwin(1, COLS,
200
SP->linesrippedoffontop++, 0), COLS);
201
202
SP->linesrippedoff++;
203
LINES--;
204
}
205
206
linesrippedoff = 0;
207
208
stdscr = newwin(LINES, COLS, SP->linesrippedoffontop, 0);
209
if (!stdscr)
210
{
211
fprintf(stderr, "initscr(): Unable to create stdscr.\n");
212
exit(1);
213
}
214
215
wclrtobot(stdscr);
216
217
/* If preserving the existing screen, don't allow a screen clear */
218
219
if (SP->_preserve)
220
{
221
untouchwin(curscr);
222
untouchwin(stdscr);
223
stdscr->_clear = FALSE;
224
curscr->_clear = FALSE;
225
}
226
else
227
curscr->_clear = TRUE;
228
229
SP->atrtab = calloc(PDC_COLOR_PAIRS, sizeof(PDC_PAIR));
230
if (!SP->atrtab)
231
return NULL;
232
PDC_init_atrtab(); /* set up default colors */
233
234
MOUSE_X_POS = MOUSE_Y_POS = -1;
235
BUTTON_STATUS(1) = BUTTON_RELEASED;
236
BUTTON_STATUS(2) = BUTTON_RELEASED;
237
BUTTON_STATUS(3) = BUTTON_RELEASED;
238
Mouse_status.changes = 0;
239
240
SP->alive = TRUE;
241
242
def_shell_mode();
243
244
sprintf(ttytype, "pdcurses|PDCurses for %s", PDC_sysname());
245
246
SP->c_buffer = malloc(_INBUFSIZ * sizeof(int));
247
if (!SP->c_buffer)
248
return NULL;
249
SP->c_pindex = 0;
250
SP->c_gindex = 1;
251
252
SP->c_ungch = malloc(NUNGETCH * sizeof(int));
253
if (!SP->c_ungch)
254
return NULL;
255
SP->c_ungind = 0;
256
SP->c_ungmax = NUNGETCH;
257
258
return stdscr;
259
}
260
261
#ifdef XCURSES
262
WINDOW *Xinitscr(int argc, char **argv)
263
{
264
PDC_LOG(("Xinitscr() - called\n"));
265
266
PDC_set_args(argc, argv);
267
return initscr();
268
}
269
#endif
270
271
int endwin(void)
272
{
273
PDC_LOG(("endwin() - called\n"));
274
275
/* Allow temporary exit from curses using endwin() */
276
277
def_prog_mode();
278
PDC_scr_close();
279
280
SP->alive = FALSE;
281
282
return OK;
283
}
284
285
bool isendwin(void)
286
{
287
PDC_LOG(("isendwin() - called\n"));
288
289
return SP ? !(SP->alive) : FALSE;
290
}
291
292
SCREEN *newterm(const char *type, FILE *outfd, FILE *infd)
293
{
294
PDC_LOG(("newterm() - called\n"));
295
296
return initscr() ? SP : NULL;
297
}
298
299
SCREEN *set_term(SCREEN *new)
300
{
301
PDC_LOG(("set_term() - called\n"));
302
303
/* We only support one screen */
304
305
return (new == SP) ? SP : NULL;
306
}
307
308
void delscreen(SCREEN *sp)
309
{
310
PDC_LOG(("delscreen() - called\n"));
311
312
if (!SP || sp != SP)
313
return;
314
315
free(SP->c_ungch);
316
free(SP->c_buffer);
317
free(SP->atrtab);
318
319
PDC_slk_free(); /* free the soft label keys, if needed */
320
321
delwin(stdscr);
322
delwin(curscr);
323
delwin(SP->lastscr);
324
stdscr = (WINDOW *)NULL;
325
curscr = (WINDOW *)NULL;
326
SP->lastscr = (WINDOW *)NULL;
327
328
SP->alive = FALSE;
329
330
PDC_scr_free();
331
332
free(SP);
333
SP = (SCREEN *)NULL;
334
}
335
336
int resize_term(int nlines, int ncols)
337
{
338
PDC_LOG(("resize_term() - called: nlines %d\n", nlines));
339
340
if (!stdscr || PDC_resize_screen(nlines, ncols) == ERR)
341
return ERR;
342
343
SP->resized = FALSE;
344
345
SP->lines = PDC_get_rows();
346
LINES = SP->lines - SP->linesrippedoff - SP->slklines;
347
SP->cols = COLS = PDC_get_columns();
348
349
if (SP->cursrow >= SP->lines)
350
SP->cursrow = SP->lines - 1;
351
if (SP->curscol >= SP->cols)
352
SP->curscol = SP->cols - 1;
353
354
if (wresize(curscr, SP->lines, SP->cols) == ERR ||
355
wresize(stdscr, LINES, COLS) == ERR ||
356
wresize(SP->lastscr, SP->lines, SP->cols) == ERR)
357
return ERR;
358
359
werase(SP->lastscr);
360
curscr->_clear = TRUE;
361
362
if (SP->slk_winptr)
363
{
364
if (wresize(SP->slk_winptr, SP->slklines, COLS) == ERR)
365
return ERR;
366
367
wmove(SP->slk_winptr, 0, 0);
368
wclrtobot(SP->slk_winptr);
369
PDC_slk_initialize();
370
slk_noutrefresh();
371
}
372
373
touchwin(stdscr);
374
wnoutrefresh(stdscr);
375
376
return OK;
377
}
378
379
bool is_termresized(void)
380
{
381
PDC_LOG(("is_termresized() - called\n"));
382
383
return SP->resized;
384
}
385
386
const char *curses_version(void)
387
{
388
return _curses_notice;
389
}
390
391
void PDC_get_version(PDC_VERSION *ver)
392
{
393
if (!ver)
394
return;
395
396
ver->flags = 0
397
#ifdef PDCDEBUG
398
| PDC_VFLAG_DEBUG
399
#endif
400
#ifdef PDC_WIDE
401
| PDC_VFLAG_WIDE
402
#endif
403
#ifdef PDC_FORCE_UTF8
404
| PDC_VFLAG_UTF8
405
#endif
406
#ifdef PDC_DLL_BUILD
407
| PDC_VFLAG_DLL
408
#endif
409
#ifdef PDC_RGB
410
| PDC_VFLAG_RGB
411
#endif
412
;
413
414
ver->build = PDC_BUILD;
415
ver->major = PDC_VER_MAJOR;
416
ver->minor = PDC_VER_MINOR;
417
ver->csize = sizeof(chtype);
418
ver->bsize = sizeof(bool);
419
}
420
421
int set_tabsize(int tabsize)
422
{
423
PDC_LOG(("set_tabsize() - called: tabsize %d\n", tabsize));
424
425
if (tabsize < 1)
426
return ERR;
427
428
TABSIZE = tabsize;
429
430
return OK;
431
}
432
433