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