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>
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 x, y, width, height, size;
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, int *x, int *y)
87 GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
89 *x = wayland_cursor->x;
90 *y = wayland_cursor->y;
92 return wayland_cursor->buffer;
96 _gdk_wayland_cursor_class_init (GdkWaylandCursorClass *wayland_cursor_class)
98 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (wayland_cursor_class);
99 GObjectClass *object_class = G_OBJECT_CLASS (wayland_cursor_class);
101 object_class->finalize = gdk_wayland_cursor_finalize;
103 cursor_class->get_image = gdk_wayland_cursor_get_image;
107 _gdk_wayland_cursor_init (GdkWaylandCursor *cursor)
112 set_pixbuf (GdkWaylandCursor *cursor, GdkPixbuf *pixbuf)
114 int stride, i, n_channels;
115 unsigned char *pixels, *end, *argb_pixels, *s, *d;
117 stride = gdk_pixbuf_get_rowstride(pixbuf);
118 pixels = gdk_pixbuf_get_pixels(pixbuf);
119 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
120 argb_pixels = cursor->map;
122 #define MULT(_d,c,a,t) \
123 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
127 for (i = 0; i < cursor->height; i++)
129 s = pixels + i * stride;
130 end = s + cursor->width * 4;
131 d = argb_pixels + i * cursor->width * 4;
136 MULT(d[0], s[2], s[3], t);
137 MULT(d[1], s[1], s[3], t);
138 MULT(d[2], s[0], s[3], t);
145 else if (n_channels == 3)
147 for (i = 0; i < cursor->height; i++)
149 s = pixels + i * stride;
150 end = s + cursor->width * 3;
151 d = argb_pixels + i * cursor->width * 4;
166 create_cursor(GdkDisplayWayland *display, GdkPixbuf *pixbuf, int x, int y)
168 GdkWaylandCursor *cursor;
171 GError *error = NULL;
173 cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
174 "cursor-type", GDK_CURSOR_IS_PIXMAP,
178 cursor->serial = theme_serial;
183 cursor->width = gdk_pixbuf_get_width (pixbuf);
184 cursor->height = gdk_pixbuf_get_height (pixbuf);
192 stride = cursor->width * 4;
193 cursor->size = stride * cursor->height;
195 fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
197 fprintf(stderr, "g_file_open_tmp failed: %s\n", error->message);
198 g_error_free (error);
205 if (ftruncate(fd, cursor->size) < 0) {
206 fprintf(stderr, "ftruncate failed: %m\n");
211 cursor->map = mmap(NULL, cursor->size,
212 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
213 if (cursor->map == MAP_FAILED) {
214 fprintf(stderr, "mmap failed: %m\n");
220 set_pixbuf (cursor, pixbuf);
222 memset (cursor->map, 0, 4);
224 cursor->buffer = wl_shm_create_buffer(display->shm,
228 stride, WL_SHM_FORMAT_ARGB8888);
232 return GDK_CURSOR (cursor);
235 static const struct {
237 const char *filename;
238 int hotspot_x, hotspot_y;
239 } cursor_definitions[] = {
240 { GDK_BLANK_CURSOR, NULL, 0, 0 },
241 { GDK_HAND1, "hand1.png", 18, 11 },
242 { GDK_HAND2, "hand2.png", 14, 8 },
243 { GDK_LEFT_PTR, "left_ptr.png", 10, 5 },
244 { GDK_SB_H_DOUBLE_ARROW, "sb_h_double_arrow.png", 15, 15 },
245 { GDK_SB_V_DOUBLE_ARROW, "sb_v_double_arrow.png", 15, 15 },
246 { GDK_XTERM, "xterm.png", 15, 15 },
247 { GDK_BOTTOM_RIGHT_CORNER, "bottom_right_corner.png", 28, 28 }
251 _gdk_wayland_display_get_cursor_for_type (GdkDisplay *display,
252 GdkCursorType cursor_type)
254 GdkDisplayWayland *wayland_display;
255 GdkPixbuf *pixbuf = NULL;
256 GError *error = NULL;
259 for (i = 0; i < G_N_ELEMENTS (cursor_definitions); i++)
261 if (cursor_definitions[i].type == cursor_type)
265 if (i == G_N_ELEMENTS (cursor_definitions))
267 g_warning ("Unhandled cursor type %d, falling back to blank\n",
272 wayland_display = GDK_DISPLAY_WAYLAND (display);
273 if (!wayland_display->cursors)
274 wayland_display->cursors =
275 g_new0 (GdkCursor *, G_N_ELEMENTS(cursor_definitions));
276 if (wayland_display->cursors[i])
277 return g_object_ref (wayland_display->cursors[i]);
280 g_message ("Creating new cursor for type %d, filename %s",
281 cursor_type, cursor_definitions[i].filename));
283 if (cursor_type != GDK_BLANK_CURSOR)
285 const gchar * const *directories;
288 directories = g_get_system_data_dirs();
290 for (j = 0; directories[j] != NULL; j++)
293 filename = g_build_filename (directories[j],
295 cursor_definitions[i].filename,
297 if (g_file_test (filename, G_FILE_TEST_EXISTS))
299 pixbuf = gdk_pixbuf_new_from_file (filename, &error);
303 g_warning ("Failed to load cursor: %s: %s",
304 filename, error->message);
314 g_warning ("Unable to find cursor for: %s",
315 cursor_definitions[i].filename);
320 wayland_display->cursors[i] =
321 create_cursor(wayland_display, pixbuf,
322 cursor_definitions[i].hotspot_x,
323 cursor_definitions[i].hotspot_y);
325 g_object_unref (pixbuf);
327 return g_object_ref (wayland_display->cursors[i]);
331 _gdk_wayland_display_get_cursor_for_name (GdkDisplay *display,
334 GdkWaylandCursor *private;
336 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
338 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
339 "cursor-type", GDK_CURSOR_IS_PIXMAP,
342 private->name = g_strdup (name);
343 private->serial = theme_serial;
345 return GDK_CURSOR (private);
349 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
354 GdkWaylandCursor *private;
356 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
357 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
358 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
359 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
361 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
362 "cursor-type", GDK_CURSOR_IS_PIXMAP,
366 private->name = NULL;
367 private->serial = theme_serial;
369 return GDK_CURSOR (private);
373 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
377 /* FIXME: wayland settings? */
383 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
392 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
398 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)