2 * a GUI application for displaying a console
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)
41 #include "winecon_private.h"
43 #include "wine/server.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
48 #define PRIVATE(data) ((struct inner_data_curse*)((data)->private))
50 #if defined(HAVE_CURSES_H) || defined(HAVE_NCURSES_H)
52 struct inner_data_curse
54 mmask_t initial_mouse_mask;
60 /******************************************************************
61 * WCCURSES_ResizeScreenBuffer
65 static void WCCURSES_ResizeScreenBuffer(struct inner_data* data)
67 /* reallocate a new pad. next event would redraw the whole pad */
68 if (PRIVATE(data)->pad) delwin(PRIVATE(data)->pad);
69 PRIVATE(data)->pad = newpad(data->curcfg.sb_height, data->curcfg.sb_width);
70 if (!PRIVATE(data)->pad)
71 WINE_FIXME("Cannot create pad\n");
72 PRIVATE(data)->line = HeapReAlloc(GetProcessHeap(), 0, PRIVATE(data)->line,
73 sizeof(chtype) * data->curcfg.sb_width);
76 /******************************************************************
79 * Set a new position for the cursor (and refresh any modified part of our pad)
81 static void WCCURSES_PosCursor(const struct inner_data* data)
83 if (data->curcfg.cursor_visible &&
84 data->cursor.Y >= data->curcfg.win_pos.Y &&
85 data->cursor.Y < data->curcfg.win_pos.Y + data->curcfg.win_height &&
86 data->cursor.X >= data->curcfg.win_pos.X &&
87 data->cursor.X < data->curcfg.win_pos.X + data->curcfg.win_width)
89 if (curs_set(2) == ERR) curs_set(1);
90 wmove(PRIVATE(data)->pad, data->cursor.Y, data->cursor.X);
96 prefresh(PRIVATE(data)->pad,
97 data->curcfg.win_pos.Y, data->curcfg.win_pos.X,
98 0, 0, data->curcfg.win_height, data->curcfg.win_width);
101 /******************************************************************
102 * WCCURSES_ShapeCursor
104 * Sets a new shape for the cursor
106 void WCCURSES_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
108 /* we can't do much about the size... */
109 data->curcfg.cursor_size = size;
110 data->curcfg.cursor_visible = vis ? TRUE : FALSE;
111 WCCURSES_PosCursor(data);
114 /******************************************************************
115 * WCCURSES_ComputePositions
117 * Recomputes all the components (mainly scroll bars) positions
119 void WCCURSES_ComputePositions(struct inner_data* data)
121 if (PRIVATE(data)->pad) WCCURSES_PosCursor(data);
124 /******************************************************************
127 * Sets the title to the wine console
129 static void WCCURSES_SetTitle(const struct inner_data* data)
133 if (WINECON_GetConsoleTitle(data->hConIn, wbuf, sizeof(wbuf)/sizeof(WCHAR)))
137 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buffer, sizeof(buffer),
139 fputs("\033]2;", stdout);
140 fputs(buffer, stdout);
146 /******************************************************************
151 static void WCCURSES_Refresh(const struct inner_data* data, int tp, int bm)
158 for (y = tp; y <= bm; y++)
160 cell = &data->cells[y * data->curcfg.sb_width];
161 for (x = 0; x < data->curcfg.sb_width; x++)
163 WideCharToMultiByte(CP_ACP, 0, &cell[x].Char.UnicodeChar, 1,
167 if (cell[x].Attributes & FOREGROUND_RED) attr |= COLOR_PAIR(COLOR_RED);
168 if (cell[x].Attributes & FOREGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE);
169 if (cell[x].Attributes & FOREGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN);
170 if (cell[x].Attributes & BACKGROUND_RED) attr |= COLOR_PAIR(COLOR_RED << 3);
171 if (cell[x].Attributes & BACKGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE << 3);
172 if (cell[x].Attributes & BACKGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN << 3);
174 if (cell[x].Attributes & FOREGROUND_INTENSITY) attr |= A_BOLD;
175 PRIVATE(data)->line[x] = attr;
177 mvwaddchnstr(PRIVATE(data)->pad, y, 0, PRIVATE(data)->line, data->curcfg.sb_width);
179 prefresh(PRIVATE(data)->pad,
180 data->curcfg.win_pos.Y, data->curcfg.win_pos.X,
181 0, 0, data->curcfg.win_height, data->curcfg.win_width);
184 /******************************************************************
189 static void WCCURSES_Scroll(struct inner_data* data, int pos, BOOL horz)
193 data->curcfg.win_pos.X = pos;
197 data->curcfg.win_pos.Y = pos;
199 WCCURSES_PosCursor(data);
202 /******************************************************************
207 static void WCCURSES_SetFont(struct inner_data* data, const WCHAR* font,
208 unsigned height, unsigned weight)
210 /* FIXME: really not much to do ? */
213 /* Ascii -> VK, generated by calling VkKeyScanA(i) */
214 static int vkkeyscan_table[256] =
216 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,
217 0,32,305,478,307,308,309,311,222,313,304,312,443,188,189,190,191,48,
218 49,50,51,52,53,54,55,56,57,442,186,444,187,446,447,306,321,322,323,
219 324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
220 341,342,343,344,345,346,219,220,221,310,445,192,65,66,67,68,69,70,71,
221 72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,475,476,477,
222 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,
223 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,
224 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,
225 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
228 static int mapvkey_0[256] =
230 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,
231 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,
232 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,
233 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,
234 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,
235 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,
236 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,
237 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,
238 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
241 /******************************************************************
242 * WCCURSES_FillSimpleChar
246 static unsigned WCCURSES_FillSimpleChar(INPUT_RECORD* ir, unsigned inchar)
254 case 127: inchar = '\b'; break;
255 case 10: inchar = '\r'; break;
257 /* we assume that ESC & and the second character are atomically generated
258 * otherwise, we'll have a race here
260 if ((second = wgetch(stdscr)) != ERR)
262 /* we got a alt-something key... */
263 /* FIXME: we don't generate the keydown message for the Alt key */
264 cks = LEFT_ALT_PRESSED;
270 ir->EventType = KEY_EVENT;
271 ir->Event.KeyEvent.bKeyDown = 1;
272 ir->Event.KeyEvent.wRepeatCount = 1;
273 ir->Event.KeyEvent.dwControlKeyState = cks;
274 vk = vkkeyscan_table[inchar];
276 ir->Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
277 if (vk & 0x0200 || (unsigned char)inchar <= 26)
278 ir->Event.KeyEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
280 ir->Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
281 ir->Event.KeyEvent.wVirtualKeyCode = vk;
282 ir->Event.KeyEvent.wVirtualScanCode = mapvkey_0[vk & 0x00ff]; /* VirtualKeyCodes to ScanCode */
283 ir->Event.KeyEvent.uChar.UnicodeChar = (unsigned char)inchar;
288 /******************************************************************
289 * WCCURSES_FillComplexChar
293 static unsigned WCCURSES_FillComplexChar(INPUT_RECORD* ir, WORD vk, WORD kc)
295 ir->EventType = KEY_EVENT;
296 ir->Event.KeyEvent.bKeyDown = 1;
297 ir->Event.KeyEvent.wRepeatCount = 1;
298 ir->Event.KeyEvent.dwControlKeyState = 0;
300 ir->Event.KeyEvent.wVirtualScanCode = vk;
301 ir->Event.KeyEvent.wVirtualKeyCode = kc;
302 ir->Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
303 ir->Event.KeyEvent.uChar.UnicodeChar = 0;
308 /******************************************************************
313 static unsigned WCCURSES_FillMouse(INPUT_RECORD* ir)
315 static unsigned bstate /* = 0 */;
316 static COORD pos /* = {0, 0} */;
320 if (getmouse(&mevt) == ERR)
323 WINE_TRACE("[%u]: (%d, %d) %08lx\n",
324 mevt.id, mevt.x, mevt.y, (unsigned long)mevt.bstate);
326 /* macros to ease mapping ncurse button numbering to windows's one */
327 #define BTN1_BIT FROM_LEFT_1ST_BUTTON_PRESSED
328 #define BTN2_BIT RIGHTMOST_BUTTON_PRESSED
329 #define BTN3_BIT FROM_LEFT_2ND_BUTTON_PRESSED
330 #define BTN4_BIT 0 /* not done yet */
332 if (mevt.bstate & BUTTON1_PRESSED) bstate |= BTN1_BIT;
333 if (mevt.bstate & BUTTON1_RELEASED) bstate &= ~BTN1_BIT;
334 if (mevt.bstate & BUTTON2_PRESSED) bstate |= BTN2_BIT;
335 if (mevt.bstate & BUTTON2_RELEASED) bstate &= ~BTN2_BIT;
336 if (mevt.bstate & BUTTON3_PRESSED) bstate |= BTN3_BIT;
337 if (mevt.bstate & BUTTON3_RELEASED) bstate &= ~BTN3_BIT;
339 ir->EventType = MOUSE_EVENT;
340 ir->Event.MouseEvent.dwMousePosition.X = mevt.x;
341 ir->Event.MouseEvent.dwMousePosition.Y = mevt.y;
343 ir->Event.MouseEvent.dwButtonState = bstate;
345 /* partial conversion */
346 ir->Event.MouseEvent.dwControlKeyState = 0;
347 if (mevt.bstate & BUTTON_SHIFT) ir->Event.MouseEvent.dwControlKeyState |= SHIFT_PRESSED;
348 /* choose to map to left ctrl... could use both ? */
349 if (mevt.bstate & BUTTON_CTRL) ir->Event.MouseEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
350 /* choose to map to left alt... could use both ? */
351 if (mevt.bstate & BUTTON_ALT) ir->Event.MouseEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
352 /* FIXME: unsupported yet flags: CAPSLOCK_ON, ENHANCED_KEY (??), NUMLOCK_ON, SCROLLLOCK_ON
353 * could be reported from the key events...
356 ir->Event.MouseEvent.dwEventFlags = 0;
357 /* FIXME: we no longer generate double click events */
359 if (!(mevt.bstate & (BUTTON1_PRESSED|BUTTON1_RELEASED|BUTTON2_PRESSED|BUTTON2_RELEASED|BUTTON3_PRESSED|BUTTON3_RELEASED)) &&
360 (mevt.x != pos.X || mevt.y != pos.Y))
362 ir->Event.MouseEvent.dwEventFlags |= MOUSE_MOVED;
364 pos.X = mevt.x; pos.Y = mevt.y;
369 /******************************************************************
374 static unsigned WCCURSES_FillCode(INPUT_RECORD* ir, int inchar)
376 unsigned secondEvent = 0;
383 secondEvent = WCCURSES_FillComplexChar(ir, 0x50, 0x28);
386 secondEvent = WCCURSES_FillComplexChar(ir, 0x48, 0x26);
389 secondEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25);
392 secondEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27);
395 secondEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24);
398 secondEvent = WCCURSES_FillSimpleChar(ir, '\b');
401 case KEY_F0: /* up to F63 */
414 secondEvent = WCCURSES_FillComplexChar(ir, 0x3b + inchar - KEY_F(1), 0);
418 secondEvent = WCCURSES_FillComplexChar(ir, 0xd9 + inchar - KEY_F(11), 0);
434 secondEvent = WCCURSES_FillComplexChar(ir, 0x51, 0x22);
437 secondEvent = WCCURSES_FillComplexChar(ir, 0x49, 0x21);
469 secondEvent = WCCURSES_FillMouse(ir);
517 WINE_FIXME("Not done yet (%d)\n", inchar);
520 WINE_ERR("Unknown val (%d)\n", inchar);
526 /******************************************************************
531 static void WCCURSES_GetEvents(struct inner_data* data)
535 unsigned secondEvent = 0;
538 if ((inchar = wgetch(stdscr)) == ERR) {WINE_FIXME("Ooch. somebody beat us\n");return;}
540 WINE_TRACE("Got %d\n", inchar);
544 if (inchar & KEY_CODE_YES)
546 secondEvent = WCCURSES_FillCode(ir, inchar);
550 secondEvent = WCCURSES_FillSimpleChar(ir, inchar);
553 if (secondEvent != 0)
557 switch (ir[1].EventType)
560 ir[1].Event.KeyEvent.bKeyDown = 0;
563 ir[1].Event.MouseEvent.dwButtonState &= ~secondEvent;
566 WINE_FIXME("oooo\n");
570 if (ir[0].EventType != 0)
571 WriteConsoleInput(data->hConIn, ir, secondEvent ? 2 : 1, &n);
574 /******************************************************************
575 * WCCURSES_DeleteBackend
579 static void WCCURSES_DeleteBackend(struct inner_data* data)
583 if (!PRIVATE(data)) return;
585 CloseHandle(PRIVATE(data)->hInput);
587 delwin(PRIVATE(data)->pad);
588 mousemask(PRIVATE(data)->initial_mouse_mask, &mm);
591 HeapFree(GetProcessHeap(), 0, PRIVATE(data)->line);
592 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
593 PRIVATE(data) = NULL;
596 /******************************************************************
601 static int WCCURSES_MainLoop(struct inner_data* data)
605 hin[0] = PRIVATE(data)->hInput;
606 hin[1] = data->hSynchro;
610 unsigned ret = WaitForMultipleObjects(2, hin, FALSE, INFINITE);
614 WCCURSES_GetEvents(data);
616 case WAIT_OBJECT_0+1:
617 if (!WINECON_GrabChanges(data)) return 0;
620 WINE_ERR("got pb\n");
627 /******************************************************************
628 * WCCURSES_InitBackend
630 * Initialisation part II: creation of window.
633 BOOL WCCURSES_InitBackend(struct inner_data* data)
635 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_curse));
636 if (!data->private) return FALSE;
638 data->fnMainLoop = WCCURSES_MainLoop;
639 data->fnPosCursor = WCCURSES_PosCursor;
640 data->fnShapeCursor = WCCURSES_ShapeCursor;
641 data->fnComputePositions = WCCURSES_ComputePositions;
642 data->fnRefresh = WCCURSES_Refresh;
643 data->fnResizeScreenBuffer = WCCURSES_ResizeScreenBuffer;
644 data->fnSetTitle = WCCURSES_SetTitle;
645 data->fnScroll = WCCURSES_Scroll;
646 data->fnSetFont = WCCURSES_SetFont;
647 data->fnDeleteBackend = WCCURSES_DeleteBackend;
649 if (wine_server_fd_to_handle(0, GENERIC_READ|SYNCHRONIZE, FALSE,
650 (obj_handle_t*)&PRIVATE(data)->hInput))
652 WINE_FIXME("Cannot open 0\n");
658 /* creating the basic colors - FIXME intensity not handled yet */
664 for (i = 0; i < 8; i++)
665 for (j = 0; j < 8; j++)
666 init_pair(i | (j << 3), i, j);
671 intrflush(stdscr, FALSE);
672 nodelay(stdscr, TRUE);
673 keypad(stdscr, TRUE);
674 mousemask(0xffffffff, &PRIVATE(data)->initial_mouse_mask);
675 /* no click event generation... we just need button up/down event */
682 BOOL WCCURSES_InitBackend(struct inner_data* data)