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