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