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