blob: 84c84e98fc5ebfb6cf797da88f49cf9b65a9fdac [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
Marc-André Lureau49f95222022-04-20 17:25:49 +040030#include "qemu/help-texts.h"
Marc-André Lureau88c39c82022-04-20 17:25:47 +040031#include "qemu-main.h"
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +090032#include "ui/clipboard.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010033#include "ui/console.h"
Gerd Hoffmann21bae112013-12-04 14:08:04 +010034#include "ui/input.h"
Akihiko Odaki6d73bb62021-03-10 23:46:02 +090035#include "ui/kbd-state.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/sysemu.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020037#include "sysemu/runstate.h"
Akihiko Odaki2910abd2022-05-29 17:25:08 +090038#include "sysemu/runstate-action.h"
Claudio Fontanab0c3cf92020-06-29 11:35:03 +020039#include "sysemu/cpu-throttle.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010040#include "qapi/error.h"
Markus Armbruster16bf5232018-12-20 09:45:59 +010041#include "qapi/qapi-commands-block.h"
Philippe Mathieu-Daudé90f8c0f2020-10-12 14:15:33 +020042#include "qapi/qapi-commands-machine.h"
Markus Armbruster16bf5232018-12-20 09:45:59 +010043#include "qapi/qapi-commands-misc.h"
John Arbuckle693a3e02015-06-19 10:53:27 +010044#include "sysemu/blockdev.h"
Programmingkid9e8204b2016-08-15 22:11:06 -040045#include "qemu-version.h"
Akihiko Odakie31746e2021-03-09 21:22:25 +090046#include "qemu/cutils.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020047#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020048#include "qemu/module.h"
John Arbuckleaaac7142016-03-23 14:26:18 +000049#include <Carbon/Carbon.h>
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020050#include "hw/core/cpu.h"
bellardda4dbf72005-03-02 22:22:43 +000051
Brendan Shanks5e246002019-01-31 23:12:25 -080052#ifndef MAC_OS_X_VERSION_10_13
53#define MAC_OS_X_VERSION_10_13 101300
54#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010055
Brendan Shanks5e246002019-01-31 23:12:25 -080056/* 10.14 deprecates NSOnState and NSOffState in favor of
57 * NSControlStateValueOn/Off, which were introduced in 10.13.
58 * Define for older versions
59 */
60#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13
61#define NSControlStateValueOn NSOnState
62#define NSControlStateValueOff NSOffState
63#endif
bellard5b0753e2005-03-01 21:37:28 +000064
thsc304f7e2008-01-22 23:25:15 +000065//#define DEBUG
66
67#ifdef DEBUG
68#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
69#else
70#define COCOA_DEBUG(...) ((void) 0)
71#endif
72
73#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000074
75typedef struct {
76 int width;
77 int height;
thsc304f7e2008-01-22 23:25:15 +000078} QEMUScreen;
79
Akihiko Odakicc7859c2021-02-19 17:44:19 +090080static void cocoa_update(DisplayChangeListener *dcl,
81 int x, int y, int w, int h);
82
83static void cocoa_switch(DisplayChangeListener *dcl,
84 DisplaySurface *surface);
85
86static void cocoa_refresh(DisplayChangeListener *dcl);
87
Akihiko Odaki99eb3132022-02-27 13:22:41 +090088static NSWindow *normalWindow;
Akihiko Odakicc7859c2021-02-19 17:44:19 +090089static const DisplayChangeListenerOps dcl_ops = {
90 .dpy_name = "cocoa",
91 .dpy_gfx_update = cocoa_update,
92 .dpy_gfx_switch = cocoa_switch,
93 .dpy_refresh = cocoa_refresh,
94};
95static DisplayChangeListener dcl = {
96 .ops = &dcl_ops,
97};
Gerd Hoffmann21bae112013-12-04 14:08:04 +010098static int last_buttons;
Gerd Hoffmann3487da62020-02-06 12:27:50 +010099static int cursor_hide = 1;
Carwyn Ellis48941a52022-01-02 17:41:52 +0000100static int left_command_key_enabled = 1;
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900101static bool swap_opt_cmd;
bellard5b0753e2005-03-01 21:37:28 +0000102
Akihiko Odakicb823402021-02-25 17:42:02 +0900103static int gArgc;
104static char **gArgv;
105static bool stretch_video;
106static NSTextField *pauseLabel;
bellard5b0753e2005-03-01 21:37:28 +0000107
Peter Maydell55888402019-02-25 10:24:33 +0000108static QemuSemaphore display_init_sem;
109static QemuSemaphore app_started_sem;
Hikaru Nishidadff742a2019-10-15 10:07:34 +0900110static bool allow_events;
Peter Maydell55888402019-02-25 10:24:33 +0000111
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +0900112static NSInteger cbchangecount = -1;
113static QemuClipboardInfo *cbinfo;
114static QemuEvent cbevent;
115
Peter Maydell60105d72019-02-25 10:24:31 +0000116// Utility functions to run specified code block with iothread lock held
Peter Maydell31819e92019-02-25 10:24:27 +0000117typedef void (^CodeBlock)(void);
Peter Maydell60105d72019-02-25 10:24:31 +0000118typedef bool (^BoolCodeBlock)(void);
Peter Maydell31819e92019-02-25 10:24:27 +0000119
120static void with_iothread_lock(CodeBlock block)
121{
122 bool locked = qemu_mutex_iothread_locked();
123 if (!locked) {
124 qemu_mutex_lock_iothread();
125 }
126 block();
127 if (!locked) {
128 qemu_mutex_unlock_iothread();
129 }
130}
131
Peter Maydell60105d72019-02-25 10:24:31 +0000132static bool bool_with_iothread_lock(BoolCodeBlock block)
133{
134 bool locked = qemu_mutex_iothread_locked();
135 bool val;
136
137 if (!locked) {
138 qemu_mutex_lock_iothread();
139 }
140 val = block();
141 if (!locked) {
142 qemu_mutex_unlock_iothread();
143 }
144 return val;
145}
146
John Arbuckleaaac7142016-03-23 14:26:18 +0000147// Mac to QKeyCode conversion
Akihiko Odakicb823402021-02-25 17:42:02 +0900148static const int mac_to_qkeycode_map[] = {
John Arbuckleaaac7142016-03-23 14:26:18 +0000149 [kVK_ANSI_A] = Q_KEY_CODE_A,
150 [kVK_ANSI_B] = Q_KEY_CODE_B,
151 [kVK_ANSI_C] = Q_KEY_CODE_C,
152 [kVK_ANSI_D] = Q_KEY_CODE_D,
153 [kVK_ANSI_E] = Q_KEY_CODE_E,
154 [kVK_ANSI_F] = Q_KEY_CODE_F,
155 [kVK_ANSI_G] = Q_KEY_CODE_G,
156 [kVK_ANSI_H] = Q_KEY_CODE_H,
157 [kVK_ANSI_I] = Q_KEY_CODE_I,
158 [kVK_ANSI_J] = Q_KEY_CODE_J,
159 [kVK_ANSI_K] = Q_KEY_CODE_K,
160 [kVK_ANSI_L] = Q_KEY_CODE_L,
161 [kVK_ANSI_M] = Q_KEY_CODE_M,
162 [kVK_ANSI_N] = Q_KEY_CODE_N,
163 [kVK_ANSI_O] = Q_KEY_CODE_O,
164 [kVK_ANSI_P] = Q_KEY_CODE_P,
165 [kVK_ANSI_Q] = Q_KEY_CODE_Q,
166 [kVK_ANSI_R] = Q_KEY_CODE_R,
167 [kVK_ANSI_S] = Q_KEY_CODE_S,
168 [kVK_ANSI_T] = Q_KEY_CODE_T,
169 [kVK_ANSI_U] = Q_KEY_CODE_U,
170 [kVK_ANSI_V] = Q_KEY_CODE_V,
171 [kVK_ANSI_W] = Q_KEY_CODE_W,
172 [kVK_ANSI_X] = Q_KEY_CODE_X,
173 [kVK_ANSI_Y] = Q_KEY_CODE_Y,
174 [kVK_ANSI_Z] = Q_KEY_CODE_Z,
ths3b46e622007-09-17 08:09:54 +0000175
John Arbuckleaaac7142016-03-23 14:26:18 +0000176 [kVK_ANSI_0] = Q_KEY_CODE_0,
177 [kVK_ANSI_1] = Q_KEY_CODE_1,
178 [kVK_ANSI_2] = Q_KEY_CODE_2,
179 [kVK_ANSI_3] = Q_KEY_CODE_3,
180 [kVK_ANSI_4] = Q_KEY_CODE_4,
181 [kVK_ANSI_5] = Q_KEY_CODE_5,
182 [kVK_ANSI_6] = Q_KEY_CODE_6,
183 [kVK_ANSI_7] = Q_KEY_CODE_7,
184 [kVK_ANSI_8] = Q_KEY_CODE_8,
185 [kVK_ANSI_9] = Q_KEY_CODE_9,
186
187 [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
188 [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
189 [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
190 [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
191 [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
192 [kVK_Tab] = Q_KEY_CODE_TAB,
193 [kVK_Return] = Q_KEY_CODE_RET,
194 [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
195 [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
196 [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
197 [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
198 [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
199 [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
200 [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
201 [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
John Arbuckleaaac7142016-03-23 14:26:18 +0000202 [kVK_Space] = Q_KEY_CODE_SPC,
203
204 [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
205 [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
206 [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
207 [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
208 [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
209 [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
210 [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
211 [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
212 [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
213 [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
214 [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
215 [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
216 [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
217 [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
218 [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
219 [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
220 [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
221 [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
222
223 [kVK_UpArrow] = Q_KEY_CODE_UP,
224 [kVK_DownArrow] = Q_KEY_CODE_DOWN,
225 [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
226 [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
227
228 [kVK_Help] = Q_KEY_CODE_INSERT,
229 [kVK_Home] = Q_KEY_CODE_HOME,
230 [kVK_PageUp] = Q_KEY_CODE_PGUP,
231 [kVK_PageDown] = Q_KEY_CODE_PGDN,
232 [kVK_End] = Q_KEY_CODE_END,
233 [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
234
235 [kVK_Escape] = Q_KEY_CODE_ESC,
236
237 /* The Power key can't be used directly because the operating system uses
238 * it. This key can be emulated by using it in place of another key such as
239 * F1. Don't forget to disable the real key binding.
240 */
241 /* [kVK_F1] = Q_KEY_CODE_POWER, */
242
243 [kVK_F1] = Q_KEY_CODE_F1,
244 [kVK_F2] = Q_KEY_CODE_F2,
245 [kVK_F3] = Q_KEY_CODE_F3,
246 [kVK_F4] = Q_KEY_CODE_F4,
247 [kVK_F5] = Q_KEY_CODE_F5,
248 [kVK_F6] = Q_KEY_CODE_F6,
249 [kVK_F7] = Q_KEY_CODE_F7,
250 [kVK_F8] = Q_KEY_CODE_F8,
251 [kVK_F9] = Q_KEY_CODE_F9,
252 [kVK_F10] = Q_KEY_CODE_F10,
253 [kVK_F11] = Q_KEY_CODE_F11,
254 [kVK_F12] = Q_KEY_CODE_F12,
255 [kVK_F13] = Q_KEY_CODE_PRINT,
256 [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
257 [kVK_F15] = Q_KEY_CODE_PAUSE,
258
Akihiko Odaki708b7252021-02-12 09:04:04 +0900259 // JIS keyboards only
260 [kVK_JIS_Yen] = Q_KEY_CODE_YEN,
261 [kVK_JIS_Underscore] = Q_KEY_CODE_RO,
262 [kVK_JIS_KeypadComma] = Q_KEY_CODE_KP_COMMA,
263 [kVK_JIS_Eisu] = Q_KEY_CODE_MUHENKAN,
264 [kVK_JIS_Kana] = Q_KEY_CODE_HENKAN,
265
John Arbuckleaaac7142016-03-23 14:26:18 +0000266 /*
267 * The eject and volume keys can't be used here because they are handled at
268 * a lower level than what an Application can see.
269 */
bellard5b0753e2005-03-01 21:37:28 +0000270};
271
Andreas Färber77047bb2009-12-13 00:55:53 +0100272static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000273{
John Arbuckleaaac7142016-03-23 14:26:18 +0000274 if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
Akihiko Odaki43137392021-02-23 22:11:06 +0900275 error_report("(cocoa) warning unknown keycode 0x%x", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000276 return 0;
277 }
John Arbuckleaaac7142016-03-23 14:26:18 +0000278 return mac_to_qkeycode_map[keycode];
bellard5b0753e2005-03-01 21:37:28 +0000279}
280
John Arbuckle693a3e02015-06-19 10:53:27 +0100281/* Displays an alert dialog box with the specified message */
282static void QEMU_Alert(NSString *message)
283{
284 NSAlert *alert;
285 alert = [NSAlert new];
286 [alert setMessageText: message];
287 [alert runModal];
288}
thsc304f7e2008-01-22 23:25:15 +0000289
John Arbuckle693a3e02015-06-19 10:53:27 +0100290/* Handles any errors that happen with a device transaction */
291static void handleAnyDeviceErrors(Error * err)
292{
293 if (err) {
294 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
295 encoding: NSASCIIStringEncoding]);
296 error_free(err);
297 }
298}
thsc304f7e2008-01-22 23:25:15 +0000299
bellard5b0753e2005-03-01 21:37:28 +0000300/*
301 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000302 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000303 ------------------------------------------------------
304*/
thsc304f7e2008-01-22 23:25:15 +0000305@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000306{
thsc304f7e2008-01-22 23:25:15 +0000307 QEMUScreen screen;
308 NSWindow *fullScreenWindow;
309 float cx,cy,cw,ch,cdx,cdy;
Peter Maydell55888402019-02-25 10:24:33 +0000310 pixman_image_t *pixman_image;
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900311 QKbdState *kbd;
Peter Maydell49b9bd42013-12-08 22:59:03 +0000312 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000313 BOOL isFullscreen;
314 BOOL isAbsoluteEnabled;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900315 CFMachPortRef eventsTap;
thsc304f7e2008-01-22 23:25:15 +0000316}
Peter Maydell72a3e312019-02-25 10:24:28 +0000317- (void) switchSurface:(pixman_image_t *)image;
thsc304f7e2008-01-22 23:25:15 +0000318- (void) grabMouse;
319- (void) ungrabMouse;
320- (void) toggleFullScreen:(id)sender;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900321- (void) setFullGrab:(id)sender;
John Arbuckle9c3a4182017-11-07 10:14:14 +0000322- (void) handleMonitorInput:(NSEvent *)event;
Peter Maydell60105d72019-02-25 10:24:31 +0000323- (bool) handleEvent:(NSEvent *)event;
324- (bool) handleEventLocked:(NSEvent *)event;
thsc304f7e2008-01-22 23:25:15 +0000325- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100326/* The state surrounding mouse grabbing is potentially confusing.
327 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
328 * pointing device an absolute-position one?"], but is only updated on
329 * next refresh.
330 * isMouseGrabbed tracks whether GUI events are directed to the guest;
331 * it controls whether special keys like Cmd get sent to the guest,
332 * and whether we capture the mouse when in non-absolute mode.
Peter Maydellf61c3872014-06-23 10:35:24 +0100333 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000334- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000335- (BOOL) isAbsoluteEnabled;
336- (float) cdx;
337- (float) cdy;
338- (QEMUScreen) gscreen;
John Arbuckle3b178b72015-09-25 23:14:00 +0100339- (void) raiseAllKeys;
thsc304f7e2008-01-22 23:25:15 +0000340@end
ths3b46e622007-09-17 08:09:54 +0000341
Andreas Färber7fee1992011-06-09 20:53:32 +0200342QemuCocoaView *cocoaView;
343
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900344static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef cgEvent, void *userInfo)
345{
346 QemuCocoaView *cocoaView = userInfo;
347 NSEvent *event = [NSEvent eventWithCGEvent:cgEvent];
348 if ([cocoaView isMouseGrabbed] && [cocoaView handleEvent:event]) {
349 COCOA_DEBUG("Global events tap: qemu handled the event, capturing!\n");
350 return NULL;
351 }
352 COCOA_DEBUG("Global events tap: qemu did not handle the event, letting it through...\n");
353
354 return cgEvent;
355}
356
thsc304f7e2008-01-22 23:25:15 +0000357@implementation QemuCocoaView
358- (id)initWithFrame:(NSRect)frameRect
359{
360 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000361
thsc304f7e2008-01-22 23:25:15 +0000362 self = [super initWithFrame:frameRect];
363 if (self) {
pbrook95219892006-04-09 01:06:34 +0000364
thsc304f7e2008-01-22 23:25:15 +0000365 screen.width = frameRect.size.width;
366 screen.height = frameRect.size.height;
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900367 kbd = qkbd_state_init(dcl.con);
bellard7c206a72005-12-18 19:18:45 +0000368
thsc304f7e2008-01-22 23:25:15 +0000369 }
370 return self;
371}
bellard7c206a72005-12-18 19:18:45 +0000372
thsc304f7e2008-01-22 23:25:15 +0000373- (void) dealloc
374{
375 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000376
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900377 if (pixman_image) {
Peter Maydell55888402019-02-25 10:24:33 +0000378 pixman_image_unref(pixman_image);
379 }
ths3b46e622007-09-17 08:09:54 +0000380
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900381 qkbd_state_free(kbd);
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900382
383 if (eventsTap) {
384 CFRelease(eventsTap);
385 }
386
thsc304f7e2008-01-22 23:25:15 +0000387 [super dealloc];
388}
bellard5cbfcd02006-06-14 15:53:24 +0000389
Andreas Färberd50f71d2009-12-13 02:03:33 +0100390- (BOOL) isOpaque
391{
392 return YES;
393}
394
Peter Maydell5dd45be2014-06-23 10:35:23 +0100395- (BOOL) screenContainsPoint:(NSPoint) p
396{
397 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
398}
399
Chen Zhang2044dff2019-06-04 17:36:00 +0800400/* Get location of event and convert to virtual screen coordinate */
401- (CGPoint) screenLocationOfEvent:(NSEvent *)ev
402{
403 NSWindow *eventWindow = [ev window];
404 // XXX: Use CGRect and -convertRectFromScreen: to support macOS 10.10
405 CGRect r = CGRectZero;
406 r.origin = [ev locationInWindow];
407 if (!eventWindow) {
408 if (!isFullscreen) {
409 return [[self window] convertRectFromScreen:r].origin;
410 } else {
411 CGPoint locationInSelfWindow = [[self window] convertRectFromScreen:r].origin;
412 CGPoint loc = [self convertPoint:locationInSelfWindow fromView:nil];
413 if (stretch_video) {
414 loc.x /= cdx;
415 loc.y /= cdy;
416 }
417 return loc;
418 }
419 } else if ([[self window] isEqual:eventWindow]) {
420 if (!isFullscreen) {
421 return r.origin;
422 } else {
423 CGPoint loc = [self convertPoint:r.origin fromView:nil];
424 if (stretch_video) {
425 loc.x /= cdx;
426 loc.y /= cdy;
427 }
428 return loc;
429 }
430 } else {
431 return [[self window] convertRectFromScreen:[eventWindow convertRectToScreen:r]].origin;
432 }
433}
434
Peter Maydell13aefd32014-06-23 10:35:25 +0100435- (void) hideCursor
436{
437 if (!cursor_hide) {
438 return;
439 }
440 [NSCursor hide];
441}
442
443- (void) unhideCursor
444{
445 if (!cursor_hide) {
446 return;
447 }
448 [NSCursor unhide];
449}
450
thsc304f7e2008-01-22 23:25:15 +0000451- (void) drawRect:(NSRect) rect
452{
453 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000454
thsc304f7e2008-01-22 23:25:15 +0000455 // get CoreGraphic context
Brendan Shanks5e246002019-01-31 23:12:25 -0800456 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] CGContext];
Brendan Shanks5e246002019-01-31 23:12:25 -0800457
thsc304f7e2008-01-22 23:25:15 +0000458 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
459 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000460
thsc304f7e2008-01-22 23:25:15 +0000461 // draw screen bitmap directly to Core Graphics context
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900462 if (!pixman_image) {
Peter Maydell7d270b12013-12-24 02:51:47 +0000463 // Draw request before any guest device has set up a framebuffer:
464 // just draw an opaque black rectangle
465 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
466 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
467 } else {
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900468 int w = pixman_image_get_width(pixman_image);
469 int h = pixman_image_get_height(pixman_image);
470 int bitsPerPixel = PIXMAN_FORMAT_BPP(pixman_image_get_format(pixman_image));
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900471 int stride = pixman_image_get_stride(pixman_image);
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900472 CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(
473 NULL,
474 pixman_image_get_data(pixman_image),
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900475 stride * h,
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900476 NULL
477 );
thsc304f7e2008-01-22 23:25:15 +0000478 CGImageRef imageRef = CGImageCreate(
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900479 w, //width
480 h, //height
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900481 DIV_ROUND_UP(bitsPerPixel, 8) * 2, //bitsPerComponent
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900482 bitsPerPixel, //bitsPerPixel
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900483 stride, //bytesPerRow
Akihiko Odakiae57d352021-03-05 21:13:04 +0900484 CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace
485 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, //bitmapInfo
thsc304f7e2008-01-22 23:25:15 +0000486 dataProviderRef, //provider
487 NULL, //decode
488 0, //interpolate
489 kCGRenderingIntentDefault //intent
490 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100491 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
492 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100493 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100494 int i;
495 CGImageRef clipImageRef;
496 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000497
Peter Maydellb63901d2015-05-19 09:11:17 +0100498 [self getRectsBeingDrawn:&rectList count:&rectCount];
499 for (i = 0; i < rectCount; i++) {
500 clipRect.origin.x = rectList[i].origin.x / cdx;
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900501 clipRect.origin.y = (float)h - (rectList[i].origin.y + rectList[i].size.height) / cdy;
Peter Maydellb63901d2015-05-19 09:11:17 +0100502 clipRect.size.width = rectList[i].size.width / cdx;
503 clipRect.size.height = rectList[i].size.height / cdy;
504 clipImageRef = CGImageCreateWithImageInRect(
505 imageRef,
506 clipRect
507 );
508 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
509 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000510 }
thsc304f7e2008-01-22 23:25:15 +0000511 CGImageRelease (imageRef);
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900512 CGDataProviderRelease(dataProviderRef);
bellard5b0753e2005-03-01 21:37:28 +0000513 }
514}
515
thsc304f7e2008-01-22 23:25:15 +0000516- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000517{
thsc304f7e2008-01-22 23:25:15 +0000518 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000519
thsc304f7e2008-01-22 23:25:15 +0000520 if (isFullscreen) {
521 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
522 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100523
524 /* stretches video, but keeps same aspect ratio */
525 if (stretch_video == true) {
526 /* use smallest stretch value - prevents clipping on sides */
527 if (MIN(cdx, cdy) == cdx) {
528 cdy = cdx;
529 } else {
530 cdx = cdy;
531 }
532 } else { /* No stretching */
533 cdx = cdy = 1;
534 }
thsc304f7e2008-01-22 23:25:15 +0000535 cw = screen.width * cdx;
536 ch = screen.height * cdy;
537 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
538 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
539 } else {
540 cx = 0;
541 cy = 0;
542 cw = screen.width;
543 ch = screen.height;
544 cdx = 1.0;
545 cdy = 1.0;
546 }
bellard5b0753e2005-03-01 21:37:28 +0000547}
548
Peter Maydell8d65dee2022-02-24 10:13:29 +0000549- (void) updateUIInfoLocked
Akihiko Odaki15280e82021-06-16 23:19:10 +0900550{
Peter Maydell8d65dee2022-02-24 10:13:29 +0000551 /* Must be called with the iothread lock, i.e. via updateUIInfo */
Akihiko Odaki15280e82021-06-16 23:19:10 +0900552 NSSize frameSize;
553 QemuUIInfo info;
554
555 if (!qemu_console_is_graphic(dcl.con)) {
556 return;
557 }
558
559 if ([self window]) {
560 NSDictionary *description = [[[self window] screen] deviceDescription];
561 CGDirectDisplayID display = [[description objectForKey:@"NSScreenNumber"] unsignedIntValue];
562 NSSize screenSize = [[[self window] screen] frame].size;
563 CGSize screenPhysicalSize = CGDisplayScreenSize(display);
564
565 frameSize = isFullscreen ? screenSize : [self frame].size;
566 info.width_mm = frameSize.width / screenSize.width * screenPhysicalSize.width;
567 info.height_mm = frameSize.height / screenSize.height * screenPhysicalSize.height;
568 } else {
569 frameSize = [self frame].size;
570 info.width_mm = 0;
571 info.height_mm = 0;
572 }
573
574 info.xoff = 0;
575 info.yoff = 0;
576 info.width = frameSize.width;
577 info.height = frameSize.height;
578
Marc-André Lureauca19ef52021-04-13 20:39:11 +0400579 dpy_set_ui_info(dcl.con, &info, TRUE);
Akihiko Odaki15280e82021-06-16 23:19:10 +0900580}
581
Peter Maydell8d65dee2022-02-24 10:13:29 +0000582- (void) updateUIInfo
583{
584 if (!allow_events) {
585 /*
586 * Don't try to tell QEMU about UI information in the application
587 * startup phase -- we haven't yet registered dcl with the QEMU UI
588 * layer, and also trying to take the iothread lock would deadlock.
589 * When cocoa_display_init() does register the dcl, the UI layer
590 * will call cocoa_switch(), which will call updateUIInfo, so
591 * we don't lose any information here.
592 */
593 return;
594 }
595
596 with_iothread_lock(^{
597 [self updateUIInfoLocked];
598 });
599}
600
Akihiko Odaki15280e82021-06-16 23:19:10 +0900601- (void)viewDidMoveToWindow
602{
603 [self updateUIInfo];
604}
605
Peter Maydell72a3e312019-02-25 10:24:28 +0000606- (void) switchSurface:(pixman_image_t *)image
thsc304f7e2008-01-22 23:25:15 +0000607{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100608 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000609
Peter Maydell72a3e312019-02-25 10:24:28 +0000610 int w = pixman_image_get_width(image);
611 int h = pixman_image_get_height(image);
Peter Maydell381600d2014-06-23 10:35:22 +0100612 /* cdx == 0 means this is our very first surface, in which case we need
613 * to recalculate the content dimensions even if it happens to be the size
614 * of the initial empty window.
615 */
616 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000617
618 int oldh = screen.height;
619 if (isResize) {
620 // Resize before we trigger the redraw, or we'll redraw at the wrong size
621 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
622 screen.width = w;
623 screen.height = h;
624 [self setContentDimensions];
625 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
626 }
Peter Maydell8510d912013-03-18 20:28:21 +0000627
thsc304f7e2008-01-22 23:25:15 +0000628 // update screenBuffer
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900629 if (pixman_image) {
Peter Maydell55888402019-02-25 10:24:33 +0000630 pixman_image_unref(pixman_image);
631 }
thsc304f7e2008-01-22 23:25:15 +0000632
Peter Maydell55888402019-02-25 10:24:33 +0000633 pixman_image = image;
thsc304f7e2008-01-22 23:25:15 +0000634
635 // update windows
636 if (isFullscreen) {
637 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000638 [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 +0000639 } else {
640 if (qemu_name)
641 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000642 [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 +0000643 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000644
645 if (isResize) {
646 [normalWindow center];
647 }
thsc304f7e2008-01-22 23:25:15 +0000648}
649
650- (void) toggleFullScreen:(id)sender
651{
652 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
653
654 if (isFullscreen) { // switch from fullscreen to desktop
655 isFullscreen = FALSE;
656 [self ungrabMouse];
657 [self setContentDimensions];
Akihiko Odaki1e8b6f22021-02-20 10:31:38 +0900658 [fullScreenWindow close];
659 [normalWindow setContentView: self];
660 [normalWindow makeKeyAndOrderFront: self];
661 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000662 } else { // switch from desktop to fullscreen
663 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100664 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000665 [self grabMouse];
666 [self setContentDimensions];
Akihiko Odaki1e8b6f22021-02-20 10:31:38 +0900667 [NSMenu setMenuBarVisible:NO];
668 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
669 styleMask:NSWindowStyleMaskBorderless
670 backing:NSBackingStoreBuffered
671 defer:NO];
672 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
673 [fullScreenWindow setHasShadow:NO];
674 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
675 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
676 [[fullScreenWindow contentView] addSubview: self];
677 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000678 }
679}
680
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900681- (void) setFullGrab:(id)sender
682{
683 COCOA_DEBUG("QemuCocoaView: setFullGrab\n");
684
685 CGEventMask mask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged);
686 eventsTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
687 mask, handleTapEvent, self);
688 if (!eventsTap) {
689 warn_report("Could not create event tap, system key combos will not be captured.\n");
690 return;
691 } else {
692 COCOA_DEBUG("Global events tap created! Will capture system key combos.\n");
693 }
694
695 CFRunLoopRef runLoop = CFRunLoopGetCurrent();
696 if (!runLoop) {
697 warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
698 return;
699 }
700
701 CFRunLoopSourceRef tapEventsSrc = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventsTap, 0);
702 if (!tapEventsSrc ) {
703 warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
704 return;
705 }
706
707 CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode);
708 CFRelease(tapEventsSrc);
709}
710
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900711- (void) toggleKey: (int)keycode {
712 qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode));
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700713}
714
John Arbuckle9c3a4182017-11-07 10:14:14 +0000715// Does the work of sending input to the monitor
716- (void) handleMonitorInput:(NSEvent *)event
717{
718 int keysym = 0;
719 int control_key = 0;
720
721 // if the control key is down
722 if ([event modifierFlags] & NSEventModifierFlagControl) {
723 control_key = 1;
724 }
725
726 /* translates Macintosh keycodes to QEMU's keysym */
727
Philippe Mathieu-Daudé94592622022-02-15 16:21:14 +0100728 static const int without_control_translation[] = {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000729 [0 ... 0xff] = 0, // invalid key
730
731 [kVK_UpArrow] = QEMU_KEY_UP,
732 [kVK_DownArrow] = QEMU_KEY_DOWN,
733 [kVK_RightArrow] = QEMU_KEY_RIGHT,
734 [kVK_LeftArrow] = QEMU_KEY_LEFT,
735 [kVK_Home] = QEMU_KEY_HOME,
736 [kVK_End] = QEMU_KEY_END,
737 [kVK_PageUp] = QEMU_KEY_PAGEUP,
738 [kVK_PageDown] = QEMU_KEY_PAGEDOWN,
739 [kVK_ForwardDelete] = QEMU_KEY_DELETE,
740 [kVK_Delete] = QEMU_KEY_BACKSPACE,
741 };
742
Philippe Mathieu-Daudé94592622022-02-15 16:21:14 +0100743 static const int with_control_translation[] = {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000744 [0 ... 0xff] = 0, // invalid key
745
746 [kVK_UpArrow] = QEMU_KEY_CTRL_UP,
747 [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN,
748 [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT,
749 [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT,
750 [kVK_Home] = QEMU_KEY_CTRL_HOME,
751 [kVK_End] = QEMU_KEY_CTRL_END,
752 [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP,
753 [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN,
754 };
755
756 if (control_key != 0) { /* If the control key is being used */
757 if ([event keyCode] < ARRAY_SIZE(with_control_translation)) {
758 keysym = with_control_translation[[event keyCode]];
759 }
760 } else {
761 if ([event keyCode] < ARRAY_SIZE(without_control_translation)) {
762 keysym = without_control_translation[[event keyCode]];
763 }
764 }
765
766 // if not a key that needs translating
767 if (keysym == 0) {
768 NSString *ks = [event characters];
769 if ([ks length] > 0) {
770 keysym = [ks characterAtIndex:0];
771 }
772 }
773
774 if (keysym) {
775 kbd_put_keysym(keysym);
776 }
777}
778
Peter Maydell60105d72019-02-25 10:24:31 +0000779- (bool) handleEvent:(NSEvent *)event
thsc304f7e2008-01-22 23:25:15 +0000780{
Hikaru Nishidadff742a2019-10-15 10:07:34 +0900781 if(!allow_events) {
782 /*
783 * Just let OSX have all events that arrive before
784 * applicationDidFinishLaunching.
785 * This avoids a deadlock on the iothread lock, which cocoa_display_init()
786 * will not drop until after the app_started_sem is posted. (In theory
787 * there should not be any such events, but OSX Catalina now emits some.)
788 */
789 return false;
790 }
Peter Maydell60105d72019-02-25 10:24:31 +0000791 return bool_with_iothread_lock(^{
792 return [self handleEventLocked:event];
Peter Maydell31819e92019-02-25 10:24:27 +0000793 });
794}
thsc304f7e2008-01-22 23:25:15 +0000795
Peter Maydell60105d72019-02-25 10:24:31 +0000796- (bool) handleEventLocked:(NSEvent *)event
Peter Maydell31819e92019-02-25 10:24:27 +0000797{
Peter Maydell60105d72019-02-25 10:24:31 +0000798 /* Return true if we handled the event, false if it should be given to OSX */
Peter Maydell31819e92019-02-25 10:24:27 +0000799 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
thsc304f7e2008-01-22 23:25:15 +0000800 int buttons = 0;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700801 int keycode = 0;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100802 bool mouse_event = false;
John Arbuckle0c6c4392018-07-02 22:00:17 -0400803 static bool switched_to_fullscreen = false;
Chen Zhang2044dff2019-06-04 17:36:00 +0800804 // Location of event in virtual screen coordinates
805 NSPoint p = [self screenLocationOfEvent:event];
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900806 NSUInteger modifiers = [event modifierFlags];
807
Akihiko Odakiad7f2f82021-03-12 22:32:12 +0900808 /*
809 * Check -[NSEvent modifierFlags] here.
810 *
811 * There is a NSEventType for an event notifying the change of
812 * -[NSEvent modifierFlags], NSEventTypeFlagsChanged but these operations
813 * are performed for any events because a modifier state may change while
814 * the application is inactive (i.e. no events fire) and we don't want to
815 * wait for another modifier state change to detect such a change.
816 *
817 * NSEventModifierFlagCapsLock requires a special treatment. The other flags
818 * are handled in similar manners.
819 *
820 * NSEventModifierFlagCapsLock
821 * ---------------------------
822 *
823 * If CapsLock state is changed, "up" and "down" events will be fired in
824 * sequence, effectively updates CapsLock state on the guest.
825 *
826 * The other flags
827 * ---------------
828 *
829 * If a flag is not set, fire "up" events for all keys which correspond to
830 * the flag. Note that "down" events are not fired here because the flags
831 * checked here do not tell what exact keys are down.
832 *
833 * If one of the keys corresponding to a flag is down, we rely on
834 * -[NSEvent keyCode] of an event whose -[NSEvent type] is
835 * NSEventTypeFlagsChanged to know the exact key which is down, which has
836 * the following two downsides:
837 * - It does not work when the application is inactive as described above.
838 * - It malfactions *after* the modifier state is changed while the
839 * application is inactive. It is because -[NSEvent keyCode] does not tell
840 * if the key is up or down, and requires to infer the current state from
841 * the previous state. It is still possible to fix such a malfanction by
842 * completely leaving your hands from the keyboard, which hopefully makes
843 * this implementation usable enough.
844 */
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900845 if (!!(modifiers & NSEventModifierFlagCapsLock) !=
846 qkbd_state_modifier_get(kbd, QKBD_MOD_CAPSLOCK)) {
847 qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, true);
848 qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, false);
849 }
850
851 if (!(modifiers & NSEventModifierFlagShift)) {
852 qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT, false);
853 qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT_R, false);
854 }
855 if (!(modifiers & NSEventModifierFlagControl)) {
856 qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL, false);
857 qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL_R, false);
858 }
859 if (!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900860 if (swap_opt_cmd) {
861 qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
862 qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
863 } else {
864 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
865 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
866 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900867 }
868 if (!(modifiers & NSEventModifierFlagCommand)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900869 if (swap_opt_cmd) {
870 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
871 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
872 } else {
873 qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
874 qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
875 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900876 }
thsc304f7e2008-01-22 23:25:15 +0000877
878 switch ([event type]) {
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700879 case NSEventTypeFlagsChanged:
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900880 switch ([event keyCode]) {
881 case kVK_Shift:
882 if (!!(modifiers & NSEventModifierFlagShift)) {
883 [self toggleKey:Q_KEY_CODE_SHIFT];
884 }
885 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700886
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900887 case kVK_RightShift:
888 if (!!(modifiers & NSEventModifierFlagShift)) {
889 [self toggleKey:Q_KEY_CODE_SHIFT_R];
890 }
891 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700892
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900893 case kVK_Control:
894 if (!!(modifiers & NSEventModifierFlagControl)) {
895 [self toggleKey:Q_KEY_CODE_CTRL];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700896 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900897 break;
898
899 case kVK_RightControl:
900 if (!!(modifiers & NSEventModifierFlagControl)) {
901 [self toggleKey:Q_KEY_CODE_CTRL_R];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700902 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900903 break;
904
905 case kVK_Option:
906 if (!!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900907 if (swap_opt_cmd) {
908 [self toggleKey:Q_KEY_CODE_META_L];
909 } else {
910 [self toggleKey:Q_KEY_CODE_ALT];
911 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700912 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900913 break;
914
915 case kVK_RightOption:
916 if (!!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900917 if (swap_opt_cmd) {
918 [self toggleKey:Q_KEY_CODE_META_R];
919 } else {
920 [self toggleKey:Q_KEY_CODE_ALT_R];
921 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700922 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900923 break;
924
925 /* Don't pass command key changes to guest unless mouse is grabbed */
926 case kVK_Command:
927 if (isMouseGrabbed &&
Akihiko Odakid6b6dea2022-03-18 00:29:49 +0900928 !!(modifiers & NSEventModifierFlagCommand) &&
929 left_command_key_enabled) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900930 if (swap_opt_cmd) {
931 [self toggleKey:Q_KEY_CODE_ALT];
932 } else {
933 [self toggleKey:Q_KEY_CODE_META_L];
934 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700935 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900936 break;
937
938 case kVK_RightCommand:
939 if (isMouseGrabbed &&
940 !!(modifiers & NSEventModifierFlagCommand)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900941 if (swap_opt_cmd) {
942 [self toggleKey:Q_KEY_CODE_ALT_R];
943 } else {
944 [self toggleKey:Q_KEY_CODE_META_R];
945 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900946 }
947 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700948 }
thsc304f7e2008-01-22 23:25:15 +0000949 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700950 case NSEventTypeKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000951 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000952
Peter Maydell88959192013-12-08 22:59:02 +0000953 // forward command key combos to the host UI unless the mouse is grabbed
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700954 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
John Arbuckle0c6c4392018-07-02 22:00:17 -0400955 /*
956 * Prevent the command key from being stuck down in the guest
957 * when using Command-F to switch to full screen mode.
958 */
959 if (keycode == Q_KEY_CODE_F) {
960 switched_to_fullscreen = true;
961 }
Peter Maydell60105d72019-02-25 10:24:31 +0000962 return false;
thsc304f7e2008-01-22 23:25:15 +0000963 }
964
965 // default
thsc304f7e2008-01-22 23:25:15 +0000966
John Arbuckle5929e362017-11-07 10:14:14 +0000967 // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700968 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
John Arbuckle5929e362017-11-07 10:14:14 +0000969 NSString *keychar = [event charactersIgnoringModifiers];
970 if ([keychar length] == 1) {
971 char key = [keychar characterAtIndex:0];
972 switch (key) {
thsc304f7e2008-01-22 23:25:15 +0000973
John Arbuckle5929e362017-11-07 10:14:14 +0000974 // enable graphic console
975 case '1' ... '9':
976 console_select(key - '0' - 1); /* ascii math */
Peter Maydell60105d72019-02-25 10:24:31 +0000977 return true;
John Arbuckle5929e362017-11-07 10:14:14 +0000978
979 // release the mouse grab
980 case 'g':
981 [self ungrabMouse];
Peter Maydell60105d72019-02-25 10:24:31 +0000982 return true;
John Arbuckle5929e362017-11-07 10:14:14 +0000983 }
thsc304f7e2008-01-22 23:25:15 +0000984 }
Peter Maydellef2088f2017-11-07 10:14:14 +0000985 }
thsc304f7e2008-01-22 23:25:15 +0000986
Peter Maydellef2088f2017-11-07 10:14:14 +0000987 if (qemu_console_is_graphic(NULL)) {
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900988 qkbd_state_key_event(kbd, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000989 } else {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000990 [self handleMonitorInput: event];
thsc304f7e2008-01-22 23:25:15 +0000991 }
992 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700993 case NSEventTypeKeyUp:
thsc304f7e2008-01-22 23:25:15 +0000994 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000995
996 // don't pass the guest a spurious key-up if we treated this
997 // command-key combo as a host UI action
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700998 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell60105d72019-02-25 10:24:31 +0000999 return true;
Peter Maydell88959192013-12-08 22:59:02 +00001000 }
1001
Peter Maydell68c0aa62013-04-17 09:16:35 +00001002 if (qemu_console_is_graphic(NULL)) {
Akihiko Odaki6d73bb62021-03-10 23:46:02 +09001003 qkbd_state_key_event(kbd, keycode, false);
thsc304f7e2008-01-22 23:25:15 +00001004 }
1005 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001006 case NSEventTypeMouseMoved:
thsc304f7e2008-01-22 23:25:15 +00001007 if (isAbsoluteEnabled) {
Chen Zhang2044dff2019-06-04 17:36:00 +08001008 // Cursor re-entered into a window might generate events bound to screen coordinates
1009 // and `nil` window property, and in full screen mode, current window might not be
1010 // key window, where event location alone should suffice.
1011 if (![self screenContainsPoint:p] || !([[self window] isKeyWindow] || isFullscreen)) {
Peter Maydellf61c3872014-06-23 10:35:24 +01001012 if (isMouseGrabbed) {
1013 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +00001014 }
1015 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +01001016 if (!isMouseGrabbed) {
1017 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +00001018 }
1019 }
1020 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001021 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001022 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001023 case NSEventTypeLeftMouseDown:
Akihiko Odaki4295f832021-02-12 09:07:06 +09001024 buttons |= MOUSE_EVENT_LBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001025 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001026 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001027 case NSEventTypeRightMouseDown:
thsc304f7e2008-01-22 23:25:15 +00001028 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001029 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001030 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001031 case NSEventTypeOtherMouseDown:
thsc304f7e2008-01-22 23:25:15 +00001032 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001033 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001034 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001035 case NSEventTypeLeftMouseDragged:
Akihiko Odaki4295f832021-02-12 09:07:06 +09001036 buttons |= MOUSE_EVENT_LBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001037 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001038 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001039 case NSEventTypeRightMouseDragged:
thsc304f7e2008-01-22 23:25:15 +00001040 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001041 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001042 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001043 case NSEventTypeOtherMouseDragged:
thsc304f7e2008-01-22 23:25:15 +00001044 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001045 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001046 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001047 case NSEventTypeLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +01001048 mouse_event = true;
1049 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
Chen Zhang8e23e342019-06-04 17:36:48 +08001050 /*
1051 * In fullscreen mode, the window of cocoaView may not be the
1052 * key window, therefore the position relative to the virtual
1053 * screen alone will be sufficient.
1054 */
1055 if(isFullscreen || [[self window] isKeyWindow]) {
Programmingkid9e8204b2016-08-15 22:11:06 -04001056 [self grabMouse];
1057 }
thsc304f7e2008-01-22 23:25:15 +00001058 }
1059 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001060 case NSEventTypeRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001061 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001062 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001063 case NSEventTypeOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001064 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +00001065 break;
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001066 case NSEventTypeScrollWheel:
John Arbuckleae7313e2018-01-08 13:07:07 -05001067 /*
1068 * Send wheel events to the guest regardless of window focus.
1069 * This is in-line with standard Mac OS X UI behaviour.
1070 */
1071
John Arbuckledc3c89d2018-07-09 11:02:35 -04001072 /*
Dmitry Petrovd70a5de2022-01-08 16:39:44 +01001073 * We shouldn't have got a scroll event when deltaY and delta Y
1074 * are zero, hence no harm in dropping the event
John Arbuckledc3c89d2018-07-09 11:02:35 -04001075 */
Dmitry Petrovd70a5de2022-01-08 16:39:44 +01001076 if ([event deltaY] != 0 || [event deltaX] != 0) {
John Arbuckleae7313e2018-01-08 13:07:07 -05001077 /* Determine if this is a scroll up or scroll down event */
Dmitry Petrovd70a5de2022-01-08 16:39:44 +01001078 if ([event deltaY] != 0) {
1079 buttons = ([event deltaY] > 0) ?
John Arbuckledc3c89d2018-07-09 11:02:35 -04001080 INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Dmitry Petrovd70a5de2022-01-08 16:39:44 +01001081 } else if ([event deltaX] != 0) {
1082 buttons = ([event deltaX] > 0) ?
1083 INPUT_BUTTON_WHEEL_LEFT : INPUT_BUTTON_WHEEL_RIGHT;
1084 }
1085
Akihiko Odakicc7859c2021-02-19 17:44:19 +09001086 qemu_input_queue_btn(dcl.con, buttons, true);
John Arbuckledc3c89d2018-07-09 11:02:35 -04001087 qemu_input_event_sync();
Akihiko Odakicc7859c2021-02-19 17:44:19 +09001088 qemu_input_queue_btn(dcl.con, buttons, false);
John Arbuckledc3c89d2018-07-09 11:02:35 -04001089 qemu_input_event_sync();
1090 }
Dmitry Petrovd70a5de2022-01-08 16:39:44 +01001091
John Arbuckleae7313e2018-01-08 13:07:07 -05001092 /*
Dmitry Petrovd70a5de2022-01-08 16:39:44 +01001093 * Since deltaX/deltaY also report scroll wheel events we prevent mouse
John Arbuckleae7313e2018-01-08 13:07:07 -05001094 * movement code from executing.
1095 */
1096 mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +00001097 break;
1098 default:
Peter Maydell60105d72019-02-25 10:24:31 +00001099 return false;
thsc304f7e2008-01-22 23:25:15 +00001100 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001101
1102 if (mouse_event) {
Peter Maydell8d3a5d92015-11-26 15:19:28 +00001103 /* Don't send button events to the guest unless we've got a
1104 * mouse grab or window focus. If we have neither then this event
1105 * is the user clicking on the background window to activate and
1106 * bring us to the front, which will be done by the sendEvent
1107 * call below. We definitely don't want to pass that click through
1108 * to the guest.
1109 */
1110 if ((isMouseGrabbed || [[self window] isKeyWindow]) &&
1111 (last_buttons != buttons)) {
Eric Blake7fb1cf12015-11-18 01:52:57 -07001112 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001113 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1114 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
John Arbuckleae7313e2018-01-08 13:07:07 -05001115 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001116 };
Akihiko Odakicc7859c2021-02-19 17:44:19 +09001117 qemu_input_update_buttons(dcl.con, bmap, last_buttons, buttons);
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001118 last_buttons = buttons;
1119 }
Peter Maydellf61c3872014-06-23 10:35:24 +01001120 if (isMouseGrabbed) {
1121 if (isAbsoluteEnabled) {
1122 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
1123 * The check on screenContainsPoint is to avoid sending out of range values for
1124 * clicks in the titlebar.
1125 */
1126 if ([self screenContainsPoint:p]) {
Akihiko Odakicc7859c2021-02-19 17:44:19 +09001127 qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x, 0, screen.width);
1128 qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
Peter Maydellf61c3872014-06-23 10:35:24 +01001129 }
1130 } else {
Akihiko Odakicc7859c2021-02-19 17:44:19 +09001131 qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, (int)[event deltaX]);
1132 qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, (int)[event deltaY]);
Peter Maydellf61c3872014-06-23 10:35:24 +01001133 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001134 } else {
Peter Maydell60105d72019-02-25 10:24:31 +00001135 return false;
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001136 }
1137 qemu_input_event_sync();
1138 }
Peter Maydell60105d72019-02-25 10:24:31 +00001139 return true;
thsc304f7e2008-01-22 23:25:15 +00001140}
1141
1142- (void) grabMouse
1143{
1144 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
1145
1146 if (!isFullscreen) {
1147 if (qemu_name)
John Arbuckle5929e362017-11-07 10:14:14 +00001148 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt + g to release Mouse)", qemu_name]];
thsc304f7e2008-01-22 23:25:15 +00001149 else
John Arbuckle5929e362017-11-07 10:14:14 +00001150 [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release Mouse)"];
thsc304f7e2008-01-22 23:25:15 +00001151 }
Peter Maydell13aefd32014-06-23 10:35:25 +01001152 [self hideCursor];
Akihiko Odakid1929062021-02-23 00:07:14 +09001153 CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled);
Peter Maydell49b9bd42013-12-08 22:59:03 +00001154 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +00001155}
1156
1157- (void) ungrabMouse
1158{
1159 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
1160
1161 if (!isFullscreen) {
1162 if (qemu_name)
1163 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
1164 else
1165 [normalWindow setTitle:@"QEMU"];
1166 }
Peter Maydell13aefd32014-06-23 10:35:25 +01001167 [self unhideCursor];
Akihiko Odakid1929062021-02-23 00:07:14 +09001168 CGAssociateMouseAndMouseCursorPosition(TRUE);
Peter Maydell49b9bd42013-12-08 22:59:03 +00001169 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +00001170}
1171
Akihiko Odakid1929062021-02-23 00:07:14 +09001172- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {
1173 isAbsoluteEnabled = tIsAbsoluteEnabled;
1174 if (isMouseGrabbed) {
1175 CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled);
1176 }
1177}
Peter Maydell49b9bd42013-12-08 22:59:03 +00001178- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +00001179- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
1180- (float) cdx {return cdx;}
1181- (float) cdy {return cdy;}
1182- (QEMUScreen) gscreen {return screen;}
John Arbuckle3b178b72015-09-25 23:14:00 +01001183
1184/*
1185 * Makes the target think all down keys are being released.
1186 * This prevents a stuck key problem, since we will not see
1187 * key up events for those keys after we have lost focus.
1188 */
1189- (void) raiseAllKeys
1190{
Peter Maydell31819e92019-02-25 10:24:27 +00001191 with_iothread_lock(^{
Akihiko Odaki6d73bb62021-03-10 23:46:02 +09001192 qkbd_state_lift_all_keys(kbd);
Peter Maydell31819e92019-02-25 10:24:27 +00001193 });
John Arbuckle3b178b72015-09-25 23:14:00 +01001194}
bellard5b0753e2005-03-01 21:37:28 +00001195@end
1196
1197
thsc304f7e2008-01-22 23:25:15 +00001198
bellard5b0753e2005-03-01 21:37:28 +00001199/*
1200 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +00001201 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +00001202 ------------------------------------------------------
1203*/
thsc304f7e2008-01-22 23:25:15 +00001204@interface QemuCocoaAppController : NSObject
John Arbuckled9bc14f2015-09-25 23:13:59 +01001205 <NSWindowDelegate, NSApplicationDelegate>
bellard5b0753e2005-03-01 21:37:28 +00001206{
1207}
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001208- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +00001209- (void)toggleFullScreen:(id)sender;
1210- (void)showQEMUDoc:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001211- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +01001212- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +01001213- (void)pauseQEMU:(id)sender;
1214- (void)resumeQEMU:(id)sender;
1215- (void)displayPause;
1216- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +01001217- (void)restartQEMU:(id)sender;
1218- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +01001219- (void)ejectDeviceMedia:(id)sender;
1220- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +01001221- (BOOL)verifyQuit;
John Arbucklef4747902016-03-23 14:26:17 +00001222- (void)openDocumentation:(NSString *)filename;
Programmingkid9e8204b2016-08-15 22:11:06 -04001223- (IBAction) do_about_menu_item: (id) sender;
John Arbucklee47ec1a2017-06-13 23:17:38 -04001224- (void)adjustSpeed:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +00001225@end
1226
thsc304f7e2008-01-22 23:25:15 +00001227@implementation QemuCocoaAppController
1228- (id) init
1229{
1230 COCOA_DEBUG("QemuCocoaAppController: init\n");
1231
1232 self = [super init];
1233 if (self) {
1234
1235 // create a view and add it to the window
1236 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
1237 if(!cocoaView) {
Akihiko Odaki43137392021-02-23 22:11:06 +09001238 error_report("(cocoa) can't create a view");
thsc304f7e2008-01-22 23:25:15 +00001239 exit(1);
1240 }
1241
1242 // create a window
1243 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001244 styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable
thsc304f7e2008-01-22 23:25:15 +00001245 backing:NSBackingStoreBuffered defer:NO];
1246 if(!normalWindow) {
Akihiko Odaki43137392021-02-23 22:11:06 +09001247 error_report("(cocoa) can't create window");
thsc304f7e2008-01-22 23:25:15 +00001248 exit(1);
1249 }
1250 [normalWindow setAcceptsMouseMovedEvents:YES];
John Arbucklea1dbc052015-10-13 21:51:18 +01001251 [normalWindow setTitle:@"QEMU"];
thsc304f7e2008-01-22 23:25:15 +00001252 [normalWindow setContentView:cocoaView];
1253 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +00001254 [normalWindow center];
John Arbuckled9bc14f2015-09-25 23:13:59 +01001255 [normalWindow setDelegate: self];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001256 stretch_video = false;
John Arbuckle8524f1c2015-06-19 10:53:27 +01001257
1258 /* Used for displaying pause on the screen */
1259 pauseLabel = [NSTextField new];
1260 [pauseLabel setBezeled:YES];
1261 [pauseLabel setDrawsBackground:YES];
1262 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
1263 [pauseLabel setEditable:NO];
1264 [pauseLabel setSelectable:NO];
1265 [pauseLabel setStringValue: @"Paused"];
1266 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
1267 [pauseLabel setTextColor: [NSColor blackColor]];
1268 [pauseLabel sizeToFit];
thsc304f7e2008-01-22 23:25:15 +00001269 }
1270 return self;
1271}
1272
1273- (void) dealloc
1274{
1275 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
1276
1277 if (cocoaView)
1278 [cocoaView release];
1279 [super dealloc];
1280}
1281
bellard5b0753e2005-03-01 21:37:28 +00001282- (void)applicationDidFinishLaunching: (NSNotification *) note
1283{
thsc304f7e2008-01-22 23:25:15 +00001284 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
Hikaru Nishidadff742a2019-10-15 10:07:34 +09001285 allow_events = true;
Peter Maydell55888402019-02-25 10:24:33 +00001286 /* Tell cocoa_display_init to proceed */
1287 qemu_sem_post(&app_started_sem);
bellard5b0753e2005-03-01 21:37:28 +00001288}
1289
1290- (void)applicationWillTerminate:(NSNotification *)aNotification
1291{
thsc304f7e2008-01-22 23:25:15 +00001292 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
1293
Akihiko Odaki2910abd2022-05-29 17:25:08 +09001294 with_iothread_lock(^{
1295 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
1296 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
1297 });
Akihiko Odaki40c01932021-02-19 20:16:52 +09001298
1299 /*
1300 * Sleep here, because returning will cause OSX to kill us
1301 * immediately; the QEMU main loop will handle the shutdown
1302 * request and terminate the process.
1303 */
1304 [NSThread sleepForTimeInterval:INFINITY];
bellard5b0753e2005-03-01 21:37:28 +00001305}
1306
Andreas Färber41ea49b2009-12-14 22:13:27 +01001307- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
1308{
1309 return YES;
1310}
1311
John Arbuckled9bc14f2015-09-25 23:13:59 +01001312- (NSApplicationTerminateReply)applicationShouldTerminate:
1313 (NSApplication *)sender
1314{
1315 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
1316 return [self verifyQuit];
1317}
1318
Akihiko Odaki15280e82021-06-16 23:19:10 +09001319- (void)windowDidChangeScreen:(NSNotification *)notification
1320{
1321 [cocoaView updateUIInfo];
1322}
1323
1324- (void)windowDidResize:(NSNotification *)notification
1325{
1326 [cocoaView updateUIInfo];
1327}
1328
John Arbuckled9bc14f2015-09-25 23:13:59 +01001329/* Called when the user clicks on a window's close button */
1330- (BOOL)windowShouldClose:(id)sender
1331{
1332 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
1333 [NSApp terminate: sender];
1334 /* If the user allows the application to quit then the call to
1335 * NSApp terminate will never return. If we get here then the user
1336 * cancelled the quit, so we should return NO to not permit the
1337 * closing of this window.
1338 */
1339 return NO;
1340}
1341
John Arbuckle3b178b72015-09-25 23:14:00 +01001342/* Called when QEMU goes into the background */
1343- (void) applicationWillResignActive: (NSNotification *)aNotification
1344{
1345 COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
Carwyn Ellis69221df2022-01-02 17:41:53 +00001346 [cocoaView ungrabMouse];
John Arbuckle3b178b72015-09-25 23:14:00 +01001347 [cocoaView raiseAllKeys];
1348}
1349
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001350/* We abstract the method called by the Enter Fullscreen menu item
1351 * because Mac OS 10.7 and higher disables it. This is because of the
1352 * menu item's old selector's name toggleFullScreen:
1353 */
1354- (void) doToggleFullScreen:(id)sender
1355{
1356 [self toggleFullScreen:(id)sender];
1357}
1358
thsc304f7e2008-01-22 23:25:15 +00001359- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +00001360{
thsc304f7e2008-01-22 23:25:15 +00001361 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
1362
1363 [cocoaView toggleFullScreen:sender];
1364}
1365
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +09001366- (void) setFullGrab:(id)sender
1367{
1368 COCOA_DEBUG("QemuCocoaAppController: setFullGrab\n");
1369
1370 [cocoaView setFullGrab:sender];
1371}
1372
John Arbucklef4747902016-03-23 14:26:17 +00001373/* Tries to find then open the specified filename */
1374- (void) openDocumentation: (NSString *) filename
1375{
1376 /* Where to look for local files */
Roman Bolshakov8d6fda82021-01-09 00:38:15 +03001377 NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"docs/"};
John Arbucklef4747902016-03-23 14:26:17 +00001378 NSString *full_file_path;
Roman Bolshakov1ff5a062021-01-02 18:07:21 +03001379 NSURL *full_file_url;
John Arbucklef4747902016-03-23 14:26:17 +00001380
1381 /* iterate thru the possible paths until the file is found */
1382 int index;
1383 for (index = 0; index < ARRAY_SIZE(path_array); index++) {
1384 full_file_path = [[NSBundle mainBundle] executablePath];
1385 full_file_path = [full_file_path stringByDeletingLastPathComponent];
1386 full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
1387 path_array[index], filename];
Roman Bolshakov1ff5a062021-01-02 18:07:21 +03001388 full_file_url = [NSURL fileURLWithPath: full_file_path
1389 isDirectory: false];
1390 if ([[NSWorkspace sharedWorkspace] openURL: full_file_url] == YES) {
John Arbucklef4747902016-03-23 14:26:17 +00001391 return;
1392 }
1393 }
1394
1395 /* If none of the paths opened a file */
1396 NSBeep();
1397 QEMU_Alert(@"Failed to open file");
1398}
1399
thsc304f7e2008-01-22 23:25:15 +00001400- (void)showQEMUDoc:(id)sender
1401{
1402 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1403
Peter Maydell1879f242020-02-28 15:36:16 +00001404 [self openDocumentation: @"index.html"];
thsc304f7e2008-01-22 23:25:15 +00001405}
1406
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001407/* Stretches video to fit host monitor size */
1408- (void)zoomToFit:(id) sender
1409{
1410 stretch_video = !stretch_video;
1411 if (stretch_video == true) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001412 [sender setState: NSControlStateValueOn];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001413 } else {
Brendan Shanks5e246002019-01-31 23:12:25 -08001414 [sender setState: NSControlStateValueOff];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001415 }
1416}
bellard5b0753e2005-03-01 21:37:28 +00001417
Programmingkidb4c6a112015-05-19 09:11:18 +01001418/* Displays the console on the screen */
1419- (void)displayConsole:(id)sender
1420{
1421 console_select([sender tag]);
1422}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001423
1424/* Pause the guest */
1425- (void)pauseQEMU:(id)sender
1426{
Peter Maydell31819e92019-02-25 10:24:27 +00001427 with_iothread_lock(^{
1428 qmp_stop(NULL);
1429 });
John Arbuckle8524f1c2015-06-19 10:53:27 +01001430 [sender setEnabled: NO];
1431 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1432 [self displayPause];
1433}
1434
1435/* Resume running the guest operating system */
1436- (void)resumeQEMU:(id) sender
1437{
Peter Maydell31819e92019-02-25 10:24:27 +00001438 with_iothread_lock(^{
1439 qmp_cont(NULL);
1440 });
John Arbuckle8524f1c2015-06-19 10:53:27 +01001441 [sender setEnabled: NO];
1442 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1443 [self removePause];
1444}
1445
1446/* Displays the word pause on the screen */
1447- (void)displayPause
1448{
1449 /* Coordinates have to be calculated each time because the window can change its size */
1450 int xCoord, yCoord, width, height;
1451 xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
1452 yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
1453 width = [pauseLabel frame].size.width;
1454 height = [pauseLabel frame].size.height;
1455 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1456 [cocoaView addSubview: pauseLabel];
1457}
1458
1459/* Removes the word pause from the screen */
1460- (void)removePause
1461{
1462 [pauseLabel removeFromSuperview];
1463}
1464
John Arbuckle27074612015-06-19 10:53:27 +01001465/* Restarts QEMU */
1466- (void)restartQEMU:(id)sender
1467{
Peter Maydell31819e92019-02-25 10:24:27 +00001468 with_iothread_lock(^{
1469 qmp_system_reset(NULL);
1470 });
John Arbuckle27074612015-06-19 10:53:27 +01001471}
1472
1473/* Powers down QEMU */
1474- (void)powerDownQEMU:(id)sender
1475{
Peter Maydell31819e92019-02-25 10:24:27 +00001476 with_iothread_lock(^{
1477 qmp_system_powerdown(NULL);
1478 });
John Arbuckle27074612015-06-19 10:53:27 +01001479}
1480
John Arbuckle693a3e02015-06-19 10:53:27 +01001481/* Ejects the media.
1482 * Uses sender's tag to figure out the device to eject.
1483 */
1484- (void)ejectDeviceMedia:(id)sender
1485{
1486 NSString * drive;
1487 drive = [sender representedObject];
1488 if(drive == nil) {
1489 NSBeep();
1490 QEMU_Alert(@"Failed to find drive to eject!");
1491 return;
1492 }
1493
Peter Maydell31819e92019-02-25 10:24:27 +00001494 __block Error *err = NULL;
1495 with_iothread_lock(^{
1496 qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding],
1497 false, NULL, false, false, &err);
1498 });
John Arbuckle693a3e02015-06-19 10:53:27 +01001499 handleAnyDeviceErrors(err);
1500}
1501
1502/* Displays a dialog box asking the user to select an image file to load.
1503 * Uses sender's represented object value to figure out which drive to use.
1504 */
1505- (void)changeDeviceMedia:(id)sender
1506{
1507 /* Find the drive name */
1508 NSString * drive;
1509 drive = [sender representedObject];
1510 if(drive == nil) {
1511 NSBeep();
1512 QEMU_Alert(@"Could not find drive!");
1513 return;
1514 }
1515
1516 /* Display the file open dialog */
1517 NSOpenPanel * openPanel;
1518 openPanel = [NSOpenPanel openPanel];
1519 [openPanel setCanChooseFiles: YES];
1520 [openPanel setAllowsMultipleSelection: NO];
Peter Maydellb5725382018-05-29 19:15:23 +01001521 if([openPanel runModal] == NSModalResponseOK) {
John Arbuckle693a3e02015-06-19 10:53:27 +01001522 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1523 if(file == nil) {
1524 NSBeep();
1525 QEMU_Alert(@"Failed to convert URL to file path!");
1526 return;
1527 }
1528
Peter Maydell31819e92019-02-25 10:24:27 +00001529 __block Error *err = NULL;
1530 with_iothread_lock(^{
1531 qmp_blockdev_change_medium(true,
1532 [drive cStringUsingEncoding:
1533 NSASCIIStringEncoding],
1534 false, NULL,
1535 [file cStringUsingEncoding:
1536 NSASCIIStringEncoding],
1537 true, "raw",
Denis V. Lunev80dd5af2022-04-13 01:18:46 +03001538 true, false,
Peter Maydell31819e92019-02-25 10:24:27 +00001539 false, 0,
1540 &err);
1541 });
John Arbuckle693a3e02015-06-19 10:53:27 +01001542 handleAnyDeviceErrors(err);
1543 }
1544}
1545
John Arbuckled9bc14f2015-09-25 23:13:59 +01001546/* Verifies if the user really wants to quit */
1547- (BOOL)verifyQuit
1548{
1549 NSAlert *alert = [NSAlert new];
1550 [alert autorelease];
1551 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1552 [alert addButtonWithTitle: @"Cancel"];
1553 [alert addButtonWithTitle: @"Quit"];
1554 if([alert runModal] == NSAlertSecondButtonReturn) {
1555 return YES;
1556 } else {
1557 return NO;
1558 }
1559}
1560
Programmingkid9e8204b2016-08-15 22:11:06 -04001561/* The action method for the About menu item */
1562- (IBAction) do_about_menu_item: (id) sender
1563{
Akihiko Odaki99eb3132022-02-27 13:22:41 +09001564 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1565 char *icon_path_c = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/512x512/apps/qemu.png");
1566 NSString *icon_path = [NSString stringWithUTF8String:icon_path_c];
1567 g_free(icon_path_c);
1568 NSImage *icon = [[NSImage alloc] initWithContentsOfFile:icon_path];
1569 NSString *version = @"QEMU emulator version " QEMU_FULL_VERSION;
1570 NSString *copyright = @QEMU_COPYRIGHT;
1571 NSDictionary *options;
1572 if (icon) {
1573 options = @{
1574 NSAboutPanelOptionApplicationIcon : icon,
1575 NSAboutPanelOptionApplicationVersion : version,
1576 @"Copyright" : copyright,
1577 };
1578 [icon release];
1579 } else {
1580 options = @{
1581 NSAboutPanelOptionApplicationVersion : version,
1582 @"Copyright" : copyright,
1583 };
Akihiko Odakia0f973f2021-03-09 21:22:26 +09001584 }
Akihiko Odaki99eb3132022-02-27 13:22:41 +09001585 [NSApp orderFrontStandardAboutPanelWithOptions:options];
1586 [pool release];
Programmingkid9e8204b2016-08-15 22:11:06 -04001587}
1588
John Arbucklee47ec1a2017-06-13 23:17:38 -04001589/* Used by the Speed menu items */
1590- (void)adjustSpeed:(id)sender
1591{
1592 int throttle_pct; /* throttle percentage */
1593 NSMenu *menu;
1594
1595 menu = [sender menu];
1596 if (menu != nil)
1597 {
1598 /* Unselect the currently selected item */
1599 for (NSMenuItem *item in [menu itemArray]) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001600 if (item.state == NSControlStateValueOn) {
1601 [item setState: NSControlStateValueOff];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001602 break;
1603 }
1604 }
1605 }
1606
1607 // check the menu item
Brendan Shanks5e246002019-01-31 23:12:25 -08001608 [sender setState: NSControlStateValueOn];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001609
1610 // get the throttle percentage
1611 throttle_pct = [sender tag];
1612
Peter Maydell31819e92019-02-25 10:24:27 +00001613 with_iothread_lock(^{
1614 cpu_throttle_set(throttle_pct);
1615 });
John Arbucklee47ec1a2017-06-13 23:17:38 -04001616 COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
1617}
1618
Programmingkidb4c6a112015-05-19 09:11:18 +01001619@end
bellard5b0753e2005-03-01 21:37:28 +00001620
Peter Maydell61a2ed42019-02-25 10:24:32 +00001621@interface QemuApplication : NSApplication
1622@end
1623
1624@implementation QemuApplication
1625- (void)sendEvent:(NSEvent *)event
1626{
1627 COCOA_DEBUG("QemuApplication: sendEvent\n");
Peter Maydell55888402019-02-25 10:24:33 +00001628 if (![cocoaView handleEvent:event]) {
1629 [super sendEvent: event];
1630 }
Peter Maydell61a2ed42019-02-25 10:24:32 +00001631}
1632@end
1633
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001634static void create_initial_menus(void)
1635{
thsc304f7e2008-01-22 23:25:15 +00001636 // Add menus
1637 NSMenu *menu;
1638 NSMenuItem *menuItem;
1639
bellard5b0753e2005-03-01 21:37:28 +00001640 [NSApp setMainMenu:[[NSMenu alloc] init]];
Akihiko Odaki5b6988c2022-02-14 18:13:20 +09001641 [NSApp setServicesMenu:[[NSMenu alloc] initWithTitle:@"Services"]];
bellard5b0753e2005-03-01 21:37:28 +00001642
thsc304f7e2008-01-22 23:25:15 +00001643 // Application menu
1644 menu = [[NSMenu alloc] initWithTitle:@""];
Programmingkid9e8204b2016-08-15 22:11:06 -04001645 [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU
thsc304f7e2008-01-22 23:25:15 +00001646 [menu addItem:[NSMenuItem separatorItem]]; //Separator
Akihiko Odaki5b6988c2022-02-14 18:13:20 +09001647 menuItem = [menu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
1648 [menuItem setSubmenu:[NSApp servicesMenu]];
1649 [menu addItem:[NSMenuItem separatorItem]];
thsc304f7e2008-01-22 23:25:15 +00001650 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1651 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001652 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
thsc304f7e2008-01-22 23:25:15 +00001653 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1654 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1655 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1656 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1657 [menuItem setSubmenu:menu];
1658 [[NSApp mainMenu] addItem:menuItem];
1659 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001660
John Arbuckle8524f1c2015-06-19 10:53:27 +01001661 // Machine menu
1662 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1663 [menu setAutoenablesItems: NO];
1664 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1665 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1666 [menu addItem: menuItem];
1667 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001668 [menu addItem: [NSMenuItem separatorItem]];
1669 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1670 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001671 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1672 [menuItem setSubmenu:menu];
1673 [[NSApp mainMenu] addItem:menuItem];
1674
thsc304f7e2008-01-22 23:25:15 +00001675 // View menu
1676 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001677 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1678 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001679 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1680 [menuItem setSubmenu:menu];
1681 [[NSApp mainMenu] addItem:menuItem];
1682
John Arbucklee47ec1a2017-06-13 23:17:38 -04001683 // Speed menu
1684 menu = [[NSMenu alloc] initWithTitle:@"Speed"];
1685
1686 // Add the rest of the Speed menu items
1687 int p, percentage, throttle_pct;
1688 for (p = 10; p >= 0; p--)
1689 {
1690 percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
1691
1692 menuItem = [[[NSMenuItem alloc]
1693 initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
1694
1695 if (percentage == 100) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001696 [menuItem setState: NSControlStateValueOn];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001697 }
1698
1699 /* Calculate the throttle percentage */
1700 throttle_pct = -1 * percentage + 100;
1701
1702 [menuItem setTag: throttle_pct];
1703 [menu addItem: menuItem];
1704 }
1705 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
1706 [menuItem setSubmenu:menu];
1707 [[NSApp mainMenu] addItem:menuItem];
1708
thsc304f7e2008-01-22 23:25:15 +00001709 // Window menu
1710 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1711 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1712 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1713 [menuItem setSubmenu:menu];
1714 [[NSApp mainMenu] addItem:menuItem];
1715 [NSApp setWindowsMenu:menu];
1716
1717 // Help menu
1718 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1719 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
thsc304f7e2008-01-22 23:25:15 +00001720 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1721 [menuItem setSubmenu:menu];
1722 [[NSApp mainMenu] addItem:menuItem];
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001723}
1724
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001725/* Returns a name for a given console */
1726static NSString * getConsoleName(QemuConsole * console)
1727{
Akihiko Odakica511602022-02-15 09:03:05 +01001728 g_autofree char *label = qemu_console_get_label(console);
1729
1730 return [NSString stringWithUTF8String:label];
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001731}
1732
1733/* Add an entry to the View menu for each console */
1734static void add_console_menu_entries(void)
1735{
1736 NSMenu *menu;
1737 NSMenuItem *menuItem;
1738 int index = 0;
1739
1740 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1741
1742 [menu addItem:[NSMenuItem separatorItem]];
1743
1744 while (qemu_console_lookup_by_index(index) != NULL) {
1745 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1746 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1747 [menuItem setTag: index];
1748 [menu addItem: menuItem];
1749 index++;
1750 }
1751}
1752
1753/* Make menu items for all removable devices.
1754 * Each device is given an 'Eject' and 'Change' menu item.
1755 */
1756static void addRemovableDevicesMenuItems(void)
1757{
1758 NSMenu *menu;
1759 NSMenuItem *menuItem;
1760 BlockInfoList *currentDevice, *pointerToFree;
1761 NSString *deviceName;
1762
1763 currentDevice = qmp_query_block(NULL);
1764 pointerToFree = currentDevice;
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001765
1766 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1767
1768 // Add a separator between related groups of menu items
1769 [menu addItem:[NSMenuItem separatorItem]];
1770
1771 // Set the attributes to the "Removable Media" menu item
1772 NSString *titleString = @"Removable Media";
1773 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1774 NSColor *newColor = [NSColor blackColor];
1775 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1776 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1777 traits:NSBoldFontMask|NSItalicFontMask
1778 weight:0
1779 size:14];
1780 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1781 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1782 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1783
1784 // Add the "Removable Media" menu item
1785 menuItem = [NSMenuItem new];
1786 [menuItem setAttributedTitle: attString];
1787 [menuItem setEnabled: NO];
1788 [menu addItem: menuItem];
1789
1790 /* Loop through all the block devices in the emulator */
1791 while (currentDevice) {
1792 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1793
1794 if(currentDevice->value->removable) {
1795 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1796 action: @selector(changeDeviceMedia:)
1797 keyEquivalent: @""];
1798 [menu addItem: menuItem];
1799 [menuItem setRepresentedObject: deviceName];
1800 [menuItem autorelease];
1801
1802 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1803 action: @selector(ejectDeviceMedia:)
1804 keyEquivalent: @""];
1805 [menu addItem: menuItem];
1806 [menuItem setRepresentedObject: deviceName];
1807 [menuItem autorelease];
1808 }
1809 currentDevice = currentDevice->next;
1810 }
1811 qapi_free_BlockInfoList(pointerToFree);
1812}
1813
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001814@interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner>
1815@end
1816
1817@implementation QemuCocoaPasteboardTypeOwner
1818
1819- (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSPasteboardType)type
1820{
1821 if (type != NSPasteboardTypeString) {
1822 return;
1823 }
1824
1825 with_iothread_lock(^{
1826 QemuClipboardInfo *info = qemu_clipboard_info_ref(cbinfo);
1827 qemu_event_reset(&cbevent);
1828 qemu_clipboard_request(info, QEMU_CLIPBOARD_TYPE_TEXT);
1829
1830 while (info == cbinfo &&
1831 info->types[QEMU_CLIPBOARD_TYPE_TEXT].available &&
1832 info->types[QEMU_CLIPBOARD_TYPE_TEXT].data == NULL) {
1833 qemu_mutex_unlock_iothread();
1834 qemu_event_wait(&cbevent);
1835 qemu_mutex_lock_iothread();
1836 }
1837
1838 if (info == cbinfo) {
1839 NSData *data = [[NSData alloc] initWithBytes:info->types[QEMU_CLIPBOARD_TYPE_TEXT].data
1840 length:info->types[QEMU_CLIPBOARD_TYPE_TEXT].size];
1841 [sender setData:data forType:NSPasteboardTypeString];
1842 [data release];
1843 }
1844
1845 qemu_clipboard_info_unref(info);
1846 });
1847}
1848
1849@end
1850
1851static QemuCocoaPasteboardTypeOwner *cbowner;
1852
1853static void cocoa_clipboard_notify(Notifier *notifier, void *data);
1854static void cocoa_clipboard_request(QemuClipboardInfo *info,
1855 QemuClipboardType type);
1856
1857static QemuClipboardPeer cbpeer = {
1858 .name = "cocoa",
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001859 .notifier = { .notify = cocoa_clipboard_notify },
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001860 .request = cocoa_clipboard_request
1861};
1862
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001863static void cocoa_clipboard_update_info(QemuClipboardInfo *info)
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001864{
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001865 if (info->owner == &cbpeer || info->selection != QEMU_CLIPBOARD_SELECTION_CLIPBOARD) {
1866 return;
1867 }
1868
1869 if (info != cbinfo) {
1870 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1871 qemu_clipboard_info_unref(cbinfo);
1872 cbinfo = qemu_clipboard_info_ref(info);
1873 cbchangecount = [[NSPasteboard generalPasteboard] declareTypes:@[NSPasteboardTypeString] owner:cbowner];
1874 [pool release];
1875 }
1876
1877 qemu_event_set(&cbevent);
1878}
1879
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001880static void cocoa_clipboard_notify(Notifier *notifier, void *data)
1881{
1882 QemuClipboardNotify *notify = data;
1883
1884 switch (notify->type) {
1885 case QEMU_CLIPBOARD_UPDATE_INFO:
1886 cocoa_clipboard_update_info(notify->info);
1887 return;
Marc-André Lureau505dbf92021-07-19 19:49:56 +04001888 case QEMU_CLIPBOARD_RESET_SERIAL:
1889 /* ignore */
1890 return;
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001891 }
1892}
1893
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001894static void cocoa_clipboard_request(QemuClipboardInfo *info,
1895 QemuClipboardType type)
1896{
1897 NSData *text;
1898
1899 switch (type) {
1900 case QEMU_CLIPBOARD_TYPE_TEXT:
1901 text = [[NSPasteboard generalPasteboard] dataForType:NSPasteboardTypeString];
1902 if (text) {
1903 qemu_clipboard_set_data(&cbpeer, info, type,
1904 [text length], [text bytes], true);
1905 [text release];
1906 }
1907 break;
1908 default:
1909 break;
1910 }
1911}
1912
Peter Maydell55888402019-02-25 10:24:33 +00001913/*
1914 * The startup process for the OSX/Cocoa UI is complicated, because
1915 * OSX insists that the UI runs on the initial main thread, and so we
1916 * need to start a second thread which runs the vl.c qemu_main():
1917 *
1918 * Initial thread: 2nd thread:
1919 * in main():
1920 * create qemu-main thread
1921 * wait on display_init semaphore
1922 * call qemu_main()
1923 * ...
1924 * in cocoa_display_init():
1925 * post the display_init semaphore
1926 * wait on app_started semaphore
1927 * create application, menus, etc
1928 * enter OSX run loop
1929 * in applicationDidFinishLaunching:
1930 * post app_started semaphore
1931 * tell main thread to fullscreen if needed
1932 * [...]
1933 * run qemu main-loop
1934 *
1935 * We do this in two stages so that we don't do the creation of the
1936 * GUI application menus and so on for command line options like --help
1937 * where we want to just print text to stdout and exit immediately.
1938 */
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001939
Peter Maydell55888402019-02-25 10:24:33 +00001940static void *call_qemu_main(void *opaque)
1941{
1942 int status;
1943
1944 COCOA_DEBUG("Second thread: calling qemu_main()\n");
1945 status = qemu_main(gArgc, gArgv, *_NSGetEnviron());
1946 COCOA_DEBUG("Second thread: qemu_main() returned, exiting\n");
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001947 [cbowner release];
Peter Maydell55888402019-02-25 10:24:33 +00001948 exit(status);
1949}
1950
Akihiko Odaki40a9aad2021-07-09 01:56:19 +09001951int main (int argc, char **argv) {
Peter Maydell55888402019-02-25 10:24:33 +00001952 QemuThread thread;
1953
1954 COCOA_DEBUG("Entered main()\n");
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001955 gArgc = argc;
Akihiko Odaki40a9aad2021-07-09 01:56:19 +09001956 gArgv = argv;
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001957
Peter Maydell55888402019-02-25 10:24:33 +00001958 qemu_sem_init(&display_init_sem, 0);
1959 qemu_sem_init(&app_started_sem, 0);
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001960
Peter Maydell55888402019-02-25 10:24:33 +00001961 qemu_thread_create(&thread, "qemu_main", call_qemu_main,
1962 NULL, QEMU_THREAD_DETACHED);
1963
1964 COCOA_DEBUG("Main thread: waiting for display_init_sem\n");
1965 qemu_sem_wait(&display_init_sem);
1966 COCOA_DEBUG("Main thread: initializing app\n");
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001967
1968 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1969
1970 // Pull this console process up to being a fully-fledged graphical
1971 // app with a menubar and Dock icon
1972 ProcessSerialNumber psn = { 0, kCurrentProcess };
1973 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1974
Peter Maydell61a2ed42019-02-25 10:24:32 +00001975 [QemuApplication sharedApplication];
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001976
1977 create_initial_menus();
thsc304f7e2008-01-22 23:25:15 +00001978
Peter Maydell55888402019-02-25 10:24:33 +00001979 /*
1980 * Create the menu entries which depend on QEMU state (for consoles
1981 * and removeable devices). These make calls back into QEMU functions,
1982 * which is OK because at this point we know that the second thread
1983 * holds the iothread lock and is synchronously waiting for us to
1984 * finish.
1985 */
1986 add_console_menu_entries();
1987 addRemovableDevicesMenuItems();
1988
thsc304f7e2008-01-22 23:25:15 +00001989 // Create an Application controller
1990 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1991 [NSApp setDelegate:appController];
1992
1993 // Start the main event loop
Peter Maydell55888402019-02-25 10:24:33 +00001994 COCOA_DEBUG("Main thread: entering OSX run loop\n");
bellard5b0753e2005-03-01 21:37:28 +00001995 [NSApp run];
Peter Maydell55888402019-02-25 10:24:33 +00001996 COCOA_DEBUG("Main thread: left OSX run loop, exiting\n");
ths3b46e622007-09-17 08:09:54 +00001997
thsc304f7e2008-01-22 23:25:15 +00001998 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001999 [pool release];
bellardcae41b12006-05-22 21:25:04 +00002000
bellard5b0753e2005-03-01 21:37:28 +00002001 return 0;
2002}
thsc304f7e2008-01-22 23:25:15 +00002003
2004
2005
2006#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01002007static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01002008 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00002009{
2010 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
2011
Peter Maydell55888402019-02-25 10:24:33 +00002012 dispatch_async(dispatch_get_main_queue(), ^{
2013 NSRect rect;
2014 if ([cocoaView cdx] == 1.0) {
2015 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
2016 } else {
2017 rect = NSMakeRect(
2018 x * [cocoaView cdx],
2019 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
2020 w * [cocoaView cdx],
2021 h * [cocoaView cdy]);
2022 }
2023 [cocoaView setNeedsDisplayInRect:rect];
2024 });
thsc304f7e2008-01-22 23:25:15 +00002025}
2026
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01002027static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01002028 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00002029{
Peter Maydell55888402019-02-25 10:24:33 +00002030 pixman_image_t *image = surface->image;
thsc304f7e2008-01-22 23:25:15 +00002031
Peter Maydell6e657e62013-04-22 10:29:46 +00002032 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Peter Maydell55888402019-02-25 10:24:33 +00002033
2034 // The DisplaySurface will be freed as soon as this callback returns.
2035 // We take a reference to the underlying pixman image here so it does
2036 // not disappear from under our feet; the switchSurface method will
2037 // deref the old image when it is done with it.
2038 pixman_image_ref(image);
2039
2040 dispatch_async(dispatch_get_main_queue(), ^{
Peter Maydell8d65dee2022-02-24 10:13:29 +00002041 [cocoaView updateUIInfo];
Peter Maydell55888402019-02-25 10:24:33 +00002042 [cocoaView switchSurface:image];
2043 });
thsc304f7e2008-01-22 23:25:15 +00002044}
2045
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01002046static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00002047{
Peter Maydell6e657e62013-04-22 10:29:46 +00002048 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
2049
thsc304f7e2008-01-22 23:25:15 +00002050 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
John Arbuckle468a8952015-10-13 21:51:18 +01002051 graphic_hw_update(NULL);
thsc304f7e2008-01-22 23:25:15 +00002052
Gerd Hoffmann21bae112013-12-04 14:08:04 +01002053 if (qemu_input_is_absolute()) {
Peter Maydell55888402019-02-25 10:24:33 +00002054 dispatch_async(dispatch_get_main_queue(), ^{
2055 if (![cocoaView isAbsoluteEnabled]) {
2056 if ([cocoaView isMouseGrabbed]) {
2057 [cocoaView ungrabMouse];
2058 }
thsc304f7e2008-01-22 23:25:15 +00002059 }
Peter Maydell55888402019-02-25 10:24:33 +00002060 [cocoaView setAbsoluteEnabled:YES];
2061 });
thsc304f7e2008-01-22 23:25:15 +00002062 }
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09002063
2064 if (cbchangecount != [[NSPasteboard generalPasteboard] changeCount]) {
2065 qemu_clipboard_info_unref(cbinfo);
2066 cbinfo = qemu_clipboard_info_new(&cbpeer, QEMU_CLIPBOARD_SELECTION_CLIPBOARD);
2067 if ([[NSPasteboard generalPasteboard] availableTypeFromArray:@[NSPasteboardTypeString]]) {
2068 cbinfo->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
2069 }
2070 qemu_clipboard_update(cbinfo);
2071 cbchangecount = [[NSPasteboard generalPasteboard] changeCount];
2072 qemu_event_set(&cbevent);
2073 }
2074
Peter Maydell6e657e62013-04-22 10:29:46 +00002075 [pool release];
thsc304f7e2008-01-22 23:25:15 +00002076}
2077
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01002078static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
thsc304f7e2008-01-22 23:25:15 +00002079{
2080 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
2081
Peter Maydell55888402019-02-25 10:24:33 +00002082 /* Tell main thread to go ahead and create the app and enter the run loop */
2083 qemu_sem_post(&display_init_sem);
2084 qemu_sem_wait(&app_started_sem);
2085 COCOA_DEBUG("cocoa_display_init: app start completed\n");
2086
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +09002087 QemuCocoaAppController *controller = (QemuCocoaAppController *)[[NSApplication sharedApplication] delegate];
Programmingkid43227af2015-05-19 09:11:17 +01002088 /* if fullscreen mode is to be used */
Gerd Hoffmann767f9bf2018-02-02 12:10:19 +01002089 if (opts->has_full_screen && opts->full_screen) {
Peter Maydell55888402019-02-25 10:24:33 +00002090 dispatch_async(dispatch_get_main_queue(), ^{
2091 [NSApp activateIgnoringOtherApps: YES];
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +09002092 [controller toggleFullScreen: nil];
2093 });
2094 }
2095 if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) {
2096 dispatch_async(dispatch_get_main_queue(), ^{
2097 [controller setFullGrab: nil];
Peter Maydell55888402019-02-25 10:24:33 +00002098 });
Programmingkid43227af2015-05-19 09:11:17 +01002099 }
Carwyn Ellis69221df2022-01-02 17:41:53 +00002100
Gerd Hoffmann3487da62020-02-06 12:27:50 +01002101 if (opts->has_show_cursor && opts->show_cursor) {
2102 cursor_hide = 0;
2103 }
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +09002104 if (opts->u.cocoa.has_swap_opt_cmd) {
2105 swap_opt_cmd = opts->u.cocoa.swap_opt_cmd;
2106 }
Programmingkid43227af2015-05-19 09:11:17 +01002107
Carwyn Ellis48941a52022-01-02 17:41:52 +00002108 if (opts->u.cocoa.has_left_command_key && !opts->u.cocoa.left_command_key) {
2109 left_command_key_enabled = 0;
2110 }
2111
aliguori9794f742009-03-04 19:25:22 +00002112 // register vga output callbacks
Akihiko Odakicc7859c2021-02-19 17:44:19 +09002113 register_displaychangelistener(&dcl);
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09002114
2115 qemu_event_init(&cbevent, false);
2116 cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];
2117 qemu_clipboard_peer_register(&cbpeer);
thsc304f7e2008-01-22 23:25:15 +00002118}
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01002119
2120static QemuDisplay qemu_display_cocoa = {
2121 .type = DISPLAY_TYPE_COCOA,
2122 .init = cocoa_display_init,
2123};
2124
2125static void register_cocoa(void)
2126{
2127 qemu_display_register(&qemu_display_cocoa);
2128}
2129
2130type_init(register_cocoa);