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