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