]>
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" |
aaac714f | 36 | #include <Carbon/Carbon.h> |
da4dbf74 | 37 | |
44e4c0ba AF |
38 | #ifndef MAC_OS_X_VERSION_10_5 |
39 | #define MAC_OS_X_VERSION_10_5 1050 | |
40 | #endif | |
2ba9de6e PM |
41 | #ifndef MAC_OS_X_VERSION_10_6 |
42 | #define MAC_OS_X_VERSION_10_6 1060 | |
43 | #endif | |
81801ae2 PM |
44 | #ifndef MAC_OS_X_VERSION_10_10 |
45 | #define MAC_OS_X_VERSION_10_10 101000 | |
46 | #endif | |
44e4c0ba | 47 | |
3b46e624 | 48 | |
c304f7e2 | 49 | //#define DEBUG |
3b46e624 | 50 | |
c304f7e2 TS |
51 | #ifdef DEBUG |
52 | #define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); } | |
b29169d2 | 53 | #else |
c304f7e2 | 54 | #define COCOA_DEBUG(...) ((void) 0) |
b29169d2 BS |
55 | #endif |
56 | ||
c304f7e2 | 57 | #define cgrect(nsrect) (*(CGRect *)&(nsrect)) |
5b0753e0 | 58 | |
c304f7e2 TS |
59 | typedef struct { |
60 | int width; | |
61 | int height; | |
62 | int bitsPerComponent; | |
63 | int bitsPerPixel; | |
64 | } QEMUScreen; | |
65 | ||
c304f7e2 | 66 | NSWindow *normalWindow; |
9794f74f | 67 | static DisplayChangeListener *dcl; |
21bae11a | 68 | static int last_buttons; |
c304f7e2 TS |
69 | |
70 | int gArgc; | |
71 | char **gArgv; | |
5d1b2eef | 72 | bool stretch_video; |
8524f1c7 | 73 | NSTextField *pauseLabel; |
693a3e01 | 74 | NSArray * supportedImageFileTypes; |
5b0753e0 | 75 | |
aaac714f JA |
76 | // Mac to QKeyCode conversion |
77 | const int mac_to_qkeycode_map[] = { | |
78 | [kVK_ANSI_A] = Q_KEY_CODE_A, | |
79 | [kVK_ANSI_B] = Q_KEY_CODE_B, | |
80 | [kVK_ANSI_C] = Q_KEY_CODE_C, | |
81 | [kVK_ANSI_D] = Q_KEY_CODE_D, | |
82 | [kVK_ANSI_E] = Q_KEY_CODE_E, | |
83 | [kVK_ANSI_F] = Q_KEY_CODE_F, | |
84 | [kVK_ANSI_G] = Q_KEY_CODE_G, | |
85 | [kVK_ANSI_H] = Q_KEY_CODE_H, | |
86 | [kVK_ANSI_I] = Q_KEY_CODE_I, | |
87 | [kVK_ANSI_J] = Q_KEY_CODE_J, | |
88 | [kVK_ANSI_K] = Q_KEY_CODE_K, | |
89 | [kVK_ANSI_L] = Q_KEY_CODE_L, | |
90 | [kVK_ANSI_M] = Q_KEY_CODE_M, | |
91 | [kVK_ANSI_N] = Q_KEY_CODE_N, | |
92 | [kVK_ANSI_O] = Q_KEY_CODE_O, | |
93 | [kVK_ANSI_P] = Q_KEY_CODE_P, | |
94 | [kVK_ANSI_Q] = Q_KEY_CODE_Q, | |
95 | [kVK_ANSI_R] = Q_KEY_CODE_R, | |
96 | [kVK_ANSI_S] = Q_KEY_CODE_S, | |
97 | [kVK_ANSI_T] = Q_KEY_CODE_T, | |
98 | [kVK_ANSI_U] = Q_KEY_CODE_U, | |
99 | [kVK_ANSI_V] = Q_KEY_CODE_V, | |
100 | [kVK_ANSI_W] = Q_KEY_CODE_W, | |
101 | [kVK_ANSI_X] = Q_KEY_CODE_X, | |
102 | [kVK_ANSI_Y] = Q_KEY_CODE_Y, | |
103 | [kVK_ANSI_Z] = Q_KEY_CODE_Z, | |
104 | ||
105 | [kVK_ANSI_0] = Q_KEY_CODE_0, | |
106 | [kVK_ANSI_1] = Q_KEY_CODE_1, | |
107 | [kVK_ANSI_2] = Q_KEY_CODE_2, | |
108 | [kVK_ANSI_3] = Q_KEY_CODE_3, | |
109 | [kVK_ANSI_4] = Q_KEY_CODE_4, | |
110 | [kVK_ANSI_5] = Q_KEY_CODE_5, | |
111 | [kVK_ANSI_6] = Q_KEY_CODE_6, | |
112 | [kVK_ANSI_7] = Q_KEY_CODE_7, | |
113 | [kVK_ANSI_8] = Q_KEY_CODE_8, | |
114 | [kVK_ANSI_9] = Q_KEY_CODE_9, | |
115 | ||
116 | [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT, | |
117 | [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS, | |
118 | [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL, | |
119 | [kVK_Delete] = Q_KEY_CODE_BACKSPACE, | |
120 | [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK, | |
121 | [kVK_Tab] = Q_KEY_CODE_TAB, | |
122 | [kVK_Return] = Q_KEY_CODE_RET, | |
123 | [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT, | |
124 | [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT, | |
125 | [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH, | |
126 | [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON, | |
127 | [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE, | |
128 | [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA, | |
129 | [kVK_ANSI_Period] = Q_KEY_CODE_DOT, | |
130 | [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH, | |
131 | [kVK_Shift] = Q_KEY_CODE_SHIFT, | |
132 | [kVK_RightShift] = Q_KEY_CODE_SHIFT_R, | |
133 | [kVK_Control] = Q_KEY_CODE_CTRL, | |
134 | [kVK_RightControl] = Q_KEY_CODE_CTRL_R, | |
135 | [kVK_Option] = Q_KEY_CODE_ALT, | |
136 | [kVK_RightOption] = Q_KEY_CODE_ALT_R, | |
137 | [kVK_Command] = Q_KEY_CODE_META_L, | |
138 | [0x36] = Q_KEY_CODE_META_R, /* There is no kVK_RightCommand */ | |
139 | [kVK_Space] = Q_KEY_CODE_SPC, | |
140 | ||
141 | [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0, | |
142 | [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1, | |
143 | [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2, | |
144 | [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3, | |
145 | [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4, | |
146 | [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5, | |
147 | [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6, | |
148 | [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7, | |
149 | [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8, | |
150 | [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9, | |
151 | [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL, | |
152 | [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER, | |
153 | [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD, | |
154 | [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT, | |
155 | [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY, | |
156 | [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE, | |
157 | [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS, | |
158 | [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK, | |
159 | ||
160 | [kVK_UpArrow] = Q_KEY_CODE_UP, | |
161 | [kVK_DownArrow] = Q_KEY_CODE_DOWN, | |
162 | [kVK_LeftArrow] = Q_KEY_CODE_LEFT, | |
163 | [kVK_RightArrow] = Q_KEY_CODE_RIGHT, | |
164 | ||
165 | [kVK_Help] = Q_KEY_CODE_INSERT, | |
166 | [kVK_Home] = Q_KEY_CODE_HOME, | |
167 | [kVK_PageUp] = Q_KEY_CODE_PGUP, | |
168 | [kVK_PageDown] = Q_KEY_CODE_PGDN, | |
169 | [kVK_End] = Q_KEY_CODE_END, | |
170 | [kVK_ForwardDelete] = Q_KEY_CODE_DELETE, | |
171 | ||
172 | [kVK_Escape] = Q_KEY_CODE_ESC, | |
173 | ||
174 | /* The Power key can't be used directly because the operating system uses | |
175 | * it. This key can be emulated by using it in place of another key such as | |
176 | * F1. Don't forget to disable the real key binding. | |
177 | */ | |
178 | /* [kVK_F1] = Q_KEY_CODE_POWER, */ | |
179 | ||
180 | [kVK_F1] = Q_KEY_CODE_F1, | |
181 | [kVK_F2] = Q_KEY_CODE_F2, | |
182 | [kVK_F3] = Q_KEY_CODE_F3, | |
183 | [kVK_F4] = Q_KEY_CODE_F4, | |
184 | [kVK_F5] = Q_KEY_CODE_F5, | |
185 | [kVK_F6] = Q_KEY_CODE_F6, | |
186 | [kVK_F7] = Q_KEY_CODE_F7, | |
187 | [kVK_F8] = Q_KEY_CODE_F8, | |
188 | [kVK_F9] = Q_KEY_CODE_F9, | |
189 | [kVK_F10] = Q_KEY_CODE_F10, | |
190 | [kVK_F11] = Q_KEY_CODE_F11, | |
191 | [kVK_F12] = Q_KEY_CODE_F12, | |
192 | [kVK_F13] = Q_KEY_CODE_PRINT, | |
193 | [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK, | |
194 | [kVK_F15] = Q_KEY_CODE_PAUSE, | |
195 | ||
196 | /* | |
197 | * The eject and volume keys can't be used here because they are handled at | |
198 | * a lower level than what an Application can see. | |
199 | */ | |
5b0753e0 FB |
200 | }; |
201 | ||
77047bb7 | 202 | static int cocoa_keycode_to_qemu(int keycode) |
5b0753e0 | 203 | { |
aaac714f | 204 | if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) { |
01cc4e6f | 205 | fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode); |
5b0753e0 FB |
206 | return 0; |
207 | } | |
aaac714f | 208 | return mac_to_qkeycode_map[keycode]; |
5b0753e0 FB |
209 | } |
210 | ||
693a3e01 JA |
211 | /* Displays an alert dialog box with the specified message */ |
212 | static void QEMU_Alert(NSString *message) | |
213 | { | |
214 | NSAlert *alert; | |
215 | alert = [NSAlert new]; | |
216 | [alert setMessageText: message]; | |
217 | [alert runModal]; | |
218 | } | |
c304f7e2 | 219 | |
693a3e01 JA |
220 | /* Handles any errors that happen with a device transaction */ |
221 | static void handleAnyDeviceErrors(Error * err) | |
222 | { | |
223 | if (err) { | |
224 | QEMU_Alert([NSString stringWithCString: error_get_pretty(err) | |
225 | encoding: NSASCIIStringEncoding]); | |
226 | error_free(err); | |
227 | } | |
228 | } | |
c304f7e2 | 229 | |
5b0753e0 FB |
230 | /* |
231 | ------------------------------------------------------ | |
c304f7e2 | 232 | QemuCocoaView |
5b0753e0 FB |
233 | ------------------------------------------------------ |
234 | */ | |
c304f7e2 | 235 | @interface QemuCocoaView : NSView |
5b0753e0 | 236 | { |
c304f7e2 TS |
237 | QEMUScreen screen; |
238 | NSWindow *fullScreenWindow; | |
239 | float cx,cy,cw,ch,cdx,cdy; | |
240 | CGDataProviderRef dataProviderRef; | |
241 | int modifiers_state[256]; | |
49b9bd4d | 242 | BOOL isMouseGrabbed; |
c304f7e2 TS |
243 | BOOL isFullscreen; |
244 | BOOL isAbsoluteEnabled; | |
f61c387e | 245 | BOOL isMouseDeassociated; |
c304f7e2 | 246 | } |
5e00d3ac | 247 | - (void) switchSurface:(DisplaySurface *)surface; |
c304f7e2 TS |
248 | - (void) grabMouse; |
249 | - (void) ungrabMouse; | |
250 | - (void) toggleFullScreen:(id)sender; | |
251 | - (void) handleEvent:(NSEvent *)event; | |
252 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; | |
f61c387e PM |
253 | /* The state surrounding mouse grabbing is potentially confusing. |
254 | * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated | |
255 | * pointing device an absolute-position one?"], but is only updated on | |
256 | * next refresh. | |
257 | * isMouseGrabbed tracks whether GUI events are directed to the guest; | |
258 | * it controls whether special keys like Cmd get sent to the guest, | |
259 | * and whether we capture the mouse when in non-absolute mode. | |
260 | * isMouseDeassociated tracks whether we've told MacOSX to disassociate | |
261 | * the mouse and mouse cursor position by calling | |
262 | * CGAssociateMouseAndMouseCursorPosition(FALSE) | |
263 | * (which basically happens if we grab in non-absolute mode). | |
264 | */ | |
49b9bd4d | 265 | - (BOOL) isMouseGrabbed; |
c304f7e2 | 266 | - (BOOL) isAbsoluteEnabled; |
f61c387e | 267 | - (BOOL) isMouseDeassociated; |
c304f7e2 TS |
268 | - (float) cdx; |
269 | - (float) cdy; | |
270 | - (QEMUScreen) gscreen; | |
3b178b71 | 271 | - (void) raiseAllKeys; |
c304f7e2 | 272 | @end |
3b46e624 | 273 | |
7fee199c AF |
274 | QemuCocoaView *cocoaView; |
275 | ||
c304f7e2 TS |
276 | @implementation QemuCocoaView |
277 | - (id)initWithFrame:(NSRect)frameRect | |
278 | { | |
279 | COCOA_DEBUG("QemuCocoaView: initWithFrame\n"); | |
3b46e624 | 280 | |
c304f7e2 TS |
281 | self = [super initWithFrame:frameRect]; |
282 | if (self) { | |
3b46e624 | 283 | |
c304f7e2 TS |
284 | screen.bitsPerComponent = 8; |
285 | screen.bitsPerPixel = 32; | |
286 | screen.width = frameRect.size.width; | |
287 | screen.height = frameRect.size.height; | |
3b46e624 | 288 | |
c304f7e2 TS |
289 | } |
290 | return self; | |
291 | } | |
3b46e624 | 292 | |
c304f7e2 TS |
293 | - (void) dealloc |
294 | { | |
295 | COCOA_DEBUG("QemuCocoaView: dealloc\n"); | |
3b46e624 | 296 | |
c304f7e2 TS |
297 | if (dataProviderRef) |
298 | CGDataProviderRelease(dataProviderRef); | |
3b46e624 | 299 | |
c304f7e2 TS |
300 | [super dealloc]; |
301 | } | |
3b46e624 | 302 | |
d50f71dc AF |
303 | - (BOOL) isOpaque |
304 | { | |
305 | return YES; | |
306 | } | |
307 | ||
5dd45bee PM |
308 | - (BOOL) screenContainsPoint:(NSPoint) p |
309 | { | |
310 | return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height); | |
311 | } | |
312 | ||
13aefd30 PM |
313 | - (void) hideCursor |
314 | { | |
315 | if (!cursor_hide) { | |
316 | return; | |
317 | } | |
318 | [NSCursor hide]; | |
319 | } | |
320 | ||
321 | - (void) unhideCursor | |
322 | { | |
323 | if (!cursor_hide) { | |
324 | return; | |
325 | } | |
326 | [NSCursor unhide]; | |
327 | } | |
328 | ||
c304f7e2 TS |
329 | - (void) drawRect:(NSRect) rect |
330 | { | |
331 | COCOA_DEBUG("QemuCocoaView: drawRect\n"); | |
332 | ||
c304f7e2 TS |
333 | // get CoreGraphic context |
334 | CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort]; | |
335 | CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone); | |
336 | CGContextSetShouldAntialias (viewContextRef, NO); | |
337 | ||
338 | // draw screen bitmap directly to Core Graphics context | |
7d270b1c PM |
339 | if (!dataProviderRef) { |
340 | // Draw request before any guest device has set up a framebuffer: | |
341 | // just draw an opaque black rectangle | |
342 | CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0); | |
343 | CGContextFillRect(viewContextRef, NSRectToCGRect(rect)); | |
344 | } else { | |
c304f7e2 TS |
345 | CGImageRef imageRef = CGImageCreate( |
346 | screen.width, //width | |
347 | screen.height, //height | |
348 | screen.bitsPerComponent, //bitsPerComponent | |
349 | screen.bitsPerPixel, //bitsPerPixel | |
9794f74f | 350 | (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow |
04afa4a8 | 351 | #ifdef __LITTLE_ENDIAN__ |
c304f7e2 | 352 | CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4 |
9794f74f | 353 | kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, |
c304f7e2 TS |
354 | #else |
355 | CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc) | |
356 | kCGImageAlphaNoneSkipFirst, //bitmapInfo | |
357 | #endif | |
358 | dataProviderRef, //provider | |
359 | NULL, //decode | |
360 | 0, //interpolate | |
361 | kCGRenderingIntentDefault //intent | |
362 | ); | |
b63901d8 PM |
363 | // selective drawing code (draws only dirty rectangles) (OS X >= 10.4) |
364 | const NSRect *rectList; | |
b63901d8 | 365 | NSInteger rectCount; |
b63901d8 PM |
366 | int i; |
367 | CGImageRef clipImageRef; | |
368 | CGRect clipRect; | |
369 | ||
370 | [self getRectsBeingDrawn:&rectList count:&rectCount]; | |
371 | for (i = 0; i < rectCount; i++) { | |
372 | clipRect.origin.x = rectList[i].origin.x / cdx; | |
373 | clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy; | |
374 | clipRect.size.width = rectList[i].size.width / cdx; | |
375 | clipRect.size.height = rectList[i].size.height / cdy; | |
376 | clipImageRef = CGImageCreateWithImageInRect( | |
377 | imageRef, | |
378 | clipRect | |
379 | ); | |
380 | CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef); | |
381 | CGImageRelease (clipImageRef); | |
5b0753e0 | 382 | } |
c304f7e2 TS |
383 | CGImageRelease (imageRef); |
384 | } | |
5b0753e0 FB |
385 | } |
386 | ||
c304f7e2 | 387 | - (void) setContentDimensions |
5b0753e0 | 388 | { |
c304f7e2 TS |
389 | COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); |
390 | ||
391 | if (isFullscreen) { | |
392 | cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width; | |
393 | cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height; | |
5d1b2eef JA |
394 | |
395 | /* stretches video, but keeps same aspect ratio */ | |
396 | if (stretch_video == true) { | |
397 | /* use smallest stretch value - prevents clipping on sides */ | |
398 | if (MIN(cdx, cdy) == cdx) { | |
399 | cdy = cdx; | |
400 | } else { | |
401 | cdx = cdy; | |
402 | } | |
403 | } else { /* No stretching */ | |
404 | cdx = cdy = 1; | |
405 | } | |
c304f7e2 TS |
406 | cw = screen.width * cdx; |
407 | ch = screen.height * cdy; | |
408 | cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; | |
409 | cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; | |
410 | } else { | |
411 | cx = 0; | |
412 | cy = 0; | |
413 | cw = screen.width; | |
414 | ch = screen.height; | |
415 | cdx = 1.0; | |
416 | cdy = 1.0; | |
417 | } | |
5b0753e0 FB |
418 | } |
419 | ||
5e00d3ac | 420 | - (void) switchSurface:(DisplaySurface *)surface |
5b0753e0 | 421 | { |
5e00d3ac | 422 | COCOA_DEBUG("QemuCocoaView: switchSurface\n"); |
c304f7e2 | 423 | |
8510d91e PM |
424 | int w = surface_width(surface); |
425 | int h = surface_height(surface); | |
381600da PM |
426 | /* cdx == 0 means this is our very first surface, in which case we need |
427 | * to recalculate the content dimensions even if it happens to be the size | |
428 | * of the initial empty window. | |
429 | */ | |
430 | bool isResize = (w != screen.width || h != screen.height || cdx == 0.0); | |
d3345a04 PM |
431 | |
432 | int oldh = screen.height; | |
433 | if (isResize) { | |
434 | // Resize before we trigger the redraw, or we'll redraw at the wrong size | |
435 | COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h); | |
436 | screen.width = w; | |
437 | screen.height = h; | |
438 | [self setContentDimensions]; | |
439 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; | |
440 | } | |
8510d91e | 441 | |
c304f7e2 TS |
442 | // update screenBuffer |
443 | if (dataProviderRef) | |
444 | CGDataProviderRelease(dataProviderRef); | |
3b46e624 | 445 | |
9794f74f | 446 | //sync host window color space with guests |
49060c29 PM |
447 | screen.bitsPerPixel = surface_bits_per_pixel(surface); |
448 | screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2; | |
9794f74f | 449 | |
5e00d3ac | 450 | dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL); |
3b46e624 | 451 | |
c304f7e2 TS |
452 | // update windows |
453 | if (isFullscreen) { | |
454 | [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]]; | |
d3345a04 | 455 | [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 |
456 | } else { |
457 | if (qemu_name) | |
458 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; | |
d3345a04 PM |
459 | [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:YES animate:NO]; |
460 | } | |
461 | ||
462 | if (isResize) { | |
463 | [normalWindow center]; | |
c304f7e2 | 464 | } |
5b0753e0 FB |
465 | } |
466 | ||
c304f7e2 TS |
467 | - (void) toggleFullScreen:(id)sender |
468 | { | |
469 | COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); | |
470 | ||
471 | if (isFullscreen) { // switch from fullscreen to desktop | |
472 | isFullscreen = FALSE; | |
473 | [self ungrabMouse]; | |
474 | [self setContentDimensions]; | |
c304f7e2 TS |
475 | if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime |
476 | [self exitFullScreenModeWithOptions:nil]; | |
477 | } else { | |
c304f7e2 TS |
478 | [fullScreenWindow close]; |
479 | [normalWindow setContentView: self]; | |
480 | [normalWindow makeKeyAndOrderFront: self]; | |
481 | [NSMenu setMenuBarVisible:YES]; | |
c304f7e2 | 482 | } |
c304f7e2 TS |
483 | } else { // switch from desktop to fullscreen |
484 | isFullscreen = TRUE; | |
5d1b2eef | 485 | [normalWindow orderOut: nil]; /* Hide the window */ |
c304f7e2 TS |
486 | [self grabMouse]; |
487 | [self setContentDimensions]; | |
c304f7e2 TS |
488 | if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime |
489 | [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys: | |
490 | [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens, | |
491 | [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting, | |
492 | nil]]; | |
493 | } else { | |
c304f7e2 TS |
494 | [NSMenu setMenuBarVisible:NO]; |
495 | fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] | |
496 | styleMask:NSBorderlessWindowMask | |
497 | backing:NSBackingStoreBuffered | |
498 | defer:NO]; | |
5d1b2eef | 499 | [fullScreenWindow setAcceptsMouseMovedEvents: YES]; |
c304f7e2 | 500 | [fullScreenWindow setHasShadow:NO]; |
5d1b2eef JA |
501 | [fullScreenWindow setBackgroundColor: [NSColor blackColor]]; |
502 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; | |
503 | [[fullScreenWindow contentView] addSubview: self]; | |
c304f7e2 | 504 | [fullScreenWindow makeKeyAndOrderFront:self]; |
c304f7e2 | 505 | } |
c304f7e2 TS |
506 | } |
507 | } | |
5b0753e0 | 508 | |
c304f7e2 | 509 | - (void) handleEvent:(NSEvent *)event |
3b46e624 | 510 | { |
c304f7e2 TS |
511 | COCOA_DEBUG("QemuCocoaView: handleEvent\n"); |
512 | ||
513 | int buttons = 0; | |
514 | int keycode; | |
21bae11a | 515 | bool mouse_event = false; |
c304f7e2 TS |
516 | NSPoint p = [event locationInWindow]; |
517 | ||
518 | switch ([event type]) { | |
519 | case NSFlagsChanged: | |
520 | keycode = cocoa_keycode_to_qemu([event keyCode]); | |
8895919a | 521 | |
aaac714f JA |
522 | if ((keycode == Q_KEY_CODE_META_L || keycode == Q_KEY_CODE_META_R) |
523 | && !isMouseGrabbed) { | |
8895919a PM |
524 | /* Don't pass command key changes to guest unless mouse is grabbed */ |
525 | keycode = 0; | |
526 | } | |
527 | ||
c304f7e2 | 528 | if (keycode) { |
aaac714f JA |
529 | // emulate caps lock and num lock keydown and keyup |
530 | if (keycode == Q_KEY_CODE_CAPS_LOCK || | |
531 | keycode == Q_KEY_CODE_NUM_LOCK) { | |
532 | qemu_input_event_send_key_qcode(dcl->con, keycode, true); | |
533 | qemu_input_event_send_key_qcode(dcl->con, keycode, false); | |
68c0aa6e | 534 | } else if (qemu_console_is_graphic(NULL)) { |
c304f7e2 | 535 | if (modifiers_state[keycode] == 0) { // keydown |
aaac714f | 536 | qemu_input_event_send_key_qcode(dcl->con, keycode, true); |
c304f7e2 TS |
537 | modifiers_state[keycode] = 1; |
538 | } else { // keyup | |
aaac714f | 539 | qemu_input_event_send_key_qcode(dcl->con, keycode, false); |
c304f7e2 TS |
540 | modifiers_state[keycode] = 0; |
541 | } | |
542 | } | |
543 | } | |
3b46e624 | 544 | |
c304f7e2 | 545 | // release Mouse grab when pressing ctrl+alt |
5d1b2eef | 546 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { |
c304f7e2 TS |
547 | [self ungrabMouse]; |
548 | } | |
549 | break; | |
550 | case NSKeyDown: | |
8895919a | 551 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
3b46e624 | 552 | |
8895919a | 553 | // forward command key combos to the host UI unless the mouse is grabbed |
49b9bd4d | 554 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
c304f7e2 TS |
555 | [NSApp sendEvent:event]; |
556 | return; | |
557 | } | |
3b46e624 | 558 | |
c304f7e2 | 559 | // default |
c304f7e2 TS |
560 | |
561 | // handle control + alt Key Combos (ctrl+alt is reserved for QEMU) | |
562 | if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) { | |
563 | switch (keycode) { | |
564 | ||
565 | // enable graphic console | |
aaac714f JA |
566 | case Q_KEY_CODE_1 ... Q_KEY_CODE_9: // '1' to '9' keys |
567 | console_select(keycode - 11); | |
c304f7e2 TS |
568 | break; |
569 | } | |
570 | ||
571 | // handle keys for graphic console | |
68c0aa6e | 572 | } else if (qemu_console_is_graphic(NULL)) { |
aaac714f | 573 | qemu_input_event_send_key_qcode(dcl->con, keycode, true); |
c304f7e2 TS |
574 | |
575 | // handlekeys for Monitor | |
576 | } else { | |
577 | int keysym = 0; | |
578 | switch([event keyCode]) { | |
579 | case 115: | |
580 | keysym = QEMU_KEY_HOME; | |
581 | break; | |
582 | case 117: | |
583 | keysym = QEMU_KEY_DELETE; | |
584 | break; | |
585 | case 119: | |
586 | keysym = QEMU_KEY_END; | |
587 | break; | |
588 | case 123: | |
589 | keysym = QEMU_KEY_LEFT; | |
590 | break; | |
591 | case 124: | |
592 | keysym = QEMU_KEY_RIGHT; | |
593 | break; | |
594 | case 125: | |
595 | keysym = QEMU_KEY_DOWN; | |
596 | break; | |
597 | case 126: | |
598 | keysym = QEMU_KEY_UP; | |
599 | break; | |
600 | default: | |
601 | { | |
602 | NSString *ks = [event characters]; | |
603 | if ([ks length] > 0) | |
604 | keysym = [ks characterAtIndex:0]; | |
605 | } | |
606 | } | |
607 | if (keysym) | |
608 | kbd_put_keysym(keysym); | |
609 | } | |
610 | break; | |
611 | case NSKeyUp: | |
612 | keycode = cocoa_keycode_to_qemu([event keyCode]); | |
8895919a PM |
613 | |
614 | // don't pass the guest a spurious key-up if we treated this | |
615 | // command-key combo as a host UI action | |
49b9bd4d | 616 | if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) { |
8895919a PM |
617 | return; |
618 | } | |
619 | ||
68c0aa6e | 620 | if (qemu_console_is_graphic(NULL)) { |
aaac714f | 621 | qemu_input_event_send_key_qcode(dcl->con, keycode, false); |
c304f7e2 TS |
622 | } |
623 | break; | |
624 | case NSMouseMoved: | |
625 | if (isAbsoluteEnabled) { | |
5dd45bee | 626 | if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) { |
f61c387e PM |
627 | if (isMouseGrabbed) { |
628 | [self ungrabMouse]; | |
c304f7e2 TS |
629 | } |
630 | } else { | |
f61c387e PM |
631 | if (!isMouseGrabbed) { |
632 | [self grabMouse]; | |
c304f7e2 TS |
633 | } |
634 | } | |
5b0753e0 | 635 | } |
21bae11a | 636 | mouse_event = true; |
c304f7e2 TS |
637 | break; |
638 | case NSLeftMouseDown: | |
639 | if ([event modifierFlags] & NSCommandKeyMask) { | |
640 | buttons |= MOUSE_EVENT_RBUTTON; | |
641 | } else { | |
642 | buttons |= MOUSE_EVENT_LBUTTON; | |
643 | } | |
21bae11a | 644 | mouse_event = true; |
c304f7e2 TS |
645 | break; |
646 | case NSRightMouseDown: | |
647 | buttons |= MOUSE_EVENT_RBUTTON; | |
21bae11a | 648 | mouse_event = true; |
c304f7e2 TS |
649 | break; |
650 | case NSOtherMouseDown: | |
651 | buttons |= MOUSE_EVENT_MBUTTON; | |
21bae11a | 652 | mouse_event = true; |
c304f7e2 TS |
653 | break; |
654 | case NSLeftMouseDragged: | |
655 | if ([event modifierFlags] & NSCommandKeyMask) { | |
656 | buttons |= MOUSE_EVENT_RBUTTON; | |
657 | } else { | |
658 | buttons |= MOUSE_EVENT_LBUTTON; | |
659 | } | |
21bae11a | 660 | mouse_event = true; |
c304f7e2 TS |
661 | break; |
662 | case NSRightMouseDragged: | |
663 | buttons |= MOUSE_EVENT_RBUTTON; | |
21bae11a | 664 | mouse_event = true; |
c304f7e2 TS |
665 | break; |
666 | case NSOtherMouseDragged: | |
667 | buttons |= MOUSE_EVENT_MBUTTON; | |
21bae11a | 668 | mouse_event = true; |
c304f7e2 TS |
669 | break; |
670 | case NSLeftMouseUp: | |
f61c387e PM |
671 | mouse_event = true; |
672 | if (!isMouseGrabbed && [self screenContainsPoint:p]) { | |
673 | [self grabMouse]; | |
c304f7e2 TS |
674 | } |
675 | break; | |
676 | case NSRightMouseUp: | |
21bae11a | 677 | mouse_event = true; |
c304f7e2 TS |
678 | break; |
679 | case NSOtherMouseUp: | |
21bae11a | 680 | mouse_event = true; |
c304f7e2 TS |
681 | break; |
682 | case NSScrollWheel: | |
f61c387e | 683 | if (isMouseGrabbed) { |
21bae11a GH |
684 | buttons |= ([event deltaY] < 0) ? |
685 | MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN; | |
c304f7e2 | 686 | } |
f61c387e | 687 | mouse_event = true; |
c304f7e2 TS |
688 | break; |
689 | default: | |
690 | [NSApp sendEvent:event]; | |
5b0753e0 | 691 | } |
21bae11a GH |
692 | |
693 | if (mouse_event) { | |
8d3a5d9b PM |
694 | /* Don't send button events to the guest unless we've got a |
695 | * mouse grab or window focus. If we have neither then this event | |
696 | * is the user clicking on the background window to activate and | |
697 | * bring us to the front, which will be done by the sendEvent | |
698 | * call below. We definitely don't want to pass that click through | |
699 | * to the guest. | |
700 | */ | |
701 | if ((isMouseGrabbed || [[self window] isKeyWindow]) && | |
702 | (last_buttons != buttons)) { | |
7fb1cf16 | 703 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
21bae11a GH |
704 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
705 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, | |
706 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, | |
f22d0af0 GH |
707 | [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP, |
708 | [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN, | |
21bae11a GH |
709 | }; |
710 | qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons); | |
711 | last_buttons = buttons; | |
712 | } | |
f61c387e PM |
713 | if (isMouseGrabbed) { |
714 | if (isAbsoluteEnabled) { | |
715 | /* Note that the origin for Cocoa mouse coords is bottom left, not top left. | |
716 | * The check on screenContainsPoint is to avoid sending out of range values for | |
717 | * clicks in the titlebar. | |
718 | */ | |
719 | if ([self screenContainsPoint:p]) { | |
720 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width); | |
721 | qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height); | |
722 | } | |
723 | } else { | |
724 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]); | |
725 | qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]); | |
726 | } | |
21bae11a GH |
727 | } else { |
728 | [NSApp sendEvent:event]; | |
729 | } | |
730 | qemu_input_event_sync(); | |
731 | } | |
5b0753e0 FB |
732 | } |
733 | ||
c304f7e2 | 734 | - (void) grabMouse |
5b0753e0 | 735 | { |
c304f7e2 | 736 | COCOA_DEBUG("QemuCocoaView: grabMouse\n"); |
3b46e624 | 737 | |
c304f7e2 TS |
738 | if (!isFullscreen) { |
739 | if (qemu_name) | |
740 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]]; | |
741 | else | |
742 | [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"]; | |
743 | } | |
13aefd30 | 744 | [self hideCursor]; |
f61c387e PM |
745 | if (!isAbsoluteEnabled) { |
746 | isMouseDeassociated = TRUE; | |
747 | CGAssociateMouseAndMouseCursorPosition(FALSE); | |
748 | } | |
49b9bd4d | 749 | isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] |
5b0753e0 | 750 | } |
3b46e624 | 751 | |
c304f7e2 TS |
752 | - (void) ungrabMouse |
753 | { | |
754 | COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); | |
3b46e624 | 755 | |
c304f7e2 TS |
756 | if (!isFullscreen) { |
757 | if (qemu_name) | |
758 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; | |
759 | else | |
760 | [normalWindow setTitle:@"QEMU"]; | |
761 | } | |
13aefd30 | 762 | [self unhideCursor]; |
f61c387e PM |
763 | if (isMouseDeassociated) { |
764 | CGAssociateMouseAndMouseCursorPosition(TRUE); | |
765 | isMouseDeassociated = FALSE; | |
766 | } | |
49b9bd4d | 767 | isMouseGrabbed = FALSE; |
5b0753e0 FB |
768 | } |
769 | ||
c304f7e2 | 770 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;} |
49b9bd4d | 771 | - (BOOL) isMouseGrabbed {return isMouseGrabbed;} |
c304f7e2 | 772 | - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} |
f61c387e | 773 | - (BOOL) isMouseDeassociated {return isMouseDeassociated;} |
c304f7e2 TS |
774 | - (float) cdx {return cdx;} |
775 | - (float) cdy {return cdy;} | |
776 | - (QEMUScreen) gscreen {return screen;} | |
3b178b71 JA |
777 | |
778 | /* | |
779 | * Makes the target think all down keys are being released. | |
780 | * This prevents a stuck key problem, since we will not see | |
781 | * key up events for those keys after we have lost focus. | |
782 | */ | |
783 | - (void) raiseAllKeys | |
784 | { | |
785 | int index; | |
786 | const int max_index = ARRAY_SIZE(modifiers_state); | |
787 | ||
788 | for (index = 0; index < max_index; index++) { | |
789 | if (modifiers_state[index]) { | |
790 | modifiers_state[index] = 0; | |
aaac714f | 791 | qemu_input_event_send_key_qcode(dcl->con, index, false); |
3b178b71 JA |
792 | } |
793 | } | |
794 | } | |
5b0753e0 FB |
795 | @end |
796 | ||
797 | ||
c304f7e2 | 798 | |
5b0753e0 FB |
799 | /* |
800 | ------------------------------------------------------ | |
c304f7e2 | 801 | QemuCocoaAppController |
5b0753e0 FB |
802 | ------------------------------------------------------ |
803 | */ | |
c304f7e2 | 804 | @interface QemuCocoaAppController : NSObject |
2a4c8c53 | 805 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) |
d9bc14f6 | 806 | <NSWindowDelegate, NSApplicationDelegate> |
2a4c8c53 | 807 | #endif |
5b0753e0 FB |
808 | { |
809 | } | |
5b0753e0 | 810 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv; |
5d1b2eef | 811 | - (void)doToggleFullScreen:(id)sender; |
c304f7e2 TS |
812 | - (void)toggleFullScreen:(id)sender; |
813 | - (void)showQEMUDoc:(id)sender; | |
814 | - (void)showQEMUTec:(id)sender; | |
5d1b2eef | 815 | - (void)zoomToFit:(id) sender; |
b4c6a112 | 816 | - (void)displayConsole:(id)sender; |
8524f1c7 JA |
817 | - (void)pauseQEMU:(id)sender; |
818 | - (void)resumeQEMU:(id)sender; | |
819 | - (void)displayPause; | |
820 | - (void)removePause; | |
27074614 JA |
821 | - (void)restartQEMU:(id)sender; |
822 | - (void)powerDownQEMU:(id)sender; | |
693a3e01 JA |
823 | - (void)ejectDeviceMedia:(id)sender; |
824 | - (void)changeDeviceMedia:(id)sender; | |
d9bc14f6 | 825 | - (BOOL)verifyQuit; |
f4747900 | 826 | - (void)openDocumentation:(NSString *)filename; |
5b0753e0 FB |
827 | @end |
828 | ||
c304f7e2 TS |
829 | @implementation QemuCocoaAppController |
830 | - (id) init | |
5b0753e0 | 831 | { |
c304f7e2 | 832 | COCOA_DEBUG("QemuCocoaAppController: init\n"); |
5a246934 | 833 | |
c304f7e2 TS |
834 | self = [super init]; |
835 | if (self) { | |
5a246934 | 836 | |
c304f7e2 TS |
837 | // create a view and add it to the window |
838 | cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)]; | |
839 | if(!cocoaView) { | |
840 | fprintf(stderr, "(cocoa) can't create a view\n"); | |
841 | exit(1); | |
842 | } | |
3b46e624 | 843 | |
c304f7e2 TS |
844 | // create a window |
845 | normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame] | |
846 | styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask | |
847 | backing:NSBackingStoreBuffered defer:NO]; | |
848 | if(!normalWindow) { | |
849 | fprintf(stderr, "(cocoa) can't create window\n"); | |
850 | exit(1); | |
851 | } | |
852 | [normalWindow setAcceptsMouseMovedEvents:YES]; | |
a1dbc05a | 853 | [normalWindow setTitle:@"QEMU"]; |
c304f7e2 | 854 | [normalWindow setContentView:cocoaView]; |
81801ae2 | 855 | #if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10) |
561ef251 | 856 | [normalWindow useOptimizedDrawing:YES]; |
81801ae2 | 857 | #endif |
c304f7e2 | 858 | [normalWindow makeKeyAndOrderFront:self]; |
49060c29 | 859 | [normalWindow center]; |
d9bc14f6 | 860 | [normalWindow setDelegate: self]; |
5d1b2eef | 861 | stretch_video = false; |
8524f1c7 JA |
862 | |
863 | /* Used for displaying pause on the screen */ | |
864 | pauseLabel = [NSTextField new]; | |
865 | [pauseLabel setBezeled:YES]; | |
866 | [pauseLabel setDrawsBackground:YES]; | |
867 | [pauseLabel setBackgroundColor: [NSColor whiteColor]]; | |
868 | [pauseLabel setEditable:NO]; | |
869 | [pauseLabel setSelectable:NO]; | |
870 | [pauseLabel setStringValue: @"Paused"]; | |
871 | [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]]; | |
872 | [pauseLabel setTextColor: [NSColor blackColor]]; | |
873 | [pauseLabel sizeToFit]; | |
693a3e01 JA |
874 | |
875 | // set the supported image file types that can be opened | |
876 | supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg", | |
9d227f19 JA |
877 | @"qcow", @"qcow2", @"cloop", @"vmdk", @"cdr", |
878 | nil]; | |
c304f7e2 TS |
879 | } |
880 | return self; | |
881 | } | |
3b46e624 | 882 | |
c304f7e2 TS |
883 | - (void) dealloc |
884 | { | |
885 | COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); | |
886 | ||
887 | if (cocoaView) | |
888 | [cocoaView release]; | |
889 | [super dealloc]; | |
890 | } | |
3b46e624 | 891 | |
c304f7e2 TS |
892 | - (void)applicationDidFinishLaunching: (NSNotification *) note |
893 | { | |
894 | COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); | |
365d7f3c JA |
895 | // launch QEMU, with the global args |
896 | [self startEmulationWithArgc:gArgc argv:(char **)gArgv]; | |
5b0753e0 FB |
897 | } |
898 | ||
899 | - (void)applicationWillTerminate:(NSNotification *)aNotification | |
900 | { | |
c304f7e2 TS |
901 | COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n"); |
902 | ||
5b0753e0 | 903 | qemu_system_shutdown_request(); |
5b0753e0 FB |
904 | exit(0); |
905 | } | |
906 | ||
41ea49b3 AF |
907 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication |
908 | { | |
909 | return YES; | |
910 | } | |
911 | ||
d9bc14f6 JA |
912 | - (NSApplicationTerminateReply)applicationShouldTerminate: |
913 | (NSApplication *)sender | |
914 | { | |
915 | COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n"); | |
916 | return [self verifyQuit]; | |
917 | } | |
918 | ||
919 | /* Called when the user clicks on a window's close button */ | |
920 | - (BOOL)windowShouldClose:(id)sender | |
921 | { | |
922 | COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n"); | |
923 | [NSApp terminate: sender]; | |
924 | /* If the user allows the application to quit then the call to | |
925 | * NSApp terminate will never return. If we get here then the user | |
926 | * cancelled the quit, so we should return NO to not permit the | |
927 | * closing of this window. | |
928 | */ | |
929 | return NO; | |
930 | } | |
931 | ||
3b178b71 JA |
932 | /* Called when QEMU goes into the background */ |
933 | - (void) applicationWillResignActive: (NSNotification *)aNotification | |
934 | { | |
935 | COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n"); | |
936 | [cocoaView raiseAllKeys]; | |
937 | } | |
938 | ||
c304f7e2 TS |
939 | - (void)startEmulationWithArgc:(int)argc argv:(char**)argv |
940 | { | |
941 | COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n"); | |
942 | ||
943 | int status; | |
3bbbee18 | 944 | status = qemu_main(argc, argv, *_NSGetEnviron()); |
c304f7e2 TS |
945 | exit(status); |
946 | } | |
947 | ||
5d1b2eef JA |
948 | /* We abstract the method called by the Enter Fullscreen menu item |
949 | * because Mac OS 10.7 and higher disables it. This is because of the | |
950 | * menu item's old selector's name toggleFullScreen: | |
951 | */ | |
952 | - (void) doToggleFullScreen:(id)sender | |
953 | { | |
954 | [self toggleFullScreen:(id)sender]; | |
955 | } | |
956 | ||
c304f7e2 TS |
957 | - (void)toggleFullScreen:(id)sender |
958 | { | |
959 | COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); | |
960 | ||
961 | [cocoaView toggleFullScreen:sender]; | |
962 | } | |
5b0753e0 | 963 | |
f4747900 JA |
964 | /* Tries to find then open the specified filename */ |
965 | - (void) openDocumentation: (NSString *) filename | |
966 | { | |
967 | /* Where to look for local files */ | |
968 | NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"}; | |
969 | NSString *full_file_path; | |
970 | ||
971 | /* iterate thru the possible paths until the file is found */ | |
972 | int index; | |
973 | for (index = 0; index < ARRAY_SIZE(path_array); index++) { | |
974 | full_file_path = [[NSBundle mainBundle] executablePath]; | |
975 | full_file_path = [full_file_path stringByDeletingLastPathComponent]; | |
976 | full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path, | |
977 | path_array[index], filename]; | |
978 | if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) { | |
979 | return; | |
980 | } | |
981 | } | |
982 | ||
983 | /* If none of the paths opened a file */ | |
984 | NSBeep(); | |
985 | QEMU_Alert(@"Failed to open file"); | |
986 | } | |
987 | ||
c304f7e2 | 988 | - (void)showQEMUDoc:(id)sender |
5b0753e0 | 989 | { |
c304f7e2 TS |
990 | COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n"); |
991 | ||
f4747900 | 992 | [self openDocumentation: @"qemu-doc.html"]; |
c304f7e2 TS |
993 | } |
994 | ||
995 | - (void)showQEMUTec:(id)sender | |
996 | { | |
997 | COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n"); | |
998 | ||
f4747900 | 999 | [self openDocumentation: @"qemu-tech.html"]; |
5b0753e0 | 1000 | } |
5d1b2eef JA |
1001 | |
1002 | /* Stretches video to fit host monitor size */ | |
1003 | - (void)zoomToFit:(id) sender | |
1004 | { | |
1005 | stretch_video = !stretch_video; | |
1006 | if (stretch_video == true) { | |
1007 | [sender setState: NSOnState]; | |
1008 | } else { | |
1009 | [sender setState: NSOffState]; | |
1010 | } | |
1011 | } | |
5b0753e0 | 1012 | |
b4c6a112 JA |
1013 | /* Displays the console on the screen */ |
1014 | - (void)displayConsole:(id)sender | |
1015 | { | |
1016 | console_select([sender tag]); | |
1017 | } | |
8524f1c7 JA |
1018 | |
1019 | /* Pause the guest */ | |
1020 | - (void)pauseQEMU:(id)sender | |
1021 | { | |
1022 | qmp_stop(NULL); | |
1023 | [sender setEnabled: NO]; | |
1024 | [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES]; | |
1025 | [self displayPause]; | |
1026 | } | |
1027 | ||
1028 | /* Resume running the guest operating system */ | |
1029 | - (void)resumeQEMU:(id) sender | |
1030 | { | |
1031 | qmp_cont(NULL); | |
1032 | [sender setEnabled: NO]; | |
1033 | [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES]; | |
1034 | [self removePause]; | |
1035 | } | |
1036 | ||
1037 | /* Displays the word pause on the screen */ | |
1038 | - (void)displayPause | |
1039 | { | |
1040 | /* Coordinates have to be calculated each time because the window can change its size */ | |
1041 | int xCoord, yCoord, width, height; | |
1042 | xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2; | |
1043 | yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5); | |
1044 | width = [pauseLabel frame].size.width; | |
1045 | height = [pauseLabel frame].size.height; | |
1046 | [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)]; | |
1047 | [cocoaView addSubview: pauseLabel]; | |
1048 | } | |
1049 | ||
1050 | /* Removes the word pause from the screen */ | |
1051 | - (void)removePause | |
1052 | { | |
1053 | [pauseLabel removeFromSuperview]; | |
1054 | } | |
1055 | ||
27074614 JA |
1056 | /* Restarts QEMU */ |
1057 | - (void)restartQEMU:(id)sender | |
1058 | { | |
1059 | qmp_system_reset(NULL); | |
1060 | } | |
1061 | ||
1062 | /* Powers down QEMU */ | |
1063 | - (void)powerDownQEMU:(id)sender | |
1064 | { | |
1065 | qmp_system_powerdown(NULL); | |
1066 | } | |
1067 | ||
693a3e01 JA |
1068 | /* Ejects the media. |
1069 | * Uses sender's tag to figure out the device to eject. | |
1070 | */ | |
1071 | - (void)ejectDeviceMedia:(id)sender | |
1072 | { | |
1073 | NSString * drive; | |
1074 | drive = [sender representedObject]; | |
1075 | if(drive == nil) { | |
1076 | NSBeep(); | |
1077 | QEMU_Alert(@"Failed to find drive to eject!"); | |
1078 | return; | |
1079 | } | |
1080 | ||
1081 | Error *err = NULL; | |
1082 | qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding], false, false, &err); | |
1083 | handleAnyDeviceErrors(err); | |
1084 | } | |
1085 | ||
1086 | /* Displays a dialog box asking the user to select an image file to load. | |
1087 | * Uses sender's represented object value to figure out which drive to use. | |
1088 | */ | |
1089 | - (void)changeDeviceMedia:(id)sender | |
1090 | { | |
1091 | /* Find the drive name */ | |
1092 | NSString * drive; | |
1093 | drive = [sender representedObject]; | |
1094 | if(drive == nil) { | |
1095 | NSBeep(); | |
1096 | QEMU_Alert(@"Could not find drive!"); | |
1097 | return; | |
1098 | } | |
1099 | ||
1100 | /* Display the file open dialog */ | |
1101 | NSOpenPanel * openPanel; | |
1102 | openPanel = [NSOpenPanel openPanel]; | |
1103 | [openPanel setCanChooseFiles: YES]; | |
1104 | [openPanel setAllowsMultipleSelection: NO]; | |
1105 | [openPanel setAllowedFileTypes: supportedImageFileTypes]; | |
1106 | if([openPanel runModal] == NSFileHandlingPanelOKButton) { | |
1107 | NSString * file = [[[openPanel URLs] objectAtIndex: 0] path]; | |
1108 | if(file == nil) { | |
1109 | NSBeep(); | |
1110 | QEMU_Alert(@"Failed to convert URL to file path!"); | |
1111 | return; | |
1112 | } | |
1113 | ||
1114 | Error *err = NULL; | |
24fb4133 HR |
1115 | qmp_blockdev_change_medium([drive cStringUsingEncoding: |
1116 | NSASCIIStringEncoding], | |
1117 | [file cStringUsingEncoding: | |
1118 | NSASCIIStringEncoding], | |
1119 | true, "raw", | |
39ff43d9 | 1120 | false, 0, |
24fb4133 | 1121 | &err); |
693a3e01 JA |
1122 | handleAnyDeviceErrors(err); |
1123 | } | |
1124 | } | |
1125 | ||
d9bc14f6 JA |
1126 | /* Verifies if the user really wants to quit */ |
1127 | - (BOOL)verifyQuit | |
1128 | { | |
1129 | NSAlert *alert = [NSAlert new]; | |
1130 | [alert autorelease]; | |
1131 | [alert setMessageText: @"Are you sure you want to quit QEMU?"]; | |
1132 | [alert addButtonWithTitle: @"Cancel"]; | |
1133 | [alert addButtonWithTitle: @"Quit"]; | |
1134 | if([alert runModal] == NSAlertSecondButtonReturn) { | |
1135 | return YES; | |
1136 | } else { | |
1137 | return NO; | |
1138 | } | |
1139 | } | |
1140 | ||
b4c6a112 | 1141 | @end |
5b0753e0 | 1142 | |
c304f7e2 | 1143 | |
c304f7e2 | 1144 | int main (int argc, const char * argv[]) { |
5b0753e0 | 1145 | |
c304f7e2 TS |
1146 | gArgc = argc; |
1147 | gArgv = (char **)argv; | |
f4918804 AF |
1148 | int i; |
1149 | ||
1150 | /* In case we don't need to display a window, let's not do that */ | |
1151 | for (i = 1; i < argc; i++) { | |
e4ebcc1a TG |
1152 | const char *opt = argv[i]; |
1153 | ||
1154 | if (opt[0] == '-') { | |
1155 | /* Treat --foo the same as -foo. */ | |
1156 | if (opt[1] == '-') { | |
1157 | opt++; | |
1158 | } | |
9851484f AR |
1159 | if (!strcmp(opt, "-h") || !strcmp(opt, "-help") || |
1160 | !strcmp(opt, "-vnc") || | |
e4ebcc1a TG |
1161 | !strcmp(opt, "-nographic") || |
1162 | !strcmp(opt, "-version") || | |
60b46aa2 | 1163 | !strcmp(opt, "-curses") || |
b12a84ce | 1164 | !strcmp(opt, "-display") || |
60b46aa2 | 1165 | !strcmp(opt, "-qtest")) { |
3bbbee18 | 1166 | return qemu_main(gArgc, gArgv, *_NSGetEnviron()); |
e4ebcc1a | 1167 | } |
f4918804 AF |
1168 | } |
1169 | } | |
5b0753e0 | 1170 | |
c304f7e2 | 1171 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
5b0753e0 | 1172 | |
42a5dfe7 PM |
1173 | // Pull this console process up to being a fully-fledged graphical |
1174 | // app with a menubar and Dock icon | |
1175 | ProcessSerialNumber psn = { 0, kCurrentProcess }; | |
1176 | TransformProcessType(&psn, kProcessTransformToForegroundApplication); | |
1177 | ||
1178 | [NSApplication sharedApplication]; | |
5b0753e0 | 1179 | |
c304f7e2 TS |
1180 | // Add menus |
1181 | NSMenu *menu; | |
1182 | NSMenuItem *menuItem; | |
5b0753e0 | 1183 | |
c304f7e2 | 1184 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
5b0753e0 | 1185 | |
c304f7e2 TS |
1186 | // Application menu |
1187 | menu = [[NSMenu alloc] initWithTitle:@""]; | |
1188 | [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU | |
1189 | [menu addItem:[NSMenuItem separatorItem]]; //Separator | |
1190 | [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU | |
1191 | menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others | |
1192 | [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; | |
1193 | [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All | |
1194 | [menu addItem:[NSMenuItem separatorItem]]; //Separator | |
1195 | [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"]; | |
1196 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; | |
1197 | [menuItem setSubmenu:menu]; | |
1198 | [[NSApp mainMenu] addItem:menuItem]; | |
1199 | [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+) | |
3b46e624 | 1200 | |
8524f1c7 JA |
1201 | // Machine menu |
1202 | menu = [[NSMenu alloc] initWithTitle: @"Machine"]; | |
1203 | [menu setAutoenablesItems: NO]; | |
1204 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]]; | |
1205 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease]; | |
1206 | [menu addItem: menuItem]; | |
1207 | [menuItem setEnabled: NO]; | |
27074614 JA |
1208 | [menu addItem: [NSMenuItem separatorItem]]; |
1209 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]]; | |
1210 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]]; | |
8524f1c7 JA |
1211 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease]; |
1212 | [menuItem setSubmenu:menu]; | |
1213 | [[NSApp mainMenu] addItem:menuItem]; | |
1214 | ||
c304f7e2 TS |
1215 | // View menu |
1216 | menu = [[NSMenu alloc] initWithTitle:@"View"]; | |
5d1b2eef JA |
1217 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen |
1218 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]]; | |
c304f7e2 TS |
1219 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease]; |
1220 | [menuItem setSubmenu:menu]; | |
5b0753e0 FB |
1221 | [[NSApp mainMenu] addItem:menuItem]; |
1222 | ||
c304f7e2 TS |
1223 | // Window menu |
1224 | menu = [[NSMenu alloc] initWithTitle:@"Window"]; | |
1225 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize | |
1226 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; | |
1227 | [menuItem setSubmenu:menu]; | |
1228 | [[NSApp mainMenu] addItem:menuItem]; | |
1229 | [NSApp setWindowsMenu:menu]; | |
1230 | ||
1231 | // Help menu | |
1232 | menu = [[NSMenu alloc] initWithTitle:@"Help"]; | |
1233 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help | |
1234 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help | |
1235 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; | |
1236 | [menuItem setSubmenu:menu]; | |
1237 | [[NSApp mainMenu] addItem:menuItem]; | |
5b0753e0 | 1238 | |
c304f7e2 TS |
1239 | // Create an Application controller |
1240 | QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init]; | |
1241 | [NSApp setDelegate:appController]; | |
5b0753e0 | 1242 | |
c304f7e2 TS |
1243 | // Start the main event loop |
1244 | [NSApp run]; | |
5b0753e0 | 1245 | |
c304f7e2 TS |
1246 | [appController release]; |
1247 | [pool release]; | |
3b46e624 | 1248 | |
c304f7e2 TS |
1249 | return 0; |
1250 | } | |
3b46e624 | 1251 | |
3b46e624 | 1252 | |
5b0753e0 | 1253 | |
c304f7e2 | 1254 | #pragma mark qemu |
7c20b4a3 | 1255 | static void cocoa_update(DisplayChangeListener *dcl, |
7c20b4a3 | 1256 | int x, int y, int w, int h) |
c304f7e2 | 1257 | { |
6e657e64 PM |
1258 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
1259 | ||
c304f7e2 TS |
1260 | COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); |
1261 | ||
1262 | NSRect rect; | |
1263 | if ([cocoaView cdx] == 1.0) { | |
1264 | rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); | |
1265 | } else { | |
1266 | rect = NSMakeRect( | |
1267 | x * [cocoaView cdx], | |
1268 | ([cocoaView gscreen].height - y - h) * [cocoaView cdy], | |
1269 | w * [cocoaView cdx], | |
1270 | h * [cocoaView cdy]); | |
1271 | } | |
17ccbc27 | 1272 | [cocoaView setNeedsDisplayInRect:rect]; |
6e657e64 PM |
1273 | |
1274 | [pool release]; | |
5b0753e0 FB |
1275 | } |
1276 | ||
c12aeb86 | 1277 | static void cocoa_switch(DisplayChangeListener *dcl, |
c12aeb86 | 1278 | DisplaySurface *surface) |
5b0753e0 | 1279 | { |
6e657e64 | 1280 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
3b46e624 | 1281 | |
6e657e64 | 1282 | COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); |
5e00d3ac | 1283 | [cocoaView switchSurface:surface]; |
6e657e64 | 1284 | [pool release]; |
c304f7e2 | 1285 | } |
3b46e624 | 1286 | |
bc2ed970 | 1287 | static void cocoa_refresh(DisplayChangeListener *dcl) |
c304f7e2 | 1288 | { |
6e657e64 PM |
1289 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
1290 | ||
c304f7e2 | 1291 | COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n"); |
468a895b | 1292 | graphic_hw_update(NULL); |
3b46e624 | 1293 | |
21bae11a | 1294 | if (qemu_input_is_absolute()) { |
c304f7e2 | 1295 | if (![cocoaView isAbsoluteEnabled]) { |
49b9bd4d | 1296 | if ([cocoaView isMouseGrabbed]) { |
c304f7e2 TS |
1297 | [cocoaView ungrabMouse]; |
1298 | } | |
1299 | } | |
1300 | [cocoaView setAbsoluteEnabled:YES]; | |
1301 | } | |
5b0753e0 | 1302 | |
c304f7e2 TS |
1303 | NSDate *distantPast; |
1304 | NSEvent *event; | |
1305 | distantPast = [NSDate distantPast]; | |
1306 | do { | |
1307 | event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast | |
1308 | inMode: NSDefaultRunLoopMode dequeue:YES]; | |
1309 | if (event != nil) { | |
1310 | [cocoaView handleEvent:event]; | |
1311 | } | |
1312 | } while(event != nil); | |
6e657e64 | 1313 | [pool release]; |
c304f7e2 | 1314 | } |
3b46e624 | 1315 | |
c304f7e2 TS |
1316 | static void cocoa_cleanup(void) |
1317 | { | |
1318 | COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n"); | |
58a06675 | 1319 | g_free(dcl); |
5b0753e0 FB |
1320 | } |
1321 | ||
7c20b4a3 GH |
1322 | static const DisplayChangeListenerOps dcl_ops = { |
1323 | .dpy_name = "cocoa", | |
8510d91e PM |
1324 | .dpy_gfx_update = cocoa_update, |
1325 | .dpy_gfx_switch = cocoa_switch, | |
1326 | .dpy_refresh = cocoa_refresh, | |
7c20b4a3 GH |
1327 | }; |
1328 | ||
b4c6a112 JA |
1329 | /* Returns a name for a given console */ |
1330 | static NSString * getConsoleName(QemuConsole * console) | |
1331 | { | |
1332 | return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)]; | |
1333 | } | |
1334 | ||
1335 | /* Add an entry to the View menu for each console */ | |
1336 | static void add_console_menu_entries(void) | |
1337 | { | |
1338 | NSMenu *menu; | |
1339 | NSMenuItem *menuItem; | |
1340 | int index = 0; | |
1341 | ||
1342 | menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu]; | |
1343 | ||
1344 | [menu addItem:[NSMenuItem separatorItem]]; | |
1345 | ||
1346 | while (qemu_console_lookup_by_index(index) != NULL) { | |
1347 | menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index)) | |
1348 | action: @selector(displayConsole:) keyEquivalent: @""] autorelease]; | |
1349 | [menuItem setTag: index]; | |
1350 | [menu addItem: menuItem]; | |
1351 | index++; | |
1352 | } | |
1353 | } | |
1354 | ||
693a3e01 JA |
1355 | /* Make menu items for all removable devices. |
1356 | * Each device is given an 'Eject' and 'Change' menu item. | |
1357 | */ | |
a7940ec0 | 1358 | static void addRemovableDevicesMenuItems(void) |
693a3e01 JA |
1359 | { |
1360 | NSMenu *menu; | |
1361 | NSMenuItem *menuItem; | |
1362 | BlockInfoList *currentDevice, *pointerToFree; | |
1363 | NSString *deviceName; | |
1364 | ||
1365 | currentDevice = qmp_query_block(NULL); | |
1366 | pointerToFree = currentDevice; | |
1367 | if(currentDevice == NULL) { | |
1368 | NSBeep(); | |
1369 | QEMU_Alert(@"Failed to query for block devices!"); | |
1370 | return; | |
1371 | } | |
1372 | ||
1373 | menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; | |
1374 | ||
1375 | // Add a separator between related groups of menu items | |
1376 | [menu addItem:[NSMenuItem separatorItem]]; | |
1377 | ||
1378 | // Set the attributes to the "Removable Media" menu item | |
1379 | NSString *titleString = @"Removable Media"; | |
1380 | NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString]; | |
1381 | NSColor *newColor = [NSColor blackColor]; | |
1382 | NSFontManager *fontManager = [NSFontManager sharedFontManager]; | |
1383 | NSFont *font = [fontManager fontWithFamily:@"Helvetica" | |
1384 | traits:NSBoldFontMask|NSItalicFontMask | |
1385 | weight:0 | |
1386 | size:14]; | |
1387 | [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])]; | |
1388 | [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])]; | |
1389 | [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])]; | |
1390 | ||
1391 | // Add the "Removable Media" menu item | |
1392 | menuItem = [NSMenuItem new]; | |
1393 | [menuItem setAttributedTitle: attString]; | |
1394 | [menuItem setEnabled: NO]; | |
1395 | [menu addItem: menuItem]; | |
1396 | ||
cb8d4c8f | 1397 | /* Loop through all the block devices in the emulator */ |
693a3e01 JA |
1398 | while (currentDevice) { |
1399 | deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain]; | |
1400 | ||
1401 | if(currentDevice->value->removable) { | |
1402 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device] | |
1403 | action: @selector(changeDeviceMedia:) | |
1404 | keyEquivalent: @""]; | |
1405 | [menu addItem: menuItem]; | |
1406 | [menuItem setRepresentedObject: deviceName]; | |
1407 | [menuItem autorelease]; | |
1408 | ||
1409 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device] | |
1410 | action: @selector(ejectDeviceMedia:) | |
1411 | keyEquivalent: @""]; | |
1412 | [menu addItem: menuItem]; | |
1413 | [menuItem setRepresentedObject: deviceName]; | |
1414 | [menuItem autorelease]; | |
1415 | } | |
1416 | currentDevice = currentDevice->next; | |
1417 | } | |
1418 | qapi_free_BlockInfoList(pointerToFree); | |
1419 | } | |
1420 | ||
c304f7e2 | 1421 | void cocoa_display_init(DisplayState *ds, int full_screen) |
5b0753e0 | 1422 | { |
c304f7e2 TS |
1423 | COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); |
1424 | ||
43227af8 JA |
1425 | /* if fullscreen mode is to be used */ |
1426 | if (full_screen == true) { | |
1427 | [NSApp activateIgnoringOtherApps: YES]; | |
1428 | [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil]; | |
1429 | } | |
1430 | ||
58a06675 BS |
1431 | dcl = g_malloc0(sizeof(DisplayChangeListener)); |
1432 | ||
9794f74f | 1433 | // register vga output callbacks |
7c20b4a3 | 1434 | dcl->ops = &dcl_ops; |
5209089f | 1435 | register_displaychangelistener(dcl); |
cae41b10 | 1436 | |
c304f7e2 TS |
1437 | // register cleanup function |
1438 | atexit(cocoa_cleanup); | |
b4c6a112 JA |
1439 | |
1440 | /* At this point QEMU has created all the consoles, so we can add View | |
1441 | * menu entries for them. | |
1442 | */ | |
1443 | add_console_menu_entries(); | |
693a3e01 JA |
1444 | |
1445 | /* Give all removable devices a menu item. | |
1446 | * Has to be called after QEMU has started to | |
1447 | * find out what removable devices it has. | |
1448 | */ | |
1449 | addRemovableDevicesMenuItems(); | |
5b0753e0 | 1450 | } |