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