--- rxvt-unicode/src/command.C 2003/11/25 11:52:42 1.3 +++ rxvt-unicode/src/command.C 2003/11/25 17:34:47 1.7 @@ -1,7 +1,7 @@ /*--------------------------------*-C-*---------------------------------* * File: command.c *----------------------------------------------------------------------* - * $Id: command.C,v 1.3 2003/11/25 11:52:42 pcg Exp $ + * $Id: command.C,v 1.7 2003/11/25 17:34:47 pcg Exp $ * * All portions of code are copyright by their respective author/s. * Copyright (c) 1992 John Bovey, University of Kent at Canterbury @@ -634,7 +634,7 @@ unsigned int n, s; unsigned char *cmdbuf_base = R->cmdbuf_base, *cmdbuf_endp = R->cmdbuf_endp, - *cmdbuf_ptr = R->cmdbuf_ptr; + *cmdbuf_ptr = R->cmdbuf_ptr; n = cmdbuf_ptr - cmdbuf_base; s = cmdbuf_base + BUFSIZ - 1 - cmdbuf_endp; @@ -665,30 +665,34 @@ XEvent xev; XNextEvent (Xdisplay, &xev); + #if defined(CURSOR_BLINK) if ((Options & Opt_cursorBlink) - && xev.type == KeyPress) { - if (hidden_cursor) { + && xev.type == KeyPress) + { + if (hidden_cursor) + { hidden_cursor = 0; want_refresh = 1; - } - want_keypress_time = 1; - } + } + + blink_ev.start (NOW + BLINK_INTERVAL); + } #endif #if defined(POINTER_BLANK) if ((Options & Opt_pointerBlank) - && (pointerBlankDelay > 0)) { + && (pointerBlankDelay > 0)) + { if (xev.type == MotionNotify || xev.type == ButtonPress - || xev.type == ButtonRelease) { - if (hidden_pointer) - rxvt_pointer_unblank(aR); - want_motion_time = 1; - } + || xev.type == ButtonRelease) + if (hidden_pointer) + pointer_unblank (); + if (xev.type == KeyPress && hidden_pointer == 0) - rxvt_pointer_blank (this); - } + pointer_blank (); + } #endif #ifdef USE_XIM @@ -700,27 +704,38 @@ } void +rxvt_term::blink_cb (time_watcher &w) +{ + w.at += BLINK_INTERVAL; + hidden_cursor = !hidden_cursor; + want_refresh = 1; + flush(); +} + +void rxvt_term::x_cb (io_watcher &w, short revents) { process_x_events (); + + flush (); } // read the next character, currently handles UTF-8 // will probably handle all sorts of other stuff in the future -static uint32_t -next_char (pR) +uint32_t +rxvt_term::next_char () { - mbstate &s = R->mbstate; + struct mbstate &s = mbstate; - while (R->cmdbuf_ptr < R->cmdbuf_endp) + while (cmdbuf_ptr < cmdbuf_endp) { - uint8_t ch = *R->cmdbuf_ptr; + uint8_t ch = *cmdbuf_ptr; if (s.cnt) { if ((ch & 0xc0) == 0x80) { - R->cmdbuf_ptr++; + cmdbuf_ptr++; /* continuation */ s.reg = (s.reg << 6) | (ch & 0x7f); @@ -739,7 +754,7 @@ if ((ch & 0xc0) == 0xc0) { - R->cmdbuf_ptr++; + cmdbuf_ptr++; /* first byte */ s.orig = ch; /* for broken encodings */ @@ -752,7 +767,7 @@ } else { - R->cmdbuf_ptr++; /* _occasional_ non-utf8 may slip through... */ + cmdbuf_ptr++; /* _occasional_ non-utf8 may slip through... */ return ch; } } @@ -760,40 +775,155 @@ return NOCHAR; } -void -rxvt_term::pty_cb (io_watcher &w, short revents) +bool +rxvt_term::pty_fill (size_t count) { - int n; - unsigned int count; + ssize_t n = cmdbuf_endp - cmdbuf_ptr; - if (count = (cmdbuf_endp - cmdbuf_ptr)) + memmove (cmdbuf_base, cmdbuf_ptr, n); + cmdbuf_ptr = cmdbuf_base; + cmdbuf_endp = cmdbuf_ptr + n; + + n = read (cmd_fd, cmdbuf_endp, count); + + if (n > 0) { - memmove (cmdbuf_base, cmdbuf_ptr, count); - cmdbuf_ptr = cmdbuf_base; - cmdbuf_endp = cmdbuf_ptr + count; + cmdbuf_endp += n; + return true; } - + else if (n < 0 && errno == EAGAIN) + return false; - for (count = BUFSIZ - count; count; count -= n, cmdbuf_endp += n) - if ((n = read(cmd_fd, cmdbuf_endp, count)) > 0) - continue; - else if (n == 0 || (n < 0 && errno == EAGAIN)) - break; - else + rxvt_clean_exit(); + exit(EXIT_FAILURE); /* bad order of events? */ +} + +void +rxvt_term::pty_cb (io_watcher &w, short revents) +{ + while (pty_fill (BUFSIZ - (cmdbuf_endp - cmdbuf_ptr))) + { + /* once we know the shell is running, send the screen size. Again! */ + //ch = rxvt_cmd_getc(aR); /* wait for something */ + //rxvt_tt_winsize(cmd_fd, TermWin.ncol, TermWin.nrow, cmd_pid); + + uint32_t ch = NOCHAR; + + for (;;) { - rxvt_clean_exit(); - exit(EXIT_FAILURE); /* bad order of events? */ + if (ch == NOCHAR) + ch = next_char (); + + if (ch == NOCHAR) // TODO: improve + break; + + if (ch >= ' ' || ch == '\t' || ch == '\n' || ch == '\r') + { + /* Read a text string from the input buffer */ + uint32_t buf[BUFSIZ]; + bool refreshnow = false; + int nlines = 0; + uint32_t *str = buf; + + *str++ = ch; + + for (;;) + { + ch = next_char (); + + if (ch == NOCHAR || (ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r')) + break; + else + { + *str++ = ch; + + if (ch == '\n') + { + nlines++; + refresh_count++; + + if (!(Options & Opt_jumpScroll) + || (refresh_count >= (refresh_limit * (TermWin.nrow - 1)))) + { + refreshnow = true; + ch = NOCHAR; + break; + } + } + + if (str >= buf + BUFSIZ) + { + ch = NOCHAR; + break; + } + } + } + + rxvt_scr_add_lines (this, buf, nlines, str - buf); + + /* + * If there have been a lot of new lines, then update the screen + * What the heck I'll cheat and only refresh less than every page-full. + * the number of pages between refreshes is refresh_limit, which + * is incremented here because we must be doing flat-out scrolling. + * + * refreshing should be correct for small scrolls, because of the + * time-out + */ + if (refreshnow) + { + if ((Options & Opt_jumpScroll) && refresh_limit < REFRESH_PERIOD) + refresh_limit++; + + rxvt_scr_refresh (this, refresh_type); + } + + } + else + { + switch (ch) + { + default: + rxvt_process_nonprinting (this, ch); + break; + case C0_ESC: /* escape char */ + rxvt_process_escape_seq (this); + break; + /*case 0x9b: */ /* CSI */ + /* rxvt_process_csi_seq (this); */ + } + + ch = NOCHAR; + } } + } - if (count != BUFSIZ) /* some characters read in */ + flush (); +} + +void +rxvt_term::flush () +{ +#ifdef TRANSPARENT + if (want_full_refresh) { - uint32_t c = next_char (this); + want_full_refresh = 0; + rxvt_scr_clear (this); + rxvt_scr_touch (this, False); + want_refresh = 1; + } +#endif -#if 0 - if (c != NOCHAR) - return c; + if (want_refresh) + { + rxvt_scr_refresh (this, refresh_type); + rxvt_scrollbar_show (this, 1); +#ifdef USE_XIM + rxvt_IMSendSpot (this); #endif } + + XFlush (Xdisplay); } /* rxvt_cmd_getc() - Return next input character */ @@ -805,6 +935,20 @@ uint32_t rxvt_cmd_getc(pR) { + for (;;) + { + uint32_t c = R->next_char (); + if (c != NOCHAR) + return c; + + // incomplete sequences should occur rarely, still, a better solution + // would be preferred. either setjmp/longjmp or better design. + fcntl (R->cmd_fd, F_SETFL, 0); + R->pty_fill (1); + fcntl (R->cmd_fd, F_SETFL, O_NONBLOCK); + } + +#if 0 #define TIMEOUT_USEC 5000 fd_set readfds; int quick_timeout, select_res; @@ -814,56 +958,12 @@ struct timeval tp; #endif - uint32_t c = next_char (aR); - if (c != NOCHAR) - return c; - for (;;) { /* loop until we can return something */ if (R->v_bufstr < R->v_bufptr) /* output any pending chars */ rxvt_tt_write(aR_ NULL, 0); -#if defined(POINTER_BLANK) || defined(CURSOR_BLINK) - tp.tv_sec = 0; /* presume == 0 implies time not yet retrieved */ -#endif -#if defined(CURSOR_BLINK) - want_keypress_time = 0; -#endif -#if defined(POINTER_BLANK) - want_motion_time = 0; -#endif - - if (XPending (R->Xdisplay)) - { - R->process_x_events (); - - /* in case button actions pushed chars to cmdbuf */ - if (R->cmdbuf_ptr < R->cmdbuf_endp) - return *R->cmdbuf_ptr++; - } - -#if defined(CURSOR_BLINK) - if (want_keypress_time) { - (void)gettimeofday(&tp, NULL); - R->lastcursorchange.tv_sec = tp.tv_sec; - R->lastcursorchange.tv_usec = tp.tv_usec; - } -#endif -#if defined(POINTER_BLANK) - if (want_motion_time) { - if (!tp.tv_sec) - (void)gettimeofday(&tp, NULL); - R->lastmotion.tv_sec = tp.tv_sec; - R->lastmotion.tv_usec = tp.tv_usec; - } -#endif - -/* - * the command input buffer is empty and we have no pending X events - */ - quick_timeout = 0; - #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING) if (R->mouse_slip_wheel_speed) { quick_timeout = 1; @@ -902,155 +1002,45 @@ } #endif /* NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING */ - FD_ZERO(&readfds); - FD_SET(R->cmd_fd, &readfds); - FD_SET(R->Xfd, &readfds); - value.tv_usec = TIMEOUT_USEC; - value.tv_sec = 0; - - if (!R->TermWin.mapped) - quick_timeout = 0; - else { - quick_timeout |= R->want_refresh; #ifdef TRANSPARENT quick_timeout |= R->want_full_refresh; #endif - } - -#if defined(POINTER_BLANK) || defined(CURSOR_BLINK) - { - int set_quick_timeout = 0; - long csdiff, psdiff; - -#define BLINK_TIME 500000L - csdiff = psdiff = 60000000L; /* or, say, LONG_MAX */ -# if defined(CURSOR_BLINK) - if (R->Options & Opt_cursorBlink) { - if (!tp.tv_sec) /* didn't get it before so get it now */ - (void)gettimeofday(&tp, NULL); - - csdiff = (tp.tv_sec - R->lastcursorchange.tv_sec) * 1000000L - + tp.tv_usec - R->lastcursorchange.tv_usec; - if (csdiff > BLINK_TIME) { /* XXX: settable blink times */ - R->lastcursorchange.tv_sec = tp.tv_sec; - R->lastcursorchange.tv_usec = tp.tv_usec; - R->hidden_cursor = !R->hidden_cursor; - csdiff = 0; - } else - csdiff = BLINK_TIME - csdiff; - set_quick_timeout = 1; - } -# endif -# if defined(POINTER_BLANK) - /* - * If we haven't moved the pointer for a while - */ - if ((R->Options & Opt_pointerBlank) - && (R->pointerBlankDelay > 0) - && (R->hidden_pointer == 0)) { - long pdelay; - - if (!tp.tv_sec) /* didn't get it before so get it now */ - (void)gettimeofday(&tp, NULL); - psdiff = (tp.tv_sec - R->lastmotion.tv_sec) * 1000000L - + tp.tv_usec - R->lastmotion.tv_usec; - pdelay = R->pointerBlankDelay * 1000000L; - if (psdiff >= pdelay) - rxvt_pointer_blank(aR); - else { - set_quick_timeout = 1; - psdiff = pdelay - psdiff; - } - } -# endif - if (!quick_timeout && set_quick_timeout) { - MIN_IT(csdiff, psdiff); - value.tv_sec = csdiff / 1000000L; - value.tv_usec = csdiff % 1000000L; - quick_timeout = 1; - } - } #endif +} - if ((select_res = select(R->num_fds, &readfds, NULL, NULL, - (quick_timeout ? &value : NULL))) == 0) { - /* select statement timed out - we're not hard and fast scrolling */ - R->refresh_limit = 1; - } -#if defined(CURSOR_BLINK) - if (R->Options & Opt_cursorBlink) - R->want_refresh = 1; -#endif +void +rxvt_term::pointer_unblank () +{ + XDefineCursor (Xdisplay, TermWin.vt, TermWin_cursor); + rxvt_recolour_cursor (this); - /* See if we can read new data from the application */ - if (select_res > 0 && FD_ISSET(R->cmd_fd, &readfds)) { - int n; - unsigned int count; - - R->cmdbuf_ptr = R->cmdbuf_endp = R->cmdbuf_base; - for (count = BUFSIZ; count; count -= n, R->cmdbuf_endp += n) - if ((n = read(R->cmd_fd, R->cmdbuf_endp, count)) > 0) - continue; - else if (n == 0 || (n < 0 && errno == EAGAIN)) - break; - else { - rxvt_clean_exit(); - exit(EXIT_FAILURE); /* bad order of events? */ - } +#ifdef POINTER_BLANK + hidden_pointer = 0; - if (count != BUFSIZ) /* some characters read in */ - { - uint32_t c = next_char (aR); - if (c != NOCHAR) - return c; - } - } -#ifdef TRANSPARENT - if (R->want_full_refresh) { - R->want_full_refresh = 0; - rxvt_scr_clear(aR); - rxvt_scr_touch(aR_ False); - R->want_refresh = 1; - } + if (Options & Opt_pointerBlank) + pointer_ev.start (NOW + pointerBlankDelay); #endif - if (R->want_refresh) { - rxvt_scr_refresh(aR_ R->refresh_type); - rxvt_scrollbar_show(aR_ 1); -#ifdef USE_XIM - rxvt_IMSendSpot(aR); -#endif - } - } -/* NOTREACHED */ } -/*}}} */ -/* EXTPROTO */ +#ifdef POINTER_BLANK void -rxvt_pointer_unblank(pR) +rxvt_term::pointer_blank () { - XDefineCursor(R->Xdisplay, R->TermWin.vt, R->TermWin_cursor); - rxvt_recolour_cursor(aR); -#ifdef POINTER_BLANK - R->hidden_pointer = 0; - if (R->pointerBlankDelay > 0) { - struct timeval tp; + pointer_ev.stop (); - (void)gettimeofday(&tp, NULL); - R->lastmotion.tv_sec = tp.tv_sec; - R->lastmotion.tv_usec = tp.tv_usec; - } -#endif + if (!(Options & Opt_pointerBlank)) + return; + + XDefineCursor (Xdisplay, TermWin.vt, blank_cursor); + XFlush (Xdisplay); + + hidden_pointer = 1; } -#ifdef POINTER_BLANK -/* INTPROTO */ void -rxvt_pointer_blank(pR) +rxvt_term::pointer_cb (time_watcher &w) { - XDefineCursor(R->Xdisplay, R->TermWin.vt, R->pointer_blank); - XFlush(R->Xdisplay); - R->hidden_pointer = 1; + pointer_blank (); } #endif @@ -1322,6 +1312,10 @@ if (R->Input_Context != NULL) XSetICFocus(R->Input_Context); #endif +#ifdef CURSOR_BLINK + if (R->Options & Opt_cursorBlink) + R->blink_ev.start (NOW + BLINK_INTERVAL); +#endif } break; @@ -1333,6 +1327,11 @@ if (R->Input_Context != NULL) XUnsetICFocus(R->Input_Context); #endif +#ifdef CURSOR_BLINK + if (R->Options & Opt_cursorBlink) + R->blink_ev.stop (); + R->hidden_cursor = 0; +#endif } break; @@ -1454,7 +1453,7 @@ case MotionNotify: #ifdef POINTER_BLANK if (R->hidden_pointer) - rxvt_pointer_unblank(aR); + R->pointer_unblank (); #endif #if MENUBAR if (isMenuBarWindow(ev->xany.window)) { @@ -3394,107 +3393,6 @@ /* ------------------------------------------------------------------------- */ -/*{{{ Read and process output from the application */ -/* LIBPROTO */ -void -rxvt_main_loop(pR) -{ - uint32_t ch, *str, buf[BUFSIZ]; - int nlines, refreshnow; - - R->cmdbuf_ptr = R->cmdbuf_endp = R->cmdbuf_base; - - /* once we know the shell is running, send the screen size. Again! */ - ch = rxvt_cmd_getc(aR); /* wait for something */ - rxvt_tt_winsize(R->cmd_fd, R->TermWin.ncol, R->TermWin.nrow, R->cmd_pid); - - refreshnow = 0; - for (;;) { - if (ch == NOCHAR) - ch = rxvt_cmd_getc(aR); - - if (ch >= ' ' || ch == '\t' || ch == '\n' || ch == '\r') - { - /* Read a text string from the input buffer */ - nlines = 0; - str = buf; - *str++ = ch; - for (;;) - { - ch = next_char (aR); - - if (ch == NOCHAR || (ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r')) - break; - else - { - *str++ = ch; - - if (ch == '\n') - { - nlines++; - R->refresh_count++; - if (!(R->Options & Opt_jumpScroll) - || (R->refresh_count >= (R->refresh_limit - * (R->TermWin.nrow - 1)))) - { - refreshnow = 1; - ch = NOCHAR; - break; - } - } - - if (str >= buf + BUFSIZ) - { - ch = NOCHAR; - break; - } - } - } - - rxvt_scr_add_lines(aR_ buf, nlines, str - buf); - - /* - * If there have been a lot of new lines, then update the screen - * What the heck I'll cheat and only refresh less than every page-full. - * the number of pages between refreshes is R->refresh_limit, which - * is incremented here because we must be doing flat-out scrolling. - * - * refreshing should be correct for small scrolls, because of the - * time-out - */ - - //TODO: REFRESH_PERIOD is one, fix it - if (refreshnow) - { - refreshnow = 0; - - if ((R->Options & Opt_jumpScroll) && R->refresh_limit < REFRESH_PERIOD) - R->refresh_limit++; - - rxvt_scr_refresh(aR_ R->refresh_type); - } - - } - else - { - switch (ch) - { - default: - rxvt_process_nonprinting(aR_ ch); - break; - case C0_ESC: /* escape char */ - rxvt_process_escape_seq(aR); - break; - /* case 0x9b: */ /* CSI */ - /* rxvt_process_csi_seq(aR); */ - } - - ch = NOCHAR; - } - } -/* NOTREACHED */ -} - /* * Send printf() formatted output to the command. * Only use for small amounts of data.