]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkcursor-wayland.c
wayland: data_device.data_offer now has a safe object wrapper
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
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/. 
23  */
24
25 #include "config.h"
26
27 #define GDK_PIXBUF_ENABLE_BACKEND
28
29 #include <string.h>
30
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>
36
37 #include <wayland-cursor.h>
38
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))
45
46 typedef struct _GdkWaylandCursor GdkWaylandCursor;
47 typedef struct _GdkWaylandCursorClass GdkWaylandCursorClass;
48
49 struct _GdkWaylandCursor
50 {
51   GdkCursor cursor;
52   gchar *name;
53   guint serial;
54   int hotspot_x, hotspot_y;
55   int width, height;
56   struct wl_buffer *buffer;
57 };
58
59 struct _GdkWaylandCursorClass
60 {
61   GdkCursorClass cursor_class;
62 };
63
64 G_DEFINE_TYPE (GdkWaylandCursor, _gdk_wayland_cursor, GDK_TYPE_CURSOR)
65
66 static guint theme_serial = 0;
67
68 static void
69 gdk_wayland_cursor_finalize (GObject *object)
70 {
71   GdkWaylandCursor *cursor = GDK_WAYLAND_CURSOR (object);
72
73   g_free (cursor->name);
74
75   G_OBJECT_CLASS (_gdk_wayland_cursor_parent_class)->finalize (object);
76 }
77
78 static GdkPixbuf*
79 gdk_wayland_cursor_get_image (GdkCursor *cursor)
80 {
81   return NULL;
82 }
83
84 struct wl_buffer *
85 _gdk_wayland_cursor_get_buffer (GdkCursor *cursor,
86                                 int       *x,
87                                 int       *y,
88                                 int       *w,
89                                 int       *h)
90 {
91   GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
92
93   *x = wayland_cursor->hotspot_x;
94   *y = wayland_cursor->hotspot_y;
95
96   *w = wayland_cursor->width;
97   *h = wayland_cursor->height;
98
99   return wayland_cursor->buffer;
100 }
101
102 static void
103 _gdk_wayland_cursor_class_init (GdkWaylandCursorClass *wayland_cursor_class)
104 {
105   GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (wayland_cursor_class);
106   GObjectClass *object_class = G_OBJECT_CLASS (wayland_cursor_class);
107
108   object_class->finalize = gdk_wayland_cursor_finalize;
109
110   cursor_class->get_image = gdk_wayland_cursor_get_image;
111 }
112
113 static void
114 _gdk_wayland_cursor_init (GdkWaylandCursor *cursor)
115 {
116 }
117
118 /* Use to implement from_pixbuf below */
119 #if 0
120 static void
121 set_pixbuf (GdkWaylandCursor *cursor, GdkPixbuf *pixbuf)
122 {
123   int stride, i, n_channels;
124   unsigned char *pixels, *end, *argb_pixels, *s, *d;
125
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;
130
131 #define MULT(_d,c,a,t) \
132         do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
133
134   if (n_channels == 4)
135     {
136       for (i = 0; i < cursor->height; i++)
137         {
138           s = pixels + i * stride;
139           end = s + cursor->width * 4;
140           d = argb_pixels + i * cursor->width * 4;
141           while (s < end)
142             {
143               unsigned int t;
144
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);
148               d[3] = s[3];
149               s += 4;
150               d += 4;
151             }
152         }
153     }
154   else if (n_channels == 3)
155     {
156       for (i = 0; i < cursor->height; i++)
157         {
158           s = pixels + i * stride;
159           end = s + cursor->width * 3;
160           d = argb_pixels + i * cursor->width * 4;
161           while (s < end)
162             {
163               d[0] = s[2];
164               d[1] = s[1];
165               d[2] = s[0];
166               d[3] = 0xff;
167               s += 3;
168               d += 4;
169             }
170         }
171     }
172 }
173
174 static GdkCursor *
175 create_cursor(GdkWaylandDisplay *display, GdkPixbuf *pixbuf, int x, int y)
176 {
177   GdkWaylandCursor *cursor;
178   int stride, fd;
179   char *filename;
180   GError *error = NULL;
181   struct wl_shm_pool *pool;
182
183   cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
184                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
185                          "display", display,
186                          NULL);
187   cursor->name = NULL;
188   cursor->serial = theme_serial;
189   cursor->x = x;
190   cursor->y = y;
191   if (pixbuf)
192     {
193       cursor->width = gdk_pixbuf_get_width (pixbuf);
194       cursor->height = gdk_pixbuf_get_height (pixbuf);
195     }
196   else
197     {
198       cursor->width = 1;
199       cursor->height = 1;
200     }
201
202   stride = cursor->width * 4;
203   cursor->size = stride * cursor->height;
204
205   fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
206   if (fd < 0)
207     {
208       g_critical (G_STRLOC ": Error opening temporary file for buffer: %s",
209                   error->message);
210       g_error_free (error);
211       return NULL;
212   }
213
214   unlink (filename);
215   g_free (filename);
216
217   if (ftruncate(fd, cursor->size) < 0)
218     {
219       g_critical (G_STRLOC ": Error truncating file for buffer: %s",
220                   g_strerror (errno));
221       close(fd);
222       return NULL;
223     }
224
225   cursor->map = mmap(NULL, cursor->size,
226                      PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
227   if (cursor->map == MAP_FAILED)
228     {
229       g_critical (G_STRLOC ": Error mmap'ing file for buffer: %s",
230                   g_strerror (errno));
231       close(fd);
232       return NULL;
233     }
234
235   if (pixbuf)
236     set_pixbuf (cursor, pixbuf);
237   else
238     memset (cursor->map, 0, 4);
239
240   cursor->buffer = wl_shm_create_buffer(display->shm,
241                                         fd,
242                                         cursor->width,
243                                         cursor->height,
244                                         stride, WL_SHM_FORMAT_ARGB8888);
245
246   close(fd);
247
248  return GDK_CURSOR (cursor);
249 }
250 #endif
251
252 /* TODO: Extend this table */
253 static const struct {
254   GdkCursorType type;
255   const gchar *cursor_name;
256 } cursor_mapping[] = {
257   { GDK_BLANK_CURSOR,          NULL   },
258   { GDK_HAND1,                "hand1" },
259   { GDK_HAND2,                "hand2" },
260   { GDK_LEFT_PTR,             "left_ptr" },
261   { GDK_SB_H_DOUBLE_ARROW,    "sb_h_double_arrow" },
262   { GDK_SB_V_DOUBLE_ARROW,    "sb_v_double_arrow" },
263   { GDK_XTERM,                "xterm" },
264   { GDK_BOTTOM_RIGHT_CORNER,  "bottom_right_corner" }
265 };
266
267 GdkCursor *
268 _gdk_wayland_display_get_cursor_for_type (GdkDisplay    *display,
269                                           GdkCursorType  cursor_type)
270 {
271   int i;
272
273   for (i = 0; i < G_N_ELEMENTS (cursor_mapping); i++)
274     {
275       if (cursor_mapping[i].type == cursor_type)
276         break;
277     }
278
279   if (i == G_N_ELEMENTS (cursor_mapping))
280     {
281       g_warning ("Unhandled cursor type %d, falling back to blank\n",
282                  cursor_type);
283       i = 0;
284     }
285
286   return _gdk_wayland_display_get_cursor_for_name (display,
287                                                    cursor_mapping[i].cursor_name);
288 }
289
290 GdkCursor *
291 _gdk_wayland_display_get_cursor_for_name (GdkDisplay  *display,
292                                           const gchar *name)
293 {
294   GdkWaylandCursor *private;
295   GdkWaylandDisplay *wayland_display = GDK_WAYLAND_DISPLAY (display);
296   struct wl_cursor *cursor;
297
298   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
299
300   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
301                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
302                           "display", display,
303                           NULL);
304   private->name = g_strdup (name);
305   private->serial = theme_serial;
306
307   /* Blank cursor case */
308   if (!name)
309     return GDK_CURSOR (private);
310
311   cursor = wl_cursor_theme_get_cursor (wayland_display->cursor_theme,
312                                        name);
313
314   if (!cursor)
315     {
316       g_warning (G_STRLOC ": Unable to load %s from the cursor theme", name);
317       g_object_unref (private);
318       return NULL;
319     }
320
321   /* TODO: Do something clever so we can do animated cursors - move the
322    * wl_pointer_set_cursor to a function here so that we can do the magic to
323    * iterate through
324    */
325   private->hotspot_x = cursor->images[0]->hotspot_x;
326   private->hotspot_y = cursor->images[0]->hotspot_y;
327   private->width = cursor->images[0]->width;
328   private->height = cursor->images[0]->height;
329
330   private->buffer = wl_cursor_image_get_buffer(cursor->images[0]);
331
332   return GDK_CURSOR (private);
333 }
334
335 /* TODO: Needs implementing */
336 GdkCursor *
337 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
338                                             GdkPixbuf  *pixbuf,
339                                             gint        x,
340                                             gint        y)
341 {
342   GdkWaylandCursor *private;
343
344   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
345   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
346   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
347   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
348
349   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
350                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
351                           "display", display,
352                           NULL);
353
354   private->name = NULL;
355   private->serial = theme_serial;
356
357   return GDK_CURSOR (private);
358 }
359
360 void
361 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
362                                               guint       *width,
363                                               guint       *height)
364 {
365   *width = 32;
366   *height = 32;
367 }
368
369 void
370 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
371                                               guint       *width,
372                                               guint       *height)
373 {
374   *width = 256;
375   *height = 256;
376 }
377
378 gboolean
379 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
380 {
381   return TRUE;
382 }
383
384 gboolean
385 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)
386 {
387   return TRUE;
388 }