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