Statistics
| Branch: | Revision:

root / cocoa.m @ 44e4c0ba

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