blob: 25e0db9dd0b65c52d9764cc316b5e4e51894d938 [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"
Richard Hendersoncc37d982023-03-15 17:43:13 +000049#include "qemu/error-report.h"
John Arbuckleaaac7142016-03-23 14:26:18 +000050#include <Carbon/Carbon.h>
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020051#include "hw/core/cpu.h"
bellardda4dbf72005-03-02 22:22:43 +000052
Brendan Shanks5e246002019-01-31 23:12:25 -080053#ifndef MAC_OS_X_VERSION_10_13
54#define MAC_OS_X_VERSION_10_13 101300
55#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010056
David Parsonsf5af8022024-02-24 14:06:20 +000057#ifndef MAC_OS_VERSION_14_0
58#define MAC_OS_VERSION_14_0 140000
59#endif
60
Brendan Shanks5e246002019-01-31 23:12:25 -080061/* 10.14 deprecates NSOnState and NSOffState in favor of
62 * NSControlStateValueOn/Off, which were introduced in 10.13.
63 * Define for older versions
64 */
65#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13
66#define NSControlStateValueOn NSOnState
67#define NSControlStateValueOff NSOffState
68#endif
bellard5b0753e2005-03-01 21:37:28 +000069
thsc304f7e2008-01-22 23:25:15 +000070//#define DEBUG
71
72#ifdef DEBUG
73#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
74#else
75#define COCOA_DEBUG(...) ((void) 0)
76#endif
77
78#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000079
Christian Schoenebeck23bdd0d2022-12-27 17:15:31 +010080#define UC_CTRL_KEY "\xe2\x8c\x83"
81#define UC_ALT_KEY "\xe2\x8c\xa5"
82
thsc304f7e2008-01-22 23:25:15 +000083typedef struct {
84 int width;
85 int height;
thsc304f7e2008-01-22 23:25:15 +000086} QEMUScreen;
87
Akihiko Odakicc7859c2021-02-19 17:44:19 +090088static void cocoa_update(DisplayChangeListener *dcl,
89 int x, int y, int w, int h);
90
91static void cocoa_switch(DisplayChangeListener *dcl,
92 DisplaySurface *surface);
93
94static void cocoa_refresh(DisplayChangeListener *dcl);
95
Akihiko Odakicc7859c2021-02-19 17:44:19 +090096static const DisplayChangeListenerOps dcl_ops = {
97 .dpy_name = "cocoa",
98 .dpy_gfx_update = cocoa_update,
99 .dpy_gfx_switch = cocoa_switch,
100 .dpy_refresh = cocoa_refresh,
101};
102static DisplayChangeListener dcl = {
103 .ops = &dcl_ops,
104};
Akihiko Odakica3de7b2024-03-19 12:08:41 +0900105static QKbdState *kbd;
Gerd Hoffmann3487da62020-02-06 12:27:50 +0100106static int cursor_hide = 1;
Carwyn Ellis48941a52022-01-02 17:41:52 +0000107static int left_command_key_enabled = 1;
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900108static bool swap_opt_cmd;
bellard5b0753e2005-03-01 21:37:28 +0000109
Carwyn Ellise28a9092023-11-10 16:17:29 +0000110static CGInterpolationQuality zoom_interpolation = kCGInterpolationNone;
Akihiko Odakicb823402021-02-25 17:42:02 +0900111static NSTextField *pauseLabel;
bellard5b0753e2005-03-01 21:37:28 +0000112
Hikaru Nishidadff742a2019-10-15 10:07:34 +0900113static bool allow_events;
Peter Maydell55888402019-02-25 10:24:33 +0000114
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +0900115static NSInteger cbchangecount = -1;
116static QemuClipboardInfo *cbinfo;
117static QemuEvent cbevent;
118
Stefan Hajnoczia4a411f2024-01-02 10:35:28 -0500119// Utility functions to run specified code block with the BQL held
Peter Maydell31819e92019-02-25 10:24:27 +0000120typedef void (^CodeBlock)(void);
Peter Maydell60105d72019-02-25 10:24:31 +0000121typedef bool (^BoolCodeBlock)(void);
Peter Maydell31819e92019-02-25 10:24:27 +0000122
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500123static void with_bql(CodeBlock block)
Peter Maydell31819e92019-02-25 10:24:27 +0000124{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500125 bool locked = bql_locked();
Peter Maydell31819e92019-02-25 10:24:27 +0000126 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500127 bql_lock();
Peter Maydell31819e92019-02-25 10:24:27 +0000128 }
129 block();
130 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500131 bql_unlock();
Peter Maydell31819e92019-02-25 10:24:27 +0000132 }
133}
134
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500135static bool bool_with_bql(BoolCodeBlock block)
Peter Maydell60105d72019-02-25 10:24:31 +0000136{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500137 bool locked = bql_locked();
Peter Maydell60105d72019-02-25 10:24:31 +0000138 bool val;
139
140 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500141 bql_lock();
Peter Maydell60105d72019-02-25 10:24:31 +0000142 }
143 val = block();
144 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500145 bql_unlock();
Peter Maydell60105d72019-02-25 10:24:31 +0000146 }
147 return val;
148}
149
John Arbuckleaaac7142016-03-23 14:26:18 +0000150// Mac to QKeyCode conversion
Akihiko Odakicb823402021-02-25 17:42:02 +0900151static const int mac_to_qkeycode_map[] = {
John Arbuckleaaac7142016-03-23 14:26:18 +0000152 [kVK_ANSI_A] = Q_KEY_CODE_A,
153 [kVK_ANSI_B] = Q_KEY_CODE_B,
154 [kVK_ANSI_C] = Q_KEY_CODE_C,
155 [kVK_ANSI_D] = Q_KEY_CODE_D,
156 [kVK_ANSI_E] = Q_KEY_CODE_E,
157 [kVK_ANSI_F] = Q_KEY_CODE_F,
158 [kVK_ANSI_G] = Q_KEY_CODE_G,
159 [kVK_ANSI_H] = Q_KEY_CODE_H,
160 [kVK_ANSI_I] = Q_KEY_CODE_I,
161 [kVK_ANSI_J] = Q_KEY_CODE_J,
162 [kVK_ANSI_K] = Q_KEY_CODE_K,
163 [kVK_ANSI_L] = Q_KEY_CODE_L,
164 [kVK_ANSI_M] = Q_KEY_CODE_M,
165 [kVK_ANSI_N] = Q_KEY_CODE_N,
166 [kVK_ANSI_O] = Q_KEY_CODE_O,
167 [kVK_ANSI_P] = Q_KEY_CODE_P,
168 [kVK_ANSI_Q] = Q_KEY_CODE_Q,
169 [kVK_ANSI_R] = Q_KEY_CODE_R,
170 [kVK_ANSI_S] = Q_KEY_CODE_S,
171 [kVK_ANSI_T] = Q_KEY_CODE_T,
172 [kVK_ANSI_U] = Q_KEY_CODE_U,
173 [kVK_ANSI_V] = Q_KEY_CODE_V,
174 [kVK_ANSI_W] = Q_KEY_CODE_W,
175 [kVK_ANSI_X] = Q_KEY_CODE_X,
176 [kVK_ANSI_Y] = Q_KEY_CODE_Y,
177 [kVK_ANSI_Z] = Q_KEY_CODE_Z,
ths3b46e622007-09-17 08:09:54 +0000178
John Arbuckleaaac7142016-03-23 14:26:18 +0000179 [kVK_ANSI_0] = Q_KEY_CODE_0,
180 [kVK_ANSI_1] = Q_KEY_CODE_1,
181 [kVK_ANSI_2] = Q_KEY_CODE_2,
182 [kVK_ANSI_3] = Q_KEY_CODE_3,
183 [kVK_ANSI_4] = Q_KEY_CODE_4,
184 [kVK_ANSI_5] = Q_KEY_CODE_5,
185 [kVK_ANSI_6] = Q_KEY_CODE_6,
186 [kVK_ANSI_7] = Q_KEY_CODE_7,
187 [kVK_ANSI_8] = Q_KEY_CODE_8,
188 [kVK_ANSI_9] = Q_KEY_CODE_9,
189
190 [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
191 [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
192 [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
193 [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
194 [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
195 [kVK_Tab] = Q_KEY_CODE_TAB,
196 [kVK_Return] = Q_KEY_CODE_RET,
197 [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
198 [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
199 [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
200 [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
201 [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
202 [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
203 [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
204 [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
John Arbuckleaaac7142016-03-23 14:26:18 +0000205 [kVK_Space] = Q_KEY_CODE_SPC,
206
207 [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
208 [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
209 [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
210 [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
211 [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
212 [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
213 [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
214 [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
215 [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
216 [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
217 [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
218 [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
219 [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
220 [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
221 [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
222 [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
223 [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
224 [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
225
226 [kVK_UpArrow] = Q_KEY_CODE_UP,
227 [kVK_DownArrow] = Q_KEY_CODE_DOWN,
228 [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
229 [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
230
231 [kVK_Help] = Q_KEY_CODE_INSERT,
232 [kVK_Home] = Q_KEY_CODE_HOME,
233 [kVK_PageUp] = Q_KEY_CODE_PGUP,
234 [kVK_PageDown] = Q_KEY_CODE_PGDN,
235 [kVK_End] = Q_KEY_CODE_END,
236 [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
237
238 [kVK_Escape] = Q_KEY_CODE_ESC,
239
240 /* The Power key can't be used directly because the operating system uses
241 * it. This key can be emulated by using it in place of another key such as
242 * F1. Don't forget to disable the real key binding.
243 */
244 /* [kVK_F1] = Q_KEY_CODE_POWER, */
245
246 [kVK_F1] = Q_KEY_CODE_F1,
247 [kVK_F2] = Q_KEY_CODE_F2,
248 [kVK_F3] = Q_KEY_CODE_F3,
249 [kVK_F4] = Q_KEY_CODE_F4,
250 [kVK_F5] = Q_KEY_CODE_F5,
251 [kVK_F6] = Q_KEY_CODE_F6,
252 [kVK_F7] = Q_KEY_CODE_F7,
253 [kVK_F8] = Q_KEY_CODE_F8,
254 [kVK_F9] = Q_KEY_CODE_F9,
255 [kVK_F10] = Q_KEY_CODE_F10,
256 [kVK_F11] = Q_KEY_CODE_F11,
257 [kVK_F12] = Q_KEY_CODE_F12,
258 [kVK_F13] = Q_KEY_CODE_PRINT,
259 [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
260 [kVK_F15] = Q_KEY_CODE_PAUSE,
261
Akihiko Odaki708b7252021-02-12 09:04:04 +0900262 // JIS keyboards only
263 [kVK_JIS_Yen] = Q_KEY_CODE_YEN,
264 [kVK_JIS_Underscore] = Q_KEY_CODE_RO,
265 [kVK_JIS_KeypadComma] = Q_KEY_CODE_KP_COMMA,
266 [kVK_JIS_Eisu] = Q_KEY_CODE_MUHENKAN,
267 [kVK_JIS_Kana] = Q_KEY_CODE_HENKAN,
268
John Arbuckleaaac7142016-03-23 14:26:18 +0000269 /*
270 * The eject and volume keys can't be used here because they are handled at
271 * a lower level than what an Application can see.
272 */
bellard5b0753e2005-03-01 21:37:28 +0000273};
274
Andreas Färber77047bb2009-12-13 00:55:53 +0100275static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000276{
John Arbuckleaaac7142016-03-23 14:26:18 +0000277 if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
Akihiko Odaki43137392021-02-23 22:11:06 +0900278 error_report("(cocoa) warning unknown keycode 0x%x", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000279 return 0;
280 }
John Arbuckleaaac7142016-03-23 14:26:18 +0000281 return mac_to_qkeycode_map[keycode];
bellard5b0753e2005-03-01 21:37:28 +0000282}
283
John Arbuckle693a3e02015-06-19 10:53:27 +0100284/* Displays an alert dialog box with the specified message */
285static void QEMU_Alert(NSString *message)
286{
287 NSAlert *alert;
288 alert = [NSAlert new];
289 [alert setMessageText: message];
290 [alert runModal];
291}
thsc304f7e2008-01-22 23:25:15 +0000292
John Arbuckle693a3e02015-06-19 10:53:27 +0100293/* Handles any errors that happen with a device transaction */
294static void handleAnyDeviceErrors(Error * err)
295{
296 if (err) {
297 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
298 encoding: NSASCIIStringEncoding]);
299 error_free(err);
300 }
301}
thsc304f7e2008-01-22 23:25:15 +0000302
bellard5b0753e2005-03-01 21:37:28 +0000303/*
304 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000305 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000306 ------------------------------------------------------
307*/
thsc304f7e2008-01-22 23:25:15 +0000308@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000309{
thsc304f7e2008-01-22 23:25:15 +0000310 QEMUScreen screen;
Peter Maydell55888402019-02-25 10:24:33 +0000311 pixman_image_t *pixman_image;
Peter Maydell49b9bd42013-12-08 22:59:03 +0000312 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000313 BOOL isAbsoluteEnabled;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900314 CFMachPortRef eventsTap;
thsc304f7e2008-01-22 23:25:15 +0000315}
Peter Maydell72a3e312019-02-25 10:24:28 +0000316- (void) switchSurface:(pixman_image_t *)image;
thsc304f7e2008-01-22 23:25:15 +0000317- (void) grabMouse;
318- (void) ungrabMouse;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900319- (void) setFullGrab:(id)sender;
John Arbuckle9c3a4182017-11-07 10:14:14 +0000320- (void) handleMonitorInput:(NSEvent *)event;
Peter Maydell60105d72019-02-25 10:24:31 +0000321- (bool) handleEvent:(NSEvent *)event;
322- (bool) handleEventLocked:(NSEvent *)event;
thsc304f7e2008-01-22 23:25:15 +0000323- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100324/* The state surrounding mouse grabbing is potentially confusing.
325 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
326 * pointing device an absolute-position one?"], but is only updated on
327 * next refresh.
328 * isMouseGrabbed tracks whether GUI events are directed to the guest;
329 * it controls whether special keys like Cmd get sent to the guest,
330 * and whether we capture the mouse when in non-absolute mode.
Peter Maydellf61c3872014-06-23 10:35:24 +0100331 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000332- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000333- (BOOL) isAbsoluteEnabled;
thsc304f7e2008-01-22 23:25:15 +0000334- (QEMUScreen) gscreen;
John Arbuckle3b178b72015-09-25 23:14:00 +0100335- (void) raiseAllKeys;
thsc304f7e2008-01-22 23:25:15 +0000336@end
ths3b46e622007-09-17 08:09:54 +0000337
Andreas Färber7fee1992011-06-09 20:53:32 +0200338QemuCocoaView *cocoaView;
339
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900340static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef cgEvent, void *userInfo)
341{
Philippe Mathieu-Daudé21eb7522023-10-04 14:00:13 +0200342 QemuCocoaView *view = userInfo;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900343 NSEvent *event = [NSEvent eventWithCGEvent:cgEvent];
Philippe Mathieu-Daudé21eb7522023-10-04 14:00:13 +0200344 if ([view isMouseGrabbed] && [view handleEvent:event]) {
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900345 COCOA_DEBUG("Global events tap: qemu handled the event, capturing!\n");
346 return NULL;
347 }
348 COCOA_DEBUG("Global events tap: qemu did not handle the event, letting it through...\n");
349
350 return cgEvent;
351}
352
thsc304f7e2008-01-22 23:25:15 +0000353@implementation QemuCocoaView
354- (id)initWithFrame:(NSRect)frameRect
355{
356 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000357
thsc304f7e2008-01-22 23:25:15 +0000358 self = [super initWithFrame:frameRect];
359 if (self) {
pbrook95219892006-04-09 01:06:34 +0000360
Akihiko Odakiccebb9a2024-03-23 15:20:03 +0900361 NSTrackingAreaOptions options = NSTrackingActiveInKeyWindow |
362 NSTrackingMouseEnteredAndExited |
363 NSTrackingMouseMoved |
364 NSTrackingInVisibleRect;
365
366 NSTrackingArea *trackingArea =
367 [[NSTrackingArea alloc] initWithRect:CGRectZero
368 options:options
369 owner:self
370 userInfo:nil];
371
372 [self addTrackingArea:trackingArea];
373 [trackingArea release];
thsc304f7e2008-01-22 23:25:15 +0000374 screen.width = frameRect.size.width;
375 screen.height = frameRect.size.height;
David Parsonsf5af8022024-02-24 14:06:20 +0000376#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_14_0
377 [self setClipsToBounds:YES];
378#endif
bellard7c206a72005-12-18 19:18:45 +0000379
thsc304f7e2008-01-22 23:25:15 +0000380 }
381 return self;
382}
bellard7c206a72005-12-18 19:18:45 +0000383
thsc304f7e2008-01-22 23:25:15 +0000384- (void) dealloc
385{
386 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000387
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900388 if (pixman_image) {
Peter Maydell55888402019-02-25 10:24:33 +0000389 pixman_image_unref(pixman_image);
390 }
ths3b46e622007-09-17 08:09:54 +0000391
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900392 if (eventsTap) {
393 CFRelease(eventsTap);
394 }
395
thsc304f7e2008-01-22 23:25:15 +0000396 [super dealloc];
397}
bellard5cbfcd02006-06-14 15:53:24 +0000398
Andreas Färberd50f71d2009-12-13 02:03:33 +0100399- (BOOL) isOpaque
400{
401 return YES;
402}
403
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900404- (void) viewDidMoveToWindow
405{
406 [self resizeWindow];
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900407}
408
Akihiko Odakica3de7b2024-03-19 12:08:41 +0900409- (void) selectConsoleLocked:(unsigned int)index
410{
411 QemuConsole *con = qemu_console_lookup_by_index(index);
412 if (!con) {
413 return;
414 }
415
416 unregister_displaychangelistener(&dcl);
417 qkbd_state_switch_console(kbd, con);
418 dcl.con = con;
419 register_displaychangelistener(&dcl);
420 [self updateUIInfo];
421}
422
Peter Maydell13aefd32014-06-23 10:35:25 +0100423- (void) hideCursor
424{
425 if (!cursor_hide) {
426 return;
427 }
428 [NSCursor hide];
429}
430
431- (void) unhideCursor
432{
433 if (!cursor_hide) {
434 return;
435 }
436 [NSCursor unhide];
437}
438
thsc304f7e2008-01-22 23:25:15 +0000439- (void) drawRect:(NSRect) rect
440{
441 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000442
thsc304f7e2008-01-22 23:25:15 +0000443 // get CoreGraphic context
Brendan Shanks5e246002019-01-31 23:12:25 -0800444 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] CGContext];
Brendan Shanks5e246002019-01-31 23:12:25 -0800445
Carwyn Ellise28a9092023-11-10 16:17:29 +0000446 CGContextSetInterpolationQuality (viewContextRef, zoom_interpolation);
thsc304f7e2008-01-22 23:25:15 +0000447 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000448
thsc304f7e2008-01-22 23:25:15 +0000449 // draw screen bitmap directly to Core Graphics context
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900450 if (!pixman_image) {
Peter Maydell7d270b12013-12-24 02:51:47 +0000451 // Draw request before any guest device has set up a framebuffer:
452 // just draw an opaque black rectangle
453 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
454 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
455 } else {
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900456 int w = pixman_image_get_width(pixman_image);
457 int h = pixman_image_get_height(pixman_image);
458 int bitsPerPixel = PIXMAN_FORMAT_BPP(pixman_image_get_format(pixman_image));
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900459 int stride = pixman_image_get_stride(pixman_image);
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900460 CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(
461 NULL,
462 pixman_image_get_data(pixman_image),
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900463 stride * h,
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900464 NULL
465 );
thsc304f7e2008-01-22 23:25:15 +0000466 CGImageRef imageRef = CGImageCreate(
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900467 w, //width
468 h, //height
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900469 DIV_ROUND_UP(bitsPerPixel, 8) * 2, //bitsPerComponent
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900470 bitsPerPixel, //bitsPerPixel
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900471 stride, //bytesPerRow
Akihiko Odakiae57d352021-03-05 21:13:04 +0900472 CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace
473 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, //bitmapInfo
thsc304f7e2008-01-22 23:25:15 +0000474 dataProviderRef, //provider
475 NULL, //decode
476 0, //interpolate
477 kCGRenderingIntentDefault //intent
478 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100479 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
480 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100481 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100482 int i;
483 CGImageRef clipImageRef;
484 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000485
Peter Maydellb63901d2015-05-19 09:11:17 +0100486 [self getRectsBeingDrawn:&rectList count:&rectCount];
487 for (i = 0; i < rectCount; i++) {
Akihiko Odakifcb03de2024-02-24 21:43:35 +0900488 clipRect = rectList[i];
489 clipRect.origin.y = (float)h - (clipRect.origin.y + clipRect.size.height);
Peter Maydellb63901d2015-05-19 09:11:17 +0100490 clipImageRef = CGImageCreateWithImageInRect(
491 imageRef,
492 clipRect
493 );
494 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
495 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000496 }
thsc304f7e2008-01-22 23:25:15 +0000497 CGImageRelease (imageRef);
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900498 CGDataProviderRelease(dataProviderRef);
bellard5b0753e2005-03-01 21:37:28 +0000499 }
500}
501
Akihiko Odakid2ee0422024-03-23 15:20:01 +0900502- (NSSize)fixAspectRatio:(NSSize)max
503{
504 NSSize scaled;
505 NSSize fixed;
506
507 scaled.width = screen.width * max.height;
508 scaled.height = screen.height * max.width;
509
510 /*
511 * Here screen is our guest's output size, and max is the size of the
512 * largest possible area of the screen we can display on.
513 * We want to scale up (screen.width x screen.height) by either:
514 * 1) max.height / screen.height
515 * 2) max.width / screen.width
516 * With the first scale factor the scale will result in an output height of
517 * max.height (i.e. we will fill the whole height of the available screen
518 * space and have black bars left and right) and with the second scale
519 * factor the scaling will result in an output width of max.width (i.e. we
520 * fill the whole width of the available screen space and have black bars
521 * top and bottom). We need to pick whichever keeps the whole of the guest
522 * output on the screen, which is to say the smaller of the two scale
523 * factors.
524 * To avoid doing more division than strictly necessary, instead of directly
525 * comparing scale factors 1 and 2 we instead calculate and compare those
526 * two scale factors multiplied by (screen.height * screen.width).
527 */
528 if (scaled.width < scaled.height) {
529 fixed.width = scaled.width / screen.height;
530 fixed.height = max.height;
531 } else {
532 fixed.width = max.width;
533 fixed.height = scaled.height / screen.width;
534 }
535
536 return fixed;
537}
538
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900539- (NSSize) screenSafeAreaSize
bellard5b0753e2005-03-01 21:37:28 +0000540{
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900541 NSSize size = [[[self window] screen] frame].size;
542 NSEdgeInsets insets = [[[self window] screen] safeAreaInsets];
543 size.width -= insets.left + insets.right;
544 size.height -= insets.top + insets.bottom;
545 return size;
546}
ths3b46e622007-09-17 08:09:54 +0000547
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900548- (void) resizeWindow
549{
550 [[self window] setContentAspectRatio:NSMakeSize(screen.width, screen.height)];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100551
Akihiko Odaki55766632024-02-24 21:43:41 +0900552 if (!([[self window] styleMask] & NSWindowStyleMaskResizable)) {
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900553 [[self window] setContentSize:NSMakeSize(screen.width, screen.height)];
554 [[self window] center];
555 } else if ([[self window] styleMask] & NSWindowStyleMaskFullScreen) {
Akihiko Odakid2ee0422024-03-23 15:20:01 +0900556 [[self window] setContentSize:[self fixAspectRatio:[self screenSafeAreaSize]]];
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900557 [[self window] center];
Akihiko Odakid2ee0422024-03-23 15:20:01 +0900558 } else {
559 [[self window] setContentSize:[self fixAspectRatio:[self frame].size]];
thsc304f7e2008-01-22 23:25:15 +0000560 }
bellard5b0753e2005-03-01 21:37:28 +0000561}
562
Akihiko Odakifcb03de2024-02-24 21:43:35 +0900563- (void) updateBounds
564{
565 [self setBoundsSize:NSMakeSize(screen.width, screen.height)];
566}
567
Peter Maydell8d65dee2022-02-24 10:13:29 +0000568- (void) updateUIInfoLocked
Akihiko Odaki15280e82021-06-16 23:19:10 +0900569{
Stefan Hajnoczia4a411f2024-01-02 10:35:28 -0500570 /* Must be called with the BQL, i.e. via updateUIInfo */
Akihiko Odaki15280e82021-06-16 23:19:10 +0900571 NSSize frameSize;
572 QemuUIInfo info;
573
574 if (!qemu_console_is_graphic(dcl.con)) {
575 return;
576 }
577
578 if ([self window]) {
579 NSDictionary *description = [[[self window] screen] deviceDescription];
580 CGDirectDisplayID display = [[description objectForKey:@"NSScreenNumber"] unsignedIntValue];
581 NSSize screenSize = [[[self window] screen] frame].size;
582 CGSize screenPhysicalSize = CGDisplayScreenSize(display);
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900583 bool isFullscreen = ([[self window] styleMask] & NSWindowStyleMaskFullScreen) != 0;
Akihiko Odaki52eaefd2022-07-02 23:25:19 +0900584 CVDisplayLinkRef displayLink;
Akihiko Odaki15280e82021-06-16 23:19:10 +0900585
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900586 frameSize = isFullscreen ? [self screenSafeAreaSize] : [self frame].size;
Akihiko Odaki52eaefd2022-07-02 23:25:19 +0900587
588 if (!CVDisplayLinkCreateWithCGDisplay(display, &displayLink)) {
589 CVTime period = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLink);
590 CVDisplayLinkRelease(displayLink);
591 if (!(period.flags & kCVTimeIsIndefinite)) {
592 update_displaychangelistener(&dcl,
593 1000 * period.timeValue / period.timeScale);
594 info.refresh_rate = (int64_t)1000 * period.timeScale / period.timeValue;
595 }
596 }
597
Akihiko Odaki15280e82021-06-16 23:19:10 +0900598 info.width_mm = frameSize.width / screenSize.width * screenPhysicalSize.width;
599 info.height_mm = frameSize.height / screenSize.height * screenPhysicalSize.height;
600 } else {
601 frameSize = [self frame].size;
602 info.width_mm = 0;
603 info.height_mm = 0;
604 }
605
606 info.xoff = 0;
607 info.yoff = 0;
608 info.width = frameSize.width;
609 info.height = frameSize.height;
610
Marc-André Lureauca19ef52021-04-13 20:39:11 +0400611 dpy_set_ui_info(dcl.con, &info, TRUE);
Akihiko Odaki15280e82021-06-16 23:19:10 +0900612}
613
Peter Maydell8d65dee2022-02-24 10:13:29 +0000614- (void) updateUIInfo
615{
616 if (!allow_events) {
617 /*
618 * Don't try to tell QEMU about UI information in the application
619 * startup phase -- we haven't yet registered dcl with the QEMU UI
Akihiko Odakibab6a302022-08-19 22:27:54 +0900620 * layer.
Peter Maydell8d65dee2022-02-24 10:13:29 +0000621 * When cocoa_display_init() does register the dcl, the UI layer
622 * will call cocoa_switch(), which will call updateUIInfo, so
623 * we don't lose any information here.
624 */
625 return;
626 }
627
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500628 with_bql(^{
Peter Maydell8d65dee2022-02-24 10:13:29 +0000629 [self updateUIInfoLocked];
630 });
631}
632
Peter Maydell72a3e312019-02-25 10:24:28 +0000633- (void) switchSurface:(pixman_image_t *)image
thsc304f7e2008-01-22 23:25:15 +0000634{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100635 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000636
Peter Maydell72a3e312019-02-25 10:24:28 +0000637 int w = pixman_image_get_width(image);
638 int h = pixman_image_get_height(image);
Peter Maydelld3345a02013-12-24 02:51:46 +0000639
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900640 if (w != screen.width || h != screen.height) {
Peter Maydelld3345a02013-12-24 02:51:46 +0000641 // Resize before we trigger the redraw, or we'll redraw at the wrong size
642 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
643 screen.width = w;
644 screen.height = h;
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900645 [self resizeWindow];
Akihiko Odakifcb03de2024-02-24 21:43:35 +0900646 [self updateBounds];
Peter Maydelld3345a02013-12-24 02:51:46 +0000647 }
Peter Maydell8510d912013-03-18 20:28:21 +0000648
thsc304f7e2008-01-22 23:25:15 +0000649 // update screenBuffer
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900650 if (pixman_image) {
Peter Maydell55888402019-02-25 10:24:33 +0000651 pixman_image_unref(pixman_image);
652 }
thsc304f7e2008-01-22 23:25:15 +0000653
Peter Maydell55888402019-02-25 10:24:33 +0000654 pixman_image = image;
thsc304f7e2008-01-22 23:25:15 +0000655}
656
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900657- (void) setFullGrab:(id)sender
658{
659 COCOA_DEBUG("QemuCocoaView: setFullGrab\n");
660
661 CGEventMask mask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged);
662 eventsTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
663 mask, handleTapEvent, self);
664 if (!eventsTap) {
665 warn_report("Could not create event tap, system key combos will not be captured.\n");
666 return;
667 } else {
668 COCOA_DEBUG("Global events tap created! Will capture system key combos.\n");
669 }
670
671 CFRunLoopRef runLoop = CFRunLoopGetCurrent();
672 if (!runLoop) {
673 warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
674 return;
675 }
676
677 CFRunLoopSourceRef tapEventsSrc = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventsTap, 0);
678 if (!tapEventsSrc ) {
679 warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
680 return;
681 }
682
683 CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode);
684 CFRelease(tapEventsSrc);
685}
686
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900687- (void) toggleKey: (int)keycode {
688 qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode));
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700689}
690
John Arbuckle9c3a4182017-11-07 10:14:14 +0000691// Does the work of sending input to the monitor
692- (void) handleMonitorInput:(NSEvent *)event
693{
694 int keysym = 0;
695 int control_key = 0;
696
697 // if the control key is down
698 if ([event modifierFlags] & NSEventModifierFlagControl) {
699 control_key = 1;
700 }
701
702 /* translates Macintosh keycodes to QEMU's keysym */
703
Philippe Mathieu-Daudé94592622022-02-15 16:21:14 +0100704 static const int without_control_translation[] = {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000705 [0 ... 0xff] = 0, // invalid key
706
707 [kVK_UpArrow] = QEMU_KEY_UP,
708 [kVK_DownArrow] = QEMU_KEY_DOWN,
709 [kVK_RightArrow] = QEMU_KEY_RIGHT,
710 [kVK_LeftArrow] = QEMU_KEY_LEFT,
711 [kVK_Home] = QEMU_KEY_HOME,
712 [kVK_End] = QEMU_KEY_END,
713 [kVK_PageUp] = QEMU_KEY_PAGEUP,
714 [kVK_PageDown] = QEMU_KEY_PAGEDOWN,
715 [kVK_ForwardDelete] = QEMU_KEY_DELETE,
716 [kVK_Delete] = QEMU_KEY_BACKSPACE,
717 };
718
Philippe Mathieu-Daudé94592622022-02-15 16:21:14 +0100719 static const int with_control_translation[] = {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000720 [0 ... 0xff] = 0, // invalid key
721
722 [kVK_UpArrow] = QEMU_KEY_CTRL_UP,
723 [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN,
724 [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT,
725 [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT,
726 [kVK_Home] = QEMU_KEY_CTRL_HOME,
727 [kVK_End] = QEMU_KEY_CTRL_END,
728 [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP,
729 [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN,
730 };
731
732 if (control_key != 0) { /* If the control key is being used */
733 if ([event keyCode] < ARRAY_SIZE(with_control_translation)) {
734 keysym = with_control_translation[[event keyCode]];
735 }
736 } else {
737 if ([event keyCode] < ARRAY_SIZE(without_control_translation)) {
738 keysym = without_control_translation[[event keyCode]];
739 }
740 }
741
742 // if not a key that needs translating
743 if (keysym == 0) {
744 NSString *ks = [event characters];
745 if ([ks length] > 0) {
746 keysym = [ks characterAtIndex:0];
747 }
748 }
749
750 if (keysym) {
Akihiko Odakica3de7b2024-03-19 12:08:41 +0900751 QemuTextConsole *con = QEMU_TEXT_CONSOLE(dcl.con);
752 qemu_text_console_put_keysym(con, keysym);
John Arbuckle9c3a4182017-11-07 10:14:14 +0000753 }
754}
755
Peter Maydell60105d72019-02-25 10:24:31 +0000756- (bool) handleEvent:(NSEvent *)event
thsc304f7e2008-01-22 23:25:15 +0000757{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500758 return bool_with_bql(^{
Peter Maydell60105d72019-02-25 10:24:31 +0000759 return [self handleEventLocked:event];
Peter Maydell31819e92019-02-25 10:24:27 +0000760 });
761}
thsc304f7e2008-01-22 23:25:15 +0000762
Peter Maydell60105d72019-02-25 10:24:31 +0000763- (bool) handleEventLocked:(NSEvent *)event
Peter Maydell31819e92019-02-25 10:24:27 +0000764{
Peter Maydell60105d72019-02-25 10:24:31 +0000765 /* Return true if we handled the event, false if it should be given to OSX */
Peter Maydell31819e92019-02-25 10:24:27 +0000766 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900767 InputButton button;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700768 int keycode = 0;
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900769 NSUInteger modifiers = [event modifierFlags];
770
Akihiko Odakiad7f2f82021-03-12 22:32:12 +0900771 /*
772 * Check -[NSEvent modifierFlags] here.
773 *
774 * There is a NSEventType for an event notifying the change of
775 * -[NSEvent modifierFlags], NSEventTypeFlagsChanged but these operations
776 * are performed for any events because a modifier state may change while
777 * the application is inactive (i.e. no events fire) and we don't want to
778 * wait for another modifier state change to detect such a change.
779 *
780 * NSEventModifierFlagCapsLock requires a special treatment. The other flags
781 * are handled in similar manners.
782 *
783 * NSEventModifierFlagCapsLock
784 * ---------------------------
785 *
786 * If CapsLock state is changed, "up" and "down" events will be fired in
787 * sequence, effectively updates CapsLock state on the guest.
788 *
789 * The other flags
790 * ---------------
791 *
792 * If a flag is not set, fire "up" events for all keys which correspond to
793 * the flag. Note that "down" events are not fired here because the flags
794 * checked here do not tell what exact keys are down.
795 *
796 * If one of the keys corresponding to a flag is down, we rely on
797 * -[NSEvent keyCode] of an event whose -[NSEvent type] is
798 * NSEventTypeFlagsChanged to know the exact key which is down, which has
799 * the following two downsides:
800 * - It does not work when the application is inactive as described above.
801 * - It malfactions *after* the modifier state is changed while the
802 * application is inactive. It is because -[NSEvent keyCode] does not tell
803 * if the key is up or down, and requires to infer the current state from
804 * the previous state. It is still possible to fix such a malfanction by
805 * completely leaving your hands from the keyboard, which hopefully makes
806 * this implementation usable enough.
807 */
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900808 if (!!(modifiers & NSEventModifierFlagCapsLock) !=
809 qkbd_state_modifier_get(kbd, QKBD_MOD_CAPSLOCK)) {
810 qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, true);
811 qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, false);
812 }
813
814 if (!(modifiers & NSEventModifierFlagShift)) {
815 qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT, false);
816 qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT_R, false);
817 }
818 if (!(modifiers & NSEventModifierFlagControl)) {
819 qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL, false);
820 qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL_R, false);
821 }
822 if (!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900823 if (swap_opt_cmd) {
824 qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
825 qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
826 } else {
827 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
828 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
829 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900830 }
831 if (!(modifiers & NSEventModifierFlagCommand)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900832 if (swap_opt_cmd) {
833 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
834 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
835 } else {
836 qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
837 qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
838 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900839 }
thsc304f7e2008-01-22 23:25:15 +0000840
841 switch ([event type]) {
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700842 case NSEventTypeFlagsChanged:
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900843 switch ([event keyCode]) {
844 case kVK_Shift:
845 if (!!(modifiers & NSEventModifierFlagShift)) {
846 [self toggleKey:Q_KEY_CODE_SHIFT];
847 }
848 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700849
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900850 case kVK_RightShift:
851 if (!!(modifiers & NSEventModifierFlagShift)) {
852 [self toggleKey:Q_KEY_CODE_SHIFT_R];
853 }
854 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700855
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900856 case kVK_Control:
857 if (!!(modifiers & NSEventModifierFlagControl)) {
858 [self toggleKey:Q_KEY_CODE_CTRL];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700859 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900860 break;
861
862 case kVK_RightControl:
863 if (!!(modifiers & NSEventModifierFlagControl)) {
864 [self toggleKey:Q_KEY_CODE_CTRL_R];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700865 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900866 break;
867
868 case kVK_Option:
869 if (!!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900870 if (swap_opt_cmd) {
871 [self toggleKey:Q_KEY_CODE_META_L];
872 } else {
873 [self toggleKey:Q_KEY_CODE_ALT];
874 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700875 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900876 break;
877
878 case kVK_RightOption:
879 if (!!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900880 if (swap_opt_cmd) {
881 [self toggleKey:Q_KEY_CODE_META_R];
882 } else {
883 [self toggleKey:Q_KEY_CODE_ALT_R];
884 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700885 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900886 break;
887
888 /* Don't pass command key changes to guest unless mouse is grabbed */
889 case kVK_Command:
890 if (isMouseGrabbed &&
Akihiko Odakid6b6dea2022-03-18 00:29:49 +0900891 !!(modifiers & NSEventModifierFlagCommand) &&
892 left_command_key_enabled) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900893 if (swap_opt_cmd) {
894 [self toggleKey:Q_KEY_CODE_ALT];
895 } else {
896 [self toggleKey:Q_KEY_CODE_META_L];
897 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700898 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900899 break;
900
901 case kVK_RightCommand:
902 if (isMouseGrabbed &&
903 !!(modifiers & NSEventModifierFlagCommand)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900904 if (swap_opt_cmd) {
905 [self toggleKey:Q_KEY_CODE_ALT_R];
906 } else {
907 [self toggleKey:Q_KEY_CODE_META_R];
908 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900909 }
910 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700911 }
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900912 return true;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700913 case NSEventTypeKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000914 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000915
Peter Maydell88959192013-12-08 22:59:02 +0000916 // forward command key combos to the host UI unless the mouse is grabbed
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700917 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell60105d72019-02-25 10:24:31 +0000918 return false;
thsc304f7e2008-01-22 23:25:15 +0000919 }
920
921 // default
thsc304f7e2008-01-22 23:25:15 +0000922
John Arbuckle5929e362017-11-07 10:14:14 +0000923 // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700924 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
John Arbuckle5929e362017-11-07 10:14:14 +0000925 NSString *keychar = [event charactersIgnoringModifiers];
926 if ([keychar length] == 1) {
927 char key = [keychar characterAtIndex:0];
928 switch (key) {
thsc304f7e2008-01-22 23:25:15 +0000929
John Arbuckle5929e362017-11-07 10:14:14 +0000930 // enable graphic console
931 case '1' ... '9':
Akihiko Odakica3de7b2024-03-19 12:08:41 +0900932 [self selectConsoleLocked:key - '0' - 1]; /* ascii math */
Peter Maydell60105d72019-02-25 10:24:31 +0000933 return true;
John Arbuckle5929e362017-11-07 10:14:14 +0000934
935 // release the mouse grab
936 case 'g':
937 [self ungrabMouse];
Peter Maydell60105d72019-02-25 10:24:31 +0000938 return true;
John Arbuckle5929e362017-11-07 10:14:14 +0000939 }
thsc304f7e2008-01-22 23:25:15 +0000940 }
Peter Maydellef2088f2017-11-07 10:14:14 +0000941 }
thsc304f7e2008-01-22 23:25:15 +0000942
Akihiko Odakica3de7b2024-03-19 12:08:41 +0900943 if (qemu_console_is_graphic(dcl.con)) {
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900944 qkbd_state_key_event(kbd, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000945 } else {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000946 [self handleMonitorInput: event];
thsc304f7e2008-01-22 23:25:15 +0000947 }
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900948 return true;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700949 case NSEventTypeKeyUp:
thsc304f7e2008-01-22 23:25:15 +0000950 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000951
952 // don't pass the guest a spurious key-up if we treated this
953 // command-key combo as a host UI action
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700954 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell60105d72019-02-25 10:24:31 +0000955 return true;
Peter Maydell88959192013-12-08 22:59:02 +0000956 }
957
Akihiko Odakica3de7b2024-03-19 12:08:41 +0900958 if (qemu_console_is_graphic(dcl.con)) {
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900959 qkbd_state_key_event(kbd, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000960 }
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900961 return true;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700962 case NSEventTypeScrollWheel:
John Arbuckleae7313e2018-01-08 13:07:07 -0500963 /*
964 * Send wheel events to the guest regardless of window focus.
965 * This is in-line with standard Mac OS X UI behaviour.
966 */
967
968 /* Determine if this is a scroll up or scroll down event */
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900969 if ([event deltaY] != 0) {
970 button = ([event deltaY] > 0) ?
John Arbuckledc3c89d2018-07-09 11:02:35 -0400971 INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900972 } else if ([event deltaX] != 0) {
973 button = ([event deltaX] > 0) ?
Dmitry Petrovd70a5de2022-01-08 16:39:44 +0100974 INPUT_BUTTON_WHEEL_LEFT : INPUT_BUTTON_WHEEL_RIGHT;
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900975 } else {
976 /*
977 * We shouldn't have got a scroll event when deltaY and delta Y
978 * are zero, hence no harm in dropping the event
979 */
980 return true;
John Arbuckledc3c89d2018-07-09 11:02:35 -0400981 }
Dmitry Petrovd70a5de2022-01-08 16:39:44 +0100982
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900983 qemu_input_queue_btn(dcl.con, button, true);
984 qemu_input_event_sync();
985 qemu_input_queue_btn(dcl.con, button, false);
986 qemu_input_event_sync();
987
988 return true;
thsc304f7e2008-01-22 23:25:15 +0000989 default:
Peter Maydell60105d72019-02-25 10:24:31 +0000990 return false;
thsc304f7e2008-01-22 23:25:15 +0000991 }
992}
993
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900994- (void) handleMouseEvent:(NSEvent *)event button:(InputButton)button down:(bool)down
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +0900995{
996 if (!isMouseGrabbed) {
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900997 return;
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +0900998 }
999
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001000 with_bql(^{
1001 qemu_input_queue_btn(dcl.con, button, down);
1002 });
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001003
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001004 [self handleMouseEvent:event];
1005}
1006
1007- (void) handleMouseEvent:(NSEvent *)event
1008{
1009 if (!isMouseGrabbed) {
1010 return;
1011 }
1012
1013 with_bql(^{
1014 if (isAbsoluteEnabled) {
1015 CGFloat d = (CGFloat)screen.height / [self frame].size.height;
1016 NSPoint p = [event locationInWindow];
1017
1018 /* Note that the origin for Cocoa mouse coords is bottom left, not top left. */
1019 qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x * d, 0, screen.width);
1020 qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y * d, 0, screen.height);
1021 } else {
1022 qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, [event deltaX]);
1023 qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, [event deltaY]);
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001024 }
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001025
1026 qemu_input_event_sync();
1027 });
1028}
1029
1030- (void) mouseExited:(NSEvent *)event
1031{
1032 if (isAbsoluteEnabled && isMouseGrabbed) {
1033 [self ungrabMouse];
1034 }
1035}
1036
1037- (void) mouseEntered:(NSEvent *)event
1038{
1039 if (isAbsoluteEnabled && !isMouseGrabbed) {
1040 [self grabMouse];
1041 }
1042}
1043
1044- (void) mouseMoved:(NSEvent *)event
1045{
1046 [self handleMouseEvent:event];
1047}
1048
1049- (void) mouseDown:(NSEvent *)event
1050{
1051 [self handleMouseEvent:event button:INPUT_BUTTON_LEFT down:true];
1052}
1053
1054- (void) rightMouseDown:(NSEvent *)event
1055{
1056 [self handleMouseEvent:event button:INPUT_BUTTON_RIGHT down:true];
1057}
1058
1059- (void) otherMouseDown:(NSEvent *)event
1060{
1061 [self handleMouseEvent:event button:INPUT_BUTTON_MIDDLE down:true];
1062}
1063
1064- (void) mouseDragged:(NSEvent *)event
1065{
1066 [self handleMouseEvent:event];
1067}
1068
1069- (void) rightMouseDragged:(NSEvent *)event
1070{
1071 [self handleMouseEvent:event];
1072}
1073
1074- (void) otherMouseDragged:(NSEvent *)event
1075{
1076 [self handleMouseEvent:event];
1077}
1078
1079- (void) mouseUp:(NSEvent *)event
1080{
1081 if (!isMouseGrabbed) {
1082 [self grabMouse];
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001083 }
1084
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001085 [self handleMouseEvent:event button:INPUT_BUTTON_LEFT down:false];
1086}
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001087
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001088- (void) rightMouseUp:(NSEvent *)event
1089{
1090 [self handleMouseEvent:event button:INPUT_BUTTON_RIGHT down:false];
1091}
1092
1093- (void) otherMouseUp:(NSEvent *)event
1094{
1095 [self handleMouseEvent:event button:INPUT_BUTTON_MIDDLE down:false];
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001096}
1097
thsc304f7e2008-01-22 23:25:15 +00001098- (void) grabMouse
1099{
1100 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
1101
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001102 if (qemu_name)
Akihiko Odaki0c358862024-02-24 21:43:38 +09001103 [[self window] setTitle:[NSString stringWithFormat:@"QEMU %s - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)", qemu_name]];
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001104 else
Akihiko Odaki0c358862024-02-24 21:43:38 +09001105 [[self window] setTitle:@"QEMU - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)"];
Peter Maydell13aefd32014-06-23 10:35:25 +01001106 [self hideCursor];
Akihiko Odakid1929062021-02-23 00:07:14 +09001107 CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled);
Peter Maydell49b9bd42013-12-08 22:59:03 +00001108 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +00001109}
1110
1111- (void) ungrabMouse
1112{
1113 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
1114
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001115 if (qemu_name)
Akihiko Odaki0c358862024-02-24 21:43:38 +09001116 [[self window] setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001117 else
Akihiko Odaki0c358862024-02-24 21:43:38 +09001118 [[self window] setTitle:@"QEMU"];
Peter Maydell13aefd32014-06-23 10:35:25 +01001119 [self unhideCursor];
Akihiko Odakid1929062021-02-23 00:07:14 +09001120 CGAssociateMouseAndMouseCursorPosition(TRUE);
Peter Maydell49b9bd42013-12-08 22:59:03 +00001121 isMouseGrabbed = FALSE;
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001122 [self raiseAllButtons];
thsc304f7e2008-01-22 23:25:15 +00001123}
1124
Akihiko Odakid1929062021-02-23 00:07:14 +09001125- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {
1126 isAbsoluteEnabled = tIsAbsoluteEnabled;
1127 if (isMouseGrabbed) {
1128 CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled);
1129 }
1130}
Peter Maydell49b9bd42013-12-08 22:59:03 +00001131- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +00001132- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
thsc304f7e2008-01-22 23:25:15 +00001133- (QEMUScreen) gscreen {return screen;}
John Arbuckle3b178b72015-09-25 23:14:00 +01001134
1135/*
1136 * Makes the target think all down keys are being released.
1137 * This prevents a stuck key problem, since we will not see
1138 * key up events for those keys after we have lost focus.
1139 */
1140- (void) raiseAllKeys
1141{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001142 with_bql(^{
Akihiko Odaki6d73bb62021-03-10 23:46:02 +09001143 qkbd_state_lift_all_keys(kbd);
Peter Maydell31819e92019-02-25 10:24:27 +00001144 });
John Arbuckle3b178b72015-09-25 23:14:00 +01001145}
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001146
1147- (void) raiseAllButtons
1148{
1149 with_bql(^{
1150 qemu_input_queue_btn(dcl.con, INPUT_BUTTON_LEFT, false);
1151 qemu_input_queue_btn(dcl.con, INPUT_BUTTON_RIGHT, false);
1152 qemu_input_queue_btn(dcl.con, INPUT_BUTTON_MIDDLE, false);
1153 });
1154}
bellard5b0753e2005-03-01 21:37:28 +00001155@end
1156
1157
thsc304f7e2008-01-22 23:25:15 +00001158
bellard5b0753e2005-03-01 21:37:28 +00001159/*
1160 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +00001161 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +00001162 ------------------------------------------------------
1163*/
thsc304f7e2008-01-22 23:25:15 +00001164@interface QemuCocoaAppController : NSObject
John Arbuckled9bc14f2015-09-25 23:13:59 +01001165 <NSWindowDelegate, NSApplicationDelegate>
bellard5b0753e2005-03-01 21:37:28 +00001166{
1167}
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001168- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +00001169- (void)showQEMUDoc:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001170- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +01001171- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +01001172- (void)pauseQEMU:(id)sender;
1173- (void)resumeQEMU:(id)sender;
1174- (void)displayPause;
1175- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +01001176- (void)restartQEMU:(id)sender;
1177- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +01001178- (void)ejectDeviceMedia:(id)sender;
1179- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +01001180- (BOOL)verifyQuit;
John Arbucklef4747902016-03-23 14:26:17 +00001181- (void)openDocumentation:(NSString *)filename;
Programmingkid9e8204b2016-08-15 22:11:06 -04001182- (IBAction) do_about_menu_item: (id) sender;
John Arbucklee47ec1a2017-06-13 23:17:38 -04001183- (void)adjustSpeed:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +00001184@end
1185
thsc304f7e2008-01-22 23:25:15 +00001186@implementation QemuCocoaAppController
1187- (id) init
1188{
Akihiko Odaki0c358862024-02-24 21:43:38 +09001189 NSWindow *window;
1190
thsc304f7e2008-01-22 23:25:15 +00001191 COCOA_DEBUG("QemuCocoaAppController: init\n");
1192
1193 self = [super init];
1194 if (self) {
1195
1196 // create a view and add it to the window
1197 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
1198 if(!cocoaView) {
Akihiko Odaki43137392021-02-23 22:11:06 +09001199 error_report("(cocoa) can't create a view");
thsc304f7e2008-01-22 23:25:15 +00001200 exit(1);
1201 }
1202
1203 // create a window
Akihiko Odaki0c358862024-02-24 21:43:38 +09001204 window = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001205 styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable
thsc304f7e2008-01-22 23:25:15 +00001206 backing:NSBackingStoreBuffered defer:NO];
Akihiko Odaki0c358862024-02-24 21:43:38 +09001207 if(!window) {
Akihiko Odaki43137392021-02-23 22:11:06 +09001208 error_report("(cocoa) can't create window");
thsc304f7e2008-01-22 23:25:15 +00001209 exit(1);
1210 }
Akihiko Odaki0c358862024-02-24 21:43:38 +09001211 [window setAcceptsMouseMovedEvents:YES];
1212 [window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
1213 [window setTitle:qemu_name ? [NSString stringWithFormat:@"QEMU %s", qemu_name] : @"QEMU"];
1214 [window setContentView:cocoaView];
1215 [window makeKeyAndOrderFront:self];
1216 [window center];
1217 [window setDelegate: self];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001218
1219 /* Used for displaying pause on the screen */
1220 pauseLabel = [NSTextField new];
1221 [pauseLabel setBezeled:YES];
1222 [pauseLabel setDrawsBackground:YES];
1223 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
1224 [pauseLabel setEditable:NO];
1225 [pauseLabel setSelectable:NO];
1226 [pauseLabel setStringValue: @"Paused"];
1227 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
1228 [pauseLabel setTextColor: [NSColor blackColor]];
1229 [pauseLabel sizeToFit];
thsc304f7e2008-01-22 23:25:15 +00001230 }
1231 return self;
1232}
1233
1234- (void) dealloc
1235{
1236 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
1237
1238 if (cocoaView)
1239 [cocoaView release];
1240 [super dealloc];
1241}
1242
bellard5b0753e2005-03-01 21:37:28 +00001243- (void)applicationDidFinishLaunching: (NSNotification *) note
1244{
thsc304f7e2008-01-22 23:25:15 +00001245 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
Hikaru Nishidadff742a2019-10-15 10:07:34 +09001246 allow_events = true;
bellard5b0753e2005-03-01 21:37:28 +00001247}
1248
1249- (void)applicationWillTerminate:(NSNotification *)aNotification
1250{
thsc304f7e2008-01-22 23:25:15 +00001251 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
1252
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001253 with_bql(^{
Akihiko Odaki2910abd2022-05-29 17:25:08 +09001254 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
1255 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
1256 });
Akihiko Odaki40c01932021-02-19 20:16:52 +09001257
1258 /*
1259 * Sleep here, because returning will cause OSX to kill us
1260 * immediately; the QEMU main loop will handle the shutdown
1261 * request and terminate the process.
1262 */
1263 [NSThread sleepForTimeInterval:INFINITY];
bellard5b0753e2005-03-01 21:37:28 +00001264}
1265
Andreas Färber41ea49b2009-12-14 22:13:27 +01001266- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
1267{
1268 return YES;
1269}
1270
John Arbuckled9bc14f2015-09-25 23:13:59 +01001271- (NSApplicationTerminateReply)applicationShouldTerminate:
1272 (NSApplication *)sender
1273{
1274 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
1275 return [self verifyQuit];
1276}
1277
Akihiko Odaki15280e82021-06-16 23:19:10 +09001278- (void)windowDidChangeScreen:(NSNotification *)notification
1279{
1280 [cocoaView updateUIInfo];
1281}
1282
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001283- (void)windowDidEnterFullScreen:(NSNotification *)notification
1284{
1285 [cocoaView grabMouse];
1286}
1287
1288- (void)windowDidExitFullScreen:(NSNotification *)notification
1289{
1290 [cocoaView resizeWindow];
1291 [cocoaView ungrabMouse];
1292}
1293
Akihiko Odaki15280e82021-06-16 23:19:10 +09001294- (void)windowDidResize:(NSNotification *)notification
1295{
Akihiko Odakifcb03de2024-02-24 21:43:35 +09001296 [cocoaView updateBounds];
Akihiko Odakiccebb9a2024-03-23 15:20:03 +09001297 [cocoaView updateUIInfo];
Akihiko Odaki15280e82021-06-16 23:19:10 +09001298}
1299
John Arbuckled9bc14f2015-09-25 23:13:59 +01001300/* Called when the user clicks on a window's close button */
1301- (BOOL)windowShouldClose:(id)sender
1302{
1303 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
1304 [NSApp terminate: sender];
1305 /* If the user allows the application to quit then the call to
1306 * NSApp terminate will never return. If we get here then the user
1307 * cancelled the quit, so we should return NO to not permit the
1308 * closing of this window.
1309 */
1310 return NO;
1311}
1312
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001313- (NSApplicationPresentationOptions) window:(NSWindow *)window
1314 willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions;
1315
1316{
1317 return (proposedOptions & ~(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)) |
1318 NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
1319}
1320
Akihiko Odaki9d9bc7d2023-02-28 16:09:46 +09001321/*
1322 * Called when QEMU goes into the background. Note that
1323 * [-NSWindowDelegate windowDidResignKey:] is used here instead of
1324 * [-NSApplicationDelegate applicationWillResignActive:] because it cannot
1325 * detect that the window loses focus when the deck is clicked on macOS 13.2.1.
1326 */
1327- (void) windowDidResignKey: (NSNotification *)aNotification
John Arbuckle3b178b72015-09-25 23:14:00 +01001328{
Akihiko Odaki9d9bc7d2023-02-28 16:09:46 +09001329 COCOA_DEBUG("%s\n", __func__);
Carwyn Ellis69221df2022-01-02 17:41:53 +00001330 [cocoaView ungrabMouse];
John Arbuckle3b178b72015-09-25 23:14:00 +01001331 [cocoaView raiseAllKeys];
1332}
1333
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001334/* We abstract the method called by the Enter Fullscreen menu item
1335 * because Mac OS 10.7 and higher disables it. This is because of the
1336 * menu item's old selector's name toggleFullScreen:
1337 */
1338- (void) doToggleFullScreen:(id)sender
1339{
Akihiko Odaki0c358862024-02-24 21:43:38 +09001340 [[cocoaView window] toggleFullScreen:sender];
thsc304f7e2008-01-22 23:25:15 +00001341}
1342
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +09001343- (void) setFullGrab:(id)sender
1344{
1345 COCOA_DEBUG("QemuCocoaAppController: setFullGrab\n");
1346
1347 [cocoaView setFullGrab:sender];
1348}
1349
John Arbucklef4747902016-03-23 14:26:17 +00001350/* Tries to find then open the specified filename */
1351- (void) openDocumentation: (NSString *) filename
1352{
1353 /* Where to look for local files */
Roman Bolshakov8d6fda82021-01-09 00:38:15 +03001354 NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"docs/"};
John Arbucklef4747902016-03-23 14:26:17 +00001355 NSString *full_file_path;
Roman Bolshakov1ff5a062021-01-02 18:07:21 +03001356 NSURL *full_file_url;
John Arbucklef4747902016-03-23 14:26:17 +00001357
1358 /* iterate thru the possible paths until the file is found */
1359 int index;
1360 for (index = 0; index < ARRAY_SIZE(path_array); index++) {
1361 full_file_path = [[NSBundle mainBundle] executablePath];
1362 full_file_path = [full_file_path stringByDeletingLastPathComponent];
1363 full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
1364 path_array[index], filename];
Roman Bolshakov1ff5a062021-01-02 18:07:21 +03001365 full_file_url = [NSURL fileURLWithPath: full_file_path
1366 isDirectory: false];
1367 if ([[NSWorkspace sharedWorkspace] openURL: full_file_url] == YES) {
John Arbucklef4747902016-03-23 14:26:17 +00001368 return;
1369 }
1370 }
1371
1372 /* If none of the paths opened a file */
1373 NSBeep();
1374 QEMU_Alert(@"Failed to open file");
1375}
1376
thsc304f7e2008-01-22 23:25:15 +00001377- (void)showQEMUDoc:(id)sender
1378{
1379 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1380
Peter Maydell1879f242020-02-28 15:36:16 +00001381 [self openDocumentation: @"index.html"];
thsc304f7e2008-01-22 23:25:15 +00001382}
1383
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001384/* Stretches video to fit host monitor size */
1385- (void)zoomToFit:(id) sender
1386{
Akihiko Odaki55766632024-02-24 21:43:41 +09001387 NSWindowStyleMask styleMask = [[cocoaView window] styleMask] ^ NSWindowStyleMaskResizable;
1388
1389 [[cocoaView window] setStyleMask:styleMask];
1390 [sender setState:styleMask & NSWindowStyleMaskResizable ? NSControlStateValueOn : NSControlStateValueOff];
Akihiko Odakif69a6f02024-03-23 15:20:02 +09001391 [cocoaView resizeWindow];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001392}
bellard5b0753e2005-03-01 21:37:28 +00001393
Carwyn Ellise28a9092023-11-10 16:17:29 +00001394- (void)toggleZoomInterpolation:(id) sender
1395{
1396 if (zoom_interpolation == kCGInterpolationNone) {
1397 zoom_interpolation = kCGInterpolationLow;
1398 [sender setState: NSControlStateValueOn];
1399 } else {
1400 zoom_interpolation = kCGInterpolationNone;
1401 [sender setState: NSControlStateValueOff];
1402 }
1403}
1404
Programmingkidb4c6a112015-05-19 09:11:18 +01001405/* Displays the console on the screen */
1406- (void)displayConsole:(id)sender
1407{
Akihiko Odaki4b49f922024-02-24 21:43:40 +09001408 with_bql(^{
Akihiko Odakica3de7b2024-03-19 12:08:41 +09001409 [cocoaView selectConsoleLocked:[sender tag]];
Akihiko Odaki4b49f922024-02-24 21:43:40 +09001410 });
Programmingkidb4c6a112015-05-19 09:11:18 +01001411}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001412
1413/* Pause the guest */
1414- (void)pauseQEMU:(id)sender
1415{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001416 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001417 qmp_stop(NULL);
1418 });
John Arbuckle8524f1c2015-06-19 10:53:27 +01001419 [sender setEnabled: NO];
1420 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1421 [self displayPause];
1422}
1423
1424/* Resume running the guest operating system */
1425- (void)resumeQEMU:(id) sender
1426{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001427 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001428 qmp_cont(NULL);
1429 });
John Arbuckle8524f1c2015-06-19 10:53:27 +01001430 [sender setEnabled: NO];
1431 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1432 [self removePause];
1433}
1434
1435/* Displays the word pause on the screen */
1436- (void)displayPause
1437{
1438 /* Coordinates have to be calculated each time because the window can change its size */
1439 int xCoord, yCoord, width, height;
Akihiko Odaki1a4b64a2024-02-24 21:43:36 +09001440 xCoord = ([cocoaView frame].size.width - [pauseLabel frame].size.width)/2;
1441 yCoord = [cocoaView frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
John Arbuckle8524f1c2015-06-19 10:53:27 +01001442 width = [pauseLabel frame].size.width;
1443 height = [pauseLabel frame].size.height;
1444 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1445 [cocoaView addSubview: pauseLabel];
1446}
1447
1448/* Removes the word pause from the screen */
1449- (void)removePause
1450{
1451 [pauseLabel removeFromSuperview];
1452}
1453
John Arbuckle27074612015-06-19 10:53:27 +01001454/* Restarts QEMU */
1455- (void)restartQEMU:(id)sender
1456{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001457 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001458 qmp_system_reset(NULL);
1459 });
John Arbuckle27074612015-06-19 10:53:27 +01001460}
1461
1462/* Powers down QEMU */
1463- (void)powerDownQEMU:(id)sender
1464{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001465 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001466 qmp_system_powerdown(NULL);
1467 });
John Arbuckle27074612015-06-19 10:53:27 +01001468}
1469
John Arbuckle693a3e02015-06-19 10:53:27 +01001470/* Ejects the media.
1471 * Uses sender's tag to figure out the device to eject.
1472 */
1473- (void)ejectDeviceMedia:(id)sender
1474{
1475 NSString * drive;
1476 drive = [sender representedObject];
1477 if(drive == nil) {
1478 NSBeep();
1479 QEMU_Alert(@"Failed to find drive to eject!");
1480 return;
1481 }
1482
Peter Maydell31819e92019-02-25 10:24:27 +00001483 __block Error *err = NULL;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001484 with_bql(^{
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001485 qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding],
1486 NULL, false, false, &err);
Peter Maydell31819e92019-02-25 10:24:27 +00001487 });
John Arbuckle693a3e02015-06-19 10:53:27 +01001488 handleAnyDeviceErrors(err);
1489}
1490
1491/* Displays a dialog box asking the user to select an image file to load.
1492 * Uses sender's represented object value to figure out which drive to use.
1493 */
1494- (void)changeDeviceMedia:(id)sender
1495{
1496 /* Find the drive name */
1497 NSString * drive;
1498 drive = [sender representedObject];
1499 if(drive == nil) {
1500 NSBeep();
1501 QEMU_Alert(@"Could not find drive!");
1502 return;
1503 }
1504
1505 /* Display the file open dialog */
1506 NSOpenPanel * openPanel;
1507 openPanel = [NSOpenPanel openPanel];
1508 [openPanel setCanChooseFiles: YES];
1509 [openPanel setAllowsMultipleSelection: NO];
Peter Maydellb5725382018-05-29 19:15:23 +01001510 if([openPanel runModal] == NSModalResponseOK) {
John Arbuckle693a3e02015-06-19 10:53:27 +01001511 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1512 if(file == nil) {
1513 NSBeep();
1514 QEMU_Alert(@"Failed to convert URL to file path!");
1515 return;
1516 }
1517
Peter Maydell31819e92019-02-25 10:24:27 +00001518 __block Error *err = NULL;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001519 with_bql(^{
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001520 qmp_blockdev_change_medium([drive cStringUsingEncoding:
Peter Maydell31819e92019-02-25 10:24:27 +00001521 NSASCIIStringEncoding],
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001522 NULL,
Peter Maydell31819e92019-02-25 10:24:27 +00001523 [file cStringUsingEncoding:
1524 NSASCIIStringEncoding],
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001525 "raw",
Denis V. Lunev80dd5af2022-04-13 01:18:46 +03001526 true, false,
Peter Maydell31819e92019-02-25 10:24:27 +00001527 false, 0,
1528 &err);
1529 });
John Arbuckle693a3e02015-06-19 10:53:27 +01001530 handleAnyDeviceErrors(err);
1531 }
1532}
1533
John Arbuckled9bc14f2015-09-25 23:13:59 +01001534/* Verifies if the user really wants to quit */
1535- (BOOL)verifyQuit
1536{
1537 NSAlert *alert = [NSAlert new];
1538 [alert autorelease];
1539 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1540 [alert addButtonWithTitle: @"Cancel"];
1541 [alert addButtonWithTitle: @"Quit"];
1542 if([alert runModal] == NSAlertSecondButtonReturn) {
1543 return YES;
1544 } else {
1545 return NO;
1546 }
1547}
1548
Programmingkid9e8204b2016-08-15 22:11:06 -04001549/* The action method for the About menu item */
1550- (IBAction) do_about_menu_item: (id) sender
1551{
Akihiko Odaki99eb3132022-02-27 13:22:41 +09001552 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1553 char *icon_path_c = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/512x512/apps/qemu.png");
1554 NSString *icon_path = [NSString stringWithUTF8String:icon_path_c];
1555 g_free(icon_path_c);
1556 NSImage *icon = [[NSImage alloc] initWithContentsOfFile:icon_path];
1557 NSString *version = @"QEMU emulator version " QEMU_FULL_VERSION;
1558 NSString *copyright = @QEMU_COPYRIGHT;
1559 NSDictionary *options;
1560 if (icon) {
1561 options = @{
1562 NSAboutPanelOptionApplicationIcon : icon,
1563 NSAboutPanelOptionApplicationVersion : version,
1564 @"Copyright" : copyright,
1565 };
1566 [icon release];
1567 } else {
1568 options = @{
1569 NSAboutPanelOptionApplicationVersion : version,
1570 @"Copyright" : copyright,
1571 };
Akihiko Odakia0f973f2021-03-09 21:22:26 +09001572 }
Akihiko Odaki99eb3132022-02-27 13:22:41 +09001573 [NSApp orderFrontStandardAboutPanelWithOptions:options];
1574 [pool release];
Programmingkid9e8204b2016-08-15 22:11:06 -04001575}
1576
John Arbucklee47ec1a2017-06-13 23:17:38 -04001577/* Used by the Speed menu items */
1578- (void)adjustSpeed:(id)sender
1579{
1580 int throttle_pct; /* throttle percentage */
1581 NSMenu *menu;
1582
1583 menu = [sender menu];
1584 if (menu != nil)
1585 {
1586 /* Unselect the currently selected item */
1587 for (NSMenuItem *item in [menu itemArray]) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001588 if (item.state == NSControlStateValueOn) {
1589 [item setState: NSControlStateValueOff];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001590 break;
1591 }
1592 }
1593 }
1594
1595 // check the menu item
Brendan Shanks5e246002019-01-31 23:12:25 -08001596 [sender setState: NSControlStateValueOn];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001597
1598 // get the throttle percentage
1599 throttle_pct = [sender tag];
1600
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001601 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001602 cpu_throttle_set(throttle_pct);
1603 });
John Arbucklee47ec1a2017-06-13 23:17:38 -04001604 COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
1605}
1606
Programmingkidb4c6a112015-05-19 09:11:18 +01001607@end
bellard5b0753e2005-03-01 21:37:28 +00001608
Peter Maydell61a2ed42019-02-25 10:24:32 +00001609@interface QemuApplication : NSApplication
1610@end
1611
1612@implementation QemuApplication
1613- (void)sendEvent:(NSEvent *)event
1614{
1615 COCOA_DEBUG("QemuApplication: sendEvent\n");
Peter Maydell55888402019-02-25 10:24:33 +00001616 if (![cocoaView handleEvent:event]) {
1617 [super sendEvent: event];
1618 }
Peter Maydell61a2ed42019-02-25 10:24:32 +00001619}
1620@end
1621
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001622static void create_initial_menus(void)
1623{
thsc304f7e2008-01-22 23:25:15 +00001624 // Add menus
1625 NSMenu *menu;
1626 NSMenuItem *menuItem;
1627
bellard5b0753e2005-03-01 21:37:28 +00001628 [NSApp setMainMenu:[[NSMenu alloc] init]];
Akihiko Odaki5b6988c2022-02-14 18:13:20 +09001629 [NSApp setServicesMenu:[[NSMenu alloc] initWithTitle:@"Services"]];
bellard5b0753e2005-03-01 21:37:28 +00001630
thsc304f7e2008-01-22 23:25:15 +00001631 // Application menu
1632 menu = [[NSMenu alloc] initWithTitle:@""];
Programmingkid9e8204b2016-08-15 22:11:06 -04001633 [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU
thsc304f7e2008-01-22 23:25:15 +00001634 [menu addItem:[NSMenuItem separatorItem]]; //Separator
Akihiko Odaki5b6988c2022-02-14 18:13:20 +09001635 menuItem = [menu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
1636 [menuItem setSubmenu:[NSApp servicesMenu]];
1637 [menu addItem:[NSMenuItem separatorItem]];
thsc304f7e2008-01-22 23:25:15 +00001638 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1639 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001640 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
thsc304f7e2008-01-22 23:25:15 +00001641 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1642 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1643 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1644 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1645 [menuItem setSubmenu:menu];
1646 [[NSApp mainMenu] addItem:menuItem];
1647 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001648
John Arbuckle8524f1c2015-06-19 10:53:27 +01001649 // Machine menu
1650 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1651 [menu setAutoenablesItems: NO];
1652 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1653 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1654 [menu addItem: menuItem];
1655 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001656 [menu addItem: [NSMenuItem separatorItem]];
1657 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1658 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001659 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1660 [menuItem setSubmenu:menu];
1661 [[NSApp mainMenu] addItem:menuItem];
1662
thsc304f7e2008-01-22 23:25:15 +00001663 // View menu
1664 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001665 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
Carwyn Ellis5ec08982023-10-27 16:49:20 +01001666 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease];
Akihiko Odaki55766632024-02-24 21:43:41 +09001667 [menuItem setState: [[cocoaView window] styleMask] & NSWindowStyleMaskResizable ? NSControlStateValueOn : NSControlStateValueOff];
Carwyn Ellis5ec08982023-10-27 16:49:20 +01001668 [menu addItem: menuItem];
Carwyn Ellise28a9092023-11-10 16:17:29 +00001669 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Zoom Interpolation" action:@selector(toggleZoomInterpolation:) keyEquivalent:@""] autorelease];
1670 [menuItem setState: zoom_interpolation == kCGInterpolationLow ? NSControlStateValueOn : NSControlStateValueOff];
1671 [menu addItem: menuItem];
thsc304f7e2008-01-22 23:25:15 +00001672 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1673 [menuItem setSubmenu:menu];
1674 [[NSApp mainMenu] addItem:menuItem];
1675
John Arbucklee47ec1a2017-06-13 23:17:38 -04001676 // Speed menu
1677 menu = [[NSMenu alloc] initWithTitle:@"Speed"];
1678
1679 // Add the rest of the Speed menu items
1680 int p, percentage, throttle_pct;
1681 for (p = 10; p >= 0; p--)
1682 {
1683 percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
1684
1685 menuItem = [[[NSMenuItem alloc]
1686 initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
1687
1688 if (percentage == 100) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001689 [menuItem setState: NSControlStateValueOn];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001690 }
1691
1692 /* Calculate the throttle percentage */
1693 throttle_pct = -1 * percentage + 100;
1694
1695 [menuItem setTag: throttle_pct];
1696 [menu addItem: menuItem];
1697 }
1698 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
1699 [menuItem setSubmenu:menu];
1700 [[NSApp mainMenu] addItem:menuItem];
1701
thsc304f7e2008-01-22 23:25:15 +00001702 // Window menu
1703 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1704 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1705 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1706 [menuItem setSubmenu:menu];
1707 [[NSApp mainMenu] addItem:menuItem];
1708 [NSApp setWindowsMenu:menu];
1709
1710 // Help menu
1711 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1712 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
thsc304f7e2008-01-22 23:25:15 +00001713 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1714 [menuItem setSubmenu:menu];
1715 [[NSApp mainMenu] addItem:menuItem];
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001716}
1717
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001718/* Returns a name for a given console */
1719static NSString * getConsoleName(QemuConsole * console)
1720{
Akihiko Odakica511602022-02-15 09:03:05 +01001721 g_autofree char *label = qemu_console_get_label(console);
1722
1723 return [NSString stringWithUTF8String:label];
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001724}
1725
1726/* Add an entry to the View menu for each console */
1727static void add_console_menu_entries(void)
1728{
1729 NSMenu *menu;
1730 NSMenuItem *menuItem;
1731 int index = 0;
1732
1733 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1734
1735 [menu addItem:[NSMenuItem separatorItem]];
1736
1737 while (qemu_console_lookup_by_index(index) != NULL) {
1738 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1739 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1740 [menuItem setTag: index];
1741 [menu addItem: menuItem];
1742 index++;
1743 }
1744}
1745
1746/* Make menu items for all removable devices.
1747 * Each device is given an 'Eject' and 'Change' menu item.
1748 */
1749static void addRemovableDevicesMenuItems(void)
1750{
1751 NSMenu *menu;
1752 NSMenuItem *menuItem;
1753 BlockInfoList *currentDevice, *pointerToFree;
1754 NSString *deviceName;
1755
1756 currentDevice = qmp_query_block(NULL);
1757 pointerToFree = currentDevice;
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001758
1759 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1760
1761 // Add a separator between related groups of menu items
1762 [menu addItem:[NSMenuItem separatorItem]];
1763
1764 // Set the attributes to the "Removable Media" menu item
1765 NSString *titleString = @"Removable Media";
1766 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1767 NSColor *newColor = [NSColor blackColor];
1768 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1769 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1770 traits:NSBoldFontMask|NSItalicFontMask
1771 weight:0
1772 size:14];
1773 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1774 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1775 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1776
1777 // Add the "Removable Media" menu item
1778 menuItem = [NSMenuItem new];
1779 [menuItem setAttributedTitle: attString];
1780 [menuItem setEnabled: NO];
1781 [menu addItem: menuItem];
1782
1783 /* Loop through all the block devices in the emulator */
1784 while (currentDevice) {
1785 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1786
1787 if(currentDevice->value->removable) {
1788 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1789 action: @selector(changeDeviceMedia:)
1790 keyEquivalent: @""];
1791 [menu addItem: menuItem];
1792 [menuItem setRepresentedObject: deviceName];
1793 [menuItem autorelease];
1794
1795 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1796 action: @selector(ejectDeviceMedia:)
1797 keyEquivalent: @""];
1798 [menu addItem: menuItem];
1799 [menuItem setRepresentedObject: deviceName];
1800 [menuItem autorelease];
1801 }
1802 currentDevice = currentDevice->next;
1803 }
1804 qapi_free_BlockInfoList(pointerToFree);
1805}
1806
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001807@interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner>
1808@end
1809
1810@implementation QemuCocoaPasteboardTypeOwner
1811
1812- (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSPasteboardType)type
1813{
1814 if (type != NSPasteboardTypeString) {
1815 return;
1816 }
1817
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001818 with_bql(^{
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001819 QemuClipboardInfo *info = qemu_clipboard_info_ref(cbinfo);
1820 qemu_event_reset(&cbevent);
1821 qemu_clipboard_request(info, QEMU_CLIPBOARD_TYPE_TEXT);
1822
1823 while (info == cbinfo &&
1824 info->types[QEMU_CLIPBOARD_TYPE_TEXT].available &&
1825 info->types[QEMU_CLIPBOARD_TYPE_TEXT].data == NULL) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001826 bql_unlock();
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001827 qemu_event_wait(&cbevent);
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001828 bql_lock();
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001829 }
1830
1831 if (info == cbinfo) {
1832 NSData *data = [[NSData alloc] initWithBytes:info->types[QEMU_CLIPBOARD_TYPE_TEXT].data
1833 length:info->types[QEMU_CLIPBOARD_TYPE_TEXT].size];
1834 [sender setData:data forType:NSPasteboardTypeString];
1835 [data release];
1836 }
1837
1838 qemu_clipboard_info_unref(info);
1839 });
1840}
1841
1842@end
1843
1844static QemuCocoaPasteboardTypeOwner *cbowner;
1845
1846static void cocoa_clipboard_notify(Notifier *notifier, void *data);
1847static void cocoa_clipboard_request(QemuClipboardInfo *info,
1848 QemuClipboardType type);
1849
1850static QemuClipboardPeer cbpeer = {
1851 .name = "cocoa",
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001852 .notifier = { .notify = cocoa_clipboard_notify },
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001853 .request = cocoa_clipboard_request
1854};
1855
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001856static void cocoa_clipboard_update_info(QemuClipboardInfo *info)
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001857{
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001858 if (info->owner == &cbpeer || info->selection != QEMU_CLIPBOARD_SELECTION_CLIPBOARD) {
1859 return;
1860 }
1861
1862 if (info != cbinfo) {
1863 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1864 qemu_clipboard_info_unref(cbinfo);
1865 cbinfo = qemu_clipboard_info_ref(info);
1866 cbchangecount = [[NSPasteboard generalPasteboard] declareTypes:@[NSPasteboardTypeString] owner:cbowner];
1867 [pool release];
1868 }
1869
1870 qemu_event_set(&cbevent);
1871}
1872
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001873static void cocoa_clipboard_notify(Notifier *notifier, void *data)
1874{
1875 QemuClipboardNotify *notify = data;
1876
1877 switch (notify->type) {
1878 case QEMU_CLIPBOARD_UPDATE_INFO:
1879 cocoa_clipboard_update_info(notify->info);
1880 return;
Marc-André Lureau505dbf92021-07-19 19:49:56 +04001881 case QEMU_CLIPBOARD_RESET_SERIAL:
1882 /* ignore */
1883 return;
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001884 }
1885}
1886
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001887static void cocoa_clipboard_request(QemuClipboardInfo *info,
1888 QemuClipboardType type)
1889{
Akihiko Odaki8c0d8022022-06-15 06:21:31 +09001890 NSAutoreleasePool *pool;
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001891 NSData *text;
1892
1893 switch (type) {
1894 case QEMU_CLIPBOARD_TYPE_TEXT:
Akihiko Odaki8c0d8022022-06-15 06:21:31 +09001895 pool = [[NSAutoreleasePool alloc] init];
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001896 text = [[NSPasteboard generalPasteboard] dataForType:NSPasteboardTypeString];
1897 if (text) {
1898 qemu_clipboard_set_data(&cbpeer, info, type,
1899 [text length], [text bytes], true);
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001900 }
Akihiko Odaki8c0d8022022-06-15 06:21:31 +09001901 [pool release];
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001902 break;
1903 default:
1904 break;
1905 }
1906}
1907
Peter Maydell55888402019-02-25 10:24:33 +00001908/*
1909 * The startup process for the OSX/Cocoa UI is complicated, because
1910 * OSX insists that the UI runs on the initial main thread, and so we
Akihiko Odakibab6a302022-08-19 22:27:54 +09001911 * need to start a second thread which runs the qemu_default_main():
Peter Maydell55888402019-02-25 10:24:33 +00001912 * in main():
Akihiko Odakibab6a302022-08-19 22:27:54 +09001913 * in cocoa_display_init():
1914 * assign cocoa_main to qemu_main
1915 * create application, menus, etc
1916 * in cocoa_main():
1917 * create qemu-main thread
1918 * enter OSX run loop
Peter Maydell55888402019-02-25 10:24:33 +00001919 */
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001920
Peter Maydell55888402019-02-25 10:24:33 +00001921static void *call_qemu_main(void *opaque)
1922{
1923 int status;
1924
Akihiko Odakibab6a302022-08-19 22:27:54 +09001925 COCOA_DEBUG("Second thread: calling qemu_default_main()\n");
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001926 bql_lock();
Akihiko Odakibab6a302022-08-19 22:27:54 +09001927 status = qemu_default_main();
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001928 bql_unlock();
Akihiko Odakibab6a302022-08-19 22:27:54 +09001929 COCOA_DEBUG("Second thread: qemu_default_main() returned, exiting\n");
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001930 [cbowner release];
Peter Maydell55888402019-02-25 10:24:33 +00001931 exit(status);
1932}
1933
Philippe Mathieu-Daudéf9750332023-04-23 18:55:28 +02001934static int cocoa_main(void)
Akihiko Odakibab6a302022-08-19 22:27:54 +09001935{
Peter Maydell55888402019-02-25 10:24:33 +00001936 QemuThread thread;
1937
Akihiko Odakibab6a302022-08-19 22:27:54 +09001938 COCOA_DEBUG("Entered %s()\n", __func__);
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001939
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001940 bql_unlock();
Peter Maydell55888402019-02-25 10:24:33 +00001941 qemu_thread_create(&thread, "qemu_main", call_qemu_main,
1942 NULL, QEMU_THREAD_DETACHED);
1943
thsc304f7e2008-01-22 23:25:15 +00001944 // Start the main event loop
Peter Maydell55888402019-02-25 10:24:33 +00001945 COCOA_DEBUG("Main thread: entering OSX run loop\n");
bellard5b0753e2005-03-01 21:37:28 +00001946 [NSApp run];
Akihiko Odakibab6a302022-08-19 22:27:54 +09001947 COCOA_DEBUG("Main thread: left OSX run loop, which should never happen\n");
ths3b46e622007-09-17 08:09:54 +00001948
Akihiko Odakibab6a302022-08-19 22:27:54 +09001949 abort();
bellard5b0753e2005-03-01 21:37:28 +00001950}
thsc304f7e2008-01-22 23:25:15 +00001951
1952
1953
1954#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001955static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001956 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001957{
1958 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1959
Peter Maydell55888402019-02-25 10:24:33 +00001960 dispatch_async(dispatch_get_main_queue(), ^{
Akihiko Odakifcb03de2024-02-24 21:43:35 +09001961 NSRect rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
Peter Maydell55888402019-02-25 10:24:33 +00001962 [cocoaView setNeedsDisplayInRect:rect];
1963 });
thsc304f7e2008-01-22 23:25:15 +00001964}
1965
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001966static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001967 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001968{
Peter Maydell55888402019-02-25 10:24:33 +00001969 pixman_image_t *image = surface->image;
thsc304f7e2008-01-22 23:25:15 +00001970
Peter Maydell6e657e62013-04-22 10:29:46 +00001971 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Peter Maydell55888402019-02-25 10:24:33 +00001972
1973 // The DisplaySurface will be freed as soon as this callback returns.
1974 // We take a reference to the underlying pixman image here so it does
1975 // not disappear from under our feet; the switchSurface method will
1976 // deref the old image when it is done with it.
1977 pixman_image_ref(image);
1978
1979 dispatch_async(dispatch_get_main_queue(), ^{
1980 [cocoaView switchSurface:image];
1981 });
thsc304f7e2008-01-22 23:25:15 +00001982}
1983
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001984static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001985{
Peter Maydell6e657e62013-04-22 10:29:46 +00001986 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1987
thsc304f7e2008-01-22 23:25:15 +00001988 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
Akihiko Odakica3de7b2024-03-19 12:08:41 +09001989 graphic_hw_update(dcl->con);
thsc304f7e2008-01-22 23:25:15 +00001990
Akihiko Odaki0337e412023-09-21 17:29:34 +09001991 if (qemu_input_is_absolute(dcl->con)) {
Peter Maydell55888402019-02-25 10:24:33 +00001992 dispatch_async(dispatch_get_main_queue(), ^{
1993 if (![cocoaView isAbsoluteEnabled]) {
1994 if ([cocoaView isMouseGrabbed]) {
1995 [cocoaView ungrabMouse];
1996 }
thsc304f7e2008-01-22 23:25:15 +00001997 }
Peter Maydell55888402019-02-25 10:24:33 +00001998 [cocoaView setAbsoluteEnabled:YES];
1999 });
thsc304f7e2008-01-22 23:25:15 +00002000 }
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09002001
2002 if (cbchangecount != [[NSPasteboard generalPasteboard] changeCount]) {
2003 qemu_clipboard_info_unref(cbinfo);
2004 cbinfo = qemu_clipboard_info_new(&cbpeer, QEMU_CLIPBOARD_SELECTION_CLIPBOARD);
2005 if ([[NSPasteboard generalPasteboard] availableTypeFromArray:@[NSPasteboardTypeString]]) {
2006 cbinfo->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
2007 }
2008 qemu_clipboard_update(cbinfo);
2009 cbchangecount = [[NSPasteboard generalPasteboard] changeCount];
2010 qemu_event_set(&cbevent);
2011 }
2012
Peter Maydell6e657e62013-04-22 10:29:46 +00002013 [pool release];
thsc304f7e2008-01-22 23:25:15 +00002014}
2015
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01002016static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
thsc304f7e2008-01-22 23:25:15 +00002017{
Akihiko Odakibab6a302022-08-19 22:27:54 +09002018 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
2019
thsc304f7e2008-01-22 23:25:15 +00002020 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
2021
Akihiko Odakibab6a302022-08-19 22:27:54 +09002022 qemu_main = cocoa_main;
Peter Maydell55888402019-02-25 10:24:33 +00002023
Akihiko Odakibab6a302022-08-19 22:27:54 +09002024 // Pull this console process up to being a fully-fledged graphical
2025 // app with a menubar and Dock icon
2026 ProcessSerialNumber psn = { 0, kCurrentProcess };
2027 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
2028
2029 [QemuApplication sharedApplication];
2030
Akihiko Odakibab6a302022-08-19 22:27:54 +09002031 // Create an Application controller
2032 QemuCocoaAppController *controller = [[QemuCocoaAppController alloc] init];
2033 [NSApp setDelegate:controller];
2034
Programmingkid43227af2015-05-19 09:11:17 +01002035 /* if fullscreen mode is to be used */
Gerd Hoffmann767f9bf2018-02-02 12:10:19 +01002036 if (opts->has_full_screen && opts->full_screen) {
Akihiko Odaki0c358862024-02-24 21:43:38 +09002037 [[cocoaView window] toggleFullScreen: nil];
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +09002038 }
2039 if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) {
Akihiko Odakibab6a302022-08-19 22:27:54 +09002040 [controller setFullGrab: nil];
Programmingkid43227af2015-05-19 09:11:17 +01002041 }
Carwyn Ellis69221df2022-01-02 17:41:53 +00002042
Gerd Hoffmann3487da62020-02-06 12:27:50 +01002043 if (opts->has_show_cursor && opts->show_cursor) {
2044 cursor_hide = 0;
2045 }
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +09002046 if (opts->u.cocoa.has_swap_opt_cmd) {
2047 swap_opt_cmd = opts->u.cocoa.swap_opt_cmd;
2048 }
Programmingkid43227af2015-05-19 09:11:17 +01002049
Carwyn Ellis48941a52022-01-02 17:41:52 +00002050 if (opts->u.cocoa.has_left_command_key && !opts->u.cocoa.left_command_key) {
2051 left_command_key_enabled = 0;
2052 }
2053
Carwyn Ellis5ec08982023-10-27 16:49:20 +01002054 if (opts->u.cocoa.has_zoom_to_fit && opts->u.cocoa.zoom_to_fit) {
Akihiko Odakib6ee03c2024-02-24 21:43:39 +09002055 [cocoaView window].styleMask |= NSWindowStyleMaskResizable;
Carwyn Ellis5ec08982023-10-27 16:49:20 +01002056 }
2057
Carwyn Ellise28a9092023-11-10 16:17:29 +00002058 if (opts->u.cocoa.has_zoom_interpolation && opts->u.cocoa.zoom_interpolation) {
2059 zoom_interpolation = kCGInterpolationLow;
2060 }
2061
Carwyn Ellis5ec08982023-10-27 16:49:20 +01002062 create_initial_menus();
2063 /*
2064 * Create the menu entries which depend on QEMU state (for consoles
2065 * and removable devices). These make calls back into QEMU functions,
2066 * which is OK because at this point we know that the second thread
Stefan Hajnoczia4a411f2024-01-02 10:35:28 -05002067 * holds the BQL and is synchronously waiting for us to
Carwyn Ellis5ec08982023-10-27 16:49:20 +01002068 * finish.
2069 */
2070 add_console_menu_entries();
2071 addRemovableDevicesMenuItems();
2072
Akihiko Odakica3de7b2024-03-19 12:08:41 +09002073 dcl.con = qemu_console_lookup_default();
2074 kbd = qkbd_state_init(dcl.con);
2075
aliguori9794f742009-03-04 19:25:22 +00002076 // register vga output callbacks
Akihiko Odakicc7859c2021-02-19 17:44:19 +09002077 register_displaychangelistener(&dcl);
Akihiko Odakica3de7b2024-03-19 12:08:41 +09002078 [cocoaView updateUIInfo];
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09002079
2080 qemu_event_init(&cbevent, false);
2081 cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];
2082 qemu_clipboard_peer_register(&cbpeer);
Akihiko Odakibab6a302022-08-19 22:27:54 +09002083
2084 [pool release];
thsc304f7e2008-01-22 23:25:15 +00002085}
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01002086
2087static QemuDisplay qemu_display_cocoa = {
2088 .type = DISPLAY_TYPE_COCOA,
2089 .init = cocoa_display_init,
2090};
2091
2092static void register_cocoa(void)
2093{
2094 qemu_display_register(&qemu_display_cocoa);
2095}
2096
2097type_init(register_cocoa);