The Tab key was considered a ctrl-key and Shift-Tab was ignored.
[wine] / programs / wineconsole / curses.c
1 /*
2  * a GUI application for displaying a console
3  *      (N)Curses back end
4  *
5  * Copyright 2002 Eric Pouech
6  *
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.
11  *
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.
16  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
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.
30  */
31
32 #include "config.h"
33 #include "wine/port.h"
34
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #ifdef HAVE_CURSES_H
39 #include <curses.h>
40 #endif
41 #ifdef HAVE_NCURSES_H
42 #include <ncurses.h>
43 #endif
44 #undef KEY_EVENT  /* avoid redefinition warning */
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <windef.h>
49 #include <winbase.h>
50 #include <winnls.h>
51 #include "winecon_private.h"
52
53 #include "wine/library.h"
54 #include "wine/server.h"
55 #include "wine/debug.h"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
58
59 #define PRIVATE(data)   ((struct inner_data_curse*)((data)->private))
60
61 #if defined(HAVE_CURSES_H) || defined(HAVE_NCURSES_H)
62
63 #ifdef HAVE_NCURSES_H
64  #define CURSES_NAME "ncurses"
65 #else
66  #define CURSES_NAME "curses"
67 #endif
68
69 struct inner_data_curse 
70 {
71     mmask_t             initial_mouse_mask;
72     HANDLE              hInput;
73     WINDOW*             pad;
74     chtype*             line;
75     int                 allow_scroll;
76 };
77
78
79 static void *nc_handle = NULL;
80
81 #define MAKE_FUNCPTR(f) static typeof(f) * p_##f;
82
83 MAKE_FUNCPTR(curs_set)
84 MAKE_FUNCPTR(delwin)
85 MAKE_FUNCPTR(endwin)
86 MAKE_FUNCPTR(getmouse)
87 MAKE_FUNCPTR(has_colors)
88 MAKE_FUNCPTR(init_pair)
89 #ifndef initscr
90 MAKE_FUNCPTR(initscr)
91 #endif
92 #ifndef intrflush
93 MAKE_FUNCPTR(intrflush)
94 #endif
95 MAKE_FUNCPTR(keypad)
96 MAKE_FUNCPTR(mouseinterval)
97 MAKE_FUNCPTR(mousemask)
98 MAKE_FUNCPTR(newpad)
99 #ifndef nodelay
100 MAKE_FUNCPTR(nodelay)
101 #endif
102 #ifndef noecho
103 MAKE_FUNCPTR(noecho)
104 #endif
105 MAKE_FUNCPTR(prefresh)
106 MAKE_FUNCPTR(raw)
107 MAKE_FUNCPTR(start_color)
108 MAKE_FUNCPTR(stdscr)
109 MAKE_FUNCPTR(waddchnstr)
110 MAKE_FUNCPTR(wmove)
111 MAKE_FUNCPTR(wgetch)
112
113 #undef MAKE_FUNCPTR
114
115 /**********************************************************************/
116
117 static BOOL WCCURSES_bind_libcurses(void)
118 {
119 #ifdef HAVE_NCURSES_H
120     static const char *ncname = SONAME_LIBNCURSES;
121 #else
122     static const char *ncname = SONAME_LIBCURSES;
123 #endif
124
125     nc_handle = wine_dlopen(ncname, RTLD_NOW, NULL, 0);
126     if(!nc_handle)
127     {
128         WINE_MESSAGE("Wine cannot find the " CURSES_NAME " library (%s).\n",
129                      ncname);
130         return FALSE;
131     }
132
133 #define LOAD_FUNCPTR(f)                                      \
134     if((p_##f = wine_dlsym(nc_handle, #f, NULL, 0)) == NULL) \
135     {                                                        \
136         WINE_WARN("Can't find symbol %s\n", #f);             \
137         goto sym_not_found;                                  \
138     }
139
140     LOAD_FUNCPTR(curs_set)
141     LOAD_FUNCPTR(delwin)
142     LOAD_FUNCPTR(endwin)
143     LOAD_FUNCPTR(getmouse)
144     LOAD_FUNCPTR(has_colors)
145     LOAD_FUNCPTR(init_pair)
146 #ifndef initscr
147     LOAD_FUNCPTR(initscr)
148 #endif
149 #ifndef intrflush
150     LOAD_FUNCPTR(intrflush)
151 #endif
152     LOAD_FUNCPTR(keypad)
153     LOAD_FUNCPTR(mouseinterval)
154     LOAD_FUNCPTR(mousemask)
155     LOAD_FUNCPTR(newpad)
156 #ifndef nodelay
157     LOAD_FUNCPTR(nodelay)
158 #endif
159 #ifndef noecho
160     LOAD_FUNCPTR(noecho)
161 #endif
162     LOAD_FUNCPTR(prefresh)
163     LOAD_FUNCPTR(raw)
164     LOAD_FUNCPTR(start_color)
165     LOAD_FUNCPTR(stdscr)
166     LOAD_FUNCPTR(waddchnstr)
167     LOAD_FUNCPTR(wmove)
168     LOAD_FUNCPTR(wgetch)
169
170 #undef LOAD_FUNCPTR
171
172     return TRUE;
173
174 sym_not_found:
175     WINE_MESSAGE(
176       "Wine cannot find certain functions that it needs inside the "
177        CURSES_NAME "\nlibrary.  To enable Wine to use " CURSES_NAME 
178       " please upgrade your " CURSES_NAME "\nlibraries\n");
179     wine_dlclose(nc_handle, NULL, 0);
180     nc_handle = NULL;
181     return FALSE;
182 }
183
184 #define curs_set p_curs_set
185 #define delwin p_delwin
186 #define endwin p_endwin
187 #define getmouse p_getmouse
188 #define has_colors p_has_colors
189 #define init_pair p_init_pair
190 #ifndef initscr
191 #define initscr p_initscr
192 #endif
193 #ifndef intrflush
194 #define intrflush p_intrflush
195 #endif
196 #define keypad p_keypad
197 #define mouseinterval p_mouseinterval
198 #define mousemask p_mousemask
199 #define newpad p_newpad
200 #ifndef nodelay
201 #define nodelay p_nodelay
202 #endif
203 #ifndef noecho
204 #define noecho p_noecho
205 #endif
206 #define prefresh p_prefresh
207 #define raw p_raw
208 #define start_color p_start_color
209 #define stdscr (*p_stdscr)
210 #define waddchnstr p_waddchnstr
211 #define wmove p_wmove
212 #define wgetch p_wgetch
213
214 /******************************************************************
215  *              WCCURSES_ResizeScreenBuffer
216  *
217  *
218  */
219 static void WCCURSES_ResizeScreenBuffer(struct inner_data* data)
220 {
221     /* reallocate a new pad. next event would redraw the whole pad */
222     if (PRIVATE(data)->pad) delwin(PRIVATE(data)->pad);
223     PRIVATE(data)->pad = newpad(data->curcfg.sb_height, data->curcfg.sb_width);
224     if (!PRIVATE(data)->pad)
225         WINE_FIXME("Cannot create pad\n");
226     PRIVATE(data)->line = HeapReAlloc(GetProcessHeap(), 0, PRIVATE(data)->line, 
227                                       sizeof(chtype) * data->curcfg.sb_width);
228 }
229
230 /******************************************************************
231  *              WCCURSES_PosCursor
232  *
233  * Set a new position for the cursor (and refresh any modified part of our pad)
234  */
235 static void     WCCURSES_PosCursor(const struct inner_data* data)
236 {
237     int scr_width;
238     int scr_height;
239
240     if (data->curcfg.cursor_visible &&
241         data->cursor.Y >= data->curcfg.win_pos.Y &&
242         data->cursor.Y < data->curcfg.win_pos.Y + data->curcfg.win_height &&
243         data->cursor.X >= data->curcfg.win_pos.X &&
244         data->cursor.X < data->curcfg.win_pos.X + data->curcfg.win_width)
245     {
246         if (curs_set(2) == ERR) curs_set(1);
247         wmove(PRIVATE(data)->pad, data->cursor.Y, data->cursor.X);
248     }
249     else
250     {
251         curs_set(0);
252     }
253     getmaxyx(stdscr, scr_height, scr_width); 
254     prefresh(PRIVATE(data)->pad,
255              data->curcfg.win_pos.Y, data->curcfg.win_pos.X,
256              0, 0, 
257              min(scr_height, data->curcfg.win_height) - 1, 
258              min(scr_width, data->curcfg.win_width) - 1);
259 }
260
261 /******************************************************************
262  *              WCCURSES_ShapeCursor
263  *
264  * Sets a new shape for the cursor
265  */
266 void    WCCURSES_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
267 {
268     /* we can't do much about the size... */
269     data->curcfg.cursor_size = size;
270     data->curcfg.cursor_visible = vis ? TRUE : FALSE;
271     WCCURSES_PosCursor(data);
272 }
273
274 /******************************************************************
275  *              WCCURSES_ComputePositions
276  *
277  * Recomputes all the components (mainly scroll bars) positions
278  */
279 void    WCCURSES_ComputePositions(struct inner_data* data)
280 {
281     int         x, y;
282
283     getmaxyx(stdscr, y, x);
284     if ((data->curcfg.win_height && y < data->curcfg.win_height) ||
285         (data->curcfg.win_width && x < data->curcfg.win_width))
286     {
287         SMALL_RECT  pos;
288
289         WINE_WARN("Window too large (%dx%d), adjusting to curses' size (%dx%d)\n",
290                   data->curcfg.win_width, data->curcfg.win_height, x, y);
291         pos.Left = pos.Top = 0;
292         pos.Right = x - 1; pos.Bottom = y - 1;
293         SetConsoleWindowInfo(data->hConOut, FALSE, &pos);
294         return; /* we'll get called again upon event for new window size */
295     }
296     if (PRIVATE(data)->pad) WCCURSES_PosCursor(data);
297 }
298
299 /******************************************************************
300  *              WCCURSES_SetTitle
301  *
302  * Sets the title to the wine console
303  */
304 static void     WCCURSES_SetTitle(const struct inner_data* data)
305 {
306     WCHAR   wbuf[256];
307
308     if (WINECON_GetConsoleTitle(data->hConIn, wbuf, sizeof(wbuf)/sizeof(WCHAR)))
309     {
310         char        buffer[256];
311         
312         WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buffer, sizeof(buffer), 
313                             NULL, NULL);
314         fputs("\033]2;", stdout);
315         fputs(buffer, stdout);
316         fputc('\a', stdout);
317         fflush(stdout);
318     }
319 }
320
321 /******************************************************************
322  *              WCCURSES_Refresh
323  *
324  *
325  */
326 static void WCCURSES_Refresh(const struct inner_data* data, int tp, int bm)
327 {
328     int         x, y;
329     CHAR_INFO*  cell;
330     DWORD       attr;
331     char        ch;
332
333     for (y = tp; y <= bm; y++)
334     {
335         cell = &data->cells[y * data->curcfg.sb_width];
336         for (x = 0; x < data->curcfg.sb_width; x++)
337         {
338             WideCharToMultiByte(CP_ACP, 0, &cell[x].Char.UnicodeChar, 1, 
339                                 &ch, 1, NULL, NULL);
340             attr = ((BYTE)ch < 32 || (BYTE)ch > 127) ? 32 : (BYTE)ch;
341
342             if (cell[x].Attributes & FOREGROUND_RED)       attr |= COLOR_PAIR(COLOR_RED);
343             if (cell[x].Attributes & FOREGROUND_BLUE)      attr |= COLOR_PAIR(COLOR_BLUE);
344             if (cell[x].Attributes & FOREGROUND_GREEN)     attr |= COLOR_PAIR(COLOR_GREEN);
345             if (cell[x].Attributes & BACKGROUND_RED)       attr |= COLOR_PAIR(COLOR_RED << 3);
346             if (cell[x].Attributes & BACKGROUND_BLUE)      attr |= COLOR_PAIR(COLOR_BLUE << 3);
347             if (cell[x].Attributes & BACKGROUND_GREEN)     attr |= COLOR_PAIR(COLOR_GREEN << 3);
348
349             if (cell[x].Attributes & FOREGROUND_INTENSITY) attr |= A_BOLD;
350             PRIVATE(data)->line[x] = attr;
351         }
352         mvwaddchnstr(PRIVATE(data)->pad, y, 0, PRIVATE(data)->line, data->curcfg.sb_width);
353     }
354
355     WCCURSES_PosCursor(data);
356 }
357
358 /******************************************************************
359  *              WCCURSES_Scroll
360  *
361  *
362  */
363 static void WCCURSES_Scroll(struct inner_data* data, int pos, BOOL horz)
364 {
365     if (horz)
366     {
367         data->curcfg.win_pos.X = pos;
368     }
369     else
370     {
371         data->curcfg.win_pos.Y = pos;
372     }
373     WCCURSES_PosCursor(data);
374 }
375
376 /******************************************************************
377  *              WCCURSES_SetFont
378  *
379  *
380  */
381 static void WCCURSES_SetFont(struct inner_data* data, const WCHAR* font, 
382                             unsigned height, unsigned weight)
383 {
384     /* FIXME: really not much to do ? */
385 }
386
387 /******************************************************************
388  *              WCCURSES_ScrollV
389  *
390  *
391  */
392 static void WCCURSES_ScrollV(struct inner_data* data, int delta)
393 {
394     int pos = data->curcfg.win_pos.Y;
395
396     pos += delta;
397     if (pos < 0) pos = 0;
398     if (pos > data->curcfg.sb_height - data->curcfg.win_height)
399         pos = data->curcfg.sb_height - data->curcfg.win_height;
400     if (pos != data->curcfg.win_pos.Y)
401     {
402         data->curcfg.win_pos.Y = pos;
403         WCCURSES_PosCursor(data);
404         WINECON_NotifyWindowChange(data);
405     }
406 }
407
408 /* Ascii -> VK, generated by calling VkKeyScanA(i) */
409 static int vkkeyscan_table[256] = 
410 {
411      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,
412      0,32,305,478,307,308,309,311,222,313,304,312,443,188,189,190,191,48,
413      49,50,51,52,53,54,55,56,57,442,186,444,187,446,447,306,321,322,323,
414      324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
415      341,342,343,344,345,346,219,220,221,310,445,192,65,66,67,68,69,70,71,
416      72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,475,476,477,
417      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,
418      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,
419      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,
420      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
421 };
422
423 static int mapvkey_0[256] = 
424 {
425      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,
426      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,
427      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,
428      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,
429      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,
430      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,
431      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,
432      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,
433      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
434 }; 
435
436 /******************************************************************
437  *              WCCURSES_InitComplexChar
438  *
439  *
440  */
441 static inline void WCCURSES_InitComplexChar(INPUT_RECORD* ir, BOOL down, WORD vk, WORD kc, DWORD cks)
442 {
443     ir->EventType                        = KEY_EVENT;
444     ir->Event.KeyEvent.bKeyDown          = down;
445     ir->Event.KeyEvent.wRepeatCount      = 1;
446     
447     ir->Event.KeyEvent.wVirtualScanCode  = vk;
448     ir->Event.KeyEvent.wVirtualKeyCode   = kc;
449     ir->Event.KeyEvent.dwControlKeyState = cks;
450     ir->Event.KeyEvent.uChar.UnicodeChar = 0;
451 }
452
453 /******************************************************************
454  *              WCCURSES_FillSimpleChar
455  *
456  *
457  */
458 static unsigned WCCURSES_FillSimpleChar(INPUT_RECORD* ir, unsigned real_inchar)
459 {
460     unsigned vk;
461     unsigned inchar;
462     unsigned numEvent = 0;
463     DWORD    cks = 0;
464
465     switch (real_inchar)
466     {
467     case   9: inchar = real_inchar;
468               real_inchar = 27; /* so that we don't think key is ctrl- something */     
469               break;
470     case  10: inchar = '\r'; 
471               real_inchar = 27; /* Fixme: so that we don't think key is ctrl- something */ 
472               break;
473     case 127: inchar = '\b'; 
474               break;
475     case  27:
476         /* we assume that ESC & and the second character are atomically
477          * generated otherwise, we'll have a race here. FIXME: This gives 1 sec. delay
478          * because curses looks for a second character.
479          */
480         if ((inchar = wgetch(stdscr)) != ERR)
481         {
482             /* we got a alt-something key... */
483             cks = LEFT_ALT_PRESSED;
484         }
485         else
486             inchar = 27;
487         break;
488     default:
489         inchar = real_inchar;
490         break;
491     }
492     if ((inchar & ~0xFF) != 0) WINE_FIXME("What a char (%u)\n", inchar);
493     vk = vkkeyscan_table[inchar];
494     if (vk & 0x0100)
495         WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x2a, 0x10, SHIFT_PRESSED);
496     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
497         WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x1d, 0x11, LEFT_CTRL_PRESSED);
498     if (vk & 0x0400)
499         WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x38, 0x12, LEFT_ALT_PRESSED);
500
501     ir[numEvent].EventType                        = KEY_EVENT;
502     ir[numEvent].Event.KeyEvent.bKeyDown          = 1;
503     ir[numEvent].Event.KeyEvent.wRepeatCount      = 1;
504     ir[numEvent].Event.KeyEvent.dwControlKeyState = cks;
505     if (vk & 0x0100)
506         ir[numEvent].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
507     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
508         ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
509     if (vk & 0x0400)
510         ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
511     ir[numEvent].Event.KeyEvent.wVirtualKeyCode = vk;
512     ir[numEvent].Event.KeyEvent.wVirtualScanCode = mapvkey_0[vk & 0x00ff]; /* VirtualKeyCodes to ScanCode */
513     ir[numEvent].Event.KeyEvent.uChar.UnicodeChar = (unsigned char)inchar;
514
515     ir[numEvent + 1] = ir[numEvent];
516     ir[numEvent + 1].Event.KeyEvent.bKeyDown      = 0;
517
518     numEvent += 2;
519
520     if (vk & 0x0400)
521         WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x38, 0x12, LEFT_ALT_PRESSED);
522     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
523         WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x1d, 0x11, 0);
524     if (vk & 0x0100)
525         WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x2a, 0x10, 0);
526
527     return numEvent;
528 }
529
530 /******************************************************************
531  *              WCCURSES_FillComplexChar
532  *
533  *
534  */
535 static unsigned WCCURSES_FillComplexChar(INPUT_RECORD* ir, WORD vk, WORD kc, DWORD cks)
536 {
537     WCCURSES_InitComplexChar(&ir[0], 1, vk, kc, ENHANCED_KEY | cks);
538     WCCURSES_InitComplexChar(&ir[1], 0, vk, kc, ENHANCED_KEY | cks);
539
540     return 2;
541 }
542
543 /******************************************************************
544  *              WCCURSES_FillMouse
545  *
546  *
547  */
548 static unsigned WCCURSES_FillMouse(INPUT_RECORD* ir)
549 {
550     static      unsigned        bstate /* = 0 */;
551     static      COORD           pos /* = {0, 0} */;
552
553     MEVENT      mevt;
554
555     if (getmouse(&mevt) == ERR)
556         return 0;
557
558     WINE_TRACE("[%u]: (%d, %d) %08lx\n", 
559                mevt.id, mevt.x, mevt.y, (unsigned long)mevt.bstate);
560
561     /* macros to ease mapping ncurse button numbering to windows's one */
562 #define BTN1_BIT        FROM_LEFT_1ST_BUTTON_PRESSED
563 #define BTN2_BIT        RIGHTMOST_BUTTON_PRESSED
564 #define BTN3_BIT        FROM_LEFT_2ND_BUTTON_PRESSED
565 #define BTN4_BIT        0 /* not done yet */
566
567     if (mevt.bstate & BUTTON1_PRESSED)   bstate |= BTN1_BIT;
568     if (mevt.bstate & BUTTON1_RELEASED)  bstate &= ~BTN1_BIT;
569     if (mevt.bstate & BUTTON2_PRESSED)   bstate |= BTN2_BIT;
570     if (mevt.bstate & BUTTON2_RELEASED)  bstate &= ~BTN2_BIT;
571     if (mevt.bstate & BUTTON3_PRESSED)   bstate |= BTN3_BIT;
572     if (mevt.bstate & BUTTON3_RELEASED)  bstate &= ~BTN3_BIT;
573
574     ir->EventType = MOUSE_EVENT;
575     ir->Event.MouseEvent.dwMousePosition.X = mevt.x;
576     ir->Event.MouseEvent.dwMousePosition.Y = mevt.y;
577
578     ir->Event.MouseEvent.dwButtonState = bstate;
579
580     /* partial conversion */
581     ir->Event.MouseEvent.dwControlKeyState = 0;
582     if (mevt.bstate & BUTTON_SHIFT)     ir->Event.MouseEvent.dwControlKeyState |= SHIFT_PRESSED;
583     /* choose to map to left ctrl... could use both ? */
584     if (mevt.bstate & BUTTON_CTRL)      ir->Event.MouseEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
585     /* choose to map to left alt... could use both ? */
586     if (mevt.bstate & BUTTON_ALT)       ir->Event.MouseEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
587     /* FIXME: unsupported yet flags: CAPSLOCK_ON, ENHANCED_KEY (??), NUMLOCK_ON, SCROLLLOCK_ON 
588      * could be reported from the key events...
589      */
590
591     ir->Event.MouseEvent.dwEventFlags = 0;
592     /* FIXME: we no longer generate double click events */
593
594     if (!(mevt.bstate & (BUTTON1_PRESSED|BUTTON1_RELEASED|BUTTON2_PRESSED|BUTTON2_RELEASED|BUTTON3_PRESSED|BUTTON3_RELEASED)) &&
595         (mevt.x != pos.X || mevt.y != pos.Y))
596     {
597         ir->Event.MouseEvent.dwEventFlags |= MOUSE_MOVED;
598     }
599     pos.X = mevt.x; pos.Y = mevt.y;
600
601     return 1;
602 }
603
604 /******************************************************************
605  *              WCCURSES_FillCode
606  *
607  *
608  */
609 static unsigned WCCURSES_FillCode(struct inner_data* data, INPUT_RECORD* ir, int inchar)
610 {
611     unsigned numEvent = 0;
612     
613     switch (inchar)
614     {
615     case KEY_BREAK:
616         goto notFound;
617     case KEY_DOWN:
618         numEvent = WCCURSES_FillComplexChar(ir, 0x50, 0x28, 0);
619         break;
620     case KEY_UP:
621         numEvent = WCCURSES_FillComplexChar(ir, 0x48, 0x26, 0);
622         break;
623     case KEY_LEFT:
624         numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, 0);
625         break;
626     case KEY_RIGHT:
627         numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, 0);
628         break;
629     case KEY_HOME:
630         numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, 0);
631         break;
632     case KEY_BACKSPACE:
633         numEvent = WCCURSES_FillSimpleChar(ir, 127);
634         break;
635         
636     case KEY_F0: /* up to F63 */
637         goto notFound;
638                     
639     case KEY_F( 1):
640     case KEY_F( 2):
641     case KEY_F( 3):
642     case KEY_F( 4):
643     case KEY_F( 5):
644     case KEY_F( 6):
645     case KEY_F( 7):
646     case KEY_F( 8):
647     case KEY_F( 9):
648     case KEY_F(10):
649         numEvent = WCCURSES_FillComplexChar(ir, 0x3b + inchar - KEY_F(1), 0, 0);
650         break;
651     case KEY_F(11):
652     case KEY_F(12):
653         if (PRIVATE(data)->allow_scroll)
654         {
655             WCCURSES_ScrollV(data, inchar == KEY_F(11) ? 8 : -8);
656         }
657         else
658         {
659             numEvent = WCCURSES_FillComplexChar(ir, 0xd9 + inchar - KEY_F(11), 0, 0);
660         }
661         break;
662                     
663     case KEY_DL:
664     case KEY_IL:
665         goto notFound;
666
667     case KEY_DC:
668         numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, 0);
669         break;
670     case KEY_IC:
671         numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, 0);
672         break;
673
674     case KEY_EIC:
675     case KEY_CLEAR:
676     case KEY_EOS:
677     case KEY_EOL:
678     case KEY_SF:
679     case KEY_SR:
680         goto notFound;
681                     
682     case KEY_NPAGE:
683         numEvent = WCCURSES_FillComplexChar(ir, 0x51, 0x22, 0);
684         break;
685     case KEY_PPAGE:
686         numEvent = WCCURSES_FillComplexChar(ir, 0x49, 0x21, 0);
687         break;
688         
689     case KEY_STAB:
690     case KEY_CTAB:
691     case KEY_CATAB:
692     case KEY_ENTER:
693     case KEY_SRESET:
694     case KEY_RESET:
695     case KEY_PRINT:
696     case KEY_LL:
697     case KEY_A1:
698     case KEY_A3:
699     case KEY_B2:
700     case KEY_C1:
701     case KEY_C3:
702         goto notFound;
703     case KEY_BTAB:      /* shift tab */
704         numEvent = WCCURSES_FillSimpleChar(ir, 0x9);
705         ir[0].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
706         ir[1].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;        
707         if (numEvent != 2) WINE_ERR("FillsimpleChar has changed");
708         break;
709
710     case KEY_BEG:
711     case KEY_CANCEL:
712     case KEY_CLOSE:
713     case KEY_COMMAND:
714     case KEY_COPY:
715     case KEY_CREATE:
716         goto notFound;
717
718     case KEY_END:
719         numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, 0);
720         break;
721         
722     case KEY_EXIT:
723     case KEY_FIND:
724     case KEY_HELP:
725     case KEY_MARK:
726     case KEY_MESSAGE:
727         goto notFound;
728                     
729     case KEY_MOUSE:
730         numEvent = WCCURSES_FillMouse(ir);
731         break;
732         
733     case KEY_MOVE:
734     case KEY_NEXT:
735     case KEY_OPEN:
736     case KEY_OPTIONS:
737     case KEY_PREVIOUS:
738     case KEY_REDO:
739     case KEY_REFERENCE:
740     case KEY_REFRESH:
741     case KEY_REPLACE:
742     case KEY_RESIZE:
743     case KEY_RESTART:
744     case KEY_RESUME:
745     case KEY_SAVE:
746     case KEY_SBEG:
747     case KEY_SCANCEL:
748     case KEY_SCOMMAND:
749     case KEY_SCOPY:
750     case KEY_SCREATE:
751         goto notFound;
752
753     case KEY_SDC:
754         numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, SHIFT_PRESSED);
755         break;
756     case KEY_SDL:
757     case KEY_SELECT:
758         goto notFound;
759
760     case KEY_SEND:
761         numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, SHIFT_PRESSED);
762         break;
763
764     case KEY_SEOL:
765     case KEY_SEXIT:
766     case KEY_SFIND:
767     case KEY_SHELP:
768         goto notFound;
769
770     case KEY_SHOME:
771         numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, SHIFT_PRESSED);
772         break;
773     case KEY_SIC:
774         numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, SHIFT_PRESSED);
775         break;
776     case KEY_SLEFT:
777         numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, SHIFT_PRESSED);
778         break;
779
780     case KEY_SMESSAGE:
781     case KEY_SMOVE:
782     case KEY_SNEXT:
783     case KEY_SOPTIONS:
784     case KEY_SPREVIOUS:
785     case KEY_SPRINT:
786     case KEY_SREDO:
787     case KEY_SREPLACE:
788         goto notFound;
789
790     case KEY_SRIGHT:
791         numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, SHIFT_PRESSED);
792         break;
793
794     case KEY_SRSUME:
795     case KEY_SSAVE:
796     case KEY_SSUSPEND:
797     case KEY_SUNDO:
798     case KEY_SUSPEND:
799     case KEY_UNDO:
800     notFound:
801         WINE_FIXME("Not done yet (%o)\n", inchar);
802         break;
803     default:
804         WINE_ERR("Unknown val (%o)\n", inchar);
805         break;
806     }
807     return numEvent;
808 }
809
810 /******************************************************************
811  *              WCCURSES_GetEvents
812  *
813  *
814  */
815 static void WCCURSES_GetEvents(struct inner_data* data)
816 {
817     int                 inchar;
818     INPUT_RECORD        ir[8];
819     unsigned            numEvent;
820     DWORD               n;
821     
822     if ((inchar = wgetch(stdscr)) == ERR) {WINE_FIXME("Ooch. somebody beat us\n");return;}
823     
824     WINE_TRACE("Got %d\n", inchar);
825     
826     if (inchar & KEY_CODE_YES)
827     {
828         numEvent = WCCURSES_FillCode(data, ir, inchar);
829     }
830     else
831     {
832         numEvent = WCCURSES_FillSimpleChar(ir, inchar);
833     }
834     if (numEvent)
835         WriteConsoleInput(data->hConIn, ir, numEvent, &n);
836 }
837
838 /******************************************************************
839  *              WCCURSES_DeleteBackend
840  *
841  *
842  */
843 static void WCCURSES_DeleteBackend(struct inner_data* data)
844 {
845     mmask_t     mm;
846
847     if (!PRIVATE(data)) return;
848
849     CloseHandle(PRIVATE(data)->hInput);
850
851     delwin(PRIVATE(data)->pad);
852     mousemask(PRIVATE(data)->initial_mouse_mask, &mm);
853     endwin();
854
855     HeapFree(GetProcessHeap(), 0, PRIVATE(data)->line);
856     HeapFree(GetProcessHeap(), 0, PRIVATE(data));
857     PRIVATE(data) = NULL;
858 }
859
860 /******************************************************************
861  *              WCCURSES_MainLoop
862  *
863  *
864  */
865 static int WCCURSES_MainLoop(struct inner_data* data)
866 {
867     HANDLE hin[2];
868
869     hin[0] = PRIVATE(data)->hInput;
870     hin[1] = data->hSynchro;
871
872     for (;;) 
873     {
874         unsigned ret = WaitForMultipleObjects(2, hin, FALSE, INFINITE);
875         switch (ret)
876         {
877         case WAIT_OBJECT_0:
878             WCCURSES_GetEvents(data);
879             break;
880         case WAIT_OBJECT_0+1:
881             if (!WINECON_GrabChanges(data)) return 0;
882             break;
883         default:
884             WINE_ERR("got pb\n");
885             /* err */
886             break;
887         }
888     }
889 }
890
891 /******************************************************************
892  *              WCCURSES_InitBackend
893  *
894  * Initialisation part II: creation of window.
895  *
896  */
897 enum init_return WCCURSES_InitBackend(struct inner_data* data)
898 {
899     if( !WCCURSES_bind_libcurses() )
900         return init_failed;
901
902     data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_curse));
903     if (!data->private) return init_failed;
904
905     data->fnMainLoop           = WCCURSES_MainLoop;
906     data->fnPosCursor          = WCCURSES_PosCursor;
907     data->fnShapeCursor        = WCCURSES_ShapeCursor;
908     data->fnComputePositions   = WCCURSES_ComputePositions;
909     data->fnRefresh            = WCCURSES_Refresh;
910     data->fnResizeScreenBuffer = WCCURSES_ResizeScreenBuffer;
911     data->fnSetTitle           = WCCURSES_SetTitle;
912     data->fnScroll             = WCCURSES_Scroll;
913     data->fnSetFont            = WCCURSES_SetFont;
914     data->fnDeleteBackend      = WCCURSES_DeleteBackend;
915
916     if (wine_server_fd_to_handle(0, GENERIC_READ|SYNCHRONIZE, FALSE, 
917                                  (obj_handle_t*)&PRIVATE(data)->hInput))
918     {
919         WINE_FIXME("Cannot open 0\n");
920         return init_failed;
921     }
922
923     /* FIXME: should find a good way to enable buffer scrolling
924      * For the time being, setting this to 1 will allow scrolling up/down 
925      * on buffer with F11/F12.
926      */
927     /* PRIVATE(data)->allow_scroll = 1; */
928
929     initscr();
930
931     /* creating the basic colors - FIXME intensity not handled yet */
932     if (has_colors())
933     {
934         int i, j;
935
936         start_color();
937         for (i = 0; i < 8; i++)
938             for (j = 0; j < 8; j++)
939                 init_pair(i | (j << 3), i, j);
940     }
941
942     raw();
943     noecho();
944     intrflush(stdscr, FALSE);
945     nodelay(stdscr, TRUE);
946     keypad(stdscr, TRUE);
947     if (data->curcfg.quick_edit)
948     {
949         mousemask(BUTTON1_PRESSED|BUTTON1_RELEASED|
950                   BUTTON2_PRESSED|BUTTON2_RELEASED|
951                   BUTTON3_PRESSED|BUTTON3_RELEASED|
952                   BUTTON_SHIFT|BUTTON_CTRL|BUTTON_ALT|REPORT_MOUSE_POSITION,
953                   &PRIVATE(data)->initial_mouse_mask);
954         /* no click event generation... we just need button up/down events
955          * it doesn't seem that mouseinterval(-1) behaves as documented... 
956          * 0 seems to be better value to disable click event generation
957          */
958         mouseinterval(0);
959     }
960     else
961     {
962         mousemask(0, &PRIVATE(data)->initial_mouse_mask);
963     }
964
965     return init_success;
966 }
967
968 #else
969 enum init_return WCCURSES_InitBackend(struct inner_data* data)
970 {
971     return init_not_supported;
972 }
973 #endif