wineconsole: Added support for curses window resizing.
[wine/multimedia.git] / programs / wineconsole / curses.c
blob35c59077476cc3bfc905e96e87830dfa683d2eff
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
30 #include "config.h"
31 #include "wine/port.h"
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #ifdef HAVE_POLL_H
37 # include <poll.h>
38 #endif
39 #ifdef HAVE_NCURSES_H
40 # include <ncurses.h>
41 #elif defined(HAVE_CURSES_H)
42 # include <curses.h>
43 #endif
44 /* avoid redefinition warnings */
45 #undef KEY_EVENT
46 #undef MOUSE_MOVED
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 #include <windef.h>
52 #include <winbase.h>
53 #include <winnls.h>
54 #include "winecon_private.h"
56 #include "wine/library.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(curses);
61 #define PRIVATE(data) ((struct inner_data_curse*)((data)->private))
63 #if defined(SONAME_LIBCURSES) || defined(SONAME_LIBNCURSES)
65 #ifdef HAVE_NCURSES_H
66 # define CURSES_NAME "ncurses"
67 #else
68 # define CURSES_NAME "curses"
69 #endif
71 struct inner_data_curse
73 unsigned long initial_mouse_mask;
74 int sync_pipe[2];
75 HANDLE input_thread;
76 WINDOW* pad;
77 chtype* line;
78 int allow_scroll;
81 static CRITICAL_SECTION WCCURSES_CritSect;
83 static void *nc_handle = NULL;
85 #ifdef initscr /* work around Solaris breakage */
86 #undef initscr
87 extern WINDOW *initscr(void);
88 #endif
90 #define MAKE_FUNCPTR(f) static typeof(f) * p_##f;
92 MAKE_FUNCPTR(curs_set)
93 MAKE_FUNCPTR(delwin)
94 MAKE_FUNCPTR(endwin)
95 #ifndef getmaxx
96 MAKE_FUNCPTR(getmaxx)
97 #endif
98 #ifndef getmaxy
99 MAKE_FUNCPTR(getmaxy)
100 #endif
101 MAKE_FUNCPTR(getmouse)
102 MAKE_FUNCPTR(has_colors)
103 MAKE_FUNCPTR(init_pair)
104 MAKE_FUNCPTR(initscr)
105 #ifndef intrflush
106 MAKE_FUNCPTR(intrflush)
107 #endif
108 MAKE_FUNCPTR(keypad)
109 MAKE_FUNCPTR(newpad)
110 #ifndef nodelay
111 MAKE_FUNCPTR(nodelay)
112 #endif
113 #ifndef noecho
114 MAKE_FUNCPTR(noecho)
115 #endif
116 MAKE_FUNCPTR(prefresh)
117 MAKE_FUNCPTR(raw)
118 MAKE_FUNCPTR(start_color)
119 MAKE_FUNCPTR(stdscr)
120 MAKE_FUNCPTR(waddchnstr)
121 MAKE_FUNCPTR(wmove)
122 MAKE_FUNCPTR(wgetch)
123 #ifdef HAVE_MOUSEMASK
124 MAKE_FUNCPTR(mouseinterval)
125 MAKE_FUNCPTR(mousemask)
126 #endif
128 #undef MAKE_FUNCPTR
130 /**********************************************************************/
132 static BOOL WCCURSES_bind_libcurses(void)
134 #ifdef SONAME_LIBNCURSES
135 static const char ncname[] = SONAME_LIBNCURSES;
136 #else
137 static const char ncname[] = SONAME_LIBCURSES;
138 #endif
140 nc_handle = wine_dlopen(ncname, RTLD_NOW, NULL, 0);
141 if(!nc_handle)
143 WINE_MESSAGE("Wine cannot find the " CURSES_NAME " library (%s).\n",
144 ncname);
145 return FALSE;
148 #define LOAD_FUNCPTR(f) \
149 if((p_##f = wine_dlsym(nc_handle, #f, NULL, 0)) == NULL) \
151 WINE_WARN("Can't find symbol %s\n", #f); \
152 goto sym_not_found; \
155 LOAD_FUNCPTR(curs_set)
156 LOAD_FUNCPTR(delwin)
157 LOAD_FUNCPTR(endwin)
158 #ifndef getmaxx
159 LOAD_FUNCPTR(getmaxx)
160 #endif
161 #ifndef getmaxy
162 LOAD_FUNCPTR(getmaxy)
163 #endif
164 LOAD_FUNCPTR(getmouse)
165 LOAD_FUNCPTR(has_colors)
166 LOAD_FUNCPTR(init_pair)
167 LOAD_FUNCPTR(initscr)
168 #ifndef intrflush
169 LOAD_FUNCPTR(intrflush)
170 #endif
171 LOAD_FUNCPTR(keypad)
172 LOAD_FUNCPTR(newpad)
173 #ifndef nodelay
174 LOAD_FUNCPTR(nodelay)
175 #endif
176 #ifndef noecho
177 LOAD_FUNCPTR(noecho)
178 #endif
179 LOAD_FUNCPTR(prefresh)
180 LOAD_FUNCPTR(raw)
181 LOAD_FUNCPTR(start_color)
182 LOAD_FUNCPTR(stdscr)
183 LOAD_FUNCPTR(waddchnstr)
184 LOAD_FUNCPTR(wmove)
185 LOAD_FUNCPTR(wgetch)
186 #ifdef HAVE_MOUSEMASK
187 LOAD_FUNCPTR(mouseinterval)
188 LOAD_FUNCPTR(mousemask)
189 #endif
191 #undef LOAD_FUNCPTR
193 return TRUE;
195 sym_not_found:
196 WINE_MESSAGE(
197 "Wine cannot find certain functions that it needs inside the "
198 CURSES_NAME "\nlibrary. To enable Wine to use " CURSES_NAME
199 " please upgrade your " CURSES_NAME "\nlibraries\n");
200 wine_dlclose(nc_handle, NULL, 0);
201 nc_handle = NULL;
202 return FALSE;
205 #define curs_set p_curs_set
206 #define delwin p_delwin
207 #define endwin p_endwin
208 #ifndef getmaxx
209 #define getmaxx p_getmaxx
210 #endif
211 #ifndef getmaxy
212 #define getmaxy p_getmaxy
213 #endif
214 #define getmouse p_getmouse
215 #define has_colors p_has_colors
216 #define init_pair p_init_pair
217 #define initscr p_initscr
218 #ifndef intrflush
219 #define intrflush p_intrflush
220 #endif
221 #define keypad p_keypad
222 #define mouseinterval p_mouseinterval
223 #define mousemask p_mousemask
224 #define newpad p_newpad
225 #ifndef nodelay
226 #define nodelay p_nodelay
227 #endif
228 #ifndef noecho
229 #define noecho p_noecho
230 #endif
231 #define prefresh p_prefresh
232 #define raw p_raw
233 #define start_color p_start_color
234 #define stdscr (*p_stdscr)
235 #define waddchnstr p_waddchnstr
236 #define wmove p_wmove
237 #define wgetch p_wgetch
239 /******************************************************************
240 * WCCURSES_ResizeScreenBuffer
244 static void WCCURSES_ResizeScreenBuffer(struct inner_data* data)
246 /* reallocate a new pad. next event would redraw the whole pad */
247 if (PRIVATE(data)->pad) delwin(PRIVATE(data)->pad);
248 PRIVATE(data)->pad = newpad(data->curcfg.sb_height, data->curcfg.sb_width);
249 if (!PRIVATE(data)->pad)
250 WINE_FIXME("Cannot create pad\n");
251 if (PRIVATE(data)->line)
252 PRIVATE(data)->line = HeapReAlloc(GetProcessHeap(), 0, PRIVATE(data)->line,
253 sizeof(chtype) * data->curcfg.sb_width);
254 else
255 PRIVATE(data)->line = HeapAlloc(GetProcessHeap(), 0,
256 sizeof(chtype) * data->curcfg.sb_width);
259 /******************************************************************
260 * WCCURSES_PosCursor
262 * Set a new position for the cursor (and refresh any modified part of our pad)
264 static void WCCURSES_PosCursor(const struct inner_data* data)
266 int scr_width;
267 int scr_height;
269 if (data->curcfg.cursor_visible &&
270 data->cursor.Y >= data->curcfg.win_pos.Y &&
271 data->cursor.Y < data->curcfg.win_pos.Y + data->curcfg.win_height &&
272 data->cursor.X >= data->curcfg.win_pos.X &&
273 data->cursor.X < data->curcfg.win_pos.X + data->curcfg.win_width)
275 if (curs_set(2) == ERR) curs_set(1);
276 wmove(PRIVATE(data)->pad, data->cursor.Y, data->cursor.X);
278 else
280 curs_set(0);
282 getmaxyx(stdscr, scr_height, scr_width);
283 prefresh(PRIVATE(data)->pad,
284 data->curcfg.win_pos.Y, data->curcfg.win_pos.X,
285 0, 0,
286 min(scr_height, data->curcfg.win_height) - 1,
287 min(scr_width, data->curcfg.win_width) - 1);
290 /******************************************************************
291 * WCCURSES_ShapeCursor
293 * Sets a new shape for the cursor
295 static void WCCURSES_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
297 /* we can't do much about the size... */
298 data->curcfg.cursor_size = size;
299 data->curcfg.cursor_visible = vis ? TRUE : FALSE;
300 WCCURSES_PosCursor(data);
303 /******************************************************************
304 * WCCURSES_ComputePositions
306 * Recomputes all the components (mainly scroll bars) positions
308 static void WCCURSES_ComputePositions(struct inner_data* data)
310 int x, y;
312 getmaxyx(stdscr, y, x);
313 if ((data->curcfg.win_height && y < data->curcfg.win_height) ||
314 (data->curcfg.win_width && x < data->curcfg.win_width))
316 SMALL_RECT pos;
318 WINE_WARN("Window too large (%dx%d), adjusting to curses' size (%dx%d)\n",
319 data->curcfg.win_width, data->curcfg.win_height, x, y);
320 pos.Left = pos.Top = 0;
321 pos.Right = x - 1; pos.Bottom = y - 1;
322 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);
323 return; /* we'll get called again upon event for new window size */
325 if (PRIVATE(data)->pad) WCCURSES_PosCursor(data);
328 /******************************************************************
329 * WCCURSES_SetTitle
331 * Sets the title to the wine console
333 static void WCCURSES_SetTitle(const struct inner_data* data)
335 WCHAR wbuf[256];
337 if (WINECON_GetConsoleTitle(data->hConIn, wbuf, sizeof(wbuf)/sizeof(WCHAR)))
339 char buffer[256];
341 WideCharToMultiByte(CP_UNIXCP, 0, wbuf, -1, buffer, sizeof(buffer),
342 NULL, NULL);
343 fputs("\033]2;", stdout);
344 fputs(buffer, stdout);
345 fputc('\a', stdout);
346 fflush(stdout);
350 /******************************************************************
351 * WCCURSES_Refresh
355 static void WCCURSES_Refresh(const struct inner_data* data, int tp, int bm)
357 unsigned int x;
358 int y;
359 CHAR_INFO* cell;
360 DWORD attr;
361 char ch;
363 for (y = tp; y <= bm; y++)
365 cell = &data->cells[y * data->curcfg.sb_width];
366 for (x = 0; x < data->curcfg.sb_width; x++)
368 WideCharToMultiByte(CP_UNIXCP, 0, &cell[x].Char.UnicodeChar, 1,
369 &ch, 1, NULL, NULL);
370 attr = ((BYTE)ch < 32) ? 32 : (BYTE)ch;
372 if (cell[x].Attributes & FOREGROUND_RED) attr |= COLOR_PAIR(COLOR_RED);
373 if (cell[x].Attributes & FOREGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE);
374 if (cell[x].Attributes & FOREGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN);
375 if (cell[x].Attributes & BACKGROUND_RED) attr |= COLOR_PAIR(COLOR_RED << 3);
376 if (cell[x].Attributes & BACKGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE << 3);
377 if (cell[x].Attributes & BACKGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN << 3);
379 if (cell[x].Attributes & FOREGROUND_INTENSITY) attr |= A_BOLD;
380 PRIVATE(data)->line[x] = attr;
382 mvwaddchnstr(PRIVATE(data)->pad, y, 0, PRIVATE(data)->line, data->curcfg.sb_width);
385 WCCURSES_PosCursor(data);
388 /******************************************************************
389 * WCCURSES_Scroll
393 static void WCCURSES_Scroll(struct inner_data* data, int pos, BOOL horz)
395 if (horz)
397 data->curcfg.win_pos.X = pos;
399 else
401 data->curcfg.win_pos.Y = pos;
403 WCCURSES_PosCursor(data);
406 /******************************************************************
407 * WCCURSES_SetFont
411 static void WCCURSES_SetFont(struct inner_data* data, const WCHAR* font,
412 unsigned height, unsigned weight)
414 /* FIXME: really not much to do ? */
417 /******************************************************************
418 * WCCURSES_Resize
421 static void WCCURSES_Resize(struct inner_data* data)
423 int width, height;
425 getmaxyx(stdscr, height, width);
426 WINECON_ResizeWithContainer(data, width, height);
429 /******************************************************************
430 * WCCURSES_ScrollV
434 static void WCCURSES_ScrollV(struct inner_data* data, int delta)
436 struct config_data cfg = data->curcfg;
438 cfg.win_pos.Y += delta;
439 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
440 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
441 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
442 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
444 WCCURSES_PosCursor(data);
445 WINECON_SetConfig(data, &cfg);
449 /* Ascii -> VK, generated by calling VkKeyScanA(i) */
450 static const int vkkeyscan_table[256] =
452 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,
453 0,32,305,478,307,308,309,311,222,313,304,312,443,188,189,190,191,48,
454 49,50,51,52,53,54,55,56,57,442,186,444,187,446,447,306,321,322,323,
455 324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
456 341,342,343,344,345,346,219,220,221,310,445,192,65,66,67,68,69,70,71,
457 72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,475,476,477,
458 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,
459 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,
460 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,
461 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
464 static const int mapvkey_0[256] =
466 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,
467 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,
468 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,
469 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,
470 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,
471 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,
472 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,
473 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,
474 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
477 /******************************************************************
478 * WCCURSES_InitComplexChar
482 static inline void WCCURSES_InitComplexChar(INPUT_RECORD* ir, BOOL down, WORD vk, WORD kc, DWORD cks)
484 ir->EventType = KEY_EVENT;
485 ir->Event.KeyEvent.bKeyDown = down;
486 ir->Event.KeyEvent.wRepeatCount = 1;
488 ir->Event.KeyEvent.wVirtualScanCode = vk;
489 ir->Event.KeyEvent.wVirtualKeyCode = kc;
490 ir->Event.KeyEvent.dwControlKeyState = cks;
491 ir->Event.KeyEvent.uChar.UnicodeChar = 0;
494 /******************************************************************
495 * WCCURSES_FillSimpleChar
499 static unsigned WCCURSES_FillSimpleChar(INPUT_RECORD* ir, unsigned real_inchar)
501 unsigned vk;
502 unsigned inchar;
503 char ch;
504 unsigned numEvent = 0;
505 DWORD cks = 0;
507 switch (real_inchar)
509 case 9: inchar = real_inchar;
510 real_inchar = 27; /* so that we don't think key is ctrl- something */
511 break;
512 case 10: inchar = '\r';
513 real_inchar = 27; /* Fixme: so that we don't think key is ctrl- something */
514 break;
515 case 127: inchar = '\b';
516 break;
517 case 27:
518 /* we assume that ESC & and the second character are atomically
519 * generated otherwise, we'll have a race here. FIXME: This gives 1 sec. delay
520 * because curses looks for a second character.
522 if ((inchar = wgetch(stdscr)) != ERR)
524 /* we got a alt-something key... */
525 cks = LEFT_ALT_PRESSED;
527 else
528 inchar = 27;
529 break;
530 default:
531 inchar = real_inchar;
532 break;
534 if ((inchar & ~0xFF) != 0) WINE_FIXME("What a char (%u)\n", inchar);
535 vk = vkkeyscan_table[inchar];
536 if (vk & 0x0100)
537 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x2a, 0x10, SHIFT_PRESSED);
538 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
539 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x1d, 0x11, LEFT_CTRL_PRESSED);
540 if (vk & 0x0400)
541 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x38, 0x12, LEFT_ALT_PRESSED);
543 ir[numEvent].EventType = KEY_EVENT;
544 ir[numEvent].Event.KeyEvent.bKeyDown = 1;
545 ir[numEvent].Event.KeyEvent.wRepeatCount = 1;
546 ir[numEvent].Event.KeyEvent.dwControlKeyState = cks;
547 if (vk & 0x0100)
548 ir[numEvent].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
549 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
550 ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
551 if (vk & 0x0400)
552 ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
553 ir[numEvent].Event.KeyEvent.wVirtualKeyCode = vk;
554 ir[numEvent].Event.KeyEvent.wVirtualScanCode = mapvkey_0[vk & 0x00ff]; /* VirtualKeyCodes to ScanCode */
556 ch = inchar;
557 MultiByteToWideChar(CP_UNIXCP, 0,&ch,1,&ir[numEvent].Event.KeyEvent.uChar.UnicodeChar, 1);
558 ir[numEvent + 1] = ir[numEvent];
559 ir[numEvent + 1].Event.KeyEvent.bKeyDown = 0;
561 numEvent += 2;
563 if (vk & 0x0400)
564 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x38, 0x12, LEFT_ALT_PRESSED);
565 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
566 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x1d, 0x11, 0);
567 if (vk & 0x0100)
568 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x2a, 0x10, 0);
570 return numEvent;
573 /******************************************************************
574 * WCCURSES_FillComplexChar
578 static unsigned WCCURSES_FillComplexChar(INPUT_RECORD* ir, WORD vk, WORD kc, DWORD cks)
580 WCCURSES_InitComplexChar(&ir[0], 1, vk, kc, ENHANCED_KEY | cks);
581 WCCURSES_InitComplexChar(&ir[1], 0, vk, kc, ENHANCED_KEY | cks);
583 return 2;
586 /******************************************************************
587 * WCCURSES_FillMouse
591 static unsigned WCCURSES_FillMouse(INPUT_RECORD* ir)
593 #ifdef HAVE_MOUSEMASK
594 static unsigned bstate /* = 0 */;
595 static COORD pos /* = {0, 0} */;
597 MEVENT mevt;
599 if (getmouse(&mevt) == ERR)
600 return 0;
602 WINE_TRACE("[%u]: (%d, %d) %08lx\n",
603 mevt.id, mevt.x, mevt.y, (unsigned long)mevt.bstate);
605 /* macros to ease mapping ncurse button numbering to windows's one */
606 #define BTN1_BIT FROM_LEFT_1ST_BUTTON_PRESSED
607 #define BTN2_BIT RIGHTMOST_BUTTON_PRESSED
608 #define BTN3_BIT FROM_LEFT_2ND_BUTTON_PRESSED
609 #define BTN4_BIT 0 /* not done yet */
611 if (mevt.bstate & BUTTON1_PRESSED) bstate |= BTN1_BIT;
612 if (mevt.bstate & BUTTON1_RELEASED) bstate &= ~BTN1_BIT;
613 if (mevt.bstate & BUTTON2_PRESSED) bstate |= BTN2_BIT;
614 if (mevt.bstate & BUTTON2_RELEASED) bstate &= ~BTN2_BIT;
615 if (mevt.bstate & BUTTON3_PRESSED) bstate |= BTN3_BIT;
616 if (mevt.bstate & BUTTON3_RELEASED) bstate &= ~BTN3_BIT;
618 ir->EventType = MOUSE_EVENT;
619 ir->Event.MouseEvent.dwMousePosition.X = mevt.x;
620 ir->Event.MouseEvent.dwMousePosition.Y = mevt.y;
622 ir->Event.MouseEvent.dwButtonState = bstate;
624 /* partial conversion */
625 ir->Event.MouseEvent.dwControlKeyState = 0;
626 if (mevt.bstate & BUTTON_SHIFT) ir->Event.MouseEvent.dwControlKeyState |= SHIFT_PRESSED;
627 /* choose to map to left ctrl... could use both ? */
628 if (mevt.bstate & BUTTON_CTRL) ir->Event.MouseEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
629 /* choose to map to left alt... could use both ? */
630 if (mevt.bstate & BUTTON_ALT) ir->Event.MouseEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
631 /* FIXME: unsupported yet flags: CAPSLOCK_ON, ENHANCED_KEY (??), NUMLOCK_ON, SCROLLLOCK_ON
632 * could be reported from the key events...
635 ir->Event.MouseEvent.dwEventFlags = 0;
636 /* FIXME: we no longer generate double click events */
638 if (!(mevt.bstate & (BUTTON1_PRESSED|BUTTON1_RELEASED|BUTTON2_PRESSED|BUTTON2_RELEASED|BUTTON3_PRESSED|BUTTON3_RELEASED)) &&
639 (mevt.x != pos.X || mevt.y != pos.Y))
641 ir->Event.MouseEvent.dwEventFlags |= MOUSE_MOVED;
643 pos.X = mevt.x; pos.Y = mevt.y;
645 return 1;
646 #else
647 return 0;
648 #endif
651 /******************************************************************
652 * WCCURSES_FillCode
656 static unsigned WCCURSES_FillCode(struct inner_data* data, INPUT_RECORD* ir, int inchar)
658 unsigned numEvent = 0;
660 switch (inchar)
662 case KEY_BREAK:
663 goto notFound;
664 case KEY_DOWN:
665 numEvent = WCCURSES_FillComplexChar(ir, 0x50, 0x28, 0);
666 break;
667 case KEY_UP:
668 numEvent = WCCURSES_FillComplexChar(ir, 0x48, 0x26, 0);
669 break;
670 case KEY_LEFT:
671 numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, 0);
672 break;
673 case KEY_RIGHT:
674 numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, 0);
675 break;
676 case KEY_HOME:
677 numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, 0);
678 break;
679 case KEY_BACKSPACE:
680 numEvent = WCCURSES_FillSimpleChar(ir, 127);
681 break;
683 case KEY_F0: /* up to F63 */
684 goto notFound;
686 case KEY_F( 1):
687 case KEY_F( 2):
688 case KEY_F( 3):
689 case KEY_F( 4):
690 case KEY_F( 5):
691 case KEY_F( 6):
692 case KEY_F( 7):
693 case KEY_F( 8):
694 case KEY_F( 9):
695 case KEY_F(10):
696 numEvent = WCCURSES_FillComplexChar(ir, 0x3b + inchar - KEY_F(1),
697 0x70 + inchar - KEY_F(1), 0);
698 break;
699 case KEY_F(11):
700 case KEY_F(12):
701 if (PRIVATE(data)->allow_scroll)
703 WCCURSES_ScrollV(data, inchar == KEY_F(11) ? 8 : -8);
705 else
707 numEvent = WCCURSES_FillComplexChar(ir, 0xd9 + inchar - KEY_F(11),
708 0x7a + inchar - KEY_F(11), 0);
710 break;
712 case KEY_DL:
713 case KEY_IL:
714 goto notFound;
716 case KEY_DC:
717 numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, 0);
718 break;
719 case KEY_IC:
720 numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, 0);
721 break;
723 case KEY_EIC:
724 case KEY_CLEAR:
725 case KEY_EOS:
726 case KEY_EOL:
727 case KEY_SF:
728 case KEY_SR:
729 goto notFound;
731 case KEY_NPAGE:
732 numEvent = WCCURSES_FillComplexChar(ir, 0x51, 0x22, 0);
733 break;
734 case KEY_PPAGE:
735 numEvent = WCCURSES_FillComplexChar(ir, 0x49, 0x21, 0);
736 break;
738 case KEY_STAB:
739 case KEY_CTAB:
740 case KEY_CATAB:
741 case KEY_ENTER:
742 case KEY_SRESET:
743 case KEY_RESET:
744 case KEY_PRINT:
745 case KEY_LL:
746 case KEY_A1:
747 case KEY_A3:
748 case KEY_B2:
749 case KEY_C1:
750 case KEY_C3:
751 goto notFound;
752 case KEY_BTAB: /* shift tab */
753 numEvent = WCCURSES_FillSimpleChar(ir, 0x9);
754 ir[0].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
755 ir[1].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
756 if (numEvent != 2) WINE_ERR("FillsimpleChar has changed\n");
757 break;
759 case KEY_BEG:
760 case KEY_CANCEL:
761 case KEY_CLOSE:
762 case KEY_COMMAND:
763 case KEY_COPY:
764 case KEY_CREATE:
765 goto notFound;
767 case KEY_END:
768 numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, 0);
769 break;
771 case KEY_EXIT:
772 case KEY_FIND:
773 case KEY_HELP:
774 case KEY_MARK:
775 case KEY_MESSAGE:
776 goto notFound;
778 case KEY_MOUSE:
779 numEvent = WCCURSES_FillMouse(ir);
780 break;
781 #ifdef KEY_RESIZE
782 case KEY_RESIZE:
783 WCCURSES_Resize(data);
784 break;
785 #endif
787 case KEY_MOVE:
788 case KEY_NEXT:
789 case KEY_OPEN:
790 case KEY_OPTIONS:
791 case KEY_PREVIOUS:
792 case KEY_REDO:
793 case KEY_REFERENCE:
794 case KEY_REFRESH:
795 case KEY_REPLACE:
796 case KEY_RESTART:
797 case KEY_RESUME:
798 case KEY_SAVE:
799 case KEY_SBEG:
800 case KEY_SCANCEL:
801 case KEY_SCOMMAND:
802 case KEY_SCOPY:
803 case KEY_SCREATE:
804 goto notFound;
806 case KEY_SDC:
807 numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, SHIFT_PRESSED);
808 break;
809 case KEY_SDL:
810 case KEY_SELECT:
811 goto notFound;
813 case KEY_SEND:
814 numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, SHIFT_PRESSED);
815 break;
817 case KEY_SEOL:
818 case KEY_SEXIT:
819 case KEY_SFIND:
820 case KEY_SHELP:
821 goto notFound;
823 case KEY_SHOME:
824 numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, SHIFT_PRESSED);
825 break;
826 case KEY_SIC:
827 numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, SHIFT_PRESSED);
828 break;
829 case KEY_SLEFT:
830 numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, SHIFT_PRESSED);
831 break;
833 case KEY_SMESSAGE:
834 case KEY_SMOVE:
835 case KEY_SNEXT:
836 case KEY_SOPTIONS:
837 case KEY_SPREVIOUS:
838 case KEY_SPRINT:
839 case KEY_SREDO:
840 case KEY_SREPLACE:
841 goto notFound;
843 case KEY_SRIGHT:
844 numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, SHIFT_PRESSED);
845 break;
847 case KEY_SRSUME:
848 case KEY_SSAVE:
849 case KEY_SSUSPEND:
850 case KEY_SUNDO:
851 case KEY_SUSPEND:
852 case KEY_UNDO:
853 notFound:
854 WINE_FIXME("Not done yet (%o)\n", inchar);
855 break;
856 default:
857 WINE_ERR("Unknown val (%o)\n", inchar);
858 break;
860 return numEvent;
863 /******************************************************************
864 * input_thread
866 static DWORD CALLBACK input_thread( void *arg )
868 struct inner_data* data = arg;
869 int inchar;
870 INPUT_RECORD ir[8];
871 unsigned numEvent;
872 DWORD n;
873 struct pollfd pfd[2];
875 pfd[0].fd = 0;
876 pfd[0].events = POLLIN;
877 pfd[1].fd = PRIVATE(data)->sync_pipe[0];
878 pfd[1].events = POLLHUP;
880 for (;;)
882 pfd[0].revents = pfd[1].revents = 0;
883 if (poll( pfd, 2, -1 ) == -1) break;
884 if (pfd[0].revents & (POLLHUP|POLLERR)) break;
885 if (pfd[1].revents & (POLLHUP|POLLERR)) break;
886 if (!(pfd[0].revents & POLLIN)) continue;
888 /* we're called from input thread (not main thread), so force unique access */
889 EnterCriticalSection(&WCCURSES_CritSect);
890 if ((inchar = wgetch(stdscr)) != ERR)
892 WINE_TRACE("Got o%o (0x%x)\n", inchar,inchar);
894 if (inchar >= KEY_MIN && inchar <= KEY_MAX)
895 numEvent = WCCURSES_FillCode(data, ir, inchar);
896 else
897 numEvent = WCCURSES_FillSimpleChar(ir, inchar);
899 if (numEvent) WriteConsoleInputW(data->hConIn, ir, numEvent, &n);
901 LeaveCriticalSection(&WCCURSES_CritSect);
903 close( PRIVATE(data)->sync_pipe[0] );
904 return 0;
907 /******************************************************************
908 * WCCURSES_DeleteBackend
912 static void WCCURSES_DeleteBackend(struct inner_data* data)
914 if (!PRIVATE(data)) return;
916 if (PRIVATE(data)->input_thread)
918 close( PRIVATE(data)->sync_pipe[1] );
919 WaitForSingleObject( PRIVATE(data)->input_thread, INFINITE );
920 CloseHandle( PRIVATE(data)->input_thread );
923 delwin(PRIVATE(data)->pad);
924 #ifdef HAVE_MOUSEMASK
926 mmask_t mm;
927 mousemask(PRIVATE(data)->initial_mouse_mask, &mm);
929 #endif
930 endwin();
932 HeapFree(GetProcessHeap(), 0, PRIVATE(data)->line);
933 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
934 data->private = NULL;
937 /******************************************************************
938 * WCCURSES_MainLoop
942 static int WCCURSES_MainLoop(struct inner_data* data)
944 DWORD id;
945 BOOL cont = TRUE;
947 WCCURSES_Resize(data);
949 if (pipe( PRIVATE(data)->sync_pipe ) == -1) return 0;
950 PRIVATE(data)->input_thread = CreateThread( NULL, 0, input_thread, data, 0, &id );
952 while (cont && WaitForSingleObject(data->hSynchro, INFINITE) == WAIT_OBJECT_0)
954 EnterCriticalSection(&WCCURSES_CritSect);
955 cont = WINECON_GrabChanges(data);
956 LeaveCriticalSection(&WCCURSES_CritSect);
959 close( PRIVATE(data)->sync_pipe[1] );
960 WaitForSingleObject( PRIVATE(data)->input_thread, INFINITE );
961 CloseHandle( PRIVATE(data)->input_thread );
962 PRIVATE(data)->input_thread = 0;
963 return 0;
966 /******************************************************************
967 * WCCURSES_InitBackend
969 * Initialisation part II: creation of window.
972 enum init_return WCCURSES_InitBackend(struct inner_data* data)
974 if( !WCCURSES_bind_libcurses() )
975 return init_failed;
977 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_curse));
978 if (!data->private) return init_failed;
980 data->fnMainLoop = WCCURSES_MainLoop;
981 data->fnPosCursor = WCCURSES_PosCursor;
982 data->fnShapeCursor = WCCURSES_ShapeCursor;
983 data->fnComputePositions = WCCURSES_ComputePositions;
984 data->fnRefresh = WCCURSES_Refresh;
985 data->fnResizeScreenBuffer = WCCURSES_ResizeScreenBuffer;
986 data->fnSetTitle = WCCURSES_SetTitle;
987 data->fnScroll = WCCURSES_Scroll;
988 data->fnSetFont = WCCURSES_SetFont;
989 data->fnDeleteBackend = WCCURSES_DeleteBackend;
990 data->hWnd = NULL;
992 /* FIXME: should find a good way to enable buffer scrolling
993 * For the time being, setting this to 1 will allow scrolling up/down
994 * on buffer with F11/F12.
996 /* PRIVATE(data)->allow_scroll = 1; */
998 initscr();
1000 /* creating the basic colors - FIXME intensity not handled yet */
1001 if (has_colors())
1003 int i, j;
1005 start_color();
1006 for (i = 0; i < 8; i++)
1007 for (j = 0; j < 8; j++)
1008 init_pair(i | (j << 3), i, j);
1011 raw();
1012 noecho();
1013 intrflush(stdscr, FALSE);
1014 nodelay(stdscr, TRUE);
1015 keypad(stdscr, TRUE);
1016 #ifdef HAVE_MOUSEMASK
1017 if (data->curcfg.quick_edit)
1019 mmask_t mm;
1020 mousemask(BUTTON1_PRESSED|BUTTON1_RELEASED|
1021 BUTTON2_PRESSED|BUTTON2_RELEASED|
1022 BUTTON3_PRESSED|BUTTON3_RELEASED|
1023 BUTTON_SHIFT|BUTTON_CTRL|BUTTON_ALT|REPORT_MOUSE_POSITION,
1024 &mm);
1025 /* no click event generation... we just need button up/down events
1026 * it doesn't seem that mouseinterval(-1) behaves as documented...
1027 * 0 seems to be better value to disable click event generation
1029 mouseinterval(0);
1030 PRIVATE(data)->initial_mouse_mask = mm;
1032 else
1034 mmask_t mm;
1035 mousemask(0, &mm);
1036 PRIVATE(data)->initial_mouse_mask = mm;
1038 #endif
1040 return init_success;
1043 #else
1044 enum init_return WCCURSES_InitBackend(struct inner_data* data)
1046 WINE_ERR("(n)curses was not found at configuration time.\n"
1047 "If you want (n)curses support, please install relevant packages.\n");
1048 return init_not_supported;
1050 #endif