/
SDL_QuartzWM.m
301 lines (238 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
20
slouken@libsdl.org
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
*/
struct WMcursor {
Cursor curs;
};
static void QZ_FreeWMCursor (_THIS, WMcursor *cursor) {
if ( cursor != NULL )
free (cursor);
}
/* Use the Carbon cursor routines for now */
static WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask,
int w, int h, int hot_x, int hot_y) {
WMcursor *cursor;
int row, bytes;
38
39
/* Allocate the cursor memory */
40
41
42
43
44
45
cursor = (WMcursor *)malloc(sizeof(WMcursor));
if ( cursor == NULL ) {
SDL_OutOfMemory();
return(NULL);
}
memset(cursor, 0, sizeof(*cursor));
46
47
48
49
50
51
52
53
54
55
if (w > 16)
w = 16;
if (h > 16)
h = 16;
bytes = (w+7)/8;
for ( row=0; row<h; ++row ) {
56
memcpy(&cursor->curs.data[row], data, bytes);
57
data += bytes;
58
}
59
for ( row=0; row<h; ++row ) {
60
memcpy(&cursor->curs.mask[row], mask, bytes);
61
mask += bytes;
62
63
64
65
}
cursor->curs.hotSpot.h = hot_x;
cursor->curs.hotSpot.v = hot_y;
66
return(cursor);
67
68
}
69
70
static int QZ_cursor_visible = 1;
71
72
73
static int QZ_ShowWMCursor (_THIS, WMcursor *cursor) {
if ( cursor == NULL) {
74
if ( QZ_cursor_visible ) {
75
HideCursor ();
76
QZ_cursor_visible = 0;
77
78
79
80
}
}
else {
SetCursor(&cursor->curs);
81
if ( ! QZ_cursor_visible ) {
82
ShowCursor ();
83
QZ_cursor_visible = 1;
84
85
86
87
88
89
}
}
return 1;
}
90
91
92
93
94
95
96
/**
* Coordinate conversion functions, for convenience
* Cocoa sets the origin at the lower left corner of the window/screen
* SDL, CoreGraphics/WindowServer, and QuickDraw use the origin at the upper left corner
* The routines were written so they could be called before SetVideoMode() has finished;
* this might have limited usefulness at the moment, but the extra cost is trivial.
**/
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Convert Cocoa screen coordinate to Cocoa window coordinate */
static void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p) {
*p = [ qz_window convertScreenToBase:*p ];
}
/* Convert Cocoa window coordinate to Cocoa screen coordinate */
static void QZ_PrivateLocalToGlobal (_THIS, NSPoint *p) {
*p = [ qz_window convertBaseToScreen:*p ];
}
/* Convert SDL coordinate to Cocoa coordinate */
static void QZ_PrivateSDLToCocoa (_THIS, NSPoint *p) {
int height;
115
116
117
118
119
if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */
height = CGDisplayPixelsHigh (display_id);
}
120
else {
121
122
123
124
125
126
height = NSHeight ( [ qz_window frame ] );
if ( [ qz_window styleMask ] & NSTitledWindowMask ) {
height -= 22;
}
127
}
128
129
p->y = height - p->y;
130
131
}
132
133
134
135
136
137
138
139
140
141
142
143
/* Convert Cocoa coordinate to SDL coordinate */
static void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) {
QZ_PrivateSDLToCocoa (this, p);
}
/* Convert SDL coordinate to window server (CoreGraphics) coordinate */
static CGPoint QZ_PrivateSDLToCG (_THIS, NSPoint *p) {
CGPoint cgp;
if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
int height;
QZ_PrivateSDLToCocoa (this, p);
QZ_PrivateLocalToGlobal (this, p);
height = CGDisplayPixelsHigh (display_id);
p->y = height - p->y;
}
cgp.x = p->x;
cgp.y = p->y;
return cgp;
}
/* Convert window server (CoreGraphics) coordinate to SDL coordinate */
static void QZ_PrivateCGToSDL (_THIS, NSPoint *p) {
if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
int height;
/* Convert CG Global to Cocoa Global */
height = CGDisplayPixelsHigh (display_id);
p->y = height - p->y;
QZ_PrivateGlobalToLocal (this, p);
QZ_PrivateCocoaToSDL (this, p);
}
}
static void QZ_PrivateWarpCursor (_THIS, int x, int y) {
NSPoint p;
CGPoint cgp;
p = NSMakePoint (x, y);
cgp = QZ_PrivateSDLToCG (this, &p);
CGDisplayMoveCursorToPoint (display_id, cgp);
warp_ticks = SDL_GetTicks();
warp_flag = 1;
}
static void QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y) {
190
191
192
193
194
/* Only allow warping when in foreground */
if ( ! inForeground )
return;
/* Do the actual warp */
195
QZ_PrivateWarpCursor (this, x, y);
196
197
198
199
200
201
202
}
static void QZ_MoveWMCursor (_THIS, int x, int y) { }
static void QZ_CheckMouseMode (_THIS) { }
static void QZ_SetCaption (_THIS, const char *title, const char *icon) {
203
if ( qz_window != nil ) {
204
205
206
NSString *string;
if ( title != NULL ) {
string = [ [ NSString alloc ] initWithCString:title ];
207
[ qz_window setTitle:string ];
208
209
210
211
[ string release ];
}
if ( icon != NULL ) {
string = [ [ NSString alloc ] initWithCString:icon ];
212
[ qz_window setMiniwindowTitle:string ];
213
214
215
[ string release ];
}
}
216
217
}
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
static void QZ_SetIcon (_THIS, SDL_Surface *icon, Uint8 *mask)
{
NSBitmapImageRep *imgrep;
NSImage *img;
SDL_Surface *mergedSurface;
Uint8 *surfPtr;
int i,j,masksize;
NSAutoreleasePool *pool;
SDL_Rect rrect;
NSSize imgSize = {icon->w, icon->h};
pool = [ [ NSAutoreleasePool alloc ] init ];
SDL_GetClipRect(icon, &rrect);
/* create a big endian RGBA surface */
mergedSurface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA,
icon->w, icon->h, 32, 0xff<<24, 0xff<<16, 0xff<<8, 0xff<<0);
if (mergedSurface==NULL) { NSLog(@"Error creating surface for
merge"); goto freePool; }
if (SDL_BlitSurface(icon,&rrect,mergedSurface,&rrect)) {
NSLog(@"Error blitting to mergedSurface");
goto freePool;
}
if (mask) {
masksize=icon->w*icon->h;
surfPtr = (Uint8 *)mergedSurface->pixels;
#define ALPHASHIFT 3
for (i=0;i<masksize;i+=8)
for (j=0;j<8;j++)
surfPtr[ALPHASHIFT+((i+j)<<2)]=(mask[i>>3]&(1<<(7-j)))?0xFF:0x00;
}
imgrep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:(unsigned char **)&mergedSurface->pixels
pixelsWide:icon->w pixelsHigh:icon->h bitsPerSample:8 samplesPerPixel:4
hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:icon->w<<2 bitsPerPixel:32];
img = [[NSImage alloc] initWithSize:imgSize];
[img addRepresentation: imgrep];
[NSApp setApplicationIconImage:img];
[img release];
[imgrep release];
SDL_FreeSurface(mergedSurface);
freePool:
[pool release];
260
261
262
263
264
}
static int QZ_IconifyWindow (_THIS) {
/* Bug! minimize erases the framebuffer */
265
266
if ( ! [ qz_window isMiniaturized ] ) {
[ qz_window miniaturize:nil ];
267
268
269
return 1;
}
else {
270
SDL_SetError ("qz_window already iconified");
271
272
273
274
275
276
return 0;
}
}
/*
static int QZ_GetWMInfo (_THIS, SDL_SysWMinfo *info) {
277
info->nsWindowPtr = qz_window;
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
return 0;
}*/
static SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode) {
switch (grab_mode) {
case SDL_GRAB_QUERY:
break;
case SDL_GRAB_OFF:
CGAssociateMouseAndMouseCursorPosition (1);
currentGrabMode = SDL_GRAB_OFF;
break;
case SDL_GRAB_ON:
QZ_WarpWMCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2);
CGAssociateMouseAndMouseCursorPosition (0);
currentGrabMode = SDL_GRAB_ON;
break;
case SDL_GRAB_FULLSCREEN:
296
297
298
299
300
301
break;
}
return currentGrabMode;
}