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