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