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