VirtualBox

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

Last change on this file since 3584 was 2980, checked in by vboxsync, 18 years ago

InnoTek -> innotek: actual code changes (headers follow).

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

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