summaryrefslogtreecommitdiff
path: root/gdk/wayland/gdkcursor-wayland.c
blob: 3358a8184a2afbb672c5d27c5a2b206540a96070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include "config.h"

#define GDK_PIXBUF_ENABLE_BACKEND

#include <string.h>

#include "gdkprivate-wayland.h"
#include "gdkcursorprivate.h"
#include "gdkdisplay-wayland.h"
#include "gdkwayland.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "cursor/wayland-cursor.h"

static void
gdk_wayland_cursor_remove_from_cache (gpointer data, GObject *cursor)
{
  GdkWaylandDisplay *display = data;

  g_hash_table_remove (display->cursor_surface_cache, cursor);
}

void
_gdk_wayland_display_init_cursors (GdkWaylandDisplay *display)
{
  display->cursor_surface_cache = g_hash_table_new_full (gdk_cursor_hash, gdk_cursor_equal, NULL, (GDestroyNotify) cairo_surface_destroy);
}

void
_gdk_wayland_display_finalize_cursors (GdkWaylandDisplay *display)
{
  GHashTableIter iter;
  gpointer cursor;

  g_hash_table_iter_init (&iter, display->cursor_surface_cache);
  while (g_hash_table_iter_next (&iter, &cursor, NULL))
    g_object_weak_unref (G_OBJECT (cursor), gdk_wayland_cursor_remove_from_cache, display);
  g_hash_table_destroy (display->cursor_surface_cache);
}

static const struct {
  const gchar *css_name, *traditional_name;
} name_map[] = {
  { "default",      "left_ptr" },
  { "help",         "question_arrow" },
  { "context-menu", "left_ptr" },
  { "pointer",      "hand" },
  { "progress",     "left_ptr_watch" },
  { "wait",         "watch" },
  { "cell",         "crosshair" },
  { "crosshair",    "cross" },
  { "text",         "xterm" },
  { "vertical-text","xterm" },
  { "alias",        "dnd-link" },
  { "copy",         "dnd-copy" },
  { "move",         "dnd-move" },
  { "no-drop",      "dnd-none" },
  { "dnd-ask",      "dnd-copy" }, /* not CSS, but we want to guarantee it anyway */
  { "not-allowed",  "crossed_circle" },
  { "grab",         "hand2" },
  { "grabbing",     "hand2" },
  { "all-scroll",   "left_ptr" },
  { "col-resize",   "h_double_arrow" },
  { "row-resize",   "v_double_arrow" },
  { "n-resize",     "top_side" },
  { "e-resize",     "right_side" },
  { "s-resize",     "bottom_side" },
  { "w-resize",     "left_side" },
  { "ne-resize",    "top_right_corner" },
  { "nw-resize",    "top_left_corner" },
  { "se-resize",    "bottom_right_corner" },
  { "sw-resize",    "bottom_left_corner" },
  { "ew-resize",    "h_double_arrow" },
  { "ns-resize",    "v_double_arrow" },
  { "nesw-resize",  "fd_double_arrow" },
  { "nwse-resize",  "bd_double_arrow" },
  { "zoom-in",      "left_ptr" },
  { "zoom-out",     "left_ptr" }
};

static const gchar *
name_fallback (const gchar *name)
{
  int i;

  for (i = 0; i < G_N_ELEMENTS (name_map); i++)
    {
      if (g_str_equal (name_map[i].css_name, name))
        return name_map[i].traditional_name;
    }

  return NULL;
}

static struct wl_cursor *
gdk_wayland_cursor_load_for_name (GdkWaylandDisplay      *display_wayland,
                                  struct wl_cursor_theme *theme,
                                  int                     scale,
                                  const char             *name)
{
  struct wl_cursor *c;

  c = wl_cursor_theme_get_cursor (theme, name, scale);
  if (!c)
    {
      const char *fallback;

      fallback = name_fallback (name);
      if (fallback)
        {
          c = wl_cursor_theme_get_cursor (theme, fallback, scale);
        }
    }

  return c;
}

static void
buffer_release_callback (void             *_data,
                         struct wl_buffer *wl_buffer)
{
  cairo_surface_t *cairo_surface = _data;

  cairo_surface_destroy (cairo_surface);
}

static const struct wl_buffer_listener buffer_listener = {
  buffer_release_callback
};

struct wl_buffer *
_gdk_wayland_cursor_get_buffer (GdkWaylandDisplay *display,
                                GdkCursor         *cursor,
                                guint              desired_scale,
                                guint              image_index,
                                int               *hotspot_x,
                                int               *hotspot_y,
                                int               *width,
                                int               *height,
                                int               *scale)
{
  GdkTexture *texture;

  if (gdk_cursor_get_name (cursor))
    {
      struct wl_cursor *c;

      if (g_str_equal (gdk_cursor_get_name (cursor), "none"))
        goto none;

      c = gdk_wayland_cursor_load_for_name (display,
                                            _gdk_wayland_display_get_cursor_theme (display),
                                            desired_scale,
                                            gdk_cursor_get_name (cursor));
      if (c)
        {
          struct wl_cursor_image *image;

          if (image_index >= c->image_count)
            {
              g_warning (G_STRLOC " out of bounds cursor image [%d / %d]",
                         image_index,
                         c->image_count - 1);
              image_index = 0;
            }

          image = c->images[image_index];

          *hotspot_x = image->hotspot_x / desired_scale;
          *hotspot_y = image->hotspot_y / desired_scale;

          *width = image->width / desired_scale;
          *height = image->height / desired_scale;
          *scale = desired_scale;

          return wl_cursor_image_get_buffer (image);
        }
    }
  else
    {
      cairo_surface_t *surface;
      struct wl_buffer *buffer;

      texture = gdk_cursor_get_texture (cursor);

from_texture:
      surface = g_hash_table_lookup (display->cursor_surface_cache, cursor);
      if (surface == NULL)
        {
          surface = _gdk_wayland_display_create_shm_surface (display,
                                                             gdk_texture_get_width (texture),
                                                             gdk_texture_get_height (texture),
                                                             1);
          
          gdk_texture_download (texture,
                                cairo_image_surface_get_data (surface),
                                cairo_image_surface_get_stride (surface));
          cairo_surface_mark_dirty (surface);

          g_object_weak_ref (G_OBJECT (cursor), gdk_wayland_cursor_remove_from_cache, display);
          g_hash_table_insert (display->cursor_surface_cache, cursor, surface);
        }

      *hotspot_x = gdk_cursor_get_hotspot_x (cursor);
      *hotspot_y = gdk_cursor_get_hotspot_y (cursor);
      *width = gdk_texture_get_width (texture);
      *height = gdk_texture_get_height (texture);
      *scale = 1;

      cairo_surface_reference (surface);
      buffer = _gdk_wayland_shm_surface_get_wl_buffer (surface);
      wl_buffer_add_listener (buffer, &buffer_listener, surface);

      return buffer;
    }

  if (gdk_cursor_get_fallback (cursor))
    return _gdk_wayland_cursor_get_buffer (display,
                                           gdk_cursor_get_fallback (cursor), 
                                           desired_scale,
                                           image_index,
                                           hotspot_x, hotspot_y,
                                           width, height,
                                           scale);
  else
    {
      texture = gdk_texture_new_from_resource ("/org/gtk/libgdk/cursor/default");
      goto from_texture;
    }

none:
  *hotspot_x = 0;
  *hotspot_y = 0;
  *width = 0;
  *height = 0;
  *scale = 1;

  return NULL;
}

guint
_gdk_wayland_cursor_get_next_image_index (GdkWaylandDisplay *display,
                                          GdkCursor         *cursor,
                                          guint              scale,
                                          guint              current_image_index,
                                          guint             *next_image_delay)
{
  struct wl_cursor *c;

  if (!gdk_cursor_get_name (cursor) ||
      g_str_equal (gdk_cursor_get_name (cursor), "none"))
    {
      *next_image_delay = 0;
      return current_image_index;
    }

  c = gdk_wayland_cursor_load_for_name (display,
                                        _gdk_wayland_display_get_cursor_theme (display),
                                        scale,
                                        gdk_cursor_get_name (cursor));

  if (c)
    {
      if (current_image_index >= c->image_count)
        {
          g_warning (G_STRLOC " out of bounds cursor image [%d / %d]",
                     current_image_index, c->image_count - 1);
          current_image_index = 0;
        }

      if (c->image_count == 1)
        {
          *next_image_delay = 0;
          return current_image_index;
        }
      else
        {
          *next_image_delay = c->images[current_image_index]->delay;
          return (current_image_index + 1) % c->image_count;
        }
    }

  if (gdk_cursor_get_fallback (cursor))
    return _gdk_wayland_cursor_get_next_image_index (display,
                                                     gdk_cursor_get_fallback (cursor),
                                                     scale,
                                                     current_image_index,
                                                     next_image_delay);

  *next_image_delay = 0;
  return current_image_index;
}