blob: 20d73098e2ac494320e6c118b91aa2076692fb48 [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 Odaki99eb3132022-02-27 13:22:41 +090096static NSWindow *normalWindow;
Akihiko Odakicc7859c2021-02-19 17:44:19 +090097static const DisplayChangeListenerOps dcl_ops = {
98 .dpy_name = "cocoa",
99 .dpy_gfx_update = cocoa_update,
100 .dpy_gfx_switch = cocoa_switch,
101 .dpy_refresh = cocoa_refresh,
102};
103static DisplayChangeListener dcl = {
104 .ops = &dcl_ops,
105};
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
Akihiko Odakicb823402021-02-25 17:42:02 +0900110static bool stretch_video;
Carwyn Ellise28a9092023-11-10 16:17:29 +0000111static CGInterpolationQuality zoom_interpolation = kCGInterpolationNone;
Akihiko Odakicb823402021-02-25 17:42:02 +0900112static NSTextField *pauseLabel;
bellard5b0753e2005-03-01 21:37:28 +0000113
Hikaru Nishidadff742a2019-10-15 10:07:34 +0900114static bool allow_events;
Peter Maydell55888402019-02-25 10:24:33 +0000115
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +0900116static NSInteger cbchangecount = -1;
117static QemuClipboardInfo *cbinfo;
118static QemuEvent cbevent;
119
Stefan Hajnoczia4a411f2024-01-02 10:35:28 -0500120// Utility functions to run specified code block with the BQL held
Peter Maydell31819e92019-02-25 10:24:27 +0000121typedef void (^CodeBlock)(void);
Peter Maydell60105d72019-02-25 10:24:31 +0000122typedef bool (^BoolCodeBlock)(void);
Peter Maydell31819e92019-02-25 10:24:27 +0000123
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500124static void with_bql(CodeBlock block)
Peter Maydell31819e92019-02-25 10:24:27 +0000125{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500126 bool locked = bql_locked();
Peter Maydell31819e92019-02-25 10:24:27 +0000127 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500128 bql_lock();
Peter Maydell31819e92019-02-25 10:24:27 +0000129 }
130 block();
131 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500132 bql_unlock();
Peter Maydell31819e92019-02-25 10:24:27 +0000133 }
134}
135
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500136static bool bool_with_bql(BoolCodeBlock block)
Peter Maydell60105d72019-02-25 10:24:31 +0000137{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500138 bool locked = bql_locked();
Peter Maydell60105d72019-02-25 10:24:31 +0000139 bool val;
140
141 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500142 bql_lock();
Peter Maydell60105d72019-02-25 10:24:31 +0000143 }
144 val = block();
145 if (!locked) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500146 bql_unlock();
Peter Maydell60105d72019-02-25 10:24:31 +0000147 }
148 return val;
149}
150
John Arbuckleaaac7142016-03-23 14:26:18 +0000151// Mac to QKeyCode conversion
Akihiko Odakicb823402021-02-25 17:42:02 +0900152static const int mac_to_qkeycode_map[] = {
John Arbuckleaaac7142016-03-23 14:26:18 +0000153 [kVK_ANSI_A] = Q_KEY_CODE_A,
154 [kVK_ANSI_B] = Q_KEY_CODE_B,
155 [kVK_ANSI_C] = Q_KEY_CODE_C,
156 [kVK_ANSI_D] = Q_KEY_CODE_D,
157 [kVK_ANSI_E] = Q_KEY_CODE_E,
158 [kVK_ANSI_F] = Q_KEY_CODE_F,
159 [kVK_ANSI_G] = Q_KEY_CODE_G,
160 [kVK_ANSI_H] = Q_KEY_CODE_H,
161 [kVK_ANSI_I] = Q_KEY_CODE_I,
162 [kVK_ANSI_J] = Q_KEY_CODE_J,
163 [kVK_ANSI_K] = Q_KEY_CODE_K,
164 [kVK_ANSI_L] = Q_KEY_CODE_L,
165 [kVK_ANSI_M] = Q_KEY_CODE_M,
166 [kVK_ANSI_N] = Q_KEY_CODE_N,
167 [kVK_ANSI_O] = Q_KEY_CODE_O,
168 [kVK_ANSI_P] = Q_KEY_CODE_P,
169 [kVK_ANSI_Q] = Q_KEY_CODE_Q,
170 [kVK_ANSI_R] = Q_KEY_CODE_R,
171 [kVK_ANSI_S] = Q_KEY_CODE_S,
172 [kVK_ANSI_T] = Q_KEY_CODE_T,
173 [kVK_ANSI_U] = Q_KEY_CODE_U,
174 [kVK_ANSI_V] = Q_KEY_CODE_V,
175 [kVK_ANSI_W] = Q_KEY_CODE_W,
176 [kVK_ANSI_X] = Q_KEY_CODE_X,
177 [kVK_ANSI_Y] = Q_KEY_CODE_Y,
178 [kVK_ANSI_Z] = Q_KEY_CODE_Z,
ths3b46e622007-09-17 08:09:54 +0000179
John Arbuckleaaac7142016-03-23 14:26:18 +0000180 [kVK_ANSI_0] = Q_KEY_CODE_0,
181 [kVK_ANSI_1] = Q_KEY_CODE_1,
182 [kVK_ANSI_2] = Q_KEY_CODE_2,
183 [kVK_ANSI_3] = Q_KEY_CODE_3,
184 [kVK_ANSI_4] = Q_KEY_CODE_4,
185 [kVK_ANSI_5] = Q_KEY_CODE_5,
186 [kVK_ANSI_6] = Q_KEY_CODE_6,
187 [kVK_ANSI_7] = Q_KEY_CODE_7,
188 [kVK_ANSI_8] = Q_KEY_CODE_8,
189 [kVK_ANSI_9] = Q_KEY_CODE_9,
190
191 [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
192 [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
193 [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
194 [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
195 [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
196 [kVK_Tab] = Q_KEY_CODE_TAB,
197 [kVK_Return] = Q_KEY_CODE_RET,
198 [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
199 [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
200 [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
201 [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
202 [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
203 [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
204 [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
205 [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
John Arbuckleaaac7142016-03-23 14:26:18 +0000206 [kVK_Space] = Q_KEY_CODE_SPC,
207
208 [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
209 [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
210 [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
211 [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
212 [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
213 [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
214 [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
215 [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
216 [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
217 [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
218 [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
219 [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
220 [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
221 [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
222 [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
223 [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
224 [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
225 [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
226
227 [kVK_UpArrow] = Q_KEY_CODE_UP,
228 [kVK_DownArrow] = Q_KEY_CODE_DOWN,
229 [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
230 [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
231
232 [kVK_Help] = Q_KEY_CODE_INSERT,
233 [kVK_Home] = Q_KEY_CODE_HOME,
234 [kVK_PageUp] = Q_KEY_CODE_PGUP,
235 [kVK_PageDown] = Q_KEY_CODE_PGDN,
236 [kVK_End] = Q_KEY_CODE_END,
237 [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
238
239 [kVK_Escape] = Q_KEY_CODE_ESC,
240
241 /* The Power key can't be used directly because the operating system uses
242 * it. This key can be emulated by using it in place of another key such as
243 * F1. Don't forget to disable the real key binding.
244 */
245 /* [kVK_F1] = Q_KEY_CODE_POWER, */
246
247 [kVK_F1] = Q_KEY_CODE_F1,
248 [kVK_F2] = Q_KEY_CODE_F2,
249 [kVK_F3] = Q_KEY_CODE_F3,
250 [kVK_F4] = Q_KEY_CODE_F4,
251 [kVK_F5] = Q_KEY_CODE_F5,
252 [kVK_F6] = Q_KEY_CODE_F6,
253 [kVK_F7] = Q_KEY_CODE_F7,
254 [kVK_F8] = Q_KEY_CODE_F8,
255 [kVK_F9] = Q_KEY_CODE_F9,
256 [kVK_F10] = Q_KEY_CODE_F10,
257 [kVK_F11] = Q_KEY_CODE_F11,
258 [kVK_F12] = Q_KEY_CODE_F12,
259 [kVK_F13] = Q_KEY_CODE_PRINT,
260 [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
261 [kVK_F15] = Q_KEY_CODE_PAUSE,
262
Akihiko Odaki708b7252021-02-12 09:04:04 +0900263 // JIS keyboards only
264 [kVK_JIS_Yen] = Q_KEY_CODE_YEN,
265 [kVK_JIS_Underscore] = Q_KEY_CODE_RO,
266 [kVK_JIS_KeypadComma] = Q_KEY_CODE_KP_COMMA,
267 [kVK_JIS_Eisu] = Q_KEY_CODE_MUHENKAN,
268 [kVK_JIS_Kana] = Q_KEY_CODE_HENKAN,
269
John Arbuckleaaac7142016-03-23 14:26:18 +0000270 /*
271 * The eject and volume keys can't be used here because they are handled at
272 * a lower level than what an Application can see.
273 */
bellard5b0753e2005-03-01 21:37:28 +0000274};
275
Andreas Färber77047bb2009-12-13 00:55:53 +0100276static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000277{
John Arbuckleaaac7142016-03-23 14:26:18 +0000278 if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
Akihiko Odaki43137392021-02-23 22:11:06 +0900279 error_report("(cocoa) warning unknown keycode 0x%x", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000280 return 0;
281 }
John Arbuckleaaac7142016-03-23 14:26:18 +0000282 return mac_to_qkeycode_map[keycode];
bellard5b0753e2005-03-01 21:37:28 +0000283}
284
John Arbuckle693a3e02015-06-19 10:53:27 +0100285/* Displays an alert dialog box with the specified message */
286static void QEMU_Alert(NSString *message)
287{
288 NSAlert *alert;
289 alert = [NSAlert new];
290 [alert setMessageText: message];
291 [alert runModal];
292}
thsc304f7e2008-01-22 23:25:15 +0000293
John Arbuckle693a3e02015-06-19 10:53:27 +0100294/* Handles any errors that happen with a device transaction */
295static void handleAnyDeviceErrors(Error * err)
296{
297 if (err) {
298 QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
299 encoding: NSASCIIStringEncoding]);
300 error_free(err);
301 }
302}
thsc304f7e2008-01-22 23:25:15 +0000303
bellard5b0753e2005-03-01 21:37:28 +0000304/*
305 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000306 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000307 ------------------------------------------------------
308*/
thsc304f7e2008-01-22 23:25:15 +0000309@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000310{
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900311 NSTrackingArea *trackingArea;
thsc304f7e2008-01-22 23:25:15 +0000312 QEMUScreen screen;
Peter Maydell55888402019-02-25 10:24:33 +0000313 pixman_image_t *pixman_image;
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900314 QKbdState *kbd;
Peter Maydell49b9bd42013-12-08 22:59:03 +0000315 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000316 BOOL isAbsoluteEnabled;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900317 CFMachPortRef eventsTap;
thsc304f7e2008-01-22 23:25:15 +0000318}
Peter Maydell72a3e312019-02-25 10:24:28 +0000319- (void) switchSurface:(pixman_image_t *)image;
thsc304f7e2008-01-22 23:25:15 +0000320- (void) grabMouse;
321- (void) ungrabMouse;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900322- (void) setFullGrab:(id)sender;
John Arbuckle9c3a4182017-11-07 10:14:14 +0000323- (void) handleMonitorInput:(NSEvent *)event;
Peter Maydell60105d72019-02-25 10:24:31 +0000324- (bool) handleEvent:(NSEvent *)event;
325- (bool) handleEventLocked:(NSEvent *)event;
thsc304f7e2008-01-22 23:25:15 +0000326- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100327/* The state surrounding mouse grabbing is potentially confusing.
328 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
329 * pointing device an absolute-position one?"], but is only updated on
330 * next refresh.
331 * isMouseGrabbed tracks whether GUI events are directed to the guest;
332 * it controls whether special keys like Cmd get sent to the guest,
333 * and whether we capture the mouse when in non-absolute mode.
Peter Maydellf61c3872014-06-23 10:35:24 +0100334 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000335- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000336- (BOOL) isAbsoluteEnabled;
thsc304f7e2008-01-22 23:25:15 +0000337- (QEMUScreen) gscreen;
John Arbuckle3b178b72015-09-25 23:14:00 +0100338- (void) raiseAllKeys;
thsc304f7e2008-01-22 23:25:15 +0000339@end
ths3b46e622007-09-17 08:09:54 +0000340
Andreas Färber7fee1992011-06-09 20:53:32 +0200341QemuCocoaView *cocoaView;
342
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900343static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef cgEvent, void *userInfo)
344{
Philippe Mathieu-Daudé21eb7522023-10-04 14:00:13 +0200345 QemuCocoaView *view = userInfo;
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900346 NSEvent *event = [NSEvent eventWithCGEvent:cgEvent];
Philippe Mathieu-Daudé21eb7522023-10-04 14:00:13 +0200347 if ([view isMouseGrabbed] && [view handleEvent:event]) {
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900348 COCOA_DEBUG("Global events tap: qemu handled the event, capturing!\n");
349 return NULL;
350 }
351 COCOA_DEBUG("Global events tap: qemu did not handle the event, letting it through...\n");
352
353 return cgEvent;
354}
355
thsc304f7e2008-01-22 23:25:15 +0000356@implementation QemuCocoaView
357- (id)initWithFrame:(NSRect)frameRect
358{
359 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000360
thsc304f7e2008-01-22 23:25:15 +0000361 self = [super initWithFrame:frameRect];
362 if (self) {
pbrook95219892006-04-09 01:06:34 +0000363
thsc304f7e2008-01-22 23:25:15 +0000364 screen.width = frameRect.size.width;
365 screen.height = frameRect.size.height;
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900366 kbd = qkbd_state_init(dcl.con);
David Parsonsf5af8022024-02-24 14:06:20 +0000367#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_14_0
368 [self setClipsToBounds:YES];
369#endif
bellard7c206a72005-12-18 19:18:45 +0000370
thsc304f7e2008-01-22 23:25:15 +0000371 }
372 return self;
373}
bellard7c206a72005-12-18 19:18:45 +0000374
thsc304f7e2008-01-22 23:25:15 +0000375- (void) dealloc
376{
377 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000378
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900379 if (pixman_image) {
Peter Maydell55888402019-02-25 10:24:33 +0000380 pixman_image_unref(pixman_image);
381 }
ths3b46e622007-09-17 08:09:54 +0000382
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900383 qkbd_state_free(kbd);
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900384
385 if (eventsTap) {
386 CFRelease(eventsTap);
387 }
388
thsc304f7e2008-01-22 23:25:15 +0000389 [super dealloc];
390}
bellard5cbfcd02006-06-14 15:53:24 +0000391
Andreas Färberd50f71d2009-12-13 02:03:33 +0100392- (BOOL) isOpaque
393{
394 return YES;
395}
396
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900397- (void) removeTrackingRect
Peter Maydell5dd45be2014-06-23 10:35:23 +0100398{
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900399 if (trackingArea) {
400 [self removeTrackingArea:trackingArea];
401 [trackingArea release];
402 trackingArea = nil;
403 }
Peter Maydell5dd45be2014-06-23 10:35:23 +0100404}
405
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900406- (void) frameUpdated
Chen Zhang2044dff2019-06-04 17:36:00 +0800407{
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900408 [self removeTrackingRect];
409
410 if ([self window]) {
411 NSTrackingAreaOptions options = NSTrackingActiveInKeyWindow |
412 NSTrackingMouseEnteredAndExited |
413 NSTrackingMouseMoved;
414 trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame]
415 options:options
416 owner:self
417 userInfo:nil];
418 [self addTrackingArea:trackingArea];
419 [self updateUIInfo];
Chen Zhang2044dff2019-06-04 17:36:00 +0800420 }
421}
422
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900423- (void) viewDidMoveToWindow
424{
425 [self resizeWindow];
426 [self frameUpdated];
427}
428
429- (void) viewWillMoveToWindow:(NSWindow *)newWindow
430{
431 [self removeTrackingRect];
432}
433
Peter Maydell13aefd32014-06-23 10:35:25 +0100434- (void) hideCursor
435{
436 if (!cursor_hide) {
437 return;
438 }
439 [NSCursor hide];
440}
441
442- (void) unhideCursor
443{
444 if (!cursor_hide) {
445 return;
446 }
447 [NSCursor unhide];
448}
449
thsc304f7e2008-01-22 23:25:15 +0000450- (void) drawRect:(NSRect) rect
451{
452 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000453
thsc304f7e2008-01-22 23:25:15 +0000454 // get CoreGraphic context
Brendan Shanks5e246002019-01-31 23:12:25 -0800455 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] CGContext];
Brendan Shanks5e246002019-01-31 23:12:25 -0800456
Carwyn Ellise28a9092023-11-10 16:17:29 +0000457 CGContextSetInterpolationQuality (viewContextRef, zoom_interpolation);
thsc304f7e2008-01-22 23:25:15 +0000458 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000459
thsc304f7e2008-01-22 23:25:15 +0000460 // draw screen bitmap directly to Core Graphics context
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900461 if (!pixman_image) {
Peter Maydell7d270b12013-12-24 02:51:47 +0000462 // Draw request before any guest device has set up a framebuffer:
463 // just draw an opaque black rectangle
464 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
465 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
466 } else {
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900467 int w = pixman_image_get_width(pixman_image);
468 int h = pixman_image_get_height(pixman_image);
469 int bitsPerPixel = PIXMAN_FORMAT_BPP(pixman_image_get_format(pixman_image));
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900470 int stride = pixman_image_get_stride(pixman_image);
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900471 CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(
472 NULL,
473 pixman_image_get_data(pixman_image),
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900474 stride * h,
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900475 NULL
476 );
thsc304f7e2008-01-22 23:25:15 +0000477 CGImageRef imageRef = CGImageCreate(
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900478 w, //width
479 h, //height
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900480 DIV_ROUND_UP(bitsPerPixel, 8) * 2, //bitsPerComponent
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900481 bitsPerPixel, //bitsPerPixel
Akihiko Odakid9c32b82021-02-22 23:40:12 +0900482 stride, //bytesPerRow
Akihiko Odakiae57d352021-03-05 21:13:04 +0900483 CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace
484 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, //bitmapInfo
thsc304f7e2008-01-22 23:25:15 +0000485 dataProviderRef, //provider
486 NULL, //decode
487 0, //interpolate
488 kCGRenderingIntentDefault //intent
489 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100490 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
491 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100492 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100493 int i;
494 CGImageRef clipImageRef;
495 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000496
Peter Maydellb63901d2015-05-19 09:11:17 +0100497 [self getRectsBeingDrawn:&rectList count:&rectCount];
498 for (i = 0; i < rectCount; i++) {
Akihiko Odakifcb03de2024-02-24 21:43:35 +0900499 clipRect = rectList[i];
500 clipRect.origin.y = (float)h - (clipRect.origin.y + clipRect.size.height);
Peter Maydellb63901d2015-05-19 09:11:17 +0100501 clipImageRef = CGImageCreateWithImageInRect(
502 imageRef,
503 clipRect
504 );
505 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
506 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000507 }
thsc304f7e2008-01-22 23:25:15 +0000508 CGImageRelease (imageRef);
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900509 CGDataProviderRelease(dataProviderRef);
bellard5b0753e2005-03-01 21:37:28 +0000510 }
511}
512
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900513- (NSSize) screenSafeAreaSize
bellard5b0753e2005-03-01 21:37:28 +0000514{
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900515 NSSize size = [[[self window] screen] frame].size;
516 NSEdgeInsets insets = [[[self window] screen] safeAreaInsets];
517 size.width -= insets.left + insets.right;
518 size.height -= insets.top + insets.bottom;
519 return size;
520}
ths3b46e622007-09-17 08:09:54 +0000521
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900522- (void) resizeWindow
523{
524 [[self window] setContentAspectRatio:NSMakeSize(screen.width, screen.height)];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100525
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900526 if (!stretch_video) {
527 [[self window] setContentSize:NSMakeSize(screen.width, screen.height)];
528 [[self window] center];
529 } else if ([[self window] styleMask] & NSWindowStyleMaskFullScreen) {
530 [[self window] setContentSize:[self screenSafeAreaSize]];
531 [[self window] center];
thsc304f7e2008-01-22 23:25:15 +0000532 }
bellard5b0753e2005-03-01 21:37:28 +0000533}
534
Akihiko Odakifcb03de2024-02-24 21:43:35 +0900535- (void) updateBounds
536{
537 [self setBoundsSize:NSMakeSize(screen.width, screen.height)];
538}
539
Peter Maydell8d65dee2022-02-24 10:13:29 +0000540- (void) updateUIInfoLocked
Akihiko Odaki15280e82021-06-16 23:19:10 +0900541{
Stefan Hajnoczia4a411f2024-01-02 10:35:28 -0500542 /* Must be called with the BQL, i.e. via updateUIInfo */
Akihiko Odaki15280e82021-06-16 23:19:10 +0900543 NSSize frameSize;
544 QemuUIInfo info;
545
546 if (!qemu_console_is_graphic(dcl.con)) {
547 return;
548 }
549
550 if ([self window]) {
551 NSDictionary *description = [[[self window] screen] deviceDescription];
552 CGDirectDisplayID display = [[description objectForKey:@"NSScreenNumber"] unsignedIntValue];
553 NSSize screenSize = [[[self window] screen] frame].size;
554 CGSize screenPhysicalSize = CGDisplayScreenSize(display);
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900555 bool isFullscreen = ([[self window] styleMask] & NSWindowStyleMaskFullScreen) != 0;
Akihiko Odaki52eaefd2022-07-02 23:25:19 +0900556 CVDisplayLinkRef displayLink;
Akihiko Odaki15280e82021-06-16 23:19:10 +0900557
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900558 frameSize = isFullscreen ? [self screenSafeAreaSize] : [self frame].size;
Akihiko Odaki52eaefd2022-07-02 23:25:19 +0900559
560 if (!CVDisplayLinkCreateWithCGDisplay(display, &displayLink)) {
561 CVTime period = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLink);
562 CVDisplayLinkRelease(displayLink);
563 if (!(period.flags & kCVTimeIsIndefinite)) {
564 update_displaychangelistener(&dcl,
565 1000 * period.timeValue / period.timeScale);
566 info.refresh_rate = (int64_t)1000 * period.timeScale / period.timeValue;
567 }
568 }
569
Akihiko Odaki15280e82021-06-16 23:19:10 +0900570 info.width_mm = frameSize.width / screenSize.width * screenPhysicalSize.width;
571 info.height_mm = frameSize.height / screenSize.height * screenPhysicalSize.height;
572 } else {
573 frameSize = [self frame].size;
574 info.width_mm = 0;
575 info.height_mm = 0;
576 }
577
578 info.xoff = 0;
579 info.yoff = 0;
580 info.width = frameSize.width;
581 info.height = frameSize.height;
582
Marc-André Lureauca19ef52021-04-13 20:39:11 +0400583 dpy_set_ui_info(dcl.con, &info, TRUE);
Akihiko Odaki15280e82021-06-16 23:19:10 +0900584}
585
Peter Maydell8d65dee2022-02-24 10:13:29 +0000586- (void) updateUIInfo
587{
588 if (!allow_events) {
589 /*
590 * Don't try to tell QEMU about UI information in the application
591 * startup phase -- we haven't yet registered dcl with the QEMU UI
Akihiko Odakibab6a302022-08-19 22:27:54 +0900592 * layer.
Peter Maydell8d65dee2022-02-24 10:13:29 +0000593 * When cocoa_display_init() does register the dcl, the UI layer
594 * will call cocoa_switch(), which will call updateUIInfo, so
595 * we don't lose any information here.
596 */
597 return;
598 }
599
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500600 with_bql(^{
Peter Maydell8d65dee2022-02-24 10:13:29 +0000601 [self updateUIInfoLocked];
602 });
603}
604
Peter Maydell72a3e312019-02-25 10:24:28 +0000605- (void) switchSurface:(pixman_image_t *)image
thsc304f7e2008-01-22 23:25:15 +0000606{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100607 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000608
Peter Maydell72a3e312019-02-25 10:24:28 +0000609 int w = pixman_image_get_width(image);
610 int h = pixman_image_get_height(image);
Peter Maydelld3345a02013-12-24 02:51:46 +0000611
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900612 if (w != screen.width || h != screen.height) {
Peter Maydelld3345a02013-12-24 02:51:46 +0000613 // Resize before we trigger the redraw, or we'll redraw at the wrong size
614 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
615 screen.width = w;
616 screen.height = h;
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900617 [self resizeWindow];
Akihiko Odakifcb03de2024-02-24 21:43:35 +0900618 [self updateBounds];
Peter Maydelld3345a02013-12-24 02:51:46 +0000619 }
Peter Maydell8510d912013-03-18 20:28:21 +0000620
thsc304f7e2008-01-22 23:25:15 +0000621 // update screenBuffer
Akihiko Odakic0ff29d2021-02-12 09:06:29 +0900622 if (pixman_image) {
Peter Maydell55888402019-02-25 10:24:33 +0000623 pixman_image_unref(pixman_image);
624 }
thsc304f7e2008-01-22 23:25:15 +0000625
Peter Maydell55888402019-02-25 10:24:33 +0000626 pixman_image = image;
thsc304f7e2008-01-22 23:25:15 +0000627}
628
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +0900629- (void) setFullGrab:(id)sender
630{
631 COCOA_DEBUG("QemuCocoaView: setFullGrab\n");
632
633 CGEventMask mask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged);
634 eventsTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
635 mask, handleTapEvent, self);
636 if (!eventsTap) {
637 warn_report("Could not create event tap, system key combos will not be captured.\n");
638 return;
639 } else {
640 COCOA_DEBUG("Global events tap created! Will capture system key combos.\n");
641 }
642
643 CFRunLoopRef runLoop = CFRunLoopGetCurrent();
644 if (!runLoop) {
645 warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
646 return;
647 }
648
649 CFRunLoopSourceRef tapEventsSrc = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventsTap, 0);
650 if (!tapEventsSrc ) {
651 warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n");
652 return;
653 }
654
655 CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode);
656 CFRelease(tapEventsSrc);
657}
658
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900659- (void) toggleKey: (int)keycode {
660 qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode));
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700661}
662
John Arbuckle9c3a4182017-11-07 10:14:14 +0000663// Does the work of sending input to the monitor
664- (void) handleMonitorInput:(NSEvent *)event
665{
666 int keysym = 0;
667 int control_key = 0;
668
669 // if the control key is down
670 if ([event modifierFlags] & NSEventModifierFlagControl) {
671 control_key = 1;
672 }
673
674 /* translates Macintosh keycodes to QEMU's keysym */
675
Philippe Mathieu-Daudé94592622022-02-15 16:21:14 +0100676 static const int without_control_translation[] = {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000677 [0 ... 0xff] = 0, // invalid key
678
679 [kVK_UpArrow] = QEMU_KEY_UP,
680 [kVK_DownArrow] = QEMU_KEY_DOWN,
681 [kVK_RightArrow] = QEMU_KEY_RIGHT,
682 [kVK_LeftArrow] = QEMU_KEY_LEFT,
683 [kVK_Home] = QEMU_KEY_HOME,
684 [kVK_End] = QEMU_KEY_END,
685 [kVK_PageUp] = QEMU_KEY_PAGEUP,
686 [kVK_PageDown] = QEMU_KEY_PAGEDOWN,
687 [kVK_ForwardDelete] = QEMU_KEY_DELETE,
688 [kVK_Delete] = QEMU_KEY_BACKSPACE,
689 };
690
Philippe Mathieu-Daudé94592622022-02-15 16:21:14 +0100691 static const int with_control_translation[] = {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000692 [0 ... 0xff] = 0, // invalid key
693
694 [kVK_UpArrow] = QEMU_KEY_CTRL_UP,
695 [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN,
696 [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT,
697 [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT,
698 [kVK_Home] = QEMU_KEY_CTRL_HOME,
699 [kVK_End] = QEMU_KEY_CTRL_END,
700 [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP,
701 [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN,
702 };
703
704 if (control_key != 0) { /* If the control key is being used */
705 if ([event keyCode] < ARRAY_SIZE(with_control_translation)) {
706 keysym = with_control_translation[[event keyCode]];
707 }
708 } else {
709 if ([event keyCode] < ARRAY_SIZE(without_control_translation)) {
710 keysym = without_control_translation[[event keyCode]];
711 }
712 }
713
714 // if not a key that needs translating
715 if (keysym == 0) {
716 NSString *ks = [event characters];
717 if ([ks length] > 0) {
718 keysym = [ks characterAtIndex:0];
719 }
720 }
721
722 if (keysym) {
Marc-André Lureaucc6ba2c2023-08-30 13:38:20 +0400723 qemu_text_console_put_keysym(NULL, keysym);
John Arbuckle9c3a4182017-11-07 10:14:14 +0000724 }
725}
726
Peter Maydell60105d72019-02-25 10:24:31 +0000727- (bool) handleEvent:(NSEvent *)event
thsc304f7e2008-01-22 23:25:15 +0000728{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500729 return bool_with_bql(^{
Peter Maydell60105d72019-02-25 10:24:31 +0000730 return [self handleEventLocked:event];
Peter Maydell31819e92019-02-25 10:24:27 +0000731 });
732}
thsc304f7e2008-01-22 23:25:15 +0000733
Peter Maydell60105d72019-02-25 10:24:31 +0000734- (bool) handleEventLocked:(NSEvent *)event
Peter Maydell31819e92019-02-25 10:24:27 +0000735{
Peter Maydell60105d72019-02-25 10:24:31 +0000736 /* Return true if we handled the event, false if it should be given to OSX */
Peter Maydell31819e92019-02-25 10:24:27 +0000737 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900738 InputButton button;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700739 int keycode = 0;
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900740 NSUInteger modifiers = [event modifierFlags];
741
Akihiko Odakiad7f2f82021-03-12 22:32:12 +0900742 /*
743 * Check -[NSEvent modifierFlags] here.
744 *
745 * There is a NSEventType for an event notifying the change of
746 * -[NSEvent modifierFlags], NSEventTypeFlagsChanged but these operations
747 * are performed for any events because a modifier state may change while
748 * the application is inactive (i.e. no events fire) and we don't want to
749 * wait for another modifier state change to detect such a change.
750 *
751 * NSEventModifierFlagCapsLock requires a special treatment. The other flags
752 * are handled in similar manners.
753 *
754 * NSEventModifierFlagCapsLock
755 * ---------------------------
756 *
757 * If CapsLock state is changed, "up" and "down" events will be fired in
758 * sequence, effectively updates CapsLock state on the guest.
759 *
760 * The other flags
761 * ---------------
762 *
763 * If a flag is not set, fire "up" events for all keys which correspond to
764 * the flag. Note that "down" events are not fired here because the flags
765 * checked here do not tell what exact keys are down.
766 *
767 * If one of the keys corresponding to a flag is down, we rely on
768 * -[NSEvent keyCode] of an event whose -[NSEvent type] is
769 * NSEventTypeFlagsChanged to know the exact key which is down, which has
770 * the following two downsides:
771 * - It does not work when the application is inactive as described above.
772 * - It malfactions *after* the modifier state is changed while the
773 * application is inactive. It is because -[NSEvent keyCode] does not tell
774 * if the key is up or down, and requires to infer the current state from
775 * the previous state. It is still possible to fix such a malfanction by
776 * completely leaving your hands from the keyboard, which hopefully makes
777 * this implementation usable enough.
778 */
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900779 if (!!(modifiers & NSEventModifierFlagCapsLock) !=
780 qkbd_state_modifier_get(kbd, QKBD_MOD_CAPSLOCK)) {
781 qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, true);
782 qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, false);
783 }
784
785 if (!(modifiers & NSEventModifierFlagShift)) {
786 qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT, false);
787 qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT_R, false);
788 }
789 if (!(modifiers & NSEventModifierFlagControl)) {
790 qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL, false);
791 qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL_R, false);
792 }
793 if (!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900794 if (swap_opt_cmd) {
795 qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
796 qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
797 } else {
798 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
799 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
800 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900801 }
802 if (!(modifiers & NSEventModifierFlagCommand)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900803 if (swap_opt_cmd) {
804 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false);
805 qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false);
806 } else {
807 qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false);
808 qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false);
809 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900810 }
thsc304f7e2008-01-22 23:25:15 +0000811
812 switch ([event type]) {
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700813 case NSEventTypeFlagsChanged:
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900814 switch ([event keyCode]) {
815 case kVK_Shift:
816 if (!!(modifiers & NSEventModifierFlagShift)) {
817 [self toggleKey:Q_KEY_CODE_SHIFT];
818 }
819 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700820
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900821 case kVK_RightShift:
822 if (!!(modifiers & NSEventModifierFlagShift)) {
823 [self toggleKey:Q_KEY_CODE_SHIFT_R];
824 }
825 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700826
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900827 case kVK_Control:
828 if (!!(modifiers & NSEventModifierFlagControl)) {
829 [self toggleKey:Q_KEY_CODE_CTRL];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700830 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900831 break;
832
833 case kVK_RightControl:
834 if (!!(modifiers & NSEventModifierFlagControl)) {
835 [self toggleKey:Q_KEY_CODE_CTRL_R];
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700836 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900837 break;
838
839 case kVK_Option:
840 if (!!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900841 if (swap_opt_cmd) {
842 [self toggleKey:Q_KEY_CODE_META_L];
843 } else {
844 [self toggleKey:Q_KEY_CODE_ALT];
845 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700846 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900847 break;
848
849 case kVK_RightOption:
850 if (!!(modifiers & NSEventModifierFlagOption)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900851 if (swap_opt_cmd) {
852 [self toggleKey:Q_KEY_CODE_META_R];
853 } else {
854 [self toggleKey:Q_KEY_CODE_ALT_R];
855 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700856 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900857 break;
858
859 /* Don't pass command key changes to guest unless mouse is grabbed */
860 case kVK_Command:
861 if (isMouseGrabbed &&
Akihiko Odakid6b6dea2022-03-18 00:29:49 +0900862 !!(modifiers & NSEventModifierFlagCommand) &&
863 left_command_key_enabled) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900864 if (swap_opt_cmd) {
865 [self toggleKey:Q_KEY_CODE_ALT];
866 } else {
867 [self toggleKey:Q_KEY_CODE_META_L];
868 }
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700869 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900870 break;
871
872 case kVK_RightCommand:
873 if (isMouseGrabbed &&
874 !!(modifiers & NSEventModifierFlagCommand)) {
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +0900875 if (swap_opt_cmd) {
876 [self toggleKey:Q_KEY_CODE_ALT_R];
877 } else {
878 [self toggleKey:Q_KEY_CODE_META_R];
879 }
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900880 }
881 break;
Ian McKellar via Qemu-develaf8862b2017-05-26 16:38:16 -0700882 }
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900883 return true;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700884 case NSEventTypeKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000885 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000886
Peter Maydell88959192013-12-08 22:59:02 +0000887 // forward command key combos to the host UI unless the mouse is grabbed
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700888 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell60105d72019-02-25 10:24:31 +0000889 return false;
thsc304f7e2008-01-22 23:25:15 +0000890 }
891
892 // default
thsc304f7e2008-01-22 23:25:15 +0000893
John Arbuckle5929e362017-11-07 10:14:14 +0000894 // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700895 if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
John Arbuckle5929e362017-11-07 10:14:14 +0000896 NSString *keychar = [event charactersIgnoringModifiers];
897 if ([keychar length] == 1) {
898 char key = [keychar characterAtIndex:0];
899 switch (key) {
thsc304f7e2008-01-22 23:25:15 +0000900
John Arbuckle5929e362017-11-07 10:14:14 +0000901 // enable graphic console
902 case '1' ... '9':
903 console_select(key - '0' - 1); /* ascii math */
Peter Maydell60105d72019-02-25 10:24:31 +0000904 return true;
John Arbuckle5929e362017-11-07 10:14:14 +0000905
906 // release the mouse grab
907 case 'g':
908 [self ungrabMouse];
Peter Maydell60105d72019-02-25 10:24:31 +0000909 return true;
John Arbuckle5929e362017-11-07 10:14:14 +0000910 }
thsc304f7e2008-01-22 23:25:15 +0000911 }
Peter Maydellef2088f2017-11-07 10:14:14 +0000912 }
thsc304f7e2008-01-22 23:25:15 +0000913
Peter Maydellef2088f2017-11-07 10:14:14 +0000914 if (qemu_console_is_graphic(NULL)) {
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900915 qkbd_state_key_event(kbd, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000916 } else {
John Arbuckle9c3a4182017-11-07 10:14:14 +0000917 [self handleMonitorInput: event];
thsc304f7e2008-01-22 23:25:15 +0000918 }
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900919 return true;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700920 case NSEventTypeKeyUp:
thsc304f7e2008-01-22 23:25:15 +0000921 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000922
923 // don't pass the guest a spurious key-up if we treated this
924 // command-key combo as a host UI action
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700925 if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
Peter Maydell60105d72019-02-25 10:24:31 +0000926 return true;
Peter Maydell88959192013-12-08 22:59:02 +0000927 }
928
Peter Maydell68c0aa62013-04-17 09:16:35 +0000929 if (qemu_console_is_graphic(NULL)) {
Akihiko Odaki6d73bb62021-03-10 23:46:02 +0900930 qkbd_state_key_event(kbd, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000931 }
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900932 return true;
Brendan Shanks4ba967a2017-04-24 23:29:52 -0700933 case NSEventTypeScrollWheel:
John Arbuckleae7313e2018-01-08 13:07:07 -0500934 /*
935 * Send wheel events to the guest regardless of window focus.
936 * This is in-line with standard Mac OS X UI behaviour.
937 */
938
939 /* Determine if this is a scroll up or scroll down event */
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900940 if ([event deltaY] != 0) {
941 button = ([event deltaY] > 0) ?
John Arbuckledc3c89d2018-07-09 11:02:35 -0400942 INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900943 } else if ([event deltaX] != 0) {
944 button = ([event deltaX] > 0) ?
Dmitry Petrovd70a5de2022-01-08 16:39:44 +0100945 INPUT_BUTTON_WHEEL_LEFT : INPUT_BUTTON_WHEEL_RIGHT;
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900946 } else {
947 /*
948 * We shouldn't have got a scroll event when deltaY and delta Y
949 * are zero, hence no harm in dropping the event
950 */
951 return true;
John Arbuckledc3c89d2018-07-09 11:02:35 -0400952 }
Dmitry Petrovd70a5de2022-01-08 16:39:44 +0100953
Akihiko Odaki0f7be472024-02-24 21:43:33 +0900954 qemu_input_queue_btn(dcl.con, button, true);
955 qemu_input_event_sync();
956 qemu_input_queue_btn(dcl.con, button, false);
957 qemu_input_event_sync();
958
959 return true;
thsc304f7e2008-01-22 23:25:15 +0000960 default:
Peter Maydell60105d72019-02-25 10:24:31 +0000961 return false;
thsc304f7e2008-01-22 23:25:15 +0000962 }
963}
964
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900965- (void) handleMouseEvent:(NSEvent *)event button:(InputButton)button down:(bool)down
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +0900966{
967 if (!isMouseGrabbed) {
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900968 return;
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +0900969 }
970
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900971 with_bql(^{
972 qemu_input_queue_btn(dcl.con, button, down);
973 });
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +0900974
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900975 [self handleMouseEvent:event];
976}
977
978- (void) handleMouseEvent:(NSEvent *)event
979{
980 if (!isMouseGrabbed) {
981 return;
982 }
983
984 with_bql(^{
985 if (isAbsoluteEnabled) {
986 CGFloat d = (CGFloat)screen.height / [self frame].size.height;
987 NSPoint p = [event locationInWindow];
988
989 /* Note that the origin for Cocoa mouse coords is bottom left, not top left. */
990 qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x * d, 0, screen.width);
991 qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y * d, 0, screen.height);
992 } else {
993 qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, [event deltaX]);
994 qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, [event deltaY]);
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +0900995 }
Akihiko Odaki91aa5082024-02-24 21:43:37 +0900996
997 qemu_input_event_sync();
998 });
999}
1000
1001- (void) mouseExited:(NSEvent *)event
1002{
1003 if (isAbsoluteEnabled && isMouseGrabbed) {
1004 [self ungrabMouse];
1005 }
1006}
1007
1008- (void) mouseEntered:(NSEvent *)event
1009{
1010 if (isAbsoluteEnabled && !isMouseGrabbed) {
1011 [self grabMouse];
1012 }
1013}
1014
1015- (void) mouseMoved:(NSEvent *)event
1016{
1017 [self handleMouseEvent:event];
1018}
1019
1020- (void) mouseDown:(NSEvent *)event
1021{
1022 [self handleMouseEvent:event button:INPUT_BUTTON_LEFT down:true];
1023}
1024
1025- (void) rightMouseDown:(NSEvent *)event
1026{
1027 [self handleMouseEvent:event button:INPUT_BUTTON_RIGHT down:true];
1028}
1029
1030- (void) otherMouseDown:(NSEvent *)event
1031{
1032 [self handleMouseEvent:event button:INPUT_BUTTON_MIDDLE down:true];
1033}
1034
1035- (void) mouseDragged:(NSEvent *)event
1036{
1037 [self handleMouseEvent:event];
1038}
1039
1040- (void) rightMouseDragged:(NSEvent *)event
1041{
1042 [self handleMouseEvent:event];
1043}
1044
1045- (void) otherMouseDragged:(NSEvent *)event
1046{
1047 [self handleMouseEvent:event];
1048}
1049
1050- (void) mouseUp:(NSEvent *)event
1051{
1052 if (!isMouseGrabbed) {
1053 [self grabMouse];
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001054 }
1055
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001056 [self handleMouseEvent:event button:INPUT_BUTTON_LEFT down:false];
1057}
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001058
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001059- (void) rightMouseUp:(NSEvent *)event
1060{
1061 [self handleMouseEvent:event button:INPUT_BUTTON_RIGHT down:false];
1062}
1063
1064- (void) otherMouseUp:(NSEvent *)event
1065{
1066 [self handleMouseEvent:event button:INPUT_BUTTON_MIDDLE down:false];
Akihiko Odakiaf4efbc2024-02-24 21:43:32 +09001067}
1068
thsc304f7e2008-01-22 23:25:15 +00001069- (void) grabMouse
1070{
1071 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
1072
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001073 if (qemu_name)
1074 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)", qemu_name]];
1075 else
1076 [normalWindow setTitle:@"QEMU - (Press " UC_CTRL_KEY " " UC_ALT_KEY " G to release Mouse)"];
Peter Maydell13aefd32014-06-23 10:35:25 +01001077 [self hideCursor];
Akihiko Odakid1929062021-02-23 00:07:14 +09001078 CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled);
Peter Maydell49b9bd42013-12-08 22:59:03 +00001079 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +00001080}
1081
1082- (void) ungrabMouse
1083{
1084 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
1085
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001086 if (qemu_name)
1087 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
1088 else
1089 [normalWindow setTitle:@"QEMU"];
Peter Maydell13aefd32014-06-23 10:35:25 +01001090 [self unhideCursor];
Akihiko Odakid1929062021-02-23 00:07:14 +09001091 CGAssociateMouseAndMouseCursorPosition(TRUE);
Peter Maydell49b9bd42013-12-08 22:59:03 +00001092 isMouseGrabbed = FALSE;
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001093 [self raiseAllButtons];
thsc304f7e2008-01-22 23:25:15 +00001094}
1095
Akihiko Odakid1929062021-02-23 00:07:14 +09001096- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {
1097 isAbsoluteEnabled = tIsAbsoluteEnabled;
1098 if (isMouseGrabbed) {
1099 CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled);
1100 }
1101}
Peter Maydell49b9bd42013-12-08 22:59:03 +00001102- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +00001103- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
thsc304f7e2008-01-22 23:25:15 +00001104- (QEMUScreen) gscreen {return screen;}
John Arbuckle3b178b72015-09-25 23:14:00 +01001105
1106/*
1107 * Makes the target think all down keys are being released.
1108 * This prevents a stuck key problem, since we will not see
1109 * key up events for those keys after we have lost focus.
1110 */
1111- (void) raiseAllKeys
1112{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001113 with_bql(^{
Akihiko Odaki6d73bb62021-03-10 23:46:02 +09001114 qkbd_state_lift_all_keys(kbd);
Peter Maydell31819e92019-02-25 10:24:27 +00001115 });
John Arbuckle3b178b72015-09-25 23:14:00 +01001116}
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001117
1118- (void) raiseAllButtons
1119{
1120 with_bql(^{
1121 qemu_input_queue_btn(dcl.con, INPUT_BUTTON_LEFT, false);
1122 qemu_input_queue_btn(dcl.con, INPUT_BUTTON_RIGHT, false);
1123 qemu_input_queue_btn(dcl.con, INPUT_BUTTON_MIDDLE, false);
1124 });
1125}
bellard5b0753e2005-03-01 21:37:28 +00001126@end
1127
1128
thsc304f7e2008-01-22 23:25:15 +00001129
bellard5b0753e2005-03-01 21:37:28 +00001130/*
1131 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +00001132 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +00001133 ------------------------------------------------------
1134*/
thsc304f7e2008-01-22 23:25:15 +00001135@interface QemuCocoaAppController : NSObject
John Arbuckled9bc14f2015-09-25 23:13:59 +01001136 <NSWindowDelegate, NSApplicationDelegate>
bellard5b0753e2005-03-01 21:37:28 +00001137{
1138}
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001139- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +00001140- (void)showQEMUDoc:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001141- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +01001142- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +01001143- (void)pauseQEMU:(id)sender;
1144- (void)resumeQEMU:(id)sender;
1145- (void)displayPause;
1146- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +01001147- (void)restartQEMU:(id)sender;
1148- (void)powerDownQEMU:(id)sender;
John Arbuckle693a3e02015-06-19 10:53:27 +01001149- (void)ejectDeviceMedia:(id)sender;
1150- (void)changeDeviceMedia:(id)sender;
John Arbuckled9bc14f2015-09-25 23:13:59 +01001151- (BOOL)verifyQuit;
John Arbucklef4747902016-03-23 14:26:17 +00001152- (void)openDocumentation:(NSString *)filename;
Programmingkid9e8204b2016-08-15 22:11:06 -04001153- (IBAction) do_about_menu_item: (id) sender;
John Arbucklee47ec1a2017-06-13 23:17:38 -04001154- (void)adjustSpeed:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +00001155@end
1156
thsc304f7e2008-01-22 23:25:15 +00001157@implementation QemuCocoaAppController
1158- (id) init
1159{
1160 COCOA_DEBUG("QemuCocoaAppController: init\n");
1161
1162 self = [super init];
1163 if (self) {
1164
1165 // create a view and add it to the window
1166 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
1167 if(!cocoaView) {
Akihiko Odaki43137392021-02-23 22:11:06 +09001168 error_report("(cocoa) can't create a view");
thsc304f7e2008-01-22 23:25:15 +00001169 exit(1);
1170 }
1171
1172 // create a window
1173 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001174 styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable
thsc304f7e2008-01-22 23:25:15 +00001175 backing:NSBackingStoreBuffered defer:NO];
1176 if(!normalWindow) {
Akihiko Odaki43137392021-02-23 22:11:06 +09001177 error_report("(cocoa) can't create window");
thsc304f7e2008-01-22 23:25:15 +00001178 exit(1);
1179 }
1180 [normalWindow setAcceptsMouseMovedEvents:YES];
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001181 [normalWindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
1182 [normalWindow setTitle:qemu_name ? [NSString stringWithFormat:@"QEMU %s", qemu_name] : @"QEMU"];
thsc304f7e2008-01-22 23:25:15 +00001183 [normalWindow setContentView:cocoaView];
1184 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +00001185 [normalWindow center];
John Arbuckled9bc14f2015-09-25 23:13:59 +01001186 [normalWindow setDelegate: self];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001187
1188 /* Used for displaying pause on the screen */
1189 pauseLabel = [NSTextField new];
1190 [pauseLabel setBezeled:YES];
1191 [pauseLabel setDrawsBackground:YES];
1192 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
1193 [pauseLabel setEditable:NO];
1194 [pauseLabel setSelectable:NO];
1195 [pauseLabel setStringValue: @"Paused"];
1196 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
1197 [pauseLabel setTextColor: [NSColor blackColor]];
1198 [pauseLabel sizeToFit];
thsc304f7e2008-01-22 23:25:15 +00001199 }
1200 return self;
1201}
1202
1203- (void) dealloc
1204{
1205 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
1206
1207 if (cocoaView)
1208 [cocoaView release];
1209 [super dealloc];
1210}
1211
bellard5b0753e2005-03-01 21:37:28 +00001212- (void)applicationDidFinishLaunching: (NSNotification *) note
1213{
thsc304f7e2008-01-22 23:25:15 +00001214 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
Hikaru Nishidadff742a2019-10-15 10:07:34 +09001215 allow_events = true;
bellard5b0753e2005-03-01 21:37:28 +00001216}
1217
1218- (void)applicationWillTerminate:(NSNotification *)aNotification
1219{
thsc304f7e2008-01-22 23:25:15 +00001220 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
1221
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001222 with_bql(^{
Akihiko Odaki2910abd2022-05-29 17:25:08 +09001223 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
1224 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
1225 });
Akihiko Odaki40c01932021-02-19 20:16:52 +09001226
1227 /*
1228 * Sleep here, because returning will cause OSX to kill us
1229 * immediately; the QEMU main loop will handle the shutdown
1230 * request and terminate the process.
1231 */
1232 [NSThread sleepForTimeInterval:INFINITY];
bellard5b0753e2005-03-01 21:37:28 +00001233}
1234
Andreas Färber41ea49b2009-12-14 22:13:27 +01001235- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
1236{
1237 return YES;
1238}
1239
John Arbuckled9bc14f2015-09-25 23:13:59 +01001240- (NSApplicationTerminateReply)applicationShouldTerminate:
1241 (NSApplication *)sender
1242{
1243 COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
1244 return [self verifyQuit];
1245}
1246
Akihiko Odaki15280e82021-06-16 23:19:10 +09001247- (void)windowDidChangeScreen:(NSNotification *)notification
1248{
1249 [cocoaView updateUIInfo];
1250}
1251
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001252- (void)windowDidEnterFullScreen:(NSNotification *)notification
1253{
1254 [cocoaView grabMouse];
1255}
1256
1257- (void)windowDidExitFullScreen:(NSNotification *)notification
1258{
1259 [cocoaView resizeWindow];
1260 [cocoaView ungrabMouse];
1261}
1262
Akihiko Odaki15280e82021-06-16 23:19:10 +09001263- (void)windowDidResize:(NSNotification *)notification
1264{
Akihiko Odakifcb03de2024-02-24 21:43:35 +09001265 [cocoaView updateBounds];
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001266 [cocoaView frameUpdated];
Akihiko Odaki15280e82021-06-16 23:19:10 +09001267}
1268
John Arbuckled9bc14f2015-09-25 23:13:59 +01001269/* Called when the user clicks on a window's close button */
1270- (BOOL)windowShouldClose:(id)sender
1271{
1272 COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
1273 [NSApp terminate: sender];
1274 /* If the user allows the application to quit then the call to
1275 * NSApp terminate will never return. If we get here then the user
1276 * cancelled the quit, so we should return NO to not permit the
1277 * closing of this window.
1278 */
1279 return NO;
1280}
1281
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001282- (NSApplicationPresentationOptions) window:(NSWindow *)window
1283 willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions;
1284
1285{
1286 return (proposedOptions & ~(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)) |
1287 NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar;
1288}
1289
Akihiko Odaki9d9bc7d2023-02-28 16:09:46 +09001290/*
1291 * Called when QEMU goes into the background. Note that
1292 * [-NSWindowDelegate windowDidResignKey:] is used here instead of
1293 * [-NSApplicationDelegate applicationWillResignActive:] because it cannot
1294 * detect that the window loses focus when the deck is clicked on macOS 13.2.1.
1295 */
1296- (void) windowDidResignKey: (NSNotification *)aNotification
John Arbuckle3b178b72015-09-25 23:14:00 +01001297{
Akihiko Odaki9d9bc7d2023-02-28 16:09:46 +09001298 COCOA_DEBUG("%s\n", __func__);
Carwyn Ellis69221df2022-01-02 17:41:53 +00001299 [cocoaView ungrabMouse];
John Arbuckle3b178b72015-09-25 23:14:00 +01001300 [cocoaView raiseAllKeys];
1301}
1302
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001303/* We abstract the method called by the Enter Fullscreen menu item
1304 * because Mac OS 10.7 and higher disables it. This is because of the
1305 * menu item's old selector's name toggleFullScreen:
1306 */
1307- (void) doToggleFullScreen:(id)sender
1308{
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001309 [normalWindow toggleFullScreen:sender];
thsc304f7e2008-01-22 23:25:15 +00001310}
1311
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +09001312- (void) setFullGrab:(id)sender
1313{
1314 COCOA_DEBUG("QemuCocoaAppController: setFullGrab\n");
1315
1316 [cocoaView setFullGrab:sender];
1317}
1318
John Arbucklef4747902016-03-23 14:26:17 +00001319/* Tries to find then open the specified filename */
1320- (void) openDocumentation: (NSString *) filename
1321{
1322 /* Where to look for local files */
Roman Bolshakov8d6fda82021-01-09 00:38:15 +03001323 NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"docs/"};
John Arbucklef4747902016-03-23 14:26:17 +00001324 NSString *full_file_path;
Roman Bolshakov1ff5a062021-01-02 18:07:21 +03001325 NSURL *full_file_url;
John Arbucklef4747902016-03-23 14:26:17 +00001326
1327 /* iterate thru the possible paths until the file is found */
1328 int index;
1329 for (index = 0; index < ARRAY_SIZE(path_array); index++) {
1330 full_file_path = [[NSBundle mainBundle] executablePath];
1331 full_file_path = [full_file_path stringByDeletingLastPathComponent];
1332 full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
1333 path_array[index], filename];
Roman Bolshakov1ff5a062021-01-02 18:07:21 +03001334 full_file_url = [NSURL fileURLWithPath: full_file_path
1335 isDirectory: false];
1336 if ([[NSWorkspace sharedWorkspace] openURL: full_file_url] == YES) {
John Arbucklef4747902016-03-23 14:26:17 +00001337 return;
1338 }
1339 }
1340
1341 /* If none of the paths opened a file */
1342 NSBeep();
1343 QEMU_Alert(@"Failed to open file");
1344}
1345
thsc304f7e2008-01-22 23:25:15 +00001346- (void)showQEMUDoc:(id)sender
1347{
1348 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
1349
Peter Maydell1879f242020-02-28 15:36:16 +00001350 [self openDocumentation: @"index.html"];
thsc304f7e2008-01-22 23:25:15 +00001351}
1352
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001353/* Stretches video to fit host monitor size */
1354- (void)zoomToFit:(id) sender
1355{
1356 stretch_video = !stretch_video;
1357 if (stretch_video == true) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001358 [sender setState: NSControlStateValueOn];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001359 } else {
Akihiko Odaki91aa5082024-02-24 21:43:37 +09001360 [cocoaView resizeWindow];
Brendan Shanks5e246002019-01-31 23:12:25 -08001361 [sender setState: NSControlStateValueOff];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001362 }
1363}
bellard5b0753e2005-03-01 21:37:28 +00001364
Carwyn Ellise28a9092023-11-10 16:17:29 +00001365- (void)toggleZoomInterpolation:(id) sender
1366{
1367 if (zoom_interpolation == kCGInterpolationNone) {
1368 zoom_interpolation = kCGInterpolationLow;
1369 [sender setState: NSControlStateValueOn];
1370 } else {
1371 zoom_interpolation = kCGInterpolationNone;
1372 [sender setState: NSControlStateValueOff];
1373 }
1374}
1375
Programmingkidb4c6a112015-05-19 09:11:18 +01001376/* Displays the console on the screen */
1377- (void)displayConsole:(id)sender
1378{
1379 console_select([sender tag]);
1380}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001381
1382/* Pause the guest */
1383- (void)pauseQEMU:(id)sender
1384{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001385 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001386 qmp_stop(NULL);
1387 });
John Arbuckle8524f1c2015-06-19 10:53:27 +01001388 [sender setEnabled: NO];
1389 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1390 [self displayPause];
1391}
1392
1393/* Resume running the guest operating system */
1394- (void)resumeQEMU:(id) sender
1395{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001396 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001397 qmp_cont(NULL);
1398 });
John Arbuckle8524f1c2015-06-19 10:53:27 +01001399 [sender setEnabled: NO];
1400 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1401 [self removePause];
1402}
1403
1404/* Displays the word pause on the screen */
1405- (void)displayPause
1406{
1407 /* Coordinates have to be calculated each time because the window can change its size */
1408 int xCoord, yCoord, width, height;
Akihiko Odaki1a4b64a2024-02-24 21:43:36 +09001409 xCoord = ([cocoaView frame].size.width - [pauseLabel frame].size.width)/2;
1410 yCoord = [cocoaView frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
John Arbuckle8524f1c2015-06-19 10:53:27 +01001411 width = [pauseLabel frame].size.width;
1412 height = [pauseLabel frame].size.height;
1413 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1414 [cocoaView addSubview: pauseLabel];
1415}
1416
1417/* Removes the word pause from the screen */
1418- (void)removePause
1419{
1420 [pauseLabel removeFromSuperview];
1421}
1422
John Arbuckle27074612015-06-19 10:53:27 +01001423/* Restarts QEMU */
1424- (void)restartQEMU:(id)sender
1425{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001426 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001427 qmp_system_reset(NULL);
1428 });
John Arbuckle27074612015-06-19 10:53:27 +01001429}
1430
1431/* Powers down QEMU */
1432- (void)powerDownQEMU:(id)sender
1433{
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001434 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001435 qmp_system_powerdown(NULL);
1436 });
John Arbuckle27074612015-06-19 10:53:27 +01001437}
1438
John Arbuckle693a3e02015-06-19 10:53:27 +01001439/* Ejects the media.
1440 * Uses sender's tag to figure out the device to eject.
1441 */
1442- (void)ejectDeviceMedia:(id)sender
1443{
1444 NSString * drive;
1445 drive = [sender representedObject];
1446 if(drive == nil) {
1447 NSBeep();
1448 QEMU_Alert(@"Failed to find drive to eject!");
1449 return;
1450 }
1451
Peter Maydell31819e92019-02-25 10:24:27 +00001452 __block Error *err = NULL;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001453 with_bql(^{
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001454 qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding],
1455 NULL, false, false, &err);
Peter Maydell31819e92019-02-25 10:24:27 +00001456 });
John Arbuckle693a3e02015-06-19 10:53:27 +01001457 handleAnyDeviceErrors(err);
1458}
1459
1460/* Displays a dialog box asking the user to select an image file to load.
1461 * Uses sender's represented object value to figure out which drive to use.
1462 */
1463- (void)changeDeviceMedia:(id)sender
1464{
1465 /* Find the drive name */
1466 NSString * drive;
1467 drive = [sender representedObject];
1468 if(drive == nil) {
1469 NSBeep();
1470 QEMU_Alert(@"Could not find drive!");
1471 return;
1472 }
1473
1474 /* Display the file open dialog */
1475 NSOpenPanel * openPanel;
1476 openPanel = [NSOpenPanel openPanel];
1477 [openPanel setCanChooseFiles: YES];
1478 [openPanel setAllowsMultipleSelection: NO];
Peter Maydellb5725382018-05-29 19:15:23 +01001479 if([openPanel runModal] == NSModalResponseOK) {
John Arbuckle693a3e02015-06-19 10:53:27 +01001480 NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1481 if(file == nil) {
1482 NSBeep();
1483 QEMU_Alert(@"Failed to convert URL to file path!");
1484 return;
1485 }
1486
Peter Maydell31819e92019-02-25 10:24:27 +00001487 __block Error *err = NULL;
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001488 with_bql(^{
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001489 qmp_blockdev_change_medium([drive cStringUsingEncoding:
Peter Maydell31819e92019-02-25 10:24:27 +00001490 NSASCIIStringEncoding],
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001491 NULL,
Peter Maydell31819e92019-02-25 10:24:27 +00001492 [file cStringUsingEncoding:
1493 NSASCIIStringEncoding],
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001494 "raw",
Denis V. Lunev80dd5af2022-04-13 01:18:46 +03001495 true, false,
Peter Maydell31819e92019-02-25 10:24:27 +00001496 false, 0,
1497 &err);
1498 });
John Arbuckle693a3e02015-06-19 10:53:27 +01001499 handleAnyDeviceErrors(err);
1500 }
1501}
1502
John Arbuckled9bc14f2015-09-25 23:13:59 +01001503/* Verifies if the user really wants to quit */
1504- (BOOL)verifyQuit
1505{
1506 NSAlert *alert = [NSAlert new];
1507 [alert autorelease];
1508 [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1509 [alert addButtonWithTitle: @"Cancel"];
1510 [alert addButtonWithTitle: @"Quit"];
1511 if([alert runModal] == NSAlertSecondButtonReturn) {
1512 return YES;
1513 } else {
1514 return NO;
1515 }
1516}
1517
Programmingkid9e8204b2016-08-15 22:11:06 -04001518/* The action method for the About menu item */
1519- (IBAction) do_about_menu_item: (id) sender
1520{
Akihiko Odaki99eb3132022-02-27 13:22:41 +09001521 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1522 char *icon_path_c = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/512x512/apps/qemu.png");
1523 NSString *icon_path = [NSString stringWithUTF8String:icon_path_c];
1524 g_free(icon_path_c);
1525 NSImage *icon = [[NSImage alloc] initWithContentsOfFile:icon_path];
1526 NSString *version = @"QEMU emulator version " QEMU_FULL_VERSION;
1527 NSString *copyright = @QEMU_COPYRIGHT;
1528 NSDictionary *options;
1529 if (icon) {
1530 options = @{
1531 NSAboutPanelOptionApplicationIcon : icon,
1532 NSAboutPanelOptionApplicationVersion : version,
1533 @"Copyright" : copyright,
1534 };
1535 [icon release];
1536 } else {
1537 options = @{
1538 NSAboutPanelOptionApplicationVersion : version,
1539 @"Copyright" : copyright,
1540 };
Akihiko Odakia0f973f2021-03-09 21:22:26 +09001541 }
Akihiko Odaki99eb3132022-02-27 13:22:41 +09001542 [NSApp orderFrontStandardAboutPanelWithOptions:options];
1543 [pool release];
Programmingkid9e8204b2016-08-15 22:11:06 -04001544}
1545
John Arbucklee47ec1a2017-06-13 23:17:38 -04001546/* Used by the Speed menu items */
1547- (void)adjustSpeed:(id)sender
1548{
1549 int throttle_pct; /* throttle percentage */
1550 NSMenu *menu;
1551
1552 menu = [sender menu];
1553 if (menu != nil)
1554 {
1555 /* Unselect the currently selected item */
1556 for (NSMenuItem *item in [menu itemArray]) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001557 if (item.state == NSControlStateValueOn) {
1558 [item setState: NSControlStateValueOff];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001559 break;
1560 }
1561 }
1562 }
1563
1564 // check the menu item
Brendan Shanks5e246002019-01-31 23:12:25 -08001565 [sender setState: NSControlStateValueOn];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001566
1567 // get the throttle percentage
1568 throttle_pct = [sender tag];
1569
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001570 with_bql(^{
Peter Maydell31819e92019-02-25 10:24:27 +00001571 cpu_throttle_set(throttle_pct);
1572 });
John Arbucklee47ec1a2017-06-13 23:17:38 -04001573 COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
1574}
1575
Programmingkidb4c6a112015-05-19 09:11:18 +01001576@end
bellard5b0753e2005-03-01 21:37:28 +00001577
Peter Maydell61a2ed42019-02-25 10:24:32 +00001578@interface QemuApplication : NSApplication
1579@end
1580
1581@implementation QemuApplication
1582- (void)sendEvent:(NSEvent *)event
1583{
1584 COCOA_DEBUG("QemuApplication: sendEvent\n");
Peter Maydell55888402019-02-25 10:24:33 +00001585 if (![cocoaView handleEvent:event]) {
1586 [super sendEvent: event];
1587 }
Peter Maydell61a2ed42019-02-25 10:24:32 +00001588}
1589@end
1590
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001591static void create_initial_menus(void)
1592{
thsc304f7e2008-01-22 23:25:15 +00001593 // Add menus
1594 NSMenu *menu;
1595 NSMenuItem *menuItem;
1596
bellard5b0753e2005-03-01 21:37:28 +00001597 [NSApp setMainMenu:[[NSMenu alloc] init]];
Akihiko Odaki5b6988c2022-02-14 18:13:20 +09001598 [NSApp setServicesMenu:[[NSMenu alloc] initWithTitle:@"Services"]];
bellard5b0753e2005-03-01 21:37:28 +00001599
thsc304f7e2008-01-22 23:25:15 +00001600 // Application menu
1601 menu = [[NSMenu alloc] initWithTitle:@""];
Programmingkid9e8204b2016-08-15 22:11:06 -04001602 [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU
thsc304f7e2008-01-22 23:25:15 +00001603 [menu addItem:[NSMenuItem separatorItem]]; //Separator
Akihiko Odaki5b6988c2022-02-14 18:13:20 +09001604 menuItem = [menu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
1605 [menuItem setSubmenu:[NSApp servicesMenu]];
1606 [menu addItem:[NSMenuItem separatorItem]];
thsc304f7e2008-01-22 23:25:15 +00001607 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1608 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
Brendan Shanks4ba967a2017-04-24 23:29:52 -07001609 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
thsc304f7e2008-01-22 23:25:15 +00001610 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1611 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1612 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1613 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1614 [menuItem setSubmenu:menu];
1615 [[NSApp mainMenu] addItem:menuItem];
1616 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001617
John Arbuckle8524f1c2015-06-19 10:53:27 +01001618 // Machine menu
1619 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1620 [menu setAutoenablesItems: NO];
1621 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1622 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1623 [menu addItem: menuItem];
1624 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001625 [menu addItem: [NSMenuItem separatorItem]];
1626 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1627 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001628 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1629 [menuItem setSubmenu:menu];
1630 [[NSApp mainMenu] addItem:menuItem];
1631
thsc304f7e2008-01-22 23:25:15 +00001632 // View menu
1633 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001634 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
Carwyn Ellis5ec08982023-10-27 16:49:20 +01001635 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease];
1636 [menuItem setState: stretch_video ? NSControlStateValueOn : NSControlStateValueOff];
1637 [menu addItem: menuItem];
Carwyn Ellise28a9092023-11-10 16:17:29 +00001638 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Zoom Interpolation" action:@selector(toggleZoomInterpolation:) keyEquivalent:@""] autorelease];
1639 [menuItem setState: zoom_interpolation == kCGInterpolationLow ? NSControlStateValueOn : NSControlStateValueOff];
1640 [menu addItem: menuItem];
thsc304f7e2008-01-22 23:25:15 +00001641 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1642 [menuItem setSubmenu:menu];
1643 [[NSApp mainMenu] addItem:menuItem];
1644
John Arbucklee47ec1a2017-06-13 23:17:38 -04001645 // Speed menu
1646 menu = [[NSMenu alloc] initWithTitle:@"Speed"];
1647
1648 // Add the rest of the Speed menu items
1649 int p, percentage, throttle_pct;
1650 for (p = 10; p >= 0; p--)
1651 {
1652 percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
1653
1654 menuItem = [[[NSMenuItem alloc]
1655 initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
1656
1657 if (percentage == 100) {
Brendan Shanks5e246002019-01-31 23:12:25 -08001658 [menuItem setState: NSControlStateValueOn];
John Arbucklee47ec1a2017-06-13 23:17:38 -04001659 }
1660
1661 /* Calculate the throttle percentage */
1662 throttle_pct = -1 * percentage + 100;
1663
1664 [menuItem setTag: throttle_pct];
1665 [menu addItem: menuItem];
1666 }
1667 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
1668 [menuItem setSubmenu:menu];
1669 [[NSApp mainMenu] addItem:menuItem];
1670
thsc304f7e2008-01-22 23:25:15 +00001671 // Window menu
1672 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1673 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1674 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1675 [menuItem setSubmenu:menu];
1676 [[NSApp mainMenu] addItem:menuItem];
1677 [NSApp setWindowsMenu:menu];
1678
1679 // Help menu
1680 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1681 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
thsc304f7e2008-01-22 23:25:15 +00001682 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1683 [menuItem setSubmenu:menu];
1684 [[NSApp mainMenu] addItem:menuItem];
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001685}
1686
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001687/* Returns a name for a given console */
1688static NSString * getConsoleName(QemuConsole * console)
1689{
Akihiko Odakica511602022-02-15 09:03:05 +01001690 g_autofree char *label = qemu_console_get_label(console);
1691
1692 return [NSString stringWithUTF8String:label];
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001693}
1694
1695/* Add an entry to the View menu for each console */
1696static void add_console_menu_entries(void)
1697{
1698 NSMenu *menu;
1699 NSMenuItem *menuItem;
1700 int index = 0;
1701
1702 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1703
1704 [menu addItem:[NSMenuItem separatorItem]];
1705
1706 while (qemu_console_lookup_by_index(index) != NULL) {
1707 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1708 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1709 [menuItem setTag: index];
1710 [menu addItem: menuItem];
1711 index++;
1712 }
1713}
1714
1715/* Make menu items for all removable devices.
1716 * Each device is given an 'Eject' and 'Change' menu item.
1717 */
1718static void addRemovableDevicesMenuItems(void)
1719{
1720 NSMenu *menu;
1721 NSMenuItem *menuItem;
1722 BlockInfoList *currentDevice, *pointerToFree;
1723 NSString *deviceName;
1724
1725 currentDevice = qmp_query_block(NULL);
1726 pointerToFree = currentDevice;
Peter Maydell8b00e4e2019-02-25 10:24:30 +00001727
1728 menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1729
1730 // Add a separator between related groups of menu items
1731 [menu addItem:[NSMenuItem separatorItem]];
1732
1733 // Set the attributes to the "Removable Media" menu item
1734 NSString *titleString = @"Removable Media";
1735 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1736 NSColor *newColor = [NSColor blackColor];
1737 NSFontManager *fontManager = [NSFontManager sharedFontManager];
1738 NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1739 traits:NSBoldFontMask|NSItalicFontMask
1740 weight:0
1741 size:14];
1742 [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1743 [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1744 [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1745
1746 // Add the "Removable Media" menu item
1747 menuItem = [NSMenuItem new];
1748 [menuItem setAttributedTitle: attString];
1749 [menuItem setEnabled: NO];
1750 [menu addItem: menuItem];
1751
1752 /* Loop through all the block devices in the emulator */
1753 while (currentDevice) {
1754 deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1755
1756 if(currentDevice->value->removable) {
1757 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1758 action: @selector(changeDeviceMedia:)
1759 keyEquivalent: @""];
1760 [menu addItem: menuItem];
1761 [menuItem setRepresentedObject: deviceName];
1762 [menuItem autorelease];
1763
1764 menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1765 action: @selector(ejectDeviceMedia:)
1766 keyEquivalent: @""];
1767 [menu addItem: menuItem];
1768 [menuItem setRepresentedObject: deviceName];
1769 [menuItem autorelease];
1770 }
1771 currentDevice = currentDevice->next;
1772 }
1773 qapi_free_BlockInfoList(pointerToFree);
1774}
1775
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001776@interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner>
1777@end
1778
1779@implementation QemuCocoaPasteboardTypeOwner
1780
1781- (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSPasteboardType)type
1782{
1783 if (type != NSPasteboardTypeString) {
1784 return;
1785 }
1786
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001787 with_bql(^{
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001788 QemuClipboardInfo *info = qemu_clipboard_info_ref(cbinfo);
1789 qemu_event_reset(&cbevent);
1790 qemu_clipboard_request(info, QEMU_CLIPBOARD_TYPE_TEXT);
1791
1792 while (info == cbinfo &&
1793 info->types[QEMU_CLIPBOARD_TYPE_TEXT].available &&
1794 info->types[QEMU_CLIPBOARD_TYPE_TEXT].data == NULL) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001795 bql_unlock();
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001796 qemu_event_wait(&cbevent);
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001797 bql_lock();
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001798 }
1799
1800 if (info == cbinfo) {
1801 NSData *data = [[NSData alloc] initWithBytes:info->types[QEMU_CLIPBOARD_TYPE_TEXT].data
1802 length:info->types[QEMU_CLIPBOARD_TYPE_TEXT].size];
1803 [sender setData:data forType:NSPasteboardTypeString];
1804 [data release];
1805 }
1806
1807 qemu_clipboard_info_unref(info);
1808 });
1809}
1810
1811@end
1812
1813static QemuCocoaPasteboardTypeOwner *cbowner;
1814
1815static void cocoa_clipboard_notify(Notifier *notifier, void *data);
1816static void cocoa_clipboard_request(QemuClipboardInfo *info,
1817 QemuClipboardType type);
1818
1819static QemuClipboardPeer cbpeer = {
1820 .name = "cocoa",
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001821 .notifier = { .notify = cocoa_clipboard_notify },
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001822 .request = cocoa_clipboard_request
1823};
1824
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001825static void cocoa_clipboard_update_info(QemuClipboardInfo *info)
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001826{
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001827 if (info->owner == &cbpeer || info->selection != QEMU_CLIPBOARD_SELECTION_CLIPBOARD) {
1828 return;
1829 }
1830
1831 if (info != cbinfo) {
1832 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1833 qemu_clipboard_info_unref(cbinfo);
1834 cbinfo = qemu_clipboard_info_ref(info);
1835 cbchangecount = [[NSPasteboard generalPasteboard] declareTypes:@[NSPasteboardTypeString] owner:cbowner];
1836 [pool release];
1837 }
1838
1839 qemu_event_set(&cbevent);
1840}
1841
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001842static void cocoa_clipboard_notify(Notifier *notifier, void *data)
1843{
1844 QemuClipboardNotify *notify = data;
1845
1846 switch (notify->type) {
1847 case QEMU_CLIPBOARD_UPDATE_INFO:
1848 cocoa_clipboard_update_info(notify->info);
1849 return;
Marc-André Lureau505dbf92021-07-19 19:49:56 +04001850 case QEMU_CLIPBOARD_RESET_SERIAL:
1851 /* ignore */
1852 return;
Marc-André Lureau1b17f1e2021-07-19 19:42:15 +04001853 }
1854}
1855
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001856static void cocoa_clipboard_request(QemuClipboardInfo *info,
1857 QemuClipboardType type)
1858{
Akihiko Odaki8c0d8022022-06-15 06:21:31 +09001859 NSAutoreleasePool *pool;
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001860 NSData *text;
1861
1862 switch (type) {
1863 case QEMU_CLIPBOARD_TYPE_TEXT:
Akihiko Odaki8c0d8022022-06-15 06:21:31 +09001864 pool = [[NSAutoreleasePool alloc] init];
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001865 text = [[NSPasteboard generalPasteboard] dataForType:NSPasteboardTypeString];
1866 if (text) {
1867 qemu_clipboard_set_data(&cbpeer, info, type,
1868 [text length], [text bytes], true);
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001869 }
Akihiko Odaki8c0d8022022-06-15 06:21:31 +09001870 [pool release];
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001871 break;
1872 default:
1873 break;
1874 }
1875}
1876
Peter Maydell55888402019-02-25 10:24:33 +00001877/*
1878 * The startup process for the OSX/Cocoa UI is complicated, because
1879 * OSX insists that the UI runs on the initial main thread, and so we
Akihiko Odakibab6a302022-08-19 22:27:54 +09001880 * need to start a second thread which runs the qemu_default_main():
Peter Maydell55888402019-02-25 10:24:33 +00001881 * in main():
Akihiko Odakibab6a302022-08-19 22:27:54 +09001882 * in cocoa_display_init():
1883 * assign cocoa_main to qemu_main
1884 * create application, menus, etc
1885 * in cocoa_main():
1886 * create qemu-main thread
1887 * enter OSX run loop
Peter Maydell55888402019-02-25 10:24:33 +00001888 */
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001889
Peter Maydell55888402019-02-25 10:24:33 +00001890static void *call_qemu_main(void *opaque)
1891{
1892 int status;
1893
Akihiko Odakibab6a302022-08-19 22:27:54 +09001894 COCOA_DEBUG("Second thread: calling qemu_default_main()\n");
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001895 bql_lock();
Akihiko Odakibab6a302022-08-19 22:27:54 +09001896 status = qemu_default_main();
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001897 bql_unlock();
Akihiko Odakibab6a302022-08-19 22:27:54 +09001898 COCOA_DEBUG("Second thread: qemu_default_main() returned, exiting\n");
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001899 [cbowner release];
Peter Maydell55888402019-02-25 10:24:33 +00001900 exit(status);
1901}
1902
Philippe Mathieu-Daudéf9750332023-04-23 18:55:28 +02001903static int cocoa_main(void)
Akihiko Odakibab6a302022-08-19 22:27:54 +09001904{
Peter Maydell55888402019-02-25 10:24:33 +00001905 QemuThread thread;
1906
Akihiko Odakibab6a302022-08-19 22:27:54 +09001907 COCOA_DEBUG("Entered %s()\n", __func__);
Peter Maydellc6fd6c72019-02-25 10:24:29 +00001908
Stefan Hajnoczi195801d2024-01-02 10:35:25 -05001909 bql_unlock();
Peter Maydell55888402019-02-25 10:24:33 +00001910 qemu_thread_create(&thread, "qemu_main", call_qemu_main,
1911 NULL, QEMU_THREAD_DETACHED);
1912
thsc304f7e2008-01-22 23:25:15 +00001913 // Start the main event loop
Peter Maydell55888402019-02-25 10:24:33 +00001914 COCOA_DEBUG("Main thread: entering OSX run loop\n");
bellard5b0753e2005-03-01 21:37:28 +00001915 [NSApp run];
Akihiko Odakibab6a302022-08-19 22:27:54 +09001916 COCOA_DEBUG("Main thread: left OSX run loop, which should never happen\n");
ths3b46e622007-09-17 08:09:54 +00001917
Akihiko Odakibab6a302022-08-19 22:27:54 +09001918 abort();
bellard5b0753e2005-03-01 21:37:28 +00001919}
thsc304f7e2008-01-22 23:25:15 +00001920
1921
1922
1923#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001924static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001925 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001926{
1927 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1928
Peter Maydell55888402019-02-25 10:24:33 +00001929 dispatch_async(dispatch_get_main_queue(), ^{
Akihiko Odakifcb03de2024-02-24 21:43:35 +09001930 NSRect rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
Peter Maydell55888402019-02-25 10:24:33 +00001931 [cocoaView setNeedsDisplayInRect:rect];
1932 });
thsc304f7e2008-01-22 23:25:15 +00001933}
1934
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001935static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001936 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001937{
Peter Maydell55888402019-02-25 10:24:33 +00001938 pixman_image_t *image = surface->image;
thsc304f7e2008-01-22 23:25:15 +00001939
Peter Maydell6e657e62013-04-22 10:29:46 +00001940 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Peter Maydell55888402019-02-25 10:24:33 +00001941
1942 // The DisplaySurface will be freed as soon as this callback returns.
1943 // We take a reference to the underlying pixman image here so it does
1944 // not disappear from under our feet; the switchSurface method will
1945 // deref the old image when it is done with it.
1946 pixman_image_ref(image);
1947
1948 dispatch_async(dispatch_get_main_queue(), ^{
Peter Maydell8d65dee2022-02-24 10:13:29 +00001949 [cocoaView updateUIInfo];
Peter Maydell55888402019-02-25 10:24:33 +00001950 [cocoaView switchSurface:image];
1951 });
thsc304f7e2008-01-22 23:25:15 +00001952}
1953
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001954static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001955{
Peter Maydell6e657e62013-04-22 10:29:46 +00001956 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1957
thsc304f7e2008-01-22 23:25:15 +00001958 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
John Arbuckle468a8952015-10-13 21:51:18 +01001959 graphic_hw_update(NULL);
thsc304f7e2008-01-22 23:25:15 +00001960
Akihiko Odaki0337e412023-09-21 17:29:34 +09001961 if (qemu_input_is_absolute(dcl->con)) {
Peter Maydell55888402019-02-25 10:24:33 +00001962 dispatch_async(dispatch_get_main_queue(), ^{
1963 if (![cocoaView isAbsoluteEnabled]) {
1964 if ([cocoaView isMouseGrabbed]) {
1965 [cocoaView ungrabMouse];
1966 }
thsc304f7e2008-01-22 23:25:15 +00001967 }
Peter Maydell55888402019-02-25 10:24:33 +00001968 [cocoaView setAbsoluteEnabled:YES];
1969 });
thsc304f7e2008-01-22 23:25:15 +00001970 }
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09001971
1972 if (cbchangecount != [[NSPasteboard generalPasteboard] changeCount]) {
1973 qemu_clipboard_info_unref(cbinfo);
1974 cbinfo = qemu_clipboard_info_new(&cbpeer, QEMU_CLIPBOARD_SELECTION_CLIPBOARD);
1975 if ([[NSPasteboard generalPasteboard] availableTypeFromArray:@[NSPasteboardTypeString]]) {
1976 cbinfo->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
1977 }
1978 qemu_clipboard_update(cbinfo);
1979 cbchangecount = [[NSPasteboard generalPasteboard] changeCount];
1980 qemu_event_set(&cbevent);
1981 }
1982
Peter Maydell6e657e62013-04-22 10:29:46 +00001983 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001984}
1985
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01001986static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
thsc304f7e2008-01-22 23:25:15 +00001987{
Akihiko Odakibab6a302022-08-19 22:27:54 +09001988 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1989
thsc304f7e2008-01-22 23:25:15 +00001990 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1991
Akihiko Odakibab6a302022-08-19 22:27:54 +09001992 qemu_main = cocoa_main;
Peter Maydell55888402019-02-25 10:24:33 +00001993
Akihiko Odakibab6a302022-08-19 22:27:54 +09001994 // Pull this console process up to being a fully-fledged graphical
1995 // app with a menubar and Dock icon
1996 ProcessSerialNumber psn = { 0, kCurrentProcess };
1997 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1998
1999 [QemuApplication sharedApplication];
2000
Akihiko Odakibab6a302022-08-19 22:27:54 +09002001 // Create an Application controller
2002 QemuCocoaAppController *controller = [[QemuCocoaAppController alloc] init];
2003 [NSApp setDelegate:controller];
2004
Programmingkid43227af2015-05-19 09:11:17 +01002005 /* if fullscreen mode is to be used */
Gerd Hoffmann767f9bf2018-02-02 12:10:19 +01002006 if (opts->has_full_screen && opts->full_screen) {
Akihiko Odaki91aa5082024-02-24 21:43:37 +09002007 [normalWindow toggleFullScreen: nil];
Gustavo Noronha Silvaf844cdb2022-03-06 21:11:18 +09002008 }
2009 if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) {
Akihiko Odakibab6a302022-08-19 22:27:54 +09002010 [controller setFullGrab: nil];
Programmingkid43227af2015-05-19 09:11:17 +01002011 }
Carwyn Ellis69221df2022-01-02 17:41:53 +00002012
Gerd Hoffmann3487da62020-02-06 12:27:50 +01002013 if (opts->has_show_cursor && opts->show_cursor) {
2014 cursor_hide = 0;
2015 }
Gustavo Noronha Silva4797adc2022-03-06 21:11:19 +09002016 if (opts->u.cocoa.has_swap_opt_cmd) {
2017 swap_opt_cmd = opts->u.cocoa.swap_opt_cmd;
2018 }
Programmingkid43227af2015-05-19 09:11:17 +01002019
Carwyn Ellis48941a52022-01-02 17:41:52 +00002020 if (opts->u.cocoa.has_left_command_key && !opts->u.cocoa.left_command_key) {
2021 left_command_key_enabled = 0;
2022 }
2023
Carwyn Ellis5ec08982023-10-27 16:49:20 +01002024 if (opts->u.cocoa.has_zoom_to_fit && opts->u.cocoa.zoom_to_fit) {
2025 stretch_video = true;
2026 }
2027
Carwyn Ellise28a9092023-11-10 16:17:29 +00002028 if (opts->u.cocoa.has_zoom_interpolation && opts->u.cocoa.zoom_interpolation) {
2029 zoom_interpolation = kCGInterpolationLow;
2030 }
2031
Carwyn Ellis5ec08982023-10-27 16:49:20 +01002032 create_initial_menus();
2033 /*
2034 * Create the menu entries which depend on QEMU state (for consoles
2035 * and removable devices). These make calls back into QEMU functions,
2036 * which is OK because at this point we know that the second thread
Stefan Hajnoczia4a411f2024-01-02 10:35:28 -05002037 * holds the BQL and is synchronously waiting for us to
Carwyn Ellis5ec08982023-10-27 16:49:20 +01002038 * finish.
2039 */
2040 add_console_menu_entries();
2041 addRemovableDevicesMenuItems();
2042
aliguori9794f742009-03-04 19:25:22 +00002043 // register vga output callbacks
Akihiko Odakicc7859c2021-02-19 17:44:19 +09002044 register_displaychangelistener(&dcl);
Akihiko Odaki7e3e20d2021-06-16 23:19:54 +09002045
2046 qemu_event_init(&cbevent, false);
2047 cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];
2048 qemu_clipboard_peer_register(&cbpeer);
Akihiko Odakibab6a302022-08-19 22:27:54 +09002049
2050 [pool release];
thsc304f7e2008-01-22 23:25:15 +00002051}
Gerd Hoffmann5013b9e2018-03-01 11:05:37 +01002052
2053static QemuDisplay qemu_display_cocoa = {
2054 .type = DISPLAY_TYPE_COCOA,
2055 .init = cocoa_display_init,
2056};
2057
2058static void register_cocoa(void)
2059{
2060 qemu_display_register(&qemu_display_cocoa);
2061}
2062
2063type_init(register_cocoa);