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 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 25 | #import <Cocoa/Cocoa.h> |
Andreas Färber | 3bbbee1 | 2011-05-29 19:42:51 +0200 | [diff] [blame] | 26 | #include <crt_externs.h> |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 27 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 28 | #include "qemu-common.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 29 | #include "ui/console.h" |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 30 | #include "ui/input.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 31 | #include "sysemu/sysemu.h" |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 32 | #include "qmp-commands.h" |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 33 | #include "sysemu/blockdev.h" |
bellard | da4dbf7 | 2005-03-02 22:22:43 +0000 | [diff] [blame] | 34 | |
Andreas Färber | 44e4c0b | 2009-12-13 00:45:40 +0100 | [diff] [blame] | 35 | #ifndef MAC_OS_X_VERSION_10_5 |
| 36 | #define MAC_OS_X_VERSION_10_5 1050 |
| 37 | #endif |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 38 | #ifndef MAC_OS_X_VERSION_10_6 |
| 39 | #define MAC_OS_X_VERSION_10_6 1060 |
| 40 | #endif |
Peter Maydell | 81801ae | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 41 | #ifndef MAC_OS_X_VERSION_10_10 |
| 42 | #define MAC_OS_X_VERSION_10_10 101000 |
| 43 | #endif |
Andreas Färber | 44e4c0b | 2009-12-13 00:45:40 +0100 | [diff] [blame] | 44 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 45 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 46 | //#define DEBUG |
| 47 | |
| 48 | #ifdef DEBUG |
| 49 | #define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); } |
| 50 | #else |
| 51 | #define COCOA_DEBUG(...) ((void) 0) |
| 52 | #endif |
| 53 | |
| 54 | #define cgrect(nsrect) (*(CGRect *)&(nsrect)) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 55 | |
| 56 | typedef struct { |
| 57 | int width; |
| 58 | int height; |
| 59 | int bitsPerComponent; |
| 60 | int bitsPerPixel; |
| 61 | } QEMUScreen; |
| 62 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 63 | NSWindow *normalWindow; |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 64 | static DisplayChangeListener *dcl; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 65 | static int last_buttons; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 66 | |
| 67 | int gArgc; |
| 68 | char **gArgv; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 69 | bool stretch_video; |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 70 | NSTextField *pauseLabel; |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 71 | NSArray * supportedImageFileTypes; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 72 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 73 | // keymap conversion |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 74 | int keymap[] = |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 75 | { |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 76 | // SdlI macI macH SdlH 104xtH 104xtC sdl |
| 77 | 30, // 0 0x00 0x1e A QZ_a |
| 78 | 31, // 1 0x01 0x1f S QZ_s |
| 79 | 32, // 2 0x02 0x20 D QZ_d |
| 80 | 33, // 3 0x03 0x21 F QZ_f |
| 81 | 35, // 4 0x04 0x23 H QZ_h |
| 82 | 34, // 5 0x05 0x22 G QZ_g |
| 83 | 44, // 6 0x06 0x2c Z QZ_z |
| 84 | 45, // 7 0x07 0x2d X QZ_x |
| 85 | 46, // 8 0x08 0x2e C QZ_c |
| 86 | 47, // 9 0x09 0x2f V QZ_v |
| 87 | 0, // 10 0x0A Undefined |
| 88 | 48, // 11 0x0B 0x30 B QZ_b |
| 89 | 16, // 12 0x0C 0x10 Q QZ_q |
| 90 | 17, // 13 0x0D 0x11 W QZ_w |
| 91 | 18, // 14 0x0E 0x12 E QZ_e |
| 92 | 19, // 15 0x0F 0x13 R QZ_r |
| 93 | 21, // 16 0x10 0x15 Y QZ_y |
| 94 | 20, // 17 0x11 0x14 T QZ_t |
| 95 | 2, // 18 0x12 0x02 1 QZ_1 |
| 96 | 3, // 19 0x13 0x03 2 QZ_2 |
| 97 | 4, // 20 0x14 0x04 3 QZ_3 |
| 98 | 5, // 21 0x15 0x05 4 QZ_4 |
| 99 | 7, // 22 0x16 0x07 6 QZ_6 |
| 100 | 6, // 23 0x17 0x06 5 QZ_5 |
| 101 | 13, // 24 0x18 0x0d = QZ_EQUALS |
| 102 | 10, // 25 0x19 0x0a 9 QZ_9 |
| 103 | 8, // 26 0x1A 0x08 7 QZ_7 |
| 104 | 12, // 27 0x1B 0x0c - QZ_MINUS |
| 105 | 9, // 28 0x1C 0x09 8 QZ_8 |
| 106 | 11, // 29 0x1D 0x0b 0 QZ_0 |
| 107 | 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET |
| 108 | 24, // 31 0x1F 0x18 O QZ_o |
| 109 | 22, // 32 0x20 0x16 U QZ_u |
| 110 | 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET |
| 111 | 23, // 34 0x22 0x17 I QZ_i |
| 112 | 25, // 35 0x23 0x19 P QZ_p |
| 113 | 28, // 36 0x24 0x1c ENTER QZ_RETURN |
| 114 | 38, // 37 0x25 0x26 L QZ_l |
| 115 | 36, // 38 0x26 0x24 J QZ_j |
| 116 | 40, // 39 0x27 0x28 ' QZ_QUOTE |
| 117 | 37, // 40 0x28 0x25 K QZ_k |
| 118 | 39, // 41 0x29 0x27 ; QZ_SEMICOLON |
| 119 | 43, // 42 0x2A 0x2b \ QZ_BACKSLASH |
| 120 | 51, // 43 0x2B 0x33 , QZ_COMMA |
| 121 | 53, // 44 0x2C 0x35 / QZ_SLASH |
| 122 | 49, // 45 0x2D 0x31 N QZ_n |
| 123 | 50, // 46 0x2E 0x32 M QZ_m |
| 124 | 52, // 47 0x2F 0x34 . QZ_PERIOD |
| 125 | 15, // 48 0x30 0x0f TAB QZ_TAB |
| 126 | 57, // 49 0x31 0x39 SPACE QZ_SPACE |
| 127 | 41, // 50 0x32 0x29 ` QZ_BACKQUOTE |
| 128 | 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE |
| 129 | 0, // 52 0x34 Undefined |
| 130 | 1, // 53 0x35 0x01 ESC QZ_ESCAPE |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 131 | 220, // 54 0x36 0xdc E0,5C R GUI QZ_RMETA |
| 132 | 219, // 55 0x37 0xdb E0,5B L GUI QZ_LMETA |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 133 | 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT |
| 134 | 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK |
| 135 | 56, // 58 0x3A 0x38 L ALT QZ_LALT |
| 136 | 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL |
| 137 | 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT |
| 138 | 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT |
| 139 | 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL |
| 140 | 0, // 63 0x3F Undefined |
| 141 | 0, // 64 0x40 Undefined |
| 142 | 0, // 65 0x41 Undefined |
| 143 | 0, // 66 0x42 Undefined |
| 144 | 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY |
| 145 | 0, // 68 0x44 Undefined |
| 146 | 78, // 69 0x45 0x4e KP + QZ_KP_PLUS |
| 147 | 0, // 70 0x46 Undefined |
| 148 | 69, // 71 0x47 0x45 NUM QZ_NUMLOCK |
| 149 | 0, // 72 0x48 Undefined |
| 150 | 0, // 73 0x49 Undefined |
| 151 | 0, // 74 0x4A Undefined |
| 152 | 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE |
| 153 | 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER |
| 154 | 0, // 77 0x4D undefined |
| 155 | 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS |
| 156 | 0, // 79 0x4F Undefined |
| 157 | 0, // 80 0x50 Undefined |
| 158 | 0, // 81 0x51 QZ_KP_EQUALS |
| 159 | 82, // 82 0x52 0x52 KP 0 QZ_KP0 |
| 160 | 79, // 83 0x53 0x4f KP 1 QZ_KP1 |
| 161 | 80, // 84 0x54 0x50 KP 2 QZ_KP2 |
| 162 | 81, // 85 0x55 0x51 KP 3 QZ_KP3 |
| 163 | 75, // 86 0x56 0x4b KP 4 QZ_KP4 |
| 164 | 76, // 87 0x57 0x4c KP 5 QZ_KP5 |
| 165 | 77, // 88 0x58 0x4d KP 6 QZ_KP6 |
| 166 | 71, // 89 0x59 0x47 KP 7 QZ_KP7 |
| 167 | 0, // 90 0x5A Undefined |
| 168 | 72, // 91 0x5B 0x48 KP 8 QZ_KP8 |
| 169 | 73, // 92 0x5C 0x49 KP 9 QZ_KP9 |
| 170 | 0, // 93 0x5D Undefined |
| 171 | 0, // 94 0x5E Undefined |
| 172 | 0, // 95 0x5F Undefined |
| 173 | 63, // 96 0x60 0x3f F5 QZ_F5 |
| 174 | 64, // 97 0x61 0x40 F6 QZ_F6 |
| 175 | 65, // 98 0x62 0x41 F7 QZ_F7 |
| 176 | 61, // 99 0x63 0x3d F3 QZ_F3 |
| 177 | 66, // 100 0x64 0x42 F8 QZ_F8 |
| 178 | 67, // 101 0x65 0x43 F9 QZ_F9 |
| 179 | 0, // 102 0x66 Undefined |
| 180 | 87, // 103 0x67 0x57 F11 QZ_F11 |
| 181 | 0, // 104 0x68 Undefined |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 182 | 183,// 105 0x69 0xb7 QZ_PRINT |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 183 | 0, // 106 0x6A Undefined |
| 184 | 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK |
| 185 | 0, // 108 0x6C Undefined |
| 186 | 68, // 109 0x6D 0x44 F10 QZ_F10 |
| 187 | 0, // 110 0x6E Undefined |
| 188 | 88, // 111 0x6F 0x58 F12 QZ_F12 |
| 189 | 0, // 112 0x70 Undefined |
| 190 | 110,// 113 0x71 0x0 QZ_PAUSE |
| 191 | 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT |
| 192 | 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME |
| 193 | 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP |
| 194 | 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE |
| 195 | 62, // 118 0x76 0x3e F4 QZ_F4 |
| 196 | 207,// 119 0x77 0xcf E0,4f END QZ_END |
| 197 | 60, // 120 0x78 0x3c F2 QZ_F2 |
| 198 | 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN |
| 199 | 59, // 122 0x7A 0x3b F1 QZ_F1 |
| 200 | 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT |
| 201 | 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT |
| 202 | 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN |
| 203 | 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP |
| 204 | /* completed according to http://www.libsdl.org/cgi/cvsweb.cgi/SDL12/src/video/quartz/SDL_QuartzKeys.h?rev=1.6&content-type=text/x-cvsweb-markup */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 205 | |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 206 | /* Additional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */ |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 207 | /* |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 208 | 221 // 0xdd e0,5d APPS |
| 209 | // E0,2A,E0,37 PRNT SCRN |
| 210 | // E1,1D,45,E1,9D,C5 PAUSE |
| 211 | 83 // 0x53 0x53 KP . |
| 212 | // ACPI Scan Codes |
| 213 | 222 // 0xde E0, 5E Power |
| 214 | 223 // 0xdf E0, 5F Sleep |
| 215 | 227 // 0xe3 E0, 63 Wake |
| 216 | // Windows Multimedia Scan Codes |
| 217 | 153 // 0x99 E0, 19 Next Track |
| 218 | 144 // 0x90 E0, 10 Previous Track |
| 219 | 164 // 0xa4 E0, 24 Stop |
| 220 | 162 // 0xa2 E0, 22 Play/Pause |
| 221 | 160 // 0xa0 E0, 20 Mute |
| 222 | 176 // 0xb0 E0, 30 Volume Up |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 223 | 174 // 0xae E0, 2E Volume Down |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 224 | 237 // 0xed E0, 6D Media Select |
| 225 | 236 // 0xec E0, 6C E-Mail |
| 226 | 161 // 0xa1 E0, 21 Calculator |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 227 | 235 // 0xeb E0, 6B My Computer |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 228 | 229 // 0xe5 E0, 65 WWW Search |
| 229 | 178 // 0xb2 E0, 32 WWW Home |
| 230 | 234 // 0xea E0, 6A WWW Back |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 231 | 233 // 0xe9 E0, 69 WWW Forward |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 232 | 232 // 0xe8 E0, 68 WWW Stop |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 233 | 231 // 0xe7 E0, 67 WWW Refresh |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 234 | 230 // 0xe6 E0, 66 WWW Favorites |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 235 | */ |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
Andreas Färber | 77047bb | 2009-12-13 00:55:53 +0100 | [diff] [blame] | 238 | static int cocoa_keycode_to_qemu(int keycode) |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 239 | { |
Stefan Weil | 5d70192 | 2013-12-07 14:48:05 +0100 | [diff] [blame] | 240 | if (ARRAY_SIZE(keymap) <= keycode) { |
Peter Maydell | 01cc4e6 | 2013-12-08 22:59:04 +0000 | [diff] [blame] | 241 | fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | return keymap[keycode]; |
| 245 | } |
| 246 | |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 247 | /* Displays an alert dialog box with the specified message */ |
| 248 | static void QEMU_Alert(NSString *message) |
| 249 | { |
| 250 | NSAlert *alert; |
| 251 | alert = [NSAlert new]; |
| 252 | [alert setMessageText: message]; |
| 253 | [alert runModal]; |
| 254 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 255 | |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 256 | /* Handles any errors that happen with a device transaction */ |
| 257 | static void handleAnyDeviceErrors(Error * err) |
| 258 | { |
| 259 | if (err) { |
| 260 | QEMU_Alert([NSString stringWithCString: error_get_pretty(err) |
| 261 | encoding: NSASCIIStringEncoding]); |
| 262 | error_free(err); |
| 263 | } |
| 264 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 265 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 266 | /* |
| 267 | ------------------------------------------------------ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 268 | QemuCocoaView |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 269 | ------------------------------------------------------ |
| 270 | */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 271 | @interface QemuCocoaView : NSView |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 272 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 273 | QEMUScreen screen; |
| 274 | NSWindow *fullScreenWindow; |
| 275 | float cx,cy,cw,ch,cdx,cdy; |
| 276 | CGDataProviderRef dataProviderRef; |
| 277 | int modifiers_state[256]; |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 278 | BOOL isMouseGrabbed; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 279 | BOOL isFullscreen; |
| 280 | BOOL isAbsoluteEnabled; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 281 | BOOL isMouseDeassociated; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 282 | } |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 283 | - (void) switchSurface:(DisplaySurface *)surface; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 284 | - (void) grabMouse; |
| 285 | - (void) ungrabMouse; |
| 286 | - (void) toggleFullScreen:(id)sender; |
| 287 | - (void) handleEvent:(NSEvent *)event; |
| 288 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 289 | /* The state surrounding mouse grabbing is potentially confusing. |
| 290 | * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated |
| 291 | * pointing device an absolute-position one?"], but is only updated on |
| 292 | * next refresh. |
| 293 | * isMouseGrabbed tracks whether GUI events are directed to the guest; |
| 294 | * it controls whether special keys like Cmd get sent to the guest, |
| 295 | * and whether we capture the mouse when in non-absolute mode. |
| 296 | * isMouseDeassociated tracks whether we've told MacOSX to disassociate |
| 297 | * the mouse and mouse cursor position by calling |
| 298 | * CGAssociateMouseAndMouseCursorPosition(FALSE) |
| 299 | * (which basically happens if we grab in non-absolute mode). |
| 300 | */ |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 301 | - (BOOL) isMouseGrabbed; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 302 | - (BOOL) isAbsoluteEnabled; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 303 | - (BOOL) isMouseDeassociated; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 304 | - (float) cdx; |
| 305 | - (float) cdy; |
| 306 | - (QEMUScreen) gscreen; |
| 307 | @end |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 308 | |
Andreas Färber | 7fee199 | 2011-06-09 20:53:32 +0200 | [diff] [blame] | 309 | QemuCocoaView *cocoaView; |
| 310 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 311 | @implementation QemuCocoaView |
| 312 | - (id)initWithFrame:(NSRect)frameRect |
| 313 | { |
| 314 | COCOA_DEBUG("QemuCocoaView: initWithFrame\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 315 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 316 | self = [super initWithFrame:frameRect]; |
| 317 | if (self) { |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 318 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 319 | screen.bitsPerComponent = 8; |
| 320 | screen.bitsPerPixel = 32; |
| 321 | screen.width = frameRect.size.width; |
| 322 | screen.height = frameRect.size.height; |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 323 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 324 | } |
| 325 | return self; |
| 326 | } |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 327 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 328 | - (void) dealloc |
| 329 | { |
| 330 | COCOA_DEBUG("QemuCocoaView: dealloc\n"); |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 331 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 332 | if (dataProviderRef) |
| 333 | CGDataProviderRelease(dataProviderRef); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 334 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 335 | [super dealloc]; |
| 336 | } |
bellard | 5cbfcd0 | 2006-06-14 15:53:24 +0000 | [diff] [blame] | 337 | |
Andreas Färber | d50f71d | 2009-12-13 02:03:33 +0100 | [diff] [blame] | 338 | - (BOOL) isOpaque |
| 339 | { |
| 340 | return YES; |
| 341 | } |
| 342 | |
Peter Maydell | 5dd45be | 2014-06-23 10:35:23 +0100 | [diff] [blame] | 343 | - (BOOL) screenContainsPoint:(NSPoint) p |
| 344 | { |
| 345 | return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height); |
| 346 | } |
| 347 | |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 348 | - (void) hideCursor |
| 349 | { |
| 350 | if (!cursor_hide) { |
| 351 | return; |
| 352 | } |
| 353 | [NSCursor hide]; |
| 354 | } |
| 355 | |
| 356 | - (void) unhideCursor |
| 357 | { |
| 358 | if (!cursor_hide) { |
| 359 | return; |
| 360 | } |
| 361 | [NSCursor unhide]; |
| 362 | } |
| 363 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 364 | - (void) drawRect:(NSRect) rect |
| 365 | { |
| 366 | COCOA_DEBUG("QemuCocoaView: drawRect\n"); |
bellard | 5cbfcd0 | 2006-06-14 15:53:24 +0000 | [diff] [blame] | 367 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 368 | // get CoreGraphic context |
| 369 | CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort]; |
| 370 | CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone); |
| 371 | CGContextSetShouldAntialias (viewContextRef, NO); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 372 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 373 | // draw screen bitmap directly to Core Graphics context |
Peter Maydell | 7d270b1 | 2013-12-24 02:51:47 +0000 | [diff] [blame] | 374 | if (!dataProviderRef) { |
| 375 | // Draw request before any guest device has set up a framebuffer: |
| 376 | // just draw an opaque black rectangle |
| 377 | CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0); |
| 378 | CGContextFillRect(viewContextRef, NSRectToCGRect(rect)); |
| 379 | } else { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 380 | CGImageRef imageRef = CGImageCreate( |
| 381 | screen.width, //width |
| 382 | screen.height, //height |
| 383 | screen.bitsPerComponent, //bitsPerComponent |
| 384 | screen.bitsPerPixel, //bitsPerPixel |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 385 | (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow |
Andreas Färber | 04afa4a | 2009-12-13 00:58:21 +0100 | [diff] [blame] | 386 | #ifdef __LITTLE_ENDIAN__ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 387 | CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4 |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 388 | kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 389 | #else |
| 390 | CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc) |
| 391 | kCGImageAlphaNoneSkipFirst, //bitmapInfo |
| 392 | #endif |
| 393 | dataProviderRef, //provider |
| 394 | NULL, //decode |
| 395 | 0, //interpolate |
| 396 | kCGRenderingIntentDefault //intent |
| 397 | ); |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 398 | // selective drawing code (draws only dirty rectangles) (OS X >= 10.4) |
| 399 | const NSRect *rectList; |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 400 | NSInteger rectCount; |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 401 | int i; |
| 402 | CGImageRef clipImageRef; |
| 403 | CGRect clipRect; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 404 | |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 405 | [self getRectsBeingDrawn:&rectList count:&rectCount]; |
| 406 | for (i = 0; i < rectCount; i++) { |
| 407 | clipRect.origin.x = rectList[i].origin.x / cdx; |
| 408 | clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy; |
| 409 | clipRect.size.width = rectList[i].size.width / cdx; |
| 410 | clipRect.size.height = rectList[i].size.height / cdy; |
| 411 | clipImageRef = CGImageCreateWithImageInRect( |
| 412 | imageRef, |
| 413 | clipRect |
| 414 | ); |
| 415 | CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef); |
| 416 | CGImageRelease (clipImageRef); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 417 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 418 | CGImageRelease (imageRef); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 422 | - (void) setContentDimensions |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 423 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 424 | COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 425 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 426 | if (isFullscreen) { |
| 427 | cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width; |
| 428 | cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 429 | |
| 430 | /* stretches video, but keeps same aspect ratio */ |
| 431 | if (stretch_video == true) { |
| 432 | /* use smallest stretch value - prevents clipping on sides */ |
| 433 | if (MIN(cdx, cdy) == cdx) { |
| 434 | cdy = cdx; |
| 435 | } else { |
| 436 | cdx = cdy; |
| 437 | } |
| 438 | } else { /* No stretching */ |
| 439 | cdx = cdy = 1; |
| 440 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 441 | cw = screen.width * cdx; |
| 442 | ch = screen.height * cdy; |
| 443 | cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; |
| 444 | cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; |
| 445 | } else { |
| 446 | cx = 0; |
| 447 | cy = 0; |
| 448 | cw = screen.width; |
| 449 | ch = screen.height; |
| 450 | cdx = 1.0; |
| 451 | cdy = 1.0; |
| 452 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 455 | - (void) switchSurface:(DisplaySurface *)surface |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 456 | { |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 457 | COCOA_DEBUG("QemuCocoaView: switchSurface\n"); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 458 | |
Peter Maydell | 8510d91 | 2013-03-18 20:28:21 +0000 | [diff] [blame] | 459 | int w = surface_width(surface); |
| 460 | int h = surface_height(surface); |
Peter Maydell | 381600d | 2014-06-23 10:35:22 +0100 | [diff] [blame] | 461 | /* cdx == 0 means this is our very first surface, in which case we need |
| 462 | * to recalculate the content dimensions even if it happens to be the size |
| 463 | * of the initial empty window. |
| 464 | */ |
| 465 | bool isResize = (w != screen.width || h != screen.height || cdx == 0.0); |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 466 | |
| 467 | int oldh = screen.height; |
| 468 | if (isResize) { |
| 469 | // Resize before we trigger the redraw, or we'll redraw at the wrong size |
| 470 | COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h); |
| 471 | screen.width = w; |
| 472 | screen.height = h; |
| 473 | [self setContentDimensions]; |
| 474 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; |
| 475 | } |
Peter Maydell | 8510d91 | 2013-03-18 20:28:21 +0000 | [diff] [blame] | 476 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 477 | // update screenBuffer |
| 478 | if (dataProviderRef) |
| 479 | CGDataProviderRelease(dataProviderRef); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 480 | |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 481 | //sync host window color space with guests |
Peter Maydell | 49060c2 | 2013-12-24 11:54:12 +0000 | [diff] [blame] | 482 | screen.bitsPerPixel = surface_bits_per_pixel(surface); |
| 483 | screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 484 | |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 485 | dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 486 | |
| 487 | // update windows |
| 488 | if (isFullscreen) { |
| 489 | [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]]; |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 490 | [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] | 491 | } else { |
| 492 | if (qemu_name) |
| 493 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 494 | [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] | 495 | } |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 496 | |
| 497 | if (isResize) { |
| 498 | [normalWindow center]; |
| 499 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | - (void) toggleFullScreen:(id)sender |
| 503 | { |
| 504 | COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); |
| 505 | |
| 506 | if (isFullscreen) { // switch from fullscreen to desktop |
| 507 | isFullscreen = FALSE; |
| 508 | [self ungrabMouse]; |
| 509 | [self setContentDimensions]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 510 | if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime |
| 511 | [self exitFullScreenModeWithOptions:nil]; |
| 512 | } else { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 513 | [fullScreenWindow close]; |
| 514 | [normalWindow setContentView: self]; |
| 515 | [normalWindow makeKeyAndOrderFront: self]; |
| 516 | [NSMenu setMenuBarVisible:YES]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 517 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 518 | } else { // switch from desktop to fullscreen |
| 519 | isFullscreen = TRUE; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 520 | [normalWindow orderOut: nil]; /* Hide the window */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 521 | [self grabMouse]; |
| 522 | [self setContentDimensions]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 523 | if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime |
| 524 | [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys: |
| 525 | [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens, |
| 526 | [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting, |
| 527 | nil]]; |
| 528 | } else { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 529 | [NSMenu setMenuBarVisible:NO]; |
| 530 | fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] |
| 531 | styleMask:NSBorderlessWindowMask |
| 532 | backing:NSBackingStoreBuffered |
| 533 | defer:NO]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 534 | [fullScreenWindow setAcceptsMouseMovedEvents: YES]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 535 | [fullScreenWindow setHasShadow:NO]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 536 | [fullScreenWindow setBackgroundColor: [NSColor blackColor]]; |
| 537 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; |
| 538 | [[fullScreenWindow contentView] addSubview: self]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 539 | [fullScreenWindow makeKeyAndOrderFront:self]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 540 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | - (void) handleEvent:(NSEvent *)event |
| 545 | { |
| 546 | COCOA_DEBUG("QemuCocoaView: handleEvent\n"); |
| 547 | |
| 548 | int buttons = 0; |
| 549 | int keycode; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 550 | bool mouse_event = false; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 551 | NSPoint p = [event locationInWindow]; |
| 552 | |
| 553 | switch ([event type]) { |
| 554 | case NSFlagsChanged: |
| 555 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 556 | |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 557 | if ((keycode == 219 || keycode == 220) && !isMouseGrabbed) { |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 558 | /* Don't pass command key changes to guest unless mouse is grabbed */ |
| 559 | keycode = 0; |
| 560 | } |
| 561 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 562 | if (keycode) { |
| 563 | if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 564 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
| 565 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 566 | } else if (qemu_console_is_graphic(NULL)) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 567 | if (modifiers_state[keycode] == 0) { // keydown |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 568 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 569 | modifiers_state[keycode] = 1; |
| 570 | } else { // keyup |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 571 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 572 | modifiers_state[keycode] = 0; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | // release Mouse grab when pressing ctrl+alt |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 578 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 579 | [self ungrabMouse]; |
| 580 | } |
| 581 | break; |
| 582 | case NSKeyDown: |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 583 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 584 | |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 585 | // 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] | 586 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 587 | [NSApp sendEvent:event]; |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | // default |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 592 | |
| 593 | // handle control + alt Key Combos (ctrl+alt is reserved for QEMU) |
| 594 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { |
| 595 | switch (keycode) { |
| 596 | |
| 597 | // enable graphic console |
| 598 | case 0x02 ... 0x0a: // '1' to '9' keys |
| 599 | console_select(keycode - 0x02); |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | // handle keys for graphic console |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 604 | } else if (qemu_console_is_graphic(NULL)) { |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 605 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 606 | |
| 607 | // handlekeys for Monitor |
| 608 | } else { |
| 609 | int keysym = 0; |
| 610 | switch([event keyCode]) { |
| 611 | case 115: |
| 612 | keysym = QEMU_KEY_HOME; |
| 613 | break; |
| 614 | case 117: |
| 615 | keysym = QEMU_KEY_DELETE; |
| 616 | break; |
| 617 | case 119: |
| 618 | keysym = QEMU_KEY_END; |
| 619 | break; |
| 620 | case 123: |
| 621 | keysym = QEMU_KEY_LEFT; |
| 622 | break; |
| 623 | case 124: |
| 624 | keysym = QEMU_KEY_RIGHT; |
| 625 | break; |
| 626 | case 125: |
| 627 | keysym = QEMU_KEY_DOWN; |
| 628 | break; |
| 629 | case 126: |
| 630 | keysym = QEMU_KEY_UP; |
| 631 | break; |
| 632 | default: |
| 633 | { |
| 634 | NSString *ks = [event characters]; |
| 635 | if ([ks length] > 0) |
| 636 | keysym = [ks characterAtIndex:0]; |
| 637 | } |
| 638 | } |
| 639 | if (keysym) |
| 640 | kbd_put_keysym(keysym); |
| 641 | } |
| 642 | break; |
| 643 | case NSKeyUp: |
| 644 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 645 | |
| 646 | // don't pass the guest a spurious key-up if we treated this |
| 647 | // command-key combo as a host UI action |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 648 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 649 | return; |
| 650 | } |
| 651 | |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 652 | if (qemu_console_is_graphic(NULL)) { |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 653 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 654 | } |
| 655 | break; |
| 656 | case NSMouseMoved: |
| 657 | if (isAbsoluteEnabled) { |
Peter Maydell | 5dd45be | 2014-06-23 10:35:23 +0100 | [diff] [blame] | 658 | if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) { |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 659 | if (isMouseGrabbed) { |
| 660 | [self ungrabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 661 | } |
| 662 | } else { |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 663 | if (!isMouseGrabbed) { |
| 664 | [self grabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | } |
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 NSLeftMouseDown: |
| 671 | if ([event modifierFlags] & NSCommandKeyMask) { |
| 672 | buttons |= MOUSE_EVENT_RBUTTON; |
| 673 | } else { |
| 674 | buttons |= MOUSE_EVENT_LBUTTON; |
| 675 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 676 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 677 | break; |
| 678 | case NSRightMouseDown: |
| 679 | buttons |= MOUSE_EVENT_RBUTTON; |
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 NSOtherMouseDown: |
| 683 | buttons |= MOUSE_EVENT_MBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 684 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 685 | break; |
| 686 | case NSLeftMouseDragged: |
| 687 | if ([event modifierFlags] & NSCommandKeyMask) { |
| 688 | buttons |= MOUSE_EVENT_RBUTTON; |
| 689 | } else { |
| 690 | buttons |= MOUSE_EVENT_LBUTTON; |
| 691 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 692 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 693 | break; |
| 694 | case NSRightMouseDragged: |
| 695 | buttons |= MOUSE_EVENT_RBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 696 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 697 | break; |
| 698 | case NSOtherMouseDragged: |
| 699 | buttons |= MOUSE_EVENT_MBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 700 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 701 | break; |
| 702 | case NSLeftMouseUp: |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 703 | mouse_event = true; |
| 704 | if (!isMouseGrabbed && [self screenContainsPoint:p]) { |
| 705 | [self grabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 706 | } |
| 707 | break; |
| 708 | case NSRightMouseUp: |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 709 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 710 | break; |
| 711 | case NSOtherMouseUp: |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 712 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 713 | break; |
| 714 | case NSScrollWheel: |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 715 | if (isMouseGrabbed) { |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 716 | buttons |= ([event deltaY] < 0) ? |
| 717 | MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 718 | } |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 719 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 720 | break; |
| 721 | default: |
| 722 | [NSApp sendEvent:event]; |
| 723 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 724 | |
| 725 | if (mouse_event) { |
| 726 | if (last_buttons != buttons) { |
| 727 | static uint32_t bmap[INPUT_BUTTON_MAX] = { |
| 728 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
| 729 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, |
| 730 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, |
| 731 | [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP, |
| 732 | [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN, |
| 733 | }; |
| 734 | qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons); |
| 735 | last_buttons = buttons; |
| 736 | } |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 737 | if (isMouseGrabbed) { |
| 738 | if (isAbsoluteEnabled) { |
| 739 | /* Note that the origin for Cocoa mouse coords is bottom left, not top left. |
| 740 | * The check on screenContainsPoint is to avoid sending out of range values for |
| 741 | * clicks in the titlebar. |
| 742 | */ |
| 743 | if ([self screenContainsPoint:p]) { |
| 744 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width); |
| 745 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height); |
| 746 | } |
| 747 | } else { |
| 748 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]); |
| 749 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]); |
| 750 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 751 | } else { |
| 752 | [NSApp sendEvent:event]; |
| 753 | } |
| 754 | qemu_input_event_sync(); |
| 755 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | - (void) grabMouse |
| 759 | { |
| 760 | COCOA_DEBUG("QemuCocoaView: grabMouse\n"); |
| 761 | |
| 762 | if (!isFullscreen) { |
| 763 | if (qemu_name) |
| 764 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]]; |
| 765 | else |
| 766 | [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"]; |
| 767 | } |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 768 | [self hideCursor]; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 769 | if (!isAbsoluteEnabled) { |
| 770 | isMouseDeassociated = TRUE; |
| 771 | CGAssociateMouseAndMouseCursorPosition(FALSE); |
| 772 | } |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 773 | isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | - (void) ungrabMouse |
| 777 | { |
| 778 | COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); |
| 779 | |
| 780 | if (!isFullscreen) { |
| 781 | if (qemu_name) |
| 782 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; |
| 783 | else |
| 784 | [normalWindow setTitle:@"QEMU"]; |
| 785 | } |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 786 | [self unhideCursor]; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 787 | if (isMouseDeassociated) { |
| 788 | CGAssociateMouseAndMouseCursorPosition(TRUE); |
| 789 | isMouseDeassociated = FALSE; |
| 790 | } |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 791 | isMouseGrabbed = FALSE; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;} |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 795 | - (BOOL) isMouseGrabbed {return isMouseGrabbed;} |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 796 | - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 797 | - (BOOL) isMouseDeassociated {return isMouseDeassociated;} |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 798 | - (float) cdx {return cdx;} |
| 799 | - (float) cdy {return cdy;} |
| 800 | - (QEMUScreen) gscreen {return screen;} |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 801 | @end |
| 802 | |
| 803 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 804 | |
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 | QemuCocoaAppController |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 808 | ------------------------------------------------------ |
| 809 | */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 810 | @interface QemuCocoaAppController : NSObject |
Peter Maydell | 2a4c8c5 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 811 | #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^] | 812 | <NSWindowDelegate, NSApplicationDelegate> |
Peter Maydell | 2a4c8c5 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 813 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 814 | { |
| 815 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 816 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv; |
Peter Maydell | de1aade | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 817 | - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 818 | - (void)doToggleFullScreen:(id)sender; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 819 | - (void)toggleFullScreen:(id)sender; |
| 820 | - (void)showQEMUDoc:(id)sender; |
| 821 | - (void)showQEMUTec:(id)sender; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 822 | - (void)zoomToFit:(id) sender; |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 823 | - (void)displayConsole:(id)sender; |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 824 | - (void)pauseQEMU:(id)sender; |
| 825 | - (void)resumeQEMU:(id)sender; |
| 826 | - (void)displayPause; |
| 827 | - (void)removePause; |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 828 | - (void)restartQEMU:(id)sender; |
| 829 | - (void)powerDownQEMU:(id)sender; |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 830 | - (void)ejectDeviceMedia:(id)sender; |
| 831 | - (void)changeDeviceMedia:(id)sender; |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame^] | 832 | - (BOOL)verifyQuit; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 833 | @end |
| 834 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 835 | @implementation QemuCocoaAppController |
| 836 | - (id) init |
| 837 | { |
| 838 | COCOA_DEBUG("QemuCocoaAppController: init\n"); |
| 839 | |
| 840 | self = [super init]; |
| 841 | if (self) { |
| 842 | |
| 843 | // create a view and add it to the window |
| 844 | cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)]; |
| 845 | if(!cocoaView) { |
| 846 | fprintf(stderr, "(cocoa) can't create a view\n"); |
| 847 | exit(1); |
| 848 | } |
| 849 | |
| 850 | // create a window |
| 851 | normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame] |
| 852 | styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask |
| 853 | backing:NSBackingStoreBuffered defer:NO]; |
| 854 | if(!normalWindow) { |
| 855 | fprintf(stderr, "(cocoa) can't create window\n"); |
| 856 | exit(1); |
| 857 | } |
| 858 | [normalWindow setAcceptsMouseMovedEvents:YES]; |
| 859 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]]; |
| 860 | [normalWindow setContentView:cocoaView]; |
Peter Maydell | 81801ae | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 861 | #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] | 862 | [normalWindow useOptimizedDrawing:YES]; |
Peter Maydell | 81801ae | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 863 | #endif |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 864 | [normalWindow makeKeyAndOrderFront:self]; |
Peter Maydell | 49060c2 | 2013-12-24 11:54:12 +0000 | [diff] [blame] | 865 | [normalWindow center]; |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame^] | 866 | [normalWindow setDelegate: self]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 867 | stretch_video = false; |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 868 | |
| 869 | /* Used for displaying pause on the screen */ |
| 870 | pauseLabel = [NSTextField new]; |
| 871 | [pauseLabel setBezeled:YES]; |
| 872 | [pauseLabel setDrawsBackground:YES]; |
| 873 | [pauseLabel setBackgroundColor: [NSColor whiteColor]]; |
| 874 | [pauseLabel setEditable:NO]; |
| 875 | [pauseLabel setSelectable:NO]; |
| 876 | [pauseLabel setStringValue: @"Paused"]; |
| 877 | [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]]; |
| 878 | [pauseLabel setTextColor: [NSColor blackColor]]; |
| 879 | [pauseLabel sizeToFit]; |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 880 | |
| 881 | // set the supported image file types that can be opened |
| 882 | supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg", |
| 883 | @"qcow", @"qcow2", @"cloop", @"vmdk", nil]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 884 | } |
| 885 | return self; |
| 886 | } |
| 887 | |
| 888 | - (void) dealloc |
| 889 | { |
| 890 | COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); |
| 891 | |
| 892 | if (cocoaView) |
| 893 | [cocoaView release]; |
| 894 | [super dealloc]; |
| 895 | } |
| 896 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 897 | - (void)applicationDidFinishLaunching: (NSNotification *) note |
| 898 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 899 | COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); |
bellard | 5a24693 | 2005-04-07 20:35:06 +0000 | [diff] [blame] | 900 | |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 901 | // Display an open dialog box if no arguments were passed or |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 902 | // if qemu was launched from the finder ( the Finder passes "-psn" ) |
| 903 | if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 904 | NSOpenPanel *op = [[NSOpenPanel alloc] init]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 905 | [op setPrompt:@"Boot image"]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 906 | [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"]; |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 907 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 908 | [op setAllowedFileTypes:supportedImageFileTypes]; |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 909 | [op beginSheetModalForWindow:normalWindow |
| 910 | completionHandler:^(NSInteger returnCode) |
| 911 | { [self openPanelDidEnd:op |
| 912 | returnCode:returnCode contextInfo:NULL ]; } ]; |
| 913 | #else |
| 914 | // Compatibility code for pre-10.6, using deprecated method |
| 915 | [op beginSheetForDirectory:nil file:nil types:filetypes |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 916 | modalForWindow:normalWindow modalDelegate:self |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 917 | didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL]; |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 918 | #endif |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 919 | } else { |
Stefan Weil | 5cbdb3a | 2012-04-07 09:23:39 +0200 | [diff] [blame] | 920 | // or launch QEMU, with the global args |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 921 | [self startEmulationWithArgc:gArgc argv:(char **)gArgv]; |
bellard | 5a24693 | 2005-04-07 20:35:06 +0000 | [diff] [blame] | 922 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | - (void)applicationWillTerminate:(NSNotification *)aNotification |
| 926 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 927 | COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n"); |
| 928 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 929 | qemu_system_shutdown_request(); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 930 | exit(0); |
| 931 | } |
| 932 | |
Andreas Färber | 41ea49b | 2009-12-14 22:13:27 +0100 | [diff] [blame] | 933 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication |
| 934 | { |
| 935 | return YES; |
| 936 | } |
| 937 | |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame^] | 938 | - (NSApplicationTerminateReply)applicationShouldTerminate: |
| 939 | (NSApplication *)sender |
| 940 | { |
| 941 | COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n"); |
| 942 | return [self verifyQuit]; |
| 943 | } |
| 944 | |
| 945 | /* Called when the user clicks on a window's close button */ |
| 946 | - (BOOL)windowShouldClose:(id)sender |
| 947 | { |
| 948 | COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n"); |
| 949 | [NSApp terminate: sender]; |
| 950 | /* If the user allows the application to quit then the call to |
| 951 | * NSApp terminate will never return. If we get here then the user |
| 952 | * cancelled the quit, so we should return NO to not permit the |
| 953 | * closing of this window. |
| 954 | */ |
| 955 | return NO; |
| 956 | } |
| 957 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 958 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv |
| 959 | { |
| 960 | COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n"); |
| 961 | |
| 962 | int status; |
Andreas Färber | 3bbbee1 | 2011-05-29 19:42:51 +0200 | [diff] [blame] | 963 | status = qemu_main(argc, argv, *_NSGetEnviron()); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 964 | exit(status); |
| 965 | } |
| 966 | |
Peter Maydell | de1aade | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 967 | - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 968 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 969 | COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 970 | |
Peter Maydell | 8617989 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 971 | /* The NSFileHandlingPanelOKButton/NSFileHandlingPanelCancelButton values for |
| 972 | * returnCode strictly only apply for the 10.6-and-up beginSheetModalForWindow |
| 973 | * API. For the legacy pre-10.6 beginSheetForDirectory API they are NSOKButton |
| 974 | * and NSCancelButton. However conveniently the values are the same. |
| 975 | * We use the non-legacy names because the others are deprecated in OSX 10.10. |
| 976 | */ |
| 977 | if (returnCode == NSFileHandlingPanelCancelButton) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 978 | exit(0); |
Peter Maydell | 8617989 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 979 | } else if (returnCode == NSFileHandlingPanelOKButton) { |
Peter Maydell | 8bb3f1e | 2013-04-22 10:29:48 +0000 | [diff] [blame] | 980 | char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 981 | |
Peter Maydell | 98db429 | 2013-12-08 22:59:06 +0000 | [diff] [blame] | 982 | char **argv = g_new(char *, 4); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 983 | |
Andreas Färber | 13766eb | 2011-10-30 23:19:54 +0100 | [diff] [blame] | 984 | [sheet close]; |
| 985 | |
Peter Maydell | 98db429 | 2013-12-08 22:59:06 +0000 | [diff] [blame] | 986 | argv[0] = g_strdup(gArgv[0]); |
| 987 | argv[1] = g_strdup("-hda"); |
| 988 | argv[2] = g_strdup(img); |
| 989 | argv[3] = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 990 | |
Peter Maydell | 98db429 | 2013-12-08 22:59:06 +0000 | [diff] [blame] | 991 | // printf("Using argc %d argv %s -hda %s\n", 3, gArgv[0], img); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 992 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 993 | [self startEmulationWithArgc:3 argv:(char**)argv]; |
| 994 | } |
| 995 | } |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 996 | |
| 997 | /* We abstract the method called by the Enter Fullscreen menu item |
| 998 | * because Mac OS 10.7 and higher disables it. This is because of the |
| 999 | * menu item's old selector's name toggleFullScreen: |
| 1000 | */ |
| 1001 | - (void) doToggleFullScreen:(id)sender |
| 1002 | { |
| 1003 | [self toggleFullScreen:(id)sender]; |
| 1004 | } |
| 1005 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1006 | - (void)toggleFullScreen:(id)sender |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1007 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1008 | COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); |
| 1009 | |
| 1010 | [cocoaView toggleFullScreen:sender]; |
| 1011 | } |
| 1012 | |
| 1013 | - (void)showQEMUDoc:(id)sender |
| 1014 | { |
| 1015 | COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n"); |
| 1016 | |
| 1017 | [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html", |
| 1018 | [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; |
| 1019 | } |
| 1020 | |
| 1021 | - (void)showQEMUTec:(id)sender |
| 1022 | { |
| 1023 | COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n"); |
| 1024 | |
| 1025 | [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html", |
| 1026 | [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1027 | } |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1028 | |
| 1029 | /* Stretches video to fit host monitor size */ |
| 1030 | - (void)zoomToFit:(id) sender |
| 1031 | { |
| 1032 | stretch_video = !stretch_video; |
| 1033 | if (stretch_video == true) { |
| 1034 | [sender setState: NSOnState]; |
| 1035 | } else { |
| 1036 | [sender setState: NSOffState]; |
| 1037 | } |
| 1038 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1039 | |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 1040 | /* Displays the console on the screen */ |
| 1041 | - (void)displayConsole:(id)sender |
| 1042 | { |
| 1043 | console_select([sender tag]); |
| 1044 | } |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1045 | |
| 1046 | /* Pause the guest */ |
| 1047 | - (void)pauseQEMU:(id)sender |
| 1048 | { |
| 1049 | qmp_stop(NULL); |
| 1050 | [sender setEnabled: NO]; |
| 1051 | [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES]; |
| 1052 | [self displayPause]; |
| 1053 | } |
| 1054 | |
| 1055 | /* Resume running the guest operating system */ |
| 1056 | - (void)resumeQEMU:(id) sender |
| 1057 | { |
| 1058 | qmp_cont(NULL); |
| 1059 | [sender setEnabled: NO]; |
| 1060 | [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES]; |
| 1061 | [self removePause]; |
| 1062 | } |
| 1063 | |
| 1064 | /* Displays the word pause on the screen */ |
| 1065 | - (void)displayPause |
| 1066 | { |
| 1067 | /* Coordinates have to be calculated each time because the window can change its size */ |
| 1068 | int xCoord, yCoord, width, height; |
| 1069 | xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2; |
| 1070 | yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5); |
| 1071 | width = [pauseLabel frame].size.width; |
| 1072 | height = [pauseLabel frame].size.height; |
| 1073 | [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)]; |
| 1074 | [cocoaView addSubview: pauseLabel]; |
| 1075 | } |
| 1076 | |
| 1077 | /* Removes the word pause from the screen */ |
| 1078 | - (void)removePause |
| 1079 | { |
| 1080 | [pauseLabel removeFromSuperview]; |
| 1081 | } |
| 1082 | |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1083 | /* Restarts QEMU */ |
| 1084 | - (void)restartQEMU:(id)sender |
| 1085 | { |
| 1086 | qmp_system_reset(NULL); |
| 1087 | } |
| 1088 | |
| 1089 | /* Powers down QEMU */ |
| 1090 | - (void)powerDownQEMU:(id)sender |
| 1091 | { |
| 1092 | qmp_system_powerdown(NULL); |
| 1093 | } |
| 1094 | |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1095 | /* Ejects the media. |
| 1096 | * Uses sender's tag to figure out the device to eject. |
| 1097 | */ |
| 1098 | - (void)ejectDeviceMedia:(id)sender |
| 1099 | { |
| 1100 | NSString * drive; |
| 1101 | drive = [sender representedObject]; |
| 1102 | if(drive == nil) { |
| 1103 | NSBeep(); |
| 1104 | QEMU_Alert(@"Failed to find drive to eject!"); |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | Error *err = NULL; |
| 1109 | qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding], false, false, &err); |
| 1110 | handleAnyDeviceErrors(err); |
| 1111 | } |
| 1112 | |
| 1113 | /* Displays a dialog box asking the user to select an image file to load. |
| 1114 | * Uses sender's represented object value to figure out which drive to use. |
| 1115 | */ |
| 1116 | - (void)changeDeviceMedia:(id)sender |
| 1117 | { |
| 1118 | /* Find the drive name */ |
| 1119 | NSString * drive; |
| 1120 | drive = [sender representedObject]; |
| 1121 | if(drive == nil) { |
| 1122 | NSBeep(); |
| 1123 | QEMU_Alert(@"Could not find drive!"); |
| 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | /* Display the file open dialog */ |
| 1128 | NSOpenPanel * openPanel; |
| 1129 | openPanel = [NSOpenPanel openPanel]; |
| 1130 | [openPanel setCanChooseFiles: YES]; |
| 1131 | [openPanel setAllowsMultipleSelection: NO]; |
| 1132 | [openPanel setAllowedFileTypes: supportedImageFileTypes]; |
| 1133 | if([openPanel runModal] == NSFileHandlingPanelOKButton) { |
| 1134 | NSString * file = [[[openPanel URLs] objectAtIndex: 0] path]; |
| 1135 | if(file == nil) { |
| 1136 | NSBeep(); |
| 1137 | QEMU_Alert(@"Failed to convert URL to file path!"); |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | Error *err = NULL; |
| 1142 | qmp_change_blockdev([drive cStringUsingEncoding: NSASCIIStringEncoding], |
| 1143 | [file cStringUsingEncoding: NSASCIIStringEncoding], |
| 1144 | "raw", |
| 1145 | &err); |
| 1146 | handleAnyDeviceErrors(err); |
| 1147 | } |
| 1148 | } |
| 1149 | |
John Arbuckle | d9bc14f | 2015-09-25 23:13:59 +0100 | [diff] [blame^] | 1150 | /* Verifies if the user really wants to quit */ |
| 1151 | - (BOOL)verifyQuit |
| 1152 | { |
| 1153 | NSAlert *alert = [NSAlert new]; |
| 1154 | [alert autorelease]; |
| 1155 | [alert setMessageText: @"Are you sure you want to quit QEMU?"]; |
| 1156 | [alert addButtonWithTitle: @"Cancel"]; |
| 1157 | [alert addButtonWithTitle: @"Quit"]; |
| 1158 | if([alert runModal] == NSAlertSecondButtonReturn) { |
| 1159 | return YES; |
| 1160 | } else { |
| 1161 | return NO; |
| 1162 | } |
| 1163 | } |
| 1164 | |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 1165 | @end |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1166 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1167 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1168 | int main (int argc, const char * argv[]) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1169 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1170 | gArgc = argc; |
| 1171 | gArgv = (char **)argv; |
Andreas Färber | f491880 | 2009-12-13 02:11:44 +0100 | [diff] [blame] | 1172 | int i; |
| 1173 | |
| 1174 | /* In case we don't need to display a window, let's not do that */ |
| 1175 | for (i = 1; i < argc; i++) { |
Tristan Gingold | e4ebcc1 | 2011-03-15 14:18:22 +0100 | [diff] [blame] | 1176 | const char *opt = argv[i]; |
| 1177 | |
| 1178 | if (opt[0] == '-') { |
| 1179 | /* Treat --foo the same as -foo. */ |
| 1180 | if (opt[1] == '-') { |
| 1181 | opt++; |
| 1182 | } |
Alexandre Raymond | 9851484 | 2011-05-29 18:22:49 -0400 | [diff] [blame] | 1183 | if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || |
| 1184 | !strcmp(opt, "-vnc") || |
Tristan Gingold | e4ebcc1 | 2011-03-15 14:18:22 +0100 | [diff] [blame] | 1185 | !strcmp(opt, "-nographic") || |
| 1186 | !strcmp(opt, "-version") || |
Andreas Färber | 60b46aa | 2012-05-28 03:18:31 +0200 | [diff] [blame] | 1187 | !strcmp(opt, "-curses") || |
Rainer Müller | b12a84c | 2015-09-09 16:08:30 +0200 | [diff] [blame] | 1188 | !strcmp(opt, "-display") || |
Andreas Färber | 60b46aa | 2012-05-28 03:18:31 +0200 | [diff] [blame] | 1189 | !strcmp(opt, "-qtest")) { |
Andreas Färber | 3bbbee1 | 2011-05-29 19:42:51 +0200 | [diff] [blame] | 1190 | return qemu_main(gArgc, gArgv, *_NSGetEnviron()); |
Tristan Gingold | e4ebcc1 | 2011-03-15 14:18:22 +0100 | [diff] [blame] | 1191 | } |
Andreas Färber | f491880 | 2009-12-13 02:11:44 +0100 | [diff] [blame] | 1192 | } |
| 1193 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1194 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1195 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1196 | |
Peter Maydell | 42a5dfe | 2013-04-22 10:29:47 +0000 | [diff] [blame] | 1197 | // Pull this console process up to being a fully-fledged graphical |
| 1198 | // app with a menubar and Dock icon |
| 1199 | ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 1200 | TransformProcessType(&psn, kProcessTransformToForegroundApplication); |
| 1201 | |
| 1202 | [NSApplication sharedApplication]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1203 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1204 | // Add menus |
| 1205 | NSMenu *menu; |
| 1206 | NSMenuItem *menuItem; |
| 1207 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1208 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1209 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1210 | // Application menu |
| 1211 | menu = [[NSMenu alloc] initWithTitle:@""]; |
| 1212 | [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU |
| 1213 | [menu addItem:[NSMenuItem separatorItem]]; //Separator |
| 1214 | [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU |
| 1215 | menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others |
| 1216 | [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; |
| 1217 | [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All |
| 1218 | [menu addItem:[NSMenuItem separatorItem]]; //Separator |
| 1219 | [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"]; |
| 1220 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; |
| 1221 | [menuItem setSubmenu:menu]; |
| 1222 | [[NSApp mainMenu] addItem:menuItem]; |
| 1223 | [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] | 1224 | |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1225 | // Machine menu |
| 1226 | menu = [[NSMenu alloc] initWithTitle: @"Machine"]; |
| 1227 | [menu setAutoenablesItems: NO]; |
| 1228 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]]; |
| 1229 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease]; |
| 1230 | [menu addItem: menuItem]; |
| 1231 | [menuItem setEnabled: NO]; |
John Arbuckle | 2707461 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1232 | [menu addItem: [NSMenuItem separatorItem]]; |
| 1233 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]]; |
| 1234 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]]; |
John Arbuckle | 8524f1c | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1235 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease]; |
| 1236 | [menuItem setSubmenu:menu]; |
| 1237 | [[NSApp mainMenu] addItem:menuItem]; |
| 1238 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1239 | // View menu |
| 1240 | menu = [[NSMenu alloc] initWithTitle:@"View"]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1241 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen |
| 1242 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1243 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease]; |
| 1244 | [menuItem setSubmenu:menu]; |
| 1245 | [[NSApp mainMenu] addItem:menuItem]; |
| 1246 | |
| 1247 | // Window menu |
| 1248 | menu = [[NSMenu alloc] initWithTitle:@"Window"]; |
| 1249 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize |
| 1250 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; |
| 1251 | [menuItem setSubmenu:menu]; |
| 1252 | [[NSApp mainMenu] addItem:menuItem]; |
| 1253 | [NSApp setWindowsMenu:menu]; |
| 1254 | |
| 1255 | // Help menu |
| 1256 | menu = [[NSMenu alloc] initWithTitle:@"Help"]; |
| 1257 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help |
| 1258 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help |
| 1259 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; |
| 1260 | [menuItem setSubmenu:menu]; |
| 1261 | [[NSApp mainMenu] addItem:menuItem]; |
| 1262 | |
| 1263 | // Create an Application controller |
| 1264 | QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init]; |
| 1265 | [NSApp setDelegate:appController]; |
| 1266 | |
| 1267 | // Start the main event loop |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1268 | [NSApp run]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1269 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1270 | [appController release]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1271 | [pool release]; |
bellard | cae41b1 | 2006-05-22 21:25:04 +0000 | [diff] [blame] | 1272 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1273 | return 0; |
| 1274 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1275 | |
| 1276 | |
| 1277 | |
| 1278 | #pragma mark qemu |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1279 | static void cocoa_update(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1280 | int x, int y, int w, int h) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1281 | { |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1282 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 1283 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1284 | COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); |
| 1285 | |
| 1286 | NSRect rect; |
| 1287 | if ([cocoaView cdx] == 1.0) { |
| 1288 | rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); |
| 1289 | } else { |
| 1290 | rect = NSMakeRect( |
| 1291 | x * [cocoaView cdx], |
| 1292 | ([cocoaView gscreen].height - y - h) * [cocoaView cdy], |
| 1293 | w * [cocoaView cdx], |
| 1294 | h * [cocoaView cdy]); |
| 1295 | } |
Andreas Färber | 17ccbc2 | 2009-12-13 02:08:58 +0100 | [diff] [blame] | 1296 | [cocoaView setNeedsDisplayInRect:rect]; |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1297 | |
| 1298 | [pool release]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Gerd Hoffmann | c12aeb8 | 2013-02-28 15:03:04 +0100 | [diff] [blame] | 1301 | static void cocoa_switch(DisplayChangeListener *dcl, |
Gerd Hoffmann | c12aeb8 | 2013-02-28 15:03:04 +0100 | [diff] [blame] | 1302 | DisplaySurface *surface) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1303 | { |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1304 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1305 | |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1306 | COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 1307 | [cocoaView switchSurface:surface]; |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1308 | [pool release]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1311 | static void cocoa_refresh(DisplayChangeListener *dcl) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1312 | { |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1313 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 1314 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1315 | COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n"); |
| 1316 | |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1317 | if (qemu_input_is_absolute()) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1318 | if (![cocoaView isAbsoluteEnabled]) { |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 1319 | if ([cocoaView isMouseGrabbed]) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1320 | [cocoaView ungrabMouse]; |
| 1321 | } |
| 1322 | } |
| 1323 | [cocoaView setAbsoluteEnabled:YES]; |
| 1324 | } |
| 1325 | |
| 1326 | NSDate *distantPast; |
| 1327 | NSEvent *event; |
| 1328 | distantPast = [NSDate distantPast]; |
| 1329 | do { |
| 1330 | event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast |
| 1331 | inMode: NSDefaultRunLoopMode dequeue:YES]; |
| 1332 | if (event != nil) { |
| 1333 | [cocoaView handleEvent:event]; |
| 1334 | } |
| 1335 | } while(event != nil); |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 1336 | graphic_hw_update(NULL); |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1337 | [pool release]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static void cocoa_cleanup(void) |
| 1341 | { |
| 1342 | COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n"); |
Blue Swirl | 58a0667 | 2011-08-21 18:42:08 +0000 | [diff] [blame] | 1343 | g_free(dcl); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1346 | static const DisplayChangeListenerOps dcl_ops = { |
| 1347 | .dpy_name = "cocoa", |
Peter Maydell | 8510d91 | 2013-03-18 20:28:21 +0000 | [diff] [blame] | 1348 | .dpy_gfx_update = cocoa_update, |
| 1349 | .dpy_gfx_switch = cocoa_switch, |
| 1350 | .dpy_refresh = cocoa_refresh, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1351 | }; |
| 1352 | |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 1353 | /* Returns a name for a given console */ |
| 1354 | static NSString * getConsoleName(QemuConsole * console) |
| 1355 | { |
| 1356 | return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)]; |
| 1357 | } |
| 1358 | |
| 1359 | /* Add an entry to the View menu for each console */ |
| 1360 | static void add_console_menu_entries(void) |
| 1361 | { |
| 1362 | NSMenu *menu; |
| 1363 | NSMenuItem *menuItem; |
| 1364 | int index = 0; |
| 1365 | |
| 1366 | menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu]; |
| 1367 | |
| 1368 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1369 | |
| 1370 | while (qemu_console_lookup_by_index(index) != NULL) { |
| 1371 | menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index)) |
| 1372 | action: @selector(displayConsole:) keyEquivalent: @""] autorelease]; |
| 1373 | [menuItem setTag: index]; |
| 1374 | [menu addItem: menuItem]; |
| 1375 | index++; |
| 1376 | } |
| 1377 | } |
| 1378 | |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1379 | /* Make menu items for all removable devices. |
| 1380 | * Each device is given an 'Eject' and 'Change' menu item. |
| 1381 | */ |
| 1382 | static void addRemovableDevicesMenuItems() |
| 1383 | { |
| 1384 | NSMenu *menu; |
| 1385 | NSMenuItem *menuItem; |
| 1386 | BlockInfoList *currentDevice, *pointerToFree; |
| 1387 | NSString *deviceName; |
| 1388 | |
| 1389 | currentDevice = qmp_query_block(NULL); |
| 1390 | pointerToFree = currentDevice; |
| 1391 | if(currentDevice == NULL) { |
| 1392 | NSBeep(); |
| 1393 | QEMU_Alert(@"Failed to query for block devices!"); |
| 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; |
| 1398 | |
| 1399 | // Add a separator between related groups of menu items |
| 1400 | [menu addItem:[NSMenuItem separatorItem]]; |
| 1401 | |
| 1402 | // Set the attributes to the "Removable Media" menu item |
| 1403 | NSString *titleString = @"Removable Media"; |
| 1404 | NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString]; |
| 1405 | NSColor *newColor = [NSColor blackColor]; |
| 1406 | NSFontManager *fontManager = [NSFontManager sharedFontManager]; |
| 1407 | NSFont *font = [fontManager fontWithFamily:@"Helvetica" |
| 1408 | traits:NSBoldFontMask|NSItalicFontMask |
| 1409 | weight:0 |
| 1410 | size:14]; |
| 1411 | [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])]; |
| 1412 | [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])]; |
| 1413 | [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])]; |
| 1414 | |
| 1415 | // Add the "Removable Media" menu item |
| 1416 | menuItem = [NSMenuItem new]; |
| 1417 | [menuItem setAttributedTitle: attString]; |
| 1418 | [menuItem setEnabled: NO]; |
| 1419 | [menu addItem: menuItem]; |
| 1420 | |
| 1421 | /* Loop thru all the block devices in the emulator */ |
| 1422 | while (currentDevice) { |
| 1423 | deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain]; |
| 1424 | |
| 1425 | if(currentDevice->value->removable) { |
| 1426 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device] |
| 1427 | action: @selector(changeDeviceMedia:) |
| 1428 | keyEquivalent: @""]; |
| 1429 | [menu addItem: menuItem]; |
| 1430 | [menuItem setRepresentedObject: deviceName]; |
| 1431 | [menuItem autorelease]; |
| 1432 | |
| 1433 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device] |
| 1434 | action: @selector(ejectDeviceMedia:) |
| 1435 | keyEquivalent: @""]; |
| 1436 | [menu addItem: menuItem]; |
| 1437 | [menuItem setRepresentedObject: deviceName]; |
| 1438 | [menuItem autorelease]; |
| 1439 | } |
| 1440 | currentDevice = currentDevice->next; |
| 1441 | } |
| 1442 | qapi_free_BlockInfoList(pointerToFree); |
| 1443 | } |
| 1444 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1445 | void cocoa_display_init(DisplayState *ds, int full_screen) |
| 1446 | { |
| 1447 | COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); |
| 1448 | |
Programmingkid | 43227af | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1449 | /* if fullscreen mode is to be used */ |
| 1450 | if (full_screen == true) { |
| 1451 | [NSApp activateIgnoringOtherApps: YES]; |
| 1452 | [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil]; |
| 1453 | } |
| 1454 | |
Blue Swirl | 58a0667 | 2011-08-21 18:42:08 +0000 | [diff] [blame] | 1455 | dcl = g_malloc0(sizeof(DisplayChangeListener)); |
| 1456 | |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 1457 | // register vga output callbacks |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1458 | dcl->ops = &dcl_ops; |
Gerd Hoffmann | 5209089 | 2013-04-23 15:44:31 +0200 | [diff] [blame] | 1459 | register_displaychangelistener(dcl); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1460 | |
| 1461 | // register cleanup function |
| 1462 | atexit(cocoa_cleanup); |
Programmingkid | b4c6a11 | 2015-05-19 09:11:18 +0100 | [diff] [blame] | 1463 | |
| 1464 | /* At this point QEMU has created all the consoles, so we can add View |
| 1465 | * menu entries for them. |
| 1466 | */ |
| 1467 | add_console_menu_entries(); |
John Arbuckle | 693a3e0 | 2015-06-19 10:53:27 +0100 | [diff] [blame] | 1468 | |
| 1469 | /* Give all removable devices a menu item. |
| 1470 | * Has to be called after QEMU has started to |
| 1471 | * find out what removable devices it has. |
| 1472 | */ |
| 1473 | addRemovableDevicesMenuItems(); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1474 | } |