Statistics
| Branch: | Revision:

root / cocoa.m @ 8c5e95d8

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 c304f7e2 ths
static void *screenBuffer;
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 (screenBuffer)
296 c304f7e2 ths
        free(screenBuffer);
297 3b46e624 ths
298 c304f7e2 ths
    if (dataProviderRef)
299 c304f7e2 ths
        CGDataProviderRelease(dataProviderRef);
300 3b46e624 ths
301 c304f7e2 ths
    [super dealloc];
302 c304f7e2 ths
}
303 3b46e624 ths
304 c304f7e2 ths
- (void) drawRect:(NSRect) rect
305 c304f7e2 ths
{
306 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: drawRect\n");
307 c304f7e2 ths
308 c304f7e2 ths
    if ((int)screenBuffer == -1)
309 c304f7e2 ths
        return;
310 c304f7e2 ths
311 c304f7e2 ths
    // get CoreGraphic context
312 c304f7e2 ths
    CGContextRef viewContextRef = [[NSGraphicsContext currentContext] graphicsPort];
313 c304f7e2 ths
    CGContextSetInterpolationQuality (viewContextRef, kCGInterpolationNone);
314 c304f7e2 ths
    CGContextSetShouldAntialias (viewContextRef, NO);
315 c304f7e2 ths
316 c304f7e2 ths
    // draw screen bitmap directly to Core Graphics context
317 c304f7e2 ths
    if (dataProviderRef) {
318 c304f7e2 ths
        CGImageRef imageRef = CGImageCreate(
319 c304f7e2 ths
            screen.width, //width
320 c304f7e2 ths
            screen.height, //height
321 c304f7e2 ths
            screen.bitsPerComponent, //bitsPerComponent
322 c304f7e2 ths
            screen.bitsPerPixel, //bitsPerPixel
323 c304f7e2 ths
            (screen.width * 4), //bytesPerRow
324 c304f7e2 ths
#if __LITTLE_ENDIAN__
325 c304f7e2 ths
            CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), //colorspace for OS X >= 10.4
326 c304f7e2 ths
            kCGImageAlphaNoneSkipLast,
327 c304f7e2 ths
#else
328 c304f7e2 ths
            CGColorSpaceCreateDeviceRGB(), //colorspace for OS X < 10.4 (actually ppc)
329 c304f7e2 ths
            kCGImageAlphaNoneSkipFirst, //bitmapInfo
330 c304f7e2 ths
#endif
331 c304f7e2 ths
            dataProviderRef, //provider
332 c304f7e2 ths
            NULL, //decode
333 c304f7e2 ths
            0, //interpolate
334 c304f7e2 ths
            kCGRenderingIntentDefault //intent
335 c304f7e2 ths
        );
336 c304f7e2 ths
// test if host support "CGImageCreateWithImageInRect" at compiletime
337 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
338 c304f7e2 ths
        if (CGImageCreateWithImageInRect == NULL) { // test if "CGImageCreateWithImageInRect" is supported on host at runtime
339 c304f7e2 ths
#endif
340 c304f7e2 ths
            // compatibility drawing code (draws everything) (OS X < 10.4)
341 c304f7e2 ths
            CGContextDrawImage (viewContextRef, CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height), imageRef);
342 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
343 c304f7e2 ths
        } else {
344 c304f7e2 ths
            // selective drawing code (draws only dirty rectangles) (OS X >= 10.4)
345 c304f7e2 ths
            const NSRect *rectList;
346 c304f7e2 ths
            int rectCount;
347 c304f7e2 ths
            int i;
348 c304f7e2 ths
            CGImageRef clipImageRef;
349 c304f7e2 ths
            CGRect clipRect;
350 c304f7e2 ths
351 c304f7e2 ths
            [self getRectsBeingDrawn:&rectList count:&rectCount];
352 c304f7e2 ths
            for (i = 0; i < rectCount; i++) {
353 c304f7e2 ths
                clipRect.origin.x = rectList[i].origin.x / cdx;
354 c304f7e2 ths
                clipRect.origin.y = (float)screen.height - (rectList[i].origin.y + rectList[i].size.height) / cdy;
355 c304f7e2 ths
                clipRect.size.width = rectList[i].size.width / cdx;
356 c304f7e2 ths
                clipRect.size.height = rectList[i].size.height / cdy;
357 c304f7e2 ths
                clipImageRef = CGImageCreateWithImageInRect(
358 c304f7e2 ths
                    imageRef,
359 c304f7e2 ths
                    clipRect
360 c304f7e2 ths
                );
361 c304f7e2 ths
                CGContextDrawImage (viewContextRef, cgrect(rectList[i]), clipImageRef);
362 c304f7e2 ths
                CGImageRelease (clipImageRef);
363 5b0753e0 bellard
            }
364 5b0753e0 bellard
        }
365 c304f7e2 ths
#endif
366 c304f7e2 ths
        CGImageRelease (imageRef);
367 c304f7e2 ths
    }
368 5b0753e0 bellard
}
369 5b0753e0 bellard
370 c304f7e2 ths
- (void) setContentDimensions
371 5b0753e0 bellard
{
372 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: setContentDimensions\n");
373 c304f7e2 ths
374 c304f7e2 ths
    if (isFullscreen) {
375 c304f7e2 ths
        cdx = [[NSScreen mainScreen] frame].size.width / (float)screen.width;
376 c304f7e2 ths
        cdy = [[NSScreen mainScreen] frame].size.height / (float)screen.height;
377 c304f7e2 ths
        cw = screen.width * cdx;
378 c304f7e2 ths
        ch = screen.height * cdy;
379 c304f7e2 ths
        cx = ([[NSScreen mainScreen] frame].size.width - cw) / 2.0;
380 c304f7e2 ths
        cy = ([[NSScreen mainScreen] frame].size.height - ch) / 2.0;
381 c304f7e2 ths
    } else {
382 c304f7e2 ths
        cx = 0;
383 c304f7e2 ths
        cy = 0;
384 c304f7e2 ths
        cw = screen.width;
385 c304f7e2 ths
        ch = screen.height;
386 c304f7e2 ths
        cdx = 1.0;
387 c304f7e2 ths
        cdy = 1.0;
388 c304f7e2 ths
    }
389 5b0753e0 bellard
}
390 5b0753e0 bellard
391 c304f7e2 ths
- (void) resizeContentToWidth:(int)w height:(int)h displayState:(DisplayState *)ds
392 5b0753e0 bellard
{
393 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: resizeContent\n");
394 c304f7e2 ths
395 c304f7e2 ths
    // update screenBuffer
396 c304f7e2 ths
    if (dataProviderRef)
397 c304f7e2 ths
        CGDataProviderRelease(dataProviderRef);
398 c304f7e2 ths
    if (screenBuffer)
399 c304f7e2 ths
        free(screenBuffer);
400 c304f7e2 ths
    screenBuffer = malloc( w * 4 * h );
401 c304f7e2 ths
402 c304f7e2 ths
    ds->data = screenBuffer;
403 c304f7e2 ths
    ds->linesize =  (w * 4);
404 c304f7e2 ths
    ds->depth = 32;
405 c304f7e2 ths
    ds->width = w;
406 c304f7e2 ths
    ds->height = h;
407 c304f7e2 ths
#ifdef __LITTLE_ENDIAN__
408 c304f7e2 ths
    ds->bgr = 1;
409 c304f7e2 ths
#else
410 c304f7e2 ths
    ds->bgr = 0;
411 c304f7e2 ths
#endif
412 3b46e624 ths
413 c304f7e2 ths
    dataProviderRef = CGDataProviderCreateWithData(NULL, screenBuffer, w * 4 * h, NULL);
414 3b46e624 ths
415 c304f7e2 ths
    // update windows
416 c304f7e2 ths
    if (isFullscreen) {
417 c304f7e2 ths
        [[fullScreenWindow contentView] setFrame:[[NSScreen mainScreen] frame]];
418 c304f7e2 ths
        [normalWindow setFrame:NSMakeRect([normalWindow frame].origin.x, [normalWindow frame].origin.y - h + screen.height, w, h + [normalWindow frame].size.height - screen.height) display:NO animate:NO];
419 c304f7e2 ths
    } else {
420 c304f7e2 ths
        if (qemu_name)
421 c304f7e2 ths
            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
422 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];
423 c304f7e2 ths
    }
424 c304f7e2 ths
    screen.width = w;
425 c304f7e2 ths
    screen.height = h;
426 c304f7e2 ths
    [self setContentDimensions];
427 c304f7e2 ths
    [self setFrame:NSMakeRect(cx, cy, cw, ch)];
428 5b0753e0 bellard
}
429 5b0753e0 bellard
430 c304f7e2 ths
- (void) toggleFullScreen:(id)sender
431 c304f7e2 ths
{
432 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: toggleFullScreen\n");
433 c304f7e2 ths
434 c304f7e2 ths
    if (isFullscreen) { // switch from fullscreen to desktop
435 c304f7e2 ths
        isFullscreen = FALSE;
436 c304f7e2 ths
        [self ungrabMouse];
437 c304f7e2 ths
        [self setContentDimensions];
438 c304f7e2 ths
// test if host support "enterFullScreenMode:withOptions" at compiletime
439 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
440 c304f7e2 ths
        if ([NSView respondsToSelector:@selector(exitFullScreenModeWithOptions:)]) { // test if "exitFullScreenModeWithOptions" is supported on host at runtime
441 c304f7e2 ths
            [self exitFullScreenModeWithOptions:nil];
442 c304f7e2 ths
        } else {
443 c304f7e2 ths
#endif
444 c304f7e2 ths
            [fullScreenWindow close];
445 c304f7e2 ths
            [normalWindow setContentView: self];
446 c304f7e2 ths
            [normalWindow makeKeyAndOrderFront: self];
447 c304f7e2 ths
            [NSMenu setMenuBarVisible:YES];
448 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
449 c304f7e2 ths
        }
450 c304f7e2 ths
#endif
451 c304f7e2 ths
    } else { // switch from desktop to fullscreen
452 c304f7e2 ths
        isFullscreen = TRUE;
453 c304f7e2 ths
        [self grabMouse];
454 c304f7e2 ths
        [self setContentDimensions];
455 c304f7e2 ths
// test if host support "enterFullScreenMode:withOptions" at compiletime
456 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
457 c304f7e2 ths
        if ([NSView respondsToSelector:@selector(enterFullScreenMode:withOptions:)]) { // test if "enterFullScreenMode:withOptions" is supported on host at runtime
458 c304f7e2 ths
            [self enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
459 c304f7e2 ths
                [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens,
460 c304f7e2 ths
                [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kCGDisplayModeIsStretched, nil], NSFullScreenModeSetting,
461 c304f7e2 ths
                 nil]];
462 c304f7e2 ths
        } else {
463 c304f7e2 ths
#endif
464 c304f7e2 ths
            [NSMenu setMenuBarVisible:NO];
465 c304f7e2 ths
            fullScreenWindow = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame]
466 c304f7e2 ths
                styleMask:NSBorderlessWindowMask
467 c304f7e2 ths
                backing:NSBackingStoreBuffered
468 c304f7e2 ths
                defer:NO];
469 c304f7e2 ths
            [fullScreenWindow setHasShadow:NO];
470 c304f7e2 ths
            [fullScreenWindow setContentView:self];
471 c304f7e2 ths
            [fullScreenWindow makeKeyAndOrderFront:self];
472 c304f7e2 ths
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
473 c304f7e2 ths
        }
474 c304f7e2 ths
#endif
475 c304f7e2 ths
    }
476 c304f7e2 ths
}
477 5b0753e0 bellard
478 c304f7e2 ths
- (void) handleEvent:(NSEvent *)event
479 3b46e624 ths
{
480 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: handleEvent\n");
481 c304f7e2 ths
482 c304f7e2 ths
    int buttons = 0;
483 c304f7e2 ths
    int keycode;
484 c304f7e2 ths
    NSPoint p = [event locationInWindow];
485 c304f7e2 ths
486 c304f7e2 ths
    switch ([event type]) {
487 c304f7e2 ths
        case NSFlagsChanged:
488 c304f7e2 ths
            keycode = cocoa_keycode_to_qemu([event keyCode]);
489 c304f7e2 ths
            if (keycode) {
490 c304f7e2 ths
                if (keycode == 58 || keycode == 69) { // emulate caps lock and num lock keydown and keyup
491 c304f7e2 ths
                    kbd_put_keycode(keycode);
492 c304f7e2 ths
                    kbd_put_keycode(keycode | 0x80);
493 c304f7e2 ths
                } else if (is_graphic_console()) {
494 c304f7e2 ths
                    if (keycode & 0x80)
495 c304f7e2 ths
                        kbd_put_keycode(0xe0);
496 c304f7e2 ths
                    if (modifiers_state[keycode] == 0) { // keydown
497 c304f7e2 ths
                        kbd_put_keycode(keycode & 0x7f);
498 c304f7e2 ths
                        modifiers_state[keycode] = 1;
499 c304f7e2 ths
                    } else { // keyup
500 c304f7e2 ths
                        kbd_put_keycode(keycode | 0x80);
501 c304f7e2 ths
                        modifiers_state[keycode] = 0;
502 c304f7e2 ths
                    }
503 c304f7e2 ths
                }
504 c304f7e2 ths
            }
505 3b46e624 ths
506 c304f7e2 ths
            // release Mouse grab when pressing ctrl+alt
507 c304f7e2 ths
            if (!isFullscreen && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
508 c304f7e2 ths
                [self ungrabMouse];
509 c304f7e2 ths
            }
510 c304f7e2 ths
            break;
511 c304f7e2 ths
        case NSKeyDown:
512 3b46e624 ths
513 c304f7e2 ths
            // forward command Key Combos
514 c304f7e2 ths
            if ([event modifierFlags] & NSCommandKeyMask) {
515 c304f7e2 ths
                [NSApp sendEvent:event];
516 c304f7e2 ths
                return;
517 c304f7e2 ths
            }
518 3b46e624 ths
519 c304f7e2 ths
            // default
520 c304f7e2 ths
            keycode = cocoa_keycode_to_qemu([event keyCode]);
521 c304f7e2 ths
522 c304f7e2 ths
            // handle control + alt Key Combos (ctrl+alt is reserved for QEMU)
523 c304f7e2 ths
            if (([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask)) {
524 c304f7e2 ths
                switch (keycode) {
525 c304f7e2 ths
526 c304f7e2 ths
                    // enable graphic console
527 c304f7e2 ths
                    case 0x02 ... 0x0a: // '1' to '9' keys
528 c304f7e2 ths
                        console_select(keycode - 0x02);
529 c304f7e2 ths
                        break;
530 c304f7e2 ths
                }
531 c304f7e2 ths
532 c304f7e2 ths
            // handle keys for graphic console
533 c304f7e2 ths
            } else if (is_graphic_console()) {
534 c304f7e2 ths
                if (keycode & 0x80) //check bit for e0 in front
535 c304f7e2 ths
                    kbd_put_keycode(0xe0);
536 c304f7e2 ths
                kbd_put_keycode(keycode & 0x7f); //remove e0 bit in front
537 c304f7e2 ths
538 c304f7e2 ths
            // handlekeys for Monitor
539 c304f7e2 ths
            } else {
540 c304f7e2 ths
                int keysym = 0;
541 c304f7e2 ths
                switch([event keyCode]) {
542 c304f7e2 ths
                case 115:
543 c304f7e2 ths
                    keysym = QEMU_KEY_HOME;
544 c304f7e2 ths
                    break;
545 c304f7e2 ths
                case 117:
546 c304f7e2 ths
                    keysym = QEMU_KEY_DELETE;
547 c304f7e2 ths
                    break;
548 c304f7e2 ths
                case 119:
549 c304f7e2 ths
                    keysym = QEMU_KEY_END;
550 c304f7e2 ths
                    break;
551 c304f7e2 ths
                case 123:
552 c304f7e2 ths
                    keysym = QEMU_KEY_LEFT;
553 c304f7e2 ths
                    break;
554 c304f7e2 ths
                case 124:
555 c304f7e2 ths
                    keysym = QEMU_KEY_RIGHT;
556 c304f7e2 ths
                    break;
557 c304f7e2 ths
                case 125:
558 c304f7e2 ths
                    keysym = QEMU_KEY_DOWN;
559 c304f7e2 ths
                    break;
560 c304f7e2 ths
                case 126:
561 c304f7e2 ths
                    keysym = QEMU_KEY_UP;
562 c304f7e2 ths
                    break;
563 c304f7e2 ths
                default:
564 c304f7e2 ths
                    {
565 c304f7e2 ths
                        NSString *ks = [event characters];
566 c304f7e2 ths
                        if ([ks length] > 0)
567 c304f7e2 ths
                            keysym = [ks characterAtIndex:0];
568 c304f7e2 ths
                    }
569 c304f7e2 ths
                }
570 c304f7e2 ths
                if (keysym)
571 c304f7e2 ths
                    kbd_put_keysym(keysym);
572 c304f7e2 ths
            }
573 c304f7e2 ths
            break;
574 c304f7e2 ths
        case NSKeyUp:
575 c304f7e2 ths
            keycode = cocoa_keycode_to_qemu([event keyCode]);
576 c304f7e2 ths
            if (is_graphic_console()) {
577 c304f7e2 ths
                if (keycode & 0x80)
578 c304f7e2 ths
                    kbd_put_keycode(0xe0);
579 c304f7e2 ths
                kbd_put_keycode(keycode | 0x80); //add 128 to signal release of key
580 c304f7e2 ths
            }
581 c304f7e2 ths
            break;
582 c304f7e2 ths
        case NSMouseMoved:
583 c304f7e2 ths
            if (isAbsoluteEnabled) {
584 c304f7e2 ths
                if (p.x < 0 || p.x > screen.width || p.y < 0 || p.y > screen.height || ![[self window] isKeyWindow]) {
585 c304f7e2 ths
                    if (isTabletEnabled) { // if we leave the window, deactivate the tablet
586 c304f7e2 ths
                        [NSCursor unhide];
587 c304f7e2 ths
                        isTabletEnabled = FALSE;
588 c304f7e2 ths
                    }
589 c304f7e2 ths
                } else {
590 c304f7e2 ths
                    if (!isTabletEnabled) { // if we enter the window, activate the tablet
591 c304f7e2 ths
                        [NSCursor hide];
592 c304f7e2 ths
                        isTabletEnabled = TRUE;
593 c304f7e2 ths
                    }
594 c304f7e2 ths
                }
595 5b0753e0 bellard
            }
596 c304f7e2 ths
            COCOA_MOUSE_EVENT
597 c304f7e2 ths
            break;
598 c304f7e2 ths
        case NSLeftMouseDown:
599 c304f7e2 ths
            if ([event modifierFlags] & NSCommandKeyMask) {
600 c304f7e2 ths
                buttons |= MOUSE_EVENT_RBUTTON;
601 c304f7e2 ths
            } else {
602 c304f7e2 ths
                buttons |= MOUSE_EVENT_LBUTTON;
603 c304f7e2 ths
            }
604 c304f7e2 ths
            COCOA_MOUSE_EVENT
605 c304f7e2 ths
            break;
606 c304f7e2 ths
        case NSRightMouseDown:
607 c304f7e2 ths
            buttons |= MOUSE_EVENT_RBUTTON;
608 c304f7e2 ths
            COCOA_MOUSE_EVENT
609 c304f7e2 ths
            break;
610 c304f7e2 ths
        case NSOtherMouseDown:
611 c304f7e2 ths
            buttons |= MOUSE_EVENT_MBUTTON;
612 c304f7e2 ths
            COCOA_MOUSE_EVENT
613 c304f7e2 ths
            break;
614 c304f7e2 ths
        case NSLeftMouseDragged:
615 c304f7e2 ths
            if ([event modifierFlags] & NSCommandKeyMask) {
616 c304f7e2 ths
                buttons |= MOUSE_EVENT_RBUTTON;
617 c304f7e2 ths
            } else {
618 c304f7e2 ths
                buttons |= MOUSE_EVENT_LBUTTON;
619 c304f7e2 ths
            }
620 c304f7e2 ths
            COCOA_MOUSE_EVENT
621 c304f7e2 ths
            break;
622 c304f7e2 ths
        case NSRightMouseDragged:
623 c304f7e2 ths
            buttons |= MOUSE_EVENT_RBUTTON;
624 c304f7e2 ths
            COCOA_MOUSE_EVENT
625 c304f7e2 ths
            break;
626 c304f7e2 ths
        case NSOtherMouseDragged:
627 c304f7e2 ths
            buttons |= MOUSE_EVENT_MBUTTON;
628 c304f7e2 ths
            COCOA_MOUSE_EVENT
629 c304f7e2 ths
            break;
630 c304f7e2 ths
        case NSLeftMouseUp:
631 c304f7e2 ths
            if (isTabletEnabled) {
632 c304f7e2 ths
                    COCOA_MOUSE_EVENT
633 c304f7e2 ths
            } else if (!isMouseGrabed) {
634 c304f7e2 ths
                if (p.x > -1 && p.x < screen.width && p.y > -1 && p.y < screen.height) {
635 c304f7e2 ths
                    [self grabMouse];
636 c304f7e2 ths
                } else {
637 c304f7e2 ths
                    [NSApp sendEvent:event];
638 c304f7e2 ths
                }
639 c304f7e2 ths
            } else {
640 c304f7e2 ths
                COCOA_MOUSE_EVENT
641 c304f7e2 ths
            }
642 c304f7e2 ths
            break;
643 c304f7e2 ths
        case NSRightMouseUp:
644 c304f7e2 ths
            COCOA_MOUSE_EVENT
645 c304f7e2 ths
            break;
646 c304f7e2 ths
        case NSOtherMouseUp:
647 c304f7e2 ths
            COCOA_MOUSE_EVENT
648 c304f7e2 ths
            break;
649 c304f7e2 ths
        case NSScrollWheel:
650 c304f7e2 ths
            if (isTabletEnabled || isMouseGrabed) {
651 c304f7e2 ths
                kbd_mouse_event(0, 0, -[event deltaY], 0);
652 c304f7e2 ths
            } else {
653 c304f7e2 ths
                [NSApp sendEvent:event];
654 c304f7e2 ths
            }
655 c304f7e2 ths
            break;
656 c304f7e2 ths
        default:
657 c304f7e2 ths
            [NSApp sendEvent:event];
658 5b0753e0 bellard
    }
659 5b0753e0 bellard
}
660 5b0753e0 bellard
661 c304f7e2 ths
- (void) grabMouse
662 5b0753e0 bellard
{
663 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: grabMouse\n");
664 3b46e624 ths
665 c304f7e2 ths
    if (!isFullscreen) {
666 c304f7e2 ths
        if (qemu_name)
667 c304f7e2 ths
            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt to release Mouse)", qemu_name]];
668 c304f7e2 ths
        else
669 c304f7e2 ths
            [normalWindow setTitle:@"QEMU - (Press ctrl + alt to release Mouse)"];
670 c304f7e2 ths
    }
671 c304f7e2 ths
    [NSCursor hide];
672 c304f7e2 ths
    CGAssociateMouseAndMouseCursorPosition(FALSE);
673 c304f7e2 ths
    isMouseGrabed = TRUE; // while isMouseGrabed = TRUE, QemuCocoaApp sends all events to [cocoaView handleEvent:]
674 5b0753e0 bellard
}
675 3b46e624 ths
676 c304f7e2 ths
- (void) ungrabMouse
677 c304f7e2 ths
{
678 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaView: ungrabMouse\n");
679 3b46e624 ths
680 c304f7e2 ths
    if (!isFullscreen) {
681 c304f7e2 ths
        if (qemu_name)
682 c304f7e2 ths
            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s", qemu_name]];
683 c304f7e2 ths
        else
684 c304f7e2 ths
            [normalWindow setTitle:@"QEMU"];
685 c304f7e2 ths
    }
686 c304f7e2 ths
    [NSCursor unhide];
687 c304f7e2 ths
    CGAssociateMouseAndMouseCursorPosition(TRUE);
688 c304f7e2 ths
    isMouseGrabed = FALSE;
689 5b0753e0 bellard
}
690 5b0753e0 bellard
691 c304f7e2 ths
- (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled {isAbsoluteEnabled = tIsAbsoluteEnabled;}
692 c304f7e2 ths
- (BOOL) isMouseGrabed {return isMouseGrabed;}
693 c304f7e2 ths
- (BOOL) isAbsoluteEnabled {return isAbsoluteEnabled;}
694 c304f7e2 ths
- (float) cdx {return cdx;}
695 c304f7e2 ths
- (float) cdy {return cdy;}
696 c304f7e2 ths
- (QEMUScreen) gscreen {return screen;}
697 5b0753e0 bellard
@end
698 5b0753e0 bellard
699 5b0753e0 bellard
700 c304f7e2 ths
701 5b0753e0 bellard
/*
702 5b0753e0 bellard
 ------------------------------------------------------
703 c304f7e2 ths
    QemuCocoaAppController
704 5b0753e0 bellard
 ------------------------------------------------------
705 5b0753e0 bellard
*/
706 c304f7e2 ths
@interface QemuCocoaAppController : NSObject
707 5b0753e0 bellard
{
708 5b0753e0 bellard
}
709 5b0753e0 bellard
- (void)startEmulationWithArgc:(int)argc argv:(char**)argv;
710 c304f7e2 ths
- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
711 c304f7e2 ths
- (void)toggleFullScreen:(id)sender;
712 c304f7e2 ths
- (void)showQEMUDoc:(id)sender;
713 c304f7e2 ths
- (void)showQEMUTec:(id)sender;
714 5b0753e0 bellard
@end
715 5b0753e0 bellard
716 c304f7e2 ths
@implementation QemuCocoaAppController
717 c304f7e2 ths
- (id) init
718 5b0753e0 bellard
{
719 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: init\n");
720 5a246934 bellard
721 c304f7e2 ths
    self = [super init];
722 c304f7e2 ths
    if (self) {
723 5a246934 bellard
724 c304f7e2 ths
        // create a view and add it to the window
725 c304f7e2 ths
        cocoaView = [[QemuCocoaView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 640.0, 480.0)];
726 c304f7e2 ths
        if(!cocoaView) {
727 c304f7e2 ths
            fprintf(stderr, "(cocoa) can't create a view\n");
728 c304f7e2 ths
            exit(1);
729 c304f7e2 ths
        }
730 3b46e624 ths
731 c304f7e2 ths
        // create a window
732 c304f7e2 ths
        normalWindow = [[NSWindow alloc] initWithContentRect:[cocoaView frame]
733 c304f7e2 ths
            styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask
734 c304f7e2 ths
            backing:NSBackingStoreBuffered defer:NO];
735 c304f7e2 ths
        if(!normalWindow) {
736 c304f7e2 ths
            fprintf(stderr, "(cocoa) can't create window\n");
737 c304f7e2 ths
            exit(1);
738 c304f7e2 ths
        }
739 c304f7e2 ths
        [normalWindow setAcceptsMouseMovedEvents:YES];
740 c304f7e2 ths
        [normalWindow setTitle:[NSString stringWithFormat:@"QEMU"]];
741 c304f7e2 ths
        [normalWindow setContentView:cocoaView];
742 c304f7e2 ths
        [normalWindow makeKeyAndOrderFront:self];
743 3b46e624 ths
744 c304f7e2 ths
    }
745 c304f7e2 ths
    return self;
746 c304f7e2 ths
}
747 3b46e624 ths
748 c304f7e2 ths
- (void) dealloc
749 c304f7e2 ths
{
750 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
751 c304f7e2 ths
752 c304f7e2 ths
    if (cocoaView)
753 c304f7e2 ths
        [cocoaView release];
754 c304f7e2 ths
    [super dealloc];
755 c304f7e2 ths
}
756 3b46e624 ths
757 c304f7e2 ths
- (void)applicationDidFinishLaunching: (NSNotification *) note
758 c304f7e2 ths
{
759 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
760 c304f7e2 ths
761 c304f7e2 ths
    // Display an open dialog box if no argument were passed or
762 c304f7e2 ths
    // if qemu was launched from the finder ( the Finder passes "-psn" )
763 c304f7e2 ths
    if( gArgc <= 1 || strncmp ((char *)gArgv[1], "-psn", 4) == 0) {
764 c304f7e2 ths
        NSOpenPanel *op = [[NSOpenPanel alloc] init];
765 c304f7e2 ths
        [op setPrompt:@"Boot image"];
766 c304f7e2 ths
        [op setMessage:@"Select the disk image you want to boot.\n\nHit the \"Cancel\" button to quit"];
767 7a674b13 bellard
        [op beginSheetForDirectory:nil file:nil types:[NSArray arrayWithObjects:@"img",@"iso",@"dmg",@"qcow",@"cow",@"cloop",@"vmdk",nil]
768 c304f7e2 ths
              modalForWindow:normalWindow modalDelegate:self
769 5b0753e0 bellard
              didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
770 c304f7e2 ths
    } else {
771 c304f7e2 ths
        // or Launch Qemu, with the global args
772 c304f7e2 ths
        [self startEmulationWithArgc:gArgc argv:(char **)gArgv];
773 5a246934 bellard
    }
774 5b0753e0 bellard
}
775 5b0753e0 bellard
776 5b0753e0 bellard
- (void)applicationWillTerminate:(NSNotification *)aNotification
777 5b0753e0 bellard
{
778 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: applicationWillTerminate\n");
779 c304f7e2 ths
780 5b0753e0 bellard
    qemu_system_shutdown_request();
781 5b0753e0 bellard
    exit(0);
782 5b0753e0 bellard
}
783 5b0753e0 bellard
784 c304f7e2 ths
- (void)startEmulationWithArgc:(int)argc argv:(char**)argv
785 c304f7e2 ths
{
786 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
787 c304f7e2 ths
788 c304f7e2 ths
    int status;
789 c304f7e2 ths
    status = qemu_main(argc, argv);
790 c304f7e2 ths
    exit(status);
791 c304f7e2 ths
}
792 c304f7e2 ths
793 5b0753e0 bellard
- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
794 5b0753e0 bellard
{
795 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: openPanelDidEnd\n");
796 3b46e624 ths
797 c304f7e2 ths
    if(returnCode == NSCancelButton) {
798 c304f7e2 ths
        exit(0);
799 c304f7e2 ths
    } else if(returnCode == NSOKButton) {
800 5b0753e0 bellard
        char *bin = "qemu";
801 c304f7e2 ths
        char *img = (char*)[ [ sheet filename ] cStringUsingEncoding:NSASCIIStringEncoding];
802 3b46e624 ths
803 5b0753e0 bellard
        char **argv = (char**)malloc( sizeof(char*)*3 );
804 3b46e624 ths
805 5b0753e0 bellard
        asprintf(&argv[0], "%s", bin);
806 5b0753e0 bellard
        asprintf(&argv[1], "-hda");
807 5b0753e0 bellard
        asprintf(&argv[2], "%s", img);
808 3b46e624 ths
809 5b0753e0 bellard
        printf("Using argc %d argv %s -hda %s\n", 3, bin, img);
810 3b46e624 ths
811 5b0753e0 bellard
        [self startEmulationWithArgc:3 argv:(char**)argv];
812 5b0753e0 bellard
    }
813 5b0753e0 bellard
}
814 c304f7e2 ths
- (void)toggleFullScreen:(id)sender
815 c304f7e2 ths
{
816 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: toggleFullScreen\n");
817 c304f7e2 ths
818 c304f7e2 ths
    [cocoaView toggleFullScreen:sender];
819 c304f7e2 ths
}
820 5b0753e0 bellard
821 c304f7e2 ths
- (void)showQEMUDoc:(id)sender
822 5b0753e0 bellard
{
823 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: showQEMUDoc\n");
824 c304f7e2 ths
825 c304f7e2 ths
    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-doc.html",
826 c304f7e2 ths
        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
827 c304f7e2 ths
}
828 c304f7e2 ths
829 c304f7e2 ths
- (void)showQEMUTec:(id)sender
830 c304f7e2 ths
{
831 c304f7e2 ths
    COCOA_DEBUG("QemuCocoaAppController: showQEMUTec\n");
832 c304f7e2 ths
833 c304f7e2 ths
    [[NSWorkspace sharedWorkspace] openFile:[NSString stringWithFormat:@"%@/../doc/qemu/qemu-tech.html",
834 c304f7e2 ths
        [[NSBundle mainBundle] resourcePath]] withApplication:@"Help Viewer"];
835 5b0753e0 bellard
}
836 5b0753e0 bellard
@end
837 5b0753e0 bellard
838 5b0753e0 bellard
839 c304f7e2 ths
840 c304f7e2 ths
// Dock Connection
841 5b0753e0 bellard
typedef struct CPSProcessSerNum
842 5b0753e0 bellard
{
843 5b0753e0 bellard
        UInt32                lo;
844 5b0753e0 bellard
        UInt32                hi;
845 5b0753e0 bellard
} CPSProcessSerNum;
846 5b0753e0 bellard
847 5b0753e0 bellard
extern OSErr    CPSGetCurrentProcess( CPSProcessSerNum *psn);
848 5b0753e0 bellard
extern OSErr    CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
849 5b0753e0 bellard
extern OSErr    CPSSetFrontProcess( CPSProcessSerNum *psn);
850 5b0753e0 bellard
851 c304f7e2 ths
int main (int argc, const char * argv[]) {
852 5b0753e0 bellard
853 c304f7e2 ths
    gArgc = argc;
854 c304f7e2 ths
    gArgv = (char **)argv;
855 c304f7e2 ths
    CPSProcessSerNum PSN;
856 5b0753e0 bellard
857 c304f7e2 ths
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
858 c304f7e2 ths
    [NSApplication sharedApplication];
859 5b0753e0 bellard
860 c304f7e2 ths
    if (!CPSGetCurrentProcess(&PSN))
861 c304f7e2 ths
        if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
862 c304f7e2 ths
            if (!CPSSetFrontProcess(&PSN))
863 c304f7e2 ths
                [NSApplication sharedApplication];
864 5b0753e0 bellard
865 c304f7e2 ths
    // Add menus
866 c304f7e2 ths
    NSMenu      *menu;
867 c304f7e2 ths
    NSMenuItem  *menuItem;
868 5b0753e0 bellard
869 c304f7e2 ths
    [NSApp setMainMenu:[[NSMenu alloc] init]];
870 5b0753e0 bellard
871 c304f7e2 ths
    // Application menu
872 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@""];
873 c304f7e2 ths
    [menu addItemWithTitle:@"About QEMU" action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; // About QEMU
874 c304f7e2 ths
    [menu addItem:[NSMenuItem separatorItem]]; //Separator
875 c304f7e2 ths
    [menu addItemWithTitle:@"Hide QEMU" action:@selector(hide:) keyEquivalent:@"h"]; //Hide QEMU
876 c304f7e2 ths
    menuItem = (NSMenuItem *)[menu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; // Hide Others
877 c304f7e2 ths
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
878 c304f7e2 ths
    [menu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; // Show All
879 c304f7e2 ths
    [menu addItem:[NSMenuItem separatorItem]]; //Separator
880 c304f7e2 ths
    [menu addItemWithTitle:@"Quit QEMU" action:@selector(terminate:) keyEquivalent:@"q"];
881 c304f7e2 ths
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" action:nil keyEquivalent:@""];
882 c304f7e2 ths
    [menuItem setSubmenu:menu];
883 c304f7e2 ths
    [[NSApp mainMenu] addItem:menuItem];
884 c304f7e2 ths
    [NSApp performSelector:@selector(setAppleMenu:) withObject:menu]; // Workaround (this method is private since 10.4+)
885 3b46e624 ths
886 c304f7e2 ths
    // View menu
887 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@"View"];
888 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Enter Fullscreen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"] autorelease]]; // Fullscreen
889 c304f7e2 ths
    menuItem = [[[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""] autorelease];
890 c304f7e2 ths
    [menuItem setSubmenu:menu];
891 5b0753e0 bellard
    [[NSApp mainMenu] addItem:menuItem];
892 5b0753e0 bellard
893 c304f7e2 ths
    // Window menu
894 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@"Window"];
895 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
896 c304f7e2 ths
    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
897 c304f7e2 ths
    [menuItem setSubmenu:menu];
898 c304f7e2 ths
    [[NSApp mainMenu] addItem:menuItem];
899 c304f7e2 ths
    [NSApp setWindowsMenu:menu];
900 c304f7e2 ths
901 c304f7e2 ths
    // Help menu
902 c304f7e2 ths
    menu = [[NSMenu alloc] initWithTitle:@"Help"];
903 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Documentation" action:@selector(showQEMUDoc:) keyEquivalent:@"?"] autorelease]]; // QEMU Help
904 c304f7e2 ths
    [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"QEMU Technology" action:@selector(showQEMUTec:) keyEquivalent:@""] autorelease]]; // QEMU Help
905 c304f7e2 ths
    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""] autorelease];
906 c304f7e2 ths
    [menuItem setSubmenu:menu];
907 c304f7e2 ths
    [[NSApp mainMenu] addItem:menuItem];
908 5b0753e0 bellard
909 c304f7e2 ths
    // Create an Application controller
910 c304f7e2 ths
    QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
911 c304f7e2 ths
    [NSApp setDelegate:appController];
912 5b0753e0 bellard
913 c304f7e2 ths
    // Start the main event loop
914 c304f7e2 ths
    [NSApp run];
915 5b0753e0 bellard
916 c304f7e2 ths
    [appController release];
917 c304f7e2 ths
    [pool release];
918 3b46e624 ths
919 c304f7e2 ths
    return 0;
920 c304f7e2 ths
}
921 3b46e624 ths
922 3b46e624 ths
923 5b0753e0 bellard
924 c304f7e2 ths
#pragma mark qemu
925 c304f7e2 ths
static void cocoa_update(DisplayState *ds, int x, int y, int w, int h)
926 c304f7e2 ths
{
927 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_update\n");
928 c304f7e2 ths
929 c304f7e2 ths
    NSRect rect;
930 c304f7e2 ths
    if ([cocoaView cdx] == 1.0) {
931 c304f7e2 ths
        rect = NSMakeRect(x, [cocoaView gscreen].height - y - h, w, h);
932 c304f7e2 ths
    } else {
933 c304f7e2 ths
        rect = NSMakeRect(
934 c304f7e2 ths
            x * [cocoaView cdx],
935 c304f7e2 ths
            ([cocoaView gscreen].height - y - h) * [cocoaView cdy],
936 c304f7e2 ths
            w * [cocoaView cdx],
937 c304f7e2 ths
            h * [cocoaView cdy]);
938 c304f7e2 ths
    }
939 c304f7e2 ths
    [cocoaView displayRect:rect];
940 5b0753e0 bellard
}
941 5b0753e0 bellard
942 c304f7e2 ths
static void cocoa_resize(DisplayState *ds, int w, int h)
943 5b0753e0 bellard
{
944 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_resize\n");
945 3b46e624 ths
946 c304f7e2 ths
    [cocoaView resizeContentToWidth:w height:h displayState:ds];
947 c304f7e2 ths
}
948 3b46e624 ths
949 c304f7e2 ths
static void cocoa_refresh(DisplayState *ds)
950 c304f7e2 ths
{
951 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_refresh\n");
952 3b46e624 ths
953 c304f7e2 ths
    if (kbd_mouse_is_absolute()) {
954 c304f7e2 ths
        if (![cocoaView isAbsoluteEnabled]) {
955 c304f7e2 ths
            if ([cocoaView isMouseGrabed]) {
956 c304f7e2 ths
                [cocoaView ungrabMouse];
957 c304f7e2 ths
            }
958 c304f7e2 ths
        }
959 c304f7e2 ths
        [cocoaView setAbsoluteEnabled:YES];
960 c304f7e2 ths
    }
961 5b0753e0 bellard
962 c304f7e2 ths
    NSDate *distantPast;
963 c304f7e2 ths
    NSEvent *event;
964 c304f7e2 ths
    distantPast = [NSDate distantPast];
965 c304f7e2 ths
    do {
966 c304f7e2 ths
        event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
967 c304f7e2 ths
                        inMode: NSDefaultRunLoopMode dequeue:YES];
968 c304f7e2 ths
        if (event != nil) {
969 c304f7e2 ths
            [cocoaView handleEvent:event];
970 c304f7e2 ths
        }
971 c304f7e2 ths
    } while(event != nil);
972 c304f7e2 ths
    vga_hw_update();
973 c304f7e2 ths
}
974 3b46e624 ths
975 c304f7e2 ths
static void cocoa_cleanup(void)
976 c304f7e2 ths
{
977 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
978 3b46e624 ths
979 5b0753e0 bellard
}
980 5b0753e0 bellard
981 c304f7e2 ths
void cocoa_display_init(DisplayState *ds, int full_screen)
982 5b0753e0 bellard
{
983 c304f7e2 ths
    COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
984 c304f7e2 ths
985 c304f7e2 ths
    // register vga outpu callbacks
986 c304f7e2 ths
    ds->dpy_update = cocoa_update;
987 c304f7e2 ths
    ds->dpy_resize = cocoa_resize;
988 c304f7e2 ths
    ds->dpy_refresh = cocoa_refresh;
989 cae41b10 bellard
990 c304f7e2 ths
    // give window a initial Size
991 c304f7e2 ths
    cocoa_resize(ds, 640, 400);
992 cae41b10 bellard
993 c304f7e2 ths
    // register cleanup function
994 c304f7e2 ths
    atexit(cocoa_cleanup);
995 5b0753e0 bellard
}