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