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