VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxBFE/SDLConsole.cpp@ 4225

Last change on this file since 4225 was 4071, checked in by vboxsync, 18 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.2 KB
Line 
1/** @file
2 *
3 * VBox frontends: Basic Frontend (BFE):
4 * Implementation of SDLConsole class
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_GUI
23#ifdef RT_OS_DARWIN
24# include <Carbon/Carbon.h>
25# define OSType VBoxOSType
26# undef PAGE_SIZE
27# undef PAGE_SHIFT
28#endif
29
30#ifdef VBOXBFE_WITHOUT_COM
31# include "COMDefs.h"
32#else
33# include <VBox/com/defs.h>
34#endif
35#include <VBox/types.h>
36#include <VBox/err.h>
37#include <VBox/param.h>
38#include <VBox/pdm.h>
39#include <VBox/log.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42#include <iprt/runtime.h>
43#include <iprt/assert.h>
44#include <iprt/semaphore.h>
45#include <iprt/stream.h>
46#include <iprt/uuid.h>
47#include <iprt/alloca.h>
48
49#ifdef VBOXBFE_WITH_X11
50# include <X11/Xlib.h>
51# include <X11/Xcursor/Xcursor.h>
52#endif
53
54#include "VBoxBFE.h"
55
56#include <vector>
57
58#include "DisplayImpl.h"
59#include "MouseImpl.h"
60#include "KeyboardImpl.h"
61#include "VMMDevInterface.h"
62#include "Framebuffer.h"
63#include "MachineDebuggerImpl.h"
64
65#include "ConsoleImpl.h"
66#include "SDLConsole.h"
67
68/*******************************************************************************
69* Internal Functions *
70*******************************************************************************/
71
72SDLConsole::SDLConsole() : Console()
73{
74 int rc;
75
76 fInputGrab = false;
77 gpDefaultCursor = NULL;
78 gpCustomCursor = NULL;
79 /** Custom window manager cursor? */
80 gpCustomWMcursor = NULL;
81 mfInitialized = false;
82
83 memset(gaModifiersState, 0, sizeof(gaModifiersState));
84
85 rc = SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
86 if (rc != 0)
87 {
88 RTPrintf("SDL Error: '%s'\n", SDL_GetError());
89 return;
90 }
91
92 /* memorize the default cursor */
93 gpDefaultCursor = SDL_GetCursor();
94 /* create a fake empty cursor */
95 {
96 uint8_t cursorData[1] = {0};
97 gpCustomCursor = SDL_CreateCursor(cursorData, cursorData, 8, 1, 0, 0);
98 gpCustomWMcursor = gpCustomCursor->wm_cursor;
99 gpCustomCursor->wm_cursor = NULL;
100 }
101#ifdef VBOXBFE_WITH_X11
102 /* get Window Manager info */
103 SDL_VERSION(&gSdlInfo.version);
104 if (!SDL_GetWMInfo(&gSdlInfo))
105 {
106 /** @todo: Is this fatal? */
107 AssertMsgFailed(("Error: could not get SDL Window Manager info!\n"));
108 }
109#endif
110
111 /*
112 * Enable keyboard repeats
113 */
114 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
115 mfInitialized = true;
116}
117
118SDLConsole::~SDLConsole()
119{
120 if (fInputGrab)
121 inputGrabEnd();
122}
123
124CONEVENT SDLConsole::eventWait()
125{
126 SDL_Event *ev = &ev1;
127
128 if (SDL_WaitEvent(ev) != 1)
129 {
130 return CONEVENT_QUIT;
131 }
132
133 switch (ev->type)
134 {
135
136 /*
137 * The screen needs to be repainted.
138 */
139 case SDL_VIDEOEXPOSE:
140 {
141 return CONEVENT_SCREENUPDATE;
142 }
143
144 /*
145 * Keyboard events.
146 */
147 case SDL_KEYDOWN:
148 case SDL_KEYUP:
149 {
150 switch (enmHKeyState)
151 {
152 case HKEYSTATE_NORMAL:
153 {
154 if ( ev->type == SDL_KEYDOWN
155 && ev->key.keysym.sym == gHostKeySym
156 && (SDL_GetModState() & ~(KMOD_MODE | KMOD_NUM | KMOD_RESERVED)) == gHostKey)
157 {
158 EvHKeyDown = *ev;
159 enmHKeyState = HKEYSTATE_DOWN;
160 break;
161 }
162 processKey(&ev->key);
163 break;
164 }
165
166 case HKEYSTATE_DOWN:
167 {
168
169 if (ev->type == SDL_KEYDOWN)
170 {
171 /* potential host key combination, try execute it */
172 int rc = handleHostKey(&ev->key);
173 if (rc == VINF_SUCCESS)
174 {
175 enmHKeyState = HKEYSTATE_USED;
176 break;
177 }
178 if (VBOX_SUCCESS(rc))
179 {
180 return CONEVENT_QUIT;
181 }
182 }
183 else /* SDL_KEYUP */
184 {
185 if (ev->key.keysym.sym == gHostKeySym)
186 {
187 /* toggle grabbing state */
188 if (!fInputGrab)
189 {
190 inputGrabStart();
191 }
192 else
193 {
194 inputGrabEnd();
195 }
196
197 /* SDL doesn't always reset the keystates, correct it */
198 resetKeys();
199 enmHKeyState = HKEYSTATE_NORMAL;
200 break;
201 }
202 }
203
204 /* not host key */
205 enmHKeyState = HKEYSTATE_NOT_IT;
206 ev1 = *ev;
207 processKey(&EvHKeyDown.key);
208 processKey(&ev->key);
209 break;
210 }
211
212 case HKEYSTATE_USED:
213 {
214 if ((SDL_GetModState() & ~(KMOD_MODE | KMOD_NUM | KMOD_RESERVED)) == 0)
215 {
216 enmHKeyState = HKEYSTATE_NORMAL;
217 }
218 if (ev->type == SDL_KEYDOWN)
219 {
220 int rc = handleHostKey(&ev->key);
221 if (VBOX_SUCCESS(rc) && rc != VINF_SUCCESS)
222 {
223 return CONEVENT_QUIT;
224 }
225 }
226 break;
227 }
228
229 default:
230 AssertMsgFailed(("enmHKeyState=%d\n", enmHKeyState));
231 /* fall thru */
232 case HKEYSTATE_NOT_IT:
233 {
234 if ((SDL_GetModState() & ~(KMOD_MODE | KMOD_NUM | KMOD_RESERVED)) == 0)
235 {
236 enmHKeyState = HKEYSTATE_NORMAL;
237 }
238 processKey(&ev->key);
239 break;
240 }
241 } /* state switch */
242 break;
243 }
244
245 /*
246 * The window was closed.
247 */
248 case SDL_QUIT:
249 {
250 return CONEVENT_QUIT;
251 }
252
253 /*
254 * The mouse has moved
255 */
256 case SDL_MOUSEMOTION:
257 {
258 if (fInputGrab || gMouse->getAbsoluteCoordinates())
259 {
260 mouseSendEvent(0);
261 }
262 break;
263 }
264
265 /*
266 * A mouse button has been clicked or released.
267 */
268 case SDL_MOUSEBUTTONDOWN:
269 case SDL_MOUSEBUTTONUP:
270 {
271 SDL_MouseButtonEvent *bev = &ev->button;
272 if (!fInputGrab && !gMouse->getAbsoluteCoordinates())
273 {
274 if (ev->type == SDL_MOUSEBUTTONDOWN && (bev->state & SDL_BUTTON_LMASK))
275 {
276 /* start grabbing all events */
277 inputGrabStart();
278 }
279 }
280 else
281 {
282 int dz = 0;
283 if (bev->button == SDL_BUTTON_WHEELUP)
284 {
285 dz = -1;
286 }
287 else if (bev->button == SDL_BUTTON_WHEELDOWN)
288 {
289 dz = 1;
290 }
291 mouseSendEvent(dz);
292 }
293 break;
294 }
295
296 /*
297 * The window has gained or lost focus.
298 */
299 case SDL_ACTIVEEVENT:
300 {
301 if (fInputGrab && (SDL_GetAppState() & SDL_ACTIVEEVENTMASK) == 0)
302 {
303 inputGrabEnd();
304 }
305 break;
306 }
307
308
309 /*
310 * User specific update event.
311 */
312 /** @todo use a common user event handler so that SDL_PeepEvents() won't
313 * possibly remove other events in the queue!
314 */
315 case SDL_USER_EVENT_UPDATERECT:
316 {
317
318 /*
319 * Decode event parameters.
320 */
321 #define DECODEX(ev) ((intptr_t)(ev)->user.data1 >> 16)
322 #define DECODEY(ev) ((intptr_t)(ev)->user.data1 & 0xFFFF)
323 #define DECODEW(ev) ((intptr_t)(ev)->user.data2 >> 16)
324 #define DECODEH(ev) ((intptr_t)(ev)->user.data2 & 0xFFFF)
325 int x = DECODEX(ev);
326 int y = DECODEY(ev);
327 int w = DECODEW(ev);
328 int h = DECODEH(ev);
329 LogFlow(("SDL_USER_EVENT_UPDATERECT: x = %d, y = %d, w = %d, h = %d\n",
330 x, y, w, h));
331
332 Assert(gFramebuffer);
333 /*
334 * Lock the framebuffer, perform the update and lock again
335 */
336 gFramebuffer->Lock();
337 gFramebuffer->update(x, y, w, h);
338 gFramebuffer->Unlock();
339
340 #undef DECODEX
341 #undef DECODEY
342 #undef DECODEW
343 #undef DECODEH
344 break;
345 }
346
347 /*
348 * User specific resize event.
349 */
350 case SDL_USER_EVENT_RESIZE:
351 return CONEVENT_USR_SCREENRESIZE;
352
353 /*
354 * User specific update title bar notification event
355 */
356 case SDL_USER_EVENT_UPDATE_TITLEBAR:
357 return CONEVENT_USR_TITLEBARUPDATE;
358
359 /*
360 * User specific termination event
361 */
362 case SDL_USER_EVENT_TERMINATE:
363 {
364 if (ev->user.code != VBOXSDL_TERM_NORMAL)
365 RTPrintf("Error: VM terminated abnormally!\n");
366 return CONEVENT_USR_QUIT;
367 }
368
369#ifdef VBOX_SECURELABEL
370 /*
371 * User specific secure label update event
372 */
373 case SDL_USER_EVENT_SECURELABEL_UPDATE:
374 return CONEVENT_USR_SECURELABELUPDATE;
375
376#endif /* VBOX_SECURELABEL */
377
378 /*
379 * User specific pointer shape change event
380 */
381 case SDL_USER_EVENT_POINTER_CHANGE:
382 {
383 PointerShapeChangeData *data =
384 (PointerShapeChangeData *) ev->user.data1;
385 setPointerShape (data);
386 delete data;
387 break;
388 }
389
390 default:
391 {
392 printf("%s:%d unknown SDL event %d\n",__FILE__,__LINE__,ev->type);
393 LogBird(("unknown SDL event %d\n", ev->type));
394 break;
395 }
396 }
397 return CONEVENT_NONE;
398}
399
400/**
401 * Push the exit event forcing the main event loop to terminate.
402 */
403void SDLConsole::eventQuit()
404{
405 SDL_Event event;
406
407 event.type = SDL_USEREVENT;
408 event.user.type = SDL_USER_EVENT_TERMINATE;
409 SDL_PushEvent(&event);
410}
411
412#if defined(RT_OS_DARWIN)
413/**
414 * Fallback keycode conversion using SDL symbols.
415 *
416 * This is used to catch keycodes that's missing from the translation table.
417 *
418 * @returns XT scancode
419 * @param ev SDL scancode
420 */
421static uint8_t Keyevent2KeycodeFallback(const SDL_KeyboardEvent *ev)
422{
423 const SDLKey sym = ev->keysym.sym;
424 Log(("SDL key event: sym=%d scancode=%#x unicode=%#x\n",
425 sym, ev->keysym.scancode, ev->keysym.unicode));
426 switch (sym)
427 { /* set 1 scan code */
428 case SDLK_ESCAPE: return 0x01;
429 case SDLK_EXCLAIM:
430 case SDLK_1: return 0x02;
431 case SDLK_AT:
432 case SDLK_2: return 0x03;
433 case SDLK_HASH:
434 case SDLK_3: return 0x04;
435 case SDLK_DOLLAR:
436 case SDLK_4: return 0x05;
437 /* % */
438 case SDLK_5: return 0x06;
439 case SDLK_CARET:
440 case SDLK_6: return 0x07;
441 case SDLK_AMPERSAND:
442 case SDLK_7: return 0x08;
443 case SDLK_ASTERISK:
444 case SDLK_8: return 0x09;
445 case SDLK_LEFTPAREN:
446 case SDLK_9: return 0x0a;
447 case SDLK_RIGHTPAREN:
448 case SDLK_0: return 0x0b;
449 case SDLK_UNDERSCORE:
450 case SDLK_MINUS: return 0x0c;
451 case SDLK_EQUALS:
452 case SDLK_PLUS: return 0x0d;
453 case SDLK_BACKSPACE: return 0x0e;
454 case SDLK_TAB: return 0x0f;
455 case SDLK_q: return 0x10;
456 case SDLK_w: return 0x11;
457 case SDLK_e: return 0x12;
458 case SDLK_r: return 0x13;
459 case SDLK_t: return 0x14;
460 case SDLK_y: return 0x15;
461 case SDLK_u: return 0x16;
462 case SDLK_i: return 0x17;
463 case SDLK_o: return 0x18;
464 case SDLK_p: return 0x19;
465 case SDLK_LEFTBRACKET: return 0x1a;
466 case SDLK_RIGHTBRACKET: return 0x1b;
467 case SDLK_RETURN: return 0x1c;
468 case SDLK_KP_ENTER: return 0x1c | 0x80;
469 case SDLK_LCTRL: return 0x1d;
470 case SDLK_RCTRL: return 0x1d | 0x80;
471 case SDLK_a: return 0x1e;
472 case SDLK_s: return 0x1f;
473 case SDLK_d: return 0x20;
474 case SDLK_f: return 0x21;
475 case SDLK_g: return 0x22;
476 case SDLK_h: return 0x23;
477 case SDLK_j: return 0x24;
478 case SDLK_k: return 0x25;
479 case SDLK_l: return 0x26;
480 case SDLK_COLON:
481 case SDLK_SEMICOLON: return 0x27;
482 case SDLK_QUOTEDBL:
483 case SDLK_QUOTE: return 0x28;
484 case SDLK_BACKQUOTE: return 0x29;
485 case SDLK_LSHIFT: return 0x2a;
486 case SDLK_BACKSLASH: return 0x2b;
487 case SDLK_z: return 0x2c;
488 case SDLK_x: return 0x2d;
489 case SDLK_c: return 0x2e;
490 case SDLK_v: return 0x2f;
491 case SDLK_b: return 0x30;
492 case SDLK_n: return 0x31;
493 case SDLK_m: return 0x32;
494 case SDLK_LESS:
495 case SDLK_COMMA: return 0x33;
496 case SDLK_GREATER:
497 case SDLK_PERIOD: return 0x34;
498 case SDLK_KP_DIVIDE: /*??*/
499 case SDLK_QUESTION:
500 case SDLK_SLASH: return 0x35;
501 case SDLK_RSHIFT: return 0x36;
502 case SDLK_KP_MULTIPLY:
503 case SDLK_PRINT: return 0x37; /* fixme */
504 case SDLK_LALT: return 0x38;
505 case SDLK_MODE: /* alt gr*/
506 case SDLK_RALT: return 0x38 | 0x80;
507 case SDLK_SPACE: return 0x39;
508 case SDLK_CAPSLOCK: return 0x3a;
509 case SDLK_F1: return 0x3b;
510 case SDLK_F2: return 0x3c;
511 case SDLK_F3: return 0x3d;
512 case SDLK_F4: return 0x3e;
513 case SDLK_F5: return 0x3f;
514 case SDLK_F6: return 0x40;
515 case SDLK_F7: return 0x41;
516 case SDLK_F8: return 0x42;
517 case SDLK_F9: return 0x43;
518 case SDLK_F10: return 0x44;
519 case SDLK_PAUSE: return 0x45; /* not right */
520 case SDLK_NUMLOCK: return 0x45;
521 case SDLK_SCROLLOCK: return 0x46;
522 case SDLK_KP7: return 0x47;
523 case SDLK_HOME: return 0x47 | 0x80;
524 case SDLK_KP8: return 0x48;
525 case SDLK_UP: return 0x48 | 0x80;
526 case SDLK_KP9: return 0x49;
527 case SDLK_PAGEUP: return 0x49 | 0x80;
528 case SDLK_KP_MINUS: return 0x4a;
529 case SDLK_KP4: return 0x4b;
530 case SDLK_LEFT: return 0x4b | 0x80;
531 case SDLK_KP5: return 0x4c;
532 case SDLK_KP6: return 0x4d;
533 case SDLK_RIGHT: return 0x4d | 0x80;
534 case SDLK_KP_PLUS: return 0x4e;
535 case SDLK_KP1: return 0x4f;
536 case SDLK_END: return 0x4f | 0x80;
537 case SDLK_KP2: return 0x50;
538 case SDLK_DOWN: return 0x50 | 0x80;
539 case SDLK_KP3: return 0x51;
540 case SDLK_PAGEDOWN: return 0x51 | 0x80;
541 case SDLK_KP0: return 0x52;
542 case SDLK_INSERT: return 0x52 | 0x80;
543 case SDLK_KP_PERIOD: return 0x53;
544 case SDLK_DELETE: return 0x53 | 0x80;
545 case SDLK_SYSREQ: return 0x54;
546 case SDLK_F11: return 0x57;
547 case SDLK_F12: return 0x58;
548 case SDLK_F13: return 0x5b;
549 case SDLK_LMETA:
550 case SDLK_LSUPER: return 0x5b | 0x80;
551 case SDLK_F14: return 0x5c;
552 case SDLK_RMETA:
553 case SDLK_RSUPER: return 0x5c | 0x80;
554 case SDLK_F15: return 0x5d;
555 case SDLK_MENU: return 0x5d | 0x80;
556#if 0
557 case SDLK_CLEAR: return 0x;
558 case SDLK_KP_EQUALS: return 0x;
559 case SDLK_COMPOSE: return 0x;
560 case SDLK_HELP: return 0x;
561 case SDLK_BREAK: return 0x;
562 case SDLK_POWER: return 0x;
563 case SDLK_EURO: return 0x;
564 case SDLK_UNDO: return 0x;
565#endif
566 default:
567 Log(("Unhandled sdl key event: sym=%d scancode=%#x unicode=%#x\n",
568 ev->keysym.sym, ev->keysym.scancode, ev->keysym.unicode));
569 return 0;
570 }
571}
572#endif /* RT_OS_DARWIN */
573
574/**
575 * Converts an SDL keyboard eventcode to a XT scancode.
576 *
577 * @returns XT scancode
578 * @param ev SDL scancode
579 */
580uint8_t SDLConsole::keyEventToKeyCode(const SDL_KeyboardEvent *ev)
581{
582 int keycode;
583
584 // start with the scancode determined by SDL
585 keycode = ev->keysym.scancode;
586
587#ifdef VBOXBFE_WITH_X11 /// @todo verify that these are X11 issues and not unique linux issues.
588 // workaround for SDL keyboard translation issues on X11
589 static const uint8_t x_keycode_to_pc_keycode[61] =
590 {
591 0xc7, /* 97 Home */
592 0xc8, /* 98 Up */
593 0xc9, /* 99 PgUp */
594 0xcb, /* 100 Left */
595 0x4c, /* 101 KP-5 */
596 0xcd, /* 102 Right */
597 0xcf, /* 103 End */
598 0xd0, /* 104 Down */
599 0xd1, /* 105 PgDn */
600 0xd2, /* 106 Ins */
601 0xd3, /* 107 Del */
602 0x9c, /* 108 Enter */
603 0x9d, /* 109 Ctrl-R */
604 0x0, /* 110 Pause */
605 0xb7, /* 111 Print */
606 0xb5, /* 112 Divide */
607 0xb8, /* 113 Alt-R */
608 0xc6, /* 114 Break */
609 0x0, /* 115 */
610 0x0, /* 116 */
611 0x0, /* 117 */
612 0x0, /* 118 */
613 0x0, /* 119 */
614 0x70, /* 120 Hiragana_Katakana */
615 0x0, /* 121 */
616 0x0, /* 122 */
617 0x73, /* 123 backslash */
618 0x0, /* 124 */
619 0x0, /* 125 */
620 0x0, /* 126 */
621 0x0, /* 127 */
622 0x0, /* 128 */
623 0x79, /* 129 Henkan */
624 0x0, /* 130 */
625 0x7b, /* 131 Muhenkan */
626 0x0, /* 132 */
627 0x7d, /* 133 Yen */
628 0x0, /* 134 */
629 0x0, /* 135 */
630 0x47, /* 136 KP_7 */
631 0x48, /* 137 KP_8 */
632 0x49, /* 138 KP_9 */
633 0x4b, /* 139 KP_4 */
634 0x4c, /* 140 KP_5 */
635 0x4d, /* 141 KP_6 */
636 0x4f, /* 142 KP_1 */
637 0x50, /* 143 KP_2 */
638 0x51, /* 144 KP_3 */
639 0x52, /* 145 KP_0 */
640 0x53, /* 146 KP_. */
641 0x47, /* 147 KP_HOME */
642 0x48, /* 148 KP_UP */
643 0x49, /* 149 KP_PgUp */
644 0x4b, /* 150 KP_Left */
645 0x4c, /* 151 KP_ */
646 0x4d, /* 152 KP_Right */
647 0x4f, /* 153 KP_End */
648 0x50, /* 154 KP_Down */
649 0x51, /* 155 KP_PgDn */
650 0x52, /* 156 KP_Ins */
651 0x53, /* 157 KP_Del */
652 };
653
654 if (keycode < 9)
655 {
656 keycode = 0;
657 }
658 else if (keycode < 97)
659 {
660 // just an offset
661 keycode -= 8;
662 }
663 else if (keycode < 158)
664 {
665 // apply conversion table
666 keycode = x_keycode_to_pc_keycode[keycode - 97];
667 }
668 else
669 {
670 keycode = 0;
671 }
672
673#elif defined(RT_OS_DARWIN)
674 /* This is derived partially from SDL_QuartzKeys.h and partially from testing. */
675 static const uint8_t s_aMacToSet1[] =
676 {
677 /* set-1 SDL_QuartzKeys.h */
678 0x1e, /* QZ_a 0x00 */
679 0x1f, /* QZ_s 0x01 */
680 0x20, /* QZ_d 0x02 */
681 0x21, /* QZ_f 0x03 */
682 0x23, /* QZ_h 0x04 */
683 0x22, /* QZ_g 0x05 */
684 0x2c, /* QZ_z 0x06 */
685 0x2d, /* QZ_x 0x07 */
686 0x2e, /* QZ_c 0x08 */
687 0x2f, /* QZ_v 0x09 */
688 0x56, /* between lshift and z. 'INT 1'? */
689 0x30, /* QZ_b 0x0B */
690 0x10, /* QZ_q 0x0C */
691 0x11, /* QZ_w 0x0D */
692 0x12, /* QZ_e 0x0E */
693 0x13, /* QZ_r 0x0F */
694 0x15, /* QZ_y 0x10 */
695 0x14, /* QZ_t 0x11 */
696 0x02, /* QZ_1 0x12 */
697 0x03, /* QZ_2 0x13 */
698 0x04, /* QZ_3 0x14 */
699 0x05, /* QZ_4 0x15 */
700 0x07, /* QZ_6 0x16 */
701 0x06, /* QZ_5 0x17 */
702 0x0d, /* QZ_EQUALS 0x18 */
703 0x0a, /* QZ_9 0x19 */
704 0x08, /* QZ_7 0x1A */
705 0x0c, /* QZ_MINUS 0x1B */
706 0x09, /* QZ_8 0x1C */
707 0x0b, /* QZ_0 0x1D */
708 0x1b, /* QZ_RIGHTBRACKET 0x1E */
709 0x18, /* QZ_o 0x1F */
710 0x16, /* QZ_u 0x20 */
711 0x1a, /* QZ_LEFTBRACKET 0x21 */
712 0x17, /* QZ_i 0x22 */
713 0x19, /* QZ_p 0x23 */
714 0x1c, /* QZ_RETURN 0x24 */
715 0x26, /* QZ_l 0x25 */
716 0x24, /* QZ_j 0x26 */
717 0x28, /* QZ_QUOTE 0x27 */
718 0x25, /* QZ_k 0x28 */
719 0x27, /* QZ_SEMICOLON 0x29 */
720 0x2b, /* QZ_BACKSLASH 0x2A */
721 0x33, /* QZ_COMMA 0x2B */
722 0x35, /* QZ_SLASH 0x2C */
723 0x31, /* QZ_n 0x2D */
724 0x32, /* QZ_m 0x2E */
725 0x34, /* QZ_PERIOD 0x2F */
726 0x0f, /* QZ_TAB 0x30 */
727 0x39, /* QZ_SPACE 0x31 */
728 0x29, /* QZ_BACKQUOTE 0x32 */
729 0x0e, /* QZ_BACKSPACE 0x33 */
730 0x9c, /* QZ_IBOOK_ENTER 0x34 */
731 0x01, /* QZ_ESCAPE 0x35 */
732 0x5c|0x80, /* QZ_RMETA 0x36 */
733 0x5b|0x80, /* QZ_LMETA 0x37 */
734 0x2a, /* QZ_LSHIFT 0x38 */
735 0x3a, /* QZ_CAPSLOCK 0x39 */
736 0x38, /* QZ_LALT 0x3A */
737 0x1d, /* QZ_LCTRL 0x3B */
738 0x36, /* QZ_RSHIFT 0x3C */
739 0x38|0x80, /* QZ_RALT 0x3D */
740 0x1d|0x80, /* QZ_RCTRL 0x3E */
741 0, /* */
742 0, /* */
743 0x53, /* QZ_KP_PERIOD 0x41 */
744 0, /* */
745 0x37, /* QZ_KP_MULTIPLY 0x43 */
746 0, /* */
747 0x4e, /* QZ_KP_PLUS 0x45 */
748 0, /* */
749 0x45, /* QZ_NUMLOCK 0x47 */
750 0, /* */
751 0, /* */
752 0, /* */
753 0x35|0x80, /* QZ_KP_DIVIDE 0x4B */
754 0x1c|0x80, /* QZ_KP_ENTER 0x4C */
755 0, /* */
756 0x4a, /* QZ_KP_MINUS 0x4E */
757 0, /* */
758 0, /* */
759 0x0d/*?*/, /* QZ_KP_EQUALS 0x51 */
760 0x52, /* QZ_KP0 0x52 */
761 0x4f, /* QZ_KP1 0x53 */
762 0x50, /* QZ_KP2 0x54 */
763 0x51, /* QZ_KP3 0x55 */
764 0x4b, /* QZ_KP4 0x56 */
765 0x4c, /* QZ_KP5 0x57 */
766 0x4d, /* QZ_KP6 0x58 */
767 0x47, /* QZ_KP7 0x59 */
768 0, /* */
769 0x48, /* QZ_KP8 0x5B */
770 0x49, /* QZ_KP9 0x5C */
771 0, /* */
772 0, /* */
773 0, /* */
774 0x3f, /* QZ_F5 0x60 */
775 0x40, /* QZ_F6 0x61 */
776 0x41, /* QZ_F7 0x62 */
777 0x3d, /* QZ_F3 0x63 */
778 0x42, /* QZ_F8 0x64 */
779 0x43, /* QZ_F9 0x65 */
780 0, /* */
781 0x57, /* QZ_F11 0x67 */
782 0, /* */
783 0x37|0x80, /* QZ_PRINT / F13 0x69 */
784 0x63, /* QZ_F16 0x6A */
785 0x46, /* QZ_SCROLLOCK 0x6B */
786 0, /* */
787 0x44, /* QZ_F10 0x6D */
788 0x5d|0x80, /* */
789 0x58, /* QZ_F12 0x6F */
790 0, /* */
791 0/* 0xe1,0x1d,0x45*/, /* QZ_PAUSE 0x71 */
792 0x52|0x80, /* QZ_INSERT / HELP 0x72 */
793 0x47|0x80, /* QZ_HOME 0x73 */
794 0x49|0x80, /* QZ_PAGEUP 0x74 */
795 0x53|0x80, /* QZ_DELETE 0x75 */
796 0x3e, /* QZ_F4 0x76 */
797 0x4f|0x80, /* QZ_END 0x77 */
798 0x3c, /* QZ_F2 0x78 */
799 0x51|0x80, /* QZ_PAGEDOWN 0x79 */
800 0x3b, /* QZ_F1 0x7A */
801 0x4b|0x80, /* QZ_LEFT 0x7B */
802 0x4d|0x80, /* QZ_RIGHT 0x7C */
803 0x50|0x80, /* QZ_DOWN 0x7D */
804 0x48|0x80, /* QZ_UP 0x7E */
805 0x5e|0x80, /* QZ_POWER 0x7F */ /* have different break key! */
806 };
807
808 if (keycode == 0)
809 {
810 /* This could be a modifier or it could be 'a'. */
811 switch (ev->keysym.sym)
812 {
813 case SDLK_LSHIFT: keycode = 0x2a; break;
814 case SDLK_RSHIFT: keycode = 0x36; break;
815 case SDLK_LCTRL: keycode = 0x1d; break;
816 case SDLK_RCTRL: keycode = 0x1d | 0x80; break;
817 case SDLK_LALT: keycode = 0x38; break;
818 case SDLK_MODE: /* alt gr */
819 case SDLK_RALT: keycode = 0x38 | 0x80; break;
820 case SDLK_RMETA:
821 case SDLK_RSUPER: keycode = 0x5c | 0x80; break;
822 case SDLK_LMETA:
823 case SDLK_LSUPER: keycode = 0x5b | 0x80; break;
824 /* Sssumes normal key. */
825 default: keycode = s_aMacToSet1[keycode]; break;
826 }
827 }
828 else
829 {
830 if ((unsigned)keycode < RT_ELEMENTS(s_aMacToSet1))
831 keycode = s_aMacToSet1[keycode];
832 else
833 keycode = 0;
834 if (!keycode)
835 {
836#ifdef DEBUG_bird
837 RTPrintf("Untranslated: keycode=%#x (%d)\n", keycode, keycode);
838#endif
839 keycode = Keyevent2KeycodeFallback(ev);
840 }
841 }
842#ifdef DEBUG_bird
843 RTPrintf("scancode=%#x -> %#x\n", ev->keysym.scancode, keycode);
844#endif
845
846#endif
847 return keycode;
848}
849
850/**
851 * Releases any modifier keys that are currently in pressed state.
852 */
853void SDLConsole::resetKeys(void)
854{
855 int i;
856 for(i = 0; i < 256; i++)
857 {
858 if (gaModifiersState[i])
859 {
860 if (i & 0x80)
861 gKeyboard->PutScancode(0xe0);
862 gKeyboard->PutScancode(i | 0x80);
863 gaModifiersState[i] = 0;
864 }
865 }
866}
867
868/**
869 * Keyboard event handler.
870 *
871 * @param ev SDL keyboard event.
872 */
873void SDLConsole::processKey(SDL_KeyboardEvent *ev)
874{
875 int keycode, v;
876
877#if defined(VBOXSDL_ADVANCED_OPTIONS) && defined(DEBUG) && 0
878#error
879 // first handle the debugger hotkeys
880 uint8_t *keystate = SDL_GetKeyState(NULL);
881 if ((keystate[SDLK_LALT]) && (ev->type == SDL_KEYDOWN))
882 {
883 switch (ev->keysym.sym)
884 {
885 // pressing Alt-F12 toggles the supervisor recompiler
886 case SDLK_F12:
887 {
888 if (gMachineDebugger)
889 {
890 BOOL recompileSupervisor;
891 gMachineDebugger->COMGETTER(RecompileSupervisor)(&recompileSupervisor);
892 gMachineDebugger->COMSETTER(RecompileSupervisor)(!recompileSupervisor);
893 }
894 break;
895 }
896 // pressing Alt-F11 toggles the user recompiler
897 case SDLK_F11:
898 {
899 if (gMachineDebugger)
900 {
901 BOOL recompileUser;
902 gMachineDebugger->COMGETTER(RecompileUser)(&recompileUser);
903 gMachineDebugger->COMSETTER(RecompileUser)(!recompileUser);
904 }
905 break;
906 }
907 // pressing Alt-F10 toggles the patch manager
908 case SDLK_F10:
909 {
910 if (gMachineDebugger)
911 {
912 BOOL patmEnabled;
913 gMachineDebugger->COMGETTER(PATMEnabled)(&patmEnabled);
914 gMachineDebugger->COMSETTER(PATMEnabled)(!patmEnabled);
915 }
916 break;
917 }
918 // pressing Alt-F9 toggles CSAM
919 case SDLK_F9:
920 {
921 if (gMachineDebugger)
922 {
923 BOOL csamEnabled;
924 gMachineDebugger->COMGETTER(CSAMEnabled)(&csamEnabled);
925 gMachineDebugger->COMSETTER(CSAMEnabled)(!csamEnabled);
926 }
927 break;
928 }
929 // pressing Alt-F8 toggles singlestepping mode
930 case SDLK_F8:
931 {
932 if (gMachineDebugger)
933 {
934 BOOL singlestepEnabled;
935 gMachineDebugger->COMGETTER(Singlestep)(&singlestepEnabled);
936 gMachineDebugger->COMSETTER(Singlestep)(!singlestepEnabled);
937 }
938 break;
939 }
940
941 default:
942 break;
943 }
944 }
945 // pressing Ctrl-F12 toggles the logger
946 else if ((keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL]) &&
947 (ev->keysym.sym == SDLK_F12) && (ev->type == SDL_KEYDOWN))
948 {
949 PRTLOGGER pLogger = RTLogDefaultInstance();
950 bool fEnabled = (pLogger && !(pLogger->fFlags & RTLOGFLAGS_DISABLED));
951 if (fEnabled)
952 {
953 RTLogFlags(pLogger, "disabled");
954 }
955 else
956 {
957 RTLogFlags(pLogger, "nodisabled");
958 }
959 }
960 // pressing F12 sets a logmark
961 else if ((ev->keysym.sym == SDLK_F12) && (ev->type == SDL_KEYDOWN))
962 {
963 RTLogPrintf("****** LOGGING MARK ******\n");
964 RTLogFlush(NULL);
965 }
966 // now update the titlebar flags
967 updateTitlebar();
968#endif
969
970 // the pause key is the weirdest, needs special handling
971 if (ev->keysym.sym == SDLK_PAUSE)
972 {
973 v = 0;
974 if (ev->type == SDL_KEYUP)
975 v |= 0x80;
976 gKeyboard->PutScancode(0xe1);
977 gKeyboard->PutScancode(0x1d | v);
978 gKeyboard->PutScancode(0x45 | v);
979 return;
980 }
981
982 /*
983 * Perform SDL key event to scancode conversion
984 */
985 keycode = keyEventToKeyCode(ev);
986
987 switch(keycode)
988 {
989 case 0x00:
990 {
991 /* sent when leaving window: reset the modifiers state */
992 resetKeys();
993 return;
994 }
995
996 case 0x2a: /* Left Shift */
997 case 0x36: /* Right Shift */
998 case 0x1d: /* Left CTRL */
999 case 0x9d: /* Right CTRL */
1000 case 0x38: /* Left ALT */
1001 case 0xb8: /* Right ALT */
1002 {
1003 if (ev->type == SDL_KEYUP)
1004 gaModifiersState[keycode] = 0;
1005 else
1006 gaModifiersState[keycode] = 1;
1007 break;
1008 }
1009
1010 case 0x45: /* num lock */
1011 case 0x3a: /* caps lock */
1012 {
1013 /* SDL does not send the key up event, so we generate it */
1014 gKeyboard->PutScancode(keycode);
1015 gKeyboard->PutScancode(keycode | 0x80);
1016 return;
1017 }
1018 }
1019
1020 /*
1021 * Now we send the event. Apply extended and release prefixes.
1022 */
1023 if (keycode & 0x80)
1024 gKeyboard->PutScancode(0xe0);
1025 if (ev->type == SDL_KEYUP)
1026 gKeyboard->PutScancode(keycode | 0x80);
1027 else
1028 gKeyboard->PutScancode(keycode & 0x7f);
1029}
1030
1031#ifdef RT_OS_DARWIN
1032
1033__BEGIN_DECLS
1034/* Private interface in 10.3 and later. */
1035typedef int CGSConnection;
1036typedef enum
1037{
1038 kCGSGlobalHotKeyEnable = 0,
1039 kCGSGlobalHotKeyDisable,
1040 kCGSGlobalHotKeyInvalid = -1 /* bird */
1041} CGSGlobalHotKeyOperatingMode;
1042extern CGSConnection _CGSDefaultConnection(void);
1043extern CGError CGSGetGlobalHotKeyOperatingMode(CGSConnection Connection, CGSGlobalHotKeyOperatingMode *enmMode);
1044extern CGError CGSSetGlobalHotKeyOperatingMode(CGSConnection Connection, CGSGlobalHotKeyOperatingMode enmMode);
1045__END_DECLS
1046
1047/** Keeping track of whether we disabled the hotkeys or not. */
1048static bool g_fHotKeysDisabled = false;
1049/** Whether we've connected or not. */
1050static bool g_fConnectedToCGS = false;
1051/** Cached connection. */
1052static CGSConnection g_CGSConnection;
1053
1054/**
1055 * Disables or enabled global hot keys.
1056 */
1057static void DisableGlobalHotKeys(bool fDisable)
1058{
1059 if (!g_fConnectedToCGS)
1060 {
1061 g_CGSConnection = _CGSDefaultConnection();
1062 g_fConnectedToCGS = true;
1063 }
1064
1065 /* get current mode. */
1066 CGSGlobalHotKeyOperatingMode enmMode = kCGSGlobalHotKeyInvalid;
1067 CGSGetGlobalHotKeyOperatingMode(g_CGSConnection, &enmMode);
1068
1069 /* calc new mode. */
1070 if (fDisable)
1071 {
1072 if (enmMode != kCGSGlobalHotKeyEnable)
1073 return;
1074 enmMode = kCGSGlobalHotKeyDisable;
1075 }
1076 else
1077 {
1078 if ( enmMode != kCGSGlobalHotKeyDisable
1079 /*|| !g_fHotKeysDisabled*/)
1080 return;
1081 enmMode = kCGSGlobalHotKeyEnable;
1082 }
1083
1084 /* try set it and check the actual result. */
1085 CGSSetGlobalHotKeyOperatingMode(g_CGSConnection, enmMode);
1086 CGSGlobalHotKeyOperatingMode enmNewMode = kCGSGlobalHotKeyInvalid;
1087 CGSGetGlobalHotKeyOperatingMode(g_CGSConnection, &enmNewMode);
1088 if (enmNewMode == enmMode)
1089 g_fHotKeysDisabled = enmMode == kCGSGlobalHotKeyDisable;
1090}
1091#endif /* RT_OS_DARWIN */
1092
1093/**
1094 * Start grabbing the mouse.
1095 */
1096void SDLConsole::inputGrabStart()
1097{
1098#ifdef RT_OS_DARWIN
1099 DisableGlobalHotKeys(true);
1100#endif
1101 if (!gMouse->getNeedsHostCursor())
1102 SDL_ShowCursor(SDL_DISABLE);
1103 SDL_WM_GrabInput(SDL_GRAB_ON);
1104 // dummy read to avoid moving the mouse
1105 SDL_GetRelativeMouseState(NULL, NULL);
1106 fInputGrab = 1;
1107 updateTitlebar();
1108}
1109
1110/**
1111 * End mouse grabbing.
1112 */
1113void SDLConsole::inputGrabEnd()
1114{
1115 SDL_WM_GrabInput(SDL_GRAB_OFF);
1116 if (!gMouse->getNeedsHostCursor())
1117 SDL_ShowCursor(SDL_ENABLE);
1118#ifdef RT_OS_DARWIN
1119 DisableGlobalHotKeys(false);
1120#endif
1121 fInputGrab = 0;
1122 updateTitlebar();
1123}
1124
1125/**
1126 * Query mouse position and button state from SDL and send to the VM
1127 *
1128 * @param dz Relative mouse wheel movement
1129 */
1130
1131extern int GetRelativeMouseState(int *, int*);
1132extern int GetMouseState(int *, int*);
1133
1134void SDLConsole::mouseSendEvent(int dz)
1135{
1136 int x, y, state, buttons;
1137 bool abs;
1138
1139 abs = (gMouse->getAbsoluteCoordinates() && !fInputGrab) || gMouse->getNeedsHostCursor();
1140
1141 state = abs ? SDL_GetMouseState(&x, &y) : SDL_GetRelativeMouseState(&x, &y);
1142
1143 // process buttons
1144 buttons = 0;
1145 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
1146 buttons |= PDMIMOUSEPORT_BUTTON_LEFT;
1147 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
1148 buttons |= PDMIMOUSEPORT_BUTTON_RIGHT;
1149 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
1150 buttons |= PDMIMOUSEPORT_BUTTON_MIDDLE;
1151
1152 // now send the mouse event
1153 if (abs)
1154 {
1155 /**
1156 * @todo
1157 * PutMouseEventAbsolute() expects x and y starting from 1,1.
1158 * should we do the increment internally in PutMouseEventAbsolute()
1159 * or state it in PutMouseEventAbsolute() docs?
1160 */
1161 /* only send if outside the extra offset area */
1162 if (y >= gFramebuffer->getYOffset())
1163 gMouse->PutMouseEventAbsolute(x + 1, y + 1 - gFramebuffer->getYOffset(), dz, buttons);
1164 }
1165 else
1166 {
1167 gMouse->PutMouseEvent(x, y, dz, buttons);
1168 }
1169}
1170
1171/**
1172 * Update the pointer shape or visibility.
1173 *
1174 * This is called when the mouse pointer shape changes or pointer is
1175 * hidden/displaying. The new shape is passed as a caller allocated
1176 * buffer that will be freed after returning.
1177 *
1178 * @param fVisible Whether the pointer is visible or not.
1179 * @param fAlpha Alpha channel information is present.
1180 * @param xHot Horizontal coordinate of the pointer hot spot.
1181 * @param yHot Vertical coordinate of the pointer hot spot.
1182 * @param width Pointer width in pixels.
1183 * @param height Pointer height in pixels.
1184 * @param pShape The shape buffer. If NULL, then only
1185 * pointer visibility is being changed
1186 */
1187void SDLConsole::onMousePointerShapeChange(bool fVisible,
1188 bool fAlpha, uint32_t xHot,
1189 uint32_t yHot, uint32_t width,
1190 uint32_t height, void *pShape)
1191{
1192 PointerShapeChangeData *data;
1193 data = new PointerShapeChangeData (fVisible, fAlpha, xHot, yHot,
1194 width, height, (const uint8_t *) pShape);
1195 Assert (data);
1196 if (!data)
1197 return;
1198
1199 SDL_Event event = {0};
1200 event.type = SDL_USEREVENT;
1201 event.user.type = SDL_USER_EVENT_POINTER_CHANGE;
1202 event.user.data1 = data;
1203
1204 int rc = SDL_PushEvent (&event);
1205 AssertMsg (!rc, ("Error: SDL_PushEvent was not successful!\n"));
1206 if (rc)
1207 delete data;
1208}
1209
1210/**
1211 * Build the titlebar string
1212 */
1213void SDLConsole::updateTitlebar()
1214{
1215 char title[1024];
1216
1217 strcpy(title, "innotek VirtualBox");
1218
1219 if (machineState == VMSTATE_SUSPENDED)
1220 strcat(title, " - [Paused]");
1221
1222 if (fInputGrab)
1223 strcat(title, " - [Input captured]");
1224
1225#if defined(VBOXSDL_ADVANCED_OPTIONS) && defined(DEBUG)
1226 // do we have a debugger interface
1227 if (gMachineDebugger)
1228 {
1229 // query the machine state
1230 BOOL recompileSupervisor = FALSE;
1231 BOOL recompileUser = FALSE;
1232 BOOL patmEnabled = FALSE;
1233 BOOL csamEnabled = FALSE;
1234 BOOL singlestepEnabled = FALSE;
1235 gMachineDebugger->COMGETTER(RecompileSupervisor)(&recompileSupervisor);
1236 gMachineDebugger->COMGETTER(RecompileUser)(&recompileUser);
1237 gMachineDebugger->COMGETTER(PATMEnabled)(&patmEnabled);
1238 gMachineDebugger->COMGETTER(CSAMEnabled)(&csamEnabled);
1239 gMachineDebugger->COMGETTER(Singlestep)(&singlestepEnabled);
1240 PRTLOGGER pLogger = RTLogDefaultInstance();
1241 bool fEnabled = (pLogger && !(pLogger->fFlags & RTLOGFLAGS_DISABLED));
1242 RTStrPrintf(title + strlen(title), sizeof(title) - strlen(title),
1243 " [STEP=%d CS=%d PAT=%d RR0=%d RR3=%d LOG=%d]",
1244 singlestepEnabled == TRUE, csamEnabled == TRUE, patmEnabled == TRUE,
1245 recompileSupervisor == FALSE, recompileUser == FALSE, fEnabled == TRUE);
1246 }
1247#endif /* DEBUG */
1248
1249 SDL_WM_SetCaption(title, "innotek VirtualBox");
1250}
1251
1252/**
1253 * Updates the title bar while saving the state.
1254 * @param iPercent Percentage.
1255 */
1256void SDLConsole::updateTitlebarSave(int iPercent)
1257{
1258 char szTitle[256];
1259 AssertMsg(iPercent >= 0 && iPercent <= 100, ("%d\n", iPercent));
1260 RTStrPrintf(szTitle, sizeof(szTitle), "innotek VirtualBox - Saving %d%%...", iPercent);
1261 SDL_WM_SetCaption(szTitle, "innotek VirtualBox");
1262}
1263
1264/**
1265 * Sets the pointer shape according to parameters.
1266 * Must be called only from the main SDL thread.
1267 */
1268void SDLConsole::setPointerShape (const PointerShapeChangeData *data)
1269{
1270 /*
1271 * don't do anything if there are no guest additions loaded (anymore)
1272 */
1273 if (!gMouse->getAbsoluteCoordinates())
1274 return;
1275
1276 if (data->shape)
1277 {
1278 bool ok = false;
1279
1280 uint32_t andMaskSize = (data->width + 7) / 8 * data->height;
1281 uint32_t srcShapePtrScan = data->width * 4;
1282
1283 const uint8_t *srcAndMaskPtr = data->shape;
1284 const uint8_t *srcShapePtr = data->shape + ((andMaskSize + 3) & ~3);
1285
1286#if defined (RT_OS_WINDOWS)
1287
1288 BITMAPV5HEADER bi;
1289 HBITMAP hBitmap;
1290 void *lpBits;
1291 HCURSOR hAlphaCursor = NULL;
1292
1293 ::ZeroMemory (&bi, sizeof (BITMAPV5HEADER));
1294 bi.bV5Size = sizeof (BITMAPV5HEADER);
1295 bi.bV5Width = data->width;
1296 bi.bV5Height = - (LONG) data->height;
1297 bi.bV5Planes = 1;
1298 bi.bV5BitCount = 32;
1299 bi.bV5Compression = BI_BITFIELDS;
1300 // specifiy a supported 32 BPP alpha format for Windows XP
1301 bi.bV5RedMask = 0x00FF0000;
1302 bi.bV5GreenMask = 0x0000FF00;
1303 bi.bV5BlueMask = 0x000000FF;
1304 if (data->alpha)
1305 bi.bV5AlphaMask = 0xFF000000;
1306 else
1307 bi.bV5AlphaMask = 0;
1308
1309 HDC hdc = ::GetDC (NULL);
1310
1311 // create the DIB section with an alpha channel
1312 hBitmap = ::CreateDIBSection (hdc, (BITMAPINFO *) &bi, DIB_RGB_COLORS,
1313 (void **) &lpBits, NULL, (DWORD) 0);
1314
1315 ::ReleaseDC (NULL, hdc);
1316
1317 HBITMAP hMonoBitmap = NULL;
1318 if (data->alpha)
1319 {
1320 // create an empty mask bitmap
1321 hMonoBitmap = ::CreateBitmap (data->width, data->height, 1, 1, NULL);
1322 }
1323 else
1324 {
1325 // for now, we assert if width is not multiple of 16. the
1326 // alternative is to manually align the AND mask to 16 bits.
1327 AssertMsg (!(data->width % 16), ("AND mask must be word-aligned!\n"));
1328
1329 // create the AND mask bitmap
1330 hMonoBitmap = ::CreateBitmap (data->width, data->height, 1, 1,
1331 srcAndMaskPtr);
1332 }
1333
1334 Assert (hBitmap);
1335 Assert (hMonoBitmap);
1336 if (hBitmap && hMonoBitmap)
1337 {
1338 DWORD *dstShapePtr = (DWORD *) lpBits;
1339
1340 for (uint32_t y = 0; y < data->height; y ++)
1341 {
1342 memcpy (dstShapePtr, srcShapePtr, srcShapePtrScan);
1343 srcShapePtr += srcShapePtrScan;
1344 dstShapePtr += data->width;
1345 }
1346
1347 ICONINFO ii;
1348 ii.fIcon = FALSE;
1349 ii.xHotspot = data->xHot;
1350 ii.yHotspot = data->yHot;
1351 ii.hbmMask = hMonoBitmap;
1352 ii.hbmColor = hBitmap;
1353
1354 hAlphaCursor = ::CreateIconIndirect (&ii);
1355 Assert (hAlphaCursor);
1356 if (hAlphaCursor)
1357 {
1358 // here we do a dirty trick by substituting a Window Manager's
1359 // cursor handle with the handle we created
1360
1361 WMcursor *old_wm_cursor = gpCustomCursor->wm_cursor;
1362
1363 // see SDL12/src/video/wincommon/SDL_sysmouse.c
1364 void *wm_cursor = malloc (sizeof (HCURSOR) + sizeof (uint8_t *) * 2);
1365 *(HCURSOR *) wm_cursor = hAlphaCursor;
1366
1367 gpCustomCursor->wm_cursor = (WMcursor *) wm_cursor;
1368 SDL_SetCursor (gpCustomCursor);
1369 SDL_ShowCursor (SDL_ENABLE);
1370
1371 if (old_wm_cursor)
1372 {
1373 ::DestroyCursor (* (HCURSOR *) old_wm_cursor);
1374 free (old_wm_cursor);
1375 }
1376
1377 ok = true;
1378 }
1379 }
1380
1381 if (hMonoBitmap)
1382 ::DeleteObject (hMonoBitmap);
1383 if (hBitmap)
1384 ::DeleteObject (hBitmap);
1385
1386#elif defined(VBOXBFE_WITH_X11)
1387
1388 XcursorImage *img = XcursorImageCreate (data->width, data->height);
1389 Assert (img);
1390 if (img)
1391 {
1392 img->xhot = data->xHot;
1393 img->yhot = data->yHot;
1394
1395 XcursorPixel *dstShapePtr = img->pixels;
1396
1397 for (uint32_t y = 0; y < data->height; y ++)
1398 {
1399 memcpy (dstShapePtr, srcShapePtr, srcShapePtrScan);
1400
1401 if (!data->alpha)
1402 {
1403 // convert AND mask to the alpha channel
1404 uint8_t byte = 0;
1405 for (uint32_t x = 0; x < data->width; x ++)
1406 {
1407 if (!(x % 8))
1408 byte = *(srcAndMaskPtr ++);
1409 else
1410 byte <<= 1;
1411
1412 if (byte & 0x80)
1413 {
1414 // X11 doesn't support inverted pixels (XOR ops,
1415 // to be exact) in cursor shapes, so we detect such
1416 // pixels and always replace them with black ones to
1417 // make them visible at least over light colors
1418 if (dstShapePtr [x] & 0x00FFFFFF)
1419 dstShapePtr [x] = 0xFF000000;
1420 else
1421 dstShapePtr [x] = 0x00000000;
1422 }
1423 else
1424 dstShapePtr [x] |= 0xFF000000;
1425 }
1426 }
1427
1428 srcShapePtr += srcShapePtrScan;
1429 dstShapePtr += data->width;
1430 }
1431
1432 Cursor cur = XcursorImageLoadCursor (gSdlInfo.info.x11.display, img);
1433 Assert (cur);
1434 if (cur)
1435 {
1436 // here we do a dirty trick by substituting a Window Manager's
1437 // cursor handle with the handle we created
1438
1439 WMcursor *old_wm_cursor = gpCustomCursor->wm_cursor;
1440
1441 // see SDL12/src/video/x11/SDL_x11mouse.c
1442 void *wm_cursor = malloc (sizeof (Cursor));
1443 *(Cursor *) wm_cursor = cur;
1444
1445 gpCustomCursor->wm_cursor = (WMcursor *) wm_cursor;
1446 SDL_SetCursor (gpCustomCursor);
1447 SDL_ShowCursor (SDL_ENABLE);
1448
1449 if (old_wm_cursor)
1450 {
1451 XFreeCursor (gSdlInfo.info.x11.display, *(Cursor *) old_wm_cursor);
1452 free (old_wm_cursor);
1453 }
1454
1455 ok = true;
1456 }
1457
1458 XcursorImageDestroy (img);
1459 }
1460
1461#endif /* VBOXBFE_WITH_X11 */
1462
1463 if (!ok)
1464 {
1465 SDL_SetCursor (gpDefaultCursor);
1466 SDL_ShowCursor (SDL_ENABLE);
1467 }
1468 }
1469 else
1470 {
1471 if (data->visible)
1472 {
1473 SDL_ShowCursor (SDL_ENABLE);
1474 }
1475 else
1476 {
1477 SDL_ShowCursor (SDL_DISABLE);
1478 }
1479 }
1480}
1481
1482void SDLConsole::resetCursor(void)
1483{
1484 SDL_SetCursor (gpDefaultCursor);
1485 SDL_ShowCursor (SDL_ENABLE);
1486}
1487
1488/**
1489 * Handles a host key down event
1490 */
1491int SDLConsole::handleHostKey(const SDL_KeyboardEvent *pEv)
1492{
1493 /*
1494 * Revalidate the host key modifier
1495 */
1496 if ((SDL_GetModState() & ~(KMOD_MODE | KMOD_NUM | KMOD_RESERVED)) != gHostKey)
1497 return VERR_NOT_SUPPORTED;
1498
1499 /*
1500 * What was pressed?
1501 */
1502 switch (pEv->keysym.sym)
1503 {
1504 /* Control-Alt-Delete */
1505 case SDLK_DELETE:
1506 {
1507 gKeyboard->PutCAD();
1508 break;
1509 }
1510
1511 /*
1512 * Fullscreen / Windowed toggle.
1513 */
1514 case SDLK_f:
1515 {
1516 if (gfAllowFullscreenToggle)
1517 {
1518 gFramebuffer->setFullscreen(!gFramebuffer->getFullscreen());
1519
1520 /*
1521 * We have switched from/to fullscreen, so request a full
1522 * screen repaint, just to be sure.
1523 */
1524 gDisplay->InvalidateAndUpdate();
1525 }
1526 break;
1527 }
1528
1529 /*
1530 * Pause / Resume toggle.
1531 */
1532 case SDLK_p:
1533 {
1534 if (machineState == VMSTATE_RUNNING)
1535 {
1536 if (fInputGrab)
1537 inputGrabEnd();
1538
1539 PVMREQ pReq;
1540 int rcVBox = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT,
1541 (PFNRT)VMR3Suspend, 1, pVM);
1542 AssertRC(rcVBox);
1543 if (VBOX_SUCCESS(rcVBox))
1544 {
1545 rcVBox = pReq->iStatus;
1546 VMR3ReqFree(pReq);
1547 }
1548 }
1549 else
1550 if (machineState == VMSTATE_SUSPENDED)
1551 {
1552 PVMREQ pReq;
1553 int rcVBox = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT,
1554 (PFNRT)VMR3Resume, 1, pVM);
1555 AssertRC(rcVBox);
1556 if (VBOX_SUCCESS(rcVBox))
1557 {
1558 rcVBox = pReq->iStatus;
1559 VMR3ReqFree(pReq);
1560 }
1561 }
1562 updateTitlebar();
1563 break;
1564 }
1565
1566 /*
1567 * Reset the VM
1568 */
1569 case SDLK_r:
1570 {
1571 PVMREQ pReq;
1572 int rcVBox = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT,
1573 (PFNRT)VMR3Reset, 1, pVM);
1574 AssertRC(rcVBox);
1575 if (VBOX_SUCCESS(rcVBox))
1576 {
1577 rcVBox = pReq->iStatus;
1578 VMR3ReqFree(pReq);
1579 }
1580 break;
1581 }
1582
1583 /*
1584 * Terminate the VM
1585 */
1586 case SDLK_q:
1587 {
1588 return VINF_EM_TERMINATE;
1589 break;
1590 }
1591
1592 /*
1593 * Send ACPI power button press event
1594 */
1595 case SDLK_h:
1596 {
1597 PPDMIBASE pBase;
1598 int vrc = PDMR3QueryDeviceLun (pVM, "acpi", 0, 0, &pBase);
1599 if (VBOX_SUCCESS (vrc))
1600 {
1601 Assert (pBase);
1602 PPDMIACPIPORT pPort =
1603 (PPDMIACPIPORT) pBase->pfnQueryInterface(pBase, PDMINTERFACE_ACPI_PORT);
1604 vrc = pPort ? pPort->pfnPowerButtonPress(pPort) : VERR_INVALID_POINTER;
1605 }
1606 break;
1607 }
1608
1609#if 0
1610 /*
1611 * Save the machine's state and exit
1612 */
1613 case SDLK_s:
1614 {
1615 resetKeys();
1616 RTThreadYield();
1617 if (fInputGrab)
1618 inputGrabEnd();
1619 RTThreadYield();
1620 updateTitlebarSave(0);
1621 gProgress = NULL;
1622 int rc = gConsole->SaveState(gProgress.asOutParam());
1623 if (rc != S_OK)
1624 {
1625 RTPrintf("Error saving state! rc = 0x%x\n", rc);
1626 return VINF_EM_TERMINATE;
1627 }
1628 Assert(gProgress);
1629
1630 /*
1631 * Wait for the operation to be completed and work
1632 * the title bar in the mean while.
1633 */
1634 LONG cPercent = 0;
1635 for (;;)
1636 {
1637 BOOL fCompleted;
1638 rc = gProgress->COMGETTER(Completed)(&fCompleted);
1639 if (FAILED(rc) || fCompleted)
1640 break;
1641 LONG cPercentNow;
1642 rc = gProgress->COMGETTER(Percent)(&cPercentNow);
1643 if (FAILED(rc))
1644 break;
1645 if (cPercentNow != cPercent)
1646 {
1647 UpdateTitlebarSave(cPercent);
1648 cPercent = cPercentNow;
1649 }
1650
1651 /* wait */
1652 rc = gProgress->WaitForCompletion(100);
1653 if (FAILED(rc))
1654 break;
1655 /// @todo process gui events.
1656 }
1657
1658 /*
1659 * What's the result of the operation?
1660 */
1661 HRESULT lrc;
1662 rc = gProgress->COMGETTER(ResultCode)(&lrc);
1663 if (FAILED(rc))
1664 lrc = ~0;
1665 if (!lrc)
1666 {
1667 UpdateTitlebarSave(100);
1668 RTThreadYield();
1669 RTPrintf("Saved the state successfully.\n");
1670 }
1671 else
1672 RTPrintf("Error saving state, lrc=%d (%#x)\n", lrc, lrc);
1673 return VINF_EM_TERMINATE;
1674 }
1675#endif
1676 /*
1677 * Not a host key combination.
1678 * Indicate this by returning false.
1679 */
1680 default:
1681 return VERR_NOT_SUPPORTED;
1682 }
1683
1684 return VINF_SUCCESS;
1685}
1686
1687
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette