blob: 7c64e1a5aaf121d91d15e40fb7a89cc245aee592 [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"
John Arbuckle8524f1c2015-06-19 10:53:27 +010032#include "qmp-commands.h"
John Arbuckle693a3e02015-06-19 10:53:27 +010033#include "sysemu/blockdev.h"
bellardda4dbf72005-03-02 22:22:43 +000034
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
Peter Maydell81801ae2015-05-19 09:11:18 +010041#ifndef MAC_OS_X_VERSION_10_10
42#define MAC_OS_X_VERSION_10_10 101000
43#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010044
bellard5b0753e2005-03-01 21:37:28 +000045
thsc304f7e2008-01-22 23:25:15 +000046//#define DEBUG
47
48#ifdef DEBUG
49#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
50#else
51#define COCOA_DEBUG(...) ((void) 0)
52#endif
53
54#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000055
56typedef struct {
57 int width;
58 int height;
59 int bitsPerComponent;
60 int bitsPerPixel;
61} QEMUScreen;
62
thsc304f7e2008-01-22 23:25:15 +000063NSWindow *normalWindow;
aliguori9794f742009-03-04 19:25:22 +000064static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +010065static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +000066
67int gArgc;
68char **gArgv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +010069bool stretch_video;
John Arbuckle8524f1c2015-06-19 10:53:27 +010070NSTextField *pauseLabel;
John Arbuckle693a3e02015-06-19 10:53:27 +010071NSArray * supportedImageFileTypes;
bellard5b0753e2005-03-01 21:37:28 +000072
thsc304f7e2008-01-22 23:25:15 +000073// keymap conversion
bellard87f48e62005-10-30 18:24:49 +000074int keymap[] =
bellard5b0753e2005-03-01 21:37:28 +000075{
bellard87f48e62005-10-30 18:24:49 +000076// SdlI macI macH SdlH 104xtH 104xtC sdl
77 30, // 0 0x00 0x1e A QZ_a
78 31, // 1 0x01 0x1f S QZ_s
79 32, // 2 0x02 0x20 D QZ_d
80 33, // 3 0x03 0x21 F QZ_f
81 35, // 4 0x04 0x23 H QZ_h
82 34, // 5 0x05 0x22 G QZ_g
83 44, // 6 0x06 0x2c Z QZ_z
84 45, // 7 0x07 0x2d X QZ_x
85 46, // 8 0x08 0x2e C QZ_c
86 47, // 9 0x09 0x2f V QZ_v
87 0, // 10 0x0A Undefined
88 48, // 11 0x0B 0x30 B QZ_b
89 16, // 12 0x0C 0x10 Q QZ_q
90 17, // 13 0x0D 0x11 W QZ_w
91 18, // 14 0x0E 0x12 E QZ_e
92 19, // 15 0x0F 0x13 R QZ_r
93 21, // 16 0x10 0x15 Y QZ_y
94 20, // 17 0x11 0x14 T QZ_t
95 2, // 18 0x12 0x02 1 QZ_1
96 3, // 19 0x13 0x03 2 QZ_2
97 4, // 20 0x14 0x04 3 QZ_3
98 5, // 21 0x15 0x05 4 QZ_4
99 7, // 22 0x16 0x07 6 QZ_6
100 6, // 23 0x17 0x06 5 QZ_5
101 13, // 24 0x18 0x0d = QZ_EQUALS
102 10, // 25 0x19 0x0a 9 QZ_9
103 8, // 26 0x1A 0x08 7 QZ_7
104 12, // 27 0x1B 0x0c - QZ_MINUS
105 9, // 28 0x1C 0x09 8 QZ_8
106 11, // 29 0x1D 0x0b 0 QZ_0
107 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET
108 24, // 31 0x1F 0x18 O QZ_o
109 22, // 32 0x20 0x16 U QZ_u
110 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET
111 23, // 34 0x22 0x17 I QZ_i
112 25, // 35 0x23 0x19 P QZ_p
113 28, // 36 0x24 0x1c ENTER QZ_RETURN
114 38, // 37 0x25 0x26 L QZ_l
115 36, // 38 0x26 0x24 J QZ_j
116 40, // 39 0x27 0x28 ' QZ_QUOTE
117 37, // 40 0x28 0x25 K QZ_k
118 39, // 41 0x29 0x27 ; QZ_SEMICOLON
119 43, // 42 0x2A 0x2b \ QZ_BACKSLASH
120 51, // 43 0x2B 0x33 , QZ_COMMA
121 53, // 44 0x2C 0x35 / QZ_SLASH
122 49, // 45 0x2D 0x31 N QZ_n
123 50, // 46 0x2E 0x32 M QZ_m
124 52, // 47 0x2F 0x34 . QZ_PERIOD
125 15, // 48 0x30 0x0f TAB QZ_TAB
126 57, // 49 0x31 0x39 SPACE QZ_SPACE
127 41, // 50 0x32 0x29 ` QZ_BACKQUOTE
128 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE
129 0, // 52 0x34 Undefined
130 1, // 53 0x35 0x01 ESC QZ_ESCAPE
Peter Maydell88959192013-12-08 22:59:02 +0000131 220, // 54 0x36 0xdc E0,5C R GUI QZ_RMETA
132 219, // 55 0x37 0xdb E0,5B L GUI QZ_LMETA
bellard87f48e62005-10-30 18:24:49 +0000133 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT
134 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK
135 56, // 58 0x3A 0x38 L ALT QZ_LALT
136 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL
137 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT
138 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT
139 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL
140 0, // 63 0x3F Undefined
141 0, // 64 0x40 Undefined
142 0, // 65 0x41 Undefined
143 0, // 66 0x42 Undefined
144 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY
145 0, // 68 0x44 Undefined
146 78, // 69 0x45 0x4e KP + QZ_KP_PLUS
147 0, // 70 0x46 Undefined
148 69, // 71 0x47 0x45 NUM QZ_NUMLOCK
149 0, // 72 0x48 Undefined
150 0, // 73 0x49 Undefined
151 0, // 74 0x4A Undefined
152 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE
153 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER
154 0, // 77 0x4D undefined
155 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS
156 0, // 79 0x4F Undefined
157 0, // 80 0x50 Undefined
158 0, // 81 0x51 QZ_KP_EQUALS
159 82, // 82 0x52 0x52 KP 0 QZ_KP0
160 79, // 83 0x53 0x4f KP 1 QZ_KP1
161 80, // 84 0x54 0x50 KP 2 QZ_KP2
162 81, // 85 0x55 0x51 KP 3 QZ_KP3
163 75, // 86 0x56 0x4b KP 4 QZ_KP4
164 76, // 87 0x57 0x4c KP 5 QZ_KP5
165 77, // 88 0x58 0x4d KP 6 QZ_KP6
166 71, // 89 0x59 0x47 KP 7 QZ_KP7
167 0, // 90 0x5A Undefined
168 72, // 91 0x5B 0x48 KP 8 QZ_KP8
169 73, // 92 0x5C 0x49 KP 9 QZ_KP9
170 0, // 93 0x5D Undefined
171 0, // 94 0x5E Undefined
172 0, // 95 0x5F Undefined
173 63, // 96 0x60 0x3f F5 QZ_F5
174 64, // 97 0x61 0x40 F6 QZ_F6
175 65, // 98 0x62 0x41 F7 QZ_F7
176 61, // 99 0x63 0x3d F3 QZ_F3
177 66, // 100 0x64 0x42 F8 QZ_F8
178 67, // 101 0x65 0x43 F9 QZ_F9
179 0, // 102 0x66 Undefined
180 87, // 103 0x67 0x57 F11 QZ_F11
181 0, // 104 0x68 Undefined
thsc304f7e2008-01-22 23:25:15 +0000182 183,// 105 0x69 0xb7 QZ_PRINT
bellard87f48e62005-10-30 18:24:49 +0000183 0, // 106 0x6A Undefined
184 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK
185 0, // 108 0x6C Undefined
186 68, // 109 0x6D 0x44 F10 QZ_F10
187 0, // 110 0x6E Undefined
188 88, // 111 0x6F 0x58 F12 QZ_F12
189 0, // 112 0x70 Undefined
190 110,// 113 0x71 0x0 QZ_PAUSE
191 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT
192 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME
193 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP
194 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE
195 62, // 118 0x76 0x3e F4 QZ_F4
196 207,// 119 0x77 0xcf E0,4f END QZ_END
197 60, // 120 0x78 0x3c F2 QZ_F2
198 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN
199 59, // 122 0x7A 0x3b F1 QZ_F1
200 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT
201 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT
202 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN
203 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP
204/* 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 +0000205
Peter Maydell49b9bd42013-12-08 22:59:03 +0000206/* Additional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */
bellard87f48e62005-10-30 18:24:49 +0000207/*
ths3b46e622007-09-17 08:09:54 +0000208 221 // 0xdd e0,5d APPS
209 // E0,2A,E0,37 PRNT SCRN
210 // E1,1D,45,E1,9D,C5 PAUSE
211 83 // 0x53 0x53 KP .
212// ACPI Scan Codes
213 222 // 0xde E0, 5E Power
214 223 // 0xdf E0, 5F Sleep
215 227 // 0xe3 E0, 63 Wake
216// Windows Multimedia Scan Codes
217 153 // 0x99 E0, 19 Next Track
218 144 // 0x90 E0, 10 Previous Track
219 164 // 0xa4 E0, 24 Stop
220 162 // 0xa2 E0, 22 Play/Pause
221 160 // 0xa0 E0, 20 Mute
222 176 // 0xb0 E0, 30 Volume Up
ths5fafdf22007-09-16 21:08:06 +0000223 174 // 0xae E0, 2E Volume Down
ths3b46e622007-09-17 08:09:54 +0000224 237 // 0xed E0, 6D Media Select
225 236 // 0xec E0, 6C E-Mail
226 161 // 0xa1 E0, 21 Calculator
ths5fafdf22007-09-16 21:08:06 +0000227 235 // 0xeb E0, 6B My Computer
ths3b46e622007-09-17 08:09:54 +0000228 229 // 0xe5 E0, 65 WWW Search
229 178 // 0xb2 E0, 32 WWW Home
230 234 // 0xea E0, 6A WWW Back
ths5fafdf22007-09-16 21:08:06 +0000231 233 // 0xe9 E0, 69 WWW Forward
ths3b46e622007-09-17 08:09:54 +0000232 232 // 0xe8 E0, 68 WWW Stop
ths5fafdf22007-09-16 21:08:06 +0000233 231 // 0xe7 E0, 67 WWW Refresh
ths3b46e622007-09-17 08:09:54 +0000234 230 // 0xe6 E0, 66 WWW Favorites
bellard87f48e62005-10-30 18:24:49 +0000235*/
bellard5b0753e2005-03-01 21:37:28 +0000236};
237
Andreas Färber77047bb2009-12-13 00:55:53 +0100238static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000239{
Stefan Weil5d701922013-12-07 14:48:05 +0100240 if (ARRAY_SIZE(keymap) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000241 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000242 return 0;
243 }
244 return keymap[keycode];
245}
246
John Arbuckle693a3e02015-06-19 10:53:27 +0100247/* Displays an alert dialog box with the specified message */
248static void QEMU_Alert(NSString *message)
249{
250 NSAlert *alert;
251 alert = [NSAlert new];
252 [alert setMessageText: message];
253 [alert runModal];
254}
thsc304f7e2008-01-22 23:25:15 +0000255
John Arbuckle693a3e02015-06-19 10:53:27 +0100256/* Handles any errors that happen with a device transaction */
257static void handleAnyDeviceErrors(Error * err)
258{
259 if (err) {
260 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
261 encoding: NSASCIIStringEncoding]);
262 error_free(err);
263 }
264}
thsc304f7e2008-01-22 23:25:15 +0000265
bellard5b0753e2005-03-01 21:37:28 +0000266/*
267 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000268 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000269 ------------------------------------------------------
270*/
thsc304f7e2008-01-22 23:25:15 +0000271@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000272{
thsc304f7e2008-01-22 23:25:15 +0000273 QEMUScreen screen;
274 NSWindow *fullScreenWindow;
275 float cx,cy,cw,ch,cdx,cdy;
276 CGDataProviderRef dataProviderRef;
277 int modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000278 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000279 BOOL isFullscreen;
280 BOOL isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100281 BOOL isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000282}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100283- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000284- (void) grabMouse;
285- (void) ungrabMouse;
286- (void) toggleFullScreen:(id)sender;
287- (void) handleEvent:(NSEvent *)event;
288- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100289/* The state surrounding mouse grabbing is potentially confusing.
290 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
291 * pointing device an absolute-position one?"], but is only updated on
292 * next refresh.
293 * isMouseGrabbed tracks whether GUI events are directed to the guest;
294 * it controls whether special keys like Cmd get sent to the guest,
295 * and whether we capture the mouse when in non-absolute mode.
296 * isMouseDeassociated tracks whether we've told MacOSX to disassociate
297 * the mouse and mouse cursor position by calling
298 * CGAssociateMouseAndMouseCursorPosition(FALSE)
299 * (which basically happens if we grab in non-absolute mode).
300 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000301- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000302- (BOOL) isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100303- (BOOL) isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000304- (float) cdx;
305- (float) cdy;
306- (QEMUScreen) gscreen;
307@end
ths3b46e622007-09-17 08:09:54 +0000308
Andreas Färber7fee1992011-06-09 20:53:32 +0200309QemuCocoaView *cocoaView;
310
thsc304f7e2008-01-22 23:25:15 +0000311@implementation QemuCocoaView
312- (id)initWithFrame:(NSRect)frameRect
313{
314 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000315
thsc304f7e2008-01-22 23:25:15 +0000316 self = [super initWithFrame:frameRect];
317 if (self) {
pbrook95219892006-04-09 01:06:34 +0000318
thsc304f7e2008-01-22 23:25:15 +0000319 screen.bitsPerComponent = 8;
320 screen.bitsPerPixel = 32;
321 screen.width = frameRect.size.width;
322 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000323
thsc304f7e2008-01-22 23:25:15 +0000324 }
325 return self;
326}
bellard7c206a72005-12-18 19:18:45 +0000327
thsc304f7e2008-01-22 23:25:15 +0000328- (void) dealloc
329{
330 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000331
thsc304f7e2008-01-22 23:25:15 +0000332 if (dataProviderRef)
333 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000334
thsc304f7e2008-01-22 23:25:15 +0000335 [super dealloc];
336}
bellard5cbfcd02006-06-14 15:53:24 +0000337
Andreas Färberd50f71d2009-12-13 02:03:33 +0100338- (BOOL) isOpaque
339{
340 return YES;
341}
342
Peter Maydell5dd45be2014-06-23 10:35:23 +0100343- (BOOL) screenContainsPoint:(NSPoint) p
344{
345 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
346}
347
Peter Maydell13aefd32014-06-23 10:35:25 +0100348- (void) hideCursor
349{
350 if (!cursor_hide) {
351 return;
352 }
353 [NSCursor hide];
354}
355
356- (void) unhideCursor
357{
358 if (!cursor_hide) {
359 return;
360 }
361 [NSCursor unhide];
362}
363
thsc304f7e2008-01-22 23:25:15 +0000364- (void) drawRect:(NSRect) rect
365{
366 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000367
thsc304f7e2008-01-22 23:25:15 +0000368 // get CoreGraphic context
369 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
370 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
371 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000372
thsc304f7e2008-01-22 23:25:15 +0000373 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000374 if (!dataProviderRef) {
375 // Draw request before any guest device has set up a framebuffer:
376 // just draw an opaque black rectangle
377 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
378 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
379 } else {
thsc304f7e2008-01-22 23:25:15 +0000380 CGImageRef imageRef = CGImageCreate(
381 screen.width, //width
382 screen.height, //height
383 screen.bitsPerComponent, //bitsPerComponent
384 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000385 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100386#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000387 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000388 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000389#else
390 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
391 kCGImageAlphaNoneSkipFirst, //bitmapInfo
392#endif
393 dataProviderRef, //provider
394 NULL, //decode
395 0, //interpolate
396 kCGRenderingIntentDefault //intent
397 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100398 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
399 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100400 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100401 int i;
402 CGImageRef clipImageRef;
403 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000404
Peter Maydellb63901d2015-05-19 09:11:17 +0100405 [self getRectsBeingDrawn:&rectList count:&rectCount];
406 for (i = 0; i < rectCount; i++) {
407 clipRect.origin.x = rectList[i].origin.x / cdx;
408 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
409 clipRect.size.width = rectList[i].size.width / cdx;
410 clipRect.size.height = rectList[i].size.height / cdy;
411 clipImageRef = CGImageCreateWithImageInRect(
412 imageRef,
413 clipRect
414 );
415 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
416 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000417 }
thsc304f7e2008-01-22 23:25:15 +0000418 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000419 }
420}
421
thsc304f7e2008-01-22 23:25:15 +0000422- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000423{
thsc304f7e2008-01-22 23:25:15 +0000424 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000425
thsc304f7e2008-01-22 23:25:15 +0000426 if (isFullscreen) {
427 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
428 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100429
430 /* stretches video, but keeps same aspect ratio */
431 if (stretch_video == true) {
432 /* use smallest stretch value - prevents clipping on sides */
433 if (MIN(cdx, cdy) == cdx) {
434 cdy = cdx;
435 } else {
436 cdx = cdy;
437 }
438 } else { /* No stretching */
439 cdx = cdy = 1;
440 }
thsc304f7e2008-01-22 23:25:15 +0000441 cw = screen.width * cdx;
442 ch = screen.height * cdy;
443 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
444 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
445 } else {
446 cx = 0;
447 cy = 0;
448 cw = screen.width;
449 ch = screen.height;
450 cdx = 1.0;
451 cdy = 1.0;
452 }
bellard5b0753e2005-03-01 21:37:28 +0000453}
454
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100455- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000456{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100457 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000458
Peter Maydell8510d912013-03-18 20:28:21 +0000459 int w = surface_width(surface);
460 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100461 /* cdx == 0 means this is our very first surface, in which case we need
462 * to recalculate the content dimensions even if it happens to be the size
463 * of the initial empty window.
464 */
465 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000466
467 int oldh = screen.height;
468 if (isResize) {
469 // Resize before we trigger the redraw, or we'll redraw at the wrong size
470 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
471 screen.width = w;
472 screen.height = h;
473 [self setContentDimensions];
474 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
475 }
Peter Maydell8510d912013-03-18 20:28:21 +0000476
thsc304f7e2008-01-22 23:25:15 +0000477 // update screenBuffer
478 if (dataProviderRef)
479 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000480
aliguori9794f742009-03-04 19:25:22 +0000481 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000482 screen.bitsPerPixel = surface_bits_per_pixel(surface);
483 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000484
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100485 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000486
487 // update windows
488 if (isFullscreen) {
489 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000490 [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 +0000491 } else {
492 if (qemu_name)
493 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000494 [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 +0000495 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000496
497 if (isResize) {
498 [normalWindow center];
499 }
thsc304f7e2008-01-22 23:25:15 +0000500}
501
502- (void) toggleFullScreen:(id)sender
503{
504 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
505
506 if (isFullscreen) { // switch from fullscreen to desktop
507 isFullscreen = FALSE;
508 [self ungrabMouse];
509 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000510 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
511 [self exitFullScreenModeWithOptions:nil];
512 } else {
thsc304f7e2008-01-22 23:25:15 +0000513 [fullScreenWindow close];
514 [normalWindow setContentView: self];
515 [normalWindow makeKeyAndOrderFront: self];
516 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000517 }
thsc304f7e2008-01-22 23:25:15 +0000518 } else { // switch from desktop to fullscreen
519 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100520 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000521 [self grabMouse];
522 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000523 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
524 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
525 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
526 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
527 nil]];
528 } else {
thsc304f7e2008-01-22 23:25:15 +0000529 [NSMenu setMenuBarVisible:NO];
530 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
531 styleMask:NSBorderlessWindowMask
532 backing:NSBackingStoreBuffered
533 defer:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100534 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
thsc304f7e2008-01-22 23:25:15 +0000535 [fullScreenWindow setHasShadow:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100536 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
537 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
538 [[fullScreenWindow contentView] addSubview: self];
thsc304f7e2008-01-22 23:25:15 +0000539 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000540 }
thsc304f7e2008-01-22 23:25:15 +0000541 }
542}
543
544- (void) handleEvent:(NSEvent *)event
545{
546 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
547
548 int buttons = 0;
549 int keycode;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100550 bool mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000551 NSPoint p = [event locationInWindow];
552
553 switch ([event type]) {
554 case NSFlagsChanged:
555 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000556
Peter Maydell49b9bd42013-12-08 22:59:03 +0000557 if ((keycode == 219 || keycode == 220) && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000558 /* Don't pass command key changes to guest unless mouse is grabbed */
559 keycode = 0;
560 }
561
thsc304f7e2008-01-22 23:25:15 +0000562 if (keycode) {
563 if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100564 qemu_input_event_send_key_number(dcl->con, keycode, true);
565 qemu_input_event_send_key_number(dcl->con, keycode, false);
Peter Maydell68c0aa62013-04-17 09:16:35 +0000566 } else if (qemu_console_is_graphic(NULL)) {
thsc304f7e2008-01-22 23:25:15 +0000567 if (modifiers_state[keycode] == 0) { // keydown
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100568 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000569 modifiers_state[keycode] = 1;
570 } else { // keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100571 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000572 modifiers_state[keycode] = 0;
573 }
574 }
575 }
576
577 // release Mouse grab when pressing ctrl+alt
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100578 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
thsc304f7e2008-01-22 23:25:15 +0000579 [self ungrabMouse];
580 }
581 break;
582 case NSKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000583 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000584
Peter Maydell88959192013-12-08 22:59:02 +0000585 // forward command key combos to the host UI unless the mouse is grabbed
Peter Maydell49b9bd42013-12-08 22:59:03 +0000586 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
thsc304f7e2008-01-22 23:25:15 +0000587 [NSApp sendEvent:event];
588 return;
589 }
590
591 // default
thsc304f7e2008-01-22 23:25:15 +0000592
593 // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
594 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
595 switch (keycode) {
596
597 // enable graphic console
598 case 0x02 ... 0x0a: // '1' to '9' keys
599 console_select(keycode - 0x02);
600 break;
601 }
602
603 // handle keys for graphic console
Peter Maydell68c0aa62013-04-17 09:16:35 +0000604 } else if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100605 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000606
607 // handlekeys for Monitor
608 } else {
609 int keysym = 0;
610 switch([event keyCode]) {
611 case 115:
612 keysym = QEMU_KEY_HOME;
613 break;
614 case 117:
615 keysym = QEMU_KEY_DELETE;
616 break;
617 case 119:
618 keysym = QEMU_KEY_END;
619 break;
620 case 123:
621 keysym = QEMU_KEY_LEFT;
622 break;
623 case 124:
624 keysym = QEMU_KEY_RIGHT;
625 break;
626 case 125:
627 keysym = QEMU_KEY_DOWN;
628 break;
629 case 126:
630 keysym = QEMU_KEY_UP;
631 break;
632 default:
633 {
634 NSString *ks = [event characters];
635 if ([ks length] > 0)
636 keysym = [ks characterAtIndex:0];
637 }
638 }
639 if (keysym)
640 kbd_put_keysym(keysym);
641 }
642 break;
643 case NSKeyUp:
644 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000645
646 // don't pass the guest a spurious key-up if we treated this
647 // command-key combo as a host UI action
Peter Maydell49b9bd42013-12-08 22:59:03 +0000648 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
Peter Maydell88959192013-12-08 22:59:02 +0000649 return;
650 }
651
Peter Maydell68c0aa62013-04-17 09:16:35 +0000652 if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100653 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000654 }
655 break;
656 case NSMouseMoved:
657 if (isAbsoluteEnabled) {
Peter Maydell5dd45be2014-06-23 10:35:23 +0100658 if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
Peter Maydellf61c3872014-06-23 10:35:24 +0100659 if (isMouseGrabbed) {
660 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +0000661 }
662 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +0100663 if (!isMouseGrabbed) {
664 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000665 }
666 }
667 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100668 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000669 break;
670 case NSLeftMouseDown:
671 if ([event modifierFlags] & NSCommandKeyMask) {
672 buttons |= MOUSE_EVENT_RBUTTON;
673 } else {
674 buttons |= MOUSE_EVENT_LBUTTON;
675 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100676 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000677 break;
678 case NSRightMouseDown:
679 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100680 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000681 break;
682 case NSOtherMouseDown:
683 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100684 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000685 break;
686 case NSLeftMouseDragged:
687 if ([event modifierFlags] & NSCommandKeyMask) {
688 buttons |= MOUSE_EVENT_RBUTTON;
689 } else {
690 buttons |= MOUSE_EVENT_LBUTTON;
691 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100692 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000693 break;
694 case NSRightMouseDragged:
695 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100696 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000697 break;
698 case NSOtherMouseDragged:
699 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100700 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000701 break;
702 case NSLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +0100703 mouse_event = true;
704 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
705 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000706 }
707 break;
708 case NSRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100709 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000710 break;
711 case NSOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100712 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000713 break;
714 case NSScrollWheel:
Peter Maydellf61c3872014-06-23 10:35:24 +0100715 if (isMouseGrabbed) {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100716 buttons |= ([event deltaY] < 0) ?
717 MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
thsc304f7e2008-01-22 23:25:15 +0000718 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100719 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000720 break;
721 default:
722 [NSApp sendEvent:event];
723 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100724
725 if (mouse_event) {
726 if (last_buttons != buttons) {
727 static uint32_t bmap[INPUT_BUTTON_MAX] = {
728 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
729 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
730 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
731 [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP,
732 [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
733 };
734 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
735 last_buttons = buttons;
736 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100737 if (isMouseGrabbed) {
738 if (isAbsoluteEnabled) {
739 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
740 * The check on screenContainsPoint is to avoid sending out of range values for
741 * clicks in the titlebar.
742 */
743 if ([self screenContainsPoint:p]) {
744 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width);
745 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height);
746 }
747 } else {
748 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
749 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
750 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100751 } else {
752 [NSApp sendEvent:event];
753 }
754 qemu_input_event_sync();
755 }
thsc304f7e2008-01-22 23:25:15 +0000756}
757
758- (void) grabMouse
759{
760 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
761
762 if (!isFullscreen) {
763 if (qemu_name)
764 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
765 else
766 [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
767 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100768 [self hideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100769 if (!isAbsoluteEnabled) {
770 isMouseDeassociated = TRUE;
771 CGAssociateMouseAndMouseCursorPosition(FALSE);
772 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000773 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000774}
775
776- (void) ungrabMouse
777{
778 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
779
780 if (!isFullscreen) {
781 if (qemu_name)
782 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
783 else
784 [normalWindow setTitle:@"QEMU"];
785 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100786 [self unhideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100787 if (isMouseDeassociated) {
788 CGAssociateMouseAndMouseCursorPosition(TRUE);
789 isMouseDeassociated = FALSE;
790 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000791 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000792}
793
794- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000795- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000796- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
Peter Maydellf61c3872014-06-23 10:35:24 +0100797- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
thsc304f7e2008-01-22 23:25:15 +0000798- (float) cdx {return cdx;}
799- (float) cdy {return cdy;}
800- (QEMUScreen) gscreen {return screen;}
bellard5b0753e2005-03-01 21:37:28 +0000801@end
802
803
thsc304f7e2008-01-22 23:25:15 +0000804
bellard5b0753e2005-03-01 21:37:28 +0000805/*
806 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000807 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000808 ------------------------------------------------------
809*/
thsc304f7e2008-01-22 23:25:15 +0000810@interface QemuCocoaAppController : NSObject
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100811#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
John Arbuckled9bc14f2015-09-25 23:13:59 +0100812 <NSWindowDelegate, NSApplicationDelegate>
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100813#endif
bellard5b0753e2005-03-01 21:37:28 +0000814{
815}
bellard5b0753e2005-03-01 21:37:28 +0000816- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
Peter Maydellde1aade2015-05-19 09:11:18 +0100817- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100818- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +0000819- (void)toggleFullScreen:(id)sender;
820- (void)showQEMUDoc:(id)sender;
821- (void)showQEMUTec:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100822- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +0100823- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100824- (void)pauseQEMU:(id)sender;
825- (void)resumeQEMU:(id)sender;
826- (void)displayPause;
827- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +0100828- (void)restartQEMU:(id)sender;
829- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +0100830- (void)ejectDeviceMedia:(id)sender;
831- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +0100832- (BOOL)verifyQuit;
bellard5b0753e2005-03-01 21:37:28 +0000833@end
834
thsc304f7e2008-01-22 23:25:15 +0000835@implementation QemuCocoaAppController
836- (id) init
837{
838 COCOA_DEBUG("QemuCocoaAppController: init\n");
839
840 self = [super init];
841 if (self) {
842
843 // create a view and add it to the window
844 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
845 if(!cocoaView) {
846 fprintf(stderr, "(cocoa) can't create a view\n");
847 exit(1);
848 }
849
850 // create a window
851 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
852 styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
853 backing:NSBackingStoreBuffered defer:NO];
854 if(!normalWindow) {
855 fprintf(stderr, "(cocoa) can't create window\n");
856 exit(1);
857 }
858 [normalWindow setAcceptsMouseMovedEvents:YES];
859 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]];
860 [normalWindow setContentView:cocoaView];
Peter Maydell81801ae2015-05-19 09:11:18 +0100861#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
Andreas Färber561ef252009-12-13 03:06:20 +0100862 [normalWindow useOptimizedDrawing:YES];
Peter Maydell81801ae2015-05-19 09:11:18 +0100863#endif
thsc304f7e2008-01-22 23:25:15 +0000864 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +0000865 [normalWindow center];
John Arbuckled9bc14f2015-09-25 23:13:59 +0100866 [normalWindow setDelegate: self];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100867 stretch_video = false;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100868
869 /* Used for displaying pause on the screen */
870 pauseLabel = [NSTextField new];
871 [pauseLabel setBezeled:YES];
872 [pauseLabel setDrawsBackground:YES];
873 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
874 [pauseLabel setEditable:NO];
875 [pauseLabel setSelectable:NO];
876 [pauseLabel setStringValue: @"Paused"];
877 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
878 [pauseLabel setTextColor: [NSColor blackColor]];
879 [pauseLabel sizeToFit];
John Arbuckle693a3e02015-06-19 10:53:27 +0100880
881 // set the supported image file types that can be opened
882 supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg",
883 @"qcow", @"qcow2", @"cloop", @"vmdk", nil];
thsc304f7e2008-01-22 23:25:15 +0000884 }
885 return self;
886}
887
888- (void) dealloc
889{
890 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
891
892 if (cocoaView)
893 [cocoaView release];
894 [super dealloc];
895}
896
bellard5b0753e2005-03-01 21:37:28 +0000897- (void)applicationDidFinishLaunching: (NSNotification *) note
898{
thsc304f7e2008-01-22 23:25:15 +0000899 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
bellard5a246932005-04-07 20:35:06 +0000900
Peter Maydell49b9bd42013-12-08 22:59:03 +0000901 // Display an open dialog box if no arguments were passed or
thsc304f7e2008-01-22 23:25:15 +0000902 // if qemu was launched from the finder ( the Finder passes "-psn" )
903 if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) {
bellard5b0753e2005-03-01 21:37:28 +0000904 NSOpenPanel *op = [[NSOpenPanel alloc] init];
bellard5b0753e2005-03-01 21:37:28 +0000905 [op setPrompt:@"Boot image"];
bellard5b0753e2005-03-01 21:37:28 +0000906 [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000907#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
John Arbuckle693a3e02015-06-19 10:53:27 +0100908 [op setAllowedFileTypes:supportedImageFileTypes];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000909 [op beginSheetModalForWindow:normalWindow
910 completionHandler:^(NSInteger returnCode)
911 { [self openPanelDidEnd:op
912 returnCode:returnCode contextInfo:NULL ]; } ];
913#else
914 // Compatibility code for pre-10.6, using deprecated method
915 [op beginSheetForDirectory:nil file:nil types:filetypes
thsc304f7e2008-01-22 23:25:15 +0000916 modalForWindow:normalWindow modalDelegate:self
bellard5b0753e2005-03-01 21:37:28 +0000917 didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000918#endif
thsc304f7e2008-01-22 23:25:15 +0000919 } else {
Stefan Weil5cbdb3a2012-04-07 09:23:39 +0200920 // or launch QEMU, with the global args
thsc304f7e2008-01-22 23:25:15 +0000921 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5a246932005-04-07 20:35:06 +0000922 }
bellard5b0753e2005-03-01 21:37:28 +0000923}
924
925- (void)applicationWillTerminate:(NSNotification *)aNotification
926{
thsc304f7e2008-01-22 23:25:15 +0000927 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
928
bellard5b0753e2005-03-01 21:37:28 +0000929 qemu_system_shutdown_request();
bellard5b0753e2005-03-01 21:37:28 +0000930 exit(0);
931}
932
Andreas Färber41ea49b2009-12-14 22:13:27 +0100933- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
934{
935 return YES;
936}
937
John Arbuckled9bc14f2015-09-25 23:13:59 +0100938- (NSApplicationTerminateReply)applicationShouldTerminate:
939 (NSApplication *)sender
940{
941 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
942 return [self verifyQuit];
943}
944
945/* Called when the user clicks on a window's close button */
946- (BOOL)windowShouldClose:(id)sender
947{
948 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
949 [NSApp terminate: sender];
950 /* If the user allows the application to quit then the call to
951 * NSApp terminate will never return. If we get here then the user
952 * cancelled the quit, so we should return NO to not permit the
953 * closing of this window.
954 */
955 return NO;
956}
957
thsc304f7e2008-01-22 23:25:15 +0000958- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
959{
960 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
961
962 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +0200963 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +0000964 exit(status);
965}
966
Peter Maydellde1aade2015-05-19 09:11:18 +0100967- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
bellard5b0753e2005-03-01 21:37:28 +0000968{
thsc304f7e2008-01-22 23:25:15 +0000969 COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n");
ths3b46e622007-09-17 08:09:54 +0000970
Peter Maydell86179892015-05-19 09:11:18 +0100971 /* The NSFileHandlingPanelOKButton/NSFileHandlingPanelCancelButton values for
972 * returnCode strictly only apply for the 10.6-and-up beginSheetModalForWindow
973 * API. For the legacy pre-10.6 beginSheetForDirectory API they are NSOKButton
974 * and NSCancelButton. However conveniently the values are the same.
975 * We use the non-legacy names because the others are deprecated in OSX 10.10.
976 */
977 if (returnCode == NSFileHandlingPanelCancelButton) {
thsc304f7e2008-01-22 23:25:15 +0000978 exit(0);
Peter Maydell86179892015-05-19 09:11:18 +0100979 } else if (returnCode == NSFileHandlingPanelOKButton) {
Peter Maydell8bb3f1e2013-04-22 10:29:48 +0000980 char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding];
ths3b46e622007-09-17 08:09:54 +0000981
Peter Maydell98db4292013-12-08 22:59:06 +0000982 char **argv = g_new(char *, 4);
ths3b46e622007-09-17 08:09:54 +0000983
Andreas Färber13766eb2011-10-30 23:19:54 +0100984 [sheet close];
985
Peter Maydell98db4292013-12-08 22:59:06 +0000986 argv[0] = g_strdup(gArgv[0]);
987 argv[1] = g_strdup("-hda");
988 argv[2] = g_strdup(img);
989 argv[3] = NULL;
ths3b46e622007-09-17 08:09:54 +0000990
Peter Maydell98db4292013-12-08 22:59:06 +0000991 // printf("Using argc %d argv %s -hda %s\n", 3, gArgv[0], img);
ths3b46e622007-09-17 08:09:54 +0000992
bellard5b0753e2005-03-01 21:37:28 +0000993 [self startEmulationWithArgc:3 argv:(char**)argv];
994 }
995}
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100996
997/* We abstract the method called by the Enter Fullscreen menu item
998 * because Mac OS 10.7 and higher disables it. This is because of the
999 * menu item's old selector's name toggleFullScreen:
1000 */
1001- (void) doToggleFullScreen:(id)sender
1002{
1003 [self toggleFullScreen:(id)sender];
1004}
1005
thsc304f7e2008-01-22 23:25:15 +00001006- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +00001007{
thsc304f7e2008-01-22 23:25:15 +00001008 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
1009
1010 [cocoaView toggleFullScreen:sender];
1011}
1012
1013- (void)showQEMUDoc:(id)sender
1014{
1015 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1016
1017 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
1018 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
1019}
1020
1021- (void)showQEMUTec:(id)sender
1022{
1023 COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
1024
1025 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
1026 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
bellard5b0753e2005-03-01 21:37:28 +00001027}
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001028
1029/* Stretches video to fit host monitor size */
1030- (void)zoomToFit:(id) sender
1031{
1032 stretch_video = !stretch_video;
1033 if (stretch_video == true) {
1034 [sender setState: NSOnState];
1035 } else {
1036 [sender setState: NSOffState];
1037 }
1038}
bellard5b0753e2005-03-01 21:37:28 +00001039
Programmingkidb4c6a112015-05-19 09:11:18 +01001040/* Displays the console on the screen */
1041- (void)displayConsole:(id)sender
1042{
1043 console_select([sender tag]);
1044}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001045
1046/* Pause the guest */
1047- (void)pauseQEMU:(id)sender
1048{
1049 qmp_stop(NULL);
1050 [sender setEnabled: NO];
1051 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1052 [self displayPause];
1053}
1054
1055/* Resume running the guest operating system */
1056- (void)resumeQEMU:(id) sender
1057{
1058 qmp_cont(NULL);
1059 [sender setEnabled: NO];
1060 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1061 [self removePause];
1062}
1063
1064/* Displays the word pause on the screen */
1065- (void)displayPause
1066{
1067 /* Coordinates have to be calculated each time because the window can change its size */
1068 int xCoord, yCoord, width, height;
1069 xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
1070 yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
1071 width = [pauseLabel frame].size.width;
1072 height = [pauseLabel frame].size.height;
1073 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1074 [cocoaView addSubview: pauseLabel];
1075}
1076
1077/* Removes the word pause from the screen */
1078- (void)removePause
1079{
1080 [pauseLabel removeFromSuperview];
1081}
1082
John Arbuckle27074612015-06-19 10:53:27 +01001083/* Restarts QEMU */
1084- (void)restartQEMU:(id)sender
1085{
1086 qmp_system_reset(NULL);
1087}
1088
1089/* Powers down QEMU */
1090- (void)powerDownQEMU:(id)sender
1091{
1092 qmp_system_powerdown(NULL);
1093}
1094
John Arbuckle693a3e02015-06-19 10:53:27 +01001095/* Ejects the media.
1096 * Uses sender's tag to figure out the device to eject.
1097 */
1098- (void)ejectDeviceMedia:(id)sender
1099{
1100 NSString * drive;
1101 drive = [sender representedObject];
1102 if(drive == nil) {
1103 NSBeep();
1104 QEMU_Alert(@"Failed to find drive to eject!");
1105 return;
1106 }
1107
1108 Error *err = NULL;
1109 qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding], false, false, &err);
1110 handleAnyDeviceErrors(err);
1111}
1112
1113/* Displays a dialog box asking the user to select an image file to load.
1114 * Uses sender's represented object value to figure out which drive to use.
1115 */
1116- (void)changeDeviceMedia:(id)sender
1117{
1118 /* Find the drive name */
1119 NSString * drive;
1120 drive = [sender representedObject];
1121 if(drive == nil) {
1122 NSBeep();
1123 QEMU_Alert(@"Could not find drive!");
1124 return;
1125 }
1126
1127 /* Display the file open dialog */
1128 NSOpenPanel * openPanel;
1129 openPanel = [NSOpenPanel openPanel];
1130 [openPanel setCanChooseFiles: YES];
1131 [openPanel setAllowsMultipleSelection: NO];
1132 [openPanel setAllowedFileTypes: supportedImageFileTypes];
1133 if([openPanel runModal] == NSFileHandlingPanelOKButton) {
1134 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1135 if(file == nil) {
1136 NSBeep();
1137 QEMU_Alert(@"Failed to convert URL to file path!");
1138 return;
1139 }
1140
1141 Error *err = NULL;
1142 qmp_change_blockdev([drive cStringUsingEncoding: NSASCIIStringEncoding],
1143 [file cStringUsingEncoding: NSASCIIStringEncoding],
1144 "raw",
1145 &err);
1146 handleAnyDeviceErrors(err);
1147 }
1148}
1149
John Arbuckled9bc14f2015-09-25 23:13:59 +01001150/* Verifies if the user really wants to quit */
1151- (BOOL)verifyQuit
1152{
1153 NSAlert *alert = [NSAlert new];
1154 [alert autorelease];
1155 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1156 [alert addButtonWithTitle: @"Cancel"];
1157 [alert addButtonWithTitle: @"Quit"];
1158 if([alert runModal] == NSAlertSecondButtonReturn) {
1159 return YES;
1160 } else {
1161 return NO;
1162 }
1163}
1164
Programmingkidb4c6a112015-05-19 09:11:18 +01001165@end
bellard5b0753e2005-03-01 21:37:28 +00001166
thsc304f7e2008-01-22 23:25:15 +00001167
thsc304f7e2008-01-22 23:25:15 +00001168int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +00001169
thsc304f7e2008-01-22 23:25:15 +00001170 gArgc = argc;
1171 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +01001172 int i;
1173
1174 /* In case we don't need to display a window, let's not do that */
1175 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001176 const char *opt = argv[i];
1177
1178 if (opt[0] == '-') {
1179 /* Treat --foo the same as -foo. */
1180 if (opt[1] == '-') {
1181 opt++;
1182 }
Alexandre Raymond98514842011-05-29 18:22:49 -04001183 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
1184 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001185 !strcmp(opt, "-nographic") ||
1186 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001187 !strcmp(opt, "-curses") ||
Rainer Müllerb12a84c2015-09-09 16:08:30 +02001188 !strcmp(opt, "-display") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001189 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +02001190 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001191 }
Andreas Färberf4918802009-12-13 02:11:44 +01001192 }
1193 }
ths3b46e622007-09-17 08:09:54 +00001194
thsc304f7e2008-01-22 23:25:15 +00001195 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +00001196
Peter Maydell42a5dfe2013-04-22 10:29:47 +00001197 // Pull this console process up to being a fully-fledged graphical
1198 // app with a menubar and Dock icon
1199 ProcessSerialNumber psn = { 0, kCurrentProcess };
1200 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1201
1202 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +00001203
thsc304f7e2008-01-22 23:25:15 +00001204 // Add menus
1205 NSMenu *menu;
1206 NSMenuItem *menuItem;
1207
bellard5b0753e2005-03-01 21:37:28 +00001208 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +00001209
thsc304f7e2008-01-22 23:25:15 +00001210 // Application menu
1211 menu = [[NSMenu alloc] initWithTitle:@""];
1212 [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
1213 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1214 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1215 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
1216 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
1217 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1218 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1219 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1220 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1221 [menuItem setSubmenu:menu];
1222 [[NSApp mainMenu] addItem:menuItem];
1223 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001224
John Arbuckle8524f1c2015-06-19 10:53:27 +01001225 // Machine menu
1226 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1227 [menu setAutoenablesItems: NO];
1228 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1229 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1230 [menu addItem: menuItem];
1231 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001232 [menu addItem: [NSMenuItem separatorItem]];
1233 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1234 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001235 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1236 [menuItem setSubmenu:menu];
1237 [[NSApp mainMenu] addItem:menuItem];
1238
thsc304f7e2008-01-22 23:25:15 +00001239 // View menu
1240 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001241 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1242 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001243 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1244 [menuItem setSubmenu:menu];
1245 [[NSApp mainMenu] addItem:menuItem];
1246
1247 // Window menu
1248 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1249 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1250 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1251 [menuItem setSubmenu:menu];
1252 [[NSApp mainMenu] addItem:menuItem];
1253 [NSApp setWindowsMenu:menu];
1254
1255 // Help menu
1256 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1257 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
1258 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
1259 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1260 [menuItem setSubmenu:menu];
1261 [[NSApp mainMenu] addItem:menuItem];
1262
1263 // Create an Application controller
1264 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1265 [NSApp setDelegate:appController];
1266
1267 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +00001268 [NSApp run];
ths3b46e622007-09-17 08:09:54 +00001269
thsc304f7e2008-01-22 23:25:15 +00001270 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001271 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001272
bellard5b0753e2005-03-01 21:37:28 +00001273 return 0;
1274}
thsc304f7e2008-01-22 23:25:15 +00001275
1276
1277
1278#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001279static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001280 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001281{
Peter Maydell6e657e62013-04-22 10:29:46 +00001282 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1283
thsc304f7e2008-01-22 23:25:15 +00001284 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1285
1286 NSRect rect;
1287 if ([cocoaView cdx] == 1.0) {
1288 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1289 } else {
1290 rect = NSMakeRect(
1291 x * [cocoaView cdx],
1292 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1293 w * [cocoaView cdx],
1294 h * [cocoaView cdy]);
1295 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001296 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001297
1298 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001299}
1300
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001301static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001302 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001303{
Peter Maydell6e657e62013-04-22 10:29:46 +00001304 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001305
Peter Maydell6e657e62013-04-22 10:29:46 +00001306 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001307 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001308 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001309}
1310
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001311static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001312{
Peter Maydell6e657e62013-04-22 10:29:46 +00001313 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1314
thsc304f7e2008-01-22 23:25:15 +00001315 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
1316
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001317 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001318 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001319 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001320 [cocoaView ungrabMouse];
1321 }
1322 }
1323 [cocoaView setAbsoluteEnabled:YES];
1324 }
1325
1326 NSDate *distantPast;
1327 NSEvent *event;
1328 distantPast = [NSDate distantPast];
1329 do {
1330 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
1331 inMode: NSDefaultRunLoopMode dequeue:YES];
1332 if (event != nil) {
1333 [cocoaView handleEvent:event];
1334 }
1335 } while(event != nil);
Peter Maydell68c0aa62013-04-17 09:16:35 +00001336 graphic_hw_update(NULL);
Peter Maydell6e657e62013-04-22 10:29:46 +00001337 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001338}
1339
1340static void cocoa_cleanup(void)
1341{
1342 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001343 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001344}
1345
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001346static const DisplayChangeListenerOps dcl_ops = {
1347 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001348 .dpy_gfx_update = cocoa_update,
1349 .dpy_gfx_switch = cocoa_switch,
1350 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001351};
1352
Programmingkidb4c6a112015-05-19 09:11:18 +01001353/* Returns a name for a given console */
1354static NSString * getConsoleName(QemuConsole * console)
1355{
1356 return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
1357}
1358
1359/* Add an entry to the View menu for each console */
1360static void add_console_menu_entries(void)
1361{
1362 NSMenu *menu;
1363 NSMenuItem *menuItem;
1364 int index = 0;
1365
1366 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1367
1368 [menu addItem:[NSMenuItem separatorItem]];
1369
1370 while (qemu_console_lookup_by_index(index) != NULL) {
1371 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1372 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1373 [menuItem setTag: index];
1374 [menu addItem: menuItem];
1375 index++;
1376 }
1377}
1378
John Arbuckle693a3e02015-06-19 10:53:27 +01001379/* Make menu items for all removable devices.
1380 * Each device is given an 'Eject' and 'Change' menu item.
1381 */
1382static void addRemovableDevicesMenuItems()
1383{
1384 NSMenu *menu;
1385 NSMenuItem *menuItem;
1386 BlockInfoList *currentDevice, *pointerToFree;
1387 NSString *deviceName;
1388
1389 currentDevice = qmp_query_block(NULL);
1390 pointerToFree = currentDevice;
1391 if(currentDevice == NULL) {
1392 NSBeep();
1393 QEMU_Alert(@"Failed to query for block devices!");
1394 return;
1395 }
1396
1397 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1398
1399 // Add a separator between related groups of menu items
1400 [menu addItem:[NSMenuItem separatorItem]];
1401
1402 // Set the attributes to the "Removable Media" menu item
1403 NSString *titleString = @"Removable Media";
1404 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1405 NSColor *newColor = [NSColor blackColor];
1406 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1407 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1408 traits:NSBoldFontMask|NSItalicFontMask
1409 weight:0
1410 size:14];
1411 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1412 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1413 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1414
1415 // Add the "Removable Media" menu item
1416 menuItem = [NSMenuItem new];
1417 [menuItem setAttributedTitle: attString];
1418 [menuItem setEnabled: NO];
1419 [menu addItem: menuItem];
1420
1421 /* Loop thru all the block devices in the emulator */
1422 while (currentDevice) {
1423 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1424
1425 if(currentDevice->value->removable) {
1426 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1427 action: @selector(changeDeviceMedia:)
1428 keyEquivalent: @""];
1429 [menu addItem: menuItem];
1430 [menuItem setRepresentedObject: deviceName];
1431 [menuItem autorelease];
1432
1433 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1434 action: @selector(ejectDeviceMedia:)
1435 keyEquivalent: @""];
1436 [menu addItem: menuItem];
1437 [menuItem setRepresentedObject: deviceName];
1438 [menuItem autorelease];
1439 }
1440 currentDevice = currentDevice->next;
1441 }
1442 qapi_free_BlockInfoList(pointerToFree);
1443}
1444
thsc304f7e2008-01-22 23:25:15 +00001445void cocoa_display_init(DisplayState *ds, int full_screen)
1446{
1447 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1448
Programmingkid43227af2015-05-19 09:11:17 +01001449 /* if fullscreen mode is to be used */
1450 if (full_screen == true) {
1451 [NSApp activateIgnoringOtherApps: YES];
1452 [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
1453 }
1454
Blue Swirl58a06672011-08-21 18:42:08 +00001455 dcl = g_malloc0(sizeof(DisplayChangeListener));
1456
aliguori9794f742009-03-04 19:25:22 +00001457 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001458 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001459 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001460
1461 // register cleanup function
1462 atexit(cocoa_cleanup);
Programmingkidb4c6a112015-05-19 09:11:18 +01001463
1464 /* At this point QEMU has created all the consoles, so we can add View
1465 * menu entries for them.
1466 */
1467 add_console_menu_entries();
John Arbuckle693a3e02015-06-19 10:53:27 +01001468
1469 /* Give all removable devices a menu item.
1470 * Has to be called after QEMU has started to
1471 * find out what removable devices it has.
1472 */
1473 addRemovableDevicesMenuItems();
thsc304f7e2008-01-22 23:25:15 +00001474}