blob: be491794dcc35aeaab87f25058131f423e167727 [file] [log] [blame]
bellard5b0753e2005-03-01 21:37:28 +00001/*
thsc304f7e2008-01-22 23:25:15 +00002 * QEMU Cocoa CG display driver
ths5fafdf22007-09-16 21:08:06 +00003 *
thsc304f7e2008-01-22 23:25:15 +00004 * Copyright (c) 2008 Mike Kronenberg
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard5b0753e2005-03-01 21:37:28 +00006 * 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 */
bellardda4dbf72005-03-02 22:22:43 +000024
bellard5b0753e2005-03-01 21:37:28 +000025#import <Cocoa/Cocoa.h>
Andreas Färber3bbbee12011-05-29 19:42:51 +020026#include <crt_externs.h>
bellard5b0753e2005-03-01 21:37:28 +000027
pbrook87ecb682007-11-17 17:14:51 +000028#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010029#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
bellardda4dbf72005-03-02 22:22:43 +000031
Andreas Färber38ec7b52009-12-13 01:52:29 +010032#ifndef MAC_OS_X_VERSION_10_4
33#define MAC_OS_X_VERSION_10_4 1040
34#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010035#ifndef MAC_OS_X_VERSION_10_5
36#define MAC_OS_X_VERSION_10_5 1050
37#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000038#ifndef MAC_OS_X_VERSION_10_6
39#define MAC_OS_X_VERSION_10_6 1060
40#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010041
bellard5b0753e2005-03-01 21:37:28 +000042
thsc304f7e2008-01-22 23:25:15 +000043//#define DEBUG
44
45#ifdef DEBUG
46#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
47#else
48#define COCOA_DEBUG(...) ((void) 0)
49#endif
50
51#define cgrect(nsrect) (*(CGRect *)&(nsrect))
52#define COCOA_MOUSE_EVENT \
53 if (isTabletEnabled) { \
aurel32b94ed572008-03-10 19:34:27 +000054 kbd_mouse_event((int)(p.x * 0x7FFF / (screen.width - 1)), (int)((screen.height - p.y) * 0x7FFF / (screen.height - 1)), 0, buttons); \
thsc304f7e2008-01-22 23:25:15 +000055 } else if (isMouseGrabed) { \
56 kbd_mouse_event((int)[event deltaX], (int)[event deltaY], 0, buttons); \
57 } else { \
58 [NSApp sendEvent:event]; \
59 }
60
61typedef struct {
62 int width;
63 int height;
64 int bitsPerComponent;
65 int bitsPerPixel;
66} QEMUScreen;
67
thsc304f7e2008-01-22 23:25:15 +000068NSWindow *normalWindow;
aliguori9794f742009-03-04 19:25:22 +000069static DisplayChangeListener *dcl;
bellard5b0753e2005-03-01 21:37:28 +000070
71int gArgc;
72char **gArgv;
bellard5b0753e2005-03-01 21:37:28 +000073
thsc304f7e2008-01-22 23:25:15 +000074// keymap conversion
bellard87f48e62005-10-30 18:24:49 +000075int keymap[] =
bellard5b0753e2005-03-01 21:37:28 +000076{
bellard87f48e62005-10-30 18:24:49 +000077// 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
132 0, // 54 0x36 QZ_RMETA
133 0, // 55 0x37 QZ_LMETA
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
thsc304f7e2008-01-22 23:25:15 +0000183 183,// 105 0x69 0xb7 QZ_PRINT
bellard87f48e62005-10-30 18:24:49 +0000184 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 */
ths3b46e622007-09-17 08:09:54 +0000206
bellard87f48e62005-10-30 18:24:49 +0000207/* Aditional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */
208/*
ths3b46e622007-09-17 08:09:54 +0000209 219 // 0xdb e0,5b L GUI
210 220 // 0xdc e0,5c R GUI
211 221 // 0xdd e0,5d APPS
212 // E0,2A,E0,37 PRNT SCRN
213 // E1,1D,45,E1,9D,C5 PAUSE
214 83 // 0x53 0x53 KP .
215// ACPI Scan Codes
216 222 // 0xde E0, 5E Power
217 223 // 0xdf E0, 5F Sleep
218 227 // 0xe3 E0, 63 Wake
219// Windows Multimedia Scan Codes
220 153 // 0x99 E0, 19 Next Track
221 144 // 0x90 E0, 10 Previous Track
222 164 // 0xa4 E0, 24 Stop
223 162 // 0xa2 E0, 22 Play/Pause
224 160 // 0xa0 E0, 20 Mute
225 176 // 0xb0 E0, 30 Volume Up
ths5fafdf22007-09-16 21:08:06 +0000226 174 // 0xae E0, 2E Volume Down
ths3b46e622007-09-17 08:09:54 +0000227 237 // 0xed E0, 6D Media Select
228 236 // 0xec E0, 6C E-Mail
229 161 // 0xa1 E0, 21 Calculator
ths5fafdf22007-09-16 21:08:06 +0000230 235 // 0xeb E0, 6B My Computer
ths3b46e622007-09-17 08:09:54 +0000231 229 // 0xe5 E0, 65 WWW Search
232 178 // 0xb2 E0, 32 WWW Home
233 234 // 0xea E0, 6A WWW Back
ths5fafdf22007-09-16 21:08:06 +0000234 233 // 0xe9 E0, 69 WWW Forward
ths3b46e622007-09-17 08:09:54 +0000235 232 // 0xe8 E0, 68 WWW Stop
ths5fafdf22007-09-16 21:08:06 +0000236 231 // 0xe7 E0, 67 WWW Refresh
ths3b46e622007-09-17 08:09:54 +0000237 230 // 0xe6 E0, 66 WWW Favorites
bellard87f48e62005-10-30 18:24:49 +0000238*/
bellard5b0753e2005-03-01 21:37:28 +0000239};
240
Andreas Färber77047bb2009-12-13 00:55:53 +0100241static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000242{
bellard87f48e62005-10-30 18:24:49 +0000243 if((sizeof(keymap)/sizeof(int)) <= keycode)
bellard5b0753e2005-03-01 21:37:28 +0000244 {
245 printf("(cocoa) warning unknow keycode 0x%x\n", keycode);
246 return 0;
247 }
248 return keymap[keycode];
249}
250
thsc304f7e2008-01-22 23:25:15 +0000251
252
bellard5b0753e2005-03-01 21:37:28 +0000253/*
254 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000255 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000256 ------------------------------------------------------
257*/
thsc304f7e2008-01-22 23:25:15 +0000258@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000259{
thsc304f7e2008-01-22 23:25:15 +0000260 QEMUScreen screen;
261 NSWindow *fullScreenWindow;
262 float cx,cy,cw,ch,cdx,cdy;
263 CGDataProviderRef dataProviderRef;
264 int modifiers_state[256];
265 BOOL isMouseGrabed;
266 BOOL isFullscreen;
267 BOOL isAbsoluteEnabled;
268 BOOL isTabletEnabled;
269}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100270- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000271- (void) grabMouse;
272- (void) ungrabMouse;
273- (void) toggleFullScreen:(id)sender;
274- (void) handleEvent:(NSEvent *)event;
275- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
276- (BOOL) isMouseGrabed;
277- (BOOL) isAbsoluteEnabled;
278- (float) cdx;
279- (float) cdy;
280- (QEMUScreen) gscreen;
281@end
ths3b46e622007-09-17 08:09:54 +0000282
Andreas Färber7fee1992011-06-09 20:53:32 +0200283QemuCocoaView *cocoaView;
284
thsc304f7e2008-01-22 23:25:15 +0000285@implementation QemuCocoaView
286- (id)initWithFrame:(NSRect)frameRect
287{
288 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000289
thsc304f7e2008-01-22 23:25:15 +0000290 self = [super initWithFrame:frameRect];
291 if (self) {
pbrook95219892006-04-09 01:06:34 +0000292
thsc304f7e2008-01-22 23:25:15 +0000293 screen.bitsPerComponent = 8;
294 screen.bitsPerPixel = 32;
295 screen.width = frameRect.size.width;
296 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000297
thsc304f7e2008-01-22 23:25:15 +0000298 }
299 return self;
300}
bellard7c206a72005-12-18 19:18:45 +0000301
thsc304f7e2008-01-22 23:25:15 +0000302- (void) dealloc
303{
304 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000305
thsc304f7e2008-01-22 23:25:15 +0000306 if (dataProviderRef)
307 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000308
thsc304f7e2008-01-22 23:25:15 +0000309 [super dealloc];
310}
bellard5cbfcd02006-06-14 15:53:24 +0000311
Andreas Färberd50f71d2009-12-13 02:03:33 +0100312- (BOOL) isOpaque
313{
314 return YES;
315}
316
thsc304f7e2008-01-22 23:25:15 +0000317- (void) drawRect:(NSRect) rect
318{
319 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000320
thsc304f7e2008-01-22 23:25:15 +0000321 // get CoreGraphic context
322 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
323 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
324 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000325
thsc304f7e2008-01-22 23:25:15 +0000326 // draw screen bitmap directly to Core Graphics context
327 if (dataProviderRef) {
328 CGImageRef imageRef = CGImageCreate(
329 screen.width, //width
330 screen.height, //height
331 screen.bitsPerComponent, //bitsPerComponent
332 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000333 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100334#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000335 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000336 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000337#else
338 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
339 kCGImageAlphaNoneSkipFirst, //bitmapInfo
340#endif
341 dataProviderRef, //provider
342 NULL, //decode
343 0, //interpolate
344 kCGRenderingIntentDefault //intent
345 );
Andreas Färber38ec7b52009-12-13 01:52:29 +0100346// test if host supports "CGImageCreateWithImageInRect" at compile time
thsc304f7e2008-01-22 23:25:15 +0000347#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
348 if (CGImageCreateWithImageInRect == NULL) { // test if "CGImageCreateWithImageInRect" is supported on host at runtime
349#endif
350 // compatibility drawing code (draws everything) (OS X < 10.4)
351 CGContextDrawImage (viewContextRef, CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height), imageRef);
352#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
353 } else {
354 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
355 const NSRect *rectList;
Andreas Färber44e4c0b2009-12-13 00:45:40 +0100356#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
357 NSInteger rectCount;
358#else
thsc304f7e2008-01-22 23:25:15 +0000359 int rectCount;
Andreas Färber44e4c0b2009-12-13 00:45:40 +0100360#endif
thsc304f7e2008-01-22 23:25:15 +0000361 int i;
362 CGImageRef clipImageRef;
363 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000364
thsc304f7e2008-01-22 23:25:15 +0000365 [self getRectsBeingDrawn:&rectList count:&rectCount];
366 for (i = 0; i < rectCount; i++) {
367 clipRect.origin.x = rectList[i].origin.x / cdx;
368 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
369 clipRect.size.width = rectList[i].size.width / cdx;
370 clipRect.size.height = rectList[i].size.height / cdy;
371 clipImageRef = CGImageCreateWithImageInRect(
372 imageRef,
373 clipRect
374 );
375 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
376 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000377 }
378 }
thsc304f7e2008-01-22 23:25:15 +0000379#endif
380 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000381 }
382}
383
thsc304f7e2008-01-22 23:25:15 +0000384- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000385{
thsc304f7e2008-01-22 23:25:15 +0000386 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000387
thsc304f7e2008-01-22 23:25:15 +0000388 if (isFullscreen) {
389 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
390 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
391 cw = screen.width * cdx;
392 ch = screen.height * cdy;
393 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
394 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
395 } else {
396 cx = 0;
397 cy = 0;
398 cw = screen.width;
399 ch = screen.height;
400 cdx = 1.0;
401 cdy = 1.0;
402 }
bellard5b0753e2005-03-01 21:37:28 +0000403}
404
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100405- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000406{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100407 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000408
Peter Maydell8510d912013-03-18 20:28:21 +0000409 int w = surface_width(surface);
410 int h = surface_height(surface);
411
thsc304f7e2008-01-22 23:25:15 +0000412 // update screenBuffer
413 if (dataProviderRef)
414 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000415
aliguori9794f742009-03-04 19:25:22 +0000416 //sync host window color space with guests
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100417 screen.bitsPerPixel = surface_bits_per_pixel(surface);
418 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000419
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100420 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000421
422 // update windows
423 if (isFullscreen) {
424 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
425 [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];
426 } else {
427 if (qemu_name)
428 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Andreas Färber12381082009-12-13 02:49:57 +0100429 [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];
thsc304f7e2008-01-22 23:25:15 +0000430 }
431 screen.width = w;
432 screen.height = h;
aliguori9794f742009-03-04 19:25:22 +0000433 [normalWindow center];
thsc304f7e2008-01-22 23:25:15 +0000434 [self setContentDimensions];
435 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
436}
437
438- (void) toggleFullScreen:(id)sender
439{
440 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
441
442 if (isFullscreen) { // switch from fullscreen to desktop
443 isFullscreen = FALSE;
444 [self ungrabMouse];
445 [self setContentDimensions];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100446// test if host supports "exitFullScreenModeWithOptions" at compile time
447#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000448 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
449 [self exitFullScreenModeWithOptions:nil];
450 } else {
451#endif
452 [fullScreenWindow close];
453 [normalWindow setContentView: self];
454 [normalWindow makeKeyAndOrderFront: self];
455 [NSMenu setMenuBarVisible:YES];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100456#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000457 }
458#endif
459 } else { // switch from desktop to fullscreen
460 isFullscreen = TRUE;
461 [self grabMouse];
462 [self setContentDimensions];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100463// test if host supports "enterFullScreenMode:withOptions" at compile time
464#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000465 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
466 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
467 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
468 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
469 nil]];
470 } else {
471#endif
472 [NSMenu setMenuBarVisible:NO];
473 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
474 styleMask:NSBorderlessWindowMask
475 backing:NSBackingStoreBuffered
476 defer:NO];
477 [fullScreenWindow setHasShadow:NO];
478 [fullScreenWindow setContentView:self];
479 [fullScreenWindow makeKeyAndOrderFront:self];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100480#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000481 }
482#endif
483 }
484}
485
486- (void) handleEvent:(NSEvent *)event
487{
488 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
489
490 int buttons = 0;
491 int keycode;
492 NSPoint p = [event locationInWindow];
493
494 switch ([event type]) {
495 case NSFlagsChanged:
496 keycode = cocoa_keycode_to_qemu([event keyCode]);
497 if (keycode) {
498 if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup
499 kbd_put_keycode(keycode);
500 kbd_put_keycode(keycode | 0x80);
Peter Maydell68c0aa62013-04-17 09:16:35 +0000501 } else if (qemu_console_is_graphic(NULL)) {
thsc304f7e2008-01-22 23:25:15 +0000502 if (keycode & 0x80)
503 kbd_put_keycode(0xe0);
504 if (modifiers_state[keycode] == 0) { // keydown
505 kbd_put_keycode(keycode & 0x7f);
506 modifiers_state[keycode] = 1;
507 } else { // keyup
508 kbd_put_keycode(keycode | 0x80);
509 modifiers_state[keycode] = 0;
510 }
511 }
512 }
513
514 // release Mouse grab when pressing ctrl+alt
515 if (!isFullscreen && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
516 [self ungrabMouse];
517 }
518 break;
519 case NSKeyDown:
520
521 // forward command Key Combos
522 if ([event modifierFlags] & NSCommandKeyMask) {
523 [NSApp sendEvent:event];
524 return;
525 }
526
527 // default
528 keycode = cocoa_keycode_to_qemu([event keyCode]);
529
530 // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
531 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
532 switch (keycode) {
533
534 // enable graphic console
535 case 0x02 ... 0x0a: // '1' to '9' keys
536 console_select(keycode - 0x02);
537 break;
538 }
539
540 // handle keys for graphic console
Peter Maydell68c0aa62013-04-17 09:16:35 +0000541 } else if (qemu_console_is_graphic(NULL)) {
thsc304f7e2008-01-22 23:25:15 +0000542 if (keycode & 0x80) //check bit for e0 in front
543 kbd_put_keycode(0xe0);
544 kbd_put_keycode(keycode & 0x7f); //remove e0 bit in front
545
546 // handlekeys for Monitor
547 } else {
548 int keysym = 0;
549 switch([event keyCode]) {
550 case 115:
551 keysym = QEMU_KEY_HOME;
552 break;
553 case 117:
554 keysym = QEMU_KEY_DELETE;
555 break;
556 case 119:
557 keysym = QEMU_KEY_END;
558 break;
559 case 123:
560 keysym = QEMU_KEY_LEFT;
561 break;
562 case 124:
563 keysym = QEMU_KEY_RIGHT;
564 break;
565 case 125:
566 keysym = QEMU_KEY_DOWN;
567 break;
568 case 126:
569 keysym = QEMU_KEY_UP;
570 break;
571 default:
572 {
573 NSString *ks = [event characters];
574 if ([ks length] > 0)
575 keysym = [ks characterAtIndex:0];
576 }
577 }
578 if (keysym)
579 kbd_put_keysym(keysym);
580 }
581 break;
582 case NSKeyUp:
583 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell68c0aa62013-04-17 09:16:35 +0000584 if (qemu_console_is_graphic(NULL)) {
thsc304f7e2008-01-22 23:25:15 +0000585 if (keycode & 0x80)
586 kbd_put_keycode(0xe0);
587 kbd_put_keycode(keycode | 0x80); //add 128 to signal release of key
588 }
589 break;
590 case NSMouseMoved:
591 if (isAbsoluteEnabled) {
592 if (p.x < 0 || p.x > screen.width || p.y < 0 || p.y > screen.height || ![[self window] isKeyWindow]) {
593 if (isTabletEnabled) { // if we leave the window, deactivate the tablet
594 [NSCursor unhide];
595 isTabletEnabled = FALSE;
596 }
597 } else {
598 if (!isTabletEnabled) { // if we enter the window, activate the tablet
599 [NSCursor hide];
600 isTabletEnabled = TRUE;
601 }
602 }
603 }
604 COCOA_MOUSE_EVENT
605 break;
606 case NSLeftMouseDown:
607 if ([event modifierFlags] & NSCommandKeyMask) {
608 buttons |= MOUSE_EVENT_RBUTTON;
609 } else {
610 buttons |= MOUSE_EVENT_LBUTTON;
611 }
612 COCOA_MOUSE_EVENT
613 break;
614 case NSRightMouseDown:
615 buttons |= MOUSE_EVENT_RBUTTON;
616 COCOA_MOUSE_EVENT
617 break;
618 case NSOtherMouseDown:
619 buttons |= MOUSE_EVENT_MBUTTON;
620 COCOA_MOUSE_EVENT
621 break;
622 case NSLeftMouseDragged:
623 if ([event modifierFlags] & NSCommandKeyMask) {
624 buttons |= MOUSE_EVENT_RBUTTON;
625 } else {
626 buttons |= MOUSE_EVENT_LBUTTON;
627 }
628 COCOA_MOUSE_EVENT
629 break;
630 case NSRightMouseDragged:
631 buttons |= MOUSE_EVENT_RBUTTON;
632 COCOA_MOUSE_EVENT
633 break;
634 case NSOtherMouseDragged:
635 buttons |= MOUSE_EVENT_MBUTTON;
636 COCOA_MOUSE_EVENT
637 break;
638 case NSLeftMouseUp:
639 if (isTabletEnabled) {
640 COCOA_MOUSE_EVENT
641 } else if (!isMouseGrabed) {
642 if (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height) {
643 [self grabMouse];
644 } else {
645 [NSApp sendEvent:event];
646 }
647 } else {
648 COCOA_MOUSE_EVENT
649 }
650 break;
651 case NSRightMouseUp:
652 COCOA_MOUSE_EVENT
653 break;
654 case NSOtherMouseUp:
655 COCOA_MOUSE_EVENT
656 break;
657 case NSScrollWheel:
658 if (isTabletEnabled || isMouseGrabed) {
659 kbd_mouse_event(0, 0, -[event deltaY], 0);
660 } else {
661 [NSApp sendEvent:event];
662 }
663 break;
664 default:
665 [NSApp sendEvent:event];
666 }
667}
668
669- (void) grabMouse
670{
671 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
672
673 if (!isFullscreen) {
674 if (qemu_name)
675 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
676 else
677 [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
678 }
679 [NSCursor hide];
680 CGAssociateMouseAndMouseCursorPosition(FALSE);
681 isMouseGrabed = TRUE; // while isMouseGrabed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
682}
683
684- (void) ungrabMouse
685{
686 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
687
688 if (!isFullscreen) {
689 if (qemu_name)
690 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
691 else
692 [normalWindow setTitle:@"QEMU"];
693 }
694 [NSCursor unhide];
695 CGAssociateMouseAndMouseCursorPosition(TRUE);
696 isMouseGrabed = FALSE;
697}
698
699- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
700- (BOOL) isMouseGrabed {return isMouseGrabed;}
701- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
702- (float) cdx {return cdx;}
703- (float) cdy {return cdy;}
704- (QEMUScreen) gscreen {return screen;}
bellard5b0753e2005-03-01 21:37:28 +0000705@end
706
707
thsc304f7e2008-01-22 23:25:15 +0000708
bellard5b0753e2005-03-01 21:37:28 +0000709/*
710 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000711 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000712 ------------------------------------------------------
713*/
thsc304f7e2008-01-22 23:25:15 +0000714@interface QemuCocoaAppController : NSObject
bellard5b0753e2005-03-01 21:37:28 +0000715{
716}
bellard5b0753e2005-03-01 21:37:28 +0000717- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
thsc304f7e2008-01-22 23:25:15 +0000718- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
719- (void)toggleFullScreen:(id)sender;
720- (void)showQEMUDoc:(id)sender;
721- (void)showQEMUTec:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +0000722@end
723
thsc304f7e2008-01-22 23:25:15 +0000724@implementation QemuCocoaAppController
725- (id) init
726{
727 COCOA_DEBUG("QemuCocoaAppController: init\n");
728
729 self = [super init];
730 if (self) {
731
732 // create a view and add it to the window
733 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
734 if(!cocoaView) {
735 fprintf(stderr, "(cocoa) can't create a view\n");
736 exit(1);
737 }
738
739 // create a window
740 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
741 styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
742 backing:NSBackingStoreBuffered defer:NO];
743 if(!normalWindow) {
744 fprintf(stderr, "(cocoa) can't create window\n");
745 exit(1);
746 }
747 [normalWindow setAcceptsMouseMovedEvents:YES];
748 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]];
749 [normalWindow setContentView:cocoaView];
Andreas Färber561ef252009-12-13 03:06:20 +0100750 [normalWindow useOptimizedDrawing:YES];
thsc304f7e2008-01-22 23:25:15 +0000751 [normalWindow makeKeyAndOrderFront:self];
aliguori9794f742009-03-04 19:25:22 +0000752 [normalWindow center];
thsc304f7e2008-01-22 23:25:15 +0000753
754 }
755 return self;
756}
757
758- (void) dealloc
759{
760 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
761
762 if (cocoaView)
763 [cocoaView release];
764 [super dealloc];
765}
766
bellard5b0753e2005-03-01 21:37:28 +0000767- (void)applicationDidFinishLaunching: (NSNotification *) note
768{
thsc304f7e2008-01-22 23:25:15 +0000769 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
bellard5a246932005-04-07 20:35:06 +0000770
thsc304f7e2008-01-22 23:25:15 +0000771 // Display an open dialog box if no argument were passed or
772 // if qemu was launched from the finder ( the Finder passes "-psn" )
773 if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) {
bellard5b0753e2005-03-01 21:37:28 +0000774 NSOpenPanel *op = [[NSOpenPanel alloc] init];
bellard5b0753e2005-03-01 21:37:28 +0000775 [op setPrompt:@"Boot image"];
bellard5b0753e2005-03-01 21:37:28 +0000776 [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000777 NSArray *filetypes = [NSArray arrayWithObjects:@"img", @"iso", @"dmg",
778 @"qcow", @"cow", @"cloop", @"vmdk", nil];
779#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
780 [op setAllowedFileTypes:filetypes];
781 [op beginSheetModalForWindow:normalWindow
782 completionHandler:^(NSInteger returnCode)
783 { [self openPanelDidEnd:op
784 returnCode:returnCode contextInfo:NULL ]; } ];
785#else
786 // Compatibility code for pre-10.6, using deprecated method
787 [op beginSheetForDirectory:nil file:nil types:filetypes
thsc304f7e2008-01-22 23:25:15 +0000788 modalForWindow:normalWindow modalDelegate:self
bellard5b0753e2005-03-01 21:37:28 +0000789 didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000790#endif
thsc304f7e2008-01-22 23:25:15 +0000791 } else {
Stefan Weil5cbdb3a2012-04-07 09:23:39 +0200792 // or launch QEMU, with the global args
thsc304f7e2008-01-22 23:25:15 +0000793 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5a246932005-04-07 20:35:06 +0000794 }
bellard5b0753e2005-03-01 21:37:28 +0000795}
796
797- (void)applicationWillTerminate:(NSNotification *)aNotification
798{
thsc304f7e2008-01-22 23:25:15 +0000799 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
800
bellard5b0753e2005-03-01 21:37:28 +0000801 qemu_system_shutdown_request();
bellard5b0753e2005-03-01 21:37:28 +0000802 exit(0);
803}
804
Andreas Färber41ea49b2009-12-14 22:13:27 +0100805- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
806{
807 return YES;
808}
809
thsc304f7e2008-01-22 23:25:15 +0000810- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
811{
812 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
813
814 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +0200815 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +0000816 exit(status);
817}
818
bellard5b0753e2005-03-01 21:37:28 +0000819- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
820{
thsc304f7e2008-01-22 23:25:15 +0000821 COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n");
ths3b46e622007-09-17 08:09:54 +0000822
thsc304f7e2008-01-22 23:25:15 +0000823 if(returnCode == NSCancelButton) {
824 exit(0);
825 } else if(returnCode == NSOKButton) {
Andreas Färberfd10a042009-12-13 01:16:27 +0100826 const char *bin = "qemu";
Peter Maydell8bb3f1e2013-04-22 10:29:48 +0000827 char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding];
ths3b46e622007-09-17 08:09:54 +0000828
bellard5b0753e2005-03-01 21:37:28 +0000829 char **argv = (char**)malloc( sizeof(char*)*3 );
ths3b46e622007-09-17 08:09:54 +0000830
Andreas Färber13766eb2011-10-30 23:19:54 +0100831 [sheet close];
832
Stefan Weil7e02dc62013-01-16 07:38:40 +0000833 argv[0] = g_strdup_printf("%s", bin);
834 argv[1] = g_strdup_printf("-hda");
835 argv[2] = g_strdup_printf("%s", img);
ths3b46e622007-09-17 08:09:54 +0000836
bellard5b0753e2005-03-01 21:37:28 +0000837 printf("Using argc %d argv %s -hda %s\n", 3, bin, img);
ths3b46e622007-09-17 08:09:54 +0000838
bellard5b0753e2005-03-01 21:37:28 +0000839 [self startEmulationWithArgc:3 argv:(char**)argv];
840 }
841}
thsc304f7e2008-01-22 23:25:15 +0000842- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +0000843{
thsc304f7e2008-01-22 23:25:15 +0000844 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
845
846 [cocoaView toggleFullScreen:sender];
847}
848
849- (void)showQEMUDoc:(id)sender
850{
851 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
852
853 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
854 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
855}
856
857- (void)showQEMUTec:(id)sender
858{
859 COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
860
861 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
862 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
bellard5b0753e2005-03-01 21:37:28 +0000863}
864@end
865
bellard5b0753e2005-03-01 21:37:28 +0000866
thsc304f7e2008-01-22 23:25:15 +0000867
thsc304f7e2008-01-22 23:25:15 +0000868int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +0000869
thsc304f7e2008-01-22 23:25:15 +0000870 gArgc = argc;
871 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +0100872 int i;
873
874 /* In case we don't need to display a window, let's not do that */
875 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100876 const char *opt = argv[i];
877
878 if (opt[0] == '-') {
879 /* Treat --foo the same as -foo. */
880 if (opt[1] == '-') {
881 opt++;
882 }
Alexandre Raymond98514842011-05-29 18:22:49 -0400883 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
884 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100885 !strcmp(opt, "-nographic") ||
886 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +0200887 !strcmp(opt, "-curses") ||
888 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +0200889 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100890 }
Andreas Färberf4918802009-12-13 02:11:44 +0100891 }
892 }
ths3b46e622007-09-17 08:09:54 +0000893
thsc304f7e2008-01-22 23:25:15 +0000894 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +0000895
Peter Maydell42a5dfe2013-04-22 10:29:47 +0000896 // Pull this console process up to being a fully-fledged graphical
897 // app with a menubar and Dock icon
898 ProcessSerialNumber psn = { 0, kCurrentProcess };
899 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
900
901 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +0000902
thsc304f7e2008-01-22 23:25:15 +0000903 // Add menus
904 NSMenu *menu;
905 NSMenuItem *menuItem;
906
bellard5b0753e2005-03-01 21:37:28 +0000907 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +0000908
thsc304f7e2008-01-22 23:25:15 +0000909 // Application menu
910 menu = [[NSMenu alloc] initWithTitle:@""];
911 [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
912 [menu addItem:[NSMenuItem separatorItem]]; //Separator
913 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
914 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
915 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
916 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
917 [menu addItem:[NSMenuItem separatorItem]]; //Separator
918 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
919 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
920 [menuItem setSubmenu:menu];
921 [[NSApp mainMenu] addItem:menuItem];
922 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +0000923
thsc304f7e2008-01-22 23:25:15 +0000924 // View menu
925 menu = [[NSMenu alloc] initWithTitle:@"View"];
926 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
927 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
928 [menuItem setSubmenu:menu];
929 [[NSApp mainMenu] addItem:menuItem];
930
931 // Window menu
932 menu = [[NSMenu alloc] initWithTitle:@"Window"];
933 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
934 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
935 [menuItem setSubmenu:menu];
936 [[NSApp mainMenu] addItem:menuItem];
937 [NSApp setWindowsMenu:menu];
938
939 // Help menu
940 menu = [[NSMenu alloc] initWithTitle:@"Help"];
941 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
942 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
943 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
944 [menuItem setSubmenu:menu];
945 [[NSApp mainMenu] addItem:menuItem];
946
947 // Create an Application controller
948 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
949 [NSApp setDelegate:appController];
950
951 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +0000952 [NSApp run];
ths3b46e622007-09-17 08:09:54 +0000953
thsc304f7e2008-01-22 23:25:15 +0000954 [appController release];
bellard5b0753e2005-03-01 21:37:28 +0000955 [pool release];
bellardcae41b12006-05-22 21:25:04 +0000956
bellard5b0753e2005-03-01 21:37:28 +0000957 return 0;
958}
thsc304f7e2008-01-22 23:25:15 +0000959
960
961
962#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100963static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100964 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +0000965{
Peter Maydell6e657e62013-04-22 10:29:46 +0000966 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
967
thsc304f7e2008-01-22 23:25:15 +0000968 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
969
970 NSRect rect;
971 if ([cocoaView cdx] == 1.0) {
972 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
973 } else {
974 rect = NSMakeRect(
975 x * [cocoaView cdx],
976 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
977 w * [cocoaView cdx],
978 h * [cocoaView cdy]);
979 }
Andreas Färber17ccbc22009-12-13 02:08:58 +0100980 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +0000981
982 [pool release];
thsc304f7e2008-01-22 23:25:15 +0000983}
984
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100985static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100986 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +0000987{
Peter Maydell6e657e62013-04-22 10:29:46 +0000988 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +0000989
Peter Maydell6e657e62013-04-22 10:29:46 +0000990 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100991 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +0000992 [pool release];
thsc304f7e2008-01-22 23:25:15 +0000993}
994
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100995static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +0000996{
Peter Maydell6e657e62013-04-22 10:29:46 +0000997 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
998
thsc304f7e2008-01-22 23:25:15 +0000999 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
1000
1001 if (kbd_mouse_is_absolute()) {
1002 if (![cocoaView isAbsoluteEnabled]) {
1003 if ([cocoaView isMouseGrabed]) {
1004 [cocoaView ungrabMouse];
1005 }
1006 }
1007 [cocoaView setAbsoluteEnabled:YES];
1008 }
1009
1010 NSDate *distantPast;
1011 NSEvent *event;
1012 distantPast = [NSDate distantPast];
1013 do {
1014 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
1015 inMode: NSDefaultRunLoopMode dequeue:YES];
1016 if (event != nil) {
1017 [cocoaView handleEvent:event];
1018 }
1019 } while(event != nil);
Peter Maydell68c0aa62013-04-17 09:16:35 +00001020 graphic_hw_update(NULL);
Peter Maydell6e657e62013-04-22 10:29:46 +00001021 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001022}
1023
1024static void cocoa_cleanup(void)
1025{
1026 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001027 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001028}
1029
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001030static const DisplayChangeListenerOps dcl_ops = {
1031 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001032 .dpy_gfx_update = cocoa_update,
1033 .dpy_gfx_switch = cocoa_switch,
1034 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001035};
1036
thsc304f7e2008-01-22 23:25:15 +00001037void cocoa_display_init(DisplayState *ds, int full_screen)
1038{
1039 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1040
Blue Swirl58a06672011-08-21 18:42:08 +00001041 dcl = g_malloc0(sizeof(DisplayChangeListener));
1042
aliguori9794f742009-03-04 19:25:22 +00001043 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001044 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001045 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001046
1047 // register cleanup function
1048 atexit(cocoa_cleanup);
1049}