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" |
bellard | da4dbf7 | 2005-03-02 22:22:43 +0000 | [diff] [blame] | 32 | |
Andreas Färber | 44e4c0b | 2009-12-13 00:45:40 +0100 | [diff] [blame] | 33 | #ifndef MAC_OS_X_VERSION_10_5 |
| 34 | #define MAC_OS_X_VERSION_10_5 1050 |
| 35 | #endif |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 36 | #ifndef MAC_OS_X_VERSION_10_6 |
| 37 | #define MAC_OS_X_VERSION_10_6 1060 |
| 38 | #endif |
Andreas Färber | 44e4c0b | 2009-12-13 00:45:40 +0100 | [diff] [blame] | 39 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 40 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 41 | //#define DEBUG |
| 42 | |
| 43 | #ifdef DEBUG |
| 44 | #define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); } |
| 45 | #else |
| 46 | #define COCOA_DEBUG(...) ((void) 0) |
| 47 | #endif |
| 48 | |
| 49 | #define cgrect(nsrect) (*(CGRect *)&(nsrect)) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 50 | |
| 51 | typedef struct { |
| 52 | int width; |
| 53 | int height; |
| 54 | int bitsPerComponent; |
| 55 | int bitsPerPixel; |
| 56 | } QEMUScreen; |
| 57 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 58 | NSWindow *normalWindow; |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 59 | static DisplayChangeListener *dcl; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 60 | static int last_buttons; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 61 | |
| 62 | int gArgc; |
| 63 | char **gArgv; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 64 | bool stretch_video; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 65 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 66 | // keymap conversion |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 67 | int keymap[] = |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 68 | { |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 69 | // SdlI macI macH SdlH 104xtH 104xtC sdl |
| 70 | 30, // 0 0x00 0x1e A QZ_a |
| 71 | 31, // 1 0x01 0x1f S QZ_s |
| 72 | 32, // 2 0x02 0x20 D QZ_d |
| 73 | 33, // 3 0x03 0x21 F QZ_f |
| 74 | 35, // 4 0x04 0x23 H QZ_h |
| 75 | 34, // 5 0x05 0x22 G QZ_g |
| 76 | 44, // 6 0x06 0x2c Z QZ_z |
| 77 | 45, // 7 0x07 0x2d X QZ_x |
| 78 | 46, // 8 0x08 0x2e C QZ_c |
| 79 | 47, // 9 0x09 0x2f V QZ_v |
| 80 | 0, // 10 0x0A Undefined |
| 81 | 48, // 11 0x0B 0x30 B QZ_b |
| 82 | 16, // 12 0x0C 0x10 Q QZ_q |
| 83 | 17, // 13 0x0D 0x11 W QZ_w |
| 84 | 18, // 14 0x0E 0x12 E QZ_e |
| 85 | 19, // 15 0x0F 0x13 R QZ_r |
| 86 | 21, // 16 0x10 0x15 Y QZ_y |
| 87 | 20, // 17 0x11 0x14 T QZ_t |
| 88 | 2, // 18 0x12 0x02 1 QZ_1 |
| 89 | 3, // 19 0x13 0x03 2 QZ_2 |
| 90 | 4, // 20 0x14 0x04 3 QZ_3 |
| 91 | 5, // 21 0x15 0x05 4 QZ_4 |
| 92 | 7, // 22 0x16 0x07 6 QZ_6 |
| 93 | 6, // 23 0x17 0x06 5 QZ_5 |
| 94 | 13, // 24 0x18 0x0d = QZ_EQUALS |
| 95 | 10, // 25 0x19 0x0a 9 QZ_9 |
| 96 | 8, // 26 0x1A 0x08 7 QZ_7 |
| 97 | 12, // 27 0x1B 0x0c - QZ_MINUS |
| 98 | 9, // 28 0x1C 0x09 8 QZ_8 |
| 99 | 11, // 29 0x1D 0x0b 0 QZ_0 |
| 100 | 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET |
| 101 | 24, // 31 0x1F 0x18 O QZ_o |
| 102 | 22, // 32 0x20 0x16 U QZ_u |
| 103 | 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET |
| 104 | 23, // 34 0x22 0x17 I QZ_i |
| 105 | 25, // 35 0x23 0x19 P QZ_p |
| 106 | 28, // 36 0x24 0x1c ENTER QZ_RETURN |
| 107 | 38, // 37 0x25 0x26 L QZ_l |
| 108 | 36, // 38 0x26 0x24 J QZ_j |
| 109 | 40, // 39 0x27 0x28 ' QZ_QUOTE |
| 110 | 37, // 40 0x28 0x25 K QZ_k |
| 111 | 39, // 41 0x29 0x27 ; QZ_SEMICOLON |
| 112 | 43, // 42 0x2A 0x2b \ QZ_BACKSLASH |
| 113 | 51, // 43 0x2B 0x33 , QZ_COMMA |
| 114 | 53, // 44 0x2C 0x35 / QZ_SLASH |
| 115 | 49, // 45 0x2D 0x31 N QZ_n |
| 116 | 50, // 46 0x2E 0x32 M QZ_m |
| 117 | 52, // 47 0x2F 0x34 . QZ_PERIOD |
| 118 | 15, // 48 0x30 0x0f TAB QZ_TAB |
| 119 | 57, // 49 0x31 0x39 SPACE QZ_SPACE |
| 120 | 41, // 50 0x32 0x29 ` QZ_BACKQUOTE |
| 121 | 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE |
| 122 | 0, // 52 0x34 Undefined |
| 123 | 1, // 53 0x35 0x01 ESC QZ_ESCAPE |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 124 | 220, // 54 0x36 0xdc E0,5C R GUI QZ_RMETA |
| 125 | 219, // 55 0x37 0xdb E0,5B L GUI QZ_LMETA |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 126 | 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT |
| 127 | 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK |
| 128 | 56, // 58 0x3A 0x38 L ALT QZ_LALT |
| 129 | 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL |
| 130 | 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT |
| 131 | 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT |
| 132 | 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL |
| 133 | 0, // 63 0x3F Undefined |
| 134 | 0, // 64 0x40 Undefined |
| 135 | 0, // 65 0x41 Undefined |
| 136 | 0, // 66 0x42 Undefined |
| 137 | 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY |
| 138 | 0, // 68 0x44 Undefined |
| 139 | 78, // 69 0x45 0x4e KP + QZ_KP_PLUS |
| 140 | 0, // 70 0x46 Undefined |
| 141 | 69, // 71 0x47 0x45 NUM QZ_NUMLOCK |
| 142 | 0, // 72 0x48 Undefined |
| 143 | 0, // 73 0x49 Undefined |
| 144 | 0, // 74 0x4A Undefined |
| 145 | 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE |
| 146 | 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER |
| 147 | 0, // 77 0x4D undefined |
| 148 | 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS |
| 149 | 0, // 79 0x4F Undefined |
| 150 | 0, // 80 0x50 Undefined |
| 151 | 0, // 81 0x51 QZ_KP_EQUALS |
| 152 | 82, // 82 0x52 0x52 KP 0 QZ_KP0 |
| 153 | 79, // 83 0x53 0x4f KP 1 QZ_KP1 |
| 154 | 80, // 84 0x54 0x50 KP 2 QZ_KP2 |
| 155 | 81, // 85 0x55 0x51 KP 3 QZ_KP3 |
| 156 | 75, // 86 0x56 0x4b KP 4 QZ_KP4 |
| 157 | 76, // 87 0x57 0x4c KP 5 QZ_KP5 |
| 158 | 77, // 88 0x58 0x4d KP 6 QZ_KP6 |
| 159 | 71, // 89 0x59 0x47 KP 7 QZ_KP7 |
| 160 | 0, // 90 0x5A Undefined |
| 161 | 72, // 91 0x5B 0x48 KP 8 QZ_KP8 |
| 162 | 73, // 92 0x5C 0x49 KP 9 QZ_KP9 |
| 163 | 0, // 93 0x5D Undefined |
| 164 | 0, // 94 0x5E Undefined |
| 165 | 0, // 95 0x5F Undefined |
| 166 | 63, // 96 0x60 0x3f F5 QZ_F5 |
| 167 | 64, // 97 0x61 0x40 F6 QZ_F6 |
| 168 | 65, // 98 0x62 0x41 F7 QZ_F7 |
| 169 | 61, // 99 0x63 0x3d F3 QZ_F3 |
| 170 | 66, // 100 0x64 0x42 F8 QZ_F8 |
| 171 | 67, // 101 0x65 0x43 F9 QZ_F9 |
| 172 | 0, // 102 0x66 Undefined |
| 173 | 87, // 103 0x67 0x57 F11 QZ_F11 |
| 174 | 0, // 104 0x68 Undefined |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 175 | 183,// 105 0x69 0xb7 QZ_PRINT |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 176 | 0, // 106 0x6A Undefined |
| 177 | 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK |
| 178 | 0, // 108 0x6C Undefined |
| 179 | 68, // 109 0x6D 0x44 F10 QZ_F10 |
| 180 | 0, // 110 0x6E Undefined |
| 181 | 88, // 111 0x6F 0x58 F12 QZ_F12 |
| 182 | 0, // 112 0x70 Undefined |
| 183 | 110,// 113 0x71 0x0 QZ_PAUSE |
| 184 | 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT |
| 185 | 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME |
| 186 | 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP |
| 187 | 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE |
| 188 | 62, // 118 0x76 0x3e F4 QZ_F4 |
| 189 | 207,// 119 0x77 0xcf E0,4f END QZ_END |
| 190 | 60, // 120 0x78 0x3c F2 QZ_F2 |
| 191 | 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN |
| 192 | 59, // 122 0x7A 0x3b F1 QZ_F1 |
| 193 | 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT |
| 194 | 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT |
| 195 | 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN |
| 196 | 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP |
| 197 | /* 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] | 198 | |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 199 | /* 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] | 200 | /* |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 201 | 221 // 0xdd e0,5d APPS |
| 202 | // E0,2A,E0,37 PRNT SCRN |
| 203 | // E1,1D,45,E1,9D,C5 PAUSE |
| 204 | 83 // 0x53 0x53 KP . |
| 205 | // ACPI Scan Codes |
| 206 | 222 // 0xde E0, 5E Power |
| 207 | 223 // 0xdf E0, 5F Sleep |
| 208 | 227 // 0xe3 E0, 63 Wake |
| 209 | // Windows Multimedia Scan Codes |
| 210 | 153 // 0x99 E0, 19 Next Track |
| 211 | 144 // 0x90 E0, 10 Previous Track |
| 212 | 164 // 0xa4 E0, 24 Stop |
| 213 | 162 // 0xa2 E0, 22 Play/Pause |
| 214 | 160 // 0xa0 E0, 20 Mute |
| 215 | 176 // 0xb0 E0, 30 Volume Up |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 216 | 174 // 0xae E0, 2E Volume Down |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 217 | 237 // 0xed E0, 6D Media Select |
| 218 | 236 // 0xec E0, 6C E-Mail |
| 219 | 161 // 0xa1 E0, 21 Calculator |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 220 | 235 // 0xeb E0, 6B My Computer |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 221 | 229 // 0xe5 E0, 65 WWW Search |
| 222 | 178 // 0xb2 E0, 32 WWW Home |
| 223 | 234 // 0xea E0, 6A WWW Back |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 224 | 233 // 0xe9 E0, 69 WWW Forward |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 225 | 232 // 0xe8 E0, 68 WWW Stop |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 226 | 231 // 0xe7 E0, 67 WWW Refresh |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 227 | 230 // 0xe6 E0, 66 WWW Favorites |
bellard | 87f48e6 | 2005-10-30 18:24:49 +0000 | [diff] [blame] | 228 | */ |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
Andreas Färber | 77047bb | 2009-12-13 00:55:53 +0100 | [diff] [blame] | 231 | static int cocoa_keycode_to_qemu(int keycode) |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 232 | { |
Stefan Weil | 5d70192 | 2013-12-07 14:48:05 +0100 | [diff] [blame] | 233 | if (ARRAY_SIZE(keymap) <= keycode) { |
Peter Maydell | 01cc4e6 | 2013-12-08 22:59:04 +0000 | [diff] [blame] | 234 | fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | return keymap[keycode]; |
| 238 | } |
| 239 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 240 | |
| 241 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 242 | /* |
| 243 | ------------------------------------------------------ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 244 | QemuCocoaView |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 245 | ------------------------------------------------------ |
| 246 | */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 247 | @interface QemuCocoaView : NSView |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 248 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 249 | QEMUScreen screen; |
| 250 | NSWindow *fullScreenWindow; |
| 251 | float cx,cy,cw,ch,cdx,cdy; |
| 252 | CGDataProviderRef dataProviderRef; |
| 253 | int modifiers_state[256]; |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 254 | BOOL isMouseGrabbed; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 255 | BOOL isFullscreen; |
| 256 | BOOL isAbsoluteEnabled; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 257 | BOOL isMouseDeassociated; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 258 | } |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 259 | - (void) switchSurface:(DisplaySurface *)surface; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 260 | - (void) grabMouse; |
| 261 | - (void) ungrabMouse; |
| 262 | - (void) toggleFullScreen:(id)sender; |
| 263 | - (void) handleEvent:(NSEvent *)event; |
| 264 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 265 | /* The state surrounding mouse grabbing is potentially confusing. |
| 266 | * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated |
| 267 | * pointing device an absolute-position one?"], but is only updated on |
| 268 | * next refresh. |
| 269 | * isMouseGrabbed tracks whether GUI events are directed to the guest; |
| 270 | * it controls whether special keys like Cmd get sent to the guest, |
| 271 | * and whether we capture the mouse when in non-absolute mode. |
| 272 | * isMouseDeassociated tracks whether we've told MacOSX to disassociate |
| 273 | * the mouse and mouse cursor position by calling |
| 274 | * CGAssociateMouseAndMouseCursorPosition(FALSE) |
| 275 | * (which basically happens if we grab in non-absolute mode). |
| 276 | */ |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 277 | - (BOOL) isMouseGrabbed; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 278 | - (BOOL) isAbsoluteEnabled; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 279 | - (BOOL) isMouseDeassociated; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 280 | - (float) cdx; |
| 281 | - (float) cdy; |
| 282 | - (QEMUScreen) gscreen; |
| 283 | @end |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 284 | |
Andreas Färber | 7fee199 | 2011-06-09 20:53:32 +0200 | [diff] [blame] | 285 | QemuCocoaView *cocoaView; |
| 286 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 287 | @implementation QemuCocoaView |
| 288 | - (id)initWithFrame:(NSRect)frameRect |
| 289 | { |
| 290 | COCOA_DEBUG("QemuCocoaView: initWithFrame\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 291 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 292 | self = [super initWithFrame:frameRect]; |
| 293 | if (self) { |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 294 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 295 | screen.bitsPerComponent = 8; |
| 296 | screen.bitsPerPixel = 32; |
| 297 | screen.width = frameRect.size.width; |
| 298 | screen.height = frameRect.size.height; |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 299 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 300 | } |
| 301 | return self; |
| 302 | } |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 303 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 304 | - (void) dealloc |
| 305 | { |
| 306 | COCOA_DEBUG("QemuCocoaView: dealloc\n"); |
bellard | 7c206a7 | 2005-12-18 19:18:45 +0000 | [diff] [blame] | 307 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 308 | if (dataProviderRef) |
| 309 | CGDataProviderRelease(dataProviderRef); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 310 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 311 | [super dealloc]; |
| 312 | } |
bellard | 5cbfcd0 | 2006-06-14 15:53:24 +0000 | [diff] [blame] | 313 | |
Andreas Färber | d50f71d | 2009-12-13 02:03:33 +0100 | [diff] [blame] | 314 | - (BOOL) isOpaque |
| 315 | { |
| 316 | return YES; |
| 317 | } |
| 318 | |
Peter Maydell | 5dd45be | 2014-06-23 10:35:23 +0100 | [diff] [blame] | 319 | - (BOOL) screenContainsPoint:(NSPoint) p |
| 320 | { |
| 321 | return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height); |
| 322 | } |
| 323 | |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 324 | - (void) hideCursor |
| 325 | { |
| 326 | if (!cursor_hide) { |
| 327 | return; |
| 328 | } |
| 329 | [NSCursor hide]; |
| 330 | } |
| 331 | |
| 332 | - (void) unhideCursor |
| 333 | { |
| 334 | if (!cursor_hide) { |
| 335 | return; |
| 336 | } |
| 337 | [NSCursor unhide]; |
| 338 | } |
| 339 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 340 | - (void) drawRect:(NSRect) rect |
| 341 | { |
| 342 | COCOA_DEBUG("QemuCocoaView: drawRect\n"); |
bellard | 5cbfcd0 | 2006-06-14 15:53:24 +0000 | [diff] [blame] | 343 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 344 | // get CoreGraphic context |
| 345 | CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort]; |
| 346 | CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone); |
| 347 | CGContextSetShouldAntialias (viewContextRef, NO); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 348 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 349 | // draw screen bitmap directly to Core Graphics context |
Peter Maydell | 7d270b1 | 2013-12-24 02:51:47 +0000 | [diff] [blame] | 350 | if (!dataProviderRef) { |
| 351 | // Draw request before any guest device has set up a framebuffer: |
| 352 | // just draw an opaque black rectangle |
| 353 | CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0); |
| 354 | CGContextFillRect(viewContextRef, NSRectToCGRect(rect)); |
| 355 | } else { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 356 | CGImageRef imageRef = CGImageCreate( |
| 357 | screen.width, //width |
| 358 | screen.height, //height |
| 359 | screen.bitsPerComponent, //bitsPerComponent |
| 360 | screen.bitsPerPixel, //bitsPerPixel |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 361 | (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow |
Andreas Färber | 04afa4a | 2009-12-13 00:58:21 +0100 | [diff] [blame] | 362 | #ifdef __LITTLE_ENDIAN__ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 363 | CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4 |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 364 | kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 365 | #else |
| 366 | CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc) |
| 367 | kCGImageAlphaNoneSkipFirst, //bitmapInfo |
| 368 | #endif |
| 369 | dataProviderRef, //provider |
| 370 | NULL, //decode |
| 371 | 0, //interpolate |
| 372 | kCGRenderingIntentDefault //intent |
| 373 | ); |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 374 | // selective drawing code (draws only dirty rectangles) (OS X >= 10.4) |
| 375 | const NSRect *rectList; |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 376 | NSInteger rectCount; |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 377 | int i; |
| 378 | CGImageRef clipImageRef; |
| 379 | CGRect clipRect; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 380 | |
Peter Maydell | b63901d | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 381 | [self getRectsBeingDrawn:&rectList count:&rectCount]; |
| 382 | for (i = 0; i < rectCount; i++) { |
| 383 | clipRect.origin.x = rectList[i].origin.x / cdx; |
| 384 | clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy; |
| 385 | clipRect.size.width = rectList[i].size.width / cdx; |
| 386 | clipRect.size.height = rectList[i].size.height / cdy; |
| 387 | clipImageRef = CGImageCreateWithImageInRect( |
| 388 | imageRef, |
| 389 | clipRect |
| 390 | ); |
| 391 | CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef); |
| 392 | CGImageRelease (clipImageRef); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 393 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 394 | CGImageRelease (imageRef); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 398 | - (void) setContentDimensions |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 399 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 400 | COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 401 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 402 | if (isFullscreen) { |
| 403 | cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width; |
| 404 | cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 405 | |
| 406 | /* stretches video, but keeps same aspect ratio */ |
| 407 | if (stretch_video == true) { |
| 408 | /* use smallest stretch value - prevents clipping on sides */ |
| 409 | if (MIN(cdx, cdy) == cdx) { |
| 410 | cdy = cdx; |
| 411 | } else { |
| 412 | cdx = cdy; |
| 413 | } |
| 414 | } else { /* No stretching */ |
| 415 | cdx = cdy = 1; |
| 416 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 417 | cw = screen.width * cdx; |
| 418 | ch = screen.height * cdy; |
| 419 | cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; |
| 420 | cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; |
| 421 | } else { |
| 422 | cx = 0; |
| 423 | cy = 0; |
| 424 | cw = screen.width; |
| 425 | ch = screen.height; |
| 426 | cdx = 1.0; |
| 427 | cdy = 1.0; |
| 428 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 431 | - (void) switchSurface:(DisplaySurface *)surface |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 432 | { |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 433 | COCOA_DEBUG("QemuCocoaView: switchSurface\n"); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 434 | |
Peter Maydell | 8510d91 | 2013-03-18 20:28:21 +0000 | [diff] [blame] | 435 | int w = surface_width(surface); |
| 436 | int h = surface_height(surface); |
Peter Maydell | 381600d | 2014-06-23 10:35:22 +0100 | [diff] [blame] | 437 | /* cdx == 0 means this is our very first surface, in which case we need |
| 438 | * to recalculate the content dimensions even if it happens to be the size |
| 439 | * of the initial empty window. |
| 440 | */ |
| 441 | bool isResize = (w != screen.width || h != screen.height || cdx == 0.0); |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 442 | |
| 443 | int oldh = screen.height; |
| 444 | if (isResize) { |
| 445 | // Resize before we trigger the redraw, or we'll redraw at the wrong size |
| 446 | COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h); |
| 447 | screen.width = w; |
| 448 | screen.height = h; |
| 449 | [self setContentDimensions]; |
| 450 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; |
| 451 | } |
Peter Maydell | 8510d91 | 2013-03-18 20:28:21 +0000 | [diff] [blame] | 452 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 453 | // update screenBuffer |
| 454 | if (dataProviderRef) |
| 455 | CGDataProviderRelease(dataProviderRef); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 456 | |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 457 | //sync host window color space with guests |
Peter Maydell | 49060c2 | 2013-12-24 11:54:12 +0000 | [diff] [blame] | 458 | screen.bitsPerPixel = surface_bits_per_pixel(surface); |
| 459 | screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 460 | |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 461 | dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 462 | |
| 463 | // update windows |
| 464 | if (isFullscreen) { |
| 465 | [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]]; |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 466 | [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] | 467 | } else { |
| 468 | if (qemu_name) |
| 469 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 470 | [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] | 471 | } |
Peter Maydell | d3345a0 | 2013-12-24 02:51:46 +0000 | [diff] [blame] | 472 | |
| 473 | if (isResize) { |
| 474 | [normalWindow center]; |
| 475 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | - (void) toggleFullScreen:(id)sender |
| 479 | { |
| 480 | COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); |
| 481 | |
| 482 | if (isFullscreen) { // switch from fullscreen to desktop |
| 483 | isFullscreen = FALSE; |
| 484 | [self ungrabMouse]; |
| 485 | [self setContentDimensions]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 486 | if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime |
| 487 | [self exitFullScreenModeWithOptions:nil]; |
| 488 | } else { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 489 | [fullScreenWindow close]; |
| 490 | [normalWindow setContentView: self]; |
| 491 | [normalWindow makeKeyAndOrderFront: self]; |
| 492 | [NSMenu setMenuBarVisible:YES]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 493 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 494 | } else { // switch from desktop to fullscreen |
| 495 | isFullscreen = TRUE; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 496 | [normalWindow orderOut: nil]; /* Hide the window */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 497 | [self grabMouse]; |
| 498 | [self setContentDimensions]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 499 | if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime |
| 500 | [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys: |
| 501 | [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens, |
| 502 | [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting, |
| 503 | nil]]; |
| 504 | } else { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 505 | [NSMenu setMenuBarVisible:NO]; |
| 506 | fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] |
| 507 | styleMask:NSBorderlessWindowMask |
| 508 | backing:NSBackingStoreBuffered |
| 509 | defer:NO]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 510 | [fullScreenWindow setAcceptsMouseMovedEvents: YES]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 511 | [fullScreenWindow setHasShadow:NO]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 512 | [fullScreenWindow setBackgroundColor: [NSColor blackColor]]; |
| 513 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; |
| 514 | [[fullScreenWindow contentView] addSubview: self]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 515 | [fullScreenWindow makeKeyAndOrderFront:self]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 516 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | - (void) handleEvent:(NSEvent *)event |
| 521 | { |
| 522 | COCOA_DEBUG("QemuCocoaView: handleEvent\n"); |
| 523 | |
| 524 | int buttons = 0; |
| 525 | int keycode; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 526 | bool mouse_event = false; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 527 | NSPoint p = [event locationInWindow]; |
| 528 | |
| 529 | switch ([event type]) { |
| 530 | case NSFlagsChanged: |
| 531 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 532 | |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 533 | if ((keycode == 219 || keycode == 220) && !isMouseGrabbed) { |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 534 | /* Don't pass command key changes to guest unless mouse is grabbed */ |
| 535 | keycode = 0; |
| 536 | } |
| 537 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 538 | if (keycode) { |
| 539 | 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] | 540 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
| 541 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 542 | } else if (qemu_console_is_graphic(NULL)) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 543 | if (modifiers_state[keycode] == 0) { // keydown |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 544 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 545 | modifiers_state[keycode] = 1; |
| 546 | } else { // keyup |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 547 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 548 | modifiers_state[keycode] = 0; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // release Mouse grab when pressing ctrl+alt |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 554 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 555 | [self ungrabMouse]; |
| 556 | } |
| 557 | break; |
| 558 | case NSKeyDown: |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 559 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 560 | |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 561 | // 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] | 562 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 563 | [NSApp sendEvent:event]; |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | // default |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 568 | |
| 569 | // handle control + alt Key Combos (ctrl+alt is reserved for QEMU) |
| 570 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { |
| 571 | switch (keycode) { |
| 572 | |
| 573 | // enable graphic console |
| 574 | case 0x02 ... 0x0a: // '1' to '9' keys |
| 575 | console_select(keycode - 0x02); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | // handle keys for graphic console |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 580 | } else if (qemu_console_is_graphic(NULL)) { |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 581 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 582 | |
| 583 | // handlekeys for Monitor |
| 584 | } else { |
| 585 | int keysym = 0; |
| 586 | switch([event keyCode]) { |
| 587 | case 115: |
| 588 | keysym = QEMU_KEY_HOME; |
| 589 | break; |
| 590 | case 117: |
| 591 | keysym = QEMU_KEY_DELETE; |
| 592 | break; |
| 593 | case 119: |
| 594 | keysym = QEMU_KEY_END; |
| 595 | break; |
| 596 | case 123: |
| 597 | keysym = QEMU_KEY_LEFT; |
| 598 | break; |
| 599 | case 124: |
| 600 | keysym = QEMU_KEY_RIGHT; |
| 601 | break; |
| 602 | case 125: |
| 603 | keysym = QEMU_KEY_DOWN; |
| 604 | break; |
| 605 | case 126: |
| 606 | keysym = QEMU_KEY_UP; |
| 607 | break; |
| 608 | default: |
| 609 | { |
| 610 | NSString *ks = [event characters]; |
| 611 | if ([ks length] > 0) |
| 612 | keysym = [ks characterAtIndex:0]; |
| 613 | } |
| 614 | } |
| 615 | if (keysym) |
| 616 | kbd_put_keysym(keysym); |
| 617 | } |
| 618 | break; |
| 619 | case NSKeyUp: |
| 620 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 621 | |
| 622 | // don't pass the guest a spurious key-up if we treated this |
| 623 | // command-key combo as a host UI action |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 624 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
Peter Maydell | 8895919 | 2013-12-08 22:59:02 +0000 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 628 | if (qemu_console_is_graphic(NULL)) { |
Gerd Hoffmann | 2e08c66 | 2013-12-04 12:53:44 +0100 | [diff] [blame] | 629 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 630 | } |
| 631 | break; |
| 632 | case NSMouseMoved: |
| 633 | if (isAbsoluteEnabled) { |
Peter Maydell | 5dd45be | 2014-06-23 10:35:23 +0100 | [diff] [blame] | 634 | if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) { |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 635 | if (isMouseGrabbed) { |
| 636 | [self ungrabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 637 | } |
| 638 | } else { |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 639 | if (!isMouseGrabbed) { |
| 640 | [self grabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 644 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 645 | break; |
| 646 | case NSLeftMouseDown: |
| 647 | if ([event modifierFlags] & NSCommandKeyMask) { |
| 648 | buttons |= MOUSE_EVENT_RBUTTON; |
| 649 | } else { |
| 650 | buttons |= MOUSE_EVENT_LBUTTON; |
| 651 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 652 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 653 | break; |
| 654 | case NSRightMouseDown: |
| 655 | buttons |= MOUSE_EVENT_RBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 656 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 657 | break; |
| 658 | case NSOtherMouseDown: |
| 659 | buttons |= MOUSE_EVENT_MBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 660 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 661 | break; |
| 662 | case NSLeftMouseDragged: |
| 663 | if ([event modifierFlags] & NSCommandKeyMask) { |
| 664 | buttons |= MOUSE_EVENT_RBUTTON; |
| 665 | } else { |
| 666 | buttons |= MOUSE_EVENT_LBUTTON; |
| 667 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 668 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 669 | break; |
| 670 | case NSRightMouseDragged: |
| 671 | buttons |= MOUSE_EVENT_RBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 672 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 673 | break; |
| 674 | case NSOtherMouseDragged: |
| 675 | buttons |= MOUSE_EVENT_MBUTTON; |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 676 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 677 | break; |
| 678 | case NSLeftMouseUp: |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 679 | mouse_event = true; |
| 680 | if (!isMouseGrabbed && [self screenContainsPoint:p]) { |
| 681 | [self grabMouse]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 682 | } |
| 683 | break; |
| 684 | case NSRightMouseUp: |
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 NSOtherMouseUp: |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 688 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 689 | break; |
| 690 | case NSScrollWheel: |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 691 | if (isMouseGrabbed) { |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 692 | buttons |= ([event deltaY] < 0) ? |
| 693 | MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 694 | } |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 695 | mouse_event = true; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 696 | break; |
| 697 | default: |
| 698 | [NSApp sendEvent:event]; |
| 699 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 700 | |
| 701 | if (mouse_event) { |
| 702 | if (last_buttons != buttons) { |
| 703 | static uint32_t bmap[INPUT_BUTTON_MAX] = { |
| 704 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
| 705 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, |
| 706 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, |
| 707 | [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP, |
| 708 | [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN, |
| 709 | }; |
| 710 | qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons); |
| 711 | last_buttons = buttons; |
| 712 | } |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 713 | if (isMouseGrabbed) { |
| 714 | if (isAbsoluteEnabled) { |
| 715 | /* Note that the origin for Cocoa mouse coords is bottom left, not top left. |
| 716 | * The check on screenContainsPoint is to avoid sending out of range values for |
| 717 | * clicks in the titlebar. |
| 718 | */ |
| 719 | if ([self screenContainsPoint:p]) { |
| 720 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width); |
| 721 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height); |
| 722 | } |
| 723 | } else { |
| 724 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]); |
| 725 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]); |
| 726 | } |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 727 | } else { |
| 728 | [NSApp sendEvent:event]; |
| 729 | } |
| 730 | qemu_input_event_sync(); |
| 731 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | - (void) grabMouse |
| 735 | { |
| 736 | COCOA_DEBUG("QemuCocoaView: grabMouse\n"); |
| 737 | |
| 738 | if (!isFullscreen) { |
| 739 | if (qemu_name) |
| 740 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]]; |
| 741 | else |
| 742 | [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"]; |
| 743 | } |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 744 | [self hideCursor]; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 745 | if (!isAbsoluteEnabled) { |
| 746 | isMouseDeassociated = TRUE; |
| 747 | CGAssociateMouseAndMouseCursorPosition(FALSE); |
| 748 | } |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 749 | isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | - (void) ungrabMouse |
| 753 | { |
| 754 | COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); |
| 755 | |
| 756 | if (!isFullscreen) { |
| 757 | if (qemu_name) |
| 758 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; |
| 759 | else |
| 760 | [normalWindow setTitle:@"QEMU"]; |
| 761 | } |
Peter Maydell | 13aefd3 | 2014-06-23 10:35:25 +0100 | [diff] [blame] | 762 | [self unhideCursor]; |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 763 | if (isMouseDeassociated) { |
| 764 | CGAssociateMouseAndMouseCursorPosition(TRUE); |
| 765 | isMouseDeassociated = FALSE; |
| 766 | } |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 767 | isMouseGrabbed = FALSE; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;} |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 771 | - (BOOL) isMouseGrabbed {return isMouseGrabbed;} |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 772 | - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} |
Peter Maydell | f61c387 | 2014-06-23 10:35:24 +0100 | [diff] [blame] | 773 | - (BOOL) isMouseDeassociated {return isMouseDeassociated;} |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 774 | - (float) cdx {return cdx;} |
| 775 | - (float) cdy {return cdy;} |
| 776 | - (QEMUScreen) gscreen {return screen;} |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 777 | @end |
| 778 | |
| 779 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 780 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 781 | /* |
| 782 | ------------------------------------------------------ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 783 | QemuCocoaAppController |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 784 | ------------------------------------------------------ |
| 785 | */ |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 786 | @interface QemuCocoaAppController : NSObject |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 787 | { |
| 788 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 789 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv; |
Peter Maydell | de1aade | 2015-05-19 09:11:18 +0100 | [diff] [blame^] | 790 | - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 791 | - (void)doToggleFullScreen:(id)sender; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 792 | - (void)toggleFullScreen:(id)sender; |
| 793 | - (void)showQEMUDoc:(id)sender; |
| 794 | - (void)showQEMUTec:(id)sender; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 795 | - (void)zoomToFit:(id) sender; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 796 | @end |
| 797 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 798 | @implementation QemuCocoaAppController |
| 799 | - (id) init |
| 800 | { |
| 801 | COCOA_DEBUG("QemuCocoaAppController: init\n"); |
| 802 | |
| 803 | self = [super init]; |
| 804 | if (self) { |
| 805 | |
| 806 | // create a view and add it to the window |
| 807 | cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)]; |
| 808 | if(!cocoaView) { |
| 809 | fprintf(stderr, "(cocoa) can't create a view\n"); |
| 810 | exit(1); |
| 811 | } |
| 812 | |
| 813 | // create a window |
| 814 | normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame] |
| 815 | styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask |
| 816 | backing:NSBackingStoreBuffered defer:NO]; |
| 817 | if(!normalWindow) { |
| 818 | fprintf(stderr, "(cocoa) can't create window\n"); |
| 819 | exit(1); |
| 820 | } |
| 821 | [normalWindow setAcceptsMouseMovedEvents:YES]; |
| 822 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]]; |
| 823 | [normalWindow setContentView:cocoaView]; |
Andreas Färber | 561ef25 | 2009-12-13 03:06:20 +0100 | [diff] [blame] | 824 | [normalWindow useOptimizedDrawing:YES]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 825 | [normalWindow makeKeyAndOrderFront:self]; |
Peter Maydell | 49060c2 | 2013-12-24 11:54:12 +0000 | [diff] [blame] | 826 | [normalWindow center]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 827 | stretch_video = false; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 828 | } |
| 829 | return self; |
| 830 | } |
| 831 | |
| 832 | - (void) dealloc |
| 833 | { |
| 834 | COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); |
| 835 | |
| 836 | if (cocoaView) |
| 837 | [cocoaView release]; |
| 838 | [super dealloc]; |
| 839 | } |
| 840 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 841 | - (void)applicationDidFinishLaunching: (NSNotification *) note |
| 842 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 843 | COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); |
bellard | 5a24693 | 2005-04-07 20:35:06 +0000 | [diff] [blame] | 844 | |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 845 | // Display an open dialog box if no arguments were passed or |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 846 | // if qemu was launched from the finder ( the Finder passes "-psn" ) |
| 847 | if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 848 | NSOpenPanel *op = [[NSOpenPanel alloc] init]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 849 | [op setPrompt:@"Boot image"]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 850 | [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"]; |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 851 | NSArray *filetypes = [NSArray arrayWithObjects:@"img", @"iso", @"dmg", |
Stefan Hajnoczi | 550830f | 2014-09-16 15:24:24 +0100 | [diff] [blame] | 852 | @"qcow", @"qcow2", @"cloop", @"vmdk", nil]; |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 853 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) |
| 854 | [op setAllowedFileTypes:filetypes]; |
| 855 | [op beginSheetModalForWindow:normalWindow |
| 856 | completionHandler:^(NSInteger returnCode) |
| 857 | { [self openPanelDidEnd:op |
| 858 | returnCode:returnCode contextInfo:NULL ]; } ]; |
| 859 | #else |
| 860 | // Compatibility code for pre-10.6, using deprecated method |
| 861 | [op beginSheetForDirectory:nil file:nil types:filetypes |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 862 | modalForWindow:normalWindow modalDelegate:self |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 863 | didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL]; |
Peter Maydell | 2ba9de6 | 2013-04-22 10:29:49 +0000 | [diff] [blame] | 864 | #endif |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 865 | } else { |
Stefan Weil | 5cbdb3a | 2012-04-07 09:23:39 +0200 | [diff] [blame] | 866 | // or launch QEMU, with the global args |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 867 | [self startEmulationWithArgc:gArgc argv:(char **)gArgv]; |
bellard | 5a24693 | 2005-04-07 20:35:06 +0000 | [diff] [blame] | 868 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | - (void)applicationWillTerminate:(NSNotification *)aNotification |
| 872 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 873 | COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n"); |
| 874 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 875 | qemu_system_shutdown_request(); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 876 | exit(0); |
| 877 | } |
| 878 | |
Andreas Färber | 41ea49b | 2009-12-14 22:13:27 +0100 | [diff] [blame] | 879 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication |
| 880 | { |
| 881 | return YES; |
| 882 | } |
| 883 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 884 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv |
| 885 | { |
| 886 | COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n"); |
| 887 | |
| 888 | int status; |
Andreas Färber | 3bbbee1 | 2011-05-29 19:42:51 +0200 | [diff] [blame] | 889 | status = qemu_main(argc, argv, *_NSGetEnviron()); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 890 | exit(status); |
| 891 | } |
| 892 | |
Peter Maydell | de1aade | 2015-05-19 09:11:18 +0100 | [diff] [blame^] | 893 | - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 894 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 895 | COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 896 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 897 | if(returnCode == NSCancelButton) { |
| 898 | exit(0); |
| 899 | } else if(returnCode == NSOKButton) { |
Peter Maydell | 8bb3f1e | 2013-04-22 10:29:48 +0000 | [diff] [blame] | 900 | char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 901 | |
Peter Maydell | 98db429 | 2013-12-08 22:59:06 +0000 | [diff] [blame] | 902 | char **argv = g_new(char *, 4); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 903 | |
Andreas Färber | 13766eb | 2011-10-30 23:19:54 +0100 | [diff] [blame] | 904 | [sheet close]; |
| 905 | |
Peter Maydell | 98db429 | 2013-12-08 22:59:06 +0000 | [diff] [blame] | 906 | argv[0] = g_strdup(gArgv[0]); |
| 907 | argv[1] = g_strdup("-hda"); |
| 908 | argv[2] = g_strdup(img); |
| 909 | argv[3] = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 910 | |
Peter Maydell | 98db429 | 2013-12-08 22:59:06 +0000 | [diff] [blame] | 911 | // printf("Using argc %d argv %s -hda %s\n", 3, gArgv[0], img); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 912 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 913 | [self startEmulationWithArgc:3 argv:(char**)argv]; |
| 914 | } |
| 915 | } |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 916 | |
| 917 | /* We abstract the method called by the Enter Fullscreen menu item |
| 918 | * because Mac OS 10.7 and higher disables it. This is because of the |
| 919 | * menu item's old selector's name toggleFullScreen: |
| 920 | */ |
| 921 | - (void) doToggleFullScreen:(id)sender |
| 922 | { |
| 923 | [self toggleFullScreen:(id)sender]; |
| 924 | } |
| 925 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 926 | - (void)toggleFullScreen:(id)sender |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 927 | { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 928 | COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); |
| 929 | |
| 930 | [cocoaView toggleFullScreen:sender]; |
| 931 | } |
| 932 | |
| 933 | - (void)showQEMUDoc:(id)sender |
| 934 | { |
| 935 | COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n"); |
| 936 | |
| 937 | [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html", |
| 938 | [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; |
| 939 | } |
| 940 | |
| 941 | - (void)showQEMUTec:(id)sender |
| 942 | { |
| 943 | COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n"); |
| 944 | |
| 945 | [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html", |
| 946 | [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 947 | } |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 948 | |
| 949 | /* Stretches video to fit host monitor size */ |
| 950 | - (void)zoomToFit:(id) sender |
| 951 | { |
| 952 | stretch_video = !stretch_video; |
| 953 | if (stretch_video == true) { |
| 954 | [sender setState: NSOnState]; |
| 955 | } else { |
| 956 | [sender setState: NSOffState]; |
| 957 | } |
| 958 | } |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 959 | @end |
| 960 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 961 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 962 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 963 | int main (int argc, const char * argv[]) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 964 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 965 | gArgc = argc; |
| 966 | gArgv = (char **)argv; |
Andreas Färber | f491880 | 2009-12-13 02:11:44 +0100 | [diff] [blame] | 967 | int i; |
| 968 | |
| 969 | /* In case we don't need to display a window, let's not do that */ |
| 970 | for (i = 1; i < argc; i++) { |
Tristan Gingold | e4ebcc1 | 2011-03-15 14:18:22 +0100 | [diff] [blame] | 971 | const char *opt = argv[i]; |
| 972 | |
| 973 | if (opt[0] == '-') { |
| 974 | /* Treat --foo the same as -foo. */ |
| 975 | if (opt[1] == '-') { |
| 976 | opt++; |
| 977 | } |
Alexandre Raymond | 9851484 | 2011-05-29 18:22:49 -0400 | [diff] [blame] | 978 | if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || |
| 979 | !strcmp(opt, "-vnc") || |
Tristan Gingold | e4ebcc1 | 2011-03-15 14:18:22 +0100 | [diff] [blame] | 980 | !strcmp(opt, "-nographic") || |
| 981 | !strcmp(opt, "-version") || |
Andreas Färber | 60b46aa | 2012-05-28 03:18:31 +0200 | [diff] [blame] | 982 | !strcmp(opt, "-curses") || |
| 983 | !strcmp(opt, "-qtest")) { |
Andreas Färber | 3bbbee1 | 2011-05-29 19:42:51 +0200 | [diff] [blame] | 984 | return qemu_main(gArgc, gArgv, *_NSGetEnviron()); |
Tristan Gingold | e4ebcc1 | 2011-03-15 14:18:22 +0100 | [diff] [blame] | 985 | } |
Andreas Färber | f491880 | 2009-12-13 02:11:44 +0100 | [diff] [blame] | 986 | } |
| 987 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 988 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 989 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 990 | |
Peter Maydell | 42a5dfe | 2013-04-22 10:29:47 +0000 | [diff] [blame] | 991 | // Pull this console process up to being a fully-fledged graphical |
| 992 | // app with a menubar and Dock icon |
| 993 | ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 994 | TransformProcessType(&psn, kProcessTransformToForegroundApplication); |
| 995 | |
| 996 | [NSApplication sharedApplication]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 997 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 998 | // Add menus |
| 999 | NSMenu *menu; |
| 1000 | NSMenuItem *menuItem; |
| 1001 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1002 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1003 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1004 | // Application menu |
| 1005 | menu = [[NSMenu alloc] initWithTitle:@""]; |
| 1006 | [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU |
| 1007 | [menu addItem:[NSMenuItem separatorItem]]; //Separator |
| 1008 | [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU |
| 1009 | menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others |
| 1010 | [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; |
| 1011 | [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All |
| 1012 | [menu addItem:[NSMenuItem separatorItem]]; //Separator |
| 1013 | [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"]; |
| 1014 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; |
| 1015 | [menuItem setSubmenu:menu]; |
| 1016 | [[NSApp mainMenu] addItem:menuItem]; |
| 1017 | [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] | 1018 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1019 | // View menu |
| 1020 | menu = [[NSMenu alloc] initWithTitle:@"View"]; |
Programmingkid | 5d1b2ee | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1021 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen |
| 1022 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1023 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease]; |
| 1024 | [menuItem setSubmenu:menu]; |
| 1025 | [[NSApp mainMenu] addItem:menuItem]; |
| 1026 | |
| 1027 | // Window menu |
| 1028 | menu = [[NSMenu alloc] initWithTitle:@"Window"]; |
| 1029 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize |
| 1030 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; |
| 1031 | [menuItem setSubmenu:menu]; |
| 1032 | [[NSApp mainMenu] addItem:menuItem]; |
| 1033 | [NSApp setWindowsMenu:menu]; |
| 1034 | |
| 1035 | // Help menu |
| 1036 | menu = [[NSMenu alloc] initWithTitle:@"Help"]; |
| 1037 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help |
| 1038 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help |
| 1039 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; |
| 1040 | [menuItem setSubmenu:menu]; |
| 1041 | [[NSApp mainMenu] addItem:menuItem]; |
| 1042 | |
| 1043 | // Create an Application controller |
| 1044 | QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init]; |
| 1045 | [NSApp setDelegate:appController]; |
| 1046 | |
| 1047 | // Start the main event loop |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1048 | [NSApp run]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1049 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1050 | [appController release]; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1051 | [pool release]; |
bellard | cae41b1 | 2006-05-22 21:25:04 +0000 | [diff] [blame] | 1052 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1055 | |
| 1056 | |
| 1057 | |
| 1058 | #pragma mark qemu |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1059 | static void cocoa_update(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1060 | int x, int y, int w, int h) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1061 | { |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1062 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 1063 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1064 | COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); |
| 1065 | |
| 1066 | NSRect rect; |
| 1067 | if ([cocoaView cdx] == 1.0) { |
| 1068 | rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); |
| 1069 | } else { |
| 1070 | rect = NSMakeRect( |
| 1071 | x * [cocoaView cdx], |
| 1072 | ([cocoaView gscreen].height - y - h) * [cocoaView cdy], |
| 1073 | w * [cocoaView cdx], |
| 1074 | h * [cocoaView cdy]); |
| 1075 | } |
Andreas Färber | 17ccbc2 | 2009-12-13 02:08:58 +0100 | [diff] [blame] | 1076 | [cocoaView setNeedsDisplayInRect:rect]; |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1077 | |
| 1078 | [pool release]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Gerd Hoffmann | c12aeb8 | 2013-02-28 15:03:04 +0100 | [diff] [blame] | 1081 | static void cocoa_switch(DisplayChangeListener *dcl, |
Gerd Hoffmann | c12aeb8 | 2013-02-28 15:03:04 +0100 | [diff] [blame] | 1082 | DisplaySurface *surface) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1083 | { |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1084 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1085 | |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1086 | COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); |
Gerd Hoffmann | 5e00d3a | 2013-03-01 12:52:06 +0100 | [diff] [blame] | 1087 | [cocoaView switchSurface:surface]; |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1088 | [pool release]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 1091 | static void cocoa_refresh(DisplayChangeListener *dcl) |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1092 | { |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1093 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 1094 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1095 | COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n"); |
| 1096 | |
Gerd Hoffmann | 21bae11 | 2013-12-04 14:08:04 +0100 | [diff] [blame] | 1097 | if (qemu_input_is_absolute()) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1098 | if (![cocoaView isAbsoluteEnabled]) { |
Peter Maydell | 49b9bd4 | 2013-12-08 22:59:03 +0000 | [diff] [blame] | 1099 | if ([cocoaView isMouseGrabbed]) { |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1100 | [cocoaView ungrabMouse]; |
| 1101 | } |
| 1102 | } |
| 1103 | [cocoaView setAbsoluteEnabled:YES]; |
| 1104 | } |
| 1105 | |
| 1106 | NSDate *distantPast; |
| 1107 | NSEvent *event; |
| 1108 | distantPast = [NSDate distantPast]; |
| 1109 | do { |
| 1110 | event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast |
| 1111 | inMode: NSDefaultRunLoopMode dequeue:YES]; |
| 1112 | if (event != nil) { |
| 1113 | [cocoaView handleEvent:event]; |
| 1114 | } |
| 1115 | } while(event != nil); |
Peter Maydell | 68c0aa6 | 2013-04-17 09:16:35 +0000 | [diff] [blame] | 1116 | graphic_hw_update(NULL); |
Peter Maydell | 6e657e6 | 2013-04-22 10:29:46 +0000 | [diff] [blame] | 1117 | [pool release]; |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | static void cocoa_cleanup(void) |
| 1121 | { |
| 1122 | COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n"); |
Blue Swirl | 58a0667 | 2011-08-21 18:42:08 +0000 | [diff] [blame] | 1123 | g_free(dcl); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1126 | static const DisplayChangeListenerOps dcl_ops = { |
| 1127 | .dpy_name = "cocoa", |
Peter Maydell | 8510d91 | 2013-03-18 20:28:21 +0000 | [diff] [blame] | 1128 | .dpy_gfx_update = cocoa_update, |
| 1129 | .dpy_gfx_switch = cocoa_switch, |
| 1130 | .dpy_refresh = cocoa_refresh, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1131 | }; |
| 1132 | |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1133 | void cocoa_display_init(DisplayState *ds, int full_screen) |
| 1134 | { |
| 1135 | COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); |
| 1136 | |
Programmingkid | 43227af | 2015-05-19 09:11:17 +0100 | [diff] [blame] | 1137 | /* if fullscreen mode is to be used */ |
| 1138 | if (full_screen == true) { |
| 1139 | [NSApp activateIgnoringOtherApps: YES]; |
| 1140 | [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil]; |
| 1141 | } |
| 1142 | |
Blue Swirl | 58a0667 | 2011-08-21 18:42:08 +0000 | [diff] [blame] | 1143 | dcl = g_malloc0(sizeof(DisplayChangeListener)); |
| 1144 | |
aliguori | 9794f74 | 2009-03-04 19:25:22 +0000 | [diff] [blame] | 1145 | // register vga output callbacks |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 1146 | dcl->ops = &dcl_ops; |
Gerd Hoffmann | 5209089 | 2013-04-23 15:44:31 +0200 | [diff] [blame] | 1147 | register_displaychangelistener(dcl); |
ths | c304f7e | 2008-01-22 23:25:15 +0000 | [diff] [blame] | 1148 | |
| 1149 | // register cleanup function |
| 1150 | atexit(cocoa_cleanup); |
| 1151 | } |