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