1 /****************************************************************************
2 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer *
31 * and: Thomas E. Dickey *
32 ****************************************************************************/
35 * TODO - GetMousePos(POINT * result) from ntconio.c
36 * TODO - implement nodelay
39 #include <curses.priv.h>
40 #define CUR my_term.type.
42 MODULE_ID("$Id: win_driver.c,v 1.28 2014/03/08 21:44:53 tom Exp $")
44 #define WINMAGIC NCDRV_MAGIC(NCDRV_WINCONSOLE)
46 #define EXP_OPTIMIZE 0
48 #define okConsoleHandle(TCB) (TCB != 0 && !InvalidConsoleHandle(TCB->hdl))
50 #define AssertTCB() assert(TCB != 0 && (TCB->magic == WINMAGIC))
51 #define SetSP() assert(TCB->csp != 0); sp = TCB->csp; (void) sp
53 #define GenMap(vKey,key) MAKELONG(key, vKey)
55 #define AdjustY(p) ((p)->buffered ? 0 : (int) (p)->SBI.srWindow.Top)
57 static const LONG keylist[] =
59 GenMap(VK_PRIOR, KEY_PPAGE),
60 GenMap(VK_NEXT, KEY_NPAGE),
61 GenMap(VK_END, KEY_END),
62 GenMap(VK_HOME, KEY_HOME),
63 GenMap(VK_LEFT, KEY_LEFT),
64 GenMap(VK_UP, KEY_UP),
65 GenMap(VK_RIGHT, KEY_RIGHT),
66 GenMap(VK_DOWN, KEY_DOWN),
67 GenMap(VK_DELETE, KEY_DC),
68 GenMap(VK_INSERT, KEY_IC)
70 #define N_INI ((int)(sizeof(keylist)/sizeof(keylist[0])))
72 #define MAPSIZE (FKEYS + N_INI)
75 typedef struct props {
76 CONSOLE_SCREEN_BUFFER_INFO SBI;
82 bool buffered; /* normally allocate console-buffer */
83 bool window_only; /* ..if not, we save buffer or window-only */
85 CHAR_INFO *save_screen;
88 #define PropOf(TCB) ((Properties*)TCB->prop)
91 _nc_mingw_ioctl(int fd GCC_UNUSED,
92 long int request GCC_UNUSED,
93 struct termios *arg GCC_UNUSED)
97 fprintf(stderr, "TERMINFO currently not supported on Windows.\n");
102 MapColor(bool fore, int color)
104 static const int _cmap[] =
105 {0, 4, 2, 6, 1, 5, 3, 7};
107 if (color < 0 || color > 7)
117 MapAttr(TERMINAL_CONTROL_BLOCK * TCB, WORD res, attr_t ch)
126 if (p > 0 && p < NUMPAIRS && TCB != 0 && sp != 0) {
128 a = PropOf(TCB)->pairs[p];
129 res = (WORD) ((res & 0xff00) | a);
133 if (ch & A_REVERSE) {
134 res = (WORD) ((res & 0xff00) |
135 (((res & 0x07) << 4) |
136 ((res & 0x70) >> 4)));
139 if (ch & A_STANDOUT) {
140 res = (WORD) ((res & 0xff00) |
141 (((res & 0x07) << 4) |
142 ((res & 0x70) >> 4)) |
143 BACKGROUND_INTENSITY);
147 res |= FOREGROUND_INTENSITY;
150 res |= BACKGROUND_INTENSITY;
155 #if USE_WIDEC_SUPPORT
157 * TODO: support surrogate pairs
158 * TODO: support combining characters
160 * TODO: check wcwidth of base character, fill if needed for double-width
161 * TODO: _nc_wacs should be part of sp.
164 con_write16(TERMINAL_CONTROL_BLOCK * TCB, int y, int x, cchar_t *str, int limit)
173 Properties *p = PropOf(TCB);
179 for (i = actual = 0; i < limit; i++) {
183 ci[actual].Char.UnicodeChar = CharOf(ch);
184 ci[actual].Attributes = MapAttr(TCB,
185 PropOf(TCB)->SBI.wAttributes,
187 if (AttrOf(ch) & A_ALTCHARSET) {
189 int which = CharOf(ch);
192 && CharOf(_nc_wacs[which]) != 0) {
193 ci[actual].Char.UnicodeChar = CharOf(_nc_wacs[which]);
195 ci[actual].Char.UnicodeChar = ' ';
204 siz.X = (short) actual;
207 rec.Left = (short) x;
208 rec.Top = (SHORT) (y + AdjustY(p));
209 rec.Right = (short) (x + limit - 1);
210 rec.Bottom = rec.Top;
212 return WriteConsoleOutputW(TCB->hdl, ci, siz, loc, &rec);
214 #define con_write(tcb, y, x, str, n) con_write16(tcb, y, x, str, n)
217 con_write8(TERMINAL_CONTROL_BLOCK * TCB, int y, int x, chtype *str, int n)
230 for (i = 0; i < n; i++) {
232 ci[i].Char.AsciiChar = ChCharOf(ch);
233 ci[i].Attributes = MapAttr(TCB,
234 PropOf(TCB)->SBI.wAttributes,
236 if (ChAttrOf(ch) & A_ALTCHARSET) {
238 ci[i].Char.AsciiChar =
239 ChCharOf(NCURSES_SP_NAME(_nc_acs_char) (sp, ChCharOf(ch)));
248 rec.Left = (short) x;
250 rec.Right = (short) (x + n - 1);
251 rec.Bottom = rec.Top;
253 return WriteConsoleOutput(TCB->hdl, ci, siz, loc, &rec);
255 #define con_write(tcb, y, x, str, n) con_write8(tcb, y, x, str, n)
260 * Comparing new/current screens, determine the last column-index for a change
261 * beginning on the given row,col position. Unlike a serial terminal, there is
262 * no cost for "moving" the "cursor" on the line as we update it.
265 find_end_of_change(SCREEN *sp, int row, int col)
268 struct ldat *curdat = CurScreen(sp)->_line + row;
269 struct ldat *newdat = NewScreen(sp)->_line + row;
271 while (col <= newdat->lastchar) {
272 #if USE_WIDEC_SUPPORT
273 if (isWidecExt(curdat->text[col]) || isWidecExt(newdat->text[col])) {
275 } else if (memcmp(&curdat->text[col],
277 sizeof(curdat->text[0]))) {
283 if (curdat->text[col] != newdat->text[col]) {
295 * Given a row,col position at the end of a change-chunk, look for the
296 * beginning of the next change-chunk.
299 find_next_change(SCREEN *sp, int row, int col)
301 struct ldat *curdat = CurScreen(sp)->_line + row;
302 struct ldat *newdat = NewScreen(sp)->_line + row;
303 int result = newdat->lastchar + 1;
305 while (++col <= newdat->lastchar) {
306 #if USE_WIDEC_SUPPORT
307 if (isWidecExt(curdat->text[col]) != isWidecExt(newdat->text[col])) {
310 } else if (memcmp(&curdat->text[col],
312 sizeof(curdat->text[0]))) {
317 if (curdat->text[col] != newdat->text[col]) {
326 #define EndChange(first) \
327 find_end_of_change(sp, y, first)
328 #define NextChange(last) \
329 find_next_change(sp, y, last)
331 #endif /* EXP_OPTIMIZE */
333 #define MARK_NOCHANGE(win,row) \
334 win->_line[row].firstchar = _NOCHANGE; \
335 win->_line[row].lastchar = _NOCHANGE
338 selectActiveHandle(TERMINAL_CONTROL_BLOCK * TCB)
340 if (PropOf(TCB)->lastOut != TCB->hdl) {
341 PropOf(TCB)->lastOut = TCB->hdl;
342 SetConsoleActiveScreenBuffer(PropOf(TCB)->lastOut);
347 restore_original_screen(TERMINAL_CONTROL_BLOCK * TCB)
350 SMALL_RECT writeRegion;
351 Properties *p = PropOf(TCB);
354 if (p->window_only) {
355 writeRegion.Top = p->SBI.srWindow.Top;
356 writeRegion.Left = p->SBI.srWindow.Left;
357 writeRegion.Bottom = p->SBI.srWindow.Bottom;
358 writeRegion.Right = p->SBI.srWindow.Right;
359 T(("... restoring window"));
362 writeRegion.Left = 0;
363 writeRegion.Bottom = (SHORT) (p->SBI.dwSize.Y - 1);
364 writeRegion.Right = (SHORT) (p->SBI.dwSize.X - 1);
365 T(("... restoring entire buffer"));
368 bufferCoord.X = bufferCoord.Y = 0;
370 if (WriteConsoleOutput(TCB->hdl,
376 mvcur(-1, -1, LINES - 2, 0);
378 T(("... restore original screen contents %s", result ? "ok" : "err"));
383 drv_doupdate(TERMINAL_CONTROL_BLOCK * TCB)
386 int y, nonempty, n, x0, x1, Width, Height;
392 T((T_CALLED("win32con::drv_doupdate(%p)"), TCB));
393 if (okConsoleHandle(TCB)) {
395 Width = screen_columns(sp);
396 Height = screen_lines(sp);
397 nonempty = min(Height, NewScreen(sp)->_maxy + 1);
399 if ((CurScreen(sp)->_clear || NewScreen(sp)->_clear)) {
401 #if USE_WIDEC_SUPPORT
402 cchar_t empty[Width];
408 for (x = 0; x < Width; x++)
409 setcchar(&empty[x], blank, 0, 0, 0);
413 for (x = 0; x < Width; x++)
417 for (y = 0; y < nonempty; y++) {
418 con_write(TCB, y, 0, empty, Width);
420 CurScreen(sp)->_line[y].text,
421 (size_t) Width * sizeof(empty[0]));
423 CurScreen(sp)->_clear = FALSE;
424 NewScreen(sp)->_clear = FALSE;
425 touchwin(NewScreen(sp));
428 for (y = 0; y < nonempty; y++) {
429 x0 = NewScreen(sp)->_line[y].firstchar;
430 if (x0 != _NOCHANGE) {
433 int limit = NewScreen(sp)->_line[y].lastchar;
434 while ((x1 = EndChange(x0)) <= limit) {
435 while ((x2 = NextChange(x1)) <= limit && x2 <= (x1 + 2)) {
439 memcpy(&CurScreen(sp)->_line[y].text[x0],
440 &NewScreen(sp)->_line[y].text[x0],
441 n * sizeof(CurScreen(sp)->_line[y].text[x0]));
445 &CurScreen(sp)->_line[y].text[x0], n);
449 /* mark line changed successfully */
450 if (y <= NewScreen(sp)->_maxy) {
451 MARK_NOCHANGE(NewScreen(sp), y);
453 if (y <= CurScreen(sp)->_maxy) {
454 MARK_NOCHANGE(CurScreen(sp), y);
457 x1 = NewScreen(sp)->_line[y].lastchar;
460 memcpy(&CurScreen(sp)->_line[y].text[x0],
461 &NewScreen(sp)->_line[y].text[x0],
462 (size_t) n * sizeof(CurScreen(sp)->_line[y].text[x0]));
466 &CurScreen(sp)->_line[y].text[x0], n);
468 /* mark line changed successfully */
469 if (y <= NewScreen(sp)->_maxy) {
470 MARK_NOCHANGE(NewScreen(sp), y);
472 if (y <= CurScreen(sp)->_maxy) {
473 MARK_NOCHANGE(CurScreen(sp), y);
480 /* put everything back in sync */
481 for (y = nonempty; y <= NewScreen(sp)->_maxy; y++) {
482 MARK_NOCHANGE(NewScreen(sp), y);
484 for (y = nonempty; y <= CurScreen(sp)->_maxy; y++) {
485 MARK_NOCHANGE(CurScreen(sp), y);
488 if (!NewScreen(sp)->_leaveok) {
489 CurScreen(sp)->_curx = NewScreen(sp)->_curx;
490 CurScreen(sp)->_cury = NewScreen(sp)->_cury;
492 TCB->drv->td_hwcur(TCB,
494 CurScreen(sp)->_cury, CurScreen(sp)->_curx);
496 selectActiveHandle(TCB);
503 drv_CanHandle(TERMINAL_CONTROL_BLOCK * TCB,
505 int *errret GCC_UNUSED)
509 T((T_CALLED("win32con::drv_CanHandle(%p)"), TCB));
514 TCB->magic = WINMAGIC;
515 if (*tname == 0 || *tname == 0 || *tname == '#') {
522 #if (NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP)
523 status = _nc_setup_tinfo(tname, &my_term.type);
527 if (status != TGETENT_YES) {
528 const TERMTYPE *fallback = _nc_fallback(tname);
531 my_term.type = *fallback;
532 status = TGETENT_YES;
533 } else if (!strcmp(tname, "unknown")) {
537 if (status == TGETENT_YES) {
538 if (generic_type || hard_copy)
544 if ((TCB->term.type.Booleans) == 0) {
545 _nc_init_termtype(&(TCB->term.type));
553 drv_dobeepflash(TERMINAL_CONTROL_BLOCK * TCB,
554 int beepFlag GCC_UNUSED)
566 drv_print(TERMINAL_CONTROL_BLOCK * TCB,
567 char *data GCC_UNUSED,
579 drv_defaultcolors(TERMINAL_CONTROL_BLOCK * TCB,
593 get_SBI(TERMINAL_CONTROL_BLOCK * TCB)
596 Properties *p = PropOf(TCB);
597 if (GetConsoleScreenBufferInfo(TCB->hdl, &(p->SBI))) {
598 T(("GetConsoleScreenBufferInfo"));
599 T(("... buffer(X:%d Y:%d)",
602 T(("... window(X:%d Y:%d)",
603 p->SBI.dwMaximumWindowSize.X,
604 p->SBI.dwMaximumWindowSize.Y));
605 T(("... cursor(X:%d Y:%d)",
606 p->SBI.dwCursorPosition.X,
607 p->SBI.dwCursorPosition.Y));
608 T(("... display(Top:%d Bottom:%d Left:%d Right:%d)",
610 p->SBI.srWindow.Bottom,
611 p->SBI.srWindow.Left,
612 p->SBI.srWindow.Right));
617 p->origin.X = p->SBI.srWindow.Left;
618 p->origin.Y = p->SBI.srWindow.Top;
622 T(("GetConsoleScreenBufferInfo ERR"));
628 drv_setcolor(TERMINAL_CONTROL_BLOCK * TCB,
631 int (*outc) (SCREEN *, int) GCC_UNUSED)
635 if (okConsoleHandle(TCB) &&
637 WORD a = MapColor(fore, color);
638 a |= (WORD) ((PropOf(TCB)->SBI.wAttributes) & (fore ? 0xfff8 : 0xff8f));
639 SetConsoleTextAttribute(TCB->hdl, a);
645 drv_rescol(TERMINAL_CONTROL_BLOCK * TCB)
650 if (okConsoleHandle(TCB)) {
651 WORD a = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN;
652 SetConsoleTextAttribute(TCB->hdl, a);
660 drv_rescolors(TERMINAL_CONTROL_BLOCK * TCB)
672 drv_size(TERMINAL_CONTROL_BLOCK * TCB, int *Lines, int *Cols)
678 T((T_CALLED("win32con::drv_size(%p)"), TCB));
680 if (okConsoleHandle(TCB) &&
684 if (PropOf(TCB)->buffered) {
685 *Lines = (int) (PropOf(TCB)->SBI.dwSize.Y);
686 *Cols = (int) (PropOf(TCB)->SBI.dwSize.X);
688 *Lines = (int) (PropOf(TCB)->SBI.srWindow.Bottom + 1 -
689 PropOf(TCB)->SBI.srWindow.Top);
690 *Cols = (int) (PropOf(TCB)->SBI.srWindow.Right + 1 -
691 PropOf(TCB)->SBI.srWindow.Left);
699 drv_setsize(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED,
708 drv_sgmode(TERMINAL_CONTROL_BLOCK * TCB, int setFlag, TTY * buf)
716 if (TCB == 0 || buf == NULL)
720 iflag = buf->c_iflag;
721 lflag = buf->c_lflag;
723 GetConsoleMode(TCB->inp, &dwFlag);
726 dwFlag |= ENABLE_LINE_INPUT;
728 dwFlag &= (DWORD) (~ENABLE_LINE_INPUT);
731 dwFlag |= ENABLE_ECHO_INPUT;
733 dwFlag &= (DWORD) (~ENABLE_ECHO_INPUT);
736 dwFlag |= ENABLE_PROCESSED_INPUT;
738 dwFlag &= (DWORD) (~ENABLE_PROCESSED_INPUT);
740 dwFlag |= ENABLE_MOUSE_INPUT;
742 buf->c_iflag = iflag;
743 buf->c_lflag = lflag;
744 SetConsoleMode(TCB->inp, dwFlag);
745 TCB->term.Nttyb = *buf;
747 iflag = TCB->term.Nttyb.c_iflag;
748 lflag = TCB->term.Nttyb.c_lflag;
749 GetConsoleMode(TCB->inp, &dwFlag);
751 if (dwFlag & ENABLE_LINE_INPUT)
754 lflag &= (tcflag_t) (~ICANON);
756 if (dwFlag & ENABLE_ECHO_INPUT)
759 lflag &= (tcflag_t) (~ECHO);
761 if (dwFlag & ENABLE_PROCESSED_INPUT)
764 iflag &= (tcflag_t) (~BRKINT);
766 TCB->term.Nttyb.c_iflag = iflag;
767 TCB->term.Nttyb.c_lflag = lflag;
769 *buf = TCB->term.Nttyb;
775 drv_mode(TERMINAL_CONTROL_BLOCK * TCB, int progFlag, int defFlag)
778 TERMINAL *_term = (TERMINAL *) TCB;
784 T((T_CALLED("win32con::drv_mode(%p, prog=%d, def=%d)"), TCB, progFlag, defFlag));
785 PropOf(TCB)->progMode = progFlag;
786 PropOf(TCB)->lastOut = progFlag ? TCB->hdl : TCB->out;
787 SetConsoleActiveScreenBuffer(PropOf(TCB)->lastOut);
789 if (progFlag) /* prog mode */ {
791 if ((drv_sgmode(TCB, FALSE, &(_term->Nttyb)) == OK)) {
792 _term->Nttyb.c_oflag &= (tcflag_t) (~OFLAGS_TABS);
796 /* reset_prog_mode */
797 if (drv_sgmode(TCB, TRUE, &(_term->Nttyb)) == OK) {
800 _nc_keypad(sp, TRUE);
805 } else { /* shell mode */
808 if (drv_sgmode(TCB, FALSE, &(_term->Ottyb)) == OK) {
812 /* reset_shell_mode */
814 _nc_keypad(sp, FALSE);
815 NCURSES_SP_NAME(_nc_flush) (sp);
817 code = drv_sgmode(TCB, TRUE, &(_term->Ottyb));
818 if (!PropOf(TCB)->buffered) {
819 if (!restore_original_screen(TCB))
829 drv_screen_init(SCREEN *sp GCC_UNUSED)
834 drv_wrap(SCREEN *sp GCC_UNUSED)
839 rkeycompare(const void *el1, const void *el2)
841 WORD key1 = (LOWORD((*((const LONG *) el1)))) & 0x7fff;
842 WORD key2 = (LOWORD((*((const LONG *) el2)))) & 0x7fff;
844 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
848 keycompare(const void *el1, const void *el2)
850 WORD key1 = HIWORD((*((const LONG *) el1)));
851 WORD key2 = HIWORD((*((const LONG *) el2)));
853 return ((key1 < key2) ? -1 : ((key1 == key2) ? 0 : 1));
857 MapKey(TERMINAL_CONTROL_BLOCK * TCB, WORD vKey)
861 LONG key = GenMap(vKey, 0);
868 (size_t) (N_INI + FKEYS),
872 key = *((LONG *) res);
874 code = (int) (nKey & 0x7fff);
882 drv_release(TERMINAL_CONTROL_BLOCK * TCB)
884 T((T_CALLED("win32con::drv_release(%p)"), TCB));
894 * Attempt to save the screen contents. PDCurses does this if
895 * PDC_RESTORE_SCREEN is set, giving the same visual appearance on restoration
896 * as if the library had allocated a console buffer.
899 save_original_screen(TERMINAL_CONTROL_BLOCK * TCB)
902 Properties *p = PropOf(TCB);
905 SMALL_RECT readRegion;
908 bufferSize.X = p->SBI.dwSize.X;
909 bufferSize.Y = p->SBI.dwSize.Y;
910 want = (size_t) (bufferSize.X * bufferSize.Y);
912 if ((p->save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
913 bufferCoord.X = bufferCoord.Y = 0;
917 readRegion.Bottom = (SHORT) (bufferSize.Y - 1);
918 readRegion.Right = (SHORT) (bufferSize.X - 1);
920 T(("... reading console buffer %dx%d into %d,%d - %d,%d at %d,%d",
921 bufferSize.Y, bufferSize.X,
929 if (ReadConsoleOutput(TCB->hdl,
936 T((" error %#lx", (unsigned long) GetLastError()));
937 FreeAndNull(p->save_screen);
939 bufferSize.X = (SHORT) (p->SBI.srWindow.Right
940 - p->SBI.srWindow.Left + 1);
941 bufferSize.Y = (SHORT) (p->SBI.srWindow.Bottom
942 - p->SBI.srWindow.Top + 1);
943 want = (size_t) (bufferSize.X * bufferSize.Y);
945 if ((p->save_screen = malloc(want * sizeof(CHAR_INFO))) != 0) {
946 bufferCoord.X = bufferCoord.Y = 0;
948 readRegion.Top = p->SBI.srWindow.Top;
949 readRegion.Left = p->SBI.srWindow.Left;
950 readRegion.Bottom = p->SBI.srWindow.Bottom;
951 readRegion.Right = p->SBI.srWindow.Right;
953 T(("... reading console window %dx%d into %d,%d - %d,%d at %d,%d",
954 bufferSize.Y, bufferSize.X,
962 if (ReadConsoleOutput(TCB->hdl,
968 p->window_only = TRUE;
970 T((" error %#lx", (unsigned long) GetLastError()));
976 T(("... save original screen contents %s", result ? "ok" : "err"));
981 drv_init(TERMINAL_CONTROL_BLOCK * TCB)
985 T((T_CALLED("win32con::drv_init(%p)"), TCB));
990 BOOL b = AllocConsole();
993 bool buffered = TRUE;
996 b = AttachConsole(ATTACH_PARENT_PROCESS);
998 TCB->inp = GetStdHandle(STD_INPUT_HANDLE);
999 TCB->out = GetStdHandle(STD_OUTPUT_HANDLE);
1001 if (getenv("NCGDB") || getenv("NCURSES_CONSOLE2")) {
1002 TCB->hdl = TCB->out;
1005 TCB->hdl = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
1008 CONSOLE_TEXTMODE_BUFFER,
1012 if (InvalidConsoleHandle(TCB->hdl)) {
1014 } else if ((TCB->prop = typeCalloc(Properties, 1)) != 0) {
1015 PropOf(TCB)->buffered = buffered;
1016 PropOf(TCB)->window_only = FALSE;
1017 if (!get_SBI(TCB)) {
1018 FreeAndNull(TCB->prop); /* force error in drv_size */
1022 if (!save_original_screen(TCB)) {
1023 FreeAndNull(TCB->prop); /* force error in drv_size */
1029 TCB->info.initcolor = TRUE;
1030 TCB->info.canchange = FALSE;
1031 TCB->info.hascolor = TRUE;
1032 TCB->info.caninit = TRUE;
1034 TCB->info.maxpairs = NUMPAIRS;
1035 TCB->info.maxcolors = 8;
1036 TCB->info.numlabels = 0;
1037 TCB->info.labelwidth = 0;
1038 TCB->info.labelheight = 0;
1039 TCB->info.nocolorvideo = 1;
1040 TCB->info.tabsize = 8;
1042 if (GetNumberOfConsoleMouseButtons(&num_buttons)) {
1043 T(("mouse has %ld buttons", num_buttons));
1044 TCB->info.numbuttons = (int) num_buttons;
1046 TCB->info.numbuttons = 1;
1049 TCB->info.defaultPalette = _nc_cga_palette;
1051 for (i = 0; i < (N_INI + FKEYS); i++) {
1053 PropOf(TCB)->rmap[i] = PropOf(TCB)->map[i] = (DWORD) keylist[i];
1055 PropOf(TCB)->rmap[i] = PropOf(TCB)->map[i] =
1056 (DWORD) GenMap((VK_F1 + (i - N_INI)), (KEY_F(1) + (i - N_INI)));
1058 qsort(PropOf(TCB)->map,
1062 qsort(PropOf(TCB)->rmap,
1067 a = MapColor(true, COLOR_WHITE) | MapColor(false, COLOR_BLACK);
1068 for (i = 0; i < NUMPAIRS; i++)
1069 PropOf(TCB)->pairs[i] = a;
1075 drv_initpair(TERMINAL_CONTROL_BLOCK * TCB,
1085 if ((pair > 0) && (pair < NUMPAIRS) && (f >= 0) && (f < 8)
1086 && (b >= 0) && (b < 8)) {
1087 PropOf(TCB)->pairs[pair] = MapColor(true, f) | MapColor(false, b);
1092 drv_initcolor(TERMINAL_CONTROL_BLOCK * TCB,
1093 int color GCC_UNUSED,
1105 drv_do_color(TERMINAL_CONTROL_BLOCK * TCB,
1106 int old_pair GCC_UNUSED,
1107 int pair GCC_UNUSED,
1108 int reverse GCC_UNUSED,
1109 int (*outc) (SCREEN *, int) GCC_UNUSED
1119 drv_initmouse(TERMINAL_CONTROL_BLOCK * TCB)
1126 sp->_mouse_type = M_TERM_DRIVER;
1130 drv_testmouse(TERMINAL_CONTROL_BLOCK * TCB, int delay)
1138 if (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
1141 rc = TCBOf(sp)->drv->td_twait(TCBOf(sp),
1145 EVENTLIST_2nd(evl));
1152 drv_mvcur(TERMINAL_CONTROL_BLOCK * TCB,
1153 int yold GCC_UNUSED, int xold GCC_UNUSED,
1157 if (okConsoleHandle(TCB)) {
1158 Properties *p = PropOf(TCB);
1161 loc.Y = (short) (y + AdjustY(p));
1162 SetConsoleCursorPosition(TCB->hdl, loc);
1169 drv_hwlabel(TERMINAL_CONTROL_BLOCK * TCB,
1170 int labnum GCC_UNUSED,
1171 char *text GCC_UNUSED)
1180 drv_hwlabelOnOff(TERMINAL_CONTROL_BLOCK * TCB,
1181 int OnFlag GCC_UNUSED)
1190 drv_conattr(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED)
1192 chtype res = A_NORMAL;
1193 res |= (A_BOLD | A_DIM | A_REVERSE | A_STANDOUT | A_COLOR);
1198 drv_setfilter(TERMINAL_CONTROL_BLOCK * TCB)
1207 drv_initacs(TERMINAL_CONTROL_BLOCK * TCB,
1208 chtype *real_map GCC_UNUSED,
1209 chtype *fake_map GCC_UNUSED)
1211 #define DATA(a,b) { a, b }
1216 DATA('a', 0xb1), /* ACS_CKBOARD */
1217 DATA('f', 0xf8), /* ACS_DEGREE */
1218 DATA('g', 0xf1), /* ACS_PLMINUS */
1219 DATA('j', 0xd9), /* ACS_LRCORNER */
1220 DATA('l', 0xda), /* ACS_ULCORNER */
1221 DATA('k', 0xbf), /* ACS_URCORNER */
1222 DATA('m', 0xc0), /* ACS_LLCORNER */
1223 DATA('n', 0xc5), /* ACS_PLUS */
1224 DATA('q', 0xc4), /* ACS_HLINE */
1225 DATA('t', 0xc3), /* ACS_LTEE */
1226 DATA('u', 0xb4), /* ACS_RTEE */
1227 DATA('v', 0xc1), /* ACS_BTEE */
1228 DATA('w', 0xc2), /* ACS_TTEE */
1229 DATA('x', 0xb3), /* ACS_VLINE */
1230 DATA('y', 0xf3), /* ACS_LEQUAL */
1231 DATA('z', 0xf2), /* ACS_GEQUAL */
1232 DATA('0', 0xdb), /* ACS_BLOCK */
1233 DATA('{', 0xe3), /* ACS_PI */
1234 DATA('}', 0x9c), /* ACS_STERLING */
1235 DATA(',', 0xae), /* ACS_LARROW */
1236 DATA('+', 0xaf), /* ACS_RARROW */
1237 DATA('~', 0xf9), /* ACS_BULLET */
1246 for (n = 0; n < SIZEOF(table); ++n) {
1247 real_map[table[n].acs_code] = (chtype) table[n].use_code | A_ALTCHARSET;
1249 sp->_screen_acs_map[table[n].acs_code] = TRUE;
1254 tdiff(FILETIME fstart, FILETIME fend)
1256 ULARGE_INTEGER ustart;
1257 ULARGE_INTEGER uend;
1260 ustart.LowPart = fstart.dwLowDateTime;
1261 ustart.HighPart = fstart.dwHighDateTime;
1262 uend.LowPart = fend.dwLowDateTime;
1263 uend.HighPart = fend.dwHighDateTime;
1265 diff = (uend.QuadPart - ustart.QuadPart) / 10000;
1270 Adjust(int milliseconds, int diff)
1272 if (milliseconds == INFINITY)
1273 return milliseconds;
1274 milliseconds -= diff;
1275 if (milliseconds < 0)
1277 return milliseconds;
1280 #define BUTTON_MASK (FROM_LEFT_1ST_BUTTON_PRESSED | \
1281 FROM_LEFT_2ND_BUTTON_PRESSED | \
1282 FROM_LEFT_3RD_BUTTON_PRESSED | \
1283 FROM_LEFT_4TH_BUTTON_PRESSED | \
1284 RIGHTMOST_BUTTON_PRESSED)
1287 decode_mouse(TERMINAL_CONTROL_BLOCK * TCB, int mask)
1295 if (mask & FROM_LEFT_1ST_BUTTON_PRESSED)
1296 result |= BUTTON1_PRESSED;
1297 if (mask & FROM_LEFT_2ND_BUTTON_PRESSED)
1298 result |= BUTTON2_PRESSED;
1299 if (mask & FROM_LEFT_3RD_BUTTON_PRESSED)
1300 result |= BUTTON3_PRESSED;
1301 if (mask & FROM_LEFT_4TH_BUTTON_PRESSED)
1302 result |= BUTTON4_PRESSED;
1304 if (mask & RIGHTMOST_BUTTON_PRESSED) {
1305 switch (TCB->info.numbuttons) {
1307 result |= BUTTON1_PRESSED;
1310 result |= BUTTON2_PRESSED;
1313 result |= BUTTON3_PRESSED;
1316 result |= BUTTON4_PRESSED;
1325 drv_twait(TERMINAL_CONTROL_BLOCK * TCB,
1329 EVENTLIST_2nd(_nc_eventlist * evl))
1332 INPUT_RECORD inp_rec;
1334 DWORD nRead = 0, rc = (DWORD) (-1);
1339 bool isImmed = (milliseconds == 0);
1341 #define CONSUME() ReadConsoleInput(TCB->inp,&inp_rec,1,&nRead)
1346 TR(TRACE_IEVENT, ("start twait: %d milliseconds, mode: %d",
1347 milliseconds, mode));
1349 if (milliseconds < 0)
1350 milliseconds = INFINITY;
1352 memset(&inp_rec, 0, sizeof(inp_rec));
1355 GetSystemTimeAsFileTime(&fstart);
1356 rc = WaitForSingleObject(TCB->inp, (DWORD) milliseconds);
1357 GetSystemTimeAsFileTime(&fend);
1358 diff = (int) tdiff(fstart, fend);
1359 milliseconds = Adjust(milliseconds, diff);
1361 if (!isImmed && milliseconds == 0)
1364 if (rc == WAIT_OBJECT_0) {
1366 b = GetNumberOfConsoleInputEvents(TCB->inp, &nRead);
1367 if (b && nRead > 0) {
1368 b = PeekConsoleInput(TCB->inp, &inp_rec, 1, &nRead);
1369 if (b && nRead > 0) {
1370 switch (inp_rec.EventType) {
1372 if (mode & TW_INPUT) {
1373 WORD vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
1374 char ch = inp_rec.Event.KeyEvent.uChar.AsciiChar;
1376 if (inp_rec.Event.KeyEvent.bKeyDown) {
1378 int nKey = MapKey(TCB, vk);
1379 if ((nKey < 0) || FALSE == sp->_keypad_on) {
1392 if (decode_mouse(TCB,
1393 (inp_rec.Event.MouseEvent.dwButtonState
1394 & BUTTON_MASK)) == 0) {
1396 } else if (mode & TW_MOUSE) {
1402 selectActiveHandle(TCB);
1410 if (rc != WAIT_TIMEOUT) {
1421 TR(TRACE_IEVENT, ("end twait: returned %d (%d), remaining time %d msec",
1422 code, errno, milliseconds));
1425 *timeleft = milliseconds;
1431 handle_mouse(TERMINAL_CONTROL_BLOCK * TCB, MOUSE_EVENT_RECORD mer)
1435 bool result = FALSE;
1440 sp->_drv_mouse_old_buttons = sp->_drv_mouse_new_buttons;
1441 sp->_drv_mouse_new_buttons = mer.dwButtonState & BUTTON_MASK;
1444 * We're only interested if the button is pressed or released.
1445 * FIXME: implement continuous event-tracking.
1447 if (sp->_drv_mouse_new_buttons != sp->_drv_mouse_old_buttons) {
1448 Properties *p = PropOf(TCB);
1450 memset(&work, 0, sizeof(work));
1452 if (sp->_drv_mouse_new_buttons) {
1454 work.bstate |= (mmask_t) decode_mouse(TCB, sp->_drv_mouse_new_buttons);
1458 /* cf: BUTTON_PRESSED, BUTTON_RELEASED */
1459 work.bstate |= (mmask_t) (decode_mouse(TCB,
1460 sp->_drv_mouse_old_buttons)
1466 work.x = mer.dwMousePosition.X;
1467 work.y = mer.dwMousePosition.Y - AdjustY(p);
1469 sp->_drv_mouse_fifo[sp->_drv_mouse_tail] = work;
1470 sp->_drv_mouse_tail += 1;
1477 drv_read(TERMINAL_CONTROL_BLOCK * TCB, int *buf)
1481 INPUT_RECORD inp_rec;
1490 memset(&inp_rec, 0, sizeof(inp_rec));
1492 T((T_CALLED("win32con::drv_read(%p)"), TCB));
1493 while ((b = ReadConsoleInput(TCB->inp, &inp_rec, 1, &nRead))) {
1494 if (b && nRead > 0) {
1495 if (inp_rec.EventType == KEY_EVENT) {
1496 if (!inp_rec.Event.KeyEvent.bKeyDown)
1498 *buf = (int) inp_rec.Event.KeyEvent.uChar.AsciiChar;
1499 vk = inp_rec.Event.KeyEvent.wVirtualKeyCode;
1501 if (sp->_keypad_on) {
1502 *buf = MapKey(TCB, vk);
1509 } else { /* *buf != 0 */
1512 } else if (inp_rec.EventType == MOUSE_EVENT) {
1513 if (handle_mouse(TCB, inp_rec.Event.MouseEvent)) {
1525 drv_nap(TERMINAL_CONTROL_BLOCK * TCB GCC_UNUSED, int ms)
1527 T((T_CALLED("win32con::drv_nap(%p, %d)"), TCB, ms));
1533 drv_kyExist(TERMINAL_CONTROL_BLOCK * TCB, int keycode)
1539 LONG key = GenMap(0, (WORD) keycode);
1546 T((T_CALLED("win32con::drv_kyExist(%p, %d)"), TCB, keycode));
1549 (size_t) (N_INI + FKEYS),
1553 key = *((LONG *) res);
1555 if (!(nKey & 0x8000))
1562 drv_kpad(TERMINAL_CONTROL_BLOCK * TCB, int flag GCC_UNUSED)
1570 T((T_CALLED("win32con::drv_kpad(%p, %d)"), TCB, flag));
1578 drv_keyok(TERMINAL_CONTROL_BLOCK * TCB, int keycode, int flag)
1585 LONG key = GenMap(0, (WORD) keycode);
1590 T((T_CALLED("win32con::drv_keyok(%p, %d, %d)"), TCB, keycode, flag));
1594 (size_t) (N_INI + FKEYS),
1598 key = *((LONG *) res);
1600 nKey = (LOWORD(key)) & 0x7fff;
1603 *(LONG *) res = GenMap(vKey, nKey);
1609 NCURSES_EXPORT_VAR (TERM_DRIVER) _nc_WIN_DRIVER = {
1611 drv_CanHandle, /* CanHandle */
1612 drv_init, /* init */
1613 drv_release, /* release */
1614 drv_size, /* size */
1615 drv_sgmode, /* sgmode */
1616 drv_conattr, /* conattr */
1617 drv_mvcur, /* hwcur */
1618 drv_mode, /* mode */
1619 drv_rescol, /* rescol */
1620 drv_rescolors, /* rescolors */
1621 drv_setcolor, /* color */
1622 drv_dobeepflash, /* DoBeepFlash */
1623 drv_initpair, /* initpair */
1624 drv_initcolor, /* initcolor */
1625 drv_do_color, /* docolor */
1626 drv_initmouse, /* initmouse */
1627 drv_testmouse, /* testmouse */
1628 drv_setfilter, /* setfilter */
1629 drv_hwlabel, /* hwlabel */
1630 drv_hwlabelOnOff, /* hwlabelOnOff */
1631 drv_doupdate, /* update */
1632 drv_defaultcolors, /* defaultcolors */
1633 drv_print, /* print */
1634 drv_size, /* getsize */
1635 drv_setsize, /* setsize */
1636 drv_initacs, /* initacs */
1637 drv_screen_init, /* scinit */
1638 drv_wrap, /* scexit */
1639 drv_twait, /* twait */
1640 drv_read, /* read */
1642 drv_kpad, /* kpad */
1643 drv_keyok, /* kyOk */
1644 drv_kyExist /* kyExist */