]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/win32con/win_driver.c
ncurses 5.9 - patch 20130831
[ncurses.git] / ncurses / win32con / win_driver.c
index 5ebc85fab7ad933e17de687e63c1b29e0d7b152f..d3660d24abd602f02ef98b26ac9658f95e009d1e 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2012,2013 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -28,7 +28,6 @@
 
 /****************************************************************************
  *  Author: Juergen Pfeifer                                                 *
- *                                                                          *
  ****************************************************************************/
 
 /*
  */
 
 #include <curses.priv.h>
+#define CUR my_term.type.
 
-MODULE_ID("$Id: win_driver.c,v 1.6 2010/02/06 19:55:44 tom Exp $")
+MODULE_ID("$Id: win_driver.c,v 1.20 2013/08/17 19:25:30 tom Exp $")
 
 #define WINMAGIC NCDRV_MAGIC(NCDRV_WINCONSOLE)
 
-#define AssertTCB() assert(TCB!=0 && TCB->magic==WINMAGIC)
-#define SetSP() assert(TCB->csp!=0); sp = TCB->csp
+#define EXP_OPTIMIZE 0
+
+#define okConsoleHandle(TCB) (TCB != 0 && !InvalidConsoleHandle(TCB->hdl))
+
+#define AssertTCB() assert(TCB != 0 && (TCB->magic == WINMAGIC))
+#define SetSP()     assert(TCB->csp != 0); sp = TCB->csp; (void) sp
 
 #define GenMap(vKey,key) MAKELONG(key, vKey)
 
@@ -68,6 +72,7 @@ static const LONG keylist[] =
 typedef struct props {
     CONSOLE_SCREEN_BUFFER_INFO SBI;
     bool progMode;
+    TERM_HANDLE lastOut;
     DWORD map[MAPSIZE];
     DWORD rmap[MAPSIZE];
     WORD pairs[NUMPAIRS];
@@ -102,7 +107,7 @@ MapColor(bool fore, int color)
 }
 
 static WORD
-MapAttr(TERMINAL_CONTROL_BLOCK * TCB, WORD res, chtype ch)
+MapAttr(TERMINAL_CONTROL_BLOCK * TCB, WORD res, attr_t ch)
 {
     if (ch & A_COLOR) {
        int p;
@@ -110,7 +115,7 @@ MapAttr(TERMINAL_CONTROL_BLOCK * TCB, WORD res, chtype ch)
 
        AssertTCB();
        SetSP();
-       p = PAIR_NUMBER(ch);
+       p = PairNumber(ch);
        if (p > 0 && p < NUMPAIRS && TCB != 0 && sp != 0) {
            WORD a;
            a = PropOf(TCB)->pairs[p];
@@ -134,8 +139,68 @@ MapAttr(TERMINAL_CONTROL_BLOCK * TCB, WORD res, chtype ch)
     return res;
 }
 
+#if USE_WIDEC_SUPPORT
+/*
+ * TODO: support surrogate pairs
+ * TODO: support combining characters
+ * TODO: support acsc
+ * TODO: check wcwidth of base character, fill if needed for double-width
+ * TODO: _nc_wacs should be part of sp.
+ */
 static BOOL
-con_write(TERMINAL_CONTROL_BLOCK * TCB, int y, int x, chtype *str, int n)
+con_write16(TERMINAL_CONTROL_BLOCK * TCB, int y, int x, cchar_t *str, int limit)
+{
+    int actual = 0;
+    CHAR_INFO ci[limit];
+    COORD loc, siz;
+    SMALL_RECT rec;
+    int i;
+    cchar_t ch;
+    SCREEN *sp;
+
+    AssertTCB();
+
+    SetSP();
+
+    for (i = actual = 0; i < limit; i++) {
+       ch = str[i];
+       if (isWidecExt(ch))
+           continue;
+       ci[actual].Char.UnicodeChar = CharOf(ch);
+       ci[actual].Attributes = MapAttr(TCB,
+                                       PropOf(TCB)->SBI.wAttributes,
+                                       AttrOf(ch));
+       if (AttrOf(ch) & A_ALTCHARSET) {
+           if (_nc_wacs) {
+               int which = CharOf(ch);
+               if (which > 0
+                   && which < ACS_LEN
+                   && CharOf(_nc_wacs[which]) != 0) {
+                   ci[actual].Char.UnicodeChar = CharOf(_nc_wacs[which]);
+               } else {
+                   ci[actual].Char.UnicodeChar = ' ';
+               }
+           }
+       }
+       ++actual;
+    }
+
+    loc.X = (short) 0;
+    loc.Y = (short) 0;
+    siz.X = (short) actual;
+    siz.Y = 1;
+
+    rec.Left = (short) x;
+    rec.Top = (short) y;
+    rec.Right = (short) (x + limit - 1);
+    rec.Bottom = rec.Top;
+
+    return WriteConsoleOutputW(TCB->hdl, ci, siz, loc, &rec);
+}
+#define con_write(tcb, y, x, str, n) con_write16(tcb, y, x, str, n)
+#else
+static BOOL
+con_write8(TERMINAL_CONTROL_BLOCK * TCB, int y, int x, chtype *str, int n)
 {
     CHAR_INFO ci[n];
     COORD loc, siz;
@@ -146,9 +211,6 @@ con_write(TERMINAL_CONTROL_BLOCK * TCB, int y, int x, chtype *str, int n)
 
     AssertTCB();
 
-    if (TCB == 0 || InvalidConsoleHandle(TCB->hdl))
-       return FALSE;
-
     SetSP();
 
     for (i = 0; i < n; i++) {
@@ -176,55 +238,163 @@ con_write(TERMINAL_CONTROL_BLOCK * TCB, int y, int x, chtype *str, int n)
 
     return WriteConsoleOutput(TCB->hdl, ci, siz, loc, &rec);
 }
+#define con_write(tcb, y, x, str, n) con_write8(tcb, y, x, str, n)
+#endif
+
+#if EXP_OPTIMIZE
+/*
+ * Comparing new/current screens, determine the last column-index for a change
+ * beginning on the given row,col position.  Unlike a serial terminal, there is
+ * no cost for "moving" the "cursor" on the line as we update it.
+ */
+static int
+find_end_of_change(SCREEN *sp, int row, int col)
+{
+    int result = col;
+    struct ldat *curdat = CurScreen(sp)->_line + row;
+    struct ldat *newdat = NewScreen(sp)->_line + row;
+
+    while (col <= newdat->lastchar) {
+#if USE_WIDEC_SUPPORT
+       if (isWidecExt(curdat->text[col]) || isWidecExt(newdat->text[col])) {
+           result = col;
+       } else if (memcmp(&curdat->text[col],
+                         &newdat->text[col],
+                         sizeof(curdat->text[0]))) {
+           result = col;
+       } else {
+           break;
+       }
+#else
+       if (curdat->text[col] != newdat->text[col]) {
+           result = col;
+       } else {
+           break;
+       }
+#endif
+       ++col;
+    }
+    return result;
+}
+
+/*
+ * Given a row,col position at the end of a change-chunk, look for the
+ * beginning of the next change-chunk.
+ */
+static int
+find_next_change(SCREEN *sp, int row, int col)
+{
+    struct ldat *curdat = CurScreen(sp)->_line + row;
+    struct ldat *newdat = NewScreen(sp)->_line + row;
+    int result = newdat->lastchar + 1;
+
+    while (++col <= newdat->lastchar) {
+#if USE_WIDEC_SUPPORT
+       if (isWidecExt(curdat->text[col]) != isWidecExt(newdat->text[col])) {
+           result = col;
+           break;
+       } else if (memcmp(&curdat->text[col],
+                         &newdat->text[col],
+                         sizeof(curdat->text[0]))) {
+           result = col;
+           break;
+       }
+#else
+       if (curdat->text[col] != newdat->text[col]) {
+           result = col;
+           break;
+       }
+#endif
+    }
+    return result;
+}
+
+#define EndChange(first) \
+       find_end_of_change(sp, y, first)
+#define NextChange(last) \
+       find_next_change(sp, y, last)
+
+#endif /* EXP_OPTIMIZE */
 
 #define MARK_NOCHANGE(win,row) \
                win->_line[row].firstchar = _NOCHANGE; \
                win->_line[row].lastchar  = _NOCHANGE
 
+static void
+selectActiveHandle(TERMINAL_CONTROL_BLOCK * TCB)
+{
+    if (PropOf(TCB)->lastOut != TCB->hdl) {
+       PropOf(TCB)->lastOut = TCB->hdl;
+       SetConsoleActiveScreenBuffer(PropOf(TCB)->lastOut);
+    }
+}
+
 static int
 drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB)
 {
+    int result = ERR;
     int y, nonempty, n, x0, x1, Width, Height;
     SCREEN *sp;
 
     AssertTCB();
     SetSP();
 
-    Width = screen_columns(sp);
-    Height = screen_lines(sp);
-    nonempty = min(Height, NewScreen(sp)->_maxy + 1);
-
-    if ((CurScreen(sp)->_clear || NewScreen(sp)->_clear)) {
-       int x;
-       chtype empty[Width];
-
-       for (x = 0; x < Width; x++)
-           empty[x] = ' ';
-
-       for (y = 0; y < nonempty; y++) {
-           con_write(TCB, y, 0, empty, Width);
-           memcpy(empty,
-                  CurScreen(sp)->_line[y].text,
-                  Width * sizeof(chtype));
+    T((T_CALLED("win32con::drv_doupdate(%p)"), TCB));
+    if (okConsoleHandle(TCB)) {
+
+       Width = screen_columns(sp);
+       Height = screen_lines(sp);
+       nonempty = min(Height, NewScreen(sp)->_maxy + 1);
+
+       if ((CurScreen(sp)->_clear || NewScreen(sp)->_clear)) {
+           int x;
+#if USE_WIDEC_SUPPORT
+           cchar_t empty[Width];
+           wchar_t blank[2] =
+           {
+               L' ', L'\0'
+           };
+
+           for (x = 0; x < Width; x++)
+               setcchar(&empty[x], blank, 0, 0, 0);
+#else
+           chtype empty[Width];
+
+           for (x = 0; x < Width; x++)
+               empty[x] = ' ';
+#endif
+
+           for (y = 0; y < nonempty; y++) {
+               con_write(TCB, y, 0, empty, Width);
+               memcpy(empty,
+                      CurScreen(sp)->_line[y].text,
+                      Width * sizeof(empty[0]));
+           }
+           CurScreen(sp)->_clear = FALSE;
+           NewScreen(sp)->_clear = FALSE;
+           touchwin(NewScreen(sp));
        }
-       CurScreen(sp)->_clear = FALSE;
-       NewScreen(sp)->_clear = FALSE;
-       touchwin(NewScreen(sp));
-    }
 
-    for (y = 0; y < nonempty; y++) {
-       x0 = NewScreen(sp)->_line[y].firstchar;
-       if (x0 != _NOCHANGE) {
-           x1 = NewScreen(sp)->_line[y].lastchar;
-           n = x1 - x0 + 1;
-           if (n > 0) {
-               memcpy(CurScreen(sp)->_line[y].text + x0,
-                      NewScreen(sp)->_line[y].text + x0,
-                      n * sizeof(chtype));
-               con_write(TCB,
-                         y,
-                         x0,
-                         ((chtype *) CurScreen(sp)->_line[y].text) + x0, n);
+       for (y = 0; y < nonempty; y++) {
+           x0 = NewScreen(sp)->_line[y].firstchar;
+           if (x0 != _NOCHANGE) {
+#if EXP_OPTIMIZE
+               int x2;
+               int limit = NewScreen(sp)->_line[y].lastchar;
+               while ((x1 = EndChange(x0)) <= limit) {
+                   while ((x2 = NextChange(x1)) <= limit && x2 <= (x1 + 2)) {
+                       x1 = x2;
+                   }
+                   n = x1 - x0 + 1;
+                   memcpy(&CurScreen(sp)->_line[y].text[x0],
+                          &NewScreen(sp)->_line[y].text[x0],
+                          n * sizeof(CurScreen(sp)->_line[y].text[x0]));
+                   con_write(TCB,
+                             y,
+                             x0,
+                             &CurScreen(sp)->_line[y].text[x0], n);
+                   x0 = NextChange(x1);
+               }
 
                /* mark line changed successfully */
                if (y <= NewScreen(sp)->_maxy) {
@@ -233,26 +403,50 @@ drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB)
                if (y <= CurScreen(sp)->_maxy) {
                    MARK_NOCHANGE(CurScreen(sp), y);
                }
+#else
+               x1 = NewScreen(sp)->_line[y].lastchar;
+               n = x1 - x0 + 1;
+               if (n > 0) {
+                   memcpy(&CurScreen(sp)->_line[y].text[x0],
+                          &NewScreen(sp)->_line[y].text[x0],
+                          n * sizeof(CurScreen(sp)->_line[y].text[x0]));
+                   con_write(TCB,
+                             y,
+                             x0,
+                             &CurScreen(sp)->_line[y].text[x0], n);
+
+                   /* mark line changed successfully */
+                   if (y <= NewScreen(sp)->_maxy) {
+                       MARK_NOCHANGE(NewScreen(sp), y);
+                   }
+                   if (y <= CurScreen(sp)->_maxy) {
+                       MARK_NOCHANGE(CurScreen(sp), y);
+                   }
+               }
+#endif
            }
        }
-    }
 
-    /* put everything back in sync */
-    for (y = nonempty; y <= NewScreen(sp)->_maxy; y++) {
-       MARK_NOCHANGE(NewScreen(sp), y);
-    }
-    for (y = nonempty; y <= CurScreen(sp)->_maxy; y++) {
-       MARK_NOCHANGE(CurScreen(sp), y);
-    }
+       /* put everything back in sync */
+       for (y = nonempty; y <= NewScreen(sp)->_maxy; y++) {
+           MARK_NOCHANGE(NewScreen(sp), y);
+       }
+       for (y = nonempty; y <= CurScreen(sp)->_maxy; y++) {
+           MARK_NOCHANGE(CurScreen(sp), y);
+       }
 
-    if (!NewScreen(sp)->_leaveok) {
-       CurScreen(sp)->_curx = NewScreen(sp)->_curx;
-       CurScreen(sp)->_cury = NewScreen(sp)->_cury;
+       if (!NewScreen(sp)->_leaveok) {
+           CurScreen(sp)->_curx = NewScreen(sp)->_curx;
+           CurScreen(sp)->_cury = NewScreen(sp)->_cury;
 
-       TCB->drv->hwcur(TCB, 0, 0, CurScreen(sp)->_cury, CurScreen(sp)->_curx);
+           TCB->drv->hwcur(TCB,
+                           0, 0,
+                           CurScreen(sp)->_cury, CurScreen(sp)->_curx);
+       }
+       selectActiveHandle(TCB);
+       result = OK;
     }
-    SetConsoleActiveScreenBuffer(TCB->hdl);
-    return OK;
+    returnCode(result);
 }
 
 static bool
@@ -262,16 +456,43 @@ drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB,
 {
     bool code = FALSE;
 
-    T((T_CALLED("drv_CanHandle(%p)"), TCB));
+    T((T_CALLED("win32con::drv_CanHandle(%p)"), TCB));
 
     assert(TCB != 0);
     assert(tname != 0);
 
     TCB->magic = WINMAGIC;
-    if (*tname == 0 || *tname == 0 || strcmp(tname, "unknown") == 0) {
+    if (*tname == 0 || *tname == 0 || *tname == '#') {
        code = TRUE;
+    } else {
+       TERMINAL my_term;
+       int status;
+
+       code = FALSE;
+#if (NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP)
+       status = _nc_setup_tinfo(tname, &my_term.type);
+#else
+       status = TGETENT_NO;
+#endif
+       if (status != TGETENT_YES) {
+           const TERMTYPE *fallback = _nc_fallback(tname);
+
+           if (fallback) {
+               my_term.type = *fallback;
+               status = TGETENT_YES;
+           } else if (!strcmp(tname, "unknown")) {
+               code = TRUE;
+           }
+       }
+       if (status == TGETENT_YES) {
+           if (generic_type || hard_copy)
+               code = TRUE;
+       }
+    }
+
+    if (code) {
        if ((TCB->term.type.Booleans) == 0) {
-           _nc_init_entry(&(TCB->term.type));
+           _nc_init_termtype(&(TCB->term.type));
        }
     }
 
@@ -280,7 +501,7 @@ drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB,
 
 static int
 drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB,
-               bool beepFlag GCC_UNUSED)
+               int beepFlag GCC_UNUSED)
 {
     SCREEN *sp;
     int res = ERR;
@@ -320,13 +541,13 @@ drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB,
 
 static void
 drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB,
-            bool fore,
+            int fore,
             int color,
             int (*outc) (SCREEN *, int) GCC_UNUSED)
 {
     AssertTCB();
 
-    if (TCB && !InvalidConsoleHandle(TCB->hdl)) {
+    if (okConsoleHandle(TCB)) {
        WORD a = MapColor(fore, color);
        a = ((PropOf(TCB)->SBI.wAttributes) & (fore ? 0xfff8 : 0xff8f)) | a;
        SetConsoleTextAttribute(TCB->hdl, a);
@@ -340,7 +561,7 @@ drv_rescol(TERMINAL_CONTROL_BLOCK * TCB)
     bool res = FALSE;
 
     AssertTCB();
-    if (TCB && !InvalidConsoleHandle(TCB->hdl)) {
+    if (okConsoleHandle(TCB)) {
        WORD a = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN;
        SetConsoleTextAttribute(TCB->hdl, a);
        GetConsoleScreenBufferInfo(TCB->hdl, &(PropOf(TCB)->SBI));
@@ -364,14 +585,20 @@ drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB)
 static int
 drv_size(TERMINAL_CONTROL_BLOCK * TCB, int *Lines, int *Cols)
 {
+    int result = ERR;
+
     AssertTCB();
 
-    if (TCB == NULL || Lines == NULL || Cols == NULL || InvalidConsoleHandle(TCB->hdl))
-       return ERR;
+    T((T_CALLED("win32con::drv_size(%p)"), TCB));
 
-    *Lines = (int) (PropOf(TCB)->SBI.dwSize.Y);
-    *Cols = (int) (PropOf(TCB)->SBI.dwSize.X);
-    return OK;
+    if (okConsoleHandle(TCB) &&
+       Lines != NULL &&
+       Cols != NULL) {
+       *Lines = (int) (PropOf(TCB)->SBI.dwSize.Y);
+       *Cols = (int) (PropOf(TCB)->SBI.dwSize.X);
+       result = OK;
+    }
+    returnCode(result);
 }
 
 static int
@@ -384,7 +611,7 @@ drv_setsize(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED,
 }
 
 static int
-drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf)
+drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, int setFlag, TTY * buf)
 {
     DWORD dwFlag = 0;
     tcflag_t iflag;
@@ -451,7 +678,7 @@ drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, bool setFlag, TTY * buf)
 }
 
 static int
-drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag)
+drv_mode(TERMINAL_CONTROL_BLOCK * TCB, int progFlag, int defFlag)
 {
     SCREEN *sp;
     TERMINAL *_term = (TERMINAL *) TCB;
@@ -461,7 +688,8 @@ drv_mode(TERMINAL_CONTROL_BLOCK * TCB, bool progFlag, bool defFlag)
     sp = TCB->csp;
 
     PropOf(TCB)->progMode = progFlag;
-    SetConsoleActiveScreenBuffer(progFlag ? TCB->hdl : TCB->out);
+    PropOf(TCB)->lastOut = progFlag ? TCB->hdl : TCB->out;
+    SetConsoleActiveScreenBuffer(PropOf(TCB)->lastOut);
 
     if (progFlag) /* prog mode */  {
        if (defFlag) {
@@ -556,7 +784,7 @@ MapKey(TERMINAL_CONTROL_BLOCK * TCB, WORD vKey)
 static void
 drv_release(TERMINAL_CONTROL_BLOCK * TCB)
 {
-    T((T_CALLED("drv_release(%p)"), TCB));
+    T((T_CALLED("win32con::drv_release(%p)"), TCB));
 
     AssertTCB();
     if (TCB->prop)
@@ -568,7 +796,9 @@ drv_release(TERMINAL_CONTROL_BLOCK * TCB)
 static void
 drv_init(TERMINAL_CONTROL_BLOCK * TCB)
 {
-    T((T_CALLED("drv_init(%p)"), TCB));
+    DWORD num_buttons;
+
+    T((T_CALLED("win32con::drv_init(%p)"), TCB));
 
     AssertTCB();
 
@@ -610,6 +840,13 @@ drv_init(TERMINAL_CONTROL_BLOCK * TCB)
        TCB->info.nocolorvideo = 1;
        TCB->info.tabsize = 8;
 
+       if (GetNumberOfConsoleMouseButtons(&num_buttons)) {
+           T(("mouse has %ld buttons", num_buttons));
+           TCB->info.numbuttons = num_buttons;
+       } else {
+           TCB->info.numbuttons = 1;
+       }
+
        TCB->info.defaultPalette = _nc_cga_palette;
 
        for (i = 0; i < (N_INI + FKEYS); i++) {
@@ -637,9 +874,9 @@ drv_init(TERMINAL_CONTROL_BLOCK * TCB)
 
 static void
 drv_initpair(TERMINAL_CONTROL_BLOCK * TCB,
-            short pair,
-            short f,
-            short b)
+            int pair,
+            int f,
+            int b)
 {
     SCREEN *sp;
 
@@ -654,10 +891,10 @@ drv_initpair(TERMINAL_CONTROL_BLOCK * TCB,
 
 static void
 drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
-             short color GCC_UNUSED,
-             short r GCC_UNUSED,
-             short g GCC_UNUSED,
-             short b GCC_UNUSED)
+             int color GCC_UNUSED,
+             int r GCC_UNUSED,
+             int g GCC_UNUSED,
+             int b GCC_UNUSED)
 {
     SCREEN *sp;
 
@@ -667,9 +904,9 @@ drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
 
 static void
 drv_do_color(TERMINAL_CONTROL_BLOCK * TCB,
-            short old_pair GCC_UNUSED,
-            short pair GCC_UNUSED,
-            bool reverse GCC_UNUSED,
+            int old_pair GCC_UNUSED,
+            int pair GCC_UNUSED,
+            int reverse GCC_UNUSED,
             int (*outc) (SCREEN *, int) GCC_UNUSED
 )
 {
@@ -690,13 +927,35 @@ drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB)
     sp->_mouse_type = M_TERM_DRIVER;
 }
 
+static int
+drv_testmouse(TERMINAL_CONTROL_BLOCK * TCB, int delay)
+{
+    int rc = 0;
+    SCREEN *sp;
+
+    AssertTCB();
+    SetSP();
+
+    if (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
+       rc = TW_MOUSE;
+    } else {
+       rc = TCBOf(sp)->drv->twait(TCBOf(sp),
+                                  TWAIT_MASK,
+                                  delay,
+                                  (int *) 0
+                                  EVENTLIST_2nd(evl));
+    }
+
+    return rc;
+}
+
 static int
 drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB,
          int yold GCC_UNUSED, int xold GCC_UNUSED,
          int y, int x)
 {
     int ret = ERR;
-    if (TCB && !InvalidConsoleHandle(TCB->hdl)) {
+    if (okConsoleHandle(TCB)) {
        COORD loc;
        loc.X = (short) x;
        loc.Y = (short) y;
@@ -719,7 +978,7 @@ drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB,
 
 static void
 drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB,
-                bool OnFlag GCC_UNUSED)
+                int OnFlag GCC_UNUSED)
 {
     SCREEN *sp;
 
@@ -818,6 +1077,50 @@ Adjust(int milliseconds, int diff)
     return milliseconds;
 }
 
+#define BUTTON_MASK (FROM_LEFT_1ST_BUTTON_PRESSED | \
+                    FROM_LEFT_2ND_BUTTON_PRESSED | \
+                    FROM_LEFT_3RD_BUTTON_PRESSED | \
+                    FROM_LEFT_4TH_BUTTON_PRESSED | \
+                    RIGHTMOST_BUTTON_PRESSED)
+
+static int
+decode_mouse(TERMINAL_CONTROL_BLOCK * TCB, int mask)
+{
+    SCREEN *sp;
+    int result = 0;
+
+    AssertTCB();
+    SetSP();
+
+    if (mask & FROM_LEFT_1ST_BUTTON_PRESSED)
+       result |= BUTTON1_PRESSED;
+    if (mask & FROM_LEFT_2ND_BUTTON_PRESSED)
+       result |= BUTTON2_PRESSED;
+    if (mask & FROM_LEFT_3RD_BUTTON_PRESSED)
+       result |= BUTTON3_PRESSED;
+    if (mask & FROM_LEFT_4TH_BUTTON_PRESSED)
+       result |= BUTTON4_PRESSED;
+
+    if (mask & RIGHTMOST_BUTTON_PRESSED) {
+       switch (TCB->info.numbuttons) {
+       case 1:
+           result |= BUTTON1_PRESSED;
+           break;
+       case 2:
+           result |= BUTTON2_PRESSED;
+           break;
+       case 3:
+           result |= BUTTON3_PRESSED;
+           break;
+       case 4:
+           result |= BUTTON4_PRESSED;
+           break;
+       }
+    }
+
+    return result;
+}
+
 static int
 drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
          int mode,
@@ -826,7 +1129,7 @@ drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
          EVENTLIST_2nd(_nc_eventlist * evl))
 {
     SCREEN *sp;
-    INPUT_RECORD inp;
+    INPUT_RECORD inp_rec;
     BOOL b;
     DWORD nRead = 0, rc = -1;
     int code = 0;
@@ -835,15 +1138,18 @@ drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
     int diff;
     bool isImmed = (milliseconds == 0);
 
-#define CONSUME() ReadConsoleInput(TCB->inp,&inp,1,&nRead)
+#define CONSUME() ReadConsoleInput(TCB->inp,&inp_rec,1,&nRead)
 
     AssertTCB();
     SetSP();
 
+    TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d",
+                     milliseconds, mode));
+
     if (milliseconds < 0)
        milliseconds = INFINITY;
 
-    memset(&inp, 0, sizeof(inp));
+    memset(&inp_rec, 0, sizeof(inp_rec));
 
     while (true) {
        GetSystemTimeAsFileTime(&fstart);
@@ -859,15 +1165,15 @@ drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
            if (mode) {
                b = GetNumberOfConsoleInputEvents(TCB->inp, &nRead);
                if (b && nRead > 0) {
-                   b = PeekConsoleInput(TCB->inp, &inp, 1, &nRead);
+                   b = PeekConsoleInput(TCB->inp, &inp_rec, 1, &nRead);
                    if (b && nRead > 0) {
-                       switch (inp.EventType) {
+                       switch (inp_rec.EventType) {
                        case KEY_EVENT:
                            if (mode & TW_INPUT) {
-                               WORD vk = inp.Event.KeyEvent.wVirtualKeyCode;
-                               char ch = inp.Event.KeyEvent.uChar.AsciiChar;
+                               WORD vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
+                               char ch = inp_rec.Event.KeyEvent.uChar.AsciiChar;
 
-                               if (inp.Event.KeyEvent.bKeyDown) {
+                               if (inp_rec.Event.KeyEvent.bKeyDown) {
                                    if (0 == ch) {
                                        int nKey = MapKey(TCB, vk);
                                        if ((nKey < 0) || FALSE == sp->_keypad_on) {
@@ -883,14 +1189,17 @@ drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
                            }
                            continue;
                        case MOUSE_EVENT:
-                           if (0 && mode & TW_MOUSE) {
+                           if (decode_mouse(TCB,
+                                            (inp_rec.Event.MouseEvent.dwButtonState
+                                             & BUTTON_MASK)) == 0) {
+                               CONSUME();
+                           } else if (mode & TW_MOUSE) {
                                code = TW_MOUSE;
                                goto end;
-                           } else
-                               continue;
+                           }
+                           continue;
                        default:
-                           SetConsoleActiveScreenBuffer(!PropOf(TCB)->progMode ?
-                                                        TCB->hdl : TCB->out);
+                           selectActiveHandle(TCB);
                            continue;
                        }
                    }
@@ -908,22 +1217,26 @@ drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
        }
     }
   end:
+
+    TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
+                     code, errno, milliseconds));
+
     if (timeleft)
        *timeleft = milliseconds;
 
     return code;
 }
 
-#define BUTTON_MASK (FROM_LEFT_1ST_BUTTON_PRESSED | \
-                    FROM_LEFT_2ND_BUTTON_PRESSED | \
-                    FROM_LEFT_3RD_BUTTON_PRESSED)
-
 static bool
-handle_mouse(SCREEN *sp, MOUSE_EVENT_RECORD mer)
+handle_mouse(TERMINAL_CONTROL_BLOCK * TCB, MOUSE_EVENT_RECORD mer)
 {
+    SCREEN *sp;
     MEVENT work;
     bool result = FALSE;
 
+    AssertTCB();
+    SetSP();
+
     sp->_drv_mouse_old_buttons = sp->_drv_mouse_new_buttons;
     sp->_drv_mouse_new_buttons = mer.dwButtonState & BUTTON_MASK;
 
@@ -937,25 +1250,12 @@ handle_mouse(SCREEN *sp, MOUSE_EVENT_RECORD mer)
 
        if (sp->_drv_mouse_new_buttons) {
 
-           if (sp->_drv_mouse_new_buttons & FROM_LEFT_1ST_BUTTON_PRESSED)
-               work.bstate |= BUTTON1_PRESSED;
-           if (sp->_drv_mouse_new_buttons & FROM_LEFT_2ND_BUTTON_PRESSED)
-               work.bstate |= BUTTON2_PRESSED;
-           if (sp->_drv_mouse_new_buttons & FROM_LEFT_3RD_BUTTON_PRESSED)
-               work.bstate |= BUTTON3_PRESSED;
-           if (sp->_drv_mouse_new_buttons & FROM_LEFT_4TH_BUTTON_PRESSED)
-               work.bstate |= BUTTON4_PRESSED;
+           work.bstate |= decode_mouse(TCB, sp->_drv_mouse_new_buttons);
 
        } else {
 
-           if (sp->_drv_mouse_old_buttons & FROM_LEFT_1ST_BUTTON_PRESSED)
-               work.bstate |= BUTTON1_RELEASED;
-           if (sp->_drv_mouse_old_buttons & FROM_LEFT_2ND_BUTTON_PRESSED)
-               work.bstate |= BUTTON2_RELEASED;
-           if (sp->_drv_mouse_old_buttons & FROM_LEFT_3RD_BUTTON_PRESSED)
-               work.bstate |= BUTTON3_RELEASED;
-           if (sp->_drv_mouse_old_buttons & FROM_LEFT_4TH_BUTTON_PRESSED)
-               work.bstate |= BUTTON4_RELEASED;
+           /* cf: BUTTON_PRESSED, BUTTON_RELEASED */
+           work.bstate |= (decode_mouse(TCB, sp->_drv_mouse_old_buttons) >> 1);
 
            result = TRUE;
        }
@@ -975,27 +1275,25 @@ drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
 {
     SCREEN *sp;
     int n = 1;
-    INPUT_RECORD inp;
+    INPUT_RECORD inp_rec;
     BOOL b;
     DWORD nRead;
     WORD vk;
-    WORD sc;
 
     AssertTCB();
     assert(buf);
     SetSP();
 
-    memset(&inp, 0, sizeof(inp));
+    memset(&inp_rec, 0, sizeof(inp_rec));
 
-    T((T_CALLED("drv_read(%p)"), TCB));
-    while ((b = ReadConsoleInput(TCB->inp, &inp, 1, &nRead))) {
+    T((T_CALLED("win32con::drv_read(%p)"), TCB));
+    while ((b = ReadConsoleInput(TCB->inp, &inp_rec, 1, &nRead))) {
        if (b && nRead > 0) {
-           if (inp.EventType == KEY_EVENT) {
-               if (!inp.Event.KeyEvent.bKeyDown)
+           if (inp_rec.EventType == KEY_EVENT) {
+               if (!inp_rec.Event.KeyEvent.bKeyDown)
                    continue;
-               *buf = (int) inp.Event.KeyEvent.uChar.AsciiChar;
-               vk = inp.Event.KeyEvent.wVirtualKeyCode;
-               sc = inp.Event.KeyEvent.wVirtualScanCode;
+               *buf = (int) inp_rec.Event.KeyEvent.uChar.AsciiChar;
+               vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
                if (*buf == 0) {
                    if (sp->_keypad_on) {
                        *buf = MapKey(TCB, vk);
@@ -1008,8 +1306,8 @@ drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
                } else {        /* *buf != 0 */
                    break;
                }
-           } else if (inp.EventType == MOUSE_EVENT) {
-               if (handle_mouse(sp, inp.Event.MouseEvent)) {
+           } else if (inp_rec.EventType == MOUSE_EVENT) {
+               if (handle_mouse(TCB, inp_rec.Event.MouseEvent)) {
                    *buf = KEY_MOUSE;
                    break;
                }
@@ -1023,8 +1321,9 @@ drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
 static int
 drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms)
 {
+    T((T_CALLED("win32con::drv_nap(%p, %d)"), TCB, ms));
     Sleep(ms);
-    return OK;
+    returnCode(OK);
 }
 
 static bool
@@ -1041,6 +1340,7 @@ drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int keycode)
 
     AssertTCB();
 
+    T((T_CALLED("win32con::drv_kyExist(%p, %d)"), TCB, keycode));
     res = bsearch(&key,
                  PropOf(TCB)->rmap,
                  (size_t) (N_INI + FKEYS),
@@ -1052,11 +1352,11 @@ drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int keycode)
        if (!(nKey & 0x8000))
            found = TRUE;
     }
-    return found;
+    returnCode(found);
 }
 
 static int
-drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool flag GCC_UNUSED)
+drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, int flag GCC_UNUSED)
 {
     SCREEN *sp;
     int code = ERR;
@@ -1064,14 +1364,15 @@ drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, bool flag GCC_UNUSED)
     AssertTCB();
     sp = TCB->csp;
 
+    T((T_CALLED("win32con::drv_kpad(%p, %d)"), TCB, flag));
     if (sp) {
        code = OK;
     }
-    return code;
+    returnCode(code);
 }
 
 static int
-drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int keycode, bool flag)
+drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int keycode, int flag)
 {
     int code = ERR;
     SCREEN *sp;
@@ -1083,6 +1384,7 @@ drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int keycode, bool flag)
     AssertTCB();
     SetSP();
 
+    T((T_CALLED("win32con::drv_keyok(%p, %d, %d)"), TCB, keycode, flag));
     if (sp) {
        res = bsearch(&key,
                      PropOf(TCB)->rmap,
@@ -1098,7 +1400,7 @@ drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int keycode, bool flag)
            *(LONG *) res = GenMap(vKey, nKey);
        }
     }
-    return code;
+    returnCode(code);
 }
 
 NCURSES_EXPORT_VAR (TERM_DRIVER) _nc_WIN_DRIVER = {
@@ -1119,6 +1421,7 @@ NCURSES_EXPORT_VAR (TERM_DRIVER) _nc_WIN_DRIVER = {
        drv_initcolor,          /* initcolor */
        drv_do_color,           /* docolor */
        drv_initmouse,          /* initmouse */
+       drv_testmouse,          /* testmouse */
        drv_setfilter,          /* setfilter */
        drv_hwlabel,            /* hwlabel */
        drv_hwlabelOnOff,       /* hwlabelOnOff */