Debian Codegraph @

gdk/wayland/gdkcursor-wayland.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #include "config.h"
26
27 #define GDK_PIXBUF_ENABLE_BACKEND
28
29 #include <string.h>
30
31 #include "gdkprivate-wayland.h"
32 #include "gdkcursorprivate.h"
33 #include "gdkdisplay-wayland.h"
34 #include "gdkwayland.h"
35 #include <gdk-pixbuf/gdk-pixbuf.h>
36
37 #include <wayland-cursor.h>
38
39 #define GDK_TYPE_WAYLAND_CURSOR (_gdk_wayland_cursor_get_type ())
40 #define GDK_WAYLAND_CURSOR(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_WAYLAND_CURSOR, GdkWaylandCursor))
41 #define GDK_WAYLAND_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_WAYLAND_CURSOR, GdkWaylandCursorClass))
42 #define GDK_IS_WAYLAND_CURSOR(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_WAYLAND_CURSOR))
43 #define GDK_IS_WAYLAND_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_WAYLAND_CURSOR))
44 #define GDK_WAYLAND_CURSOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_WAYLAND_CURSOR, GdkWaylandCursorClass))
45
46 typedef struct _GdkWaylandCursor GdkWaylandCursor;
47 typedef struct _GdkWaylandCursorClass GdkWaylandCursorClass;
48
49 struct _GdkWaylandCursor
50 {
51 GdkCursor cursor;
52 gchar *name;
53
54 struct
55 {
56 int hotspot_x, hotspot_y;
57 int width, height, scale;
58 cairo_surface_t *cairo_surface;
59 } surface;
60
61 struct wl_cursor *wl_cursor;
62 int scale;
63 };
64
65 struct _GdkWaylandCursorClass
66 {
67 GdkCursorClass cursor_class;
68 };
69
70 GType _gdk_wayland_cursor_get_type (void);
71
72 G_DEFINE_TYPE (GdkWaylandCursor, _gdk_wayland_cursor, GDK_TYPE_CURSOR)
73
74 void
75 _gdk_wayland_display_init_cursors (GdkWaylandDisplay *display)
76 {
77 display->cursor_cache = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
78 }
79
80 void
81 _gdk_wayland_display_finalize_cursors (GdkWaylandDisplay *display)
82 {
83 g_hash_table_destroy (display->cursor_cache);
84 }
85
86 static const struct {
87 const gchar *css_name, *traditional_name;
88 } name_map[] = {
89 { "default", "left_ptr" },
90 { "help", "left_ptr" },
91 { "context-menu", "left_ptr" },
92 { "pointer", "hand" },
93 { "progress", "left_ptr_watch" },
94 { "wait", "watch" },
95 { "cell", "crosshair" },
96 { "crosshair", "cross" },
97 { "text", "xterm" },
98 { "vertical-text","xterm" },
99 { "alias", "dnd-link" },
100 { "copy", "dnd-copy" },
101 { "move", "dnd-move" },
102 { "no-drop", "dnd-none" },
103 { "dnd-ask", "dnd-copy" }, /* not CSS, but we want to guarantee it anyway */
104 { "dnd-move", "default" },
105 { "not-allowed", "crossed_circle" },
106 { "grab", "hand2" },
107 { "grabbing", "hand2" },
108 { "all-scroll", "left_ptr" },
109 { "col-resize", "h_double_arrow" },
110 { "row-resize", "v_double_arrow" },
111 { "n-resize", "top_side" },
112 { "e-resize", "right_side" },
113 { "s-resize", "bottom_side" },
114 { "w-resize", "left_side" },
115 { "ne-resize", "top_right_corner" },
116 { "nw-resize", "top_left_corner" },
117 { "se-resize", "bottom_right_corner" },
118 { "sw-resize", "bottom_left_corner" },
119 { "ew-resize", "h_double_arrow" },
120 { "ns-resize", "v_double_arrow" },
121 { "nesw-resize", "fd_double_arrow" },
122 { "nwse-resize", "bd_double_arrow" },
123 { "zoom-in", "left_ptr" },
124 { "zoom-out", "left_ptr" },
125 { "all-resize", "move" }, /* not CSS, but we want to guarantee it anyway */
126 { NULL, NULL }
127 };
128
129 static const gchar *
130 name_fallback (const gchar *name)
131 {
132 gint i;
133
134 for (i = 0; name_map[i].css_name; i++)
135 {
136 if (g_str_equal (name_map[i].css_name, name))
137 return name_map[i].traditional_name;
138 }
139
140 return NULL;
141 }
142
143 static gboolean
144 _gdk_wayland_cursor_update (GdkWaylandDisplay *display_wayland,
145 GdkWaylandCursor *cursor)
146 {
147 struct wl_cursor *c;
148 struct wl_cursor_theme *theme;
149
150 /* Do nothing if this is not a wl_cursor cursor. */
151 if (cursor->name == NULL)
152 return FALSE;
153
154 theme = _gdk_wayland_display_get_scaled_cursor_theme (display_wayland,
155 cursor->scale);
156 c = wl_cursor_theme_get_cursor (theme, cursor->name);
157 if (!c)
158 {
159 const char *fallback;
160
161 fallback = name_fallback (cursor->name);
162 if (fallback)
163 {
164 c = wl_cursor_theme_get_cursor (theme, name_fallback (cursor->name));
165 if (!c)
166 c = wl_cursor_theme_get_cursor (theme, "default");
167 }
168 }
169
170 if (!c)
171 {
172 g_message ("Unable to load %s from the cursor theme", cursor->name);
173 return FALSE;
174 }
175
176 cursor->wl_cursor = c;
177
178 return TRUE;
179 }
180
181 void
182 _gdk_wayland_display_update_cursors (GdkWaylandDisplay *display)
183 {
184 GHashTableIter iter;
185 const char *name;
186 GdkWaylandCursor *cursor;
187
188 g_hash_table_iter_init (&iter, display->cursor_cache);
189
190 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer *) &cursor))
191 _gdk_wayland_cursor_update (display, cursor);
192 }
193
194 static void
195 gdk_wayland_cursor_finalize (GObject *object)
196 {
197 GdkWaylandCursor *cursor = GDK_WAYLAND_CURSOR (object);
198
199 g_free (cursor->name);
200 if (cursor->surface.cairo_surface)
201 cairo_surface_destroy (cursor->surface.cairo_surface);
202
203 G_OBJECT_CLASS (_gdk_wayland_cursor_parent_class)->finalize (object);
204 }
205
206 static cairo_surface_t *
207 gdk_wayland_cursor_get_surface (GdkCursor *cursor,
208 gdouble *x_hot,
209 gdouble *y_hot)
210 {
211 return NULL;
212 }
213
214 const char *
215 _gdk_wayland_cursor_get_name (GdkCursor *cursor)
216 {
217 GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
218 return wayland_cursor->name;
219 }
220
221 struct wl_buffer *
222 _gdk_wayland_cursor_get_buffer (GdkCursor *cursor,
223 guint image_index,
224 int *hotspot_x,
225 int *hotspot_y,
226 int *w,
227 int *h,
228 int *scale)
229 {
230 GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
231
232 if (wayland_cursor->wl_cursor && wayland_cursor->wl_cursor->image_count > 0)
233 {
234 struct wl_cursor_image *image;
235 int cursor_scale;
236
237 if (image_index >= wayland_cursor->wl_cursor->image_count)
238 {
239 g_warning (G_STRLOC " out of bounds cursor image [%d / %d]",
240 image_index,
241 wayland_cursor->wl_cursor->image_count - 1);
242 image_index = 0;
243 }
244
245 image = wayland_cursor->wl_cursor->images[image_index];
246
247 cursor_scale = wayland_cursor->scale;
248 while ((image->width % cursor_scale != 0) ||
249 (image->height % cursor_scale != 0))
250 {
251 g_warning_once (G_STRLOC " cursor image size (%dx%d) not an integer"
252 "multiple of scale (%d)", image->width, image->height,
253 cursor_scale);
254 cursor_scale--;
255 }
256
257 *hotspot_x = image->hotspot_x / cursor_scale;
258 *hotspot_y = image->hotspot_y / cursor_scale;
259
260 *w = image->width / cursor_scale;
261 *h = image->height / cursor_scale;
262 *scale = cursor_scale;
263
264 return wl_cursor_image_get_buffer (image);
265 }
266 else if (wayland_cursor->name == NULL) /* From surface */
267 {
268 *hotspot_x =
269 wayland_cursor->surface.hotspot_x / wayland_cursor->surface.scale;
270 *hotspot_y =
271 wayland_cursor->surface.hotspot_y / wayland_cursor->surface.scale;
272
273 *w = wayland_cursor->surface.width / wayland_cursor->surface.scale;
274 *h = wayland_cursor->surface.height / wayland_cursor->surface.scale;
275 *scale = wayland_cursor->surface.scale;
276
277 cairo_surface_reference (wayland_cursor->surface.cairo_surface);
278
279 if (wayland_cursor->surface.cairo_surface)
280 return _gdk_wayland_shm_surface_get_wl_buffer (wayland_cursor->surface.cairo_surface);
281 }
282 else
283 {
284 *hotspot_x = 0;
285 *hotspot_y = 0;
286 *w = 0;
287 *h = 0;
288 *scale = 1;
289 }
290
291 return NULL;
292 }
293
294 guint
295 _gdk_wayland_cursor_get_next_image_index (GdkCursor *cursor,
296 guint current_image_index,
297 guint *next_image_delay)
298 {
299 struct wl_cursor *wl_cursor = GDK_WAYLAND_CURSOR (cursor)->wl_cursor;
300
301 if (wl_cursor && wl_cursor->image_count > 1)
302 {
303 if (current_image_index >= wl_cursor->image_count)
304 {
305 g_warning (G_STRLOC " out of bounds cursor image [%d / %d]",
306 current_image_index, wl_cursor->image_count - 1);
307 current_image_index = 0;
308 }
309
310 /* Return the time to next image */
311 if (next_image_delay)
312 *next_image_delay = wl_cursor->images[current_image_index]->delay;
313
314 return (current_image_index + 1) % wl_cursor->image_count;
315 }
316 else
317 return current_image_index;
318 }
319
320 void
321 _gdk_wayland_cursor_set_scale (GdkCursor *cursor,
322 guint scale)
323 {
324 GdkWaylandDisplay *display_wayland =
325 GDK_WAYLAND_DISPLAY (gdk_cursor_get_display (cursor));
326 GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
327
328 if (scale > GDK_WAYLAND_MAX_THEME_SCALE)
329 {
330 g_warning (G_STRLOC ": cursor theme size %u too large", scale);
331 scale = GDK_WAYLAND_MAX_THEME_SCALE;
332 }
333
334 if (wayland_cursor->scale == scale)
335 return;
336
337 wayland_cursor->scale = scale;
338
339 /* Blank cursor case */
340 if (g_strcmp0 (wayland_cursor->name, "none") == 0)
341 return;
342
343 _gdk_wayland_cursor_update (display_wayland, wayland_cursor);
344 }
345
346 static void
347 _gdk_wayland_cursor_class_init (GdkWaylandCursorClass *wayland_cursor_class)
348 {
349 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (wayland_cursor_class);
350 GObjectClass *object_class = G_OBJECT_CLASS (wayland_cursor_class);
351
352 object_class->finalize = gdk_wayland_cursor_finalize;
353
354 cursor_class->get_surface = gdk_wayland_cursor_get_surface;
355 }
356
357 static void
358 _gdk_wayland_cursor_init (GdkWaylandCursor *cursor)
359 {
360 }
361
362 GdkCursor *
363 _gdk_wayland_display_get_cursor_for_name_with_scale (GdkDisplay *display,
364 const gchar *name,
365 guint scale)
366 {
367 GdkWaylandCursor *wayland_cursor;
368 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
369
370 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
371
372 wayland_cursor = g_hash_table_lookup (display_wayland->cursor_cache, name);
373 if (wayland_cursor && wayland_cursor->scale == scale)
374 return GDK_CURSOR (g_object_ref (wayland_cursor));
375
376 wayland_cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
377 "cursor-type", GDK_CURSOR_IS_PIXMAP,
378 "display", display,
379 NULL);
380
381 /* Blank cursor case */
382 if (!name || g_str_equal (name, "none") || g_str_equal (name, "blank_cursor"))
383 {
384 wayland_cursor->name = g_strdup ("none");
385 wayland_cursor->scale = scale;
386
387 return GDK_CURSOR (wayland_cursor);
388 }
389
390 wayland_cursor->name = g_strdup (name);
391 wayland_cursor->scale = scale;
392
393 if (!_gdk_wayland_cursor_update (display_wayland, wayland_cursor))
394 {
395 g_object_unref (wayland_cursor);
396 return NULL;
397 }
398
399 /* Insert into cache. */
400 g_hash_table_replace (display_wayland->cursor_cache,
401 wayland_cursor->name,
402 g_object_ref (wayland_cursor));
403 return GDK_CURSOR (wayland_cursor);
404 }
405
406 GdkCursor *
407 _gdk_wayland_display_get_cursor_for_name (GdkDisplay *display,
408 const gchar *name)
409 {
410 return _gdk_wayland_display_get_cursor_for_name_with_scale (display, name, 1);
411 }
412
413 GdkCursor *
414 _gdk_wayland_display_get_cursor_for_type_with_scale (GdkDisplay *display,
415 GdkCursorType cursor_type,
416 guint scale)
417 {
418 GEnumClass *enum_class;
419 GEnumValue *enum_value;
420 gchar *cursor_name;
421 GdkCursor *result;
422
423 enum_class = g_type_class_ref (GDK_TYPE_CURSOR_TYPE);
424 enum_value = g_enum_get_value (enum_class, cursor_type);
425 cursor_name = g_strdup (enum_value->value_nick);
426 g_strdelimit (cursor_name, "-", '_');
427 g_type_class_unref (enum_class);
428
429 result = _gdk_wayland_display_get_cursor_for_name_with_scale (display,
430 cursor_name,
431 scale);
432
433 g_free (cursor_name);
434
435 if (!result)
436 {
437 const char *name = NULL;
438
439 /* Map cursors back to standard names.
440 * Currently, we just list the cursor values
441 * that are used in GTK. More can be added.
442 */
443 switch ((int)cursor_type)
444 {
445 case GDK_XTERM:
446 name = "text";
447 break;
448 case GDK_FLEUR:
449 name = "move";
450 break;
451 case GDK_CROSSHAIR:
452 name = "cross";
453 break;
454 default:
455 name = "default";
456 break;
457 }
458
459 if (name)
460 result = _gdk_wayland_display_get_cursor_for_name_with_scale (display,
461 name,
462 scale);
463 }
464
465 return result;
466 }
467
468 GdkCursor *
469 _gdk_wayland_display_get_cursor_for_type (GdkDisplay *display,
470 GdkCursorType cursor_type)
471 {
472 return _gdk_wayland_display_get_cursor_for_type_with_scale (display,
473 cursor_type,
474 1);
475 }
476
477 static void
478 buffer_release_callback (void *_data,
479 struct wl_buffer *wl_buffer)
480 {
481 cairo_surface_t *cairo_surface = _data;
482
483 cairo_surface_destroy (cairo_surface);
484 }
485
486 static const struct wl_buffer_listener buffer_listener = {
487 buffer_release_callback
488 };
489
490 GdkCursor *
491 _gdk_wayland_display_get_cursor_for_surface (GdkDisplay *display,
492 cairo_surface_t *surface,
493 gdouble x,
494 gdouble y)
495 {
496 GdkWaylandCursor *cursor;
497 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
498 struct wl_buffer *buffer;
499 cairo_t *cr;
500
501 cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
502 "cursor-type", GDK_CURSOR_IS_PIXMAP,
503 "display", display_wayland,
504 NULL);
505 cursor->name = NULL;
506 cursor->surface.hotspot_x = x;
507 cursor->surface.hotspot_y = y;
508
509 if (surface)
510 {
511 cursor->surface.width = cairo_image_surface_get_width (surface);
512 cursor->surface.height = cairo_image_surface_get_height (surface);
513
514 double sx, sy;
515 cairo_surface_get_device_scale (surface, &sx, &sy);
516 cursor->surface.scale = (int)sx;
517
518 while ((cursor->surface.width % cursor->surface.scale != 0) ||
519 (cursor->surface.height % cursor->surface.scale != 0))
520 {
521 g_warning_once (G_STRLOC " cursor image size (%dx%d) not an integer"
522 "multiple of scale (%d)", cursor->surface.width, cursor->surface.height,
523 cursor->surface.scale);
524 cursor->surface.scale--;
525 }
526
527 cursor->surface.hotspot_x *= sx;
528 cursor->surface.hotspot_y *= sx;
529 }
530 else
531 {
532 cursor->surface.width = 1;
533 cursor->surface.height = 1;
534 cursor->surface.scale = 1;
535 }
536
537 cursor->surface.cairo_surface =
538 _gdk_wayland_display_create_shm_surface (display_wayland,
539 cursor->surface.width / cursor->surface.scale,
540 cursor->surface.height / cursor->surface.scale,
541 cursor->surface.scale);
542
543 buffer = _gdk_wayland_shm_surface_get_wl_buffer (cursor->surface.cairo_surface);
544 wl_buffer_add_listener (buffer, &buffer_listener, cursor->surface.cairo_surface);
545
546 if (surface)
547 {
548 cr = cairo_create (cursor->surface.cairo_surface);
549 cairo_set_source_surface (cr, surface, 0, 0);
550 cairo_paint (cr);
551 cairo_destroy (cr);
552 }
553
554 return GDK_CURSOR (cursor);
555 }
556
557 void
558 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
559 guint *width,
560 guint *height)
561 {
562 *width = 32;
563 *height = 32;
564 }
565
566 void
567 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
568 guint *width,
569 guint *height)
570 {
571 *width = 256;
572 *height = 256;
573 }
574
575 gboolean
576 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
577 {
578 return TRUE;
579 }
580
581 gboolean
582 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)
583 {
584 return TRUE;
585 }