blob: a270a464d8c134424e748bf98fec77978a3c03cd [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"
bellardda4dbf72005-03-02 22:22:43 +000032
Andreas Färber38ec7b52009-12-13 01:52:29 +010033#ifndef MAC_OS_X_VERSION_10_4
34#define MAC_OS_X_VERSION_10_4 1040
35#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010036#ifndef MAC_OS_X_VERSION_10_5
37#define MAC_OS_X_VERSION_10_5 1050
38#endif
Peter Maydell2ba9de62013-04-22 10:29:49 +000039#ifndef MAC_OS_X_VERSION_10_6
40#define MAC_OS_X_VERSION_10_6 1060
41#endif
Andreas Färber44e4c0b2009-12-13 00:45:40 +010042
bellard5b0753e2005-03-01 21:37:28 +000043
thsc304f7e2008-01-22 23:25:15 +000044//#define DEBUG
45
46#ifdef DEBUG
47#define COCOA_DEBUG(...) { (void) fprintf (stdout, __VA_ARGS__); }
48#else
49#define COCOA_DEBUG(...) ((void) 0)
50#endif
51
52#define cgrect(nsrect) (*(CGRect *)&(nsrect))
thsc304f7e2008-01-22 23:25:15 +000053
54typedef struct {
55 int width;
56 int height;
57 int bitsPerComponent;
58 int bitsPerPixel;
59} QEMUScreen;
60
thsc304f7e2008-01-22 23:25:15 +000061NSWindow *normalWindow;
aliguori9794f742009-03-04 19:25:22 +000062static DisplayChangeListener *dcl;
Gerd Hoffmann21bae112013-12-04 14:08:04 +010063static int last_buttons;
bellard5b0753e2005-03-01 21:37:28 +000064
65int gArgc;
66char **gArgv;
bellard5b0753e2005-03-01 21:37:28 +000067
thsc304f7e2008-01-22 23:25:15 +000068// keymap conversion
bellard87f48e62005-10-30 18:24:49 +000069int keymap[] =
bellard5b0753e2005-03-01 21:37:28 +000070{
bellard87f48e62005-10-30 18:24:49 +000071// SdlI macI macH SdlH 104xtH 104xtC sdl
72 30, // 0 0x00 0x1e A QZ_a
73 31, // 1 0x01 0x1f S QZ_s
74 32, // 2 0x02 0x20 D QZ_d
75 33, // 3 0x03 0x21 F QZ_f
76 35, // 4 0x04 0x23 H QZ_h
77 34, // 5 0x05 0x22 G QZ_g
78 44, // 6 0x06 0x2c Z QZ_z
79 45, // 7 0x07 0x2d X QZ_x
80 46, // 8 0x08 0x2e C QZ_c
81 47, // 9 0x09 0x2f V QZ_v
82 0, // 10 0x0A Undefined
83 48, // 11 0x0B 0x30 B QZ_b
84 16, // 12 0x0C 0x10 Q QZ_q
85 17, // 13 0x0D 0x11 W QZ_w
86 18, // 14 0x0E 0x12 E QZ_e
87 19, // 15 0x0F 0x13 R QZ_r
88 21, // 16 0x10 0x15 Y QZ_y
89 20, // 17 0x11 0x14 T QZ_t
90 2, // 18 0x12 0x02 1 QZ_1
91 3, // 19 0x13 0x03 2 QZ_2
92 4, // 20 0x14 0x04 3 QZ_3
93 5, // 21 0x15 0x05 4 QZ_4
94 7, // 22 0x16 0x07 6 QZ_6
95 6, // 23 0x17 0x06 5 QZ_5
96 13, // 24 0x18 0x0d = QZ_EQUALS
97 10, // 25 0x19 0x0a 9 QZ_9
98 8, // 26 0x1A 0x08 7 QZ_7
99 12, // 27 0x1B 0x0c - QZ_MINUS
100 9, // 28 0x1C 0x09 8 QZ_8
101 11, // 29 0x1D 0x0b 0 QZ_0
102 27, // 30 0x1E 0x1b ] QZ_RIGHTBRACKET
103 24, // 31 0x1F 0x18 O QZ_o
104 22, // 32 0x20 0x16 U QZ_u
105 26, // 33 0x21 0x1a [ QZ_LEFTBRACKET
106 23, // 34 0x22 0x17 I QZ_i
107 25, // 35 0x23 0x19 P QZ_p
108 28, // 36 0x24 0x1c ENTER QZ_RETURN
109 38, // 37 0x25 0x26 L QZ_l
110 36, // 38 0x26 0x24 J QZ_j
111 40, // 39 0x27 0x28 ' QZ_QUOTE
112 37, // 40 0x28 0x25 K QZ_k
113 39, // 41 0x29 0x27 ; QZ_SEMICOLON
114 43, // 42 0x2A 0x2b \ QZ_BACKSLASH
115 51, // 43 0x2B 0x33 , QZ_COMMA
116 53, // 44 0x2C 0x35 / QZ_SLASH
117 49, // 45 0x2D 0x31 N QZ_n
118 50, // 46 0x2E 0x32 M QZ_m
119 52, // 47 0x2F 0x34 . QZ_PERIOD
120 15, // 48 0x30 0x0f TAB QZ_TAB
121 57, // 49 0x31 0x39 SPACE QZ_SPACE
122 41, // 50 0x32 0x29 ` QZ_BACKQUOTE
123 14, // 51 0x33 0x0e BKSP QZ_BACKSPACE
124 0, // 52 0x34 Undefined
125 1, // 53 0x35 0x01 ESC QZ_ESCAPE
Peter Maydell88959192013-12-08 22:59:02 +0000126 220, // 54 0x36 0xdc E0,5C R GUI QZ_RMETA
127 219, // 55 0x37 0xdb E0,5B L GUI QZ_LMETA
bellard87f48e62005-10-30 18:24:49 +0000128 42, // 56 0x38 0x2a L SHFT QZ_LSHIFT
129 58, // 57 0x39 0x3a CAPS QZ_CAPSLOCK
130 56, // 58 0x3A 0x38 L ALT QZ_LALT
131 29, // 59 0x3B 0x1d L CTRL QZ_LCTRL
132 54, // 60 0x3C 0x36 R SHFT QZ_RSHIFT
133 184,// 61 0x3D 0xb8 E0,38 R ALT QZ_RALT
134 157,// 62 0x3E 0x9d E0,1D R CTRL QZ_RCTRL
135 0, // 63 0x3F Undefined
136 0, // 64 0x40 Undefined
137 0, // 65 0x41 Undefined
138 0, // 66 0x42 Undefined
139 55, // 67 0x43 0x37 KP * QZ_KP_MULTIPLY
140 0, // 68 0x44 Undefined
141 78, // 69 0x45 0x4e KP + QZ_KP_PLUS
142 0, // 70 0x46 Undefined
143 69, // 71 0x47 0x45 NUM QZ_NUMLOCK
144 0, // 72 0x48 Undefined
145 0, // 73 0x49 Undefined
146 0, // 74 0x4A Undefined
147 181,// 75 0x4B 0xb5 E0,35 KP / QZ_KP_DIVIDE
148 152,// 76 0x4C 0x9c E0,1C KP EN QZ_KP_ENTER
149 0, // 77 0x4D undefined
150 74, // 78 0x4E 0x4a KP - QZ_KP_MINUS
151 0, // 79 0x4F Undefined
152 0, // 80 0x50 Undefined
153 0, // 81 0x51 QZ_KP_EQUALS
154 82, // 82 0x52 0x52 KP 0 QZ_KP0
155 79, // 83 0x53 0x4f KP 1 QZ_KP1
156 80, // 84 0x54 0x50 KP 2 QZ_KP2
157 81, // 85 0x55 0x51 KP 3 QZ_KP3
158 75, // 86 0x56 0x4b KP 4 QZ_KP4
159 76, // 87 0x57 0x4c KP 5 QZ_KP5
160 77, // 88 0x58 0x4d KP 6 QZ_KP6
161 71, // 89 0x59 0x47 KP 7 QZ_KP7
162 0, // 90 0x5A Undefined
163 72, // 91 0x5B 0x48 KP 8 QZ_KP8
164 73, // 92 0x5C 0x49 KP 9 QZ_KP9
165 0, // 93 0x5D Undefined
166 0, // 94 0x5E Undefined
167 0, // 95 0x5F Undefined
168 63, // 96 0x60 0x3f F5 QZ_F5
169 64, // 97 0x61 0x40 F6 QZ_F6
170 65, // 98 0x62 0x41 F7 QZ_F7
171 61, // 99 0x63 0x3d F3 QZ_F3
172 66, // 100 0x64 0x42 F8 QZ_F8
173 67, // 101 0x65 0x43 F9 QZ_F9
174 0, // 102 0x66 Undefined
175 87, // 103 0x67 0x57 F11 QZ_F11
176 0, // 104 0x68 Undefined
thsc304f7e2008-01-22 23:25:15 +0000177 183,// 105 0x69 0xb7 QZ_PRINT
bellard87f48e62005-10-30 18:24:49 +0000178 0, // 106 0x6A Undefined
179 70, // 107 0x6B 0x46 SCROLL QZ_SCROLLOCK
180 0, // 108 0x6C Undefined
181 68, // 109 0x6D 0x44 F10 QZ_F10
182 0, // 110 0x6E Undefined
183 88, // 111 0x6F 0x58 F12 QZ_F12
184 0, // 112 0x70 Undefined
185 110,// 113 0x71 0x0 QZ_PAUSE
186 210,// 114 0x72 0xd2 E0,52 INSERT QZ_INSERT
187 199,// 115 0x73 0xc7 E0,47 HOME QZ_HOME
188 201,// 116 0x74 0xc9 E0,49 PG UP QZ_PAGEUP
189 211,// 117 0x75 0xd3 E0,53 DELETE QZ_DELETE
190 62, // 118 0x76 0x3e F4 QZ_F4
191 207,// 119 0x77 0xcf E0,4f END QZ_END
192 60, // 120 0x78 0x3c F2 QZ_F2
193 209,// 121 0x79 0xd1 E0,51 PG DN QZ_PAGEDOWN
194 59, // 122 0x7A 0x3b F1 QZ_F1
195 203,// 123 0x7B 0xcb e0,4B L ARROW QZ_LEFT
196 205,// 124 0x7C 0xcd e0,4D R ARROW QZ_RIGHT
197 208,// 125 0x7D 0xd0 E0,50 D ARROW QZ_DOWN
198 200,// 126 0x7E 0xc8 E0,48 U ARROW QZ_UP
199/* 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 +0000200
Peter Maydell49b9bd42013-12-08 22:59:03 +0000201/* Additional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */
bellard87f48e62005-10-30 18:24:49 +0000202/*
ths3b46e622007-09-17 08:09:54 +0000203 221 // 0xdd e0,5d APPS
204 // E0,2A,E0,37 PRNT SCRN
205 // E1,1D,45,E1,9D,C5 PAUSE
206 83 // 0x53 0x53 KP .
207// ACPI Scan Codes
208 222 // 0xde E0, 5E Power
209 223 // 0xdf E0, 5F Sleep
210 227 // 0xe3 E0, 63 Wake
211// Windows Multimedia Scan Codes
212 153 // 0x99 E0, 19 Next Track
213 144 // 0x90 E0, 10 Previous Track
214 164 // 0xa4 E0, 24 Stop
215 162 // 0xa2 E0, 22 Play/Pause
216 160 // 0xa0 E0, 20 Mute
217 176 // 0xb0 E0, 30 Volume Up
ths5fafdf22007-09-16 21:08:06 +0000218 174 // 0xae E0, 2E Volume Down
ths3b46e622007-09-17 08:09:54 +0000219 237 // 0xed E0, 6D Media Select
220 236 // 0xec E0, 6C E-Mail
221 161 // 0xa1 E0, 21 Calculator
ths5fafdf22007-09-16 21:08:06 +0000222 235 // 0xeb E0, 6B My Computer
ths3b46e622007-09-17 08:09:54 +0000223 229 // 0xe5 E0, 65 WWW Search
224 178 // 0xb2 E0, 32 WWW Home
225 234 // 0xea E0, 6A WWW Back
ths5fafdf22007-09-16 21:08:06 +0000226 233 // 0xe9 E0, 69 WWW Forward
ths3b46e622007-09-17 08:09:54 +0000227 232 // 0xe8 E0, 68 WWW Stop
ths5fafdf22007-09-16 21:08:06 +0000228 231 // 0xe7 E0, 67 WWW Refresh
ths3b46e622007-09-17 08:09:54 +0000229 230 // 0xe6 E0, 66 WWW Favorites
bellard87f48e62005-10-30 18:24:49 +0000230*/
bellard5b0753e2005-03-01 21:37:28 +0000231};
232
Andreas Färber77047bb2009-12-13 00:55:53 +0100233static int cocoa_keycode_to_qemu(int keycode)
bellard5b0753e2005-03-01 21:37:28 +0000234{
Stefan Weil5d701922013-12-07 14:48:05 +0100235 if (ARRAY_SIZE(keymap) <= keycode) {
Peter Maydell01cc4e62013-12-08 22:59:04 +0000236 fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
bellard5b0753e2005-03-01 21:37:28 +0000237 return 0;
238 }
239 return keymap[keycode];
240}
241
thsc304f7e2008-01-22 23:25:15 +0000242
243
bellard5b0753e2005-03-01 21:37:28 +0000244/*
245 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000246 QemuCocoaView
bellard5b0753e2005-03-01 21:37:28 +0000247 ------------------------------------------------------
248*/
thsc304f7e2008-01-22 23:25:15 +0000249@interface QemuCocoaView : NSView
bellard5b0753e2005-03-01 21:37:28 +0000250{
thsc304f7e2008-01-22 23:25:15 +0000251 QEMUScreen screen;
252 NSWindow *fullScreenWindow;
253 float cx,cy,cw,ch,cdx,cdy;
254 CGDataProviderRef dataProviderRef;
255 int modifiers_state[256];
Peter Maydell49b9bd42013-12-08 22:59:03 +0000256 BOOL isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000257 BOOL isFullscreen;
258 BOOL isAbsoluteEnabled;
259 BOOL isTabletEnabled;
260}
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100261- (void) switchSurface:(DisplaySurface *)surface;
thsc304f7e2008-01-22 23:25:15 +0000262- (void) grabMouse;
263- (void) ungrabMouse;
264- (void) toggleFullScreen:(id)sender;
265- (void) handleEvent:(NSEvent *)event;
266- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
Peter Maydell49b9bd42013-12-08 22:59:03 +0000267- (BOOL) isMouseGrabbed;
thsc304f7e2008-01-22 23:25:15 +0000268- (BOOL) isAbsoluteEnabled;
269- (float) cdx;
270- (float) cdy;
271- (QEMUScreen) gscreen;
272@end
ths3b46e622007-09-17 08:09:54 +0000273
Andreas Färber7fee1992011-06-09 20:53:32 +0200274QemuCocoaView *cocoaView;
275
thsc304f7e2008-01-22 23:25:15 +0000276@implementation QemuCocoaView
277- (id)initWithFrame:(NSRect)frameRect
278{
279 COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
ths3b46e622007-09-17 08:09:54 +0000280
thsc304f7e2008-01-22 23:25:15 +0000281 self = [super initWithFrame:frameRect];
282 if (self) {
pbrook95219892006-04-09 01:06:34 +0000283
thsc304f7e2008-01-22 23:25:15 +0000284 screen.bitsPerComponent = 8;
285 screen.bitsPerPixel = 32;
286 screen.width = frameRect.size.width;
287 screen.height = frameRect.size.height;
bellard7c206a72005-12-18 19:18:45 +0000288
thsc304f7e2008-01-22 23:25:15 +0000289 }
290 return self;
291}
bellard7c206a72005-12-18 19:18:45 +0000292
thsc304f7e2008-01-22 23:25:15 +0000293- (void) dealloc
294{
295 COCOA_DEBUG("QemuCocoaView: dealloc\n");
bellard7c206a72005-12-18 19:18:45 +0000296
thsc304f7e2008-01-22 23:25:15 +0000297 if (dataProviderRef)
298 CGDataProviderRelease(dataProviderRef);
ths3b46e622007-09-17 08:09:54 +0000299
thsc304f7e2008-01-22 23:25:15 +0000300 [super dealloc];
301}
bellard5cbfcd02006-06-14 15:53:24 +0000302
Andreas Färberd50f71d2009-12-13 02:03:33 +0100303- (BOOL) isOpaque
304{
305 return YES;
306}
307
thsc304f7e2008-01-22 23:25:15 +0000308- (void) drawRect:(NSRect) rect
309{
310 COCOA_DEBUG("QemuCocoaView: drawRect\n");
bellard5cbfcd02006-06-14 15:53:24 +0000311
thsc304f7e2008-01-22 23:25:15 +0000312 // get CoreGraphic context
313 CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
314 CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
315 CGContextSetShouldAntialias (viewContextRef, NO);
ths3b46e622007-09-17 08:09:54 +0000316
thsc304f7e2008-01-22 23:25:15 +0000317 // draw screen bitmap directly to Core Graphics context
Peter Maydell7d270b12013-12-24 02:51:47 +0000318 if (!dataProviderRef) {
319 // Draw request before any guest device has set up a framebuffer:
320 // just draw an opaque black rectangle
321 CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
322 CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
323 } else {
thsc304f7e2008-01-22 23:25:15 +0000324 CGImageRef imageRef = CGImageCreate(
325 screen.width, //width
326 screen.height, //height
327 screen.bitsPerComponent, //bitsPerComponent
328 screen.bitsPerPixel, //bitsPerPixel
aliguori9794f742009-03-04 19:25:22 +0000329 (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
Andreas Färber04afa4a2009-12-13 00:58:21 +0100330#ifdef __LITTLE_ENDIAN__
thsc304f7e2008-01-22 23:25:15 +0000331 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
aliguori9794f742009-03-04 19:25:22 +0000332 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
thsc304f7e2008-01-22 23:25:15 +0000333#else
334 CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
335 kCGImageAlphaNoneSkipFirst, //bitmapInfo
336#endif
337 dataProviderRef, //provider
338 NULL, //decode
339 0, //interpolate
340 kCGRenderingIntentDefault //intent
341 );
Andreas Färber38ec7b52009-12-13 01:52:29 +0100342// test if host supports "CGImageCreateWithImageInRect" at compile time
thsc304f7e2008-01-22 23:25:15 +0000343#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
344 if (CGImageCreateWithImageInRect == NULL) { // test if "CGImageCreateWithImageInRect" is supported on host at runtime
345#endif
346 // compatibility drawing code (draws everything) (OS X < 10.4)
347 CGContextDrawImage (viewContextRef, CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height), imageRef);
348#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
349 } else {
350 // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
351 const NSRect *rectList;
Andreas Färber44e4c0b2009-12-13 00:45:40 +0100352#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
353 NSInteger rectCount;
354#else
thsc304f7e2008-01-22 23:25:15 +0000355 int rectCount;
Andreas Färber44e4c0b2009-12-13 00:45:40 +0100356#endif
thsc304f7e2008-01-22 23:25:15 +0000357 int i;
358 CGImageRef clipImageRef;
359 CGRect clipRect;
ths3b46e622007-09-17 08:09:54 +0000360
thsc304f7e2008-01-22 23:25:15 +0000361 [self getRectsBeingDrawn:&rectList count:&rectCount];
362 for (i = 0; i < rectCount; i++) {
363 clipRect.origin.x = rectList[i].origin.x / cdx;
364 clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
365 clipRect.size.width = rectList[i].size.width / cdx;
366 clipRect.size.height = rectList[i].size.height / cdy;
367 clipImageRef = CGImageCreateWithImageInRect(
368 imageRef,
369 clipRect
370 );
371 CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
372 CGImageRelease (clipImageRef);
bellard5b0753e2005-03-01 21:37:28 +0000373 }
374 }
thsc304f7e2008-01-22 23:25:15 +0000375#endif
376 CGImageRelease (imageRef);
bellard5b0753e2005-03-01 21:37:28 +0000377 }
378}
379
thsc304f7e2008-01-22 23:25:15 +0000380- (void) setContentDimensions
bellard5b0753e2005-03-01 21:37:28 +0000381{
thsc304f7e2008-01-22 23:25:15 +0000382 COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
ths3b46e622007-09-17 08:09:54 +0000383
thsc304f7e2008-01-22 23:25:15 +0000384 if (isFullscreen) {
385 cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
386 cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
387 cw = screen.width * cdx;
388 ch = screen.height * cdy;
389 cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
390 cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
391 } else {
392 cx = 0;
393 cy = 0;
394 cw = screen.width;
395 ch = screen.height;
396 cdx = 1.0;
397 cdy = 1.0;
398 }
bellard5b0753e2005-03-01 21:37:28 +0000399}
400
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100401- (void) switchSurface:(DisplaySurface *)surface
thsc304f7e2008-01-22 23:25:15 +0000402{
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100403 COCOA_DEBUG("QemuCocoaView: switchSurface\n");
thsc304f7e2008-01-22 23:25:15 +0000404
Peter Maydell8510d912013-03-18 20:28:21 +0000405 int w = surface_width(surface);
406 int h = surface_height(surface);
Peter Maydell381600d2014-06-23 10:35:22 +0100407 /* cdx == 0 means this is our very first surface, in which case we need
408 * to recalculate the content dimensions even if it happens to be the size
409 * of the initial empty window.
410 */
411 bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
Peter Maydelld3345a02013-12-24 02:51:46 +0000412
413 int oldh = screen.height;
414 if (isResize) {
415 // Resize before we trigger the redraw, or we'll redraw at the wrong size
416 COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
417 screen.width = w;
418 screen.height = h;
419 [self setContentDimensions];
420 [self setFrame:NSMakeRect(cx, cy, cw, ch)];
421 }
Peter Maydell8510d912013-03-18 20:28:21 +0000422
thsc304f7e2008-01-22 23:25:15 +0000423 // update screenBuffer
424 if (dataProviderRef)
425 CGDataProviderRelease(dataProviderRef);
thsc304f7e2008-01-22 23:25:15 +0000426
aliguori9794f742009-03-04 19:25:22 +0000427 //sync host window color space with guests
Peter Maydell49060c22013-12-24 11:54:12 +0000428 screen.bitsPerPixel = surface_bits_per_pixel(surface);
429 screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
thsc304f7e2008-01-22 23:25:15 +0000430
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +0100431 dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
thsc304f7e2008-01-22 23:25:15 +0000432
433 // update windows
434 if (isFullscreen) {
435 [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000436 [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 +0000437 } else {
438 if (qemu_name)
439 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
Peter Maydelld3345a02013-12-24 02:51:46 +0000440 [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 +0000441 }
Peter Maydelld3345a02013-12-24 02:51:46 +0000442
443 if (isResize) {
444 [normalWindow center];
445 }
thsc304f7e2008-01-22 23:25:15 +0000446}
447
448- (void) toggleFullScreen:(id)sender
449{
450 COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
451
452 if (isFullscreen) { // switch from fullscreen to desktop
453 isFullscreen = FALSE;
454 [self ungrabMouse];
455 [self setContentDimensions];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100456// test if host supports "exitFullScreenModeWithOptions" at compile time
457#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000458 if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
459 [self exitFullScreenModeWithOptions:nil];
460 } else {
461#endif
462 [fullScreenWindow close];
463 [normalWindow setContentView: self];
464 [normalWindow makeKeyAndOrderFront: self];
465 [NSMenu setMenuBarVisible:YES];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100466#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000467 }
468#endif
469 } else { // switch from desktop to fullscreen
470 isFullscreen = TRUE;
471 [self grabMouse];
472 [self setContentDimensions];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100473// test if host supports "enterFullScreenMode:withOptions" at compile time
474#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000475 if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
476 [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
477 [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
478 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
479 nil]];
480 } else {
481#endif
482 [NSMenu setMenuBarVisible:NO];
483 fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
484 styleMask:NSBorderlessWindowMask
485 backing:NSBackingStoreBuffered
486 defer:NO];
487 [fullScreenWindow setHasShadow:NO];
488 [fullScreenWindow setContentView:self];
489 [fullScreenWindow makeKeyAndOrderFront:self];
Andreas Färber38ec7b52009-12-13 01:52:29 +0100490#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
thsc304f7e2008-01-22 23:25:15 +0000491 }
492#endif
493 }
494}
495
496- (void) handleEvent:(NSEvent *)event
497{
498 COCOA_DEBUG("QemuCocoaView: handleEvent\n");
499
500 int buttons = 0;
501 int keycode;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100502 bool mouse_event = false;
thsc304f7e2008-01-22 23:25:15 +0000503 NSPoint p = [event locationInWindow];
504
505 switch ([event type]) {
506 case NSFlagsChanged:
507 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000508
Peter Maydell49b9bd42013-12-08 22:59:03 +0000509 if ((keycode == 219 || keycode == 220) && !isMouseGrabbed) {
Peter Maydell88959192013-12-08 22:59:02 +0000510 /* Don't pass command key changes to guest unless mouse is grabbed */
511 keycode = 0;
512 }
513
thsc304f7e2008-01-22 23:25:15 +0000514 if (keycode) {
515 if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100516 qemu_input_event_send_key_number(dcl->con, keycode, true);
517 qemu_input_event_send_key_number(dcl->con, keycode, false);
Peter Maydell68c0aa62013-04-17 09:16:35 +0000518 } else if (qemu_console_is_graphic(NULL)) {
thsc304f7e2008-01-22 23:25:15 +0000519 if (modifiers_state[keycode] == 0) { // keydown
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100520 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000521 modifiers_state[keycode] = 1;
522 } else { // keyup
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100523 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000524 modifiers_state[keycode] = 0;
525 }
526 }
527 }
528
529 // release Mouse grab when pressing ctrl+alt
530 if (!isFullscreen && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
531 [self ungrabMouse];
532 }
533 break;
534 case NSKeyDown:
Peter Maydell88959192013-12-08 22:59:02 +0000535 keycode = cocoa_keycode_to_qemu([event keyCode]);
thsc304f7e2008-01-22 23:25:15 +0000536
Peter Maydell88959192013-12-08 22:59:02 +0000537 // forward command key combos to the host UI unless the mouse is grabbed
Peter Maydell49b9bd42013-12-08 22:59:03 +0000538 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
thsc304f7e2008-01-22 23:25:15 +0000539 [NSApp sendEvent:event];
540 return;
541 }
542
543 // default
thsc304f7e2008-01-22 23:25:15 +0000544
545 // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
546 if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
547 switch (keycode) {
548
549 // enable graphic console
550 case 0x02 ... 0x0a: // '1' to '9' keys
551 console_select(keycode - 0x02);
552 break;
553 }
554
555 // handle keys for graphic console
Peter Maydell68c0aa62013-04-17 09:16:35 +0000556 } else if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100557 qemu_input_event_send_key_number(dcl->con, keycode, true);
thsc304f7e2008-01-22 23:25:15 +0000558
559 // handlekeys for Monitor
560 } else {
561 int keysym = 0;
562 switch([event keyCode]) {
563 case 115:
564 keysym = QEMU_KEY_HOME;
565 break;
566 case 117:
567 keysym = QEMU_KEY_DELETE;
568 break;
569 case 119:
570 keysym = QEMU_KEY_END;
571 break;
572 case 123:
573 keysym = QEMU_KEY_LEFT;
574 break;
575 case 124:
576 keysym = QEMU_KEY_RIGHT;
577 break;
578 case 125:
579 keysym = QEMU_KEY_DOWN;
580 break;
581 case 126:
582 keysym = QEMU_KEY_UP;
583 break;
584 default:
585 {
586 NSString *ks = [event characters];
587 if ([ks length] > 0)
588 keysym = [ks characterAtIndex:0];
589 }
590 }
591 if (keysym)
592 kbd_put_keysym(keysym);
593 }
594 break;
595 case NSKeyUp:
596 keycode = cocoa_keycode_to_qemu([event keyCode]);
Peter Maydell88959192013-12-08 22:59:02 +0000597
598 // don't pass the guest a spurious key-up if we treated this
599 // command-key combo as a host UI action
Peter Maydell49b9bd42013-12-08 22:59:03 +0000600 if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
Peter Maydell88959192013-12-08 22:59:02 +0000601 return;
602 }
603
Peter Maydell68c0aa62013-04-17 09:16:35 +0000604 if (qemu_console_is_graphic(NULL)) {
Gerd Hoffmann2e08c662013-12-04 12:53:44 +0100605 qemu_input_event_send_key_number(dcl->con, keycode, false);
thsc304f7e2008-01-22 23:25:15 +0000606 }
607 break;
608 case NSMouseMoved:
609 if (isAbsoluteEnabled) {
610 if (p.x < 0 || p.x > screen.width || p.y < 0 || p.y > screen.height || ![[self window] isKeyWindow]) {
611 if (isTabletEnabled) { // if we leave the window, deactivate the tablet
612 [NSCursor unhide];
613 isTabletEnabled = FALSE;
614 }
615 } else {
616 if (!isTabletEnabled) { // if we enter the window, activate the tablet
617 [NSCursor hide];
618 isTabletEnabled = TRUE;
619 }
620 }
621 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100622 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000623 break;
624 case NSLeftMouseDown:
625 if ([event modifierFlags] & NSCommandKeyMask) {
626 buttons |= MOUSE_EVENT_RBUTTON;
627 } else {
628 buttons |= MOUSE_EVENT_LBUTTON;
629 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100630 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000631 break;
632 case NSRightMouseDown:
633 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100634 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000635 break;
636 case NSOtherMouseDown:
637 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100638 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000639 break;
640 case NSLeftMouseDragged:
641 if ([event modifierFlags] & NSCommandKeyMask) {
642 buttons |= MOUSE_EVENT_RBUTTON;
643 } else {
644 buttons |= MOUSE_EVENT_LBUTTON;
645 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100646 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000647 break;
648 case NSRightMouseDragged:
649 buttons |= MOUSE_EVENT_RBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100650 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000651 break;
652 case NSOtherMouseDragged:
653 buttons |= MOUSE_EVENT_MBUTTON;
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100654 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000655 break;
656 case NSLeftMouseUp:
657 if (isTabletEnabled) {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100658 mouse_event = true;
Peter Maydell49b9bd42013-12-08 22:59:03 +0000659 } else if (!isMouseGrabbed) {
thsc304f7e2008-01-22 23:25:15 +0000660 if (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height) {
661 [self grabMouse];
662 } else {
663 [NSApp sendEvent:event];
664 }
665 } else {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100666 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000667 }
668 break;
669 case NSRightMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100670 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000671 break;
672 case NSOtherMouseUp:
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100673 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000674 break;
675 case NSScrollWheel:
Peter Maydell49b9bd42013-12-08 22:59:03 +0000676 if (isTabletEnabled || isMouseGrabbed) {
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100677 buttons |= ([event deltaY] < 0) ?
678 MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
679 mouse_event = true;
thsc304f7e2008-01-22 23:25:15 +0000680 } else {
681 [NSApp sendEvent:event];
682 }
683 break;
684 default:
685 [NSApp sendEvent:event];
686 }
Gerd Hoffmann21bae112013-12-04 14:08:04 +0100687
688 if (mouse_event) {
689 if (last_buttons != buttons) {
690 static uint32_t bmap[INPUT_BUTTON_MAX] = {
691 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
692 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
693 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
694 [INPUT_BUTTON_WHEEL_UP] = MOUSE_EVENT_WHEELUP,
695 [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
696 };
697 qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
698 last_buttons = buttons;
699 }
700 if (isTabletEnabled) {
701 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width);
702 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, p.y, screen.height);
703 } else if (isMouseGrabbed) {
704 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
705 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
706 } else {
707 [NSApp sendEvent:event];
708 }
709 qemu_input_event_sync();
710 }
thsc304f7e2008-01-22 23:25:15 +0000711}
712
713- (void) grabMouse
714{
715 COCOA_DEBUG("QemuCocoaView: grabMouse\n");
716
717 if (!isFullscreen) {
718 if (qemu_name)
719 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
720 else
721 [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
722 }
723 [NSCursor hide];
724 CGAssociateMouseAndMouseCursorPosition(FALSE);
Peter Maydell49b9bd42013-12-08 22:59:03 +0000725 isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
thsc304f7e2008-01-22 23:25:15 +0000726}
727
728- (void) ungrabMouse
729{
730 COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
731
732 if (!isFullscreen) {
733 if (qemu_name)
734 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
735 else
736 [normalWindow setTitle:@"QEMU"];
737 }
738 [NSCursor unhide];
739 CGAssociateMouseAndMouseCursorPosition(TRUE);
Peter Maydell49b9bd42013-12-08 22:59:03 +0000740 isMouseGrabbed = FALSE;
thsc304f7e2008-01-22 23:25:15 +0000741}
742
743- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
Peter Maydell49b9bd42013-12-08 22:59:03 +0000744- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
thsc304f7e2008-01-22 23:25:15 +0000745- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
746- (float) cdx {return cdx;}
747- (float) cdy {return cdy;}
748- (QEMUScreen) gscreen {return screen;}
bellard5b0753e2005-03-01 21:37:28 +0000749@end
750
751
thsc304f7e2008-01-22 23:25:15 +0000752
bellard5b0753e2005-03-01 21:37:28 +0000753/*
754 ------------------------------------------------------
thsc304f7e2008-01-22 23:25:15 +0000755 QemuCocoaAppController
bellard5b0753e2005-03-01 21:37:28 +0000756 ------------------------------------------------------
757*/
thsc304f7e2008-01-22 23:25:15 +0000758@interface QemuCocoaAppController : NSObject
bellard5b0753e2005-03-01 21:37:28 +0000759{
760}
bellard5b0753e2005-03-01 21:37:28 +0000761- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
thsc304f7e2008-01-22 23:25:15 +0000762- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
763- (void)toggleFullScreen:(id)sender;
764- (void)showQEMUDoc:(id)sender;
765- (void)showQEMUTec:(id)sender;
bellard5b0753e2005-03-01 21:37:28 +0000766@end
767
thsc304f7e2008-01-22 23:25:15 +0000768@implementation QemuCocoaAppController
769- (id) init
770{
771 COCOA_DEBUG("QemuCocoaAppController: init\n");
772
773 self = [super init];
774 if (self) {
775
776 // create a view and add it to the window
777 cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
778 if(!cocoaView) {
779 fprintf(stderr, "(cocoa) can't create a view\n");
780 exit(1);
781 }
782
783 // create a window
784 normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
785 styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
786 backing:NSBackingStoreBuffered defer:NO];
787 if(!normalWindow) {
788 fprintf(stderr, "(cocoa) can't create window\n");
789 exit(1);
790 }
791 [normalWindow setAcceptsMouseMovedEvents:YES];
792 [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]];
793 [normalWindow setContentView:cocoaView];
Andreas Färber561ef252009-12-13 03:06:20 +0100794 [normalWindow useOptimizedDrawing:YES];
thsc304f7e2008-01-22 23:25:15 +0000795 [normalWindow makeKeyAndOrderFront:self];
Peter Maydell49060c22013-12-24 11:54:12 +0000796 [normalWindow center];
thsc304f7e2008-01-22 23:25:15 +0000797
798 }
799 return self;
800}
801
802- (void) dealloc
803{
804 COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
805
806 if (cocoaView)
807 [cocoaView release];
808 [super dealloc];
809}
810
bellard5b0753e2005-03-01 21:37:28 +0000811- (void)applicationDidFinishLaunching: (NSNotification *) note
812{
thsc304f7e2008-01-22 23:25:15 +0000813 COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
bellard5a246932005-04-07 20:35:06 +0000814
Peter Maydell49b9bd42013-12-08 22:59:03 +0000815 // Display an open dialog box if no arguments were passed or
thsc304f7e2008-01-22 23:25:15 +0000816 // if qemu was launched from the finder ( the Finder passes "-psn" )
817 if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) {
bellard5b0753e2005-03-01 21:37:28 +0000818 NSOpenPanel *op = [[NSOpenPanel alloc] init];
bellard5b0753e2005-03-01 21:37:28 +0000819 [op setPrompt:@"Boot image"];
bellard5b0753e2005-03-01 21:37:28 +0000820 [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000821 NSArray *filetypes = [NSArray arrayWithObjects:@"img", @"iso", @"dmg",
Peter Maydell5342f992013-12-08 22:59:05 +0000822 @"qcow", @"qcow2", @"cow", @"cloop", @"vmdk", nil];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000823#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
824 [op setAllowedFileTypes:filetypes];
825 [op beginSheetModalForWindow:normalWindow
826 completionHandler:^(NSInteger returnCode)
827 { [self openPanelDidEnd:op
828 returnCode:returnCode contextInfo:NULL ]; } ];
829#else
830 // Compatibility code for pre-10.6, using deprecated method
831 [op beginSheetForDirectory:nil file:nil types:filetypes
thsc304f7e2008-01-22 23:25:15 +0000832 modalForWindow:normalWindow modalDelegate:self
bellard5b0753e2005-03-01 21:37:28 +0000833 didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
Peter Maydell2ba9de62013-04-22 10:29:49 +0000834#endif
thsc304f7e2008-01-22 23:25:15 +0000835 } else {
Stefan Weil5cbdb3a2012-04-07 09:23:39 +0200836 // or launch QEMU, with the global args
thsc304f7e2008-01-22 23:25:15 +0000837 [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
bellard5a246932005-04-07 20:35:06 +0000838 }
bellard5b0753e2005-03-01 21:37:28 +0000839}
840
841- (void)applicationWillTerminate:(NSNotification *)aNotification
842{
thsc304f7e2008-01-22 23:25:15 +0000843 COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
844
bellard5b0753e2005-03-01 21:37:28 +0000845 qemu_system_shutdown_request();
bellard5b0753e2005-03-01 21:37:28 +0000846 exit(0);
847}
848
Andreas Färber41ea49b2009-12-14 22:13:27 +0100849- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
850{
851 return YES;
852}
853
thsc304f7e2008-01-22 23:25:15 +0000854- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
855{
856 COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
857
858 int status;
Andreas Färber3bbbee12011-05-29 19:42:51 +0200859 status = qemu_main(argc, argv, *_NSGetEnviron());
thsc304f7e2008-01-22 23:25:15 +0000860 exit(status);
861}
862
bellard5b0753e2005-03-01 21:37:28 +0000863- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
864{
thsc304f7e2008-01-22 23:25:15 +0000865 COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n");
ths3b46e622007-09-17 08:09:54 +0000866
thsc304f7e2008-01-22 23:25:15 +0000867 if(returnCode == NSCancelButton) {
868 exit(0);
869 } else if(returnCode == NSOKButton) {
Peter Maydell8bb3f1e2013-04-22 10:29:48 +0000870 char *img = (char*)[ [ [ sheet URL ] path ] cStringUsingEncoding:NSASCIIStringEncoding];
ths3b46e622007-09-17 08:09:54 +0000871
Peter Maydell98db4292013-12-08 22:59:06 +0000872 char **argv = g_new(char *, 4);
ths3b46e622007-09-17 08:09:54 +0000873
Andreas Färber13766eb2011-10-30 23:19:54 +0100874 [sheet close];
875
Peter Maydell98db4292013-12-08 22:59:06 +0000876 argv[0] = g_strdup(gArgv[0]);
877 argv[1] = g_strdup("-hda");
878 argv[2] = g_strdup(img);
879 argv[3] = NULL;
ths3b46e622007-09-17 08:09:54 +0000880
Peter Maydell98db4292013-12-08 22:59:06 +0000881 // printf("Using argc %d argv %s -hda %s\n", 3, gArgv[0], img);
ths3b46e622007-09-17 08:09:54 +0000882
bellard5b0753e2005-03-01 21:37:28 +0000883 [self startEmulationWithArgc:3 argv:(char**)argv];
884 }
885}
thsc304f7e2008-01-22 23:25:15 +0000886- (void)toggleFullScreen:(id)sender
bellard5b0753e2005-03-01 21:37:28 +0000887{
thsc304f7e2008-01-22 23:25:15 +0000888 COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
889
890 [cocoaView toggleFullScreen:sender];
891}
892
893- (void)showQEMUDoc:(id)sender
894{
895 COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
896
897 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
898 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
899}
900
901- (void)showQEMUTec:(id)sender
902{
903 COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
904
905 [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
906 [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
bellard5b0753e2005-03-01 21:37:28 +0000907}
908@end
909
bellard5b0753e2005-03-01 21:37:28 +0000910
thsc304f7e2008-01-22 23:25:15 +0000911
thsc304f7e2008-01-22 23:25:15 +0000912int main (int argc, const char * argv[]) {
ths3b46e622007-09-17 08:09:54 +0000913
thsc304f7e2008-01-22 23:25:15 +0000914 gArgc = argc;
915 gArgv = (char **)argv;
Andreas Färberf4918802009-12-13 02:11:44 +0100916 int i;
917
918 /* In case we don't need to display a window, let's not do that */
919 for (i = 1; i < argc; i++) {
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100920 const char *opt = argv[i];
921
922 if (opt[0] == '-') {
923 /* Treat --foo the same as -foo. */
924 if (opt[1] == '-') {
925 opt++;
926 }
Alexandre Raymond98514842011-05-29 18:22:49 -0400927 if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
928 !strcmp(opt, "-vnc") ||
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100929 !strcmp(opt, "-nographic") ||
930 !strcmp(opt, "-version") ||
Andreas Färber60b46aa2012-05-28 03:18:31 +0200931 !strcmp(opt, "-curses") ||
932 !strcmp(opt, "-qtest")) {
Andreas Färber3bbbee12011-05-29 19:42:51 +0200933 return qemu_main(gArgc, gArgv, *_NSGetEnviron());
Tristan Gingolde4ebcc12011-03-15 14:18:22 +0100934 }
Andreas Färberf4918802009-12-13 02:11:44 +0100935 }
936 }
ths3b46e622007-09-17 08:09:54 +0000937
thsc304f7e2008-01-22 23:25:15 +0000938 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ths3b46e622007-09-17 08:09:54 +0000939
Peter Maydell42a5dfe2013-04-22 10:29:47 +0000940 // Pull this console process up to being a fully-fledged graphical
941 // app with a menubar and Dock icon
942 ProcessSerialNumber psn = { 0, kCurrentProcess };
943 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
944
945 [NSApplication sharedApplication];
ths3b46e622007-09-17 08:09:54 +0000946
thsc304f7e2008-01-22 23:25:15 +0000947 // Add menus
948 NSMenu *menu;
949 NSMenuItem *menuItem;
950
bellard5b0753e2005-03-01 21:37:28 +0000951 [NSApp setMainMenu:[[NSMenu alloc] init]];
bellard5b0753e2005-03-01 21:37:28 +0000952
thsc304f7e2008-01-22 23:25:15 +0000953 // Application menu
954 menu = [[NSMenu alloc] initWithTitle:@""];
955 [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
956 [menu addItem:[NSMenuItem separatorItem]]; //Separator
957 [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
958 menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
959 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
960 [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
961 [menu addItem:[NSMenuItem separatorItem]]; //Separator
962 [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
963 menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
964 [menuItem setSubmenu:menu];
965 [[NSApp mainMenu] addItem:menuItem];
966 [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
ths3b46e622007-09-17 08:09:54 +0000967
thsc304f7e2008-01-22 23:25:15 +0000968 // View menu
969 menu = [[NSMenu alloc] initWithTitle:@"View"];
970 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
971 menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
972 [menuItem setSubmenu:menu];
973 [[NSApp mainMenu] addItem:menuItem];
974
975 // Window menu
976 menu = [[NSMenu alloc] initWithTitle:@"Window"];
977 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
978 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
979 [menuItem setSubmenu:menu];
980 [[NSApp mainMenu] addItem:menuItem];
981 [NSApp setWindowsMenu:menu];
982
983 // Help menu
984 menu = [[NSMenu alloc] initWithTitle:@"Help"];
985 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
986 [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
987 menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
988 [menuItem setSubmenu:menu];
989 [[NSApp mainMenu] addItem:menuItem];
990
991 // Create an Application controller
992 QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
993 [NSApp setDelegate:appController];
994
995 // Start the main event loop
bellard5b0753e2005-03-01 21:37:28 +0000996 [NSApp run];
ths3b46e622007-09-17 08:09:54 +0000997
thsc304f7e2008-01-22 23:25:15 +0000998 [appController release];
bellard5b0753e2005-03-01 21:37:28 +0000999 [pool release];
bellardcae41b12006-05-22 21:25:04 +00001000
bellard5b0753e2005-03-01 21:37:28 +00001001 return 0;
1002}
thsc304f7e2008-01-22 23:25:15 +00001003
1004
1005
1006#pragma mark qemu
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001007static void cocoa_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001008 int x, int y, int w, int h)
thsc304f7e2008-01-22 23:25:15 +00001009{
Peter Maydell6e657e62013-04-22 10:29:46 +00001010 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1011
thsc304f7e2008-01-22 23:25:15 +00001012 COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
1013
1014 NSRect rect;
1015 if ([cocoaView cdx] == 1.0) {
1016 rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
1017 } else {
1018 rect = NSMakeRect(
1019 x * [cocoaView cdx],
1020 ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
1021 w * [cocoaView cdx],
1022 h * [cocoaView cdy]);
1023 }
Andreas Färber17ccbc22009-12-13 02:08:58 +01001024 [cocoaView setNeedsDisplayInRect:rect];
Peter Maydell6e657e62013-04-22 10:29:46 +00001025
1026 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001027}
1028
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001029static void cocoa_switch(DisplayChangeListener *dcl,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +01001030 DisplaySurface *surface)
thsc304f7e2008-01-22 23:25:15 +00001031{
Peter Maydell6e657e62013-04-22 10:29:46 +00001032 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thsc304f7e2008-01-22 23:25:15 +00001033
Peter Maydell6e657e62013-04-22 10:29:46 +00001034 COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
Gerd Hoffmann5e00d3a2013-03-01 12:52:06 +01001035 [cocoaView switchSurface:surface];
Peter Maydell6e657e62013-04-22 10:29:46 +00001036 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001037}
1038
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +01001039static void cocoa_refresh(DisplayChangeListener *dcl)
thsc304f7e2008-01-22 23:25:15 +00001040{
Peter Maydell6e657e62013-04-22 10:29:46 +00001041 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
1042
thsc304f7e2008-01-22 23:25:15 +00001043 COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
1044
Gerd Hoffmann21bae112013-12-04 14:08:04 +01001045 if (qemu_input_is_absolute()) {
thsc304f7e2008-01-22 23:25:15 +00001046 if (![cocoaView isAbsoluteEnabled]) {
Peter Maydell49b9bd42013-12-08 22:59:03 +00001047 if ([cocoaView isMouseGrabbed]) {
thsc304f7e2008-01-22 23:25:15 +00001048 [cocoaView ungrabMouse];
1049 }
1050 }
1051 [cocoaView setAbsoluteEnabled:YES];
1052 }
1053
1054 NSDate *distantPast;
1055 NSEvent *event;
1056 distantPast = [NSDate distantPast];
1057 do {
1058 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
1059 inMode: NSDefaultRunLoopMode dequeue:YES];
1060 if (event != nil) {
1061 [cocoaView handleEvent:event];
1062 }
1063 } while(event != nil);
Peter Maydell68c0aa62013-04-17 09:16:35 +00001064 graphic_hw_update(NULL);
Peter Maydell6e657e62013-04-22 10:29:46 +00001065 [pool release];
thsc304f7e2008-01-22 23:25:15 +00001066}
1067
1068static void cocoa_cleanup(void)
1069{
1070 COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
Blue Swirl58a06672011-08-21 18:42:08 +00001071 g_free(dcl);
thsc304f7e2008-01-22 23:25:15 +00001072}
1073
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001074static const DisplayChangeListenerOps dcl_ops = {
1075 .dpy_name = "cocoa",
Peter Maydell8510d912013-03-18 20:28:21 +00001076 .dpy_gfx_update = cocoa_update,
1077 .dpy_gfx_switch = cocoa_switch,
1078 .dpy_refresh = cocoa_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001079};
1080
thsc304f7e2008-01-22 23:25:15 +00001081void cocoa_display_init(DisplayState *ds, int full_screen)
1082{
1083 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1084
Blue Swirl58a06672011-08-21 18:42:08 +00001085 dcl = g_malloc0(sizeof(DisplayChangeListener));
1086
aliguori9794f742009-03-04 19:25:22 +00001087 // register vga output callbacks
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001088 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001089 register_displaychangelistener(dcl);
thsc304f7e2008-01-22 23:25:15 +00001090
1091 // register cleanup function
1092 atexit(cocoa_cleanup);
1093}