blob: f39c792cba09bce85159a038a76ae89029a89786 [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
Peter Maydelle4a096b2016-01-29 16:23:34 +000025#include "qemu/osdep.h"
26
bellard5b0753e2005-03-01 21:37:28 +000027#import <Cocoa/Cocoa.h>
Andreas Färber3bbbee12011-05-29 19:42:51 +020028#include <crt_externs.h>
bellard5b0753e2005-03-01 21:37:28 +000029
pbrook87ecb682007-11-17 17:14:51 +000030#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010031#include "ui/console.h"
Gerd Hoffmann21bae112013-12-04 14:08:04 +010032#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
John Arbuckle8524f1c2015-06-19 10:53:27 +010034#include "qmp-commands.h"
John Arbuckle693a3e02015-06-19 10:53:27 +010035#include "sysemu/blockdev.h"
Programmingkid9e8204b2016-08-15 22:11:06 -040036#include "qemu-version.h"
John Arbuckleaaac7142016-03-23 14:26:18 +000037#include <Carbon/Carbon.h>
John Arbucklee47ec1a2017-06-13 23:17:38 -040038#include "qom/cpu.h"
bellardda4dbf72005-03-02 22:22:43 +000039
Andreas Färber44e4c0b2009-12-13 00:45:40 +010040#ifndef MAC_OS_X_VERSION_10_5
41#define MAC_OS_X_VERSION_10_5 1050
42#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000043#ifndef MAC_OS_X_VERSION_10_6
44#define MAC_OS_X_VERSION_10_6 1060
45#endif
Peter Maydell81801ae2015-05-19 09:11:18 +010046#ifndef MAC_OS_X_VERSION_10_10
47#define MAC_OS_X_VERSION_10_10 101000
48#endif
Brendan Shanks4ba967a2017-04-24 23:29:52 -070049#ifndef MAC_OS_X_VERSION_10_12
50#define MAC_OS_X_VERSION_10_12 101200
51#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010052
Brendan Shanks4ba967a2017-04-24 23:29:52 -070053/* macOS 10.12 deprecated many constants, #define the new names for older SDKs */
54#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
55#define NSEventMaskAny NSAnyEventMask
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -070056#define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
57#define NSEventModifierFlagShift NSShiftKeyMask
Brendan Shanks4ba967a2017-04-24 23:29:52 -070058#define NSEventModifierFlagCommand NSCommandKeyMask
59#define NSEventModifierFlagControl NSControlKeyMask
60#define NSEventModifierFlagOption NSAlternateKeyMask
61#define NSEventTypeFlagsChanged NSFlagsChanged
62#define NSEventTypeKeyUp NSKeyUp
63#define NSEventTypeKeyDown NSKeyDown
64#define NSEventTypeMouseMoved NSMouseMoved
65#define NSEventTypeLeftMouseDown NSLeftMouseDown
66#define NSEventTypeRightMouseDown NSRightMouseDown
67#define NSEventTypeOtherMouseDown NSOtherMouseDown
68#define NSEventTypeLeftMouseDragged NSLeftMouseDragged
69#define NSEventTypeRightMouseDragged NSRightMouseDragged
70#define NSEventTypeOtherMouseDragged NSOtherMouseDragged
71#define NSEventTypeLeftMouseUp NSLeftMouseUp
72#define NSEventTypeRightMouseUp NSRightMouseUp
73#define NSEventTypeOtherMouseUp NSOtherMouseUp
74#define NSEventTypeScrollWheel NSScrollWheel
75#define NSTextAlignmentCenter NSCenterTextAlignment
76#define NSWindowStyleMaskBorderless NSBorderlessWindowMask
77#define NSWindowStyleMaskClosable NSClosableWindowMask
78#define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
79#define NSWindowStyleMaskTitled NSTitledWindowMask
80#endif
bellard5b0753e2005-03-01 21:37:28 +000081
thsc304f7e2008-01-22 23:25:15 +000082//#define DEBUG
83
84#ifdef DEBUG
85#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
86#else
87#define COCOA_DEBUG(...) ((void) 0)
88#endif
89
90#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000091
92typedef struct {
93 int width;
94 int height;
95 int bitsPerComponent;
96 int bitsPerPixel;
97} QEMUScreen;
98
Programmingkid9e8204b2016-08-15 22:11:06 -040099NSWindow *normalWindow, *about_window;
aliguori9794f742009-03-04 19:25:22 +0000100static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100101static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +0000102
103int gArgc;
104char **gArgv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100105bool stretch_video;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100106NSTextField *pauseLabel;
John Arbuckle693a3e02015-06-19 10:53:27 +0100107NSArray * supportedImageFileTypes;
bellard5b0753e2005-03-01 21:37:28 +0000108
John Arbuckleaaac7142016-03-23 14:26:18 +0000109// Mac to QKeyCode conversion
110const int mac_to_qkeycode_map[] = {
111 [kVK_ANSI_A] = Q_KEY_CODE_A,
112 [kVK_ANSI_B] = Q_KEY_CODE_B,
113 [kVK_ANSI_C] = Q_KEY_CODE_C,
114 [kVK_ANSI_D] = Q_KEY_CODE_D,
115 [kVK_ANSI_E] = Q_KEY_CODE_E,
116 [kVK_ANSI_F] = Q_KEY_CODE_F,
117 [kVK_ANSI_G] = Q_KEY_CODE_G,
118 [kVK_ANSI_H] = Q_KEY_CODE_H,
119 [kVK_ANSI_I] = Q_KEY_CODE_I,
120 [kVK_ANSI_J] = Q_KEY_CODE_J,
121 [kVK_ANSI_K] = Q_KEY_CODE_K,
122 [kVK_ANSI_L] = Q_KEY_CODE_L,
123 [kVK_ANSI_M] = Q_KEY_CODE_M,
124 [kVK_ANSI_N] = Q_KEY_CODE_N,
125 [kVK_ANSI_O] = Q_KEY_CODE_O,
126 [kVK_ANSI_P] = Q_KEY_CODE_P,
127 [kVK_ANSI_Q] = Q_KEY_CODE_Q,
128 [kVK_ANSI_R] = Q_KEY_CODE_R,
129 [kVK_ANSI_S] = Q_KEY_CODE_S,
130 [kVK_ANSI_T] = Q_KEY_CODE_T,
131 [kVK_ANSI_U] = Q_KEY_CODE_U,
132 [kVK_ANSI_V] = Q_KEY_CODE_V,
133 [kVK_ANSI_W] = Q_KEY_CODE_W,
134 [kVK_ANSI_X] = Q_KEY_CODE_X,
135 [kVK_ANSI_Y] = Q_KEY_CODE_Y,
136 [kVK_ANSI_Z] = Q_KEY_CODE_Z,
ths3b46e622007-09-17 08:09:54 +0000137
John Arbuckleaaac7142016-03-23 14:26:18 +0000138 [kVK_ANSI_0] = Q_KEY_CODE_0,
139 [kVK_ANSI_1] = Q_KEY_CODE_1,
140 [kVK_ANSI_2] = Q_KEY_CODE_2,
141 [kVK_ANSI_3] = Q_KEY_CODE_3,
142 [kVK_ANSI_4] = Q_KEY_CODE_4,
143 [kVK_ANSI_5] = Q_KEY_CODE_5,
144 [kVK_ANSI_6] = Q_KEY_CODE_6,
145 [kVK_ANSI_7] = Q_KEY_CODE_7,
146 [kVK_ANSI_8] = Q_KEY_CODE_8,
147 [kVK_ANSI_9] = Q_KEY_CODE_9,
148
149 [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
150 [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
151 [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
152 [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
153 [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
154 [kVK_Tab] = Q_KEY_CODE_TAB,
155 [kVK_Return] = Q_KEY_CODE_RET,
156 [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
157 [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
158 [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
159 [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
160 [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
161 [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
162 [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
163 [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
164 [kVK_Shift] = Q_KEY_CODE_SHIFT,
165 [kVK_RightShift] = Q_KEY_CODE_SHIFT_R,
166 [kVK_Control] = Q_KEY_CODE_CTRL,
167 [kVK_RightControl] = Q_KEY_CODE_CTRL_R,
168 [kVK_Option] = Q_KEY_CODE_ALT,
169 [kVK_RightOption] = Q_KEY_CODE_ALT_R,
170 [kVK_Command] = Q_KEY_CODE_META_L,
171 [0x36] = Q_KEY_CODE_META_R, /* There is no kVK_RightCommand */
172 [kVK_Space] = Q_KEY_CODE_SPC,
173
174 [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
175 [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
176 [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
177 [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
178 [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
179 [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
180 [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
181 [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
182 [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
183 [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
184 [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
185 [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
186 [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
187 [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
188 [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
189 [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
190 [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
191 [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
192
193 [kVK_UpArrow] = Q_KEY_CODE_UP,
194 [kVK_DownArrow] = Q_KEY_CODE_DOWN,
195 [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
196 [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
197
198 [kVK_Help] = Q_KEY_CODE_INSERT,
199 [kVK_Home] = Q_KEY_CODE_HOME,
200 [kVK_PageUp] = Q_KEY_CODE_PGUP,
201 [kVK_PageDown] = Q_KEY_CODE_PGDN,
202 [kVK_End] = Q_KEY_CODE_END,
203 [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
204
205 [kVK_Escape] = Q_KEY_CODE_ESC,
206
207 /* The Power key can't be used directly because the operating system uses
208 * it. This key can be emulated by using it in place of another key such as
209 * F1. Don't forget to disable the real key binding.
210 */
211 /* [kVK_F1] = Q_KEY_CODE_POWER, */
212
213 [kVK_F1] = Q_KEY_CODE_F1,
214 [kVK_F2] = Q_KEY_CODE_F2,
215 [kVK_F3] = Q_KEY_CODE_F3,
216 [kVK_F4] = Q_KEY_CODE_F4,
217 [kVK_F5] = Q_KEY_CODE_F5,
218 [kVK_F6] = Q_KEY_CODE_F6,
219 [kVK_F7] = Q_KEY_CODE_F7,
220 [kVK_F8] = Q_KEY_CODE_F8,
221 [kVK_F9] = Q_KEY_CODE_F9,
222 [kVK_F10] = Q_KEY_CODE_F10,
223 [kVK_F11] = Q_KEY_CODE_F11,
224 [kVK_F12] = Q_KEY_CODE_F12,
225 [kVK_F13] = Q_KEY_CODE_PRINT,
226 [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
227 [kVK_F15] = Q_KEY_CODE_PAUSE,
228
229 /*
230 * The eject and volume keys can't be used here because they are handled at
231 * a lower level than what an Application can see.
232 */
bellard5b0753e2005-03-01 21:37:28 +0000233};
234
Andreas Färber77047bb2009-12-13 00:55:53 +0100235static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000236{
John Arbuckleaaac7142016-03-23 14:26:18 +0000237 if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000238 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000239 return 0;
240 }
John Arbuckleaaac7142016-03-23 14:26:18 +0000241 return mac_to_qkeycode_map[keycode];
bellard5b0753e2005-03-01 21:37:28 +0000242}
243
John Arbuckle693a3e02015-06-19 10:53:27 +0100244/* Displays an alert dialog box with the specified message */
245static void QEMU_Alert(NSString *message)
246{
247 NSAlert *alert;
248 alert = [NSAlert new];
249 [alert setMessageText: message];
250 [alert runModal];
251}
thsc304f7e2008-01-22 23:25:15 +0000252
John Arbuckle693a3e02015-06-19 10:53:27 +0100253/* Handles any errors that happen with a device transaction */
254static void handleAnyDeviceErrors(Error * err)
255{
256 if (err) {
257 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
258 encoding: NSASCIIStringEncoding]);
259 error_free(err);
260 }
261}
thsc304f7e2008-01-22 23:25:15 +0000262
bellard5b0753e2005-03-01 21:37:28 +0000263/*
264 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000265 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000266 ------------------------------------------------------
267*/
thsc304f7e2008-01-22 23:25:15 +0000268@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000269{
thsc304f7e2008-01-22 23:25:15 +0000270 QEMUScreen screen;
271 NSWindow *fullScreenWindow;
272 float cx,cy,cw,ch,cdx,cdy;
273 CGDataProviderRef dataProviderRef;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700274 BOOL modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000275 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000276 BOOL isFullscreen;
277 BOOL isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100278 BOOL isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000279}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100280- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000281- (void) grabMouse;
282- (void) ungrabMouse;
283- (void) toggleFullScreen:(id)sender;
John Arbuckle9c3a4182017-11-07 10:14:14 +0000284- (void) handleMonitorInput:(NSEvent *)event;
thsc304f7e2008-01-22 23:25:15 +0000285- (void) handleEvent:(NSEvent *)event;
286- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100287/* The state surrounding mouse grabbing is potentially confusing.
288 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
289 * pointing device an absolute-position one?"], but is only updated on
290 * next refresh.
291 * isMouseGrabbed tracks whether GUI events are directed to the guest;
292 * it controls whether special keys like Cmd get sent to the guest,
293 * and whether we capture the mouse when in non-absolute mode.
294 * isMouseDeassociated tracks whether we've told MacOSX to disassociate
295 * the mouse and mouse cursor position by calling
296 * CGAssociateMouseAndMouseCursorPosition(FALSE)
297 * (which basically happens if we grab in non-absolute mode).
298 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000299- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000300- (BOOL) isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100301- (BOOL) isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000302- (float) cdx;
303- (float) cdy;
304- (QEMUScreen) gscreen;
John Arbuckle3b178b72015-09-25 23:14:00 +0100305- (void) raiseAllKeys;
thsc304f7e2008-01-22 23:25:15 +0000306@end
ths3b46e622007-09-17 08:09:54 +0000307
Andreas Färber7fee1992011-06-09 20:53:32 +0200308QemuCocoaView *cocoaView;
309
thsc304f7e2008-01-22 23:25:15 +0000310@implementation QemuCocoaView
311- (id)initWithFrame:(NSRect)frameRect
312{
313 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000314
thsc304f7e2008-01-22 23:25:15 +0000315 self = [super initWithFrame:frameRect];
316 if (self) {
pbrook95219892006-04-09 01:06:34 +0000317
thsc304f7e2008-01-22 23:25:15 +0000318 screen.bitsPerComponent = 8;
319 screen.bitsPerPixel = 32;
320 screen.width = frameRect.size.width;
321 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000322
thsc304f7e2008-01-22 23:25:15 +0000323 }
324 return self;
325}
bellard7c206a72005-12-18 19:18:45 +0000326
thsc304f7e2008-01-22 23:25:15 +0000327- (void) dealloc
328{
329 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000330
thsc304f7e2008-01-22 23:25:15 +0000331 if (dataProviderRef)
332 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000333
thsc304f7e2008-01-22 23:25:15 +0000334 [super dealloc];
335}
bellard5cbfcd02006-06-14 15:53:24 +0000336
Andreas Färberd50f71d2009-12-13 02:03:33 +0100337- (BOOL) isOpaque
338{
339 return YES;
340}
341
Peter Maydell5dd45be2014-06-23 10:35:23 +0100342- (BOOL) screenContainsPoint:(NSPoint) p
343{
344 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
345}
346
Peter Maydell13aefd32014-06-23 10:35:25 +0100347- (void) hideCursor
348{
349 if (!cursor_hide) {
350 return;
351 }
352 [NSCursor hide];
353}
354
355- (void) unhideCursor
356{
357 if (!cursor_hide) {
358 return;
359 }
360 [NSCursor unhide];
361}
362
thsc304f7e2008-01-22 23:25:15 +0000363- (void) drawRect:(NSRect) rect
364{
365 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000366
thsc304f7e2008-01-22 23:25:15 +0000367 // get CoreGraphic context
368 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
369 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
370 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000371
thsc304f7e2008-01-22 23:25:15 +0000372 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000373 if (!dataProviderRef) {
374 // Draw request before any guest device has set up a framebuffer:
375 // just draw an opaque black rectangle
376 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
377 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
378 } else {
thsc304f7e2008-01-22 23:25:15 +0000379 CGImageRef imageRef = CGImageCreate(
380 screen.width, //width
381 screen.height, //height
382 screen.bitsPerComponent, //bitsPerComponent
383 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000384 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100385#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000386 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000387 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000388#else
389 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
390 kCGImageAlphaNoneSkipFirst, //bitmapInfo
391#endif
392 dataProviderRef, //provider
393 NULL, //decode
394 0, //interpolate
395 kCGRenderingIntentDefault //intent
396 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100397 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
398 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100399 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100400 int i;
401 CGImageRef clipImageRef;
402 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000403
Peter Maydellb63901d2015-05-19 09:11:17 +0100404 [self getRectsBeingDrawn:&rectList count:&rectCount];
405 for (i = 0; i < rectCount; i++) {
406 clipRect.origin.x = rectList[i].origin.x / cdx;
407 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
408 clipRect.size.width = rectList[i].size.width / cdx;
409 clipRect.size.height = rectList[i].size.height / cdy;
410 clipImageRef = CGImageCreateWithImageInRect(
411 imageRef,
412 clipRect
413 );
414 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
415 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000416 }
thsc304f7e2008-01-22 23:25:15 +0000417 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000418 }
419}
420
thsc304f7e2008-01-22 23:25:15 +0000421- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000422{
thsc304f7e2008-01-22 23:25:15 +0000423 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000424
thsc304f7e2008-01-22 23:25:15 +0000425 if (isFullscreen) {
426 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
427 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100428
429 /* stretches video, but keeps same aspect ratio */
430 if (stretch_video == true) {
431 /* use smallest stretch value - prevents clipping on sides */
432 if (MIN(cdx, cdy) == cdx) {
433 cdy = cdx;
434 } else {
435 cdx = cdy;
436 }
437 } else { /* No stretching */
438 cdx = cdy = 1;
439 }
thsc304f7e2008-01-22 23:25:15 +0000440 cw = screen.width * cdx;
441 ch = screen.height * cdy;
442 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
443 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
444 } else {
445 cx = 0;
446 cy = 0;
447 cw = screen.width;
448 ch = screen.height;
449 cdx = 1.0;
450 cdy = 1.0;
451 }
bellard5b0753e2005-03-01 21:37:28 +0000452}
453
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100454- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000455{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100456 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000457
Peter Maydell8510d912013-03-18 20:28:21 +0000458 int w = surface_width(surface);
459 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100460 /* cdx == 0 means this is our very first surface, in which case we need
461 * to recalculate the content dimensions even if it happens to be the size
462 * of the initial empty window.
463 */
464 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000465
466 int oldh = screen.height;
467 if (isResize) {
468 // Resize before we trigger the redraw, or we'll redraw at the wrong size
469 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
470 screen.width = w;
471 screen.height = h;
472 [self setContentDimensions];
473 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
474 }
Peter Maydell8510d912013-03-18 20:28:21 +0000475
thsc304f7e2008-01-22 23:25:15 +0000476 // update screenBuffer
477 if (dataProviderRef)
478 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000479
aliguori9794f742009-03-04 19:25:22 +0000480 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000481 screen.bitsPerPixel = surface_bits_per_pixel(surface);
482 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000483
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100484 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000485
486 // update windows
487 if (isFullscreen) {
488 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000489 [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 +0000490 } else {
491 if (qemu_name)
492 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000493 [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 +0000494 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000495
496 if (isResize) {
497 [normalWindow center];
498 }
thsc304f7e2008-01-22 23:25:15 +0000499}
500
501- (void) toggleFullScreen:(id)sender
502{
503 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
504
505 if (isFullscreen) { // switch from fullscreen to desktop
506 isFullscreen = FALSE;
507 [self ungrabMouse];
508 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000509 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
510 [self exitFullScreenModeWithOptions:nil];
511 } else {
thsc304f7e2008-01-22 23:25:15 +0000512 [fullScreenWindow close];
513 [normalWindow setContentView: self];
514 [normalWindow makeKeyAndOrderFront: self];
515 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000516 }
thsc304f7e2008-01-22 23:25:15 +0000517 } else { // switch from desktop to fullscreen
518 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100519 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000520 [self grabMouse];
521 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000522 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
523 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
524 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
525 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
526 nil]];
527 } else {
thsc304f7e2008-01-22 23:25:15 +0000528 [NSMenu setMenuBarVisible:NO];
529 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700530 styleMask:NSWindowStyleMaskBorderless
thsc304f7e2008-01-22 23:25:15 +0000531 backing:NSBackingStoreBuffered
532 defer:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100533 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
thsc304f7e2008-01-22 23:25:15 +0000534 [fullScreenWindow setHasShadow:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100535 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
536 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
537 [[fullScreenWindow contentView] addSubview: self];
thsc304f7e2008-01-22 23:25:15 +0000538 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000539 }
thsc304f7e2008-01-22 23:25:15 +0000540 }
541}
542
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700543- (void) toggleModifier: (int)keycode {
544 // Toggle the stored state.
545 modifiers_state[keycode] = !modifiers_state[keycode];
546 // Send a keyup or keydown depending on the state.
547 qemu_input_event_send_key_qcode(dcl->con, keycode, modifiers_state[keycode]);
548}
549
550- (void) toggleStatefulModifier: (int)keycode {
551 // Toggle the stored state.
552 modifiers_state[keycode] = !modifiers_state[keycode];
553 // Generate keydown and keyup.
554 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
555 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
556}
557
John Arbuckle9c3a4182017-11-07 10:14:14 +0000558// Does the work of sending input to the monitor
559- (void) handleMonitorInput:(NSEvent *)event
560{
561 int keysym = 0;
562 int control_key = 0;
563
564 // if the control key is down
565 if ([event modifierFlags] & NSEventModifierFlagControl) {
566 control_key = 1;
567 }
568
569 /* translates Macintosh keycodes to QEMU's keysym */
570
571 int without_control_translation[] = {
572 [0 ... 0xff] = 0, // invalid key
573
574 [kVK_UpArrow] = QEMU_KEY_UP,
575 [kVK_DownArrow] = QEMU_KEY_DOWN,
576 [kVK_RightArrow] = QEMU_KEY_RIGHT,
577 [kVK_LeftArrow] = QEMU_KEY_LEFT,
578 [kVK_Home] = QEMU_KEY_HOME,
579 [kVK_End] = QEMU_KEY_END,
580 [kVK_PageUp] = QEMU_KEY_PAGEUP,
581 [kVK_PageDown] = QEMU_KEY_PAGEDOWN,
582 [kVK_ForwardDelete] = QEMU_KEY_DELETE,
583 [kVK_Delete] = QEMU_KEY_BACKSPACE,
584 };
585
586 int with_control_translation[] = {
587 [0 ... 0xff] = 0, // invalid key
588
589 [kVK_UpArrow] = QEMU_KEY_CTRL_UP,
590 [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN,
591 [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT,
592 [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT,
593 [kVK_Home] = QEMU_KEY_CTRL_HOME,
594 [kVK_End] = QEMU_KEY_CTRL_END,
595 [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP,
596 [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN,
597 };
598
599 if (control_key != 0) { /* If the control key is being used */
600 if ([event keyCode] < ARRAY_SIZE(with_control_translation)) {
601 keysym = with_control_translation[[event keyCode]];
602 }
603 } else {
604 if ([event keyCode] < ARRAY_SIZE(without_control_translation)) {
605 keysym = without_control_translation[[event keyCode]];
606 }
607 }
608
609 // if not a key that needs translating
610 if (keysym == 0) {
611 NSString *ks = [event characters];
612 if ([ks length] > 0) {
613 keysym = [ks characterAtIndex:0];
614 }
615 }
616
617 if (keysym) {
618 kbd_put_keysym(keysym);
619 }
620}
621
thsc304f7e2008-01-22 23:25:15 +0000622- (void) handleEvent:(NSEvent *)event
623{
624 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
625
626 int buttons = 0;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700627 int keycode = 0;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100628 bool mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000629 NSPoint p = [event locationInWindow];
630
631 switch ([event type]) {
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700632 case NSEventTypeFlagsChanged:
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700633 if ([event keyCode] == 0) {
634 // When the Cocoa keyCode is zero that means keys should be
635 // synthesized based on the values in in the eventModifiers
636 // bitmask.
637
638 if (qemu_console_is_graphic(NULL)) {
John Arbucklee7206242017-06-28 15:37:16 -0400639 NSUInteger modifiers = [event modifierFlags];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700640
641 if (!!(modifiers & NSEventModifierFlagCapsLock) != !!modifiers_state[Q_KEY_CODE_CAPS_LOCK]) {
642 [self toggleStatefulModifier:Q_KEY_CODE_CAPS_LOCK];
643 }
644 if (!!(modifiers & NSEventModifierFlagShift) != !!modifiers_state[Q_KEY_CODE_SHIFT]) {
645 [self toggleModifier:Q_KEY_CODE_SHIFT];
646 }
647 if (!!(modifiers & NSEventModifierFlagControl) != !!modifiers_state[Q_KEY_CODE_CTRL]) {
648 [self toggleModifier:Q_KEY_CODE_CTRL];
649 }
650 if (!!(modifiers & NSEventModifierFlagOption) != !!modifiers_state[Q_KEY_CODE_ALT]) {
651 [self toggleModifier:Q_KEY_CODE_ALT];
652 }
653 if (!!(modifiers & NSEventModifierFlagCommand) != !!modifiers_state[Q_KEY_CODE_META_L]) {
654 [self toggleModifier:Q_KEY_CODE_META_L];
655 }
656 }
657 } else {
658 keycode = cocoa_keycode_to_qemu([event keyCode]);
659 }
Peter Maydell88959192013-12-08 22:59:02 +0000660
John Arbuckleaaac7142016-03-23 14:26:18 +0000661 if ((keycode == Q_KEY_CODE_META_L || keycode == Q_KEY_CODE_META_R)
662 && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000663 /* Don't pass command key changes to guest unless mouse is grabbed */
664 keycode = 0;
665 }
666
thsc304f7e2008-01-22 23:25:15 +0000667 if (keycode) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000668 // emulate caps lock and num lock keydown and keyup
669 if (keycode == Q_KEY_CODE_CAPS_LOCK ||
670 keycode == Q_KEY_CODE_NUM_LOCK) {
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700671 [self toggleStatefulModifier:keycode];
Peter Maydell68c0aa62013-04-17 09:16:35 +0000672 } else if (qemu_console_is_graphic(NULL)) {
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700673 [self toggleModifier:keycode];
thsc304f7e2008-01-22 23:25:15 +0000674 }
675 }
676
thsc304f7e2008-01-22 23:25:15 +0000677 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700678 case NSEventTypeKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000679 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000680
Peter Maydell88959192013-12-08 22:59:02 +0000681 // forward command key combos to the host UI unless the mouse is grabbed
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700682 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
thsc304f7e2008-01-22 23:25:15 +0000683 [NSApp sendEvent:event];
684 return;
685 }
686
687 // default
thsc304f7e2008-01-22 23:25:15 +0000688
John Arbuckle5929e362017-11-07 10:14:14 +0000689 // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700690 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
John Arbuckle5929e362017-11-07 10:14:14 +0000691 NSString *keychar = [event charactersIgnoringModifiers];
692 if ([keychar length] == 1) {
693 char key = [keychar characterAtIndex:0];
694 switch (key) {
thsc304f7e2008-01-22 23:25:15 +0000695
John Arbuckle5929e362017-11-07 10:14:14 +0000696 // enable graphic console
697 case '1' ... '9':
698 console_select(key - '0' - 1); /* ascii math */
699 return;
700
701 // release the mouse grab
702 case 'g':
703 [self ungrabMouse];
704 return;
705 }
thsc304f7e2008-01-22 23:25:15 +0000706 }
707
708 // handle keys for graphic console
Peter Maydell68c0aa62013-04-17 09:16:35 +0000709 } else if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000710 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000711
712 // handlekeys for Monitor
713 } else {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000714 [self handleMonitorInput: event];
thsc304f7e2008-01-22 23:25:15 +0000715 }
716 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700717 case NSEventTypeKeyUp:
thsc304f7e2008-01-22 23:25:15 +0000718 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000719
720 // don't pass the guest a spurious key-up if we treated this
721 // command-key combo as a host UI action
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700722 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell88959192013-12-08 22:59:02 +0000723 return;
724 }
725
Peter Maydell68c0aa62013-04-17 09:16:35 +0000726 if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000727 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000728 }
729 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700730 case NSEventTypeMouseMoved:
thsc304f7e2008-01-22 23:25:15 +0000731 if (isAbsoluteEnabled) {
Peter Maydell5dd45be2014-06-23 10:35:23 +0100732 if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
Peter Maydellf61c3872014-06-23 10:35:24 +0100733 if (isMouseGrabbed) {
734 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +0000735 }
736 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +0100737 if (!isMouseGrabbed) {
738 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000739 }
740 }
741 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100742 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000743 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700744 case NSEventTypeLeftMouseDown:
745 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000746 buttons |= MOUSE_EVENT_RBUTTON;
747 } else {
748 buttons |= MOUSE_EVENT_LBUTTON;
749 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100750 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000751 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700752 case NSEventTypeRightMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000753 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100754 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000755 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700756 case NSEventTypeOtherMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000757 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100758 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000759 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700760 case NSEventTypeLeftMouseDragged:
761 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000762 buttons |= MOUSE_EVENT_RBUTTON;
763 } else {
764 buttons |= MOUSE_EVENT_LBUTTON;
765 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100766 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000767 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700768 case NSEventTypeRightMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000769 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100770 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000771 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700772 case NSEventTypeOtherMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000773 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100774 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000775 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700776 case NSEventTypeLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +0100777 mouse_event = true;
778 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
Programmingkid9e8204b2016-08-15 22:11:06 -0400779 if([[self window] isKeyWindow]) {
780 [self grabMouse];
781 }
thsc304f7e2008-01-22 23:25:15 +0000782 }
783 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700784 case NSEventTypeRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100785 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000786 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700787 case NSEventTypeOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100788 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000789 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700790 case NSEventTypeScrollWheel:
Peter Maydellf61c3872014-06-23 10:35:24 +0100791 if (isMouseGrabbed) {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100792 buttons |= ([event deltaY] < 0) ?
793 MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
thsc304f7e2008-01-22 23:25:15 +0000794 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100795 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000796 break;
797 default:
798 [NSApp sendEvent:event];
799 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100800
801 if (mouse_event) {
Peter Maydell8d3a5d92015-11-26 15:19:28 +0000802 /* Don't send button events to the guest unless we've got a
803 * mouse grab or window focus. If we have neither then this event
804 * is the user clicking on the background window to activate and
805 * bring us to the front, which will be done by the sendEvent
806 * call below. We definitely don't want to pass that click through
807 * to the guest.
808 */
809 if ((isMouseGrabbed || [[self window] isKeyWindow]) &&
810 (last_buttons != buttons)) {
Eric Blake7fb1cf12015-11-18 01:52:57 -0700811 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100812 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
813 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
814 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100815 [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP,
816 [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100817 };
818 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
819 last_buttons = buttons;
820 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100821 if (isMouseGrabbed) {
822 if (isAbsoluteEnabled) {
823 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
824 * The check on screenContainsPoint is to avoid sending out of range values for
825 * clicks in the titlebar.
826 */
827 if ([self screenContainsPoint:p]) {
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200828 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, 0, screen.width);
829 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
Peter Maydellf61c3872014-06-23 10:35:24 +0100830 }
831 } else {
832 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
833 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
834 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100835 } else {
836 [NSApp sendEvent:event];
837 }
838 qemu_input_event_sync();
839 }
thsc304f7e2008-01-22 23:25:15 +0000840}
841
842- (void) grabMouse
843{
844 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
845
846 if (!isFullscreen) {
847 if (qemu_name)
John Arbuckle5929e362017-11-07 10:14:14 +0000848 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt + g to release Mouse)", qemu_name]];
thsc304f7e2008-01-22 23:25:15 +0000849 else
John Arbuckle5929e362017-11-07 10:14:14 +0000850 [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release Mouse)"];
thsc304f7e2008-01-22 23:25:15 +0000851 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100852 [self hideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100853 if (!isAbsoluteEnabled) {
854 isMouseDeassociated = TRUE;
855 CGAssociateMouseAndMouseCursorPosition(FALSE);
856 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000857 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000858}
859
860- (void) ungrabMouse
861{
862 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
863
864 if (!isFullscreen) {
865 if (qemu_name)
866 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
867 else
868 [normalWindow setTitle:@"QEMU"];
869 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100870 [self unhideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100871 if (isMouseDeassociated) {
872 CGAssociateMouseAndMouseCursorPosition(TRUE);
873 isMouseDeassociated = FALSE;
874 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000875 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000876}
877
878- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000879- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000880- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
Peter Maydellf61c3872014-06-23 10:35:24 +0100881- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
thsc304f7e2008-01-22 23:25:15 +0000882- (float) cdx {return cdx;}
883- (float) cdy {return cdy;}
884- (QEMUScreen) gscreen {return screen;}
John Arbuckle3b178b72015-09-25 23:14:00 +0100885
886/*
887 * Makes the target think all down keys are being released.
888 * This prevents a stuck key problem, since we will not see
889 * key up events for those keys after we have lost focus.
890 */
891- (void) raiseAllKeys
892{
893 int index;
894 const int max_index = ARRAY_SIZE(modifiers_state);
895
896 for (index = 0; index < max_index; index++) {
897 if (modifiers_state[index]) {
898 modifiers_state[index] = 0;
John Arbuckleaaac7142016-03-23 14:26:18 +0000899 qemu_input_event_send_key_qcode(dcl->con, index, false);
John Arbuckle3b178b72015-09-25 23:14:00 +0100900 }
901 }
902}
bellard5b0753e2005-03-01 21:37:28 +0000903@end
904
905
thsc304f7e2008-01-22 23:25:15 +0000906
bellard5b0753e2005-03-01 21:37:28 +0000907/*
908 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000909 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000910 ------------------------------------------------------
911*/
thsc304f7e2008-01-22 23:25:15 +0000912@interface QemuCocoaAppController : NSObject
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100913#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
John Arbuckled9bc14f2015-09-25 23:13:59 +0100914 <NSWindowDelegate, NSApplicationDelegate>
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100915#endif
bellard5b0753e2005-03-01 21:37:28 +0000916{
917}
bellard5b0753e2005-03-01 21:37:28 +0000918- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100919- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +0000920- (void)toggleFullScreen:(id)sender;
921- (void)showQEMUDoc:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100922- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +0100923- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100924- (void)pauseQEMU:(id)sender;
925- (void)resumeQEMU:(id)sender;
926- (void)displayPause;
927- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +0100928- (void)restartQEMU:(id)sender;
929- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +0100930- (void)ejectDeviceMedia:(id)sender;
931- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +0100932- (BOOL)verifyQuit;
John Arbucklef4747902016-03-23 14:26:17 +0000933- (void)openDocumentation:(NSString *)filename;
Programmingkid9e8204b2016-08-15 22:11:06 -0400934- (IBAction) do_about_menu_item: (id) sender;
935- (void)make_about_window;
John Arbucklee47ec1a2017-06-13 23:17:38 -0400936- (void)adjustSpeed:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +0000937@end
938
thsc304f7e2008-01-22 23:25:15 +0000939@implementation QemuCocoaAppController
940- (id) init
941{
942 COCOA_DEBUG("QemuCocoaAppController: init\n");
943
944 self = [super init];
945 if (self) {
946
947 // create a view and add it to the window
948 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
949 if(!cocoaView) {
950 fprintf(stderr, "(cocoa) can't create a view\n");
951 exit(1);
952 }
953
954 // create a window
955 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700956 styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable
thsc304f7e2008-01-22 23:25:15 +0000957 backing:NSBackingStoreBuffered defer:NO];
958 if(!normalWindow) {
959 fprintf(stderr, "(cocoa) can't create window\n");
960 exit(1);
961 }
962 [normalWindow setAcceptsMouseMovedEvents:YES];
John Arbucklea1dbc052015-10-13 21:51:18 +0100963 [normalWindow setTitle:@"QEMU"];
thsc304f7e2008-01-22 23:25:15 +0000964 [normalWindow setContentView:cocoaView];
Peter Maydell81801ae2015-05-19 09:11:18 +0100965#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
Andreas Färber561ef252009-12-13 03:06:20 +0100966 [normalWindow useOptimizedDrawing:YES];
Peter Maydell81801ae2015-05-19 09:11:18 +0100967#endif
thsc304f7e2008-01-22 23:25:15 +0000968 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +0000969 [normalWindow center];
John Arbuckled9bc14f2015-09-25 23:13:59 +0100970 [normalWindow setDelegate: self];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100971 stretch_video = false;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100972
973 /* Used for displaying pause on the screen */
974 pauseLabel = [NSTextField new];
975 [pauseLabel setBezeled:YES];
976 [pauseLabel setDrawsBackground:YES];
977 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
978 [pauseLabel setEditable:NO];
979 [pauseLabel setSelectable:NO];
980 [pauseLabel setStringValue: @"Paused"];
981 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
982 [pauseLabel setTextColor: [NSColor blackColor]];
983 [pauseLabel sizeToFit];
John Arbuckle693a3e02015-06-19 10:53:27 +0100984
985 // set the supported image file types that can be opened
986 supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg",
John Arbuckle9d227f12016-03-30 12:37:11 -0400987 @"qcow", @"qcow2", @"cloop", @"vmdk", @"cdr",
Programmingkid5f26fcf2016-12-30 15:42:21 -0500988 @"toast", nil];
Programmingkid9e8204b2016-08-15 22:11:06 -0400989 [self make_about_window];
thsc304f7e2008-01-22 23:25:15 +0000990 }
991 return self;
992}
993
994- (void) dealloc
995{
996 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
997
998 if (cocoaView)
999 [cocoaView release];
1000 [super dealloc];
1001}
1002
bellard5b0753e2005-03-01 21:37:28 +00001003- (void)applicationDidFinishLaunching: (NSNotification *) note
1004{
thsc304f7e2008-01-22 23:25:15 +00001005 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
John Arbuckle365d7f32015-09-25 23:14:00 +01001006 // launch QEMU, with the global args
1007 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5b0753e2005-03-01 21:37:28 +00001008}
1009
1010- (void)applicationWillTerminate:(NSNotification *)aNotification
1011{
thsc304f7e2008-01-22 23:25:15 +00001012 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
1013
Eric Blakecf83f142017-05-15 16:41:13 -05001014 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
bellard5b0753e2005-03-01 21:37:28 +00001015 exit(0);
1016}
1017
Andreas Färber41ea49b2009-12-14 22:13:27 +01001018- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
1019{
1020 return YES;
1021}
1022
John Arbuckled9bc14f2015-09-25 23:13:59 +01001023- (NSApplicationTerminateReply)applicationShouldTerminate:
1024 (NSApplication *)sender
1025{
1026 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
1027 return [self verifyQuit];
1028}
1029
1030/* Called when the user clicks on a window's close button */
1031- (BOOL)windowShouldClose:(id)sender
1032{
1033 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
1034 [NSApp terminate: sender];
1035 /* If the user allows the application to quit then the call to
1036 * NSApp terminate will never return. If we get here then the user
1037 * cancelled the quit, so we should return NO to not permit the
1038 * closing of this window.
1039 */
1040 return NO;
1041}
1042
John Arbuckle3b178b72015-09-25 23:14:00 +01001043/* Called when QEMU goes into the background */
1044- (void) applicationWillResignActive: (NSNotification *)aNotification
1045{
1046 COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
1047 [cocoaView raiseAllKeys];
1048}
1049
thsc304f7e2008-01-22 23:25:15 +00001050- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
1051{
1052 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
1053
1054 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +02001055 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +00001056 exit(status);
1057}
1058
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001059/* We abstract the method called by the Enter Fullscreen menu item
1060 * because Mac OS 10.7 and higher disables it. This is because of the
1061 * menu item's old selector's name toggleFullScreen:
1062 */
1063- (void) doToggleFullScreen:(id)sender
1064{
1065 [self toggleFullScreen:(id)sender];
1066}
1067
thsc304f7e2008-01-22 23:25:15 +00001068- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +00001069{
thsc304f7e2008-01-22 23:25:15 +00001070 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
1071
1072 [cocoaView toggleFullScreen:sender];
1073}
1074
John Arbucklef4747902016-03-23 14:26:17 +00001075/* Tries to find then open the specified filename */
1076- (void) openDocumentation: (NSString *) filename
1077{
1078 /* Where to look for local files */
1079 NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"};
1080 NSString *full_file_path;
1081
1082 /* iterate thru the possible paths until the file is found */
1083 int index;
1084 for (index = 0; index < ARRAY_SIZE(path_array); index++) {
1085 full_file_path = [[NSBundle mainBundle] executablePath];
1086 full_file_path = [full_file_path stringByDeletingLastPathComponent];
1087 full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
1088 path_array[index], filename];
1089 if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) {
1090 return;
1091 }
1092 }
1093
1094 /* If none of the paths opened a file */
1095 NSBeep();
1096 QEMU_Alert(@"Failed to open file");
1097}
1098
thsc304f7e2008-01-22 23:25:15 +00001099- (void)showQEMUDoc:(id)sender
1100{
1101 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1102
John Arbucklef4747902016-03-23 14:26:17 +00001103 [self openDocumentation: @"qemu-doc.html"];
thsc304f7e2008-01-22 23:25:15 +00001104}
1105
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001106/* Stretches video to fit host monitor size */
1107- (void)zoomToFit:(id) sender
1108{
1109 stretch_video = !stretch_video;
1110 if (stretch_video == true) {
1111 [sender setState: NSOnState];
1112 } else {
1113 [sender setState: NSOffState];
1114 }
1115}
bellard5b0753e2005-03-01 21:37:28 +00001116
Programmingkidb4c6a112015-05-19 09:11:18 +01001117/* Displays the console on the screen */
1118- (void)displayConsole:(id)sender
1119{
1120 console_select([sender tag]);
1121}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001122
1123/* Pause the guest */
1124- (void)pauseQEMU:(id)sender
1125{
1126 qmp_stop(NULL);
1127 [sender setEnabled: NO];
1128 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1129 [self displayPause];
1130}
1131
1132/* Resume running the guest operating system */
1133- (void)resumeQEMU:(id) sender
1134{
1135 qmp_cont(NULL);
1136 [sender setEnabled: NO];
1137 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1138 [self removePause];
1139}
1140
1141/* Displays the word pause on the screen */
1142- (void)displayPause
1143{
1144 /* Coordinates have to be calculated each time because the window can change its size */
1145 int xCoord, yCoord, width, height;
1146 xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
1147 yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
1148 width = [pauseLabel frame].size.width;
1149 height = [pauseLabel frame].size.height;
1150 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1151 [cocoaView addSubview: pauseLabel];
1152}
1153
1154/* Removes the word pause from the screen */
1155- (void)removePause
1156{
1157 [pauseLabel removeFromSuperview];
1158}
1159
John Arbuckle27074612015-06-19 10:53:27 +01001160/* Restarts QEMU */
1161- (void)restartQEMU:(id)sender
1162{
1163 qmp_system_reset(NULL);
1164}
1165
1166/* Powers down QEMU */
1167- (void)powerDownQEMU:(id)sender
1168{
1169 qmp_system_powerdown(NULL);
1170}
1171
John Arbuckle693a3e02015-06-19 10:53:27 +01001172/* Ejects the media.
1173 * Uses sender's tag to figure out the device to eject.
1174 */
1175- (void)ejectDeviceMedia:(id)sender
1176{
1177 NSString * drive;
1178 drive = [sender representedObject];
1179 if(drive == nil) {
1180 NSBeep();
1181 QEMU_Alert(@"Failed to find drive to eject!");
1182 return;
1183 }
1184
1185 Error *err = NULL;
Kevin Wolffbe2d812016-09-20 13:38:46 +02001186 qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding],
1187 false, NULL, false, false, &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001188 handleAnyDeviceErrors(err);
1189}
1190
1191/* Displays a dialog box asking the user to select an image file to load.
1192 * Uses sender's represented object value to figure out which drive to use.
1193 */
1194- (void)changeDeviceMedia:(id)sender
1195{
1196 /* Find the drive name */
1197 NSString * drive;
1198 drive = [sender representedObject];
1199 if(drive == nil) {
1200 NSBeep();
1201 QEMU_Alert(@"Could not find drive!");
1202 return;
1203 }
1204
1205 /* Display the file open dialog */
1206 NSOpenPanel * openPanel;
1207 openPanel = [NSOpenPanel openPanel];
1208 [openPanel setCanChooseFiles: YES];
1209 [openPanel setAllowsMultipleSelection: NO];
1210 [openPanel setAllowedFileTypes: supportedImageFileTypes];
1211 if([openPanel runModal] == NSFileHandlingPanelOKButton) {
1212 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1213 if(file == nil) {
1214 NSBeep();
1215 QEMU_Alert(@"Failed to convert URL to file path!");
1216 return;
1217 }
1218
1219 Error *err = NULL;
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001220 qmp_blockdev_change_medium(true,
1221 [drive cStringUsingEncoding:
Max Reitz24fb4132015-11-06 16:27:06 +01001222 NSASCIIStringEncoding],
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001223 false, NULL,
Max Reitz24fb4132015-11-06 16:27:06 +01001224 [file cStringUsingEncoding:
1225 NSASCIIStringEncoding],
1226 true, "raw",
Max Reitz39ff43d2015-11-11 04:49:44 +01001227 false, 0,
Max Reitz24fb4132015-11-06 16:27:06 +01001228 &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001229 handleAnyDeviceErrors(err);
1230 }
1231}
1232
John Arbuckled9bc14f2015-09-25 23:13:59 +01001233/* Verifies if the user really wants to quit */
1234- (BOOL)verifyQuit
1235{
1236 NSAlert *alert = [NSAlert new];
1237 [alert autorelease];
1238 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1239 [alert addButtonWithTitle: @"Cancel"];
1240 [alert addButtonWithTitle: @"Quit"];
1241 if([alert runModal] == NSAlertSecondButtonReturn) {
1242 return YES;
1243 } else {
1244 return NO;
1245 }
1246}
1247
Programmingkid9e8204b2016-08-15 22:11:06 -04001248/* The action method for the About menu item */
1249- (IBAction) do_about_menu_item: (id) sender
1250{
1251 [about_window makeKeyAndOrderFront: nil];
1252}
1253
1254/* Create and display the about dialog */
1255- (void)make_about_window
1256{
1257 /* Make the window */
1258 int x = 0, y = 0, about_width = 400, about_height = 200;
1259 NSRect window_rect = NSMakeRect(x, y, about_width, about_height);
1260 about_window = [[NSWindow alloc] initWithContentRect:window_rect
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001261 styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
1262 NSWindowStyleMaskMiniaturizable
Programmingkid9e8204b2016-08-15 22:11:06 -04001263 backing:NSBackingStoreBuffered
1264 defer:NO];
1265 [about_window setTitle: @"About"];
1266 [about_window setReleasedWhenClosed: NO];
1267 [about_window center];
1268 NSView *superView = [about_window contentView];
1269
1270 /* Create the dimensions of the picture */
1271 int picture_width = 80, picture_height = 80;
1272 x = (about_width - picture_width)/2;
1273 y = about_height - picture_height - 10;
1274 NSRect picture_rect = NSMakeRect(x, y, picture_width, picture_height);
1275
1276 /* Get the path to the QEMU binary */
1277 NSString *binary_name = [NSString stringWithCString: gArgv[0]
1278 encoding: NSASCIIStringEncoding];
1279 binary_name = [binary_name lastPathComponent];
1280 NSString *program_path = [[NSString alloc] initWithFormat: @"%@/%@",
1281 [[NSBundle mainBundle] bundlePath], binary_name];
1282
1283 /* Make the picture of QEMU */
1284 NSImageView *picture_view = [[NSImageView alloc] initWithFrame:
1285 picture_rect];
1286 NSImage *qemu_image = [[NSWorkspace sharedWorkspace] iconForFile:
1287 program_path];
1288 [picture_view setImage: qemu_image];
1289 [picture_view setImageScaling: NSImageScaleProportionallyUpOrDown];
1290 [superView addSubview: picture_view];
1291
1292 /* Make the name label */
1293 x = 0;
1294 y = y - 25;
1295 int name_width = about_width, name_height = 20;
1296 NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
1297 NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
1298 [name_label setEditable: NO];
1299 [name_label setBezeled: NO];
1300 [name_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001301 [name_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001302 NSString *qemu_name = [[NSString alloc] initWithCString: gArgv[0]
1303 encoding: NSASCIIStringEncoding];
1304 qemu_name = [qemu_name lastPathComponent];
1305 [name_label setStringValue: qemu_name];
1306 [superView addSubview: name_label];
1307
1308 /* Set the version label's attributes */
1309 x = 0;
1310 y = 50;
1311 int version_width = about_width, version_height = 20;
1312 NSRect version_rect = NSMakeRect(x, y, version_width, version_height);
1313 NSTextField *version_label = [[NSTextField alloc] initWithFrame:
1314 version_rect];
1315 [version_label setEditable: NO];
1316 [version_label setBezeled: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001317 [version_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001318 [version_label setDrawsBackground: NO];
1319
1320 /* Create the version string*/
1321 NSString *version_string;
1322 version_string = [[NSString alloc] initWithFormat:
1323 @"QEMU emulator version %s%s", QEMU_VERSION, QEMU_PKGVERSION];
1324 [version_label setStringValue: version_string];
1325 [superView addSubview: version_label];
1326
1327 /* Make copyright label */
1328 x = 0;
1329 y = 35;
1330 int copyright_width = about_width, copyright_height = 20;
1331 NSRect copyright_rect = NSMakeRect(x, y, copyright_width, copyright_height);
1332 NSTextField *copyright_label = [[NSTextField alloc] initWithFrame:
1333 copyright_rect];
1334 [copyright_label setEditable: NO];
1335 [copyright_label setBezeled: NO];
1336 [copyright_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001337 [copyright_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001338 [copyright_label setStringValue: [NSString stringWithFormat: @"%s",
1339 QEMU_COPYRIGHT]];
1340 [superView addSubview: copyright_label];
1341}
1342
John Arbucklee47ec1a2017-06-13 23:17:38 -04001343/* Used by the Speed menu items */
1344- (void)adjustSpeed:(id)sender
1345{
1346 int throttle_pct; /* throttle percentage */
1347 NSMenu *menu;
1348
1349 menu = [sender menu];
1350 if (menu != nil)
1351 {
1352 /* Unselect the currently selected item */
1353 for (NSMenuItem *item in [menu itemArray]) {
1354 if (item.state == NSOnState) {
1355 [item setState: NSOffState];
1356 break;
1357 }
1358 }
1359 }
1360
1361 // check the menu item
1362 [sender setState: NSOnState];
1363
1364 // get the throttle percentage
1365 throttle_pct = [sender tag];
1366
1367 cpu_throttle_set(throttle_pct);
1368 COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
1369}
1370
Programmingkidb4c6a112015-05-19 09:11:18 +01001371@end
bellard5b0753e2005-03-01 21:37:28 +00001372
thsc304f7e2008-01-22 23:25:15 +00001373
thsc304f7e2008-01-22 23:25:15 +00001374int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +00001375
thsc304f7e2008-01-22 23:25:15 +00001376 gArgc = argc;
1377 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +01001378 int i;
1379
1380 /* In case we don't need to display a window, let's not do that */
1381 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001382 const char *opt = argv[i];
1383
1384 if (opt[0] == '-') {
1385 /* Treat --foo the same as -foo. */
1386 if (opt[1] == '-') {
1387 opt++;
1388 }
Alexandre Raymond98514842011-05-29 18:22:49 -04001389 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
1390 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001391 !strcmp(opt, "-nographic") ||
1392 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001393 !strcmp(opt, "-curses") ||
Rainer Müllerb12a84c2015-09-09 16:08:30 +02001394 !strcmp(opt, "-display") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001395 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +02001396 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001397 }
Andreas Färberf4918802009-12-13 02:11:44 +01001398 }
1399 }
ths3b46e622007-09-17 08:09:54 +00001400
thsc304f7e2008-01-22 23:25:15 +00001401 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +00001402
Peter Maydell42a5dfe2013-04-22 10:29:47 +00001403 // Pull this console process up to being a fully-fledged graphical
1404 // app with a menubar and Dock icon
1405 ProcessSerialNumber psn = { 0, kCurrentProcess };
1406 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1407
1408 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +00001409
thsc304f7e2008-01-22 23:25:15 +00001410 // Add menus
1411 NSMenu *menu;
1412 NSMenuItem *menuItem;
1413
bellard5b0753e2005-03-01 21:37:28 +00001414 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +00001415
thsc304f7e2008-01-22 23:25:15 +00001416 // Application menu
1417 menu = [[NSMenu alloc] initWithTitle:@""];
Programmingkid9e8204b2016-08-15 22:11:06 -04001418 [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU
thsc304f7e2008-01-22 23:25:15 +00001419 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1420 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1421 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001422 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
thsc304f7e2008-01-22 23:25:15 +00001423 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1424 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1425 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1426 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1427 [menuItem setSubmenu:menu];
1428 [[NSApp mainMenu] addItem:menuItem];
1429 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001430
John Arbuckle8524f1c2015-06-19 10:53:27 +01001431 // Machine menu
1432 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1433 [menu setAutoenablesItems: NO];
1434 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1435 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1436 [menu addItem: menuItem];
1437 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001438 [menu addItem: [NSMenuItem separatorItem]];
1439 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1440 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001441 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1442 [menuItem setSubmenu:menu];
1443 [[NSApp mainMenu] addItem:menuItem];
1444
thsc304f7e2008-01-22 23:25:15 +00001445 // View menu
1446 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001447 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1448 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001449 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1450 [menuItem setSubmenu:menu];
1451 [[NSApp mainMenu] addItem:menuItem];
1452
John Arbucklee47ec1a2017-06-13 23:17:38 -04001453 // Speed menu
1454 menu = [[NSMenu alloc] initWithTitle:@"Speed"];
1455
1456 // Add the rest of the Speed menu items
1457 int p, percentage, throttle_pct;
1458 for (p = 10; p >= 0; p--)
1459 {
1460 percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
1461
1462 menuItem = [[[NSMenuItem alloc]
1463 initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
1464
1465 if (percentage == 100) {
1466 [menuItem setState: NSOnState];
1467 }
1468
1469 /* Calculate the throttle percentage */
1470 throttle_pct = -1 * percentage + 100;
1471
1472 [menuItem setTag: throttle_pct];
1473 [menu addItem: menuItem];
1474 }
1475 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
1476 [menuItem setSubmenu:menu];
1477 [[NSApp mainMenu] addItem:menuItem];
1478
thsc304f7e2008-01-22 23:25:15 +00001479 // Window menu
1480 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1481 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1482 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1483 [menuItem setSubmenu:menu];
1484 [[NSApp mainMenu] addItem:menuItem];
1485 [NSApp setWindowsMenu:menu];
1486
1487 // Help menu
1488 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1489 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
thsc304f7e2008-01-22 23:25:15 +00001490 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1491 [menuItem setSubmenu:menu];
1492 [[NSApp mainMenu] addItem:menuItem];
1493
1494 // Create an Application controller
1495 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1496 [NSApp setDelegate:appController];
1497
1498 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +00001499 [NSApp run];
ths3b46e622007-09-17 08:09:54 +00001500
thsc304f7e2008-01-22 23:25:15 +00001501 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001502 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001503
bellard5b0753e2005-03-01 21:37:28 +00001504 return 0;
1505}
thsc304f7e2008-01-22 23:25:15 +00001506
1507
1508
1509#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001510static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001511 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001512{
Peter Maydell6e657e62013-04-22 10:29:46 +00001513 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1514
thsc304f7e2008-01-22 23:25:15 +00001515 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1516
1517 NSRect rect;
1518 if ([cocoaView cdx] == 1.0) {
1519 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1520 } else {
1521 rect = NSMakeRect(
1522 x * [cocoaView cdx],
1523 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1524 w * [cocoaView cdx],
1525 h * [cocoaView cdy]);
1526 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001527 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001528
1529 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001530}
1531
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001532static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001533 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001534{
Peter Maydell6e657e62013-04-22 10:29:46 +00001535 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001536
Peter Maydell6e657e62013-04-22 10:29:46 +00001537 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001538 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001539 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001540}
1541
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001542static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001543{
Peter Maydell6e657e62013-04-22 10:29:46 +00001544 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1545
thsc304f7e2008-01-22 23:25:15 +00001546 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
John Arbuckle468a8952015-10-13 21:51:18 +01001547 graphic_hw_update(NULL);
thsc304f7e2008-01-22 23:25:15 +00001548
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001549 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001550 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001551 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001552 [cocoaView ungrabMouse];
1553 }
1554 }
1555 [cocoaView setAbsoluteEnabled:YES];
1556 }
1557
1558 NSDate *distantPast;
1559 NSEvent *event;
1560 distantPast = [NSDate distantPast];
1561 do {
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001562 event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:distantPast
thsc304f7e2008-01-22 23:25:15 +00001563 inMode: NSDefaultRunLoopMode dequeue:YES];
1564 if (event != nil) {
1565 [cocoaView handleEvent:event];
1566 }
1567 } while(event != nil);
Peter Maydell6e657e62013-04-22 10:29:46 +00001568 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001569}
1570
1571static void cocoa_cleanup(void)
1572{
1573 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001574 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001575}
1576
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001577static const DisplayChangeListenerOps dcl_ops = {
1578 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001579 .dpy_gfx_update = cocoa_update,
1580 .dpy_gfx_switch = cocoa_switch,
1581 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001582};
1583
Programmingkidb4c6a112015-05-19 09:11:18 +01001584/* Returns a name for a given console */
1585static NSString * getConsoleName(QemuConsole * console)
1586{
1587 return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
1588}
1589
1590/* Add an entry to the View menu for each console */
1591static void add_console_menu_entries(void)
1592{
1593 NSMenu *menu;
1594 NSMenuItem *menuItem;
1595 int index = 0;
1596
1597 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1598
1599 [menu addItem:[NSMenuItem separatorItem]];
1600
1601 while (qemu_console_lookup_by_index(index) != NULL) {
1602 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1603 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1604 [menuItem setTag: index];
1605 [menu addItem: menuItem];
1606 index++;
1607 }
1608}
1609
John Arbuckle693a3e02015-06-19 10:53:27 +01001610/* Make menu items for all removable devices.
1611 * Each device is given an 'Eject' and 'Change' menu item.
1612 */
John Arbucklea7940ec2015-10-13 21:51:18 +01001613static void addRemovableDevicesMenuItems(void)
John Arbuckle693a3e02015-06-19 10:53:27 +01001614{
1615 NSMenu *menu;
1616 NSMenuItem *menuItem;
1617 BlockInfoList *currentDevice, *pointerToFree;
1618 NSString *deviceName;
1619
1620 currentDevice = qmp_query_block(NULL);
1621 pointerToFree = currentDevice;
1622 if(currentDevice == NULL) {
1623 NSBeep();
1624 QEMU_Alert(@"Failed to query for block devices!");
1625 return;
1626 }
1627
1628 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1629
1630 // Add a separator between related groups of menu items
1631 [menu addItem:[NSMenuItem separatorItem]];
1632
1633 // Set the attributes to the "Removable Media" menu item
1634 NSString *titleString = @"Removable Media";
1635 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1636 NSColor *newColor = [NSColor blackColor];
1637 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1638 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1639 traits:NSBoldFontMask|NSItalicFontMask
1640 weight:0
1641 size:14];
1642 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1643 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1644 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1645
1646 // Add the "Removable Media" menu item
1647 menuItem = [NSMenuItem new];
1648 [menuItem setAttributedTitle: attString];
1649 [menuItem setEnabled: NO];
1650 [menu addItem: menuItem];
1651
Stefan Weilcb8d4c82016-03-23 15:59:57 +01001652 /* Loop through all the block devices in the emulator */
John Arbuckle693a3e02015-06-19 10:53:27 +01001653 while (currentDevice) {
1654 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1655
1656 if(currentDevice->value->removable) {
1657 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1658 action: @selector(changeDeviceMedia:)
1659 keyEquivalent: @""];
1660 [menu addItem: menuItem];
1661 [menuItem setRepresentedObject: deviceName];
1662 [menuItem autorelease];
1663
1664 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1665 action: @selector(ejectDeviceMedia:)
1666 keyEquivalent: @""];
1667 [menu addItem: menuItem];
1668 [menuItem setRepresentedObject: deviceName];
1669 [menuItem autorelease];
1670 }
1671 currentDevice = currentDevice->next;
1672 }
1673 qapi_free_BlockInfoList(pointerToFree);
1674}
1675
thsc304f7e2008-01-22 23:25:15 +00001676void cocoa_display_init(DisplayState *ds, int full_screen)
1677{
1678 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1679
Programmingkid43227af2015-05-19 09:11:17 +01001680 /* if fullscreen mode is to be used */
1681 if (full_screen == true) {
1682 [NSApp activateIgnoringOtherApps: YES];
1683 [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
1684 }
1685
Blue Swirl58a06672011-08-21 18:42:08 +00001686 dcl = g_malloc0(sizeof(DisplayChangeListener));
1687
aliguori9794f742009-03-04 19:25:22 +00001688 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001689 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001690 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001691
1692 // register cleanup function
1693 atexit(cocoa_cleanup);
Programmingkidb4c6a112015-05-19 09:11:18 +01001694
1695 /* At this point QEMU has created all the consoles, so we can add View
1696 * menu entries for them.
1697 */
1698 add_console_menu_entries();
John Arbuckle693a3e02015-06-19 10:53:27 +01001699
1700 /* Give all removable devices a menu item.
1701 * Has to be called after QEMU has started to
1702 * find out what removable devices it has.
1703 */
1704 addRemovableDevicesMenuItems();
thsc304f7e2008-01-22 23:25:15 +00001705}