Statistics
| Branch: | Revision:

root / ui / cocoa.m @ e4ebcc1a

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