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)
26 * - finish buffer scrolling (mainly, need to decide of a nice way for
27 * requesting the UP/DOWN operations
40 #undef KEY_EVENT /* avoid redefinition warning */
43 #include "winecon_private.h"
45 #include "wine/server.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
50 #define PRIVATE(data) ((struct inner_data_curse*)((data)->private))
52 #if defined(HAVE_CURSES_H) || defined(HAVE_NCURSES_H)
54 struct inner_data_curse
56 mmask_t initial_mouse_mask;
63 /******************************************************************
64 * WCCURSES_ResizeScreenBuffer
68 static void WCCURSES_ResizeScreenBuffer(struct inner_data* data)
70 /* reallocate a new pad. next event would redraw the whole pad */
71 if (PRIVATE(data)->pad) delwin(PRIVATE(data)->pad);
72 PRIVATE(data)->pad = newpad(data->curcfg.sb_height, data->curcfg.sb_width);
73 if (!PRIVATE(data)->pad)
74 WINE_FIXME("Cannot create pad\n");
75 PRIVATE(data)->line = HeapReAlloc(GetProcessHeap(), 0, PRIVATE(data)->line,
76 sizeof(chtype) * data->curcfg.sb_width);
79 /******************************************************************
82 * Set a new position for the cursor (and refresh any modified part of our pad)
84 static void WCCURSES_PosCursor(const struct inner_data* data)
86 if (data->curcfg.cursor_visible &&
87 data->cursor.Y >= data->curcfg.win_pos.Y &&
88 data->cursor.Y < data->curcfg.win_pos.Y + data->curcfg.win_height &&
89 data->cursor.X >= data->curcfg.win_pos.X &&
90 data->cursor.X < data->curcfg.win_pos.X + data->curcfg.win_width)
92 if (curs_set(2) == ERR) curs_set(1);
93 wmove(PRIVATE(data)->pad, data->cursor.Y, data->cursor.X);
99 prefresh(PRIVATE(data)->pad,
100 data->curcfg.win_pos.Y, data->curcfg.win_pos.X,
101 0, 0, data->curcfg.win_height, data->curcfg.win_width);
104 /******************************************************************
105 * WCCURSES_ShapeCursor
107 * Sets a new shape for the cursor
109 void WCCURSES_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
111 /* we can't do much about the size... */
112 data->curcfg.cursor_size = size;
113 data->curcfg.cursor_visible = vis ? TRUE : FALSE;
114 WCCURSES_PosCursor(data);
117 /******************************************************************
118 * WCCURSES_ComputePositions
120 * Recomputes all the components (mainly scroll bars) positions
122 void WCCURSES_ComputePositions(struct inner_data* data)
124 if (PRIVATE(data)->pad) WCCURSES_PosCursor(data);
127 /******************************************************************
130 * Sets the title to the wine console
132 static void WCCURSES_SetTitle(const struct inner_data* data)
136 if (WINECON_GetConsoleTitle(data->hConIn, wbuf, sizeof(wbuf)/sizeof(WCHAR)))
140 WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buffer, sizeof(buffer),
142 fputs("\033]2;", stdout);
143 fputs(buffer, stdout);
149 /******************************************************************
154 static void WCCURSES_Refresh(const struct inner_data* data, int tp, int bm)
161 for (y = tp; y <= bm; y++)
163 cell = &data->cells[y * data->curcfg.sb_width];
164 for (x = 0; x < data->curcfg.sb_width; x++)
166 WideCharToMultiByte(CP_ACP, 0, &cell[x].Char.UnicodeChar, 1,
168 attr = ((BYTE)ch < 32 || (BYTE)ch > 127) ? 32 : (BYTE)ch;
170 if (cell[x].Attributes & FOREGROUND_RED) attr |= COLOR_PAIR(COLOR_RED);
171 if (cell[x].Attributes & FOREGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE);
172 if (cell[x].Attributes & FOREGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN);
173 if (cell[x].Attributes & BACKGROUND_RED) attr |= COLOR_PAIR(COLOR_RED << 3);
174 if (cell[x].Attributes & BACKGROUND_BLUE) attr |= COLOR_PAIR(COLOR_BLUE << 3);
175 if (cell[x].Attributes & BACKGROUND_GREEN) attr |= COLOR_PAIR(COLOR_GREEN << 3);
177 if (cell[x].Attributes & FOREGROUND_INTENSITY) attr |= A_BOLD;
178 PRIVATE(data)->line[x] = attr;
180 mvwaddchnstr(PRIVATE(data)->pad, y, 0, PRIVATE(data)->line, data->curcfg.sb_width);
182 prefresh(PRIVATE(data)->pad,
183 data->curcfg.win_pos.Y, data->curcfg.win_pos.X,
184 0, 0, data->curcfg.win_height, data->curcfg.win_width);
187 /******************************************************************
192 static void WCCURSES_Scroll(struct inner_data* data, int pos, BOOL horz)
196 data->curcfg.win_pos.X = pos;
200 data->curcfg.win_pos.Y = pos;
202 WCCURSES_PosCursor(data);
205 /******************************************************************
210 static void WCCURSES_SetFont(struct inner_data* data, const WCHAR* font,
211 unsigned height, unsigned weight)
213 /* FIXME: really not much to do ? */
216 /******************************************************************
221 static void WCCURSES_ScrollV(struct inner_data* data, int delta)
223 int pos = data->curcfg.win_pos.Y;
226 if (pos < 0) pos = 0;
227 if (pos > data->curcfg.sb_height - data->curcfg.win_height)
228 pos = data->curcfg.sb_height - data->curcfg.win_height;
229 if (pos != data->curcfg.win_pos.Y)
231 data->curcfg.win_pos.Y = pos;
232 WCCURSES_PosCursor(data);
233 WINECON_NotifyWindowChange(data);
237 /* Ascii -> VK, generated by calling VkKeyScanA(i) */
238 static int vkkeyscan_table[256] =
240 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,
241 0,32,305,478,307,308,309,311,222,313,304,312,443,188,189,190,191,48,
242 49,50,51,52,53,54,55,56,57,442,186,444,187,446,447,306,321,322,323,
243 324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
244 341,342,343,344,345,346,219,220,221,310,445,192,65,66,67,68,69,70,71,
245 72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,475,476,477,
246 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,
247 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
248 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
249 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
252 static int mapvkey_0[256] =
254 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,
255 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,
256 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,
257 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,
258 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,
259 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,
260 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,
261 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,
262 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
265 /******************************************************************
266 * WCCURSES_InitComplexChar
270 static inline void WCCURSES_InitComplexChar(INPUT_RECORD* ir, BOOL down, WORD vk, WORD kc, DWORD cks)
272 ir->EventType = KEY_EVENT;
273 ir->Event.KeyEvent.bKeyDown = down;
274 ir->Event.KeyEvent.wRepeatCount = 1;
276 ir->Event.KeyEvent.wVirtualScanCode = vk;
277 ir->Event.KeyEvent.wVirtualKeyCode = kc;
278 ir->Event.KeyEvent.dwControlKeyState = cks;
279 ir->Event.KeyEvent.uChar.UnicodeChar = 0;
282 /******************************************************************
283 * WCCURSES_FillSimpleChar
287 static unsigned WCCURSES_FillSimpleChar(INPUT_RECORD* ir, unsigned real_inchar)
291 unsigned numEvent = 0;
296 case 127: inchar = '\b'; break;
297 case 10: inchar = '\r'; real_inchar = 27; /* so that we don't think key is ctrl- something */ break;
299 /* we assume that ESC & and the second character are atomically generated
300 * otherwise, we'll have a race here
302 if ((inchar = wgetch(stdscr)) != ERR)
304 /* we got a alt-something key... */
305 cks = LEFT_ALT_PRESSED;
311 inchar = real_inchar;
314 if ((inchar & ~0xFF) != 0) WINE_FIXME("What a char (%u)\n", inchar);
315 vk = vkkeyscan_table[inchar];
317 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x2a, 0x10, SHIFT_PRESSED);
318 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
319 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x1d, 0x11, LEFT_CTRL_PRESSED);
321 WCCURSES_InitComplexChar(&ir[numEvent++], 1, 0x38, 0x12, LEFT_ALT_PRESSED);
323 ir[numEvent].EventType = KEY_EVENT;
324 ir[numEvent].Event.KeyEvent.bKeyDown = 1;
325 ir[numEvent].Event.KeyEvent.wRepeatCount = 1;
326 ir[numEvent].Event.KeyEvent.dwControlKeyState = cks;
328 ir[numEvent].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
329 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
330 ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
332 ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
333 ir[numEvent].Event.KeyEvent.wVirtualKeyCode = vk;
334 ir[numEvent].Event.KeyEvent.wVirtualScanCode = mapvkey_0[vk & 0x00ff]; /* VirtualKeyCodes to ScanCode */
335 ir[numEvent].Event.KeyEvent.uChar.UnicodeChar = (unsigned char)inchar;
337 ir[numEvent + 1] = ir[numEvent];
338 ir[numEvent + 1].Event.KeyEvent.bKeyDown = 0;
343 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x38, 0x12, LEFT_ALT_PRESSED);
344 if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
345 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x1d, 0x11, 0);
347 WCCURSES_InitComplexChar(&ir[numEvent++], 0, 0x2a, 0x10, 0);
352 /******************************************************************
353 * WCCURSES_FillComplexChar
357 static unsigned WCCURSES_FillComplexChar(INPUT_RECORD* ir, WORD vk, WORD kc, DWORD cks)
359 WCCURSES_InitComplexChar(&ir[0], 1, vk, kc, ENHANCED_KEY | cks);
360 WCCURSES_InitComplexChar(&ir[1], 0, vk, kc, ENHANCED_KEY | cks);
365 /******************************************************************
370 static unsigned WCCURSES_FillMouse(INPUT_RECORD* ir)
372 static unsigned bstate /* = 0 */;
373 static COORD pos /* = {0, 0} */;
377 if (getmouse(&mevt) == ERR)
380 WINE_TRACE("[%u]: (%d, %d) %08lx\n",
381 mevt.id, mevt.x, mevt.y, (unsigned long)mevt.bstate);
383 /* macros to ease mapping ncurse button numbering to windows's one */
384 #define BTN1_BIT FROM_LEFT_1ST_BUTTON_PRESSED
385 #define BTN2_BIT RIGHTMOST_BUTTON_PRESSED
386 #define BTN3_BIT FROM_LEFT_2ND_BUTTON_PRESSED
387 #define BTN4_BIT 0 /* not done yet */
389 if (mevt.bstate & BUTTON1_PRESSED) bstate |= BTN1_BIT;
390 if (mevt.bstate & BUTTON1_RELEASED) bstate &= ~BTN1_BIT;
391 if (mevt.bstate & BUTTON2_PRESSED) bstate |= BTN2_BIT;
392 if (mevt.bstate & BUTTON2_RELEASED) bstate &= ~BTN2_BIT;
393 if (mevt.bstate & BUTTON3_PRESSED) bstate |= BTN3_BIT;
394 if (mevt.bstate & BUTTON3_RELEASED) bstate &= ~BTN3_BIT;
396 ir->EventType = MOUSE_EVENT;
397 ir->Event.MouseEvent.dwMousePosition.X = mevt.x;
398 ir->Event.MouseEvent.dwMousePosition.Y = mevt.y;
400 ir->Event.MouseEvent.dwButtonState = bstate;
402 /* partial conversion */
403 ir->Event.MouseEvent.dwControlKeyState = 0;
404 if (mevt.bstate & BUTTON_SHIFT) ir->Event.MouseEvent.dwControlKeyState |= SHIFT_PRESSED;
405 /* choose to map to left ctrl... could use both ? */
406 if (mevt.bstate & BUTTON_CTRL) ir->Event.MouseEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
407 /* choose to map to left alt... could use both ? */
408 if (mevt.bstate & BUTTON_ALT) ir->Event.MouseEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
409 /* FIXME: unsupported yet flags: CAPSLOCK_ON, ENHANCED_KEY (??), NUMLOCK_ON, SCROLLLOCK_ON
410 * could be reported from the key events...
413 ir->Event.MouseEvent.dwEventFlags = 0;
414 /* FIXME: we no longer generate double click events */
416 if (!(mevt.bstate & (BUTTON1_PRESSED|BUTTON1_RELEASED|BUTTON2_PRESSED|BUTTON2_RELEASED|BUTTON3_PRESSED|BUTTON3_RELEASED)) &&
417 (mevt.x != pos.X || mevt.y != pos.Y))
419 ir->Event.MouseEvent.dwEventFlags |= MOUSE_MOVED;
421 pos.X = mevt.x; pos.Y = mevt.y;
426 /******************************************************************
431 static unsigned WCCURSES_FillCode(struct inner_data* data, INPUT_RECORD* ir, int inchar)
433 unsigned numEvent = 0;
440 numEvent = WCCURSES_FillComplexChar(ir, 0x50, 0x28, 0);
443 numEvent = WCCURSES_FillComplexChar(ir, 0x48, 0x26, 0);
446 numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, 0);
449 numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, 0);
452 numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, 0);
455 numEvent = WCCURSES_FillSimpleChar(ir, '\b');
458 case KEY_F0: /* up to F63 */
471 numEvent = WCCURSES_FillComplexChar(ir, 0x3b + inchar - KEY_F(1), 0, 0);
475 if (PRIVATE(data)->allow_scroll)
477 WCCURSES_ScrollV(data, inchar == KEY_F(11) ? 8 : -8);
481 numEvent = WCCURSES_FillComplexChar(ir, 0xd9 + inchar - KEY_F(11), 0, 0);
490 numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, 0);
493 numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, 0);
505 numEvent = WCCURSES_FillComplexChar(ir, 0x51, 0x22, 0);
508 numEvent = WCCURSES_FillComplexChar(ir, 0x49, 0x21, 0);
534 numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, 0);
545 numEvent = WCCURSES_FillMouse(ir);
569 numEvent = WCCURSES_FillComplexChar(ir, 0x53, 0x2e, SHIFT_PRESSED);
576 numEvent = WCCURSES_FillComplexChar(ir, 0x4f, 0x23, SHIFT_PRESSED);
586 numEvent = WCCURSES_FillComplexChar(ir, 0x47, 0x24, SHIFT_PRESSED);
589 numEvent = WCCURSES_FillComplexChar(ir, 0x52, 0x2d, SHIFT_PRESSED);
592 numEvent = WCCURSES_FillComplexChar(ir, 0x4b, 0x25, SHIFT_PRESSED);
606 numEvent = WCCURSES_FillComplexChar(ir, 0x4d, 0x27, SHIFT_PRESSED);
616 WINE_FIXME("Not done yet (%o)\n", inchar);
619 WINE_ERR("Unknown val (%o)\n", inchar);
625 /******************************************************************
630 static void WCCURSES_GetEvents(struct inner_data* data)
637 if ((inchar = wgetch(stdscr)) == ERR) {WINE_FIXME("Ooch. somebody beat us\n");return;}
639 WINE_TRACE("Got %d\n", inchar);
641 if (inchar & KEY_CODE_YES)
643 numEvent = WCCURSES_FillCode(data, ir, inchar);
647 numEvent = WCCURSES_FillSimpleChar(ir, inchar);
650 WriteConsoleInput(data->hConIn, ir, numEvent, &n);
653 /******************************************************************
654 * WCCURSES_DeleteBackend
658 static void WCCURSES_DeleteBackend(struct inner_data* data)
662 if (!PRIVATE(data)) return;
664 CloseHandle(PRIVATE(data)->hInput);
666 delwin(PRIVATE(data)->pad);
667 mousemask(PRIVATE(data)->initial_mouse_mask, &mm);
670 HeapFree(GetProcessHeap(), 0, PRIVATE(data)->line);
671 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
672 PRIVATE(data) = NULL;
675 /******************************************************************
680 static int WCCURSES_MainLoop(struct inner_data* data)
684 hin[0] = PRIVATE(data)->hInput;
685 hin[1] = data->hSynchro;
689 unsigned ret = WaitForMultipleObjects(2, hin, FALSE, INFINITE);
693 WCCURSES_GetEvents(data);
695 case WAIT_OBJECT_0+1:
696 if (!WINECON_GrabChanges(data)) return 0;
699 WINE_ERR("got pb\n");
706 /******************************************************************
707 * WCCURSES_InitBackend
709 * Initialisation part II: creation of window.
712 BOOL WCCURSES_InitBackend(struct inner_data* data)
714 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_curse));
715 if (!data->private) return FALSE;
717 data->fnMainLoop = WCCURSES_MainLoop;
718 data->fnPosCursor = WCCURSES_PosCursor;
719 data->fnShapeCursor = WCCURSES_ShapeCursor;
720 data->fnComputePositions = WCCURSES_ComputePositions;
721 data->fnRefresh = WCCURSES_Refresh;
722 data->fnResizeScreenBuffer = WCCURSES_ResizeScreenBuffer;
723 data->fnSetTitle = WCCURSES_SetTitle;
724 data->fnScroll = WCCURSES_Scroll;
725 data->fnSetFont = WCCURSES_SetFont;
726 data->fnDeleteBackend = WCCURSES_DeleteBackend;
728 if (wine_server_fd_to_handle(0, GENERIC_READ|SYNCHRONIZE, FALSE,
729 (obj_handle_t*)&PRIVATE(data)->hInput))
731 WINE_FIXME("Cannot open 0\n");
735 /* FIXME: should find a good way to enable buffer scrolling
736 * For the time being, setting this to 1 will allow scrolling up/down
737 * on buffer with F11/F12.
739 /* data->allow_scroll = 1; */
743 /* creating the basic colors - FIXME intensity not handled yet */
749 for (i = 0; i < 8; i++)
750 for (j = 0; j < 8; j++)
751 init_pair(i | (j << 3), i, j);
756 intrflush(stdscr, FALSE);
757 nodelay(stdscr, TRUE);
758 keypad(stdscr, TRUE);
759 if (data->curcfg.quick_edit)
761 mousemask(BUTTON1_PRESSED|BUTTON1_RELEASED|
762 BUTTON2_PRESSED|BUTTON2_RELEASED|
763 BUTTON3_PRESSED|BUTTON3_RELEASED|
764 BUTTON_SHIFT|BUTTON_CTRL|BUTTON_ALT|REPORT_MOUSE_POSITION,
765 &PRIVATE(data)->initial_mouse_mask);
766 /* no click event generation... we just need button up/down events
767 * it doesn't seem that mouseinterval(-1) behaves as documented...
768 * 0 seems to be better value to disable click event generation
774 mousemask(0, &PRIVATE(data)->initial_mouse_mask);
781 BOOL WCCURSES_InitBackend(struct inner_data* data)