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;
171 struct wl_visual *visual;
174 GError *error = NULL;
176 cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
177 "cursor-type", GDK_CURSOR_IS_PIXMAP,
181 cursor->serial = theme_serial;
184 cursor->width = gdk_pixbuf_get_width (pixbuf);
185 cursor->height = gdk_pixbuf_get_height (pixbuf);
187 stride = cursor->width * 4;
188 cursor->size = stride * cursor->height;
190 fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
192 fprintf(stderr, "g_file_open_tmp failed: %s\n", error->message);
193 g_error_free (error);
200 if (ftruncate(fd, cursor->size) < 0) {
201 fprintf(stderr, "ftruncate failed: %m\n");
206 cursor->map = mmap(NULL, cursor->size,
207 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
208 if (cursor->map == MAP_FAILED) {
209 fprintf(stderr, "mmap failed: %m\n");
214 set_pixbuf (cursor, pixbuf);
216 visual = wl_display_get_premultiplied_argb_visual(display->wl_display);
217 cursor->buffer = wl_shm_create_buffer(display->shm,
225 return GDK_CURSOR (cursor);
228 #define DATADIR "/usr/share/wayland"
230 static const struct {
232 const char *filename;
233 int hotspot_x, hotspot_y;
234 } cursor_definitions[] = {
235 { GDK_XTERM, DATADIR "/xterm.png", 15, 15 },
236 { GDK_BOTTOM_RIGHT_CORNER, DATADIR "/bottom_right_corner.png", 28, 28 }
240 _gdk_wayland_display_get_cursor_for_type (GdkDisplay *display,
241 GdkCursorType cursor_type)
243 GdkDisplayWayland *wayland_display;
245 GError *error = NULL;
248 for (i = 0; i < G_N_ELEMENTS (cursor_definitions); i++)
250 if (cursor_definitions[i].type == cursor_type)
254 if (i == G_N_ELEMENTS (cursor_definitions))
257 wayland_display = GDK_DISPLAY_WAYLAND (display);
258 if (!wayland_display->cursors)
259 wayland_display->cursors =
260 g_new0 (GdkCursor *, G_N_ELEMENTS(cursor_definitions));
261 if (wayland_display->cursors[i])
262 return g_object_ref (wayland_display->cursors[i]);
264 pixbuf = gdk_pixbuf_new_from_file(cursor_definitions[i].filename, &error);
271 wayland_display->cursors[i] =
272 create_cursor(wayland_display, pixbuf,
273 cursor_definitions[i].hotspot_x,
274 cursor_definitions[i].hotspot_y);
275 g_object_unref (pixbuf);
277 return g_object_ref (wayland_display->cursors[i]);
281 _gdk_wayland_display_get_cursor_for_name (GdkDisplay *display,
284 GdkWaylandCursor *private;
286 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
288 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
289 "cursor-type", GDK_CURSOR_IS_PIXMAP,
292 private->name = g_strdup (name);
293 private->serial = theme_serial;
295 return GDK_CURSOR (private);
299 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
304 GdkWaylandCursor *private;
306 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
307 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
308 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
309 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
311 private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
312 "cursor-type", GDK_CURSOR_IS_PIXMAP,
316 private->name = NULL;
317 private->serial = theme_serial;
319 return GDK_CURSOR (private);
323 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
327 /* FIXME: wayland settings? */
333 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
342 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
348 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)