]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkcursor-wayland.c
x11: Make stored xsettings window a GdkWindow
[~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 GdkCursor *
253 _gdk_wayland_display_get_cursor_for_type (GdkDisplay    *display,
254                                           GdkCursorType  cursor_type)
255 {
256   GEnumClass *enum_class;
257   GEnumValue *enum_value;
258   gchar *cursor_name;
259   GdkCursor *result;
260
261   enum_class = g_type_class_ref (GDK_TYPE_CURSOR_TYPE);
262   enum_value = g_enum_get_value (enum_class, cursor_type);
263   cursor_name = g_strdup (enum_value->value_nick);
264   g_strdelimit (cursor_name, "-", '_');
265   g_type_class_unref (enum_class);
266
267   result = _gdk_wayland_display_get_cursor_for_name (display, cursor_name);
268
269   g_free (cursor_name);
270
271   return result;
272 }
273
274 GdkCursor *
275 _gdk_wayland_display_get_cursor_for_name (GdkDisplay  *display,
276                                           const gchar *name)
277 {
278   GdkWaylandCursor *private;
279   GdkWaylandDisplay *wayland_display = GDK_WAYLAND_DISPLAY (display);
280   struct wl_cursor *cursor;
281
282   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
283
284   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
285                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
286                           "display", display,
287                           NULL);
288   private->name = g_strdup (name);
289   private->serial = theme_serial;
290
291   /* Blank cursor case */
292   if (!name || g_str_equal (name, "blank_cursor"))
293     return GDK_CURSOR (private);
294
295   cursor = wl_cursor_theme_get_cursor (wayland_display->cursor_theme,
296                                        name);
297
298   if (!cursor)
299     {
300       g_warning (G_STRLOC ": Unable to load %s from the cursor theme", name);
301
302       /* return the left_ptr cursor as a fallback */
303       cursor = wl_cursor_theme_get_cursor (wayland_display->cursor_theme,
304                                            "left_ptr");
305
306       /* if the fallback failed to load, return a blank pointer */
307       if (!cursor)
308         return GDK_CURSOR (private);
309     }
310
311   /* TODO: Do something clever so we can do animated cursors - move the
312    * wl_pointer_set_cursor to a function here so that we can do the magic to
313    * iterate through
314    */
315   private->hotspot_x = cursor->images[0]->hotspot_x;
316   private->hotspot_y = cursor->images[0]->hotspot_y;
317   private->width = cursor->images[0]->width;
318   private->height = cursor->images[0]->height;
319
320   private->buffer = wl_cursor_image_get_buffer(cursor->images[0]);
321
322   return GDK_CURSOR (private);
323 }
324
325 /* TODO: Needs implementing */
326 GdkCursor *
327 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
328                                             GdkPixbuf  *pixbuf,
329                                             gint        x,
330                                             gint        y)
331 {
332   GdkWaylandCursor *private;
333
334   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
335   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
336   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
337   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
338
339   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
340                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
341                           "display", display,
342                           NULL);
343
344   private->name = NULL;
345   private->serial = theme_serial;
346
347   return GDK_CURSOR (private);
348 }
349
350 void
351 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
352                                               guint       *width,
353                                               guint       *height)
354 {
355   *width = 32;
356   *height = 32;
357 }
358
359 void
360 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
361                                               guint       *width,
362                                               guint       *height)
363 {
364   *width = 256;
365   *height = 256;
366 }
367
368 gboolean
369 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
370 {
371   return TRUE;
372 }
373
374 gboolean
375 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)
376 {
377   return TRUE;
378 }