diff options
| -rw-r--r-- | terms.c | 243 | 
1 files changed, 127 insertions, 116 deletions
| @@ -1,4 +1,4 @@ -/* $Id: terms.c,v 1.22 2001/12/12 17:04:21 ukai Exp $ */ +/* $Id: terms.c,v 1.23 2001/12/14 17:40:09 ukai Exp $ */  /*    * An original curses library for EUC-kanji by Akinori ITO,     December 1989   * revised by Akinori ITO, January 1995 @@ -114,14 +114,14 @@ init_win32_console_handle(void)  static void  expand_win32_console_input_buffer(int n)  { -  if (nConIn + n >= nConInMax) { -    char *oldv; +    if (nConIn + n >= nConInMax) { +	char *oldv; -    nConInMax = ((nConIn + n) / 2 + 1) * 3; -    oldv = ConInV; -    ConInV = GC_MALLOC_ATOMIC(nConInMax); -    memcpy(ConInV, oldv, nConIn); -  } +	nConInMax = ((nConIn + n) / 2 + 1) * 3; +	oldv = ConInV; +	ConInV = GC_MALLOC_ATOMIC(nConInMax); +	memcpy(ConInV, oldv, nConIn); +    }  }  static int @@ -136,41 +136,47 @@ read_win32_console_input(void)  	    expand_win32_console_input_buffer(3);  	    if (ReadConsole(hConIn, &ConInV[nConIn], 1, &nevents, NULL)) { -	      nConIn += nevents; -	      return nevents; -    } +		nConIn += nevents; +		return nevents; +	    } -		break; +	    break;  #ifdef USE_MOUSE  	case MOUSE_EVENT: -	    if ((lastConMouse.dwButtonState ^ rec.Event.MouseEvent.dwButtonState) & ~(~0 << 5)) { -	      int down; -	      MOUSE_EVENT_RECORD *mer; -	      INPUT_RECORD dummy; - -	      expand_win32_console_input_buffer(6); -	      mer = &rec.Event.MouseEvent; -	      ConInV[nConIn] = '\033'; -	      ConInV[nConIn + 1] = '['; -	      ConInV[nConIn + 2] = 'M'; - -	      if (~(mer->dwButtonState) & lastConMouse.dwButtonState & ~(~0 << 5)) -		ConInV[nConIn + 3] = MOUSE_BTN_UP + ' '; -	      else if (!(down = mer->dwButtonState & ~lastConMouse.dwButtonState & ~(~0 << 5))) -		break; -	      else -		ConInV[nConIn + 3] = (down & (1 << 0) ? MOUSE_BTN1_DOWN : -				      down & (1 << 1) ? MOUSE_BTN3_DOWN : -				      down & (1 << 2) ? MOUSE_BTN2_DOWN : -				      down & (1 << 3) ? MOUSE_BTN4_DOWN_XTERM : -				      MOUSE_BTN5_DOWN_XTERM) + ' '; - -	      ConInV[nConIn + 4] = mer->dwMousePosition.X + '!'; -	      ConInV[nConIn + 5] = mer->dwMousePosition.Y + '!'; -	      nConIn += 6; -	      lastConMouse = *mer; -	      ReadConsoleInput(hConIn, &rec, 1, &nevents); -	      return 6; +	    if ((lastConMouse.dwButtonState ^ rec.Event.MouseEvent. +		 dwButtonState) & ~(~0 << 5)) { +		int down; +		MOUSE_EVENT_RECORD *mer; +		INPUT_RECORD dummy; + +		expand_win32_console_input_buffer(6); +		mer = &rec.Event.MouseEvent; +		ConInV[nConIn] = '\033'; +		ConInV[nConIn + 1] = '['; +		ConInV[nConIn + 2] = 'M'; + +		if (~(mer->dwButtonState) & lastConMouse. +		    dwButtonState & ~(~0 << 5)) +		    ConInV[nConIn + 3] = MOUSE_BTN_UP + ' '; +		else if (! +			 (down = +			  mer->dwButtonState & ~lastConMouse. +			  dwButtonState & ~(~0 << 5))) +		    break; +		else +		    ConInV[nConIn + 3] = (down & (1 << 0) ? MOUSE_BTN1_DOWN : +					  down & (1 << 1) ? MOUSE_BTN3_DOWN : +					  down & (1 << 2) ? MOUSE_BTN2_DOWN : +					  down & (1 << 3) ? +					  MOUSE_BTN4_DOWN_XTERM : +					  MOUSE_BTN5_DOWN_XTERM) + ' '; + +		ConInV[nConIn + 4] = mer->dwMousePosition.X + '!'; +		ConInV[nConIn + 5] = mer->dwMousePosition.Y + '!'; +		nConIn += 6; +		lastConMouse = *mer; +		ReadConsoleInput(hConIn, &rec, 1, &nevents); +		return 6;  	    }  #endif  	default: @@ -190,110 +196,112 @@ read_win32_console(char *s, int n)      if (hConIn == INVALID_HANDLE_VALUE)  	return read(tty, s, n); -  if (n > 0) -    for (;;) { -      if (iConIn < nConIn) { -	if (n > nConIn - iConIn) -	  n = nConIn - iConIn; +    if (n > 0) +	for (;;) { +	    if (iConIn < nConIn) { +		if (n > nConIn - iConIn) +		    n = nConIn - iConIn; -	memcpy(s, ConInV, n); +		memcpy(s, ConInV, n); -	if ((iConIn += n) >= nConIn) -	  iConIn = nConIn = 0; +		if ((iConIn += n) >= nConIn) +		    iConIn = nConIn = 0; -			break; -		    } +		break; +	    } -      iConIn = nConIn = 0; +	    iConIn = nConIn = 0; -      while (!read_win32_console_input()) -	; -		} +	    while (!read_win32_console_input()) ; +	} -  return n; +    return n;  }  static int  cmp_tv(const struct timeval *tv_a, const struct timeval *tv_b)  { -  return ((tv_a->tv_sec < tv_b->tv_sec) ? -1 : -	  (tv_a->tv_sec > tv_b->tv_sec) ? 1 : -	  (tv_a->tv_usec < tv_b->tv_usec) ? -1 : -	  (tv_a->tv_usec > tv_b->tv_usec) ? 1 : -	  0); +    return ((tv_a->tv_sec < tv_b->tv_sec) ? -1 : +	    (tv_a->tv_sec > tv_b->tv_sec) ? 1 : +	    (tv_a->tv_usec < tv_b->tv_usec) ? -1 : +	    (tv_a->tv_usec > tv_b->tv_usec) ? 1 : 0);  }  static int  subtract_tv(struct timeval *dst, const struct timeval *src)  { -  if ((dst->tv_usec -= src->tv_usec) < 0) { -    --(dst->tv_sec); -    dst->tv_usec += 1000000; -  } +    if ((dst->tv_usec -= src->tv_usec) < 0) { +	--(dst->tv_sec); +	dst->tv_usec += 1000000; +    } -  return ((dst->tv_sec -= src->tv_sec) < 0 ? -1 : -	  !dst->tv_sec ? (dst->tv_usec < 0 ? -1 : !dst->tv_usec ? 0 : 1) : 1); +    return ((dst->tv_sec -= src->tv_sec) < 0 ? -1 : +	    !dst->tv_sec ? (dst->tv_usec < +			    0 ? -1 : !dst->tv_usec ? 0 : 1) : 1);  }  int -select_or_poll_win32_console(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tout) +select_or_poll_win32_console(int n, fd_set * rfds, fd_set * wfds, +			     fd_set * efds, struct timeval *tout)  { -  int m; -  DWORD nevents; +    int m; +    DWORD nevents; -  if ((m = select(n, rfds, wfds, efds, tout)) < 0) -    return m; +    if ((m = select(n, rfds, wfds, efds, tout)) < 0) +	return m; -  if (iConIn < nConIn) { -    FD_SET(tty, rfds); -    ++m; -	    } -	else { -	    iConIn = nConIn = 0; +    if (iConIn < nConIn) { +	FD_SET(tty, rfds); +	++m; +    } +    else { +	iConIn = nConIn = 0; -    while (GetNumberOfConsoleInputEvents(hConIn, &nevents) && nevents) -      read_win32_console_input(); +	while (GetNumberOfConsoleInputEvents(hConIn, &nevents) && nevents) +	    read_win32_console_input(); -    if (nConIn) { -      FD_SET(tty, rfds); -      ++m; -    } +	if (nConIn) { +	    FD_SET(tty, rfds); +	    ++m;  	} +    } -  return m; +    return m;  }  int -select_win32_console(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tout) +select_win32_console(int n, fd_set * rfds, fd_set * wfds, fd_set * efds, +		     struct timeval *tout)  { -  static struct timeval polltv = {0, 1000000 / CLOCKS_PER_SEC}; -  int m; -  struct timeval tv; +    static struct timeval polltv = { 0, 1000000 / CLOCKS_PER_SEC }; +    int m; +    struct timeval tv; -  if (hConIn == INVALID_HANDLE_VALUE || tty < 0 || tty >= n || !rfds || !FD_ISSET(tty, rfds)) -    return select(n, rfds, wfds, efds, tout); +    if (hConIn == INVALID_HANDLE_VALUE || tty < 0 || tty >= n || !rfds +	|| !FD_ISSET(tty, rfds)) +	return select(n, rfds, wfds, efds, tout); -  FD_CLR(tty, rfds); +    FD_CLR(tty, rfds); -  if (tout) { -    while (cmp_tv(tout, &polltv) > 0) { -      tv = polltv; - -      if ((m = select_or_poll_win32_console(n, rfds, wfds, efds, &tv))) -	return m; +    if (tout) { +	while (cmp_tv(tout, &polltv) > 0) { +	    tv = polltv; -      subtract_tv(tout, &polltv); -    } +	    if ((m = select_or_poll_win32_console(n, rfds, wfds, efds, &tv))) +		return m; -    return select_or_poll_win32_console(n, rfds, wfds, efds, tout); -  } -  else -    for (;;) { -      tv = polltv; +	    subtract_tv(tout, &polltv); +	} -      if ((m = select_or_poll_win32_console(n, rfds, wfds, efds, &tv))) -	return m; +	return select_or_poll_win32_console(n, rfds, wfds, efds, tout);      } +    else +	for (;;) { +	    tv = polltv; + +	    if ((m = select_or_poll_win32_console(n, rfds, wfds, efds, &tv))) +		return m; +	}  }  #endif @@ -470,13 +478,16 @@ static struct mouse_term_info {      char *term;      int flag;  } xterm_mouse_term[] = { -	{"xterm", NEED_XTERM_ON|NEED_XTERM_OFF}, -	{"kterm", NEED_XTERM_ON|NEED_XTERM_OFF}, -	{"rxvt", NEED_XTERM_ON|NEED_XTERM_OFF}, +    { +    "xterm", NEED_XTERM_ON | NEED_XTERM_OFF}, { +    "kterm", NEED_XTERM_ON | NEED_XTERM_OFF}, { +    "rxvt", NEED_XTERM_ON | NEED_XTERM_OFF},  #ifdef __CYGWIN__ -	{"cygwin", NEED_XTERM_ON}, +    { +    "cygwin", NEED_XTERM_ON},  #endif -	{NULL, 0} +    { +    NULL, 0}  };  #endif @@ -504,8 +515,8 @@ set_tty(void)  	char *term = getenv("TERM");  	struct mouse_term_info *p;  	for (p = xterm_mouse_term; p->term != NULL; p++) { -	     if (!strncmp(term, p->term, strlen(p->term))) { -		  is_xterm = p->flag; +	    if (!strncmp(term, p->term, strlen(p->term))) { +		is_xterm = p->flag;  		break;  	    }  	} @@ -1965,11 +1976,11 @@ sleep_till_anykey(int sec, int purge)      if (  #ifdef __CYGWIN__ -	select_win32_console(tty + 1, &rfd, 0, 0, &tim) +	   select_win32_console(tty + 1, &rfd, 0, 0, &tim)  #else -	select(tty + 1, &rfd, 0, 0, &tim) +	   select(tty + 1, &rfd, 0, 0, &tim)  #endif -	> 0 && purge) { +	   > 0 && purge) {  	c = getch();  	if (c == ESC_CODE)  	    skip_escseq(); | 
