bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1 | /* |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2 | * QEMU Cocoa CG display driver |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 4 | * Copyright (c) 2008 Mike Kronenberg |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 6 | * 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 | */ |
bellard | da4dbf7 | 2005-03-02 22:22:43 +0000 | [diff] [blame] | 24 | |
Peter Maydell | e4a096b | 2016-01-29 16:23:34 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
| 26 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 27 | #import <Cocoa/Cocoa.h> |
Andreas Färber | 3bbbee1 | 2011-05-29 19:42:51 +0200 | [diff] [blame] | 28 | #include <crt_externs.h> |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 29 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 30 | #include "qemu-common.h" |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 31 | #include "ui/clipboard.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 32 | #include "ui/console.h" |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 33 | #include "ui/input.h" |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 34 | #include "ui/kbd-state.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/sysemu.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 36 | #include "sysemu/runstate.h" |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 37 | #include "sysemu/cpu-throttle.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 38 | #include "qapi/error.h" |
Markus Armbruster | 16bf523 | 2018-12-20 09:45:59 +0100 | [diff] [blame] | 39 | #include "qapi/qapi-commands-block.h" |
Philippe Mathieu-Daudé | 90f8c0f | 2020-10-12 14:15:33 +0200 | [diff] [blame] | 40 | #include "qapi/qapi-commands-machine.h" |
Markus Armbruster | 16bf523 | 2018-12-20 09:45:59 +0100 | [diff] [blame] | 41 | #include "qapi/qapi-commands-misc.h" |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 42 | #include "sysemu/blockdev.h" |
Programmingkid | 9e8204b | 2016-08-15 22:11:06 -0400 | [diff] [blame] | 43 | #include "qemu-version.h" |
Akihiko Odaki | e31746e | 2021-03-09 21:22:25 +0900 | [diff] [blame] | 44 | #include "qemu/cutils.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 45 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 46 | #include "qemu/module.h" |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 47 | #include <Carbon/Carbon.h> |
Markus Armbruster | 2e5b09f | 2019-07-09 17:20:52 +0200 | [diff] [blame] | 48 | #include "hw/core/cpu.h" |
bellard | da4dbf7 | 2005-03-02 22:22:43 +0000 | [diff] [blame] | 49 | |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 50 | #ifndef MAC_OS_X_VERSION_10_13 |
| 51 | #define MAC_OS_X_VERSION_10_13 101300 |
| 52 | #endif |
Andreas Färber | 44e4c0b | 2009-12-13 00:45:40 +0100 | [diff] [blame] | 53 | |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 54 | /* 10.14 deprecates NSOnState and NSOffState in favor of |
| 55 | * NSControlStateValueOn/Off, which were introduced in 10.13. |
| 56 | * Define for older versions |
| 57 | */ |
| 58 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13 |
| 59 | #define NSControlStateValueOn NSOnState |
| 60 | #define NSControlStateValueOff NSOffState |
| 61 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 62 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 63 | //#define DEBUG |
| 64 | |
| 65 | #ifdef DEBUG |
| 66 | #define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); } |
| 67 | #else |
| 68 | #define COCOA_DEBUG(...) ((void) 0) |
| 69 | #endif |
| 70 | |
| 71 | #define cgrect(nsrect) (*(CGRect *)&(nsrect)) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 72 | |
| 73 | typedef struct { |
| 74 | int width; |
| 75 | int height; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 76 | } QEMUScreen; |
| 77 | |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 78 | static void cocoa_update(DisplayChangeListener *dcl, |
| 79 | int x, int y, int w, int h); |
| 80 | |
| 81 | static void cocoa_switch(DisplayChangeListener *dcl, |
| 82 | DisplaySurface *surface); |
| 83 | |
| 84 | static void cocoa_refresh(DisplayChangeListener *dcl); |
| 85 | |
Akihiko Odaki | 99eb313 | 2022-02-27 13:22:41 +0900 | [diff] [blame] | 86 | static NSWindow *normalWindow; |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 87 | static const DisplayChangeListenerOps dcl_ops = { |
| 88 | .dpy_name = "cocoa", |
| 89 | .dpy_gfx_update = cocoa_update, |
| 90 | .dpy_gfx_switch = cocoa_switch, |
| 91 | .dpy_refresh = cocoa_refresh, |
| 92 | }; |
| 93 | static DisplayChangeListener dcl = { |
| 94 | .ops = &dcl_ops, |
| 95 | }; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 96 | static int last_buttons; |
Gerd Hoffmann | 3487da6 | 2020-02-06 12:27:50 +0100 | [diff] [blame] | 97 | static int cursor_hide = 1; |
Carwyn Ellis | 48941a5 | 2022-01-02 17:41:52 +0000 | [diff] [blame] | 98 | static int left_command_key_enabled = 1; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 99 | |
Akihiko Odaki | cb82340 | 2021-02-25 17:42:02 +0900 | [diff] [blame] | 100 | static int gArgc; |
| 101 | static char **gArgv; |
| 102 | static bool stretch_video; |
| 103 | static NSTextField *pauseLabel; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 104 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 105 | static QemuSemaphore display_init_sem; |
| 106 | static QemuSemaphore app_started_sem; |
Hikaru Nishida | dff742a | 2019-10-15 10:07:34 +0900 | [diff] [blame] | 107 | static bool allow_events; |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 108 | |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 109 | static NSInteger cbchangecount = -1; |
| 110 | static QemuClipboardInfo *cbinfo; |
| 111 | static QemuEvent cbevent; |
| 112 | |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 113 | // Utility functions to run specified code block with iothread lock held |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 114 | typedef void (^CodeBlock)(void); |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 115 | typedef bool (^BoolCodeBlock)(void); |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 116 | |
| 117 | static void with_iothread_lock(CodeBlock block) |
| 118 | { |
| 119 | bool locked = qemu_mutex_iothread_locked(); |
| 120 | if (!locked) { |
| 121 | qemu_mutex_lock_iothread(); |
| 122 | } |
| 123 | block(); |
| 124 | if (!locked) { |
| 125 | qemu_mutex_unlock_iothread(); |
| 126 | } |
| 127 | } |
| 128 | |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 129 | static bool bool_with_iothread_lock(BoolCodeBlock block) |
| 130 | { |
| 131 | bool locked = qemu_mutex_iothread_locked(); |
| 132 | bool val; |
| 133 | |
| 134 | if (!locked) { |
| 135 | qemu_mutex_lock_iothread(); |
| 136 | } |
| 137 | val = block(); |
| 138 | if (!locked) { |
| 139 | qemu_mutex_unlock_iothread(); |
| 140 | } |
| 141 | return val; |
| 142 | } |
| 143 | |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 144 | // Mac to QKeyCode conversion |
Akihiko Odaki | cb82340 | 2021-02-25 17:42:02 +0900 | [diff] [blame] | 145 | static const int mac_to_qkeycode_map[] = { |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 146 | [kVK_ANSI_A] = Q_KEY_CODE_A, |
| 147 | [kVK_ANSI_B] = Q_KEY_CODE_B, |
| 148 | [kVK_ANSI_C] = Q_KEY_CODE_C, |
| 149 | [kVK_ANSI_D] = Q_KEY_CODE_D, |
| 150 | [kVK_ANSI_E] = Q_KEY_CODE_E, |
| 151 | [kVK_ANSI_F] = Q_KEY_CODE_F, |
| 152 | [kVK_ANSI_G] = Q_KEY_CODE_G, |
| 153 | [kVK_ANSI_H] = Q_KEY_CODE_H, |
| 154 | [kVK_ANSI_I] = Q_KEY_CODE_I, |
| 155 | [kVK_ANSI_J] = Q_KEY_CODE_J, |
| 156 | [kVK_ANSI_K] = Q_KEY_CODE_K, |
| 157 | [kVK_ANSI_L] = Q_KEY_CODE_L, |
| 158 | [kVK_ANSI_M] = Q_KEY_CODE_M, |
| 159 | [kVK_ANSI_N] = Q_KEY_CODE_N, |
| 160 | [kVK_ANSI_O] = Q_KEY_CODE_O, |
| 161 | [kVK_ANSI_P] = Q_KEY_CODE_P, |
| 162 | [kVK_ANSI_Q] = Q_KEY_CODE_Q, |
| 163 | [kVK_ANSI_R] = Q_KEY_CODE_R, |
| 164 | [kVK_ANSI_S] = Q_KEY_CODE_S, |
| 165 | [kVK_ANSI_T] = Q_KEY_CODE_T, |
| 166 | [kVK_ANSI_U] = Q_KEY_CODE_U, |
| 167 | [kVK_ANSI_V] = Q_KEY_CODE_V, |
| 168 | [kVK_ANSI_W] = Q_KEY_CODE_W, |
| 169 | [kVK_ANSI_X] = Q_KEY_CODE_X, |
| 170 | [kVK_ANSI_Y] = Q_KEY_CODE_Y, |
| 171 | [kVK_ANSI_Z] = Q_KEY_CODE_Z, |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 172 | |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 173 | [kVK_ANSI_0] = Q_KEY_CODE_0, |
| 174 | [kVK_ANSI_1] = Q_KEY_CODE_1, |
| 175 | [kVK_ANSI_2] = Q_KEY_CODE_2, |
| 176 | [kVK_ANSI_3] = Q_KEY_CODE_3, |
| 177 | [kVK_ANSI_4] = Q_KEY_CODE_4, |
| 178 | [kVK_ANSI_5] = Q_KEY_CODE_5, |
| 179 | [kVK_ANSI_6] = Q_KEY_CODE_6, |
| 180 | [kVK_ANSI_7] = Q_KEY_CODE_7, |
| 181 | [kVK_ANSI_8] = Q_KEY_CODE_8, |
| 182 | [kVK_ANSI_9] = Q_KEY_CODE_9, |
| 183 | |
| 184 | [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT, |
| 185 | [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS, |
| 186 | [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL, |
| 187 | [kVK_Delete] = Q_KEY_CODE_BACKSPACE, |
| 188 | [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK, |
| 189 | [kVK_Tab] = Q_KEY_CODE_TAB, |
| 190 | [kVK_Return] = Q_KEY_CODE_RET, |
| 191 | [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT, |
| 192 | [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT, |
| 193 | [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH, |
| 194 | [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON, |
| 195 | [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE, |
| 196 | [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA, |
| 197 | [kVK_ANSI_Period] = Q_KEY_CODE_DOT, |
| 198 | [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH, |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 199 | [kVK_Space] = Q_KEY_CODE_SPC, |
| 200 | |
| 201 | [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0, |
| 202 | [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1, |
| 203 | [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2, |
| 204 | [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3, |
| 205 | [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4, |
| 206 | [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5, |
| 207 | [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6, |
| 208 | [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7, |
| 209 | [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8, |
| 210 | [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9, |
| 211 | [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL, |
| 212 | [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER, |
| 213 | [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD, |
| 214 | [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT, |
| 215 | [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY, |
| 216 | [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE, |
| 217 | [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS, |
| 218 | [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK, |
| 219 | |
| 220 | [kVK_UpArrow] = Q_KEY_CODE_UP, |
| 221 | [kVK_DownArrow] = Q_KEY_CODE_DOWN, |
| 222 | [kVK_LeftArrow] = Q_KEY_CODE_LEFT, |
| 223 | [kVK_RightArrow] = Q_KEY_CODE_RIGHT, |
| 224 | |
| 225 | [kVK_Help] = Q_KEY_CODE_INSERT, |
| 226 | [kVK_Home] = Q_KEY_CODE_HOME, |
| 227 | [kVK_PageUp] = Q_KEY_CODE_PGUP, |
| 228 | [kVK_PageDown] = Q_KEY_CODE_PGDN, |
| 229 | [kVK_End] = Q_KEY_CODE_END, |
| 230 | [kVK_ForwardDelete] = Q_KEY_CODE_DELETE, |
| 231 | |
| 232 | [kVK_Escape] = Q_KEY_CODE_ESC, |
| 233 | |
| 234 | /* The Power key can't be used directly because the operating system uses |
| 235 | * it. This key can be emulated by using it in place of another key such as |
| 236 | * F1. Don't forget to disable the real key binding. |
| 237 | */ |
| 238 | /* [kVK_F1] = Q_KEY_CODE_POWER, */ |
| 239 | |
| 240 | [kVK_F1] = Q_KEY_CODE_F1, |
| 241 | [kVK_F2] = Q_KEY_CODE_F2, |
| 242 | [kVK_F3] = Q_KEY_CODE_F3, |
| 243 | [kVK_F4] = Q_KEY_CODE_F4, |
| 244 | [kVK_F5] = Q_KEY_CODE_F5, |
| 245 | [kVK_F6] = Q_KEY_CODE_F6, |
| 246 | [kVK_F7] = Q_KEY_CODE_F7, |
| 247 | [kVK_F8] = Q_KEY_CODE_F8, |
| 248 | [kVK_F9] = Q_KEY_CODE_F9, |
| 249 | [kVK_F10] = Q_KEY_CODE_F10, |
| 250 | [kVK_F11] = Q_KEY_CODE_F11, |
| 251 | [kVK_F12] = Q_KEY_CODE_F12, |
| 252 | [kVK_F13] = Q_KEY_CODE_PRINT, |
| 253 | [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK, |
| 254 | [kVK_F15] = Q_KEY_CODE_PAUSE, |
| 255 | |
Akihiko Odaki | 708b725 | 2021-02-12 09:04:04 +0900 | [diff] [blame] | 256 | // JIS keyboards only |
| 257 | [kVK_JIS_Yen] = Q_KEY_CODE_YEN, |
| 258 | [kVK_JIS_Underscore] = Q_KEY_CODE_RO, |
| 259 | [kVK_JIS_KeypadComma] = Q_KEY_CODE_KP_COMMA, |
| 260 | [kVK_JIS_Eisu] = Q_KEY_CODE_MUHENKAN, |
| 261 | [kVK_JIS_Kana] = Q_KEY_CODE_HENKAN, |
| 262 | |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 263 | /* |
| 264 | * The eject and volume keys can't be used here because they are handled at |
| 265 | * a lower level than what an Application can see. |
| 266 | */ |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 267 | }; |
| 268 | |
Andreas Färber | 77047bb | 2009-12-13 00:55:53 +0100 | [diff] [blame] | 269 | static int cocoa_keycode_to_qemu(int keycode) |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 270 | { |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 271 | if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) { |
Akihiko Odaki | 4313739 | 2021-02-23 22:11:06 +0900 | [diff] [blame] | 272 | error_report("(cocoa) warning unknown keycode 0x%x", keycode); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 273 | return 0; |
| 274 | } |
John Arbuckle | aaac714 | 2016-03-23 14:26:18 +0000 | [diff] [blame] | 275 | return mac_to_qkeycode_map[keycode]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 276 | } |
| 277 | |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 278 | /* Displays an alert dialog box with the specified message */ |
| 279 | static void QEMU_Alert(NSString *message) |
| 280 | { |
| 281 | NSAlert *alert; |
| 282 | alert = [NSAlert new]; |
| 283 | [alert setMessageText: message]; |
| 284 | [alert runModal]; |
| 285 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 286 | |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 287 | /* Handles any errors that happen with a device transaction */ |
| 288 | static void handleAnyDeviceErrors(Error * err) |
| 289 | { |
| 290 | if (err) { |
| 291 | QEMU_Alert([NSString stringWithCString: error_get_pretty(err) |
| 292 | encoding: NSASCIIStringEncoding]); |
| 293 | error_free(err); |
| 294 | } |
| 295 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 296 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 297 | /* |
| 298 | ------------------------------------------------------ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 299 | QemuCocoaView |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 300 | ------------------------------------------------------ |
| 301 | */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 302 | @interface QemuCocoaView : NSView |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 303 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 304 | QEMUScreen screen; |
| 305 | NSWindow *fullScreenWindow; |
| 306 | float cx,cy,cw,ch,cdx,cdy; |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 307 | pixman_image_t *pixman_image; |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 308 | QKbdState *kbd; |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 309 | BOOL isMouseGrabbed; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 310 | BOOL isFullscreen; |
| 311 | BOOL isAbsoluteEnabled; |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 312 | CFMachPortRef eventsTap; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 313 | } |
Peter Maydell | 72a3e31 | 2019-02-25 10:24:28 +0000 | [diff] [blame] | 314 | - (void) switchSurface:(pixman_image_t *)image; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 315 | - (void) grabMouse; |
| 316 | - (void) ungrabMouse; |
| 317 | - (void) toggleFullScreen:(id)sender; |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 318 | - (void) setFullGrab:(id)sender; |
John Arbuckle | 9c3a418 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 319 | - (void) handleMonitorInput:(NSEvent *)event; |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 320 | - (bool) handleEvent:(NSEvent *)event; |
| 321 | - (bool) handleEventLocked:(NSEvent *)event; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 322 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 323 | /* The state surrounding mouse grabbing is potentially confusing. |
| 324 | * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated |
| 325 | * pointing device an absolute-position one?"], but is only updated on |
| 326 | * next refresh. |
| 327 | * isMouseGrabbed tracks whether GUI events are directed to the guest; |
| 328 | * it controls whether special keys like Cmd get sent to the guest, |
| 329 | * and whether we capture the mouse when in non-absolute mode. |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 330 | */ |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 331 | - (BOOL) isMouseGrabbed; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 332 | - (BOOL) isAbsoluteEnabled; |
| 333 | - (float) cdx; |
| 334 | - (float) cdy; |
| 335 | - (QEMUScreen) gscreen; |
John Arbuckle | 3b178b7 | 2015-09-25 23:14:00 +0100 | [diff] [blame] | 336 | - (void) raiseAllKeys; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 337 | @end |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 338 | |
Andreas Färber | 7fee199 | 2011-06-09 20:53:32 +0200 | [diff] [blame] | 339 | QemuCocoaView *cocoaView; |
| 340 | |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 341 | static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef cgEvent, void *userInfo) |
| 342 | { |
| 343 | QemuCocoaView *cocoaView = userInfo; |
| 344 | NSEvent *event = [NSEvent eventWithCGEvent:cgEvent]; |
| 345 | if ([cocoaView isMouseGrabbed] && [cocoaView handleEvent:event]) { |
| 346 | COCOA_DEBUG("Global events tap: qemu handled the event, capturing!\n"); |
| 347 | return NULL; |
| 348 | } |
| 349 | COCOA_DEBUG("Global events tap: qemu did not handle the event, letting it through...\n"); |
| 350 | |
| 351 | return cgEvent; |
| 352 | } |
| 353 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 354 | @implementation QemuCocoaView |
| 355 | - (id)initWithFrame:(NSRect)frameRect |
| 356 | { |
| 357 | COCOA_DEBUG("QemuCocoaView: initWithFrame\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 358 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 359 | self = [super initWithFrame:frameRect]; |
| 360 | if (self) { |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 361 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 362 | screen.width = frameRect.size.width; |
| 363 | screen.height = frameRect.size.height; |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 364 | kbd = qkbd_state_init(dcl.con); |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 365 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 366 | } |
| 367 | return self; |
| 368 | } |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 369 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 370 | - (void) dealloc |
| 371 | { |
| 372 | COCOA_DEBUG("QemuCocoaView: dealloc\n"); |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 373 | |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 374 | if (pixman_image) { |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 375 | pixman_image_unref(pixman_image); |
| 376 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 377 | |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 378 | qkbd_state_free(kbd); |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 379 | |
| 380 | if (eventsTap) { |
| 381 | CFRelease(eventsTap); |
| 382 | } |
| 383 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 384 | [super dealloc]; |
| 385 | } |
bellard | 5cbfcd0 | 2006-06-14 15:53:24 +0000 | [diff] [blame] | 386 | |
Andreas Färber | d50f71d | 2009-12-13 02:03:33 +0100 | [diff] [blame] | 387 | - (BOOL) isOpaque |
| 388 | { |
| 389 | return YES; |
| 390 | } |
| 391 | |
Peter Maydell | 5dd45be | 2014-06-23 10:35:23 +0100 | [diff] [blame] | 392 | - (BOOL) screenContainsPoint:(NSPoint) p |
| 393 | { |
| 394 | return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height); |
| 395 | } |
| 396 | |
Chen Zhang | 2044dff | 2019-06-04 17:36:00 +0800 | [diff] [blame] | 397 | /* Get location of event and convert to virtual screen coordinate */ |
| 398 | - (CGPoint) screenLocationOfEvent:(NSEvent *)ev |
| 399 | { |
| 400 | NSWindow *eventWindow = [ev window]; |
| 401 | // XXX: Use CGRect and -convertRectFromScreen: to support macOS 10.10 |
| 402 | CGRect r = CGRectZero; |
| 403 | r.origin = [ev locationInWindow]; |
| 404 | if (!eventWindow) { |
| 405 | if (!isFullscreen) { |
| 406 | return [[self window] convertRectFromScreen:r].origin; |
| 407 | } else { |
| 408 | CGPoint locationInSelfWindow = [[self window] convertRectFromScreen:r].origin; |
| 409 | CGPoint loc = [self convertPoint:locationInSelfWindow fromView:nil]; |
| 410 | if (stretch_video) { |
| 411 | loc.x /= cdx; |
| 412 | loc.y /= cdy; |
| 413 | } |
| 414 | return loc; |
| 415 | } |
| 416 | } else if ([[self window] isEqual:eventWindow]) { |
| 417 | if (!isFullscreen) { |
| 418 | return r.origin; |
| 419 | } else { |
| 420 | CGPoint loc = [self convertPoint:r.origin fromView:nil]; |
| 421 | if (stretch_video) { |
| 422 | loc.x /= cdx; |
| 423 | loc.y /= cdy; |
| 424 | } |
| 425 | return loc; |
| 426 | } |
| 427 | } else { |
| 428 | return [[self window] convertRectFromScreen:[eventWindow convertRectToScreen:r]].origin; |
| 429 | } |
| 430 | } |
| 431 | |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 432 | - (void) hideCursor |
| 433 | { |
| 434 | if (!cursor_hide) { |
| 435 | return; |
| 436 | } |
| 437 | [NSCursor hide]; |
| 438 | } |
| 439 | |
| 440 | - (void) unhideCursor |
| 441 | { |
| 442 | if (!cursor_hide) { |
| 443 | return; |
| 444 | } |
| 445 | [NSCursor unhide]; |
| 446 | } |
| 447 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 448 | - (void) drawRect:(NSRect) rect |
| 449 | { |
| 450 | COCOA_DEBUG("QemuCocoaView: drawRect\n"); |
bellard | 5cbfcd0 | 2006-06-14 15:53:24 +0000 | [diff] [blame] | 451 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 452 | // get CoreGraphic context |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 453 | CGContextRef viewContextRef = [[NSGraphicsContext currentContext] CGContext]; |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 454 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 455 | CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone); |
| 456 | CGContextSetShouldAntialias (viewContextRef, NO); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 457 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 458 | // draw screen bitmap directly to Core Graphics context |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 459 | if (!pixman_image) { |
Peter Maydell | 7d270b1 | 2013-12-24 02:51:47 +0000 | [diff] [blame] | 460 | // Draw request before any guest device has set up a framebuffer: |
| 461 | // just draw an opaque black rectangle |
| 462 | CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0); |
| 463 | CGContextFillRect(viewContextRef, NSRectToCGRect(rect)); |
| 464 | } else { |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 465 | int w = pixman_image_get_width(pixman_image); |
| 466 | int h = pixman_image_get_height(pixman_image); |
| 467 | int bitsPerPixel = PIXMAN_FORMAT_BPP(pixman_image_get_format(pixman_image)); |
Akihiko Odaki | d9c32b8 | 2021-02-22 23:40:12 +0900 | [diff] [blame] | 468 | int stride = pixman_image_get_stride(pixman_image); |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 469 | CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData( |
| 470 | NULL, |
| 471 | pixman_image_get_data(pixman_image), |
Akihiko Odaki | d9c32b8 | 2021-02-22 23:40:12 +0900 | [diff] [blame] | 472 | stride * h, |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 473 | NULL |
| 474 | ); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 475 | CGImageRef imageRef = CGImageCreate( |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 476 | w, //width |
| 477 | h, //height |
Akihiko Odaki | d9c32b8 | 2021-02-22 23:40:12 +0900 | [diff] [blame] | 478 | DIV_ROUND_UP(bitsPerPixel, 8) * 2, //bitsPerComponent |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 479 | bitsPerPixel, //bitsPerPixel |
Akihiko Odaki | d9c32b8 | 2021-02-22 23:40:12 +0900 | [diff] [blame] | 480 | stride, //bytesPerRow |
Akihiko Odaki | ae57d35 | 2021-03-05 21:13:04 +0900 | [diff] [blame] | 481 | CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace |
| 482 | kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, //bitmapInfo |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 483 | dataProviderRef, //provider |
| 484 | NULL, //decode |
| 485 | 0, //interpolate |
| 486 | kCGRenderingIntentDefault //intent |
| 487 | ); |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 488 | // selective drawing code (draws only dirty rectangles) (OS X >= 10.4) |
| 489 | const NSRect *rectList; |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 490 | NSInteger rectCount; |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 491 | int i; |
| 492 | CGImageRef clipImageRef; |
| 493 | CGRect clipRect; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 494 | |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 495 | [self getRectsBeingDrawn:&rectList count:&rectCount]; |
| 496 | for (i = 0; i < rectCount; i++) { |
| 497 | clipRect.origin.x = rectList[i].origin.x / cdx; |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 498 | clipRect.origin.y = (float)h - (rectList[i].origin.y + rectList[i].size.height) / cdy; |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 499 | clipRect.size.width = rectList[i].size.width / cdx; |
| 500 | clipRect.size.height = rectList[i].size.height / cdy; |
| 501 | clipImageRef = CGImageCreateWithImageInRect( |
| 502 | imageRef, |
| 503 | clipRect |
| 504 | ); |
| 505 | CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef); |
| 506 | CGImageRelease (clipImageRef); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 507 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 508 | CGImageRelease (imageRef); |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 509 | CGDataProviderRelease(dataProviderRef); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 513 | - (void) setContentDimensions |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 514 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 515 | COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 516 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 517 | if (isFullscreen) { |
| 518 | cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width; |
| 519 | cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 520 | |
| 521 | /* stretches video, but keeps same aspect ratio */ |
| 522 | if (stretch_video == true) { |
| 523 | /* use smallest stretch value - prevents clipping on sides */ |
| 524 | if (MIN(cdx, cdy) == cdx) { |
| 525 | cdy = cdx; |
| 526 | } else { |
| 527 | cdx = cdy; |
| 528 | } |
| 529 | } else { /* No stretching */ |
| 530 | cdx = cdy = 1; |
| 531 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 532 | cw = screen.width * cdx; |
| 533 | ch = screen.height * cdy; |
| 534 | cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; |
| 535 | cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; |
| 536 | } else { |
| 537 | cx = 0; |
| 538 | cy = 0; |
| 539 | cw = screen.width; |
| 540 | ch = screen.height; |
| 541 | cdx = 1.0; |
| 542 | cdy = 1.0; |
| 543 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Peter Maydell | 8d65dee | 2022-02-24 10:13:29 +0000 | [diff] [blame] | 546 | - (void) updateUIInfoLocked |
Akihiko Odaki | 15280e8 | 2021-06-16 23:19:10 +0900 | [diff] [blame] | 547 | { |
Peter Maydell | 8d65dee | 2022-02-24 10:13:29 +0000 | [diff] [blame] | 548 | /* Must be called with the iothread lock, i.e. via updateUIInfo */ |
Akihiko Odaki | 15280e8 | 2021-06-16 23:19:10 +0900 | [diff] [blame] | 549 | NSSize frameSize; |
| 550 | QemuUIInfo info; |
| 551 | |
| 552 | if (!qemu_console_is_graphic(dcl.con)) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | if ([self window]) { |
| 557 | NSDictionary *description = [[[self window] screen] deviceDescription]; |
| 558 | CGDirectDisplayID display = [[description objectForKey:@"NSScreenNumber"] unsignedIntValue]; |
| 559 | NSSize screenSize = [[[self window] screen] frame].size; |
| 560 | CGSize screenPhysicalSize = CGDisplayScreenSize(display); |
| 561 | |
| 562 | frameSize = isFullscreen ? screenSize : [self frame].size; |
| 563 | info.width_mm = frameSize.width / screenSize.width * screenPhysicalSize.width; |
| 564 | info.height_mm = frameSize.height / screenSize.height * screenPhysicalSize.height; |
| 565 | } else { |
| 566 | frameSize = [self frame].size; |
| 567 | info.width_mm = 0; |
| 568 | info.height_mm = 0; |
| 569 | } |
| 570 | |
| 571 | info.xoff = 0; |
| 572 | info.yoff = 0; |
| 573 | info.width = frameSize.width; |
| 574 | info.height = frameSize.height; |
| 575 | |
Marc-André Lureau | ca19ef5 | 2021-04-13 20:39:11 +0400 | [diff] [blame] | 576 | dpy_set_ui_info(dcl.con, &info, TRUE); |
Akihiko Odaki | 15280e8 | 2021-06-16 23:19:10 +0900 | [diff] [blame] | 577 | } |
| 578 | |
Peter Maydell | 8d65dee | 2022-02-24 10:13:29 +0000 | [diff] [blame] | 579 | - (void) updateUIInfo |
| 580 | { |
| 581 | if (!allow_events) { |
| 582 | /* |
| 583 | * Don't try to tell QEMU about UI information in the application |
| 584 | * startup phase -- we haven't yet registered dcl with the QEMU UI |
| 585 | * layer, and also trying to take the iothread lock would deadlock. |
| 586 | * When cocoa_display_init() does register the dcl, the UI layer |
| 587 | * will call cocoa_switch(), which will call updateUIInfo, so |
| 588 | * we don't lose any information here. |
| 589 | */ |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | with_iothread_lock(^{ |
| 594 | [self updateUIInfoLocked]; |
| 595 | }); |
| 596 | } |
| 597 | |
Akihiko Odaki | 15280e8 | 2021-06-16 23:19:10 +0900 | [diff] [blame] | 598 | - (void)viewDidMoveToWindow |
| 599 | { |
| 600 | [self updateUIInfo]; |
| 601 | } |
| 602 | |
Peter Maydell | 72a3e31 | 2019-02-25 10:24:28 +0000 | [diff] [blame] | 603 | - (void) switchSurface:(pixman_image_t *)image |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 604 | { |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 605 | COCOA_DEBUG("QemuCocoaView: switchSurface\n"); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 606 | |
Peter Maydell | 72a3e31 | 2019-02-25 10:24:28 +0000 | [diff] [blame] | 607 | int w = pixman_image_get_width(image); |
| 608 | int h = pixman_image_get_height(image); |
Peter Maydell | 381600d | 2014-06-23 10:35:22 +0100 | [diff] [blame] | 609 | /* cdx == 0 means this is our very first surface, in which case we need |
| 610 | * to recalculate the content dimensions even if it happens to be the size |
| 611 | * of the initial empty window. |
| 612 | */ |
| 613 | bool isResize = (w != screen.width || h != screen.height || cdx == 0.0); |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 614 | |
| 615 | int oldh = screen.height; |
| 616 | if (isResize) { |
| 617 | // Resize before we trigger the redraw, or we'll redraw at the wrong size |
| 618 | COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h); |
| 619 | screen.width = w; |
| 620 | screen.height = h; |
| 621 | [self setContentDimensions]; |
| 622 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; |
| 623 | } |
Peter Maydell | 8510d91 | 2013-03-18 20:28:21 +0000 | [diff] [blame] | 624 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 625 | // update screenBuffer |
Akihiko Odaki | c0ff29d | 2021-02-12 09:06:29 +0900 | [diff] [blame] | 626 | if (pixman_image) { |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 627 | pixman_image_unref(pixman_image); |
| 628 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 629 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 630 | pixman_image = image; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 631 | |
| 632 | // update windows |
| 633 | if (isFullscreen) { |
| 634 | [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]]; |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 635 | [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:NO animate:NO]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 636 | } else { |
| 637 | if (qemu_name) |
| 638 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 639 | [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:YES animate:NO]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 640 | } |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 641 | |
| 642 | if (isResize) { |
| 643 | [normalWindow center]; |
| 644 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | - (void) toggleFullScreen:(id)sender |
| 648 | { |
| 649 | COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); |
| 650 | |
| 651 | if (isFullscreen) { // switch from fullscreen to desktop |
| 652 | isFullscreen = FALSE; |
| 653 | [self ungrabMouse]; |
| 654 | [self setContentDimensions]; |
Akihiko Odaki | 1e8b6f2 | 2021-02-20 10:31:38 +0900 | [diff] [blame] | 655 | [fullScreenWindow close]; |
| 656 | [normalWindow setContentView: self]; |
| 657 | [normalWindow makeKeyAndOrderFront: self]; |
| 658 | [NSMenu setMenuBarVisible:YES]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 659 | } else { // switch from desktop to fullscreen |
| 660 | isFullscreen = TRUE; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 661 | [normalWindow orderOut: nil]; /* Hide the window */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 662 | [self grabMouse]; |
| 663 | [self setContentDimensions]; |
Akihiko Odaki | 1e8b6f2 | 2021-02-20 10:31:38 +0900 | [diff] [blame] | 664 | [NSMenu setMenuBarVisible:NO]; |
| 665 | fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] |
| 666 | styleMask:NSWindowStyleMaskBorderless |
| 667 | backing:NSBackingStoreBuffered |
| 668 | defer:NO]; |
| 669 | [fullScreenWindow setAcceptsMouseMovedEvents: YES]; |
| 670 | [fullScreenWindow setHasShadow:NO]; |
| 671 | [fullScreenWindow setBackgroundColor: [NSColor blackColor]]; |
| 672 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; |
| 673 | [[fullScreenWindow contentView] addSubview: self]; |
| 674 | [fullScreenWindow makeKeyAndOrderFront:self]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 678 | - (void) setFullGrab:(id)sender |
| 679 | { |
| 680 | COCOA_DEBUG("QemuCocoaView: setFullGrab\n"); |
| 681 | |
| 682 | CGEventMask mask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged); |
| 683 | eventsTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, |
| 684 | mask, handleTapEvent, self); |
| 685 | if (!eventsTap) { |
| 686 | warn_report("Could not create event tap, system key combos will not be captured.\n"); |
| 687 | return; |
| 688 | } else { |
| 689 | COCOA_DEBUG("Global events tap created! Will capture system key combos.\n"); |
| 690 | } |
| 691 | |
| 692 | CFRunLoopRef runLoop = CFRunLoopGetCurrent(); |
| 693 | if (!runLoop) { |
| 694 | warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n"); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | CFRunLoopSourceRef tapEventsSrc = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventsTap, 0); |
| 699 | if (!tapEventsSrc ) { |
| 700 | warn_report("Could not obtain current CF RunLoop, system key combos will not be captured.\n"); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | CFRunLoopAddSource(runLoop, tapEventsSrc, kCFRunLoopDefaultMode); |
| 705 | CFRelease(tapEventsSrc); |
| 706 | } |
| 707 | |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 708 | - (void) toggleKey: (int)keycode { |
| 709 | qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode)); |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 710 | } |
| 711 | |
John Arbuckle | 9c3a418 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 712 | // Does the work of sending input to the monitor |
| 713 | - (void) handleMonitorInput:(NSEvent *)event |
| 714 | { |
| 715 | int keysym = 0; |
| 716 | int control_key = 0; |
| 717 | |
| 718 | // if the control key is down |
| 719 | if ([event modifierFlags] & NSEventModifierFlagControl) { |
| 720 | control_key = 1; |
| 721 | } |
| 722 | |
| 723 | /* translates Macintosh keycodes to QEMU's keysym */ |
| 724 | |
Philippe Mathieu-Daudé | 9459262 | 2022-02-15 16:21:14 +0100 | [diff] [blame] | 725 | static const int without_control_translation[] = { |
John Arbuckle | 9c3a418 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 726 | [0 ... 0xff] = 0, // invalid key |
| 727 | |
| 728 | [kVK_UpArrow] = QEMU_KEY_UP, |
| 729 | [kVK_DownArrow] = QEMU_KEY_DOWN, |
| 730 | [kVK_RightArrow] = QEMU_KEY_RIGHT, |
| 731 | [kVK_LeftArrow] = QEMU_KEY_LEFT, |
| 732 | [kVK_Home] = QEMU_KEY_HOME, |
| 733 | [kVK_End] = QEMU_KEY_END, |
| 734 | [kVK_PageUp] = QEMU_KEY_PAGEUP, |
| 735 | [kVK_PageDown] = QEMU_KEY_PAGEDOWN, |
| 736 | [kVK_ForwardDelete] = QEMU_KEY_DELETE, |
| 737 | [kVK_Delete] = QEMU_KEY_BACKSPACE, |
| 738 | }; |
| 739 | |
Philippe Mathieu-Daudé | 9459262 | 2022-02-15 16:21:14 +0100 | [diff] [blame] | 740 | static const int with_control_translation[] = { |
John Arbuckle | 9c3a418 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 741 | [0 ... 0xff] = 0, // invalid key |
| 742 | |
| 743 | [kVK_UpArrow] = QEMU_KEY_CTRL_UP, |
| 744 | [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN, |
| 745 | [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT, |
| 746 | [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT, |
| 747 | [kVK_Home] = QEMU_KEY_CTRL_HOME, |
| 748 | [kVK_End] = QEMU_KEY_CTRL_END, |
| 749 | [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP, |
| 750 | [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN, |
| 751 | }; |
| 752 | |
| 753 | if (control_key != 0) { /* If the control key is being used */ |
| 754 | if ([event keyCode] < ARRAY_SIZE(with_control_translation)) { |
| 755 | keysym = with_control_translation[[event keyCode]]; |
| 756 | } |
| 757 | } else { |
| 758 | if ([event keyCode] < ARRAY_SIZE(without_control_translation)) { |
| 759 | keysym = without_control_translation[[event keyCode]]; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // if not a key that needs translating |
| 764 | if (keysym == 0) { |
| 765 | NSString *ks = [event characters]; |
| 766 | if ([ks length] > 0) { |
| 767 | keysym = [ks characterAtIndex:0]; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (keysym) { |
| 772 | kbd_put_keysym(keysym); |
| 773 | } |
| 774 | } |
| 775 | |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 776 | - (bool) handleEvent:(NSEvent *)event |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 777 | { |
Hikaru Nishida | dff742a | 2019-10-15 10:07:34 +0900 | [diff] [blame] | 778 | if(!allow_events) { |
| 779 | /* |
| 780 | * Just let OSX have all events that arrive before |
| 781 | * applicationDidFinishLaunching. |
| 782 | * This avoids a deadlock on the iothread lock, which cocoa_display_init() |
| 783 | * will not drop until after the app_started_sem is posted. (In theory |
| 784 | * there should not be any such events, but OSX Catalina now emits some.) |
| 785 | */ |
| 786 | return false; |
| 787 | } |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 788 | return bool_with_iothread_lock(^{ |
| 789 | return [self handleEventLocked:event]; |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 790 | }); |
| 791 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 792 | |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 793 | - (bool) handleEventLocked:(NSEvent *)event |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 794 | { |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 795 | /* Return true if we handled the event, false if it should be given to OSX */ |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 796 | COCOA_DEBUG("QemuCocoaView: handleEvent\n"); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 797 | int buttons = 0; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 798 | int keycode = 0; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 799 | bool mouse_event = false; |
John Arbuckle | 0c6c439 | 2018-07-02 22:00:17 -0400 | [diff] [blame] | 800 | static bool switched_to_fullscreen = false; |
Chen Zhang | 2044dff | 2019-06-04 17:36:00 +0800 | [diff] [blame] | 801 | // Location of event in virtual screen coordinates |
| 802 | NSPoint p = [self screenLocationOfEvent:event]; |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 803 | NSUInteger modifiers = [event modifierFlags]; |
| 804 | |
Akihiko Odaki | ad7f2f8 | 2021-03-12 22:32:12 +0900 | [diff] [blame] | 805 | /* |
| 806 | * Check -[NSEvent modifierFlags] here. |
| 807 | * |
| 808 | * There is a NSEventType for an event notifying the change of |
| 809 | * -[NSEvent modifierFlags], NSEventTypeFlagsChanged but these operations |
| 810 | * are performed for any events because a modifier state may change while |
| 811 | * the application is inactive (i.e. no events fire) and we don't want to |
| 812 | * wait for another modifier state change to detect such a change. |
| 813 | * |
| 814 | * NSEventModifierFlagCapsLock requires a special treatment. The other flags |
| 815 | * are handled in similar manners. |
| 816 | * |
| 817 | * NSEventModifierFlagCapsLock |
| 818 | * --------------------------- |
| 819 | * |
| 820 | * If CapsLock state is changed, "up" and "down" events will be fired in |
| 821 | * sequence, effectively updates CapsLock state on the guest. |
| 822 | * |
| 823 | * The other flags |
| 824 | * --------------- |
| 825 | * |
| 826 | * If a flag is not set, fire "up" events for all keys which correspond to |
| 827 | * the flag. Note that "down" events are not fired here because the flags |
| 828 | * checked here do not tell what exact keys are down. |
| 829 | * |
| 830 | * If one of the keys corresponding to a flag is down, we rely on |
| 831 | * -[NSEvent keyCode] of an event whose -[NSEvent type] is |
| 832 | * NSEventTypeFlagsChanged to know the exact key which is down, which has |
| 833 | * the following two downsides: |
| 834 | * - It does not work when the application is inactive as described above. |
| 835 | * - It malfactions *after* the modifier state is changed while the |
| 836 | * application is inactive. It is because -[NSEvent keyCode] does not tell |
| 837 | * if the key is up or down, and requires to infer the current state from |
| 838 | * the previous state. It is still possible to fix such a malfanction by |
| 839 | * completely leaving your hands from the keyboard, which hopefully makes |
| 840 | * this implementation usable enough. |
| 841 | */ |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 842 | if (!!(modifiers & NSEventModifierFlagCapsLock) != |
| 843 | qkbd_state_modifier_get(kbd, QKBD_MOD_CAPSLOCK)) { |
| 844 | qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, true); |
| 845 | qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, false); |
| 846 | } |
| 847 | |
| 848 | if (!(modifiers & NSEventModifierFlagShift)) { |
| 849 | qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT, false); |
| 850 | qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT_R, false); |
| 851 | } |
| 852 | if (!(modifiers & NSEventModifierFlagControl)) { |
| 853 | qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL, false); |
| 854 | qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL_R, false); |
| 855 | } |
| 856 | if (!(modifiers & NSEventModifierFlagOption)) { |
| 857 | qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false); |
| 858 | qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false); |
| 859 | } |
| 860 | if (!(modifiers & NSEventModifierFlagCommand)) { |
| 861 | qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false); |
| 862 | qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false); |
| 863 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 864 | |
| 865 | switch ([event type]) { |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 866 | case NSEventTypeFlagsChanged: |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 867 | switch ([event keyCode]) { |
| 868 | case kVK_Shift: |
| 869 | if (!!(modifiers & NSEventModifierFlagShift)) { |
| 870 | [self toggleKey:Q_KEY_CODE_SHIFT]; |
| 871 | } |
| 872 | break; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 873 | |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 874 | case kVK_RightShift: |
| 875 | if (!!(modifiers & NSEventModifierFlagShift)) { |
| 876 | [self toggleKey:Q_KEY_CODE_SHIFT_R]; |
| 877 | } |
| 878 | break; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 879 | |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 880 | case kVK_Control: |
| 881 | if (!!(modifiers & NSEventModifierFlagControl)) { |
| 882 | [self toggleKey:Q_KEY_CODE_CTRL]; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 883 | } |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 884 | break; |
| 885 | |
| 886 | case kVK_RightControl: |
| 887 | if (!!(modifiers & NSEventModifierFlagControl)) { |
| 888 | [self toggleKey:Q_KEY_CODE_CTRL_R]; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 889 | } |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 890 | break; |
| 891 | |
| 892 | case kVK_Option: |
| 893 | if (!!(modifiers & NSEventModifierFlagOption)) { |
| 894 | [self toggleKey:Q_KEY_CODE_ALT]; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 895 | } |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 896 | break; |
| 897 | |
| 898 | case kVK_RightOption: |
| 899 | if (!!(modifiers & NSEventModifierFlagOption)) { |
| 900 | [self toggleKey:Q_KEY_CODE_ALT_R]; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 901 | } |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 902 | break; |
| 903 | |
| 904 | /* Don't pass command key changes to guest unless mouse is grabbed */ |
| 905 | case kVK_Command: |
| 906 | if (isMouseGrabbed && |
Carwyn Ellis | 48941a5 | 2022-01-02 17:41:52 +0000 | [diff] [blame] | 907 | !!(modifiers & NSEventModifierFlagCommand) && |
| 908 | left_command_key_enabled) { |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 909 | [self toggleKey:Q_KEY_CODE_META_L]; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 910 | } |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 911 | break; |
| 912 | |
| 913 | case kVK_RightCommand: |
| 914 | if (isMouseGrabbed && |
| 915 | !!(modifiers & NSEventModifierFlagCommand)) { |
| 916 | [self toggleKey:Q_KEY_CODE_META_R]; |
| 917 | } |
| 918 | break; |
Ian McKellar via Qemu-devel | af8862b | 2017-05-26 16:38:16 -0700 | [diff] [blame] | 919 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 920 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 921 | case NSEventTypeKeyDown: |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 922 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 923 | |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 924 | // forward command key combos to the host UI unless the mouse is grabbed |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 925 | if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) { |
John Arbuckle | 0c6c439 | 2018-07-02 22:00:17 -0400 | [diff] [blame] | 926 | /* |
| 927 | * Prevent the command key from being stuck down in the guest |
| 928 | * when using Command-F to switch to full screen mode. |
| 929 | */ |
| 930 | if (keycode == Q_KEY_CODE_F) { |
| 931 | switched_to_fullscreen = true; |
| 932 | } |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 933 | return false; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | // default |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 937 | |
John Arbuckle | 5929e36 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 938 | // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU) |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 939 | if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) { |
John Arbuckle | 5929e36 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 940 | NSString *keychar = [event charactersIgnoringModifiers]; |
| 941 | if ([keychar length] == 1) { |
| 942 | char key = [keychar characterAtIndex:0]; |
| 943 | switch (key) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 944 | |
John Arbuckle | 5929e36 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 945 | // enable graphic console |
| 946 | case '1' ... '9': |
| 947 | console_select(key - '0' - 1); /* ascii math */ |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 948 | return true; |
John Arbuckle | 5929e36 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 949 | |
| 950 | // release the mouse grab |
| 951 | case 'g': |
| 952 | [self ungrabMouse]; |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 953 | return true; |
John Arbuckle | 5929e36 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 954 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 955 | } |
Peter Maydell | ef2088f | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 956 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 957 | |
Peter Maydell | ef2088f | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 958 | if (qemu_console_is_graphic(NULL)) { |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 959 | qkbd_state_key_event(kbd, keycode, true); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 960 | } else { |
John Arbuckle | 9c3a418 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 961 | [self handleMonitorInput: event]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 962 | } |
| 963 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 964 | case NSEventTypeKeyUp: |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 965 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 966 | |
| 967 | // don't pass the guest a spurious key-up if we treated this |
| 968 | // command-key combo as a host UI action |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 969 | if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) { |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 970 | return true; |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 973 | if (qemu_console_is_graphic(NULL)) { |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 974 | qkbd_state_key_event(kbd, keycode, false); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 975 | } |
| 976 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 977 | case NSEventTypeMouseMoved: |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 978 | if (isAbsoluteEnabled) { |
Chen Zhang | 2044dff | 2019-06-04 17:36:00 +0800 | [diff] [blame] | 979 | // Cursor re-entered into a window might generate events bound to screen coordinates |
| 980 | // and `nil` window property, and in full screen mode, current window might not be |
| 981 | // key window, where event location alone should suffice. |
| 982 | if (![self screenContainsPoint:p] || !([[self window] isKeyWindow] || isFullscreen)) { |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 983 | if (isMouseGrabbed) { |
| 984 | [self ungrabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 985 | } |
| 986 | } else { |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 987 | if (!isMouseGrabbed) { |
| 988 | [self grabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 992 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 993 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 994 | case NSEventTypeLeftMouseDown: |
Akihiko Odaki | 4295f83 | 2021-02-12 09:07:06 +0900 | [diff] [blame] | 995 | buttons |= MOUSE_EVENT_LBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 996 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 997 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 998 | case NSEventTypeRightMouseDown: |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 999 | buttons |= MOUSE_EVENT_RBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1000 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1001 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1002 | case NSEventTypeOtherMouseDown: |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1003 | buttons |= MOUSE_EVENT_MBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1004 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1005 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1006 | case NSEventTypeLeftMouseDragged: |
Akihiko Odaki | 4295f83 | 2021-02-12 09:07:06 +0900 | [diff] [blame] | 1007 | buttons |= MOUSE_EVENT_LBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1008 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1009 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1010 | case NSEventTypeRightMouseDragged: |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1011 | buttons |= MOUSE_EVENT_RBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1012 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1013 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1014 | case NSEventTypeOtherMouseDragged: |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1015 | buttons |= MOUSE_EVENT_MBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1016 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1017 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1018 | case NSEventTypeLeftMouseUp: |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 1019 | mouse_event = true; |
| 1020 | if (!isMouseGrabbed && [self screenContainsPoint:p]) { |
Chen Zhang | 8e23e34 | 2019-06-04 17:36:48 +0800 | [diff] [blame] | 1021 | /* |
| 1022 | * In fullscreen mode, the window of cocoaView may not be the |
| 1023 | * key window, therefore the position relative to the virtual |
| 1024 | * screen alone will be sufficient. |
| 1025 | */ |
| 1026 | if(isFullscreen || [[self window] isKeyWindow]) { |
Programmingkid | 9e8204b | 2016-08-15 22:11:06 -0400 | [diff] [blame] | 1027 | [self grabMouse]; |
| 1028 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1029 | } |
| 1030 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1031 | case NSEventTypeRightMouseUp: |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1032 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1033 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1034 | case NSEventTypeOtherMouseUp: |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1035 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1036 | break; |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1037 | case NSEventTypeScrollWheel: |
John Arbuckle | ae7313e | 2018-01-08 13:07:07 -0500 | [diff] [blame] | 1038 | /* |
| 1039 | * Send wheel events to the guest regardless of window focus. |
| 1040 | * This is in-line with standard Mac OS X UI behaviour. |
| 1041 | */ |
| 1042 | |
John Arbuckle | dc3c89d | 2018-07-09 11:02:35 -0400 | [diff] [blame] | 1043 | /* |
Dmitry Petrov | d70a5de | 2022-01-08 16:39:44 +0100 | [diff] [blame] | 1044 | * We shouldn't have got a scroll event when deltaY and delta Y |
| 1045 | * are zero, hence no harm in dropping the event |
John Arbuckle | dc3c89d | 2018-07-09 11:02:35 -0400 | [diff] [blame] | 1046 | */ |
Dmitry Petrov | d70a5de | 2022-01-08 16:39:44 +0100 | [diff] [blame] | 1047 | if ([event deltaY] != 0 || [event deltaX] != 0) { |
John Arbuckle | ae7313e | 2018-01-08 13:07:07 -0500 | [diff] [blame] | 1048 | /* Determine if this is a scroll up or scroll down event */ |
Dmitry Petrov | d70a5de | 2022-01-08 16:39:44 +0100 | [diff] [blame] | 1049 | if ([event deltaY] != 0) { |
| 1050 | buttons = ([event deltaY] > 0) ? |
John Arbuckle | dc3c89d | 2018-07-09 11:02:35 -0400 | [diff] [blame] | 1051 | INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN; |
Dmitry Petrov | d70a5de | 2022-01-08 16:39:44 +0100 | [diff] [blame] | 1052 | } else if ([event deltaX] != 0) { |
| 1053 | buttons = ([event deltaX] > 0) ? |
| 1054 | INPUT_BUTTON_WHEEL_LEFT : INPUT_BUTTON_WHEEL_RIGHT; |
| 1055 | } |
| 1056 | |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 1057 | qemu_input_queue_btn(dcl.con, buttons, true); |
John Arbuckle | dc3c89d | 2018-07-09 11:02:35 -0400 | [diff] [blame] | 1058 | qemu_input_event_sync(); |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 1059 | qemu_input_queue_btn(dcl.con, buttons, false); |
John Arbuckle | dc3c89d | 2018-07-09 11:02:35 -0400 | [diff] [blame] | 1060 | qemu_input_event_sync(); |
| 1061 | } |
Dmitry Petrov | d70a5de | 2022-01-08 16:39:44 +0100 | [diff] [blame] | 1062 | |
John Arbuckle | ae7313e | 2018-01-08 13:07:07 -0500 | [diff] [blame] | 1063 | /* |
Dmitry Petrov | d70a5de | 2022-01-08 16:39:44 +0100 | [diff] [blame] | 1064 | * Since deltaX/deltaY also report scroll wheel events we prevent mouse |
John Arbuckle | ae7313e | 2018-01-08 13:07:07 -0500 | [diff] [blame] | 1065 | * movement code from executing. |
| 1066 | */ |
| 1067 | mouse_event = false; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1068 | break; |
| 1069 | default: |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 1070 | return false; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1071 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1072 | |
| 1073 | if (mouse_event) { |
Peter Maydell | 8d3a5d9 | 2015-11-26 15:19:28 +0000 | [diff] [blame] | 1074 | /* Don't send button events to the guest unless we've got a |
| 1075 | * mouse grab or window focus. If we have neither then this event |
| 1076 | * is the user clicking on the background window to activate and |
| 1077 | * bring us to the front, which will be done by the sendEvent |
| 1078 | * call below. We definitely don't want to pass that click through |
| 1079 | * to the guest. |
| 1080 | */ |
| 1081 | if ((isMouseGrabbed || [[self window] isKeyWindow]) && |
| 1082 | (last_buttons != buttons)) { |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 1083 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1084 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
| 1085 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, |
John Arbuckle | ae7313e | 2018-01-08 13:07:07 -0500 | [diff] [blame] | 1086 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1087 | }; |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 1088 | qemu_input_update_buttons(dcl.con, bmap, last_buttons, buttons); |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1089 | last_buttons = buttons; |
| 1090 | } |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 1091 | if (isMouseGrabbed) { |
| 1092 | if (isAbsoluteEnabled) { |
| 1093 | /* Note that the origin for Cocoa mouse coords is bottom left, not top left. |
| 1094 | * The check on screenContainsPoint is to avoid sending out of range values for |
| 1095 | * clicks in the titlebar. |
| 1096 | */ |
| 1097 | if ([self screenContainsPoint:p]) { |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 1098 | qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x, 0, screen.width); |
| 1099 | qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height); |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 1100 | } |
| 1101 | } else { |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 1102 | qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, (int)[event deltaX]); |
| 1103 | qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, (int)[event deltaY]); |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 1104 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1105 | } else { |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 1106 | return false; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1107 | } |
| 1108 | qemu_input_event_sync(); |
| 1109 | } |
Peter Maydell | 60105d7 | 2019-02-25 10:24:31 +0000 | [diff] [blame] | 1110 | return true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | - (void) grabMouse |
| 1114 | { |
| 1115 | COCOA_DEBUG("QemuCocoaView: grabMouse\n"); |
| 1116 | |
| 1117 | if (!isFullscreen) { |
| 1118 | if (qemu_name) |
John Arbuckle | 5929e36 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 1119 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt + g to release Mouse)", qemu_name]]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1120 | else |
John Arbuckle | 5929e36 | 2017-11-07 10:14:14 +0000 | [diff] [blame] | 1121 | [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release Mouse)"]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1122 | } |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 1123 | [self hideCursor]; |
Akihiko Odaki | d192906 | 2021-02-23 00:07:14 +0900 | [diff] [blame] | 1124 | CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled); |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 1125 | isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | - (void) ungrabMouse |
| 1129 | { |
| 1130 | COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); |
| 1131 | |
| 1132 | if (!isFullscreen) { |
| 1133 | if (qemu_name) |
| 1134 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; |
| 1135 | else |
| 1136 | [normalWindow setTitle:@"QEMU"]; |
| 1137 | } |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 1138 | [self unhideCursor]; |
Akihiko Odaki | d192906 | 2021-02-23 00:07:14 +0900 | [diff] [blame] | 1139 | CGAssociateMouseAndMouseCursorPosition(TRUE); |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 1140 | isMouseGrabbed = FALSE; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Akihiko Odaki | d192906 | 2021-02-23 00:07:14 +0900 | [diff] [blame] | 1143 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled { |
| 1144 | isAbsoluteEnabled = tIsAbsoluteEnabled; |
| 1145 | if (isMouseGrabbed) { |
| 1146 | CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled); |
| 1147 | } |
| 1148 | } |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 1149 | - (BOOL) isMouseGrabbed {return isMouseGrabbed;} |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1150 | - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} |
| 1151 | - (float) cdx {return cdx;} |
| 1152 | - (float) cdy {return cdy;} |
| 1153 | - (QEMUScreen) gscreen {return screen;} |
John Arbuckle | 3b178b7 | 2015-09-25 23:14:00 +0100 | [diff] [blame] | 1154 | |
| 1155 | /* |
| 1156 | * Makes the target think all down keys are being released. |
| 1157 | * This prevents a stuck key problem, since we will not see |
| 1158 | * key up events for those keys after we have lost focus. |
| 1159 | */ |
| 1160 | - (void) raiseAllKeys |
| 1161 | { |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1162 | with_iothread_lock(^{ |
Akihiko Odaki | 6d73bb6 | 2021-03-10 23:46:02 +0900 | [diff] [blame] | 1163 | qkbd_state_lift_all_keys(kbd); |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1164 | }); |
John Arbuckle | 3b178b7 | 2015-09-25 23:14:00 +0100 | [diff] [blame] | 1165 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1166 | @end |
| 1167 | |
| 1168 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1169 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1170 | /* |
| 1171 | ------------------------------------------------------ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1172 | QemuCocoaAppController |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1173 | ------------------------------------------------------ |
| 1174 | */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1175 | @interface QemuCocoaAppController : NSObject |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame] | 1176 | <NSWindowDelegate, NSApplicationDelegate> |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1177 | { |
| 1178 | } |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1179 | - (void)doToggleFullScreen:(id)sender; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1180 | - (void)toggleFullScreen:(id)sender; |
| 1181 | - (void)showQEMUDoc:(id)sender; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1182 | - (void)zoomToFit:(id) sender; |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 1183 | - (void)displayConsole:(id)sender; |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1184 | - (void)pauseQEMU:(id)sender; |
| 1185 | - (void)resumeQEMU:(id)sender; |
| 1186 | - (void)displayPause; |
| 1187 | - (void)removePause; |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1188 | - (void)restartQEMU:(id)sender; |
| 1189 | - (void)powerDownQEMU:(id)sender; |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1190 | - (void)ejectDeviceMedia:(id)sender; |
| 1191 | - (void)changeDeviceMedia:(id)sender; |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame] | 1192 | - (BOOL)verifyQuit; |
John Arbuckle | f474790 | 2016-03-23 14:26:17 +0000 | [diff] [blame] | 1193 | - (void)openDocumentation:(NSString *)filename; |
Programmingkid | 9e8204b | 2016-08-15 22:11:06 -0400 | [diff] [blame] | 1194 | - (IBAction) do_about_menu_item: (id) sender; |
John Arbuckle | e47ec1a | 2017-06-13 23:17:38 -0400 | [diff] [blame] | 1195 | - (void)adjustSpeed:(id)sender; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1196 | @end |
| 1197 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1198 | @implementation QemuCocoaAppController |
| 1199 | - (id) init |
| 1200 | { |
| 1201 | COCOA_DEBUG("QemuCocoaAppController: init\n"); |
| 1202 | |
| 1203 | self = [super init]; |
| 1204 | if (self) { |
| 1205 | |
| 1206 | // create a view and add it to the window |
| 1207 | cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)]; |
| 1208 | if(!cocoaView) { |
Akihiko Odaki | 4313739 | 2021-02-23 22:11:06 +0900 | [diff] [blame] | 1209 | error_report("(cocoa) can't create a view"); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1210 | exit(1); |
| 1211 | } |
| 1212 | |
| 1213 | // create a window |
| 1214 | normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame] |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1215 | styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1216 | backing:NSBackingStoreBuffered defer:NO]; |
| 1217 | if(!normalWindow) { |
Akihiko Odaki | 4313739 | 2021-02-23 22:11:06 +0900 | [diff] [blame] | 1218 | error_report("(cocoa) can't create window"); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1219 | exit(1); |
| 1220 | } |
| 1221 | [normalWindow setAcceptsMouseMovedEvents:YES]; |
John Arbuckle | a1dbc05 | 2015-10-13 21:51:18 +0100 | [diff] [blame] | 1222 | [normalWindow setTitle:@"QEMU"]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1223 | [normalWindow setContentView:cocoaView]; |
| 1224 | [normalWindow makeKeyAndOrderFront:self]; |
Peter Maydell | 49060c2 | 2013-12-24 11:54:12 +0000 | [diff] [blame] | 1225 | [normalWindow center]; |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame] | 1226 | [normalWindow setDelegate: self]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1227 | stretch_video = false; |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1228 | |
| 1229 | /* Used for displaying pause on the screen */ |
| 1230 | pauseLabel = [NSTextField new]; |
| 1231 | [pauseLabel setBezeled:YES]; |
| 1232 | [pauseLabel setDrawsBackground:YES]; |
| 1233 | [pauseLabel setBackgroundColor: [NSColor whiteColor]]; |
| 1234 | [pauseLabel setEditable:NO]; |
| 1235 | [pauseLabel setSelectable:NO]; |
| 1236 | [pauseLabel setStringValue: @"Paused"]; |
| 1237 | [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]]; |
| 1238 | [pauseLabel setTextColor: [NSColor blackColor]]; |
| 1239 | [pauseLabel sizeToFit]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1240 | } |
| 1241 | return self; |
| 1242 | } |
| 1243 | |
| 1244 | - (void) dealloc |
| 1245 | { |
| 1246 | COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); |
| 1247 | |
| 1248 | if (cocoaView) |
| 1249 | [cocoaView release]; |
| 1250 | [super dealloc]; |
| 1251 | } |
| 1252 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1253 | - (void)applicationDidFinishLaunching: (NSNotification *) note |
| 1254 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1255 | COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); |
Hikaru Nishida | dff742a | 2019-10-15 10:07:34 +0900 | [diff] [blame] | 1256 | allow_events = true; |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1257 | /* Tell cocoa_display_init to proceed */ |
| 1258 | qemu_sem_post(&app_started_sem); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | - (void)applicationWillTerminate:(NSNotification *)aNotification |
| 1262 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1263 | COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n"); |
| 1264 | |
Eric Blake | cf83f14 | 2017-05-15 16:41:13 -0500 | [diff] [blame] | 1265 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI); |
Akihiko Odaki | 40c0193 | 2021-02-19 20:16:52 +0900 | [diff] [blame] | 1266 | |
| 1267 | /* |
| 1268 | * Sleep here, because returning will cause OSX to kill us |
| 1269 | * immediately; the QEMU main loop will handle the shutdown |
| 1270 | * request and terminate the process. |
| 1271 | */ |
| 1272 | [NSThread sleepForTimeInterval:INFINITY]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Andreas Färber | 41ea49b | 2009-12-14 22:13:27 +0100 | [diff] [blame] | 1275 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication |
| 1276 | { |
| 1277 | return YES; |
| 1278 | } |
| 1279 | |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame] | 1280 | - (NSApplicationTerminateReply)applicationShouldTerminate: |
| 1281 | (NSApplication *)sender |
| 1282 | { |
| 1283 | COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n"); |
| 1284 | return [self verifyQuit]; |
| 1285 | } |
| 1286 | |
Akihiko Odaki | 15280e8 | 2021-06-16 23:19:10 +0900 | [diff] [blame] | 1287 | - (void)windowDidChangeScreen:(NSNotification *)notification |
| 1288 | { |
| 1289 | [cocoaView updateUIInfo]; |
| 1290 | } |
| 1291 | |
| 1292 | - (void)windowDidResize:(NSNotification *)notification |
| 1293 | { |
| 1294 | [cocoaView updateUIInfo]; |
| 1295 | } |
| 1296 | |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame] | 1297 | /* Called when the user clicks on a window's close button */ |
| 1298 | - (BOOL)windowShouldClose:(id)sender |
| 1299 | { |
| 1300 | COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n"); |
| 1301 | [NSApp terminate: sender]; |
| 1302 | /* If the user allows the application to quit then the call to |
| 1303 | * NSApp terminate will never return. If we get here then the user |
| 1304 | * cancelled the quit, so we should return NO to not permit the |
| 1305 | * closing of this window. |
| 1306 | */ |
| 1307 | return NO; |
| 1308 | } |
| 1309 | |
John Arbuckle | 3b178b7 | 2015-09-25 23:14:00 +0100 | [diff] [blame] | 1310 | /* Called when QEMU goes into the background */ |
| 1311 | - (void) applicationWillResignActive: (NSNotification *)aNotification |
| 1312 | { |
| 1313 | COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n"); |
Carwyn Ellis | 69221df | 2022-01-02 17:41:53 +0000 | [diff] [blame] | 1314 | [cocoaView ungrabMouse]; |
John Arbuckle | 3b178b7 | 2015-09-25 23:14:00 +0100 | [diff] [blame] | 1315 | [cocoaView raiseAllKeys]; |
| 1316 | } |
| 1317 | |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1318 | /* We abstract the method called by the Enter Fullscreen menu item |
| 1319 | * because Mac OS 10.7 and higher disables it. This is because of the |
| 1320 | * menu item's old selector's name toggleFullScreen: |
| 1321 | */ |
| 1322 | - (void) doToggleFullScreen:(id)sender |
| 1323 | { |
| 1324 | [self toggleFullScreen:(id)sender]; |
| 1325 | } |
| 1326 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1327 | - (void)toggleFullScreen:(id)sender |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1328 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1329 | COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); |
| 1330 | |
| 1331 | [cocoaView toggleFullScreen:sender]; |
| 1332 | } |
| 1333 | |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 1334 | - (void) setFullGrab:(id)sender |
| 1335 | { |
| 1336 | COCOA_DEBUG("QemuCocoaAppController: setFullGrab\n"); |
| 1337 | |
| 1338 | [cocoaView setFullGrab:sender]; |
| 1339 | } |
| 1340 | |
John Arbuckle | f474790 | 2016-03-23 14:26:17 +0000 | [diff] [blame] | 1341 | /* Tries to find then open the specified filename */ |
| 1342 | - (void) openDocumentation: (NSString *) filename |
| 1343 | { |
| 1344 | /* Where to look for local files */ |
Roman Bolshakov | 8d6fda8 | 2021-01-09 00:38:15 +0300 | [diff] [blame] | 1345 | NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"docs/"}; |
John Arbuckle | f474790 | 2016-03-23 14:26:17 +0000 | [diff] [blame] | 1346 | NSString *full_file_path; |
Roman Bolshakov | 1ff5a06 | 2021-01-02 18:07:21 +0300 | [diff] [blame] | 1347 | NSURL *full_file_url; |
John Arbuckle | f474790 | 2016-03-23 14:26:17 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* iterate thru the possible paths until the file is found */ |
| 1350 | int index; |
| 1351 | for (index = 0; index < ARRAY_SIZE(path_array); index++) { |
| 1352 | full_file_path = [[NSBundle mainBundle] executablePath]; |
| 1353 | full_file_path = [full_file_path stringByDeletingLastPathComponent]; |
| 1354 | full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path, |
| 1355 | path_array[index], filename]; |
Roman Bolshakov | 1ff5a06 | 2021-01-02 18:07:21 +0300 | [diff] [blame] | 1356 | full_file_url = [NSURL fileURLWithPath: full_file_path |
| 1357 | isDirectory: false]; |
| 1358 | if ([[NSWorkspace sharedWorkspace] openURL: full_file_url] == YES) { |
John Arbuckle | f474790 | 2016-03-23 14:26:17 +0000 | [diff] [blame] | 1359 | return; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | /* If none of the paths opened a file */ |
| 1364 | NSBeep(); |
| 1365 | QEMU_Alert(@"Failed to open file"); |
| 1366 | } |
| 1367 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1368 | - (void)showQEMUDoc:(id)sender |
| 1369 | { |
| 1370 | COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n"); |
| 1371 | |
Peter Maydell | 1879f24 | 2020-02-28 15:36:16 +0000 | [diff] [blame] | 1372 | [self openDocumentation: @"index.html"]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1375 | /* Stretches video to fit host monitor size */ |
| 1376 | - (void)zoomToFit:(id) sender |
| 1377 | { |
| 1378 | stretch_video = !stretch_video; |
| 1379 | if (stretch_video == true) { |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 1380 | [sender setState: NSControlStateValueOn]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1381 | } else { |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 1382 | [sender setState: NSControlStateValueOff]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1383 | } |
| 1384 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1385 | |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 1386 | /* Displays the console on the screen */ |
| 1387 | - (void)displayConsole:(id)sender |
| 1388 | { |
| 1389 | console_select([sender tag]); |
| 1390 | } |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1391 | |
| 1392 | /* Pause the guest */ |
| 1393 | - (void)pauseQEMU:(id)sender |
| 1394 | { |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1395 | with_iothread_lock(^{ |
| 1396 | qmp_stop(NULL); |
| 1397 | }); |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1398 | [sender setEnabled: NO]; |
| 1399 | [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES]; |
| 1400 | [self displayPause]; |
| 1401 | } |
| 1402 | |
| 1403 | /* Resume running the guest operating system */ |
| 1404 | - (void)resumeQEMU:(id) sender |
| 1405 | { |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1406 | with_iothread_lock(^{ |
| 1407 | qmp_cont(NULL); |
| 1408 | }); |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1409 | [sender setEnabled: NO]; |
| 1410 | [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES]; |
| 1411 | [self removePause]; |
| 1412 | } |
| 1413 | |
| 1414 | /* Displays the word pause on the screen */ |
| 1415 | - (void)displayPause |
| 1416 | { |
| 1417 | /* Coordinates have to be calculated each time because the window can change its size */ |
| 1418 | int xCoord, yCoord, width, height; |
| 1419 | xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2; |
| 1420 | yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5); |
| 1421 | width = [pauseLabel frame].size.width; |
| 1422 | height = [pauseLabel frame].size.height; |
| 1423 | [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)]; |
| 1424 | [cocoaView addSubview: pauseLabel]; |
| 1425 | } |
| 1426 | |
| 1427 | /* Removes the word pause from the screen */ |
| 1428 | - (void)removePause |
| 1429 | { |
| 1430 | [pauseLabel removeFromSuperview]; |
| 1431 | } |
| 1432 | |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1433 | /* Restarts QEMU */ |
| 1434 | - (void)restartQEMU:(id)sender |
| 1435 | { |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1436 | with_iothread_lock(^{ |
| 1437 | qmp_system_reset(NULL); |
| 1438 | }); |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /* Powers down QEMU */ |
| 1442 | - (void)powerDownQEMU:(id)sender |
| 1443 | { |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1444 | with_iothread_lock(^{ |
| 1445 | qmp_system_powerdown(NULL); |
| 1446 | }); |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1447 | } |
| 1448 | |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1449 | /* Ejects the media. |
| 1450 | * Uses sender's tag to figure out the device to eject. |
| 1451 | */ |
| 1452 | - (void)ejectDeviceMedia:(id)sender |
| 1453 | { |
| 1454 | NSString * drive; |
| 1455 | drive = [sender representedObject]; |
| 1456 | if(drive == nil) { |
| 1457 | NSBeep(); |
| 1458 | QEMU_Alert(@"Failed to find drive to eject!"); |
| 1459 | return; |
| 1460 | } |
| 1461 | |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1462 | __block Error *err = NULL; |
| 1463 | with_iothread_lock(^{ |
| 1464 | qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding], |
| 1465 | false, NULL, false, false, &err); |
| 1466 | }); |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1467 | handleAnyDeviceErrors(err); |
| 1468 | } |
| 1469 | |
| 1470 | /* Displays a dialog box asking the user to select an image file to load. |
| 1471 | * Uses sender's represented object value to figure out which drive to use. |
| 1472 | */ |
| 1473 | - (void)changeDeviceMedia:(id)sender |
| 1474 | { |
| 1475 | /* Find the drive name */ |
| 1476 | NSString * drive; |
| 1477 | drive = [sender representedObject]; |
| 1478 | if(drive == nil) { |
| 1479 | NSBeep(); |
| 1480 | QEMU_Alert(@"Could not find drive!"); |
| 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | /* Display the file open dialog */ |
| 1485 | NSOpenPanel * openPanel; |
| 1486 | openPanel = [NSOpenPanel openPanel]; |
| 1487 | [openPanel setCanChooseFiles: YES]; |
| 1488 | [openPanel setAllowsMultipleSelection: NO]; |
Peter Maydell | b572538 | 2018-05-29 19:15:23 +0100 | [diff] [blame] | 1489 | if([openPanel runModal] == NSModalResponseOK) { |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1490 | NSString * file = [[[openPanel URLs] objectAtIndex: 0] path]; |
| 1491 | if(file == nil) { |
| 1492 | NSBeep(); |
| 1493 | QEMU_Alert(@"Failed to convert URL to file path!"); |
| 1494 | return; |
| 1495 | } |
| 1496 | |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1497 | __block Error *err = NULL; |
| 1498 | with_iothread_lock(^{ |
| 1499 | qmp_blockdev_change_medium(true, |
| 1500 | [drive cStringUsingEncoding: |
| 1501 | NSASCIIStringEncoding], |
| 1502 | false, NULL, |
| 1503 | [file cStringUsingEncoding: |
| 1504 | NSASCIIStringEncoding], |
| 1505 | true, "raw", |
| 1506 | false, 0, |
| 1507 | &err); |
| 1508 | }); |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1509 | handleAnyDeviceErrors(err); |
| 1510 | } |
| 1511 | } |
| 1512 | |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame] | 1513 | /* Verifies if the user really wants to quit */ |
| 1514 | - (BOOL)verifyQuit |
| 1515 | { |
| 1516 | NSAlert *alert = [NSAlert new]; |
| 1517 | [alert autorelease]; |
| 1518 | [alert setMessageText: @"Are you sure you want to quit QEMU?"]; |
| 1519 | [alert addButtonWithTitle: @"Cancel"]; |
| 1520 | [alert addButtonWithTitle: @"Quit"]; |
| 1521 | if([alert runModal] == NSAlertSecondButtonReturn) { |
| 1522 | return YES; |
| 1523 | } else { |
| 1524 | return NO; |
| 1525 | } |
| 1526 | } |
| 1527 | |
Programmingkid | 9e8204b | 2016-08-15 22:11:06 -0400 | [diff] [blame] | 1528 | /* The action method for the About menu item */ |
| 1529 | - (IBAction) do_about_menu_item: (id) sender |
| 1530 | { |
Akihiko Odaki | 99eb313 | 2022-02-27 13:22:41 +0900 | [diff] [blame] | 1531 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 1532 | char *icon_path_c = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/512x512/apps/qemu.png"); |
| 1533 | NSString *icon_path = [NSString stringWithUTF8String:icon_path_c]; |
| 1534 | g_free(icon_path_c); |
| 1535 | NSImage *icon = [[NSImage alloc] initWithContentsOfFile:icon_path]; |
| 1536 | NSString *version = @"QEMU emulator version " QEMU_FULL_VERSION; |
| 1537 | NSString *copyright = @QEMU_COPYRIGHT; |
| 1538 | NSDictionary *options; |
| 1539 | if (icon) { |
| 1540 | options = @{ |
| 1541 | NSAboutPanelOptionApplicationIcon : icon, |
| 1542 | NSAboutPanelOptionApplicationVersion : version, |
| 1543 | @"Copyright" : copyright, |
| 1544 | }; |
| 1545 | [icon release]; |
| 1546 | } else { |
| 1547 | options = @{ |
| 1548 | NSAboutPanelOptionApplicationVersion : version, |
| 1549 | @"Copyright" : copyright, |
| 1550 | }; |
Akihiko Odaki | a0f973f | 2021-03-09 21:22:26 +0900 | [diff] [blame] | 1551 | } |
Akihiko Odaki | 99eb313 | 2022-02-27 13:22:41 +0900 | [diff] [blame] | 1552 | [NSApp orderFrontStandardAboutPanelWithOptions:options]; |
| 1553 | [pool release]; |
Programmingkid | 9e8204b | 2016-08-15 22:11:06 -0400 | [diff] [blame] | 1554 | } |
| 1555 | |
John Arbuckle | e47ec1a | 2017-06-13 23:17:38 -0400 | [diff] [blame] | 1556 | /* Used by the Speed menu items */ |
| 1557 | - (void)adjustSpeed:(id)sender |
| 1558 | { |
| 1559 | int throttle_pct; /* throttle percentage */ |
| 1560 | NSMenu *menu; |
| 1561 | |
| 1562 | menu = [sender menu]; |
| 1563 | if (menu != nil) |
| 1564 | { |
| 1565 | /* Unselect the currently selected item */ |
| 1566 | for (NSMenuItem *item in [menu itemArray]) { |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 1567 | if (item.state == NSControlStateValueOn) { |
| 1568 | [item setState: NSControlStateValueOff]; |
John Arbuckle | e47ec1a | 2017-06-13 23:17:38 -0400 | [diff] [blame] | 1569 | break; |
| 1570 | } |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | // check the menu item |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 1575 | [sender setState: NSControlStateValueOn]; |
John Arbuckle | e47ec1a | 2017-06-13 23:17:38 -0400 | [diff] [blame] | 1576 | |
| 1577 | // get the throttle percentage |
| 1578 | throttle_pct = [sender tag]; |
| 1579 | |
Peter Maydell | 31819e9 | 2019-02-25 10:24:27 +0000 | [diff] [blame] | 1580 | with_iothread_lock(^{ |
| 1581 | cpu_throttle_set(throttle_pct); |
| 1582 | }); |
John Arbuckle | e47ec1a | 2017-06-13 23:17:38 -0400 | [diff] [blame] | 1583 | COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%'); |
| 1584 | } |
| 1585 | |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 1586 | @end |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1587 | |
Peter Maydell | 61a2ed4 | 2019-02-25 10:24:32 +0000 | [diff] [blame] | 1588 | @interface QemuApplication : NSApplication |
| 1589 | @end |
| 1590 | |
| 1591 | @implementation QemuApplication |
| 1592 | - (void)sendEvent:(NSEvent *)event |
| 1593 | { |
| 1594 | COCOA_DEBUG("QemuApplication: sendEvent\n"); |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1595 | if (![cocoaView handleEvent:event]) { |
| 1596 | [super sendEvent: event]; |
| 1597 | } |
Peter Maydell | 61a2ed4 | 2019-02-25 10:24:32 +0000 | [diff] [blame] | 1598 | } |
| 1599 | @end |
| 1600 | |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1601 | static void create_initial_menus(void) |
| 1602 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1603 | // Add menus |
| 1604 | NSMenu *menu; |
| 1605 | NSMenuItem *menuItem; |
| 1606 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1607 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
Akihiko Odaki | 5b6988c | 2022-02-14 18:13:20 +0900 | [diff] [blame] | 1608 | [NSApp setServicesMenu:[[NSMenu alloc] initWithTitle:@"Services"]]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1609 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1610 | // Application menu |
| 1611 | menu = [[NSMenu alloc] initWithTitle:@""]; |
Programmingkid | 9e8204b | 2016-08-15 22:11:06 -0400 | [diff] [blame] | 1612 | [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1613 | [menu addItem:[NSMenuItem separatorItem]]; //Separator |
Akihiko Odaki | 5b6988c | 2022-02-14 18:13:20 +0900 | [diff] [blame] | 1614 | menuItem = [menu addItemWithTitle:@"Services" action:nil keyEquivalent:@""]; |
| 1615 | [menuItem setSubmenu:[NSApp servicesMenu]]; |
| 1616 | [menu addItem:[NSMenuItem separatorItem]]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1617 | [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU |
| 1618 | menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others |
Brendan Shanks | 4ba967a | 2017-04-24 23:29:52 -0700 | [diff] [blame] | 1619 | [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1620 | [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All |
| 1621 | [menu addItem:[NSMenuItem separatorItem]]; //Separator |
| 1622 | [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"]; |
| 1623 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; |
| 1624 | [menuItem setSubmenu:menu]; |
| 1625 | [[NSApp mainMenu] addItem:menuItem]; |
| 1626 | [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+) |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1627 | |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1628 | // Machine menu |
| 1629 | menu = [[NSMenu alloc] initWithTitle: @"Machine"]; |
| 1630 | [menu setAutoenablesItems: NO]; |
| 1631 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]]; |
| 1632 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease]; |
| 1633 | [menu addItem: menuItem]; |
| 1634 | [menuItem setEnabled: NO]; |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1635 | [menu addItem: [NSMenuItem separatorItem]]; |
| 1636 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]]; |
| 1637 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]]; |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1638 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease]; |
| 1639 | [menuItem setSubmenu:menu]; |
| 1640 | [[NSApp mainMenu] addItem:menuItem]; |
| 1641 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1642 | // View menu |
| 1643 | menu = [[NSMenu alloc] initWithTitle:@"View"]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1644 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen |
| 1645 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1646 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease]; |
| 1647 | [menuItem setSubmenu:menu]; |
| 1648 | [[NSApp mainMenu] addItem:menuItem]; |
| 1649 | |
John Arbuckle | e47ec1a | 2017-06-13 23:17:38 -0400 | [diff] [blame] | 1650 | // Speed menu |
| 1651 | menu = [[NSMenu alloc] initWithTitle:@"Speed"]; |
| 1652 | |
| 1653 | // Add the rest of the Speed menu items |
| 1654 | int p, percentage, throttle_pct; |
| 1655 | for (p = 10; p >= 0; p--) |
| 1656 | { |
| 1657 | percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item |
| 1658 | |
| 1659 | menuItem = [[[NSMenuItem alloc] |
| 1660 | initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]; |
| 1661 | |
| 1662 | if (percentage == 100) { |
Brendan Shanks | 5e24600 | 2019-01-31 23:12:25 -0800 | [diff] [blame] | 1663 | [menuItem setState: NSControlStateValueOn]; |
John Arbuckle | e47ec1a | 2017-06-13 23:17:38 -0400 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | /* Calculate the throttle percentage */ |
| 1667 | throttle_pct = -1 * percentage + 100; |
| 1668 | |
| 1669 | [menuItem setTag: throttle_pct]; |
| 1670 | [menu addItem: menuItem]; |
| 1671 | } |
| 1672 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease]; |
| 1673 | [menuItem setSubmenu:menu]; |
| 1674 | [[NSApp mainMenu] addItem:menuItem]; |
| 1675 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1676 | // Window menu |
| 1677 | menu = [[NSMenu alloc] initWithTitle:@"Window"]; |
| 1678 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize |
| 1679 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; |
| 1680 | [menuItem setSubmenu:menu]; |
| 1681 | [[NSApp mainMenu] addItem:menuItem]; |
| 1682 | [NSApp setWindowsMenu:menu]; |
| 1683 | |
| 1684 | // Help menu |
| 1685 | menu = [[NSMenu alloc] initWithTitle:@"Help"]; |
| 1686 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1687 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; |
| 1688 | [menuItem setSubmenu:menu]; |
| 1689 | [[NSApp mainMenu] addItem:menuItem]; |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Peter Maydell | 8b00e4e | 2019-02-25 10:24:30 +0000 | [diff] [blame] | 1692 | /* Returns a name for a given console */ |
| 1693 | static NSString * getConsoleName(QemuConsole * console) |
| 1694 | { |
Akihiko Odaki | ca51160 | 2022-02-15 09:03:05 +0100 | [diff] [blame] | 1695 | g_autofree char *label = qemu_console_get_label(console); |
| 1696 | |
| 1697 | return [NSString stringWithUTF8String:label]; |
Peter Maydell | 8b00e4e | 2019-02-25 10:24:30 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* Add an entry to the View menu for each console */ |
| 1701 | static void add_console_menu_entries(void) |
| 1702 | { |
| 1703 | NSMenu *menu; |
| 1704 | NSMenuItem *menuItem; |
| 1705 | int index = 0; |
| 1706 | |
| 1707 | menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu]; |
| 1708 | |
| 1709 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1710 | |
| 1711 | while (qemu_console_lookup_by_index(index) != NULL) { |
| 1712 | menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index)) |
| 1713 | action: @selector(displayConsole:) keyEquivalent: @""] autorelease]; |
| 1714 | [menuItem setTag: index]; |
| 1715 | [menu addItem: menuItem]; |
| 1716 | index++; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | /* Make menu items for all removable devices. |
| 1721 | * Each device is given an 'Eject' and 'Change' menu item. |
| 1722 | */ |
| 1723 | static void addRemovableDevicesMenuItems(void) |
| 1724 | { |
| 1725 | NSMenu *menu; |
| 1726 | NSMenuItem *menuItem; |
| 1727 | BlockInfoList *currentDevice, *pointerToFree; |
| 1728 | NSString *deviceName; |
| 1729 | |
| 1730 | currentDevice = qmp_query_block(NULL); |
| 1731 | pointerToFree = currentDevice; |
Peter Maydell | 8b00e4e | 2019-02-25 10:24:30 +0000 | [diff] [blame] | 1732 | |
| 1733 | menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; |
| 1734 | |
| 1735 | // Add a separator between related groups of menu items |
| 1736 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1737 | |
| 1738 | // Set the attributes to the "Removable Media" menu item |
| 1739 | NSString *titleString = @"Removable Media"; |
| 1740 | NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString]; |
| 1741 | NSColor *newColor = [NSColor blackColor]; |
| 1742 | NSFontManager *fontManager = [NSFontManager sharedFontManager]; |
| 1743 | NSFont *font = [fontManager fontWithFamily:@"Helvetica" |
| 1744 | traits:NSBoldFontMask|NSItalicFontMask |
| 1745 | weight:0 |
| 1746 | size:14]; |
| 1747 | [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])]; |
| 1748 | [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])]; |
| 1749 | [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])]; |
| 1750 | |
| 1751 | // Add the "Removable Media" menu item |
| 1752 | menuItem = [NSMenuItem new]; |
| 1753 | [menuItem setAttributedTitle: attString]; |
| 1754 | [menuItem setEnabled: NO]; |
| 1755 | [menu addItem: menuItem]; |
| 1756 | |
| 1757 | /* Loop through all the block devices in the emulator */ |
| 1758 | while (currentDevice) { |
| 1759 | deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain]; |
| 1760 | |
| 1761 | if(currentDevice->value->removable) { |
| 1762 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device] |
| 1763 | action: @selector(changeDeviceMedia:) |
| 1764 | keyEquivalent: @""]; |
| 1765 | [menu addItem: menuItem]; |
| 1766 | [menuItem setRepresentedObject: deviceName]; |
| 1767 | [menuItem autorelease]; |
| 1768 | |
| 1769 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device] |
| 1770 | action: @selector(ejectDeviceMedia:) |
| 1771 | keyEquivalent: @""]; |
| 1772 | [menu addItem: menuItem]; |
| 1773 | [menuItem setRepresentedObject: deviceName]; |
| 1774 | [menuItem autorelease]; |
| 1775 | } |
| 1776 | currentDevice = currentDevice->next; |
| 1777 | } |
| 1778 | qapi_free_BlockInfoList(pointerToFree); |
| 1779 | } |
| 1780 | |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 1781 | @interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner> |
| 1782 | @end |
| 1783 | |
| 1784 | @implementation QemuCocoaPasteboardTypeOwner |
| 1785 | |
| 1786 | - (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSPasteboardType)type |
| 1787 | { |
| 1788 | if (type != NSPasteboardTypeString) { |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | with_iothread_lock(^{ |
| 1793 | QemuClipboardInfo *info = qemu_clipboard_info_ref(cbinfo); |
| 1794 | qemu_event_reset(&cbevent); |
| 1795 | qemu_clipboard_request(info, QEMU_CLIPBOARD_TYPE_TEXT); |
| 1796 | |
| 1797 | while (info == cbinfo && |
| 1798 | info->types[QEMU_CLIPBOARD_TYPE_TEXT].available && |
| 1799 | info->types[QEMU_CLIPBOARD_TYPE_TEXT].data == NULL) { |
| 1800 | qemu_mutex_unlock_iothread(); |
| 1801 | qemu_event_wait(&cbevent); |
| 1802 | qemu_mutex_lock_iothread(); |
| 1803 | } |
| 1804 | |
| 1805 | if (info == cbinfo) { |
| 1806 | NSData *data = [[NSData alloc] initWithBytes:info->types[QEMU_CLIPBOARD_TYPE_TEXT].data |
| 1807 | length:info->types[QEMU_CLIPBOARD_TYPE_TEXT].size]; |
| 1808 | [sender setData:data forType:NSPasteboardTypeString]; |
| 1809 | [data release]; |
| 1810 | } |
| 1811 | |
| 1812 | qemu_clipboard_info_unref(info); |
| 1813 | }); |
| 1814 | } |
| 1815 | |
| 1816 | @end |
| 1817 | |
| 1818 | static QemuCocoaPasteboardTypeOwner *cbowner; |
| 1819 | |
| 1820 | static void cocoa_clipboard_notify(Notifier *notifier, void *data); |
| 1821 | static void cocoa_clipboard_request(QemuClipboardInfo *info, |
| 1822 | QemuClipboardType type); |
| 1823 | |
| 1824 | static QemuClipboardPeer cbpeer = { |
| 1825 | .name = "cocoa", |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 1826 | .notifier = { .notify = cocoa_clipboard_notify }, |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 1827 | .request = cocoa_clipboard_request |
| 1828 | }; |
| 1829 | |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 1830 | static void cocoa_clipboard_update_info(QemuClipboardInfo *info) |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 1831 | { |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 1832 | if (info->owner == &cbpeer || info->selection != QEMU_CLIPBOARD_SELECTION_CLIPBOARD) { |
| 1833 | return; |
| 1834 | } |
| 1835 | |
| 1836 | if (info != cbinfo) { |
| 1837 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 1838 | qemu_clipboard_info_unref(cbinfo); |
| 1839 | cbinfo = qemu_clipboard_info_ref(info); |
| 1840 | cbchangecount = [[NSPasteboard generalPasteboard] declareTypes:@[NSPasteboardTypeString] owner:cbowner]; |
| 1841 | [pool release]; |
| 1842 | } |
| 1843 | |
| 1844 | qemu_event_set(&cbevent); |
| 1845 | } |
| 1846 | |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 1847 | static void cocoa_clipboard_notify(Notifier *notifier, void *data) |
| 1848 | { |
| 1849 | QemuClipboardNotify *notify = data; |
| 1850 | |
| 1851 | switch (notify->type) { |
| 1852 | case QEMU_CLIPBOARD_UPDATE_INFO: |
| 1853 | cocoa_clipboard_update_info(notify->info); |
| 1854 | return; |
Marc-André Lureau | 505dbf9 | 2021-07-19 19:49:56 +0400 | [diff] [blame] | 1855 | case QEMU_CLIPBOARD_RESET_SERIAL: |
| 1856 | /* ignore */ |
| 1857 | return; |
Marc-André Lureau | 1b17f1e | 2021-07-19 19:42:15 +0400 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 1861 | static void cocoa_clipboard_request(QemuClipboardInfo *info, |
| 1862 | QemuClipboardType type) |
| 1863 | { |
| 1864 | NSData *text; |
| 1865 | |
| 1866 | switch (type) { |
| 1867 | case QEMU_CLIPBOARD_TYPE_TEXT: |
| 1868 | text = [[NSPasteboard generalPasteboard] dataForType:NSPasteboardTypeString]; |
| 1869 | if (text) { |
| 1870 | qemu_clipboard_set_data(&cbpeer, info, type, |
| 1871 | [text length], [text bytes], true); |
| 1872 | [text release]; |
| 1873 | } |
| 1874 | break; |
| 1875 | default: |
| 1876 | break; |
| 1877 | } |
| 1878 | } |
| 1879 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1880 | /* |
| 1881 | * The startup process for the OSX/Cocoa UI is complicated, because |
| 1882 | * OSX insists that the UI runs on the initial main thread, and so we |
| 1883 | * need to start a second thread which runs the vl.c qemu_main(): |
| 1884 | * |
| 1885 | * Initial thread: 2nd thread: |
| 1886 | * in main(): |
| 1887 | * create qemu-main thread |
| 1888 | * wait on display_init semaphore |
| 1889 | * call qemu_main() |
| 1890 | * ... |
| 1891 | * in cocoa_display_init(): |
| 1892 | * post the display_init semaphore |
| 1893 | * wait on app_started semaphore |
| 1894 | * create application, menus, etc |
| 1895 | * enter OSX run loop |
| 1896 | * in applicationDidFinishLaunching: |
| 1897 | * post app_started semaphore |
| 1898 | * tell main thread to fullscreen if needed |
| 1899 | * [...] |
| 1900 | * run qemu main-loop |
| 1901 | * |
| 1902 | * We do this in two stages so that we don't do the creation of the |
| 1903 | * GUI application menus and so on for command line options like --help |
| 1904 | * where we want to just print text to stdout and exit immediately. |
| 1905 | */ |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1906 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1907 | static void *call_qemu_main(void *opaque) |
| 1908 | { |
| 1909 | int status; |
| 1910 | |
| 1911 | COCOA_DEBUG("Second thread: calling qemu_main()\n"); |
| 1912 | status = qemu_main(gArgc, gArgv, *_NSGetEnviron()); |
| 1913 | COCOA_DEBUG("Second thread: qemu_main() returned, exiting\n"); |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 1914 | [cbowner release]; |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1915 | exit(status); |
| 1916 | } |
| 1917 | |
Akihiko Odaki | 40a9aad | 2021-07-09 01:56:19 +0900 | [diff] [blame] | 1918 | int main (int argc, char **argv) { |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1919 | QemuThread thread; |
| 1920 | |
| 1921 | COCOA_DEBUG("Entered main()\n"); |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1922 | gArgc = argc; |
Akihiko Odaki | 40a9aad | 2021-07-09 01:56:19 +0900 | [diff] [blame] | 1923 | gArgv = argv; |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1924 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1925 | qemu_sem_init(&display_init_sem, 0); |
| 1926 | qemu_sem_init(&app_started_sem, 0); |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1927 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1928 | qemu_thread_create(&thread, "qemu_main", call_qemu_main, |
| 1929 | NULL, QEMU_THREAD_DETACHED); |
| 1930 | |
| 1931 | COCOA_DEBUG("Main thread: waiting for display_init_sem\n"); |
| 1932 | qemu_sem_wait(&display_init_sem); |
| 1933 | COCOA_DEBUG("Main thread: initializing app\n"); |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1934 | |
| 1935 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 1936 | |
| 1937 | // Pull this console process up to being a fully-fledged graphical |
| 1938 | // app with a menubar and Dock icon |
| 1939 | ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 1940 | TransformProcessType(&psn, kProcessTransformToForegroundApplication); |
| 1941 | |
Peter Maydell | 61a2ed4 | 2019-02-25 10:24:32 +0000 | [diff] [blame] | 1942 | [QemuApplication sharedApplication]; |
Peter Maydell | c6fd6c7 | 2019-02-25 10:24:29 +0000 | [diff] [blame] | 1943 | |
| 1944 | create_initial_menus(); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1945 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1946 | /* |
| 1947 | * Create the menu entries which depend on QEMU state (for consoles |
| 1948 | * and removeable devices). These make calls back into QEMU functions, |
| 1949 | * which is OK because at this point we know that the second thread |
| 1950 | * holds the iothread lock and is synchronously waiting for us to |
| 1951 | * finish. |
| 1952 | */ |
| 1953 | add_console_menu_entries(); |
| 1954 | addRemovableDevicesMenuItems(); |
| 1955 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1956 | // Create an Application controller |
| 1957 | QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init]; |
| 1958 | [NSApp setDelegate:appController]; |
| 1959 | |
| 1960 | // Start the main event loop |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1961 | COCOA_DEBUG("Main thread: entering OSX run loop\n"); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1962 | [NSApp run]; |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1963 | COCOA_DEBUG("Main thread: left OSX run loop, exiting\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1964 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1965 | [appController release]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1966 | [pool release]; |
bellard | cae41b1 | 2006-05-22 21:25:04 +0000 | [diff] [blame] | 1967 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1968 | return 0; |
| 1969 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1970 | |
| 1971 | |
| 1972 | |
| 1973 | #pragma mark qemu |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1974 | static void cocoa_update(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1975 | int x, int y, int w, int h) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1976 | { |
| 1977 | COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); |
| 1978 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1979 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 1980 | NSRect rect; |
| 1981 | if ([cocoaView cdx] == 1.0) { |
| 1982 | rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); |
| 1983 | } else { |
| 1984 | rect = NSMakeRect( |
| 1985 | x * [cocoaView cdx], |
| 1986 | ([cocoaView gscreen].height - y - h) * [cocoaView cdy], |
| 1987 | w * [cocoaView cdx], |
| 1988 | h * [cocoaView cdy]); |
| 1989 | } |
| 1990 | [cocoaView setNeedsDisplayInRect:rect]; |
| 1991 | }); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Gerd Hoffmann | c12aeb8 | 2013-02-28 15:03:04 +0100 | [diff] [blame] | 1994 | static void cocoa_switch(DisplayChangeListener *dcl, |
Gerd Hoffmann | c12aeb8 | 2013-02-28 15:03:04 +0100 | [diff] [blame] | 1995 | DisplaySurface *surface) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1996 | { |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 1997 | pixman_image_t *image = surface->image; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1998 | |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1999 | COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 2000 | |
| 2001 | // The DisplaySurface will be freed as soon as this callback returns. |
| 2002 | // We take a reference to the underlying pixman image here so it does |
| 2003 | // not disappear from under our feet; the switchSurface method will |
| 2004 | // deref the old image when it is done with it. |
| 2005 | pixman_image_ref(image); |
| 2006 | |
| 2007 | dispatch_async(dispatch_get_main_queue(), ^{ |
Peter Maydell | 8d65dee | 2022-02-24 10:13:29 +0000 | [diff] [blame] | 2008 | [cocoaView updateUIInfo]; |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 2009 | [cocoaView switchSurface:image]; |
| 2010 | }); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 2013 | static void cocoa_refresh(DisplayChangeListener *dcl) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2014 | { |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 2015 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 2016 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2017 | COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n"); |
John Arbuckle | 468a895 | 2015-10-13 21:51:18 +0100 | [diff] [blame] | 2018 | graphic_hw_update(NULL); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2019 | |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 2020 | if (qemu_input_is_absolute()) { |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 2021 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 2022 | if (![cocoaView isAbsoluteEnabled]) { |
| 2023 | if ([cocoaView isMouseGrabbed]) { |
| 2024 | [cocoaView ungrabMouse]; |
| 2025 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2026 | } |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 2027 | [cocoaView setAbsoluteEnabled:YES]; |
| 2028 | }); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2029 | } |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 2030 | |
| 2031 | if (cbchangecount != [[NSPasteboard generalPasteboard] changeCount]) { |
| 2032 | qemu_clipboard_info_unref(cbinfo); |
| 2033 | cbinfo = qemu_clipboard_info_new(&cbpeer, QEMU_CLIPBOARD_SELECTION_CLIPBOARD); |
| 2034 | if ([[NSPasteboard generalPasteboard] availableTypeFromArray:@[NSPasteboardTypeString]]) { |
| 2035 | cbinfo->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true; |
| 2036 | } |
| 2037 | qemu_clipboard_update(cbinfo); |
| 2038 | cbchangecount = [[NSPasteboard generalPasteboard] changeCount]; |
| 2039 | qemu_event_set(&cbevent); |
| 2040 | } |
| 2041 | |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 2042 | [pool release]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Gerd Hoffmann | 5013b9e | 2018-03-01 11:05:37 +0100 | [diff] [blame] | 2045 | static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2046 | { |
| 2047 | COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); |
| 2048 | |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 2049 | /* Tell main thread to go ahead and create the app and enter the run loop */ |
| 2050 | qemu_sem_post(&display_init_sem); |
| 2051 | qemu_sem_wait(&app_started_sem); |
| 2052 | COCOA_DEBUG("cocoa_display_init: app start completed\n"); |
| 2053 | |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 2054 | QemuCocoaAppController *controller = (QemuCocoaAppController *)[[NSApplication sharedApplication] delegate]; |
Programmingkid | 43227af | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 2055 | /* if fullscreen mode is to be used */ |
Gerd Hoffmann | 767f9bf | 2018-02-02 12:10:19 +0100 | [diff] [blame] | 2056 | if (opts->has_full_screen && opts->full_screen) { |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 2057 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 2058 | [NSApp activateIgnoringOtherApps: YES]; |
Gustavo Noronha Silva | f844cdb | 2022-03-06 21:11:18 +0900 | [diff] [blame^] | 2059 | [controller toggleFullScreen: nil]; |
| 2060 | }); |
| 2061 | } |
| 2062 | if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) { |
| 2063 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 2064 | [controller setFullGrab: nil]; |
Peter Maydell | 5588840 | 2019-02-25 10:24:33 +0000 | [diff] [blame] | 2065 | }); |
Programmingkid | 43227af | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 2066 | } |
Carwyn Ellis | 69221df | 2022-01-02 17:41:53 +0000 | [diff] [blame] | 2067 | |
Gerd Hoffmann | 3487da6 | 2020-02-06 12:27:50 +0100 | [diff] [blame] | 2068 | if (opts->has_show_cursor && opts->show_cursor) { |
| 2069 | cursor_hide = 0; |
| 2070 | } |
Programmingkid | 43227af | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 2071 | |
Carwyn Ellis | 48941a5 | 2022-01-02 17:41:52 +0000 | [diff] [blame] | 2072 | if (opts->u.cocoa.has_left_command_key && !opts->u.cocoa.left_command_key) { |
| 2073 | left_command_key_enabled = 0; |
| 2074 | } |
| 2075 | |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 2076 | // register vga output callbacks |
Akihiko Odaki | cc7859c | 2021-02-19 17:44:19 +0900 | [diff] [blame] | 2077 | register_displaychangelistener(&dcl); |
Akihiko Odaki | 7e3e20d | 2021-06-16 23:19:54 +0900 | [diff] [blame] | 2078 | |
| 2079 | qemu_event_init(&cbevent, false); |
| 2080 | cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init]; |
| 2081 | qemu_clipboard_peer_register(&cbpeer); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 2082 | } |
Gerd Hoffmann | 5013b9e | 2018-03-01 11:05:37 +0100 | [diff] [blame] | 2083 | |
| 2084 | static QemuDisplay qemu_display_cocoa = { |
| 2085 | .type = DISPLAY_TYPE_COCOA, |
| 2086 | .init = cocoa_display_init, |
| 2087 | }; |
| 2088 | |
| 2089 | static void register_cocoa(void) |
| 2090 | { |
| 2091 | qemu_display_register(&qemu_display_cocoa); |
| 2092 | } |
| 2093 | |
| 2094 | type_init(register_cocoa); |