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