wineconsole: Properly manage window position (in wineconsole) from within wineserver.
[wine.git] / programs / wineconsole / curses.c
blob5038bcef15fbf33ec15b1d7730579a89e5774e1d
1 /*
2 * a GUI application for displaying a console
3 * (N)Curses back end
5 * Copyright 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 /* Known issues & FIXME:
23 * - not all key mapping functions have been written
24 * - allow dyn loading of curses library (extreme care should be taken for
25 * functions which can be implemented as macros)
26 * - finish buffer scrolling (mainly, need to decide of a nice way for
27 * requesting the UP/DOWN operations
28 * - Resizing (unix) terminal does not change (Win32) console size.
29 * - Initial console size comes from registry and not from terminal size.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #ifdef HAVE_POLL_H
39 # include <poll.h>
40 #endif
41 #ifdef HAVE_NCURSES_H
42 # include <ncurses.h>
43 #elif defined(HAVE_CURSES_H)
44 # include <curses.h>
45 #endif
46 /* avoid redefinition warnings */
47 #undef KEY_EVENT
48 #undef MOUSE_MOVED
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53 #include <windef.h>
54 #include <winbase.h>
55 #include <winnls.h>
56 #include "winecon_private.h"
58 #include "wine/library.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(curses);
63 #define PRIVATE(data) ((struct inner_data_curse*)((data)->private))
65 #if defined(SONAME_LIBCURSES) || defined(SONAME_LIBNCURSES)
67 #ifdef HAVE_NCURSES_H
68 # define CURSES_NAME "ncurses"
69 #else
70 # define CURSES_NAME "curses"
71 #endif
73 struct inner_data_curse
75 unsigned long initial_mouse_mask;
76 int sync_pipe[2];
77 HANDLE input_thread;
78 WINDOW* pad;
79 chtype* line;
80 int allow_scroll;
84 static void *nc_handle = NULL;
86 #ifdef initscr /* work around Solaris breakage */
87 #undef initscr
88 extern WINDOW *initscr(void);
89 #endif
91 #define MAKE_FUNCPTR(f) static typeof(f) * p_##f;
93 MAKE_FUNCPTR(curs_set)
94 MAKE_FUNCPTR(delwin)
95 MAKE_FUNCPTR(endwin)
96 #ifndef getmaxx
97 MAKE_FUNCPTR(getmaxx)
98 #endif
99 #ifndef getmaxy
100 MAKE_FUNCPTR(getmaxy)
101 #endif
102 MAKE_FUNCPTR(getmouse)
103 MAKE_FUNCPTR(has_colors)
104 MAKE_FUNCPTR(init_pair)
105 MAKE_FUNCPTR(initscr)
106 #ifndef intrflush
107 MAKE_FUNCPTR(intrflush)
108 #endif
109 MAKE_FUNCPTR(keypad)
110 MAKE_FUNCPTR(newpad)
111 #ifndef nodelay
112 MAKE_FUNCPTR(nodelay)
113 #endif
114 #ifndef noecho
115 MAKE_FUNCPTR(noecho)
116 #endif
117 MAKE_FUNCPTR(prefresh)
118 MAKE_FUNCPTR(raw)
119 MAKE_FUNCPTR(start_color)
120 MAKE_FUNCPTR(stdscr)
121 MAKE_FUNCPTR(waddchnstr)
122 MAKE_FUNCPTR(wmove)
123 MAKE_FUNCPTR(wgetch)
124 #ifdef HAVE_MOUSEMASK
125 MAKE_FUNCPTR(mouseinterval)
126 MAKE_FUNCPTR(mousemask)
127 #endif
129 #undef MAKE_FUNCPTR
131 /**********************************************************************/
133 static BOOL WCCURSES_bind_libcurses(void)
135 #ifdef SONAME_LIBNCURSES
136 static const char ncname[] = SONAME_LIBNCURSES;
137 #else
138 static const char ncname[] = SONAME_LIBCURSES;
139 #endif
141 nc_handle = wine_dlopen(ncname, RTLD_NOW, NULL, 0);
142 if(!nc_handle)
144 WINE_MESSAGE("Wine cannot find the " CURSES_NAME " library (%s).\n",
145 ncname);
146 return FALSE;
149 #define LOAD_FUNCPTR(f) \
150 if((p_##f = wine_dlsym(nc_handle, #f, NULL, 0)) == NULL) \
152 WINE_WARN("Can't find symbol %s\n", #f); \
153 goto sym_not_found; \
156 LOAD_FUNCPTR(curs_set)
157 LOAD_FUNCPTR(delwin)
158 LOAD_FUNCPTR(endwin)
159 #ifndef getmaxx
160 LOAD_FUNCPTR(getmaxx)
161 #endif
162 #ifndef getmaxy
163 LOAD_FUNCPTR(getmaxy)
164 #endif
165 LOAD_FUNCPTR(getmouse)
166 LOAD_FUNCPTR(has_colors)
167 LOAD_FUNCPTR(init_pair)
168 LOAD_FUNCPTR(initscr)
169 #ifndef intrflush
170 LOAD_FUNCPTR(intrflush)
171 #endif
172 LOAD_FUNCPTR(keypad)
173 LOAD_FUNCPTR(newpad)
174 #ifndef nodelay
175 LOAD_FUNCPTR(nodelay)
176 #endif
177 #ifndef noecho
178 LOAD_FUNCPTR(noecho)
179 #endif
180 LOAD_FUNCPTR(prefresh)
181 LOAD_FUNCPTR(raw)
182 LOAD_FUNCPTR(start_color)
183 LOAD_FUNCPTR(stdscr)
184 LOAD_FUNCPTR(waddchnstr)
185 LOAD_FUNCPTR(wmove)
186 LOAD_FUNCPTR(wgetch)
187 #ifdef HAVE_MOUSEMASK
188 LOAD_FUNCPTR(mouseinterval)
189 LOAD_FUNCPTR(mousemask)
190 #endif
192 #undef LOAD_FUNCPTR
194 return TRUE;
196 sym_not_found:
197 WINE_MESSAGE(
198 "Wine cannot find certain functions that it needs inside the "
199 CURSES_NAME "\nlibrary. To enable Wine to use " CURSES_NAME
200 " please upgrade your " CURSES_NAME "\nlibraries\n");
201 wine_dlclose(nc_handle, NULL, 0);
202 nc_handle = NULL;
203 return FALSE;
206 #define curs_set p_curs_set
207 #define delwin p_delwin
208 #define endwin p_endwin
209 #ifndef getmaxx
210 #define getmaxx p_getmaxx
211 #endif
212 #ifndef getmaxy
213 #define getmaxy p_getmaxy
214 #endif
215 #define getmouse p_getmouse
216 #define has_colors p_has_colors
217 #define init_pair p_init_pair
218 #define initscr p_initscr
219 #ifndef intrflush
220 #define intrflush p_intrflush
221 #endif
222 #define keypad p_keypad
223 #define mouseinterval p_mouseinterval
224 #define mousemask p_mousemask
225 #define newpad p_newpad
226 #ifndef nodelay
227 #define nodelay p_nodelay
228 #endif
229 #ifndef noecho
230 #define noecho p_noecho
231 #endif
232 #define prefresh p_prefresh
233 #define raw p_raw
234 #define start_color p_start_color
235 #define stdscr (*p_stdscr)
236 #define waddchnstr p_waddchnstr
237 #define wmove p_wmove
238 #define wgetch p_wgetch
240 /******************************************************************
241 * WCCURSES_ResizeScreenBuffer
245 static void WCCURSES_ResizeScreenBuffer(struct inner_data* data)
247 /* reallocate a new pad. next event would redraw the whole pad */
248 if (PRIVATE(data)->pad) delwin(PRIVATE(data)->pad);
249 PRIVATE(data)->pad = newpad(data->curcfg.sb_height, data->curcfg.sb_width);
250 if (!PRIVATE(data)->pad)
251 WINE_FIXME("Cannot create pad\n");
252 if (PRIVATE(data)->line)
253 PRIVATE(data)->line = HeapReAlloc(GetProcessHeap(), 0, PRIVATE(data)->line,
254 sizeof(chtype) * data->curcfg.sb_width);
255 else
256 PRIVATE(data)->line = HeapAlloc(GetProcessHeap(), 0,
257 sizeof(chtype) * data->curcfg.sb_width);
260 /******************************************************************
261 * WCCURSES_PosCursor
263 * Set a new position for the cursor (and refresh any modified part of our pad)
265 static void WCCURSES_PosCursor(const struct inner_data* data)
267 int scr_width;
268 int scr_height;
270 if (data->curcfg.cursor_visible &&
271 data->cursor.Y >= data->curcfg.win_pos.Y &&
272 data->cursor.Y < data->curcfg.win_pos.Y + data->curcfg.win_height &&
273 data->cursor.X >= data->curcfg.win_pos.X &&
274 data->cursor.X < data->curcfg.win_pos.X + data->curcfg.win_width)
276 if (curs_set(2) == ERR) curs_set(1);
277 wmove(PRIVATE(data)->pad, data->cursor.Y, data->cursor.X);
279 else
281 curs_set(0);
283 getmaxyx(stdscr, scr_height, scr_width);
284 prefresh(PRIVATE(data)->pad,
285 data->curcfg.win_pos.Y, data->curcfg.win_pos.X,
286 0, 0,
287 min(scr_height, data->curcfg.win_height) - 1,
288 min(scr_width, data->curcfg.win_width) - 1);
291 /******************************************************************
292 * WCCURSES_ShapeCursor
294 * Sets a new shape for the cursor
296 static void WCCURSES_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
298 /* we can't do much about the size... */
299 data->curcfg.cursor_size = size;
300 data->curcfg.cursor_visible = vis ? TRUE : FALSE;
301 WCCURSES_PosCursor(data);
304 /******************************************************************
305 * WCCURSES_ComputePositions
307 * Recomputes all the components (mainly scroll bars) positions
309 static void WCCURSES_ComputePositions(struct inner_data* data)
311 int x, y;
313 getmaxyx(stdscr, y, x);
314 if ((data->curcfg.win_height && y < data->curcfg.win_height) ||
315 (data->curcfg.win_width && x < data->curcfg.win_width))
317 SMALL_RECT pos;
319 WINE_WARN("Window too large (%dx%d), adjusting to curses' size (%dx%d)\n",
320 data->curcfg.win_width, data->curcfg.win_height, x, y);
321 pos.Left = pos.Top = 0;
322 pos.Right = x - 1; pos.Bottom = y - 1;
323 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);
324 return; /* we'll get called again upon event for new window size */
326 if (PRIVATE(data)->pad) WCCURSES_PosCursor(data);
329 /******************************************************************
330 * WCCURSES_SetTitle
332 * Sets the title to the wine console
334 static void WCCURSES_SetTitle(const struct inner_data* data)
336 WCHAR wbuf[256];
338 if (WINECON_GetConsoleTitle(data->hConIn, wbuf, sizeof(wbuf)/sizeof(WCHAR)))
340 char buffer[256];
342 WideCharToMultiByte(CP_UNIXCP, 0, wbuf, -1, buffer, sizeof(buffer),
343 NULL, NULL);
344 fputs("\033]2;", stdout);
345 fputs(buffer, stdout);
346 fputc('\a', stdout);
347 fflush(stdout);
351 /******************************************************************
352 * WCCURSES_Refresh
356 static void WCCURSES_Refresh(const struct inner_data* data, int tp, int bm)
358 unsigned int x;
359 int y;
360 CHAR_INFO* cell;
361 DWORD attr;
362 char ch;
364 for (y = tp; y <= bm; y++)
366 cell = &data->cells[y * data->curcfg.sb_width];
367 for (x = 0; x < data->curcfg.sb_width; x++)
369 WideCharToMultiByte(CP_UNIXCP, 0, &cell[x].Char.UnicodeChar, 1,
370 &ch, 1, NULL, NULL);
371 attr = ((BYTE)ch < 32) ? 32 : (BYTE)ch;
373 if (cell[x].Attributes & FOREGROUND_RED) attr |= COLOR_PAIR(COLOR_RED);
374 if (cell[x].Attributes & FOREGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE);
375 if (cell[x].Attributes & FOREGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN);
376 if (cell[x].Attributes & BACKGROUND_RED) attr |= COLOR_PAIR(COLOR_RED << 3);
377 if (cell[x].Attributes & BACKGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE << 3);
378 if (cell[x].Attributes & BACKGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN << 3);
380 if (cell[x].Attributes & FOREGROUND_INTENSITY) attr |= A_BOLD;
381 PRIVATE(data)->line[x] = attr;
383 mvwaddchnstr(PRIVATE(data)->pad, y, 0, PRIVATE(data)->line, data->curcfg.sb_width);
386 WCCURSES_PosCursor(data);
389 /******************************************************************
390 * WCCURSES_Scroll
394 static void WCCURSES_Scroll(struct inner_data* data, int pos, BOOL horz)
396 if (horz)
398 data->curcfg.win_pos.X = pos;
400 else
402 data->curcfg.win_pos.Y = pos;
404 WCCURSES_PosCursor(data);
407 /******************************************************************
408 * WCCURSES_SetFont
412 static void WCCURSES_SetFont(struct inner_data* data, const WCHAR* font,
413 unsigned height, unsigned weight)
415 /* FIXME: really not much to do ? */
418 /******************************************************************
419 * WCCURSES_ScrollV
423 static void WCCURSES_ScrollV(struct inner_data* data, int delta)
425 struct config_data cfg = data->curcfg;
427 cfg.win_pos.Y += delta;
428 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
429 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
430 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
431 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
433 WCCURSES_PosCursor(data);
434 WINECON_SetConfig(data, &cfg);
438 /* Ascii -> VK, generated by calling VkKeyScanA(i) */
439 static const int vkkeyscan_table[256] =
441 0,0,0,0,0,0,0,0,8,9,0,0,0,13,0,0,0,0,0,19,145,556,0,0,0,0,0,27,0,0,0,
442 0,32,305,478,307,308,309,311,222,313,304,312,443,188,189,190,191,48,
443 49,50,51,52,53,54,55,56,57,442,186,444,187,446,447,306,321,322,323,
444 324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
445 341,342,343,344,345,346,219,220,221,310,445,192,65,66,67,68,69,70,71,
446 72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,475,476,477,
447 448,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
448 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
449 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
450 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,400,0,0,0,0,0,0
453 static const int mapvkey_0[256] =
455 0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,69,58,0,0,0,0,0,0,1,0,0,
456 0,0,57,73,81,79,71,75,72,77,80,0,0,0,55,82,83,0,11,2,3,4,5,6,7,8,9,
457 10,0,0,0,0,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,
458 19,31,20,22,47,17,45,21,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,78,0,74,
459 0,53,59,60,61,62,63,64,65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
460 0,0,0,0,0,0,69,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
461 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,0,0,0,0,
462 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,76,96,0,0,0,0,0,0,0,0,
463 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
466 /******************************************************************
467 * WCCURSES_InitComplexChar
471 static inline void WCCURSES_InitComplexChar(INPUT_RECORD* ir, BOOL down, WORD vk, WORD kc, DWORD cks)
473 ir->EventType = KEY_EVENT;
474 ir->Event.KeyEvent.bKeyDown = down;
475 ir->Event.KeyEvent.wRepeatCount = 1;
477 ir->Event.KeyEvent.wVirtualScanCode = vk;
478 ir->Event.KeyEvent.wVirtualKeyCode = kc;
479 ir->Event.KeyEvent.dwControlKeyState = cks;
480 ir->Event.KeyEvent.uChar.UnicodeChar = 0;
483 /******************************************************************
484 * WCCURSES_FillSimpleChar
488 static unsigned WCCURSES_FillSimpleChar(INPUT_RECORD* ir, unsigned real_inchar)
490 unsigned vk;
491 unsigned inchar;
492 char ch;
493 unsigned numEvent = 0;
494 DWORD cks = 0;
496 switch (real_inchar)
498 case 9: inchar = real_inchar;
499 real_inchar = 27; /* so that we don't think key is ctrl- something */
500 break;
501 case 10: inchar = '\r';
502 real_inchar = 27; /* Fixme: so that we don't think key is ctrl- something */
503 break;
504 case 127: inchar = '\b';
505 break;
506 case 27:
507 /* we assume that ESC & and the second character are atomically
508 * generated otherwise, we'll have a race here. FIXME: This gives 1 sec. delay
509 * because curses looks for a second character.
511 if ((inchar = wgetch(stdscr)) != ERR)
513 /* we got a alt-something key... */
514 cks = LEFT_ALT_PRESSED;
516 else
517 inchar = 27;
518 break;
519 default:
520 inchar = real_inchar;
521 break;
523 if ((inchar & ~0xFF) != 0) WINE_FIXME("What a char (%u)\n", inchar);
524 vk = vkkeyscan_table[inchar];
525 if (vk & 0x0100)
526 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x2a, 0x10, SHIFT_PRESSED);
527 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
528 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x1d, 0x11, LEFT_CTRL_PRESSED);
529 if (vk & 0x0400)
530 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x38, 0x12, LEFT_ALT_PRESSED);
532 ir[numEvent].EventType = KEY_EVENT;
533 ir[numEvent].Event.KeyEvent.bKeyDown = 1;
534 ir[numEvent].Event.KeyEvent.wRepeatCount = 1;
535 ir[numEvent].Event.KeyEvent.dwControlKeyState = cks;
536 if (vk & 0x0100)
537 ir[numEvent].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
538 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
539 ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
540 if (vk & 0x0400)
541 ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
542 ir[numEvent].Event.KeyEvent.wVirtualKeyCode = vk;
543 ir[numEvent].Event.KeyEvent.wVirtualScanCode = mapvkey_0[vk & 0x00ff]; /* VirtualKeyCodes to ScanCode */
545 ch = inchar;
546 MultiByteToWideChar(CP_UNIXCP, 0,&ch,1,&ir[numEvent].Event.KeyEvent.uChar.UnicodeChar, 1);
547 ir[numEvent + 1] = ir[numEvent];
548 ir[numEvent + 1].Event.KeyEvent.bKeyDown = 0;
550 numEvent += 2;
552 if (vk & 0x0400)
553 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x38, 0x12, LEFT_ALT_PRESSED);
554 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
555 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x1d, 0x11, 0);
556 if (vk & 0x0100)
557 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x2a, 0x10, 0);
559 return numEvent;
562 /******************************************************************
563 * WCCURSES_FillComplexChar
567 static unsigned WCCURSES_FillComplexChar(INPUT_RECORD* ir, WORD vk, WORD kc, DWORD cks)
569 WCCURSES_InitComplexChar(&ir[0], 1, vk, kc, ENHANCED_KEY | cks);
570 WCCURSES_InitComplexChar(&ir[1], 0, vk, kc, ENHANCED_KEY | cks);
572 return 2;
575 /******************************************************************
576 * WCCURSES_FillMouse
580 static unsigned WCCURSES_FillMouse(INPUT_RECORD* ir)
582 #ifdef HAVE_MOUSEMASK
583 static unsigned bstate /* = 0 */;
584 static COORD pos /* = {0, 0} */;
586 MEVENT mevt;
588 if (getmouse(&mevt) == ERR)
589 return 0;
591 WINE_TRACE("[%u]: (%d, %d) %08lx\n",
592 mevt.id, mevt.x, mevt.y, (unsigned long)mevt.bstate);
594 /* macros to ease mapping ncurse button numbering to windows's one */
595 #define BTN1_BIT FROM_LEFT_1ST_BUTTON_PRESSED
596 #define BTN2_BIT RIGHTMOST_BUTTON_PRESSED
597 #define BTN3_BIT FROM_LEFT_2ND_BUTTON_PRESSED
598 #define BTN4_BIT 0 /* not done yet */
600 if (mevt.bstate & BUTTON1_PRESSED) bstate |= BTN1_BIT;
601 if (mevt.bstate & BUTTON1_RELEASED) bstate &= ~BTN1_BIT;
602 if (mevt.bstate & BUTTON2_PRESSED) bstate |= BTN2_BIT;
603 if (mevt.bstate & BUTTON2_RELEASED) bstate &= ~BTN2_BIT;
604 if (mevt.bstate & BUTTON3_PRESSED) bstate |= BTN3_BIT;
605 if (mevt.bstate & BUTTON3_RELEASED) bstate &= ~BTN3_BIT;
607 ir->EventType = MOUSE_EVENT;
608 ir->Event.MouseEvent.dwMousePosition.X = mevt.x;
609 ir->Event.MouseEvent.dwMousePosition.Y = mevt.y;
611 ir->Event.MouseEvent.dwButtonState = bstate;
613 /* partial conversion */
614 ir->Event.MouseEvent.dwControlKeyState = 0;
615 if (mevt.bstate & BUTTON_SHIFT) ir->Event.MouseEvent.dwControlKeyState |= SHIFT_PRESSED;
616 /* choose to map to left ctrl... could use both ? */
617 if (mevt.bstate & BUTTON_CTRL) ir->Event.MouseEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
618 /* choose to map to left alt... could use both ? */
619 if (mevt.bstate & BUTTON_ALT) ir->Event.MouseEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
620 /* FIXME: unsupported yet flags: CAPSLOCK_ON, ENHANCED_KEY (??), NUMLOCK_ON, SCROLLLOCK_ON
621 * could be reported from the key events...
624 ir->Event.MouseEvent.dwEventFlags = 0;
625 /* FIXME: we no longer generate double click events */
627 if (!(mevt.bstate & (BUTTON1_PRESSED|BUTTON1_RELEASED|BUTTON2_PRESSED|BUTTON2_RELEASED|BUTTON3_PRESSED|BUTTON3_RELEASED)) &&
628 (mevt.x != pos.X || mevt.y != pos.Y))
630 ir->Event.MouseEvent.dwEventFlags |= MOUSE_MOVED;
632 pos.X = mevt.x; pos.Y = mevt.y;
634 return 1;
635 #else
636 return 0;
637 #endif
640 /******************************************************************
641 * WCCURSES_FillCode
645 static unsigned WCCURSES_FillCode(struct inner_data* data, INPUT_RECORD* ir, int inchar)
647 unsigned numEvent = 0;
649 switch (inchar)
651 case KEY_BREAK:
652 goto notFound;
653 case KEY_DOWN:
654 numEvent = WCCURSES_FillComplexChar(ir, 0x50, 0x28, 0);
655 break;
656 case KEY_UP:
657 numEvent = WCCURSES_FillComplexChar(ir, 0x48, 0x26, 0);
658 break;
659 case KEY_LEFT:
660 numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, 0);
661 break;
662 case KEY_RIGHT:
663 numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, 0);
664 break;
665 case KEY_HOME:
666 numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, 0);
667 break;
668 case KEY_BACKSPACE:
669 numEvent = WCCURSES_FillSimpleChar(ir, 127);
670 break;
672 case KEY_F0: /* up to F63 */
673 goto notFound;
675 case KEY_F( 1):
676 case KEY_F( 2):
677 case KEY_F( 3):
678 case KEY_F( 4):
679 case KEY_F( 5):
680 case KEY_F( 6):
681 case KEY_F( 7):
682 case KEY_F( 8):
683 case KEY_F( 9):
684 case KEY_F(10):
685 numEvent = WCCURSES_FillComplexChar(ir, 0x3b + inchar - KEY_F(1),
686 0x70 + inchar - KEY_F(1), 0);
687 break;
688 case KEY_F(11):
689 case KEY_F(12):
690 if (PRIVATE(data)->allow_scroll)
692 WCCURSES_ScrollV(data, inchar == KEY_F(11) ? 8 : -8);
694 else
696 numEvent = WCCURSES_FillComplexChar(ir, 0xd9 + inchar - KEY_F(11),
697 0x7a + inchar - KEY_F(11), 0);
699 break;
701 case KEY_DL:
702 case KEY_IL:
703 goto notFound;
705 case KEY_DC:
706 numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, 0);
707 break;
708 case KEY_IC:
709 numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, 0);
710 break;
712 case KEY_EIC:
713 case KEY_CLEAR:
714 case KEY_EOS:
715 case KEY_EOL:
716 case KEY_SF:
717 case KEY_SR:
718 goto notFound;
720 case KEY_NPAGE:
721 numEvent = WCCURSES_FillComplexChar(ir, 0x51, 0x22, 0);
722 break;
723 case KEY_PPAGE:
724 numEvent = WCCURSES_FillComplexChar(ir, 0x49, 0x21, 0);
725 break;
727 case KEY_STAB:
728 case KEY_CTAB:
729 case KEY_CATAB:
730 case KEY_ENTER:
731 case KEY_SRESET:
732 case KEY_RESET:
733 case KEY_PRINT:
734 case KEY_LL:
735 case KEY_A1:
736 case KEY_A3:
737 case KEY_B2:
738 case KEY_C1:
739 case KEY_C3:
740 goto notFound;
741 case KEY_BTAB: /* shift tab */
742 numEvent = WCCURSES_FillSimpleChar(ir, 0x9);
743 ir[0].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
744 ir[1].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
745 if (numEvent != 2) WINE_ERR("FillsimpleChar has changed\n");
746 break;
748 case KEY_BEG:
749 case KEY_CANCEL:
750 case KEY_CLOSE:
751 case KEY_COMMAND:
752 case KEY_COPY:
753 case KEY_CREATE:
754 goto notFound;
756 case KEY_END:
757 numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, 0);
758 break;
760 case KEY_EXIT:
761 case KEY_FIND:
762 case KEY_HELP:
763 case KEY_MARK:
764 case KEY_MESSAGE:
765 goto notFound;
767 case KEY_MOUSE:
768 numEvent = WCCURSES_FillMouse(ir);
769 break;
771 case KEY_MOVE:
772 case KEY_NEXT:
773 case KEY_OPEN:
774 case KEY_OPTIONS:
775 case KEY_PREVIOUS:
776 case KEY_REDO:
777 case KEY_REFERENCE:
778 case KEY_REFRESH:
779 case KEY_REPLACE:
780 case KEY_RESTART:
781 case KEY_RESUME:
782 case KEY_SAVE:
783 case KEY_SBEG:
784 case KEY_SCANCEL:
785 case KEY_SCOMMAND:
786 case KEY_SCOPY:
787 case KEY_SCREATE:
788 #ifdef KEY_RESIZE
789 case KEY_RESIZE:
790 #endif
791 goto notFound;
793 case KEY_SDC:
794 numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, SHIFT_PRESSED);
795 break;
796 case KEY_SDL:
797 case KEY_SELECT:
798 goto notFound;
800 case KEY_SEND:
801 numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, SHIFT_PRESSED);
802 break;
804 case KEY_SEOL:
805 case KEY_SEXIT:
806 case KEY_SFIND:
807 case KEY_SHELP:
808 goto notFound;
810 case KEY_SHOME:
811 numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, SHIFT_PRESSED);
812 break;
813 case KEY_SIC:
814 numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, SHIFT_PRESSED);
815 break;
816 case KEY_SLEFT:
817 numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, SHIFT_PRESSED);
818 break;
820 case KEY_SMESSAGE:
821 case KEY_SMOVE:
822 case KEY_SNEXT:
823 case KEY_SOPTIONS:
824 case KEY_SPREVIOUS:
825 case KEY_SPRINT:
826 case KEY_SREDO:
827 case KEY_SREPLACE:
828 goto notFound;
830 case KEY_SRIGHT:
831 numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, SHIFT_PRESSED);
832 break;
834 case KEY_SRSUME:
835 case KEY_SSAVE:
836 case KEY_SSUSPEND:
837 case KEY_SUNDO:
838 case KEY_SUSPEND:
839 case KEY_UNDO:
840 notFound:
841 WINE_FIXME("Not done yet (%o)\n", inchar);
842 break;
843 default:
844 WINE_ERR("Unknown val (%o)\n", inchar);
845 break;
847 return numEvent;
850 /******************************************************************
851 * input_thread
853 static DWORD CALLBACK input_thread( void *arg )
855 struct inner_data* data = arg;
856 int inchar;
857 INPUT_RECORD ir[8];
858 unsigned numEvent;
859 DWORD n;
860 struct pollfd pfd[2];
862 pfd[0].fd = 0;
863 pfd[0].events = POLLIN;
864 pfd[1].fd = PRIVATE(data)->sync_pipe[0];
865 pfd[1].events = POLLHUP;
867 for (;;)
869 pfd[0].revents = pfd[1].revents = 0;
870 if (poll( pfd, 2, -1 ) == -1) break;
871 if (pfd[0].revents & (POLLHUP|POLLERR)) break;
872 if (pfd[1].revents & (POLLHUP|POLLERR)) break;
873 if (!(pfd[0].revents & POLLIN)) continue;
875 if ((inchar = wgetch(stdscr)) == ERR) continue;
877 WINE_TRACE("Got o%o (0x%x)\n", inchar,inchar);
879 if (inchar >= KEY_MIN && inchar <= KEY_MAX)
880 numEvent = WCCURSES_FillCode(data, ir, inchar);
881 else
882 numEvent = WCCURSES_FillSimpleChar(ir, inchar);
884 if (numEvent) WriteConsoleInputW(data->hConIn, ir, numEvent, &n);
886 close( PRIVATE(data)->sync_pipe[0] );
887 return 0;
890 /******************************************************************
891 * WCCURSES_DeleteBackend
895 static void WCCURSES_DeleteBackend(struct inner_data* data)
897 if (!PRIVATE(data)) return;
899 if (PRIVATE(data)->input_thread)
901 close( PRIVATE(data)->sync_pipe[1] );
902 WaitForSingleObject( PRIVATE(data)->input_thread, INFINITE );
903 CloseHandle( PRIVATE(data)->input_thread );
906 delwin(PRIVATE(data)->pad);
907 #ifdef HAVE_MOUSEMASK
909 mmask_t mm;
910 mousemask(PRIVATE(data)->initial_mouse_mask, &mm);
912 #endif
913 endwin();
915 HeapFree(GetProcessHeap(), 0, PRIVATE(data)->line);
916 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
917 data->private = NULL;
920 /******************************************************************
921 * WCCURSES_MainLoop
925 static int WCCURSES_MainLoop(struct inner_data* data)
927 DWORD id;
929 if (pipe( PRIVATE(data)->sync_pipe ) == -1) return 0;
930 PRIVATE(data)->input_thread = CreateThread( NULL, 0, input_thread, data, 0, &id );
932 while (WaitForSingleObject(data->hSynchro, INFINITE) == WAIT_OBJECT_0)
934 if (!WINECON_GrabChanges(data)) break;
937 close( PRIVATE(data)->sync_pipe[1] );
938 WaitForSingleObject( PRIVATE(data)->input_thread, INFINITE );
939 CloseHandle( PRIVATE(data)->input_thread );
940 PRIVATE(data)->input_thread = 0;
941 return 0;
944 /******************************************************************
945 * WCCURSES_InitBackend
947 * Initialisation part II: creation of window.
950 enum init_return WCCURSES_InitBackend(struct inner_data* data)
952 if( !WCCURSES_bind_libcurses() )
953 return init_failed;
955 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_curse));
956 if (!data->private) return init_failed;
958 data->fnMainLoop = WCCURSES_MainLoop;
959 data->fnPosCursor = WCCURSES_PosCursor;
960 data->fnShapeCursor = WCCURSES_ShapeCursor;
961 data->fnComputePositions = WCCURSES_ComputePositions;
962 data->fnRefresh = WCCURSES_Refresh;
963 data->fnResizeScreenBuffer = WCCURSES_ResizeScreenBuffer;
964 data->fnSetTitle = WCCURSES_SetTitle;
965 data->fnScroll = WCCURSES_Scroll;
966 data->fnSetFont = WCCURSES_SetFont;
967 data->fnDeleteBackend = WCCURSES_DeleteBackend;
968 data->hWnd = NULL;
970 /* FIXME: should find a good way to enable buffer scrolling
971 * For the time being, setting this to 1 will allow scrolling up/down
972 * on buffer with F11/F12.
974 /* PRIVATE(data)->allow_scroll = 1; */
976 initscr();
978 /* creating the basic colors - FIXME intensity not handled yet */
979 if (has_colors())
981 int i, j;
983 start_color();
984 for (i = 0; i < 8; i++)
985 for (j = 0; j < 8; j++)
986 init_pair(i | (j << 3), i, j);
989 raw();
990 noecho();
991 intrflush(stdscr, FALSE);
992 nodelay(stdscr, TRUE);
993 keypad(stdscr, TRUE);
994 #ifdef HAVE_MOUSEMASK
995 if (data->curcfg.quick_edit)
997 mmask_t mm;
998 mousemask(BUTTON1_PRESSED|BUTTON1_RELEASED|
999 BUTTON2_PRESSED|BUTTON2_RELEASED|
1000 BUTTON3_PRESSED|BUTTON3_RELEASED|
1001 BUTTON_SHIFT|BUTTON_CTRL|BUTTON_ALT|REPORT_MOUSE_POSITION,
1002 &mm);
1003 /* no click event generation... we just need button up/down events
1004 * it doesn't seem that mouseinterval(-1) behaves as documented...
1005 * 0 seems to be better value to disable click event generation
1007 mouseinterval(0);
1008 PRIVATE(data)->initial_mouse_mask = mm;
1010 else
1012 mmask_t mm;
1013 mousemask(0, &mm);
1014 PRIVATE(data)->initial_mouse_mask = mm;
1016 #endif
1018 return init_success;
1021 #else
1022 enum init_return WCCURSES_InitBackend(struct inner_data* data)
1024 WINE_ERR("(n)curses was not found at configuration time.\n"
1025 "If you want (n)curses support, please install relevant packages.\n");
1026 return init_not_supported;
1028 #endif