Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmpdcurses/pdcurses/kernel.c
3153 views
1
/* PDCurses */
2
3
#include <curspriv.h>
4
5
/*man-start**************************************************************
6
7
kernel
8
------
9
10
### Synopsis
11
12
int def_prog_mode(void);
13
int def_shell_mode(void);
14
int reset_prog_mode(void);
15
int reset_shell_mode(void);
16
int resetty(void);
17
int savetty(void);
18
int ripoffline(int line, int (*init)(WINDOW *, int));
19
int curs_set(int visibility);
20
int napms(int ms);
21
22
int draino(int ms);
23
int resetterm(void);
24
int fixterm(void);
25
int saveterm(void);
26
27
### Description
28
29
def_prog_mode() and def_shell_mode() save the current terminal modes
30
as the "program" (in curses) or "shell" (not in curses) state for use
31
by the reset_prog_mode() and reset_shell_mode() functions. This is
32
done automatically by initscr().
33
34
reset_prog_mode() and reset_shell_mode() restore the terminal to
35
"program" (in curses) or "shell" (not in curses) state. These are
36
done automatically by endwin() and doupdate() after an endwin(), so
37
they would normally not be called before these functions.
38
39
savetty() and resetty() save and restore the state of the terminal
40
modes. savetty() saves the current state in a buffer, and resetty()
41
restores the state to what it was at the last call to savetty().
42
43
curs_set() alters the appearance of the cursor. A visibility of 0
44
makes it disappear; 1 makes it appear "normal" (usually an underline)
45
and 2 makes it "highly visible" (usually a block).
46
47
ripoffline() reduces the size of stdscr by one line. If the "line"
48
parameter is positive, the line is removed from the top of the
49
screen; if negative, from the bottom. Up to 5 lines can be ripped off
50
stdscr by calling ripoffline() repeatedly. The function argument,
51
init, is called from within initscr() or newterm(), so ripoffline()
52
must be called before either of these functions. The init function
53
receives a pointer to a one-line WINDOW, and the width of the window.
54
Calling ripoffline() with a NULL init function pointer is an error.
55
56
napms() suspends the program for the specified number of
57
milliseconds. draino() is an archaic equivalent. Note that since
58
napms() attempts to give up a time slice and yield control back to
59
the OS, all times are approximate. (In DOS, the delay is actually
60
rounded down to 50ms (1/20th sec) intervals, with a minimum of one
61
interval; i.e., 1-99 will wait 50ms, 100-149 will wait 100ms, etc.)
62
0 returns immediately.
63
64
resetterm(), fixterm() and saveterm() are archaic equivalents for
65
reset_shell_mode(), reset_prog_mode() and def_prog_mode(),
66
respectively.
67
68
### Return Value
69
70
All functions return OK on success and ERR on error, except
71
curs_set(), which returns the previous visibility.
72
73
### Portability
74
X/Open ncurses NetBSD
75
def_prog_mode Y Y Y
76
def_shell_mode Y Y Y
77
reset_prog_mode Y Y Y
78
reset_shell_mode Y Y Y
79
resetty Y Y Y
80
savetty Y Y Y
81
ripoffline Y Y Y
82
curs_set Y Y Y
83
napms Y Y Y
84
fixterm - Y -
85
resetterm - Y -
86
saveterm - Y -
87
draino - - -
88
89
**man-end****************************************************************/
90
91
#include <string.h>
92
93
RIPPEDOFFLINE linesripped[5];
94
char linesrippedoff = 0;
95
96
static struct cttyset
97
{
98
bool been_set;
99
SCREEN saved;
100
} ctty[3];
101
102
enum { PDC_SH_TTY, PDC_PR_TTY, PDC_SAVE_TTY };
103
104
static void _save_mode(int i)
105
{
106
ctty[i].been_set = TRUE;
107
108
memcpy(&(ctty[i].saved), SP, sizeof(SCREEN));
109
110
PDC_save_screen_mode(i);
111
}
112
113
static int _restore_mode(int i)
114
{
115
if (ctty[i].been_set == TRUE)
116
{
117
memcpy(SP, &(ctty[i].saved), sizeof(SCREEN));
118
119
if (ctty[i].saved.raw_out)
120
raw();
121
122
PDC_restore_screen_mode(i);
123
124
if ((LINES != ctty[i].saved.lines) ||
125
(COLS != ctty[i].saved.cols))
126
resize_term(ctty[i].saved.lines, ctty[i].saved.cols);
127
128
PDC_curs_set(ctty[i].saved.visibility);
129
130
PDC_gotoyx(ctty[i].saved.cursrow, ctty[i].saved.curscol);
131
}
132
133
return ctty[i].been_set ? OK : ERR;
134
}
135
136
int def_prog_mode(void)
137
{
138
PDC_LOG(("def_prog_mode() - called\n"));
139
140
if (!SP)
141
return ERR;
142
143
_save_mode(PDC_PR_TTY);
144
145
return OK;
146
}
147
148
int def_shell_mode(void)
149
{
150
PDC_LOG(("def_shell_mode() - called\n"));
151
152
if (!SP)
153
return ERR;
154
155
_save_mode(PDC_SH_TTY);
156
157
return OK;
158
}
159
160
int reset_prog_mode(void)
161
{
162
PDC_LOG(("reset_prog_mode() - called\n"));
163
164
if (!SP)
165
return ERR;
166
167
_restore_mode(PDC_PR_TTY);
168
PDC_reset_prog_mode();
169
170
return OK;
171
}
172
173
int reset_shell_mode(void)
174
{
175
PDC_LOG(("reset_shell_mode() - called\n"));
176
177
if (!SP)
178
return ERR;
179
180
_restore_mode(PDC_SH_TTY);
181
PDC_reset_shell_mode();
182
183
return OK;
184
}
185
186
int resetty(void)
187
{
188
PDC_LOG(("resetty() - called\n"));
189
190
if (!SP)
191
return ERR;
192
193
return _restore_mode(PDC_SAVE_TTY);
194
}
195
196
int savetty(void)
197
{
198
PDC_LOG(("savetty() - called\n"));
199
200
if (!SP)
201
return ERR;
202
203
_save_mode(PDC_SAVE_TTY);
204
205
return OK;
206
}
207
208
int curs_set(int visibility)
209
{
210
int ret_vis;
211
212
PDC_LOG(("curs_set() - called: visibility=%d\n", visibility));
213
214
if (!SP || visibility < 0 || visibility > 2)
215
return ERR;
216
217
ret_vis = PDC_curs_set(visibility);
218
219
/* If the cursor is changing from invisible to visible, update
220
its position */
221
222
if (visibility && !ret_vis)
223
PDC_gotoyx(SP->cursrow, SP->curscol);
224
225
return ret_vis;
226
}
227
228
int napms(int ms)
229
{
230
PDC_LOG(("napms() - called: ms=%d\n", ms));
231
232
if (!SP)
233
return ERR;
234
235
if (SP->dirty)
236
{
237
int curs_state = SP->visibility;
238
bool leave_state = is_leaveok(curscr);
239
240
SP->dirty = FALSE;
241
242
leaveok(curscr, TRUE);
243
244
wrefresh(curscr);
245
246
leaveok(curscr, leave_state);
247
curs_set(curs_state);
248
}
249
250
if (ms)
251
PDC_napms(ms);
252
253
return OK;
254
}
255
256
int ripoffline(int line, int (*init)(WINDOW *, int))
257
{
258
PDC_LOG(("ripoffline() - called: line=%d\n", line));
259
260
if (linesrippedoff < 5 && line && init)
261
{
262
linesripped[(int)linesrippedoff].line = line;
263
linesripped[(int)linesrippedoff++].init = init;
264
265
return OK;
266
}
267
268
return ERR;
269
}
270
271
int draino(int ms)
272
{
273
PDC_LOG(("draino() - called\n"));
274
275
return napms(ms);
276
}
277
278
int resetterm(void)
279
{
280
PDC_LOG(("resetterm() - called\n"));
281
282
return reset_shell_mode();
283
}
284
285
int fixterm(void)
286
{
287
PDC_LOG(("fixterm() - called\n"));
288
289
return reset_prog_mode();
290
}
291
292
int saveterm(void)
293
{
294
PDC_LOG(("saveterm() - called\n"));
295
296
return def_prog_mode();
297
}
298
299