]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkcursor-wayland.c
wayland: Update to recent API changes
[~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   int stride, fd;
172   char *filename;
173   GError *error = NULL;
174
175   cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
176                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
177                          "display", display,
178                          NULL);
179   cursor->name = NULL;
180   cursor->serial = theme_serial;
181   cursor->x = x;
182   cursor->y = y;
183   if (pixbuf)
184     {
185       cursor->width = gdk_pixbuf_get_width (pixbuf);
186       cursor->height = gdk_pixbuf_get_height (pixbuf);
187     }
188   else
189     {
190       cursor->width = 1;
191       cursor->height = 1;
192     }
193
194   stride = cursor->width * 4;
195   cursor->size = stride * cursor->height;
196
197   fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
198   if (fd < 0) {
199     fprintf(stderr, "g_file_open_tmp failed: %s\n", error->message);
200     g_error_free (error);
201     return NULL;
202   }
203
204   unlink (filename);
205   g_free (filename);
206
207   if (ftruncate(fd, cursor->size) < 0) {
208     fprintf(stderr, "ftruncate failed: %m\n");
209     close(fd);
210     return NULL;
211   }
212
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");
217     close(fd);
218     return NULL;
219   }
220
221   if (pixbuf)
222     set_pixbuf (cursor, pixbuf);
223   else
224     memset (cursor->map, 0, 4);
225
226   cursor->buffer = wl_shm_create_buffer(display->shm,
227                                         fd,
228                                         cursor->width,
229                                         cursor->height,
230                                         stride, WL_SHM_FORMAT_ARGB32);
231
232   close(fd);
233
234   return GDK_CURSOR (cursor);
235 }
236
237 #define DATADIR "/usr/share/wayland"
238
239 static const struct {
240   GdkCursorType type;
241   const char *filename;
242   int hotspot_x, hotspot_y;
243 } cursor_definitions[] = {
244   { GDK_BLANK_CURSOR, NULL, 0, 0 },
245   { GDK_HAND1, DATADIR "/hand1.png", 18, 11 },
246   { GDK_HAND2, DATADIR "/hand2.png", 14,  8 },
247   { GDK_LEFT_PTR, DATADIR "/left_ptr.png", 10, 5 },
248   { GDK_SB_H_DOUBLE_ARROW, DATADIR "/sb_h_double_arrow.png", 15, 15 },
249   { GDK_SB_V_DOUBLE_ARROW, DATADIR "/sb_v_double_arrow.png", 15, 15 },
250   { GDK_XTERM, DATADIR "/xterm.png", 15, 15 },
251   { GDK_BOTTOM_RIGHT_CORNER, DATADIR "/bottom_right_corner.png", 28, 28 }
252 };
253
254 GdkCursor *
255 _gdk_wayland_display_get_cursor_for_type (GdkDisplay    *display,
256                                           GdkCursorType  cursor_type)
257 {
258   GdkDisplayWayland *wayland_display;
259   GdkPixbuf *pixbuf;
260   GError *error = NULL;
261   int i;
262
263   for (i = 0; i < G_N_ELEMENTS (cursor_definitions); i++)
264     {
265       if (cursor_definitions[i].type == cursor_type)
266         break;
267     }
268
269   if (i == G_N_ELEMENTS (cursor_definitions))
270     {
271       g_warning("unhandled cursor type %d, falling back to blank\n",
272                 cursor_type);
273       i = 0;
274     }
275
276   wayland_display = GDK_DISPLAY_WAYLAND (display);
277   if (!wayland_display->cursors)
278     wayland_display->cursors =
279       g_new0 (GdkCursor *, G_N_ELEMENTS(cursor_definitions));
280   if (wayland_display->cursors[i])
281     return g_object_ref (wayland_display->cursors[i]);
282
283   GDK_NOTE (CURSOR,
284             g_message ("creating new cursor for type %d, filename %s",
285                        cursor_type, cursor_definitions[i].filename));
286
287   if (cursor_type != GDK_BLANK_CURSOR)
288     pixbuf = gdk_pixbuf_new_from_file(cursor_definitions[i].filename, &error);
289   else
290     pixbuf = NULL;
291   if (error != NULL)
292     {
293       GDK_NOTE (CURSOR,
294                 g_message ("failed to load %s: %s",
295                            cursor_definitions[i].filename, error->message));
296       g_error_free(error);
297       return NULL;
298     }
299
300   wayland_display->cursors[i] =
301     create_cursor(wayland_display, pixbuf,
302                   cursor_definitions[i].hotspot_x,
303                   cursor_definitions[i].hotspot_y);
304   if (pixbuf)
305     g_object_unref (pixbuf);
306
307   return g_object_ref (wayland_display->cursors[i]);
308 }
309
310 GdkCursor*
311 _gdk_wayland_display_get_cursor_for_name (GdkDisplay  *display,
312                                           const gchar *name)
313 {
314   GdkWaylandCursor *private;
315
316   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
317
318   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
319                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
320                           "display", display,
321                           NULL);
322   private->name = g_strdup (name);
323   private->serial = theme_serial;
324
325   return GDK_CURSOR (private);
326 }
327
328 GdkCursor *
329 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
330                                             GdkPixbuf  *pixbuf,
331                                             gint        x,
332                                             gint        y)
333 {
334   GdkWaylandCursor *private;
335
336   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
337   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
338   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
339   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
340
341   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
342                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
343                           "display", display,
344                           NULL);
345
346   private->name = NULL;
347   private->serial = theme_serial;
348
349   return GDK_CURSOR (private);
350 }
351
352 void
353 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
354                                               guint       *width,
355                                               guint       *height)
356 {
357   /* FIXME: wayland settings? */
358   *width = 64;
359   *height = 64;
360 }
361
362 void
363 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
364                                               guint       *width,
365                                               guint       *height)
366 {
367   *width = 256;
368   *height = 256;
369 }
370
371 gboolean
372 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
373 {
374   return TRUE;
375 }
376
377 gboolean
378 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)
379 {
380   return TRUE;
381 }