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