blob: 1f010d3ae7f5ff6c48a22d4fceda6c6b07e2fef5 [file] [log] [blame]
bellard5b0753e2005-03-01 21:37:28 +00001/*
thsc304f7e2008-01-22 23:25:15 +00002 * QEMU Cocoa CG display driver
ths5fafdf22007-09-16 21:08:06 +00003 *
thsc304f7e2008-01-22 23:25:15 +00004 * Copyright (c) 2008 Mike Kronenberg
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard5b0753e2005-03-01 21:37:28 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
bellardda4dbf72005-03-02 22:22:43 +000024
Peter Maydelle4a096b2016-01-29 16:23:34 +000025#include "qemu/osdep.h"
26
bellard5b0753e2005-03-01 21:37:28 +000027#import <Cocoa/Cocoa.h>
Andreas Färber3bbbee12011-05-29 19:42:51 +020028#include <crt_externs.h>
bellard5b0753e2005-03-01 21:37:28 +000029
pbrook87ecb682007-11-17 17:14:51 +000030#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010031#include "ui/console.h"
Gerd Hoffmann21bae112013-12-04 14:08:04 +010032#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
John Arbuckle8524f1c2015-06-19 10:53:27 +010034#include "qmp-commands.h"
John Arbuckle693a3e02015-06-19 10:53:27 +010035#include "sysemu/blockdev.h"
Programmingkid9e8204b2016-08-15 22:11:06 -040036#include "qemu-version.h"
John Arbuckleaaac7142016-03-23 14:26:18 +000037#include <Carbon/Carbon.h>
bellardda4dbf72005-03-02 22:22:43 +000038
Andreas Färber44e4c0b2009-12-13 00:45:40 +010039#ifndef MAC_OS_X_VERSION_10_5
40#define MAC_OS_X_VERSION_10_5 1050
41#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000042#ifndef MAC_OS_X_VERSION_10_6
43#define MAC_OS_X_VERSION_10_6 1060
44#endif
Peter Maydell81801ae2015-05-19 09:11:18 +010045#ifndef MAC_OS_X_VERSION_10_10
46#define MAC_OS_X_VERSION_10_10 101000
47#endif
Brendan Shanks4ba967a2017-04-24 23:29:52 -070048#ifndef MAC_OS_X_VERSION_10_12
49#define MAC_OS_X_VERSION_10_12 101200
50#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010051
Brendan Shanks4ba967a2017-04-24 23:29:52 -070052/* macOS 10.12 deprecated many constants, #define the new names for older SDKs */
53#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
54#define NSEventMaskAny NSAnyEventMask
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -070055#define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
56#define NSEventModifierFlagShift NSShiftKeyMask
Brendan Shanks4ba967a2017-04-24 23:29:52 -070057#define NSEventModifierFlagCommand NSCommandKeyMask
58#define NSEventModifierFlagControl NSControlKeyMask
59#define NSEventModifierFlagOption NSAlternateKeyMask
60#define NSEventTypeFlagsChanged NSFlagsChanged
61#define NSEventTypeKeyUp NSKeyUp
62#define NSEventTypeKeyDown NSKeyDown
63#define NSEventTypeMouseMoved NSMouseMoved
64#define NSEventTypeLeftMouseDown NSLeftMouseDown
65#define NSEventTypeRightMouseDown NSRightMouseDown
66#define NSEventTypeOtherMouseDown NSOtherMouseDown
67#define NSEventTypeLeftMouseDragged NSLeftMouseDragged
68#define NSEventTypeRightMouseDragged NSRightMouseDragged
69#define NSEventTypeOtherMouseDragged NSOtherMouseDragged
70#define NSEventTypeLeftMouseUp NSLeftMouseUp
71#define NSEventTypeRightMouseUp NSRightMouseUp
72#define NSEventTypeOtherMouseUp NSOtherMouseUp
73#define NSEventTypeScrollWheel NSScrollWheel
74#define NSTextAlignmentCenter NSCenterTextAlignment
75#define NSWindowStyleMaskBorderless NSBorderlessWindowMask
76#define NSWindowStyleMaskClosable NSClosableWindowMask
77#define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
78#define NSWindowStyleMaskTitled NSTitledWindowMask
79#endif
bellard5b0753e2005-03-01 21:37:28 +000080
thsc304f7e2008-01-22 23:25:15 +000081//#define DEBUG
82
83#ifdef DEBUG
84#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
85#else
86#define COCOA_DEBUG(...) ((void) 0)
87#endif
88
89#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000090
91typedef struct {
92 int width;
93 int height;
94 int bitsPerComponent;
95 int bitsPerPixel;
96} QEMUScreen;
97
Programmingkid9e8204b2016-08-15 22:11:06 -040098NSWindow *normalWindow, *about_window;
aliguori9794f742009-03-04 19:25:22 +000099static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100100static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +0000101
102int gArgc;
103char **gArgv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100104bool stretch_video;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100105NSTextField *pauseLabel;
John Arbuckle693a3e02015-06-19 10:53:27 +0100106NSArray * supportedImageFileTypes;
bellard5b0753e2005-03-01 21:37:28 +0000107
John Arbuckleaaac7142016-03-23 14:26:18 +0000108// Mac to QKeyCode conversion
109const int mac_to_qkeycode_map[] = {
110 [kVK_ANSI_A] = Q_KEY_CODE_A,
111 [kVK_ANSI_B] = Q_KEY_CODE_B,
112 [kVK_ANSI_C] = Q_KEY_CODE_C,
113 [kVK_ANSI_D] = Q_KEY_CODE_D,
114 [kVK_ANSI_E] = Q_KEY_CODE_E,
115 [kVK_ANSI_F] = Q_KEY_CODE_F,
116 [kVK_ANSI_G] = Q_KEY_CODE_G,
117 [kVK_ANSI_H] = Q_KEY_CODE_H,
118 [kVK_ANSI_I] = Q_KEY_CODE_I,
119 [kVK_ANSI_J] = Q_KEY_CODE_J,
120 [kVK_ANSI_K] = Q_KEY_CODE_K,
121 [kVK_ANSI_L] = Q_KEY_CODE_L,
122 [kVK_ANSI_M] = Q_KEY_CODE_M,
123 [kVK_ANSI_N] = Q_KEY_CODE_N,
124 [kVK_ANSI_O] = Q_KEY_CODE_O,
125 [kVK_ANSI_P] = Q_KEY_CODE_P,
126 [kVK_ANSI_Q] = Q_KEY_CODE_Q,
127 [kVK_ANSI_R] = Q_KEY_CODE_R,
128 [kVK_ANSI_S] = Q_KEY_CODE_S,
129 [kVK_ANSI_T] = Q_KEY_CODE_T,
130 [kVK_ANSI_U] = Q_KEY_CODE_U,
131 [kVK_ANSI_V] = Q_KEY_CODE_V,
132 [kVK_ANSI_W] = Q_KEY_CODE_W,
133 [kVK_ANSI_X] = Q_KEY_CODE_X,
134 [kVK_ANSI_Y] = Q_KEY_CODE_Y,
135 [kVK_ANSI_Z] = Q_KEY_CODE_Z,
ths3b46e622007-09-17 08:09:54 +0000136
John Arbuckleaaac7142016-03-23 14:26:18 +0000137 [kVK_ANSI_0] = Q_KEY_CODE_0,
138 [kVK_ANSI_1] = Q_KEY_CODE_1,
139 [kVK_ANSI_2] = Q_KEY_CODE_2,
140 [kVK_ANSI_3] = Q_KEY_CODE_3,
141 [kVK_ANSI_4] = Q_KEY_CODE_4,
142 [kVK_ANSI_5] = Q_KEY_CODE_5,
143 [kVK_ANSI_6] = Q_KEY_CODE_6,
144 [kVK_ANSI_7] = Q_KEY_CODE_7,
145 [kVK_ANSI_8] = Q_KEY_CODE_8,
146 [kVK_ANSI_9] = Q_KEY_CODE_9,
147
148 [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
149 [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
150 [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
151 [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
152 [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
153 [kVK_Tab] = Q_KEY_CODE_TAB,
154 [kVK_Return] = Q_KEY_CODE_RET,
155 [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
156 [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
157 [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
158 [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
159 [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
160 [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
161 [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
162 [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
163 [kVK_Shift] = Q_KEY_CODE_SHIFT,
164 [kVK_RightShift] = Q_KEY_CODE_SHIFT_R,
165 [kVK_Control] = Q_KEY_CODE_CTRL,
166 [kVK_RightControl] = Q_KEY_CODE_CTRL_R,
167 [kVK_Option] = Q_KEY_CODE_ALT,
168 [kVK_RightOption] = Q_KEY_CODE_ALT_R,
169 [kVK_Command] = Q_KEY_CODE_META_L,
170 [0x36] = Q_KEY_CODE_META_R, /* There is no kVK_RightCommand */
171 [kVK_Space] = Q_KEY_CODE_SPC,
172
173 [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
174 [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
175 [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
176 [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
177 [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
178 [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
179 [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
180 [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
181 [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
182 [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
183 [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
184 [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
185 [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
186 [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
187 [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
188 [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
189 [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
190 [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
191
192 [kVK_UpArrow] = Q_KEY_CODE_UP,
193 [kVK_DownArrow] = Q_KEY_CODE_DOWN,
194 [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
195 [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
196
197 [kVK_Help] = Q_KEY_CODE_INSERT,
198 [kVK_Home] = Q_KEY_CODE_HOME,
199 [kVK_PageUp] = Q_KEY_CODE_PGUP,
200 [kVK_PageDown] = Q_KEY_CODE_PGDN,
201 [kVK_End] = Q_KEY_CODE_END,
202 [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
203
204 [kVK_Escape] = Q_KEY_CODE_ESC,
205
206 /* The Power key can't be used directly because the operating system uses
207 * it. This key can be emulated by using it in place of another key such as
208 * F1. Don't forget to disable the real key binding.
209 */
210 /* [kVK_F1] = Q_KEY_CODE_POWER, */
211
212 [kVK_F1] = Q_KEY_CODE_F1,
213 [kVK_F2] = Q_KEY_CODE_F2,
214 [kVK_F3] = Q_KEY_CODE_F3,
215 [kVK_F4] = Q_KEY_CODE_F4,
216 [kVK_F5] = Q_KEY_CODE_F5,
217 [kVK_F6] = Q_KEY_CODE_F6,
218 [kVK_F7] = Q_KEY_CODE_F7,
219 [kVK_F8] = Q_KEY_CODE_F8,
220 [kVK_F9] = Q_KEY_CODE_F9,
221 [kVK_F10] = Q_KEY_CODE_F10,
222 [kVK_F11] = Q_KEY_CODE_F11,
223 [kVK_F12] = Q_KEY_CODE_F12,
224 [kVK_F13] = Q_KEY_CODE_PRINT,
225 [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
226 [kVK_F15] = Q_KEY_CODE_PAUSE,
227
228 /*
229 * The eject and volume keys can't be used here because they are handled at
230 * a lower level than what an Application can see.
231 */
bellard5b0753e2005-03-01 21:37:28 +0000232};
233
Andreas Färber77047bb2009-12-13 00:55:53 +0100234static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000235{
John Arbuckleaaac7142016-03-23 14:26:18 +0000236 if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000237 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000238 return 0;
239 }
John Arbuckleaaac7142016-03-23 14:26:18 +0000240 return mac_to_qkeycode_map[keycode];
bellard5b0753e2005-03-01 21:37:28 +0000241}
242
John Arbuckle693a3e02015-06-19 10:53:27 +0100243/* Displays an alert dialog box with the specified message */
244static void QEMU_Alert(NSString *message)
245{
246 NSAlert *alert;
247 alert = [NSAlert new];
248 [alert setMessageText: message];
249 [alert runModal];
250}
thsc304f7e2008-01-22 23:25:15 +0000251
John Arbuckle693a3e02015-06-19 10:53:27 +0100252/* Handles any errors that happen with a device transaction */
253static void handleAnyDeviceErrors(Error * err)
254{
255 if (err) {
256 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
257 encoding: NSASCIIStringEncoding]);
258 error_free(err);
259 }
260}
thsc304f7e2008-01-22 23:25:15 +0000261
bellard5b0753e2005-03-01 21:37:28 +0000262/*
263 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000264 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000265 ------------------------------------------------------
266*/
thsc304f7e2008-01-22 23:25:15 +0000267@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000268{
thsc304f7e2008-01-22 23:25:15 +0000269 QEMUScreen screen;
270 NSWindow *fullScreenWindow;
271 float cx,cy,cw,ch,cdx,cdy;
272 CGDataProviderRef dataProviderRef;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700273 BOOL modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000274 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000275 BOOL isFullscreen;
276 BOOL isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100277 BOOL isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000278}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100279- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000280- (void) grabMouse;
281- (void) ungrabMouse;
282- (void) toggleFullScreen:(id)sender;
283- (void) handleEvent:(NSEvent *)event;
284- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100285/* The state surrounding mouse grabbing is potentially confusing.
286 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
287 * pointing device an absolute-position one?"], but is only updated on
288 * next refresh.
289 * isMouseGrabbed tracks whether GUI events are directed to the guest;
290 * it controls whether special keys like Cmd get sent to the guest,
291 * and whether we capture the mouse when in non-absolute mode.
292 * isMouseDeassociated tracks whether we've told MacOSX to disassociate
293 * the mouse and mouse cursor position by calling
294 * CGAssociateMouseAndMouseCursorPosition(FALSE)
295 * (which basically happens if we grab in non-absolute mode).
296 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000297- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000298- (BOOL) isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100299- (BOOL) isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000300- (float) cdx;
301- (float) cdy;
302- (QEMUScreen) gscreen;
John Arbuckle3b178b72015-09-25 23:14:00 +0100303- (void) raiseAllKeys;
thsc304f7e2008-01-22 23:25:15 +0000304@end
ths3b46e622007-09-17 08:09:54 +0000305
Andreas Färber7fee1992011-06-09 20:53:32 +0200306QemuCocoaView *cocoaView;
307
thsc304f7e2008-01-22 23:25:15 +0000308@implementation QemuCocoaView
309- (id)initWithFrame:(NSRect)frameRect
310{
311 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000312
thsc304f7e2008-01-22 23:25:15 +0000313 self = [super initWithFrame:frameRect];
314 if (self) {
pbrook95219892006-04-09 01:06:34 +0000315
thsc304f7e2008-01-22 23:25:15 +0000316 screen.bitsPerComponent = 8;
317 screen.bitsPerPixel = 32;
318 screen.width = frameRect.size.width;
319 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000320
thsc304f7e2008-01-22 23:25:15 +0000321 }
322 return self;
323}
bellard7c206a72005-12-18 19:18:45 +0000324
thsc304f7e2008-01-22 23:25:15 +0000325- (void) dealloc
326{
327 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000328
thsc304f7e2008-01-22 23:25:15 +0000329 if (dataProviderRef)
330 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000331
thsc304f7e2008-01-22 23:25:15 +0000332 [super dealloc];
333}
bellard5cbfcd02006-06-14 15:53:24 +0000334
Andreas Färberd50f71d2009-12-13 02:03:33 +0100335- (BOOL) isOpaque
336{
337 return YES;
338}
339
Peter Maydell5dd45be2014-06-23 10:35:23 +0100340- (BOOL) screenContainsPoint:(NSPoint) p
341{
342 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
343}
344
Peter Maydell13aefd32014-06-23 10:35:25 +0100345- (void) hideCursor
346{
347 if (!cursor_hide) {
348 return;
349 }
350 [NSCursor hide];
351}
352
353- (void) unhideCursor
354{
355 if (!cursor_hide) {
356 return;
357 }
358 [NSCursor unhide];
359}
360
thsc304f7e2008-01-22 23:25:15 +0000361- (void) drawRect:(NSRect) rect
362{
363 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000364
thsc304f7e2008-01-22 23:25:15 +0000365 // get CoreGraphic context
366 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
367 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
368 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000369
thsc304f7e2008-01-22 23:25:15 +0000370 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000371 if (!dataProviderRef) {
372 // Draw request before any guest device has set up a framebuffer:
373 // just draw an opaque black rectangle
374 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
375 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
376 } else {
thsc304f7e2008-01-22 23:25:15 +0000377 CGImageRef imageRef = CGImageCreate(
378 screen.width, //width
379 screen.height, //height
380 screen.bitsPerComponent, //bitsPerComponent
381 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000382 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100383#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000384 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000385 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000386#else
387 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
388 kCGImageAlphaNoneSkipFirst, //bitmapInfo
389#endif
390 dataProviderRef, //provider
391 NULL, //decode
392 0, //interpolate
393 kCGRenderingIntentDefault //intent
394 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100395 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
396 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100397 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100398 int i;
399 CGImageRef clipImageRef;
400 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000401
Peter Maydellb63901d2015-05-19 09:11:17 +0100402 [self getRectsBeingDrawn:&rectList count:&rectCount];
403 for (i = 0; i < rectCount; i++) {
404 clipRect.origin.x = rectList[i].origin.x / cdx;
405 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
406 clipRect.size.width = rectList[i].size.width / cdx;
407 clipRect.size.height = rectList[i].size.height / cdy;
408 clipImageRef = CGImageCreateWithImageInRect(
409 imageRef,
410 clipRect
411 );
412 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
413 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000414 }
thsc304f7e2008-01-22 23:25:15 +0000415 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000416 }
417}
418
thsc304f7e2008-01-22 23:25:15 +0000419- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000420{
thsc304f7e2008-01-22 23:25:15 +0000421 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000422
thsc304f7e2008-01-22 23:25:15 +0000423 if (isFullscreen) {
424 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
425 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100426
427 /* stretches video, but keeps same aspect ratio */
428 if (stretch_video == true) {
429 /* use smallest stretch value - prevents clipping on sides */
430 if (MIN(cdx, cdy) == cdx) {
431 cdy = cdx;
432 } else {
433 cdx = cdy;
434 }
435 } else { /* No stretching */
436 cdx = cdy = 1;
437 }
thsc304f7e2008-01-22 23:25:15 +0000438 cw = screen.width * cdx;
439 ch = screen.height * cdy;
440 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
441 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
442 } else {
443 cx = 0;
444 cy = 0;
445 cw = screen.width;
446 ch = screen.height;
447 cdx = 1.0;
448 cdy = 1.0;
449 }
bellard5b0753e2005-03-01 21:37:28 +0000450}
451
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100452- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000453{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100454 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000455
Peter Maydell8510d912013-03-18 20:28:21 +0000456 int w = surface_width(surface);
457 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100458 /* cdx == 0 means this is our very first surface, in which case we need
459 * to recalculate the content dimensions even if it happens to be the size
460 * of the initial empty window.
461 */
462 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000463
464 int oldh = screen.height;
465 if (isResize) {
466 // Resize before we trigger the redraw, or we'll redraw at the wrong size
467 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
468 screen.width = w;
469 screen.height = h;
470 [self setContentDimensions];
471 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
472 }
Peter Maydell8510d912013-03-18 20:28:21 +0000473
thsc304f7e2008-01-22 23:25:15 +0000474 // update screenBuffer
475 if (dataProviderRef)
476 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000477
aliguori9794f742009-03-04 19:25:22 +0000478 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000479 screen.bitsPerPixel = surface_bits_per_pixel(surface);
480 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000481
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100482 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000483
484 // update windows
485 if (isFullscreen) {
486 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000487 [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 +0000488 } else {
489 if (qemu_name)
490 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000491 [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 +0000492 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000493
494 if (isResize) {
495 [normalWindow center];
496 }
thsc304f7e2008-01-22 23:25:15 +0000497}
498
499- (void) toggleFullScreen:(id)sender
500{
501 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
502
503 if (isFullscreen) { // switch from fullscreen to desktop
504 isFullscreen = FALSE;
505 [self ungrabMouse];
506 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000507 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
508 [self exitFullScreenModeWithOptions:nil];
509 } else {
thsc304f7e2008-01-22 23:25:15 +0000510 [fullScreenWindow close];
511 [normalWindow setContentView: self];
512 [normalWindow makeKeyAndOrderFront: self];
513 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000514 }
thsc304f7e2008-01-22 23:25:15 +0000515 } else { // switch from desktop to fullscreen
516 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100517 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000518 [self grabMouse];
519 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000520 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
521 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
522 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
523 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
524 nil]];
525 } else {
thsc304f7e2008-01-22 23:25:15 +0000526 [NSMenu setMenuBarVisible:NO];
527 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700528 styleMask:NSWindowStyleMaskBorderless
thsc304f7e2008-01-22 23:25:15 +0000529 backing:NSBackingStoreBuffered
530 defer:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100531 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
thsc304f7e2008-01-22 23:25:15 +0000532 [fullScreenWindow setHasShadow:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100533 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
534 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
535 [[fullScreenWindow contentView] addSubview: self];
thsc304f7e2008-01-22 23:25:15 +0000536 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000537 }
thsc304f7e2008-01-22 23:25:15 +0000538 }
539}
540
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700541- (void) toggleModifier: (int)keycode {
542 // Toggle the stored state.
543 modifiers_state[keycode] = !modifiers_state[keycode];
544 // Send a keyup or keydown depending on the state.
545 qemu_input_event_send_key_qcode(dcl->con, keycode, modifiers_state[keycode]);
546}
547
548- (void) toggleStatefulModifier: (int)keycode {
549 // Toggle the stored state.
550 modifiers_state[keycode] = !modifiers_state[keycode];
551 // Generate keydown and keyup.
552 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
553 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
554}
555
thsc304f7e2008-01-22 23:25:15 +0000556- (void) handleEvent:(NSEvent *)event
557{
558 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
559
560 int buttons = 0;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700561 int keycode = 0;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100562 bool mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000563 NSPoint p = [event locationInWindow];
564
565 switch ([event type]) {
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700566 case NSEventTypeFlagsChanged:
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700567 if ([event keyCode] == 0) {
568 // When the Cocoa keyCode is zero that means keys should be
569 // synthesized based on the values in in the eventModifiers
570 // bitmask.
571
572 if (qemu_console_is_graphic(NULL)) {
573 NSEventModifierFlags modifiers = [event modifierFlags];
574
575 if (!!(modifiers & NSEventModifierFlagCapsLock) != !!modifiers_state[Q_KEY_CODE_CAPS_LOCK]) {
576 [self toggleStatefulModifier:Q_KEY_CODE_CAPS_LOCK];
577 }
578 if (!!(modifiers & NSEventModifierFlagShift) != !!modifiers_state[Q_KEY_CODE_SHIFT]) {
579 [self toggleModifier:Q_KEY_CODE_SHIFT];
580 }
581 if (!!(modifiers & NSEventModifierFlagControl) != !!modifiers_state[Q_KEY_CODE_CTRL]) {
582 [self toggleModifier:Q_KEY_CODE_CTRL];
583 }
584 if (!!(modifiers & NSEventModifierFlagOption) != !!modifiers_state[Q_KEY_CODE_ALT]) {
585 [self toggleModifier:Q_KEY_CODE_ALT];
586 }
587 if (!!(modifiers & NSEventModifierFlagCommand) != !!modifiers_state[Q_KEY_CODE_META_L]) {
588 [self toggleModifier:Q_KEY_CODE_META_L];
589 }
590 }
591 } else {
592 keycode = cocoa_keycode_to_qemu([event keyCode]);
593 }
Peter Maydell88959192013-12-08 22:59:02 +0000594
John Arbuckleaaac7142016-03-23 14:26:18 +0000595 if ((keycode == Q_KEY_CODE_META_L || keycode == Q_KEY_CODE_META_R)
596 && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000597 /* Don't pass command key changes to guest unless mouse is grabbed */
598 keycode = 0;
599 }
600
thsc304f7e2008-01-22 23:25:15 +0000601 if (keycode) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000602 // emulate caps lock and num lock keydown and keyup
603 if (keycode == Q_KEY_CODE_CAPS_LOCK ||
604 keycode == Q_KEY_CODE_NUM_LOCK) {
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700605 [self toggleStatefulModifier:keycode];
Peter Maydell68c0aa62013-04-17 09:16:35 +0000606 } else if (qemu_console_is_graphic(NULL)) {
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700607 [self toggleModifier:keycode];
thsc304f7e2008-01-22 23:25:15 +0000608 }
609 }
610
611 // release Mouse grab when pressing ctrl+alt
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700612 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
thsc304f7e2008-01-22 23:25:15 +0000613 [self ungrabMouse];
614 }
615 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700616 case NSEventTypeKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000617 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000618
Peter Maydell88959192013-12-08 22:59:02 +0000619 // forward command key combos to the host UI unless the mouse is grabbed
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700620 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
thsc304f7e2008-01-22 23:25:15 +0000621 [NSApp sendEvent:event];
622 return;
623 }
624
625 // default
thsc304f7e2008-01-22 23:25:15 +0000626
627 // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700628 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
thsc304f7e2008-01-22 23:25:15 +0000629 switch (keycode) {
630
631 // enable graphic console
John Arbuckleaaac7142016-03-23 14:26:18 +0000632 case Q_KEY_CODE_1 ... Q_KEY_CODE_9: // '1' to '9' keys
633 console_select(keycode - 11);
thsc304f7e2008-01-22 23:25:15 +0000634 break;
635 }
636
637 // handle keys for graphic console
Peter Maydell68c0aa62013-04-17 09:16:35 +0000638 } else if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000639 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000640
641 // handlekeys for Monitor
642 } else {
643 int keysym = 0;
644 switch([event keyCode]) {
645 case 115:
646 keysym = QEMU_KEY_HOME;
647 break;
648 case 117:
649 keysym = QEMU_KEY_DELETE;
650 break;
651 case 119:
652 keysym = QEMU_KEY_END;
653 break;
654 case 123:
655 keysym = QEMU_KEY_LEFT;
656 break;
657 case 124:
658 keysym = QEMU_KEY_RIGHT;
659 break;
660 case 125:
661 keysym = QEMU_KEY_DOWN;
662 break;
663 case 126:
664 keysym = QEMU_KEY_UP;
665 break;
666 default:
667 {
668 NSString *ks = [event characters];
669 if ([ks length] > 0)
670 keysym = [ks characterAtIndex:0];
671 }
672 }
673 if (keysym)
674 kbd_put_keysym(keysym);
675 }
676 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700677 case NSEventTypeKeyUp:
thsc304f7e2008-01-22 23:25:15 +0000678 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000679
680 // don't pass the guest a spurious key-up if we treated this
681 // command-key combo as a host UI action
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700682 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell88959192013-12-08 22:59:02 +0000683 return;
684 }
685
Peter Maydell68c0aa62013-04-17 09:16:35 +0000686 if (qemu_console_is_graphic(NULL)) {
John Arbuckleaaac7142016-03-23 14:26:18 +0000687 qemu_input_event_send_key_qcode(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000688 }
689 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700690 case NSEventTypeMouseMoved:
thsc304f7e2008-01-22 23:25:15 +0000691 if (isAbsoluteEnabled) {
Peter Maydell5dd45be2014-06-23 10:35:23 +0100692 if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
Peter Maydellf61c3872014-06-23 10:35:24 +0100693 if (isMouseGrabbed) {
694 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +0000695 }
696 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +0100697 if (!isMouseGrabbed) {
698 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000699 }
700 }
701 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100702 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000703 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700704 case NSEventTypeLeftMouseDown:
705 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000706 buttons |= MOUSE_EVENT_RBUTTON;
707 } else {
708 buttons |= MOUSE_EVENT_LBUTTON;
709 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100710 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000711 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700712 case NSEventTypeRightMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000713 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100714 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000715 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700716 case NSEventTypeOtherMouseDown:
thsc304f7e2008-01-22 23:25:15 +0000717 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100718 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000719 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700720 case NSEventTypeLeftMouseDragged:
721 if ([event modifierFlags] & NSEventModifierFlagCommand) {
thsc304f7e2008-01-22 23:25:15 +0000722 buttons |= MOUSE_EVENT_RBUTTON;
723 } else {
724 buttons |= MOUSE_EVENT_LBUTTON;
725 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100726 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000727 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700728 case NSEventTypeRightMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000729 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100730 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000731 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700732 case NSEventTypeOtherMouseDragged:
thsc304f7e2008-01-22 23:25:15 +0000733 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100734 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000735 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700736 case NSEventTypeLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +0100737 mouse_event = true;
738 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
Programmingkid9e8204b2016-08-15 22:11:06 -0400739 if([[self window] isKeyWindow]) {
740 [self grabMouse];
741 }
thsc304f7e2008-01-22 23:25:15 +0000742 }
743 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700744 case NSEventTypeRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100745 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000746 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700747 case NSEventTypeOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100748 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000749 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700750 case NSEventTypeScrollWheel:
Peter Maydellf61c3872014-06-23 10:35:24 +0100751 if (isMouseGrabbed) {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100752 buttons |= ([event deltaY] < 0) ?
753 MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
thsc304f7e2008-01-22 23:25:15 +0000754 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100755 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000756 break;
757 default:
758 [NSApp sendEvent:event];
759 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100760
761 if (mouse_event) {
Peter Maydell8d3a5d92015-11-26 15:19:28 +0000762 /* Don't send button events to the guest unless we've got a
763 * mouse grab or window focus. If we have neither then this event
764 * is the user clicking on the background window to activate and
765 * bring us to the front, which will be done by the sendEvent
766 * call below. We definitely don't want to pass that click through
767 * to the guest.
768 */
769 if ((isMouseGrabbed || [[self window] isKeyWindow]) &&
770 (last_buttons != buttons)) {
Eric Blake7fb1cf12015-11-18 01:52:57 -0700771 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100772 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
773 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
774 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100775 [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP,
776 [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100777 };
778 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
779 last_buttons = buttons;
780 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100781 if (isMouseGrabbed) {
782 if (isAbsoluteEnabled) {
783 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
784 * The check on screenContainsPoint is to avoid sending out of range values for
785 * clicks in the titlebar.
786 */
787 if ([self screenContainsPoint:p]) {
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200788 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, 0, screen.width);
789 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
Peter Maydellf61c3872014-06-23 10:35:24 +0100790 }
791 } else {
792 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
793 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
794 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100795 } else {
796 [NSApp sendEvent:event];
797 }
798 qemu_input_event_sync();
799 }
thsc304f7e2008-01-22 23:25:15 +0000800}
801
802- (void) grabMouse
803{
804 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
805
806 if (!isFullscreen) {
807 if (qemu_name)
808 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
809 else
810 [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
811 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100812 [self hideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100813 if (!isAbsoluteEnabled) {
814 isMouseDeassociated = TRUE;
815 CGAssociateMouseAndMouseCursorPosition(FALSE);
816 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000817 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000818}
819
820- (void) ungrabMouse
821{
822 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
823
824 if (!isFullscreen) {
825 if (qemu_name)
826 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
827 else
828 [normalWindow setTitle:@"QEMU"];
829 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100830 [self unhideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100831 if (isMouseDeassociated) {
832 CGAssociateMouseAndMouseCursorPosition(TRUE);
833 isMouseDeassociated = FALSE;
834 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000835 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000836}
837
838- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000839- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000840- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
Peter Maydellf61c3872014-06-23 10:35:24 +0100841- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
thsc304f7e2008-01-22 23:25:15 +0000842- (float) cdx {return cdx;}
843- (float) cdy {return cdy;}
844- (QEMUScreen) gscreen {return screen;}
John Arbuckle3b178b72015-09-25 23:14:00 +0100845
846/*
847 * Makes the target think all down keys are being released.
848 * This prevents a stuck key problem, since we will not see
849 * key up events for those keys after we have lost focus.
850 */
851- (void) raiseAllKeys
852{
853 int index;
854 const int max_index = ARRAY_SIZE(modifiers_state);
855
856 for (index = 0; index < max_index; index++) {
857 if (modifiers_state[index]) {
858 modifiers_state[index] = 0;
John Arbuckleaaac7142016-03-23 14:26:18 +0000859 qemu_input_event_send_key_qcode(dcl->con, index, false);
John Arbuckle3b178b72015-09-25 23:14:00 +0100860 }
861 }
862}
bellard5b0753e2005-03-01 21:37:28 +0000863@end
864
865
thsc304f7e2008-01-22 23:25:15 +0000866
bellard5b0753e2005-03-01 21:37:28 +0000867/*
868 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000869 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000870 ------------------------------------------------------
871*/
thsc304f7e2008-01-22 23:25:15 +0000872@interface QemuCocoaAppController : NSObject
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100873#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
John Arbuckled9bc14f2015-09-25 23:13:59 +0100874 <NSWindowDelegate, NSApplicationDelegate>
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100875#endif
bellard5b0753e2005-03-01 21:37:28 +0000876{
877}
bellard5b0753e2005-03-01 21:37:28 +0000878- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100879- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +0000880- (void)toggleFullScreen:(id)sender;
881- (void)showQEMUDoc:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100882- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +0100883- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100884- (void)pauseQEMU:(id)sender;
885- (void)resumeQEMU:(id)sender;
886- (void)displayPause;
887- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +0100888- (void)restartQEMU:(id)sender;
889- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +0100890- (void)ejectDeviceMedia:(id)sender;
891- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +0100892- (BOOL)verifyQuit;
John Arbucklef4747902016-03-23 14:26:17 +0000893- (void)openDocumentation:(NSString *)filename;
Programmingkid9e8204b2016-08-15 22:11:06 -0400894- (IBAction) do_about_menu_item: (id) sender;
895- (void)make_about_window;
bellard5b0753e2005-03-01 21:37:28 +0000896@end
897
thsc304f7e2008-01-22 23:25:15 +0000898@implementation QemuCocoaAppController
899- (id) init
900{
901 COCOA_DEBUG("QemuCocoaAppController: init\n");
902
903 self = [super init];
904 if (self) {
905
906 // create a view and add it to the window
907 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
908 if(!cocoaView) {
909 fprintf(stderr, "(cocoa) can't create a view\n");
910 exit(1);
911 }
912
913 // create a window
914 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700915 styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable
thsc304f7e2008-01-22 23:25:15 +0000916 backing:NSBackingStoreBuffered defer:NO];
917 if(!normalWindow) {
918 fprintf(stderr, "(cocoa) can't create window\n");
919 exit(1);
920 }
921 [normalWindow setAcceptsMouseMovedEvents:YES];
John Arbucklea1dbc052015-10-13 21:51:18 +0100922 [normalWindow setTitle:@"QEMU"];
thsc304f7e2008-01-22 23:25:15 +0000923 [normalWindow setContentView:cocoaView];
Peter Maydell81801ae2015-05-19 09:11:18 +0100924#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
Andreas Färber561ef252009-12-13 03:06:20 +0100925 [normalWindow useOptimizedDrawing:YES];
Peter Maydell81801ae2015-05-19 09:11:18 +0100926#endif
thsc304f7e2008-01-22 23:25:15 +0000927 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +0000928 [normalWindow center];
John Arbuckled9bc14f2015-09-25 23:13:59 +0100929 [normalWindow setDelegate: self];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100930 stretch_video = false;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100931
932 /* Used for displaying pause on the screen */
933 pauseLabel = [NSTextField new];
934 [pauseLabel setBezeled:YES];
935 [pauseLabel setDrawsBackground:YES];
936 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
937 [pauseLabel setEditable:NO];
938 [pauseLabel setSelectable:NO];
939 [pauseLabel setStringValue: @"Paused"];
940 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
941 [pauseLabel setTextColor: [NSColor blackColor]];
942 [pauseLabel sizeToFit];
John Arbuckle693a3e02015-06-19 10:53:27 +0100943
944 // set the supported image file types that can be opened
945 supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg",
John Arbuckle9d227f12016-03-30 12:37:11 -0400946 @"qcow", @"qcow2", @"cloop", @"vmdk", @"cdr",
Programmingkid5f26fcf2016-12-30 15:42:21 -0500947 @"toast", nil];
Programmingkid9e8204b2016-08-15 22:11:06 -0400948 [self make_about_window];
thsc304f7e2008-01-22 23:25:15 +0000949 }
950 return self;
951}
952
953- (void) dealloc
954{
955 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
956
957 if (cocoaView)
958 [cocoaView release];
959 [super dealloc];
960}
961
bellard5b0753e2005-03-01 21:37:28 +0000962- (void)applicationDidFinishLaunching: (NSNotification *) note
963{
thsc304f7e2008-01-22 23:25:15 +0000964 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
John Arbuckle365d7f32015-09-25 23:14:00 +0100965 // launch QEMU, with the global args
966 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5b0753e2005-03-01 21:37:28 +0000967}
968
969- (void)applicationWillTerminate:(NSNotification *)aNotification
970{
thsc304f7e2008-01-22 23:25:15 +0000971 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
972
Eric Blakecf83f142017-05-15 16:41:13 -0500973 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
bellard5b0753e2005-03-01 21:37:28 +0000974 exit(0);
975}
976
Andreas Färber41ea49b2009-12-14 22:13:27 +0100977- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
978{
979 return YES;
980}
981
John Arbuckled9bc14f2015-09-25 23:13:59 +0100982- (NSApplicationTerminateReply)applicationShouldTerminate:
983 (NSApplication *)sender
984{
985 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
986 return [self verifyQuit];
987}
988
989/* Called when the user clicks on a window's close button */
990- (BOOL)windowShouldClose:(id)sender
991{
992 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
993 [NSApp terminate: sender];
994 /* If the user allows the application to quit then the call to
995 * NSApp terminate will never return. If we get here then the user
996 * cancelled the quit, so we should return NO to not permit the
997 * closing of this window.
998 */
999 return NO;
1000}
1001
John Arbuckle3b178b72015-09-25 23:14:00 +01001002/* Called when QEMU goes into the background */
1003- (void) applicationWillResignActive: (NSNotification *)aNotification
1004{
1005 COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
1006 [cocoaView raiseAllKeys];
1007}
1008
thsc304f7e2008-01-22 23:25:15 +00001009- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
1010{
1011 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
1012
1013 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +02001014 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +00001015 exit(status);
1016}
1017
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001018/* We abstract the method called by the Enter Fullscreen menu item
1019 * because Mac OS 10.7 and higher disables it. This is because of the
1020 * menu item's old selector's name toggleFullScreen:
1021 */
1022- (void) doToggleFullScreen:(id)sender
1023{
1024 [self toggleFullScreen:(id)sender];
1025}
1026
thsc304f7e2008-01-22 23:25:15 +00001027- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +00001028{
thsc304f7e2008-01-22 23:25:15 +00001029 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
1030
1031 [cocoaView toggleFullScreen:sender];
1032}
1033
John Arbucklef4747902016-03-23 14:26:17 +00001034/* Tries to find then open the specified filename */
1035- (void) openDocumentation: (NSString *) filename
1036{
1037 /* Where to look for local files */
1038 NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"};
1039 NSString *full_file_path;
1040
1041 /* iterate thru the possible paths until the file is found */
1042 int index;
1043 for (index = 0; index < ARRAY_SIZE(path_array); index++) {
1044 full_file_path = [[NSBundle mainBundle] executablePath];
1045 full_file_path = [full_file_path stringByDeletingLastPathComponent];
1046 full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
1047 path_array[index], filename];
1048 if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) {
1049 return;
1050 }
1051 }
1052
1053 /* If none of the paths opened a file */
1054 NSBeep();
1055 QEMU_Alert(@"Failed to open file");
1056}
1057
thsc304f7e2008-01-22 23:25:15 +00001058- (void)showQEMUDoc:(id)sender
1059{
1060 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1061
John Arbucklef4747902016-03-23 14:26:17 +00001062 [self openDocumentation: @"qemu-doc.html"];
thsc304f7e2008-01-22 23:25:15 +00001063}
1064
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001065/* Stretches video to fit host monitor size */
1066- (void)zoomToFit:(id) sender
1067{
1068 stretch_video = !stretch_video;
1069 if (stretch_video == true) {
1070 [sender setState: NSOnState];
1071 } else {
1072 [sender setState: NSOffState];
1073 }
1074}
bellard5b0753e2005-03-01 21:37:28 +00001075
Programmingkidb4c6a112015-05-19 09:11:18 +01001076/* Displays the console on the screen */
1077- (void)displayConsole:(id)sender
1078{
1079 console_select([sender tag]);
1080}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001081
1082/* Pause the guest */
1083- (void)pauseQEMU:(id)sender
1084{
1085 qmp_stop(NULL);
1086 [sender setEnabled: NO];
1087 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1088 [self displayPause];
1089}
1090
1091/* Resume running the guest operating system */
1092- (void)resumeQEMU:(id) sender
1093{
1094 qmp_cont(NULL);
1095 [sender setEnabled: NO];
1096 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1097 [self removePause];
1098}
1099
1100/* Displays the word pause on the screen */
1101- (void)displayPause
1102{
1103 /* Coordinates have to be calculated each time because the window can change its size */
1104 int xCoord, yCoord, width, height;
1105 xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
1106 yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
1107 width = [pauseLabel frame].size.width;
1108 height = [pauseLabel frame].size.height;
1109 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1110 [cocoaView addSubview: pauseLabel];
1111}
1112
1113/* Removes the word pause from the screen */
1114- (void)removePause
1115{
1116 [pauseLabel removeFromSuperview];
1117}
1118
John Arbuckle27074612015-06-19 10:53:27 +01001119/* Restarts QEMU */
1120- (void)restartQEMU:(id)sender
1121{
1122 qmp_system_reset(NULL);
1123}
1124
1125/* Powers down QEMU */
1126- (void)powerDownQEMU:(id)sender
1127{
1128 qmp_system_powerdown(NULL);
1129}
1130
John Arbuckle693a3e02015-06-19 10:53:27 +01001131/* Ejects the media.
1132 * Uses sender's tag to figure out the device to eject.
1133 */
1134- (void)ejectDeviceMedia:(id)sender
1135{
1136 NSString * drive;
1137 drive = [sender representedObject];
1138 if(drive == nil) {
1139 NSBeep();
1140 QEMU_Alert(@"Failed to find drive to eject!");
1141 return;
1142 }
1143
1144 Error *err = NULL;
Kevin Wolffbe2d812016-09-20 13:38:46 +02001145 qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding],
1146 false, NULL, false, false, &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001147 handleAnyDeviceErrors(err);
1148}
1149
1150/* Displays a dialog box asking the user to select an image file to load.
1151 * Uses sender's represented object value to figure out which drive to use.
1152 */
1153- (void)changeDeviceMedia:(id)sender
1154{
1155 /* Find the drive name */
1156 NSString * drive;
1157 drive = [sender representedObject];
1158 if(drive == nil) {
1159 NSBeep();
1160 QEMU_Alert(@"Could not find drive!");
1161 return;
1162 }
1163
1164 /* Display the file open dialog */
1165 NSOpenPanel * openPanel;
1166 openPanel = [NSOpenPanel openPanel];
1167 [openPanel setCanChooseFiles: YES];
1168 [openPanel setAllowsMultipleSelection: NO];
1169 [openPanel setAllowedFileTypes: supportedImageFileTypes];
1170 if([openPanel runModal] == NSFileHandlingPanelOKButton) {
1171 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1172 if(file == nil) {
1173 NSBeep();
1174 QEMU_Alert(@"Failed to convert URL to file path!");
1175 return;
1176 }
1177
1178 Error *err = NULL;
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001179 qmp_blockdev_change_medium(true,
1180 [drive cStringUsingEncoding:
Max Reitz24fb4132015-11-06 16:27:06 +01001181 NSASCIIStringEncoding],
Kevin Wolf70e2cb32016-09-20 13:38:47 +02001182 false, NULL,
Max Reitz24fb4132015-11-06 16:27:06 +01001183 [file cStringUsingEncoding:
1184 NSASCIIStringEncoding],
1185 true, "raw",
Max Reitz39ff43d2015-11-11 04:49:44 +01001186 false, 0,
Max Reitz24fb4132015-11-06 16:27:06 +01001187 &err);
John Arbuckle693a3e02015-06-19 10:53:27 +01001188 handleAnyDeviceErrors(err);
1189 }
1190}
1191
John Arbuckled9bc14f2015-09-25 23:13:59 +01001192/* Verifies if the user really wants to quit */
1193- (BOOL)verifyQuit
1194{
1195 NSAlert *alert = [NSAlert new];
1196 [alert autorelease];
1197 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1198 [alert addButtonWithTitle: @"Cancel"];
1199 [alert addButtonWithTitle: @"Quit"];
1200 if([alert runModal] == NSAlertSecondButtonReturn) {
1201 return YES;
1202 } else {
1203 return NO;
1204 }
1205}
1206
Programmingkid9e8204b2016-08-15 22:11:06 -04001207/* The action method for the About menu item */
1208- (IBAction) do_about_menu_item: (id) sender
1209{
1210 [about_window makeKeyAndOrderFront: nil];
1211}
1212
1213/* Create and display the about dialog */
1214- (void)make_about_window
1215{
1216 /* Make the window */
1217 int x = 0, y = 0, about_width = 400, about_height = 200;
1218 NSRect window_rect = NSMakeRect(x, y, about_width, about_height);
1219 about_window = [[NSWindow alloc] initWithContentRect:window_rect
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001220 styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
1221 NSWindowStyleMaskMiniaturizable
Programmingkid9e8204b2016-08-15 22:11:06 -04001222 backing:NSBackingStoreBuffered
1223 defer:NO];
1224 [about_window setTitle: @"About"];
1225 [about_window setReleasedWhenClosed: NO];
1226 [about_window center];
1227 NSView *superView = [about_window contentView];
1228
1229 /* Create the dimensions of the picture */
1230 int picture_width = 80, picture_height = 80;
1231 x = (about_width - picture_width)/2;
1232 y = about_height - picture_height - 10;
1233 NSRect picture_rect = NSMakeRect(x, y, picture_width, picture_height);
1234
1235 /* Get the path to the QEMU binary */
1236 NSString *binary_name = [NSString stringWithCString: gArgv[0]
1237 encoding: NSASCIIStringEncoding];
1238 binary_name = [binary_name lastPathComponent];
1239 NSString *program_path = [[NSString alloc] initWithFormat: @"%@/%@",
1240 [[NSBundle mainBundle] bundlePath], binary_name];
1241
1242 /* Make the picture of QEMU */
1243 NSImageView *picture_view = [[NSImageView alloc] initWithFrame:
1244 picture_rect];
1245 NSImage *qemu_image = [[NSWorkspace sharedWorkspace] iconForFile:
1246 program_path];
1247 [picture_view setImage: qemu_image];
1248 [picture_view setImageScaling: NSImageScaleProportionallyUpOrDown];
1249 [superView addSubview: picture_view];
1250
1251 /* Make the name label */
1252 x = 0;
1253 y = y - 25;
1254 int name_width = about_width, name_height = 20;
1255 NSRect name_rect = NSMakeRect(x, y, name_width, name_height);
1256 NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect];
1257 [name_label setEditable: NO];
1258 [name_label setBezeled: NO];
1259 [name_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001260 [name_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001261 NSString *qemu_name = [[NSString alloc] initWithCString: gArgv[0]
1262 encoding: NSASCIIStringEncoding];
1263 qemu_name = [qemu_name lastPathComponent];
1264 [name_label setStringValue: qemu_name];
1265 [superView addSubview: name_label];
1266
1267 /* Set the version label's attributes */
1268 x = 0;
1269 y = 50;
1270 int version_width = about_width, version_height = 20;
1271 NSRect version_rect = NSMakeRect(x, y, version_width, version_height);
1272 NSTextField *version_label = [[NSTextField alloc] initWithFrame:
1273 version_rect];
1274 [version_label setEditable: NO];
1275 [version_label setBezeled: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001276 [version_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001277 [version_label setDrawsBackground: NO];
1278
1279 /* Create the version string*/
1280 NSString *version_string;
1281 version_string = [[NSString alloc] initWithFormat:
1282 @"QEMU emulator version %s%s", QEMU_VERSION, QEMU_PKGVERSION];
1283 [version_label setStringValue: version_string];
1284 [superView addSubview: version_label];
1285
1286 /* Make copyright label */
1287 x = 0;
1288 y = 35;
1289 int copyright_width = about_width, copyright_height = 20;
1290 NSRect copyright_rect = NSMakeRect(x, y, copyright_width, copyright_height);
1291 NSTextField *copyright_label = [[NSTextField alloc] initWithFrame:
1292 copyright_rect];
1293 [copyright_label setEditable: NO];
1294 [copyright_label setBezeled: NO];
1295 [copyright_label setDrawsBackground: NO];
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001296 [copyright_label setAlignment: NSTextAlignmentCenter];
Programmingkid9e8204b2016-08-15 22:11:06 -04001297 [copyright_label setStringValue: [NSString stringWithFormat: @"%s",
1298 QEMU_COPYRIGHT]];
1299 [superView addSubview: copyright_label];
1300}
1301
Programmingkidb4c6a112015-05-19 09:11:18 +01001302@end
bellard5b0753e2005-03-01 21:37:28 +00001303
thsc304f7e2008-01-22 23:25:15 +00001304
thsc304f7e2008-01-22 23:25:15 +00001305int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +00001306
thsc304f7e2008-01-22 23:25:15 +00001307 gArgc = argc;
1308 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +01001309 int i;
1310
1311 /* In case we don't need to display a window, let's not do that */
1312 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001313 const char *opt = argv[i];
1314
1315 if (opt[0] == '-') {
1316 /* Treat --foo the same as -foo. */
1317 if (opt[1] == '-') {
1318 opt++;
1319 }
Alexandre Raymond98514842011-05-29 18:22:49 -04001320 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
1321 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001322 !strcmp(opt, "-nographic") ||
1323 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001324 !strcmp(opt, "-curses") ||
Rainer Müllerb12a84c2015-09-09 16:08:30 +02001325 !strcmp(opt, "-display") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001326 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +02001327 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001328 }
Andreas Färberf4918802009-12-13 02:11:44 +01001329 }
1330 }
ths3b46e622007-09-17 08:09:54 +00001331
thsc304f7e2008-01-22 23:25:15 +00001332 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +00001333
Peter Maydell42a5dfe2013-04-22 10:29:47 +00001334 // Pull this console process up to being a fully-fledged graphical
1335 // app with a menubar and Dock icon
1336 ProcessSerialNumber psn = { 0, kCurrentProcess };
1337 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1338
1339 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +00001340
thsc304f7e2008-01-22 23:25:15 +00001341 // Add menus
1342 NSMenu *menu;
1343 NSMenuItem *menuItem;
1344
bellard5b0753e2005-03-01 21:37:28 +00001345 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +00001346
thsc304f7e2008-01-22 23:25:15 +00001347 // Application menu
1348 menu = [[NSMenu alloc] initWithTitle:@""];
Programmingkid9e8204b2016-08-15 22:11:06 -04001349 [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU
thsc304f7e2008-01-22 23:25:15 +00001350 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1351 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1352 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001353 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
thsc304f7e2008-01-22 23:25:15 +00001354 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1355 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1356 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1357 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1358 [menuItem setSubmenu:menu];
1359 [[NSApp mainMenu] addItem:menuItem];
1360 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001361
John Arbuckle8524f1c2015-06-19 10:53:27 +01001362 // Machine menu
1363 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1364 [menu setAutoenablesItems: NO];
1365 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1366 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1367 [menu addItem: menuItem];
1368 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001369 [menu addItem: [NSMenuItem separatorItem]];
1370 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1371 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001372 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1373 [menuItem setSubmenu:menu];
1374 [[NSApp mainMenu] addItem:menuItem];
1375
thsc304f7e2008-01-22 23:25:15 +00001376 // View menu
1377 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001378 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1379 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001380 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1381 [menuItem setSubmenu:menu];
1382 [[NSApp mainMenu] addItem:menuItem];
1383
1384 // Window menu
1385 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1386 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1387 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1388 [menuItem setSubmenu:menu];
1389 [[NSApp mainMenu] addItem:menuItem];
1390 [NSApp setWindowsMenu:menu];
1391
1392 // Help menu
1393 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1394 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
thsc304f7e2008-01-22 23:25:15 +00001395 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1396 [menuItem setSubmenu:menu];
1397 [[NSApp mainMenu] addItem:menuItem];
1398
1399 // Create an Application controller
1400 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1401 [NSApp setDelegate:appController];
1402
1403 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +00001404 [NSApp run];
ths3b46e622007-09-17 08:09:54 +00001405
thsc304f7e2008-01-22 23:25:15 +00001406 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001407 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001408
bellard5b0753e2005-03-01 21:37:28 +00001409 return 0;
1410}
thsc304f7e2008-01-22 23:25:15 +00001411
1412
1413
1414#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001415static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001416 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001417{
Peter Maydell6e657e62013-04-22 10:29:46 +00001418 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1419
thsc304f7e2008-01-22 23:25:15 +00001420 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1421
1422 NSRect rect;
1423 if ([cocoaView cdx] == 1.0) {
1424 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1425 } else {
1426 rect = NSMakeRect(
1427 x * [cocoaView cdx],
1428 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1429 w * [cocoaView cdx],
1430 h * [cocoaView cdy]);
1431 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001432 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001433
1434 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001435}
1436
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001437static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001438 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001439{
Peter Maydell6e657e62013-04-22 10:29:46 +00001440 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001441
Peter Maydell6e657e62013-04-22 10:29:46 +00001442 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001443 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001444 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001445}
1446
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001447static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001448{
Peter Maydell6e657e62013-04-22 10:29:46 +00001449 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1450
thsc304f7e2008-01-22 23:25:15 +00001451 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
John Arbuckle468a8952015-10-13 21:51:18 +01001452 graphic_hw_update(NULL);
thsc304f7e2008-01-22 23:25:15 +00001453
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001454 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001455 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001456 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001457 [cocoaView ungrabMouse];
1458 }
1459 }
1460 [cocoaView setAbsoluteEnabled:YES];
1461 }
1462
1463 NSDate *distantPast;
1464 NSEvent *event;
1465 distantPast = [NSDate distantPast];
1466 do {
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001467 event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:distantPast
thsc304f7e2008-01-22 23:25:15 +00001468 inMode: NSDefaultRunLoopMode dequeue:YES];
1469 if (event != nil) {
1470 [cocoaView handleEvent:event];
1471 }
1472 } while(event != nil);
Peter Maydell6e657e62013-04-22 10:29:46 +00001473 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001474}
1475
1476static void cocoa_cleanup(void)
1477{
1478 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001479 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001480}
1481
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001482static const DisplayChangeListenerOps dcl_ops = {
1483 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001484 .dpy_gfx_update = cocoa_update,
1485 .dpy_gfx_switch = cocoa_switch,
1486 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001487};
1488
Programmingkidb4c6a112015-05-19 09:11:18 +01001489/* Returns a name for a given console */
1490static NSString * getConsoleName(QemuConsole * console)
1491{
1492 return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
1493}
1494
1495/* Add an entry to the View menu for each console */
1496static void add_console_menu_entries(void)
1497{
1498 NSMenu *menu;
1499 NSMenuItem *menuItem;
1500 int index = 0;
1501
1502 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1503
1504 [menu addItem:[NSMenuItem separatorItem]];
1505
1506 while (qemu_console_lookup_by_index(index) != NULL) {
1507 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1508 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1509 [menuItem setTag: index];
1510 [menu addItem: menuItem];
1511 index++;
1512 }
1513}
1514
John Arbuckle693a3e02015-06-19 10:53:27 +01001515/* Make menu items for all removable devices.
1516 * Each device is given an 'Eject' and 'Change' menu item.
1517 */
John Arbucklea7940ec2015-10-13 21:51:18 +01001518static void addRemovableDevicesMenuItems(void)
John Arbuckle693a3e02015-06-19 10:53:27 +01001519{
1520 NSMenu *menu;
1521 NSMenuItem *menuItem;
1522 BlockInfoList *currentDevice, *pointerToFree;
1523 NSString *deviceName;
1524
1525 currentDevice = qmp_query_block(NULL);
1526 pointerToFree = currentDevice;
1527 if(currentDevice == NULL) {
1528 NSBeep();
1529 QEMU_Alert(@"Failed to query for block devices!");
1530 return;
1531 }
1532
1533 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1534
1535 // Add a separator between related groups of menu items
1536 [menu addItem:[NSMenuItem separatorItem]];
1537
1538 // Set the attributes to the "Removable Media" menu item
1539 NSString *titleString = @"Removable Media";
1540 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1541 NSColor *newColor = [NSColor blackColor];
1542 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1543 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1544 traits:NSBoldFontMask|NSItalicFontMask
1545 weight:0
1546 size:14];
1547 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1548 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1549 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1550
1551 // Add the "Removable Media" menu item
1552 menuItem = [NSMenuItem new];
1553 [menuItem setAttributedTitle: attString];
1554 [menuItem setEnabled: NO];
1555 [menu addItem: menuItem];
1556
Stefan Weilcb8d4c82016-03-23 15:59:57 +01001557 /* Loop through all the block devices in the emulator */
John Arbuckle693a3e02015-06-19 10:53:27 +01001558 while (currentDevice) {
1559 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1560
1561 if(currentDevice->value->removable) {
1562 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1563 action: @selector(changeDeviceMedia:)
1564 keyEquivalent: @""];
1565 [menu addItem: menuItem];
1566 [menuItem setRepresentedObject: deviceName];
1567 [menuItem autorelease];
1568
1569 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1570 action: @selector(ejectDeviceMedia:)
1571 keyEquivalent: @""];
1572 [menu addItem: menuItem];
1573 [menuItem setRepresentedObject: deviceName];
1574 [menuItem autorelease];
1575 }
1576 currentDevice = currentDevice->next;
1577 }
1578 qapi_free_BlockInfoList(pointerToFree);
1579}
1580
thsc304f7e2008-01-22 23:25:15 +00001581void cocoa_display_init(DisplayState *ds, int full_screen)
1582{
1583 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1584
Programmingkid43227af2015-05-19 09:11:17 +01001585 /* if fullscreen mode is to be used */
1586 if (full_screen == true) {
1587 [NSApp activateIgnoringOtherApps: YES];
1588 [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
1589 }
1590
Blue Swirl58a06672011-08-21 18:42:08 +00001591 dcl = g_malloc0(sizeof(DisplayChangeListener));
1592
aliguori9794f742009-03-04 19:25:22 +00001593 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001594 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001595 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001596
1597 // register cleanup function
1598 atexit(cocoa_cleanup);
Programmingkidb4c6a112015-05-19 09:11:18 +01001599
1600 /* At this point QEMU has created all the consoles, so we can add View
1601 * menu entries for them.
1602 */
1603 add_console_menu_entries();
John Arbuckle693a3e02015-06-19 10:53:27 +01001604
1605 /* Give all removable devices a menu item.
1606 * Has to be called after QEMU has started to
1607 * find out what removable devices it has.
1608 */
1609 addRemovableDevicesMenuItems();
thsc304f7e2008-01-22 23:25:15 +00001610}