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