void gnt_init() { char *filename; const char *locale; if (channel) return; locale = setlocale(LC_ALL, ""); setup_io(); #ifdef NO_WIDECHAR ascii_only = TRUE; #else if (locale && (strstr(locale, "UTF") || strstr(locale, "utf"))) ascii_only = FALSE; else ascii_only = TRUE; #endif initscr(); typeahead(-1); noecho(); curs_set(0); gnt_init_keys(); gnt_init_styles(); filename = g_build_filename(g_get_home_dir(), ".gntrc", NULL); gnt_style_read_configure_file(filename); g_free(filename); gnt_init_colors(); wbkgdset(stdscr, '\0' | gnt_color_pair(GNT_COLOR_NORMAL)); refresh(); #ifdef ALL_MOUSE_EVENTS if ((mouse_enabled = gnt_style_get_bool(GNT_STYLE_MOUSE, FALSE))) mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL); #endif wbkgdset(stdscr, '\0' | gnt_color_pair(GNT_COLOR_NORMAL)); werase(stdscr); wrefresh(stdscr); #ifdef SIGWINCH org_winch_handler = signal(SIGWINCH, sighandler); #endif signal(SIGCHLD, sighandler); signal(SIGINT, sighandler); signal(SIGPIPE, SIG_IGN); g_type_init(); init_wm(); clipboard = g_object_new(GNT_TYPE_CLIPBOARD, NULL); }
/* * Setup and run the interactive portion of the program. */ void screen_start(void) { if (initscr() == 0) /* should return a "WINDOW *" */ exit(EXIT_FAILURE); #if HAVE_KEYPAD keypad(stdscr, TRUE); #endif #if HAVE_DEFINE_KEY define_key("\033Ok", '+'); define_key("\033Ol", ','); define_key("\033Om", '-'); define_key("\033On", '.'); define_key("\033Op", '0'); define_key("\033Oq", '1'); define_key("\033Or", '2'); define_key("\033Os", '3'); define_key("\033Ot", '4'); define_key("\033Ou", '5'); define_key("\033Ov", '6'); define_key("\033Ow", '7'); define_key("\033Ox", '8'); define_key("\033Oy", '9'); define_key("\033OM", '\n'); #endif #if defined(COLOR_BLUE) && defined(COLOR_WHITE) && HAVE_COLOR_PAIR if (has_colors()) { start_color(); init_pair(1, COLOR_WHITE, COLOR_BLUE); /* normal */ SetColors(1); #if HAVE_BKGD bkgd(CURRENT_COLOR); #endif } #endif #if HAVE_TYPEAHEAD typeahead(-1); /* disable typeahead */ #endif #if SYS_MSDOS && defined(F_GRAY) && defined(B_BLUE) wattrset(stdscr, F_GRAY | B_BLUE); /* patch for old PD-Curses */ #endif raw(); nonl(); noecho(); set_screensize(); }
static void start_curses(void) { keymap_init(); mutt_signal_init(); if (initscr() == NULL) { puts ("Error initializing terminal."); exit(RETURN_ERR_TERM); } ci_start_color(); keypad(stdscr, TRUE); cbreak(); noecho(); typeahead(-1); /* simulate smooth scrolling */ meta(stdscr, TRUE); init_extended_keys(); }
EIF_INTEGER c_ecurses_typeahead(EIF_INTEGER fd) { return typeahead ((int) fd); };
newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp) { int value; int errret; SCREEN *current; SCREEN *result = 0; TERMINAL *its_term; START_TRACE(); T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp)); _nc_init_pthreads(); _nc_lock_global(curses); current = SP; its_term = (SP ? SP->_term : 0); /* this loads the capability entry, then sets LINES and COLS */ if (setupterm(name, fileno(ofp), &errret) != ERR) { int slk_format = _nc_globals.slk_format; /* * This actually allocates the screen structure, and saves the original * terminal settings. */ _nc_set_screen(0); /* allow user to set maximum escape delay from the environment */ if ((value = _nc_getenv_num("ESCDELAY")) >= 0) { set_escdelay(value); } if (_nc_setupscreen(LINES, COLS, ofp, _nc_prescreen.filter_mode, slk_format) == ERR) { _nc_set_screen(current); result = 0; } else { assert(SP != 0); /* * In setupterm() we did a set_curterm(), but it was before we set * SP. So the "current" screen's terminal pointer was overwritten * with a different terminal. Later, in _nc_setupscreen(), we set * SP and the terminal pointer in the new screen. * * Restore the terminal-pointer for the pre-existing screen, if * any. */ if (current) current->_term = its_term; /* if the terminal type has real soft labels, set those up */ if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format)) _nc_slk_initialize(stdscr, COLS); SP->_ifd = fileno(ifp); typeahead(fileno(ifp)); #ifdef TERMIOS SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 && !(cur_term->Ottyb.c_iflag & ISTRIP)); #else SP->_use_meta = FALSE; #endif SP->_endwin = FALSE; /* * Check whether we can optimize scrolling under dumb terminals in * case we do not have any of these capabilities, scrolling * optimization will be useless. */ SP->_scrolling = ((scroll_forward && scroll_reverse) || ((parm_rindex || parm_insert_line || insert_line) && (parm_index || parm_delete_line || delete_line))); baudrate(); /* sets a field in the SP structure */ SP->_keytry = 0; /* * Check for mismatched graphic-rendition capabilities. Most SVr4 * terminfo trees contain entries that have rmul or rmso equated to * sgr0 (Solaris curses copes with those entries). We do this only * for curses, since many termcap applications assume that * smso/rmso and smul/rmul are paired, and will not function * properly if we remove rmso or rmul. Curses applications * shouldn't be looking at this detail. */ #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode)) SP->_use_rmso = SGR0_TEST(exit_standout_mode); SP->_use_rmul = SGR0_TEST(exit_underline_mode); /* compute movement costs so we can do better move optimization */ _nc_mvcur_init(); /* initialize terminal to a sane state */ _nc_screen_init(); /* Initialize the terminal line settings. */ _nc_initscr(); _nc_signal_handler(TRUE); result = SP; } } _nc_unlock_global(curses); returnSP(result); }
/* * Create a new terminal screen. Used if a program is going to be sending * output to more than one terminal. It returns a SCREEN* for the terminal. * The parameters are a terminal name, output FILE*, and input FILE*. If * the terminal name is null then $TERM is used. The program must also * call endwin() for each terminal being used before exiting from curses. * If newterm() is called more than once for the same terminal, the first * terminal referred to must be the last one for which endwin() is called. */ SCREEN * newterm(char *term, FILE *out_fp, FILE *in_fp) { WINDOW *w; t_wide_io *wio; SCREEN *sp, *osp; int i, n, y, errret; /* * Input stream should be unbuffered so that m_tfgetc() works * correctly on BSD and SUN systems. */ (void) setvbuf(in_fp, (char *) 0, _IONBF, BUFSIZ); #if 0 /* * Not sure whether we really want to concern ourselves with the output * buffer scheme. Might be best to leave it upto the application to * deal with buffer schemes and when to perform flushes. * * MKS Vi uses MKS Curses and so must support the ability to switch in * and out of Curses mode when switching from Vi to Ex and back. * Problem is that in Vi mode you would prefer full buffered output to * give updates a smoother appearance and Ex mode you require line * buffered in order to see prompts and messages. */ (void) setvbuf(out_fp, (char *) 0, _IOLBF, BUFSIZ); #endif errno = 0; if (__m_setupterm(term, fileno(in_fp), fileno(out_fp), &errret) == ERR) { switch (errret) { case -1: errno = ENOMEM; break; case 2: errno = ENAMETOOLONG; break; case 0: default: errno = ENOENT; break; } goto error1; } if (__m_doupdate_init()) goto error1; if ((sp = (SCREEN *) calloc(1, sizeof (*sp))) == NULL) goto error1; sp->_kfd = -1; sp->_if = in_fp; sp->_of = out_fp; sp->_term = cur_term; sp->_unget._size = __m_decode_init((t_decode **) &sp->_decode); /* * Maximum length of a multbyte key sequence, including * multibyte characters and terminal function keys. */ if (sp->_unget._size < (M_TYPEAHEAD_SIZE + MB_LEN_MAX)) sp->_unget._size = M_TYPEAHEAD_SIZE + MB_LEN_MAX; sp->_unget._stack = calloc((size_t) sp->_unget._size, sizeof (*sp->_unget._stack)); if (sp->_unget._stack == NULL) goto error2; if ((wio = (t_wide_io *) calloc(1, sizeof (*wio))) == NULL) goto error2; /* Setup wide input for XCurses. */ wio->get = (int (*)(void *)) wgetch; wio->unget = __xc_ungetc; wio->reset = __xc_clearerr; wio->iserror = __xc_ferror; wio->iseof = __xc_feof; sp->_in = wio; if (assume_one_line) { /* Assume only one line. */ lines = 1; /* Disable capabilities that assume more than one line. */ clear_screen = clr_eos = cursor_up = cursor_down = NULL; cursor_home = cursor_to_ll = cursor_address = NULL; row_address = parm_up_cursor = parm_down_cursor = NULL; /* Re-evaluate the cursor motion costs. */ __m_mvcur_cost(); /* Reset flag for subsequent calls to newterm(). */ assume_one_line = FALSE; } if ((sp->_curscr = newwin(lines, columns, 0, 0)) == NULL) goto error2; if ((sp->_newscr = newwin(lines, columns, 0, 0)) == NULL) goto error2; #if defined(_LP64) sp->_hash = (unsigned int *) calloc(lines, sizeof (*sp->_hash)); #else sp->_hash = (unsigned long *) calloc(lines, sizeof (*sp->_hash)); #endif if (sp->_hash == NULL) goto error2; if (0 <= __m_slk_format && __m_slk_init(sp, __m_slk_format) == ERR) { goto error2; } /* * doupdate() will perform the final screen preparations like * enter_ca_mode, reset_prog_mode() (to assert the termios * changes), etc. */ sp->_flags |= S_ENDWIN; #ifdef SIGTSTP (void) signal(SIGTSTP, tstp); #endif /* Assert that __m_screen is set to the new terminal. */ osp = set_term(sp); /* Disable echo in tty driver, Curses does software echo. */ PTERMIOS(_prog)->c_lflag &= ~ECHO; /* Enable mappnig of cr -> nl on input and nl -> crlf on output. */ PTERMIOS(_prog)->c_iflag |= ICRNL; PTERMIOS(_prog)->c_oflag |= OPOST; #ifdef ONLCR PTERMIOS(_prog)->c_oflag |= ONLCR; #endif cur_term->_flags |= __TERM_NL_IS_CRLF; #ifdef TAB0 /* Use real tabs. */ PTERMIOS(_prog)->c_oflag &= ~(TAB1|TAB2|TAB3); #endif /* * Default to 'cbreak' mode as per * test /tset/CAPIxcurses/fcbreak/fcbreak1{4} */ cur_term->_flags &= ~__TERM_HALF_DELAY; /* * Default to 'idcok' mode as per * test /tset/CAPIxcurses/fidcok/fidcok1{3} */ __m_screen->_flags |= S_INS_DEL_CHAR; PTERMIOS(_prog)->c_cc[VMIN] = 1; PTERMIOS(_prog)->c_cc[VTIME] = 0; PTERMIOS(_prog)->c_lflag &= ~ICANON; (void) __m_tty_set_prog_mode(); (void) __m_set_echo(1); (void) typeahead(fileno(in_fp)); (void) __m_slk_clear(1); n = rip.top - rip.bottom; if (stdscr == NULL) { stdscr = newwin(lines - n, 0, rip.top, 0); if (stdscr == NULL) goto error3; } /* * Create and initialise ripped off line windows. * It is the application's responsiblity to free the * windows when the application terminates. */ for (i = 0; i < n; ++i) { if (rip.line[i].created) continue; y = rip.line[i].dy; if (y < 0) y += lines; w = newwin(1, 0, y, 0); if (rip.line[i].init != (int (*)(WINDOW *, int)) 0) (void) (*rip.line[i].init)(w, columns); rip.line[i].created = 1; } LINES = stdscr->_maxy = sp->_curscr->_maxy - n; return (sp); error3: (void) set_term(osp); error2: delscreen(sp); error1: return (NULL); }
static void inputTest(WINDOW *win) { int answered; int repeat; int w, h, bx, by, sw, sh, i, c, num; char buffer[80]; WINDOW *subWin; wclear(win); getmaxyx(win, h, w); getbegyx(win, by, bx); sw = w / 3; sh = h / 3; if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == NULL) return; #ifdef A_COLOR if (has_colors()) { init_pair(2, COLOR_WHITE, COLOR_RED); wbkgd(subWin, (chtype) COLOR_PAIR(2) | A_BOLD); } else wbkgd(subWin, A_BOLD); #else wbkgd(subWin, A_BOLD); #endif box(subWin, ACS_VLINE, ACS_HLINE); wrefresh(win); nocbreak(); MvWAddStr(win, 2, 1, "Press some keys for 5 seconds"); MvWAddStr(win, 1, 1, "Pressing ^C should do nothing"); wrefresh(win); werase(subWin); box(subWin, ACS_VLINE, ACS_HLINE); for (i = 0; i < 5; i++) { MvWPrintw(subWin, 1, 1, "Time = %d", i); wrefresh(subWin); napms(1000); flushinp(); } delwin(subWin); werase(win); flash(); wrefresh(win); napms(500); MvWAddStr(win, 2, 1, "Press a key, followed by ENTER"); wmove(win, 9, 10); wrefresh(win); echo(); noraw(); wgetch(win); flushinp(); wmove(win, 9, 10); wdelch(win); MvWAddStr(win, 4, 1, "The character should now have been deleted"); Continue(win); wclear(win); MvWAddStr(win, 1, 1, "Press keys (or mouse buttons) to show their names"); MvWAddStr(win, 2, 1, "Press spacebar to finish"); wrefresh(win); keypad(win, TRUE); raw(); noecho(); #if HAVE_TYPEAHEAD typeahead(-1); #endif #ifdef NCURSES_MOUSE_VERSION mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0); #endif #if defined(PDCURSES) mouse_set(ALL_MOUSE_EVENTS); #endif for (;;) { wmove(win, 3, 5); c = wgetch(win); wclrtobot(win); if (c >= KEY_MIN) wprintw(win, "Key Pressed: %s", keyname(c)); else if (isprint(c)) wprintw(win, "Key Pressed: %c", c); else wprintw(win, "Key Pressed: %s", unctrl(UChar(c))); #ifdef KEY_MOUSE if (c == KEY_MOUSE) { #if defined(NCURSES_MOUSE_VERSION) #define ButtonChanged(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, 037)) #define ButtonPressed(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_PRESSED)) #define ButtonDouble(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_DOUBLE_CLICKED)) #define ButtonTriple(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_TRIPLE_CLICKED)) #define ButtonRelease(n) ((event.bstate) & NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_RELEASED)) MEVENT event; int button = 0; getmouse(&event); if (ButtonChanged(1)) button = 1; else if (ButtonChanged(2)) button = 2; else if (ButtonChanged(3)) button = 3; else button = 0; wmove(win, 4, 18); wprintw(win, "Button %d: ", button); if (ButtonPressed(button)) wprintw(win, "pressed: "); else if (ButtonDouble(button)) wprintw(win, "double: "); else if (ButtonTriple(button)) wprintw(win, "triple: "); else wprintw(win, "released: "); wprintw(win, " Position: Y: %d X: %d", event.y, event.x); #elif defined(PDCURSES) int button = 0; request_mouse_pos(); if (BUTTON_CHANGED(1)) button = 1; else if (BUTTON_CHANGED(2)) button = 2; else if (BUTTON_CHANGED(3)) button = 3; else button = 0; wmove(win, 4, 18); wprintw(win, "Button %d: ", button); if (MOUSE_MOVED) wprintw(win, "moved: "); else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_PRESSED) wprintw(win, "pressed: "); else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_DOUBLE_CLICKED) wprintw(win, "double: "); else wprintw(win, "released: "); wprintw(win, " Position: Y: %d X: %d", MOUSE_Y_POS, MOUSE_X_POS); #endif /* NCURSES_VERSION vs PDCURSES */ } #endif /* KEY_MOUSE */ wrefresh(win); if (c == ' ') break; } #if 0 nodelay(win, TRUE); wgetch(win); nodelay(win, FALSE); #endif #if defined(PDCURSES) mouse_set(0L); #endif refresh(); repeat = 0; do { static const char *fmt[] = { "%d %10s", "%d %[a-zA-Z]s", "%d %[][a-zA-Z]s", "%d %[^0-9]" }; char *format = strdup(fmt[(unsigned) repeat % SIZEOF(fmt)]); wclear(win); MvWAddStr(win, 3, 2, "The window should have moved"); MvWAddStr(win, 4, 2, "This text should have appeared without you pressing a key"); MvWPrintw(win, 6, 2, "Scanning with format \"%s\"", format); mvwin(win, 2 + 2 * (repeat % 4), 1 + 2 * (repeat % 4)); erase(); refresh(); wrefresh(win); echo(); noraw(); num = 0; *buffer = 0; answered = mvwscanw(win, 7, 6, format, &num, buffer); MvWPrintw(win, 8, 6, "String: %s Number: %d (%d values read)", buffer, num, answered); Continue(win); ++repeat; free(format); } while (answered > 0); }
SCREEN * newterm(NCURSES_CONST char *name, FILE * ofp, FILE * ifp) { int errret; int slk_format = _nc_slk_format; SCREEN *current; #ifdef TRACE int t = _nc_getenv_num("NCURSES_TRACE"); if (t >= 0) trace(t); #endif T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp)); /* this loads the capability entry, then sets LINES and COLS */ if (setupterm(name, fileno(ofp), &errret) == ERR) return 0; /* implement filter mode */ if (filter_mode) { LINES = 1; if (VALID_NUMERIC(init_tabs)) TABSIZE = init_tabs; else TABSIZE = 8; T(("TABSIZE = %d", TABSIZE)); clear_screen = 0; cursor_down = parm_down_cursor = 0; cursor_address = 0; cursor_up = parm_up_cursor = 0; row_address = 0; cursor_home = carriage_return; } /* If we must simulate soft labels, grab off the line to be used. We assume that we must simulate, if it is none of the standard formats (4-4 or 3-2-3) for which there may be some hardware support. */ if (num_labels <= 0 || !SLK_STDFMT(slk_format)) if (slk_format) { if (ERR == _nc_ripoffline(-SLK_LINES(slk_format), _nc_slk_initialize)) return 0; } /* this actually allocates the screen structure, and saves the * original terminal settings. */ current = SP; _nc_set_screen(0); if (_nc_setupscreen(LINES, COLS, ofp) == ERR) { _nc_set_screen(current); return 0; } /* if the terminal type has real soft labels, set those up */ if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format)) _nc_slk_initialize(stdscr, COLS); SP->_ifd = fileno(ifp); SP->_checkfd = fileno(ifp); typeahead(fileno(ifp)); #ifdef TERMIOS SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 && !(cur_term->Ottyb.c_iflag & ISTRIP)); #else SP->_use_meta = FALSE; #endif SP->_endwin = FALSE; /* Check whether we can optimize scrolling under dumb terminals in case * we do not have any of these capabilities, scrolling optimization * will be useless. */ SP->_scrolling = ((scroll_forward && scroll_reverse) || ((parm_rindex || parm_insert_line || insert_line) && (parm_index || parm_delete_line || delete_line))); baudrate(); /* sets a field in the SP structure */ SP->_keytry = 0; /* * Check for mismatched graphic-rendition capabilities. Most SVr4 * terminfo trees contain entries that have rmul or rmso equated to * sgr0 (Solaris curses copes with those entries). We do this only for * curses, since many termcap applications assume that smso/rmso and * smul/rmul are paired, and will not function properly if we remove * rmso or rmul. Curses applications shouldn't be looking at this * detail. */ #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode)) SP->_use_rmso = SGR0_TEST(exit_standout_mode); SP->_use_rmul = SGR0_TEST(exit_underline_mode); #if USE_WIDEC_SUPPORT /* * XFree86 xterm can be configured to support UTF-8 based on environment * variable settings. */ { char *s; s = getenv("LC_ALL"); if (s == NULL || *s == '\0') { s = getenv("LC_CTYPE"); if (s == NULL || *s == '\0') { s = getenv("LANG"); } } if (s != NULL && *s != '\0' && strstr(s, "UTF-8") != NULL) { SP->_outch = _nc_utf8_outch; } } #endif /* compute movement costs so we can do better move optimization */ _nc_mvcur_init(); /* initialize terminal to a sane state */ _nc_screen_init(); /* Initialize the terminal line settings. */ _nc_initscr(); _nc_signal_handler(TRUE); T((T_RETURN("%p"), SP)); return (SP); }
int chkr_typeahead(int filedes) { if (filedes != -1) fd_used_by_prog(filedes); return(typeahead(filedes)); }