]>
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" |
8524f1c7 | 32 | #include "qmp-commands.h" |
693a3e01 | 33 | #include "sysemu/blockdev.h" |
da4dbf74 | 34 | |
44e4c0ba AF |
35 | #ifndef MAC_OS_X_VERSION_10_5 |
36 | #define MAC_OS_X_VERSION_10_5 1050 | |
37 | #endif | |
2ba9de6e PM |
38 | #ifndef MAC_OS_X_VERSION_10_6 |
39 | #define MAC_OS_X_VERSION_10_6 1060 | |
40 | #endif | |
81801ae2 PM |
41 | #ifndef MAC_OS_X_VERSION_10_10 |
42 | #define MAC_OS_X_VERSION_10_10 101000 | |
43 | #endif | |
44e4c0ba | 44 | |
3b46e624 | 45 | |
c304f7e2 | 46 | //#define DEBUG |
3b46e624 | 47 | |
c304f7e2 TS |
48 | #ifdef DEBUG |
49 | #define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); } | |
b29169d2 | 50 | #else |
c304f7e2 | 51 | #define COCOA_DEBUG(...) ((void) 0) |
b29169d2 BS |
52 | #endif |
53 | ||
c304f7e2 | 54 | #define cgrect(nsrect) (*(CGRect *)&(nsrect)) |
5b0753e0 | 55 | |
c304f7e2 TS |
56 | typedef struct { |
57 | int width; | |
58 | int height; | |
59 | int bitsPerComponent; | |
60 | int bitsPerPixel; | |
61 | } QEMUScreen; | |
62 | ||
c304f7e2 | 63 | NSWindow *normalWindow; |
9794f74f | 64 | static DisplayChangeListener *dcl; |
21bae11a | 65 | static int last_buttons; |
c304f7e2 TS |
66 | |
67 | int gArgc; | |
68 | char **gArgv; | |
5d1b2eef | 69 | bool stretch_video; |
8524f1c7 | 70 | NSTextField *pauseLabel; |
693a3e01 | 71 | NSArray * supportedImageFileTypes; |
5b0753e0 | 72 | |
c304f7e2 | 73 | // keymap conversion |
87f48e6a | 74 | int keymap[] = |
5b0753e0 | 75 | { |
87f48e6a FB |
76 | // SdlI macI macH SdlH 104xtH 104xtC sdl |
77 | 30, // 0 0x00 0x1e A QZ_a | |
78 | 31, // 1 0x01 0x1f S QZ_s | |
79 | 32, // 2 0x02 0x20 D QZ_d | |
80 | 33, // 3 0x03 0x21 F QZ_f | |
81 | 35, // 4 0x04 0x23 H QZ_h | |
82 | 34, // 5 0x05 0x22 G QZ_g | |
83 | 44, // 6 0x06 0x2c Z QZ_z | |
84 | 45, // 7 0x07 0x2d X QZ_x | |
85 | 46, // 8 0x08 0x2e C QZ_c | |
86 | 47, // 9 0x09 0x2f V QZ_v | |
87 | 0, // 10 0x0A Undefined | |
88 | 48, // 11 0x0B 0x30 B QZ_b | |
89 | 16, // 12 0x0C 0x10 Q QZ_q | |
90 | 17, // 13 0x0D 0x11 W QZ_w | |
91 | 18, // 14 0x0E 0x12 E QZ_e | |
92 | 19, // 15 0x0F 0x13 R QZ_r | |
93 | 21, // 16 0x10 0x15 Y QZ_y | |
94 | 20, // 17 0x11 0x14 T QZ_t | |
95 | 2, // 18 0x12 0x02 1 QZ_1 | |
96 | 3, // 19 0x13 0x03 2 QZ_2 | |
97 | 4, // 20 0x14 0x04 3 QZ_3 | |
98 | 5, // 21 0x15 0x05 4 QZ_4 | |
99 | 7, // 22 0x16 0x07 6 QZ_6 | |
100 | 6, // 23 0x17 0x06 5 QZ_5 | |
101 | 13, // 24 0x18 0x0d = QZ_EQUALS | |
102 | 10, // 25 0x19 0x0a 9 QZ_9 | |
103 | 8, // 26 0x1A 0x08 7 QZ_7 | |
104 | 12, // 27 0x1B 0x0c - QZ_MINUS | |
105 | 9, // 28 0x1C 0x09 8 QZ_8 | |
106 | 11, // 29 0x1D 0x0b 0 QZ_0 | |
107 | 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET | |
108 | 24, // 31 0x1F 0x18 O QZ_o | |
109 | 22, // 32 0x20 0x16 U QZ_u | |
110 | 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET | |
111 | 23, // 34 0x22 0x17 I QZ_i | |
112 | 25, // 35 0x23 0x19 P QZ_p | |
113 | 28, // 36 0x24 0x1c ENTER QZ_RETURN | |
114 | 38, // 37 0x25 0x26 L QZ_l | |
115 | 36, // 38 0x26 0x24 J QZ_j | |
116 | 40, // 39 0x27 0x28 ' QZ_QUOTE | |
117 | 37, // 40 0x28 0x25 K QZ_k | |
118 | 39, // 41 0x29 0x27 ; QZ_SEMICOLON | |
119 | 43, // 42 0x2A 0x2b \ QZ_BACKSLASH | |
120 | 51, // 43 0x2B 0x33 , QZ_COMMA | |
121 | 53, // 44 0x2C 0x35 / QZ_SLASH | |
122 | 49, // 45 0x2D 0x31 N QZ_n | |
123 | 50, // 46 0x2E 0x32 M QZ_m | |
124 | 52, // 47 0x2F 0x34 . QZ_PERIOD | |
125 | 15, // 48 0x30 0x0f TAB QZ_TAB | |
126 | 57, // 49 0x31 0x39 SPACE QZ_SPACE | |
127 | 41, // 50 0x32 0x29 ` QZ_BACKQUOTE | |
128 | 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE | |
129 | 0, // 52 0x34 Undefined | |
130 | 1, // 53 0x35 0x01 ESC QZ_ESCAPE | |
8895919a PM |
131 | 220, // 54 0x36 0xdc E0,5C R GUI QZ_RMETA |
132 | 219, // 55 0x37 0xdb E0,5B L GUI QZ_LMETA | |
87f48e6a FB |
133 | 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT |
134 | 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK | |
135 | 56, // 58 0x3A 0x38 L ALT QZ_LALT | |
136 | 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL | |
137 | 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT | |
138 | 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT | |
139 | 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL | |
140 | 0, // 63 0x3F Undefined | |
141 | 0, // 64 0x40 Undefined | |
142 | 0, // 65 0x41 Undefined | |
143 | 0, // 66 0x42 Undefined | |
144 | 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY | |
145 | 0, // 68 0x44 Undefined | |
146 | 78, // 69 0x45 0x4e KP + QZ_KP_PLUS | |
147 | 0, // 70 0x46 Undefined | |
148 | 69, // 71 0x47 0x45 NUM QZ_NUMLOCK | |
149 | 0, // 72 0x48 Undefined | |
150 | 0, // 73 0x49 Undefined | |
151 | 0, // 74 0x4A Undefined | |
152 | 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE | |
153 | 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER | |
154 | 0, // 77 0x4D undefined | |
155 | 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS | |
156 | 0, // 79 0x4F Undefined | |
157 | 0, // 80 0x50 Undefined | |
158 | 0, // 81 0x51 QZ_KP_EQUALS | |
159 | 82, // 82 0x52 0x52 KP 0 QZ_KP0 | |
160 | 79, // 83 0x53 0x4f KP 1 QZ_KP1 | |
161 | 80, // 84 0x54 0x50 KP 2 QZ_KP2 | |
162 | 81, // 85 0x55 0x51 KP 3 QZ_KP3 | |
163 | 75, // 86 0x56 0x4b KP 4 QZ_KP4 | |
164 | 76, // 87 0x57 0x4c KP 5 QZ_KP5 | |
165 | 77, // 88 0x58 0x4d KP 6 QZ_KP6 | |
166 | 71, // 89 0x59 0x47 KP 7 QZ_KP7 | |
167 | 0, // 90 0x5A Undefined | |
168 | 72, // 91 0x5B 0x48 KP 8 QZ_KP8 | |
169 | 73, // 92 0x5C 0x49 KP 9 QZ_KP9 | |
170 | 0, // 93 0x5D Undefined | |
171 | 0, // 94 0x5E Undefined | |
172 | 0, // 95 0x5F Undefined | |
173 | 63, // 96 0x60 0x3f F5 QZ_F5 | |
174 | 64, // 97 0x61 0x40 F6 QZ_F6 | |
175 | 65, // 98 0x62 0x41 F7 QZ_F7 | |
176 | 61, // 99 0x63 0x3d F3 QZ_F3 | |
177 | 66, // 100 0x64 0x42 F8 QZ_F8 | |
178 | 67, // 101 0x65 0x43 F9 QZ_F9 | |
179 | 0, // 102 0x66 Undefined | |
180 | 87, // 103 0x67 0x57 F11 QZ_F11 | |
181 | 0, // 104 0x68 Undefined | |
c304f7e2 | 182 | 183,// 105 0x69 0xb7 QZ_PRINT |
87f48e6a FB |
183 | 0, // 106 0x6A Undefined |
184 | 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK | |
185 | 0, // 108 0x6C Undefined | |
186 | 68, // 109 0x6D 0x44 F10 QZ_F10 | |
187 | 0, // 110 0x6E Undefined | |
188 | 88, // 111 0x6F 0x58 F12 QZ_F12 | |
189 | 0, // 112 0x70 Undefined | |
190 | 110,// 113 0x71 0x0 QZ_PAUSE | |
191 | 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT | |
192 | 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME | |
193 | 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP | |
194 | 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE | |
195 | 62, // 118 0x76 0x3e F4 QZ_F4 | |
196 | 207,// 119 0x77 0xcf E0,4f END QZ_END | |
197 | 60, // 120 0x78 0x3c F2 QZ_F2 | |
198 | 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN | |
199 | 59, // 122 0x7A 0x3b F1 QZ_F1 | |
200 | 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT | |
201 | 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT | |
202 | 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN | |
203 | 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP | |
204 | /* 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 | 205 | |
49b9bd4d | 206 | /* Additional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */ |
87f48e6a | 207 | /* |
3b46e624 TS |
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 | { |
5d70192b | 240 | if (ARRAY_SIZE(keymap) <= keycode) { |
01cc4e6f | 241 | fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode); |
5b0753e0 FB |
242 | return 0; |
243 | } | |
244 | return keymap[keycode]; | |
245 | } | |
246 | ||
693a3e01 JA |
247 | /* Displays an alert dialog box with the specified message */ |
248 | static void QEMU_Alert(NSString *message) | |
249 | { | |
250 | NSAlert *alert; | |
251 | alert = [NSAlert new]; | |
252 | [alert setMessageText: message]; | |
253 | [alert runModal]; | |
254 | } | |
c304f7e2 | 255 | |
693a3e01 JA |
256 | /* Handles any errors that happen with a device transaction */ |
257 | static void handleAnyDeviceErrors(Error * err) | |
258 | { | |
259 | if (err) { | |
260 | QEMU_Alert([NSString stringWithCString: error_get_pretty(err) | |
261 | encoding: NSASCIIStringEncoding]); | |
262 | error_free(err); | |
263 | } | |
264 | } | |
c304f7e2 | 265 | |
5b0753e0 FB |
266 | /* |
267 | ------------------------------------------------------ | |
c304f7e2 | 268 | QemuCocoaView |
5b0753e0 FB |
269 | ------------------------------------------------------ |
270 | */ | |
c304f7e2 | 271 | @interface QemuCocoaView : NSView |
5b0753e0 | 272 | { |
c304f7e2 TS |
273 | QEMUScreen screen; |
274 | NSWindow *fullScreenWindow; | |
275 | float cx,cy,cw,ch,cdx,cdy; | |
276 | CGDataProviderRef dataProviderRef; | |
277 | int modifiers_state[256]; | |
49b9bd4d | 278 | BOOL isMouseGrabbed; |
c304f7e2 TS |
279 | BOOL isFullscreen; |
280 | BOOL isAbsoluteEnabled; | |
f61c387e | 281 | BOOL isMouseDeassociated; |
c304f7e2 | 282 | } |
5e00d3ac | 283 | - (void) switchSurface:(DisplaySurface *)surface; |
c304f7e2 TS |
284 | - (void) grabMouse; |
285 | - (void) ungrabMouse; | |
286 | - (void) toggleFullScreen:(id)sender; | |
287 | - (void) handleEvent:(NSEvent *)event; | |
288 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; | |
f61c387e PM |
289 | /* The state surrounding mouse grabbing is potentially confusing. |
290 | * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated | |
291 | * pointing device an absolute-position one?"], but is only updated on | |
292 | * next refresh. | |
293 | * isMouseGrabbed tracks whether GUI events are directed to the guest; | |
294 | * it controls whether special keys like Cmd get sent to the guest, | |
295 | * and whether we capture the mouse when in non-absolute mode. | |
296 | * isMouseDeassociated tracks whether we've told MacOSX to disassociate | |
297 | * the mouse and mouse cursor position by calling | |
298 | * CGAssociateMouseAndMouseCursorPosition(FALSE) | |
299 | * (which basically happens if we grab in non-absolute mode). | |
300 | */ | |
49b9bd4d | 301 | - (BOOL) isMouseGrabbed; |
c304f7e2 | 302 | - (BOOL) isAbsoluteEnabled; |
f61c387e | 303 | - (BOOL) isMouseDeassociated; |
c304f7e2 TS |
304 | - (float) cdx; |
305 | - (float) cdy; | |
306 | - (QEMUScreen) gscreen; | |
307 | @end | |
3b46e624 | 308 | |
7fee199c AF |
309 | QemuCocoaView *cocoaView; |
310 | ||
c304f7e2 TS |
311 | @implementation QemuCocoaView |
312 | - (id)initWithFrame:(NSRect)frameRect | |
313 | { | |
314 | COCOA_DEBUG("QemuCocoaView: initWithFrame\n"); | |
3b46e624 | 315 | |
c304f7e2 TS |
316 | self = [super initWithFrame:frameRect]; |
317 | if (self) { | |
3b46e624 | 318 | |
c304f7e2 TS |
319 | screen.bitsPerComponent = 8; |
320 | screen.bitsPerPixel = 32; | |
321 | screen.width = frameRect.size.width; | |
322 | screen.height = frameRect.size.height; | |
3b46e624 | 323 | |
c304f7e2 TS |
324 | } |
325 | return self; | |
326 | } | |
3b46e624 | 327 | |
c304f7e2 TS |
328 | - (void) dealloc |
329 | { | |
330 | COCOA_DEBUG("QemuCocoaView: dealloc\n"); | |
3b46e624 | 331 | |
c304f7e2 TS |
332 | if (dataProviderRef) |
333 | CGDataProviderRelease(dataProviderRef); | |
3b46e624 | 334 | |
c304f7e2 TS |
335 | [super dealloc]; |
336 | } | |
3b46e624 | 337 | |
d50f71dc AF |
338 | - (BOOL) isOpaque |
339 | { | |
340 | return YES; | |
341 | } | |
342 | ||
5dd45bee PM |
343 | - (BOOL) screenContainsPoint:(NSPoint) p |
344 | { | |
345 | return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height); | |
346 | } | |
347 | ||
13aefd30 PM |
348 | - (void) hideCursor |
349 | { | |
350 | if (!cursor_hide) { | |
351 | return; | |
352 | } | |
353 | [NSCursor hide]; | |
354 | } | |
355 | ||
356 | - (void) unhideCursor | |
357 | { | |
358 | if (!cursor_hide) { | |
359 | return; | |
360 | } | |
361 | [NSCursor unhide]; | |
362 | } | |
363 | ||
c304f7e2 TS |
364 | - (void) drawRect:(NSRect) rect |
365 | { | |
366 | COCOA_DEBUG("QemuCocoaView: drawRect\n"); | |
367 | ||
c304f7e2 TS |
368 | // get CoreGraphic context |
369 | CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort]; | |
370 | CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone); | |
371 | CGContextSetShouldAntialias (viewContextRef, NO); | |
372 | ||
373 | // draw screen bitmap directly to Core Graphics context | |
7d270b1c PM |
374 | if (!dataProviderRef) { |
375 | // Draw request before any guest device has set up a framebuffer: | |
376 | // just draw an opaque black rectangle | |
377 | CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0); | |
378 | CGContextFillRect(viewContextRef, NSRectToCGRect(rect)); | |
379 | } else { | |
c304f7e2 TS |
380 | CGImageRef imageRef = CGImageCreate( |
381 | screen.width, //width | |
382 | screen.height, //height | |
383 | screen.bitsPerComponent, //bitsPerComponent | |
384 | screen.bitsPerPixel, //bitsPerPixel | |
9794f74f | 385 | (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow |
04afa4a8 | 386 | #ifdef __LITTLE_ENDIAN__ |
c304f7e2 | 387 | CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4 |
9794f74f | 388 | kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, |
c304f7e2 TS |
389 | #else |
390 | CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc) | |
391 | kCGImageAlphaNoneSkipFirst, //bitmapInfo | |
392 | #endif | |
393 | dataProviderRef, //provider | |
394 | NULL, //decode | |
395 | 0, //interpolate | |
396 | kCGRenderingIntentDefault //intent | |
397 | ); | |
b63901d8 PM |
398 | // selective drawing code (draws only dirty rectangles) (OS X >= 10.4) |
399 | const NSRect *rectList; | |
b63901d8 | 400 | NSInteger rectCount; |
b63901d8 PM |
401 | int i; |
402 | CGImageRef clipImageRef; | |
403 | CGRect clipRect; | |
404 | ||
405 | [self getRectsBeingDrawn:&rectList count:&rectCount]; | |
406 | for (i = 0; i < rectCount; i++) { | |
407 | clipRect.origin.x = rectList[i].origin.x / cdx; | |
408 | clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy; | |
409 | clipRect.size.width = rectList[i].size.width / cdx; | |
410 | clipRect.size.height = rectList[i].size.height / cdy; | |
411 | clipImageRef = CGImageCreateWithImageInRect( | |
412 | imageRef, | |
413 | clipRect | |
414 | ); | |
415 | CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef); | |
416 | CGImageRelease (clipImageRef); | |
5b0753e0 | 417 | } |
c304f7e2 TS |
418 | CGImageRelease (imageRef); |
419 | } | |
5b0753e0 FB |
420 | } |
421 | ||
c304f7e2 | 422 | - (void) setContentDimensions |
5b0753e0 | 423 | { |
c304f7e2 TS |
424 | COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); |
425 | ||
426 | if (isFullscreen) { | |
427 | cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width; | |
428 | cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height; | |
5d1b2eef JA |
429 | |
430 | /* stretches video, but keeps same aspect ratio */ | |
431 | if (stretch_video == true) { | |
432 | /* use smallest stretch value - prevents clipping on sides */ | |
433 | if (MIN(cdx, cdy) == cdx) { | |
434 | cdy = cdx; | |
435 | } else { | |
436 | cdx = cdy; | |
437 | } | |
438 | } else { /* No stretching */ | |
439 | cdx = cdy = 1; | |
440 | } | |
c304f7e2 TS |
441 | cw = screen.width * cdx; |
442 | ch = screen.height * cdy; | |
443 | cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; | |
444 | cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; | |
445 | } else { | |
446 | cx = 0; | |
447 | cy = 0; | |
448 | cw = screen.width; | |
449 | ch = screen.height; | |
450 | cdx = 1.0; | |
451 | cdy = 1.0; | |
452 | } | |
5b0753e0 FB |
453 | } |
454 | ||
5e00d3ac | 455 | - (void) switchSurface:(DisplaySurface *)surface |
5b0753e0 | 456 | { |
5e00d3ac | 457 | COCOA_DEBUG("QemuCocoaView: switchSurface\n"); |
c304f7e2 | 458 | |
8510d91e PM |
459 | int w = surface_width(surface); |
460 | int h = surface_height(surface); | |
381600da PM |
461 | /* cdx == 0 means this is our very first surface, in which case we need |
462 | * to recalculate the content dimensions even if it happens to be the size | |
463 | * of the initial empty window. | |
464 | */ | |
465 | bool isResize = (w != screen.width || h != screen.height || cdx == 0.0); | |
d3345a04 PM |
466 | |
467 | int oldh = screen.height; | |
468 | if (isResize) { | |
469 | // Resize before we trigger the redraw, or we'll redraw at the wrong size | |
470 | COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h); | |
471 | screen.width = w; | |
472 | screen.height = h; | |
473 | [self setContentDimensions]; | |
474 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; | |
475 | } | |
8510d91e | 476 | |
c304f7e2 TS |
477 | // update screenBuffer |
478 | if (dataProviderRef) | |
479 | CGDataProviderRelease(dataProviderRef); | |
3b46e624 | 480 | |
9794f74f | 481 | //sync host window color space with guests |
49060c29 PM |
482 | screen.bitsPerPixel = surface_bits_per_pixel(surface); |
483 | screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2; | |
9794f74f | 484 | |
5e00d3ac | 485 | dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL); |
3b46e624 | 486 | |
c304f7e2 TS |
487 | // update windows |
488 | if (isFullscreen) { | |
489 | [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]]; | |
d3345a04 | 490 | [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 |
491 | } else { |
492 | if (qemu_name) | |
493 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; | |
d3345a04 PM |
494 | [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:YES animate:NO]; |
495 | } | |
496 | ||
497 | if (isResize) { | |
498 | [normalWindow center]; | |
c304f7e2 | 499 | } |
5b0753e0 FB |
500 | } |
501 | ||
c304f7e2 TS |
502 | - (void) toggleFullScreen:(id)sender |
503 | { | |
504 | COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); | |
505 | ||
506 | if (isFullscreen) { // switch from fullscreen to desktop | |
507 | isFullscreen = FALSE; | |
508 | [self ungrabMouse]; | |
509 | [self setContentDimensions]; | |
c304f7e2 TS |
510 | if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime |
511 | [self exitFullScreenModeWithOptions:nil]; | |
512 | } else { | |
c304f7e2 TS |
513 | [fullScreenWindow close]; |
514 | [normalWindow setContentView: self]; | |
515 | [normalWindow makeKeyAndOrderFront: self]; | |
516 | [NSMenu setMenuBarVisible:YES]; | |
c304f7e2 | 517 | } |
c304f7e2 TS |
518 | } else { // switch from desktop to fullscreen |
519 | isFullscreen = TRUE; | |
5d1b2eef | 520 | [normalWindow orderOut: nil]; /* Hide the window */ |
c304f7e2 TS |
521 | [self grabMouse]; |
522 | [self setContentDimensions]; | |
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 { | |
c304f7e2 TS |
529 | [NSMenu setMenuBarVisible:NO]; |
530 | fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] | |
531 | styleMask:NSBorderlessWindowMask | |
532 | backing:NSBackingStoreBuffered | |
533 | defer:NO]; | |
5d1b2eef | 534 | [fullScreenWindow setAcceptsMouseMovedEvents: YES]; |
c304f7e2 | 535 | [fullScreenWindow setHasShadow:NO]; |
5d1b2eef JA |
536 | [fullScreenWindow setBackgroundColor: [NSColor blackColor]]; |
537 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; | |
538 | [[fullScreenWindow contentView] addSubview: self]; | |
c304f7e2 | 539 | [fullScreenWindow makeKeyAndOrderFront:self]; |
c304f7e2 | 540 | } |
c304f7e2 TS |
541 | } |
542 | } | |
5b0753e0 | 543 | |
c304f7e2 | 544 | - (void) handleEvent:(NSEvent *)event |
3b46e624 | 545 | { |
c304f7e2 TS |
546 | COCOA_DEBUG("QemuCocoaView: handleEvent\n"); |
547 | ||
548 | int buttons = 0; | |
549 | int keycode; | |
21bae11a | 550 | bool mouse_event = false; |
c304f7e2 TS |
551 | NSPoint p = [event locationInWindow]; |
552 | ||
553 | switch ([event type]) { | |
554 | case NSFlagsChanged: | |
555 | keycode = cocoa_keycode_to_qemu([event keyCode]); | |
8895919a | 556 | |
49b9bd4d | 557 | if ((keycode == 219 || keycode == 220) && !isMouseGrabbed) { |
8895919a PM |
558 | /* Don't pass command key changes to guest unless mouse is grabbed */ |
559 | keycode = 0; | |
560 | } | |
561 | ||
c304f7e2 TS |
562 | if (keycode) { |
563 | if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup | |
2e08c665 GH |
564 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
565 | qemu_input_event_send_key_number(dcl->con, keycode, false); | |
68c0aa6e | 566 | } else if (qemu_console_is_graphic(NULL)) { |
c304f7e2 | 567 | if (modifiers_state[keycode] == 0) { // keydown |
2e08c665 | 568 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
c304f7e2 TS |
569 | modifiers_state[keycode] = 1; |
570 | } else { // keyup | |
2e08c665 | 571 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
c304f7e2 TS |
572 | modifiers_state[keycode] = 0; |
573 | } | |
574 | } | |
575 | } | |
3b46e624 | 576 | |
c304f7e2 | 577 | // release Mouse grab when pressing ctrl+alt |
5d1b2eef | 578 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { |
c304f7e2 TS |
579 | [self ungrabMouse]; |
580 | } | |
581 | break; | |
582 | case NSKeyDown: | |
8895919a | 583 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
3b46e624 | 584 | |
8895919a | 585 | // forward command key combos to the host UI unless the mouse is grabbed |
49b9bd4d | 586 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
c304f7e2 TS |
587 | [NSApp sendEvent:event]; |
588 | return; | |
589 | } | |
3b46e624 | 590 | |
c304f7e2 | 591 | // default |
c304f7e2 TS |
592 | |
593 | // handle control + alt Key Combos (ctrl+alt is reserved for QEMU) | |
594 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { | |
595 | switch (keycode) { | |
596 | ||
597 | // enable graphic console | |
598 | case 0x02 ... 0x0a: // '1' to '9' keys | |
599 | console_select(keycode - 0x02); | |
600 | break; | |
601 | } | |
602 | ||
603 | // handle keys for graphic console | |
68c0aa6e | 604 | } else if (qemu_console_is_graphic(NULL)) { |
2e08c665 | 605 | qemu_input_event_send_key_number(dcl->con, keycode, true); |
c304f7e2 TS |
606 | |
607 | // handlekeys for Monitor | |
608 | } else { | |
609 | int keysym = 0; | |
610 | switch([event keyCode]) { | |
611 | case 115: | |
612 | keysym = QEMU_KEY_HOME; | |
613 | break; | |
614 | case 117: | |
615 | keysym = QEMU_KEY_DELETE; | |
616 | break; | |
617 | case 119: | |
618 | keysym = QEMU_KEY_END; | |
619 | break; | |
620 | case 123: | |
621 | keysym = QEMU_KEY_LEFT; | |
622 | break; | |
623 | case 124: | |
624 | keysym = QEMU_KEY_RIGHT; | |
625 | break; | |
626 | case 125: | |
627 | keysym = QEMU_KEY_DOWN; | |
628 | break; | |
629 | case 126: | |
630 | keysym = QEMU_KEY_UP; | |
631 | break; | |
632 | default: | |
633 | { | |
634 | NSString *ks = [event characters]; | |
635 | if ([ks length] > 0) | |
636 | keysym = [ks characterAtIndex:0]; | |
637 | } | |
638 | } | |
639 | if (keysym) | |
640 | kbd_put_keysym(keysym); | |
641 | } | |
642 | break; | |
643 | case NSKeyUp: | |
644 | keycode = cocoa_keycode_to_qemu([event keyCode]); | |
8895919a PM |
645 | |
646 | // don't pass the guest a spurious key-up if we treated this | |
647 | // command-key combo as a host UI action | |
49b9bd4d | 648 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
8895919a PM |
649 | return; |
650 | } | |
651 | ||
68c0aa6e | 652 | if (qemu_console_is_graphic(NULL)) { |
2e08c665 | 653 | qemu_input_event_send_key_number(dcl->con, keycode, false); |
c304f7e2 TS |
654 | } |
655 | break; | |
656 | case NSMouseMoved: | |
657 | if (isAbsoluteEnabled) { | |
5dd45bee | 658 | if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) { |
f61c387e PM |
659 | if (isMouseGrabbed) { |
660 | [self ungrabMouse]; | |
c304f7e2 TS |
661 | } |
662 | } else { | |
f61c387e PM |
663 | if (!isMouseGrabbed) { |
664 | [self grabMouse]; | |
c304f7e2 TS |
665 | } |
666 | } | |
5b0753e0 | 667 | } |
21bae11a | 668 | mouse_event = true; |
c304f7e2 TS |
669 | break; |
670 | case NSLeftMouseDown: | |
671 | if ([event modifierFlags] & NSCommandKeyMask) { | |
672 | buttons |= MOUSE_EVENT_RBUTTON; | |
673 | } else { | |
674 | buttons |= MOUSE_EVENT_LBUTTON; | |
675 | } | |
21bae11a | 676 | mouse_event = true; |
c304f7e2 TS |
677 | break; |
678 | case NSRightMouseDown: | |
679 | buttons |= MOUSE_EVENT_RBUTTON; | |
21bae11a | 680 | mouse_event = true; |
c304f7e2 TS |
681 | break; |
682 | case NSOtherMouseDown: | |
683 | buttons |= MOUSE_EVENT_MBUTTON; | |
21bae11a | 684 | mouse_event = true; |
c304f7e2 TS |
685 | break; |
686 | case NSLeftMouseDragged: | |
687 | if ([event modifierFlags] & NSCommandKeyMask) { | |
688 | buttons |= MOUSE_EVENT_RBUTTON; | |
689 | } else { | |
690 | buttons |= MOUSE_EVENT_LBUTTON; | |
691 | } | |
21bae11a | 692 | mouse_event = true; |
c304f7e2 TS |
693 | break; |
694 | case NSRightMouseDragged: | |
695 | buttons |= MOUSE_EVENT_RBUTTON; | |
21bae11a | 696 | mouse_event = true; |
c304f7e2 TS |
697 | break; |
698 | case NSOtherMouseDragged: | |
699 | buttons |= MOUSE_EVENT_MBUTTON; | |
21bae11a | 700 | mouse_event = true; |
c304f7e2 TS |
701 | break; |
702 | case NSLeftMouseUp: | |
f61c387e PM |
703 | mouse_event = true; |
704 | if (!isMouseGrabbed && [self screenContainsPoint:p]) { | |
705 | [self grabMouse]; | |
c304f7e2 TS |
706 | } |
707 | break; | |
708 | case NSRightMouseUp: | |
21bae11a | 709 | mouse_event = true; |
c304f7e2 TS |
710 | break; |
711 | case NSOtherMouseUp: | |
21bae11a | 712 | mouse_event = true; |
c304f7e2 TS |
713 | break; |
714 | case NSScrollWheel: | |
f61c387e | 715 | if (isMouseGrabbed) { |
21bae11a GH |
716 | buttons |= ([event deltaY] < 0) ? |
717 | MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN; | |
c304f7e2 | 718 | } |
f61c387e | 719 | mouse_event = true; |
c304f7e2 TS |
720 | break; |
721 | default: | |
722 | [NSApp sendEvent:event]; | |
5b0753e0 | 723 | } |
21bae11a GH |
724 | |
725 | if (mouse_event) { | |
726 | if (last_buttons != buttons) { | |
727 | static uint32_t bmap[INPUT_BUTTON_MAX] = { | |
728 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, | |
729 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, | |
730 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, | |
731 | [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP, | |
732 | [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN, | |
733 | }; | |
734 | qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons); | |
735 | last_buttons = buttons; | |
736 | } | |
f61c387e PM |
737 | if (isMouseGrabbed) { |
738 | if (isAbsoluteEnabled) { | |
739 | /* Note that the origin for Cocoa mouse coords is bottom left, not top left. | |
740 | * The check on screenContainsPoint is to avoid sending out of range values for | |
741 | * clicks in the titlebar. | |
742 | */ | |
743 | if ([self screenContainsPoint:p]) { | |
744 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width); | |
745 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height); | |
746 | } | |
747 | } else { | |
748 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]); | |
749 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]); | |
750 | } | |
21bae11a GH |
751 | } else { |
752 | [NSApp sendEvent:event]; | |
753 | } | |
754 | qemu_input_event_sync(); | |
755 | } | |
5b0753e0 FB |
756 | } |
757 | ||
c304f7e2 | 758 | - (void) grabMouse |
5b0753e0 | 759 | { |
c304f7e2 | 760 | COCOA_DEBUG("QemuCocoaView: grabMouse\n"); |
3b46e624 | 761 | |
c304f7e2 TS |
762 | if (!isFullscreen) { |
763 | if (qemu_name) | |
764 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]]; | |
765 | else | |
766 | [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"]; | |
767 | } | |
13aefd30 | 768 | [self hideCursor]; |
f61c387e PM |
769 | if (!isAbsoluteEnabled) { |
770 | isMouseDeassociated = TRUE; | |
771 | CGAssociateMouseAndMouseCursorPosition(FALSE); | |
772 | } | |
49b9bd4d | 773 | isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] |
5b0753e0 | 774 | } |
3b46e624 | 775 | |
c304f7e2 TS |
776 | - (void) ungrabMouse |
777 | { | |
778 | COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); | |
3b46e624 | 779 | |
c304f7e2 TS |
780 | if (!isFullscreen) { |
781 | if (qemu_name) | |
782 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; | |
783 | else | |
784 | [normalWindow setTitle:@"QEMU"]; | |
785 | } | |
13aefd30 | 786 | [self unhideCursor]; |
f61c387e PM |
787 | if (isMouseDeassociated) { |
788 | CGAssociateMouseAndMouseCursorPosition(TRUE); | |
789 | isMouseDeassociated = FALSE; | |
790 | } | |
49b9bd4d | 791 | isMouseGrabbed = FALSE; |
5b0753e0 FB |
792 | } |
793 | ||
c304f7e2 | 794 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;} |
49b9bd4d | 795 | - (BOOL) isMouseGrabbed {return isMouseGrabbed;} |
c304f7e2 | 796 | - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} |
f61c387e | 797 | - (BOOL) isMouseDeassociated {return isMouseDeassociated;} |
c304f7e2 TS |
798 | - (float) cdx {return cdx;} |
799 | - (float) cdy {return cdy;} | |
800 | - (QEMUScreen) gscreen {return screen;} | |
5b0753e0 FB |
801 | @end |
802 | ||
803 | ||
c304f7e2 | 804 | |
5b0753e0 FB |
805 | /* |
806 | ------------------------------------------------------ | |
c304f7e2 | 807 | QemuCocoaAppController |
5b0753e0 FB |
808 | ------------------------------------------------------ |
809 | */ | |
c304f7e2 | 810 | @interface QemuCocoaAppController : NSObject |
2a4c8c53 PM |
811 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) |
812 | <NSApplicationDelegate> | |
813 | #endif | |
5b0753e0 FB |
814 | { |
815 | } | |
5b0753e0 | 816 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv; |
de1aadee | 817 | - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)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; |
b4c6a112 | 823 | - (void)displayConsole:(id)sender; |
8524f1c7 JA |
824 | - (void)pauseQEMU:(id)sender; |
825 | - (void)resumeQEMU:(id)sender; | |
826 | - (void)displayPause; | |
827 | - (void)removePause; | |
27074614 JA |
828 | - (void)restartQEMU:(id)sender; |
829 | - (void)powerDownQEMU:(id)sender; | |
693a3e01 JA |
830 | - (void)ejectDeviceMedia:(id)sender; |
831 | - (void)changeDeviceMedia:(id)sender; | |
5b0753e0 FB |
832 | @end |
833 | ||
c304f7e2 TS |
834 | @implementation QemuCocoaAppController |
835 | - (id) init | |
5b0753e0 | 836 | { |
c304f7e2 | 837 | COCOA_DEBUG("QemuCocoaAppController: init\n"); |
5a246934 | 838 | |
c304f7e2 TS |
839 | self = [super init]; |
840 | if (self) { | |
5a246934 | 841 | |
c304f7e2 TS |
842 | // create a view and add it to the window |
843 | cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)]; | |
844 | if(!cocoaView) { | |
845 | fprintf(stderr, "(cocoa) can't create a view\n"); | |
846 | exit(1); | |
847 | } | |
3b46e624 | 848 | |
c304f7e2 TS |
849 | // create a window |
850 | normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame] | |
851 | styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask | |
852 | backing:NSBackingStoreBuffered defer:NO]; | |
853 | if(!normalWindow) { | |
854 | fprintf(stderr, "(cocoa) can't create window\n"); | |
855 | exit(1); | |
856 | } | |
857 | [normalWindow setAcceptsMouseMovedEvents:YES]; | |
858 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]]; | |
859 | [normalWindow setContentView:cocoaView]; | |
81801ae2 | 860 | #if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10) |
561ef251 | 861 | [normalWindow useOptimizedDrawing:YES]; |
81801ae2 | 862 | #endif |
c304f7e2 | 863 | [normalWindow makeKeyAndOrderFront:self]; |
49060c29 | 864 | [normalWindow center]; |
5d1b2eef | 865 | stretch_video = false; |
8524f1c7 JA |
866 | |
867 | /* Used for displaying pause on the screen */ | |
868 | pauseLabel = [NSTextField new]; | |
869 | [pauseLabel setBezeled:YES]; | |
870 | [pauseLabel setDrawsBackground:YES]; | |
871 | [pauseLabel setBackgroundColor: [NSColor whiteColor]]; | |
872 | [pauseLabel setEditable:NO]; | |
873 | [pauseLabel setSelectable:NO]; | |
874 | [pauseLabel setStringValue: @"Paused"]; | |
875 | [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]]; | |
876 | [pauseLabel setTextColor: [NSColor blackColor]]; | |
877 | [pauseLabel sizeToFit]; | |
693a3e01 JA |
878 | |
879 | // set the supported image file types that can be opened | |
880 | supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg", | |
881 | @"qcow", @"qcow2", @"cloop", @"vmdk", nil]; | |
c304f7e2 TS |
882 | } |
883 | return self; | |
884 | } | |
3b46e624 | 885 | |
c304f7e2 TS |
886 | - (void) dealloc |
887 | { | |
888 | COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); | |
889 | ||
890 | if (cocoaView) | |
891 | [cocoaView release]; | |
892 | [super dealloc]; | |
893 | } | |
3b46e624 | 894 | |
c304f7e2 TS |
895 | - (void)applicationDidFinishLaunching: (NSNotification *) note |
896 | { | |
897 | COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); | |
898 | ||
49b9bd4d | 899 | // Display an open dialog box if no arguments were passed or |
c304f7e2 TS |
900 | // if qemu was launched from the finder ( the Finder passes "-psn" ) |
901 | if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) { | |
902 | NSOpenPanel *op = [[NSOpenPanel alloc] init]; | |
903 | [op setPrompt:@"Boot image"]; | |
904 | [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"]; | |
2ba9de6e | 905 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) |
693a3e01 | 906 | [op setAllowedFileTypes:supportedImageFileTypes]; |
2ba9de6e PM |
907 | [op beginSheetModalForWindow:normalWindow |
908 | completionHandler:^(NSInteger returnCode) | |
909 | { [self openPanelDidEnd:op | |
910 | returnCode:returnCode contextInfo:NULL ]; } ]; | |
911 | #else | |
912 | // Compatibility code for pre-10.6, using deprecated method | |
913 | [op beginSheetForDirectory:nil file:nil types:filetypes | |
c304f7e2 | 914 | modalForWindow:normalWindow modalDelegate:self |
5b0753e0 | 915 | didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL]; |
2ba9de6e | 916 | #endif |
c304f7e2 | 917 | } else { |
5cbdb3a3 | 918 | // or launch QEMU, with the global args |
c304f7e2 | 919 | [self startEmulationWithArgc:gArgc argv:(char **)gArgv]; |
5a246934 | 920 | } |
5b0753e0 FB |
921 | } |
922 | ||
923 | - (void)applicationWillTerminate:(NSNotification *)aNotification | |
924 | { | |
c304f7e2 TS |
925 | COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n"); |
926 | ||
5b0753e0 | 927 | qemu_system_shutdown_request(); |
5b0753e0 FB |
928 | exit(0); |
929 | } | |
930 | ||
41ea49b3 AF |
931 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication |
932 | { | |
933 | return YES; | |
934 | } | |
935 | ||
c304f7e2 TS |
936 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv |
937 | { | |
938 | COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n"); | |
939 | ||
940 | int status; | |
3bbbee18 | 941 | status = qemu_main(argc, argv, *_NSGetEnviron()); |
c304f7e2 TS |
942 | exit(status); |
943 | } | |
944 | ||
de1aadee | 945 | - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo |
5b0753e0 | 946 | { |
c304f7e2 | 947 | COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n"); |
3b46e624 | 948 | |
8617989e PM |
949 | /* The NSFileHandlingPanelOKButton/NSFileHandlingPanelCancelButton values for |
950 | * returnCode strictly only apply for the 10.6-and-up beginSheetModalForWindow | |
951 | * API. For the legacy pre-10.6 beginSheetForDirectory API they are NSOKButton | |
952 | * and NSCancelButton. However conveniently the values are the same. | |
953 | * We use the non-legacy names because the others are deprecated in OSX 10.10. | |
954 | */ | |
955 | if (returnCode == NSFileHandlingPanelCancelButton) { | |
c304f7e2 | 956 | exit(0); |
8617989e | 957 | } else if (returnCode == NSFileHandlingPanelOKButton) { |
8bb3f1e3 | 958 | char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding]; |
3b46e624 | 959 | |
98db429d | 960 | char **argv = g_new(char *, 4); |
3b46e624 | 961 | |
13766eb1 AF |
962 | [sheet close]; |
963 | ||
98db429d PM |
964 | argv[0] = g_strdup(gArgv[0]); |
965 | argv[1] = g_strdup("-hda"); | |
966 | argv[2] = g_strdup(img); | |
967 | argv[3] = NULL; | |
3b46e624 | 968 | |
98db429d | 969 | // printf("Using argc %d argv %s -hda %s\n", 3, gArgv[0], img); |
3b46e624 | 970 | |
5b0753e0 FB |
971 | [self startEmulationWithArgc:3 argv:(char**)argv]; |
972 | } | |
973 | } | |
5d1b2eef JA |
974 | |
975 | /* We abstract the method called by the Enter Fullscreen menu item | |
976 | * because Mac OS 10.7 and higher disables it. This is because of the | |
977 | * menu item's old selector's name toggleFullScreen: | |
978 | */ | |
979 | - (void) doToggleFullScreen:(id)sender | |
980 | { | |
981 | [self toggleFullScreen:(id)sender]; | |
982 | } | |
983 | ||
c304f7e2 TS |
984 | - (void)toggleFullScreen:(id)sender |
985 | { | |
986 | COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); | |
987 | ||
988 | [cocoaView toggleFullScreen:sender]; | |
989 | } | |
5b0753e0 | 990 | |
c304f7e2 | 991 | - (void)showQEMUDoc:(id)sender |
5b0753e0 | 992 | { |
c304f7e2 TS |
993 | COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n"); |
994 | ||
995 | [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html", | |
996 | [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; | |
997 | } | |
998 | ||
999 | - (void)showQEMUTec:(id)sender | |
1000 | { | |
1001 | COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n"); | |
1002 | ||
1003 | [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html", | |
1004 | [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"]; | |
5b0753e0 | 1005 | } |
5d1b2eef JA |
1006 | |
1007 | /* Stretches video to fit host monitor size */ | |
1008 | - (void)zoomToFit:(id) sender | |
1009 | { | |
1010 | stretch_video = !stretch_video; | |
1011 | if (stretch_video == true) { | |
1012 | [sender setState: NSOnState]; | |
1013 | } else { | |
1014 | [sender setState: NSOffState]; | |
1015 | } | |
1016 | } | |
5b0753e0 | 1017 | |
b4c6a112 JA |
1018 | /* Displays the console on the screen */ |
1019 | - (void)displayConsole:(id)sender | |
1020 | { | |
1021 | console_select([sender tag]); | |
1022 | } | |
8524f1c7 JA |
1023 | |
1024 | /* Pause the guest */ | |
1025 | - (void)pauseQEMU:(id)sender | |
1026 | { | |
1027 | qmp_stop(NULL); | |
1028 | [sender setEnabled: NO]; | |
1029 | [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES]; | |
1030 | [self displayPause]; | |
1031 | } | |
1032 | ||
1033 | /* Resume running the guest operating system */ | |
1034 | - (void)resumeQEMU:(id) sender | |
1035 | { | |
1036 | qmp_cont(NULL); | |
1037 | [sender setEnabled: NO]; | |
1038 | [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES]; | |
1039 | [self removePause]; | |
1040 | } | |
1041 | ||
1042 | /* Displays the word pause on the screen */ | |
1043 | - (void)displayPause | |
1044 | { | |
1045 | /* Coordinates have to be calculated each time because the window can change its size */ | |
1046 | int xCoord, yCoord, width, height; | |
1047 | xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2; | |
1048 | yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5); | |
1049 | width = [pauseLabel frame].size.width; | |
1050 | height = [pauseLabel frame].size.height; | |
1051 | [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)]; | |
1052 | [cocoaView addSubview: pauseLabel]; | |
1053 | } | |
1054 | ||
1055 | /* Removes the word pause from the screen */ | |
1056 | - (void)removePause | |
1057 | { | |
1058 | [pauseLabel removeFromSuperview]; | |
1059 | } | |
1060 | ||
27074614 JA |
1061 | /* Restarts QEMU */ |
1062 | - (void)restartQEMU:(id)sender | |
1063 | { | |
1064 | qmp_system_reset(NULL); | |
1065 | } | |
1066 | ||
1067 | /* Powers down QEMU */ | |
1068 | - (void)powerDownQEMU:(id)sender | |
1069 | { | |
1070 | qmp_system_powerdown(NULL); | |
1071 | } | |
1072 | ||
693a3e01 JA |
1073 | /* Ejects the media. |
1074 | * Uses sender's tag to figure out the device to eject. | |
1075 | */ | |
1076 | - (void)ejectDeviceMedia:(id)sender | |
1077 | { | |
1078 | NSString * drive; | |
1079 | drive = [sender representedObject]; | |
1080 | if(drive == nil) { | |
1081 | NSBeep(); | |
1082 | QEMU_Alert(@"Failed to find drive to eject!"); | |
1083 | return; | |
1084 | } | |
1085 | ||
1086 | Error *err = NULL; | |
1087 | qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding], false, false, &err); | |
1088 | handleAnyDeviceErrors(err); | |
1089 | } | |
1090 | ||
1091 | /* Displays a dialog box asking the user to select an image file to load. | |
1092 | * Uses sender's represented object value to figure out which drive to use. | |
1093 | */ | |
1094 | - (void)changeDeviceMedia:(id)sender | |
1095 | { | |
1096 | /* Find the drive name */ | |
1097 | NSString * drive; | |
1098 | drive = [sender representedObject]; | |
1099 | if(drive == nil) { | |
1100 | NSBeep(); | |
1101 | QEMU_Alert(@"Could not find drive!"); | |
1102 | return; | |
1103 | } | |
1104 | ||
1105 | /* Display the file open dialog */ | |
1106 | NSOpenPanel * openPanel; | |
1107 | openPanel = [NSOpenPanel openPanel]; | |
1108 | [openPanel setCanChooseFiles: YES]; | |
1109 | [openPanel setAllowsMultipleSelection: NO]; | |
1110 | [openPanel setAllowedFileTypes: supportedImageFileTypes]; | |
1111 | if([openPanel runModal] == NSFileHandlingPanelOKButton) { | |
1112 | NSString * file = [[[openPanel URLs] objectAtIndex: 0] path]; | |
1113 | if(file == nil) { | |
1114 | NSBeep(); | |
1115 | QEMU_Alert(@"Failed to convert URL to file path!"); | |
1116 | return; | |
1117 | } | |
1118 | ||
1119 | Error *err = NULL; | |
1120 | qmp_change_blockdev([drive cStringUsingEncoding: NSASCIIStringEncoding], | |
1121 | [file cStringUsingEncoding: NSASCIIStringEncoding], | |
1122 | "raw", | |
1123 | &err); | |
1124 | handleAnyDeviceErrors(err); | |
1125 | } | |
1126 | } | |
1127 | ||
b4c6a112 | 1128 | @end |
5b0753e0 | 1129 | |
c304f7e2 | 1130 | |
c304f7e2 | 1131 | int main (int argc, const char * argv[]) { |
5b0753e0 | 1132 | |
c304f7e2 TS |
1133 | gArgc = argc; |
1134 | gArgv = (char **)argv; | |
f4918804 AF |
1135 | int i; |
1136 | ||
1137 | /* In case we don't need to display a window, let's not do that */ | |
1138 | for (i = 1; i < argc; i++) { | |
e4ebcc1a TG |
1139 | const char *opt = argv[i]; |
1140 | ||
1141 | if (opt[0] == '-') { | |
1142 | /* Treat --foo the same as -foo. */ | |
1143 | if (opt[1] == '-') { | |
1144 | opt++; | |
1145 | } | |
9851484f AR |
1146 | if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || |
1147 | !strcmp(opt, "-vnc") || | |
e4ebcc1a TG |
1148 | !strcmp(opt, "-nographic") || |
1149 | !strcmp(opt, "-version") || | |
60b46aa2 | 1150 | !strcmp(opt, "-curses") || |
b12a84ce | 1151 | !strcmp(opt, "-display") || |
60b46aa2 | 1152 | !strcmp(opt, "-qtest")) { |
3bbbee18 | 1153 | return qemu_main(gArgc, gArgv, *_NSGetEnviron()); |
e4ebcc1a | 1154 | } |
f4918804 AF |
1155 | } |
1156 | } | |
5b0753e0 | 1157 | |
c304f7e2 | 1158 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
5b0753e0 | 1159 | |
42a5dfe7 PM |
1160 | // Pull this console process up to being a fully-fledged graphical |
1161 | // app with a menubar and Dock icon | |
1162 | ProcessSerialNumber psn = { 0, kCurrentProcess }; | |
1163 | TransformProcessType(&psn, kProcessTransformToForegroundApplication); | |
1164 | ||
1165 | [NSApplication sharedApplication]; | |
5b0753e0 | 1166 | |
c304f7e2 TS |
1167 | // Add menus |
1168 | NSMenu *menu; | |
1169 | NSMenuItem *menuItem; | |
5b0753e0 | 1170 | |
c304f7e2 | 1171 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
5b0753e0 | 1172 | |
c304f7e2 TS |
1173 | // Application menu |
1174 | menu = [[NSMenu alloc] initWithTitle:@""]; | |
1175 | [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU | |
1176 | [menu addItem:[NSMenuItem separatorItem]]; //Separator | |
1177 | [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU | |
1178 | menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others | |
1179 | [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; | |
1180 | [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All | |
1181 | [menu addItem:[NSMenuItem separatorItem]]; //Separator | |
1182 | [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"]; | |
1183 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; | |
1184 | [menuItem setSubmenu:menu]; | |
1185 | [[NSApp mainMenu] addItem:menuItem]; | |
1186 | [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+) | |
3b46e624 | 1187 | |
8524f1c7 JA |
1188 | // Machine menu |
1189 | menu = [[NSMenu alloc] initWithTitle: @"Machine"]; | |
1190 | [menu setAutoenablesItems: NO]; | |
1191 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]]; | |
1192 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease]; | |
1193 | [menu addItem: menuItem]; | |
1194 | [menuItem setEnabled: NO]; | |
27074614 JA |
1195 | [menu addItem: [NSMenuItem separatorItem]]; |
1196 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]]; | |
1197 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]]; | |
8524f1c7 JA |
1198 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease]; |
1199 | [menuItem setSubmenu:menu]; | |
1200 | [[NSApp mainMenu] addItem:menuItem]; | |
1201 | ||
c304f7e2 TS |
1202 | // View menu |
1203 | menu = [[NSMenu alloc] initWithTitle:@"View"]; | |
5d1b2eef JA |
1204 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen |
1205 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]]; | |
c304f7e2 TS |
1206 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease]; |
1207 | [menuItem setSubmenu:menu]; | |
5b0753e0 FB |
1208 | [[NSApp mainMenu] addItem:menuItem]; |
1209 | ||
c304f7e2 TS |
1210 | // Window menu |
1211 | menu = [[NSMenu alloc] initWithTitle:@"Window"]; | |
1212 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize | |
1213 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; | |
1214 | [menuItem setSubmenu:menu]; | |
1215 | [[NSApp mainMenu] addItem:menuItem]; | |
1216 | [NSApp setWindowsMenu:menu]; | |
1217 | ||
1218 | // Help menu | |
1219 | menu = [[NSMenu alloc] initWithTitle:@"Help"]; | |
1220 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help | |
1221 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help | |
1222 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; | |
1223 | [menuItem setSubmenu:menu]; | |
1224 | [[NSApp mainMenu] addItem:menuItem]; | |
5b0753e0 | 1225 | |
c304f7e2 TS |
1226 | // Create an Application controller |
1227 | QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init]; | |
1228 | [NSApp setDelegate:appController]; | |
5b0753e0 | 1229 | |
c304f7e2 TS |
1230 | // Start the main event loop |
1231 | [NSApp run]; | |
5b0753e0 | 1232 | |
c304f7e2 TS |
1233 | [appController release]; |
1234 | [pool release]; | |
3b46e624 | 1235 | |
c304f7e2 TS |
1236 | return 0; |
1237 | } | |
3b46e624 | 1238 | |
3b46e624 | 1239 | |
5b0753e0 | 1240 | |
c304f7e2 | 1241 | #pragma mark qemu |
7c20b4a3 | 1242 | static void cocoa_update(DisplayChangeListener *dcl, |
7c20b4a3 | 1243 | int x, int y, int w, int h) |
c304f7e2 | 1244 | { |
6e657e64 PM |
1245 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
1246 | ||
c304f7e2 TS |
1247 | COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); |
1248 | ||
1249 | NSRect rect; | |
1250 | if ([cocoaView cdx] == 1.0) { | |
1251 | rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); | |
1252 | } else { | |
1253 | rect = NSMakeRect( | |
1254 | x * [cocoaView cdx], | |
1255 | ([cocoaView gscreen].height - y - h) * [cocoaView cdy], | |
1256 | w * [cocoaView cdx], | |
1257 | h * [cocoaView cdy]); | |
1258 | } | |
17ccbc27 | 1259 | [cocoaView setNeedsDisplayInRect:rect]; |
6e657e64 PM |
1260 | |
1261 | [pool release]; | |
5b0753e0 FB |
1262 | } |
1263 | ||
c12aeb86 | 1264 | static void cocoa_switch(DisplayChangeListener *dcl, |
c12aeb86 | 1265 | DisplaySurface *surface) |
5b0753e0 | 1266 | { |
6e657e64 | 1267 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
3b46e624 | 1268 | |
6e657e64 | 1269 | COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); |
5e00d3ac | 1270 | [cocoaView switchSurface:surface]; |
6e657e64 | 1271 | [pool release]; |
c304f7e2 | 1272 | } |
3b46e624 | 1273 | |
bc2ed970 | 1274 | static void cocoa_refresh(DisplayChangeListener *dcl) |
c304f7e2 | 1275 | { |
6e657e64 PM |
1276 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
1277 | ||
c304f7e2 | 1278 | COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n"); |
3b46e624 | 1279 | |
21bae11a | 1280 | if (qemu_input_is_absolute()) { |
c304f7e2 | 1281 | if (![cocoaView isAbsoluteEnabled]) { |
49b9bd4d | 1282 | if ([cocoaView isMouseGrabbed]) { |
c304f7e2 TS |
1283 | [cocoaView ungrabMouse]; |
1284 | } | |
1285 | } | |
1286 | [cocoaView setAbsoluteEnabled:YES]; | |
1287 | } | |
5b0753e0 | 1288 | |
c304f7e2 TS |
1289 | NSDate *distantPast; |
1290 | NSEvent *event; | |
1291 | distantPast = [NSDate distantPast]; | |
1292 | do { | |
1293 | event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast | |
1294 | inMode: NSDefaultRunLoopMode dequeue:YES]; | |
1295 | if (event != nil) { | |
1296 | [cocoaView handleEvent:event]; | |
1297 | } | |
1298 | } while(event != nil); | |
68c0aa6e | 1299 | graphic_hw_update(NULL); |
6e657e64 | 1300 | [pool release]; |
c304f7e2 | 1301 | } |
3b46e624 | 1302 | |
c304f7e2 TS |
1303 | static void cocoa_cleanup(void) |
1304 | { | |
1305 | COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n"); | |
58a06675 | 1306 | g_free(dcl); |
5b0753e0 FB |
1307 | } |
1308 | ||
7c20b4a3 GH |
1309 | static const DisplayChangeListenerOps dcl_ops = { |
1310 | .dpy_name = "cocoa", | |
8510d91e PM |
1311 | .dpy_gfx_update = cocoa_update, |
1312 | .dpy_gfx_switch = cocoa_switch, | |
1313 | .dpy_refresh = cocoa_refresh, | |
7c20b4a3 GH |
1314 | }; |
1315 | ||
b4c6a112 JA |
1316 | /* Returns a name for a given console */ |
1317 | static NSString * getConsoleName(QemuConsole * console) | |
1318 | { | |
1319 | return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)]; | |
1320 | } | |
1321 | ||
1322 | /* Add an entry to the View menu for each console */ | |
1323 | static void add_console_menu_entries(void) | |
1324 | { | |
1325 | NSMenu *menu; | |
1326 | NSMenuItem *menuItem; | |
1327 | int index = 0; | |
1328 | ||
1329 | menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu]; | |
1330 | ||
1331 | [menu addItem:[NSMenuItem separatorItem]]; | |
1332 | ||
1333 | while (qemu_console_lookup_by_index(index) != NULL) { | |
1334 | menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index)) | |
1335 | action: @selector(displayConsole:) keyEquivalent: @""] autorelease]; | |
1336 | [menuItem setTag: index]; | |
1337 | [menu addItem: menuItem]; | |
1338 | index++; | |
1339 | } | |
1340 | } | |
1341 | ||
693a3e01 JA |
1342 | /* Make menu items for all removable devices. |
1343 | * Each device is given an 'Eject' and 'Change' menu item. | |
1344 | */ | |
1345 | static void addRemovableDevicesMenuItems() | |
1346 | { | |
1347 | NSMenu *menu; | |
1348 | NSMenuItem *menuItem; | |
1349 | BlockInfoList *currentDevice, *pointerToFree; | |
1350 | NSString *deviceName; | |
1351 | ||
1352 | currentDevice = qmp_query_block(NULL); | |
1353 | pointerToFree = currentDevice; | |
1354 | if(currentDevice == NULL) { | |
1355 | NSBeep(); | |
1356 | QEMU_Alert(@"Failed to query for block devices!"); | |
1357 | return; | |
1358 | } | |
1359 | ||
1360 | menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; | |
1361 | ||
1362 | // Add a separator between related groups of menu items | |
1363 | [menu addItem:[NSMenuItem separatorItem]]; | |
1364 | ||
1365 | // Set the attributes to the "Removable Media" menu item | |
1366 | NSString *titleString = @"Removable Media"; | |
1367 | NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString]; | |
1368 | NSColor *newColor = [NSColor blackColor]; | |
1369 | NSFontManager *fontManager = [NSFontManager sharedFontManager]; | |
1370 | NSFont *font = [fontManager fontWithFamily:@"Helvetica" | |
1371 | traits:NSBoldFontMask|NSItalicFontMask | |
1372 | weight:0 | |
1373 | size:14]; | |
1374 | [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])]; | |
1375 | [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])]; | |
1376 | [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])]; | |
1377 | ||
1378 | // Add the "Removable Media" menu item | |
1379 | menuItem = [NSMenuItem new]; | |
1380 | [menuItem setAttributedTitle: attString]; | |
1381 | [menuItem setEnabled: NO]; | |
1382 | [menu addItem: menuItem]; | |
1383 | ||
1384 | /* Loop thru all the block devices in the emulator */ | |
1385 | while (currentDevice) { | |
1386 | deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain]; | |
1387 | ||
1388 | if(currentDevice->value->removable) { | |
1389 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device] | |
1390 | action: @selector(changeDeviceMedia:) | |
1391 | keyEquivalent: @""]; | |
1392 | [menu addItem: menuItem]; | |
1393 | [menuItem setRepresentedObject: deviceName]; | |
1394 | [menuItem autorelease]; | |
1395 | ||
1396 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device] | |
1397 | action: @selector(ejectDeviceMedia:) | |
1398 | keyEquivalent: @""]; | |
1399 | [menu addItem: menuItem]; | |
1400 | [menuItem setRepresentedObject: deviceName]; | |
1401 | [menuItem autorelease]; | |
1402 | } | |
1403 | currentDevice = currentDevice->next; | |
1404 | } | |
1405 | qapi_free_BlockInfoList(pointerToFree); | |
1406 | } | |
1407 | ||
c304f7e2 | 1408 | void cocoa_display_init(DisplayState *ds, int full_screen) |
5b0753e0 | 1409 | { |
c304f7e2 TS |
1410 | COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); |
1411 | ||
43227af8 JA |
1412 | /* if fullscreen mode is to be used */ |
1413 | if (full_screen == true) { | |
1414 | [NSApp activateIgnoringOtherApps: YES]; | |
1415 | [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil]; | |
1416 | } | |
1417 | ||
58a06675 BS |
1418 | dcl = g_malloc0(sizeof(DisplayChangeListener)); |
1419 | ||
9794f74f | 1420 | // register vga output callbacks |
7c20b4a3 | 1421 | dcl->ops = &dcl_ops; |
5209089f | 1422 | register_displaychangelistener(dcl); |
cae41b10 | 1423 | |
c304f7e2 TS |
1424 | // register cleanup function |
1425 | atexit(cocoa_cleanup); | |
b4c6a112 JA |
1426 | |
1427 | /* At this point QEMU has created all the consoles, so we can add View | |
1428 | * menu entries for them. | |
1429 | */ | |
1430 | add_console_menu_entries(); | |
693a3e01 JA |
1431 | |
1432 | /* Give all removable devices a menu item. | |
1433 | * Has to be called after QEMU has started to | |
1434 | * find out what removable devices it has. | |
1435 | */ | |
1436 | addRemovableDevicesMenuItems(); | |
5b0753e0 | 1437 | } |