Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
296 lines (249 loc) · 7.91 KB

SDL_x11mouse.c

File metadata and controls

296 lines (249 loc) · 7.91 KB
 
Jul 26, 2006
Jul 26, 2006
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
Jul 26, 2006
Jul 26, 2006
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
Mar 11, 2011
Mar 11, 2011
23
24
#include "SDL_assert.h"
Jul 26, 2006
Jul 26, 2006
25
#include "SDL_x11video.h"
Jan 1, 2009
Jan 1, 2009
26
#include "SDL_x11mouse.h"
Jul 26, 2006
Jul 26, 2006
27
28
#include "../../events/SDL_mouse_c.h"
Mar 11, 2011
Mar 11, 2011
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* FIXME: Find a better place to put this... */
static Cursor x11_empty_cursor = None;
static Display *
GetDisplay(void)
{
return ((SDL_VideoData *)SDL_GetVideoDevice()->driverdata)->display;
}
static Cursor
X11_CreateEmptyCursor()
{
if (x11_empty_cursor == None) {
Display *display = GetDisplay();
char data[1];
XColor color;
Pixmap pixmap;
SDL_zero(data);
color.red = color.green = color.blue = 0;
pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
data, 1, 1);
if (pixmap) {
x11_empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap,
&color, &color, 0, 0);
XFreePixmap(display, pixmap);
}
}
return x11_empty_cursor;
}
static void
X11_DestroyEmptyCursor(void)
{
if (x11_empty_cursor != None) {
XFreeCursor(GetDisplay(), x11_empty_cursor);
x11_empty_cursor = None;
}
}
static SDL_Cursor *
X11_CreateDefaultCursor()
{
SDL_Cursor *cursor;
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
/* None is used to indicate the default cursor */
cursor->driverdata = (void*)None;
} else {
SDL_OutOfMemory();
}
return cursor;
}
Mar 11, 2011
Mar 11, 2011
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#if SDL_VIDEO_DRIVER_X11_XCURSOR
static Cursor
X11_CreateXCursorCursor(SDL_Surface * surface, int hot_x, int hot_y)
{
Display *display = GetDisplay();
Cursor cursor = None;
XcursorImage *image;
image = XcursorImageCreate(surface->w, surface->h);
if (!image) {
SDL_OutOfMemory();
return None;
}
image->xhot = hot_x;
image->yhot = hot_y;
image->delay = 0;
SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
SDL_assert(surface->pitch == surface->w * 4);
SDL_memcpy(image->pixels, surface->pixels, surface->h * surface->pitch);
cursor = XcursorImageLoadCursor(display, image);
XcursorImageDestroy(image);
return cursor;
}
#endif /* SDL_VIDEO_DRIVER_X11_XCURSOR */
Mar 11, 2011
Mar 11, 2011
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
static Cursor
X11_CreatePixmapCursor(SDL_Surface * surface, int hot_x, int hot_y)
{
Display *display = GetDisplay();
XColor fg, bg;
Cursor cursor = None;
Uint32 *ptr;
Uint8 *data_bits, *mask_bits;
Pixmap data_pixmap, mask_pixmap;
int x, y;
unsigned int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
data_bits = SDL_calloc(1, surface->h * width_bytes);
mask_bits = SDL_calloc(1, surface->h * width_bytes);
if (!data_bits || !mask_bits) {
SDL_OutOfMemory();
return None;
}
Mar 11, 2011
Mar 11, 2011
135
136
137
/* Code below assumes ARGB pixel format */
SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
Mar 11, 2011
Mar 11, 2011
138
139
140
141
142
143
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
190
191
192
193
194
195
196
197
198
rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
for (y = 0; y < surface->h; ++y) {
ptr = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch);
for (x = 0; x < surface->w; ++x) {
int alpha = (*ptr >> 24) & 0xff;
int red = (*ptr >> 16) & 0xff;
int green = (*ptr >> 8) & 0xff;
int blue = (*ptr >> 0) & 0xff;
if (alpha > 25) {
mask_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
if ((red + green + blue) > 0x40) {
fgBits++;
rfg += red;
gfg += green;
bfg += blue;
data_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
} else {
bgBits++;
rbg += red;
gbg += green;
bbg += blue;
}
}
++ptr;
}
}
if (fgBits) {
fg.red = rfg * 257 / fgBits;
fg.green = gfg * 257 / fgBits;
fg.blue = bfg * 257 / fgBits;
}
else fg.red = fg.green = fg.blue = 0;
if (bgBits) {
bg.red = rbg * 257 / bgBits;
bg.green = gbg * 257 / bgBits;
bg.blue = bbg * 257 / bgBits;
}
else bg.red = bg.green = bg.blue = 0;
data_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
data_bits, surface->w, surface->h);
mask_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
mask_bits, surface->w, surface->h);
cursor = XCreatePixmapCursor(display, data_pixmap, mask_pixmap,
&fg, &bg, hot_x, hot_y);
XFreePixmap(display, data_pixmap);
XFreePixmap(display, mask_pixmap);
return cursor;
}
static SDL_Cursor *
X11_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
{
SDL_Cursor *cursor;
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
Mar 11, 2011
Mar 11, 2011
199
200
201
202
203
204
205
206
207
208
209
Cursor x11_cursor = None;
#if SDL_VIDEO_DRIVER_X11_XCURSOR
if (SDL_X11_HAVE_XCURSOR) {
x11_cursor = X11_CreateXCursorCursor(surface, hot_x, hot_y);
}
#endif
if (x11_cursor == None) {
x11_cursor = X11_CreatePixmapCursor(surface, hot_x, hot_y);
}
cursor->driverdata = (void*)x11_cursor;
Mar 11, 2011
Mar 11, 2011
210
211
212
213
214
215
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
} else {
SDL_OutOfMemory();
}
return cursor;
}
static void
X11_FreeCursor(SDL_Cursor * cursor)
{
Cursor x11_cursor = (Cursor)cursor->driverdata;
if (x11_cursor != None) {
XFreeCursor(GetDisplay(), x11_cursor);
}
SDL_free(cursor);
}
static int
X11_ShowCursor(SDL_Cursor * cursor)
{
Cursor x11_cursor = 0;
if (cursor) {
x11_cursor = (Cursor)cursor->driverdata;
} else {
x11_cursor = X11_CreateEmptyCursor();
}
/* FIXME: Is there a better way than this? */
{
SDL_VideoDevice *video = SDL_GetVideoDevice();
Display *display = GetDisplay();
SDL_Window *window;
SDL_WindowData *data;
for (window = video->windows; window; window = window->next) {
data = (SDL_WindowData *)window->driverdata;
if (x11_cursor != None) {
XDefineCursor(display, data->xwindow, x11_cursor);
} else {
XUndefineCursor(display, data->xwindow);
}
}
XFlush(display);
}
return 0;
}
static void
X11_WarpMouse(SDL_Window * window, int x, int y)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display = data->videodata->display;
XWarpPointer(display, None, data->xwindow, 0, 0, 0, 0, x, y);
XSync(display, False);
}
static int
X11_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_Unsupported();
return -1;
}
Jul 26, 2006
Jul 26, 2006
276
277
278
void
X11_InitMouse(_THIS)
{
Mar 11, 2011
Mar 11, 2011
279
280
281
282
283
284
285
286
287
SDL_Mouse *mouse = SDL_GetMouse();
mouse->CreateCursor = X11_CreateCursor;
mouse->ShowCursor = X11_ShowCursor;
mouse->FreeCursor = X11_FreeCursor;
mouse->WarpMouse = X11_WarpMouse;
mouse->SetRelativeMouseMode = X11_SetRelativeMouseMode;
SDL_SetDefaultCursor(X11_CreateDefaultCursor());
Jul 26, 2006
Jul 26, 2006
288
289
290
291
292
}
void
X11_QuitMouse(_THIS)
{
Mar 11, 2011
Mar 11, 2011
293
X11_DestroyEmptyCursor();
Jul 26, 2006
Jul 26, 2006
294
295
296
}
/* vi: set ts=4 sw=4 expandtab: */