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