]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkcursor-wayland.c
wayland: Support setting cursors
[~andy/gtk] / gdk / wayland / gdkcursor-wayland.c
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28
29 #define GDK_PIXBUF_ENABLE_BACKEND
30
31 #include <string.h>
32
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>
38
39 #include <sys/mman.h>
40
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))
47
48 typedef struct _GdkWaylandCursor GdkWaylandCursor;
49 typedef struct _GdkWaylandCursorClass GdkWaylandCursorClass;
50
51 struct _GdkWaylandCursor
52 {
53   GdkCursor cursor;
54   gchar *name;
55   guint serial;
56   int x, y, width, height, size;
57   void *map;
58   struct wl_buffer *buffer;
59 };
60
61 struct _GdkWaylandCursorClass
62 {
63   GdkCursorClass cursor_class;
64 };
65
66 G_DEFINE_TYPE (GdkWaylandCursor, _gdk_wayland_cursor, GDK_TYPE_CURSOR)
67
68 static guint theme_serial = 0;
69
70 static void
71 gdk_wayland_cursor_finalize (GObject *object)
72 {
73   GdkWaylandCursor *cursor = GDK_WAYLAND_CURSOR (object);
74
75   g_free (cursor->name);
76
77   G_OBJECT_CLASS (_gdk_wayland_cursor_parent_class)->finalize (object);
78 }
79
80 static GdkPixbuf*
81 gdk_wayland_cursor_get_image (GdkCursor *cursor)
82 {
83   return NULL;
84 }
85
86 struct wl_buffer *
87 _gdk_wayland_cursor_get_buffer (GdkCursor *cursor, int *x, int *y)
88 {
89   GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
90
91   *x = wayland_cursor->x;
92   *y = wayland_cursor->y;
93
94   return wayland_cursor->buffer;
95 }
96
97 static void
98 _gdk_wayland_cursor_class_init (GdkWaylandCursorClass *wayland_cursor_class)
99 {
100   GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (wayland_cursor_class);
101   GObjectClass *object_class = G_OBJECT_CLASS (wayland_cursor_class);
102
103   object_class->finalize = gdk_wayland_cursor_finalize;
104
105   cursor_class->get_image = gdk_wayland_cursor_get_image;
106 }
107
108 static void
109 _gdk_wayland_cursor_init (GdkWaylandCursor *cursor)
110 {
111 }
112
113 static void
114 set_pixbuf (GdkWaylandCursor *cursor, GdkPixbuf *pixbuf)
115 {
116   int stride, i, n_channels;
117   unsigned char *pixels, *end, *argb_pixels, *s, *d;
118
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;
123
124 #define MULT(_d,c,a,t) \
125         do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
126
127   if (n_channels == 4)
128     {
129       for (i = 0; i < cursor->height; i++)
130         {
131           s = pixels + i * stride;
132           end = s + cursor->width * 4;
133           d = argb_pixels + i * cursor->width * 4;
134           while (s < end)
135             {
136               unsigned int t;
137
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);
141               d[3] = s[3];
142               s += 4;
143               d += 4;
144             }
145         }
146     }
147   else if (n_channels == 3)
148     {
149       for (i = 0; i < cursor->height; i++)
150         {
151           s = pixels + i * stride;
152           end = s + cursor->width * 3;
153           d = argb_pixels + i * cursor->width * 4;
154           while (s < end)
155             {
156               d[0] = s[2];
157               d[1] = s[1];
158               d[2] = s[0];
159               d[3] = 0xff;
160               s += 3;
161               d += 4;
162             }
163         }
164     }
165 }
166
167 static GdkCursor *
168 create_cursor(GdkDisplayWayland *display, GdkPixbuf *pixbuf, int x, int y)
169 {
170   GdkWaylandCursor *cursor;
171   struct wl_visual *visual;
172   int stride, fd;
173   char *filename;
174   GError *error = NULL;
175
176   cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
177                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
178                          "display", display,
179                          NULL);
180   cursor->name = NULL;
181   cursor->serial = theme_serial;
182   cursor->x = x;
183   cursor->y = y;
184   cursor->width = gdk_pixbuf_get_width (pixbuf);
185   cursor->height = gdk_pixbuf_get_height (pixbuf);
186
187   stride = cursor->width * 4;
188   cursor->size = stride * cursor->height;
189
190   fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
191   if (fd < 0) {
192     fprintf(stderr, "g_file_open_tmp failed: %s\n", error->message);
193     g_error_free (error);
194     return NULL;
195   }
196
197   unlink (filename);
198   g_free (filename);
199
200   if (ftruncate(fd, cursor->size) < 0) {
201     fprintf(stderr, "ftruncate failed: %m\n");
202     close(fd);
203     return NULL;
204   }
205
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");
210     close(fd);
211     return NULL;
212   }
213
214   set_pixbuf (cursor, pixbuf);
215
216   visual = wl_display_get_premultiplied_argb_visual(display->wl_display);
217   cursor->buffer = wl_shm_create_buffer(display->shm,
218                                         fd,
219                                         cursor->width,
220                                         cursor->height,
221                                         stride, visual);
222
223   close(fd);
224
225   return GDK_CURSOR (cursor);
226 }
227
228 #define DATADIR "/usr/share/wayland"
229
230 static const struct {
231   GdkCursorType type;
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 }
237 };
238
239 GdkCursor *
240 _gdk_wayland_display_get_cursor_for_type (GdkDisplay    *display,
241                                           GdkCursorType  cursor_type)
242 {
243   GdkDisplayWayland *wayland_display;
244   GdkPixbuf *pixbuf;
245   GError *error = NULL;
246   int i;
247
248   for (i = 0; i < G_N_ELEMENTS (cursor_definitions); i++)
249     {
250       if (cursor_definitions[i].type == cursor_type)
251         break;
252     }
253
254   if (i == G_N_ELEMENTS (cursor_definitions))
255     return NULL;
256
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]);
263
264   pixbuf = gdk_pixbuf_new_from_file(cursor_definitions[i].filename, &error);
265   if (error != NULL)
266     {
267       g_error_free(error);
268       return NULL;
269     }
270
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);
276
277   return g_object_ref (wayland_display->cursors[i]);
278 }
279
280 GdkCursor*
281 _gdk_wayland_display_get_cursor_for_name (GdkDisplay  *display,
282                                           const gchar *name)
283 {
284   GdkWaylandCursor *private;
285
286   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
287
288   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
289                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
290                           "display", display,
291                           NULL);
292   private->name = g_strdup (name);
293   private->serial = theme_serial;
294
295   return GDK_CURSOR (private);
296 }
297
298 GdkCursor *
299 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
300                                             GdkPixbuf  *pixbuf,
301                                             gint        x,
302                                             gint        y)
303 {
304   GdkWaylandCursor *private;
305
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);
310
311   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
312                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
313                           "display", display,
314                           NULL);
315
316   private->name = NULL;
317   private->serial = theme_serial;
318
319   return GDK_CURSOR (private);
320 }
321
322 void
323 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
324                                               guint       *width,
325                                               guint       *height)
326 {
327   /* FIXME: wayland settings? */
328   *width = 64;
329   *height = 64;
330 }
331
332 void
333 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
334                                               guint       *width,
335                                               guint       *height)
336 {
337   *width = 256;
338   *height = 256;
339 }
340
341 gboolean
342 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
343 {
344   return TRUE;
345 }
346
347 gboolean
348 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)
349 {
350   return TRUE;
351 }