Statistics
| Branch: | Revision:

root / cocoa.m @ 3f4cb3d3

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