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