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