diff options
Diffstat (limited to 'scripts/kconfig/libcurses/initscr.c')
| -rw-r--r-- | scripts/kconfig/libcurses/initscr.c | 433 |
1 files changed, 433 insertions, 0 deletions
diff --git a/scripts/kconfig/libcurses/initscr.c b/scripts/kconfig/libcurses/initscr.c new file mode 100644 index 000000000..a4208642a --- /dev/null +++ b/scripts/kconfig/libcurses/initscr.c | |||
| @@ -0,0 +1,433 @@ | |||
| 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 | |||
| 87 | Function | X/Open | ncurses | NetBSD | ||
| 88 | :---------------------|:------:|:-------:|:------: | ||
| 89 | initscr | Y | Y | Y | ||
| 90 | endwin | Y | Y | Y | ||
| 91 | isendwin | Y | Y | Y | ||
| 92 | newterm | Y | Y | Y | ||
| 93 | set_term | Y | Y | Y | ||
| 94 | delscreen | Y | Y | Y | ||
| 95 | resize_term | - | Y | Y | ||
| 96 | set_tabsize | - | Y | Y | ||
| 97 | curses_version | - | Y | - | ||
| 98 | is_termresized | - | - | - | ||
| 99 | |||
| 100 | **man-end****************************************************************/ | ||
| 101 | |||
| 102 | #include <stdlib.h> | ||
| 103 | |||
| 104 | char ttytype[128]; | ||
| 105 | |||
| 106 | const char *_curses_notice = "PDCurses " PDC_VERDOT " - " __DATE__; | ||
| 107 | |||
| 108 | SCREEN *SP = (SCREEN*)NULL; /* curses variables */ | ||
| 109 | WINDOW *curscr = (WINDOW *)NULL; /* the current screen image */ | ||
| 110 | WINDOW *stdscr = (WINDOW *)NULL; /* the default screen window */ | ||
| 111 | |||
| 112 | int LINES = 0; /* current terminal height */ | ||
| 113 | int COLS = 0; /* current terminal width */ | ||
| 114 | int TABSIZE = 8; | ||
| 115 | |||
| 116 | MOUSE_STATUS Mouse_status; | ||
| 117 | |||
| 118 | extern RIPPEDOFFLINE linesripped[5]; | ||
| 119 | extern char linesrippedoff; | ||
| 120 | |||
| 121 | WINDOW *initscr(void) | ||
| 122 | { | ||
| 123 | int i; | ||
| 124 | |||
| 125 | PDC_LOG(("initscr() - called\n")); | ||
| 126 | |||
| 127 | if (SP && SP->alive) | ||
| 128 | return NULL; | ||
| 129 | |||
| 130 | SP = calloc(1, sizeof(SCREEN)); | ||
| 131 | if (!SP) | ||
| 132 | return NULL; | ||
| 133 | |||
| 134 | if (PDC_scr_open() == ERR) | ||
| 135 | { | ||
| 136 | fprintf(stderr, "initscr(): Unable to create SP\n"); | ||
| 137 | exit(8); | ||
| 138 | } | ||
| 139 | |||
| 140 | SP->autocr = TRUE; /* cr -> lf by default */ | ||
| 141 | SP->raw_out = FALSE; /* tty I/O modes */ | ||
| 142 | SP->raw_inp = FALSE; /* tty I/O modes */ | ||
| 143 | SP->cbreak = TRUE; | ||
| 144 | SP->key_modifiers = 0L; | ||
| 145 | SP->return_key_modifiers = FALSE; | ||
| 146 | SP->echo = TRUE; | ||
| 147 | SP->visibility = 1; | ||
| 148 | SP->resized = FALSE; | ||
| 149 | SP->_trap_mbe = 0L; | ||
| 150 | SP->linesrippedoff = 0; | ||
| 151 | SP->linesrippedoffontop = 0; | ||
| 152 | SP->delaytenths = 0; | ||
| 153 | SP->line_color = -1; | ||
| 154 | SP->lastscr = (WINDOW *)NULL; | ||
| 155 | SP->dbfp = NULL; | ||
| 156 | SP->color_started = FALSE; | ||
| 157 | SP->dirty = FALSE; | ||
| 158 | SP->sel_start = -1; | ||
| 159 | SP->sel_end = -1; | ||
| 160 | |||
| 161 | SP->orig_cursor = PDC_get_cursor_mode(); | ||
| 162 | |||
| 163 | LINES = SP->lines = PDC_get_rows(); | ||
| 164 | COLS = SP->cols = PDC_get_columns(); | ||
| 165 | |||
| 166 | if (LINES < 2 || COLS < 2) | ||
| 167 | { | ||
| 168 | fprintf(stderr, "initscr(): LINES=%d COLS=%d: too small.\n", | ||
| 169 | LINES, COLS); | ||
| 170 | exit(4); | ||
| 171 | } | ||
| 172 | |||
| 173 | curscr = newwin(LINES, COLS, 0, 0); | ||
| 174 | if (!curscr) | ||
| 175 | { | ||
| 176 | fprintf(stderr, "initscr(): Unable to create curscr.\n"); | ||
| 177 | exit(2); | ||
| 178 | } | ||
| 179 | |||
| 180 | SP->lastscr = newwin(LINES, COLS, 0, 0); | ||
| 181 | if (!SP->lastscr) | ||
| 182 | { | ||
| 183 | fprintf(stderr, "initscr(): Unable to create SP->lastscr.\n"); | ||
| 184 | exit(2); | ||
| 185 | } | ||
| 186 | |||
| 187 | wattrset(SP->lastscr, (chtype)(-1)); | ||
| 188 | werase(SP->lastscr); | ||
| 189 | |||
| 190 | PDC_slk_initialize(); | ||
| 191 | LINES -= SP->slklines; | ||
| 192 | |||
| 193 | /* We have to sort out ripped off lines here, and reduce the height | ||
| 194 | of stdscr by the number of lines ripped off */ | ||
| 195 | |||
| 196 | for (i = 0; i < linesrippedoff; i++) | ||
| 197 | { | ||
| 198 | if (linesripped[i].line < 0) | ||
| 199 | (*linesripped[i].init)(newwin(1, COLS, LINES - 1, 0), COLS); | ||
| 200 | else | ||
| 201 | (*linesripped[i].init)(newwin(1, COLS, | ||
| 202 | SP->linesrippedoffontop++, 0), COLS); | ||
| 203 | |||
| 204 | SP->linesrippedoff++; | ||
| 205 | LINES--; | ||
| 206 | } | ||
| 207 | |||
| 208 | linesrippedoff = 0; | ||
| 209 | |||
| 210 | stdscr = newwin(LINES, COLS, SP->linesrippedoffontop, 0); | ||
| 211 | if (!stdscr) | ||
| 212 | { | ||
| 213 | fprintf(stderr, "initscr(): Unable to create stdscr.\n"); | ||
| 214 | exit(1); | ||
| 215 | } | ||
| 216 | |||
| 217 | wclrtobot(stdscr); | ||
| 218 | |||
| 219 | /* If preserving the existing screen, don't allow a screen clear */ | ||
| 220 | |||
| 221 | if (SP->_preserve) | ||
| 222 | { | ||
| 223 | untouchwin(curscr); | ||
| 224 | untouchwin(stdscr); | ||
| 225 | stdscr->_clear = FALSE; | ||
| 226 | curscr->_clear = FALSE; | ||
| 227 | } | ||
| 228 | else | ||
| 229 | curscr->_clear = TRUE; | ||
| 230 | |||
| 231 | SP->atrtab = calloc(PDC_COLOR_PAIRS, sizeof(PDC_PAIR)); | ||
| 232 | if (!SP->atrtab) | ||
| 233 | return NULL; | ||
| 234 | PDC_init_atrtab(); /* set up default colors */ | ||
| 235 | |||
| 236 | MOUSE_X_POS = MOUSE_Y_POS = -1; | ||
| 237 | BUTTON_STATUS(1) = BUTTON_RELEASED; | ||
| 238 | BUTTON_STATUS(2) = BUTTON_RELEASED; | ||
| 239 | BUTTON_STATUS(3) = BUTTON_RELEASED; | ||
| 240 | Mouse_status.changes = 0; | ||
| 241 | |||
| 242 | SP->alive = TRUE; | ||
| 243 | |||
| 244 | def_shell_mode(); | ||
| 245 | |||
| 246 | sprintf(ttytype, "pdcurses|PDCurses for %s", PDC_sysname()); | ||
| 247 | |||
| 248 | SP->c_buffer = malloc(_INBUFSIZ * sizeof(int)); | ||
| 249 | if (!SP->c_buffer) | ||
| 250 | return NULL; | ||
| 251 | SP->c_pindex = 0; | ||
| 252 | SP->c_gindex = 1; | ||
| 253 | |||
| 254 | SP->c_ungch = malloc(NUNGETCH * sizeof(int)); | ||
| 255 | if (!SP->c_ungch) | ||
| 256 | return NULL; | ||
| 257 | SP->c_ungind = 0; | ||
| 258 | SP->c_ungmax = NUNGETCH; | ||
| 259 | |||
| 260 | return stdscr; | ||
| 261 | } | ||
| 262 | |||
| 263 | #ifdef XCURSES | ||
| 264 | WINDOW *Xinitscr(int argc, char **argv) | ||
| 265 | { | ||
| 266 | PDC_LOG(("Xinitscr() - called\n")); | ||
| 267 | |||
| 268 | PDC_set_args(argc, argv); | ||
| 269 | return initscr(); | ||
| 270 | } | ||
| 271 | #endif | ||
| 272 | |||
| 273 | int endwin(void) | ||
| 274 | { | ||
| 275 | PDC_LOG(("endwin() - called\n")); | ||
| 276 | |||
| 277 | /* Allow temporary exit from curses using endwin() */ | ||
| 278 | |||
| 279 | def_prog_mode(); | ||
| 280 | PDC_scr_close(); | ||
| 281 | |||
| 282 | SP->alive = FALSE; | ||
| 283 | |||
| 284 | return OK; | ||
| 285 | } | ||
| 286 | |||
| 287 | bool isendwin(void) | ||
| 288 | { | ||
| 289 | PDC_LOG(("isendwin() - called\n")); | ||
| 290 | |||
| 291 | return SP ? !(SP->alive) : FALSE; | ||
| 292 | } | ||
| 293 | |||
| 294 | SCREEN *newterm(const char *type, FILE *outfd, FILE *infd) | ||
| 295 | { | ||
| 296 | PDC_LOG(("newterm() - called\n")); | ||
| 297 | |||
| 298 | return initscr() ? SP : NULL; | ||
| 299 | } | ||
| 300 | |||
| 301 | SCREEN *set_term(SCREEN *new) | ||
| 302 | { | ||
| 303 | PDC_LOG(("set_term() - called\n")); | ||
| 304 | |||
| 305 | /* We only support one screen */ | ||
| 306 | |||
| 307 | return (new == SP) ? SP : NULL; | ||
| 308 | } | ||
| 309 | |||
| 310 | void delscreen(SCREEN *sp) | ||
| 311 | { | ||
| 312 | PDC_LOG(("delscreen() - called\n")); | ||
| 313 | |||
| 314 | if (!SP || sp != SP) | ||
| 315 | return; | ||
| 316 | |||
| 317 | free(SP->c_ungch); | ||
| 318 | free(SP->c_buffer); | ||
| 319 | free(SP->atrtab); | ||
| 320 | |||
| 321 | PDC_slk_free(); /* free the soft label keys, if needed */ | ||
| 322 | |||
| 323 | delwin(stdscr); | ||
| 324 | delwin(curscr); | ||
| 325 | delwin(SP->lastscr); | ||
| 326 | stdscr = (WINDOW *)NULL; | ||
| 327 | curscr = (WINDOW *)NULL; | ||
| 328 | SP->lastscr = (WINDOW *)NULL; | ||
| 329 | |||
| 330 | SP->alive = FALSE; | ||
| 331 | |||
| 332 | PDC_scr_free(); | ||
| 333 | |||
| 334 | free(SP); | ||
| 335 | SP = (SCREEN *)NULL; | ||
| 336 | } | ||
| 337 | |||
| 338 | int resize_term(int nlines, int ncols) | ||
| 339 | { | ||
| 340 | PDC_LOG(("resize_term() - called: nlines %d\n", nlines)); | ||
| 341 | |||
| 342 | if (!stdscr || PDC_resize_screen(nlines, ncols) == ERR) | ||
| 343 | return ERR; | ||
| 344 | |||
| 345 | SP->resized = FALSE; | ||
| 346 | |||
| 347 | SP->lines = PDC_get_rows(); | ||
| 348 | LINES = SP->lines - SP->linesrippedoff - SP->slklines; | ||
| 349 | SP->cols = COLS = PDC_get_columns(); | ||
| 350 | |||
| 351 | if (SP->cursrow >= SP->lines) | ||
| 352 | SP->cursrow = SP->lines - 1; | ||
| 353 | if (SP->curscol >= SP->cols) | ||
| 354 | SP->curscol = SP->cols - 1; | ||
| 355 | |||
| 356 | if (wresize(curscr, SP->lines, SP->cols) == ERR || | ||
| 357 | wresize(stdscr, LINES, COLS) == ERR || | ||
| 358 | wresize(SP->lastscr, SP->lines, SP->cols) == ERR) | ||
| 359 | return ERR; | ||
| 360 | |||
| 361 | werase(SP->lastscr); | ||
| 362 | curscr->_clear = TRUE; | ||
| 363 | |||
| 364 | if (SP->slk_winptr) | ||
| 365 | { | ||
| 366 | if (wresize(SP->slk_winptr, SP->slklines, COLS) == ERR) | ||
| 367 | return ERR; | ||
| 368 | |||
| 369 | wmove(SP->slk_winptr, 0, 0); | ||
| 370 | wclrtobot(SP->slk_winptr); | ||
| 371 | PDC_slk_initialize(); | ||
| 372 | slk_noutrefresh(); | ||
| 373 | } | ||
| 374 | |||
| 375 | touchwin(stdscr); | ||
| 376 | wnoutrefresh(stdscr); | ||
| 377 | |||
| 378 | return OK; | ||
| 379 | } | ||
| 380 | |||
| 381 | bool is_termresized(void) | ||
| 382 | { | ||
| 383 | PDC_LOG(("is_termresized() - called\n")); | ||
| 384 | |||
| 385 | return SP->resized; | ||
| 386 | } | ||
| 387 | |||
| 388 | const char *curses_version(void) | ||
| 389 | { | ||
| 390 | return _curses_notice; | ||
| 391 | } | ||
| 392 | |||
| 393 | void PDC_get_version(PDC_VERSION *ver) | ||
| 394 | { | ||
| 395 | if (!ver) | ||
| 396 | return; | ||
| 397 | |||
| 398 | ver->flags = 0 | ||
| 399 | #ifdef PDCDEBUG | ||
| 400 | | PDC_VFLAG_DEBUG | ||
| 401 | #endif | ||
| 402 | #ifdef PDC_WIDE | ||
| 403 | | PDC_VFLAG_WIDE | ||
| 404 | #endif | ||
| 405 | #ifdef PDC_FORCE_UTF8 | ||
| 406 | | PDC_VFLAG_UTF8 | ||
| 407 | #endif | ||
| 408 | #ifdef PDC_DLL_BUILD | ||
| 409 | | PDC_VFLAG_DLL | ||
| 410 | #endif | ||
| 411 | #ifdef PDC_RGB | ||
| 412 | | PDC_VFLAG_RGB | ||
| 413 | #endif | ||
| 414 | ; | ||
| 415 | |||
| 416 | ver->build = PDC_BUILD; | ||
| 417 | ver->major = PDC_VER_MAJOR; | ||
| 418 | ver->minor = PDC_VER_MINOR; | ||
| 419 | ver->csize = sizeof(chtype); | ||
| 420 | ver->bsize = sizeof(bool); | ||
| 421 | } | ||
| 422 | |||
| 423 | int set_tabsize(int tabsize) | ||
| 424 | { | ||
| 425 | PDC_LOG(("set_tabsize() - called: tabsize %d\n", tabsize)); | ||
| 426 | |||
| 427 | if (tabsize < 1) | ||
| 428 | return ERR; | ||
| 429 | |||
| 430 | TABSIZE = tabsize; | ||
| 431 | |||
| 432 | return OK; | ||
| 433 | } | ||
