]>
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" |
6d73bb64 | 33 | #include "ui/kbd-state.h" |
9c17d615 | 34 | #include "sysemu/sysemu.h" |
54d31236 | 35 | #include "sysemu/runstate.h" |
b0c3cf94 | 36 | #include "sysemu/cpu-throttle.h" |
e688df6b | 37 | #include "qapi/error.h" |
16bf5234 | 38 | #include "qapi/qapi-commands-block.h" |
90f8c0f9 | 39 | #include "qapi/qapi-commands-machine.h" |
16bf5234 | 40 | #include "qapi/qapi-commands-misc.h" |
693a3e01 | 41 | #include "sysemu/blockdev.h" |
9e8204b1 | 42 | #include "qemu-version.h" |
e31746ec | 43 | #include "qemu/cutils.h" |
db725815 | 44 | #include "qemu/main-loop.h" |
0b8fa32f | 45 | #include "qemu/module.h" |
aaac714f | 46 | #include <Carbon/Carbon.h> |
2e5b09fd | 47 | #include "hw/core/cpu.h" |
da4dbf74 | 48 | |
5e24600a BS |
49 | #ifndef MAC_OS_X_VERSION_10_13 |
50 | #define MAC_OS_X_VERSION_10_13 101300 | |
51 | #endif | |
44e4c0ba | 52 | |
5e24600a BS |
53 | /* 10.14 deprecates NSOnState and NSOffState in favor of |
54 | * NSControlStateValueOn/Off, which were introduced in 10.13. | |
55 | * Define for older versions | |
56 | */ | |
57 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13 | |
58 | #define NSControlStateValueOn NSOnState | |
59 | #define NSControlStateValueOff NSOffState | |
60 | #endif | |
3b46e624 | 61 | |
c304f7e2 | 62 | //#define DEBUG |
3b46e624 | 63 | |
c304f7e2 TS |
64 | #ifdef DEBUG |
65 | #define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); } | |
b29169d2 | 66 | #else |
c304f7e2 | 67 | #define COCOA_DEBUG(...) ((void) 0) |
b29169d2 BS |
68 | #endif |
69 | ||
c304f7e2 | 70 | #define cgrect(nsrect) (*(CGRect *)&(nsrect)) |
5b0753e0 | 71 | |
c304f7e2 TS |
72 | typedef struct { |
73 | int width; | |
74 | int height; | |
c304f7e2 TS |
75 | } QEMUScreen; |
76 | ||
cc7859c3 AO |
77 | static void cocoa_update(DisplayChangeListener *dcl, |
78 | int x, int y, int w, int h); | |
79 | ||
80 | static void cocoa_switch(DisplayChangeListener *dcl, | |
81 | DisplaySurface *surface); | |
82 | ||
83 | static void cocoa_refresh(DisplayChangeListener *dcl); | |
84 | ||
cb823408 | 85 | static NSWindow *normalWindow, *about_window; |
cc7859c3 AO |
86 | static const DisplayChangeListenerOps dcl_ops = { |
87 | .dpy_name = "cocoa", | |
88 | .dpy_gfx_update = cocoa_update, | |
89 | .dpy_gfx_switch = cocoa_switch, | |
90 | .dpy_refresh = cocoa_refresh, | |
91 | }; | |
92 | static DisplayChangeListener dcl = { | |
93 | .ops = &dcl_ops, | |
94 | }; | |
21bae11a | 95 | static int last_buttons; |
3487da6a | 96 | static int cursor_hide = 1; |
c304f7e2 | 97 | |
cb823408 AO |
98 | static int gArgc; |
99 | static char **gArgv; | |
100 | static bool stretch_video; | |
101 | static NSTextField *pauseLabel; | |
102 | static NSArray * supportedImageFileTypes; | |
5b0753e0 | 103 | |
5588840f PM |
104 | static QemuSemaphore display_init_sem; |
105 | static QemuSemaphore app_started_sem; | |
dff742ad | 106 | static bool allow_events; |
5588840f | 107 | |
60105d7a | 108 | // Utility functions to run specified code block with iothread lock held |
31819e95 | 109 | typedef void (^CodeBlock)(void); |
60105d7a | 110 | typedef bool (^BoolCodeBlock)(void); |
31819e95 PM |
111 | |
112 | static void with_iothread_lock(CodeBlock block) | |
113 | { | |
114 | bool locked = qemu_mutex_iothread_locked(); | |
115 | if (!locked) { | |
116 | qemu_mutex_lock_iothread(); | |
117 | } | |
118 | block(); | |
119 | if (!locked) { | |
120 | qemu_mutex_unlock_iothread(); | |
121 | } | |
122 | } | |
123 | ||
60105d7a PM |
124 | static bool bool_with_iothread_lock(BoolCodeBlock block) |
125 | { | |
126 | bool locked = qemu_mutex_iothread_locked(); | |
127 | bool val; | |
128 | ||
129 | if (!locked) { | |
130 | qemu_mutex_lock_iothread(); | |
131 | } | |
132 | val = block(); | |
133 | if (!locked) { | |
134 | qemu_mutex_unlock_iothread(); | |
135 | } | |
136 | return val; | |
137 | } | |
138 | ||
aaac714f | 139 | // Mac to QKeyCode conversion |
cb823408 | 140 | static const int mac_to_qkeycode_map[] = { |
aaac714f JA |
141 | [kVK_ANSI_A] = Q_KEY_CODE_A, |
142 | [kVK_ANSI_B] = Q_KEY_CODE_B, | |
143 | [kVK_ANSI_C] = Q_KEY_CODE_C, | |
144 | [kVK_ANSI_D] = Q_KEY_CODE_D, | |
145 | [kVK_ANSI_E] = Q_KEY_CODE_E, | |
146 | [kVK_ANSI_F] = Q_KEY_CODE_F, | |
147 | [kVK_ANSI_G] = Q_KEY_CODE_G, | |
148 | [kVK_ANSI_H] = Q_KEY_CODE_H, | |
149 | [kVK_ANSI_I] = Q_KEY_CODE_I, | |
150 | [kVK_ANSI_J] = Q_KEY_CODE_J, | |
151 | [kVK_ANSI_K] = Q_KEY_CODE_K, | |
152 | [kVK_ANSI_L] = Q_KEY_CODE_L, | |
153 | [kVK_ANSI_M] = Q_KEY_CODE_M, | |
154 | [kVK_ANSI_N] = Q_KEY_CODE_N, | |
155 | [kVK_ANSI_O] = Q_KEY_CODE_O, | |
156 | [kVK_ANSI_P] = Q_KEY_CODE_P, | |
157 | [kVK_ANSI_Q] = Q_KEY_CODE_Q, | |
158 | [kVK_ANSI_R] = Q_KEY_CODE_R, | |
159 | [kVK_ANSI_S] = Q_KEY_CODE_S, | |
160 | [kVK_ANSI_T] = Q_KEY_CODE_T, | |
161 | [kVK_ANSI_U] = Q_KEY_CODE_U, | |
162 | [kVK_ANSI_V] = Q_KEY_CODE_V, | |
163 | [kVK_ANSI_W] = Q_KEY_CODE_W, | |
164 | [kVK_ANSI_X] = Q_KEY_CODE_X, | |
165 | [kVK_ANSI_Y] = Q_KEY_CODE_Y, | |
166 | [kVK_ANSI_Z] = Q_KEY_CODE_Z, | |
167 | ||
168 | [kVK_ANSI_0] = Q_KEY_CODE_0, | |
169 | [kVK_ANSI_1] = Q_KEY_CODE_1, | |
170 | [kVK_ANSI_2] = Q_KEY_CODE_2, | |
171 | [kVK_ANSI_3] = Q_KEY_CODE_3, | |
172 | [kVK_ANSI_4] = Q_KEY_CODE_4, | |
173 | [kVK_ANSI_5] = Q_KEY_CODE_5, | |
174 | [kVK_ANSI_6] = Q_KEY_CODE_6, | |
175 | [kVK_ANSI_7] = Q_KEY_CODE_7, | |
176 | [kVK_ANSI_8] = Q_KEY_CODE_8, | |
177 | [kVK_ANSI_9] = Q_KEY_CODE_9, | |
178 | ||
179 | [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT, | |
180 | [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS, | |
181 | [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL, | |
182 | [kVK_Delete] = Q_KEY_CODE_BACKSPACE, | |
183 | [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK, | |
184 | [kVK_Tab] = Q_KEY_CODE_TAB, | |
185 | [kVK_Return] = Q_KEY_CODE_RET, | |
186 | [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT, | |
187 | [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT, | |
188 | [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH, | |
189 | [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON, | |
190 | [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE, | |
191 | [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA, | |
192 | [kVK_ANSI_Period] = Q_KEY_CODE_DOT, | |
193 | [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH, | |
aaac714f JA |
194 | [kVK_Space] = Q_KEY_CODE_SPC, |
195 | ||
196 | [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0, | |
197 | [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1, | |
198 | [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2, | |
199 | [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3, | |
200 | [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4, | |
201 | [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5, | |
202 | [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6, | |
203 | [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7, | |
204 | [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8, | |
205 | [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9, | |
206 | [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL, | |
207 | [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER, | |
208 | [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD, | |
209 | [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT, | |
210 | [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY, | |
211 | [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE, | |
212 | [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS, | |
213 | [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK, | |
214 | ||
215 | [kVK_UpArrow] = Q_KEY_CODE_UP, | |
216 | [kVK_DownArrow] = Q_KEY_CODE_DOWN, | |
217 | [kVK_LeftArrow] = Q_KEY_CODE_LEFT, | |
218 | [kVK_RightArrow] = Q_KEY_CODE_RIGHT, | |
219 | ||
220 | [kVK_Help] = Q_KEY_CODE_INSERT, | |
221 | [kVK_Home] = Q_KEY_CODE_HOME, | |
222 | [kVK_PageUp] = Q_KEY_CODE_PGUP, | |
223 | [kVK_PageDown] = Q_KEY_CODE_PGDN, | |
224 | [kVK_End] = Q_KEY_CODE_END, | |
225 | [kVK_ForwardDelete] = Q_KEY_CODE_DELETE, | |
226 | ||
227 | [kVK_Escape] = Q_KEY_CODE_ESC, | |
228 | ||
229 | /* The Power key can't be used directly because the operating system uses | |
230 | * it. This key can be emulated by using it in place of another key such as | |
231 | * F1. Don't forget to disable the real key binding. | |
232 | */ | |
233 | /* [kVK_F1] = Q_KEY_CODE_POWER, */ | |
234 | ||
235 | [kVK_F1] = Q_KEY_CODE_F1, | |
236 | [kVK_F2] = Q_KEY_CODE_F2, | |
237 | [kVK_F3] = Q_KEY_CODE_F3, | |
238 | [kVK_F4] = Q_KEY_CODE_F4, | |
239 | [kVK_F5] = Q_KEY_CODE_F5, | |
240 | [kVK_F6] = Q_KEY_CODE_F6, | |
241 | [kVK_F7] = Q_KEY_CODE_F7, | |
242 | [kVK_F8] = Q_KEY_CODE_F8, | |
243 | [kVK_F9] = Q_KEY_CODE_F9, | |
244 | [kVK_F10] = Q_KEY_CODE_F10, | |
245 | [kVK_F11] = Q_KEY_CODE_F11, | |
246 | [kVK_F12] = Q_KEY_CODE_F12, | |
247 | [kVK_F13] = Q_KEY_CODE_PRINT, | |
248 | [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK, | |
249 | [kVK_F15] = Q_KEY_CODE_PAUSE, | |
250 | ||
708b7255 AO |
251 | // JIS keyboards only |
252 | [kVK_JIS_Yen] = Q_KEY_CODE_YEN, | |
253 | [kVK_JIS_Underscore] = Q_KEY_CODE_RO, | |
254 | [kVK_JIS_KeypadComma] = Q_KEY_CODE_KP_COMMA, | |
255 | [kVK_JIS_Eisu] = Q_KEY_CODE_MUHENKAN, | |
256 | [kVK_JIS_Kana] = Q_KEY_CODE_HENKAN, | |
257 | ||
aaac714f JA |
258 | /* |
259 | * The eject and volume keys can't be used here because they are handled at | |
260 | * a lower level than what an Application can see. | |
261 | */ | |
5b0753e0 FB |
262 | }; |
263 | ||
77047bb7 | 264 | static int cocoa_keycode_to_qemu(int keycode) |
5b0753e0 | 265 | { |
aaac714f | 266 | if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) { |
4313739a | 267 | error_report("(cocoa) warning unknown keycode 0x%x", keycode); |
5b0753e0 FB |
268 | return 0; |
269 | } | |
aaac714f | 270 | return mac_to_qkeycode_map[keycode]; |
5b0753e0 FB |
271 | } |
272 | ||
693a3e01 JA |
273 | /* Displays an alert dialog box with the specified message */ |
274 | static void QEMU_Alert(NSString *message) | |
275 | { | |
276 | NSAlert *alert; | |
277 | alert = [NSAlert new]; | |
278 | [alert setMessageText: message]; | |
279 | [alert runModal]; | |
280 | } | |
c304f7e2 | 281 | |
693a3e01 JA |
282 | /* Handles any errors that happen with a device transaction */ |
283 | static void handleAnyDeviceErrors(Error * err) | |
284 | { | |
285 | if (err) { | |
286 | QEMU_Alert([NSString stringWithCString: error_get_pretty(err) | |
287 | encoding: NSASCIIStringEncoding]); | |
288 | error_free(err); | |
289 | } | |
290 | } | |
c304f7e2 | 291 | |
5b0753e0 FB |
292 | /* |
293 | ------------------------------------------------------ | |
c304f7e2 | 294 | QemuCocoaView |
5b0753e0 FB |
295 | ------------------------------------------------------ |
296 | */ | |
c304f7e2 | 297 | @interface QemuCocoaView : NSView |
5b0753e0 | 298 | { |
c304f7e2 TS |
299 | QEMUScreen screen; |
300 | NSWindow *fullScreenWindow; | |
301 | float cx,cy,cw,ch,cdx,cdy; | |
5588840f | 302 | pixman_image_t *pixman_image; |
6d73bb64 | 303 | QKbdState *kbd; |
49b9bd4d | 304 | BOOL isMouseGrabbed; |
c304f7e2 TS |
305 | BOOL isFullscreen; |
306 | BOOL isAbsoluteEnabled; | |
c304f7e2 | 307 | } |
72a3e316 | 308 | - (void) switchSurface:(pixman_image_t *)image; |
c304f7e2 TS |
309 | - (void) grabMouse; |
310 | - (void) ungrabMouse; | |
311 | - (void) toggleFullScreen:(id)sender; | |
9c3a418e | 312 | - (void) handleMonitorInput:(NSEvent *)event; |
60105d7a PM |
313 | - (bool) handleEvent:(NSEvent *)event; |
314 | - (bool) handleEventLocked:(NSEvent *)event; | |
c304f7e2 | 315 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; |
f61c387e PM |
316 | /* The state surrounding mouse grabbing is potentially confusing. |
317 | * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated | |
318 | * pointing device an absolute-position one?"], but is only updated on | |
319 | * next refresh. | |
320 | * isMouseGrabbed tracks whether GUI events are directed to the guest; | |
321 | * it controls whether special keys like Cmd get sent to the guest, | |
322 | * and whether we capture the mouse when in non-absolute mode. | |
f61c387e | 323 | */ |
49b9bd4d | 324 | - (BOOL) isMouseGrabbed; |
c304f7e2 TS |
325 | - (BOOL) isAbsoluteEnabled; |
326 | - (float) cdx; | |
327 | - (float) cdy; | |
328 | - (QEMUScreen) gscreen; | |
3b178b71 | 329 | - (void) raiseAllKeys; |
c304f7e2 | 330 | @end |
3b46e624 | 331 | |
7fee199c AF |
332 | QemuCocoaView *cocoaView; |
333 | ||
c304f7e2 TS |
334 | @implementation QemuCocoaView |
335 | - (id)initWithFrame:(NSRect)frameRect | |
336 | { | |
337 | COCOA_DEBUG("QemuCocoaView: initWithFrame\n"); | |
3b46e624 | 338 | |
c304f7e2 TS |
339 | self = [super initWithFrame:frameRect]; |
340 | if (self) { | |
3b46e624 | 341 | |
c304f7e2 TS |
342 | screen.width = frameRect.size.width; |
343 | screen.height = frameRect.size.height; | |
6d73bb64 | 344 | kbd = qkbd_state_init(dcl.con); |
3b46e624 | 345 | |
c304f7e2 TS |
346 | } |
347 | return self; | |
348 | } | |
3b46e624 | 349 | |
c304f7e2 TS |
350 | - (void) dealloc |
351 | { | |
352 | COCOA_DEBUG("QemuCocoaView: dealloc\n"); | |
3b46e624 | 353 | |
c0ff29d1 | 354 | if (pixman_image) { |
5588840f PM |
355 | pixman_image_unref(pixman_image); |
356 | } | |
3b46e624 | 357 | |
6d73bb64 | 358 | qkbd_state_free(kbd); |
c304f7e2 TS |
359 | [super dealloc]; |
360 | } | |
3b46e624 | 361 | |
d50f71dc AF |
362 | - (BOOL) isOpaque |
363 | { | |
364 | return YES; | |
365 | } | |
366 | ||
5dd45bee PM |
367 | - (BOOL) screenContainsPoint:(NSPoint) p |
368 | { | |
369 | return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height); | |
370 | } | |
371 | ||
2044dff8 CZ |
372 | /* Get location of event and convert to virtual screen coordinate */ |
373 | - (CGPoint) screenLocationOfEvent:(NSEvent *)ev | |
374 | { | |
375 | NSWindow *eventWindow = [ev window]; | |
376 | // XXX: Use CGRect and -convertRectFromScreen: to support macOS 10.10 | |
377 | CGRect r = CGRectZero; | |
378 | r.origin = [ev locationInWindow]; | |
379 | if (!eventWindow) { | |
380 | if (!isFullscreen) { | |
381 | return [[self window] convertRectFromScreen:r].origin; | |
382 | } else { | |
383 | CGPoint locationInSelfWindow = [[self window] convertRectFromScreen:r].origin; | |
384 | CGPoint loc = [self convertPoint:locationInSelfWindow fromView:nil]; | |
385 | if (stretch_video) { | |
386 | loc.x /= cdx; | |
387 | loc.y /= cdy; | |
388 | } | |
389 | return loc; | |
390 | } | |
391 | } else if ([[self window] isEqual:eventWindow]) { | |
392 | if (!isFullscreen) { | |
393 | return r.origin; | |
394 | } else { | |
395 | CGPoint loc = [self convertPoint:r.origin fromView:nil]; | |
396 | if (stretch_video) { | |
397 | loc.x /= cdx; | |
398 | loc.y /= cdy; | |
399 | } | |
400 | return loc; | |
401 | } | |
402 | } else { | |
403 | return [[self window] convertRectFromScreen:[eventWindow convertRectToScreen:r]].origin; | |
404 | } | |
405 | } | |
406 | ||
13aefd30 PM |
407 | - (void) hideCursor |
408 | { | |
409 | if (!cursor_hide) { | |
410 | return; | |
411 | } | |
412 | [NSCursor hide]; | |
413 | } | |
414 | ||
415 | - (void) unhideCursor | |
416 | { | |
417 | if (!cursor_hide) { | |
418 | return; | |
419 | } | |
420 | [NSCursor unhide]; | |
421 | } | |
422 | ||
c304f7e2 TS |
423 | - (void) drawRect:(NSRect) rect |
424 | { | |
425 | COCOA_DEBUG("QemuCocoaView: drawRect\n"); | |
426 | ||
c304f7e2 | 427 | // get CoreGraphic context |
5e24600a | 428 | CGContextRef viewContextRef = [[NSGraphicsContext currentContext] CGContext]; |
5e24600a | 429 | |
c304f7e2 TS |
430 | CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone); |
431 | CGContextSetShouldAntialias (viewContextRef, NO); | |
432 | ||
433 | // draw screen bitmap directly to Core Graphics context | |
c0ff29d1 | 434 | if (!pixman_image) { |
7d270b1c PM |
435 | // Draw request before any guest device has set up a framebuffer: |
436 | // just draw an opaque black rectangle | |
437 | CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0); | |
438 | CGContextFillRect(viewContextRef, NSRectToCGRect(rect)); | |
439 | } else { | |
c0ff29d1 AO |
440 | int w = pixman_image_get_width(pixman_image); |
441 | int h = pixman_image_get_height(pixman_image); | |
442 | int bitsPerPixel = PIXMAN_FORMAT_BPP(pixman_image_get_format(pixman_image)); | |
d9c32b8f | 443 | int stride = pixman_image_get_stride(pixman_image); |
c0ff29d1 AO |
444 | CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData( |
445 | NULL, | |
446 | pixman_image_get_data(pixman_image), | |
d9c32b8f | 447 | stride * h, |
c0ff29d1 AO |
448 | NULL |
449 | ); | |
c304f7e2 | 450 | CGImageRef imageRef = CGImageCreate( |
c0ff29d1 AO |
451 | w, //width |
452 | h, //height | |
d9c32b8f | 453 | DIV_ROUND_UP(bitsPerPixel, 8) * 2, //bitsPerComponent |
c0ff29d1 | 454 | bitsPerPixel, //bitsPerPixel |
d9c32b8f | 455 | stride, //bytesPerRow |
ae57d35c AO |
456 | CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace |
457 | kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, //bitmapInfo | |
c304f7e2 TS |
458 | dataProviderRef, //provider |
459 | NULL, //decode | |
460 | 0, //interpolate | |
461 | kCGRenderingIntentDefault //intent | |
462 | ); | |
b63901d8 PM |
463 | // selective drawing code (draws only dirty rectangles) (OS X >= 10.4) |
464 | const NSRect *rectList; | |
b63901d8 | 465 | NSInteger rectCount; |
b63901d8 PM |
466 | int i; |
467 | CGImageRef clipImageRef; | |
468 | CGRect clipRect; | |
469 | ||
470 | [self getRectsBeingDrawn:&rectList count:&rectCount]; | |
471 | for (i = 0; i < rectCount; i++) { | |
472 | clipRect.origin.x = rectList[i].origin.x / cdx; | |
c0ff29d1 | 473 | clipRect.origin.y = (float)h - (rectList[i].origin.y + rectList[i].size.height) / cdy; |
b63901d8 PM |
474 | clipRect.size.width = rectList[i].size.width / cdx; |
475 | clipRect.size.height = rectList[i].size.height / cdy; | |
476 | clipImageRef = CGImageCreateWithImageInRect( | |
477 | imageRef, | |
478 | clipRect | |
479 | ); | |
480 | CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef); | |
481 | CGImageRelease (clipImageRef); | |
5b0753e0 | 482 | } |
c304f7e2 | 483 | CGImageRelease (imageRef); |
c0ff29d1 | 484 | CGDataProviderRelease(dataProviderRef); |
c304f7e2 | 485 | } |
5b0753e0 FB |
486 | } |
487 | ||
c304f7e2 | 488 | - (void) setContentDimensions |
5b0753e0 | 489 | { |
c304f7e2 TS |
490 | COCOA_DEBUG("QemuCocoaView: setContentDimensions\n"); |
491 | ||
492 | if (isFullscreen) { | |
493 | cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width; | |
494 | cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height; | |
5d1b2eef JA |
495 | |
496 | /* stretches video, but keeps same aspect ratio */ | |
497 | if (stretch_video == true) { | |
498 | /* use smallest stretch value - prevents clipping on sides */ | |
499 | if (MIN(cdx, cdy) == cdx) { | |
500 | cdy = cdx; | |
501 | } else { | |
502 | cdx = cdy; | |
503 | } | |
504 | } else { /* No stretching */ | |
505 | cdx = cdy = 1; | |
506 | } | |
c304f7e2 TS |
507 | cw = screen.width * cdx; |
508 | ch = screen.height * cdy; | |
509 | cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0; | |
510 | cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0; | |
511 | } else { | |
512 | cx = 0; | |
513 | cy = 0; | |
514 | cw = screen.width; | |
515 | ch = screen.height; | |
516 | cdx = 1.0; | |
517 | cdy = 1.0; | |
518 | } | |
5b0753e0 FB |
519 | } |
520 | ||
72a3e316 | 521 | - (void) switchSurface:(pixman_image_t *)image |
5b0753e0 | 522 | { |
5e00d3ac | 523 | COCOA_DEBUG("QemuCocoaView: switchSurface\n"); |
c304f7e2 | 524 | |
72a3e316 PM |
525 | int w = pixman_image_get_width(image); |
526 | int h = pixman_image_get_height(image); | |
381600da PM |
527 | /* cdx == 0 means this is our very first surface, in which case we need |
528 | * to recalculate the content dimensions even if it happens to be the size | |
529 | * of the initial empty window. | |
530 | */ | |
531 | bool isResize = (w != screen.width || h != screen.height || cdx == 0.0); | |
d3345a04 PM |
532 | |
533 | int oldh = screen.height; | |
534 | if (isResize) { | |
535 | // Resize before we trigger the redraw, or we'll redraw at the wrong size | |
536 | COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h); | |
537 | screen.width = w; | |
538 | screen.height = h; | |
539 | [self setContentDimensions]; | |
540 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; | |
541 | } | |
8510d91e | 542 | |
c304f7e2 | 543 | // update screenBuffer |
c0ff29d1 | 544 | if (pixman_image) { |
5588840f PM |
545 | pixman_image_unref(pixman_image); |
546 | } | |
3b46e624 | 547 | |
5588840f | 548 | pixman_image = image; |
3b46e624 | 549 | |
c304f7e2 TS |
550 | // update windows |
551 | if (isFullscreen) { | |
552 | [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]]; | |
d3345a04 | 553 | [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 |
554 | } else { |
555 | if (qemu_name) | |
556 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; | |
d3345a04 PM |
557 | [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:YES animate:NO]; |
558 | } | |
559 | ||
560 | if (isResize) { | |
561 | [normalWindow center]; | |
c304f7e2 | 562 | } |
5b0753e0 FB |
563 | } |
564 | ||
c304f7e2 TS |
565 | - (void) toggleFullScreen:(id)sender |
566 | { | |
567 | COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n"); | |
568 | ||
569 | if (isFullscreen) { // switch from fullscreen to desktop | |
570 | isFullscreen = FALSE; | |
571 | [self ungrabMouse]; | |
572 | [self setContentDimensions]; | |
1e8b6f2b AO |
573 | [fullScreenWindow close]; |
574 | [normalWindow setContentView: self]; | |
575 | [normalWindow makeKeyAndOrderFront: self]; | |
576 | [NSMenu setMenuBarVisible:YES]; | |
c304f7e2 TS |
577 | } else { // switch from desktop to fullscreen |
578 | isFullscreen = TRUE; | |
5d1b2eef | 579 | [normalWindow orderOut: nil]; /* Hide the window */ |
c304f7e2 TS |
580 | [self grabMouse]; |
581 | [self setContentDimensions]; | |
1e8b6f2b AO |
582 | [NSMenu setMenuBarVisible:NO]; |
583 | fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] | |
584 | styleMask:NSWindowStyleMaskBorderless | |
585 | backing:NSBackingStoreBuffered | |
586 | defer:NO]; | |
587 | [fullScreenWindow setAcceptsMouseMovedEvents: YES]; | |
588 | [fullScreenWindow setHasShadow:NO]; | |
589 | [fullScreenWindow setBackgroundColor: [NSColor blackColor]]; | |
590 | [self setFrame:NSMakeRect(cx, cy, cw, ch)]; | |
591 | [[fullScreenWindow contentView] addSubview: self]; | |
592 | [fullScreenWindow makeKeyAndOrderFront:self]; | |
c304f7e2 TS |
593 | } |
594 | } | |
5b0753e0 | 595 | |
6d73bb64 AO |
596 | - (void) toggleKey: (int)keycode { |
597 | qkbd_state_key_event(kbd, keycode, !qkbd_state_key_get(kbd, keycode)); | |
af8862b2 IM |
598 | } |
599 | ||
9c3a418e JA |
600 | // Does the work of sending input to the monitor |
601 | - (void) handleMonitorInput:(NSEvent *)event | |
602 | { | |
603 | int keysym = 0; | |
604 | int control_key = 0; | |
605 | ||
606 | // if the control key is down | |
607 | if ([event modifierFlags] & NSEventModifierFlagControl) { | |
608 | control_key = 1; | |
609 | } | |
610 | ||
611 | /* translates Macintosh keycodes to QEMU's keysym */ | |
612 | ||
613 | int without_control_translation[] = { | |
614 | [0 ... 0xff] = 0, // invalid key | |
615 | ||
616 | [kVK_UpArrow] = QEMU_KEY_UP, | |
617 | [kVK_DownArrow] = QEMU_KEY_DOWN, | |
618 | [kVK_RightArrow] = QEMU_KEY_RIGHT, | |
619 | [kVK_LeftArrow] = QEMU_KEY_LEFT, | |
620 | [kVK_Home] = QEMU_KEY_HOME, | |
621 | [kVK_End] = QEMU_KEY_END, | |
622 | [kVK_PageUp] = QEMU_KEY_PAGEUP, | |
623 | [kVK_PageDown] = QEMU_KEY_PAGEDOWN, | |
624 | [kVK_ForwardDelete] = QEMU_KEY_DELETE, | |
625 | [kVK_Delete] = QEMU_KEY_BACKSPACE, | |
626 | }; | |
627 | ||
628 | int with_control_translation[] = { | |
629 | [0 ... 0xff] = 0, // invalid key | |
630 | ||
631 | [kVK_UpArrow] = QEMU_KEY_CTRL_UP, | |
632 | [kVK_DownArrow] = QEMU_KEY_CTRL_DOWN, | |
633 | [kVK_RightArrow] = QEMU_KEY_CTRL_RIGHT, | |
634 | [kVK_LeftArrow] = QEMU_KEY_CTRL_LEFT, | |
635 | [kVK_Home] = QEMU_KEY_CTRL_HOME, | |
636 | [kVK_End] = QEMU_KEY_CTRL_END, | |
637 | [kVK_PageUp] = QEMU_KEY_CTRL_PAGEUP, | |
638 | [kVK_PageDown] = QEMU_KEY_CTRL_PAGEDOWN, | |
639 | }; | |
640 | ||
641 | if (control_key != 0) { /* If the control key is being used */ | |
642 | if ([event keyCode] < ARRAY_SIZE(with_control_translation)) { | |
643 | keysym = with_control_translation[[event keyCode]]; | |
644 | } | |
645 | } else { | |
646 | if ([event keyCode] < ARRAY_SIZE(without_control_translation)) { | |
647 | keysym = without_control_translation[[event keyCode]]; | |
648 | } | |
649 | } | |
650 | ||
651 | // if not a key that needs translating | |
652 | if (keysym == 0) { | |
653 | NSString *ks = [event characters]; | |
654 | if ([ks length] > 0) { | |
655 | keysym = [ks characterAtIndex:0]; | |
656 | } | |
657 | } | |
658 | ||
659 | if (keysym) { | |
660 | kbd_put_keysym(keysym); | |
661 | } | |
662 | } | |
663 | ||
60105d7a | 664 | - (bool) handleEvent:(NSEvent *)event |
3b46e624 | 665 | { |
dff742ad HN |
666 | if(!allow_events) { |
667 | /* | |
668 | * Just let OSX have all events that arrive before | |
669 | * applicationDidFinishLaunching. | |
670 | * This avoids a deadlock on the iothread lock, which cocoa_display_init() | |
671 | * will not drop until after the app_started_sem is posted. (In theory | |
672 | * there should not be any such events, but OSX Catalina now emits some.) | |
673 | */ | |
674 | return false; | |
675 | } | |
60105d7a PM |
676 | return bool_with_iothread_lock(^{ |
677 | return [self handleEventLocked:event]; | |
31819e95 PM |
678 | }); |
679 | } | |
c304f7e2 | 680 | |
60105d7a | 681 | - (bool) handleEventLocked:(NSEvent *)event |
31819e95 | 682 | { |
60105d7a | 683 | /* Return true if we handled the event, false if it should be given to OSX */ |
31819e95 | 684 | COCOA_DEBUG("QemuCocoaView: handleEvent\n"); |
c304f7e2 | 685 | int buttons = 0; |
af8862b2 | 686 | int keycode = 0; |
21bae11a | 687 | bool mouse_event = false; |
0c6c4395 | 688 | static bool switched_to_fullscreen = false; |
2044dff8 CZ |
689 | // Location of event in virtual screen coordinates |
690 | NSPoint p = [self screenLocationOfEvent:event]; | |
6d73bb64 AO |
691 | NSUInteger modifiers = [event modifierFlags]; |
692 | ||
ad7f2f8e AO |
693 | /* |
694 | * Check -[NSEvent modifierFlags] here. | |
695 | * | |
696 | * There is a NSEventType for an event notifying the change of | |
697 | * -[NSEvent modifierFlags], NSEventTypeFlagsChanged but these operations | |
698 | * are performed for any events because a modifier state may change while | |
699 | * the application is inactive (i.e. no events fire) and we don't want to | |
700 | * wait for another modifier state change to detect such a change. | |
701 | * | |
702 | * NSEventModifierFlagCapsLock requires a special treatment. The other flags | |
703 | * are handled in similar manners. | |
704 | * | |
705 | * NSEventModifierFlagCapsLock | |
706 | * --------------------------- | |
707 | * | |
708 | * If CapsLock state is changed, "up" and "down" events will be fired in | |
709 | * sequence, effectively updates CapsLock state on the guest. | |
710 | * | |
711 | * The other flags | |
712 | * --------------- | |
713 | * | |
714 | * If a flag is not set, fire "up" events for all keys which correspond to | |
715 | * the flag. Note that "down" events are not fired here because the flags | |
716 | * checked here do not tell what exact keys are down. | |
717 | * | |
718 | * If one of the keys corresponding to a flag is down, we rely on | |
719 | * -[NSEvent keyCode] of an event whose -[NSEvent type] is | |
720 | * NSEventTypeFlagsChanged to know the exact key which is down, which has | |
721 | * the following two downsides: | |
722 | * - It does not work when the application is inactive as described above. | |
723 | * - It malfactions *after* the modifier state is changed while the | |
724 | * application is inactive. It is because -[NSEvent keyCode] does not tell | |
725 | * if the key is up or down, and requires to infer the current state from | |
726 | * the previous state. It is still possible to fix such a malfanction by | |
727 | * completely leaving your hands from the keyboard, which hopefully makes | |
728 | * this implementation usable enough. | |
729 | */ | |
6d73bb64 AO |
730 | if (!!(modifiers & NSEventModifierFlagCapsLock) != |
731 | qkbd_state_modifier_get(kbd, QKBD_MOD_CAPSLOCK)) { | |
732 | qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, true); | |
733 | qkbd_state_key_event(kbd, Q_KEY_CODE_CAPS_LOCK, false); | |
734 | } | |
735 | ||
736 | if (!(modifiers & NSEventModifierFlagShift)) { | |
737 | qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT, false); | |
738 | qkbd_state_key_event(kbd, Q_KEY_CODE_SHIFT_R, false); | |
739 | } | |
740 | if (!(modifiers & NSEventModifierFlagControl)) { | |
741 | qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL, false); | |
742 | qkbd_state_key_event(kbd, Q_KEY_CODE_CTRL_R, false); | |
743 | } | |
744 | if (!(modifiers & NSEventModifierFlagOption)) { | |
745 | qkbd_state_key_event(kbd, Q_KEY_CODE_ALT, false); | |
746 | qkbd_state_key_event(kbd, Q_KEY_CODE_ALT_R, false); | |
747 | } | |
748 | if (!(modifiers & NSEventModifierFlagCommand)) { | |
749 | qkbd_state_key_event(kbd, Q_KEY_CODE_META_L, false); | |
750 | qkbd_state_key_event(kbd, Q_KEY_CODE_META_R, false); | |
751 | } | |
c304f7e2 TS |
752 | |
753 | switch ([event type]) { | |
4ba967ad | 754 | case NSEventTypeFlagsChanged: |
6d73bb64 AO |
755 | switch ([event keyCode]) { |
756 | case kVK_Shift: | |
757 | if (!!(modifiers & NSEventModifierFlagShift)) { | |
758 | [self toggleKey:Q_KEY_CODE_SHIFT]; | |
759 | } | |
760 | break; | |
af8862b2 | 761 | |
6d73bb64 AO |
762 | case kVK_RightShift: |
763 | if (!!(modifiers & NSEventModifierFlagShift)) { | |
764 | [self toggleKey:Q_KEY_CODE_SHIFT_R]; | |
af8862b2 | 765 | } |
6d73bb64 AO |
766 | break; |
767 | ||
768 | case kVK_Control: | |
769 | if (!!(modifiers & NSEventModifierFlagControl)) { | |
770 | [self toggleKey:Q_KEY_CODE_CTRL]; | |
af8862b2 | 771 | } |
6d73bb64 AO |
772 | break; |
773 | ||
774 | case kVK_RightControl: | |
775 | if (!!(modifiers & NSEventModifierFlagControl)) { | |
776 | [self toggleKey:Q_KEY_CODE_CTRL_R]; | |
af8862b2 | 777 | } |
6d73bb64 AO |
778 | break; |
779 | ||
780 | case kVK_Option: | |
781 | if (!!(modifiers & NSEventModifierFlagOption)) { | |
782 | [self toggleKey:Q_KEY_CODE_ALT]; | |
af8862b2 | 783 | } |
6d73bb64 AO |
784 | break; |
785 | ||
786 | case kVK_RightOption: | |
787 | if (!!(modifiers & NSEventModifierFlagOption)) { | |
788 | [self toggleKey:Q_KEY_CODE_ALT_R]; | |
af8862b2 | 789 | } |
6d73bb64 | 790 | break; |
8895919a | 791 | |
6d73bb64 AO |
792 | /* Don't pass command key changes to guest unless mouse is grabbed */ |
793 | case kVK_Command: | |
794 | if (isMouseGrabbed && | |
795 | !!(modifiers & NSEventModifierFlagCommand)) { | |
796 | [self toggleKey:Q_KEY_CODE_META_L]; | |
797 | } | |
798 | break; | |
8895919a | 799 | |
6d73bb64 AO |
800 | case kVK_RightCommand: |
801 | if (isMouseGrabbed && | |
802 | !!(modifiers & NSEventModifierFlagCommand)) { | |
803 | [self toggleKey:Q_KEY_CODE_META_R]; | |
0c6c4395 | 804 | } |
6d73bb64 | 805 | break; |
c304f7e2 | 806 | } |
c304f7e2 | 807 | break; |
4ba967ad | 808 | case NSEventTypeKeyDown: |
8895919a | 809 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
3b46e624 | 810 | |
8895919a | 811 | // forward command key combos to the host UI unless the mouse is grabbed |
4ba967ad | 812 | if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) { |
0c6c4395 JA |
813 | /* |
814 | * Prevent the command key from being stuck down in the guest | |
815 | * when using Command-F to switch to full screen mode. | |
816 | */ | |
817 | if (keycode == Q_KEY_CODE_F) { | |
818 | switched_to_fullscreen = true; | |
819 | } | |
60105d7a | 820 | return false; |
c304f7e2 | 821 | } |
3b46e624 | 822 | |
c304f7e2 | 823 | // default |
c304f7e2 | 824 | |
5929e36c | 825 | // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU) |
4ba967ad | 826 | if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) { |
5929e36c JA |
827 | NSString *keychar = [event charactersIgnoringModifiers]; |
828 | if ([keychar length] == 1) { | |
829 | char key = [keychar characterAtIndex:0]; | |
830 | switch (key) { | |
831 | ||
832 | // enable graphic console | |
833 | case '1' ... '9': | |
834 | console_select(key - '0' - 1); /* ascii math */ | |
60105d7a | 835 | return true; |
5929e36c JA |
836 | |
837 | // release the mouse grab | |
838 | case 'g': | |
839 | [self ungrabMouse]; | |
60105d7a | 840 | return true; |
5929e36c | 841 | } |
c304f7e2 | 842 | } |
ef2088f9 | 843 | } |
c304f7e2 | 844 | |
ef2088f9 | 845 | if (qemu_console_is_graphic(NULL)) { |
6d73bb64 | 846 | qkbd_state_key_event(kbd, keycode, true); |
c304f7e2 | 847 | } else { |
9c3a418e | 848 | [self handleMonitorInput: event]; |
c304f7e2 TS |
849 | } |
850 | break; | |
4ba967ad | 851 | case NSEventTypeKeyUp: |
c304f7e2 | 852 | keycode = cocoa_keycode_to_qemu([event keyCode]); |
8895919a PM |
853 | |
854 | // don't pass the guest a spurious key-up if we treated this | |
855 | // command-key combo as a host UI action | |
4ba967ad | 856 | if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) { |
60105d7a | 857 | return true; |
8895919a PM |
858 | } |
859 | ||
68c0aa6e | 860 | if (qemu_console_is_graphic(NULL)) { |
6d73bb64 | 861 | qkbd_state_key_event(kbd, keycode, false); |
c304f7e2 TS |
862 | } |
863 | break; | |
4ba967ad | 864 | case NSEventTypeMouseMoved: |
c304f7e2 | 865 | if (isAbsoluteEnabled) { |
2044dff8 CZ |
866 | // Cursor re-entered into a window might generate events bound to screen coordinates |
867 | // and `nil` window property, and in full screen mode, current window might not be | |
868 | // key window, where event location alone should suffice. | |
869 | if (![self screenContainsPoint:p] || !([[self window] isKeyWindow] || isFullscreen)) { | |
f61c387e PM |
870 | if (isMouseGrabbed) { |
871 | [self ungrabMouse]; | |
c304f7e2 TS |
872 | } |
873 | } else { | |
f61c387e PM |
874 | if (!isMouseGrabbed) { |
875 | [self grabMouse]; | |
c304f7e2 TS |
876 | } |
877 | } | |
5b0753e0 | 878 | } |
21bae11a | 879 | mouse_event = true; |
c304f7e2 | 880 | break; |
4ba967ad | 881 | case NSEventTypeLeftMouseDown: |
4295f836 | 882 | buttons |= MOUSE_EVENT_LBUTTON; |
21bae11a | 883 | mouse_event = true; |
c304f7e2 | 884 | break; |
4ba967ad | 885 | case NSEventTypeRightMouseDown: |
c304f7e2 | 886 | buttons |= MOUSE_EVENT_RBUTTON; |
21bae11a | 887 | mouse_event = true; |
c304f7e2 | 888 | break; |
4ba967ad | 889 | case NSEventTypeOtherMouseDown: |
c304f7e2 | 890 | buttons |= MOUSE_EVENT_MBUTTON; |
21bae11a | 891 | mouse_event = true; |
c304f7e2 | 892 | break; |
4ba967ad | 893 | case NSEventTypeLeftMouseDragged: |
4295f836 | 894 | buttons |= MOUSE_EVENT_LBUTTON; |
21bae11a | 895 | mouse_event = true; |
c304f7e2 | 896 | break; |
4ba967ad | 897 | case NSEventTypeRightMouseDragged: |
c304f7e2 | 898 | buttons |= MOUSE_EVENT_RBUTTON; |
21bae11a | 899 | mouse_event = true; |
c304f7e2 | 900 | break; |
4ba967ad | 901 | case NSEventTypeOtherMouseDragged: |
c304f7e2 | 902 | buttons |= MOUSE_EVENT_MBUTTON; |
21bae11a | 903 | mouse_event = true; |
c304f7e2 | 904 | break; |
4ba967ad | 905 | case NSEventTypeLeftMouseUp: |
f61c387e PM |
906 | mouse_event = true; |
907 | if (!isMouseGrabbed && [self screenContainsPoint:p]) { | |
8e23e34d CZ |
908 | /* |
909 | * In fullscreen mode, the window of cocoaView may not be the | |
910 | * key window, therefore the position relative to the virtual | |
911 | * screen alone will be sufficient. | |
912 | */ | |
913 | if(isFullscreen || [[self window] isKeyWindow]) { | |
9e8204b1 JA |
914 | [self grabMouse]; |
915 | } | |
c304f7e2 TS |
916 | } |
917 | break; | |
4ba967ad | 918 | case NSEventTypeRightMouseUp: |
21bae11a | 919 | mouse_event = true; |
c304f7e2 | 920 | break; |
4ba967ad | 921 | case NSEventTypeOtherMouseUp: |
21bae11a | 922 | mouse_event = true; |
c304f7e2 | 923 | break; |
4ba967ad | 924 | case NSEventTypeScrollWheel: |
ae7313e7 JA |
925 | /* |
926 | * Send wheel events to the guest regardless of window focus. | |
927 | * This is in-line with standard Mac OS X UI behaviour. | |
928 | */ | |
929 | ||
dc3c89d6 JA |
930 | /* |
931 | * When deltaY is zero, it means that this scrolling event was | |
932 | * either horizontal, or so fine that it only appears in | |
933 | * scrollingDeltaY. So we drop the event. | |
934 | */ | |
935 | if ([event deltaY] != 0) { | |
ae7313e7 | 936 | /* Determine if this is a scroll up or scroll down event */ |
dc3c89d6 JA |
937 | buttons = ([event deltaY] > 0) ? |
938 | INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN; | |
cc7859c3 | 939 | qemu_input_queue_btn(dcl.con, buttons, true); |
dc3c89d6 | 940 | qemu_input_event_sync(); |
cc7859c3 | 941 | qemu_input_queue_btn(dcl.con, buttons, false); |
dc3c89d6 JA |
942 | qemu_input_event_sync(); |
943 | } | |
ae7313e7 JA |
944 | /* |
945 | * Since deltaY also reports scroll wheel events we prevent mouse | |
946 | * movement code from executing. | |
947 | */ | |
948 | mouse_event = false; | |
c304f7e2 TS |
949 | break; |
950 | default: | |
60105d7a | 951 | return false; |
5b0753e0 | 952 | } |
21bae11a GH |
953 | |
954 | if (mouse_event) { | |
8d3a5d9b PM |
955 | /* Don't send button events to the guest unless we've got a |
956 | * mouse grab or window focus. If we have neither then this event | |
957 | * is the user clicking on the background window to activate and | |
958 | * bring us to the front, which will be done by the sendEvent | |
959 | * call below. We definitely don't want to pass that click through | |
960 | * to the guest. | |
961 | */ | |
962 | if ((isMouseGrabbed || [[self window] isKeyWindow]) && | |
963 | (last_buttons != buttons)) { | |
7fb1cf16 | 964 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
21bae11a GH |
965 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
966 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, | |
ae7313e7 | 967 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON |
21bae11a | 968 | }; |
cc7859c3 | 969 | qemu_input_update_buttons(dcl.con, bmap, last_buttons, buttons); |
21bae11a GH |
970 | last_buttons = buttons; |
971 | } | |
f61c387e PM |
972 | if (isMouseGrabbed) { |
973 | if (isAbsoluteEnabled) { | |
974 | /* Note that the origin for Cocoa mouse coords is bottom left, not top left. | |
975 | * The check on screenContainsPoint is to avoid sending out of range values for | |
976 | * clicks in the titlebar. | |
977 | */ | |
978 | if ([self screenContainsPoint:p]) { | |
cc7859c3 AO |
979 | qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x, 0, screen.width); |
980 | qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height); | |
f61c387e PM |
981 | } |
982 | } else { | |
cc7859c3 AO |
983 | qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, (int)[event deltaX]); |
984 | qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, (int)[event deltaY]); | |
f61c387e | 985 | } |
21bae11a | 986 | } else { |
60105d7a | 987 | return false; |
21bae11a GH |
988 | } |
989 | qemu_input_event_sync(); | |
990 | } | |
60105d7a | 991 | return true; |
5b0753e0 FB |
992 | } |
993 | ||
c304f7e2 | 994 | - (void) grabMouse |
5b0753e0 | 995 | { |
c304f7e2 | 996 | COCOA_DEBUG("QemuCocoaView: grabMouse\n"); |
3b46e624 | 997 | |
c304f7e2 TS |
998 | if (!isFullscreen) { |
999 | if (qemu_name) | |
5929e36c | 1000 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt + g to release Mouse)", qemu_name]]; |
c304f7e2 | 1001 | else |
5929e36c | 1002 | [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release Mouse)"]; |
c304f7e2 | 1003 | } |
13aefd30 | 1004 | [self hideCursor]; |
d1929069 | 1005 | CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled); |
49b9bd4d | 1006 | isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:] |
5b0753e0 | 1007 | } |
3b46e624 | 1008 | |
c304f7e2 TS |
1009 | - (void) ungrabMouse |
1010 | { | |
1011 | COCOA_DEBUG("QemuCocoaView: ungrabMouse\n"); | |
3b46e624 | 1012 | |
c304f7e2 TS |
1013 | if (!isFullscreen) { |
1014 | if (qemu_name) | |
1015 | [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]]; | |
1016 | else | |
1017 | [normalWindow setTitle:@"QEMU"]; | |
1018 | } | |
13aefd30 | 1019 | [self unhideCursor]; |
d1929069 | 1020 | CGAssociateMouseAndMouseCursorPosition(TRUE); |
49b9bd4d | 1021 | isMouseGrabbed = FALSE; |
5b0753e0 FB |
1022 | } |
1023 | ||
d1929069 AO |
1024 | - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled { |
1025 | isAbsoluteEnabled = tIsAbsoluteEnabled; | |
1026 | if (isMouseGrabbed) { | |
1027 | CGAssociateMouseAndMouseCursorPosition(isAbsoluteEnabled); | |
1028 | } | |
1029 | } | |
49b9bd4d | 1030 | - (BOOL) isMouseGrabbed {return isMouseGrabbed;} |
c304f7e2 TS |
1031 | - (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;} |
1032 | - (float) cdx {return cdx;} | |
1033 | - (float) cdy {return cdy;} | |
1034 | - (QEMUScreen) gscreen {return screen;} | |
3b178b71 JA |
1035 | |
1036 | /* | |
1037 | * Makes the target think all down keys are being released. | |
1038 | * This prevents a stuck key problem, since we will not see | |
1039 | * key up events for those keys after we have lost focus. | |
1040 | */ | |
1041 | - (void) raiseAllKeys | |
1042 | { | |
31819e95 | 1043 | with_iothread_lock(^{ |
6d73bb64 | 1044 | qkbd_state_lift_all_keys(kbd); |
31819e95 | 1045 | }); |
3b178b71 | 1046 | } |
5b0753e0 FB |
1047 | @end |
1048 | ||
1049 | ||
c304f7e2 | 1050 | |
5b0753e0 FB |
1051 | /* |
1052 | ------------------------------------------------------ | |
c304f7e2 | 1053 | QemuCocoaAppController |
5b0753e0 FB |
1054 | ------------------------------------------------------ |
1055 | */ | |
c304f7e2 | 1056 | @interface QemuCocoaAppController : NSObject |
d9bc14f6 | 1057 | <NSWindowDelegate, NSApplicationDelegate> |
5b0753e0 FB |
1058 | { |
1059 | } | |
5d1b2eef | 1060 | - (void)doToggleFullScreen:(id)sender; |
c304f7e2 TS |
1061 | - (void)toggleFullScreen:(id)sender; |
1062 | - (void)showQEMUDoc:(id)sender; | |
5d1b2eef | 1063 | - (void)zoomToFit:(id) sender; |
b4c6a112 | 1064 | - (void)displayConsole:(id)sender; |
8524f1c7 JA |
1065 | - (void)pauseQEMU:(id)sender; |
1066 | - (void)resumeQEMU:(id)sender; | |
1067 | - (void)displayPause; | |
1068 | - (void)removePause; | |
27074614 JA |
1069 | - (void)restartQEMU:(id)sender; |
1070 | - (void)powerDownQEMU:(id)sender; | |
693a3e01 JA |
1071 | - (void)ejectDeviceMedia:(id)sender; |
1072 | - (void)changeDeviceMedia:(id)sender; | |
d9bc14f6 | 1073 | - (BOOL)verifyQuit; |
f4747900 | 1074 | - (void)openDocumentation:(NSString *)filename; |
9e8204b1 JA |
1075 | - (IBAction) do_about_menu_item: (id) sender; |
1076 | - (void)make_about_window; | |
e47ec1a9 | 1077 | - (void)adjustSpeed:(id)sender; |
5b0753e0 FB |
1078 | @end |
1079 | ||
c304f7e2 TS |
1080 | @implementation QemuCocoaAppController |
1081 | - (id) init | |
5b0753e0 | 1082 | { |
c304f7e2 | 1083 | COCOA_DEBUG("QemuCocoaAppController: init\n"); |
5a246934 | 1084 | |
c304f7e2 TS |
1085 | self = [super init]; |
1086 | if (self) { | |
5a246934 | 1087 | |
c304f7e2 TS |
1088 | // create a view and add it to the window |
1089 | cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)]; | |
1090 | if(!cocoaView) { | |
4313739a | 1091 | error_report("(cocoa) can't create a view"); |
c304f7e2 TS |
1092 | exit(1); |
1093 | } | |
3b46e624 | 1094 | |
c304f7e2 TS |
1095 | // create a window |
1096 | normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame] | |
4ba967ad | 1097 | styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskClosable |
c304f7e2 TS |
1098 | backing:NSBackingStoreBuffered defer:NO]; |
1099 | if(!normalWindow) { | |
4313739a | 1100 | error_report("(cocoa) can't create window"); |
c304f7e2 TS |
1101 | exit(1); |
1102 | } | |
1103 | [normalWindow setAcceptsMouseMovedEvents:YES]; | |
a1dbc05a | 1104 | [normalWindow setTitle:@"QEMU"]; |
c304f7e2 TS |
1105 | [normalWindow setContentView:cocoaView]; |
1106 | [normalWindow makeKeyAndOrderFront:self]; | |
49060c29 | 1107 | [normalWindow center]; |
d9bc14f6 | 1108 | [normalWindow setDelegate: self]; |
5d1b2eef | 1109 | stretch_video = false; |
8524f1c7 JA |
1110 | |
1111 | /* Used for displaying pause on the screen */ | |
1112 | pauseLabel = [NSTextField new]; | |
1113 | [pauseLabel setBezeled:YES]; | |
1114 | [pauseLabel setDrawsBackground:YES]; | |
1115 | [pauseLabel setBackgroundColor: [NSColor whiteColor]]; | |
1116 | [pauseLabel setEditable:NO]; | |
1117 | [pauseLabel setSelectable:NO]; | |
1118 | [pauseLabel setStringValue: @"Paused"]; | |
1119 | [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]]; | |
1120 | [pauseLabel setTextColor: [NSColor blackColor]]; | |
1121 | [pauseLabel sizeToFit]; | |
693a3e01 JA |
1122 | |
1123 | // set the supported image file types that can be opened | |
1124 | supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg", | |
9d227f19 | 1125 | @"qcow", @"qcow2", @"cloop", @"vmdk", @"cdr", |
5f26fcfb | 1126 | @"toast", nil]; |
9e8204b1 | 1127 | [self make_about_window]; |
c304f7e2 TS |
1128 | } |
1129 | return self; | |
1130 | } | |
3b46e624 | 1131 | |
c304f7e2 TS |
1132 | - (void) dealloc |
1133 | { | |
1134 | COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); | |
1135 | ||
1136 | if (cocoaView) | |
1137 | [cocoaView release]; | |
1138 | [super dealloc]; | |
1139 | } | |
3b46e624 | 1140 | |
c304f7e2 TS |
1141 | - (void)applicationDidFinishLaunching: (NSNotification *) note |
1142 | { | |
1143 | COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n"); | |
dff742ad | 1144 | allow_events = true; |
5588840f PM |
1145 | /* Tell cocoa_display_init to proceed */ |
1146 | qemu_sem_post(&app_started_sem); | |
5b0753e0 FB |
1147 | } |
1148 | ||
1149 | - (void)applicationWillTerminate:(NSNotification *)aNotification | |
1150 | { | |
c304f7e2 TS |
1151 | COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n"); |
1152 | ||
cf83f140 | 1153 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI); |
40c01937 AO |
1154 | |
1155 | /* | |
1156 | * Sleep here, because returning will cause OSX to kill us | |
1157 | * immediately; the QEMU main loop will handle the shutdown | |
1158 | * request and terminate the process. | |
1159 | */ | |
1160 | [NSThread sleepForTimeInterval:INFINITY]; | |
5b0753e0 FB |
1161 | } |
1162 | ||
41ea49b3 AF |
1163 | - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication |
1164 | { | |
1165 | return YES; | |
1166 | } | |
1167 | ||
d9bc14f6 JA |
1168 | - (NSApplicationTerminateReply)applicationShouldTerminate: |
1169 | (NSApplication *)sender | |
1170 | { | |
1171 | COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n"); | |
1172 | return [self verifyQuit]; | |
1173 | } | |
1174 | ||
1175 | /* Called when the user clicks on a window's close button */ | |
1176 | - (BOOL)windowShouldClose:(id)sender | |
1177 | { | |
1178 | COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n"); | |
1179 | [NSApp terminate: sender]; | |
1180 | /* If the user allows the application to quit then the call to | |
1181 | * NSApp terminate will never return. If we get here then the user | |
1182 | * cancelled the quit, so we should return NO to not permit the | |
1183 | * closing of this window. | |
1184 | */ | |
1185 | return NO; | |
1186 | } | |
1187 | ||
3b178b71 JA |
1188 | /* Called when QEMU goes into the background */ |
1189 | - (void) applicationWillResignActive: (NSNotification *)aNotification | |
1190 | { | |
1191 | COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n"); | |
1192 | [cocoaView raiseAllKeys]; | |
1193 | } | |
1194 | ||
5d1b2eef JA |
1195 | /* We abstract the method called by the Enter Fullscreen menu item |
1196 | * because Mac OS 10.7 and higher disables it. This is because of the | |
1197 | * menu item's old selector's name toggleFullScreen: | |
1198 | */ | |
1199 | - (void) doToggleFullScreen:(id)sender | |
1200 | { | |
1201 | [self toggleFullScreen:(id)sender]; | |
1202 | } | |
1203 | ||
c304f7e2 TS |
1204 | - (void)toggleFullScreen:(id)sender |
1205 | { | |
1206 | COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n"); | |
1207 | ||
1208 | [cocoaView toggleFullScreen:sender]; | |
1209 | } | |
5b0753e0 | 1210 | |
f4747900 JA |
1211 | /* Tries to find then open the specified filename */ |
1212 | - (void) openDocumentation: (NSString *) filename | |
1213 | { | |
1214 | /* Where to look for local files */ | |
8d6fda8c | 1215 | NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"docs/"}; |
f4747900 | 1216 | NSString *full_file_path; |
1ff5a063 | 1217 | NSURL *full_file_url; |
f4747900 JA |
1218 | |
1219 | /* iterate thru the possible paths until the file is found */ | |
1220 | int index; | |
1221 | for (index = 0; index < ARRAY_SIZE(path_array); index++) { | |
1222 | full_file_path = [[NSBundle mainBundle] executablePath]; | |
1223 | full_file_path = [full_file_path stringByDeletingLastPathComponent]; | |
1224 | full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path, | |
1225 | path_array[index], filename]; | |
1ff5a063 RB |
1226 | full_file_url = [NSURL fileURLWithPath: full_file_path |
1227 | isDirectory: false]; | |
1228 | if ([[NSWorkspace sharedWorkspace] openURL: full_file_url] == YES) { | |
f4747900 JA |
1229 | return; |
1230 | } | |
1231 | } | |
1232 | ||
1233 | /* If none of the paths opened a file */ | |
1234 | NSBeep(); | |
1235 | QEMU_Alert(@"Failed to open file"); | |
1236 | } | |
1237 | ||
c304f7e2 | 1238 | - (void)showQEMUDoc:(id)sender |
5b0753e0 | 1239 | { |
c304f7e2 TS |
1240 | COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n"); |
1241 | ||
1879f241 | 1242 | [self openDocumentation: @"index.html"]; |
c304f7e2 TS |
1243 | } |
1244 | ||
5d1b2eef JA |
1245 | /* Stretches video to fit host monitor size */ |
1246 | - (void)zoomToFit:(id) sender | |
1247 | { | |
1248 | stretch_video = !stretch_video; | |
1249 | if (stretch_video == true) { | |
5e24600a | 1250 | [sender setState: NSControlStateValueOn]; |
5d1b2eef | 1251 | } else { |
5e24600a | 1252 | [sender setState: NSControlStateValueOff]; |
5d1b2eef JA |
1253 | } |
1254 | } | |
5b0753e0 | 1255 | |
b4c6a112 JA |
1256 | /* Displays the console on the screen */ |
1257 | - (void)displayConsole:(id)sender | |
1258 | { | |
1259 | console_select([sender tag]); | |
1260 | } | |
8524f1c7 JA |
1261 | |
1262 | /* Pause the guest */ | |
1263 | - (void)pauseQEMU:(id)sender | |
1264 | { | |
31819e95 PM |
1265 | with_iothread_lock(^{ |
1266 | qmp_stop(NULL); | |
1267 | }); | |
8524f1c7 JA |
1268 | [sender setEnabled: NO]; |
1269 | [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES]; | |
1270 | [self displayPause]; | |
1271 | } | |
1272 | ||
1273 | /* Resume running the guest operating system */ | |
1274 | - (void)resumeQEMU:(id) sender | |
1275 | { | |
31819e95 PM |
1276 | with_iothread_lock(^{ |
1277 | qmp_cont(NULL); | |
1278 | }); | |
8524f1c7 JA |
1279 | [sender setEnabled: NO]; |
1280 | [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES]; | |
1281 | [self removePause]; | |
1282 | } | |
1283 | ||
1284 | /* Displays the word pause on the screen */ | |
1285 | - (void)displayPause | |
1286 | { | |
1287 | /* Coordinates have to be calculated each time because the window can change its size */ | |
1288 | int xCoord, yCoord, width, height; | |
1289 | xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2; | |
1290 | yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5); | |
1291 | width = [pauseLabel frame].size.width; | |
1292 | height = [pauseLabel frame].size.height; | |
1293 | [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)]; | |
1294 | [cocoaView addSubview: pauseLabel]; | |
1295 | } | |
1296 | ||
1297 | /* Removes the word pause from the screen */ | |
1298 | - (void)removePause | |
1299 | { | |
1300 | [pauseLabel removeFromSuperview]; | |
1301 | } | |
1302 | ||
27074614 JA |
1303 | /* Restarts QEMU */ |
1304 | - (void)restartQEMU:(id)sender | |
1305 | { | |
31819e95 PM |
1306 | with_iothread_lock(^{ |
1307 | qmp_system_reset(NULL); | |
1308 | }); | |
27074614 JA |
1309 | } |
1310 | ||
1311 | /* Powers down QEMU */ | |
1312 | - (void)powerDownQEMU:(id)sender | |
1313 | { | |
31819e95 PM |
1314 | with_iothread_lock(^{ |
1315 | qmp_system_powerdown(NULL); | |
1316 | }); | |
27074614 JA |
1317 | } |
1318 | ||
693a3e01 JA |
1319 | /* Ejects the media. |
1320 | * Uses sender's tag to figure out the device to eject. | |
1321 | */ | |
1322 | - (void)ejectDeviceMedia:(id)sender | |
1323 | { | |
1324 | NSString * drive; | |
1325 | drive = [sender representedObject]; | |
1326 | if(drive == nil) { | |
1327 | NSBeep(); | |
1328 | QEMU_Alert(@"Failed to find drive to eject!"); | |
1329 | return; | |
1330 | } | |
1331 | ||
31819e95 PM |
1332 | __block Error *err = NULL; |
1333 | with_iothread_lock(^{ | |
1334 | qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding], | |
1335 | false, NULL, false, false, &err); | |
1336 | }); | |
693a3e01 JA |
1337 | handleAnyDeviceErrors(err); |
1338 | } | |
1339 | ||
1340 | /* Displays a dialog box asking the user to select an image file to load. | |
1341 | * Uses sender's represented object value to figure out which drive to use. | |
1342 | */ | |
1343 | - (void)changeDeviceMedia:(id)sender | |
1344 | { | |
1345 | /* Find the drive name */ | |
1346 | NSString * drive; | |
1347 | drive = [sender representedObject]; | |
1348 | if(drive == nil) { | |
1349 | NSBeep(); | |
1350 | QEMU_Alert(@"Could not find drive!"); | |
1351 | return; | |
1352 | } | |
1353 | ||
1354 | /* Display the file open dialog */ | |
1355 | NSOpenPanel * openPanel; | |
1356 | openPanel = [NSOpenPanel openPanel]; | |
1357 | [openPanel setCanChooseFiles: YES]; | |
1358 | [openPanel setAllowsMultipleSelection: NO]; | |
1359 | [openPanel setAllowedFileTypes: supportedImageFileTypes]; | |
b5725385 | 1360 | if([openPanel runModal] == NSModalResponseOK) { |
693a3e01 JA |
1361 | NSString * file = [[[openPanel URLs] objectAtIndex: 0] path]; |
1362 | if(file == nil) { | |
1363 | NSBeep(); | |
1364 | QEMU_Alert(@"Failed to convert URL to file path!"); | |
1365 | return; | |
1366 | } | |
1367 | ||
31819e95 PM |
1368 | __block Error *err = NULL; |
1369 | with_iothread_lock(^{ | |
1370 | qmp_blockdev_change_medium(true, | |
1371 | [drive cStringUsingEncoding: | |
1372 | NSASCIIStringEncoding], | |
1373 | false, NULL, | |
1374 | [file cStringUsingEncoding: | |
1375 | NSASCIIStringEncoding], | |
1376 | true, "raw", | |
1377 | false, 0, | |
1378 | &err); | |
1379 | }); | |
693a3e01 JA |
1380 | handleAnyDeviceErrors(err); |
1381 | } | |
1382 | } | |
1383 | ||
d9bc14f6 JA |
1384 | /* Verifies if the user really wants to quit */ |
1385 | - (BOOL)verifyQuit | |
1386 | { | |
1387 | NSAlert *alert = [NSAlert new]; | |
1388 | [alert autorelease]; | |
1389 | [alert setMessageText: @"Are you sure you want to quit QEMU?"]; | |
1390 | [alert addButtonWithTitle: @"Cancel"]; | |
1391 | [alert addButtonWithTitle: @"Quit"]; | |
1392 | if([alert runModal] == NSAlertSecondButtonReturn) { | |
1393 | return YES; | |
1394 | } else { | |
1395 | return NO; | |
1396 | } | |
1397 | } | |
1398 | ||
9e8204b1 JA |
1399 | /* The action method for the About menu item */ |
1400 | - (IBAction) do_about_menu_item: (id) sender | |
1401 | { | |
1402 | [about_window makeKeyAndOrderFront: nil]; | |
1403 | } | |
1404 | ||
1405 | /* Create and display the about dialog */ | |
1406 | - (void)make_about_window | |
1407 | { | |
1408 | /* Make the window */ | |
1409 | int x = 0, y = 0, about_width = 400, about_height = 200; | |
1410 | NSRect window_rect = NSMakeRect(x, y, about_width, about_height); | |
1411 | about_window = [[NSWindow alloc] initWithContentRect:window_rect | |
4ba967ad BS |
1412 | styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | |
1413 | NSWindowStyleMaskMiniaturizable | |
9e8204b1 JA |
1414 | backing:NSBackingStoreBuffered |
1415 | defer:NO]; | |
1416 | [about_window setTitle: @"About"]; | |
1417 | [about_window setReleasedWhenClosed: NO]; | |
1418 | [about_window center]; | |
1419 | NSView *superView = [about_window contentView]; | |
1420 | ||
1421 | /* Create the dimensions of the picture */ | |
1422 | int picture_width = 80, picture_height = 80; | |
1423 | x = (about_width - picture_width)/2; | |
1424 | y = about_height - picture_height - 10; | |
1425 | NSRect picture_rect = NSMakeRect(x, y, picture_width, picture_height); | |
1426 | ||
9e8204b1 JA |
1427 | /* Make the picture of QEMU */ |
1428 | NSImageView *picture_view = [[NSImageView alloc] initWithFrame: | |
1429 | picture_rect]; | |
e31746ec AO |
1430 | char *qemu_image_path_c = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/512x512/apps/qemu.png"); |
1431 | NSString *qemu_image_path = [NSString stringWithUTF8String:qemu_image_path_c]; | |
1432 | g_free(qemu_image_path_c); | |
1433 | NSImage *qemu_image = [[NSImage alloc] initWithContentsOfFile:qemu_image_path]; | |
9e8204b1 JA |
1434 | [picture_view setImage: qemu_image]; |
1435 | [picture_view setImageScaling: NSImageScaleProportionallyUpOrDown]; | |
1436 | [superView addSubview: picture_view]; | |
1437 | ||
1438 | /* Make the name label */ | |
a0f973f9 AO |
1439 | NSBundle *bundle = [NSBundle mainBundle]; |
1440 | if (bundle) { | |
1441 | x = 0; | |
1442 | y = y - 25; | |
1443 | int name_width = about_width, name_height = 20; | |
1444 | NSRect name_rect = NSMakeRect(x, y, name_width, name_height); | |
1445 | NSTextField *name_label = [[NSTextField alloc] initWithFrame: name_rect]; | |
1446 | [name_label setEditable: NO]; | |
1447 | [name_label setBezeled: NO]; | |
1448 | [name_label setDrawsBackground: NO]; | |
1449 | [name_label setAlignment: NSTextAlignmentCenter]; | |
1450 | NSString *qemu_name = [[bundle executablePath] lastPathComponent]; | |
1451 | [name_label setStringValue: qemu_name]; | |
1452 | [superView addSubview: name_label]; | |
1453 | } | |
9e8204b1 JA |
1454 | |
1455 | /* Set the version label's attributes */ | |
1456 | x = 0; | |
1457 | y = 50; | |
1458 | int version_width = about_width, version_height = 20; | |
1459 | NSRect version_rect = NSMakeRect(x, y, version_width, version_height); | |
1460 | NSTextField *version_label = [[NSTextField alloc] initWithFrame: | |
1461 | version_rect]; | |
1462 | [version_label setEditable: NO]; | |
1463 | [version_label setBezeled: NO]; | |
4ba967ad | 1464 | [version_label setAlignment: NSTextAlignmentCenter]; |
9e8204b1 JA |
1465 | [version_label setDrawsBackground: NO]; |
1466 | ||
1467 | /* Create the version string*/ | |
1468 | NSString *version_string; | |
1469 | version_string = [[NSString alloc] initWithFormat: | |
7e563bfb | 1470 | @"QEMU emulator version %s", QEMU_FULL_VERSION]; |
9e8204b1 JA |
1471 | [version_label setStringValue: version_string]; |
1472 | [superView addSubview: version_label]; | |
1473 | ||
1474 | /* Make copyright label */ | |
1475 | x = 0; | |
1476 | y = 35; | |
1477 | int copyright_width = about_width, copyright_height = 20; | |
1478 | NSRect copyright_rect = NSMakeRect(x, y, copyright_width, copyright_height); | |
1479 | NSTextField *copyright_label = [[NSTextField alloc] initWithFrame: | |
1480 | copyright_rect]; | |
1481 | [copyright_label setEditable: NO]; | |
1482 | [copyright_label setBezeled: NO]; | |
1483 | [copyright_label setDrawsBackground: NO]; | |
4ba967ad | 1484 | [copyright_label setAlignment: NSTextAlignmentCenter]; |
9e8204b1 JA |
1485 | [copyright_label setStringValue: [NSString stringWithFormat: @"%s", |
1486 | QEMU_COPYRIGHT]]; | |
1487 | [superView addSubview: copyright_label]; | |
1488 | } | |
1489 | ||
e47ec1a9 JA |
1490 | /* Used by the Speed menu items */ |
1491 | - (void)adjustSpeed:(id)sender | |
1492 | { | |
1493 | int throttle_pct; /* throttle percentage */ | |
1494 | NSMenu *menu; | |
1495 | ||
1496 | menu = [sender menu]; | |
1497 | if (menu != nil) | |
1498 | { | |
1499 | /* Unselect the currently selected item */ | |
1500 | for (NSMenuItem *item in [menu itemArray]) { | |
5e24600a BS |
1501 | if (item.state == NSControlStateValueOn) { |
1502 | [item setState: NSControlStateValueOff]; | |
e47ec1a9 JA |
1503 | break; |
1504 | } | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | // check the menu item | |
5e24600a | 1509 | [sender setState: NSControlStateValueOn]; |
e47ec1a9 JA |
1510 | |
1511 | // get the throttle percentage | |
1512 | throttle_pct = [sender tag]; | |
1513 | ||
31819e95 PM |
1514 | with_iothread_lock(^{ |
1515 | cpu_throttle_set(throttle_pct); | |
1516 | }); | |
e47ec1a9 JA |
1517 | COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%'); |
1518 | } | |
1519 | ||
b4c6a112 | 1520 | @end |
5b0753e0 | 1521 | |
61a2ed44 PM |
1522 | @interface QemuApplication : NSApplication |
1523 | @end | |
1524 | ||
1525 | @implementation QemuApplication | |
1526 | - (void)sendEvent:(NSEvent *)event | |
1527 | { | |
1528 | COCOA_DEBUG("QemuApplication: sendEvent\n"); | |
5588840f PM |
1529 | if (![cocoaView handleEvent:event]) { |
1530 | [super sendEvent: event]; | |
1531 | } | |
61a2ed44 PM |
1532 | } |
1533 | @end | |
1534 | ||
c6fd6c70 PM |
1535 | static void create_initial_menus(void) |
1536 | { | |
c304f7e2 TS |
1537 | // Add menus |
1538 | NSMenu *menu; | |
1539 | NSMenuItem *menuItem; | |
5b0753e0 | 1540 | |
c304f7e2 | 1541 | [NSApp setMainMenu:[[NSMenu alloc] init]]; |
5b0753e0 | 1542 | |
c304f7e2 TS |
1543 | // Application menu |
1544 | menu = [[NSMenu alloc] initWithTitle:@""]; | |
9e8204b1 | 1545 | [menu addItemWithTitle:@"About QEMU" action:@selector(do_about_menu_item:) keyEquivalent:@""]; // About QEMU |
c304f7e2 TS |
1546 | [menu addItem:[NSMenuItem separatorItem]]; //Separator |
1547 | [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU | |
1548 | menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others | |
4ba967ad | 1549 | [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)]; |
c304f7e2 TS |
1550 | [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All |
1551 | [menu addItem:[NSMenuItem separatorItem]]; //Separator | |
1552 | [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"]; | |
1553 | menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""]; | |
1554 | [menuItem setSubmenu:menu]; | |
1555 | [[NSApp mainMenu] addItem:menuItem]; | |
1556 | [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+) | |
3b46e624 | 1557 | |
8524f1c7 JA |
1558 | // Machine menu |
1559 | menu = [[NSMenu alloc] initWithTitle: @"Machine"]; | |
1560 | [menu setAutoenablesItems: NO]; | |
1561 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]]; | |
1562 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease]; | |
1563 | [menu addItem: menuItem]; | |
1564 | [menuItem setEnabled: NO]; | |
27074614 JA |
1565 | [menu addItem: [NSMenuItem separatorItem]]; |
1566 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]]; | |
1567 | [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]]; | |
8524f1c7 JA |
1568 | menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease]; |
1569 | [menuItem setSubmenu:menu]; | |
1570 | [[NSApp mainMenu] addItem:menuItem]; | |
1571 | ||
c304f7e2 TS |
1572 | // View menu |
1573 | menu = [[NSMenu alloc] initWithTitle:@"View"]; | |
5d1b2eef JA |
1574 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen |
1575 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]]; | |
c304f7e2 TS |
1576 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease]; |
1577 | [menuItem setSubmenu:menu]; | |
5b0753e0 FB |
1578 | [[NSApp mainMenu] addItem:menuItem]; |
1579 | ||
e47ec1a9 JA |
1580 | // Speed menu |
1581 | menu = [[NSMenu alloc] initWithTitle:@"Speed"]; | |
1582 | ||
1583 | // Add the rest of the Speed menu items | |
1584 | int p, percentage, throttle_pct; | |
1585 | for (p = 10; p >= 0; p--) | |
1586 | { | |
1587 | percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item | |
1588 | ||
1589 | menuItem = [[[NSMenuItem alloc] | |
1590 | initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]; | |
1591 | ||
1592 | if (percentage == 100) { | |
5e24600a | 1593 | [menuItem setState: NSControlStateValueOn]; |
e47ec1a9 JA |
1594 | } |
1595 | ||
1596 | /* Calculate the throttle percentage */ | |
1597 | throttle_pct = -1 * percentage + 100; | |
1598 | ||
1599 | [menuItem setTag: throttle_pct]; | |
1600 | [menu addItem: menuItem]; | |
1601 | } | |
1602 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease]; | |
1603 | [menuItem setSubmenu:menu]; | |
1604 | [[NSApp mainMenu] addItem:menuItem]; | |
1605 | ||
c304f7e2 TS |
1606 | // Window menu |
1607 | menu = [[NSMenu alloc] initWithTitle:@"Window"]; | |
1608 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize | |
1609 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; | |
1610 | [menuItem setSubmenu:menu]; | |
1611 | [[NSApp mainMenu] addItem:menuItem]; | |
1612 | [NSApp setWindowsMenu:menu]; | |
1613 | ||
1614 | // Help menu | |
1615 | menu = [[NSMenu alloc] initWithTitle:@"Help"]; | |
1616 | [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help | |
c304f7e2 TS |
1617 | menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease]; |
1618 | [menuItem setSubmenu:menu]; | |
1619 | [[NSApp mainMenu] addItem:menuItem]; | |
c6fd6c70 PM |
1620 | } |
1621 | ||
8b00e4e7 PM |
1622 | /* Returns a name for a given console */ |
1623 | static NSString * getConsoleName(QemuConsole * console) | |
1624 | { | |
1625 | return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)]; | |
1626 | } | |
1627 | ||
1628 | /* Add an entry to the View menu for each console */ | |
1629 | static void add_console_menu_entries(void) | |
1630 | { | |
1631 | NSMenu *menu; | |
1632 | NSMenuItem *menuItem; | |
1633 | int index = 0; | |
1634 | ||
1635 | menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu]; | |
1636 | ||
1637 | [menu addItem:[NSMenuItem separatorItem]]; | |
1638 | ||
1639 | while (qemu_console_lookup_by_index(index) != NULL) { | |
1640 | menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index)) | |
1641 | action: @selector(displayConsole:) keyEquivalent: @""] autorelease]; | |
1642 | [menuItem setTag: index]; | |
1643 | [menu addItem: menuItem]; | |
1644 | index++; | |
1645 | } | |
1646 | } | |
1647 | ||
1648 | /* Make menu items for all removable devices. | |
1649 | * Each device is given an 'Eject' and 'Change' menu item. | |
1650 | */ | |
1651 | static void addRemovableDevicesMenuItems(void) | |
1652 | { | |
1653 | NSMenu *menu; | |
1654 | NSMenuItem *menuItem; | |
1655 | BlockInfoList *currentDevice, *pointerToFree; | |
1656 | NSString *deviceName; | |
1657 | ||
1658 | currentDevice = qmp_query_block(NULL); | |
1659 | pointerToFree = currentDevice; | |
1660 | if(currentDevice == NULL) { | |
1661 | NSBeep(); | |
1662 | QEMU_Alert(@"Failed to query for block devices!"); | |
1663 | return; | |
1664 | } | |
1665 | ||
1666 | menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu]; | |
1667 | ||
1668 | // Add a separator between related groups of menu items | |
1669 | [menu addItem:[NSMenuItem separatorItem]]; | |
1670 | ||
1671 | // Set the attributes to the "Removable Media" menu item | |
1672 | NSString *titleString = @"Removable Media"; | |
1673 | NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString]; | |
1674 | NSColor *newColor = [NSColor blackColor]; | |
1675 | NSFontManager *fontManager = [NSFontManager sharedFontManager]; | |
1676 | NSFont *font = [fontManager fontWithFamily:@"Helvetica" | |
1677 | traits:NSBoldFontMask|NSItalicFontMask | |
1678 | weight:0 | |
1679 | size:14]; | |
1680 | [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])]; | |
1681 | [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])]; | |
1682 | [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])]; | |
1683 | ||
1684 | // Add the "Removable Media" menu item | |
1685 | menuItem = [NSMenuItem new]; | |
1686 | [menuItem setAttributedTitle: attString]; | |
1687 | [menuItem setEnabled: NO]; | |
1688 | [menu addItem: menuItem]; | |
1689 | ||
1690 | /* Loop through all the block devices in the emulator */ | |
1691 | while (currentDevice) { | |
1692 | deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain]; | |
1693 | ||
1694 | if(currentDevice->value->removable) { | |
1695 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device] | |
1696 | action: @selector(changeDeviceMedia:) | |
1697 | keyEquivalent: @""]; | |
1698 | [menu addItem: menuItem]; | |
1699 | [menuItem setRepresentedObject: deviceName]; | |
1700 | [menuItem autorelease]; | |
1701 | ||
1702 | menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device] | |
1703 | action: @selector(ejectDeviceMedia:) | |
1704 | keyEquivalent: @""]; | |
1705 | [menu addItem: menuItem]; | |
1706 | [menuItem setRepresentedObject: deviceName]; | |
1707 | [menuItem autorelease]; | |
1708 | } | |
1709 | currentDevice = currentDevice->next; | |
1710 | } | |
1711 | qapi_free_BlockInfoList(pointerToFree); | |
1712 | } | |
1713 | ||
5588840f PM |
1714 | /* |
1715 | * The startup process for the OSX/Cocoa UI is complicated, because | |
1716 | * OSX insists that the UI runs on the initial main thread, and so we | |
1717 | * need to start a second thread which runs the vl.c qemu_main(): | |
1718 | * | |
1719 | * Initial thread: 2nd thread: | |
1720 | * in main(): | |
1721 | * create qemu-main thread | |
1722 | * wait on display_init semaphore | |
1723 | * call qemu_main() | |
1724 | * ... | |
1725 | * in cocoa_display_init(): | |
1726 | * post the display_init semaphore | |
1727 | * wait on app_started semaphore | |
1728 | * create application, menus, etc | |
1729 | * enter OSX run loop | |
1730 | * in applicationDidFinishLaunching: | |
1731 | * post app_started semaphore | |
1732 | * tell main thread to fullscreen if needed | |
1733 | * [...] | |
1734 | * run qemu main-loop | |
1735 | * | |
1736 | * We do this in two stages so that we don't do the creation of the | |
1737 | * GUI application menus and so on for command line options like --help | |
1738 | * where we want to just print text to stdout and exit immediately. | |
1739 | */ | |
1740 | ||
1741 | static void *call_qemu_main(void *opaque) | |
1742 | { | |
1743 | int status; | |
1744 | ||
1745 | COCOA_DEBUG("Second thread: calling qemu_main()\n"); | |
1746 | status = qemu_main(gArgc, gArgv, *_NSGetEnviron()); | |
1747 | COCOA_DEBUG("Second thread: qemu_main() returned, exiting\n"); | |
1748 | exit(status); | |
1749 | } | |
1750 | ||
c6fd6c70 | 1751 | int main (int argc, const char * argv[]) { |
5588840f | 1752 | QemuThread thread; |
c6fd6c70 | 1753 | |
5588840f | 1754 | COCOA_DEBUG("Entered main()\n"); |
c6fd6c70 PM |
1755 | gArgc = argc; |
1756 | gArgv = (char **)argv; | |
c6fd6c70 | 1757 | |
5588840f PM |
1758 | qemu_sem_init(&display_init_sem, 0); |
1759 | qemu_sem_init(&app_started_sem, 0); | |
c6fd6c70 | 1760 | |
5588840f PM |
1761 | qemu_thread_create(&thread, "qemu_main", call_qemu_main, |
1762 | NULL, QEMU_THREAD_DETACHED); | |
1763 | ||
1764 | COCOA_DEBUG("Main thread: waiting for display_init_sem\n"); | |
1765 | qemu_sem_wait(&display_init_sem); | |
1766 | COCOA_DEBUG("Main thread: initializing app\n"); | |
c6fd6c70 PM |
1767 | |
1768 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
1769 | ||
1770 | // Pull this console process up to being a fully-fledged graphical | |
1771 | // app with a menubar and Dock icon | |
1772 | ProcessSerialNumber psn = { 0, kCurrentProcess }; | |
1773 | TransformProcessType(&psn, kProcessTransformToForegroundApplication); | |
1774 | ||
61a2ed44 | 1775 | [QemuApplication sharedApplication]; |
c6fd6c70 PM |
1776 | |
1777 | create_initial_menus(); | |
5b0753e0 | 1778 | |
5588840f PM |
1779 | /* |
1780 | * Create the menu entries which depend on QEMU state (for consoles | |
1781 | * and removeable devices). These make calls back into QEMU functions, | |
1782 | * which is OK because at this point we know that the second thread | |
1783 | * holds the iothread lock and is synchronously waiting for us to | |
1784 | * finish. | |
1785 | */ | |
1786 | add_console_menu_entries(); | |
1787 | addRemovableDevicesMenuItems(); | |
1788 | ||
c304f7e2 TS |
1789 | // Create an Application controller |
1790 | QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init]; | |
1791 | [NSApp setDelegate:appController]; | |
5b0753e0 | 1792 | |
c304f7e2 | 1793 | // Start the main event loop |
5588840f | 1794 | COCOA_DEBUG("Main thread: entering OSX run loop\n"); |
c304f7e2 | 1795 | [NSApp run]; |
5588840f | 1796 | COCOA_DEBUG("Main thread: left OSX run loop, exiting\n"); |
5b0753e0 | 1797 | |
c304f7e2 TS |
1798 | [appController release]; |
1799 | [pool release]; | |
3b46e624 | 1800 | |
c304f7e2 TS |
1801 | return 0; |
1802 | } | |
3b46e624 | 1803 | |
3b46e624 | 1804 | |
5b0753e0 | 1805 | |
c304f7e2 | 1806 | #pragma mark qemu |
7c20b4a3 | 1807 | static void cocoa_update(DisplayChangeListener *dcl, |
7c20b4a3 | 1808 | int x, int y, int w, int h) |
c304f7e2 | 1809 | { |
6e657e64 PM |
1810 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
1811 | ||
c304f7e2 TS |
1812 | COCOA_DEBUG("qemu_cocoa: cocoa_update\n"); |
1813 | ||
5588840f PM |
1814 | dispatch_async(dispatch_get_main_queue(), ^{ |
1815 | NSRect rect; | |
1816 | if ([cocoaView cdx] == 1.0) { | |
1817 | rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h); | |
1818 | } else { | |
1819 | rect = NSMakeRect( | |
1820 | x * [cocoaView cdx], | |
1821 | ([cocoaView gscreen].height - y - h) * [cocoaView cdy], | |
1822 | w * [cocoaView cdx], | |
1823 | h * [cocoaView cdy]); | |
1824 | } | |
1825 | [cocoaView setNeedsDisplayInRect:rect]; | |
1826 | }); | |
6e657e64 PM |
1827 | |
1828 | [pool release]; | |
5b0753e0 FB |
1829 | } |
1830 | ||
c12aeb86 | 1831 | static void cocoa_switch(DisplayChangeListener *dcl, |
c12aeb86 | 1832 | DisplaySurface *surface) |
5b0753e0 | 1833 | { |
6e657e64 | 1834 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
5588840f | 1835 | pixman_image_t *image = surface->image; |
3b46e624 | 1836 | |
6e657e64 | 1837 | COCOA_DEBUG("qemu_cocoa: cocoa_switch\n"); |
5588840f PM |
1838 | |
1839 | // The DisplaySurface will be freed as soon as this callback returns. | |
1840 | // We take a reference to the underlying pixman image here so it does | |
1841 | // not disappear from under our feet; the switchSurface method will | |
1842 | // deref the old image when it is done with it. | |
1843 | pixman_image_ref(image); | |
1844 | ||
1845 | dispatch_async(dispatch_get_main_queue(), ^{ | |
1846 | [cocoaView switchSurface:image]; | |
1847 | }); | |
6e657e64 | 1848 | [pool release]; |
c304f7e2 | 1849 | } |
3b46e624 | 1850 | |
bc2ed970 | 1851 | static void cocoa_refresh(DisplayChangeListener *dcl) |
c304f7e2 | 1852 | { |
6e657e64 PM |
1853 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
1854 | ||
c304f7e2 | 1855 | COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n"); |
468a895b | 1856 | graphic_hw_update(NULL); |
3b46e624 | 1857 | |
21bae11a | 1858 | if (qemu_input_is_absolute()) { |
5588840f PM |
1859 | dispatch_async(dispatch_get_main_queue(), ^{ |
1860 | if (![cocoaView isAbsoluteEnabled]) { | |
1861 | if ([cocoaView isMouseGrabbed]) { | |
1862 | [cocoaView ungrabMouse]; | |
1863 | } | |
c304f7e2 | 1864 | } |
5588840f PM |
1865 | [cocoaView setAbsoluteEnabled:YES]; |
1866 | }); | |
c304f7e2 | 1867 | } |
6e657e64 | 1868 | [pool release]; |
c304f7e2 | 1869 | } |
3b46e624 | 1870 | |
5013b9e4 | 1871 | static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts) |
5b0753e0 | 1872 | { |
c304f7e2 TS |
1873 | COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); |
1874 | ||
5588840f PM |
1875 | /* Tell main thread to go ahead and create the app and enter the run loop */ |
1876 | qemu_sem_post(&display_init_sem); | |
1877 | qemu_sem_wait(&app_started_sem); | |
1878 | COCOA_DEBUG("cocoa_display_init: app start completed\n"); | |
1879 | ||
43227af8 | 1880 | /* if fullscreen mode is to be used */ |
767f9bf3 | 1881 | if (opts->has_full_screen && opts->full_screen) { |
5588840f PM |
1882 | dispatch_async(dispatch_get_main_queue(), ^{ |
1883 | [NSApp activateIgnoringOtherApps: YES]; | |
1884 | [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil]; | |
1885 | }); | |
43227af8 | 1886 | } |
3487da6a GH |
1887 | if (opts->has_show_cursor && opts->show_cursor) { |
1888 | cursor_hide = 0; | |
1889 | } | |
43227af8 | 1890 | |
9794f74f | 1891 | // register vga output callbacks |
cc7859c3 | 1892 | register_displaychangelistener(&dcl); |
5b0753e0 | 1893 | } |
5013b9e4 GH |
1894 | |
1895 | static QemuDisplay qemu_display_cocoa = { | |
1896 | .type = DISPLAY_TYPE_COCOA, | |
1897 | .init = cocoa_display_init, | |
1898 | }; | |
1899 | ||
1900 | static void register_cocoa(void) | |
1901 | { | |
1902 | qemu_display_register(&qemu_display_cocoa); | |
1903 | } | |
1904 | ||
1905 | type_init(register_cocoa); |