blob: ddc058e76efcfd4a6e43d022feee241ab7509f41 [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"
Markus Armbrustere688df62018-02-01 12:18:31 +010034#include "qapi/error.h"
Markus Armbruster16bf5232018-12-20 09:45:59 +010035#include "qapi/qapi-commands-block.h"
36#include "qapi/qapi-commands-misc.h"
John Arbuckle693a3e02015-06-19 10:53:27 +010037#include "sysemu/blockdev.h"
Programmingkid9e8204b2016-08-15 22:11:06 -040038#include "qemu-version.h"
John Arbuckleaaac7142016-03-23 14:26:18 +000039#include <Carbon/Carbon.h>
John Arbucklee47ec1a2017-06-13 23:17:38 -040040#include "qom/cpu.h"
bellardda4dbf72005-03-02 22:22:43 +000041
Andreas Färber44e4c0b2009-12-13 00:45:40 +010042#ifndef MAC_OS_X_VERSION_10_5
43#define MAC_OS_X_VERSION_10_5 1050
44#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000045#ifndef MAC_OS_X_VERSION_10_6
46#define MAC_OS_X_VERSION_10_6 1060
47#endif
Peter Maydellb5725382018-05-29 19:15:23 +010048#ifndef MAC_OS_X_VERSION_10_9
49#define MAC_OS_X_VERSION_10_9 1090
50#endif
Peter Maydell81801ae2015-05-19 09:11:18 +010051#ifndef MAC_OS_X_VERSION_10_10
52#define MAC_OS_X_VERSION_10_10 101000
53#endif
Brendan Shanks4ba967a2017-04-24 23:29:52 -070054#ifndef MAC_OS_X_VERSION_10_12
55#define MAC_OS_X_VERSION_10_12 101200
56#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010057
Brendan Shanks4ba967a2017-04-24 23:29:52 -070058/* macOS 10.12 deprecated many constants, #define the new names for older SDKs */
59#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
60#define NSEventMaskAny NSAnyEventMask
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -070061#define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
62#define NSEventModifierFlagShift NSShiftKeyMask
Brendan Shanks4ba967a2017-04-24 23:29:52 -070063#define NSEventModifierFlagCommand NSCommandKeyMask
64#define NSEventModifierFlagControl NSControlKeyMask
65#define NSEventModifierFlagOption NSAlternateKeyMask
66#define NSEventTypeFlagsChanged NSFlagsChanged
67#define NSEventTypeKeyUp NSKeyUp
68#define NSEventTypeKeyDown NSKeyDown
69#define NSEventTypeMouseMoved NSMouseMoved
70#define NSEventTypeLeftMouseDown NSLeftMouseDown
71#define NSEventTypeRightMouseDown NSRightMouseDown
72#define NSEventTypeOtherMouseDown NSOtherMouseDown
73#define NSEventTypeLeftMouseDragged NSLeftMouseDragged
74#define NSEventTypeRightMouseDragged NSRightMouseDragged
75#define NSEventTypeOtherMouseDragged NSOtherMouseDragged
76#define NSEventTypeLeftMouseUp NSLeftMouseUp
77#define NSEventTypeRightMouseUp NSRightMouseUp
78#define NSEventTypeOtherMouseUp NSOtherMouseUp
79#define NSEventTypeScrollWheel NSScrollWheel
80#define NSTextAlignmentCenter NSCenterTextAlignment
81#define NSWindowStyleMaskBorderless NSBorderlessWindowMask
82#define NSWindowStyleMaskClosable NSClosableWindowMask
83#define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
84#define NSWindowStyleMaskTitled NSTitledWindowMask
85#endif
Peter Maydellb5725382018-05-29 19:15:23 +010086/* 10.13 deprecates NSFileHandlingPanelOKButton in favour of
87 * NSModalResponseOK, which was introduced in 10.9. Define
88 * it for older versions.
89 */
90#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9
91#define NSModalResponseOK NSFileHandlingPanelOKButton
92#endif
bellard5b0753e2005-03-01 21:37:28 +000093
thsc304f7e2008-01-22 23:25:15 +000094//#define DEBUG
95
96#ifdef DEBUG
97#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
98#else
99#define COCOA_DEBUG(...) ((void) 0)
100#endif
101
102#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +0000103
104typedef struct {
105 int width;
106 int height;
107 int bitsPerComponent;
108 int bitsPerPixel;
109} QEMUScreen;
110
Programmingkid9e8204b2016-08-15 22:11:06 -0400111NSWindow *normalWindow, *about_window;
aliguori9794f742009-03-04 19:25:22 +0000112static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100113static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +0000114
115int gArgc;
116char **gArgv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100117bool stretch_video;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100118NSTextField *pauseLabel;
John Arbuckle693a3e02015-06-19 10:53:27 +0100119NSArray * supportedImageFileTypes;
bellard5b0753e2005-03-01 21:37:28 +0000120
John Arbuckleaaac7142016-03-23 14:26:18 +0000121// Mac to QKeyCode conversion
122const int mac_to_qkeycode_map[] = {
123 [kVK_ANSI_A] = Q_KEY_CODE_A,
124 [kVK_ANSI_B] = Q_KEY_CODE_B,
125 [kVK_ANSI_C] = Q_KEY_CODE_C,
126 [kVK_ANSI_D] = Q_KEY_CODE_D,
127 [kVK_ANSI_E] = Q_KEY_CODE_E,
128 [kVK_ANSI_F] = Q_KEY_CODE_F,
129 [kVK_ANSI_G] = Q_KEY_CODE_G,
130 [kVK_ANSI_H] = Q_KEY_CODE_H,
131 [kVK_ANSI_I] = Q_KEY_CODE_I,
132 [kVK_ANSI_J] = Q_KEY_CODE_J,
133 [kVK_ANSI_K] = Q_KEY_CODE_K,
134 [kVK_ANSI_L] = Q_KEY_CODE_L,
135 [kVK_ANSI_M] = Q_KEY_CODE_M,
136 [kVK_ANSI_N] = Q_KEY_CODE_N,
137 [kVK_ANSI_O] = Q_KEY_CODE_O,
138 [kVK_ANSI_P] = Q_KEY_CODE_P,
139 [kVK_ANSI_Q] = Q_KEY_CODE_Q,
140 [kVK_ANSI_R] = Q_KEY_CODE_R,
141 [kVK_ANSI_S] = Q_KEY_CODE_S,
142 [kVK_ANSI_T] = Q_KEY_CODE_T,
143 [kVK_ANSI_U] = Q_KEY_CODE_U,
144 [kVK_ANSI_V] = Q_KEY_CODE_V,
145 [kVK_ANSI_W] = Q_KEY_CODE_W,
146 [kVK_ANSI_X] = Q_KEY_CODE_X,
147 [kVK_ANSI_Y] = Q_KEY_CODE_Y,
148 [kVK_ANSI_Z] = Q_KEY_CODE_Z,
ths3b46e622007-09-17 08:09:54 +0000149
John Arbuckleaaac7142016-03-23 14:26:18 +0000150 [kVK_ANSI_0] = Q_KEY_CODE_0,
151 [kVK_ANSI_1] = Q_KEY_CODE_1,
152 [kVK_ANSI_2] = Q_KEY_CODE_2,
153 [kVK_ANSI_3] = Q_KEY_CODE_3,
154 [kVK_ANSI_4] = Q_KEY_CODE_4,
155 [kVK_ANSI_5] = Q_KEY_CODE_5,
156 [kVK_ANSI_6] = Q_KEY_CODE_6,
157 [kVK_ANSI_7] = Q_KEY_CODE_7,
158 [kVK_ANSI_8] = Q_KEY_CODE_8,
159 [kVK_ANSI_9] = Q_KEY_CODE_9,
160
161 [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
162 [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
163 [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
164 [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
165 [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
166 [kVK_Tab] = Q_KEY_CODE_TAB,
167 [kVK_Return] = Q_KEY_CODE_RET,
168 [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
169 [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
170 [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
171 [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
172 [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
173 [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
174 [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
175 [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
176 [kVK_Shift] = Q_KEY_CODE_SHIFT,
177 [kVK_RightShift] = Q_KEY_CODE_SHIFT_R,
178 [kVK_Control] = Q_KEY_CODE_CTRL,
179 [kVK_RightControl] = Q_KEY_CODE_CTRL_R,
180 [kVK_Option] = Q_KEY_CODE_ALT,
181 [kVK_RightOption] = Q_KEY_CODE_ALT_R,
182 [kVK_Command] = Q_KEY_CODE_META_L,
183 [0x36] = Q_KEY_CODE_META_R, /* There is no kVK_RightCommand */
184 [kVK_Space] = Q_KEY_CODE_SPC,
185
186 [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
187 [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
188 [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
189 [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
190 [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
191 [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
192 [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
193 [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
194 [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
195 [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
196 [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
197 [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
198 [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
199 [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
200 [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
201 [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
202 [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
203 [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
204
205 [kVK_UpArrow] = Q_KEY_CODE_UP,
206 [kVK_DownArrow] = Q_KEY_CODE_DOWN,
207 [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
208 [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
209
210 [kVK_Help] = Q_KEY_CODE_INSERT,
211 [kVK_Home] = Q_KEY_CODE_HOME,
212 [kVK_PageUp] = Q_KEY_CODE_PGUP,
213 [kVK_PageDown] = Q_KEY_CODE_PGDN,
214 [kVK_End] = Q_KEY_CODE_END,
215 [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
216
217 [kVK_Escape] = Q_KEY_CODE_ESC,
218
219 /* The Power key can't be used directly because the operating system uses
220 * it. This key can be emulated by using it in place of another key such as
221 * F1. Don't forget to disable the real key binding.
222 */
223 /* [kVK_F1] = Q_KEY_CODE_POWER, */
224
225 [kVK_F1] = Q_KEY_CODE_F1,
226 [kVK_F2] = Q_KEY_CODE_F2,
227 [kVK_F3] = Q_KEY_CODE_F3,
228 [kVK_F4] = Q_KEY_CODE_F4,
229 [kVK_F5] = Q_KEY_CODE_F5,
230 [kVK_F6] = Q_KEY_CODE_F6,
231 [kVK_F7] = Q_KEY_CODE_F7,
232 [kVK_F8] = Q_KEY_CODE_F8,
233 [kVK_F9] = Q_KEY_CODE_F9,
234 [kVK_F10] = Q_KEY_CODE_F10,
235 [kVK_F11] = Q_KEY_CODE_F11,
236 [kVK_F12] = Q_KEY_CODE_F12,
237 [kVK_F13] = Q_KEY_CODE_PRINT,
238 [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
239 [kVK_F15] = Q_KEY_CODE_PAUSE,
240
241 /*
242 * The eject and volume keys can't be used here because they are handled at
243 * a lower level than what an Application can see.
244 */
bellard5b0753e2005-03-01 21:37:28 +0000245};
246
Andreas Färber77047bb2009-12-13 00:55:53 +0100247static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000248{
John Arbuckleaaac7142016-03-23 14:26:18 +0000249 if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000250 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000251 return 0;
252 }
John Arbuckleaaac7142016-03-23 14:26:18 +0000253 return mac_to_qkeycode_map[keycode];
bellard5b0753e2005-03-01 21:37:28 +0000254}
255
John Arbuckle693a3e02015-06-19 10:53:27 +0100256/* Displays an alert dialog box with the specified message */
257static void QEMU_Alert(NSString *message)
258{
259 NSAlert *alert;
260 alert = [NSAlert new];
261 [alert setMessageText: message];
262 [alert runModal];
263}
thsc304f7e2008-01-22 23:25:15 +0000264
John Arbuckle693a3e02015-06-19 10:53:27 +0100265/* Handles any errors that happen with a device transaction */
266static void handleAnyDeviceErrors(Error * err)
267{
268 if (err) {
269 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
270 encoding: NSASCIIStringEncoding]);
271 error_free(err);
272 }
273}
thsc304f7e2008-01-22 23:25:15 +0000274
bellard5b0753e2005-03-01 21:37:28 +0000275/*
276 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000277 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000278 ------------------------------------------------------
279*/
thsc304f7e2008-01-22 23:25:15 +0000280@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000281{
thsc304f7e2008-01-22 23:25:15 +0000282 QEMUScreen screen;
283 NSWindow *fullScreenWindow;
284 float cx,cy,cw,ch,cdx,cdy;
285 CGDataProviderRef dataProviderRef;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700286 BOOL modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000287 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000288 BOOL isFullscreen;
289 BOOL isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100290 BOOL isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000291}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100292- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000293- (void) grabMouse;
294- (void) ungrabMouse;
295- (void) toggleFullScreen:(id)sender;
John Arbuckle9c3a4182017-11-07 10:14:14 +0000296- (void) handleMonitorInput:(NSEvent *)event;
thsc304f7e2008-01-22 23:25:15 +0000297- (void) handleEvent:(NSEvent *)event;
298- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100299/* The state surrounding mouse grabbing is potentially confusing.
300 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
301 * pointing device an absolute-position one?"], but is only updated on
302 * next refresh.
303 * isMouseGrabbed tracks whether GUI events are directed to the guest;
304 * it controls whether special keys like Cmd get sent to the guest,
305 * and whether we capture the mouse when in non-absolute mode.
306 * isMouseDeassociated tracks whether we've told MacOSX to disassociate
307 * the mouse and mouse cursor position by calling
308 * CGAssociateMouseAndMouseCursorPosition(FALSE)
309 * (which basically happens if we grab in non-absolute mode).
310 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000311- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000312- (BOOL) isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100313- (BOOL) isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000314- (float) cdx;
315- (float) cdy;
316- (QEMUScreen) gscreen;
John Arbuckle3b178b72015-09-25 23:14:00 +0100317- (void) raiseAllKeys;
thsc304f7e2008-01-22 23:25:15 +0000318@end
ths3b46e622007-09-17 08:09:54 +0000319
Andreas Färber7fee1992011-06-09 20:53:32 +0200320QemuCocoaView *cocoaView;
321
thsc304f7e2008-01-22 23:25:15 +0000322@implementation QemuCocoaView
323- (id)initWithFrame:(NSRect)frameRect
324{
325 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000326
thsc304f7e2008-01-22 23:25:15 +0000327 self = [super initWithFrame:frameRect];
328 if (self) {
pbrook95219892006-04-09 01:06:34 +0000329
thsc304f7e2008-01-22 23:25:15 +0000330 screen.bitsPerComponent = 8;
331 screen.bitsPerPixel = 32;
332 screen.width = frameRect.size.width;
333 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000334
thsc304f7e2008-01-22 23:25:15 +0000335 }
336 return self;
337}
bellard7c206a72005-12-18 19:18:45 +0000338
thsc304f7e2008-01-22 23:25:15 +0000339- (void) dealloc
340{
341 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000342
thsc304f7e2008-01-22 23:25:15 +0000343 if (dataProviderRef)
344 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000345
thsc304f7e2008-01-22 23:25:15 +0000346 [super dealloc];
347}
bellard5cbfcd02006-06-14 15:53:24 +0000348
Andreas Färberd50f71d2009-12-13 02:03:33 +0100349- (BOOL) isOpaque
350{
351 return YES;
352}
353
Peter Maydell5dd45be2014-06-23 10:35:23 +0100354- (BOOL) screenContainsPoint:(NSPoint) p
355{
356 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
357}
358
Peter Maydell13aefd32014-06-23 10:35:25 +0100359- (void) hideCursor
360{
361 if (!cursor_hide) {
362 return;
363 }
364 [NSCursor hide];
365}
366
367- (void) unhideCursor
368{
369 if (!cursor_hide) {
370 return;
371 }
372 [NSCursor unhide];
373}
374
thsc304f7e2008-01-22 23:25:15 +0000375- (void) drawRect:(NSRect) rect
376{
377 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000378
thsc304f7e2008-01-22 23:25:15 +0000379 // get CoreGraphic context
380 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
381 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
382 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000383
thsc304f7e2008-01-22 23:25:15 +0000384 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000385 if (!dataProviderRef) {
386 // Draw request before any guest device has set up a framebuffer:
387 // just draw an opaque black rectangle
388 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
389 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
390 } else {
thsc304f7e2008-01-22 23:25:15 +0000391 CGImageRef imageRef = CGImageCreate(
392 screen.width, //width
393 screen.height, //height
394 screen.bitsPerComponent, //bitsPerComponent
395 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000396 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100397#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000398 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000399 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000400#else
401 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
402 kCGImageAlphaNoneSkipFirst, //bitmapInfo
403#endif
404 dataProviderRef, //provider
405 NULL, //decode
406 0, //interpolate
407 kCGRenderingIntentDefault //intent
408 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100409 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
410 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100411 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100412 int i;
413 CGImageRef clipImageRef;
414 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000415
Peter Maydellb63901d2015-05-19 09:11:17 +0100416 [self getRectsBeingDrawn:&rectList count:&rectCount];
417 for (i = 0; i < rectCount; i++) {
418 clipRect.origin.x = rectList[i].origin.x / cdx;
419 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
420 clipRect.size.width = rectList[i].size.width / cdx;
421 clipRect.size.height = rectList[i].size.height / cdy;
422 clipImageRef = CGImageCreateWithImageInRect(
423 imageRef,
424 clipRect
425 );
426 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
427 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000428 }
thsc304f7e2008-01-22 23:25:15 +0000429 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000430 }
431}
432
thsc304f7e2008-01-22 23:25:15 +0000433- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000434{
thsc304f7e2008-01-22 23:25:15 +0000435 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000436
thsc304f7e2008-01-22 23:25:15 +0000437 if (isFullscreen) {
438 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
439 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100440
441 /* stretches video, but keeps same aspect ratio */
442 if (stretch_video == true) {
443 /* use smallest stretch value - prevents clipping on sides */
444 if (MIN(cdx, cdy) == cdx) {
445 cdy = cdx;
446 } else {
447 cdx = cdy;
448 }
449 } else { /* No stretching */
450 cdx = cdy = 1;
451 }
thsc304f7e2008-01-22 23:25:15 +0000452 cw = screen.width * cdx;
453 ch = screen.height * cdy;
454 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
455 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
456 } else {
457 cx = 0;
458 cy = 0;
459 cw = screen.width;
460 ch = screen.height;
461 cdx = 1.0;
462 cdy = 1.0;
463 }
bellard5b0753e2005-03-01 21:37:28 +0000464}
465
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100466- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000467{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100468 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000469
Peter Maydell8510d912013-03-18 20:28:21 +0000470 int w = surface_width(surface);
471 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100472 /* cdx == 0 means this is our very first surface, in which case we need
473 * to recalculate the content dimensions even if it happens to be the size
474 * of the initial empty window.
475 */
476 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000477
478 int oldh = screen.height;
479 if (isResize) {
480 // Resize before we trigger the redraw, or we'll redraw at the wrong size
481 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
482 screen.width = w;
483 screen.height = h;
484 [self setContentDimensions];
485 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
486 }
Peter Maydell8510d912013-03-18 20:28:21 +0000487
thsc304f7e2008-01-22 23:25:15 +0000488 // update screenBuffer
489 if (dataProviderRef)
490 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000491
aliguori9794f742009-03-04 19:25:22 +0000492 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000493 screen.bitsPerPixel = surface_bits_per_pixel(surface);
494 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000495
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100496 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000497
498 // update windows
499 if (isFullscreen) {
500 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000501 [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 +0000502 } else {
503 if (qemu_name)
504 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000505 [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 +0000506 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000507
508 if (isResize) {
509 [normalWindow center];
510 }
thsc304f7e2008-01-22 23:25:15 +0000511}
512
513- (void) toggleFullScreen:(id)sender
514{
515 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
516
517 if (isFullscreen) { // switch from fullscreen to desktop
518 isFullscreen = FALSE;
519 [self ungrabMouse];
520 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000521 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
522 [self exitFullScreenModeWithOptions:nil];
523 } else {
thsc304f7e2008-01-22 23:25:15 +0000524 [fullScreenWindow close];
525 [normalWindow setContentView: self];
526 [normalWindow makeKeyAndOrderFront: self];
527 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000528 }
thsc304f7e2008-01-22 23:25:15 +0000529 } else { // switch from desktop to fullscreen
530 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100531 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000532 [self grabMouse];
533 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000534 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
535 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
536 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
537 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
538 nil]];
539 } else {
thsc304f7e2008-01-22 23:25:15 +0000540 [NSMenu setMenuBarVisible:NO];
541 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700542 styleMask:NSWindowStyleMaskBorderless
thsc304f7e2008-01-22 23:25:15 +0000543 backing:NSBackingStoreBuffered
544 defer:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100545 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
thsc304f7e2008-01-22 23:25:15 +0000546 [fullScreenWindow setHasShadow:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100547 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
548 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
549 [[fullScreenWindow contentView] addSubview: self];
thsc304f7e2008-01-22 23:25:15 +0000550 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000551 }
thsc304f7e2008-01-22 23:25:15 +0000552 }
553}
554
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700555- (void) toggleModifier: (int)keycode {
556 // Toggle the stored state.
557 modifiers_state[keycode] = !modifiers_state[keycode];
558 // Send a keyup or keydown depending on the state.
559 qemu_input_event_send_key_qcode(dcl->con, keycode, modifiers_state[keycode]);
560}
561
562- (void) toggleStatefulModifier: (int)keycode {
563 // Toggle the stored state.
564 modifiers_state[keycode] = !modifiers_state[keycode];
565 // Generate keydown and keyup.
566 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
567 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
568}
569
John Arbuckle9c3a4182017-11-07 10:14:14 +0000570// Does the work of sending input to the monitor
571- (void) handleMonitorInput:(NSEvent *)event
572{
573 int keysym = 0;
574 int control_key = 0;
575
576 // if the control key is down
577 if ([event modifierFlags] & NSEventModifierFlagControl) {
578 control_key = 1;
579 }
580
581 /* translates Macintosh keycodes to QEMU's keysym */
582
583 int without_control_translation[] = {
584 [0 ... 0xff] = 0, // invalid key
585
586 [kVK_UpArrow] = QEMU_KEY_UP,
587 [kVK_DownArrow] = QEMU_KEY_DOWN,
588 [kVK_RightArrow] = QEMU_KEY_RIGHT,
589 [kVK_LeftArrow] = QEMU_KEY_LEFT,
590 [kVK_Home] = QEMU_KEY_HOME,
591 [kVK_End] = QEMU_KEY_END,
592 [kVK_PageUp] = QEMU_KEY_PAGEUP,
593 [kVK_PageDown] = QEMU_KEY_PAGEDOWN,
594 [kVK_ForwardDelete] = QEMU_KEY_DELETE,
595 [kVK_Delete] = QEMU_KEY_BACKSPACE,
596 };
597
598 int with_control_translation[] = {
599 [0 ... 0xff] = 0, // invalid key
600
601 [kVK_UpArrow] = QEMU_KEY_CTRL_UP,
602 [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN,
603 [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT,
604 [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT,
605 [kVK_Home] = QEMU_KEY_CTRL_HOME,
606 [kVK_End] = QEMU_KEY_CTRL_END,
607 [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP,
608 [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN,
609 };
610
611 if (control_key != 0) { /* If the control key is being used */
612 if ([event keyCode] < ARRAY_SIZE(with_control_translation)) {
613 keysym = with_control_translation[[event keyCode]];
614 }
615 } else {
616 if ([event keyCode] < ARRAY_SIZE(without_control_translation)) {
617 keysym = without_control_translation[[event keyCode]];
618 }
619 }
620
621 // if not a key that needs translating
622 if (keysym == 0) {
623 NSString *ks = [event characters];
624 if ([ks length] > 0) {
625 keysym = [ks characterAtIndex:0];
626 }
627 }
628
629 if (keysym) {
630 kbd_put_keysym(keysym);
631 }
632}
633
thsc304f7e2008-01-22 23:25:15 +0000634- (void) handleEvent:(NSEvent *)event
635{
636 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
637
638 int buttons = 0;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700639 int keycode = 0;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100640 bool mouse_event = false;
John Arbuckle0c6c4392018-07-02 22:00:17 -0400641 static bool switched_to_fullscreen = false;
thsc304f7e2008-01-22 23:25:15 +0000642 NSPoint p = [event locationInWindow];
643
644 switch ([event type]) {
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700645 case NSEventTypeFlagsChanged:
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700646 if ([event keyCode] == 0) {
647 // When the Cocoa keyCode is zero that means keys should be
648 // synthesized based on the values in in the eventModifiers
649 // bitmask.
650
651 if (qemu_console_is_graphic(NULL)) {
John Arbucklee7206242017-06-28 15:37:16 -0400652 NSUInteger modifiers = [event modifierFlags];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700653
654 if (!!(modifiers & NSEventModifierFlagCapsLock) != !!modifiers_state[Q_KEY_CODE_CAPS_LOCK]) {
655 [self toggleStatefulModifier:Q_KEY_CODE_CAPS_LOCK];
656 }
657 if (!!(modifiers & NSEventModifierFlagShift) != !!modifiers_state[Q_KEY_CODE_SHIFT]) {
658 [self toggleModifier:Q_KEY_CODE_SHIFT];
659 }
660 if (!!(modifiers & NSEventModifierFlagControl) != !!modifiers_state[Q_KEY_CODE_CTRL]) {
661 [self toggleModifier:Q_KEY_CODE_CTRL];
662 }
663 if (!!(modifiers & NSEventModifierFlagOption) != !!modifiers_state[Q_KEY_CODE_ALT]) {
664 [self toggleModifier:Q_KEY_CODE_ALT];
665 }
666 if (!!(modifiers & NSEventModifierFlagCommand) != !!modifiers_state[Q_KEY_CODE_META_L]) {
667 [self toggleModifier:Q_KEY_CODE_META_L];
668 }
669 }
670 } else {
671 keycode = cocoa_keycode_to_qemu([event keyCode]);
672 }
Peter Maydell88959192013-12-08 22:59:02 +0000673
John Arbuckleaaac7142016-03-23 14:26:18 +0000674 if ((keycode == Q_KEY_CODE_META_L || keycode == Q_KEY_CODE_META_R)
675 && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000676 /* Don't pass command key changes to guest unless mouse is grabbed */
677 keycode = 0;
678 }
679
thsc304f7e2008-01-22 23:25:15 +0000680 if (keycode) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000681 // emulate caps lock and num lock keydown and keyup
682 if (keycode == Q_KEY_CODE_CAPS_LOCK ||
683 keycode == Q_KEY_CODE_NUM_LOCK) {
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700684 [self toggleStatefulModifier:keycode];
Peter Maydell68c0aa62013-04-17 09:16:35 +0000685 } else if (qemu_console_is_graphic(NULL)) {
John Arbuckle0c6c4392018-07-02 22:00:17 -0400686 if (switched_to_fullscreen) {
687 switched_to_fullscreen = false;
688 } else {
689 [self toggleModifier:keycode];
690 }
thsc304f7e2008-01-22 23:25:15 +0000691 }
692 }
693
thsc304f7e2008-01-22 23:25:15 +0000694 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700695 case NSEventTypeKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000696 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000697
Peter Maydell88959192013-12-08 22:59:02 +0000698 // forward command key combos to the host UI unless the mouse is grabbed
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700699 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
John Arbuckle0c6c4392018-07-02 22:00:17 -0400700 /*
701 * Prevent the command key from being stuck down in the guest
702 * when using Command-F to switch to full screen mode.
703 */
704 if (keycode == Q_KEY_CODE_F) {
705 switched_to_fullscreen = true;
706 }
thsc304f7e2008-01-22 23:25:15 +0000707 [NSApp sendEvent:event];
708 return;
709 }
710
711 // default
thsc304f7e2008-01-22 23:25:15 +0000712
John Arbuckle5929e362017-11-07 10:14:14 +0000713 // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700714 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
John Arbuckle5929e362017-11-07 10:14:14 +0000715 NSString *keychar = [event charactersIgnoringModifiers];
716 if ([keychar length] == 1) {
717 char key = [keychar characterAtIndex:0];
718 switch (key) {
thsc304f7e2008-01-22 23:25:15 +0000719
John Arbuckle5929e362017-11-07 10:14:14 +0000720 // enable graphic console
721 case '1' ... '9':
722 console_select(key - '0' - 1); /* ascii math */
723 return;
724
725 // release the mouse grab
726 case 'g':
727 [self ungrabMouse];
728 return;
729 }
thsc304f7e2008-01-22 23:25:15 +0000730 }
Peter Maydellef2088f2017-11-07 10:14:14 +0000731 }
thsc304f7e2008-01-22 23:25:15 +0000732
Peter Maydellef2088f2017-11-07 10:14:14 +0000733 if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000734 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000735 } else {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000736 [self handleMonitorInput: event];
thsc304f7e2008-01-22 23:25:15 +0000737 }
738 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700739 case NSEventTypeKeyUp:
thsc304f7e2008-01-22 23:25:15 +0000740 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000741
742 // don't pass the guest a spurious key-up if we treated this
743 // command-key combo as a host UI action
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700744 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell88959192013-12-08 22:59:02 +0000745 return;
746 }
747
Peter Maydell68c0aa62013-04-17 09:16:35 +0000748 if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000749 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000750 }
751 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700752 case NSEventTypeMouseMoved:
thsc304f7e2008-01-22 23:25:15 +0000753 if (isAbsoluteEnabled) {
Peter Maydell5dd45be2014-06-23 10:35:23 +0100754 if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
Peter Maydellf61c3872014-06-23 10:35:24 +0100755 if (isMouseGrabbed) {
756 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +0000757 }
758 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +0100759 if (!isMouseGrabbed) {
760 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000761 }
762 }
763 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100764 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000765 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700766 case NSEventTypeLeftMouseDown:
767 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000768 buttons |= MOUSE_EVENT_RBUTTON;
769 } else {
770 buttons |= MOUSE_EVENT_LBUTTON;
771 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100772 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000773 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700774 case NSEventTypeRightMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000775 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100776 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000777 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700778 case NSEventTypeOtherMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000779 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100780 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000781 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700782 case NSEventTypeLeftMouseDragged:
783 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000784 buttons |= MOUSE_EVENT_RBUTTON;
785 } else {
786 buttons |= MOUSE_EVENT_LBUTTON;
787 }
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 NSEventTypeRightMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000791 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100792 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000793 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700794 case NSEventTypeOtherMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000795 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100796 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000797 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700798 case NSEventTypeLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +0100799 mouse_event = true;
800 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
Programmingkid9e8204b2016-08-15 22:11:06 -0400801 if([[self window] isKeyWindow]) {
802 [self grabMouse];
803 }
thsc304f7e2008-01-22 23:25:15 +0000804 }
805 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700806 case NSEventTypeRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100807 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000808 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700809 case NSEventTypeOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100810 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000811 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700812 case NSEventTypeScrollWheel:
John Arbuckleae7313e2018-01-08 13:07:07 -0500813 /*
814 * Send wheel events to the guest regardless of window focus.
815 * This is in-line with standard Mac OS X UI behaviour.
816 */
817
John Arbuckledc3c89d2018-07-09 11:02:35 -0400818 /*
819 * When deltaY is zero, it means that this scrolling event was
820 * either horizontal, or so fine that it only appears in
821 * scrollingDeltaY. So we drop the event.
822 */
823 if ([event deltaY] != 0) {
John Arbuckleae7313e2018-01-08 13:07:07 -0500824 /* Determine if this is a scroll up or scroll down event */
John Arbuckledc3c89d2018-07-09 11:02:35 -0400825 buttons = ([event deltaY] > 0) ?
826 INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
827 qemu_input_queue_btn(dcl->con, buttons, true);
828 qemu_input_event_sync();
829 qemu_input_queue_btn(dcl->con, buttons, false);
830 qemu_input_event_sync();
831 }
John Arbuckleae7313e2018-01-08 13:07:07 -0500832 /*
833 * Since deltaY also reports scroll wheel events we prevent mouse
834 * movement code from executing.
835 */
836 mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000837 break;
838 default:
839 [NSApp sendEvent:event];
840 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100841
842 if (mouse_event) {
Peter Maydell8d3a5d92015-11-26 15:19:28 +0000843 /* Don't send button events to the guest unless we've got a
844 * mouse grab or window focus. If we have neither then this event
845 * is the user clicking on the background window to activate and
846 * bring us to the front, which will be done by the sendEvent
847 * call below. We definitely don't want to pass that click through
848 * to the guest.
849 */
850 if ((isMouseGrabbed || [[self window] isKeyWindow]) &&
851 (last_buttons != buttons)) {
Eric Blake7fb1cf12015-11-18 01:52:57 -0700852 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100853 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
854 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
John Arbuckleae7313e2018-01-08 13:07:07 -0500855 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100856 };
857 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
858 last_buttons = buttons;
859 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100860 if (isMouseGrabbed) {
861 if (isAbsoluteEnabled) {
862 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
863 * The check on screenContainsPoint is to avoid sending out of range values for
864 * clicks in the titlebar.
865 */
866 if ([self screenContainsPoint:p]) {
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200867 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, 0, screen.width);
868 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
Peter Maydellf61c3872014-06-23 10:35:24 +0100869 }
870 } else {
871 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
872 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
873 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100874 } else {
875 [NSApp sendEvent:event];
876 }
877 qemu_input_event_sync();
878 }
thsc304f7e2008-01-22 23:25:15 +0000879}
880
881- (void) grabMouse
882{
883 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
884
885 if (!isFullscreen) {
886 if (qemu_name)
John Arbuckle5929e362017-11-07 10:14:14 +0000887 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt + g to release Mouse)", qemu_name]];
thsc304f7e2008-01-22 23:25:15 +0000888 else
John Arbuckle5929e362017-11-07 10:14:14 +0000889 [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release Mouse)"];
thsc304f7e2008-01-22 23:25:15 +0000890 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100891 [self hideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100892 if (!isAbsoluteEnabled) {
893 isMouseDeassociated = TRUE;
894 CGAssociateMouseAndMouseCursorPosition(FALSE);
895 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000896 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000897}
898
899- (void) ungrabMouse
900{
901 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
902
903 if (!isFullscreen) {
904 if (qemu_name)
905 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
906 else
907 [normalWindow setTitle:@"QEMU"];
908 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100909 [self unhideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100910 if (isMouseDeassociated) {
911 CGAssociateMouseAndMouseCursorPosition(TRUE);
912 isMouseDeassociated = FALSE;
913 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000914 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000915}
916
917- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000918- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000919- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
Peter Maydellf61c3872014-06-23 10:35:24 +0100920- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
thsc304f7e2008-01-22 23:25:15 +0000921- (float) cdx {return cdx;}
922- (float) cdy {return cdy;}
923- (QEMUScreen) gscreen {return screen;}
John Arbuckle3b178b72015-09-25 23:14:00 +0100924
925/*
926 * Makes the target think all down keys are being released.
927 * This prevents a stuck key problem, since we will not see
928 * key up events for those keys after we have lost focus.
929 */
930- (void) raiseAllKeys
931{
932 int index;
933 const int max_index = ARRAY_SIZE(modifiers_state);
934
935 for (index = 0; index < max_index; index++) {
936 if (modifiers_state[index]) {
937 modifiers_state[index] = 0;
John Arbuckleaaac7142016-03-23 14:26:18 +0000938 qemu_input_event_send_key_qcode(dcl->con, index, false);
John Arbuckle3b178b72015-09-25 23:14:00 +0100939 }
940 }
941}
bellard5b0753e2005-03-01 21:37:28 +0000942@end
943
944
thsc304f7e2008-01-22 23:25:15 +0000945
bellard5b0753e2005-03-01 21:37:28 +0000946/*
947 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000948 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000949 ------------------------------------------------------
950*/
thsc304f7e2008-01-22 23:25:15 +0000951@interface QemuCocoaAppController : NSObject
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100952#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
John Arbuckled9bc14f2015-09-25 23:13:59 +0100953 <NSWindowDelegate, NSApplicationDelegate>
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100954#endif
bellard5b0753e2005-03-01 21:37:28 +0000955{
956}
bellard5b0753e2005-03-01 21:37:28 +0000957- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100958- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +0000959- (void)toggleFullScreen:(id)sender;
960- (void)showQEMUDoc:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100961- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +0100962- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100963- (void)pauseQEMU:(id)sender;
964- (void)resumeQEMU:(id)sender;
965- (void)displayPause;
966- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +0100967- (void)restartQEMU:(id)sender;
968- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +0100969- (void)ejectDeviceMedia:(id)sender;
970- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +0100971- (BOOL)verifyQuit;
John Arbucklef4747902016-03-23 14:26:17 +0000972- (void)openDocumentation:(NSString *)filename;
Programmingkid9e8204b2016-08-15 22:11:06 -0400973- (IBAction) do_about_menu_item: (id) sender;
974- (void)make_about_window;
John Arbucklee47ec1a2017-06-13 23:17:38 -0400975- (void)adjustSpeed:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +0000976@end
977
thsc304f7e2008-01-22 23:25:15 +0000978@implementation QemuCocoaAppController
979- (id) init
980{
981 COCOA_DEBUG("QemuCocoaAppController: init\n");
982
983 self = [super init];
984 if (self) {
985
986 // create a view and add it to the window
987 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
988 if(!cocoaView) {
989 fprintf(stderr, "(cocoa) can't create a view\n");
990 exit(1);
991 }
992
993 // create a window
994 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700995 styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable
thsc304f7e2008-01-22 23:25:15 +0000996 backing:NSBackingStoreBuffered defer:NO];
997 if(!normalWindow) {
998 fprintf(stderr, "(cocoa) can't create window\n");
999 exit(1);
1000 }
1001 [normalWindow setAcceptsMouseMovedEvents:YES];
John Arbucklea1dbc052015-10-13 21:51:18 +01001002 [normalWindow setTitle:@"QEMU"];
thsc304f7e2008-01-22 23:25:15 +00001003 [normalWindow setContentView:cocoaView];
Peter Maydell81801ae2015-05-19 09:11:18 +01001004#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
Andreas Färber561ef252009-12-13 03:06:20 +01001005 [normalWindow useOptimizedDrawing:YES];
Peter Maydell81801ae2015-05-19 09:11:18 +01001006#endif
thsc304f7e2008-01-22 23:25:15 +00001007 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +00001008 [normalWindow center];
John Arbuckled9bc14f2015-09-25 23:13:59 +01001009 [normalWindow setDelegate: self];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001010 stretch_video = false;
John Arbuckle8524f1c2015-06-19 10:53:27 +01001011
1012 /* Used for displaying pause on the screen */
1013 pauseLabel = [NSTextField new];
1014 [pauseLabel setBezeled:YES];
1015 [pauseLabel setDrawsBackground:YES];
1016 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
1017 [pauseLabel setEditable:NO];
1018 [pauseLabel setSelectable:NO];
1019 [pauseLabel setStringValue: @"Paused"];
1020 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
1021 [pauseLabel setTextColor: [NSColor blackColor]];
1022 [pauseLabel sizeToFit];
John Arbuckle693a3e02015-06-19 10:53:27 +01001023
1024 // set the supported image file types that can be opened
1025 supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg",
John Arbuckle9d227f12016-03-30 12:37:11 -04001026 @"qcow", @"qcow2", @"cloop", @"vmdk", @"cdr",
Programmingkid5f26fcf2016-12-30 15:42:21 -05001027 @"toast", nil];
Programmingkid9e8204b2016-08-15 22:11:06 -04001028 [self make_about_window];
thsc304f7e2008-01-22 23:25:15 +00001029 }
1030 return self;
1031}
1032
1033- (void) dealloc
1034{
1035 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
1036
1037 if (cocoaView)
1038 [cocoaView release];
1039 [super dealloc];
1040}
1041
bellard5b0753e2005-03-01 21:37:28 +00001042- (void)applicationDidFinishLaunching: (NSNotification *) note
1043{
thsc304f7e2008-01-22 23:25:15 +00001044 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
John Arbuckle365d7f32015-09-25 23:14:00 +01001045 // launch QEMU, with the global args
1046 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5b0753e2005-03-01 21:37:28 +00001047}
1048
1049- (void)applicationWillTerminate:(NSNotification *)aNotification
1050{
thsc304f7e2008-01-22 23:25:15 +00001051 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
1052
Eric Blakecf83f142017-05-15 16:41:13 -05001053 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
bellard5b0753e2005-03-01 21:37:28 +00001054 exit(0);
1055}
1056
Andreas Färber41ea49b2009-12-14 22:13:27 +01001057- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
1058{
1059 return YES;
1060}
1061
John Arbuckled9bc14f2015-09-25 23:13:59 +01001062- (NSApplicationTerminateReply)applicationShouldTerminate:
1063 (NSApplication *)sender
1064{
1065 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
1066 return [self verifyQuit];
1067}
1068
1069/* Called when the user clicks on a window's close button */
1070- (BOOL)windowShouldClose:(id)sender
1071{
1072 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
1073 [NSApp terminate: sender];
1074 /* If the user allows the application to quit then the call to
1075 * NSApp terminate will never return. If we get here then the user
1076 * cancelled the quit, so we should return NO to not permit the
1077 * closing of this window.
1078 */
1079 return NO;
1080}
1081
John Arbuckle3b178b72015-09-25 23:14:00 +01001082/* Called when QEMU goes into the background */
1083- (void) applicationWillResignActive: (NSNotification *)aNotification
1084{
1085 COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
1086 [cocoaView raiseAllKeys];
1087}
1088
thsc304f7e2008-01-22 23:25:15 +00001089- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
1090{
1091 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
1092
1093 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +02001094 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +00001095 exit(status);
1096}
1097
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001098/* We abstract the method called by the Enter Fullscreen menu item
1099 * because Mac OS 10.7 and higher disables it. This is because of the
1100 * menu item's old selector's name toggleFullScreen:
1101 */
1102- (void) doToggleFullScreen:(id)sender
1103{
1104 [self toggleFullScreen:(id)sender];
1105}
1106
thsc304f7e2008-01-22 23:25:15 +00001107- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +00001108{
thsc304f7e2008-01-22 23:25:15 +00001109 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
1110
1111 [cocoaView toggleFullScreen:sender];
1112}
1113
John Arbucklef4747902016-03-23 14:26:17 +00001114/* Tries to find then open the specified filename */
1115- (void) openDocumentation: (NSString *) filename
1116{
1117 /* Where to look for local files */
1118 NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"};
1119 NSString *full_file_path;
1120
1121 /* iterate thru the possible paths until the file is found */
1122 int index;
1123 for (index = 0; index < ARRAY_SIZE(path_array); index++) {
1124 full_file_path = [[NSBundle mainBundle] executablePath];
1125 full_file_path = [full_file_path stringByDeletingLastPathComponent];
1126 full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
1127 path_array[index], filename];
1128 if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) {
1129 return;
1130 }
1131 }
1132
1133 /* If none of the paths opened a file */
1134 NSBeep();
1135 QEMU_Alert(@"Failed to open file");
1136}
1137
thsc304f7e2008-01-22 23:25:15 +00001138- (void)showQEMUDoc:(id)sender
1139{
1140 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1141
John Arbucklef4747902016-03-23 14:26:17 +00001142 [self openDocumentation: @"qemu-doc.html"];
thsc304f7e2008-01-22 23:25:15 +00001143}
1144
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001145/* Stretches video to fit host monitor size */
1146- (void)zoomToFit:(id) sender
1147{
1148 stretch_video = !stretch_video;
1149 if (stretch_video == true) {
1150 [sender setState: NSOnState];
1151 } else {
1152 [sender setState: NSOffState];
1153 }
1154}
bellard5b0753e2005-03-01 21:37:28 +00001155
Programmingkidb4c6a112015-05-19 09:11:18 +01001156/* Displays the console on the screen */
1157- (void)displayConsole:(id)sender
1158{
1159 console_select([sender tag]);
1160}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001161
1162/* Pause the guest */
1163- (void)pauseQEMU:(id)sender
1164{
1165 qmp_stop(NULL);
1166 [sender setEnabled: NO];
1167 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1168 [self displayPause];
1169}
1170
1171/* Resume running the guest operating system */
1172- (void)resumeQEMU:(id) sender
1173{
1174 qmp_cont(NULL);
1175 [sender setEnabled: NO];
1176 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1177 [self removePause];
1178}
1179
1180/* Displays the word pause on the screen */
1181- (void)displayPause
1182{
1183 /* Coordinates have to be calculated each time because the window can change its size */
1184 int xCoord, yCoord, width, height;
1185 xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
1186 yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
1187 width = [pauseLabel frame].size.width;
1188 height = [pauseLabel frame].size.height;
1189 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1190 [cocoaView addSubview: pauseLabel];
1191}
1192
1193/* Removes the word pause from the screen */
1194- (void)removePause
1195{
1196 [pauseLabel removeFromSuperview];
1197}
1198
John Arbuckle27074612015-06-19 10:53:27 +01001199/* Restarts QEMU */
1200- (void)restartQEMU:(id)sender
1201{
1202 qmp_system_reset(NULL);
1203}
1204
1205/* Powers down QEMU */
1206- (void)powerDownQEMU:(id)sender
1207{
1208 qmp_system_powerdown(NULL);
1209}
1210
John Arbuckle693a3e02015-06-19 10:53:27 +01001211/* Ejects the media.
1212 * Uses sender's tag to figure out the device to eject.
1213 */
1214- (void)ejectDeviceMedia:(id)sender
1215{
1216 NSString * drive;
1217 drive = [sender representedObject];
1218 if(drive == nil) {
1219 NSBeep();
1220 QEMU_Alert(@"Failed to find drive to eject!");
1221 return;
1222 }
1223
1224 Error *err = NULL;
Kevin Wolffbe2d812016-09-20 13:38:46 +02001225 qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding],
1226 false, NULL, false, false, &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001227 handleAnyDeviceErrors(err);
1228}
1229
1230/* Displays a dialog box asking the user to select an image file to load.
1231 * Uses sender's represented object value to figure out which drive to use.
1232 */
1233- (void)changeDeviceMedia:(id)sender
1234{
1235 /* Find the drive name */
1236 NSString * drive;
1237 drive = [sender representedObject];
1238 if(drive == nil) {
1239 NSBeep();
1240 QEMU_Alert(@"Could not find drive!");
1241 return;
1242 }
1243
1244 /* Display the file open dialog */
1245 NSOpenPanel * openPanel;
1246 openPanel = [NSOpenPanel openPanel];
1247 [openPanel setCanChooseFiles: YES];
1248 [openPanel setAllowsMultipleSelection: NO];
1249 [openPanel setAllowedFileTypes: supportedImageFileTypes];
Peter Maydellb5725382018-05-29 19:15:23 +01001250 if([openPanel runModal] == NSModalResponseOK) {
John Arbuckle693a3e02015-06-19 10:53:27 +01001251 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1252 if(file == nil) {
1253 NSBeep();
1254 QEMU_Alert(@"Failed to convert URL to file path!");
1255 return;
1256 }
1257
1258 Error *err = NULL;
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001259 qmp_blockdev_change_medium(true,
1260 [drive cStringUsingEncoding:
Max Reitz24fb4132015-11-06 16:27:06 +01001261 NSASCIIStringEncoding],
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001262 false, NULL,
Max Reitz24fb4132015-11-06 16:27:06 +01001263 [file cStringUsingEncoding:
1264 NSASCIIStringEncoding],
1265 true, "raw",
Max Reitz39ff43d2015-11-11 04:49:44 +01001266 false, 0,
Max Reitz24fb4132015-11-06 16:27:06 +01001267 &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001268 handleAnyDeviceErrors(err);
1269 }
1270}
1271
John Arbuckled9bc14f2015-09-25 23:13:59 +01001272/* Verifies if the user really wants to quit */
1273- (BOOL)verifyQuit
1274{
1275 NSAlert *alert = [NSAlert new];
1276 [alert autorelease];
1277 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1278 [alert addButtonWithTitle: @"Cancel"];
1279 [alert addButtonWithTitle: @"Quit"];
1280 if([alert runModal] == NSAlertSecondButtonReturn) {
1281 return YES;
1282 } else {
1283 return NO;
1284 }
1285}
1286
Programmingkid9e8204b2016-08-15 22:11:06 -04001287/* The action method for the About menu item */
1288- (IBAction) do_about_menu_item: (id) sender
1289{
1290 [about_window makeKeyAndOrderFront: nil];
1291}
1292
1293/* Create and display the about dialog */
1294- (void)make_about_window
1295{
1296 /* Make the window */
1297 int x = 0, y = 0, about_width = 400, about_height = 200;
1298 NSRect window_rect = NSMakeRect(x, y, about_width, about_height);
1299 about_window = [[NSWindow alloc] initWithContentRect:window_rect
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001300 styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
1301 NSWindowStyleMaskMiniaturizable
Programmingkid9e8204b2016-08-15 22:11:06 -04001302 backing:NSBackingStoreBuffered
1303 defer:NO];
1304 [about_window setTitle: @"About"];
1305 [about_window setReleasedWhenClosed: NO];
1306 [about_window center];
1307 NSView *superView = [about_window contentView];
1308
1309 /* Create the dimensions of the picture */
1310 int picture_width = 80, picture_height = 80;
1311 x = (about_width - picture_width)/2;
1312 y = about_height - picture_height - 10;
1313 NSRect picture_rect = NSMakeRect(x, y, picture_width, picture_height);
1314
1315 /* Get the path to the QEMU binary */
1316 NSString *binary_name = [NSString stringWithCString: gArgv[0]
1317 encoding: NSASCIIStringEncoding];
1318 binary_name = [binary_name lastPathComponent];
1319 NSString *program_path = [[NSString alloc] initWithFormat: @"%@/%@",
1320 [[NSBundle mainBundle] bundlePath], binary_name];
1321
1322 /* Make the picture of QEMU */
1323 NSImageView *picture_view = [[NSImageView alloc] initWithFrame:
1324 picture_rect];
1325 NSImage *qemu_image = [[NSWorkspace sharedWorkspace] iconForFile:
1326 program_path];
1327 [picture_view setImage: qemu_image];
1328 [picture_view setImageScaling: NSImageScaleProportionallyUpOrDown];
1329 [superView addSubview: picture_view];
1330
1331 /* Make the name label */
1332 x = 0;
1333 y = y - 25;
1334 int name_width = about_width, name_height = 20;
1335 NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
1336 NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
1337 [name_label setEditable: NO];
1338 [name_label setBezeled: NO];
1339 [name_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001340 [name_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001341 NSString *qemu_name = [[NSString alloc] initWithCString: gArgv[0]
1342 encoding: NSASCIIStringEncoding];
1343 qemu_name = [qemu_name lastPathComponent];
1344 [name_label setStringValue: qemu_name];
1345 [superView addSubview: name_label];
1346
1347 /* Set the version label's attributes */
1348 x = 0;
1349 y = 50;
1350 int version_width = about_width, version_height = 20;
1351 NSRect version_rect = NSMakeRect(x, y, version_width, version_height);
1352 NSTextField *version_label = [[NSTextField alloc] initWithFrame:
1353 version_rect];
1354 [version_label setEditable: NO];
1355 [version_label setBezeled: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001356 [version_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001357 [version_label setDrawsBackground: NO];
1358
1359 /* Create the version string*/
1360 NSString *version_string;
1361 version_string = [[NSString alloc] initWithFormat:
Thomas Huth7e563bf2018-02-15 12:06:47 +01001362 @"QEMU emulator version %s", QEMU_FULL_VERSION];
Programmingkid9e8204b2016-08-15 22:11:06 -04001363 [version_label setStringValue: version_string];
1364 [superView addSubview: version_label];
1365
1366 /* Make copyright label */
1367 x = 0;
1368 y = 35;
1369 int copyright_width = about_width, copyright_height = 20;
1370 NSRect copyright_rect = NSMakeRect(x, y, copyright_width, copyright_height);
1371 NSTextField *copyright_label = [[NSTextField alloc] initWithFrame:
1372 copyright_rect];
1373 [copyright_label setEditable: NO];
1374 [copyright_label setBezeled: NO];
1375 [copyright_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001376 [copyright_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001377 [copyright_label setStringValue: [NSString stringWithFormat: @"%s",
1378 QEMU_COPYRIGHT]];
1379 [superView addSubview: copyright_label];
1380}
1381
John Arbucklee47ec1a2017-06-13 23:17:38 -04001382/* Used by the Speed menu items */
1383- (void)adjustSpeed:(id)sender
1384{
1385 int throttle_pct; /* throttle percentage */
1386 NSMenu *menu;
1387
1388 menu = [sender menu];
1389 if (menu != nil)
1390 {
1391 /* Unselect the currently selected item */
1392 for (NSMenuItem *item in [menu itemArray]) {
1393 if (item.state == NSOnState) {
1394 [item setState: NSOffState];
1395 break;
1396 }
1397 }
1398 }
1399
1400 // check the menu item
1401 [sender setState: NSOnState];
1402
1403 // get the throttle percentage
1404 throttle_pct = [sender tag];
1405
1406 cpu_throttle_set(throttle_pct);
1407 COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
1408}
1409
Programmingkidb4c6a112015-05-19 09:11:18 +01001410@end
bellard5b0753e2005-03-01 21:37:28 +00001411
thsc304f7e2008-01-22 23:25:15 +00001412
thsc304f7e2008-01-22 23:25:15 +00001413int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +00001414
thsc304f7e2008-01-22 23:25:15 +00001415 gArgc = argc;
1416 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +01001417 int i;
1418
1419 /* In case we don't need to display a window, let's not do that */
1420 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001421 const char *opt = argv[i];
1422
1423 if (opt[0] == '-') {
1424 /* Treat --foo the same as -foo. */
1425 if (opt[1] == '-') {
1426 opt++;
1427 }
Alexandre Raymond98514842011-05-29 18:22:49 -04001428 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
1429 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001430 !strcmp(opt, "-nographic") ||
1431 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001432 !strcmp(opt, "-curses") ||
Rainer Müllerb12a84c2015-09-09 16:08:30 +02001433 !strcmp(opt, "-display") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001434 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +02001435 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001436 }
Andreas Färberf4918802009-12-13 02:11:44 +01001437 }
1438 }
ths3b46e622007-09-17 08:09:54 +00001439
thsc304f7e2008-01-22 23:25:15 +00001440 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +00001441
Peter Maydell42a5dfe2013-04-22 10:29:47 +00001442 // Pull this console process up to being a fully-fledged graphical
1443 // app with a menubar and Dock icon
1444 ProcessSerialNumber psn = { 0, kCurrentProcess };
1445 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1446
1447 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +00001448
thsc304f7e2008-01-22 23:25:15 +00001449 // Add menus
1450 NSMenu *menu;
1451 NSMenuItem *menuItem;
1452
bellard5b0753e2005-03-01 21:37:28 +00001453 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +00001454
thsc304f7e2008-01-22 23:25:15 +00001455 // Application menu
1456 menu = [[NSMenu alloc] initWithTitle:@""];
Programmingkid9e8204b2016-08-15 22:11:06 -04001457 [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU
thsc304f7e2008-01-22 23:25:15 +00001458 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1459 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1460 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001461 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
thsc304f7e2008-01-22 23:25:15 +00001462 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1463 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1464 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1465 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1466 [menuItem setSubmenu:menu];
1467 [[NSApp mainMenu] addItem:menuItem];
1468 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001469
John Arbuckle8524f1c2015-06-19 10:53:27 +01001470 // Machine menu
1471 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1472 [menu setAutoenablesItems: NO];
1473 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1474 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1475 [menu addItem: menuItem];
1476 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001477 [menu addItem: [NSMenuItem separatorItem]];
1478 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1479 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001480 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1481 [menuItem setSubmenu:menu];
1482 [[NSApp mainMenu] addItem:menuItem];
1483
thsc304f7e2008-01-22 23:25:15 +00001484 // View menu
1485 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001486 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1487 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001488 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1489 [menuItem setSubmenu:menu];
1490 [[NSApp mainMenu] addItem:menuItem];
1491
John Arbucklee47ec1a2017-06-13 23:17:38 -04001492 // Speed menu
1493 menu = [[NSMenu alloc] initWithTitle:@"Speed"];
1494
1495 // Add the rest of the Speed menu items
1496 int p, percentage, throttle_pct;
1497 for (p = 10; p >= 0; p--)
1498 {
1499 percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
1500
1501 menuItem = [[[NSMenuItem alloc]
1502 initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
1503
1504 if (percentage == 100) {
1505 [menuItem setState: NSOnState];
1506 }
1507
1508 /* Calculate the throttle percentage */
1509 throttle_pct = -1 * percentage + 100;
1510
1511 [menuItem setTag: throttle_pct];
1512 [menu addItem: menuItem];
1513 }
1514 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
1515 [menuItem setSubmenu:menu];
1516 [[NSApp mainMenu] addItem:menuItem];
1517
thsc304f7e2008-01-22 23:25:15 +00001518 // Window menu
1519 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1520 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1521 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1522 [menuItem setSubmenu:menu];
1523 [[NSApp mainMenu] addItem:menuItem];
1524 [NSApp setWindowsMenu:menu];
1525
1526 // Help menu
1527 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1528 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
thsc304f7e2008-01-22 23:25:15 +00001529 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1530 [menuItem setSubmenu:menu];
1531 [[NSApp mainMenu] addItem:menuItem];
1532
1533 // Create an Application controller
1534 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1535 [NSApp setDelegate:appController];
1536
1537 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +00001538 [NSApp run];
ths3b46e622007-09-17 08:09:54 +00001539
thsc304f7e2008-01-22 23:25:15 +00001540 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001541 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001542
bellard5b0753e2005-03-01 21:37:28 +00001543 return 0;
1544}
thsc304f7e2008-01-22 23:25:15 +00001545
1546
1547
1548#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001549static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001550 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001551{
Peter Maydell6e657e62013-04-22 10:29:46 +00001552 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1553
thsc304f7e2008-01-22 23:25:15 +00001554 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1555
1556 NSRect rect;
1557 if ([cocoaView cdx] == 1.0) {
1558 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1559 } else {
1560 rect = NSMakeRect(
1561 x * [cocoaView cdx],
1562 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1563 w * [cocoaView cdx],
1564 h * [cocoaView cdy]);
1565 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001566 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001567
1568 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001569}
1570
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001571static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001572 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001573{
Peter Maydell6e657e62013-04-22 10:29:46 +00001574 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001575
Peter Maydell6e657e62013-04-22 10:29:46 +00001576 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001577 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001578 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001579}
1580
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001581static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001582{
Peter Maydell6e657e62013-04-22 10:29:46 +00001583 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1584
thsc304f7e2008-01-22 23:25:15 +00001585 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
John Arbuckle468a8952015-10-13 21:51:18 +01001586 graphic_hw_update(NULL);
thsc304f7e2008-01-22 23:25:15 +00001587
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001588 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001589 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001590 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001591 [cocoaView ungrabMouse];
1592 }
1593 }
1594 [cocoaView setAbsoluteEnabled:YES];
1595 }
1596
1597 NSDate *distantPast;
1598 NSEvent *event;
1599 distantPast = [NSDate distantPast];
1600 do {
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001601 event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:distantPast
thsc304f7e2008-01-22 23:25:15 +00001602 inMode: NSDefaultRunLoopMode dequeue:YES];
1603 if (event != nil) {
1604 [cocoaView handleEvent:event];
1605 }
1606 } while(event != nil);
Peter Maydell6e657e62013-04-22 10:29:46 +00001607 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001608}
1609
1610static void cocoa_cleanup(void)
1611{
1612 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001613 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001614}
1615
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001616static const DisplayChangeListenerOps dcl_ops = {
1617 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001618 .dpy_gfx_update = cocoa_update,
1619 .dpy_gfx_switch = cocoa_switch,
1620 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001621};
1622
Programmingkidb4c6a112015-05-19 09:11:18 +01001623/* Returns a name for a given console */
1624static NSString * getConsoleName(QemuConsole * console)
1625{
1626 return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
1627}
1628
1629/* Add an entry to the View menu for each console */
1630static void add_console_menu_entries(void)
1631{
1632 NSMenu *menu;
1633 NSMenuItem *menuItem;
1634 int index = 0;
1635
1636 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1637
1638 [menu addItem:[NSMenuItem separatorItem]];
1639
1640 while (qemu_console_lookup_by_index(index) != NULL) {
1641 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1642 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1643 [menuItem setTag: index];
1644 [menu addItem: menuItem];
1645 index++;
1646 }
1647}
1648
John Arbuckle693a3e02015-06-19 10:53:27 +01001649/* Make menu items for all removable devices.
1650 * Each device is given an 'Eject' and 'Change' menu item.
1651 */
John Arbucklea7940ec2015-10-13 21:51:18 +01001652static void addRemovableDevicesMenuItems(void)
John Arbuckle693a3e02015-06-19 10:53:27 +01001653{
1654 NSMenu *menu;
1655 NSMenuItem *menuItem;
1656 BlockInfoList *currentDevice, *pointerToFree;
1657 NSString *deviceName;
1658
1659 currentDevice = qmp_query_block(NULL);
1660 pointerToFree = currentDevice;
1661 if(currentDevice == NULL) {
1662 NSBeep();
1663 QEMU_Alert(@"Failed to query for block devices!");
1664 return;
1665 }
1666
1667 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1668
1669 // Add a separator between related groups of menu items
1670 [menu addItem:[NSMenuItem separatorItem]];
1671
1672 // Set the attributes to the "Removable Media" menu item
1673 NSString *titleString = @"Removable Media";
1674 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1675 NSColor *newColor = [NSColor blackColor];
1676 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1677 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1678 traits:NSBoldFontMask|NSItalicFontMask
1679 weight:0
1680 size:14];
1681 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1682 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1683 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1684
1685 // Add the "Removable Media" menu item
1686 menuItem = [NSMenuItem new];
1687 [menuItem setAttributedTitle: attString];
1688 [menuItem setEnabled: NO];
1689 [menu addItem: menuItem];
1690
Stefan Weilcb8d4c82016-03-23 15:59:57 +01001691 /* Loop through all the block devices in the emulator */
John Arbuckle693a3e02015-06-19 10:53:27 +01001692 while (currentDevice) {
1693 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1694
1695 if(currentDevice->value->removable) {
1696 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1697 action: @selector(changeDeviceMedia:)
1698 keyEquivalent: @""];
1699 [menu addItem: menuItem];
1700 [menuItem setRepresentedObject: deviceName];
1701 [menuItem autorelease];
1702
1703 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1704 action: @selector(ejectDeviceMedia:)
1705 keyEquivalent: @""];
1706 [menu addItem: menuItem];
1707 [menuItem setRepresentedObject: deviceName];
1708 [menuItem autorelease];
1709 }
1710 currentDevice = currentDevice->next;
1711 }
1712 qapi_free_BlockInfoList(pointerToFree);
1713}
1714
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01001715static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
thsc304f7e2008-01-22 23:25:15 +00001716{
1717 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1718
Programmingkid43227af2015-05-19 09:11:17 +01001719 /* if fullscreen mode is to be used */
Gerd Hoffmann767f9bf2018-02-02 12:10:19 +01001720 if (opts->has_full_screen && opts->full_screen) {
Programmingkid43227af2015-05-19 09:11:17 +01001721 [NSApp activateIgnoringOtherApps: YES];
1722 [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
1723 }
1724
Blue Swirl58a06672011-08-21 18:42:08 +00001725 dcl = g_malloc0(sizeof(DisplayChangeListener));
1726
aliguori9794f742009-03-04 19:25:22 +00001727 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001728 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001729 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001730
1731 // register cleanup function
1732 atexit(cocoa_cleanup);
Programmingkidb4c6a112015-05-19 09:11:18 +01001733
1734 /* At this point QEMU has created all the consoles, so we can add View
1735 * menu entries for them.
1736 */
1737 add_console_menu_entries();
John Arbuckle693a3e02015-06-19 10:53:27 +01001738
1739 /* Give all removable devices a menu item.
1740 * Has to be called after QEMU has started to
1741 * find out what removable devices it has.
1742 */
1743 addRemovableDevicesMenuItems();
thsc304f7e2008-01-22 23:25:15 +00001744}
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01001745
1746static QemuDisplay qemu_display_cocoa = {
1747 .type = DISPLAY_TYPE_COCOA,
1748 .init = cocoa_display_init,
1749};
1750
1751static void register_cocoa(void)
1752{
1753 qemu_display_register(&qemu_display_cocoa);
1754}
1755
1756type_init(register_cocoa);