int InitializeGraphics(int scroll_text_up) { int fno, i; char *screen_tty; struct winsize winsize; fno = fileno(stdout); if (!isatty(fno)) { fprintf(stderr, "stdout must be a tty\n"); return 0; } screen_tty = ttyname(fno); #ifdef HAVE_TERMIOS_H GET_TERMIOS(fno, &ttmode); ttmode.c_lflag &= ~ECHO; SET_TERMIOS(fno, &ttmode); #endif infd = fopen(screen_tty, "rw"); #ifdef HAVE_TERMIOS_H fno2 = fileno(infd); GET_TERMIOS(fno2, &ttmode); ttmode.c_lflag &= ~ECHO; SET_TERMIOS(fno2, &ttmode); #endif /* query rxvt to find if graphics are available */ fflush(stdout); printf("\033GQ"); fflush(stdout); while (1) { if ((fgets(line, LINESZ, infd) != NULL) && (sscanf(line, "\033G%d", &i) == 1)) { if (!i) { fprintf(stderr, "rxvt graphics not available\n"); CloseGraphics(); return 0; } break; } } if (scroll_text_up) { ioctl(fno, TIOCGWINSZ, &winsize); fflush(stdout); for (i = 0; i < winsize.ws_row; i++) putchar('\n'); fflush(stdout); } return i; }
void SLang_reset_tty (void) { SLsig_block_signals (); if (TTY_Inited == 0) { SLsig_unblock_signals (); return; } while ((-1 == SET_TERMIOS(SLang_TT_Read_FD, &Old_TTY)) && (errno == EINTR)) ; if (TTY_Open) { while ((-1 == close (SLang_TT_Read_FD)) && (errno == EINTR)) ; TTY_Open = 0; SLang_TT_Read_FD = -1; } TTY_Inited = 0; SLsig_unblock_signals (); }
void CloseGraphics(void) { DefaultRendition(); fflush(stdout); #ifdef HAVE_TERMIOS_H ttmode.c_lflag |= ECHO; SET_TERMIOS(fno2, &ttmode); #endif fclose(infd); }
void SLtty_set_suspend_state (int mode) { TTY_Termio_Type newtty; SLsig_block_signals (); if (TTY_Inited == 0) { SLsig_unblock_signals (); return; } while ((-1 == GET_TERMIOS (SLang_TT_Read_FD, &newtty)) && (errno == EINTR)) ; #ifndef HAVE_TERMIOS_H /* I do not know if all systems define the t_dsuspc field */ if (mode == 0) { newtty.lt.t_suspc = 255; newtty.lt.t_dsuspc = 255; } else { newtty.lt.t_suspc = Old_TTY.lt.t_suspc; newtty.lt.t_dsuspc = Old_TTY.lt.t_dsuspc; } #else if (mode == 0) { newtty.c_cc[VSUSP] = NULL_VALUE; #ifdef VDSUSP newtty.c_cc[VDSUSP] = NULL_VALUE; #endif } else { newtty.c_cc[VSUSP] = Old_TTY.c_cc[VSUSP]; #ifdef VDSUSP newtty.c_cc[VDSUSP] = Old_TTY.c_cc[VDSUSP]; #endif } #endif while ((-1 == SET_TERMIOS (SLang_TT_Read_FD, &newtty)) && (errno == EINTR)) ; SLsig_unblock_signals (); }
int SLang_init_tty (int abort_char, int no_flow_control, int opost) { TTY_Termio_Type newtty; SLsig_block_signals (); if (TTY_Inited) { SLsig_unblock_signals (); return 0; } TTY_Open = 0; SLKeyBoard_Quit = 0; if ((SLang_TT_Read_FD == -1) || (1 != isatty (SLang_TT_Read_FD))) { #ifdef O_RDWR # if !defined(__BEOS__) && !defined(__APPLE__) /* I have been told that BEOS will HANG if passed /dev/tty */ if ((SLang_TT_Read_FD = open("/dev/tty", O_RDWR)) >= 0) { # ifdef FD_CLOEXEC /* Make sure /dev/tty is closed upon exec */ int flags = fcntl (SLang_TT_Read_FD, F_GETFD); if (flags >= 0) (void) fcntl(SLang_TT_Read_FD, F_SETFD, flags | FD_CLOEXEC); # endif TTY_Open = 1; } # endif #endif if (TTY_Open == 0) { SLang_TT_Read_FD = fileno (stderr); if (1 != isatty (SLang_TT_Read_FD)) { SLang_TT_Read_FD = fileno (stdin); if (1 != isatty (SLang_TT_Read_FD)) { fprintf (stderr, "Failed to open terminal."); return -1; } } } } SLang_Abort_Char = abort_char; /* Some systems may not permit signals to be blocked. As a result, the * return code must be checked. */ while (-1 == GET_TERMIOS(SLang_TT_Read_FD, &Old_TTY)) { if (errno != EINTR) { SLsig_unblock_signals (); return -1; } } while (-1 == GET_TERMIOS(SLang_TT_Read_FD, &newtty)) { if (errno != EINTR) { SLsig_unblock_signals (); return -1; } } #ifndef HAVE_TERMIOS_H (void) opost; (void) no_flow_control; newtty.s.sg_flags &= ~(ECHO); newtty.s.sg_flags &= ~(CRMOD); /* if (Flow_Control == 0) newtty.s.sg_flags &= ~IXON; */ newtty.t.t_eofc = 1; if (abort_char == -1) SLang_Abort_Char = newtty.t.t_intrc; newtty.t.t_intrc = SLang_Abort_Char; /* ^G */ newtty.t.t_quitc = 255; newtty.lt.t_suspc = 255; /* to ignore ^Z */ newtty.lt.t_dsuspc = 255; /* to ignore ^Y */ newtty.lt.t_lnextc = 255; newtty.s.sg_flags |= CBREAK; /* do I want cbreak or raw????? */ #else /* get baud rate */ newtty.c_iflag &= ~(ECHO | INLCR | ICRNL); #ifdef ISTRIP /* newtty.c_iflag &= ~ISTRIP; */ #endif if (opost == 0) newtty.c_oflag &= ~OPOST; set_baud_rate (&newtty); if (no_flow_control) newtty.c_iflag &= ~IXON; else newtty.c_iflag |= IXON; newtty.c_cc[VEOF] = 1; newtty.c_cc[VMIN] = 1; newtty.c_cc[VTIME] = 0; newtty.c_lflag = ISIG | NOFLSH; if (abort_char == -1) SLang_Abort_Char = newtty.c_cc[VINTR]; newtty.c_cc[VINTR] = SLang_Abort_Char; /* ^G */ newtty.c_cc[VQUIT] = NULL_VALUE; newtty.c_cc[VSUSP] = NULL_VALUE; /* to ignore ^Z */ #ifdef VDSUSP newtty.c_cc[VDSUSP] = NULL_VALUE; /* to ignore ^Y */ #endif #ifdef VLNEXT newtty.c_cc[VLNEXT] = NULL_VALUE; /* to ignore ^V ? */ #endif #ifdef VSWTCH newtty.c_cc[VSWTCH] = NULL_VALUE; /* to ignore who knows what */ #endif #endif /* NOT HAVE_TERMIOS_H */ while (-1 == SET_TERMIOS(SLang_TT_Read_FD, &newtty)) { if (errno != EINTR) { SLsig_unblock_signals (); return -1; } } TTY_Inited = 1; SLsig_unblock_signals (); return 0; }