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