xref: /qemu/ui/cocoa.m (revision 9d227f19)
13e230dd2SCorentin Chary/*
23e230dd2SCorentin Chary * QEMU Cocoa CG display driver
33e230dd2SCorentin Chary *
43e230dd2SCorentin Chary * Copyright (c) 2008 Mike Kronenberg
53e230dd2SCorentin Chary *
63e230dd2SCorentin Chary * Permission is hereby granted, free of charge, to any person obtaining a copy
73e230dd2SCorentin Chary * of this software and associated documentation files (the "Software"), to deal
83e230dd2SCorentin Chary * in the Software without restriction, including without limitation the rights
93e230dd2SCorentin Chary * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
103e230dd2SCorentin Chary * copies of the Software, and to permit persons to whom the Software is
113e230dd2SCorentin Chary * furnished to do so, subject to the following conditions:
123e230dd2SCorentin Chary *
133e230dd2SCorentin Chary * The above copyright notice and this permission notice shall be included in
143e230dd2SCorentin Chary * all copies or substantial portions of the Software.
153e230dd2SCorentin Chary *
163e230dd2SCorentin Chary * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173e230dd2SCorentin Chary * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
183e230dd2SCorentin Chary * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
193e230dd2SCorentin Chary * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
203e230dd2SCorentin Chary * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
213e230dd2SCorentin Chary * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
223e230dd2SCorentin Chary * THE SOFTWARE.
233e230dd2SCorentin Chary */
243e230dd2SCorentin Chary
25e4a096b1SPeter Maydell#include "qemu/osdep.h"
26e4a096b1SPeter Maydell
273e230dd2SCorentin Chary#import <Cocoa/Cocoa.h>
283bbbee18SAndreas Färber#include <crt_externs.h>
293e230dd2SCorentin Chary
303e230dd2SCorentin Chary#include "qemu-common.h"
3128ecbaeeSPaolo Bonzini#include "ui/console.h"
3221bae11aSGerd Hoffmann#include "ui/input.h"
339c17d615SPaolo Bonzini#include "sysemu/sysemu.h"
348524f1c7SJohn Arbuckle#include "qmp-commands.h"
35693a3e01SJohn Arbuckle#include "sysemu/blockdev.h"
36aaac714fSJohn Arbuckle#include <Carbon/Carbon.h>
373e230dd2SCorentin Chary
383e230dd2SCorentin Chary#ifndef MAC_OS_X_VERSION_10_5
393e230dd2SCorentin Chary#define MAC_OS_X_VERSION_10_5 1050
403e230dd2SCorentin Chary#endif
412ba9de6eSPeter Maydell#ifndef MAC_OS_X_VERSION_10_6
422ba9de6eSPeter Maydell#define MAC_OS_X_VERSION_10_6 1060
432ba9de6eSPeter Maydell#endif
4481801ae2SPeter Maydell#ifndef MAC_OS_X_VERSION_10_10
4581801ae2SPeter Maydell#define MAC_OS_X_VERSION_10_10 101000
4681801ae2SPeter Maydell#endif
473e230dd2SCorentin Chary
483e230dd2SCorentin Chary
493e230dd2SCorentin Chary//#define DEBUG
503e230dd2SCorentin Chary
513e230dd2SCorentin Chary#ifdef DEBUG
523e230dd2SCorentin Chary#define COCOA_DEBUG(...)  { (void) fprintf (stdout, __VA_ARGS__); }
533e230dd2SCorentin Chary#else
543e230dd2SCorentin Chary#define COCOA_DEBUG(...)  ((void) 0)
553e230dd2SCorentin Chary#endif
563e230dd2SCorentin Chary
573e230dd2SCorentin Chary#define cgrect(nsrect) (*(CGRect *)&(nsrect))
583e230dd2SCorentin Chary
593e230dd2SCorentin Charytypedef struct {
603e230dd2SCorentin Chary    int width;
613e230dd2SCorentin Chary    int height;
623e230dd2SCorentin Chary    int bitsPerComponent;
633e230dd2SCorentin Chary    int bitsPerPixel;
643e230dd2SCorentin Chary} QEMUScreen;
653e230dd2SCorentin Chary
663e230dd2SCorentin CharyNSWindow *normalWindow;
673e230dd2SCorentin Charystatic DisplayChangeListener *dcl;
6821bae11aSGerd Hoffmannstatic int last_buttons;
693e230dd2SCorentin Chary
703e230dd2SCorentin Charyint gArgc;
713e230dd2SCorentin Charychar **gArgv;
725d1b2eefSProgrammingkidbool stretch_video;
738524f1c7SJohn ArbuckleNSTextField *pauseLabel;
74693a3e01SJohn ArbuckleNSArray * supportedImageFileTypes;
753e230dd2SCorentin Chary
76aaac714fSJohn Arbuckle// Mac to QKeyCode conversion
77aaac714fSJohn Arbuckleconst int mac_to_qkeycode_map[] = {
78aaac714fSJohn Arbuckle    [kVK_ANSI_A] = Q_KEY_CODE_A,
79aaac714fSJohn Arbuckle    [kVK_ANSI_B] = Q_KEY_CODE_B,
80aaac714fSJohn Arbuckle    [kVK_ANSI_C] = Q_KEY_CODE_C,
81aaac714fSJohn Arbuckle    [kVK_ANSI_D] = Q_KEY_CODE_D,
82aaac714fSJohn Arbuckle    [kVK_ANSI_E] = Q_KEY_CODE_E,
83aaac714fSJohn Arbuckle    [kVK_ANSI_F] = Q_KEY_CODE_F,
84aaac714fSJohn Arbuckle    [kVK_ANSI_G] = Q_KEY_CODE_G,
85aaac714fSJohn Arbuckle    [kVK_ANSI_H] = Q_KEY_CODE_H,
86aaac714fSJohn Arbuckle    [kVK_ANSI_I] = Q_KEY_CODE_I,
87aaac714fSJohn Arbuckle    [kVK_ANSI_J] = Q_KEY_CODE_J,
88aaac714fSJohn Arbuckle    [kVK_ANSI_K] = Q_KEY_CODE_K,
89aaac714fSJohn Arbuckle    [kVK_ANSI_L] = Q_KEY_CODE_L,
90aaac714fSJohn Arbuckle    [kVK_ANSI_M] = Q_KEY_CODE_M,
91aaac714fSJohn Arbuckle    [kVK_ANSI_N] = Q_KEY_CODE_N,
92aaac714fSJohn Arbuckle    [kVK_ANSI_O] = Q_KEY_CODE_O,
93aaac714fSJohn Arbuckle    [kVK_ANSI_P] = Q_KEY_CODE_P,
94aaac714fSJohn Arbuckle    [kVK_ANSI_Q] = Q_KEY_CODE_Q,
95aaac714fSJohn Arbuckle    [kVK_ANSI_R] = Q_KEY_CODE_R,
96aaac714fSJohn Arbuckle    [kVK_ANSI_S] = Q_KEY_CODE_S,
97aaac714fSJohn Arbuckle    [kVK_ANSI_T] = Q_KEY_CODE_T,
98aaac714fSJohn Arbuckle    [kVK_ANSI_U] = Q_KEY_CODE_U,
99aaac714fSJohn Arbuckle    [kVK_ANSI_V] = Q_KEY_CODE_V,
100aaac714fSJohn Arbuckle    [kVK_ANSI_W] = Q_KEY_CODE_W,
101aaac714fSJohn Arbuckle    [kVK_ANSI_X] = Q_KEY_CODE_X,
102aaac714fSJohn Arbuckle    [kVK_ANSI_Y] = Q_KEY_CODE_Y,
103aaac714fSJohn Arbuckle    [kVK_ANSI_Z] = Q_KEY_CODE_Z,
1043e230dd2SCorentin Chary
105aaac714fSJohn Arbuckle    [kVK_ANSI_0] = Q_KEY_CODE_0,
106aaac714fSJohn Arbuckle    [kVK_ANSI_1] = Q_KEY_CODE_1,
107aaac714fSJohn Arbuckle    [kVK_ANSI_2] = Q_KEY_CODE_2,
108aaac714fSJohn Arbuckle    [kVK_ANSI_3] = Q_KEY_CODE_3,
109aaac714fSJohn Arbuckle    [kVK_ANSI_4] = Q_KEY_CODE_4,
110aaac714fSJohn Arbuckle    [kVK_ANSI_5] = Q_KEY_CODE_5,
111aaac714fSJohn Arbuckle    [kVK_ANSI_6] = Q_KEY_CODE_6,
112aaac714fSJohn Arbuckle    [kVK_ANSI_7] = Q_KEY_CODE_7,
113aaac714fSJohn Arbuckle    [kVK_ANSI_8] = Q_KEY_CODE_8,
114aaac714fSJohn Arbuckle    [kVK_ANSI_9] = Q_KEY_CODE_9,
115aaac714fSJohn Arbuckle
116aaac714fSJohn Arbuckle    [kVK_ANSI_Grave] = Q_KEY_CODE_GRAVE_ACCENT,
117aaac714fSJohn Arbuckle    [kVK_ANSI_Minus] = Q_KEY_CODE_MINUS,
118aaac714fSJohn Arbuckle    [kVK_ANSI_Equal] = Q_KEY_CODE_EQUAL,
119aaac714fSJohn Arbuckle    [kVK_Delete] = Q_KEY_CODE_BACKSPACE,
120aaac714fSJohn Arbuckle    [kVK_CapsLock] = Q_KEY_CODE_CAPS_LOCK,
121aaac714fSJohn Arbuckle    [kVK_Tab] = Q_KEY_CODE_TAB,
122aaac714fSJohn Arbuckle    [kVK_Return] = Q_KEY_CODE_RET,
123aaac714fSJohn Arbuckle    [kVK_ANSI_LeftBracket] = Q_KEY_CODE_BRACKET_LEFT,
124aaac714fSJohn Arbuckle    [kVK_ANSI_RightBracket] = Q_KEY_CODE_BRACKET_RIGHT,
125aaac714fSJohn Arbuckle    [kVK_ANSI_Backslash] = Q_KEY_CODE_BACKSLASH,
126aaac714fSJohn Arbuckle    [kVK_ANSI_Semicolon] = Q_KEY_CODE_SEMICOLON,
127aaac714fSJohn Arbuckle    [kVK_ANSI_Quote] = Q_KEY_CODE_APOSTROPHE,
128aaac714fSJohn Arbuckle    [kVK_ANSI_Comma] = Q_KEY_CODE_COMMA,
129aaac714fSJohn Arbuckle    [kVK_ANSI_Period] = Q_KEY_CODE_DOT,
130aaac714fSJohn Arbuckle    [kVK_ANSI_Slash] = Q_KEY_CODE_SLASH,
131aaac714fSJohn Arbuckle    [kVK_Shift] = Q_KEY_CODE_SHIFT,
132aaac714fSJohn Arbuckle    [kVK_RightShift] = Q_KEY_CODE_SHIFT_R,
133aaac714fSJohn Arbuckle    [kVK_Control] = Q_KEY_CODE_CTRL,
134aaac714fSJohn Arbuckle    [kVK_RightControl] = Q_KEY_CODE_CTRL_R,
135aaac714fSJohn Arbuckle    [kVK_Option] = Q_KEY_CODE_ALT,
136aaac714fSJohn Arbuckle    [kVK_RightOption] = Q_KEY_CODE_ALT_R,
137aaac714fSJohn Arbuckle    [kVK_Command] = Q_KEY_CODE_META_L,
138aaac714fSJohn Arbuckle    [0x36] = Q_KEY_CODE_META_R, /* There is no kVK_RightCommand */
139aaac714fSJohn Arbuckle    [kVK_Space] = Q_KEY_CODE_SPC,
140aaac714fSJohn Arbuckle
141aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad0] = Q_KEY_CODE_KP_0,
142aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad1] = Q_KEY_CODE_KP_1,
143aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad2] = Q_KEY_CODE_KP_2,
144aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad3] = Q_KEY_CODE_KP_3,
145aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad4] = Q_KEY_CODE_KP_4,
146aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad5] = Q_KEY_CODE_KP_5,
147aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad6] = Q_KEY_CODE_KP_6,
148aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad7] = Q_KEY_CODE_KP_7,
149aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad8] = Q_KEY_CODE_KP_8,
150aaac714fSJohn Arbuckle    [kVK_ANSI_Keypad9] = Q_KEY_CODE_KP_9,
151aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadDecimal] = Q_KEY_CODE_KP_DECIMAL,
152aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadEnter] = Q_KEY_CODE_KP_ENTER,
153aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadPlus] = Q_KEY_CODE_KP_ADD,
154aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadMinus] = Q_KEY_CODE_KP_SUBTRACT,
155aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadMultiply] = Q_KEY_CODE_KP_MULTIPLY,
156aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadDivide] = Q_KEY_CODE_KP_DIVIDE,
157aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadEquals] = Q_KEY_CODE_KP_EQUALS,
158aaac714fSJohn Arbuckle    [kVK_ANSI_KeypadClear] = Q_KEY_CODE_NUM_LOCK,
159aaac714fSJohn Arbuckle
160aaac714fSJohn Arbuckle    [kVK_UpArrow] = Q_KEY_CODE_UP,
161aaac714fSJohn Arbuckle    [kVK_DownArrow] = Q_KEY_CODE_DOWN,
162aaac714fSJohn Arbuckle    [kVK_LeftArrow] = Q_KEY_CODE_LEFT,
163aaac714fSJohn Arbuckle    [kVK_RightArrow] = Q_KEY_CODE_RIGHT,
164aaac714fSJohn Arbuckle
165aaac714fSJohn Arbuckle    [kVK_Help] = Q_KEY_CODE_INSERT,
166aaac714fSJohn Arbuckle    [kVK_Home] = Q_KEY_CODE_HOME,
167aaac714fSJohn Arbuckle    [kVK_PageUp] = Q_KEY_CODE_PGUP,
168aaac714fSJohn Arbuckle    [kVK_PageDown] = Q_KEY_CODE_PGDN,
169aaac714fSJohn Arbuckle    [kVK_End] = Q_KEY_CODE_END,
170aaac714fSJohn Arbuckle    [kVK_ForwardDelete] = Q_KEY_CODE_DELETE,
171aaac714fSJohn Arbuckle
172aaac714fSJohn Arbuckle    [kVK_Escape] = Q_KEY_CODE_ESC,
173aaac714fSJohn Arbuckle
174aaac714fSJohn Arbuckle    /* The Power key can't be used directly because the operating system uses
175aaac714fSJohn Arbuckle     * it. This key can be emulated by using it in place of another key such as
176aaac714fSJohn Arbuckle     * F1. Don't forget to disable the real key binding.
177aaac714fSJohn Arbuckle     */
178aaac714fSJohn Arbuckle    /* [kVK_F1] = Q_KEY_CODE_POWER, */
179aaac714fSJohn Arbuckle
180aaac714fSJohn Arbuckle    [kVK_F1] = Q_KEY_CODE_F1,
181aaac714fSJohn Arbuckle    [kVK_F2] = Q_KEY_CODE_F2,
182aaac714fSJohn Arbuckle    [kVK_F3] = Q_KEY_CODE_F3,
183aaac714fSJohn Arbuckle    [kVK_F4] = Q_KEY_CODE_F4,
184aaac714fSJohn Arbuckle    [kVK_F5] = Q_KEY_CODE_F5,
185aaac714fSJohn Arbuckle    [kVK_F6] = Q_KEY_CODE_F6,
186aaac714fSJohn Arbuckle    [kVK_F7] = Q_KEY_CODE_F7,
187aaac714fSJohn Arbuckle    [kVK_F8] = Q_KEY_CODE_F8,
188aaac714fSJohn Arbuckle    [kVK_F9] = Q_KEY_CODE_F9,
189aaac714fSJohn Arbuckle    [kVK_F10] = Q_KEY_CODE_F10,
190aaac714fSJohn Arbuckle    [kVK_F11] = Q_KEY_CODE_F11,
191aaac714fSJohn Arbuckle    [kVK_F12] = Q_KEY_CODE_F12,
192aaac714fSJohn Arbuckle    [kVK_F13] = Q_KEY_CODE_PRINT,
193aaac714fSJohn Arbuckle    [kVK_F14] = Q_KEY_CODE_SCROLL_LOCK,
194aaac714fSJohn Arbuckle    [kVK_F15] = Q_KEY_CODE_PAUSE,
195aaac714fSJohn Arbuckle
1963e230dd2SCorentin Chary    /*
197aaac714fSJohn Arbuckle     * The eject and volume keys can't be used here because they are handled at
198aaac714fSJohn Arbuckle     * a lower level than what an Application can see.
1993e230dd2SCorentin Chary     */
2003e230dd2SCorentin Chary};
2013e230dd2SCorentin Chary
2023e230dd2SCorentin Charystatic int cocoa_keycode_to_qemu(int keycode)
2033e230dd2SCorentin Chary{
204aaac714fSJohn Arbuckle    if (ARRAY_SIZE(mac_to_qkeycode_map) <= keycode) {
20501cc4e6fSPeter Maydell        fprintf(stderr, "(cocoa) warning unknown keycode 0x%x\n", keycode);
2063e230dd2SCorentin Chary        return 0;
2073e230dd2SCorentin Chary    }
208aaac714fSJohn Arbuckle    return mac_to_qkeycode_map[keycode];
2093e230dd2SCorentin Chary}
2103e230dd2SCorentin Chary
211693a3e01SJohn Arbuckle/* Displays an alert dialog box with the specified message */
212693a3e01SJohn Arbucklestatic void QEMU_Alert(NSString *message)
213693a3e01SJohn Arbuckle{
214693a3e01SJohn Arbuckle    NSAlert *alert;
215693a3e01SJohn Arbuckle    alert = [NSAlert new];
216693a3e01SJohn Arbuckle    [alert setMessageText: message];
217693a3e01SJohn Arbuckle    [alert runModal];
218693a3e01SJohn Arbuckle}
2193e230dd2SCorentin Chary
220693a3e01SJohn Arbuckle/* Handles any errors that happen with a device transaction */
221693a3e01SJohn Arbucklestatic void handleAnyDeviceErrors(Error * err)
222693a3e01SJohn Arbuckle{
223693a3e01SJohn Arbuckle    if (err) {
224693a3e01SJohn Arbuckle        QEMU_Alert([NSString stringWithCString: error_get_pretty(err)
225693a3e01SJohn Arbuckle                                      encoding: NSASCIIStringEncoding]);
226693a3e01SJohn Arbuckle        error_free(err);
227693a3e01SJohn Arbuckle    }
228693a3e01SJohn Arbuckle}
2293e230dd2SCorentin Chary
2303e230dd2SCorentin Chary/*
2313e230dd2SCorentin Chary ------------------------------------------------------
2323e230dd2SCorentin Chary    QemuCocoaView
2333e230dd2SCorentin Chary ------------------------------------------------------
2343e230dd2SCorentin Chary*/
2353e230dd2SCorentin Chary@interface QemuCocoaView : NSView
2363e230dd2SCorentin Chary{
2373e230dd2SCorentin Chary    QEMUScreen screen;
2383e230dd2SCorentin Chary    NSWindow *fullScreenWindow;
2393e230dd2SCorentin Chary    float cx,cy,cw,ch,cdx,cdy;
2403e230dd2SCorentin Chary    CGDataProviderRef dataProviderRef;
2413e230dd2SCorentin Chary    int modifiers_state[256];
24249b9bd4dSPeter Maydell    BOOL isMouseGrabbed;
2433e230dd2SCorentin Chary    BOOL isFullscreen;
2443e230dd2SCorentin Chary    BOOL isAbsoluteEnabled;
245f61c387eSPeter Maydell    BOOL isMouseDeassociated;
2463e230dd2SCorentin Chary}
2475e00d3acSGerd Hoffmann- (void) switchSurface:(DisplaySurface *)surface;
2483e230dd2SCorentin Chary- (void) grabMouse;
2493e230dd2SCorentin Chary- (void) ungrabMouse;
2503e230dd2SCorentin Chary- (void) toggleFullScreen:(id)sender;
2513e230dd2SCorentin Chary- (void) handleEvent:(NSEvent *)event;
2523e230dd2SCorentin Chary- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
253f61c387eSPeter Maydell/* The state surrounding mouse grabbing is potentially confusing.
254f61c387eSPeter Maydell * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated
255f61c387eSPeter Maydell *   pointing device an absolute-position one?"], but is only updated on
256f61c387eSPeter Maydell *   next refresh.
257f61c387eSPeter Maydell * isMouseGrabbed tracks whether GUI events are directed to the guest;
258f61c387eSPeter Maydell *   it controls whether special keys like Cmd get sent to the guest,
259f61c387eSPeter Maydell *   and whether we capture the mouse when in non-absolute mode.
260f61c387eSPeter Maydell * isMouseDeassociated tracks whether we've told MacOSX to disassociate
261f61c387eSPeter Maydell *   the mouse and mouse cursor position by calling
262f61c387eSPeter Maydell *   CGAssociateMouseAndMouseCursorPosition(FALSE)
263f61c387eSPeter Maydell *   (which basically happens if we grab in non-absolute mode).
264f61c387eSPeter Maydell */
26549b9bd4dSPeter Maydell- (BOOL) isMouseGrabbed;
2663e230dd2SCorentin Chary- (BOOL) isAbsoluteEnabled;
267f61c387eSPeter Maydell- (BOOL) isMouseDeassociated;
2683e230dd2SCorentin Chary- (float) cdx;
2693e230dd2SCorentin Chary- (float) cdy;
2703e230dd2SCorentin Chary- (QEMUScreen) gscreen;
2713b178b71SJohn Arbuckle- (void) raiseAllKeys;
2723e230dd2SCorentin Chary@end
2733e230dd2SCorentin Chary
2747fee199cSAndreas FärberQemuCocoaView *cocoaView;
2757fee199cSAndreas Färber
2763e230dd2SCorentin Chary@implementation QemuCocoaView
2773e230dd2SCorentin Chary- (id)initWithFrame:(NSRect)frameRect
2783e230dd2SCorentin Chary{
2793e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
2803e230dd2SCorentin Chary
2813e230dd2SCorentin Chary    self = [super initWithFrame:frameRect];
2823e230dd2SCorentin Chary    if (self) {
2833e230dd2SCorentin Chary
2843e230dd2SCorentin Chary        screen.bitsPerComponent = 8;
2853e230dd2SCorentin Chary        screen.bitsPerPixel = 32;
2863e230dd2SCorentin Chary        screen.width = frameRect.size.width;
2873e230dd2SCorentin Chary        screen.height = frameRect.size.height;
2883e230dd2SCorentin Chary
2893e230dd2SCorentin Chary    }
2903e230dd2SCorentin Chary    return self;
2913e230dd2SCorentin Chary}
2923e230dd2SCorentin Chary
2933e230dd2SCorentin Chary- (void) dealloc
2943e230dd2SCorentin Chary{
2953e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: dealloc\n");
2963e230dd2SCorentin Chary
2973e230dd2SCorentin Chary    if (dataProviderRef)
2983e230dd2SCorentin Chary        CGDataProviderRelease(dataProviderRef);
2993e230dd2SCorentin Chary
3003e230dd2SCorentin Chary    [super dealloc];
3013e230dd2SCorentin Chary}
3023e230dd2SCorentin Chary
3033e230dd2SCorentin Chary- (BOOL) isOpaque
3043e230dd2SCorentin Chary{
3053e230dd2SCorentin Chary    return YES;
3063e230dd2SCorentin Chary}
3073e230dd2SCorentin Chary
3085dd45beeSPeter Maydell- (BOOL) screenContainsPoint:(NSPoint) p
3095dd45beeSPeter Maydell{
3105dd45beeSPeter Maydell    return (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height);
3115dd45beeSPeter Maydell}
3125dd45beeSPeter Maydell
31313aefd30SPeter Maydell- (void) hideCursor
31413aefd30SPeter Maydell{
31513aefd30SPeter Maydell    if (!cursor_hide) {
31613aefd30SPeter Maydell        return;
31713aefd30SPeter Maydell    }
31813aefd30SPeter Maydell    [NSCursor hide];
31913aefd30SPeter Maydell}
32013aefd30SPeter Maydell
32113aefd30SPeter Maydell- (void) unhideCursor
32213aefd30SPeter Maydell{
32313aefd30SPeter Maydell    if (!cursor_hide) {
32413aefd30SPeter Maydell        return;
32513aefd30SPeter Maydell    }
32613aefd30SPeter Maydell    [NSCursor unhide];
32713aefd30SPeter Maydell}
32813aefd30SPeter Maydell
3293e230dd2SCorentin Chary- (void) drawRect:(NSRect) rect
3303e230dd2SCorentin Chary{
3313e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: drawRect\n");
3323e230dd2SCorentin Chary
3333e230dd2SCorentin Chary    // get CoreGraphic context
3343e230dd2SCorentin Chary    CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
3353e230dd2SCorentin Chary    CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
3363e230dd2SCorentin Chary    CGContextSetShouldAntialias (viewContextRef, NO);
3373e230dd2SCorentin Chary
3383e230dd2SCorentin Chary    // draw screen bitmap directly to Core Graphics context
3397d270b1cSPeter Maydell    if (!dataProviderRef) {
3407d270b1cSPeter Maydell        // Draw request before any guest device has set up a framebuffer:
3417d270b1cSPeter Maydell        // just draw an opaque black rectangle
3427d270b1cSPeter Maydell        CGContextSetRGBFillColor(viewContextRef, 0, 0, 0, 1.0);
3437d270b1cSPeter Maydell        CGContextFillRect(viewContextRef, NSRectToCGRect(rect));
3447d270b1cSPeter Maydell    } else {
3453e230dd2SCorentin Chary        CGImageRef imageRef = CGImageCreate(
3463e230dd2SCorentin Chary            screen.width, //width
3473e230dd2SCorentin Chary            screen.height, //height
3483e230dd2SCorentin Chary            screen.bitsPerComponent, //bitsPerComponent
3493e230dd2SCorentin Chary            screen.bitsPerPixel, //bitsPerPixel
3503e230dd2SCorentin Chary            (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
3513e230dd2SCorentin Chary#ifdef __LITTLE_ENDIAN__
3523e230dd2SCorentin Chary            CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
3533e230dd2SCorentin Chary            kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
3543e230dd2SCorentin Chary#else
3553e230dd2SCorentin Chary            CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
3563e230dd2SCorentin Chary            kCGImageAlphaNoneSkipFirst, //bitmapInfo
3573e230dd2SCorentin Chary#endif
3583e230dd2SCorentin Chary            dataProviderRef, //provider
3593e230dd2SCorentin Chary            NULL, //decode
3603e230dd2SCorentin Chary            0, //interpolate
3613e230dd2SCorentin Chary            kCGRenderingIntentDefault //intent
3623e230dd2SCorentin Chary        );
3633e230dd2SCorentin Chary        // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
3643e230dd2SCorentin Chary        const NSRect *rectList;
3653e230dd2SCorentin Chary        NSInteger rectCount;
3663e230dd2SCorentin Chary        int i;
3673e230dd2SCorentin Chary        CGImageRef clipImageRef;
3683e230dd2SCorentin Chary        CGRect clipRect;
3693e230dd2SCorentin Chary
3703e230dd2SCorentin Chary        [self getRectsBeingDrawn:&rectList count:&rectCount];
3713e230dd2SCorentin Chary        for (i = 0; i < rectCount; i++) {
3723e230dd2SCorentin Chary            clipRect.origin.x = rectList[i].origin.x / cdx;
3733e230dd2SCorentin Chary            clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
3743e230dd2SCorentin Chary            clipRect.size.width = rectList[i].size.width / cdx;
3753e230dd2SCorentin Chary            clipRect.size.height = rectList[i].size.height / cdy;
3763e230dd2SCorentin Chary            clipImageRef = CGImageCreateWithImageInRect(
3773e230dd2SCorentin Chary                                                        imageRef,
3783e230dd2SCorentin Chary                                                        clipRect
3793e230dd2SCorentin Chary                                                        );
3803e230dd2SCorentin Chary            CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
3813e230dd2SCorentin Chary            CGImageRelease (clipImageRef);
3823e230dd2SCorentin Chary        }
3833e230dd2SCorentin Chary        CGImageRelease (imageRef);
3843e230dd2SCorentin Chary    }
3853e230dd2SCorentin Chary}
3863e230dd2SCorentin Chary
3873e230dd2SCorentin Chary- (void) setContentDimensions
3883e230dd2SCorentin Chary{
3893e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
3903e230dd2SCorentin Chary
3913e230dd2SCorentin Chary    if (isFullscreen) {
3923e230dd2SCorentin Chary        cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
3933e230dd2SCorentin Chary        cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
3945d1b2eefSProgrammingkid
3955d1b2eefSProgrammingkid        /* stretches video, but keeps same aspect ratio */
3965d1b2eefSProgrammingkid        if (stretch_video == true) {
3975d1b2eefSProgrammingkid            /* use smallest stretch value - prevents clipping on sides */
3985d1b2eefSProgrammingkid            if (MIN(cdx, cdy) == cdx) {
3995d1b2eefSProgrammingkid                cdy = cdx;
4005d1b2eefSProgrammingkid            } else {
4015d1b2eefSProgrammingkid                cdx = cdy;
4025d1b2eefSProgrammingkid            }
4035d1b2eefSProgrammingkid        } else {  /* No stretching */
4045d1b2eefSProgrammingkid            cdx = cdy = 1;
4055d1b2eefSProgrammingkid        }
4063e230dd2SCorentin Chary        cw = screen.width * cdx;
4073e230dd2SCorentin Chary        ch = screen.height * cdy;
4083e230dd2SCorentin Chary        cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
4093e230dd2SCorentin Chary        cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
4103e230dd2SCorentin Chary    } else {
4113e230dd2SCorentin Chary        cx = 0;
4123e230dd2SCorentin Chary        cy = 0;
4133e230dd2SCorentin Chary        cw = screen.width;
4143e230dd2SCorentin Chary        ch = screen.height;
4153e230dd2SCorentin Chary        cdx = 1.0;
4163e230dd2SCorentin Chary        cdy = 1.0;
4173e230dd2SCorentin Chary    }
4183e230dd2SCorentin Chary}
4193e230dd2SCorentin Chary
4205e00d3acSGerd Hoffmann- (void) switchSurface:(DisplaySurface *)surface
4213e230dd2SCorentin Chary{
4225e00d3acSGerd Hoffmann    COCOA_DEBUG("QemuCocoaView: switchSurface\n");
4233e230dd2SCorentin Chary
4248510d91eSPeter Maydell    int w = surface_width(surface);
4258510d91eSPeter Maydell    int h = surface_height(surface);
426381600daSPeter Maydell    /* cdx == 0 means this is our very first surface, in which case we need
427381600daSPeter Maydell     * to recalculate the content dimensions even if it happens to be the size
428381600daSPeter Maydell     * of the initial empty window.
429381600daSPeter Maydell     */
430381600daSPeter Maydell    bool isResize = (w != screen.width || h != screen.height || cdx == 0.0);
431d3345a04SPeter Maydell
432d3345a04SPeter Maydell    int oldh = screen.height;
433d3345a04SPeter Maydell    if (isResize) {
434d3345a04SPeter Maydell        // Resize before we trigger the redraw, or we'll redraw at the wrong size
435d3345a04SPeter Maydell        COCOA_DEBUG("switchSurface: new size %d x %d\n", w, h);
436d3345a04SPeter Maydell        screen.width = w;
437d3345a04SPeter Maydell        screen.height = h;
438d3345a04SPeter Maydell        [self setContentDimensions];
439d3345a04SPeter Maydell        [self setFrame:NSMakeRect(cx, cy, cw, ch)];
440d3345a04SPeter Maydell    }
4418510d91eSPeter Maydell
4423e230dd2SCorentin Chary    // update screenBuffer
4433e230dd2SCorentin Chary    if (dataProviderRef)
4443e230dd2SCorentin Chary        CGDataProviderRelease(dataProviderRef);
4453e230dd2SCorentin Chary
4463e230dd2SCorentin Chary    //sync host window color space with guests
4475e00d3acSGerd Hoffmann    screen.bitsPerPixel = surface_bits_per_pixel(surface);
4485e00d3acSGerd Hoffmann    screen.bitsPerComponent = surface_bytes_per_pixel(surface) * 2;
4493e230dd2SCorentin Chary
4505e00d3acSGerd Hoffmann    dataProviderRef = CGDataProviderCreateWithData(NULL, surface_data(surface), w * 4 * h, NULL);
4513e230dd2SCorentin Chary
4523e230dd2SCorentin Chary    // update windows
4533e230dd2SCorentin Chary    if (isFullscreen) {
4543e230dd2SCorentin Chary        [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
455d3345a04SPeter Maydell        [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:NO animate:NO];
4563e230dd2SCorentin Chary    } else {
4573e230dd2SCorentin Chary        if (qemu_name)
4583e230dd2SCorentin Chary            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
459d3345a04SPeter Maydell        [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + oldh, w, h + [normalWindow frame].size.height - oldh) display:YES animate:NO];
4603e230dd2SCorentin Chary    }
461d3345a04SPeter Maydell
462d3345a04SPeter Maydell    if (isResize) {
4633e230dd2SCorentin Chary        [normalWindow center];
464d3345a04SPeter Maydell    }
4653e230dd2SCorentin Chary}
4663e230dd2SCorentin Chary
4673e230dd2SCorentin Chary- (void) toggleFullScreen:(id)sender
4683e230dd2SCorentin Chary{
4693e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
4703e230dd2SCorentin Chary
4713e230dd2SCorentin Chary    if (isFullscreen) { // switch from fullscreen to desktop
4723e230dd2SCorentin Chary        isFullscreen = FALSE;
4733e230dd2SCorentin Chary        [self ungrabMouse];
4743e230dd2SCorentin Chary        [self setContentDimensions];
4753e230dd2SCorentin Chary        if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
4763e230dd2SCorentin Chary            [self exitFullScreenModeWithOptions:nil];
4773e230dd2SCorentin Chary        } else {
4783e230dd2SCorentin Chary            [fullScreenWindow close];
4793e230dd2SCorentin Chary            [normalWindow setContentView: self];
4803e230dd2SCorentin Chary            [normalWindow makeKeyAndOrderFront: self];
4813e230dd2SCorentin Chary            [NSMenu setMenuBarVisible:YES];
4823e230dd2SCorentin Chary        }
4833e230dd2SCorentin Chary    } else { // switch from desktop to fullscreen
4843e230dd2SCorentin Chary        isFullscreen = TRUE;
4855d1b2eefSProgrammingkid        [normalWindow orderOut: nil]; /* Hide the window */
4863e230dd2SCorentin Chary        [self grabMouse];
4873e230dd2SCorentin Chary        [self setContentDimensions];
4883e230dd2SCorentin Chary        if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
4893e230dd2SCorentin Chary            [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
4903e230dd2SCorentin Chary                [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
4913e230dd2SCorentin Chary                [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
4923e230dd2SCorentin Chary                 nil]];
4933e230dd2SCorentin Chary        } else {
4943e230dd2SCorentin Chary            [NSMenu setMenuBarVisible:NO];
4953e230dd2SCorentin Chary            fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
4963e230dd2SCorentin Chary                styleMask:NSBorderlessWindowMask
4973e230dd2SCorentin Chary                backing:NSBackingStoreBuffered
4983e230dd2SCorentin Chary                defer:NO];
4995d1b2eefSProgrammingkid            [fullScreenWindow setAcceptsMouseMovedEvents: YES];
5003e230dd2SCorentin Chary            [fullScreenWindow setHasShadow:NO];
5015d1b2eefSProgrammingkid            [fullScreenWindow setBackgroundColor: [NSColor blackColor]];
5025d1b2eefSProgrammingkid            [self setFrame:NSMakeRect(cx, cy, cw, ch)];
5035d1b2eefSProgrammingkid            [[fullScreenWindow contentView] addSubview: self];
5043e230dd2SCorentin Chary            [fullScreenWindow makeKeyAndOrderFront:self];
5053e230dd2SCorentin Chary        }
5063e230dd2SCorentin Chary    }
5073e230dd2SCorentin Chary}
5083e230dd2SCorentin Chary
5093e230dd2SCorentin Chary- (void) handleEvent:(NSEvent *)event
5103e230dd2SCorentin Chary{
5113e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: handleEvent\n");
5123e230dd2SCorentin Chary
5133e230dd2SCorentin Chary    int buttons = 0;
5143e230dd2SCorentin Chary    int keycode;
51521bae11aSGerd Hoffmann    bool mouse_event = false;
5163e230dd2SCorentin Chary    NSPoint p = [event locationInWindow];
5173e230dd2SCorentin Chary
5183e230dd2SCorentin Chary    switch ([event type]) {
5193e230dd2SCorentin Chary        case NSFlagsChanged:
5203e230dd2SCorentin Chary            keycode = cocoa_keycode_to_qemu([event keyCode]);
5218895919aSPeter Maydell
522aaac714fSJohn Arbuckle            if ((keycode == Q_KEY_CODE_META_L || keycode == Q_KEY_CODE_META_R)
523aaac714fSJohn Arbuckle               && !isMouseGrabbed) {
5248895919aSPeter Maydell              /* Don't pass command key changes to guest unless mouse is grabbed */
5258895919aSPeter Maydell              keycode = 0;
5268895919aSPeter Maydell            }
5278895919aSPeter Maydell
5283e230dd2SCorentin Chary            if (keycode) {
529aaac714fSJohn Arbuckle                // emulate caps lock and num lock keydown and keyup
530aaac714fSJohn Arbuckle                if (keycode == Q_KEY_CODE_CAPS_LOCK ||
531aaac714fSJohn Arbuckle                    keycode == Q_KEY_CODE_NUM_LOCK) {
532aaac714fSJohn Arbuckle                    qemu_input_event_send_key_qcode(dcl->con, keycode, true);
533aaac714fSJohn Arbuckle                    qemu_input_event_send_key_qcode(dcl->con, keycode, false);
53468c0aa6eSPeter Maydell                } else if (qemu_console_is_graphic(NULL)) {
5353e230dd2SCorentin Chary                    if (modifiers_state[keycode] == 0) { // keydown
536aaac714fSJohn Arbuckle                        qemu_input_event_send_key_qcode(dcl->con, keycode, true);
5373e230dd2SCorentin Chary                        modifiers_state[keycode] = 1;
5383e230dd2SCorentin Chary                    } else { // keyup
539aaac714fSJohn Arbuckle                        qemu_input_event_send_key_qcode(dcl->con, keycode, false);
5403e230dd2SCorentin Chary                        modifiers_state[keycode] = 0;
5413e230dd2SCorentin Chary                    }
5423e230dd2SCorentin Chary                }
5433e230dd2SCorentin Chary            }
5443e230dd2SCorentin Chary
5453e230dd2SCorentin Chary            // release Mouse grab when pressing ctrl+alt
5465d1b2eefSProgrammingkid            if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
5473e230dd2SCorentin Chary                [self ungrabMouse];
5483e230dd2SCorentin Chary            }
5493e230dd2SCorentin Chary            break;
5503e230dd2SCorentin Chary        case NSKeyDown:
5518895919aSPeter Maydell            keycode = cocoa_keycode_to_qemu([event keyCode]);
5523e230dd2SCorentin Chary
5538895919aSPeter Maydell            // forward command key combos to the host UI unless the mouse is grabbed
55449b9bd4dSPeter Maydell            if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
5553e230dd2SCorentin Chary                [NSApp sendEvent:event];
5563e230dd2SCorentin Chary                return;
5573e230dd2SCorentin Chary            }
5583e230dd2SCorentin Chary
5593e230dd2SCorentin Chary            // default
5603e230dd2SCorentin Chary
5613e230dd2SCorentin Chary            // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
5623e230dd2SCorentin Chary            if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
5633e230dd2SCorentin Chary                switch (keycode) {
5643e230dd2SCorentin Chary
5653e230dd2SCorentin Chary                    // enable graphic console
566aaac714fSJohn Arbuckle                    case Q_KEY_CODE_1 ... Q_KEY_CODE_9: // '1' to '9' keys
567aaac714fSJohn Arbuckle                        console_select(keycode - 11);
5683e230dd2SCorentin Chary                        break;
5693e230dd2SCorentin Chary                }
5703e230dd2SCorentin Chary
5713e230dd2SCorentin Chary            // handle keys for graphic console
57268c0aa6eSPeter Maydell            } else if (qemu_console_is_graphic(NULL)) {
573aaac714fSJohn Arbuckle                qemu_input_event_send_key_qcode(dcl->con, keycode, true);
5743e230dd2SCorentin Chary
5753e230dd2SCorentin Chary            // handlekeys for Monitor
5763e230dd2SCorentin Chary            } else {
5773e230dd2SCorentin Chary                int keysym = 0;
5783e230dd2SCorentin Chary                switch([event keyCode]) {
5793e230dd2SCorentin Chary                case 115:
5803e230dd2SCorentin Chary                    keysym = QEMU_KEY_HOME;
5813e230dd2SCorentin Chary                    break;
5823e230dd2SCorentin Chary                case 117:
5833e230dd2SCorentin Chary                    keysym = QEMU_KEY_DELETE;
5843e230dd2SCorentin Chary                    break;
5853e230dd2SCorentin Chary                case 119:
5863e230dd2SCorentin Chary                    keysym = QEMU_KEY_END;
5873e230dd2SCorentin Chary                    break;
5883e230dd2SCorentin Chary                case 123:
5893e230dd2SCorentin Chary                    keysym = QEMU_KEY_LEFT;
5903e230dd2SCorentin Chary                    break;
5913e230dd2SCorentin Chary                case 124:
5923e230dd2SCorentin Chary                    keysym = QEMU_KEY_RIGHT;
5933e230dd2SCorentin Chary                    break;
5943e230dd2SCorentin Chary                case 125:
5953e230dd2SCorentin Chary                    keysym = QEMU_KEY_DOWN;
5963e230dd2SCorentin Chary                    break;
5973e230dd2SCorentin Chary                case 126:
5983e230dd2SCorentin Chary                    keysym = QEMU_KEY_UP;
5993e230dd2SCorentin Chary                    break;
6003e230dd2SCorentin Chary                default:
6013e230dd2SCorentin Chary                    {
6023e230dd2SCorentin Chary                        NSString *ks = [event characters];
6033e230dd2SCorentin Chary                        if ([ks length] > 0)
6043e230dd2SCorentin Chary                            keysym = [ks characterAtIndex:0];
6053e230dd2SCorentin Chary                    }
6063e230dd2SCorentin Chary                }
6073e230dd2SCorentin Chary                if (keysym)
6083e230dd2SCorentin Chary                    kbd_put_keysym(keysym);
6093e230dd2SCorentin Chary            }
6103e230dd2SCorentin Chary            break;
6113e230dd2SCorentin Chary        case NSKeyUp:
6123e230dd2SCorentin Chary            keycode = cocoa_keycode_to_qemu([event keyCode]);
6138895919aSPeter Maydell
6148895919aSPeter Maydell            // don't pass the guest a spurious key-up if we treated this
6158895919aSPeter Maydell            // command-key combo as a host UI action
61649b9bd4dSPeter Maydell            if (!isMouseGrabbed && ([event modifierFlags] & NSCommandKeyMask)) {
6178895919aSPeter Maydell                return;
6188895919aSPeter Maydell            }
6198895919aSPeter Maydell
62068c0aa6eSPeter Maydell            if (qemu_console_is_graphic(NULL)) {
621aaac714fSJohn Arbuckle                qemu_input_event_send_key_qcode(dcl->con, keycode, false);
6223e230dd2SCorentin Chary            }
6233e230dd2SCorentin Chary            break;
6243e230dd2SCorentin Chary        case NSMouseMoved:
6253e230dd2SCorentin Chary            if (isAbsoluteEnabled) {
6265dd45beeSPeter Maydell                if (![self screenContainsPoint:p] || ![[self window] isKeyWindow]) {
627f61c387eSPeter Maydell                    if (isMouseGrabbed) {
628f61c387eSPeter Maydell                        [self ungrabMouse];
6293e230dd2SCorentin Chary                    }
6303e230dd2SCorentin Chary                } else {
631f61c387eSPeter Maydell                    if (!isMouseGrabbed) {
632f61c387eSPeter Maydell                        [self grabMouse];
6333e230dd2SCorentin Chary                    }
6343e230dd2SCorentin Chary                }
6353e230dd2SCorentin Chary            }
63621bae11aSGerd Hoffmann            mouse_event = true;
6373e230dd2SCorentin Chary            break;
6383e230dd2SCorentin Chary        case NSLeftMouseDown:
6393e230dd2SCorentin Chary            if ([event modifierFlags] & NSCommandKeyMask) {
6403e230dd2SCorentin Chary                buttons |= MOUSE_EVENT_RBUTTON;
6413e230dd2SCorentin Chary            } else {
6423e230dd2SCorentin Chary                buttons |= MOUSE_EVENT_LBUTTON;
6433e230dd2SCorentin Chary            }
64421bae11aSGerd Hoffmann            mouse_event = true;
6453e230dd2SCorentin Chary            break;
6463e230dd2SCorentin Chary        case NSRightMouseDown:
6473e230dd2SCorentin Chary            buttons |= MOUSE_EVENT_RBUTTON;
64821bae11aSGerd Hoffmann            mouse_event = true;
6493e230dd2SCorentin Chary            break;
6503e230dd2SCorentin Chary        case NSOtherMouseDown:
6513e230dd2SCorentin Chary            buttons |= MOUSE_EVENT_MBUTTON;
65221bae11aSGerd Hoffmann            mouse_event = true;
6533e230dd2SCorentin Chary            break;
6543e230dd2SCorentin Chary        case NSLeftMouseDragged:
6553e230dd2SCorentin Chary            if ([event modifierFlags] & NSCommandKeyMask) {
6563e230dd2SCorentin Chary                buttons |= MOUSE_EVENT_RBUTTON;
6573e230dd2SCorentin Chary            } else {
6583e230dd2SCorentin Chary                buttons |= MOUSE_EVENT_LBUTTON;
6593e230dd2SCorentin Chary            }
66021bae11aSGerd Hoffmann            mouse_event = true;
6613e230dd2SCorentin Chary            break;
6623e230dd2SCorentin Chary        case NSRightMouseDragged:
6633e230dd2SCorentin Chary            buttons |= MOUSE_EVENT_RBUTTON;
66421bae11aSGerd Hoffmann            mouse_event = true;
6653e230dd2SCorentin Chary            break;
6663e230dd2SCorentin Chary        case NSOtherMouseDragged:
6673e230dd2SCorentin Chary            buttons |= MOUSE_EVENT_MBUTTON;
66821bae11aSGerd Hoffmann            mouse_event = true;
6693e230dd2SCorentin Chary            break;
6703e230dd2SCorentin Chary        case NSLeftMouseUp:
67121bae11aSGerd Hoffmann            mouse_event = true;
672f61c387eSPeter Maydell            if (!isMouseGrabbed && [self screenContainsPoint:p]) {
6733e230dd2SCorentin Chary                [self grabMouse];
6743e230dd2SCorentin Chary            }
6753e230dd2SCorentin Chary            break;
6763e230dd2SCorentin Chary        case NSRightMouseUp:
67721bae11aSGerd Hoffmann            mouse_event = true;
6783e230dd2SCorentin Chary            break;
6793e230dd2SCorentin Chary        case NSOtherMouseUp:
68021bae11aSGerd Hoffmann            mouse_event = true;
6813e230dd2SCorentin Chary            break;
6823e230dd2SCorentin Chary        case NSScrollWheel:
683f61c387eSPeter Maydell            if (isMouseGrabbed) {
68421bae11aSGerd Hoffmann                buttons |= ([event deltaY] < 0) ?
68521bae11aSGerd Hoffmann                    MOUSE_EVENT_WHEELUP : MOUSE_EVENT_WHEELDN;
6863e230dd2SCorentin Chary            }
687f61c387eSPeter Maydell            mouse_event = true;
6883e230dd2SCorentin Chary            break;
6893e230dd2SCorentin Chary        default:
6903e230dd2SCorentin Chary            [NSApp sendEvent:event];
6913e230dd2SCorentin Chary    }
69221bae11aSGerd Hoffmann
69321bae11aSGerd Hoffmann    if (mouse_event) {
6948d3a5d9bSPeter Maydell        /* Don't send button events to the guest unless we've got a
6958d3a5d9bSPeter Maydell         * mouse grab or window focus. If we have neither then this event
6968d3a5d9bSPeter Maydell         * is the user clicking on the background window to activate and
6978d3a5d9bSPeter Maydell         * bring us to the front, which will be done by the sendEvent
6988d3a5d9bSPeter Maydell         * call below. We definitely don't want to pass that click through
6998d3a5d9bSPeter Maydell         * to the guest.
7008d3a5d9bSPeter Maydell         */
7018d3a5d9bSPeter Maydell        if ((isMouseGrabbed || [[self window] isKeyWindow]) &&
7028d3a5d9bSPeter Maydell            (last_buttons != buttons)) {
7037fb1cf16SEric Blake            static uint32_t bmap[INPUT_BUTTON__MAX] = {
70421bae11aSGerd Hoffmann                [INPUT_BUTTON_LEFT]       = MOUSE_EVENT_LBUTTON,
70521bae11aSGerd Hoffmann                [INPUT_BUTTON_MIDDLE]     = MOUSE_EVENT_MBUTTON,
70621bae11aSGerd Hoffmann                [INPUT_BUTTON_RIGHT]      = MOUSE_EVENT_RBUTTON,
707f22d0af0SGerd Hoffmann                [INPUT_BUTTON_WHEEL_UP]   = MOUSE_EVENT_WHEELUP,
708f22d0af0SGerd Hoffmann                [INPUT_BUTTON_WHEEL_DOWN] = MOUSE_EVENT_WHEELDN,
70921bae11aSGerd Hoffmann            };
71021bae11aSGerd Hoffmann            qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
71121bae11aSGerd Hoffmann            last_buttons = buttons;
71221bae11aSGerd Hoffmann        }
713f61c387eSPeter Maydell        if (isMouseGrabbed) {
714f61c387eSPeter Maydell            if (isAbsoluteEnabled) {
715f61c387eSPeter Maydell                /* Note that the origin for Cocoa mouse coords is bottom left, not top left.
716f61c387eSPeter Maydell                 * The check on screenContainsPoint is to avoid sending out of range values for
717f61c387eSPeter Maydell                 * clicks in the titlebar.
718f61c387eSPeter Maydell                 */
719f61c387eSPeter Maydell                if ([self screenContainsPoint:p]) {
72021bae11aSGerd Hoffmann                    qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, screen.width);
721f61c387eSPeter Maydell                    qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, screen.height);
722f61c387eSPeter Maydell                }
723f61c387eSPeter Maydell            } else {
72421bae11aSGerd Hoffmann                qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
72521bae11aSGerd Hoffmann                qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
726f61c387eSPeter Maydell            }
72721bae11aSGerd Hoffmann        } else {
72821bae11aSGerd Hoffmann            [NSApp sendEvent:event];
72921bae11aSGerd Hoffmann        }
73021bae11aSGerd Hoffmann        qemu_input_event_sync();
73121bae11aSGerd Hoffmann    }
7323e230dd2SCorentin Chary}
7333e230dd2SCorentin Chary
7343e230dd2SCorentin Chary- (void) grabMouse
7353e230dd2SCorentin Chary{
7363e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: grabMouse\n");
7373e230dd2SCorentin Chary
7383e230dd2SCorentin Chary    if (!isFullscreen) {
7393e230dd2SCorentin Chary        if (qemu_name)
7403e230dd2SCorentin Chary            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
7413e230dd2SCorentin Chary        else
7423e230dd2SCorentin Chary            [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
7433e230dd2SCorentin Chary    }
74413aefd30SPeter Maydell    [self hideCursor];
745f61c387eSPeter Maydell    if (!isAbsoluteEnabled) {
746f61c387eSPeter Maydell        isMouseDeassociated = TRUE;
7473e230dd2SCorentin Chary        CGAssociateMouseAndMouseCursorPosition(FALSE);
748f61c387eSPeter Maydell    }
74949b9bd4dSPeter Maydell    isMouseGrabbed = TRUE; // while isMouseGrabbed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
7503e230dd2SCorentin Chary}
7513e230dd2SCorentin Chary
7523e230dd2SCorentin Chary- (void) ungrabMouse
7533e230dd2SCorentin Chary{
7543e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
7553e230dd2SCorentin Chary
7563e230dd2SCorentin Chary    if (!isFullscreen) {
7573e230dd2SCorentin Chary        if (qemu_name)
7583e230dd2SCorentin Chary            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
7593e230dd2SCorentin Chary        else
7603e230dd2SCorentin Chary            [normalWindow setTitle:@"QEMU"];
7613e230dd2SCorentin Chary    }
76213aefd30SPeter Maydell    [self unhideCursor];
763f61c387eSPeter Maydell    if (isMouseDeassociated) {
7643e230dd2SCorentin Chary        CGAssociateMouseAndMouseCursorPosition(TRUE);
765f61c387eSPeter Maydell        isMouseDeassociated = FALSE;
766f61c387eSPeter Maydell    }
76749b9bd4dSPeter Maydell    isMouseGrabbed = FALSE;
7683e230dd2SCorentin Chary}
7693e230dd2SCorentin Chary
7703e230dd2SCorentin Chary- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
77149b9bd4dSPeter Maydell- (BOOL) isMouseGrabbed {return isMouseGrabbed;}
7723e230dd2SCorentin Chary- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
773f61c387eSPeter Maydell- (BOOL) isMouseDeassociated {return isMouseDeassociated;}
7743e230dd2SCorentin Chary- (float) cdx {return cdx;}
7753e230dd2SCorentin Chary- (float) cdy {return cdy;}
7763e230dd2SCorentin Chary- (QEMUScreen) gscreen {return screen;}
7773b178b71SJohn Arbuckle
7783b178b71SJohn Arbuckle/*
7793b178b71SJohn Arbuckle * Makes the target think all down keys are being released.
7803b178b71SJohn Arbuckle * This prevents a stuck key problem, since we will not see
7813b178b71SJohn Arbuckle * key up events for those keys after we have lost focus.
7823b178b71SJohn Arbuckle */
7833b178b71SJohn Arbuckle- (void) raiseAllKeys
7843b178b71SJohn Arbuckle{
7853b178b71SJohn Arbuckle    int index;
7863b178b71SJohn Arbuckle    const int max_index = ARRAY_SIZE(modifiers_state);
7873b178b71SJohn Arbuckle
7883b178b71SJohn Arbuckle   for (index = 0; index < max_index; index++) {
7893b178b71SJohn Arbuckle       if (modifiers_state[index]) {
7903b178b71SJohn Arbuckle           modifiers_state[index] = 0;
791aaac714fSJohn Arbuckle           qemu_input_event_send_key_qcode(dcl->con, index, false);
7923b178b71SJohn Arbuckle       }
7933b178b71SJohn Arbuckle   }
7943b178b71SJohn Arbuckle}
7953e230dd2SCorentin Chary@end
7963e230dd2SCorentin Chary
7973e230dd2SCorentin Chary
7983e230dd2SCorentin Chary
7993e230dd2SCorentin Chary/*
8003e230dd2SCorentin Chary ------------------------------------------------------
8013e230dd2SCorentin Chary    QemuCocoaAppController
8023e230dd2SCorentin Chary ------------------------------------------------------
8033e230dd2SCorentin Chary*/
8043e230dd2SCorentin Chary@interface QemuCocoaAppController : NSObject
8052a4c8c53SPeter Maydell#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
806d9bc14f6SJohn Arbuckle                                       <NSWindowDelegate, NSApplicationDelegate>
8072a4c8c53SPeter Maydell#endif
8083e230dd2SCorentin Chary{
8093e230dd2SCorentin Chary}
8103e230dd2SCorentin Chary- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
8115d1b2eefSProgrammingkid- (void)doToggleFullScreen:(id)sender;
8123e230dd2SCorentin Chary- (void)toggleFullScreen:(id)sender;
8133e230dd2SCorentin Chary- (void)showQEMUDoc:(id)sender;
8143e230dd2SCorentin Chary- (void)showQEMUTec:(id)sender;
8155d1b2eefSProgrammingkid- (void)zoomToFit:(id) sender;
816b4c6a112SProgrammingkid- (void)displayConsole:(id)sender;
8178524f1c7SJohn Arbuckle- (void)pauseQEMU:(id)sender;
8188524f1c7SJohn Arbuckle- (void)resumeQEMU:(id)sender;
8198524f1c7SJohn Arbuckle- (void)displayPause;
8208524f1c7SJohn Arbuckle- (void)removePause;
82127074614SJohn Arbuckle- (void)restartQEMU:(id)sender;
82227074614SJohn Arbuckle- (void)powerDownQEMU:(id)sender;
823693a3e01SJohn Arbuckle- (void)ejectDeviceMedia:(id)sender;
824693a3e01SJohn Arbuckle- (void)changeDeviceMedia:(id)sender;
825d9bc14f6SJohn Arbuckle- (BOOL)verifyQuit;
826f4747900SJohn Arbuckle- (void)openDocumentation:(NSString *)filename;
8273e230dd2SCorentin Chary@end
8283e230dd2SCorentin Chary
8293e230dd2SCorentin Chary@implementation QemuCocoaAppController
8303e230dd2SCorentin Chary- (id) init
8313e230dd2SCorentin Chary{
8323e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: init\n");
8333e230dd2SCorentin Chary
8343e230dd2SCorentin Chary    self = [super init];
8353e230dd2SCorentin Chary    if (self) {
8363e230dd2SCorentin Chary
8373e230dd2SCorentin Chary        // create a view and add it to the window
8383e230dd2SCorentin Chary        cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
8393e230dd2SCorentin Chary        if(!cocoaView) {
8403e230dd2SCorentin Chary            fprintf(stderr, "(cocoa) can't create a view\n");
8413e230dd2SCorentin Chary            exit(1);
8423e230dd2SCorentin Chary        }
8433e230dd2SCorentin Chary
8443e230dd2SCorentin Chary        // create a window
8453e230dd2SCorentin Chary        normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
8463e230dd2SCorentin Chary            styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
8473e230dd2SCorentin Chary            backing:NSBackingStoreBuffered defer:NO];
8483e230dd2SCorentin Chary        if(!normalWindow) {
8493e230dd2SCorentin Chary            fprintf(stderr, "(cocoa) can't create window\n");
8503e230dd2SCorentin Chary            exit(1);
8513e230dd2SCorentin Chary        }
8523e230dd2SCorentin Chary        [normalWindow setAcceptsMouseMovedEvents:YES];
853a1dbc05aSJohn Arbuckle        [normalWindow setTitle:@"QEMU"];
8543e230dd2SCorentin Chary        [normalWindow setContentView:cocoaView];
85581801ae2SPeter Maydell#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10)
8563e230dd2SCorentin Chary        [normalWindow useOptimizedDrawing:YES];
85781801ae2SPeter Maydell#endif
8583e230dd2SCorentin Chary        [normalWindow makeKeyAndOrderFront:self];
8593e230dd2SCorentin Chary        [normalWindow center];
860d9bc14f6SJohn Arbuckle        [normalWindow setDelegate: self];
8615d1b2eefSProgrammingkid        stretch_video = false;
8628524f1c7SJohn Arbuckle
8638524f1c7SJohn Arbuckle        /* Used for displaying pause on the screen */
8648524f1c7SJohn Arbuckle        pauseLabel = [NSTextField new];
8658524f1c7SJohn Arbuckle        [pauseLabel setBezeled:YES];
8668524f1c7SJohn Arbuckle        [pauseLabel setDrawsBackground:YES];
8678524f1c7SJohn Arbuckle        [pauseLabel setBackgroundColor: [NSColor whiteColor]];
8688524f1c7SJohn Arbuckle        [pauseLabel setEditable:NO];
8698524f1c7SJohn Arbuckle        [pauseLabel setSelectable:NO];
8708524f1c7SJohn Arbuckle        [pauseLabel setStringValue: @"Paused"];
8718524f1c7SJohn Arbuckle        [pauseLabel setFont: [NSFont fontWithName: @"Helvetica" size: 90]];
8728524f1c7SJohn Arbuckle        [pauseLabel setTextColor: [NSColor blackColor]];
8738524f1c7SJohn Arbuckle        [pauseLabel sizeToFit];
874693a3e01SJohn Arbuckle
875693a3e01SJohn Arbuckle        // set the supported image file types that can be opened
876693a3e01SJohn Arbuckle        supportedImageFileTypes = [NSArray arrayWithObjects: @"img", @"iso", @"dmg",
877*9d227f19SJohn Arbuckle                                 @"qcow", @"qcow2", @"cloop", @"vmdk", @"cdr",
878*9d227f19SJohn Arbuckle                                  nil];
8793e230dd2SCorentin Chary    }
8803e230dd2SCorentin Chary    return self;
8813e230dd2SCorentin Chary}
8823e230dd2SCorentin Chary
8833e230dd2SCorentin Chary- (void) dealloc
8843e230dd2SCorentin Chary{
8853e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
8863e230dd2SCorentin Chary
8873e230dd2SCorentin Chary    if (cocoaView)
8883e230dd2SCorentin Chary        [cocoaView release];
8893e230dd2SCorentin Chary    [super dealloc];
8903e230dd2SCorentin Chary}
8913e230dd2SCorentin Chary
8923e230dd2SCorentin Chary- (void)applicationDidFinishLaunching: (NSNotification *) note
8933e230dd2SCorentin Chary{
8943e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
895365d7f3cSJohn Arbuckle    // launch QEMU, with the global args
8963e230dd2SCorentin Chary    [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
8973e230dd2SCorentin Chary}
8983e230dd2SCorentin Chary
8993e230dd2SCorentin Chary- (void)applicationWillTerminate:(NSNotification *)aNotification
9003e230dd2SCorentin Chary{
9013e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
9023e230dd2SCorentin Chary
9033e230dd2SCorentin Chary    qemu_system_shutdown_request();
9043e230dd2SCorentin Chary    exit(0);
9053e230dd2SCorentin Chary}
9063e230dd2SCorentin Chary
9073e230dd2SCorentin Chary- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
9083e230dd2SCorentin Chary{
9093e230dd2SCorentin Chary    return YES;
9103e230dd2SCorentin Chary}
9113e230dd2SCorentin Chary
912d9bc14f6SJohn Arbuckle- (NSApplicationTerminateReply)applicationShouldTerminate:
913d9bc14f6SJohn Arbuckle                                                         (NSApplication *)sender
914d9bc14f6SJohn Arbuckle{
915d9bc14f6SJohn Arbuckle    COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n");
916d9bc14f6SJohn Arbuckle    return [self verifyQuit];
917d9bc14f6SJohn Arbuckle}
918d9bc14f6SJohn Arbuckle
919d9bc14f6SJohn Arbuckle/* Called when the user clicks on a window's close button */
920d9bc14f6SJohn Arbuckle- (BOOL)windowShouldClose:(id)sender
921d9bc14f6SJohn Arbuckle{
922d9bc14f6SJohn Arbuckle    COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n");
923d9bc14f6SJohn Arbuckle    [NSApp terminate: sender];
924d9bc14f6SJohn Arbuckle    /* If the user allows the application to quit then the call to
925d9bc14f6SJohn Arbuckle     * NSApp terminate will never return. If we get here then the user
926d9bc14f6SJohn Arbuckle     * cancelled the quit, so we should return NO to not permit the
927d9bc14f6SJohn Arbuckle     * closing of this window.
928d9bc14f6SJohn Arbuckle     */
929d9bc14f6SJohn Arbuckle    return NO;
930d9bc14f6SJohn Arbuckle}
931d9bc14f6SJohn Arbuckle
9323b178b71SJohn Arbuckle/* Called when QEMU goes into the background */
9333b178b71SJohn Arbuckle- (void) applicationWillResignActive: (NSNotification *)aNotification
9343b178b71SJohn Arbuckle{
9353b178b71SJohn Arbuckle    COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
9363b178b71SJohn Arbuckle    [cocoaView raiseAllKeys];
9373b178b71SJohn Arbuckle}
9383b178b71SJohn Arbuckle
9393e230dd2SCorentin Chary- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
9403e230dd2SCorentin Chary{
9413e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
9423e230dd2SCorentin Chary
9433e230dd2SCorentin Chary    int status;
9443bbbee18SAndreas Färber    status = qemu_main(argc, argv, *_NSGetEnviron());
9453e230dd2SCorentin Chary    exit(status);
9463e230dd2SCorentin Chary}
9473e230dd2SCorentin Chary
9485d1b2eefSProgrammingkid/* We abstract the method called by the Enter Fullscreen menu item
9495d1b2eefSProgrammingkid * because Mac OS 10.7 and higher disables it. This is because of the
9505d1b2eefSProgrammingkid * menu item's old selector's name toggleFullScreen:
9515d1b2eefSProgrammingkid */
9525d1b2eefSProgrammingkid- (void) doToggleFullScreen:(id)sender
9535d1b2eefSProgrammingkid{
9545d1b2eefSProgrammingkid    [self toggleFullScreen:(id)sender];
9555d1b2eefSProgrammingkid}
9565d1b2eefSProgrammingkid
9573e230dd2SCorentin Chary- (void)toggleFullScreen:(id)sender
9583e230dd2SCorentin Chary{
9593e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
9603e230dd2SCorentin Chary
9613e230dd2SCorentin Chary    [cocoaView toggleFullScreen:sender];
9623e230dd2SCorentin Chary}
9633e230dd2SCorentin Chary
964f4747900SJohn Arbuckle/* Tries to find then open the specified filename */
965f4747900SJohn Arbuckle- (void) openDocumentation: (NSString *) filename
966f4747900SJohn Arbuckle{
967f4747900SJohn Arbuckle    /* Where to look for local files */
968f4747900SJohn Arbuckle    NSString *path_array[] = {@"../share/doc/qemu/", @"../doc/qemu/", @"../"};
969f4747900SJohn Arbuckle    NSString *full_file_path;
970f4747900SJohn Arbuckle
971f4747900SJohn Arbuckle    /* iterate thru the possible paths until the file is found */
972f4747900SJohn Arbuckle    int index;
973f4747900SJohn Arbuckle    for (index = 0; index < ARRAY_SIZE(path_array); index++) {
974f4747900SJohn Arbuckle        full_file_path = [[NSBundle mainBundle] executablePath];
975f4747900SJohn Arbuckle        full_file_path = [full_file_path stringByDeletingLastPathComponent];
976f4747900SJohn Arbuckle        full_file_path = [NSString stringWithFormat: @"%@/%@%@", full_file_path,
977f4747900SJohn Arbuckle                          path_array[index], filename];
978f4747900SJohn Arbuckle        if ([[NSWorkspace sharedWorkspace] openFile: full_file_path] == YES) {
979f4747900SJohn Arbuckle            return;
980f4747900SJohn Arbuckle        }
981f4747900SJohn Arbuckle    }
982f4747900SJohn Arbuckle
983f4747900SJohn Arbuckle    /* If none of the paths opened a file */
984f4747900SJohn Arbuckle    NSBeep();
985f4747900SJohn Arbuckle    QEMU_Alert(@"Failed to open file");
986f4747900SJohn Arbuckle}
987f4747900SJohn Arbuckle
9883e230dd2SCorentin Chary- (void)showQEMUDoc:(id)sender
9893e230dd2SCorentin Chary{
9903e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
9913e230dd2SCorentin Chary
992f4747900SJohn Arbuckle    [self openDocumentation: @"qemu-doc.html"];
9933e230dd2SCorentin Chary}
9943e230dd2SCorentin Chary
9953e230dd2SCorentin Chary- (void)showQEMUTec:(id)sender
9963e230dd2SCorentin Chary{
9973e230dd2SCorentin Chary    COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
9983e230dd2SCorentin Chary
999f4747900SJohn Arbuckle    [self openDocumentation: @"qemu-tech.html"];
10003e230dd2SCorentin Chary}
10015d1b2eefSProgrammingkid
10025d1b2eefSProgrammingkid/* Stretches video to fit host monitor size */
10035d1b2eefSProgrammingkid- (void)zoomToFit:(id) sender
10045d1b2eefSProgrammingkid{
10055d1b2eefSProgrammingkid    stretch_video = !stretch_video;
10065d1b2eefSProgrammingkid    if (stretch_video == true) {
10075d1b2eefSProgrammingkid        [sender setState: NSOnState];
10085d1b2eefSProgrammingkid    } else {
10095d1b2eefSProgrammingkid        [sender setState: NSOffState];
10105d1b2eefSProgrammingkid    }
10115d1b2eefSProgrammingkid}
10123e230dd2SCorentin Chary
1013b4c6a112SProgrammingkid/* Displays the console on the screen */
1014b4c6a112SProgrammingkid- (void)displayConsole:(id)sender
1015b4c6a112SProgrammingkid{
1016b4c6a112SProgrammingkid    console_select([sender tag]);
1017b4c6a112SProgrammingkid}
10188524f1c7SJohn Arbuckle
10198524f1c7SJohn Arbuckle/* Pause the guest */
10208524f1c7SJohn Arbuckle- (void)pauseQEMU:(id)sender
10218524f1c7SJohn Arbuckle{
10228524f1c7SJohn Arbuckle    qmp_stop(NULL);
10238524f1c7SJohn Arbuckle    [sender setEnabled: NO];
10248524f1c7SJohn Arbuckle    [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES];
10258524f1c7SJohn Arbuckle    [self displayPause];
10268524f1c7SJohn Arbuckle}
10278524f1c7SJohn Arbuckle
10288524f1c7SJohn Arbuckle/* Resume running the guest operating system */
10298524f1c7SJohn Arbuckle- (void)resumeQEMU:(id) sender
10308524f1c7SJohn Arbuckle{
10318524f1c7SJohn Arbuckle    qmp_cont(NULL);
10328524f1c7SJohn Arbuckle    [sender setEnabled: NO];
10338524f1c7SJohn Arbuckle    [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES];
10348524f1c7SJohn Arbuckle    [self removePause];
10358524f1c7SJohn Arbuckle}
10368524f1c7SJohn Arbuckle
10378524f1c7SJohn Arbuckle/* Displays the word pause on the screen */
10388524f1c7SJohn Arbuckle- (void)displayPause
10398524f1c7SJohn Arbuckle{
10408524f1c7SJohn Arbuckle    /* Coordinates have to be calculated each time because the window can change its size */
10418524f1c7SJohn Arbuckle    int xCoord, yCoord, width, height;
10428524f1c7SJohn Arbuckle    xCoord = ([normalWindow frame].size.width - [pauseLabel frame].size.width)/2;
10438524f1c7SJohn Arbuckle    yCoord = [normalWindow frame].size.height - [pauseLabel frame].size.height - ([pauseLabel frame].size.height * .5);
10448524f1c7SJohn Arbuckle    width = [pauseLabel frame].size.width;
10458524f1c7SJohn Arbuckle    height = [pauseLabel frame].size.height;
10468524f1c7SJohn Arbuckle    [pauseLabel setFrame: NSMakeRect(xCoord, yCoord, width, height)];
10478524f1c7SJohn Arbuckle    [cocoaView addSubview: pauseLabel];
10488524f1c7SJohn Arbuckle}
10498524f1c7SJohn Arbuckle
10508524f1c7SJohn Arbuckle/* Removes the word pause from the screen */
10518524f1c7SJohn Arbuckle- (void)removePause
10528524f1c7SJohn Arbuckle{
10538524f1c7SJohn Arbuckle    [pauseLabel removeFromSuperview];
10548524f1c7SJohn Arbuckle}
10558524f1c7SJohn Arbuckle
105627074614SJohn Arbuckle/* Restarts QEMU */
105727074614SJohn Arbuckle- (void)restartQEMU:(id)sender
105827074614SJohn Arbuckle{
105927074614SJohn Arbuckle    qmp_system_reset(NULL);
106027074614SJohn Arbuckle}
106127074614SJohn Arbuckle
106227074614SJohn Arbuckle/* Powers down QEMU */
106327074614SJohn Arbuckle- (void)powerDownQEMU:(id)sender
106427074614SJohn Arbuckle{
106527074614SJohn Arbuckle    qmp_system_powerdown(NULL);
106627074614SJohn Arbuckle}
106727074614SJohn Arbuckle
1068693a3e01SJohn Arbuckle/* Ejects the media.
1069693a3e01SJohn Arbuckle * Uses sender's tag to figure out the device to eject.
1070693a3e01SJohn Arbuckle */
1071693a3e01SJohn Arbuckle- (void)ejectDeviceMedia:(id)sender
1072693a3e01SJohn Arbuckle{
1073693a3e01SJohn Arbuckle    NSString * drive;
1074693a3e01SJohn Arbuckle    drive = [sender representedObject];
1075693a3e01SJohn Arbuckle    if(drive == nil) {
1076693a3e01SJohn Arbuckle        NSBeep();
1077693a3e01SJohn Arbuckle        QEMU_Alert(@"Failed to find drive to eject!");
1078693a3e01SJohn Arbuckle        return;
1079693a3e01SJohn Arbuckle    }
1080693a3e01SJohn Arbuckle
1081693a3e01SJohn Arbuckle    Error *err = NULL;
1082693a3e01SJohn Arbuckle    qmp_eject([drive cStringUsingEncoding: NSASCIIStringEncoding], false, false, &err);
1083693a3e01SJohn Arbuckle    handleAnyDeviceErrors(err);
1084693a3e01SJohn Arbuckle}
1085693a3e01SJohn Arbuckle
1086693a3e01SJohn Arbuckle/* Displays a dialog box asking the user to select an image file to load.
1087693a3e01SJohn Arbuckle * Uses sender's represented object value to figure out which drive to use.
1088693a3e01SJohn Arbuckle */
1089693a3e01SJohn Arbuckle- (void)changeDeviceMedia:(id)sender
1090693a3e01SJohn Arbuckle{
1091693a3e01SJohn Arbuckle    /* Find the drive name */
1092693a3e01SJohn Arbuckle    NSString * drive;
1093693a3e01SJohn Arbuckle    drive = [sender representedObject];
1094693a3e01SJohn Arbuckle    if(drive == nil) {
1095693a3e01SJohn Arbuckle        NSBeep();
1096693a3e01SJohn Arbuckle        QEMU_Alert(@"Could not find drive!");
1097693a3e01SJohn Arbuckle        return;
1098693a3e01SJohn Arbuckle    }
1099693a3e01SJohn Arbuckle
1100693a3e01SJohn Arbuckle    /* Display the file open dialog */
1101693a3e01SJohn Arbuckle    NSOpenPanel * openPanel;
1102693a3e01SJohn Arbuckle    openPanel = [NSOpenPanel openPanel];
1103693a3e01SJohn Arbuckle    [openPanel setCanChooseFiles: YES];
1104693a3e01SJohn Arbuckle    [openPanel setAllowsMultipleSelection: NO];
1105693a3e01SJohn Arbuckle    [openPanel setAllowedFileTypes: supportedImageFileTypes];
1106693a3e01SJohn Arbuckle    if([openPanel runModal] == NSFileHandlingPanelOKButton) {
1107693a3e01SJohn Arbuckle        NSString * file = [[[openPanel URLs] objectAtIndex: 0] path];
1108693a3e01SJohn Arbuckle        if(file == nil) {
1109693a3e01SJohn Arbuckle            NSBeep();
1110693a3e01SJohn Arbuckle            QEMU_Alert(@"Failed to convert URL to file path!");
1111693a3e01SJohn Arbuckle            return;
1112693a3e01SJohn Arbuckle        }
1113693a3e01SJohn Arbuckle
1114693a3e01SJohn Arbuckle        Error *err = NULL;
111524fb4133SMax Reitz        qmp_blockdev_change_medium([drive cStringUsingEncoding:
111624fb4133SMax Reitz                                          NSASCIIStringEncoding],
111724fb4133SMax Reitz                                   [file cStringUsingEncoding:
111824fb4133SMax Reitz                                         NSASCIIStringEncoding],
111924fb4133SMax Reitz                                   true, "raw",
112039ff43d9SMax Reitz                                   false, 0,
1121693a3e01SJohn Arbuckle                                   &err);
1122693a3e01SJohn Arbuckle        handleAnyDeviceErrors(err);
1123693a3e01SJohn Arbuckle    }
1124693a3e01SJohn Arbuckle}
1125693a3e01SJohn Arbuckle
1126d9bc14f6SJohn Arbuckle/* Verifies if the user really wants to quit */
1127d9bc14f6SJohn Arbuckle- (BOOL)verifyQuit
1128d9bc14f6SJohn Arbuckle{
1129d9bc14f6SJohn Arbuckle    NSAlert *alert = [NSAlert new];
1130d9bc14f6SJohn Arbuckle    [alert autorelease];
1131d9bc14f6SJohn Arbuckle    [alert setMessageText: @"Are you sure you want to quit QEMU?"];
1132d9bc14f6SJohn Arbuckle    [alert addButtonWithTitle: @"Cancel"];
1133d9bc14f6SJohn Arbuckle    [alert addButtonWithTitle: @"Quit"];
1134d9bc14f6SJohn Arbuckle    if([alert runModal] == NSAlertSecondButtonReturn) {
1135d9bc14f6SJohn Arbuckle        return YES;
1136d9bc14f6SJohn Arbuckle    } else {
1137d9bc14f6SJohn Arbuckle        return NO;
1138d9bc14f6SJohn Arbuckle    }
1139d9bc14f6SJohn Arbuckle}
1140d9bc14f6SJohn Arbuckle
1141b4c6a112SProgrammingkid@end
11423e230dd2SCorentin Chary
11433e230dd2SCorentin Chary
11443e230dd2SCorentin Charyint main (int argc, const char * argv[]) {
11453e230dd2SCorentin Chary
11463e230dd2SCorentin Chary    gArgc = argc;
11473e230dd2SCorentin Chary    gArgv = (char **)argv;
11483e230dd2SCorentin Chary    int i;
11493e230dd2SCorentin Chary
11503e230dd2SCorentin Chary    /* In case we don't need to display a window, let's not do that */
11513e230dd2SCorentin Chary    for (i = 1; i < argc; i++) {
1152e4ebcc1aSTristan Gingold        const char *opt = argv[i];
1153e4ebcc1aSTristan Gingold
1154e4ebcc1aSTristan Gingold        if (opt[0] == '-') {
1155e4ebcc1aSTristan Gingold            /* Treat --foo the same as -foo.  */
1156e4ebcc1aSTristan Gingold            if (opt[1] == '-') {
1157e4ebcc1aSTristan Gingold                opt++;
1158e4ebcc1aSTristan Gingold            }
11599851484fSAlexandre Raymond            if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
11609851484fSAlexandre Raymond                !strcmp(opt, "-vnc") ||
1161e4ebcc1aSTristan Gingold                !strcmp(opt, "-nographic") ||
1162e4ebcc1aSTristan Gingold                !strcmp(opt, "-version") ||
116360b46aa2SAndreas Färber                !strcmp(opt, "-curses") ||
1164b12a84ceSRainer Müller                !strcmp(opt, "-display") ||
116560b46aa2SAndreas Färber                !strcmp(opt, "-qtest")) {
11663bbbee18SAndreas Färber                return qemu_main(gArgc, gArgv, *_NSGetEnviron());
11673e230dd2SCorentin Chary            }
11683e230dd2SCorentin Chary        }
1169e4ebcc1aSTristan Gingold    }
11703e230dd2SCorentin Chary
11713e230dd2SCorentin Chary    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
11723e230dd2SCorentin Chary
117342a5dfe7SPeter Maydell    // Pull this console process up to being a fully-fledged graphical
117442a5dfe7SPeter Maydell    // app with a menubar and Dock icon
117542a5dfe7SPeter Maydell    ProcessSerialNumber psn = { 0, kCurrentProcess };
117642a5dfe7SPeter Maydell    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
117742a5dfe7SPeter Maydell
11783e230dd2SCorentin Chary    [NSApplication sharedApplication];
11793e230dd2SCorentin Chary
11803e230dd2SCorentin Chary    // Add menus
11813e230dd2SCorentin Chary    NSMenu      *menu;
11823e230dd2SCorentin Chary    NSMenuItem  *menuItem;
11833e230dd2SCorentin Chary
11843e230dd2SCorentin Chary    [NSApp setMainMenu:[[NSMenu alloc] init]];
11853e230dd2SCorentin Chary
11863e230dd2SCorentin Chary    // Application menu
11873e230dd2SCorentin Chary    menu = [[NSMenu alloc] initWithTitle:@""];
11883e230dd2SCorentin Chary    [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
11893e230dd2SCorentin Chary    [menu addItem:[NSMenuItem separatorItem]]; //Separator
11903e230dd2SCorentin Chary    [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
11913e230dd2SCorentin Chary    menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
11923e230dd2SCorentin Chary    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
11933e230dd2SCorentin Chary    [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
11943e230dd2SCorentin Chary    [menu addItem:[NSMenuItem separatorItem]]; //Separator
11953e230dd2SCorentin Chary    [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
11963e230dd2SCorentin Chary    menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
11973e230dd2SCorentin Chary    [menuItem setSubmenu:menu];
11983e230dd2SCorentin Chary    [[NSApp mainMenu] addItem:menuItem];
11993e230dd2SCorentin Chary    [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
12003e230dd2SCorentin Chary
12018524f1c7SJohn Arbuckle    // Machine menu
12028524f1c7SJohn Arbuckle    menu = [[NSMenu alloc] initWithTitle: @"Machine"];
12038524f1c7SJohn Arbuckle    [menu setAutoenablesItems: NO];
12048524f1c7SJohn Arbuckle    [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Pause" action: @selector(pauseQEMU:) keyEquivalent: @""] autorelease]];
12058524f1c7SJohn Arbuckle    menuItem = [[[NSMenuItem alloc] initWithTitle: @"Resume" action: @selector(resumeQEMU:) keyEquivalent: @""] autorelease];
12068524f1c7SJohn Arbuckle    [menu addItem: menuItem];
12078524f1c7SJohn Arbuckle    [menuItem setEnabled: NO];
120827074614SJohn Arbuckle    [menu addItem: [NSMenuItem separatorItem]];
120927074614SJohn Arbuckle    [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Reset" action: @selector(restartQEMU:) keyEquivalent: @""] autorelease]];
121027074614SJohn Arbuckle    [menu addItem: [[[NSMenuItem alloc] initWithTitle: @"Power Down" action: @selector(powerDownQEMU:) keyEquivalent: @""] autorelease]];
12118524f1c7SJohn Arbuckle    menuItem = [[[NSMenuItem alloc] initWithTitle: @"Machine" action:nil keyEquivalent:@""] autorelease];
12128524f1c7SJohn Arbuckle    [menuItem setSubmenu:menu];
12138524f1c7SJohn Arbuckle    [[NSApp mainMenu] addItem:menuItem];
12148524f1c7SJohn Arbuckle
12153e230dd2SCorentin Chary    // View menu
12163e230dd2SCorentin Chary    menu = [[NSMenu alloc] initWithTitle:@"View"];
12175d1b2eefSProgrammingkid    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(doToggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
12185d1b2eefSProgrammingkid    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Zoom To Fit" action:@selector(zoomToFit:) keyEquivalent:@""] autorelease]];
12193e230dd2SCorentin Chary    menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
12203e230dd2SCorentin Chary    [menuItem setSubmenu:menu];
12213e230dd2SCorentin Chary    [[NSApp mainMenu] addItem:menuItem];
12223e230dd2SCorentin Chary
12233e230dd2SCorentin Chary    // Window menu
12243e230dd2SCorentin Chary    menu = [[NSMenu alloc] initWithTitle:@"Window"];
12253e230dd2SCorentin Chary    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
12263e230dd2SCorentin Chary    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
12273e230dd2SCorentin Chary    [menuItem setSubmenu:menu];
12283e230dd2SCorentin Chary    [[NSApp mainMenu] addItem:menuItem];
12293e230dd2SCorentin Chary    [NSApp setWindowsMenu:menu];
12303e230dd2SCorentin Chary
12313e230dd2SCorentin Chary    // Help menu
12323e230dd2SCorentin Chary    menu = [[NSMenu alloc] initWithTitle:@"Help"];
12333e230dd2SCorentin Chary    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
12343e230dd2SCorentin Chary    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
12353e230dd2SCorentin Chary    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
12363e230dd2SCorentin Chary    [menuItem setSubmenu:menu];
12373e230dd2SCorentin Chary    [[NSApp mainMenu] addItem:menuItem];
12383e230dd2SCorentin Chary
12393e230dd2SCorentin Chary    // Create an Application controller
12403e230dd2SCorentin Chary    QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
12413e230dd2SCorentin Chary    [NSApp setDelegate:appController];
12423e230dd2SCorentin Chary
12433e230dd2SCorentin Chary    // Start the main event loop
12443e230dd2SCorentin Chary    [NSApp run];
12453e230dd2SCorentin Chary
12463e230dd2SCorentin Chary    [appController release];
12473e230dd2SCorentin Chary    [pool release];
12483e230dd2SCorentin Chary
12493e230dd2SCorentin Chary    return 0;
12503e230dd2SCorentin Chary}
12513e230dd2SCorentin Chary
12523e230dd2SCorentin Chary
12533e230dd2SCorentin Chary
12543e230dd2SCorentin Chary#pragma mark qemu
12557c20b4a3SGerd Hoffmannstatic void cocoa_update(DisplayChangeListener *dcl,
12567c20b4a3SGerd Hoffmann                         int x, int y, int w, int h)
12573e230dd2SCorentin Chary{
12586e657e64SPeter Maydell    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
12596e657e64SPeter Maydell
12603e230dd2SCorentin Chary    COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
12613e230dd2SCorentin Chary
12623e230dd2SCorentin Chary    NSRect rect;
12633e230dd2SCorentin Chary    if ([cocoaView cdx] == 1.0) {
12643e230dd2SCorentin Chary        rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
12653e230dd2SCorentin Chary    } else {
12663e230dd2SCorentin Chary        rect = NSMakeRect(
12673e230dd2SCorentin Chary            x * [cocoaView cdx],
12683e230dd2SCorentin Chary            ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
12693e230dd2SCorentin Chary            w * [cocoaView cdx],
12703e230dd2SCorentin Chary            h * [cocoaView cdy]);
12713e230dd2SCorentin Chary    }
12723e230dd2SCorentin Chary    [cocoaView setNeedsDisplayInRect:rect];
12736e657e64SPeter Maydell
12746e657e64SPeter Maydell    [pool release];
12753e230dd2SCorentin Chary}
12763e230dd2SCorentin Chary
1277c12aeb86SGerd Hoffmannstatic void cocoa_switch(DisplayChangeListener *dcl,
1278c12aeb86SGerd Hoffmann                         DisplaySurface *surface)
12793e230dd2SCorentin Chary{
12806e657e64SPeter Maydell    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
12813e230dd2SCorentin Chary
12826e657e64SPeter Maydell    COCOA_DEBUG("qemu_cocoa: cocoa_switch\n");
12835e00d3acSGerd Hoffmann    [cocoaView switchSurface:surface];
12846e657e64SPeter Maydell    [pool release];
12853e230dd2SCorentin Chary}
12863e230dd2SCorentin Chary
1287bc2ed970SGerd Hoffmannstatic void cocoa_refresh(DisplayChangeListener *dcl)
12883e230dd2SCorentin Chary{
12896e657e64SPeter Maydell    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
12906e657e64SPeter Maydell
12913e230dd2SCorentin Chary    COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
1292468a895bSJohn Arbuckle    graphic_hw_update(NULL);
12933e230dd2SCorentin Chary
129421bae11aSGerd Hoffmann    if (qemu_input_is_absolute()) {
12953e230dd2SCorentin Chary        if (![cocoaView isAbsoluteEnabled]) {
129649b9bd4dSPeter Maydell            if ([cocoaView isMouseGrabbed]) {
12973e230dd2SCorentin Chary                [cocoaView ungrabMouse];
12983e230dd2SCorentin Chary            }
12993e230dd2SCorentin Chary        }
13003e230dd2SCorentin Chary        [cocoaView setAbsoluteEnabled:YES];
13013e230dd2SCorentin Chary    }
13023e230dd2SCorentin Chary
13033e230dd2SCorentin Chary    NSDate *distantPast;
13043e230dd2SCorentin Chary    NSEvent *event;
13053e230dd2SCorentin Chary    distantPast = [NSDate distantPast];
13063e230dd2SCorentin Chary    do {
13073e230dd2SCorentin Chary        event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
13083e230dd2SCorentin Chary                        inMode: NSDefaultRunLoopMode dequeue:YES];
13093e230dd2SCorentin Chary        if (event != nil) {
13103e230dd2SCorentin Chary            [cocoaView handleEvent:event];
13113e230dd2SCorentin Chary        }
13123e230dd2SCorentin Chary    } while(event != nil);
13136e657e64SPeter Maydell    [pool release];
13143e230dd2SCorentin Chary}
13153e230dd2SCorentin Chary
13163e230dd2SCorentin Charystatic void cocoa_cleanup(void)
13173e230dd2SCorentin Chary{
13183e230dd2SCorentin Chary    COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
131958a06675SBlue Swirl    g_free(dcl);
13203e230dd2SCorentin Chary}
13213e230dd2SCorentin Chary
13227c20b4a3SGerd Hoffmannstatic const DisplayChangeListenerOps dcl_ops = {
13237c20b4a3SGerd Hoffmann    .dpy_name          = "cocoa",
13248510d91eSPeter Maydell    .dpy_gfx_update = cocoa_update,
13258510d91eSPeter Maydell    .dpy_gfx_switch = cocoa_switch,
13268510d91eSPeter Maydell    .dpy_refresh = cocoa_refresh,
13277c20b4a3SGerd Hoffmann};
13287c20b4a3SGerd Hoffmann
1329b4c6a112SProgrammingkid/* Returns a name for a given console */
1330b4c6a112SProgrammingkidstatic NSString * getConsoleName(QemuConsole * console)
1331b4c6a112SProgrammingkid{
1332b4c6a112SProgrammingkid    return [NSString stringWithFormat: @"%s", qemu_console_get_label(console)];
1333b4c6a112SProgrammingkid}
1334b4c6a112SProgrammingkid
1335b4c6a112SProgrammingkid/* Add an entry to the View menu for each console */
1336b4c6a112SProgrammingkidstatic void add_console_menu_entries(void)
1337b4c6a112SProgrammingkid{
1338b4c6a112SProgrammingkid    NSMenu *menu;
1339b4c6a112SProgrammingkid    NSMenuItem *menuItem;
1340b4c6a112SProgrammingkid    int index = 0;
1341b4c6a112SProgrammingkid
1342b4c6a112SProgrammingkid    menu = [[[NSApp mainMenu] itemWithTitle:@"View"] submenu];
1343b4c6a112SProgrammingkid
1344b4c6a112SProgrammingkid    [menu addItem:[NSMenuItem separatorItem]];
1345b4c6a112SProgrammingkid
1346b4c6a112SProgrammingkid    while (qemu_console_lookup_by_index(index) != NULL) {
1347b4c6a112SProgrammingkid        menuItem = [[[NSMenuItem alloc] initWithTitle: getConsoleName(qemu_console_lookup_by_index(index))
1348b4c6a112SProgrammingkid                                               action: @selector(displayConsole:) keyEquivalent: @""] autorelease];
1349b4c6a112SProgrammingkid        [menuItem setTag: index];
1350b4c6a112SProgrammingkid        [menu addItem: menuItem];
1351b4c6a112SProgrammingkid        index++;
1352b4c6a112SProgrammingkid    }
1353b4c6a112SProgrammingkid}
1354b4c6a112SProgrammingkid
1355693a3e01SJohn Arbuckle/* Make menu items for all removable devices.
1356693a3e01SJohn Arbuckle * Each device is given an 'Eject' and 'Change' menu item.
1357693a3e01SJohn Arbuckle */
1358a7940ec0SJohn Arbucklestatic void addRemovableDevicesMenuItems(void)
1359693a3e01SJohn Arbuckle{
1360693a3e01SJohn Arbuckle    NSMenu *menu;
1361693a3e01SJohn Arbuckle    NSMenuItem *menuItem;
1362693a3e01SJohn Arbuckle    BlockInfoList *currentDevice, *pointerToFree;
1363693a3e01SJohn Arbuckle    NSString *deviceName;
1364693a3e01SJohn Arbuckle
1365693a3e01SJohn Arbuckle    currentDevice = qmp_query_block(NULL);
1366693a3e01SJohn Arbuckle    pointerToFree = currentDevice;
1367693a3e01SJohn Arbuckle    if(currentDevice == NULL) {
1368693a3e01SJohn Arbuckle        NSBeep();
1369693a3e01SJohn Arbuckle        QEMU_Alert(@"Failed to query for block devices!");
1370693a3e01SJohn Arbuckle        return;
1371693a3e01SJohn Arbuckle    }
1372693a3e01SJohn Arbuckle
1373693a3e01SJohn Arbuckle    menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
1374693a3e01SJohn Arbuckle
1375693a3e01SJohn Arbuckle    // Add a separator between related groups of menu items
1376693a3e01SJohn Arbuckle    [menu addItem:[NSMenuItem separatorItem]];
1377693a3e01SJohn Arbuckle
1378693a3e01SJohn Arbuckle    // Set the attributes to the "Removable Media" menu item
1379693a3e01SJohn Arbuckle    NSString *titleString = @"Removable Media";
1380693a3e01SJohn Arbuckle    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:titleString];
1381693a3e01SJohn Arbuckle    NSColor *newColor = [NSColor blackColor];
1382693a3e01SJohn Arbuckle    NSFontManager *fontManager = [NSFontManager sharedFontManager];
1383693a3e01SJohn Arbuckle    NSFont *font = [fontManager fontWithFamily:@"Helvetica"
1384693a3e01SJohn Arbuckle                                          traits:NSBoldFontMask|NSItalicFontMask
1385693a3e01SJohn Arbuckle                                          weight:0
1386693a3e01SJohn Arbuckle                                            size:14];
1387693a3e01SJohn Arbuckle    [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [titleString length])];
1388693a3e01SJohn Arbuckle    [attString addAttribute:NSForegroundColorAttributeName value:newColor range:NSMakeRange(0, [titleString length])];
1389693a3e01SJohn Arbuckle    [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt: 1] range:NSMakeRange(0, [titleString length])];
1390693a3e01SJohn Arbuckle
1391693a3e01SJohn Arbuckle    // Add the "Removable Media" menu item
1392693a3e01SJohn Arbuckle    menuItem = [NSMenuItem new];
1393693a3e01SJohn Arbuckle    [menuItem setAttributedTitle: attString];
1394693a3e01SJohn Arbuckle    [menuItem setEnabled: NO];
1395693a3e01SJohn Arbuckle    [menu addItem: menuItem];
1396693a3e01SJohn Arbuckle
1397693a3e01SJohn Arbuckle    /* Loop thru all the block devices in the emulator */
1398693a3e01SJohn Arbuckle    while (currentDevice) {
1399693a3e01SJohn Arbuckle        deviceName = [[NSString stringWithFormat: @"%s", currentDevice->value->device] retain];
1400693a3e01SJohn Arbuckle
1401693a3e01SJohn Arbuckle        if(currentDevice->value->removable) {
1402693a3e01SJohn Arbuckle            menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Change %s...", currentDevice->value->device]
1403693a3e01SJohn Arbuckle                                                  action: @selector(changeDeviceMedia:)
1404693a3e01SJohn Arbuckle                                           keyEquivalent: @""];
1405693a3e01SJohn Arbuckle            [menu addItem: menuItem];
1406693a3e01SJohn Arbuckle            [menuItem setRepresentedObject: deviceName];
1407693a3e01SJohn Arbuckle            [menuItem autorelease];
1408693a3e01SJohn Arbuckle
1409693a3e01SJohn Arbuckle            menuItem = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"Eject %s", currentDevice->value->device]
1410693a3e01SJohn Arbuckle                                                  action: @selector(ejectDeviceMedia:)
1411693a3e01SJohn Arbuckle                                           keyEquivalent: @""];
1412693a3e01SJohn Arbuckle            [menu addItem: menuItem];
1413693a3e01SJohn Arbuckle            [menuItem setRepresentedObject: deviceName];
1414693a3e01SJohn Arbuckle            [menuItem autorelease];
1415693a3e01SJohn Arbuckle        }
1416693a3e01SJohn Arbuckle        currentDevice = currentDevice->next;
1417693a3e01SJohn Arbuckle    }
1418693a3e01SJohn Arbuckle    qapi_free_BlockInfoList(pointerToFree);
1419693a3e01SJohn Arbuckle}
1420693a3e01SJohn Arbuckle
14213e230dd2SCorentin Charyvoid cocoa_display_init(DisplayState *ds, int full_screen)
14223e230dd2SCorentin Chary{
14233e230dd2SCorentin Chary    COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
14243e230dd2SCorentin Chary
142543227af8SProgrammingkid    /* if fullscreen mode is to be used */
142643227af8SProgrammingkid    if (full_screen == true) {
142743227af8SProgrammingkid        [NSApp activateIgnoringOtherApps: YES];
142843227af8SProgrammingkid        [(QemuCocoaAppController *)[[NSApplication sharedApplication] delegate] toggleFullScreen: nil];
142943227af8SProgrammingkid    }
143043227af8SProgrammingkid
143158a06675SBlue Swirl    dcl = g_malloc0(sizeof(DisplayChangeListener));
14323e230dd2SCorentin Chary
14333e230dd2SCorentin Chary    // register vga output callbacks
14347c20b4a3SGerd Hoffmann    dcl->ops = &dcl_ops;
14355209089fSGerd Hoffmann    register_displaychangelistener(dcl);
14363e230dd2SCorentin Chary
14373e230dd2SCorentin Chary    // register cleanup function
14383e230dd2SCorentin Chary    atexit(cocoa_cleanup);
1439b4c6a112SProgrammingkid
1440b4c6a112SProgrammingkid    /* At this point QEMU has created all the consoles, so we can add View
1441b4c6a112SProgrammingkid     * menu entries for them.
1442b4c6a112SProgrammingkid     */
1443b4c6a112SProgrammingkid    add_console_menu_entries();
1444693a3e01SJohn Arbuckle
1445693a3e01SJohn Arbuckle    /* Give all removable devices a menu item.
1446693a3e01SJohn Arbuckle     * Has to be called after QEMU has started to
1447693a3e01SJohn Arbuckle     * find out what removable devices it has.
1448693a3e01SJohn Arbuckle     */
1449693a3e01SJohn Arbuckle    addRemovableDevicesMenuItems();
14503e230dd2SCorentin Chary}
1451