Statistics
| Branch: | Revision:

root / ui / cocoa.m @ 7fee199c

History | View | Annotate | Download (37.4 kB)

1 5b0753e0 bellard
/*
2 c304f7e2 ths
 * QEMU Cocoa CG display driver
3 5fafdf24 ths
 *
4 c304f7e2 ths
 * Copyright (c) 2008 Mike Kronenberg
5 5fafdf24 ths
 *
6 5b0753e0 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 5b0753e0 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 5b0753e0 bellard
 * in the Software without restriction, including without limitation the rights
9 5b0753e0 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 5b0753e0 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 5b0753e0 bellard
 * furnished to do so, subject to the following conditions:
12 5b0753e0 bellard
 *
13 5b0753e0 bellard
 * The above copyright notice and this permission notice shall be included in
14 5b0753e0 bellard
 * all copies or substantial portions of the Software.
15 5b0753e0 bellard
 *
16 5b0753e0 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 5b0753e0 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 5b0753e0 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 5b0753e0 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 5b0753e0 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 5b0753e0 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 5b0753e0 bellard
 * THE SOFTWARE.
23 5b0753e0 bellard
 */
24 da4dbf74 bellard
25 5b0753e0 bellard
#import <Cocoa/Cocoa.h>
26 3bbbee18 Andreas Färber
#include <crt_externs.h>
27 5b0753e0 bellard
28 87ecb68b pbrook
#include "qemu-common.h"
29 87ecb68b pbrook
#include "console.h"
30 87ecb68b pbrook
#include "sysemu.h"
31 da4dbf74 bellard
32 38ec7b53 Andreas Färber
#ifndef MAC_OS_X_VERSION_10_4
33 38ec7b53 Andreas Färber
#define MAC_OS_X_VERSION_10_4 1040
34 38ec7b53 Andreas Färber
#endif
35 44e4c0ba Andreas Färber
#ifndef MAC_OS_X_VERSION_10_5
36 44e4c0ba Andreas Färber
#define MAC_OS_X_VERSION_10_5 1050
37 44e4c0ba Andreas Färber
#endif
38 44e4c0ba Andreas Färber
39 3b46e624 ths
40 c304f7e2 ths
//#define DEBUG
41 3b46e624 ths
42 c304f7e2 ths
#ifdef DEBUG
43 c304f7e2 ths
#define COCOA_DEBUG(...)  { (void) fprintf (stdout, __VA_ARGS__); }
44 b29169d2 blueswir1
#else
45 c304f7e2 ths
#define COCOA_DEBUG(...)  ((void) 0)
46 b29169d2 blueswir1
#endif
47 b29169d2 blueswir1
48 c304f7e2 ths
#define cgrect(nsrect) (*(CGRect *)&(nsrect))
49 c304f7e2 ths
#define COCOA_MOUSE_EVENT \
50 c304f7e2 ths
        if (isTabletEnabled) { \
51 b94ed577 aurel32
            kbd_mouse_event((int)(p.x * 0x7FFF / (screen.width - 1)), (int)((screen.height - p.y) * 0x7FFF / (screen.height - 1)), 0, buttons); \
52 c304f7e2 ths
        } else if (isMouseGrabed) { \
53 c304f7e2 ths
            kbd_mouse_event((int)[event deltaX], (int)[event deltaY], 0, buttons); \
54 c304f7e2 ths
        } else { \
55 c304f7e2 ths
            [NSApp sendEvent:event]; \
56 c304f7e2 ths
        }
57 5b0753e0 bellard
58 c304f7e2 ths
typedef struct {
59 c304f7e2 ths
    int width;
60 c304f7e2 ths
    int height;
61 c304f7e2 ths
    int bitsPerComponent;
62 c304f7e2 ths
    int bitsPerPixel;
63 c304f7e2 ths
} QEMUScreen;
64 c304f7e2 ths
65 c304f7e2 ths
NSWindow *normalWindow;
66 9794f74f aliguori
static DisplayChangeListener *dcl;
67 c304f7e2 ths
68 c304f7e2 ths
int gArgc;
69 c304f7e2 ths
char **gArgv;
70 5b0753e0 bellard
71 c304f7e2 ths
// keymap conversion
72 87f48e6a bellard
int keymap[] =
73 5b0753e0 bellard
{
74 87f48e6a bellard
//  SdlI    macI    macH    SdlH    104xtH  104xtC  sdl
75 87f48e6a bellard
    30, //  0       0x00    0x1e            A       QZ_a
76 87f48e6a bellard
    31, //  1       0x01    0x1f            S       QZ_s
77 87f48e6a bellard
    32, //  2       0x02    0x20            D       QZ_d
78 87f48e6a bellard
    33, //  3       0x03    0x21            F       QZ_f
79 87f48e6a bellard
    35, //  4       0x04    0x23            H       QZ_h
80 87f48e6a bellard
    34, //  5       0x05    0x22            G       QZ_g
81 87f48e6a bellard
    44, //  6       0x06    0x2c            Z       QZ_z
82 87f48e6a bellard
    45, //  7       0x07    0x2d            X       QZ_x
83 87f48e6a bellard
    46, //  8       0x08    0x2e            C       QZ_c
84 87f48e6a bellard
    47, //  9       0x09    0x2f            V       QZ_v
85 87f48e6a bellard
    0,  //  10      0x0A    Undefined
86 87f48e6a bellard
    48, //  11      0x0B    0x30            B       QZ_b
87 87f48e6a bellard
    16, //  12      0x0C    0x10            Q       QZ_q
88 87f48e6a bellard
    17, //  13      0x0D    0x11            W       QZ_w
89 87f48e6a bellard
    18, //  14      0x0E    0x12            E       QZ_e
90 87f48e6a bellard
    19, //  15      0x0F    0x13            R       QZ_r
91 87f48e6a bellard
    21, //  16      0x10    0x15            Y       QZ_y
92 87f48e6a bellard
    20, //  17      0x11    0x14            T       QZ_t
93 87f48e6a bellard
    2,  //  18      0x12    0x02            1       QZ_1
94 87f48e6a bellard
    3,  //  19      0x13    0x03            2       QZ_2
95 87f48e6a bellard
    4,  //  20      0x14    0x04            3       QZ_3
96 87f48e6a bellard
    5,  //  21      0x15    0x05            4       QZ_4
97 87f48e6a bellard
    7,  //  22      0x16    0x07            6       QZ_6
98 87f48e6a bellard
    6,  //  23      0x17    0x06            5       QZ_5
99 87f48e6a bellard
    13, //  24      0x18    0x0d            =       QZ_EQUALS
100 87f48e6a bellard
    10, //  25      0x19    0x0a            9       QZ_9
101 87f48e6a bellard
    8,  //  26      0x1A    0x08            7       QZ_7
102 87f48e6a bellard
    12, //  27      0x1B    0x0c            -       QZ_MINUS
103 87f48e6a bellard
    9,  //  28      0x1C    0x09            8       QZ_8
104 87f48e6a bellard
    11, //  29      0x1D    0x0b            0       QZ_0
105 87f48e6a bellard
    27, //  30      0x1E    0x1b            ]       QZ_RIGHTBRACKET
106 87f48e6a bellard
    24, //  31      0x1F    0x18            O       QZ_o
107 87f48e6a bellard
    22, //  32      0x20    0x16            U       QZ_u
108 87f48e6a bellard
    26, //  33      0x21    0x1a            [       QZ_LEFTBRACKET
109 87f48e6a bellard
    23, //  34      0x22    0x17            I       QZ_i
110 87f48e6a bellard
    25, //  35      0x23    0x19            P       QZ_p
111 87f48e6a bellard
    28, //  36      0x24    0x1c            ENTER   QZ_RETURN
112 87f48e6a bellard
    38, //  37      0x25    0x26            L       QZ_l
113 87f48e6a bellard
    36, //  38      0x26    0x24            J       QZ_j
114 87f48e6a bellard
    40, //  39      0x27    0x28            '       QZ_QUOTE
115 87f48e6a bellard
    37, //  40      0x28    0x25            K       QZ_k
116 87f48e6a bellard
    39, //  41      0x29    0x27            ;       QZ_SEMICOLON
117 87f48e6a bellard
    43, //  42      0x2A    0x2b            \       QZ_BACKSLASH
118 87f48e6a bellard
    51, //  43      0x2B    0x33            ,       QZ_COMMA
119 87f48e6a bellard
    53, //  44      0x2C    0x35            /       QZ_SLASH
120 87f48e6a bellard
    49, //  45      0x2D    0x31            N       QZ_n
121 87f48e6a bellard
    50, //  46      0x2E    0x32            M       QZ_m
122 87f48e6a bellard
    52, //  47      0x2F    0x34            .       QZ_PERIOD
123 87f48e6a bellard
    15, //  48      0x30    0x0f            TAB     QZ_TAB
124 87f48e6a bellard
    57, //  49      0x31    0x39            SPACE   QZ_SPACE
125 87f48e6a bellard
    41, //  50      0x32    0x29            `       QZ_BACKQUOTE
126 87f48e6a bellard
    14, //  51      0x33    0x0e            BKSP    QZ_BACKSPACE
127 87f48e6a bellard
    0,  //  52      0x34    Undefined
128 87f48e6a bellard
    1,  //  53      0x35    0x01            ESC     QZ_ESCAPE
129 87f48e6a bellard
    0,  //  54      0x36                            QZ_RMETA
130 87f48e6a bellard
    0,  //  55      0x37                            QZ_LMETA
131 87f48e6a bellard
    42, //  56      0x38    0x2a            L SHFT  QZ_LSHIFT
132 87f48e6a bellard
    58, //  57      0x39    0x3a            CAPS    QZ_CAPSLOCK
133 87f48e6a bellard
    56, //  58      0x3A    0x38            L ALT   QZ_LALT
134 87f48e6a bellard
    29, //  59      0x3B    0x1d            L CTRL  QZ_LCTRL
135 87f48e6a bellard
    54, //  60      0x3C    0x36            R SHFT  QZ_RSHIFT
136 87f48e6a bellard
    184,//  61      0x3D    0xb8    E0,38   R ALT   QZ_RALT
137 87f48e6a bellard
    157,//  62      0x3E    0x9d    E0,1D   R CTRL  QZ_RCTRL
138 87f48e6a bellard
    0,  //  63      0x3F    Undefined
139 87f48e6a bellard
    0,  //  64      0x40    Undefined
140 87f48e6a bellard
    0,  //  65      0x41    Undefined
141 87f48e6a bellard
    0,  //  66      0x42    Undefined
142 87f48e6a bellard
    55, //  67      0x43    0x37            KP *    QZ_KP_MULTIPLY
143 87f48e6a bellard
    0,  //  68      0x44    Undefined
144 87f48e6a bellard
    78, //  69      0x45    0x4e            KP +    QZ_KP_PLUS
145 87f48e6a bellard
    0,  //  70      0x46    Undefined
146 87f48e6a bellard
    69, //  71      0x47    0x45            NUM     QZ_NUMLOCK
147 87f48e6a bellard
    0,  //  72      0x48    Undefined
148 87f48e6a bellard
    0,  //  73      0x49    Undefined
149 87f48e6a bellard
    0,  //  74      0x4A    Undefined
150 87f48e6a bellard
    181,//  75      0x4B    0xb5    E0,35   KP /    QZ_KP_DIVIDE
151 87f48e6a bellard
    152,//  76      0x4C    0x9c    E0,1C   KP EN   QZ_KP_ENTER
152 87f48e6a bellard
    0,  //  77      0x4D    undefined
153 87f48e6a bellard
    74, //  78      0x4E    0x4a            KP -    QZ_KP_MINUS
154 87f48e6a bellard
    0,  //  79      0x4F    Undefined
155 87f48e6a bellard
    0,  //  80      0x50    Undefined
156 87f48e6a bellard
    0,  //  81      0x51                            QZ_KP_EQUALS
157 87f48e6a bellard
    82, //  82      0x52    0x52            KP 0    QZ_KP0
158 87f48e6a bellard
    79, //  83      0x53    0x4f            KP 1    QZ_KP1
159 87f48e6a bellard
    80, //  84      0x54    0x50            KP 2    QZ_KP2
160 87f48e6a bellard
    81, //  85      0x55    0x51            KP 3    QZ_KP3
161 87f48e6a bellard
    75, //  86      0x56    0x4b            KP 4    QZ_KP4
162 87f48e6a bellard
    76, //  87      0x57    0x4c            KP 5    QZ_KP5
163 87f48e6a bellard
    77, //  88      0x58    0x4d            KP 6    QZ_KP6
164 87f48e6a bellard
    71, //  89      0x59    0x47            KP 7    QZ_KP7
165 87f48e6a bellard
    0,  //  90      0x5A    Undefined
166 87f48e6a bellard
    72, //  91      0x5B    0x48            KP 8    QZ_KP8
167 87f48e6a bellard
    73, //  92      0x5C    0x49            KP 9    QZ_KP9
168 87f48e6a bellard
    0,  //  93      0x5D    Undefined
169 87f48e6a bellard
    0,  //  94      0x5E    Undefined
170 87f48e6a bellard
    0,  //  95      0x5F    Undefined
171 87f48e6a bellard
    63, //  96      0x60    0x3f            F5      QZ_F5
172 87f48e6a bellard
    64, //  97      0x61    0x40            F6      QZ_F6
173 87f48e6a bellard
    65, //  98      0x62    0x41            F7      QZ_F7
174 87f48e6a bellard
    61, //  99      0x63    0x3d            F3      QZ_F3
175 87f48e6a bellard
    66, //  100     0x64    0x42            F8      QZ_F8
176 87f48e6a bellard
    67, //  101     0x65    0x43            F9      QZ_F9
177 87f48e6a bellard
    0,  //  102     0x66    Undefined
178 87f48e6a bellard
    87, //  103     0x67    0x57            F11     QZ_F11
179 87f48e6a bellard
    0,  //  104     0x68    Undefined
180 c304f7e2 ths
    183,//  105     0x69    0xb7                    QZ_PRINT
181 87f48e6a bellard
    0,  //  106     0x6A    Undefined
182 87f48e6a bellard
    70, //  107     0x6B    0x46            SCROLL  QZ_SCROLLOCK
183 87f48e6a bellard
    0,  //  108     0x6C    Undefined
184 87f48e6a bellard
    68, //  109     0x6D    0x44            F10     QZ_F10
185 87f48e6a bellard
    0,  //  110     0x6E    Undefined
186 87f48e6a bellard
    88, //  111     0x6F    0x58            F12     QZ_F12
187 87f48e6a bellard
    0,  //  112     0x70    Undefined
188 87f48e6a bellard
    110,//  113     0x71    0x0                     QZ_PAUSE
189 87f48e6a bellard
    210,//  114     0x72    0xd2    E0,52   INSERT  QZ_INSERT
190 87f48e6a bellard
    199,//  115     0x73    0xc7    E0,47   HOME    QZ_HOME
191 87f48e6a bellard
    201,//  116     0x74    0xc9    E0,49   PG UP   QZ_PAGEUP
192 87f48e6a bellard
    211,//  117     0x75    0xd3    E0,53   DELETE  QZ_DELETE
193 87f48e6a bellard
    62, //  118     0x76    0x3e            F4      QZ_F4
194 87f48e6a bellard
    207,//  119     0x77    0xcf    E0,4f   END     QZ_END
195 87f48e6a bellard
    60, //  120     0x78    0x3c            F2      QZ_F2
196 87f48e6a bellard
    209,//  121     0x79    0xd1    E0,51   PG DN   QZ_PAGEDOWN
197 87f48e6a bellard
    59, //  122     0x7A    0x3b            F1      QZ_F1
198 87f48e6a bellard
    203,//  123     0x7B    0xcb    e0,4B   L ARROW QZ_LEFT
199 87f48e6a bellard
    205,//  124     0x7C    0xcd    e0,4D   R ARROW QZ_RIGHT
200 87f48e6a bellard
    208,//  125     0x7D    0xd0    E0,50   D ARROW QZ_DOWN
201 87f48e6a bellard
    200,//  126     0x7E    0xc8    E0,48   U ARROW QZ_UP
202 87f48e6a bellard
/* 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 */
203 3b46e624 ths
204 87f48e6a bellard
/* Aditional 104 Key XP-Keyboard Scancodes from http://www.computer-engineering.org/ps2keyboard/scancodes1.html */
205 87f48e6a bellard
/*
206 3b46e624 ths
    219 //          0xdb            e0,5b   L GUI
207 3b46e624 ths
    220 //          0xdc            e0,5c   R GUI
208 3b46e624 ths
    221 //          0xdd            e0,5d   APPS
209 3b46e624 ths
        //              E0,2A,E0,37         PRNT SCRN
210 3b46e624 ths
        //              E1,1D,45,E1,9D,C5   PAUSE
211 3b46e624 ths
    83  //          0x53    0x53            KP .
212 3b46e624 ths
// ACPI Scan Codes
213 3b46e624 ths
    222 //          0xde            E0, 5E  Power
214 3b46e624 ths
    223 //          0xdf            E0, 5F  Sleep
215 3b46e624 ths
    227 //          0xe3            E0, 63  Wake
216 3b46e624 ths
// Windows Multimedia Scan Codes
217 3b46e624 ths
    153 //          0x99            E0, 19  Next Track
218 3b46e624 ths
    144 //          0x90            E0, 10  Previous Track
219 3b46e624 ths
    164 //          0xa4            E0, 24  Stop
220 3b46e624 ths
    162 //          0xa2            E0, 22  Play/Pause
221 3b46e624 ths
    160 //          0xa0            E0, 20  Mute
222 3b46e624 ths
    176 //          0xb0            E0, 30  Volume Up
223 5fafdf24 ths
    174 //          0xae            E0, 2E  Volume Down
224 3b46e624 ths
    237 //          0xed            E0, 6D  Media Select
225 3b46e624 ths
    236 //          0xec            E0, 6C  E-Mail
226 3b46e624 ths
    161 //          0xa1            E0, 21  Calculator
227 5fafdf24 ths
    235 //          0xeb            E0, 6B  My Computer
228 3b46e624 ths
    229 //          0xe5            E0, 65  WWW Search
229 3b46e624 ths
    178 //          0xb2            E0, 32  WWW Home
230 3b46e624 ths
    234 //          0xea            E0, 6A  WWW Back
231 5fafdf24 ths
    233 //          0xe9            E0, 69  WWW Forward
232 3b46e624 ths
    232 //          0xe8            E0, 68  WWW Stop
233 5fafdf24 ths
    231 //          0xe7            E0, 67  WWW Refresh
234 3b46e624 ths
    230 //          0xe6            E0, 66  WWW Favorites
235 87f48e6a bellard
*/
236 5b0753e0 bellard
};
237 5b0753e0 bellard
238 77047bb7 Andreas Färber
static int cocoa_keycode_to_qemu(int keycode)
239 5b0753e0 bellard
{
240 87f48e6a bellard
    if((sizeof(keymap)/sizeof(int)) <= keycode)
241 5b0753e0 bellard
    {
242 5b0753e0 bellard
        printf("(cocoa) warning unknow keycode 0x%x\n", keycode);
243 5b0753e0 bellard
        return 0;
244 5b0753e0 bellard
    }
245 5b0753e0 bellard
    return keymap[keycode];
246 5b0753e0 bellard
}
247 5b0753e0 bellard
248 c304f7e2 ths
249 c304f7e2 ths
250 5b0753e0 bellard
/*
251 5b0753e0 bellard
 ------------------------------------------------------
252 c304f7e2 ths
    QemuCocoaView
253 5b0753e0 bellard
 ------------------------------------------------------
254 5b0753e0 bellard
*/
255 c304f7e2 ths
@interface QemuCocoaView : NSView
256 5b0753e0 bellard
{
257 c304f7e2 ths
    QEMUScreen screen;
258 c304f7e2 ths
    NSWindow *fullScreenWindow;
259 c304f7e2 ths
    float cx,cy,cw,ch,cdx,cdy;
260 c304f7e2 ths
    CGDataProviderRef dataProviderRef;
261 c304f7e2 ths
    int modifiers_state[256];
262 c304f7e2 ths
    BOOL isMouseGrabed;
263 c304f7e2 ths
    BOOL isFullscreen;
264 c304f7e2 ths
    BOOL isAbsoluteEnabled;
265 c304f7e2 ths
    BOOL isTabletEnabled;
266 c304f7e2 ths
}
267 c304f7e2 ths
- (void) resizeContentToWidth:(int)w height:(int)h displayState:(DisplayState *)ds;
268 c304f7e2 ths
- (void) grabMouse;
269 c304f7e2 ths
- (void) ungrabMouse;
270 c304f7e2 ths
- (void) toggleFullScreen:(id)sender;
271 c304f7e2 ths
- (void) handleEvent:(NSEvent *)event;
272 c304f7e2 ths
- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
273 c304f7e2 ths
- (BOOL) isMouseGrabed;
274 c304f7e2 ths
- (BOOL) isAbsoluteEnabled;
275 c304f7e2 ths
- (float) cdx;
276 c304f7e2 ths
- (float) cdy;
277 c304f7e2 ths
- (QEMUScreen) gscreen;
278 c304f7e2 ths
@end
279 3b46e624 ths
280 7fee199c Andreas Färber
QemuCocoaView *cocoaView;
281 7fee199c Andreas Färber
282 c304f7e2 ths
@implementation QemuCocoaView
283 c304f7e2 ths
- (id)initWithFrame:(NSRect)frameRect
284 c304f7e2 ths
{
285 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: initWithFrame\n");
286 3b46e624 ths
287 c304f7e2 ths
    self = [super initWithFrame:frameRect];
288 c304f7e2 ths
    if (self) {
289 3b46e624 ths
290 c304f7e2 ths
        screen.bitsPerComponent = 8;
291 c304f7e2 ths
        screen.bitsPerPixel = 32;
292 c304f7e2 ths
        screen.width = frameRect.size.width;
293 c304f7e2 ths
        screen.height = frameRect.size.height;
294 3b46e624 ths
295 c304f7e2 ths
    }
296 c304f7e2 ths
    return self;
297 c304f7e2 ths
}
298 3b46e624 ths
299 c304f7e2 ths
- (void) dealloc
300 c304f7e2 ths
{
301 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: dealloc\n");
302 3b46e624 ths
303 c304f7e2 ths
    if (dataProviderRef)
304 c304f7e2 ths
        CGDataProviderRelease(dataProviderRef);
305 3b46e624 ths
306 c304f7e2 ths
    [super dealloc];
307 c304f7e2 ths
}
308 3b46e624 ths
309 d50f71dc Andreas Färber
- (BOOL) isOpaque
310 d50f71dc Andreas Färber
{
311 d50f71dc Andreas Färber
    return YES;
312 d50f71dc Andreas Färber
}
313 d50f71dc Andreas Färber
314 c304f7e2 ths
- (void) drawRect:(NSRect) rect
315 c304f7e2 ths
{
316 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: drawRect\n");
317 c304f7e2 ths
318 c304f7e2 ths
    // get CoreGraphic context
319 c304f7e2 ths
    CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
320 c304f7e2 ths
    CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
321 c304f7e2 ths
    CGContextSetShouldAntialias (viewContextRef, NO);
322 c304f7e2 ths
323 c304f7e2 ths
    // draw screen bitmap directly to Core Graphics context
324 c304f7e2 ths
    if (dataProviderRef) {
325 c304f7e2 ths
        CGImageRef imageRef = CGImageCreate(
326 c304f7e2 ths
            screen.width, //width
327 c304f7e2 ths
            screen.height, //height
328 c304f7e2 ths
            screen.bitsPerComponent, //bitsPerComponent
329 c304f7e2 ths
            screen.bitsPerPixel, //bitsPerPixel
330 9794f74f aliguori
            (screen.width * (screen.bitsPerComponent/2)), //bytesPerRow
331 04afa4a8 Andreas Färber
#ifdef __LITTLE_ENDIAN__
332 c304f7e2 ths
            CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
333 9794f74f aliguori
            kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst,
334 c304f7e2 ths
#else
335 c304f7e2 ths
            CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
336 c304f7e2 ths
            kCGImageAlphaNoneSkipFirst, //bitmapInfo
337 c304f7e2 ths
#endif
338 c304f7e2 ths
            dataProviderRef, //provider
339 c304f7e2 ths
            NULL, //decode
340 c304f7e2 ths
            0, //interpolate
341 c304f7e2 ths
            kCGRenderingIntentDefault //intent
342 c304f7e2 ths
        );
343 38ec7b53 Andreas Färber
// test if host supports "CGImageCreateWithImageInRect" at compile time
344 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
345 c304f7e2 ths
        if (CGImageCreateWithImageInRect == NULL) { // test if "CGImageCreateWithImageInRect" is supported on host at runtime
346 c304f7e2 ths
#endif
347 c304f7e2 ths
            // compatibility drawing code (draws everything) (OS X < 10.4)
348 c304f7e2 ths
            CGContextDrawImage (viewContextRef, CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height), imageRef);
349 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
350 c304f7e2 ths
        } else {
351 c304f7e2 ths
            // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
352 c304f7e2 ths
            const NSRect *rectList;
353 44e4c0ba Andreas Färber
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
354 44e4c0ba Andreas Färber
            NSInteger rectCount;
355 44e4c0ba Andreas Färber
#else
356 c304f7e2 ths
            int rectCount;
357 44e4c0ba Andreas Färber
#endif
358 c304f7e2 ths
            int i;
359 c304f7e2 ths
            CGImageRef clipImageRef;
360 c304f7e2 ths
            CGRect clipRect;
361 c304f7e2 ths
362 c304f7e2 ths
            [self getRectsBeingDrawn:&rectList count:&rectCount];
363 c304f7e2 ths
            for (i = 0; i < rectCount; i++) {
364 c304f7e2 ths
                clipRect.origin.x = rectList[i].origin.x / cdx;
365 c304f7e2 ths
                clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
366 c304f7e2 ths
                clipRect.size.width = rectList[i].size.width / cdx;
367 c304f7e2 ths
                clipRect.size.height = rectList[i].size.height / cdy;
368 c304f7e2 ths
                clipImageRef = CGImageCreateWithImageInRect(
369 c304f7e2 ths
                    imageRef,
370 c304f7e2 ths
                    clipRect
371 c304f7e2 ths
                );
372 c304f7e2 ths
                CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
373 c304f7e2 ths
                CGImageRelease (clipImageRef);
374 5b0753e0 bellard
            }
375 5b0753e0 bellard
        }
376 c304f7e2 ths
#endif
377 c304f7e2 ths
        CGImageRelease (imageRef);
378 c304f7e2 ths
    }
379 5b0753e0 bellard
}
380 5b0753e0 bellard
381 c304f7e2 ths
- (void) setContentDimensions
382 5b0753e0 bellard
{
383 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
384 c304f7e2 ths
385 c304f7e2 ths
    if (isFullscreen) {
386 c304f7e2 ths
        cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
387 c304f7e2 ths
        cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
388 c304f7e2 ths
        cw = screen.width * cdx;
389 c304f7e2 ths
        ch = screen.height * cdy;
390 c304f7e2 ths
        cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
391 c304f7e2 ths
        cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
392 c304f7e2 ths
    } else {
393 c304f7e2 ths
        cx = 0;
394 c304f7e2 ths
        cy = 0;
395 c304f7e2 ths
        cw = screen.width;
396 c304f7e2 ths
        ch = screen.height;
397 c304f7e2 ths
        cdx = 1.0;
398 c304f7e2 ths
        cdy = 1.0;
399 c304f7e2 ths
    }
400 5b0753e0 bellard
}
401 5b0753e0 bellard
402 c304f7e2 ths
- (void) resizeContentToWidth:(int)w height:(int)h displayState:(DisplayState *)ds
403 5b0753e0 bellard
{
404 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: resizeContent\n");
405 c304f7e2 ths
406 c304f7e2 ths
    // update screenBuffer
407 c304f7e2 ths
    if (dataProviderRef)
408 c304f7e2 ths
        CGDataProviderRelease(dataProviderRef);
409 3b46e624 ths
410 9794f74f aliguori
    //sync host window color space with guests
411 9794f74f aliguori
	screen.bitsPerPixel = ds_get_bits_per_pixel(ds);
412 9794f74f aliguori
	screen.bitsPerComponent = ds_get_bytes_per_pixel(ds) * 2;
413 9794f74f aliguori
414 9794f74f aliguori
    dataProviderRef = CGDataProviderCreateWithData(NULL, ds_get_data(ds), w * 4 * h, NULL);
415 3b46e624 ths
416 c304f7e2 ths
    // update windows
417 c304f7e2 ths
    if (isFullscreen) {
418 c304f7e2 ths
        [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
419 c304f7e2 ths
        [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + screen.height, w, h + [normalWindow frame].size.height - screen.height) display:NO animate:NO];
420 c304f7e2 ths
    } else {
421 c304f7e2 ths
        if (qemu_name)
422 c304f7e2 ths
            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
423 12381085 Andreas Färber
        [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + screen.height, w, h + [normalWindow frame].size.height - screen.height) display:YES animate:NO];
424 c304f7e2 ths
    }
425 c304f7e2 ths
    screen.width = w;
426 c304f7e2 ths
    screen.height = h;
427 9794f74f aliguori
	[normalWindow center];
428 c304f7e2 ths
    [self setContentDimensions];
429 c304f7e2 ths
    [self setFrame:NSMakeRect(cx, cy, cw, ch)];
430 5b0753e0 bellard
}
431 5b0753e0 bellard
432 c304f7e2 ths
- (void) toggleFullScreen:(id)sender
433 c304f7e2 ths
{
434 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
435 c304f7e2 ths
436 c304f7e2 ths
    if (isFullscreen) { // switch from fullscreen to desktop
437 c304f7e2 ths
        isFullscreen = FALSE;
438 c304f7e2 ths
        [self ungrabMouse];
439 c304f7e2 ths
        [self setContentDimensions];
440 38ec7b53 Andreas Färber
// test if host supports "exitFullScreenModeWithOptions" at compile time
441 38ec7b53 Andreas Färber
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
442 c304f7e2 ths
        if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
443 c304f7e2 ths
            [self exitFullScreenModeWithOptions:nil];
444 c304f7e2 ths
        } else {
445 c304f7e2 ths
#endif
446 c304f7e2 ths
            [fullScreenWindow close];
447 c304f7e2 ths
            [normalWindow setContentView: self];
448 c304f7e2 ths
            [normalWindow makeKeyAndOrderFront: self];
449 c304f7e2 ths
            [NSMenu setMenuBarVisible:YES];
450 38ec7b53 Andreas Färber
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
451 c304f7e2 ths
        }
452 c304f7e2 ths
#endif
453 c304f7e2 ths
    } else { // switch from desktop to fullscreen
454 c304f7e2 ths
        isFullscreen = TRUE;
455 c304f7e2 ths
        [self grabMouse];
456 c304f7e2 ths
        [self setContentDimensions];
457 38ec7b53 Andreas Färber
// test if host supports "enterFullScreenMode:withOptions" at compile time
458 38ec7b53 Andreas Färber
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
459 c304f7e2 ths
        if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
460 c304f7e2 ths
            [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
461 c304f7e2 ths
                [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
462 c304f7e2 ths
                [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
463 c304f7e2 ths
                 nil]];
464 c304f7e2 ths
        } else {
465 c304f7e2 ths
#endif
466 c304f7e2 ths
            [NSMenu setMenuBarVisible:NO];
467 c304f7e2 ths
            fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
468 c304f7e2 ths
                styleMask:NSBorderlessWindowMask
469 c304f7e2 ths
                backing:NSBackingStoreBuffered
470 c304f7e2 ths
                defer:NO];
471 c304f7e2 ths
            [fullScreenWindow setHasShadow:NO];
472 c304f7e2 ths
            [fullScreenWindow setContentView:self];
473 c304f7e2 ths
            [fullScreenWindow makeKeyAndOrderFront:self];
474 38ec7b53 Andreas Färber
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
475 c304f7e2 ths
        }
476 c304f7e2 ths
#endif
477 c304f7e2 ths
    }
478 c304f7e2 ths
}
479 5b0753e0 bellard
480 c304f7e2 ths
- (void) handleEvent:(NSEvent *)event
481 3b46e624 ths
{
482 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: handleEvent\n");
483 c304f7e2 ths
484 c304f7e2 ths
    int buttons = 0;
485 c304f7e2 ths
    int keycode;
486 c304f7e2 ths
    NSPoint p = [event locationInWindow];
487 c304f7e2 ths
488 c304f7e2 ths
    switch ([event type]) {
489 c304f7e2 ths
        case NSFlagsChanged:
490 c304f7e2 ths
            keycode = cocoa_keycode_to_qemu([event keyCode]);
491 c304f7e2 ths
            if (keycode) {
492 c304f7e2 ths
                if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup
493 c304f7e2 ths
                    kbd_put_keycode(keycode);
494 c304f7e2 ths
                    kbd_put_keycode(keycode | 0x80);
495 c304f7e2 ths
                } else if (is_graphic_console()) {
496 c304f7e2 ths
                    if (keycode & 0x80)
497 c304f7e2 ths
                        kbd_put_keycode(0xe0);
498 c304f7e2 ths
                    if (modifiers_state[keycode] == 0) { // keydown
499 c304f7e2 ths
                        kbd_put_keycode(keycode & 0x7f);
500 c304f7e2 ths
                        modifiers_state[keycode] = 1;
501 c304f7e2 ths
                    } else { // keyup
502 c304f7e2 ths
                        kbd_put_keycode(keycode | 0x80);
503 c304f7e2 ths
                        modifiers_state[keycode] = 0;
504 c304f7e2 ths
                    }
505 c304f7e2 ths
                }
506 c304f7e2 ths
            }
507 3b46e624 ths
508 c304f7e2 ths
            // release Mouse grab when pressing ctrl+alt
509 c304f7e2 ths
            if (!isFullscreen && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
510 c304f7e2 ths
                [self ungrabMouse];
511 c304f7e2 ths
            }
512 c304f7e2 ths
            break;
513 c304f7e2 ths
        case NSKeyDown:
514 3b46e624 ths
515 c304f7e2 ths
            // forward command Key Combos
516 c304f7e2 ths
            if ([event modifierFlags] & NSCommandKeyMask) {
517 c304f7e2 ths
                [NSApp sendEvent:event];
518 c304f7e2 ths
                return;
519 c304f7e2 ths
            }
520 3b46e624 ths
521 c304f7e2 ths
            // default
522 c304f7e2 ths
            keycode = cocoa_keycode_to_qemu([event keyCode]);
523 c304f7e2 ths
524 c304f7e2 ths
            // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
525 c304f7e2 ths
            if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
526 c304f7e2 ths
                switch (keycode) {
527 c304f7e2 ths
528 c304f7e2 ths
                    // enable graphic console
529 c304f7e2 ths
                    case 0x02 ... 0x0a: // '1' to '9' keys
530 c304f7e2 ths
                        console_select(keycode - 0x02);
531 c304f7e2 ths
                        break;
532 c304f7e2 ths
                }
533 c304f7e2 ths
534 c304f7e2 ths
            // handle keys for graphic console
535 c304f7e2 ths
            } else if (is_graphic_console()) {
536 c304f7e2 ths
                if (keycode & 0x80) //check bit for e0 in front
537 c304f7e2 ths
                    kbd_put_keycode(0xe0);
538 c304f7e2 ths
                kbd_put_keycode(keycode & 0x7f); //remove e0 bit in front
539 c304f7e2 ths
540 c304f7e2 ths
            // handlekeys for Monitor
541 c304f7e2 ths
            } else {
542 c304f7e2 ths
                int keysym = 0;
543 c304f7e2 ths
                switch([event keyCode]) {
544 c304f7e2 ths
                case 115:
545 c304f7e2 ths
                    keysym = QEMU_KEY_HOME;
546 c304f7e2 ths
                    break;
547 c304f7e2 ths
                case 117:
548 c304f7e2 ths
                    keysym = QEMU_KEY_DELETE;
549 c304f7e2 ths
                    break;
550 c304f7e2 ths
                case 119:
551 c304f7e2 ths
                    keysym = QEMU_KEY_END;
552 c304f7e2 ths
                    break;
553 c304f7e2 ths
                case 123:
554 c304f7e2 ths
                    keysym = QEMU_KEY_LEFT;
555 c304f7e2 ths
                    break;
556 c304f7e2 ths
                case 124:
557 c304f7e2 ths
                    keysym = QEMU_KEY_RIGHT;
558 c304f7e2 ths
                    break;
559 c304f7e2 ths
                case 125:
560 c304f7e2 ths
                    keysym = QEMU_KEY_DOWN;
561 c304f7e2 ths
                    break;
562 c304f7e2 ths
                case 126:
563 c304f7e2 ths
                    keysym = QEMU_KEY_UP;
564 c304f7e2 ths
                    break;
565 c304f7e2 ths
                default:
566 c304f7e2 ths
                    {
567 c304f7e2 ths
                        NSString *ks = [event characters];
568 c304f7e2 ths
                        if ([ks length] > 0)
569 c304f7e2 ths
                            keysym = [ks characterAtIndex:0];
570 c304f7e2 ths
                    }
571 c304f7e2 ths
                }
572 c304f7e2 ths
                if (keysym)
573 c304f7e2 ths
                    kbd_put_keysym(keysym);
574 c304f7e2 ths
            }
575 c304f7e2 ths
            break;
576 c304f7e2 ths
        case NSKeyUp:
577 c304f7e2 ths
            keycode = cocoa_keycode_to_qemu([event keyCode]);
578 c304f7e2 ths
            if (is_graphic_console()) {
579 c304f7e2 ths
                if (keycode & 0x80)
580 c304f7e2 ths
                    kbd_put_keycode(0xe0);
581 c304f7e2 ths
                kbd_put_keycode(keycode | 0x80); //add 128 to signal release of key
582 c304f7e2 ths
            }
583 c304f7e2 ths
            break;
584 c304f7e2 ths
        case NSMouseMoved:
585 c304f7e2 ths
            if (isAbsoluteEnabled) {
586 c304f7e2 ths
                if (p.x < 0 || p.x > screen.width || p.y < 0 || p.y > screen.height || ![[self window] isKeyWindow]) {
587 c304f7e2 ths
                    if (isTabletEnabled) { // if we leave the window, deactivate the tablet
588 c304f7e2 ths
                        [NSCursor unhide];
589 c304f7e2 ths
                        isTabletEnabled = FALSE;
590 c304f7e2 ths
                    }
591 c304f7e2 ths
                } else {
592 c304f7e2 ths
                    if (!isTabletEnabled) { // if we enter the window, activate the tablet
593 c304f7e2 ths
                        [NSCursor hide];
594 c304f7e2 ths
                        isTabletEnabled = TRUE;
595 c304f7e2 ths
                    }
596 c304f7e2 ths
                }
597 5b0753e0 bellard
            }
598 c304f7e2 ths
            COCOA_MOUSE_EVENT
599 c304f7e2 ths
            break;
600 c304f7e2 ths
        case NSLeftMouseDown:
601 c304f7e2 ths
            if ([event modifierFlags] & NSCommandKeyMask) {
602 c304f7e2 ths
                buttons |= MOUSE_EVENT_RBUTTON;
603 c304f7e2 ths
            } else {
604 c304f7e2 ths
                buttons |= MOUSE_EVENT_LBUTTON;
605 c304f7e2 ths
            }
606 c304f7e2 ths
            COCOA_MOUSE_EVENT
607 c304f7e2 ths
            break;
608 c304f7e2 ths
        case NSRightMouseDown:
609 c304f7e2 ths
            buttons |= MOUSE_EVENT_RBUTTON;
610 c304f7e2 ths
            COCOA_MOUSE_EVENT
611 c304f7e2 ths
            break;
612 c304f7e2 ths
        case NSOtherMouseDown:
613 c304f7e2 ths
            buttons |= MOUSE_EVENT_MBUTTON;
614 c304f7e2 ths
            COCOA_MOUSE_EVENT
615 c304f7e2 ths
            break;
616 c304f7e2 ths
        case NSLeftMouseDragged:
617 c304f7e2 ths
            if ([event modifierFlags] & NSCommandKeyMask) {
618 c304f7e2 ths
                buttons |= MOUSE_EVENT_RBUTTON;
619 c304f7e2 ths
            } else {
620 c304f7e2 ths
                buttons |= MOUSE_EVENT_LBUTTON;
621 c304f7e2 ths
            }
622 c304f7e2 ths
            COCOA_MOUSE_EVENT
623 c304f7e2 ths
            break;
624 c304f7e2 ths
        case NSRightMouseDragged:
625 c304f7e2 ths
            buttons |= MOUSE_EVENT_RBUTTON;
626 c304f7e2 ths
            COCOA_MOUSE_EVENT
627 c304f7e2 ths
            break;
628 c304f7e2 ths
        case NSOtherMouseDragged:
629 c304f7e2 ths
            buttons |= MOUSE_EVENT_MBUTTON;
630 c304f7e2 ths
            COCOA_MOUSE_EVENT
631 c304f7e2 ths
            break;
632 c304f7e2 ths
        case NSLeftMouseUp:
633 c304f7e2 ths
            if (isTabletEnabled) {
634 c304f7e2 ths
                    COCOA_MOUSE_EVENT
635 c304f7e2 ths
            } else if (!isMouseGrabed) {
636 c304f7e2 ths
                if (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height) {
637 c304f7e2 ths
                    [self grabMouse];
638 c304f7e2 ths
                } else {
639 c304f7e2 ths
                    [NSApp sendEvent:event];
640 c304f7e2 ths
                }
641 c304f7e2 ths
            } else {
642 c304f7e2 ths
                COCOA_MOUSE_EVENT
643 c304f7e2 ths
            }
644 c304f7e2 ths
            break;
645 c304f7e2 ths
        case NSRightMouseUp:
646 c304f7e2 ths
            COCOA_MOUSE_EVENT
647 c304f7e2 ths
            break;
648 c304f7e2 ths
        case NSOtherMouseUp:
649 c304f7e2 ths
            COCOA_MOUSE_EVENT
650 c304f7e2 ths
            break;
651 c304f7e2 ths
        case NSScrollWheel:
652 c304f7e2 ths
            if (isTabletEnabled || isMouseGrabed) {
653 c304f7e2 ths
                kbd_mouse_event(0, 0, -[event deltaY], 0);
654 c304f7e2 ths
            } else {
655 c304f7e2 ths
                [NSApp sendEvent:event];
656 c304f7e2 ths
            }
657 c304f7e2 ths
            break;
658 c304f7e2 ths
        default:
659 c304f7e2 ths
            [NSApp sendEvent:event];
660 5b0753e0 bellard
    }
661 5b0753e0 bellard
}
662 5b0753e0 bellard
663 c304f7e2 ths
- (void) grabMouse
664 5b0753e0 bellard
{
665 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: grabMouse\n");
666 3b46e624 ths
667 c304f7e2 ths
    if (!isFullscreen) {
668 c304f7e2 ths
        if (qemu_name)
669 c304f7e2 ths
            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
670 c304f7e2 ths
        else
671 c304f7e2 ths
            [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
672 c304f7e2 ths
    }
673 c304f7e2 ths
    [NSCursor hide];
674 c304f7e2 ths
    CGAssociateMouseAndMouseCursorPosition(FALSE);
675 c304f7e2 ths
    isMouseGrabed = TRUE; // while isMouseGrabed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
676 5b0753e0 bellard
}
677 3b46e624 ths
678 c304f7e2 ths
- (void) ungrabMouse
679 c304f7e2 ths
{
680 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
681 3b46e624 ths
682 c304f7e2 ths
    if (!isFullscreen) {
683 c304f7e2 ths
        if (qemu_name)
684 c304f7e2 ths
            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
685 c304f7e2 ths
        else
686 c304f7e2 ths
            [normalWindow setTitle:@"QEMU"];
687 c304f7e2 ths
    }
688 c304f7e2 ths
    [NSCursor unhide];
689 c304f7e2 ths
    CGAssociateMouseAndMouseCursorPosition(TRUE);
690 c304f7e2 ths
    isMouseGrabed = FALSE;
691 5b0753e0 bellard
}
692 5b0753e0 bellard
693 c304f7e2 ths
- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
694 c304f7e2 ths
- (BOOL) isMouseGrabed {return isMouseGrabed;}
695 c304f7e2 ths
- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
696 c304f7e2 ths
- (float) cdx {return cdx;}
697 c304f7e2 ths
- (float) cdy {return cdy;}
698 c304f7e2 ths
- (QEMUScreen) gscreen {return screen;}
699 5b0753e0 bellard
@end
700 5b0753e0 bellard
701 5b0753e0 bellard
702 c304f7e2 ths
703 5b0753e0 bellard
/*
704 5b0753e0 bellard
 ------------------------------------------------------
705 c304f7e2 ths
    QemuCocoaAppController
706 5b0753e0 bellard
 ------------------------------------------------------
707 5b0753e0 bellard
*/
708 c304f7e2 ths
@interface QemuCocoaAppController : NSObject
709 5b0753e0 bellard
{
710 5b0753e0 bellard
}
711 5b0753e0 bellard
- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
712 c304f7e2 ths
- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
713 c304f7e2 ths
- (void)toggleFullScreen:(id)sender;
714 c304f7e2 ths
- (void)showQEMUDoc:(id)sender;
715 c304f7e2 ths
- (void)showQEMUTec:(id)sender;
716 5b0753e0 bellard
@end
717 5b0753e0 bellard
718 c304f7e2 ths
@implementation QemuCocoaAppController
719 c304f7e2 ths
- (id) init
720 5b0753e0 bellard
{
721 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: init\n");
722 5a246934 bellard
723 c304f7e2 ths
    self = [super init];
724 c304f7e2 ths
    if (self) {
725 5a246934 bellard
726 c304f7e2 ths
        // create a view and add it to the window
727 c304f7e2 ths
        cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
728 c304f7e2 ths
        if(!cocoaView) {
729 c304f7e2 ths
            fprintf(stderr, "(cocoa) can't create a view\n");
730 c304f7e2 ths
            exit(1);
731 c304f7e2 ths
        }
732 3b46e624 ths
733 c304f7e2 ths
        // create a window
734 c304f7e2 ths
        normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
735 c304f7e2 ths
            styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
736 c304f7e2 ths
            backing:NSBackingStoreBuffered defer:NO];
737 c304f7e2 ths
        if(!normalWindow) {
738 c304f7e2 ths
            fprintf(stderr, "(cocoa) can't create window\n");
739 c304f7e2 ths
            exit(1);
740 c304f7e2 ths
        }
741 c304f7e2 ths
        [normalWindow setAcceptsMouseMovedEvents:YES];
742 c304f7e2 ths
        [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]];
743 c304f7e2 ths
        [normalWindow setContentView:cocoaView];
744 561ef251 Andreas Färber
        [normalWindow useOptimizedDrawing:YES];
745 c304f7e2 ths
        [normalWindow makeKeyAndOrderFront:self];
746 9794f74f aliguori
		[normalWindow center];
747 3b46e624 ths
748 c304f7e2 ths
    }
749 c304f7e2 ths
    return self;
750 c304f7e2 ths
}
751 3b46e624 ths
752 c304f7e2 ths
- (void) dealloc
753 c304f7e2 ths
{
754 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
755 c304f7e2 ths
756 c304f7e2 ths
    if (cocoaView)
757 c304f7e2 ths
        [cocoaView release];
758 c304f7e2 ths
    [super dealloc];
759 c304f7e2 ths
}
760 3b46e624 ths
761 c304f7e2 ths
- (void)applicationDidFinishLaunching: (NSNotification *) note
762 c304f7e2 ths
{
763 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
764 c304f7e2 ths
765 c304f7e2 ths
    // Display an open dialog box if no argument were passed or
766 c304f7e2 ths
    // if qemu was launched from the finder ( the Finder passes "-psn" )
767 c304f7e2 ths
    if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) {
768 c304f7e2 ths
        NSOpenPanel *op = [[NSOpenPanel alloc] init];
769 c304f7e2 ths
        [op setPrompt:@"Boot image"];
770 c304f7e2 ths
        [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
771 7a674b13 bellard
        [op beginSheetForDirectory:nil file:nil types:[NSArray arrayWithObjects:@"img",@"iso",@"dmg",@"qcow",@"cow",@"cloop",@"vmdk",nil]
772 c304f7e2 ths
              modalForWindow:normalWindow modalDelegate:self
773 5b0753e0 bellard
              didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
774 c304f7e2 ths
    } else {
775 c304f7e2 ths
        // or Launch Qemu, with the global args
776 c304f7e2 ths
        [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
777 5a246934 bellard
    }
778 5b0753e0 bellard
}
779 5b0753e0 bellard
780 5b0753e0 bellard
- (void)applicationWillTerminate:(NSNotification *)aNotification
781 5b0753e0 bellard
{
782 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
783 c304f7e2 ths
784 5b0753e0 bellard
    qemu_system_shutdown_request();
785 5b0753e0 bellard
    exit(0);
786 5b0753e0 bellard
}
787 5b0753e0 bellard
788 41ea49b3 Andreas Färber
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
789 41ea49b3 Andreas Färber
{
790 41ea49b3 Andreas Färber
    return YES;
791 41ea49b3 Andreas Färber
}
792 41ea49b3 Andreas Färber
793 c304f7e2 ths
- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
794 c304f7e2 ths
{
795 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
796 c304f7e2 ths
797 c304f7e2 ths
    int status;
798 3bbbee18 Andreas Färber
    status = qemu_main(argc, argv, *_NSGetEnviron());
799 c304f7e2 ths
    exit(status);
800 c304f7e2 ths
}
801 c304f7e2 ths
802 5b0753e0 bellard
- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
803 5b0753e0 bellard
{
804 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n");
805 3b46e624 ths
806 c304f7e2 ths
    if(returnCode == NSCancelButton) {
807 c304f7e2 ths
        exit(0);
808 c304f7e2 ths
    } else if(returnCode == NSOKButton) {
809 fd10a04e Andreas Färber
        const char *bin = "qemu";
810 c304f7e2 ths
        char *img = (char*)[ [ sheet filename ] cStringUsingEncoding:NSASCIIStringEncoding];
811 3b46e624 ths
812 5b0753e0 bellard
        char **argv = (char**)malloc( sizeof(char*)*3 );
813 3b46e624 ths
814 5b0753e0 bellard
        asprintf(&argv[0], "%s", bin);
815 5b0753e0 bellard
        asprintf(&argv[1], "-hda");
816 5b0753e0 bellard
        asprintf(&argv[2], "%s", img);
817 3b46e624 ths
818 5b0753e0 bellard
        printf("Using argc %d argv %s -hda %s\n", 3, bin, img);
819 3b46e624 ths
820 5b0753e0 bellard
        [self startEmulationWithArgc:3 argv:(char**)argv];
821 5b0753e0 bellard
    }
822 5b0753e0 bellard
}
823 c304f7e2 ths
- (void)toggleFullScreen:(id)sender
824 c304f7e2 ths
{
825 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
826 c304f7e2 ths
827 c304f7e2 ths
    [cocoaView toggleFullScreen:sender];
828 c304f7e2 ths
}
829 5b0753e0 bellard
830 c304f7e2 ths
- (void)showQEMUDoc:(id)sender
831 5b0753e0 bellard
{
832 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
833 c304f7e2 ths
834 c304f7e2 ths
    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
835 c304f7e2 ths
        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
836 c304f7e2 ths
}
837 c304f7e2 ths
838 c304f7e2 ths
- (void)showQEMUTec:(id)sender
839 c304f7e2 ths
{
840 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
841 c304f7e2 ths
842 c304f7e2 ths
    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
843 c304f7e2 ths
        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
844 5b0753e0 bellard
}
845 5b0753e0 bellard
@end
846 5b0753e0 bellard
847 5b0753e0 bellard
848 c304f7e2 ths
849 c304f7e2 ths
// Dock Connection
850 5b0753e0 bellard
typedef struct CPSProcessSerNum
851 5b0753e0 bellard
{
852 5b0753e0 bellard
        UInt32                lo;
853 5b0753e0 bellard
        UInt32                hi;
854 5b0753e0 bellard
} CPSProcessSerNum;
855 5b0753e0 bellard
856 64b85a8f Blue Swirl
OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
857 64b85a8f Blue Swirl
OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
858 64b85a8f Blue Swirl
OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
859 5b0753e0 bellard
860 c304f7e2 ths
int main (int argc, const char * argv[]) {
861 5b0753e0 bellard
862 c304f7e2 ths
    gArgc = argc;
863 c304f7e2 ths
    gArgv = (char **)argv;
864 c304f7e2 ths
    CPSProcessSerNum PSN;
865 f4918804 Andreas Färber
    int i;
866 f4918804 Andreas Färber
867 f4918804 Andreas Färber
    /* In case we don't need to display a window, let's not do that */
868 f4918804 Andreas Färber
    for (i = 1; i < argc; i++) {
869 e4ebcc1a Tristan Gingold
        const char *opt = argv[i];
870 e4ebcc1a Tristan Gingold
871 e4ebcc1a Tristan Gingold
        if (opt[0] == '-') {
872 e4ebcc1a Tristan Gingold
            /* Treat --foo the same as -foo.  */
873 e4ebcc1a Tristan Gingold
            if (opt[1] == '-') {
874 e4ebcc1a Tristan Gingold
                opt++;
875 e4ebcc1a Tristan Gingold
            }
876 9851484f Alexandre Raymond
            if (!strcmp(opt, "-h") || !strcmp(opt, "-help") ||
877 9851484f Alexandre Raymond
                !strcmp(opt, "-vnc") ||
878 e4ebcc1a Tristan Gingold
                !strcmp(opt, "-nographic") ||
879 e4ebcc1a Tristan Gingold
                !strcmp(opt, "-version") ||
880 e4ebcc1a Tristan Gingold
                !strcmp(opt, "-curses")) {
881 3bbbee18 Andreas Färber
                return qemu_main(gArgc, gArgv, *_NSGetEnviron());
882 e4ebcc1a Tristan Gingold
            }
883 f4918804 Andreas Färber
        }
884 f4918804 Andreas Färber
    }
885 5b0753e0 bellard
886 c304f7e2 ths
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
887 c304f7e2 ths
    [NSApplication sharedApplication];
888 5b0753e0 bellard
889 c304f7e2 ths
    if (!CPSGetCurrentProcess(&PSN))
890 c304f7e2 ths
        if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
891 c304f7e2 ths
            if (!CPSSetFrontProcess(&PSN))
892 c304f7e2 ths
                [NSApplication sharedApplication];
893 5b0753e0 bellard
894 c304f7e2 ths
    // Add menus
895 c304f7e2 ths
    NSMenu      *menu;
896 c304f7e2 ths
    NSMenuItem  *menuItem;
897 5b0753e0 bellard
898 c304f7e2 ths
    [NSApp setMainMenu:[[NSMenu alloc] init]];
899 5b0753e0 bellard
900 c304f7e2 ths
    // Application menu
901 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@""];
902 c304f7e2 ths
    [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
903 c304f7e2 ths
    [menu addItem:[NSMenuItem separatorItem]]; //Separator
904 c304f7e2 ths
    [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
905 c304f7e2 ths
    menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
906 c304f7e2 ths
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
907 c304f7e2 ths
    [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
908 c304f7e2 ths
    [menu addItem:[NSMenuItem separatorItem]]; //Separator
909 c304f7e2 ths
    [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
910 c304f7e2 ths
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
911 c304f7e2 ths
    [menuItem setSubmenu:menu];
912 c304f7e2 ths
    [[NSApp mainMenu] addItem:menuItem];
913 c304f7e2 ths
    [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
914 3b46e624 ths
915 c304f7e2 ths
    // View menu
916 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@"View"];
917 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
918 c304f7e2 ths
    menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
919 c304f7e2 ths
    [menuItem setSubmenu:menu];
920 5b0753e0 bellard
    [[NSApp mainMenu] addItem:menuItem];
921 5b0753e0 bellard
922 c304f7e2 ths
    // Window menu
923 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@"Window"];
924 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
925 c304f7e2 ths
    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
926 c304f7e2 ths
    [menuItem setSubmenu:menu];
927 c304f7e2 ths
    [[NSApp mainMenu] addItem:menuItem];
928 c304f7e2 ths
    [NSApp setWindowsMenu:menu];
929 c304f7e2 ths
930 c304f7e2 ths
    // Help menu
931 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@"Help"];
932 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
933 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
934 c304f7e2 ths
    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
935 c304f7e2 ths
    [menuItem setSubmenu:menu];
936 c304f7e2 ths
    [[NSApp mainMenu] addItem:menuItem];
937 5b0753e0 bellard
938 c304f7e2 ths
    // Create an Application controller
939 c304f7e2 ths
    QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
940 c304f7e2 ths
    [NSApp setDelegate:appController];
941 5b0753e0 bellard
942 c304f7e2 ths
    // Start the main event loop
943 c304f7e2 ths
    [NSApp run];
944 5b0753e0 bellard
945 c304f7e2 ths
    [appController release];
946 c304f7e2 ths
    [pool release];
947 3b46e624 ths
948 c304f7e2 ths
    return 0;
949 c304f7e2 ths
}
950 3b46e624 ths
951 3b46e624 ths
952 5b0753e0 bellard
953 c304f7e2 ths
#pragma mark qemu
954 c304f7e2 ths
static void cocoa_update(DisplayState *ds, int x, int y, int w, int h)
955 c304f7e2 ths
{
956 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
957 c304f7e2 ths
958 c304f7e2 ths
    NSRect rect;
959 c304f7e2 ths
    if ([cocoaView cdx] == 1.0) {
960 c304f7e2 ths
        rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
961 c304f7e2 ths
    } else {
962 c304f7e2 ths
        rect = NSMakeRect(
963 c304f7e2 ths
            x * [cocoaView cdx],
964 c304f7e2 ths
            ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
965 c304f7e2 ths
            w * [cocoaView cdx],
966 c304f7e2 ths
            h * [cocoaView cdy]);
967 c304f7e2 ths
    }
968 17ccbc27 Andreas Färber
    [cocoaView setNeedsDisplayInRect:rect];
969 5b0753e0 bellard
}
970 5b0753e0 bellard
971 9794f74f aliguori
static void cocoa_resize(DisplayState *ds)
972 5b0753e0 bellard
{
973 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_resize\n");
974 3b46e624 ths
975 9794f74f aliguori
    [cocoaView resizeContentToWidth:(int)(ds_get_width(ds)) height:(int)(ds_get_height(ds)) displayState:ds];
976 c304f7e2 ths
}
977 3b46e624 ths
978 c304f7e2 ths
static void cocoa_refresh(DisplayState *ds)
979 c304f7e2 ths
{
980 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
981 3b46e624 ths
982 c304f7e2 ths
    if (kbd_mouse_is_absolute()) {
983 c304f7e2 ths
        if (![cocoaView isAbsoluteEnabled]) {
984 c304f7e2 ths
            if ([cocoaView isMouseGrabed]) {
985 c304f7e2 ths
                [cocoaView ungrabMouse];
986 c304f7e2 ths
            }
987 c304f7e2 ths
        }
988 c304f7e2 ths
        [cocoaView setAbsoluteEnabled:YES];
989 c304f7e2 ths
    }
990 5b0753e0 bellard
991 c304f7e2 ths
    NSDate *distantPast;
992 c304f7e2 ths
    NSEvent *event;
993 c304f7e2 ths
    distantPast = [NSDate distantPast];
994 c304f7e2 ths
    do {
995 c304f7e2 ths
        event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
996 c304f7e2 ths
                        inMode: NSDefaultRunLoopMode dequeue:YES];
997 c304f7e2 ths
        if (event != nil) {
998 c304f7e2 ths
            [cocoaView handleEvent:event];
999 c304f7e2 ths
        }
1000 c304f7e2 ths
    } while(event != nil);
1001 c304f7e2 ths
    vga_hw_update();
1002 c304f7e2 ths
}
1003 3b46e624 ths
1004 c304f7e2 ths
static void cocoa_cleanup(void)
1005 c304f7e2 ths
{
1006 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
1007 9794f74f aliguori
	qemu_free(dcl);
1008 5b0753e0 bellard
}
1009 5b0753e0 bellard
1010 c304f7e2 ths
void cocoa_display_init(DisplayState *ds, int full_screen)
1011 5b0753e0 bellard
{
1012 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
1013 c304f7e2 ths
1014 9794f74f aliguori
	dcl = qemu_mallocz(sizeof(DisplayChangeListener));
1015 9794f74f aliguori
	
1016 9794f74f aliguori
    // register vga output callbacks
1017 9794f74f aliguori
    dcl->dpy_update = cocoa_update;
1018 9794f74f aliguori
    dcl->dpy_resize = cocoa_resize;
1019 9794f74f aliguori
    dcl->dpy_refresh = cocoa_refresh;
1020 cae41b10 bellard
1021 9794f74f aliguori
	register_displaychangelistener(ds, dcl);
1022 cae41b10 bellard
1023 c304f7e2 ths
    // register cleanup function
1024 c304f7e2 ths
    atexit(cocoa_cleanup);
1025 5b0753e0 bellard
}