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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #define GDK_PIXBUF_ENABLE_BACKEND
33 #include "gdkprivate-wayland.h"
34 #include "gdkcursorprivate.h"
35 #include "gdkdisplay-wayland.h"
36 #include "gdkwayland.h"
37 #include <gdk-pixbuf/gdk-pixbuf.h>
41 #define GDK_TYPE_WAYLAND_CURSOR (_gdk_wayland_cursor_get_type ())
42 #define GDK_WAYLAND_CURSOR(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_WAYLAND_CURSOR, GdkWaylandCursor))
43 #define GDK_WAYLAND_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_WAYLAND_CURSOR, GdkWaylandCursorClass))
44 #define GDK_IS_WAYLAND_CURSOR(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_WAYLAND_CURSOR))
45 #define GDK_IS_WAYLAND_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_WAYLAND_CURSOR))
46 #define GDK_WAYLAND_CURSOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_WAYLAND_CURSOR, GdkWaylandCursorClass))
48 typedef struct _GdkWaylandCursor GdkWaylandCursor;
49 typedef struct _GdkWaylandCursorClass GdkWaylandCursorClass;
51 struct _GdkWaylandCursor
56 int x, y, width, height, size;
58 struct wl_buffer *buffer;
61 struct _GdkWaylandCursorClass
63 GdkCursorClass cursor_class;
66 G_DEFINE_TYPE (GdkWaylandCursor, _gdk_wayland_cursor, GDK_TYPE_CURSOR)
68 static guint theme_serial = 0;
71 gdk_wayland_cursor_finalize (GObject *object)
73 GdkWaylandCursor *cursor = GDK_WAYLAND_CURSOR (object);
75 g_free (cursor->name);
77 G_OBJECT_CLASS (_gdk_wayland_cursor_parent_class)->finalize (object);
81 gdk_wayland_cursor_get_image (GdkCursor *cursor)
87 _gdk_wayland_cursor_get_buffer (GdkCursor *cursor, int *x, int *y)
89 GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
91 *x = wayland_cursor->x;
92 *y = wayland_cursor->y;
94 return wayland_cursor->buffer;
98 _gdk_wayland_cursor_class_init (GdkWaylandCursorClass *wayland_cursor_class)
100 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (wayland_cursor_class);
101 GObjectClass *object_class = G_OBJECT_CLASS (wayland_cursor_class);
103 object_class->finalize = gdk_wayland_cursor_finalize;
105 cursor_class->get_image = gdk_wayland_cursor_get_image;
109 _gdk_wayland_cursor_init (GdkWaylandCursor *cursor)
114 set_pixbuf (GdkWaylandCursor *cursor, GdkPixbuf *pixbuf)
116 int stride, i, n_channels;
117 unsigned char *pixels, *end, *argb_pixels, *s, *d;
119 stride = gdk_pixbuf_get_rowstride(pixbuf);
120 pixels = gdk_pixbuf_get_pixels(pixbuf);
121 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
122 argb_pixels = cursor->map;
124 #define MULT(_d,c,a,t) \
125 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
129 for (i = 0; i < cursor->height; i++)
131 s = pixels + i * stride;
132 end = s + cursor->width * 4;
133 d = argb_pixels + i * cursor->width * 4;
138 MULT(d[0], s[2], s[3], t);
139 MULT(d[1], s[1], s[3], t);
140 MULT(d[2], s[0], s[3], t);
147 else if (n_channels == 3)
149 for (i = 0; i < cursor->height; i++)
151 s = pixels + i * stride;
152 end = s + cursor->width * 3;
153 d = argb_pixels + i * cursor->width * 4;
168 create_cursor(GdkDisplayWayland *display, GdkPixbuf *pixbuf, int x, int y)
170 GdkWaylandCursor *cursor;
173 GError *error = NULL;
175 cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
176 "cursor-type", GDK_CURSOR_IS_PIXMAP,
180 cursor->serial = theme_serial;
185 cursor->width = gdk_pixbuf_get_width (pixbuf);
186 cursor->height = gdk_pixbuf_get_height (pixbuf);
194 stride = cursor->width * 4;
195 cursor->size = stride * cursor->height;
197 fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
199 fprintf(stderr, "g_file_open_tmp failed: %s\n", error->message);
200 g_error_free (error);
207 if (ftruncate(fd, cursor->size) < 0) {
208 fprintf(stderr, "ftruncate failed: %m\n");
213 cursor->map = mmap(NULL, cursor->size,
214 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
215 if (cursor->map == MAP_FAILED) {
216 fprintf(stderr, "mmap failed: %m\n");
222 set_pixbuf (cursor, pixbuf);
224 memset (cursor->map, 0, 4);
226 cursor->buffer = wl_shm_create_buffer(display->shm,
230 stride, WL_SHM_FORMAT_ARGB8888);
234 return GDK_CURSOR (cursor);
237 static const struct {
239 const char *filename;
240 int hotspot_x, hotspot_y;
241 } cursor_definitions[] = {
242 { GDK_BLANK_CURSOR, NULL, 0, 0 },
243 { GDK_HAND1, "hand1.png", 18, 11 },
244 { GDK_HAND2, "hand2.png", 14, 8 },
245 { GDK_LEFT_PTR, "left_ptr.png", 10, 5 },
246 { GDK_SB_H_DOUBLE_ARROW, "sb_h_double_arrow.png", 15, 15 },
247 { GDK_SB_V_DOUBLE_ARROW, "sb_v_double_arrow.png", 15, 15 },
248 { GDK_XTERM, "xterm.png", 15, 15 },
249 { GDK_BOTTOM_RIGHT_CORNER, "bottom_right_corner.png", 28, 28 }
253 _gdk_wayland_display_get_cursor_for_type (GdkDisplay *display,
254 GdkCursorType cursor_type)
256 GdkDisplayWayland *wayland_display;
257 GdkPixbuf *pixbuf = NULL;
258 GError *error = NULL;
261 for (i = 0; i < G_N_ELEMENTS (cursor_definitions); i++)
263 if (cursor_definitions[i].type == cursor_type)
267 if (i == G_N_ELEMENTS (cursor_definitions))
269 g_warning ("Unhandled cursor type %d, falling back to blank\n",
274 wayland_display = GDK_DISPLAY_WAYLAND (display);
275 if (!wayland_display->cursors)
276 wayland_display->cursors =
277 g_new0 (GdkCursor *, G_N_ELEMENTS(cursor_definitions));
278 if (wayland_display->cursors[i])
279 return g_object_ref (wayland_display->cursors[i]);
282 g_message ("Creating new cursor for type %d, filename %s",
283 cursor_type, cursor_definitions[i].filename));
285 if (cursor_type != GDK_BLANK_CURSOR)
287 const gchar * const *directories;
290 directories = g_get_system_data_dirs();
292 for (j = 0; directories[j] != NULL; j++)
295 filename = g_build_filename (directories[j],
297 cursor_definitions[i].filename,
299 if (g_file_test (filename, G_FILE_TEST_EXISTS))
301 pixbuf = gdk_pixbuf_new_from_file (filename, &error);
305 g_warning ("Failed to load cursor: %s: %s",
306 filename, error->message);
316 g_warning ("Unable to find cursor for: %s",
317 cursor_definitions[i].filename);
322 wayland_display->cursors[i] =
323 create_cursor(wayland_display, pixbuf,
324 cursor_definitions[i].hotspot_x,
325 cursor_definitions[i].hotspot_y);
327 g_object_unref (pixbuf);
329 return g_object_ref (wayland_display->cursors[i]);
333 _gdk_wayland_display_get_cursor_for_name (GdkDisplay *display,
336 GdkWaylandCursor *private;
338 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
340 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
341 "cursor-type", GDK_CURSOR_IS_PIXMAP,
344 private->name = g_strdup (name);
345 private->serial = theme_serial;
347 return GDK_CURSOR (private);
351 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
356 GdkWaylandCursor *private;
358 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
359 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
360 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
361 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
363 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
364 "cursor-type", GDK_CURSOR_IS_PIXMAP,
368 private->name = NULL;
369 private->serial = theme_serial;
371 return GDK_CURSOR (private);
375 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
379 /* FIXME: wayland settings? */
385 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
394 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
400 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)