blob: 559058ba39a80d0f7a79ecb4ae660a42130c3b44 [file] [log] [blame]
bellard5b0753e2005-03-01 21:37:28 +00001/*
thsc304f7e2008-01-22 23:25:15 +00002 * QEMU Cocoa CG display driver
ths5fafdf22007-09-16 21:08:06 +00003 *
thsc304f7e2008-01-22 23:25:15 +00004 * Copyright (c) 2008 Mike Kronenberg
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard5b0753e2005-03-01 21:37:28 +00006 * 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 */
bellardda4dbf72005-03-02 22:22:43 +000024
bellard5b0753e2005-03-01 21:37:28 +000025#import <Cocoa/Cocoa.h>
Andreas Färber3bbbee12011-05-29 19:42:51 +020026#include <crt_externs.h>
bellard5b0753e2005-03-01 21:37:28 +000027
pbrook87ecb682007-11-17 17:14:51 +000028#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010029#include "ui/console.h"
Gerd Hoffmann21bae112013-12-04 14:08:04 +010030#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
John Arbuckle8524f1c2015-06-19 10:53:27 +010032#include "qmp-commands.h"
bellardda4dbf72005-03-02 22:22:43 +000033
Andreas Färber44e4c0b2009-12-13 00:45:40 +010034#ifndef MAC_OS_X_VERSION_10_5
35#define MAC_OS_X_VERSION_10_5 1050
36#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000037#ifndef MAC_OS_X_VERSION_10_6
38#define MAC_OS_X_VERSION_10_6 1060
39#endif
Peter Maydell81801ae2015-05-19 09:11:18 +010040#ifndef MAC_OS_X_VERSION_10_10
41#define MAC_OS_X_VERSION_10_10 101000
42#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010043
bellard5b0753e2005-03-01 21:37:28 +000044
thsc304f7e2008-01-22 23:25:15 +000045//#define DEBUG
46
47#ifdef DEBUG
48#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
49#else
50#define COCOA_DEBUG(...) ((void) 0)
51#endif
52
53#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000054
55typedef struct {
56 int width;
57 int height;
58 int bitsPerComponent;
59 int bitsPerPixel;
60} QEMUScreen;
61
thsc304f7e2008-01-22 23:25:15 +000062NSWindow *normalWindow;
aliguori9794f742009-03-04 19:25:22 +000063static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +010064static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +000065
66int gArgc;
67char **gArgv;
Programmingkid5d1b2ee2015-05-19 09:11:17 +010068bool stretch_video;
John Arbuckle8524f1c2015-06-19 10:53:27 +010069NSTextField *pauseLabel;
bellard5b0753e2005-03-01 21:37:28 +000070
thsc304f7e2008-01-22 23:25:15 +000071// keymap conversion
bellard87f48e62005-10-30 18:24:49 +000072int keymap[] =
bellard5b0753e2005-03-01 21:37:28 +000073{
bellard87f48e62005-10-30 18:24:49 +000074// SdlI macI macH SdlH 104xtH 104xtC sdl
75 30, // 0 0x00 0x1e A QZ_a
76 31, // 1 0x01 0x1f S QZ_s
77 32, // 2 0x02 0x20 D QZ_d
78 33, // 3 0x03 0x21 F QZ_f
79 35, // 4 0x04 0x23 H QZ_h
80 34, // 5 0x05 0x22 G QZ_g
81 44, // 6 0x06 0x2c Z QZ_z
82 45, // 7 0x07 0x2d X QZ_x
83 46, // 8 0x08 0x2e C QZ_c
84 47, // 9 0x09 0x2f V QZ_v
85 0, // 10 0x0A Undefined
86 48, // 11 0x0B 0x30 B QZ_b
87 16, // 12 0x0C 0x10 Q QZ_q
88 17, // 13 0x0D 0x11 W QZ_w
89 18, // 14 0x0E 0x12 E QZ_e
90 19, // 15 0x0F 0x13 R QZ_r
91 21, // 16 0x10 0x15 Y QZ_y
92 20, // 17 0x11 0x14 T QZ_t
93 2, // 18 0x12 0x02 1 QZ_1
94 3, // 19 0x13 0x03 2 QZ_2
95 4, // 20 0x14 0x04 3 QZ_3
96 5, // 21 0x15 0x05 4 QZ_4
97 7, // 22 0x16 0x07 6 QZ_6
98 6, // 23 0x17 0x06 5 QZ_5
99 13, // 24 0x18 0x0d = QZ_EQUALS
100 10, // 25 0x19 0x0a 9 QZ_9
101 8, // 26 0x1A 0x08 7 QZ_7
102 12, // 27 0x1B 0x0c - QZ_MINUS
103 9, // 28 0x1C 0x09 8 QZ_8
104 11, // 29 0x1D 0x0b 0 QZ_0
105 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET
106 24, // 31 0x1F 0x18 O QZ_o
107 22, // 32 0x20 0x16 U QZ_u
108 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET
109 23, // 34 0x22 0x17 I QZ_i
110 25, // 35 0x23 0x19 P QZ_p
111 28, // 36 0x24 0x1c ENTER QZ_RETURN
112 38, // 37 0x25 0x26 L QZ_l
113 36, // 38 0x26 0x24 J QZ_j
114 40, // 39 0x27 0x28 ' QZ_QUOTE
115 37, // 40 0x28 0x25 K QZ_k
116 39, // 41 0x29 0x27 ; QZ_SEMICOLON
117 43, // 42 0x2A 0x2b \ QZ_BACKSLASH
118 51, // 43 0x2B 0x33 , QZ_COMMA
119 53, // 44 0x2C 0x35 / QZ_SLASH
120 49, // 45 0x2D 0x31 N QZ_n
121 50, // 46 0x2E 0x32 M QZ_m
122 52, // 47 0x2F 0x34 . QZ_PERIOD
123 15, // 48 0x30 0x0f TAB QZ_TAB
124 57, // 49 0x31 0x39 SPACE QZ_SPACE
125 41, // 50 0x32 0x29 ` QZ_BACKQUOTE
126 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE
127 0, // 52 0x34 Undefined
128 1, // 53 0x35 0x01 ESC QZ_ESCAPE
Peter Maydell88959192013-12-08 22:59:02 +0000129 220, // 54 0x36 0xdc E0,5C R GUI QZ_RMETA
130 219, // 55 0x37 0xdb E0,5B L GUI QZ_LMETA
bellard87f48e62005-10-30 18:24:49 +0000131 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT
132 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK
133 56, // 58 0x3A 0x38 L ALT QZ_LALT
134 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL
135 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT
136 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT
137 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL
138 0, // 63 0x3F Undefined
139 0, // 64 0x40 Undefined
140 0, // 65 0x41 Undefined
141 0, // 66 0x42 Undefined
142 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY
143 0, // 68 0x44 Undefined
144 78, // 69 0x45 0x4e KP + QZ_KP_PLUS
145 0, // 70 0x46 Undefined
146 69, // 71 0x47 0x45 NUM QZ_NUMLOCK
147 0, // 72 0x48 Undefined
148 0, // 73 0x49 Undefined
149 0, // 74 0x4A Undefined
150 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE
151 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER
152 0, // 77 0x4D undefined
153 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS
154 0, // 79 0x4F Undefined
155 0, // 80 0x50 Undefined
156 0, // 81 0x51 QZ_KP_EQUALS
157 82, // 82 0x52 0x52 KP 0 QZ_KP0
158 79, // 83 0x53 0x4f KP 1 QZ_KP1
159 80, // 84 0x54 0x50 KP 2 QZ_KP2
160 81, // 85 0x55 0x51 KP 3 QZ_KP3
161 75, // 86 0x56 0x4b KP 4 QZ_KP4
162 76, // 87 0x57 0x4c KP 5 QZ_KP5
163 77, // 88 0x58 0x4d KP 6 QZ_KP6
164 71, // 89 0x59 0x47 KP 7 QZ_KP7
165 0, // 90 0x5A Undefined
166 72, // 91 0x5B 0x48 KP 8 QZ_KP8
167 73, // 92 0x5C 0x49 KP 9 QZ_KP9
168 0, // 93 0x5D Undefined
169 0, // 94 0x5E Undefined
170 0, // 95 0x5F Undefined
171 63, // 96 0x60 0x3f F5 QZ_F5
172 64, // 97 0x61 0x40 F6 QZ_F6
173 65, // 98 0x62 0x41 F7 QZ_F7
174 61, // 99 0x63 0x3d F3 QZ_F3
175 66, // 100 0x64 0x42 F8 QZ_F8
176 67, // 101 0x65 0x43 F9 QZ_F9
177 0, // 102 0x66 Undefined
178 87, // 103 0x67 0x57 F11 QZ_F11
179 0, // 104 0x68 Undefined
thsc304f7e2008-01-22 23:25:15 +0000180 183,// 105 0x69 0xb7 QZ_PRINT
bellard87f48e62005-10-30 18:24:49 +0000181 0, // 106 0x6A Undefined
182 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK
183 0, // 108 0x6C Undefined
184 68, // 109 0x6D 0x44 F10 QZ_F10
185 0, // 110 0x6E Undefined
186 88, // 111 0x6F 0x58 F12 QZ_F12
187 0, // 112 0x70 Undefined
188 110,// 113 0x71 0x0 QZ_PAUSE
189 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT
190 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME
191 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP
192 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE
193 62, // 118 0x76 0x3e F4 QZ_F4
194 207,// 119 0x77 0xcf E0,4f END QZ_END
195 60, // 120 0x78 0x3c F2 QZ_F2
196 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN
197 59, // 122 0x7A 0x3b F1 QZ_F1
198 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT
199 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT
200 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN
201 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP
202/* completed according to http://www.libsdl.org/cgi/cvsweb.cgi/SDL12/src/video/quartz/SDL_QuartzKeys.h?rev=1.6&content-type=text/x-cvsweb-markup */
ths3b46e622007-09-17 08:09:54 +0000203
Peter Maydell49b9bd42013-12-08 22:59:03 +0000204/* Additional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */
bellard87f48e62005-10-30 18:24:49 +0000205/*
ths3b46e622007-09-17 08:09:54 +0000206 221 // 0xdd e0,5d APPS
207 // E0,2A,E0,37 PRNT SCRN
208 // E1,1D,45,E1,9D,C5 PAUSE
209 83 // 0x53 0x53 KP .
210// ACPI Scan Codes
211 222 // 0xde E0, 5E Power
212 223 // 0xdf E0, 5F Sleep
213 227 // 0xe3 E0, 63 Wake
214// Windows Multimedia Scan Codes
215 153 // 0x99 E0, 19 Next Track
216 144 // 0x90 E0, 10 Previous Track
217 164 // 0xa4 E0, 24 Stop
218 162 // 0xa2 E0, 22 Play/Pause
219 160 // 0xa0 E0, 20 Mute
220 176 // 0xb0 E0, 30 Volume Up
ths5fafdf22007-09-16 21:08:06 +0000221 174 // 0xae E0, 2E Volume Down
ths3b46e622007-09-17 08:09:54 +0000222 237 // 0xed E0, 6D Media Select
223 236 // 0xec E0, 6C E-Mail
224 161 // 0xa1 E0, 21 Calculator
ths5fafdf22007-09-16 21:08:06 +0000225 235 // 0xeb E0, 6B My Computer
ths3b46e622007-09-17 08:09:54 +0000226 229 // 0xe5 E0, 65 WWW Search
227 178 // 0xb2 E0, 32 WWW Home
228 234 // 0xea E0, 6A WWW Back
ths5fafdf22007-09-16 21:08:06 +0000229 233 // 0xe9 E0, 69 WWW Forward
ths3b46e622007-09-17 08:09:54 +0000230 232 // 0xe8 E0, 68 WWW Stop
ths5fafdf22007-09-16 21:08:06 +0000231 231 // 0xe7 E0, 67 WWW Refresh
ths3b46e622007-09-17 08:09:54 +0000232 230 // 0xe6 E0, 66 WWW Favorites
bellard87f48e62005-10-30 18:24:49 +0000233*/
bellard5b0753e2005-03-01 21:37:28 +0000234};
235
Andreas Färber77047bb2009-12-13 00:55:53 +0100236static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000237{
Stefan Weil5d701922013-12-07 14:48:05 +0100238 if (ARRAY_SIZE(keymap) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000239 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000240 return 0;
241 }
242 return keymap[keycode];
243}
244
thsc304f7e2008-01-22 23:25:15 +0000245
246
bellard5b0753e2005-03-01 21:37:28 +0000247/*
248 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000249 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000250 ------------------------------------------------------
251*/
thsc304f7e2008-01-22 23:25:15 +0000252@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000253{
thsc304f7e2008-01-22 23:25:15 +0000254 QEMUScreen screen;
255 NSWindow *fullScreenWindow;
256 float cx,cy,cw,ch,cdx,cdy;
257 CGDataProviderRef dataProviderRef;
258 int modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000259 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000260 BOOL isFullscreen;
261 BOOL isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100262 BOOL isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000263}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100264- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000265- (void) grabMouse;
266- (void) ungrabMouse;
267- (void) toggleFullScreen:(id)sender;
268- (void) handleEvent:(NSEvent *)event;
269- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100270/* The state surrounding mouse grabbing is potentially confusing.
271 * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
272 * pointing device an absolute-position one?"], but is only updated on
273 * next refresh.
274 * isMouseGrabbed tracks whether GUI events are directed to the guest;
275 * it controls whether special keys like Cmd get sent to the guest,
276 * and whether we capture the mouse when in non-absolute mode.
277 * isMouseDeassociated tracks whether we've told MacOSX to disassociate
278 * the mouse and mouse cursor position by calling
279 * CGAssociateMouseAndMouseCursorPosition(FALSE)
280 * (which basically happens if we grab in non-absolute mode).
281 */
Peter Maydell49b9bd42013-12-08 22:59:03 +0000282- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000283- (BOOL) isAbsoluteEnabled;
Peter Maydellf61c3872014-06-23 10:35:24 +0100284- (BOOL) isMouseDeassociated;
thsc304f7e2008-01-22 23:25:15 +0000285- (float) cdx;
286- (float) cdy;
287- (QEMUScreen) gscreen;
288@end
ths3b46e622007-09-17 08:09:54 +0000289
Andreas Färber7fee1992011-06-09 20:53:32 +0200290QemuCocoaView *cocoaView;
291
thsc304f7e2008-01-22 23:25:15 +0000292@implementation QemuCocoaView
293- (id)initWithFrame:(NSRect)frameRect
294{
295 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000296
thsc304f7e2008-01-22 23:25:15 +0000297 self = [super initWithFrame:frameRect];
298 if (self) {
pbrook95219892006-04-09 01:06:34 +0000299
thsc304f7e2008-01-22 23:25:15 +0000300 screen.bitsPerComponent = 8;
301 screen.bitsPerPixel = 32;
302 screen.width = frameRect.size.width;
303 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000304
thsc304f7e2008-01-22 23:25:15 +0000305 }
306 return self;
307}
bellard7c206a72005-12-18 19:18:45 +0000308
thsc304f7e2008-01-22 23:25:15 +0000309- (void) dealloc
310{
311 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000312
thsc304f7e2008-01-22 23:25:15 +0000313 if (dataProviderRef)
314 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000315
thsc304f7e2008-01-22 23:25:15 +0000316 [super dealloc];
317}
bellard5cbfcd02006-06-14 15:53:24 +0000318
Andreas Färberd50f71d2009-12-13 02:03:33 +0100319- (BOOL) isOpaque
320{
321 return YES;
322}
323
Peter Maydell5dd45be2014-06-23 10:35:23 +0100324- (BOOL) screenContainsPoint:(NSPoint) p
325{
326 return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
327}
328
Peter Maydell13aefd32014-06-23 10:35:25 +0100329- (void) hideCursor
330{
331 if (!cursor_hide) {
332 return;
333 }
334 [NSCursor hide];
335}
336
337- (void) unhideCursor
338{
339 if (!cursor_hide) {
340 return;
341 }
342 [NSCursor unhide];
343}
344
thsc304f7e2008-01-22 23:25:15 +0000345- (void) drawRect:(NSRect) rect
346{
347 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000348
thsc304f7e2008-01-22 23:25:15 +0000349 // get CoreGraphic context
350 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
351 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
352 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000353
thsc304f7e2008-01-22 23:25:15 +0000354 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000355 if (!dataProviderRef) {
356 // Draw request before any guest device has set up a framebuffer:
357 // just draw an opaque black rectangle
358 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
359 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
360 } else {
thsc304f7e2008-01-22 23:25:15 +0000361 CGImageRef imageRef = CGImageCreate(
362 screen.width, //width
363 screen.height, //height
364 screen.bitsPerComponent, //bitsPerComponent
365 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000366 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100367#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000368 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000369 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000370#else
371 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
372 kCGImageAlphaNoneSkipFirst, //bitmapInfo
373#endif
374 dataProviderRef, //provider
375 NULL, //decode
376 0, //interpolate
377 kCGRenderingIntentDefault //intent
378 );
Peter Maydellb63901d2015-05-19 09:11:17 +0100379 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
380 const NSRect *rectList;
Peter Maydellb63901d2015-05-19 09:11:17 +0100381 NSInteger rectCount;
Peter Maydellb63901d2015-05-19 09:11:17 +0100382 int i;
383 CGImageRef clipImageRef;
384 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000385
Peter Maydellb63901d2015-05-19 09:11:17 +0100386 [self getRectsBeingDrawn:&rectList count:&rectCount];
387 for (i = 0; i < rectCount; i++) {
388 clipRect.origin.x = rectList[i].origin.x / cdx;
389 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
390 clipRect.size.width = rectList[i].size.width / cdx;
391 clipRect.size.height = rectList[i].size.height / cdy;
392 clipImageRef = CGImageCreateWithImageInRect(
393 imageRef,
394 clipRect
395 );
396 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
397 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000398 }
thsc304f7e2008-01-22 23:25:15 +0000399 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000400 }
401}
402
thsc304f7e2008-01-22 23:25:15 +0000403- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000404{
thsc304f7e2008-01-22 23:25:15 +0000405 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000406
thsc304f7e2008-01-22 23:25:15 +0000407 if (isFullscreen) {
408 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
409 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100410
411 /* stretches video, but keeps same aspect ratio */
412 if (stretch_video == true) {
413 /* use smallest stretch value - prevents clipping on sides */
414 if (MIN(cdx, cdy) == cdx) {
415 cdy = cdx;
416 } else {
417 cdx = cdy;
418 }
419 } else { /* No stretching */
420 cdx = cdy = 1;
421 }
thsc304f7e2008-01-22 23:25:15 +0000422 cw = screen.width * cdx;
423 ch = screen.height * cdy;
424 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
425 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
426 } else {
427 cx = 0;
428 cy = 0;
429 cw = screen.width;
430 ch = screen.height;
431 cdx = 1.0;
432 cdy = 1.0;
433 }
bellard5b0753e2005-03-01 21:37:28 +0000434}
435
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100436- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000437{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100438 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000439
Peter Maydell8510d912013-03-18 20:28:21 +0000440 int w = surface_width(surface);
441 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100442 /* cdx == 0 means this is our very first surface, in which case we need
443 * to recalculate the content dimensions even if it happens to be the size
444 * of the initial empty window.
445 */
446 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000447
448 int oldh = screen.height;
449 if (isResize) {
450 // Resize before we trigger the redraw, or we'll redraw at the wrong size
451 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
452 screen.width = w;
453 screen.height = h;
454 [self setContentDimensions];
455 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
456 }
Peter Maydell8510d912013-03-18 20:28:21 +0000457
thsc304f7e2008-01-22 23:25:15 +0000458 // update screenBuffer
459 if (dataProviderRef)
460 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000461
aliguori9794f742009-03-04 19:25:22 +0000462 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000463 screen.bitsPerPixel = surface_bits_per_pixel(surface);
464 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000465
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100466 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000467
468 // update windows
469 if (isFullscreen) {
470 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000471 [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:NO animate:NO];
thsc304f7e2008-01-22 23:25:15 +0000472 } else {
473 if (qemu_name)
474 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000475 [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:YES animate:NO];
thsc304f7e2008-01-22 23:25:15 +0000476 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000477
478 if (isResize) {
479 [normalWindow center];
480 }
thsc304f7e2008-01-22 23:25:15 +0000481}
482
483- (void) toggleFullScreen:(id)sender
484{
485 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
486
487 if (isFullscreen) { // switch from fullscreen to desktop
488 isFullscreen = FALSE;
489 [self ungrabMouse];
490 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000491 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
492 [self exitFullScreenModeWithOptions:nil];
493 } else {
thsc304f7e2008-01-22 23:25:15 +0000494 [fullScreenWindow close];
495 [normalWindow setContentView: self];
496 [normalWindow makeKeyAndOrderFront: self];
497 [NSMenu setMenuBarVisible:YES];
thsc304f7e2008-01-22 23:25:15 +0000498 }
thsc304f7e2008-01-22 23:25:15 +0000499 } else { // switch from desktop to fullscreen
500 isFullscreen = TRUE;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100501 [normalWindow orderOut: nil]; /* Hide the window */
thsc304f7e2008-01-22 23:25:15 +0000502 [self grabMouse];
503 [self setContentDimensions];
thsc304f7e2008-01-22 23:25:15 +0000504 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
505 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
506 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
507 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
508 nil]];
509 } else {
thsc304f7e2008-01-22 23:25:15 +0000510 [NSMenu setMenuBarVisible:NO];
511 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
512 styleMask:NSBorderlessWindowMask
513 backing:NSBackingStoreBuffered
514 defer:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100515 [fullScreenWindow setAcceptsMouseMovedEvents: YES];
thsc304f7e2008-01-22 23:25:15 +0000516 [fullScreenWindow setHasShadow:NO];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100517 [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
518 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
519 [[fullScreenWindow contentView] addSubview: self];
thsc304f7e2008-01-22 23:25:15 +0000520 [fullScreenWindow makeKeyAndOrderFront:self];
thsc304f7e2008-01-22 23:25:15 +0000521 }
thsc304f7e2008-01-22 23:25:15 +0000522 }
523}
524
525- (void) handleEvent:(NSEvent *)event
526{
527 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
528
529 int buttons = 0;
530 int keycode;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100531 bool mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000532 NSPoint p = [event locationInWindow];
533
534 switch ([event type]) {
535 case NSFlagsChanged:
536 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000537
Peter Maydell49b9bd42013-12-08 22:59:03 +0000538 if ((keycode == 219 || keycode == 220) && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000539 /* Don't pass command key changes to guest unless mouse is grabbed */
540 keycode = 0;
541 }
542
thsc304f7e2008-01-22 23:25:15 +0000543 if (keycode) {
544 if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100545 qemu_input_event_send_key_number(dcl->con, keycode, true);
546 qemu_input_event_send_key_number(dcl->con, keycode, false);
Peter Maydell68c0aa62013-04-17 09:16:35 +0000547 } else if (qemu_console_is_graphic(NULL)) {
thsc304f7e2008-01-22 23:25:15 +0000548 if (modifiers_state[keycode] == 0) { // keydown
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100549 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000550 modifiers_state[keycode] = 1;
551 } else { // keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100552 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000553 modifiers_state[keycode] = 0;
554 }
555 }
556 }
557
558 // release Mouse grab when pressing ctrl+alt
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100559 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
thsc304f7e2008-01-22 23:25:15 +0000560 [self ungrabMouse];
561 }
562 break;
563 case NSKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000564 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000565
Peter Maydell88959192013-12-08 22:59:02 +0000566 // forward command key combos to the host UI unless the mouse is grabbed
Peter Maydell49b9bd42013-12-08 22:59:03 +0000567 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
thsc304f7e2008-01-22 23:25:15 +0000568 [NSApp sendEvent:event];
569 return;
570 }
571
572 // default
thsc304f7e2008-01-22 23:25:15 +0000573
574 // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
575 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
576 switch (keycode) {
577
578 // enable graphic console
579 case 0x02 ... 0x0a: // '1' to '9' keys
580 console_select(keycode - 0x02);
581 break;
582 }
583
584 // handle keys for graphic console
Peter Maydell68c0aa62013-04-17 09:16:35 +0000585 } else if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100586 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000587
588 // handlekeys for Monitor
589 } else {
590 int keysym = 0;
591 switch([event keyCode]) {
592 case 115:
593 keysym = QEMU_KEY_HOME;
594 break;
595 case 117:
596 keysym = QEMU_KEY_DELETE;
597 break;
598 case 119:
599 keysym = QEMU_KEY_END;
600 break;
601 case 123:
602 keysym = QEMU_KEY_LEFT;
603 break;
604 case 124:
605 keysym = QEMU_KEY_RIGHT;
606 break;
607 case 125:
608 keysym = QEMU_KEY_DOWN;
609 break;
610 case 126:
611 keysym = QEMU_KEY_UP;
612 break;
613 default:
614 {
615 NSString *ks = [event characters];
616 if ([ks length] > 0)
617 keysym = [ks characterAtIndex:0];
618 }
619 }
620 if (keysym)
621 kbd_put_keysym(keysym);
622 }
623 break;
624 case NSKeyUp:
625 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000626
627 // don't pass the guest a spurious key-up if we treated this
628 // command-key combo as a host UI action
Peter Maydell49b9bd42013-12-08 22:59:03 +0000629 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
Peter Maydell88959192013-12-08 22:59:02 +0000630 return;
631 }
632
Peter Maydell68c0aa62013-04-17 09:16:35 +0000633 if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100634 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000635 }
636 break;
637 case NSMouseMoved:
638 if (isAbsoluteEnabled) {
Peter Maydell5dd45be2014-06-23 10:35:23 +0100639 if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
Peter Maydellf61c3872014-06-23 10:35:24 +0100640 if (isMouseGrabbed) {
641 [self ungrabMouse];
thsc304f7e2008-01-22 23:25:15 +0000642 }
643 } else {
Peter Maydellf61c3872014-06-23 10:35:24 +0100644 if (!isMouseGrabbed) {
645 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000646 }
647 }
648 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100649 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000650 break;
651 case NSLeftMouseDown:
652 if ([event modifierFlags] & NSCommandKeyMask) {
653 buttons |= MOUSE_EVENT_RBUTTON;
654 } else {
655 buttons |= MOUSE_EVENT_LBUTTON;
656 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100657 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000658 break;
659 case NSRightMouseDown:
660 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100661 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000662 break;
663 case NSOtherMouseDown:
664 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100665 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000666 break;
667 case NSLeftMouseDragged:
668 if ([event modifierFlags] & NSCommandKeyMask) {
669 buttons |= MOUSE_EVENT_RBUTTON;
670 } else {
671 buttons |= MOUSE_EVENT_LBUTTON;
672 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100673 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000674 break;
675 case NSRightMouseDragged:
676 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100677 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000678 break;
679 case NSOtherMouseDragged:
680 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100681 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000682 break;
683 case NSLeftMouseUp:
Peter Maydellf61c3872014-06-23 10:35:24 +0100684 mouse_event = true;
685 if (!isMouseGrabbed && [self screenContainsPoint:p]) {
686 [self grabMouse];
thsc304f7e2008-01-22 23:25:15 +0000687 }
688 break;
689 case NSRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100690 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000691 break;
692 case NSOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100693 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000694 break;
695 case NSScrollWheel:
Peter Maydellf61c3872014-06-23 10:35:24 +0100696 if (isMouseGrabbed) {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100697 buttons |= ([event deltaY] < 0) ?
698 MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
thsc304f7e2008-01-22 23:25:15 +0000699 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100700 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000701 break;
702 default:
703 [NSApp sendEvent:event];
704 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100705
706 if (mouse_event) {
707 if (last_buttons != buttons) {
708 static uint32_t bmap[INPUT_BUTTON_MAX] = {
709 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
710 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
711 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
712 [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP,
713 [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
714 };
715 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
716 last_buttons = buttons;
717 }
Peter Maydellf61c3872014-06-23 10:35:24 +0100718 if (isMouseGrabbed) {
719 if (isAbsoluteEnabled) {
720 /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
721 * The check on screenContainsPoint is to avoid sending out of range values for
722 * clicks in the titlebar.
723 */
724 if ([self screenContainsPoint:p]) {
725 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width);
726 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height);
727 }
728 } else {
729 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
730 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
731 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100732 } else {
733 [NSApp sendEvent:event];
734 }
735 qemu_input_event_sync();
736 }
thsc304f7e2008-01-22 23:25:15 +0000737}
738
739- (void) grabMouse
740{
741 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
742
743 if (!isFullscreen) {
744 if (qemu_name)
745 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
746 else
747 [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
748 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100749 [self hideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100750 if (!isAbsoluteEnabled) {
751 isMouseDeassociated = TRUE;
752 CGAssociateMouseAndMouseCursorPosition(FALSE);
753 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000754 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000755}
756
757- (void) ungrabMouse
758{
759 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
760
761 if (!isFullscreen) {
762 if (qemu_name)
763 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
764 else
765 [normalWindow setTitle:@"QEMU"];
766 }
Peter Maydell13aefd32014-06-23 10:35:25 +0100767 [self unhideCursor];
Peter Maydellf61c3872014-06-23 10:35:24 +0100768 if (isMouseDeassociated) {
769 CGAssociateMouseAndMouseCursorPosition(TRUE);
770 isMouseDeassociated = FALSE;
771 }
Peter Maydell49b9bd42013-12-08 22:59:03 +0000772 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000773}
774
775- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000776- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000777- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
Peter Maydellf61c3872014-06-23 10:35:24 +0100778- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
thsc304f7e2008-01-22 23:25:15 +0000779- (float) cdx {return cdx;}
780- (float) cdy {return cdy;}
781- (QEMUScreen) gscreen {return screen;}
bellard5b0753e2005-03-01 21:37:28 +0000782@end
783
784
thsc304f7e2008-01-22 23:25:15 +0000785
bellard5b0753e2005-03-01 21:37:28 +0000786/*
787 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000788 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000789 ------------------------------------------------------
790*/
thsc304f7e2008-01-22 23:25:15 +0000791@interface QemuCocoaAppController : NSObject
Peter Maydell2a4c8c52015-05-19 09:11:18 +0100792#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
793 <NSApplicationDelegate>
794#endif
bellard5b0753e2005-03-01 21:37:28 +0000795{
796}
bellard5b0753e2005-03-01 21:37:28 +0000797- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
Peter Maydellde1aade2015-05-19 09:11:18 +0100798- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100799- (void)doToggleFullScreen:(id)sender;
thsc304f7e2008-01-22 23:25:15 +0000800- (void)toggleFullScreen:(id)sender;
801- (void)showQEMUDoc:(id)sender;
802- (void)showQEMUTec:(id)sender;
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100803- (void)zoomToFit:(id) sender;
Programmingkidb4c6a112015-05-19 09:11:18 +0100804- (void)displayConsole:(id)sender;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100805- (void)pauseQEMU:(id)sender;
806- (void)resumeQEMU:(id)sender;
807- (void)displayPause;
808- (void)removePause;
John Arbuckle27074612015-06-19 10:53:27 +0100809- (void)restartQEMU:(id)sender;
810- (void)powerDownQEMU:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +0000811@end
812
thsc304f7e2008-01-22 23:25:15 +0000813@implementation QemuCocoaAppController
814- (id) init
815{
816 COCOA_DEBUG("QemuCocoaAppController: init\n");
817
818 self = [super init];
819 if (self) {
820
821 // create a view and add it to the window
822 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
823 if(!cocoaView) {
824 fprintf(stderr, "(cocoa) can't create a view\n");
825 exit(1);
826 }
827
828 // create a window
829 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
830 styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
831 backing:NSBackingStoreBuffered defer:NO];
832 if(!normalWindow) {
833 fprintf(stderr, "(cocoa) can't create window\n");
834 exit(1);
835 }
836 [normalWindow setAcceptsMouseMovedEvents:YES];
837 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]];
838 [normalWindow setContentView:cocoaView];
Peter Maydell81801ae2015-05-19 09:11:18 +0100839#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
Andreas Färber561ef252009-12-13 03:06:20 +0100840 [normalWindow useOptimizedDrawing:YES];
Peter Maydell81801ae2015-05-19 09:11:18 +0100841#endif
thsc304f7e2008-01-22 23:25:15 +0000842 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +0000843 [normalWindow center];
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100844 stretch_video = false;
John Arbuckle8524f1c2015-06-19 10:53:27 +0100845
846 /* Used for displaying pause on the screen */
847 pauseLabel = [NSTextField new];
848 [pauseLabel setBezeled:YES];
849 [pauseLabel setDrawsBackground:YES];
850 [pauseLabel setBackgroundColor: [NSColor whiteColor]];
851 [pauseLabel setEditable:NO];
852 [pauseLabel setSelectable:NO];
853 [pauseLabel setStringValue: @"Paused"];
854 [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
855 [pauseLabel setTextColor: [NSColor blackColor]];
856 [pauseLabel sizeToFit];
thsc304f7e2008-01-22 23:25:15 +0000857 }
858 return self;
859}
860
861- (void) dealloc
862{
863 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
864
865 if (cocoaView)
866 [cocoaView release];
867 [super dealloc];
868}
869
bellard5b0753e2005-03-01 21:37:28 +0000870- (void)applicationDidFinishLaunching: (NSNotification *) note
871{
thsc304f7e2008-01-22 23:25:15 +0000872 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
bellard5a246932005-04-07 20:35:06 +0000873
Peter Maydell49b9bd42013-12-08 22:59:03 +0000874 // Display an open dialog box if no arguments were passed or
thsc304f7e2008-01-22 23:25:15 +0000875 // if qemu was launched from the finder ( the Finder passes "-psn" )
876 if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) {
bellard5b0753e2005-03-01 21:37:28 +0000877 NSOpenPanel *op = [[NSOpenPanel alloc] init];
bellard5b0753e2005-03-01 21:37:28 +0000878 [op setPrompt:@"Boot image"];
bellard5b0753e2005-03-01 21:37:28 +0000879 [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000880 NSArray *filetypes = [NSArray arrayWithObjects:@"img", @"iso", @"dmg",
Stefan Hajnoczi550830f2014-09-16 15:24:24 +0100881 @"qcow", @"qcow2", @"cloop", @"vmdk", nil];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000882#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
883 [op setAllowedFileTypes:filetypes];
884 [op beginSheetModalForWindow:normalWindow
885 completionHandler:^(NSInteger returnCode)
886 { [self openPanelDidEnd:op
887 returnCode:returnCode contextInfo:NULL ]; } ];
888#else
889 // Compatibility code for pre-10.6, using deprecated method
890 [op beginSheetForDirectory:nil file:nil types:filetypes
thsc304f7e2008-01-22 23:25:15 +0000891 modalForWindow:normalWindow modalDelegate:self
bellard5b0753e2005-03-01 21:37:28 +0000892 didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000893#endif
thsc304f7e2008-01-22 23:25:15 +0000894 } else {
Stefan Weil5cbdb3a2012-04-07 09:23:39 +0200895 // or launch QEMU, with the global args
thsc304f7e2008-01-22 23:25:15 +0000896 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5a246932005-04-07 20:35:06 +0000897 }
bellard5b0753e2005-03-01 21:37:28 +0000898}
899
900- (void)applicationWillTerminate:(NSNotification *)aNotification
901{
thsc304f7e2008-01-22 23:25:15 +0000902 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
903
bellard5b0753e2005-03-01 21:37:28 +0000904 qemu_system_shutdown_request();
bellard5b0753e2005-03-01 21:37:28 +0000905 exit(0);
906}
907
Andreas Färber41ea49b2009-12-14 22:13:27 +0100908- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
909{
910 return YES;
911}
912
thsc304f7e2008-01-22 23:25:15 +0000913- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
914{
915 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
916
917 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +0200918 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +0000919 exit(status);
920}
921
Peter Maydellde1aade2015-05-19 09:11:18 +0100922- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
bellard5b0753e2005-03-01 21:37:28 +0000923{
thsc304f7e2008-01-22 23:25:15 +0000924 COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n");
ths3b46e622007-09-17 08:09:54 +0000925
Peter Maydell86179892015-05-19 09:11:18 +0100926 /* The NSFileHandlingPanelOKButton/NSFileHandlingPanelCancelButton values for
927 * returnCode strictly only apply for the 10.6-and-up beginSheetModalForWindow
928 * API. For the legacy pre-10.6 beginSheetForDirectory API they are NSOKButton
929 * and NSCancelButton. However conveniently the values are the same.
930 * We use the non-legacy names because the others are deprecated in OSX 10.10.
931 */
932 if (returnCode == NSFileHandlingPanelCancelButton) {
thsc304f7e2008-01-22 23:25:15 +0000933 exit(0);
Peter Maydell86179892015-05-19 09:11:18 +0100934 } else if (returnCode == NSFileHandlingPanelOKButton) {
Peter Maydell8bb3f1e2013-04-22 10:29:48 +0000935 char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding];
ths3b46e622007-09-17 08:09:54 +0000936
Peter Maydell98db4292013-12-08 22:59:06 +0000937 char **argv = g_new(char *, 4);
ths3b46e622007-09-17 08:09:54 +0000938
Andreas Färber13766eb2011-10-30 23:19:54 +0100939 [sheet close];
940
Peter Maydell98db4292013-12-08 22:59:06 +0000941 argv[0] = g_strdup(gArgv[0]);
942 argv[1] = g_strdup("-hda");
943 argv[2] = g_strdup(img);
944 argv[3] = NULL;
ths3b46e622007-09-17 08:09:54 +0000945
Peter Maydell98db4292013-12-08 22:59:06 +0000946 // printf("Using argc %d argv %s -hda %s\n", 3, gArgv[0], img);
ths3b46e622007-09-17 08:09:54 +0000947
bellard5b0753e2005-03-01 21:37:28 +0000948 [self startEmulationWithArgc:3 argv:(char**)argv];
949 }
950}
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100951
952/* We abstract the method called by the Enter Fullscreen menu item
953 * because Mac OS 10.7 and higher disables it. This is because of the
954 * menu item's old selector's name toggleFullScreen:
955 */
956- (void) doToggleFullScreen:(id)sender
957{
958 [self toggleFullScreen:(id)sender];
959}
960
thsc304f7e2008-01-22 23:25:15 +0000961- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +0000962{
thsc304f7e2008-01-22 23:25:15 +0000963 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
964
965 [cocoaView toggleFullScreen:sender];
966}
967
968- (void)showQEMUDoc:(id)sender
969{
970 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
971
972 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
973 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
974}
975
976- (void)showQEMUTec:(id)sender
977{
978 COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
979
980 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
981 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
bellard5b0753e2005-03-01 21:37:28 +0000982}
Programmingkid5d1b2ee2015-05-19 09:11:17 +0100983
984/* Stretches video to fit host monitor size */
985- (void)zoomToFit:(id) sender
986{
987 stretch_video = !stretch_video;
988 if (stretch_video == true) {
989 [sender setState: NSOnState];
990 } else {
991 [sender setState: NSOffState];
992 }
993}
bellard5b0753e2005-03-01 21:37:28 +0000994
Programmingkidb4c6a112015-05-19 09:11:18 +0100995/* Displays the console on the screen */
996- (void)displayConsole:(id)sender
997{
998 console_select([sender tag]);
999}
John Arbuckle8524f1c2015-06-19 10:53:27 +01001000
1001/* Pause the guest */
1002- (void)pauseQEMU:(id)sender
1003{
1004 qmp_stop(NULL);
1005 [sender setEnabled: NO];
1006 [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
1007 [self displayPause];
1008}
1009
1010/* Resume running the guest operating system */
1011- (void)resumeQEMU:(id) sender
1012{
1013 qmp_cont(NULL);
1014 [sender setEnabled: NO];
1015 [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
1016 [self removePause];
1017}
1018
1019/* Displays the word pause on the screen */
1020- (void)displayPause
1021{
1022 /* Coordinates have to be calculated each time because the window can change its size */
1023 int xCoord, yCoord, width, height;
1024 xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
1025 yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
1026 width = [pauseLabel frame].size.width;
1027 height = [pauseLabel frame].size.height;
1028 [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
1029 [cocoaView addSubview: pauseLabel];
1030}
1031
1032/* Removes the word pause from the screen */
1033- (void)removePause
1034{
1035 [pauseLabel removeFromSuperview];
1036}
1037
John Arbuckle27074612015-06-19 10:53:27 +01001038/* Restarts QEMU */
1039- (void)restartQEMU:(id)sender
1040{
1041 qmp_system_reset(NULL);
1042}
1043
1044/* Powers down QEMU */
1045- (void)powerDownQEMU:(id)sender
1046{
1047 qmp_system_powerdown(NULL);
1048}
1049
Programmingkidb4c6a112015-05-19 09:11:18 +01001050@end
bellard5b0753e2005-03-01 21:37:28 +00001051
thsc304f7e2008-01-22 23:25:15 +00001052
thsc304f7e2008-01-22 23:25:15 +00001053int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +00001054
thsc304f7e2008-01-22 23:25:15 +00001055 gArgc = argc;
1056 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +01001057 int i;
1058
1059 /* In case we don't need to display a window, let's not do that */
1060 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001061 const char *opt = argv[i];
1062
1063 if (opt[0] == '-') {
1064 /* Treat --foo the same as -foo. */
1065 if (opt[1] == '-') {
1066 opt++;
1067 }
Alexandre Raymond98514842011-05-29 18:22:49 -04001068 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
1069 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001070 !strcmp(opt, "-nographic") ||
1071 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +02001072 !strcmp(opt, "-curses") ||
1073 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +02001074 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +01001075 }
Andreas Färberf4918802009-12-13 02:11:44 +01001076 }
1077 }
ths3b46e622007-09-17 08:09:54 +00001078
thsc304f7e2008-01-22 23:25:15 +00001079 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +00001080
Peter Maydell42a5dfe2013-04-22 10:29:47 +00001081 // Pull this console process up to being a fully-fledged graphical
1082 // app with a menubar and Dock icon
1083 ProcessSerialNumber psn = { 0, kCurrentProcess };
1084 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
1085
1086 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +00001087
thsc304f7e2008-01-22 23:25:15 +00001088 // Add menus
1089 NSMenu *menu;
1090 NSMenuItem *menuItem;
1091
bellard5b0753e2005-03-01 21:37:28 +00001092 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +00001093
thsc304f7e2008-01-22 23:25:15 +00001094 // Application menu
1095 menu = [[NSMenu alloc] initWithTitle:@""];
1096 [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
1097 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1098 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
1099 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
1100 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
1101 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
1102 [menu addItem:[NSMenuItem separatorItem]]; //Separator
1103 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
1104 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
1105 [menuItem setSubmenu:menu];
1106 [[NSApp mainMenu] addItem:menuItem];
1107 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +00001108
John Arbuckle8524f1c2015-06-19 10:53:27 +01001109 // Machine menu
1110 menu = [[NSMenu alloc] initWithTitle: @"Machine"];
1111 [menu setAutoenablesItems: NO];
1112 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
1113 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
1114 [menu addItem: menuItem];
1115 [menuItem setEnabled: NO];
John Arbuckle27074612015-06-19 10:53:27 +01001116 [menu addItem: [NSMenuItem separatorItem]];
1117 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
1118 [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
John Arbuckle8524f1c2015-06-19 10:53:27 +01001119 menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
1120 [menuItem setSubmenu:menu];
1121 [[NSApp mainMenu] addItem:menuItem];
1122
thsc304f7e2008-01-22 23:25:15 +00001123 // View menu
1124 menu = [[NSMenu alloc] initWithTitle:@"View"];
Programmingkid5d1b2ee2015-05-19 09:11:17 +01001125 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
1126 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
thsc304f7e2008-01-22 23:25:15 +00001127 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
1128 [menuItem setSubmenu:menu];
1129 [[NSApp mainMenu] addItem:menuItem];
1130
1131 // Window menu
1132 menu = [[NSMenu alloc] initWithTitle:@"Window"];
1133 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
1134 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1135 [menuItem setSubmenu:menu];
1136 [[NSApp mainMenu] addItem:menuItem];
1137 [NSApp setWindowsMenu:menu];
1138
1139 // Help menu
1140 menu = [[NSMenu alloc] initWithTitle:@"Help"];
1141 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
1142 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
1143 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
1144 [menuItem setSubmenu:menu];
1145 [[NSApp mainMenu] addItem:menuItem];
1146
1147 // Create an Application controller
1148 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
1149 [NSApp setDelegate:appController];
1150
1151 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +00001152 [NSApp run];
ths3b46e622007-09-17 08:09:54 +00001153
thsc304f7e2008-01-22 23:25:15 +00001154 [appController release];
bellard5b0753e2005-03-01 21:37:28 +00001155 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001156
bellard5b0753e2005-03-01 21:37:28 +00001157 return 0;
1158}
thsc304f7e2008-01-22 23:25:15 +00001159
1160
1161
1162#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001163static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001164 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001165{
Peter Maydell6e657e62013-04-22 10:29:46 +00001166 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1167
thsc304f7e2008-01-22 23:25:15 +00001168 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1169
1170 NSRect rect;
1171 if ([cocoaView cdx] == 1.0) {
1172 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1173 } else {
1174 rect = NSMakeRect(
1175 x * [cocoaView cdx],
1176 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1177 w * [cocoaView cdx],
1178 h * [cocoaView cdy]);
1179 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001180 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001181
1182 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001183}
1184
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001185static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001186 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001187{
Peter Maydell6e657e62013-04-22 10:29:46 +00001188 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001189
Peter Maydell6e657e62013-04-22 10:29:46 +00001190 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001191 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001192 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001193}
1194
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001195static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001196{
Peter Maydell6e657e62013-04-22 10:29:46 +00001197 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1198
thsc304f7e2008-01-22 23:25:15 +00001199 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
1200
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001201 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001202 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001203 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001204 [cocoaView ungrabMouse];
1205 }
1206 }
1207 [cocoaView setAbsoluteEnabled:YES];
1208 }
1209
1210 NSDate *distantPast;
1211 NSEvent *event;
1212 distantPast = [NSDate distantPast];
1213 do {
1214 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
1215 inMode: NSDefaultRunLoopMode dequeue:YES];
1216 if (event != nil) {
1217 [cocoaView handleEvent:event];
1218 }
1219 } while(event != nil);
Peter Maydell68c0aa62013-04-17 09:16:35 +00001220 graphic_hw_update(NULL);
Peter Maydell6e657e62013-04-22 10:29:46 +00001221 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001222}
1223
1224static void cocoa_cleanup(void)
1225{
1226 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001227 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001228}
1229
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001230static const DisplayChangeListenerOps dcl_ops = {
1231 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001232 .dpy_gfx_update = cocoa_update,
1233 .dpy_gfx_switch = cocoa_switch,
1234 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001235};
1236
Programmingkidb4c6a112015-05-19 09:11:18 +01001237/* Returns a name for a given console */
1238static NSString * getConsoleName(QemuConsole * console)
1239{
1240 return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
1241}
1242
1243/* Add an entry to the View menu for each console */
1244static void add_console_menu_entries(void)
1245{
1246 NSMenu *menu;
1247 NSMenuItem *menuItem;
1248 int index = 0;
1249
1250 menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1251
1252 [menu addItem:[NSMenuItem separatorItem]];
1253
1254 while (qemu_console_lookup_by_index(index) != NULL) {
1255 menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1256 action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1257 [menuItem setTag: index];
1258 [menu addItem: menuItem];
1259 index++;
1260 }
1261}
1262
thsc304f7e2008-01-22 23:25:15 +00001263void cocoa_display_init(DisplayState *ds, int full_screen)
1264{
1265 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1266
Programmingkid43227af2015-05-19 09:11:17 +01001267 /* if fullscreen mode is to be used */
1268 if (full_screen == true) {
1269 [NSApp activateIgnoringOtherApps: YES];
1270 [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
1271 }
1272
Blue Swirl58a06672011-08-21 18:42:08 +00001273 dcl = g_malloc0(sizeof(DisplayChangeListener));
1274
aliguori9794f742009-03-04 19:25:22 +00001275 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001276 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001277 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001278
1279 // register cleanup function
1280 atexit(cocoa_cleanup);
Programmingkidb4c6a112015-05-19 09:11:18 +01001281
1282 /* At this point QEMU has created all the consoles, so we can add View
1283 * menu entries for them.
1284 */
1285 add_console_menu_entries();
thsc304f7e2008-01-22 23:25:15 +00001286}