]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkcursor-wayland.c
Change FSF Address
[~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 <sys/mman.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 x, y, width, height, size;
55   void *map;
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, int *x, int *y)
86 {
87   GdkWaylandCursor *wayland_cursor = GDK_WAYLAND_CURSOR (cursor);
88
89   *x = wayland_cursor->x;
90   *y = wayland_cursor->y;
91
92   return wayland_cursor->buffer;
93 }
94
95 static void
96 _gdk_wayland_cursor_class_init (GdkWaylandCursorClass *wayland_cursor_class)
97 {
98   GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (wayland_cursor_class);
99   GObjectClass *object_class = G_OBJECT_CLASS (wayland_cursor_class);
100
101   object_class->finalize = gdk_wayland_cursor_finalize;
102
103   cursor_class->get_image = gdk_wayland_cursor_get_image;
104 }
105
106 static void
107 _gdk_wayland_cursor_init (GdkWaylandCursor *cursor)
108 {
109 }
110
111 static void
112 set_pixbuf (GdkWaylandCursor *cursor, GdkPixbuf *pixbuf)
113 {
114   int stride, i, n_channels;
115   unsigned char *pixels, *end, *argb_pixels, *s, *d;
116
117   stride = gdk_pixbuf_get_rowstride(pixbuf);
118   pixels = gdk_pixbuf_get_pixels(pixbuf);
119   n_channels = gdk_pixbuf_get_n_channels(pixbuf);
120   argb_pixels = cursor->map;
121
122 #define MULT(_d,c,a,t) \
123         do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
124
125   if (n_channels == 4)
126     {
127       for (i = 0; i < cursor->height; i++)
128         {
129           s = pixels + i * stride;
130           end = s + cursor->width * 4;
131           d = argb_pixels + i * cursor->width * 4;
132           while (s < end)
133             {
134               unsigned int t;
135
136               MULT(d[0], s[2], s[3], t);
137               MULT(d[1], s[1], s[3], t);
138               MULT(d[2], s[0], s[3], t);
139               d[3] = s[3];
140               s += 4;
141               d += 4;
142             }
143         }
144     }
145   else if (n_channels == 3)
146     {
147       for (i = 0; i < cursor->height; i++)
148         {
149           s = pixels + i * stride;
150           end = s + cursor->width * 3;
151           d = argb_pixels + i * cursor->width * 4;
152           while (s < end)
153             {
154               d[0] = s[2];
155               d[1] = s[1];
156               d[2] = s[0];
157               d[3] = 0xff;
158               s += 3;
159               d += 4;
160             }
161         }
162     }
163 }
164
165 static GdkCursor *
166 create_cursor(GdkDisplayWayland *display, GdkPixbuf *pixbuf, int x, int y)
167 {
168   GdkWaylandCursor *cursor;
169   int stride, fd;
170   char *filename;
171   GError *error = NULL;
172
173   cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
174                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
175                          "display", display,
176                          NULL);
177   cursor->name = NULL;
178   cursor->serial = theme_serial;
179   cursor->x = x;
180   cursor->y = y;
181   if (pixbuf)
182     {
183       cursor->width = gdk_pixbuf_get_width (pixbuf);
184       cursor->height = gdk_pixbuf_get_height (pixbuf);
185     }
186   else
187     {
188       cursor->width = 1;
189       cursor->height = 1;
190     }
191
192   stride = cursor->width * 4;
193   cursor->size = stride * cursor->height;
194
195   fd = g_file_open_tmp("wayland-shm-XXXXXX", &filename, &error);
196   if (fd < 0) {
197     fprintf(stderr, "g_file_open_tmp failed: %s\n", error->message);
198     g_error_free (error);
199     return NULL;
200   }
201
202   unlink (filename);
203   g_free (filename);
204
205   if (ftruncate(fd, cursor->size) < 0) {
206     fprintf(stderr, "ftruncate failed: %m\n");
207     close(fd);
208     return NULL;
209   }
210
211   cursor->map = mmap(NULL, cursor->size,
212                      PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
213   if (cursor->map == MAP_FAILED) {
214     fprintf(stderr, "mmap failed: %m\n");
215     close(fd);
216     return NULL;
217   }
218
219   if (pixbuf)
220     set_pixbuf (cursor, pixbuf);
221   else
222     memset (cursor->map, 0, 4);
223
224   cursor->buffer = wl_shm_create_buffer(display->shm,
225                                         fd,
226                                         cursor->width,
227                                         cursor->height,
228                                         stride, WL_SHM_FORMAT_ARGB8888);
229
230   close(fd);
231
232   return GDK_CURSOR (cursor);
233 }
234
235 static const struct {
236   GdkCursorType type;
237   const char *filename;
238   int hotspot_x, hotspot_y;
239 } cursor_definitions[] = {
240   { GDK_BLANK_CURSOR, NULL, 0, 0 },
241   { GDK_HAND1, "hand1.png", 18, 11 },
242   { GDK_HAND2, "hand2.png", 14,  8 },
243   { GDK_LEFT_PTR, "left_ptr.png", 10, 5 },
244   { GDK_SB_H_DOUBLE_ARROW, "sb_h_double_arrow.png", 15, 15 },
245   { GDK_SB_V_DOUBLE_ARROW, "sb_v_double_arrow.png", 15, 15 },
246   { GDK_XTERM, "xterm.png", 15, 15 },
247   { GDK_BOTTOM_RIGHT_CORNER, "bottom_right_corner.png", 28, 28 }
248 };
249
250 GdkCursor *
251 _gdk_wayland_display_get_cursor_for_type (GdkDisplay    *display,
252                                           GdkCursorType  cursor_type)
253 {
254   GdkDisplayWayland *wayland_display;
255   GdkPixbuf *pixbuf = NULL;
256   GError *error = NULL;
257   int i;
258
259   for (i = 0; i < G_N_ELEMENTS (cursor_definitions); i++)
260     {
261       if (cursor_definitions[i].type == cursor_type)
262         break;
263     }
264
265   if (i == G_N_ELEMENTS (cursor_definitions))
266     {
267       g_warning ("Unhandled cursor type %d, falling back to blank\n",
268                  cursor_type);
269       i = 0;
270     }
271
272   wayland_display = GDK_DISPLAY_WAYLAND (display);
273   if (!wayland_display->cursors)
274     wayland_display->cursors =
275       g_new0 (GdkCursor *, G_N_ELEMENTS(cursor_definitions));
276   if (wayland_display->cursors[i])
277     return g_object_ref (wayland_display->cursors[i]);
278
279   GDK_NOTE (CURSOR,
280             g_message ("Creating new cursor for type %d, filename %s",
281                        cursor_type, cursor_definitions[i].filename));
282
283   if (cursor_type != GDK_BLANK_CURSOR)
284     {
285       const gchar * const *directories;
286       gint j;
287
288       directories = g_get_system_data_dirs();
289
290       for (j = 0; directories[j] != NULL; j++)
291         {
292           gchar *filename;
293           filename = g_build_filename (directories[j],
294                                        "weston",
295                                        cursor_definitions[i].filename,
296                                        NULL);
297           if (g_file_test (filename, G_FILE_TEST_EXISTS))
298             {
299               pixbuf = gdk_pixbuf_new_from_file (filename, &error);
300
301               if (error != NULL)
302                 {
303                   g_warning ("Failed to load cursor: %s: %s",
304                              filename, error->message);
305                   g_error_free(error);
306                   return NULL;
307                 }
308               break;
309             }
310         }
311
312       if (!pixbuf)
313         {
314           g_warning ("Unable to find cursor for: %s",
315                      cursor_definitions[i].filename);
316           return NULL;
317         }
318     }
319
320   wayland_display->cursors[i] =
321     create_cursor(wayland_display, pixbuf,
322                   cursor_definitions[i].hotspot_x,
323                   cursor_definitions[i].hotspot_y);
324   if (pixbuf)
325     g_object_unref (pixbuf);
326
327   return g_object_ref (wayland_display->cursors[i]);
328 }
329
330 GdkCursor*
331 _gdk_wayland_display_get_cursor_for_name (GdkDisplay  *display,
332                                           const gchar *name)
333 {
334   GdkWaylandCursor *private;
335
336   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
337
338   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
339                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
340                           "display", display,
341                           NULL);
342   private->name = g_strdup (name);
343   private->serial = theme_serial;
344
345   return GDK_CURSOR (private);
346 }
347
348 GdkCursor *
349 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
350                                             GdkPixbuf  *pixbuf,
351                                             gint        x,
352                                             gint        y)
353 {
354   GdkWaylandCursor *private;
355
356   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
357   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
358   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
359   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
360
361   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
362                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
363                           "display", display,
364                           NULL);
365
366   private->name = NULL;
367   private->serial = theme_serial;
368
369   return GDK_CURSOR (private);
370 }
371
372 void
373 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
374                                               guint       *width,
375                                               guint       *height)
376 {
377   /* FIXME: wayland settings? */
378   *width = 64;
379   *height = 64;
380 }
381
382 void
383 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
384                                               guint       *width,
385                                               guint       *height)
386 {
387   *width = 256;
388   *height = 256;
389 }
390
391 gboolean
392 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
393 {
394   return TRUE;
395 }
396
397 gboolean
398 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)
399 {
400   return TRUE;
401 }