blob: 0a51fbc51885c75457881864fa8a16480eb020ad [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"
Gerd Hoffmann21bae112013-12-04 14:08:04 +010030#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
bellardda4dbf72005-03-02 22:22:43 +000032
Andreas Färber44e4c0b2009-12-13 00:45:40 +010033#ifndef MAC_OS_X_VERSION_10_5
34#define MAC_OS_X_VERSION_10_5 1050
35#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000036#ifndef MAC_OS_X_VERSION_10_6
37#define MAC_OS_X_VERSION_10_6 1060
38#endif
Peter Maydell81801ae2015-05-19 09:11:18 +010039#ifndef MAC_OS_X_VERSION_10_10
40#define MAC_OS_X_VERSION_10_10 101000
41#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010042
bellard5b0753e2005-03-01 21:37:28 +000043
thsc304f7e2008-01-22 23:25:15 +000044//#define DEBUG
45
46#ifdef DEBUG
47#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
48#else
49#define COCOA_DEBUG(...) ((void) 0)
50#endif
51
52#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000053
54typedef struct {
55 int width;
56 int height;
57 int bitsPerComponent;
58 int bitsPerPixel;
59} QEMUScreen;
60
thsc304f7e2008-01-22 23:25:15 +000061NSWindow *normalWindow;
aliguori9794f742009-03-04 19:25:22 +000062static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +010063static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +000064
65int gArgc;
66char **gArgv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +010067bool stretch_video;
bellard5b0753e2005-03-01 21:37:28 +000068
thsc304f7e2008-01-22 23:25:15 +000069// keymap conversion
bellard87f48e62005-10-30 18:24:49 +000070int keymap[] =
bellard5b0753e2005-03-01 21:37:28 +000071{
bellard87f48e62005-10-30 18:24:49 +000072// SdlI macI macH SdlH 104xtH 104xtC sdl
73 30, // 0 0x00 0x1e A QZ_a
74 31, // 1 0x01 0x1f S QZ_s
75 32, // 2 0x02 0x20 D QZ_d
76 33, // 3 0x03 0x21 F QZ_f
77 35, // 4 0x04 0x23 H QZ_h
78 34, // 5 0x05 0x22 G QZ_g
79 44, // 6 0x06 0x2c Z QZ_z
80 45, // 7 0x07 0x2d X QZ_x
81 46, // 8 0x08 0x2e C QZ_c
82 47, // 9 0x09 0x2f V QZ_v
83 0, // 10 0x0A Undefined
84 48, // 11 0x0B 0x30 B QZ_b
85 16, // 12 0x0C 0x10 Q QZ_q
86 17, // 13 0x0D 0x11 W QZ_w
87 18, // 14 0x0E 0x12 E QZ_e
88 19, // 15 0x0F 0x13 R QZ_r
89 21, // 16 0x10 0x15 Y QZ_y
90 20, // 17 0x11 0x14 T QZ_t
91 2, // 18 0x12 0x02 1 QZ_1
92 3, // 19 0x13 0x03 2 QZ_2
93 4, // 20 0x14 0x04 3 QZ_3
94 5, // 21 0x15 0x05 4 QZ_4
95 7, // 22 0x16 0x07 6 QZ_6
96 6, // 23 0x17 0x06 5 QZ_5
97 13, // 24 0x18 0x0d = QZ_EQUALS
98 10, // 25 0x19 0x0a 9 QZ_9
99 8, // 26 0x1A 0x08 7 QZ_7
100 12, // 27 0x1B 0x0c - QZ_MINUS
101 9, // 28 0x1C 0x09 8 QZ_8
102 11, // 29 0x1D 0x0b 0 QZ_0
103 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET
104 24, // 31 0x1F 0x18 O QZ_o
105 22, // 32 0x20 0x16 U QZ_u
106 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET
107 23, // 34 0x22 0x17 I QZ_i
108 25, // 35 0x23 0x19 P QZ_p
109 28, // 36 0x24 0x1c ENTER QZ_RETURN
110 38, // 37 0x25 0x26 L QZ_l
111 36, // 38 0x26 0x24 J QZ_j
112 40, // 39 0x27 0x28 ' QZ_QUOTE
113 37, // 40 0x28 0x25 K QZ_k
114 39, // 41 0x29 0x27 ; QZ_SEMICOLON
115 43, // 42 0x2A 0x2b \ QZ_BACKSLASH
116 51, // 43 0x2B 0x33 , QZ_COMMA
117 53, // 44 0x2C 0x35 / QZ_SLASH
118 49, // 45 0x2D 0x31 N QZ_n
119 50, // 46 0x2E 0x32 M QZ_m
120 52, // 47 0x2F 0x34 . QZ_PERIOD
121 15, // 48 0x30 0x0f TAB QZ_TAB
122 57, // 49 0x31 0x39 SPACE QZ_SPACE
123 41, // 50 0x32 0x29 ` QZ_BACKQUOTE
124 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE
125 0, // 52 0x34 Undefined
126 1, // 53 0x35 0x01 ESC QZ_ESCAPE
Peter Maydell88959192013-12-08 22:59:02 +0000127 220, // 54 0x36 0xdc E0,5C R GUI QZ_RMETA
128 219, // 55 0x37 0xdb E0,5B L GUI QZ_LMETA
bellard87f48e62005-10-30 18:24:49 +0000129 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT
130 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK
131 56, // 58 0x3A 0x38 L ALT QZ_LALT
132 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL
133 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT
134 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT
135 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL
136 0, // 63 0x3F Undefined
137 0, // 64 0x40 Undefined
138 0, // 65 0x41 Undefined
139 0, // 66 0x42 Undefined
140 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY
141 0, // 68 0x44 Undefined
142 78, // 69 0x45 0x4e KP + QZ_KP_PLUS
143 0, // 70 0x46 Undefined
144 69, // 71 0x47 0x45 NUM QZ_NUMLOCK
145 0, // 72 0x48 Undefined
146 0, // 73 0x49 Undefined
147 0, // 74 0x4A Undefined
148 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE
149 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER
150 0, // 77 0x4D undefined
151 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS
152 0, // 79 0x4F Undefined
153 0, // 80 0x50 Undefined
154 0, // 81 0x51 QZ_KP_EQUALS
155 82, // 82 0x52 0x52 KP 0 QZ_KP0
156 79, // 83 0x53 0x4f KP 1 QZ_KP1
157 80, // 84 0x54 0x50 KP 2 QZ_KP2
158 81, // 85 0x55 0x51 KP 3 QZ_KP3
159 75, // 86 0x56 0x4b KP 4 QZ_KP4
160 76, // 87 0x57 0x4c KP 5 QZ_KP5
161 77, // 88 0x58 0x4d KP 6 QZ_KP6
162 71, // 89 0x59 0x47 KP 7 QZ_KP7
163 0, // 90 0x5A Undefined
164 72, // 91 0x5B 0x48 KP 8 QZ_KP8
165 73, // 92 0x5C 0x49 KP 9 QZ_KP9
166 0, // 93 0x5D Undefined
167 0, // 94 0x5E Undefined
168 0, // 95 0x5F Undefined
169 63, // 96 0x60 0x3f F5 QZ_F5
170 64, // 97 0x61 0x40 F6 QZ_F6
171 65, // 98 0x62 0x41 F7 QZ_F7
172 61, // 99 0x63 0x3d F3 QZ_F3
173 66, // 100 0x64 0x42 F8 QZ_F8
174 67, // 101 0x65 0x43 F9 QZ_F9
175 0, // 102 0x66 Undefined
176 87, // 103 0x67 0x57 F11 QZ_F11
177 0, // 104 0x68 Undefined
thsc304f7e2008-01-22 23:25:15 +0000178 183,// 105 0x69 0xb7 QZ_PRINT
bellard87f48e62005-10-30 18:24:49 +0000179 0, // 106 0x6A Undefined
180 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK
181 0, // 108 0x6C Undefined
182 68, // 109 0x6D 0x44 F10 QZ_F10
183 0, // 110 0x6E Undefined
184 88, // 111 0x6F 0x58 F12 QZ_F12
185 0, // 112 0x70 Undefined
186 110,// 113 0x71 0x0 QZ_PAUSE
187 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT
188 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME
189 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP
190 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE
191 62, // 118 0x76 0x3e F4 QZ_F4
192 207,// 119 0x77 0xcf E0,4f END QZ_END
193 60, // 120 0x78 0x3c F2 QZ_F2
194 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN
195 59, // 122 0x7A 0x3b F1 QZ_F1
196 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT
197 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT
198 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN
199 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP
200/* 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 +0000201
Peter Maydell49b9bd42013-12-08 22:59:03 +0000202/* Additional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */
bellard87f48e62005-10-30 18:24:49 +0000203/*
ths3b46e622007-09-17 08:09:54 +0000204 221 // 0xdd e0,5d APPS
205 // E0,2A,E0,37 PRNT SCRN
206 // E1,1D,45,E1,9D,C5 PAUSE
207 83 // 0x53 0x53 KP .
208// ACPI Scan Codes
209 222 // 0xde E0, 5E Power
210 223 // 0xdf E0, 5F Sleep
211 227 // 0xe3 E0, 63 Wake
212// Windows Multimedia Scan Codes
213 153 // 0x99 E0, 19 Next Track
214 144 // 0x90 E0, 10 Previous Track
215 164 // 0xa4 E0, 24 Stop
216 162 // 0xa2 E0, 22 Play/Pause
217 160 // 0xa0 E0, 20 Mute
218 176 // 0xb0 E0, 30 Volume Up
ths5fafdf22007-09-16 21:08:06 +0000219 174 // 0xae E0, 2E Volume Down
ths3b46e622007-09-17 08:09:54 +0000220 237 // 0xed E0, 6D Media Select
221 236 // 0xec E0, 6C E-Mail
222 161 // 0xa1 E0, 21 Calculator
ths5fafdf22007-09-16 21:08:06 +0000223 235 // 0xeb E0, 6B My Computer
ths3b46e622007-09-17 08:09:54 +0000224 229 // 0xe5 E0, 65 WWW Search
225 178 // 0xb2 E0, 32 WWW Home
226 234 // 0xea E0, 6A WWW Back
ths5fafdf22007-09-16 21:08:06 +0000227 233 // 0xe9 E0, 69 WWW Forward
ths3b46e622007-09-17 08:09:54 +0000228 232 // 0xe8 E0, 68 WWW Stop
ths5fafdf22007-09-16 21:08:06 +0000229 231 // 0xe7 E0, 67 WWW Refresh
ths3b46e622007-09-17 08:09:54 +0000230 230 // 0xe6 E0, 66 WWW Favorites
bellard87f48e62005-10-30 18:24:49 +0000231*/
bellard5b0753e2005-03-01 21:37:28 +0000232};
233
Andreas Färber77047bb2009-12-13 00:55:53 +0100234static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000235{
Stefan Weil5d701922013-12-07 14:48:05 +0100236 if (ARRAY_SIZE(keymap) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000237 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000238 return 0;
239 }
240 return keymap[keycode];
241}
242
thsc304f7e2008-01-22 23:25:15 +0000243
244
bellard5b0753e2005-03-01 21:37:28 +0000245/*
246 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000247 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000248 ------------------------------------------------------
249*/
thsc304f7e2008-01-22 23:25:15 +0000250@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000251{
thsc304f7e2008-01-22 23:25:15 +0000252 QEMUScreen screen;
253 NSWindow *fullScreenWindow;
254 float cx,cy,cw,ch,cdx,cdy;
255 CGDataProviderRef dataProviderRef;
256 int modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000257 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000258 BOOL isFullscreen;
259 BOOL isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100260 BOOL isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000261}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100262- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000263- (void) grabMouse;
264- (void) ungrabMouse;
265- (void) toggleFullScreen:(id)sender;
266- (void) handleEvent:(NSEvent *)event;
267- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100268/* The state surrounding mouse grabbing is potentially confusing.
269 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
270 * pointing device an absolute-position one?"], but is only updated on
271 * next refresh.
272 * isMouseGrabbed tracks whether GUI events are directed to the guest;
273 * it controls whether special keys like Cmd get sent to the guest,
274 * and whether we capture the mouse when in non-absolute mode.
275 * isMouseDeassociated tracks whether we've told MacOSX to disassociate
276 * the mouse and mouse cursor position by calling
277 * CGAssociateMouseAndMouseCursorPosition(FALSE)
278 * (which basically happens if we grab in non-absolute mode).
279 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000280- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000281- (BOOL) isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100282- (BOOL) isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000283- (float) cdx;
284- (float) cdy;
285- (QEMUScreen) gscreen;
286@end
ths3b46e622007-09-17 08:09:54 +0000287
Andreas Färber7fee1992011-06-09 20:53:32 +0200288QemuCocoaView *cocoaView;
289
thsc304f7e2008-01-22 23:25:15 +0000290@implementation QemuCocoaView
291- (id)initWithFrame:(NSRect)frameRect
292{
293 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000294
thsc304f7e2008-01-22 23:25:15 +0000295 self = [super initWithFrame:frameRect];
296 if (self) {
pbrook95219892006-04-09 01:06:34 +0000297
thsc304f7e2008-01-22 23:25:15 +0000298 screen.bitsPerComponent = 8;
299 screen.bitsPerPixel = 32;
300 screen.width = frameRect.size.width;
301 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000302
thsc304f7e2008-01-22 23:25:15 +0000303 }
304 return self;
305}
bellard7c206a72005-12-18 19:18:45 +0000306
thsc304f7e2008-01-22 23:25:15 +0000307- (void) dealloc
308{
309 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000310
thsc304f7e2008-01-22 23:25:15 +0000311 if (dataProviderRef)
312 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000313
thsc304f7e2008-01-22 23:25:15 +0000314 [super dealloc];
315}
bellard5cbfcd02006-06-14 15:53:24 +0000316
Andreas Färberd50f71d2009-12-13 02:03:33 +0100317- (BOOL) isOpaque
318{
319 return YES;
320}
321
Peter Maydell5dd45be2014-06-23 10:35:23 +0100322- (BOOL) screenContainsPoint:(NSPoint) p
323{
324 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
325}
326
Peter Maydell13aefd32014-06-23 10:35:25 +0100327- (void) hideCursor
328{
329 if (!cursor_hide) {
330 return;
331 }
332 [NSCursor hide];
333}
334
335- (void) unhideCursor
336{
337 if (!cursor_hide) {
338 return;
339 }
340 [NSCursor unhide];
341}
342
thsc304f7e2008-01-22 23:25:15 +0000343- (void) drawRect:(NSRect) rect
344{
345 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000346
thsc304f7e2008-01-22 23:25:15 +0000347 // get CoreGraphic context
348 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
349 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
350 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000351
thsc304f7e2008-01-22 23:25:15 +0000352 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000353 if (!dataProviderRef) {
354 // Draw request before any guest device has set up a framebuffer:
355 // just draw an opaque black rectangle
356 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
357 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
358 } else {
thsc304f7e2008-01-22 23:25:15 +0000359 CGImageRef imageRef = CGImageCreate(
360 screen.width, //width
361 screen.height, //height
362 screen.bitsPerComponent, //bitsPerComponent
363 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000364 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100365#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000366 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000367 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000368#else
369 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
370 kCGImageAlphaNoneSkipFirst, //bitmapInfo
371#endif
372 dataProviderRef, //provider
373 NULL, //decode
374 0, //interpolate
375 kCGRenderingIntentDefault //intent
376 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100377 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
378 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100379 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100380 int i;
381 CGImageRef clipImageRef;
382 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000383
Peter Maydellb63901d2015-05-19 09:11:17 +0100384 [self getRectsBeingDrawn:&rectList count:&rectCount];
385 for (i = 0; i < rectCount; i++) {
386 clipRect.origin.x = rectList[i].origin.x / cdx;
387 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
388 clipRect.size.width = rectList[i].size.width / cdx;
389 clipRect.size.height = rectList[i].size.height / cdy;
390 clipImageRef = CGImageCreateWithImageInRect(
391 imageRef,
392 clipRect
393 );
394 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
395 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000396 }
thsc304f7e2008-01-22 23:25:15 +0000397 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000398 }
399}
400
thsc304f7e2008-01-22 23:25:15 +0000401- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000402{
thsc304f7e2008-01-22 23:25:15 +0000403 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000404
thsc304f7e2008-01-22 23:25:15 +0000405 if (isFullscreen) {
406 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
407 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100408
409 /* stretches video, but keeps same aspect ratio */
410 if (stretch_video == true) {
411 /* use smallest stretch value - prevents clipping on sides */
412 if (MIN(cdx, cdy) == cdx) {
413 cdy = cdx;
414 } else {
415 cdx = cdy;
416 }
417 } else { /* No stretching */
418 cdx = cdy = 1;
419 }
thsc304f7e2008-01-22 23:25:15 +0000420 cw = screen.width * cdx;
421 ch = screen.height * cdy;
422 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
423 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
424 } else {
425 cx = 0;
426 cy = 0;
427 cw = screen.width;
428 ch = screen.height;
429 cdx = 1.0;
430 cdy = 1.0;
431 }
bellard5b0753e2005-03-01 21:37:28 +0000432}
433
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100434- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000435{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100436 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000437
Peter Maydell8510d912013-03-18 20:28:21 +0000438 int w = surface_width(surface);
439 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100440 /* cdx == 0 means this is our very first surface, in which case we need
441 * to recalculate the content dimensions even if it happens to be the size
442 * of the initial empty window.
443 */
444 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000445
446 int oldh = screen.height;
447 if (isResize) {
448 // Resize before we trigger the redraw, or we'll redraw at the wrong size
449 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
450 screen.width = w;
451 screen.height = h;
452 [self setContentDimensions];
453 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
454 }
Peter Maydell8510d912013-03-18 20:28:21 +0000455
thsc304f7e2008-01-22 23:25:15 +0000456 // update screenBuffer
457 if (dataProviderRef)
458 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000459
aliguori9794f742009-03-04 19:25:22 +0000460 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000461 screen.bitsPerPixel = surface_bits_per_pixel(surface);
462 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000463
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100464 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000465
466 // update windows
467 if (isFullscreen) {
468 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000469 [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:NO animate:NO];
thsc304f7e2008-01-22 23:25:15 +0000470 } else {
471 if (qemu_name)
472 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000473 [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:YES animate:NO];
thsc304f7e2008-01-22 23:25:15 +0000474 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000475
476 if (isResize) {
477 [normalWindow center];
478 }
thsc304f7e2008-01-22 23:25:15 +0000479}
480
481- (void) toggleFullScreen:(id)sender
482{
483 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
484
485 if (isFullscreen) { // switch from fullscreen to desktop
486 isFullscreen = FALSE;
487 [self ungrabMouse];
488 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000489 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
490 [self exitFullScreenModeWithOptions:nil];
491 } else {
thsc304f7e2008-01-22 23:25:15 +0000492 [fullScreenWindow close];
493 [normalWindow setContentView: self];
494 [normalWindow makeKeyAndOrderFront: self];
495 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000496 }
thsc304f7e2008-01-22 23:25:15 +0000497 } else { // switch from desktop to fullscreen
498 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100499 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000500 [self grabMouse];
501 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000502 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
503 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
504 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
505 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
506 nil]];
507 } else {
thsc304f7e2008-01-22 23:25:15 +0000508 [NSMenu setMenuBarVisible:NO];
509 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
510 styleMask:NSBorderlessWindowMask
511 backing:NSBackingStoreBuffered
512 defer:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100513 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
thsc304f7e2008-01-22 23:25:15 +0000514 [fullScreenWindow setHasShadow:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100515 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
516 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
517 [[fullScreenWindow contentView] addSubview: self];
thsc304f7e2008-01-22 23:25:15 +0000518 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000519 }
thsc304f7e2008-01-22 23:25:15 +0000520 }
521}
522
523- (void) handleEvent:(NSEvent *)event
524{
525 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
526
527 int buttons = 0;
528 int keycode;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100529 bool mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000530 NSPoint p = [event locationInWindow];
531
532 switch ([event type]) {
533 case NSFlagsChanged:
534 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000535
Peter Maydell49b9bd42013-12-08 22:59:03 +0000536 if ((keycode == 219 || keycode == 220) && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000537 /* Don't pass command key changes to guest unless mouse is grabbed */
538 keycode = 0;
539 }
540
thsc304f7e2008-01-22 23:25:15 +0000541 if (keycode) {
542 if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100543 qemu_input_event_send_key_number(dcl->con, keycode, true);
544 qemu_input_event_send_key_number(dcl->con, keycode, false);
Peter Maydell68c0aa62013-04-17 09:16:35 +0000545 } else if (qemu_console_is_graphic(NULL)) {
thsc304f7e2008-01-22 23:25:15 +0000546 if (modifiers_state[keycode] == 0) { // keydown
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100547 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000548 modifiers_state[keycode] = 1;
549 } else { // keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100550 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000551 modifiers_state[keycode] = 0;
552 }
553 }
554 }
555
556 // release Mouse grab when pressing ctrl+alt
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100557 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
thsc304f7e2008-01-22 23:25:15 +0000558 [self ungrabMouse];
559 }
560 break;
561 case NSKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000562 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000563
Peter Maydell88959192013-12-08 22:59:02 +0000564 // forward command key combos to the host UI unless the mouse is grabbed
Peter Maydell49b9bd42013-12-08 22:59:03 +0000565 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
thsc304f7e2008-01-22 23:25:15 +0000566 [NSApp sendEvent:event];
567 return;
568 }
569
570 // default
thsc304f7e2008-01-22 23:25:15 +0000571
572 // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
573 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
574 switch (keycode) {
575
576 // enable graphic console
577 case 0x02 ... 0x0a: // '1' to '9' keys
578 console_select(keycode - 0x02);
579 break;
580 }
581
582 // handle keys for graphic console
Peter Maydell68c0aa62013-04-17 09:16:35 +0000583 } else if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100584 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000585
586 // handlekeys for Monitor
587 } else {
588 int keysym = 0;
589 switch([event keyCode]) {
590 case 115:
591 keysym = QEMU_KEY_HOME;
592 break;
593 case 117:
594 keysym = QEMU_KEY_DELETE;
595 break;
596 case 119:
597 keysym = QEMU_KEY_END;
598 break;
599 case 123:
600 keysym = QEMU_KEY_LEFT;
601 break;
602 case 124:
603 keysym = QEMU_KEY_RIGHT;
604 break;
605 case 125:
606 keysym = QEMU_KEY_DOWN;
607 break;
608 case 126:
609 keysym = QEMU_KEY_UP;
610 break;
611 default:
612 {
613 NSString *ks = [event characters];
614 if ([ks length] > 0)
615 keysym = [ks characterAtIndex:0];
616 }
617 }
618 if (keysym)
619 kbd_put_keysym(keysym);
620 }
621 break;
622 case NSKeyUp:
623 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000624
625 // don't pass the guest a spurious key-up if we treated this
626 // command-key combo as a host UI action
Peter Maydell49b9bd42013-12-08 22:59:03 +0000627 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
Peter Maydell88959192013-12-08 22:59:02 +0000628 return;
629 }
630
Peter Maydell68c0aa62013-04-17 09:16:35 +0000631 if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100632 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000633 }
634 break;
635 case NSMouseMoved:
636 if (isAbsoluteEnabled) {
Peter Maydell5dd45be2014-06-23 10:35:23 +0100637 if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
Peter Maydellf61c3872014-06-23 10:35:24 +0100638 if (isMouseGrabbed) {
639 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +0000640 }
641 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +0100642 if (!isMouseGrabbed) {
643 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000644 }
645 }
646 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100647 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000648 break;
649 case NSLeftMouseDown:
650 if ([event modifierFlags] & NSCommandKeyMask) {
651 buttons |= MOUSE_EVENT_RBUTTON;
652 } else {
653 buttons |= MOUSE_EVENT_LBUTTON;
654 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100655 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000656 break;
657 case NSRightMouseDown:
658 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100659 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000660 break;
661 case NSOtherMouseDown:
662 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100663 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000664 break;
665 case NSLeftMouseDragged:
666 if ([event modifierFlags] & NSCommandKeyMask) {
667 buttons |= MOUSE_EVENT_RBUTTON;
668 } else {
669 buttons |= MOUSE_EVENT_LBUTTON;
670 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100671 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000672 break;
673 case NSRightMouseDragged:
674 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100675 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000676 break;
677 case NSOtherMouseDragged:
678 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100679 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000680 break;
681 case NSLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +0100682 mouse_event = true;
683 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
684 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000685 }
686 break;
687 case NSRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100688 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000689 break;
690 case NSOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100691 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000692 break;
693 case NSScrollWheel:
Peter Maydellf61c3872014-06-23 10:35:24 +0100694 if (isMouseGrabbed) {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100695 buttons |= ([event deltaY] < 0) ?
696 MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
thsc304f7e2008-01-22 23:25:15 +0000697 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100698 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000699 break;
700 default:
701 [NSApp sendEvent:event];
702 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100703
704 if (mouse_event) {
705 if (last_buttons != buttons) {
706 static uint32_t bmap[INPUT_BUTTON_MAX] = {
707 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
708 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
709 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
710 [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP,
711 [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
712 };
713 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
714 last_buttons = buttons;
715 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100716 if (isMouseGrabbed) {
717 if (isAbsoluteEnabled) {
718 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
719 * The check on screenContainsPoint is to avoid sending out of range values for
720 * clicks in the titlebar.
721 */
722 if ([self screenContainsPoint:p]) {
723 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width);
724 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height);
725 }
726 } else {
727 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
728 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
729 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100730 } else {
731 [NSApp sendEvent:event];
732 }
733 qemu_input_event_sync();
734 }
thsc304f7e2008-01-22 23:25:15 +0000735}
736
737- (void) grabMouse
738{
739 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
740
741 if (!isFullscreen) {
742 if (qemu_name)
743 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
744 else
745 [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
746 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100747 [self hideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100748 if (!isAbsoluteEnabled) {
749 isMouseDeassociated = TRUE;
750 CGAssociateMouseAndMouseCursorPosition(FALSE);
751 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000752 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000753}
754
755- (void) ungrabMouse
756{
757 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
758
759 if (!isFullscreen) {
760 if (qemu_name)
761 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
762 else
763 [normalWindow setTitle:@"QEMU"];
764 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100765 [self unhideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100766 if (isMouseDeassociated) {
767 CGAssociateMouseAndMouseCursorPosition(TRUE);
768 isMouseDeassociated = FALSE;
769 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000770 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000771}
772
773- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000774- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000775- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
Peter Maydellf61c3872014-06-23 10:35:24 +0100776- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
thsc304f7e2008-01-22 23:25:15 +0000777- (float) cdx {return cdx;}
778- (float) cdy {return cdy;}
779- (QEMUScreen) gscreen {return screen;}
bellard5b0753e2005-03-01 21:37:28 +0000780@end
781
782
thsc304f7e2008-01-22 23:25:15 +0000783
bellard5b0753e2005-03-01 21:37:28 +0000784/*
785 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000786 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000787 ------------------------------------------------------
788*/
thsc304f7e2008-01-22 23:25:15 +0000789@interface QemuCocoaAppController : NSObject
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100790#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
791 <NSApplicationDelegate>
792#endif
bellard5b0753e2005-03-01 21:37:28 +0000793{
794}
bellard5b0753e2005-03-01 21:37:28 +0000795- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
Peter Maydellde1aade2015-05-19 09:11:18 +0100796- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100797- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +0000798- (void)toggleFullScreen:(id)sender;
799- (void)showQEMUDoc:(id)sender;
800- (void)showQEMUTec:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100801- (void)zoomToFit:(id) sender;
bellard5b0753e2005-03-01 21:37:28 +0000802@end
803
thsc304f7e2008-01-22 23:25:15 +0000804@implementation QemuCocoaAppController
805- (id) init
806{
807 COCOA_DEBUG("QemuCocoaAppController: init\n");
808
809 self = [super init];
810 if (self) {
811
812 // create a view and add it to the window
813 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
814 if(!cocoaView) {
815 fprintf(stderr, "(cocoa) can't create a view\n");
816 exit(1);
817 }
818
819 // create a window
820 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
821 styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
822 backing:NSBackingStoreBuffered defer:NO];
823 if(!normalWindow) {
824 fprintf(stderr, "(cocoa) can't create window\n");
825 exit(1);
826 }
827 [normalWindow setAcceptsMouseMovedEvents:YES];
828 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]];
829 [normalWindow setContentView:cocoaView];
Peter Maydell81801ae2015-05-19 09:11:18 +0100830#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
Andreas Färber561ef252009-12-13 03:06:20 +0100831 [normalWindow useOptimizedDrawing:YES];
Peter Maydell81801ae2015-05-19 09:11:18 +0100832#endif
thsc304f7e2008-01-22 23:25:15 +0000833 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +0000834 [normalWindow center];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100835 stretch_video = false;
thsc304f7e2008-01-22 23:25:15 +0000836 }
837 return self;
838}
839
840- (void) dealloc
841{
842 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
843
844 if (cocoaView)
845 [cocoaView release];
846 [super dealloc];
847}
848
bellard5b0753e2005-03-01 21:37:28 +0000849- (void)applicationDidFinishLaunching: (NSNotification *) note
850{
thsc304f7e2008-01-22 23:25:15 +0000851 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
bellard5a246932005-04-07 20:35:06 +0000852
Peter Maydell49b9bd42013-12-08 22:59:03 +0000853 // Display an open dialog box if no arguments were passed or
thsc304f7e2008-01-22 23:25:15 +0000854 // if qemu was launched from the finder ( the Finder passes "-psn" )
855 if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) {
bellard5b0753e2005-03-01 21:37:28 +0000856 NSOpenPanel *op = [[NSOpenPanel alloc] init];
bellard5b0753e2005-03-01 21:37:28 +0000857 [op setPrompt:@"Boot image"];
bellard5b0753e2005-03-01 21:37:28 +0000858 [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000859 NSArray *filetypes = [NSArray arrayWithObjects:@"img", @"iso", @"dmg",
Stefan Hajnoczi550830f2014-09-16 15:24:24 +0100860 @"qcow", @"qcow2", @"cloop", @"vmdk", nil];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000861#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
862 [op setAllowedFileTypes:filetypes];
863 [op beginSheetModalForWindow:normalWindow
864 completionHandler:^(NSInteger returnCode)
865 { [self openPanelDidEnd:op
866 returnCode:returnCode contextInfo:NULL ]; } ];
867#else
868 // Compatibility code for pre-10.6, using deprecated method
869 [op beginSheetForDirectory:nil file:nil types:filetypes
thsc304f7e2008-01-22 23:25:15 +0000870 modalForWindow:normalWindow modalDelegate:self
bellard5b0753e2005-03-01 21:37:28 +0000871 didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000872#endif
thsc304f7e2008-01-22 23:25:15 +0000873 } else {
Stefan Weil5cbdb3a2012-04-07 09:23:39 +0200874 // or launch QEMU, with the global args
thsc304f7e2008-01-22 23:25:15 +0000875 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5a246932005-04-07 20:35:06 +0000876 }
bellard5b0753e2005-03-01 21:37:28 +0000877}
878
879- (void)applicationWillTerminate:(NSNotification *)aNotification
880{
thsc304f7e2008-01-22 23:25:15 +0000881 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
882
bellard5b0753e2005-03-01 21:37:28 +0000883 qemu_system_shutdown_request();
bellard5b0753e2005-03-01 21:37:28 +0000884 exit(0);
885}
886
Andreas Färber41ea49b2009-12-14 22:13:27 +0100887- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
888{
889 return YES;
890}
891
thsc304f7e2008-01-22 23:25:15 +0000892- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
893{
894 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
895
896 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +0200897 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +0000898 exit(status);
899}
900
Peter Maydellde1aade2015-05-19 09:11:18 +0100901- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
bellard5b0753e2005-03-01 21:37:28 +0000902{
thsc304f7e2008-01-22 23:25:15 +0000903 COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n");
ths3b46e622007-09-17 08:09:54 +0000904
Peter Maydell86179892015-05-19 09:11:18 +0100905 /* The NSFileHandlingPanelOKButton/NSFileHandlingPanelCancelButton values for
906 * returnCode strictly only apply for the 10.6-and-up beginSheetModalForWindow
907 * API. For the legacy pre-10.6 beginSheetForDirectory API they are NSOKButton
908 * and NSCancelButton. However conveniently the values are the same.
909 * We use the non-legacy names because the others are deprecated in OSX 10.10.
910 */
911 if (returnCode == NSFileHandlingPanelCancelButton) {
thsc304f7e2008-01-22 23:25:15 +0000912 exit(0);
Peter Maydell86179892015-05-19 09:11:18 +0100913 } else if (returnCode == NSFileHandlingPanelOKButton) {
Peter Maydell8bb3f1e2013-04-22 10:29:48 +0000914 char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding];
ths3b46e622007-09-17 08:09:54 +0000915
Peter Maydell98db4292013-12-08 22:59:06 +0000916 char **argv = g_new(char *, 4);
ths3b46e622007-09-17 08:09:54 +0000917
Andreas Färber13766eb2011-10-30 23:19:54 +0100918 [sheet close];
919
Peter Maydell98db4292013-12-08 22:59:06 +0000920 argv[0] = g_strdup(gArgv[0]);
921 argv[1] = g_strdup("-hda");
922 argv[2] = g_strdup(img);
923 argv[3] = NULL;
ths3b46e622007-09-17 08:09:54 +0000924
Peter Maydell98db4292013-12-08 22:59:06 +0000925 // printf("Using argc %d argv %s -hda %s\n", 3, gArgv[0], img);
ths3b46e622007-09-17 08:09:54 +0000926
bellard5b0753e2005-03-01 21:37:28 +0000927 [self startEmulationWithArgc:3 argv:(char**)argv];
928 }
929}
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100930
931/* We abstract the method called by the Enter Fullscreen menu item
932 * because Mac OS 10.7 and higher disables it. This is because of the
933 * menu item's old selector's name toggleFullScreen:
934 */
935- (void) doToggleFullScreen:(id)sender
936{
937 [self toggleFullScreen:(id)sender];
938}
939
thsc304f7e2008-01-22 23:25:15 +0000940- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +0000941{
thsc304f7e2008-01-22 23:25:15 +0000942 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
943
944 [cocoaView toggleFullScreen:sender];
945}
946
947- (void)showQEMUDoc:(id)sender
948{
949 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
950
951 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
952 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
953}
954
955- (void)showQEMUTec:(id)sender
956{
957 COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
958
959 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
960 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
bellard5b0753e2005-03-01 21:37:28 +0000961}
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100962
963/* Stretches video to fit host monitor size */
964- (void)zoomToFit:(id) sender
965{
966 stretch_video = !stretch_video;
967 if (stretch_video == true) {
968 [sender setState: NSOnState];
969 } else {
970 [sender setState: NSOffState];
971 }
972}
bellard5b0753e2005-03-01 21:37:28 +0000973@end
974
bellard5b0753e2005-03-01 21:37:28 +0000975
thsc304f7e2008-01-22 23:25:15 +0000976
thsc304f7e2008-01-22 23:25:15 +0000977int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +0000978
thsc304f7e2008-01-22 23:25:15 +0000979 gArgc = argc;
980 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +0100981 int i;
982
983 /* In case we don't need to display a window, let's not do that */
984 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100985 const char *opt = argv[i];
986
987 if (opt[0] == '-') {
988 /* Treat --foo the same as -foo. */
989 if (opt[1] == '-') {
990 opt++;
991 }
Alexandre Raymond98514842011-05-29 18:22:49 -0400992 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
993 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100994 !strcmp(opt, "-nographic") ||
995 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +0200996 !strcmp(opt, "-curses") ||
997 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +0200998 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100999 }
Andreas Färberf4918802009-12-13 02:11:44 +01001000 }
1001 }
ths3b46e622007-09-17 08:09:54 +00001002
thsc304f7e2008-01-22 23:25:15 +00001003 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +00001004
Peter Maydell42a5dfe2013-04-22 10:29:47 +00001005 // Pull this console process up to being a fully-fledged graphical
1006 // app with a menubar and Dock icon
1007 ProcessSerialNumber psn = { 0, kCurrentProcess };
1008 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1009
1010 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +00001011
thsc304f7e2008-01-22 23:25:15 +00001012 // Add menus
1013 NSMenu *menu;
1014 NSMenuItem *menuItem;
1015
bellard5b0753e2005-03-01 21:37:28 +00001016 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +00001017
thsc304f7e2008-01-22 23:25:15 +00001018 // Application menu
1019 menu = [[NSMenu alloc] initWithTitle:@""];
1020 [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
1021 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1022 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1023 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
1024 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
1025 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1026 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1027 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1028 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1029 [menuItem setSubmenu:menu];
1030 [[NSApp mainMenu] addItem:menuItem];
1031 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001032
thsc304f7e2008-01-22 23:25:15 +00001033 // View menu
1034 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001035 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1036 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001037 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1038 [menuItem setSubmenu:menu];
1039 [[NSApp mainMenu] addItem:menuItem];
1040
1041 // Window menu
1042 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1043 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1044 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1045 [menuItem setSubmenu:menu];
1046 [[NSApp mainMenu] addItem:menuItem];
1047 [NSApp setWindowsMenu:menu];
1048
1049 // Help menu
1050 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1051 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
1052 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
1053 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1054 [menuItem setSubmenu:menu];
1055 [[NSApp mainMenu] addItem:menuItem];
1056
1057 // Create an Application controller
1058 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1059 [NSApp setDelegate:appController];
1060
1061 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +00001062 [NSApp run];
ths3b46e622007-09-17 08:09:54 +00001063
thsc304f7e2008-01-22 23:25:15 +00001064 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001065 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001066
bellard5b0753e2005-03-01 21:37:28 +00001067 return 0;
1068}
thsc304f7e2008-01-22 23:25:15 +00001069
1070
1071
1072#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001073static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001074 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001075{
Peter Maydell6e657e62013-04-22 10:29:46 +00001076 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1077
thsc304f7e2008-01-22 23:25:15 +00001078 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1079
1080 NSRect rect;
1081 if ([cocoaView cdx] == 1.0) {
1082 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1083 } else {
1084 rect = NSMakeRect(
1085 x * [cocoaView cdx],
1086 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1087 w * [cocoaView cdx],
1088 h * [cocoaView cdy]);
1089 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001090 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001091
1092 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001093}
1094
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001095static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001096 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001097{
Peter Maydell6e657e62013-04-22 10:29:46 +00001098 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001099
Peter Maydell6e657e62013-04-22 10:29:46 +00001100 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001101 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001102 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001103}
1104
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001105static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001106{
Peter Maydell6e657e62013-04-22 10:29:46 +00001107 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1108
thsc304f7e2008-01-22 23:25:15 +00001109 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
1110
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001111 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001112 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001113 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001114 [cocoaView ungrabMouse];
1115 }
1116 }
1117 [cocoaView setAbsoluteEnabled:YES];
1118 }
1119
1120 NSDate *distantPast;
1121 NSEvent *event;
1122 distantPast = [NSDate distantPast];
1123 do {
1124 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
1125 inMode: NSDefaultRunLoopMode dequeue:YES];
1126 if (event != nil) {
1127 [cocoaView handleEvent:event];
1128 }
1129 } while(event != nil);
Peter Maydell68c0aa62013-04-17 09:16:35 +00001130 graphic_hw_update(NULL);
Peter Maydell6e657e62013-04-22 10:29:46 +00001131 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001132}
1133
1134static void cocoa_cleanup(void)
1135{
1136 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001137 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001138}
1139
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001140static const DisplayChangeListenerOps dcl_ops = {
1141 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001142 .dpy_gfx_update = cocoa_update,
1143 .dpy_gfx_switch = cocoa_switch,
1144 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001145};
1146
thsc304f7e2008-01-22 23:25:15 +00001147void cocoa_display_init(DisplayState *ds, int full_screen)
1148{
1149 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1150
Programmingkid43227af2015-05-19 09:11:17 +01001151 /* if fullscreen mode is to be used */
1152 if (full_screen == true) {
1153 [NSApp activateIgnoringOtherApps: YES];
1154 [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
1155 }
1156
Blue Swirl58a06672011-08-21 18:42:08 +00001157 dcl = g_malloc0(sizeof(DisplayChangeListener));
1158
aliguori9794f742009-03-04 19:25:22 +00001159 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001160 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001161 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001162
1163 // register cleanup function
1164 atexit(cocoa_cleanup);
1165}