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