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