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