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