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