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