blob: ecf12bfc2e49634f21e90ab242cc097a39ca0a70 [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 Armbrustereb815e22018-02-11 10:36:05 +010035#include "qapi/qapi-commands.h"
John Arbuckle693a3e02015-06-19 10:53:27 +010036#include "sysemu/blockdev.h"
Programmingkid9e8204b2016-08-15 22:11:06 -040037#include "qemu-version.h"
John Arbuckleaaac7142016-03-23 14:26:18 +000038#include <Carbon/Carbon.h>
John Arbucklee47ec1a2017-06-13 23:17:38 -040039#include "qom/cpu.h"
bellardda4dbf72005-03-02 22:22:43 +000040
Andreas Färber44e4c0b2009-12-13 00:45:40 +010041#ifndef MAC_OS_X_VERSION_10_5
42#define MAC_OS_X_VERSION_10_5 1050
43#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000044#ifndef MAC_OS_X_VERSION_10_6
45#define MAC_OS_X_VERSION_10_6 1060
46#endif
Peter Maydellb5725382018-05-29 19:15:23 +010047#ifndef MAC_OS_X_VERSION_10_9
48#define MAC_OS_X_VERSION_10_9 1090
49#endif
Peter Maydell81801ae2015-05-19 09:11:18 +010050#ifndef MAC_OS_X_VERSION_10_10
51#define MAC_OS_X_VERSION_10_10 101000
52#endif
Brendan Shanks4ba967a2017-04-24 23:29:52 -070053#ifndef MAC_OS_X_VERSION_10_12
54#define MAC_OS_X_VERSION_10_12 101200
55#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010056
Brendan Shanks4ba967a2017-04-24 23:29:52 -070057/* macOS 10.12 deprecated many constants, #define the new names for older SDKs */
58#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
59#define NSEventMaskAny NSAnyEventMask
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -070060#define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
61#define NSEventModifierFlagShift NSShiftKeyMask
Brendan Shanks4ba967a2017-04-24 23:29:52 -070062#define NSEventModifierFlagCommand NSCommandKeyMask
63#define NSEventModifierFlagControl NSControlKeyMask
64#define NSEventModifierFlagOption NSAlternateKeyMask
65#define NSEventTypeFlagsChanged NSFlagsChanged
66#define NSEventTypeKeyUp NSKeyUp
67#define NSEventTypeKeyDown NSKeyDown
68#define NSEventTypeMouseMoved NSMouseMoved
69#define NSEventTypeLeftMouseDown NSLeftMouseDown
70#define NSEventTypeRightMouseDown NSRightMouseDown
71#define NSEventTypeOtherMouseDown NSOtherMouseDown
72#define NSEventTypeLeftMouseDragged NSLeftMouseDragged
73#define NSEventTypeRightMouseDragged NSRightMouseDragged
74#define NSEventTypeOtherMouseDragged NSOtherMouseDragged
75#define NSEventTypeLeftMouseUp NSLeftMouseUp
76#define NSEventTypeRightMouseUp NSRightMouseUp
77#define NSEventTypeOtherMouseUp NSOtherMouseUp
78#define NSEventTypeScrollWheel NSScrollWheel
79#define NSTextAlignmentCenter NSCenterTextAlignment
80#define NSWindowStyleMaskBorderless NSBorderlessWindowMask
81#define NSWindowStyleMaskClosable NSClosableWindowMask
82#define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
83#define NSWindowStyleMaskTitled NSTitledWindowMask
84#endif
Peter Maydellb5725382018-05-29 19:15:23 +010085/* 10.13 deprecates NSFileHandlingPanelOKButton in favour of
86 * NSModalResponseOK, which was introduced in 10.9. Define
87 * it for older versions.
88 */
89#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9
90#define NSModalResponseOK NSFileHandlingPanelOKButton
91#endif
bellard5b0753e2005-03-01 21:37:28 +000092
thsc304f7e2008-01-22 23:25:15 +000093//#define DEBUG
94
95#ifdef DEBUG
96#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
97#else
98#define COCOA_DEBUG(...) ((void) 0)
99#endif
100
101#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +0000102
103typedef struct {
104 int width;
105 int height;
106 int bitsPerComponent;
107 int bitsPerPixel;
108} QEMUScreen;
109
Programmingkid9e8204b2016-08-15 22:11:06 -0400110NSWindow *normalWindow, *about_window;
aliguori9794f742009-03-04 19:25:22 +0000111static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100112static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +0000113
114int gArgc;
115char **gArgv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100116bool stretch_video;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100117NSTextField *pauseLabel;
John Arbuckle693a3e02015-06-19 10:53:27 +0100118NSArray * supportedImageFileTypes;
bellard5b0753e2005-03-01 21:37:28 +0000119
John Arbuckleaaac7142016-03-23 14:26:18 +0000120// Mac to QKeyCode conversion
121const int mac_to_qkeycode_map[] = {
122 [kVK_ANSI_A] = Q_KEY_CODE_A,
123 [kVK_ANSI_B] = Q_KEY_CODE_B,
124 [kVK_ANSI_C] = Q_KEY_CODE_C,
125 [kVK_ANSI_D] = Q_KEY_CODE_D,
126 [kVK_ANSI_E] = Q_KEY_CODE_E,
127 [kVK_ANSI_F] = Q_KEY_CODE_F,
128 [kVK_ANSI_G] = Q_KEY_CODE_G,
129 [kVK_ANSI_H] = Q_KEY_CODE_H,
130 [kVK_ANSI_I] = Q_KEY_CODE_I,
131 [kVK_ANSI_J] = Q_KEY_CODE_J,
132 [kVK_ANSI_K] = Q_KEY_CODE_K,
133 [kVK_ANSI_L] = Q_KEY_CODE_L,
134 [kVK_ANSI_M] = Q_KEY_CODE_M,
135 [kVK_ANSI_N] = Q_KEY_CODE_N,
136 [kVK_ANSI_O] = Q_KEY_CODE_O,
137 [kVK_ANSI_P] = Q_KEY_CODE_P,
138 [kVK_ANSI_Q] = Q_KEY_CODE_Q,
139 [kVK_ANSI_R] = Q_KEY_CODE_R,
140 [kVK_ANSI_S] = Q_KEY_CODE_S,
141 [kVK_ANSI_T] = Q_KEY_CODE_T,
142 [kVK_ANSI_U] = Q_KEY_CODE_U,
143 [kVK_ANSI_V] = Q_KEY_CODE_V,
144 [kVK_ANSI_W] = Q_KEY_CODE_W,
145 [kVK_ANSI_X] = Q_KEY_CODE_X,
146 [kVK_ANSI_Y] = Q_KEY_CODE_Y,
147 [kVK_ANSI_Z] = Q_KEY_CODE_Z,
ths3b46e622007-09-17 08:09:54 +0000148
John Arbuckleaaac7142016-03-23 14:26:18 +0000149 [kVK_ANSI_0] = Q_KEY_CODE_0,
150 [kVK_ANSI_1] = Q_KEY_CODE_1,
151 [kVK_ANSI_2] = Q_KEY_CODE_2,
152 [kVK_ANSI_3] = Q_KEY_CODE_3,
153 [kVK_ANSI_4] = Q_KEY_CODE_4,
154 [kVK_ANSI_5] = Q_KEY_CODE_5,
155 [kVK_ANSI_6] = Q_KEY_CODE_6,
156 [kVK_ANSI_7] = Q_KEY_CODE_7,
157 [kVK_ANSI_8] = Q_KEY_CODE_8,
158 [kVK_ANSI_9] = Q_KEY_CODE_9,
159
160 [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
161 [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
162 [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
163 [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
164 [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
165 [kVK_Tab] = Q_KEY_CODE_TAB,
166 [kVK_Return] = Q_KEY_CODE_RET,
167 [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
168 [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
169 [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
170 [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
171 [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
172 [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
173 [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
174 [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
175 [kVK_Shift] = Q_KEY_CODE_SHIFT,
176 [kVK_RightShift] = Q_KEY_CODE_SHIFT_R,
177 [kVK_Control] = Q_KEY_CODE_CTRL,
178 [kVK_RightControl] = Q_KEY_CODE_CTRL_R,
179 [kVK_Option] = Q_KEY_CODE_ALT,
180 [kVK_RightOption] = Q_KEY_CODE_ALT_R,
181 [kVK_Command] = Q_KEY_CODE_META_L,
182 [0x36] = Q_KEY_CODE_META_R, /* There is no kVK_RightCommand */
183 [kVK_Space] = Q_KEY_CODE_SPC,
184
185 [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
186 [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
187 [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
188 [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
189 [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
190 [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
191 [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
192 [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
193 [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
194 [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
195 [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
196 [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
197 [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
198 [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
199 [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
200 [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
201 [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
202 [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
203
204 [kVK_UpArrow] = Q_KEY_CODE_UP,
205 [kVK_DownArrow] = Q_KEY_CODE_DOWN,
206 [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
207 [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
208
209 [kVK_Help] = Q_KEY_CODE_INSERT,
210 [kVK_Home] = Q_KEY_CODE_HOME,
211 [kVK_PageUp] = Q_KEY_CODE_PGUP,
212 [kVK_PageDown] = Q_KEY_CODE_PGDN,
213 [kVK_End] = Q_KEY_CODE_END,
214 [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
215
216 [kVK_Escape] = Q_KEY_CODE_ESC,
217
218 /* The Power key can't be used directly because the operating system uses
219 * it. This key can be emulated by using it in place of another key such as
220 * F1. Don't forget to disable the real key binding.
221 */
222 /* [kVK_F1] = Q_KEY_CODE_POWER, */
223
224 [kVK_F1] = Q_KEY_CODE_F1,
225 [kVK_F2] = Q_KEY_CODE_F2,
226 [kVK_F3] = Q_KEY_CODE_F3,
227 [kVK_F4] = Q_KEY_CODE_F4,
228 [kVK_F5] = Q_KEY_CODE_F5,
229 [kVK_F6] = Q_KEY_CODE_F6,
230 [kVK_F7] = Q_KEY_CODE_F7,
231 [kVK_F8] = Q_KEY_CODE_F8,
232 [kVK_F9] = Q_KEY_CODE_F9,
233 [kVK_F10] = Q_KEY_CODE_F10,
234 [kVK_F11] = Q_KEY_CODE_F11,
235 [kVK_F12] = Q_KEY_CODE_F12,
236 [kVK_F13] = Q_KEY_CODE_PRINT,
237 [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
238 [kVK_F15] = Q_KEY_CODE_PAUSE,
239
240 /*
241 * The eject and volume keys can't be used here because they are handled at
242 * a lower level than what an Application can see.
243 */
bellard5b0753e2005-03-01 21:37:28 +0000244};
245
Andreas Färber77047bb2009-12-13 00:55:53 +0100246static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000247{
John Arbuckleaaac7142016-03-23 14:26:18 +0000248 if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000249 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000250 return 0;
251 }
John Arbuckleaaac7142016-03-23 14:26:18 +0000252 return mac_to_qkeycode_map[keycode];
bellard5b0753e2005-03-01 21:37:28 +0000253}
254
John Arbuckle693a3e02015-06-19 10:53:27 +0100255/* Displays an alert dialog box with the specified message */
256static void QEMU_Alert(NSString *message)
257{
258 NSAlert *alert;
259 alert = [NSAlert new];
260 [alert setMessageText: message];
261 [alert runModal];
262}
thsc304f7e2008-01-22 23:25:15 +0000263
John Arbuckle693a3e02015-06-19 10:53:27 +0100264/* Handles any errors that happen with a device transaction */
265static void handleAnyDeviceErrors(Error * err)
266{
267 if (err) {
268 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
269 encoding: NSASCIIStringEncoding]);
270 error_free(err);
271 }
272}
thsc304f7e2008-01-22 23:25:15 +0000273
bellard5b0753e2005-03-01 21:37:28 +0000274/*
275 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000276 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000277 ------------------------------------------------------
278*/
thsc304f7e2008-01-22 23:25:15 +0000279@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000280{
thsc304f7e2008-01-22 23:25:15 +0000281 QEMUScreen screen;
282 NSWindow *fullScreenWindow;
283 float cx,cy,cw,ch,cdx,cdy;
284 CGDataProviderRef dataProviderRef;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700285 BOOL modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000286 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000287 BOOL isFullscreen;
288 BOOL isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100289 BOOL isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000290}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100291- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000292- (void) grabMouse;
293- (void) ungrabMouse;
294- (void) toggleFullScreen:(id)sender;
John Arbuckle9c3a4182017-11-07 10:14:14 +0000295- (void) handleMonitorInput:(NSEvent *)event;
thsc304f7e2008-01-22 23:25:15 +0000296- (void) handleEvent:(NSEvent *)event;
297- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100298/* The state surrounding mouse grabbing is potentially confusing.
299 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
300 * pointing device an absolute-position one?"], but is only updated on
301 * next refresh.
302 * isMouseGrabbed tracks whether GUI events are directed to the guest;
303 * it controls whether special keys like Cmd get sent to the guest,
304 * and whether we capture the mouse when in non-absolute mode.
305 * isMouseDeassociated tracks whether we've told MacOSX to disassociate
306 * the mouse and mouse cursor position by calling
307 * CGAssociateMouseAndMouseCursorPosition(FALSE)
308 * (which basically happens if we grab in non-absolute mode).
309 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000310- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000311- (BOOL) isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100312- (BOOL) isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000313- (float) cdx;
314- (float) cdy;
315- (QEMUScreen) gscreen;
John Arbuckle3b178b72015-09-25 23:14:00 +0100316- (void) raiseAllKeys;
thsc304f7e2008-01-22 23:25:15 +0000317@end
ths3b46e622007-09-17 08:09:54 +0000318
Andreas Färber7fee1992011-06-09 20:53:32 +0200319QemuCocoaView *cocoaView;
320
thsc304f7e2008-01-22 23:25:15 +0000321@implementation QemuCocoaView
322- (id)initWithFrame:(NSRect)frameRect
323{
324 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000325
thsc304f7e2008-01-22 23:25:15 +0000326 self = [super initWithFrame:frameRect];
327 if (self) {
pbrook95219892006-04-09 01:06:34 +0000328
thsc304f7e2008-01-22 23:25:15 +0000329 screen.bitsPerComponent = 8;
330 screen.bitsPerPixel = 32;
331 screen.width = frameRect.size.width;
332 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000333
thsc304f7e2008-01-22 23:25:15 +0000334 }
335 return self;
336}
bellard7c206a72005-12-18 19:18:45 +0000337
thsc304f7e2008-01-22 23:25:15 +0000338- (void) dealloc
339{
340 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000341
thsc304f7e2008-01-22 23:25:15 +0000342 if (dataProviderRef)
343 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000344
thsc304f7e2008-01-22 23:25:15 +0000345 [super dealloc];
346}
bellard5cbfcd02006-06-14 15:53:24 +0000347
Andreas Färberd50f71d2009-12-13 02:03:33 +0100348- (BOOL) isOpaque
349{
350 return YES;
351}
352
Peter Maydell5dd45be2014-06-23 10:35:23 +0100353- (BOOL) screenContainsPoint:(NSPoint) p
354{
355 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
356}
357
Peter Maydell13aefd32014-06-23 10:35:25 +0100358- (void) hideCursor
359{
360 if (!cursor_hide) {
361 return;
362 }
363 [NSCursor hide];
364}
365
366- (void) unhideCursor
367{
368 if (!cursor_hide) {
369 return;
370 }
371 [NSCursor unhide];
372}
373
thsc304f7e2008-01-22 23:25:15 +0000374- (void) drawRect:(NSRect) rect
375{
376 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000377
thsc304f7e2008-01-22 23:25:15 +0000378 // get CoreGraphic context
379 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
380 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
381 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000382
thsc304f7e2008-01-22 23:25:15 +0000383 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000384 if (!dataProviderRef) {
385 // Draw request before any guest device has set up a framebuffer:
386 // just draw an opaque black rectangle
387 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
388 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
389 } else {
thsc304f7e2008-01-22 23:25:15 +0000390 CGImageRef imageRef = CGImageCreate(
391 screen.width, //width
392 screen.height, //height
393 screen.bitsPerComponent, //bitsPerComponent
394 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000395 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100396#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000397 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000398 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000399#else
400 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
401 kCGImageAlphaNoneSkipFirst, //bitmapInfo
402#endif
403 dataProviderRef, //provider
404 NULL, //decode
405 0, //interpolate
406 kCGRenderingIntentDefault //intent
407 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100408 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
409 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100410 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100411 int i;
412 CGImageRef clipImageRef;
413 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000414
Peter Maydellb63901d2015-05-19 09:11:17 +0100415 [self getRectsBeingDrawn:&rectList count:&rectCount];
416 for (i = 0; i < rectCount; i++) {
417 clipRect.origin.x = rectList[i].origin.x / cdx;
418 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
419 clipRect.size.width = rectList[i].size.width / cdx;
420 clipRect.size.height = rectList[i].size.height / cdy;
421 clipImageRef = CGImageCreateWithImageInRect(
422 imageRef,
423 clipRect
424 );
425 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
426 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000427 }
thsc304f7e2008-01-22 23:25:15 +0000428 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000429 }
430}
431
thsc304f7e2008-01-22 23:25:15 +0000432- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000433{
thsc304f7e2008-01-22 23:25:15 +0000434 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000435
thsc304f7e2008-01-22 23:25:15 +0000436 if (isFullscreen) {
437 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
438 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100439
440 /* stretches video, but keeps same aspect ratio */
441 if (stretch_video == true) {
442 /* use smallest stretch value - prevents clipping on sides */
443 if (MIN(cdx, cdy) == cdx) {
444 cdy = cdx;
445 } else {
446 cdx = cdy;
447 }
448 } else { /* No stretching */
449 cdx = cdy = 1;
450 }
thsc304f7e2008-01-22 23:25:15 +0000451 cw = screen.width * cdx;
452 ch = screen.height * cdy;
453 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
454 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
455 } else {
456 cx = 0;
457 cy = 0;
458 cw = screen.width;
459 ch = screen.height;
460 cdx = 1.0;
461 cdy = 1.0;
462 }
bellard5b0753e2005-03-01 21:37:28 +0000463}
464
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100465- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000466{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100467 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000468
Peter Maydell8510d912013-03-18 20:28:21 +0000469 int w = surface_width(surface);
470 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100471 /* cdx == 0 means this is our very first surface, in which case we need
472 * to recalculate the content dimensions even if it happens to be the size
473 * of the initial empty window.
474 */
475 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000476
477 int oldh = screen.height;
478 if (isResize) {
479 // Resize before we trigger the redraw, or we'll redraw at the wrong size
480 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
481 screen.width = w;
482 screen.height = h;
483 [self setContentDimensions];
484 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
485 }
Peter Maydell8510d912013-03-18 20:28:21 +0000486
thsc304f7e2008-01-22 23:25:15 +0000487 // update screenBuffer
488 if (dataProviderRef)
489 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000490
aliguori9794f742009-03-04 19:25:22 +0000491 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000492 screen.bitsPerPixel = surface_bits_per_pixel(surface);
493 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000494
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100495 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000496
497 // update windows
498 if (isFullscreen) {
499 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000500 [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 +0000501 } else {
502 if (qemu_name)
503 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000504 [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 +0000505 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000506
507 if (isResize) {
508 [normalWindow center];
509 }
thsc304f7e2008-01-22 23:25:15 +0000510}
511
512- (void) toggleFullScreen:(id)sender
513{
514 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
515
516 if (isFullscreen) { // switch from fullscreen to desktop
517 isFullscreen = FALSE;
518 [self ungrabMouse];
519 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000520 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
521 [self exitFullScreenModeWithOptions:nil];
522 } else {
thsc304f7e2008-01-22 23:25:15 +0000523 [fullScreenWindow close];
524 [normalWindow setContentView: self];
525 [normalWindow makeKeyAndOrderFront: self];
526 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000527 }
thsc304f7e2008-01-22 23:25:15 +0000528 } else { // switch from desktop to fullscreen
529 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100530 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000531 [self grabMouse];
532 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000533 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
534 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
535 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
536 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
537 nil]];
538 } else {
thsc304f7e2008-01-22 23:25:15 +0000539 [NSMenu setMenuBarVisible:NO];
540 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700541 styleMask:NSWindowStyleMaskBorderless
thsc304f7e2008-01-22 23:25:15 +0000542 backing:NSBackingStoreBuffered
543 defer:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100544 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
thsc304f7e2008-01-22 23:25:15 +0000545 [fullScreenWindow setHasShadow:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100546 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
547 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
548 [[fullScreenWindow contentView] addSubview: self];
thsc304f7e2008-01-22 23:25:15 +0000549 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000550 }
thsc304f7e2008-01-22 23:25:15 +0000551 }
552}
553
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700554- (void) toggleModifier: (int)keycode {
555 // Toggle the stored state.
556 modifiers_state[keycode] = !modifiers_state[keycode];
557 // Send a keyup or keydown depending on the state.
558 qemu_input_event_send_key_qcode(dcl->con, keycode, modifiers_state[keycode]);
559}
560
561- (void) toggleStatefulModifier: (int)keycode {
562 // Toggle the stored state.
563 modifiers_state[keycode] = !modifiers_state[keycode];
564 // Generate keydown and keyup.
565 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
566 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
567}
568
John Arbuckle9c3a4182017-11-07 10:14:14 +0000569// Does the work of sending input to the monitor
570- (void) handleMonitorInput:(NSEvent *)event
571{
572 int keysym = 0;
573 int control_key = 0;
574
575 // if the control key is down
576 if ([event modifierFlags] & NSEventModifierFlagControl) {
577 control_key = 1;
578 }
579
580 /* translates Macintosh keycodes to QEMU's keysym */
581
582 int without_control_translation[] = {
583 [0 ... 0xff] = 0, // invalid key
584
585 [kVK_UpArrow] = QEMU_KEY_UP,
586 [kVK_DownArrow] = QEMU_KEY_DOWN,
587 [kVK_RightArrow] = QEMU_KEY_RIGHT,
588 [kVK_LeftArrow] = QEMU_KEY_LEFT,
589 [kVK_Home] = QEMU_KEY_HOME,
590 [kVK_End] = QEMU_KEY_END,
591 [kVK_PageUp] = QEMU_KEY_PAGEUP,
592 [kVK_PageDown] = QEMU_KEY_PAGEDOWN,
593 [kVK_ForwardDelete] = QEMU_KEY_DELETE,
594 [kVK_Delete] = QEMU_KEY_BACKSPACE,
595 };
596
597 int with_control_translation[] = {
598 [0 ... 0xff] = 0, // invalid key
599
600 [kVK_UpArrow] = QEMU_KEY_CTRL_UP,
601 [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN,
602 [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT,
603 [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT,
604 [kVK_Home] = QEMU_KEY_CTRL_HOME,
605 [kVK_End] = QEMU_KEY_CTRL_END,
606 [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP,
607 [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN,
608 };
609
610 if (control_key != 0) { /* If the control key is being used */
611 if ([event keyCode] < ARRAY_SIZE(with_control_translation)) {
612 keysym = with_control_translation[[event keyCode]];
613 }
614 } else {
615 if ([event keyCode] < ARRAY_SIZE(without_control_translation)) {
616 keysym = without_control_translation[[event keyCode]];
617 }
618 }
619
620 // if not a key that needs translating
621 if (keysym == 0) {
622 NSString *ks = [event characters];
623 if ([ks length] > 0) {
624 keysym = [ks characterAtIndex:0];
625 }
626 }
627
628 if (keysym) {
629 kbd_put_keysym(keysym);
630 }
631}
632
thsc304f7e2008-01-22 23:25:15 +0000633- (void) handleEvent:(NSEvent *)event
634{
635 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
636
637 int buttons = 0;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700638 int keycode = 0;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100639 bool mouse_event = false;
John Arbuckle0c6c4392018-07-02 22:00:17 -0400640 static bool switched_to_fullscreen = false;
thsc304f7e2008-01-22 23:25:15 +0000641 NSPoint p = [event locationInWindow];
642
643 switch ([event type]) {
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700644 case NSEventTypeFlagsChanged:
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700645 if ([event keyCode] == 0) {
646 // When the Cocoa keyCode is zero that means keys should be
647 // synthesized based on the values in in the eventModifiers
648 // bitmask.
649
650 if (qemu_console_is_graphic(NULL)) {
John Arbucklee7206242017-06-28 15:37:16 -0400651 NSUInteger modifiers = [event modifierFlags];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700652
653 if (!!(modifiers & NSEventModifierFlagCapsLock) != !!modifiers_state[Q_KEY_CODE_CAPS_LOCK]) {
654 [self toggleStatefulModifier:Q_KEY_CODE_CAPS_LOCK];
655 }
656 if (!!(modifiers & NSEventModifierFlagShift) != !!modifiers_state[Q_KEY_CODE_SHIFT]) {
657 [self toggleModifier:Q_KEY_CODE_SHIFT];
658 }
659 if (!!(modifiers & NSEventModifierFlagControl) != !!modifiers_state[Q_KEY_CODE_CTRL]) {
660 [self toggleModifier:Q_KEY_CODE_CTRL];
661 }
662 if (!!(modifiers & NSEventModifierFlagOption) != !!modifiers_state[Q_KEY_CODE_ALT]) {
663 [self toggleModifier:Q_KEY_CODE_ALT];
664 }
665 if (!!(modifiers & NSEventModifierFlagCommand) != !!modifiers_state[Q_KEY_CODE_META_L]) {
666 [self toggleModifier:Q_KEY_CODE_META_L];
667 }
668 }
669 } else {
670 keycode = cocoa_keycode_to_qemu([event keyCode]);
671 }
Peter Maydell88959192013-12-08 22:59:02 +0000672
John Arbuckleaaac7142016-03-23 14:26:18 +0000673 if ((keycode == Q_KEY_CODE_META_L || keycode == Q_KEY_CODE_META_R)
674 && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000675 /* Don't pass command key changes to guest unless mouse is grabbed */
676 keycode = 0;
677 }
678
thsc304f7e2008-01-22 23:25:15 +0000679 if (keycode) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000680 // emulate caps lock and num lock keydown and keyup
681 if (keycode == Q_KEY_CODE_CAPS_LOCK ||
682 keycode == Q_KEY_CODE_NUM_LOCK) {
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700683 [self toggleStatefulModifier:keycode];
Peter Maydell68c0aa62013-04-17 09:16:35 +0000684 } else if (qemu_console_is_graphic(NULL)) {
John Arbuckle0c6c4392018-07-02 22:00:17 -0400685 if (switched_to_fullscreen) {
686 switched_to_fullscreen = false;
687 } else {
688 [self toggleModifier:keycode];
689 }
thsc304f7e2008-01-22 23:25:15 +0000690 }
691 }
692
thsc304f7e2008-01-22 23:25:15 +0000693 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700694 case NSEventTypeKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000695 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000696
Peter Maydell88959192013-12-08 22:59:02 +0000697 // forward command key combos to the host UI unless the mouse is grabbed
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700698 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
John Arbuckle0c6c4392018-07-02 22:00:17 -0400699 /*
700 * Prevent the command key from being stuck down in the guest
701 * when using Command-F to switch to full screen mode.
702 */
703 if (keycode == Q_KEY_CODE_F) {
704 switched_to_fullscreen = true;
705 }
thsc304f7e2008-01-22 23:25:15 +0000706 [NSApp sendEvent:event];
707 return;
708 }
709
710 // default
thsc304f7e2008-01-22 23:25:15 +0000711
John Arbuckle5929e362017-11-07 10:14:14 +0000712 // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700713 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
John Arbuckle5929e362017-11-07 10:14:14 +0000714 NSString *keychar = [event charactersIgnoringModifiers];
715 if ([keychar length] == 1) {
716 char key = [keychar characterAtIndex:0];
717 switch (key) {
thsc304f7e2008-01-22 23:25:15 +0000718
John Arbuckle5929e362017-11-07 10:14:14 +0000719 // enable graphic console
720 case '1' ... '9':
721 console_select(key - '0' - 1); /* ascii math */
722 return;
723
724 // release the mouse grab
725 case 'g':
726 [self ungrabMouse];
727 return;
728 }
thsc304f7e2008-01-22 23:25:15 +0000729 }
Peter Maydellef2088f2017-11-07 10:14:14 +0000730 }
thsc304f7e2008-01-22 23:25:15 +0000731
Peter Maydellef2088f2017-11-07 10:14:14 +0000732 if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000733 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000734 } else {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000735 [self handleMonitorInput: event];
thsc304f7e2008-01-22 23:25:15 +0000736 }
737 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700738 case NSEventTypeKeyUp:
thsc304f7e2008-01-22 23:25:15 +0000739 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000740
741 // don't pass the guest a spurious key-up if we treated this
742 // command-key combo as a host UI action
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700743 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell88959192013-12-08 22:59:02 +0000744 return;
745 }
746
Peter Maydell68c0aa62013-04-17 09:16:35 +0000747 if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000748 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000749 }
750 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700751 case NSEventTypeMouseMoved:
thsc304f7e2008-01-22 23:25:15 +0000752 if (isAbsoluteEnabled) {
Peter Maydell5dd45be2014-06-23 10:35:23 +0100753 if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
Peter Maydellf61c3872014-06-23 10:35:24 +0100754 if (isMouseGrabbed) {
755 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +0000756 }
757 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +0100758 if (!isMouseGrabbed) {
759 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000760 }
761 }
762 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100763 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000764 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700765 case NSEventTypeLeftMouseDown:
766 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000767 buttons |= MOUSE_EVENT_RBUTTON;
768 } else {
769 buttons |= MOUSE_EVENT_LBUTTON;
770 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100771 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000772 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700773 case NSEventTypeRightMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000774 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100775 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000776 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700777 case NSEventTypeOtherMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000778 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100779 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000780 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700781 case NSEventTypeLeftMouseDragged:
782 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000783 buttons |= MOUSE_EVENT_RBUTTON;
784 } else {
785 buttons |= MOUSE_EVENT_LBUTTON;
786 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100787 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000788 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700789 case NSEventTypeRightMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000790 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100791 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000792 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700793 case NSEventTypeOtherMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000794 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100795 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000796 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700797 case NSEventTypeLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +0100798 mouse_event = true;
799 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
Programmingkid9e8204b2016-08-15 22:11:06 -0400800 if([[self window] isKeyWindow]) {
801 [self grabMouse];
802 }
thsc304f7e2008-01-22 23:25:15 +0000803 }
804 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700805 case NSEventTypeRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100806 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000807 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700808 case NSEventTypeOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100809 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000810 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700811 case NSEventTypeScrollWheel:
John Arbuckleae7313e2018-01-08 13:07:07 -0500812 /*
813 * Send wheel events to the guest regardless of window focus.
814 * This is in-line with standard Mac OS X UI behaviour.
815 */
816
John Arbuckledc3c89d2018-07-09 11:02:35 -0400817 /*
818 * When deltaY is zero, it means that this scrolling event was
819 * either horizontal, or so fine that it only appears in
820 * scrollingDeltaY. So we drop the event.
821 */
822 if ([event deltaY] != 0) {
John Arbuckleae7313e2018-01-08 13:07:07 -0500823 /* Determine if this is a scroll up or scroll down event */
John Arbuckledc3c89d2018-07-09 11:02:35 -0400824 buttons = ([event deltaY] > 0) ?
825 INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
826 qemu_input_queue_btn(dcl->con, buttons, true);
827 qemu_input_event_sync();
828 qemu_input_queue_btn(dcl->con, buttons, false);
829 qemu_input_event_sync();
830 }
John Arbuckleae7313e2018-01-08 13:07:07 -0500831 /*
832 * Since deltaY also reports scroll wheel events we prevent mouse
833 * movement code from executing.
834 */
835 mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000836 break;
837 default:
838 [NSApp sendEvent:event];
839 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100840
841 if (mouse_event) {
Peter Maydell8d3a5d92015-11-26 15:19:28 +0000842 /* Don't send button events to the guest unless we've got a
843 * mouse grab or window focus. If we have neither then this event
844 * is the user clicking on the background window to activate and
845 * bring us to the front, which will be done by the sendEvent
846 * call below. We definitely don't want to pass that click through
847 * to the guest.
848 */
849 if ((isMouseGrabbed || [[self window] isKeyWindow]) &&
850 (last_buttons != buttons)) {
Eric Blake7fb1cf12015-11-18 01:52:57 -0700851 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100852 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
853 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
John Arbuckleae7313e2018-01-08 13:07:07 -0500854 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100855 };
856 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
857 last_buttons = buttons;
858 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100859 if (isMouseGrabbed) {
860 if (isAbsoluteEnabled) {
861 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
862 * The check on screenContainsPoint is to avoid sending out of range values for
863 * clicks in the titlebar.
864 */
865 if ([self screenContainsPoint:p]) {
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200866 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, 0, screen.width);
867 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
Peter Maydellf61c3872014-06-23 10:35:24 +0100868 }
869 } else {
870 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
871 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
872 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100873 } else {
874 [NSApp sendEvent:event];
875 }
876 qemu_input_event_sync();
877 }
thsc304f7e2008-01-22 23:25:15 +0000878}
879
880- (void) grabMouse
881{
882 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
883
884 if (!isFullscreen) {
885 if (qemu_name)
John Arbuckle5929e362017-11-07 10:14:14 +0000886 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt + g to release Mouse)", qemu_name]];
thsc304f7e2008-01-22 23:25:15 +0000887 else
John Arbuckle5929e362017-11-07 10:14:14 +0000888 [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release Mouse)"];
thsc304f7e2008-01-22 23:25:15 +0000889 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100890 [self hideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100891 if (!isAbsoluteEnabled) {
892 isMouseDeassociated = TRUE;
893 CGAssociateMouseAndMouseCursorPosition(FALSE);
894 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000895 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000896}
897
898- (void) ungrabMouse
899{
900 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
901
902 if (!isFullscreen) {
903 if (qemu_name)
904 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
905 else
906 [normalWindow setTitle:@"QEMU"];
907 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100908 [self unhideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100909 if (isMouseDeassociated) {
910 CGAssociateMouseAndMouseCursorPosition(TRUE);
911 isMouseDeassociated = FALSE;
912 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000913 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000914}
915
916- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000917- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000918- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
Peter Maydellf61c3872014-06-23 10:35:24 +0100919- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
thsc304f7e2008-01-22 23:25:15 +0000920- (float) cdx {return cdx;}
921- (float) cdy {return cdy;}
922- (QEMUScreen) gscreen {return screen;}
John Arbuckle3b178b72015-09-25 23:14:00 +0100923
924/*
925 * Makes the target think all down keys are being released.
926 * This prevents a stuck key problem, since we will not see
927 * key up events for those keys after we have lost focus.
928 */
929- (void) raiseAllKeys
930{
931 int index;
932 const int max_index = ARRAY_SIZE(modifiers_state);
933
934 for (index = 0; index < max_index; index++) {
935 if (modifiers_state[index]) {
936 modifiers_state[index] = 0;
John Arbuckleaaac7142016-03-23 14:26:18 +0000937 qemu_input_event_send_key_qcode(dcl->con, index, false);
John Arbuckle3b178b72015-09-25 23:14:00 +0100938 }
939 }
940}
bellard5b0753e2005-03-01 21:37:28 +0000941@end
942
943
thsc304f7e2008-01-22 23:25:15 +0000944
bellard5b0753e2005-03-01 21:37:28 +0000945/*
946 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000947 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000948 ------------------------------------------------------
949*/
thsc304f7e2008-01-22 23:25:15 +0000950@interface QemuCocoaAppController : NSObject
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100951#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
John Arbuckled9bc14f2015-09-25 23:13:59 +0100952 <NSWindowDelegate, NSApplicationDelegate>
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100953#endif
bellard5b0753e2005-03-01 21:37:28 +0000954{
955}
bellard5b0753e2005-03-01 21:37:28 +0000956- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100957- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +0000958- (void)toggleFullScreen:(id)sender;
959- (void)showQEMUDoc:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100960- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +0100961- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100962- (void)pauseQEMU:(id)sender;
963- (void)resumeQEMU:(id)sender;
964- (void)displayPause;
965- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +0100966- (void)restartQEMU:(id)sender;
967- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +0100968- (void)ejectDeviceMedia:(id)sender;
969- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +0100970- (BOOL)verifyQuit;
John Arbucklef4747902016-03-23 14:26:17 +0000971- (void)openDocumentation:(NSString *)filename;
Programmingkid9e8204b2016-08-15 22:11:06 -0400972- (IBAction) do_about_menu_item: (id) sender;
973- (void)make_about_window;
John Arbucklee47ec1a2017-06-13 23:17:38 -0400974- (void)adjustSpeed:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +0000975@end
976
thsc304f7e2008-01-22 23:25:15 +0000977@implementation QemuCocoaAppController
978- (id) init
979{
980 COCOA_DEBUG("QemuCocoaAppController: init\n");
981
982 self = [super init];
983 if (self) {
984
985 // create a view and add it to the window
986 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
987 if(!cocoaView) {
988 fprintf(stderr, "(cocoa) can't create a view\n");
989 exit(1);
990 }
991
992 // create a window
993 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700994 styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable
thsc304f7e2008-01-22 23:25:15 +0000995 backing:NSBackingStoreBuffered defer:NO];
996 if(!normalWindow) {
997 fprintf(stderr, "(cocoa) can't create window\n");
998 exit(1);
999 }
1000 [normalWindow setAcceptsMouseMovedEvents:YES];
John Arbucklea1dbc052015-10-13 21:51:18 +01001001 [normalWindow setTitle:@"QEMU"];
thsc304f7e2008-01-22 23:25:15 +00001002 [normalWindow setContentView:cocoaView];
Peter Maydell81801ae2015-05-19 09:11:18 +01001003#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
Andreas Färber561ef252009-12-13 03:06:20 +01001004 [normalWindow useOptimizedDrawing:YES];
Peter Maydell81801ae2015-05-19 09:11:18 +01001005#endif
thsc304f7e2008-01-22 23:25:15 +00001006 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +00001007 [normalWindow center];
John Arbuckled9bc14f2015-09-25 23:13:59 +01001008 [normalWindow setDelegate: self];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001009 stretch_video = false;
John Arbuckle8524f1c2015-06-19 10:53:27 +01001010
1011 /* Used for displaying pause on the screen */
1012 pauseLabel = [NSTextField new];
1013 [pauseLabel setBezeled:YES];
1014 [pauseLabel setDrawsBackground:YES];
1015 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
1016 [pauseLabel setEditable:NO];
1017 [pauseLabel setSelectable:NO];
1018 [pauseLabel setStringValue: @"Paused"];
1019 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
1020 [pauseLabel setTextColor: [NSColor blackColor]];
1021 [pauseLabel sizeToFit];
John Arbuckle693a3e02015-06-19 10:53:27 +01001022
1023 // set the supported image file types that can be opened
1024 supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg",
John Arbuckle9d227f12016-03-30 12:37:11 -04001025 @"qcow", @"qcow2", @"cloop", @"vmdk", @"cdr",
Programmingkid5f26fcf2016-12-30 15:42:21 -05001026 @"toast", nil];
Programmingkid9e8204b2016-08-15 22:11:06 -04001027 [self make_about_window];
thsc304f7e2008-01-22 23:25:15 +00001028 }
1029 return self;
1030}
1031
1032- (void) dealloc
1033{
1034 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
1035
1036 if (cocoaView)
1037 [cocoaView release];
1038 [super dealloc];
1039}
1040
bellard5b0753e2005-03-01 21:37:28 +00001041- (void)applicationDidFinishLaunching: (NSNotification *) note
1042{
thsc304f7e2008-01-22 23:25:15 +00001043 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
John Arbuckle365d7f32015-09-25 23:14:00 +01001044 // launch QEMU, with the global args
1045 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5b0753e2005-03-01 21:37:28 +00001046}
1047
1048- (void)applicationWillTerminate:(NSNotification *)aNotification
1049{
thsc304f7e2008-01-22 23:25:15 +00001050 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
1051
Eric Blakecf83f142017-05-15 16:41:13 -05001052 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
bellard5b0753e2005-03-01 21:37:28 +00001053 exit(0);
1054}
1055
Andreas Färber41ea49b2009-12-14 22:13:27 +01001056- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
1057{
1058 return YES;
1059}
1060
John Arbuckled9bc14f2015-09-25 23:13:59 +01001061- (NSApplicationTerminateReply)applicationShouldTerminate:
1062 (NSApplication *)sender
1063{
1064 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
1065 return [self verifyQuit];
1066}
1067
1068/* Called when the user clicks on a window's close button */
1069- (BOOL)windowShouldClose:(id)sender
1070{
1071 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
1072 [NSApp terminate: sender];
1073 /* If the user allows the application to quit then the call to
1074 * NSApp terminate will never return. If we get here then the user
1075 * cancelled the quit, so we should return NO to not permit the
1076 * closing of this window.
1077 */
1078 return NO;
1079}
1080
John Arbuckle3b178b72015-09-25 23:14:00 +01001081/* Called when QEMU goes into the background */
1082- (void) applicationWillResignActive: (NSNotification *)aNotification
1083{
1084 COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
1085 [cocoaView raiseAllKeys];
1086}
1087
thsc304f7e2008-01-22 23:25:15 +00001088- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
1089{
1090 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
1091
1092 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +02001093 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +00001094 exit(status);
1095}
1096
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001097/* We abstract the method called by the Enter Fullscreen menu item
1098 * because Mac OS 10.7 and higher disables it. This is because of the
1099 * menu item's old selector's name toggleFullScreen:
1100 */
1101- (void) doToggleFullScreen:(id)sender
1102{
1103 [self toggleFullScreen:(id)sender];
1104}
1105
thsc304f7e2008-01-22 23:25:15 +00001106- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +00001107{
thsc304f7e2008-01-22 23:25:15 +00001108 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
1109
1110 [cocoaView toggleFullScreen:sender];
1111}
1112
John Arbucklef4747902016-03-23 14:26:17 +00001113/* Tries to find then open the specified filename */
1114- (void) openDocumentation: (NSString *) filename
1115{
1116 /* Where to look for local files */
1117 NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"};
1118 NSString *full_file_path;
1119
1120 /* iterate thru the possible paths until the file is found */
1121 int index;
1122 for (index = 0; index < ARRAY_SIZE(path_array); index++) {
1123 full_file_path = [[NSBundle mainBundle] executablePath];
1124 full_file_path = [full_file_path stringByDeletingLastPathComponent];
1125 full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
1126 path_array[index], filename];
1127 if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) {
1128 return;
1129 }
1130 }
1131
1132 /* If none of the paths opened a file */
1133 NSBeep();
1134 QEMU_Alert(@"Failed to open file");
1135}
1136
thsc304f7e2008-01-22 23:25:15 +00001137- (void)showQEMUDoc:(id)sender
1138{
1139 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1140
John Arbucklef4747902016-03-23 14:26:17 +00001141 [self openDocumentation: @"qemu-doc.html"];
thsc304f7e2008-01-22 23:25:15 +00001142}
1143
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001144/* Stretches video to fit host monitor size */
1145- (void)zoomToFit:(id) sender
1146{
1147 stretch_video = !stretch_video;
1148 if (stretch_video == true) {
1149 [sender setState: NSOnState];
1150 } else {
1151 [sender setState: NSOffState];
1152 }
1153}
bellard5b0753e2005-03-01 21:37:28 +00001154
Programmingkidb4c6a112015-05-19 09:11:18 +01001155/* Displays the console on the screen */
1156- (void)displayConsole:(id)sender
1157{
1158 console_select([sender tag]);
1159}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001160
1161/* Pause the guest */
1162- (void)pauseQEMU:(id)sender
1163{
1164 qmp_stop(NULL);
1165 [sender setEnabled: NO];
1166 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1167 [self displayPause];
1168}
1169
1170/* Resume running the guest operating system */
1171- (void)resumeQEMU:(id) sender
1172{
1173 qmp_cont(NULL);
1174 [sender setEnabled: NO];
1175 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1176 [self removePause];
1177}
1178
1179/* Displays the word pause on the screen */
1180- (void)displayPause
1181{
1182 /* Coordinates have to be calculated each time because the window can change its size */
1183 int xCoord, yCoord, width, height;
1184 xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
1185 yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
1186 width = [pauseLabel frame].size.width;
1187 height = [pauseLabel frame].size.height;
1188 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1189 [cocoaView addSubview: pauseLabel];
1190}
1191
1192/* Removes the word pause from the screen */
1193- (void)removePause
1194{
1195 [pauseLabel removeFromSuperview];
1196}
1197
John Arbuckle27074612015-06-19 10:53:27 +01001198/* Restarts QEMU */
1199- (void)restartQEMU:(id)sender
1200{
1201 qmp_system_reset(NULL);
1202}
1203
1204/* Powers down QEMU */
1205- (void)powerDownQEMU:(id)sender
1206{
1207 qmp_system_powerdown(NULL);
1208}
1209
John Arbuckle693a3e02015-06-19 10:53:27 +01001210/* Ejects the media.
1211 * Uses sender's tag to figure out the device to eject.
1212 */
1213- (void)ejectDeviceMedia:(id)sender
1214{
1215 NSString * drive;
1216 drive = [sender representedObject];
1217 if(drive == nil) {
1218 NSBeep();
1219 QEMU_Alert(@"Failed to find drive to eject!");
1220 return;
1221 }
1222
1223 Error *err = NULL;
Kevin Wolffbe2d812016-09-20 13:38:46 +02001224 qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding],
1225 false, NULL, false, false, &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001226 handleAnyDeviceErrors(err);
1227}
1228
1229/* Displays a dialog box asking the user to select an image file to load.
1230 * Uses sender's represented object value to figure out which drive to use.
1231 */
1232- (void)changeDeviceMedia:(id)sender
1233{
1234 /* Find the drive name */
1235 NSString * drive;
1236 drive = [sender representedObject];
1237 if(drive == nil) {
1238 NSBeep();
1239 QEMU_Alert(@"Could not find drive!");
1240 return;
1241 }
1242
1243 /* Display the file open dialog */
1244 NSOpenPanel * openPanel;
1245 openPanel = [NSOpenPanel openPanel];
1246 [openPanel setCanChooseFiles: YES];
1247 [openPanel setAllowsMultipleSelection: NO];
1248 [openPanel setAllowedFileTypes: supportedImageFileTypes];
Peter Maydellb5725382018-05-29 19:15:23 +01001249 if([openPanel runModal] == NSModalResponseOK) {
John Arbuckle693a3e02015-06-19 10:53:27 +01001250 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1251 if(file == nil) {
1252 NSBeep();
1253 QEMU_Alert(@"Failed to convert URL to file path!");
1254 return;
1255 }
1256
1257 Error *err = NULL;
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001258 qmp_blockdev_change_medium(true,
1259 [drive cStringUsingEncoding:
Max Reitz24fb4132015-11-06 16:27:06 +01001260 NSASCIIStringEncoding],
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001261 false, NULL,
Max Reitz24fb4132015-11-06 16:27:06 +01001262 [file cStringUsingEncoding:
1263 NSASCIIStringEncoding],
1264 true, "raw",
Max Reitz39ff43d2015-11-11 04:49:44 +01001265 false, 0,
Max Reitz24fb4132015-11-06 16:27:06 +01001266 &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001267 handleAnyDeviceErrors(err);
1268 }
1269}
1270
John Arbuckled9bc14f2015-09-25 23:13:59 +01001271/* Verifies if the user really wants to quit */
1272- (BOOL)verifyQuit
1273{
1274 NSAlert *alert = [NSAlert new];
1275 [alert autorelease];
1276 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1277 [alert addButtonWithTitle: @"Cancel"];
1278 [alert addButtonWithTitle: @"Quit"];
1279 if([alert runModal] == NSAlertSecondButtonReturn) {
1280 return YES;
1281 } else {
1282 return NO;
1283 }
1284}
1285
Programmingkid9e8204b2016-08-15 22:11:06 -04001286/* The action method for the About menu item */
1287- (IBAction) do_about_menu_item: (id) sender
1288{
1289 [about_window makeKeyAndOrderFront: nil];
1290}
1291
1292/* Create and display the about dialog */
1293- (void)make_about_window
1294{
1295 /* Make the window */
1296 int x = 0, y = 0, about_width = 400, about_height = 200;
1297 NSRect window_rect = NSMakeRect(x, y, about_width, about_height);
1298 about_window = [[NSWindow alloc] initWithContentRect:window_rect
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001299 styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
1300 NSWindowStyleMaskMiniaturizable
Programmingkid9e8204b2016-08-15 22:11:06 -04001301 backing:NSBackingStoreBuffered
1302 defer:NO];
1303 [about_window setTitle: @"About"];
1304 [about_window setReleasedWhenClosed: NO];
1305 [about_window center];
1306 NSView *superView = [about_window contentView];
1307
1308 /* Create the dimensions of the picture */
1309 int picture_width = 80, picture_height = 80;
1310 x = (about_width - picture_width)/2;
1311 y = about_height - picture_height - 10;
1312 NSRect picture_rect = NSMakeRect(x, y, picture_width, picture_height);
1313
1314 /* Get the path to the QEMU binary */
1315 NSString *binary_name = [NSString stringWithCString: gArgv[0]
1316 encoding: NSASCIIStringEncoding];
1317 binary_name = [binary_name lastPathComponent];
1318 NSString *program_path = [[NSString alloc] initWithFormat: @"%@/%@",
1319 [[NSBundle mainBundle] bundlePath], binary_name];
1320
1321 /* Make the picture of QEMU */
1322 NSImageView *picture_view = [[NSImageView alloc] initWithFrame:
1323 picture_rect];
1324 NSImage *qemu_image = [[NSWorkspace sharedWorkspace] iconForFile:
1325 program_path];
1326 [picture_view setImage: qemu_image];
1327 [picture_view setImageScaling: NSImageScaleProportionallyUpOrDown];
1328 [superView addSubview: picture_view];
1329
1330 /* Make the name label */
1331 x = 0;
1332 y = y - 25;
1333 int name_width = about_width, name_height = 20;
1334 NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
1335 NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
1336 [name_label setEditable: NO];
1337 [name_label setBezeled: NO];
1338 [name_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001339 [name_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001340 NSString *qemu_name = [[NSString alloc] initWithCString: gArgv[0]
1341 encoding: NSASCIIStringEncoding];
1342 qemu_name = [qemu_name lastPathComponent];
1343 [name_label setStringValue: qemu_name];
1344 [superView addSubview: name_label];
1345
1346 /* Set the version label's attributes */
1347 x = 0;
1348 y = 50;
1349 int version_width = about_width, version_height = 20;
1350 NSRect version_rect = NSMakeRect(x, y, version_width, version_height);
1351 NSTextField *version_label = [[NSTextField alloc] initWithFrame:
1352 version_rect];
1353 [version_label setEditable: NO];
1354 [version_label setBezeled: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001355 [version_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001356 [version_label setDrawsBackground: NO];
1357
1358 /* Create the version string*/
1359 NSString *version_string;
1360 version_string = [[NSString alloc] initWithFormat:
Thomas Huth7e563bf2018-02-15 12:06:47 +01001361 @"QEMU emulator version %s", QEMU_FULL_VERSION];
Programmingkid9e8204b2016-08-15 22:11:06 -04001362 [version_label setStringValue: version_string];
1363 [superView addSubview: version_label];
1364
1365 /* Make copyright label */
1366 x = 0;
1367 y = 35;
1368 int copyright_width = about_width, copyright_height = 20;
1369 NSRect copyright_rect = NSMakeRect(x, y, copyright_width, copyright_height);
1370 NSTextField *copyright_label = [[NSTextField alloc] initWithFrame:
1371 copyright_rect];
1372 [copyright_label setEditable: NO];
1373 [copyright_label setBezeled: NO];
1374 [copyright_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001375 [copyright_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001376 [copyright_label setStringValue: [NSString stringWithFormat: @"%s",
1377 QEMU_COPYRIGHT]];
1378 [superView addSubview: copyright_label];
1379}
1380
John Arbucklee47ec1a2017-06-13 23:17:38 -04001381/* Used by the Speed menu items */
1382- (void)adjustSpeed:(id)sender
1383{
1384 int throttle_pct; /* throttle percentage */
1385 NSMenu *menu;
1386
1387 menu = [sender menu];
1388 if (menu != nil)
1389 {
1390 /* Unselect the currently selected item */
1391 for (NSMenuItem *item in [menu itemArray]) {
1392 if (item.state == NSOnState) {
1393 [item setState: NSOffState];
1394 break;
1395 }
1396 }
1397 }
1398
1399 // check the menu item
1400 [sender setState: NSOnState];
1401
1402 // get the throttle percentage
1403 throttle_pct = [sender tag];
1404
1405 cpu_throttle_set(throttle_pct);
1406 COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
1407}
1408
Programmingkidb4c6a112015-05-19 09:11:18 +01001409@end
bellard5b0753e2005-03-01 21:37:28 +00001410
thsc304f7e2008-01-22 23:25:15 +00001411
thsc304f7e2008-01-22 23:25:15 +00001412int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +00001413
thsc304f7e2008-01-22 23:25:15 +00001414 gArgc = argc;
1415 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +01001416 int i;
1417
1418 /* In case we don't need to display a window, let's not do that */
1419 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001420 const char *opt = argv[i];
1421
1422 if (opt[0] == '-') {
1423 /* Treat --foo the same as -foo. */
1424 if (opt[1] == '-') {
1425 opt++;
1426 }
Alexandre Raymond98514842011-05-29 18:22:49 -04001427 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
1428 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001429 !strcmp(opt, "-nographic") ||
1430 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001431 !strcmp(opt, "-curses") ||
Rainer Müllerb12a84c2015-09-09 16:08:30 +02001432 !strcmp(opt, "-display") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001433 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +02001434 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001435 }
Andreas Färberf4918802009-12-13 02:11:44 +01001436 }
1437 }
ths3b46e622007-09-17 08:09:54 +00001438
thsc304f7e2008-01-22 23:25:15 +00001439 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +00001440
Peter Maydell42a5dfe2013-04-22 10:29:47 +00001441 // Pull this console process up to being a fully-fledged graphical
1442 // app with a menubar and Dock icon
1443 ProcessSerialNumber psn = { 0, kCurrentProcess };
1444 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1445
1446 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +00001447
thsc304f7e2008-01-22 23:25:15 +00001448 // Add menus
1449 NSMenu *menu;
1450 NSMenuItem *menuItem;
1451
bellard5b0753e2005-03-01 21:37:28 +00001452 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +00001453
thsc304f7e2008-01-22 23:25:15 +00001454 // Application menu
1455 menu = [[NSMenu alloc] initWithTitle:@""];
Programmingkid9e8204b2016-08-15 22:11:06 -04001456 [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU
thsc304f7e2008-01-22 23:25:15 +00001457 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1458 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1459 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001460 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
thsc304f7e2008-01-22 23:25:15 +00001461 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1462 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1463 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1464 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1465 [menuItem setSubmenu:menu];
1466 [[NSApp mainMenu] addItem:menuItem];
1467 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001468
John Arbuckle8524f1c2015-06-19 10:53:27 +01001469 // Machine menu
1470 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1471 [menu setAutoenablesItems: NO];
1472 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1473 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1474 [menu addItem: menuItem];
1475 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001476 [menu addItem: [NSMenuItem separatorItem]];
1477 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1478 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001479 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1480 [menuItem setSubmenu:menu];
1481 [[NSApp mainMenu] addItem:menuItem];
1482
thsc304f7e2008-01-22 23:25:15 +00001483 // View menu
1484 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001485 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1486 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001487 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1488 [menuItem setSubmenu:menu];
1489 [[NSApp mainMenu] addItem:menuItem];
1490
John Arbucklee47ec1a2017-06-13 23:17:38 -04001491 // Speed menu
1492 menu = [[NSMenu alloc] initWithTitle:@"Speed"];
1493
1494 // Add the rest of the Speed menu items
1495 int p, percentage, throttle_pct;
1496 for (p = 10; p >= 0; p--)
1497 {
1498 percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
1499
1500 menuItem = [[[NSMenuItem alloc]
1501 initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
1502
1503 if (percentage == 100) {
1504 [menuItem setState: NSOnState];
1505 }
1506
1507 /* Calculate the throttle percentage */
1508 throttle_pct = -1 * percentage + 100;
1509
1510 [menuItem setTag: throttle_pct];
1511 [menu addItem: menuItem];
1512 }
1513 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
1514 [menuItem setSubmenu:menu];
1515 [[NSApp mainMenu] addItem:menuItem];
1516
thsc304f7e2008-01-22 23:25:15 +00001517 // Window menu
1518 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1519 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1520 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1521 [menuItem setSubmenu:menu];
1522 [[NSApp mainMenu] addItem:menuItem];
1523 [NSApp setWindowsMenu:menu];
1524
1525 // Help menu
1526 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1527 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
thsc304f7e2008-01-22 23:25:15 +00001528 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1529 [menuItem setSubmenu:menu];
1530 [[NSApp mainMenu] addItem:menuItem];
1531
1532 // Create an Application controller
1533 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1534 [NSApp setDelegate:appController];
1535
1536 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +00001537 [NSApp run];
ths3b46e622007-09-17 08:09:54 +00001538
thsc304f7e2008-01-22 23:25:15 +00001539 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001540 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001541
bellard5b0753e2005-03-01 21:37:28 +00001542 return 0;
1543}
thsc304f7e2008-01-22 23:25:15 +00001544
1545
1546
1547#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001548static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001549 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001550{
Peter Maydell6e657e62013-04-22 10:29:46 +00001551 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1552
thsc304f7e2008-01-22 23:25:15 +00001553 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1554
1555 NSRect rect;
1556 if ([cocoaView cdx] == 1.0) {
1557 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1558 } else {
1559 rect = NSMakeRect(
1560 x * [cocoaView cdx],
1561 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1562 w * [cocoaView cdx],
1563 h * [cocoaView cdy]);
1564 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001565 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001566
1567 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001568}
1569
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001570static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001571 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001572{
Peter Maydell6e657e62013-04-22 10:29:46 +00001573 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001574
Peter Maydell6e657e62013-04-22 10:29:46 +00001575 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001576 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001577 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001578}
1579
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001580static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001581{
Peter Maydell6e657e62013-04-22 10:29:46 +00001582 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1583
thsc304f7e2008-01-22 23:25:15 +00001584 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
John Arbuckle468a8952015-10-13 21:51:18 +01001585 graphic_hw_update(NULL);
thsc304f7e2008-01-22 23:25:15 +00001586
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001587 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001588 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001589 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001590 [cocoaView ungrabMouse];
1591 }
1592 }
1593 [cocoaView setAbsoluteEnabled:YES];
1594 }
1595
1596 NSDate *distantPast;
1597 NSEvent *event;
1598 distantPast = [NSDate distantPast];
1599 do {
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001600 event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:distantPast
thsc304f7e2008-01-22 23:25:15 +00001601 inMode: NSDefaultRunLoopMode dequeue:YES];
1602 if (event != nil) {
1603 [cocoaView handleEvent:event];
1604 }
1605 } while(event != nil);
Peter Maydell6e657e62013-04-22 10:29:46 +00001606 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001607}
1608
1609static void cocoa_cleanup(void)
1610{
1611 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001612 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001613}
1614
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001615static const DisplayChangeListenerOps dcl_ops = {
1616 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001617 .dpy_gfx_update = cocoa_update,
1618 .dpy_gfx_switch = cocoa_switch,
1619 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001620};
1621
Programmingkidb4c6a112015-05-19 09:11:18 +01001622/* Returns a name for a given console */
1623static NSString * getConsoleName(QemuConsole * console)
1624{
1625 return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
1626}
1627
1628/* Add an entry to the View menu for each console */
1629static void add_console_menu_entries(void)
1630{
1631 NSMenu *menu;
1632 NSMenuItem *menuItem;
1633 int index = 0;
1634
1635 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1636
1637 [menu addItem:[NSMenuItem separatorItem]];
1638
1639 while (qemu_console_lookup_by_index(index) != NULL) {
1640 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1641 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1642 [menuItem setTag: index];
1643 [menu addItem: menuItem];
1644 index++;
1645 }
1646}
1647
John Arbuckle693a3e02015-06-19 10:53:27 +01001648/* Make menu items for all removable devices.
1649 * Each device is given an 'Eject' and 'Change' menu item.
1650 */
John Arbucklea7940ec2015-10-13 21:51:18 +01001651static void addRemovableDevicesMenuItems(void)
John Arbuckle693a3e02015-06-19 10:53:27 +01001652{
1653 NSMenu *menu;
1654 NSMenuItem *menuItem;
1655 BlockInfoList *currentDevice, *pointerToFree;
1656 NSString *deviceName;
1657
1658 currentDevice = qmp_query_block(NULL);
1659 pointerToFree = currentDevice;
1660 if(currentDevice == NULL) {
1661 NSBeep();
1662 QEMU_Alert(@"Failed to query for block devices!");
1663 return;
1664 }
1665
1666 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1667
1668 // Add a separator between related groups of menu items
1669 [menu addItem:[NSMenuItem separatorItem]];
1670
1671 // Set the attributes to the "Removable Media" menu item
1672 NSString *titleString = @"Removable Media";
1673 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1674 NSColor *newColor = [NSColor blackColor];
1675 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1676 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1677 traits:NSBoldFontMask|NSItalicFontMask
1678 weight:0
1679 size:14];
1680 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1681 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1682 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1683
1684 // Add the "Removable Media" menu item
1685 menuItem = [NSMenuItem new];
1686 [menuItem setAttributedTitle: attString];
1687 [menuItem setEnabled: NO];
1688 [menu addItem: menuItem];
1689
Stefan Weilcb8d4c82016-03-23 15:59:57 +01001690 /* Loop through all the block devices in the emulator */
John Arbuckle693a3e02015-06-19 10:53:27 +01001691 while (currentDevice) {
1692 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1693
1694 if(currentDevice->value->removable) {
1695 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1696 action: @selector(changeDeviceMedia:)
1697 keyEquivalent: @""];
1698 [menu addItem: menuItem];
1699 [menuItem setRepresentedObject: deviceName];
1700 [menuItem autorelease];
1701
1702 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1703 action: @selector(ejectDeviceMedia:)
1704 keyEquivalent: @""];
1705 [menu addItem: menuItem];
1706 [menuItem setRepresentedObject: deviceName];
1707 [menuItem autorelease];
1708 }
1709 currentDevice = currentDevice->next;
1710 }
1711 qapi_free_BlockInfoList(pointerToFree);
1712}
1713
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01001714static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
thsc304f7e2008-01-22 23:25:15 +00001715{
1716 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1717
Programmingkid43227af2015-05-19 09:11:17 +01001718 /* if fullscreen mode is to be used */
Gerd Hoffmann767f9bf2018-02-02 12:10:19 +01001719 if (opts->has_full_screen && opts->full_screen) {
Programmingkid43227af2015-05-19 09:11:17 +01001720 [NSApp activateIgnoringOtherApps: YES];
1721 [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
1722 }
1723
Blue Swirl58a06672011-08-21 18:42:08 +00001724 dcl = g_malloc0(sizeof(DisplayChangeListener));
1725
aliguori9794f742009-03-04 19:25:22 +00001726 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001727 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001728 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001729
1730 // register cleanup function
1731 atexit(cocoa_cleanup);
Programmingkidb4c6a112015-05-19 09:11:18 +01001732
1733 /* At this point QEMU has created all the consoles, so we can add View
1734 * menu entries for them.
1735 */
1736 add_console_menu_entries();
John Arbuckle693a3e02015-06-19 10:53:27 +01001737
1738 /* Give all removable devices a menu item.
1739 * Has to be called after QEMU has started to
1740 * find out what removable devices it has.
1741 */
1742 addRemovableDevicesMenuItems();
thsc304f7e2008-01-22 23:25:15 +00001743}
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01001744
1745static QemuDisplay qemu_display_cocoa = {
1746 .type = DISPLAY_TYPE_COCOA,
1747 .init = cocoa_display_init,
1748};
1749
1750static void register_cocoa(void)
1751{
1752 qemu_display_register(&qemu_display_cocoa);
1753}
1754
1755type_init(register_cocoa);