Debian Codegraph @

src/x/xcursor.c

raw
1 #include <X11/Xlib.h>
2 #include "allegro5/allegro.h"
3 #include "allegro5/internal/aintern_bitmap.h"
4 #include "allegro5/internal/aintern_x.h"
5 #include "allegro5/internal/aintern_xcursor.h"
6 #include "allegro5/internal/aintern_xdisplay.h"
7 #include "allegro5/internal/aintern_xsystem.h"
8
9 #ifdef ALLEGRO_RASPBERRYPI
10 #include "allegro5/internal/aintern_raspberrypi.h"
11 #define ALLEGRO_SYSTEM_XGLX ALLEGRO_SYSTEM_RASPBERRYPI
12 #define ALLEGRO_DISPLAY_XGLX ALLEGRO_DISPLAY_RASPBERRYPI
13 #endif
14
15 #include <X11/cursorfont.h>
16
17 #ifdef ALLEGRO_XWINDOWS_WITH_XCURSOR
18 #include <X11/Xcursor/Xcursor.h>
19 #else
20 /* This requirement could be lifted for compatibility with older systems at the
21 * expense of functionality, but it's probably not worthwhile.
22 */
23 #error This file requires Xcursor.
24 #endif
25
26 ALLEGRO_MOUSE_CURSOR *_al_xwin_create_mouse_cursor(ALLEGRO_BITMAP *bmp,
27 int x_focus, int y_focus)
28 {
29 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
30 Display *xdisplay = system->x11display;
31
32 int bmp_w;
33 int bmp_h;
34 ALLEGRO_MOUSE_CURSOR_XWIN *xcursor;
35 XcursorImage *image;
36 int c, ix, iy;
37 bool was_locked;
38
39 bmp_w = al_get_bitmap_width(bmp);
40 bmp_h = al_get_bitmap_height(bmp);
41
42 xcursor = al_malloc(sizeof *xcursor);
43 if (!xcursor) {
44 return NULL;
45 }
46
47 image = XcursorImageCreate(bmp->w, bmp->h);
48 if (image == None) {
49 al_free(xcursor);
50 return NULL;
51 }
52
53 was_locked = al_is_bitmap_locked(bmp);
54 if (!was_locked) {
55 al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
56 }
57
58 c = 0;
59 for (iy = 0; iy < bmp_h; iy++) {
60 for (ix = 0; ix < bmp_w; ix++) {
61 ALLEGRO_COLOR col;
62 unsigned char r, g, b, a;
63
64 col = al_get_pixel(bmp, ix, iy);
65 al_unmap_rgba(col, &r, &g, &b, &a);
66 image->pixels[c++] = (a<<24) | (r<<16) | (g<<8) | (b);
67 }
68 }
69
70 if (!was_locked) {
71 al_unlock_bitmap(bmp);
72 }
73
74 image->xhot = x_focus;
75 image->yhot = y_focus;
76
77 _al_mutex_lock(&system->lock);
78 xcursor->cursor = XcursorImageLoadCursor(xdisplay, image);
79 _al_mutex_unlock(&system->lock);
80
81 XcursorImageDestroy(image);
82
83 return (ALLEGRO_MOUSE_CURSOR *)xcursor;
84 }
85
86
87
88 void _al_xwin_destroy_mouse_cursor(ALLEGRO_MOUSE_CURSOR *cursor)
89 {
90 ALLEGRO_MOUSE_CURSOR_XWIN *xcursor = (ALLEGRO_MOUSE_CURSOR_XWIN *)cursor;
91 ALLEGRO_SYSTEM *sys = al_get_system_driver();
92 ALLEGRO_SYSTEM_XGLX *sysx = (ALLEGRO_SYSTEM_XGLX *)sys;
93 unsigned i;
94
95 _al_mutex_lock(&sysx->lock);
96
97 for (i = 0; i < _al_vector_size(&sys->displays); i++) {
98 ALLEGRO_DISPLAY_XGLX **slot = _al_vector_ref(&sys->displays, i);
99 ALLEGRO_DISPLAY_XGLX *glx = *slot;
100
101 if (glx->current_cursor == xcursor->cursor) {
102 if (!glx->cursor_hidden)
103 XUndefineCursor(sysx->x11display, glx->window);
104 glx->current_cursor = None;
105 glx->is_system_cursor = true;
106 }
107 }
108
109 XFreeCursor(sysx->x11display, xcursor->cursor);
110 al_free(xcursor);
111
112 _al_mutex_unlock(&sysx->lock);
113 }
114
115
116
117 static bool xdpy_set_mouse_cursor(ALLEGRO_DISPLAY *display,
118 ALLEGRO_MOUSE_CURSOR *cursor)
119 {
120 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
121 ALLEGRO_MOUSE_CURSOR_XWIN *xcursor = (ALLEGRO_MOUSE_CURSOR_XWIN *)cursor;
122 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
123 Display *xdisplay = system->x11display;
124 Window xwindow = glx->window;
125
126 _al_mutex_lock(&system->lock);
127 if (glx->is_system_cursor && glx->current_cursor) {
128 XFreeCursor(xdisplay, glx->current_cursor);
129 }
130
131 glx->current_cursor = xcursor->cursor;
132 glx->is_system_cursor = false;
133
134 if (!glx->cursor_hidden) {
135 XDefineCursor(xdisplay, xwindow, glx->current_cursor);
136 }
137 _al_mutex_unlock(&system->lock);
138
139 return true;
140 }
141
142
143
144 static bool xdpy_set_system_mouse_cursor(ALLEGRO_DISPLAY *display,
145 ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id)
146 {
147 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
148 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
149 Display *xdisplay = system->x11display;
150 Window xwindow = glx->window;
151 unsigned int cursor_shape;
152
153 switch (cursor_id) {
154 case ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT:
155 case ALLEGRO_SYSTEM_MOUSE_CURSOR_ARROW:
156 case ALLEGRO_SYSTEM_MOUSE_CURSOR_PROGRESS:
157 cursor_shape = XC_left_ptr;
158 break;
159 case ALLEGRO_SYSTEM_MOUSE_CURSOR_BUSY:
160 cursor_shape = XC_watch;
161 break;
162 case ALLEGRO_SYSTEM_MOUSE_CURSOR_QUESTION:
163 cursor_shape = XC_question_arrow;
164 break;
165 case ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT:
166 cursor_shape = XC_xterm;
167 break;
168 case ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE:
169 cursor_shape = XC_fleur;
170 break;
171 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N:
172 cursor_shape = XC_top_side;
173 break;
174 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_S:
175 cursor_shape = XC_bottom_side;
176 break;
177 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E:
178 cursor_shape = XC_right_side;
179 break;
180 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_W:
181 cursor_shape = XC_left_side;
182 break;
183 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE:
184 cursor_shape = XC_top_right_corner;
185 break;
186 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SW:
187 cursor_shape = XC_bottom_left_corner;
188 break;
189 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW:
190 cursor_shape = XC_top_left_corner;
191 break;
192 case ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SE:
193 cursor_shape = XC_bottom_right_corner;
194 break;
195 case ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION:
196 cursor_shape = XC_crosshair;
197 break;
198 case ALLEGRO_SYSTEM_MOUSE_CURSOR_LINK:
199 cursor_shape = XC_hand2;
200 break;
201 case ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT:
202 cursor_shape = XC_hand1;
203 break;
204 case ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE:
205 cursor_shape = XC_X_cursor;
206 break;
207 default:
208 return false;
209 }
210
211 _al_mutex_lock(&system->lock);
212
213 if (glx->is_system_cursor && glx->current_cursor) {
214 XFreeCursor(xdisplay, glx->current_cursor);
215 }
216 glx->current_cursor = XCreateFontCursor(xdisplay, cursor_shape);
217 glx->is_system_cursor = true;
218
219 if (!glx->cursor_hidden) {
220 XDefineCursor(xdisplay, xwindow, glx->current_cursor);
221 }
222
223 _al_mutex_unlock(&system->lock);
224
225 return true;
226 }
227
228
229
230 /* Show the system mouse cursor. */
231 static bool xdpy_show_mouse_cursor(ALLEGRO_DISPLAY *display)
232 {
233 ALLEGRO_DISPLAY_XGLX *glx = (void *)display;
234 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
235 Display *xdisplay = system->x11display;
236 Window xwindow = glx->window;
237
238 if (!glx->cursor_hidden)
239 return true;
240
241 _al_mutex_lock(&system->lock);
242 XDefineCursor(xdisplay, xwindow, glx->current_cursor);
243 glx->cursor_hidden = false;
244 _al_mutex_unlock(&system->lock);
245 return true;
246 }
247
248
249
250 /* Hide the system mouse cursor. */
251 static bool xdpy_hide_mouse_cursor(ALLEGRO_DISPLAY *display)
252 {
253 ALLEGRO_DISPLAY_XGLX *glx = (void *)display;
254 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
255 Display *xdisplay = system->x11display;
256 Window xwindow = glx->window;
257
258 if (glx->cursor_hidden)
259 return true;
260
261 _al_mutex_lock(&system->lock);
262
263 if (glx->invisible_cursor == None) {
264 unsigned long gcmask;
265 XGCValues gcvalues;
266
267 Pixmap pixmap = XCreatePixmap(xdisplay, xwindow, 1, 1, 1);
268
269 GC temp_gc;
270 XColor color;
271
272 gcmask = GCFunction | GCForeground | GCBackground;
273 gcvalues.function = GXcopy;
274 gcvalues.foreground = 0;
275 gcvalues.background = 0;
276 temp_gc = XCreateGC(xdisplay, pixmap, gcmask, &gcvalues);
277 XDrawPoint(xdisplay, pixmap, temp_gc, 0, 0);
278 XFreeGC(xdisplay, temp_gc);
279 color.pixel = 0;
280 color.red = color.green = color.blue = 0;
281 color.flags = DoRed | DoGreen | DoBlue;
282 glx->invisible_cursor = XCreatePixmapCursor(xdisplay, pixmap,
283 pixmap, &color, &color, 0, 0);
284 XFreePixmap(xdisplay, pixmap);
285 }
286
287 XDefineCursor(xdisplay, xwindow, glx->invisible_cursor);
288 glx->cursor_hidden = true;
289
290 _al_mutex_unlock(&system->lock);
291
292 return true;
293 }
294
295
296
297 void _al_xwin_add_cursor_functions(ALLEGRO_DISPLAY_INTERFACE *vt)
298 {
299 vt->set_mouse_cursor = xdpy_set_mouse_cursor;
300 vt->set_system_mouse_cursor = xdpy_set_system_mouse_cursor;
301 vt->show_mouse_cursor = xdpy_show_mouse_cursor;
302 vt->hide_mouse_cursor = xdpy_hide_mouse_cursor;
303 }
304
305
306 /* vim: set sts=3 sw=3 et: */