1/* 2 * QEMU Cocoa CG display driver 3 * 4 * Copyright (c) 2008 Mike Kronenberg 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25#import <Cocoa/Cocoa.h> 26#include <crt_externs.h> 27 28#include "qemu-common.h" 29#include "ui/console.h" 30#include "sysemu/sysemu.h" 31 32#ifndef MAC_OS_X_VERSION_10_4 33#define MAC_OS_X_VERSION_10_4 1040 34#endif 35#ifndef MAC_OS_X_VERSION_10_5 36#define MAC_OS_X_VERSION_10_5 1050 37#endif 38 39 40//#define DEBUG 41 42#ifdef DEBUG 43#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); } 44#else 45#define COCOA_DEBUG(...) ((void) 0) 46#endif 47 48#define cgrect(nsrect) (*(CGRect *)&(nsrect)) 49#define COCOA_MOUSE_EVENT \ 50 if (isTabletEnabled) { \ 51 kbd_mouse_event((int)(p.x * 0x7FFF / (screen.width - 1)), (int)((screen.height - p.y) * 0x7FFF / (screen.height - 1)), 0, buttons); \ 52 } else if (isMouseGrabed) { \ 53 kbd_mouse_event((int)[event deltaX], (int)[event deltaY], 0, buttons); \ 54 } else { \ 55 [NSApp sendEvent:event]; \ 56 } 57 58typedef struct { 59 int width; 60 int height; 61 int bitsPerComponent; 62 int bitsPerPixel; 63} QEMUScreen; 64 65NSWindow *normalWindow; 66static DisplayChangeListener *dcl; 67 68int gArgc; 69char **gArgv; 70 71// keymap conversion 72int keymap[] = 73{ 74// SdlI macI macH SdlH 104xtH 104xtC sdl 75 30, // 0 0x00 0x1e A QZ_a 76 31, // 1 0x01 0x1f S QZ_s 77 32, // 2 0x02 0x20 D QZ_d 78 33, // 3 0x03 0x21 F QZ_f 79 35, // 4 0x04 0x23 H QZ_h 80 34, // 5 0x05 0x22 G QZ_g 81 44, // 6 0x06 0x2c Z QZ_z 82 45, // 7 0x07 0x2d X QZ_x 83 46, // 8 0x08 0x2e C QZ_c 84 47, // 9 0x09 0x2f V QZ_v 85 0, // 10 0x0A Undefined 86 48, // 11 0x0B 0x30 B QZ_b 87 16, // 12 0x0C 0x10 Q QZ_q 88 17, // 13 0x0D 0x11 W QZ_w 89 18, // 14 0x0E 0x12 E QZ_e 90 19, // 15 0x0F 0x13 R QZ_r 91 21, // 16 0x10 0x15 Y QZ_y 92 20, // 17 0x11 0x14 T QZ_t 93 2, // 18 0x12 0x02 1 QZ_1 94 3, // 19 0x13 0x03 2 QZ_2 95 4, // 20 0x14 0x04 3 QZ_3 96 5, // 21 0x15 0x05 4 QZ_4 97 7, // 22 0x16 0x07 6 QZ_6 98 6, // 23 0x17 0x06 5 QZ_5 99 13, // 24 0x18 0x0d = QZ_EQUALS 100 10, // 25 0x19 0x0a 9 QZ_9 101 8, // 26 0x1A 0x08 7 QZ_7 102 12, // 27 0x1B 0x0c - QZ_MINUS 103 9, // 28 0x1C 0x09 8 QZ_8 104 11, // 29 0x1D 0x0b 0 QZ_0 105 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET 106 24, // 31 0x1F 0x18 O QZ_o 107 22, // 32 0x20 0x16 U QZ_u 108 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET 109 23, // 34 0x22 0x17 I QZ_i 110 25, // 35 0x23 0x19 P QZ_p 111 28, // 36 0x24 0x1c ENTER QZ_RETURN 112 38, // 37 0x25 0x26 L QZ_l 113 36, // 38 0x26 0x24 J QZ_j 114 40, // 39 0x27 0x28 ' QZ_QUOTE 115 37, // 40 0x28 0x25 K QZ_k 116 39, // 41 0x29 0x27 ; QZ_SEMICOLON 117 43, // 42 0x2A 0x2b \ QZ_BACKSLASH 118 51, // 43 0x2B 0x33 , QZ_COMMA 119 53, // 44 0x2C 0x35 / QZ_SLASH 120 49, // 45 0x2D 0x31 N QZ_n 121 50, // 46 0x2E 0x32 M QZ_m 122 52, // 47 0x2F 0x34 . QZ_PERIOD 123 15, // 48 0x30 0x0f TAB QZ_TAB 124 57, // 49 0x31 0x39 SPACE QZ_SPACE 125 41, // 50 0x32 0x29 ` QZ_BACKQUOTE 126 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE 127 0, // 52 0x34 Undefined 128 1, // 53 0x35 0x01 ESC QZ_ESCAPE 129 0, // 54 0x36 QZ_RMETA 130 0, // 55 0x37 QZ_LMETA 131 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT 132 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK 133 56, // 58 0x3A 0x38 L ALT QZ_LALT 134 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL 135 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT 136 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT 137 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL 138 0, // 63 0x3F Undefined 139 0, // 64 0x40 Undefined 140 0, // 65 0x41 Undefined 141 0, // 66 0x42 Undefined 142 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY 143 0, // 68 0x44 Undefined 144 78, // 69 0x45 0x4e KP + QZ_KP_PLUS 145 0, // 70 0x46 Undefined 146 69, // 71 0x47 0x45 NUM QZ_NUMLOCK 147 0, // 72 0x48 Undefined 148 0, // 73 0x49 Undefined 149 0, // 74 0x4A Undefined 150 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE 151 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER 152 0, // 77 0x4D undefined 153 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS 154 0, // 79 0x4F Undefined 155 0, // 80 0x50 Undefined 156 0, // 81 0x51 QZ_KP_EQUALS 157 82, // 82 0x52 0x52 KP 0 QZ_KP0 158 79, // 83 0x53 0x4f KP 1 QZ_KP1 159 80, // 84 0x54 0x50 KP 2 QZ_KP2 160 81, // 85 0x55 0x51 KP 3 QZ_KP3 161 75, // 86 0x56 0x4b KP 4 QZ_KP4 162 76, // 87 0x57 0x4c KP 5 QZ_KP5 163 77, // 88 0x58 0x4d KP 6 QZ_KP6 164 71, // 89 0x59 0x47 KP 7 QZ_KP7 165 0, // 90 0x5A Undefined 166 72, // 91 0x5B 0x48 KP 8 QZ_KP8 167 73, // 92 0x5C 0x49 KP 9 QZ_KP9 168 0, // 93 0x5D Undefined 169 0, // 94 0x5E Undefined 170 0, // 95 0x5F Undefined 171 63, // 96 0x60 0x3f F5 QZ_F5 172 64, // 97 0x61 0x40 F6 QZ_F6 173 65, // 98 0x62 0x41 F7 QZ_F7 174 61, // 99 0x63 0x3d F3 QZ_F3 175 66, // 100 0x64 0x42 F8 QZ_F8 176 67, // 101 0x65 0x43 F9 QZ_F9 177 0, // 102 0x66 Undefined 178 87, // 103 0x67 0x57 F11 QZ_F11 179 0, // 104 0x68 Undefined 180 183,// 105 0x69 0xb7 QZ_PRINT 181 0, // 106 0x6A Undefined 182 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK 183 0, // 108 0x6C Undefined 184 68, // 109 0x6D 0x44 F10 QZ_F10 185 0, // 110 0x6E Undefined 186 88, // 111 0x6F 0x58 F12 QZ_F12 187 0, // 112 0x70 Undefined 188 110,// 113 0x71 0x0 QZ_PAUSE 189 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT 190 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME 191 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP 192 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE 193 62, // 118 0x76 0x3e F4 QZ_F4 194 207,// 119 0x77 0xcf E0,4f END QZ_END 195 60, // 120 0x78 0x3c F2 QZ_F2 196 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN 197 59, // 122 0x7A 0x3b F1 QZ_F1 198 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT 199 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT 200 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN 201 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP 202/* completed according to http://www.libsdl.org/cgi/cvsweb.cgi/SDL12/src/video/quartz/SDL_QuartzKeys.h?rev=1.6&content-type=text/x-cvsweb-markup */ 203 204/* Aditional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */ 205/* 206 219 // 0xdb e0,5b L GUI 207 220 // 0xdc e0,5c R GUI 208 221 // 0xdd e0,5d APPS 209 // E0,2A,E0,37 PRNT SCRN 210 // E1,1D,45,E1,9D,C5 PAUSE 211 83 // 0x53 0x53 KP . 212// ACPI Scan Codes 213 222 // 0xde E0, 5E Power 214 223 // 0xdf E0, 5F Sleep 215 227 // 0xe3 E0, 63 Wake 216// Windows Multimedia Scan Codes 217 153 // 0x99 E0, 19 Next Track 218 144 // 0x90 E0, 10 Previous Track 219 164 // 0xa4 E0, 24 Stop 220 162 // 0xa2 E0, 22 Play/Pause 221 160 // 0xa0 E0, 20 Mute 222 176 // 0xb0 E0, 30 Volume Up 223 174 // 0xae E0, 2E Volume Down 224 237 // 0xed E0, 6D Media Select 225 236 // 0xec E0, 6C E-Mail 226 161 // 0xa1 E0, 21 Calculator 227 235 // 0xeb E0, 6B My Computer 228 229 // 0xe5 E0, 65 WWW Search 229 178 // 0xb2 E0, 32 WWW Home 230 234 // 0xea E0, 6A WWW Back 231 233 // 0xe9 E0, 69 WWW Forward 232 232 // 0xe8 E0, 68 WWW Stop 233 231 // 0xe7 E0, 67 WWW Refresh 234 230 // 0xe6 E0, 66 WWW Favorites 235*/ 236}; 237 238static int cocoa_keycode_to_qemu(int keycode) 239{ 240 if((sizeof(keymap)/sizeof(int)) <= keycode) 241 { 242 printf("(cocoa) warning unknow keycode 0x%x\n", keycode); 243 return 0; 244 } 245 return keymap[keycode]; 246} 247 248 249 250/* 251 ------------------------------------------------------ 252 QemuCocoaView 253 ------------------------------------------------------ 254*/ 255@interface QemuCocoaView : NSView 256{ 257 QEMUScreen screen; 258 NSWindow *fullScreenWindow; 259 float cx,cy,cw,ch,cdx,cdy; 260 CGDataProviderRef dataProviderRef; 261 int modifiers_state[256]; 262 BOOL isMouseGrabed; 263 BOOL isFullscreen; 264 BOOL isAbsoluteEnabled; 265 BOOL isTabletEnabled; 266} 267- (void) resizeContentToWidth:(int)w height:(int)h displayState:(DisplayState *)ds; 268- (void) updateDataOffset:(DisplayState *)ds; 269- (void) grabMouse; 270- (void) ungrabMouse; 271- (void) toggleFullScreen:(id)sender; 272- (void) handleEvent:(NSEvent *)event; 273- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; 274- (BOOL) isMouseGrabed; 275- (BOOL) isAbsoluteEnabled; 276- (float) cdx; 277- (float) cdy; 278- (QEMUScreen) gscreen; 279@end 280 281QemuCocoaView *cocoaView; 282 283@implementation QemuCocoaView 284- (id)initWithFrame:(NSRect)frameRect 285{ 286 COCOA_DEBUG("QemuCocoaView: initWithFrame\n"); 287 288 self = [super initWithFrame:frameRect]; 289 if (self) { 290 291 screen.bitsPerComponent = 8; 292 screen.bitsPerPixel = 32; 293 screen.width = frameRect.size.width; 294 screen.height = frameRect.size.height; 295 296 } 297 return self; 298} 299 300- (void) dealloc 301{ 302 COCOA_DEBUG("QemuCocoaView: dealloc\n"); 303 304 if (dataProviderRef) 305 CGDataProviderRelease(dataProviderRef); 306 307 [super dealloc]; 308} 309 310- (BOOL) isOpaque 311{ 312 return YES; 313} 314 315- (void) drawRect:(NSRect) rect 316{ 317 COCOA_DEBUG("QemuCocoaView: drawRect\n"); 318 319 // get CoreGraphic context 320 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort]; 321 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone); 322 CGContextSetShouldAntialias (viewContextRef, NO); 323 324 // draw screen bitmap directly to Core Graphics context 325 if (dataProviderRef) { 326 CGImageRef imageRef = CGImageCreate( 327 screen.width, //width 328 screen.height, //height 329 screen.bitsPerComponent, //bitsPerComponent 330 screen.bitsPerPixel, //bitsPerPixel 331 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow 332#ifdef __LITTLE_ENDIAN__ 333 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4 334 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, 335#else 336 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc) 337 kCGImageAlphaNoneSkipFirst, //bitmapInfo 338#endif 339 dataProviderRef, //provider 340 NULL, //decode 341 0, //interpolate 342 kCGRenderingIntentDefault //intent 343 ); 344// test if host supports "CGImageCreateWithImageInRect" at compile time 345#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4) 346 if (CGImageCreateWithImageInRect == NULL) { // test if "CGImageCreateWithImageInRect" is supported on host at runtime 347#endif 348 // compatibility drawing code (draws everything) (OS X < 10.4) 349 CGContextDrawImage (viewContextRef, CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height), imageRef); 350#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4) 351 } else { 352 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4) 353 const NSRect *rectList; 354#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) 355 NSInteger rectCount; 356#else 357 int rectCount; 358#endif 359 int i; 360 CGImageRef clipImageRef; 361 CGRect clipRect; 362 363 [self getRectsBeingDrawn:&rectList count:&rectCount]; 364 for (i = 0; i < rectCount; i++) { 365 clipRect.origin.x = rectList[i].origin.x / cdx; 366 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy; 367 clipRect.size.width = rectList[i].size.width / cdx; 368 clipRect.size.height = rectList[i].size.height / cdy; 369 clipImageRef = CGImageCreateWithImageInRect( 370 imageRef, 371 clipRect 372 ); 373 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef); 374 CGImageRelease (clipImageRef); 375 } 376 } 377#endif 378 CGImageRelease (imageRef); 379 } 380} 381 382- (void) setContentDimensions 383{ 384 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); 385 386 if (isFullscreen) { 387 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width; 388 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height; 389 cw = screen.width * cdx; 390 ch = screen.height * cdy; 391 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; 392 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; 393 } else { 394 cx = 0; 395 cy = 0; 396 cw = screen.width; 397 ch = screen.height; 398 cdx = 1.0; 399 cdy = 1.0; 400 } 401} 402 403- (void) resizeContentToWidth:(int)w height:(int)h displayState:(DisplayState *)ds 404{ 405 COCOA_DEBUG("QemuCocoaView: resizeContent\n"); 406 407 // update screenBuffer 408 if (dataProviderRef) 409 CGDataProviderRelease(dataProviderRef); 410 411 //sync host window color space with guests 412 screen.bitsPerPixel = ds_get_bits_per_pixel(ds); 413 screen.bitsPerComponent = ds_get_bytes_per_pixel(ds) * 2; 414 415 dataProviderRef = CGDataProviderCreateWithData(NULL, ds_get_data(ds), w * 4 * h, NULL); 416 417 // update windows 418 if (isFullscreen) { 419 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]]; 420 [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + screen.height, w, h + [normalWindow frame].size.height - screen.height) display:NO animate:NO]; 421 } else { 422 if (qemu_name) 423 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; 424 [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + screen.height, w, h + [normalWindow frame].size.height - screen.height) display:YES animate:NO]; 425 } 426 screen.width = w; 427 screen.height = h; 428 [normalWindow center]; 429 [self setContentDimensions]; 430 [self setFrame:NSMakeRect(cx, cy, cw, ch)]; 431} 432 433- (void) updateDataOffset:(DisplayState *)ds 434{ 435 COCOA_DEBUG("QemuCocoaView: UpdateDataOffset\n"); 436 437 // update screenBuffer 438 if (dataProviderRef) { 439 CGDataProviderRelease(dataProviderRef); 440 } 441 442 size_t size = ds_get_width(ds) * 4 * ds_get_height(ds); 443 dataProviderRef = CGDataProviderCreateWithData(NULL, ds_get_data(ds), 444 size, NULL); 445} 446 447- (void) toggleFullScreen:(id)sender 448{ 449 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); 450 451 if (isFullscreen) { // switch from fullscreen to desktop 452 isFullscreen = FALSE; 453 [self ungrabMouse]; 454 [self setContentDimensions]; 455// test if host supports "exitFullScreenModeWithOptions" at compile time 456#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) 457 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime 458 [self exitFullScreenModeWithOptions:nil]; 459 } else { 460#endif 461 [fullScreenWindow close]; 462 [normalWindow setContentView: self]; 463 [normalWindow makeKeyAndOrderFront: self]; 464 [NSMenu setMenuBarVisible:YES]; 465#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) 466 } 467#endif 468 } else { // switch from desktop to fullscreen 469 isFullscreen = TRUE; 470 [self grabMouse]; 471 [self setContentDimensions]; 472// test if host supports "enterFullScreenMode:withOptions" at compile time 473#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) 474 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime 475 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys: 476 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens, 477 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting, 478 nil]]; 479 } else { 480#endif 481 [NSMenu setMenuBarVisible:NO]; 482 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] 483 styleMask:NSBorderlessWindowMask 484 backing:NSBackingStoreBuffered 485 defer:NO]; 486 [fullScreenWindow setHasShadow:NO]; 487 [fullScreenWindow setContentView:self]; 488 [fullScreenWindow makeKeyAndOrderFront:self]; 489#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) 490 } 491#endif 492 } 493} 494 495- (void) handleEvent:(NSEvent *)event 496{ 497 COCOA_DEBUG("QemuCocoaView: handleEvent\n"); 498 499 int buttons = 0; 500 int keycode; 501 NSPoint p = [event locationInWindow]; 502 503 switch ([event type]) { 504 case NSFlagsChanged: 505 keycode = cocoa_keycode_to_qemu([event keyCode]); 506 if (keycode) { 507 if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup 508 kbd_put_keycode(keycode); 509 kbd_put_keycode(keycode | 0x80); 510 } else if (is_graphic_console()) { 511 if (keycode & 0x80) 512 kbd_put_keycode(0xe0); 513 if (modifiers_state[keycode] == 0) { // keydown 514 kbd_put_keycode(keycode & 0x7f); 515 modifiers_state[keycode] = 1; 516 } else { // keyup 517 kbd_put_keycode(keycode | 0x80); 518 modifiers_state[keycode] = 0; 519 } 520 } 521 } 522 523 // release Mouse grab when pressing ctrl+alt 524 if (!isFullscreen && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { 525 [self ungrabMouse]; 526 } 527 break; 528 case NSKeyDown: 529 530 // forward command Key Combos 531 if ([event modifierFlags] & NSCommandKeyMask) { 532 [NSApp sendEvent:event]; 533 return; 534 } 535 536 // default 537 keycode = cocoa_keycode_to_qemu([event keyCode]); 538 539 // handle control + alt Key Combos (ctrl+alt is reserved for QEMU) 540 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { 541 switch (keycode) { 542 543 // enable graphic console 544 case 0x02 ... 0x0a: // '1' to '9' keys 545 console_select(keycode - 0x02); 546 break; 547 } 548 549 // handle keys for graphic console 550 } else if (is_graphic_console()) { 551 if (keycode & 0x80) //check bit for e0 in front 552 kbd_put_keycode(0xe0); 553 kbd_put_keycode(keycode & 0x7f); //remove e0 bit in front 554 555 // handlekeys for Monitor 556 } else { 557 int keysym = 0; 558 switch([event keyCode]) { 559 case 115: 560 keysym = QEMU_KEY_HOME; 561 break; 562 case 117: 563 keysym = QEMU_KEY_DELETE; 564 break; 565 case 119: 566 keysym = QEMU_KEY_END; 567 break; 568 case 123: 569 keysym = QEMU_KEY_LEFT; 570 break; 571 case 124: 572 keysym = QEMU_KEY_RIGHT; 573 break; 574 case 125: 575 keysym = QEMU_KEY_DOWN; 576 break; 577 case 126: 578 keysym = QEMU_KEY_UP; 579 break; 580 default: 581 { 582 NSString *ks = [event characters]; 583 if ([ks length] > 0) 584 keysym = [ks characterAtIndex:0]; 585 } 586 } 587 if (keysym) 588 kbd_put_keysym(keysym); 589 } 590 break; 591 case NSKeyUp: 592 keycode = cocoa_keycode_to_qemu([event keyCode]); 593 if (is_graphic_console()) { 594 if (keycode & 0x80) 595 kbd_put_keycode(0xe0); 596 kbd_put_keycode(keycode | 0x80); //add 128 to signal release of key 597 } 598 break; 599 case NSMouseMoved: 600 if (isAbsoluteEnabled) { 601 if (p.x < 0 || p.x > screen.width || p.y < 0 || p.y > screen.height || ![[self window] isKeyWindow]) { 602 if (isTabletEnabled) { // if we leave the window, deactivate the tablet 603 [NSCursor unhide]; 604 isTabletEnabled = FALSE; 605 } 606 } else { 607 if (!isTabletEnabled) { // if we enter the window, activate the tablet 608 [NSCursor hide]; 609 isTabletEnabled = TRUE; 610 } 611 } 612 } 613 COCOA_MOUSE_EVENT 614 break; 615 case NSLeftMouseDown: 616 if ([event modifierFlags] & NSCommandKeyMask) { 617 buttons |= MOUSE_EVENT_RBUTTON; 618 } else { 619 buttons |= MOUSE_EVENT_LBUTTON; 620 } 621 COCOA_MOUSE_EVENT 622 break; 623 case NSRightMouseDown: 624 buttons |= MOUSE_EVENT_RBUTTON; 625 COCOA_MOUSE_EVENT 626 break; 627 case NSOtherMouseDown: 628 buttons |= MOUSE_EVENT_MBUTTON; 629 COCOA_MOUSE_EVENT 630 break; 631 case NSLeftMouseDragged: 632 if ([event modifierFlags] & NSCommandKeyMask) { 633 buttons |= MOUSE_EVENT_RBUTTON; 634 } else { 635 buttons |= MOUSE_EVENT_LBUTTON; 636 } 637 COCOA_MOUSE_EVENT 638 break; 639 case NSRightMouseDragged: 640 buttons |= MOUSE_EVENT_RBUTTON; 641 COCOA_MOUSE_EVENT 642 break; 643 case NSOtherMouseDragged: 644 buttons |= MOUSE_EVENT_MBUTTON; 645 COCOA_MOUSE_EVENT 646 break; 647 case NSLeftMouseUp: 648 if (isTabletEnabled) { 649 COCOA_MOUSE_EVENT 650 } else if (!isMouseGrabed) { 651 if (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height) { 652 [self grabMouse]; 653 } else { 654 [NSApp sendEvent:event]; 655 } 656 } else { 657 COCOA_MOUSE_EVENT 658 } 659 break; 660 case NSRightMouseUp: 661 COCOA_MOUSE_EVENT 662 break; 663 case NSOtherMouseUp: 664 COCOA_MOUSE_EVENT 665 break; 666 case NSScrollWheel: 667 if (isTabletEnabled || isMouseGrabed) { 668 kbd_mouse_event(0, 0, -[event deltaY], 0); 669 } else { 670 [NSApp sendEvent:event]; 671 } 672 break; 673 default: 674 [NSApp sendEvent:event]; 675 } 676} 677 678- (void) grabMouse 679{ 680 COCOA_DEBUG("QemuCocoaView: grabMouse\n"); 681 682 if (!isFullscreen) { 683 if (qemu_name) 684 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]]; 685 else 686 [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"]; 687 } 688 [NSCursor hide]; 689 CGAssociateMouseAndMouseCursorPosition(FALSE); 690 isMouseGrabed = TRUE; // while isMouseGrabed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] 691} 692 693- (void) ungrabMouse 694{ 695 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); 696 697 if (!isFullscreen) { 698 if (qemu_name) 699 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; 700 else 701 [normalWindow setTitle:@"QEMU"]; 702 } 703 [NSCursor unhide]; 704 CGAssociateMouseAndMouseCursorPosition(TRUE); 705 isMouseGrabed = FALSE; 706} 707 708- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;} 709- (BOOL) isMouseGrabed {return isMouseGrabed;} 710- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} 711- (float) cdx {return cdx;} 712- (float) cdy {return cdy;} 713- (QEMUScreen) gscreen {return screen;} 714@end 715 716 717 718/* 719 ------------------------------------------------------ 720 QemuCocoaAppController 721 ------------------------------------------------------ 722*/ 723@interface QemuCocoaAppController : NSObject 724{ 725} 726- (void)startEmulationWithArgc:(int)argc argv:(char**)argv; 727- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo; 728- (void)toggleFullScreen:(id)sender; 729- (void)showQEMUDoc:(id)sender; 730- (void)showQEMUTec:(id)sender; 731@end 732 733@implementation QemuCocoaAppController 734- (id) init 735{ 736 COCOA_DEBUG("QemuCocoaAppController: init\n"); 737 738 self = [super init]; 739 if (self) { 740 741 // create a view and add it to the window 742 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)]; 743 if(!cocoaView) { 744 fprintf(stderr, "(cocoa) can't create a view\n"); 745 exit(1); 746 } 747 748 // create a window 749 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame] 750 styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask 751 backing:NSBackingStoreBuffered defer:NO]; 752 if(!normalWindow) { 753 fprintf(stderr, "(cocoa) can't create window\n"); 754 exit(1); 755 } 756 [normalWindow setAcceptsMouseMovedEvents:YES]; 757 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]]; 758 [normalWindow setContentView:cocoaView]; 759 [normalWindow useOptimizedDrawing:YES]; 760 [normalWindow makeKeyAndOrderFront:self]; 761 [normalWindow center]; 762 763 } 764 return self; 765} 766 767- (void) dealloc 768{ 769 COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); 770 771 if (cocoaView) 772 [cocoaView release]; 773 [super dealloc]; 774} 775 776- (void)applicationDidFinishLaunching: (NSNotification *) note 777{ 778 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); 779 780 // Display an open dialog box if no argument were passed or 781 // if qemu was launched from the finder ( the Finder passes "-psn" ) 782 if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) { 783 NSOpenPanel *op = [[NSOpenPanel alloc] init]; 784 [op setPrompt:@"Boot image"]; 785 [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"]; 786 [op beginSheetForDirectory:nil file:nil types:[NSArray arrayWithObjects:@"img",@"iso",@"dmg",@"qcow",@"cow",@"cloop",@"vmdk",nil] 787 modalForWindow:normalWindow modalDelegate:self 788 didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL]; 789 } else { 790 // or launch QEMU, with the global args 791 [self startEmulationWithArgc:gArgc argv:(char **)gArgv]; 792 } 793} 794 795- (void)applicationWillTerminate:(NSNotification *)aNotification 796{ 797 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n"); 798 799 qemu_system_shutdown_request(); 800 exit(0); 801} 802 803- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication 804{ 805 return YES; 806} 807 808- (void)startEmulationWithArgc:(int)argc argv:(char**)argv 809{ 810 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n"); 811 812 int status; 813 status = qemu_main(argc, argv, *_NSGetEnviron()); 814 exit(status); 815} 816 817- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo 818{ 819 COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n"); 820 821 if(returnCode == NSCancelButton) { 822 exit(0); 823 } else if(returnCode == NSOKButton) { 824 const char *bin = "qemu"; 825 char *img = (char*)[ [ sheet filename ] cStringUsingEncoding:NSASCIIStringEncoding]; 826 827 char **argv = (char**)malloc( sizeof(char*)*3 ); 828 829 [sheet close]; 830 831 argv[0] = g_strdup_printf("%s", bin); 832 argv[1] = g_strdup_printf("-hda"); 833 argv[2] = g_strdup_printf("%s", img); 834 835 printf("Using argc %d argv %s -hda %s\n", 3, bin, img); 836 837 [self startEmulationWithArgc:3 argv:(char**)argv]; 838 } 839} 840- (void)toggleFullScreen:(id)sender 841{ 842 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); 843 844 [cocoaView toggleFullScreen:sender]; 845} 846 847- (void)showQEMUDoc:(id)sender 848{ 849 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n"); 850 851 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html", 852 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; 853} 854 855- (void)showQEMUTec:(id)sender 856{ 857 COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n"); 858 859 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html", 860 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; 861} 862@end 863 864 865 866// Dock Connection 867typedef struct CPSProcessSerNum 868{ 869 UInt32 lo; 870 UInt32 hi; 871} CPSProcessSerNum; 872 873OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); 874OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); 875OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); 876 877int main (int argc, const char * argv[]) { 878 879 gArgc = argc; 880 gArgv = (char **)argv; 881 CPSProcessSerNum PSN; 882 int i; 883 884 /* In case we don't need to display a window, let's not do that */ 885 for (i = 1; i < argc; i++) { 886 const char *opt = argv[i]; 887 888 if (opt[0] == '-') { 889 /* Treat --foo the same as -foo. */ 890 if (opt[1] == '-') { 891 opt++; 892 } 893 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || 894 !strcmp(opt, "-vnc") || 895 !strcmp(opt, "-nographic") || 896 !strcmp(opt, "-version") || 897 !strcmp(opt, "-curses") || 898 !strcmp(opt, "-qtest")) { 899 return qemu_main(gArgc, gArgv, *_NSGetEnviron()); 900 } 901 } 902 } 903 904 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 905 [NSApplication sharedApplication]; 906 907 if (!CPSGetCurrentProcess(&PSN)) 908 if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103)) 909 if (!CPSSetFrontProcess(&PSN)) 910 [NSApplication sharedApplication]; 911 912 // Add menus 913 NSMenu *menu; 914 NSMenuItem *menuItem; 915 916 [NSApp setMainMenu:[[NSMenu alloc] init]]; 917 918 // Application menu 919 menu = [[NSMenu alloc] initWithTitle:@""]; 920 [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU 921 [menu addItem:[NSMenuItem separatorItem]]; //Separator 922 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU 923 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others 924 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; 925 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All 926 [menu addItem:[NSMenuItem separatorItem]]; //Separator 927 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"]; 928 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; 929 [menuItem setSubmenu:menu]; 930 [[NSApp mainMenu] addItem:menuItem]; 931 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+) 932 933 // View menu 934 menu = [[NSMenu alloc] initWithTitle:@"View"]; 935 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen 936 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease]; 937 [menuItem setSubmenu:menu]; 938 [[NSApp mainMenu] addItem:menuItem]; 939 940 // Window menu 941 menu = [[NSMenu alloc] initWithTitle:@"Window"]; 942 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize 943 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; 944 [menuItem setSubmenu:menu]; 945 [[NSApp mainMenu] addItem:menuItem]; 946 [NSApp setWindowsMenu:menu]; 947 948 // Help menu 949 menu = [[NSMenu alloc] initWithTitle:@"Help"]; 950 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help 951 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help 952 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; 953 [menuItem setSubmenu:menu]; 954 [[NSApp mainMenu] addItem:menuItem]; 955 956 // Create an Application controller 957 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init]; 958 [NSApp setDelegate:appController]; 959 960 // Start the main event loop 961 [NSApp run]; 962 963 [appController release]; 964 [pool release]; 965 966 return 0; 967} 968 969 970 971#pragma mark qemu 972static void cocoa_update(DisplayState *ds, int x, int y, int w, int h) 973{ 974 COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); 975 976 NSRect rect; 977 if ([cocoaView cdx] == 1.0) { 978 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); 979 } else { 980 rect = NSMakeRect( 981 x * [cocoaView cdx], 982 ([cocoaView gscreen].height - y - h) * [cocoaView cdy], 983 w * [cocoaView cdx], 984 h * [cocoaView cdy]); 985 } 986 [cocoaView setNeedsDisplayInRect:rect]; 987} 988 989static void cocoa_resize(DisplayState *ds) 990{ 991 COCOA_DEBUG("qemu_cocoa: cocoa_resize\n"); 992 993 [cocoaView resizeContentToWidth:(int)(ds_get_width(ds)) height:(int)(ds_get_height(ds)) displayState:ds]; 994} 995 996static void cocoa_refresh(DisplayState *ds) 997{ 998 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n"); 999 1000 if (kbd_mouse_is_absolute()) { 1001 if (![cocoaView isAbsoluteEnabled]) { 1002 if ([cocoaView isMouseGrabed]) { 1003 [cocoaView ungrabMouse]; 1004 } 1005 } 1006 [cocoaView setAbsoluteEnabled:YES]; 1007 } 1008 1009 NSDate *distantPast; 1010 NSEvent *event; 1011 distantPast = [NSDate distantPast]; 1012 do { 1013 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast 1014 inMode: NSDefaultRunLoopMode dequeue:YES]; 1015 if (event != nil) { 1016 [cocoaView handleEvent:event]; 1017 } 1018 } while(event != nil); 1019 vga_hw_update(); 1020} 1021 1022static void cocoa_setdata(DisplayState *ds) 1023{ 1024 [cocoaView updateDataOffset:ds]; 1025} 1026 1027static void cocoa_cleanup(void) 1028{ 1029 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n"); 1030 g_free(dcl); 1031} 1032 1033void cocoa_display_init(DisplayState *ds, int full_screen) 1034{ 1035 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); 1036 1037 dcl = g_malloc0(sizeof(DisplayChangeListener)); 1038 1039 // register vga output callbacks 1040 dcl->dpy_gfx_update = cocoa_update; 1041 dcl->dpy_gfx_resize = cocoa_resize; 1042 dcl->dpy_refresh = cocoa_refresh; 1043 dcl->dpy_gfx_setdata = cocoa_setdata; 1044 1045 register_displaychangelistener(ds, dcl); 1046 1047 // register cleanup function 1048 atexit(cocoa_cleanup); 1049} 1050