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