1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
27 #define GDK_PIXBUF_ENABLE_BACKEND
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>
37 #include <wayland-cursor.h>
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))
46 typedef struct _GdkWaylandCursor GdkWaylandCursor;
47 typedef struct _GdkWaylandCursorClass GdkWaylandCursorClass;
49 struct _GdkWaylandCursor
54 int hotspot_x, hotspot_y;
56 struct wl_buffer *buffer;
59 struct _GdkWaylandCursorClass
61 GdkCursorClass cursor_class;
64 G_DEFINE_TYPE (GdkWaylandCursor, _gdk_wayland_cursor, GDK_TYPE_CURSOR)
66 static guint theme_serial = 0;
69 gdk_wayland_cursor_finalize (GObject *object)
71 GdkWaylandCursor *cursor = GDK_WAYLAND_CURSOR (object);
73 g_free (cursor->name);
75 G_OBJECT_CLASS (_gdk_wayland_cursor_parent_class)->finalize (object);
79 gdk_wayland_cursor_get_image (GdkCursor *cursor)
85 _gdk_wayland_cursor_get_buffer (GdkCursor *cursor,
91 GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
93 *x = wayland_cursor->hotspot_x;
94 *y = wayland_cursor->hotspot_y;
96 *w = wayland_cursor->width;
97 *h = wayland_cursor->height;
99 return wayland_cursor->buffer;
103 _gdk_wayland_cursor_class_init (GdkWaylandCursorClass *wayland_cursor_class)
105 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (wayland_cursor_class);
106 GObjectClass *object_class = G_OBJECT_CLASS (wayland_cursor_class);
108 object_class->finalize = gdk_wayland_cursor_finalize;
110 cursor_class->get_image = gdk_wayland_cursor_get_image;
114 _gdk_wayland_cursor_init (GdkWaylandCursor *cursor)
118 /* Use to implement from_pixbuf below */
121 set_pixbuf (GdkWaylandCursor *cursor, GdkPixbuf *pixbuf)
123 int stride, i, n_channels;
124 unsigned char *pixels, *end, *argb_pixels, *s, *d;
126 stride = gdk_pixbuf_get_rowstride(pixbuf);
127 pixels = gdk_pixbuf_get_pixels(pixbuf);
128 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
129 argb_pixels = cursor->map;
131 #define MULT(_d,c,a,t) \
132 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
136 for (i = 0; i < cursor->height; i++)
138 s = pixels + i * stride;
139 end = s + cursor->width * 4;
140 d = argb_pixels + i * cursor->width * 4;
145 MULT(d[0], s[2], s[3], t);
146 MULT(d[1], s[1], s[3], t);
147 MULT(d[2], s[0], s[3], t);
154 else if (n_channels == 3)
156 for (i = 0; i < cursor->height; i++)
158 s = pixels + i * stride;
159 end = s + cursor->width * 3;
160 d = argb_pixels + i * cursor->width * 4;
175 create_cursor(GdkWaylandDisplay *display, GdkPixbuf *pixbuf, int x, int y)
177 GdkWaylandCursor *cursor;
180 GError *error = NULL;
181 struct wl_shm_pool *pool;
183 cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
184 "cursor-type", GDK_CURSOR_IS_PIXMAP,
188 cursor->serial = theme_serial;
193 cursor->width = gdk_pixbuf_get_width (pixbuf);
194 cursor->height = gdk_pixbuf_get_height (pixbuf);
202 stride = cursor->width * 4;
203 cursor->size = stride * cursor->height;
205 fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
208 g_critical (G_STRLOC ": Error opening temporary file for buffer: %s",
210 g_error_free (error);
217 if (ftruncate(fd, cursor->size) < 0)
219 g_critical (G_STRLOC ": Error truncating file for buffer: %s",
225 cursor->map = mmap(NULL, cursor->size,
226 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
227 if (cursor->map == MAP_FAILED)
229 g_critical (G_STRLOC ": Error mmap'ing file for buffer: %s",
236 set_pixbuf (cursor, pixbuf);
238 memset (cursor->map, 0, 4);
240 cursor->buffer = wl_shm_create_buffer(display->shm,
244 stride, WL_SHM_FORMAT_ARGB8888);
248 return GDK_CURSOR (cursor);
253 _gdk_wayland_display_get_cursor_for_type (GdkDisplay *display,
254 GdkCursorType cursor_type)
256 GEnumClass *enum_class;
257 GEnumValue *enum_value;
261 enum_class = g_type_class_ref (GDK_TYPE_CURSOR_TYPE);
262 enum_value = g_enum_get_value (enum_class, cursor_type);
263 cursor_name = g_strdup (enum_value->value_nick);
264 g_strdelimit (cursor_name, "-", '_');
265 g_type_class_unref (enum_class);
267 result = _gdk_wayland_display_get_cursor_for_name (display, cursor_name);
269 g_free (cursor_name);
275 _gdk_wayland_display_get_cursor_for_name (GdkDisplay *display,
278 GdkWaylandCursor *private;
279 GdkWaylandDisplay *wayland_display = GDK_WAYLAND_DISPLAY (display);
280 struct wl_cursor *cursor;
282 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
284 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
285 "cursor-type", GDK_CURSOR_IS_PIXMAP,
288 private->name = g_strdup (name);
289 private->serial = theme_serial;
291 /* Blank cursor case */
292 if (!name || g_str_equal (name, "blank_cursor"))
293 return GDK_CURSOR (private);
295 cursor = wl_cursor_theme_get_cursor (wayland_display->cursor_theme,
300 g_warning (G_STRLOC ": Unable to load %s from the cursor theme", name);
302 /* return the left_ptr cursor as a fallback */
303 cursor = wl_cursor_theme_get_cursor (wayland_display->cursor_theme,
306 /* if the fallback failed to load, return a blank pointer */
308 return GDK_CURSOR (private);
311 /* TODO: Do something clever so we can do animated cursors - move the
312 * wl_pointer_set_cursor to a function here so that we can do the magic to
315 private->hotspot_x = cursor->images[0]->hotspot_x;
316 private->hotspot_y = cursor->images[0]->hotspot_y;
317 private->width = cursor->images[0]->width;
318 private->height = cursor->images[0]->height;
320 private->buffer = wl_cursor_image_get_buffer(cursor->images[0]);
322 return GDK_CURSOR (private);
325 /* TODO: Needs implementing */
327 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
332 GdkWaylandCursor *private;
334 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
335 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
336 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
337 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
339 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
340 "cursor-type", GDK_CURSOR_IS_PIXMAP,
344 private->name = NULL;
345 private->serial = theme_serial;
347 return GDK_CURSOR (private);
351 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
360 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
369 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
375 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)