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