]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkcursor-wayland.c
6e3e14b438dd7730be363020c331c1bbcb730896
[~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_ARGB8888);
231
232   close(fd);
233
234   return GDK_CURSOR (cursor);
235 }
236
237 static const struct {
238   GdkCursorType type;
239   const char *filename;
240   int hotspot_x, hotspot_y;
241 } cursor_definitions[] = {
242   { GDK_BLANK_CURSOR, NULL, 0, 0 },
243   { GDK_HAND1, "hand1.png", 18, 11 },
244   { GDK_HAND2, "hand2.png", 14,  8 },
245   { GDK_LEFT_PTR, "left_ptr.png", 10, 5 },
246   { GDK_SB_H_DOUBLE_ARROW, "sb_h_double_arrow.png", 15, 15 },
247   { GDK_SB_V_DOUBLE_ARROW, "sb_v_double_arrow.png", 15, 15 },
248   { GDK_XTERM, "xterm.png", 15, 15 },
249   { GDK_BOTTOM_RIGHT_CORNER, "bottom_right_corner.png", 28, 28 }
250 };
251
252 GdkCursor *
253 _gdk_wayland_display_get_cursor_for_type (GdkDisplay    *display,
254                                           GdkCursorType  cursor_type)
255 {
256   GdkDisplayWayland *wayland_display;
257   GdkPixbuf *pixbuf = NULL;
258   GError *error = NULL;
259   int i;
260
261   for (i = 0; i < G_N_ELEMENTS (cursor_definitions); i++)
262     {
263       if (cursor_definitions[i].type == cursor_type)
264         break;
265     }
266
267   if (i == G_N_ELEMENTS (cursor_definitions))
268     {
269       g_warning ("Unhandled cursor type %d, falling back to blank\n",
270                  cursor_type);
271       i = 0;
272     }
273
274   wayland_display = GDK_DISPLAY_WAYLAND (display);
275   if (!wayland_display->cursors)
276     wayland_display->cursors =
277       g_new0 (GdkCursor *, G_N_ELEMENTS(cursor_definitions));
278   if (wayland_display->cursors[i])
279     return g_object_ref (wayland_display->cursors[i]);
280
281   GDK_NOTE (CURSOR,
282             g_message ("Creating new cursor for type %d, filename %s",
283                        cursor_type, cursor_definitions[i].filename));
284
285   if (cursor_type != GDK_BLANK_CURSOR)
286     {
287       const gchar * const *directories;
288       gint j;
289
290       directories = g_get_system_data_dirs();
291
292       for (j = 0; directories[j] != NULL; j++)
293         {
294           gchar *filename;
295           filename = g_build_filename (directories[j],
296                                        "weston",
297                                        cursor_definitions[i].filename,
298                                        NULL);
299           if (g_file_test (filename, G_FILE_TEST_EXISTS))
300             {
301               pixbuf = gdk_pixbuf_new_from_file (filename, &error);
302
303               if (error != NULL)
304                 {
305                   g_warning ("Failed to load cursor: %s: %s",
306                              filename, error->message);
307                   g_error_free(error);
308                   return NULL;
309                 }
310               break;
311             }
312         }
313
314       if (!pixbuf)
315         {
316           g_warning ("Unable to find cursor for: %s",
317                      cursor_definitions[i].filename);
318           return NULL;
319         }
320     }
321
322   wayland_display->cursors[i] =
323     create_cursor(wayland_display, pixbuf,
324                   cursor_definitions[i].hotspot_x,
325                   cursor_definitions[i].hotspot_y);
326   if (pixbuf)
327     g_object_unref (pixbuf);
328
329   return g_object_ref (wayland_display->cursors[i]);
330 }
331
332 GdkCursor*
333 _gdk_wayland_display_get_cursor_for_name (GdkDisplay  *display,
334                                           const gchar *name)
335 {
336   GdkWaylandCursor *private;
337
338   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
339
340   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
341                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
342                           "display", display,
343                           NULL);
344   private->name = g_strdup (name);
345   private->serial = theme_serial;
346
347   return GDK_CURSOR (private);
348 }
349
350 GdkCursor *
351 _gdk_wayland_display_get_cursor_for_pixbuf (GdkDisplay *display,
352                                             GdkPixbuf  *pixbuf,
353                                             gint        x,
354                                             gint        y)
355 {
356   GdkWaylandCursor *private;
357
358   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
359   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
360   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
361   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
362
363   private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
364                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
365                           "display", display,
366                           NULL);
367
368   private->name = NULL;
369   private->serial = theme_serial;
370
371   return GDK_CURSOR (private);
372 }
373
374 void
375 _gdk_wayland_display_get_default_cursor_size (GdkDisplay *display,
376                                               guint       *width,
377                                               guint       *height)
378 {
379   /* FIXME: wayland settings? */
380   *width = 64;
381   *height = 64;
382 }
383
384 void
385 _gdk_wayland_display_get_maximal_cursor_size (GdkDisplay *display,
386                                               guint       *width,
387                                               guint       *height)
388 {
389   *width = 256;
390   *height = 256;
391 }
392
393 gboolean
394 _gdk_wayland_display_supports_cursor_alpha (GdkDisplay *display)
395 {
396   return TRUE;
397 }
398
399 gboolean
400 _gdk_wayland_display_supports_cursor_color (GdkDisplay *display)
401 {
402   return TRUE;
403 }