summaryrefslogtreecommitdiff
path: root/src/wayland/meta-wayland-cursor-surface.c
blob: 95f8186df874544cbc6f8189d2963771d5ec101b (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Wayland Support
 *
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "config.h"

#include "wayland/meta-wayland-cursor-surface.h"

#include "backends/meta-backend-private.h"
#include "backends/meta-logical-monitor.h"
#include "cogl/cogl-wayland-server.h"
#include "cogl/cogl.h"
#include "core/boxes-private.h"
#include "wayland/meta-cursor-sprite-wayland.h"
#include "wayland/meta-wayland-buffer.h"
#include "wayland/meta-wayland-private.h"
#include "wayland/meta-xwayland.h"

typedef struct _MetaWaylandCursorSurfacePrivate MetaWaylandCursorSurfacePrivate;

struct _MetaWaylandCursorSurfacePrivate
{
  int hot_x;
  int hot_y;
  MetaCursorSpriteWayland *cursor_sprite;
  MetaCursorRenderer *cursor_renderer;
  MetaWaylandBuffer *buffer;
  struct wl_list frame_callbacks;
  gulong cursor_painted_handler_id;
};

G_DEFINE_TYPE_WITH_PRIVATE (MetaWaylandCursorSurface,
                            meta_wayland_cursor_surface,
                            META_TYPE_WAYLAND_SURFACE_ROLE)

static void
update_cursor_sprite_texture (MetaWaylandCursorSurface *cursor_surface)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);
  MetaWaylandSurface *surface =
    meta_wayland_surface_role_get_surface (META_WAYLAND_SURFACE_ROLE (cursor_surface));
  MetaCursorSprite *cursor_sprite = META_CURSOR_SPRITE (priv->cursor_sprite);
  CoglTexture *texture;

  if (!priv->cursor_renderer)
    return;

  texture = meta_wayland_surface_get_texture (surface);
  if (texture)
    {
      meta_cursor_sprite_set_texture (cursor_sprite,
                                      texture,
                                      priv->hot_x * surface->scale,
                                      priv->hot_y * surface->scale);
    }
  else
    {
      meta_cursor_sprite_set_texture (cursor_sprite, NULL, 0, 0);
    }

  meta_cursor_renderer_force_update (priv->cursor_renderer);
}

static void
cursor_sprite_prepare_at (MetaCursorSprite         *cursor_sprite,
                          int                       x,
                          int                       y,
                          MetaWaylandCursorSurface *cursor_surface)
{
  MetaWaylandSurfaceRole *role = META_WAYLAND_SURFACE_ROLE (cursor_surface);
  MetaWaylandSurface *surface = meta_wayland_surface_role_get_surface (role);

  if (!meta_xwayland_is_xwayland_surface (surface))
    {
      MetaBackend *backend = meta_get_backend ();
      MetaMonitorManager *monitor_manager =
        meta_backend_get_monitor_manager (backend);
      MetaLogicalMonitor *logical_monitor;

      logical_monitor =
        meta_monitor_manager_get_logical_monitor_at (monitor_manager, x, y);
      if (logical_monitor)
        {
          float texture_scale;

          if (meta_is_stage_views_scaled ())
            texture_scale = 1.0 / surface->scale;
          else
            texture_scale = (meta_logical_monitor_get_scale (logical_monitor) /
                             surface->scale);

          meta_cursor_sprite_set_texture_scale (cursor_sprite, texture_scale);
        }
    }
  meta_wayland_surface_update_outputs (surface);
}

static void
meta_wayland_cursor_surface_assigned (MetaWaylandSurfaceRole *surface_role)
{
  MetaWaylandSurface *surface =
    meta_wayland_surface_role_get_surface (surface_role);
  MetaWaylandCursorSurface *cursor_surface =
    META_WAYLAND_CURSOR_SURFACE (surface_role);
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);

  wl_list_insert_list (&priv->frame_callbacks,
                       &surface->unassigned.pending_frame_callback_list);
  wl_list_init (&surface->unassigned.pending_frame_callback_list);
}

static void
meta_wayland_cursor_surface_pre_apply_state (MetaWaylandSurfaceRole  *surface_role,
                                             MetaWaylandSurfaceState *pending)
{
  MetaWaylandCursorSurface *cursor_surface =
    META_WAYLAND_CURSOR_SURFACE (surface_role);
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);
  MetaWaylandSurface *surface =
    meta_wayland_surface_role_get_surface (surface_role);

  if (pending->newly_attached && priv->buffer)
    {
      meta_wayland_surface_unref_buffer_use_count (surface);
      g_clear_object (&priv->buffer);
    }
}

static void
meta_wayland_cursor_surface_apply_state (MetaWaylandSurfaceRole  *surface_role,
                                         MetaWaylandSurfaceState *pending)
{
  MetaWaylandCursorSurface *cursor_surface =
    META_WAYLAND_CURSOR_SURFACE (surface_role);
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);
  MetaWaylandSurface *surface =
    meta_wayland_surface_role_get_surface (surface_role);
  MetaWaylandBuffer *buffer = meta_wayland_surface_get_buffer (surface);

  if (pending->newly_attached)
    {
      g_set_object (&priv->buffer, buffer);
      if (priv->buffer)
        meta_wayland_surface_ref_buffer_use_count (surface);
    }

  wl_list_insert_list (&priv->frame_callbacks,
                       &pending->frame_callback_list);
  wl_list_init (&pending->frame_callback_list);

  if (pending->newly_attached &&
      ((!cairo_region_is_empty (pending->surface_damage) ||
        !cairo_region_is_empty (pending->buffer_damage)) ||
       !priv->buffer))
    update_cursor_sprite_texture (META_WAYLAND_CURSOR_SURFACE (surface_role));
}

static gboolean
meta_wayland_cursor_surface_is_on_logical_monitor (MetaWaylandSurfaceRole *role,
                                                   MetaLogicalMonitor     *logical_monitor)
{
  MetaWaylandSurface *surface =
    meta_wayland_surface_role_get_surface (role);
  MetaWaylandCursorSurface *cursor_surface =
    META_WAYLAND_CURSOR_SURFACE (surface->role);
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);
  graphene_point_t point;
  graphene_rect_t logical_monitor_rect;

  if (!priv->cursor_renderer)
    return FALSE;

  logical_monitor_rect =
    meta_rectangle_to_graphene_rect (&logical_monitor->rect);

  point = meta_cursor_renderer_get_position (priv->cursor_renderer);

  return graphene_rect_contains_point (&logical_monitor_rect, &point);
}

static void
meta_wayland_cursor_surface_dispose (GObject *object)
{
  MetaWaylandCursorSurface *cursor_surface =
    META_WAYLAND_CURSOR_SURFACE (object);
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);
  MetaWaylandSurface *surface =
    meta_wayland_surface_role_get_surface (META_WAYLAND_SURFACE_ROLE (object));
  MetaWaylandFrameCallback *cb, *next;

  wl_list_for_each_safe (cb, next, &priv->frame_callbacks, link)
    wl_resource_destroy (cb->resource);

  g_signal_handlers_disconnect_by_func (priv->cursor_sprite,
                                        cursor_sprite_prepare_at, cursor_surface);

  g_clear_object (&priv->cursor_renderer);
  g_clear_object (&priv->cursor_sprite);

  if (priv->buffer)
    {
      meta_wayland_surface_unref_buffer_use_count (surface);
      g_clear_object (&priv->buffer);
    }

  G_OBJECT_CLASS (meta_wayland_cursor_surface_parent_class)->dispose (object);
}

static void
meta_wayland_cursor_surface_constructed (GObject *object)
{
  MetaWaylandCursorSurface *cursor_surface =
    META_WAYLAND_CURSOR_SURFACE (object);
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);
  MetaWaylandSurfaceRole *surface_role =
    META_WAYLAND_SURFACE_ROLE (cursor_surface);
  MetaWaylandSurface *surface =
    meta_wayland_surface_role_get_surface (surface_role);
  MetaWaylandBuffer *buffer;

  buffer = meta_wayland_surface_get_buffer (surface);

  g_warn_if_fail (!buffer || buffer->resource);

  if (buffer && buffer->resource)
    {
      g_set_object (&priv->buffer, buffer);
      meta_wayland_surface_ref_buffer_use_count (surface);
    }

  priv->cursor_sprite = meta_cursor_sprite_wayland_new (surface);
  g_signal_connect_object (priv->cursor_sprite,
                           "prepare-at",
                           G_CALLBACK (cursor_sprite_prepare_at),
                           cursor_surface,
                           0);
}

static void
meta_wayland_cursor_surface_init (MetaWaylandCursorSurface *role)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (role);

  wl_list_init (&priv->frame_callbacks);
}

static void
meta_wayland_cursor_surface_class_init (MetaWaylandCursorSurfaceClass *klass)
{
  MetaWaylandSurfaceRoleClass *surface_role_class =
    META_WAYLAND_SURFACE_ROLE_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  surface_role_class->assigned = meta_wayland_cursor_surface_assigned;
  surface_role_class->pre_apply_state =
    meta_wayland_cursor_surface_pre_apply_state;
  surface_role_class->apply_state = meta_wayland_cursor_surface_apply_state;
  surface_role_class->is_on_logical_monitor =
    meta_wayland_cursor_surface_is_on_logical_monitor;

  object_class->constructed = meta_wayland_cursor_surface_constructed;
  object_class->dispose = meta_wayland_cursor_surface_dispose;
}

MetaCursorSprite *
meta_wayland_cursor_surface_get_sprite (MetaWaylandCursorSurface *cursor_surface)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);

  return META_CURSOR_SPRITE (priv->cursor_sprite);
}

void
meta_wayland_cursor_surface_set_hotspot (MetaWaylandCursorSurface *cursor_surface,
                                         int                       hotspot_x,
                                         int                       hotspot_y)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);

  if (priv->hot_x == hotspot_x &&
      priv->hot_y == hotspot_y)
    return;

  priv->hot_x = hotspot_x;
  priv->hot_y = hotspot_y;
  update_cursor_sprite_texture (cursor_surface);
}

void
meta_wayland_cursor_surface_get_hotspot (MetaWaylandCursorSurface *cursor_surface,
                                         int                      *hotspot_x,
                                         int                      *hotspot_y)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);

  if (hotspot_x)
    *hotspot_x = priv->hot_x;
  if (hotspot_y)
    *hotspot_y = priv->hot_y;
}

static void
on_cursor_painted (MetaCursorRenderer       *renderer,
                   MetaCursorSprite         *displayed_sprite,
                   MetaWaylandCursorSurface *cursor_surface)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);
  guint32 time = (guint32) (g_get_monotonic_time () / 1000);

  if (displayed_sprite != META_CURSOR_SPRITE (priv->cursor_sprite))
    return;

  while (!wl_list_empty (&priv->frame_callbacks))
    {
      MetaWaylandFrameCallback *callback =
        wl_container_of (priv->frame_callbacks.next, callback, link);

      wl_callback_send_done (callback->resource, time);
      wl_resource_destroy (callback->resource);
    }
}

void
meta_wayland_cursor_surface_set_renderer (MetaWaylandCursorSurface *cursor_surface,
                                          MetaCursorRenderer       *renderer)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);

  if (priv->cursor_renderer == renderer)
    return;

  if (priv->cursor_renderer)
    {
      g_clear_signal_handler (&priv->cursor_painted_handler_id,
                              priv->cursor_renderer);
      g_object_unref (priv->cursor_renderer);
    }
  if (renderer)
    {
      priv->cursor_painted_handler_id =
        g_signal_connect_object (renderer, "cursor-painted",
                                 G_CALLBACK (on_cursor_painted), cursor_surface, 0);
      g_object_ref (renderer);
    }

  priv->cursor_renderer = renderer;
  update_cursor_sprite_texture (cursor_surface);
}

MetaCursorRenderer *
meta_wayland_cursor_surface_get_renderer (MetaWaylandCursorSurface *cursor_surface)
{
  MetaWaylandCursorSurfacePrivate *priv =
    meta_wayland_cursor_surface_get_instance_private (cursor_surface);

  return priv->cursor_renderer;
}