layersurface.c 22.3 KB
Newer Older
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
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
/*
 * Copyright (C) 2018 Purism SPC
 * SPDX-License-Identifier: GPL-3.0+
 * Author: Guido Günther <agx@sigxcpu.org>
 */
/*

WARNING: this file is taken directly from phosh, with no modificaions apart from this message. Please update phosh instead of changing this file. Please copy the file back here afterwards, with the same notice.

*/

#define G_LOG_DOMAIN "phosh-layer-surface"

#include "config.h"
#include "layersurface.h"

#include <gdk/gdkwayland.h>

enum {
  PHOSH_LAYER_SURFACE_PROP_0,
  PHOSH_LAYER_SURFACE_PROP_LAYER_SHELL,
  PHOSH_LAYER_SURFACE_PROP_WL_OUTPUT,
  PHOSH_LAYER_SURFACE_PROP_ANCHOR,
  PHOSH_LAYER_SURFACE_PROP_LAYER,
  PHOSH_LAYER_SURFACE_PROP_KBD_INTERACTIVITY,
  PHOSH_LAYER_SURFACE_PROP_EXCLUSIVE_ZONE,
27
28
29
30
  PHOSH_LAYER_SURFACE_PROP_MARGIN_TOP,
  PHOSH_LAYER_SURFACE_PROP_MARGIN_BOTTOM,
  PHOSH_LAYER_SURFACE_PROP_MARGIN_LEFT,
  PHOSH_LAYER_SURFACE_PROP_MARGIN_RIGHT,
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
31
32
  PHOSH_LAYER_SURFACE_PROP_LAYER_WIDTH,
  PHOSH_LAYER_SURFACE_PROP_LAYER_HEIGHT,
33
34
  PHOSH_LAYER_SURFACE_PROP_CONFIGURED_WIDTH,
  PHOSH_LAYER_SURFACE_PROP_CONFIGURED_HEIGHT,
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  PHOSH_LAYER_SURFACE_PROP_NAMESPACE,
  PHOSH_LAYER_SURFACE_PROP_LAST_PROP
};
static GParamSpec *props[PHOSH_LAYER_SURFACE_PROP_LAST_PROP];

enum {
  CONFIGURED,
  N_SIGNALS
};
static guint signals [N_SIGNALS];


typedef struct {
  struct wl_surface *wl_surface;
  struct zwlr_layer_surface_v1 *layer_surface;

  /* Properties */
  guint anchor;
  guint layer;
  gboolean kbd_interactivity;
  gint exclusive_zone;
56
57
  gint margin_top, margin_bottom;
  gint margin_left, margin_right;
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
58
  gint width, height;
59
  gint configured_width, configured_height;
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  gchar *namespace;
  struct zwlr_layer_shell_v1 *layer_shell;
  struct wl_output *wl_output;
} PhoshLayerSurfacePrivate;

G_DEFINE_TYPE_WITH_PRIVATE (PhoshLayerSurface, phosh_layer_surface, GTK_TYPE_WINDOW)

static void layer_surface_configure(void                         *data,
                                    struct zwlr_layer_surface_v1 *surface,
                                    uint32_t                      serial,
                                    uint32_t                      width,
                                    uint32_t                      height)
{
  PhoshLayerSurface *self = data;
74
  PhoshLayerSurfacePrivate *priv;
75

76
77
  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));
  priv = phosh_layer_surface_get_instance_private (self);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
78
79
80
  gtk_window_resize (GTK_WINDOW (self), width, height);
  zwlr_layer_surface_v1_ack_configure(surface, serial);

81
82
83
84
85
86
87
88
89
90
91
  if (priv->configured_height != height) {
    priv->configured_height = height;
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_CONFIGURED_HEIGHT]);
  }

  if (priv->configured_width != width) {
    priv->configured_width = width;
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_CONFIGURED_WIDTH]);
  }

  g_debug("Configured %p", self);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  g_signal_emit (self, signals[CONFIGURED], 0);
}


static void layer_surface_closed (void                         *data,
                                  struct zwlr_layer_surface_v1 *surface)
{
  PhoshLayerSurface *self = data;
  PhoshLayerSurfacePrivate *priv = phosh_layer_surface_get_instance_private (self);

  g_return_if_fail (priv->layer_surface == surface);
  zwlr_layer_surface_v1_destroy(priv->layer_surface);
  priv->layer_surface = NULL;
  gtk_widget_destroy (GTK_WIDGET (self));
}

static struct zwlr_layer_surface_v1_listener layer_surface_listener = {
109
110
    .configure = layer_surface_configure,
    .closed = layer_surface_closed,
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
111
112
113
114
115
116
117
118
119
120
};

static void
phosh_layer_surface_set_property (GObject      *object,
                                  guint         property_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  PhoshLayerSurface *self = PHOSH_LAYER_SURFACE (object);
  PhoshLayerSurfacePrivate *priv = phosh_layer_surface_get_instance_private (self);
121
  gint width, height;
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

  switch (property_id) {
  case PHOSH_LAYER_SURFACE_PROP_LAYER_SHELL:
    priv->layer_shell = g_value_get_pointer (value);
    break;
  case PHOSH_LAYER_SURFACE_PROP_WL_OUTPUT:
    priv->wl_output = g_value_get_pointer (value);
    break;
  case PHOSH_LAYER_SURFACE_PROP_ANCHOR:
    priv->anchor = g_value_get_uint (value);
    break;
  case PHOSH_LAYER_SURFACE_PROP_LAYER:
    priv->layer = g_value_get_uint (value);
    break;
  case PHOSH_LAYER_SURFACE_PROP_KBD_INTERACTIVITY:
137
    phosh_layer_surface_set_kbd_interactivity (self, g_value_get_boolean (value));
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
138
139
    break;
  case PHOSH_LAYER_SURFACE_PROP_EXCLUSIVE_ZONE:
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
    phosh_layer_surface_set_exclusive_zone (self, g_value_get_int (value));
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_TOP:
    phosh_layer_surface_set_margins (self,
                                     g_value_get_int (value),
                                     priv->margin_right,
                                     priv->margin_bottom,
                                     priv->margin_left);
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_BOTTOM:
    phosh_layer_surface_set_margins (self,
                                     priv->margin_top,
                                     priv->margin_right,
                                     g_value_get_int (value),
                                     priv->margin_left);
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_LEFT:
    phosh_layer_surface_set_margins (self,
                                     priv->margin_top,
                                     priv->margin_right,
                                     priv->margin_bottom,
                                     g_value_get_int (value));
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_RIGHT:
    phosh_layer_surface_set_margins (self,
                                     priv->margin_top,
                                     g_value_get_int (value),
                                     priv->margin_bottom,
                                     priv->margin_left);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
169
170
    break;
  case PHOSH_LAYER_SURFACE_PROP_LAYER_WIDTH:
171
172
    width = g_value_get_uint (value);
    phosh_layer_surface_set_size(self, width, priv->height);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
173
174
    break;
  case PHOSH_LAYER_SURFACE_PROP_LAYER_HEIGHT:
175
176
    height = g_value_get_uint (value);
    phosh_layer_surface_set_size(self, priv->width, height);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
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
    break;
  case PHOSH_LAYER_SURFACE_PROP_NAMESPACE:
    g_free (priv->namespace);
    priv->namespace = g_value_dup_string (value);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
phosh_layer_surface_get_property (GObject    *object,
                                  guint       property_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  PhoshLayerSurface *self = PHOSH_LAYER_SURFACE (object);
  PhoshLayerSurfacePrivate *priv = phosh_layer_surface_get_instance_private (self);

  switch (property_id) {
  case PHOSH_LAYER_SURFACE_PROP_LAYER_SHELL:
    g_value_set_pointer (value, priv->layer_shell);
    break;
  case PHOSH_LAYER_SURFACE_PROP_WL_OUTPUT:
    g_value_set_pointer (value, priv->wl_output);
    break;
  case PHOSH_LAYER_SURFACE_PROP_ANCHOR:
    g_value_set_uint (value, priv->anchor);
    break;
  case PHOSH_LAYER_SURFACE_PROP_LAYER:
    g_value_set_uint (value, priv->layer);
    break;
  case PHOSH_LAYER_SURFACE_PROP_KBD_INTERACTIVITY:
    g_value_set_boolean (value, priv->kbd_interactivity);
    break;
  case PHOSH_LAYER_SURFACE_PROP_EXCLUSIVE_ZONE:
215
216
217
218
219
220
221
222
223
224
225
226
227
    g_value_set_int (value, priv->exclusive_zone);
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_TOP:
    g_value_set_int (value, priv->margin_top);
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_BOTTOM:
    g_value_set_int (value, priv->margin_bottom);
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_LEFT:
    g_value_set_int (value, priv->margin_left);
    break;
  case PHOSH_LAYER_SURFACE_PROP_MARGIN_RIGHT:
    g_value_set_int (value, priv->margin_right);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
228
229
230
231
232
233
234
    break;
  case PHOSH_LAYER_SURFACE_PROP_LAYER_WIDTH:
    g_value_set_uint (value, priv->width);
    break;
  case PHOSH_LAYER_SURFACE_PROP_LAYER_HEIGHT:
    g_value_set_uint (value, priv->height);
    break;
235
236
237
238
239
240
  case PHOSH_LAYER_SURFACE_PROP_CONFIGURED_WIDTH:
    g_value_set_uint (value, priv->configured_width);
    break;
  case PHOSH_LAYER_SURFACE_PROP_CONFIGURED_HEIGHT:
    g_value_set_uint (value, priv->configured_height);
    break;
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
241
242
243
244
245
246
247
248
249
250
251
  case PHOSH_LAYER_SURFACE_PROP_NAMESPACE:
    g_value_set_string (value, priv->namespace);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
252
on_phosh_layer_surface_realized (PhoshLayerSurface *self, gpointer unused)
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
253
{
254
  PhoshLayerSurfacePrivate *priv;
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
255
256
  GdkWindow *gdk_window;

257
258
259
  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));

  priv = phosh_layer_surface_get_instance_private (self);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
260
261
262

  gdk_window = gtk_widget_get_window (GTK_WIDGET (self));
  gdk_wayland_window_set_use_custom_surface (gdk_window);
263
  priv->wl_surface = gdk_wayland_window_get_wl_surface (gdk_window);
264

265
266
  gtk_window_set_decorated (GTK_WINDOW (self), FALSE);
}
267

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

static void
on_phosh_layer_surface_mapped (PhoshLayerSurface *self, gpointer unused)
{
  PhoshLayerSurfacePrivate *priv;
  GdkWindow *gdk_window;

  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));
  priv = phosh_layer_surface_get_instance_private (self);

  if (!priv->wl_surface) {
      gdk_window = gtk_widget_get_window (GTK_WIDGET (self));
      gdk_wayland_window_set_use_custom_surface (gdk_window);
      priv->wl_surface = gdk_wayland_window_get_wl_surface (gdk_window);
  }
  g_debug ("Mapped %p", priv->wl_surface);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
284
285
286
287
288
289
290
291
292

  priv->layer_surface = zwlr_layer_shell_v1_get_layer_surface(priv->layer_shell,
                                                              priv->wl_surface,
                                                              priv->wl_output,
                                                              priv->layer,
                                                              priv->namespace);
  zwlr_layer_surface_v1_set_exclusive_zone(priv->layer_surface, priv->exclusive_zone);
  zwlr_layer_surface_v1_set_size(priv->layer_surface, priv->width, priv->height);
  zwlr_layer_surface_v1_set_anchor(priv->layer_surface, priv->anchor);
293
294
295
296
297
  zwlr_layer_surface_v1_set_margin(priv->layer_surface,
                                   priv->margin_top,
                                   priv->margin_right,
                                   priv->margin_bottom,
                                   priv->margin_left);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
298
299
300
301
302
  zwlr_layer_surface_v1_set_keyboard_interactivity(priv->layer_surface, priv->kbd_interactivity);
  zwlr_layer_surface_v1_add_listener(priv->layer_surface,
                                     &layer_surface_listener,
                                     self);
  wl_surface_commit(priv->wl_surface);
303
304
305
306
307
308

  /* Process all pending events, otherwise we end up sending ack configure
   * to a not yet configured surface */
  wl_display_roundtrip (gdk_wayland_display_get_wl_display (gdk_display_get_default ()));
}

309
310
311
312
313
314
315
316
317
318
319
320
321
322
static void
on_phosh_layer_surface_unmapped (PhoshLayerSurface *self, gpointer unused)
{
  PhoshLayerSurfacePrivate *priv;

  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));

  priv = phosh_layer_surface_get_instance_private (self);
  if (priv->layer_surface) {
    zwlr_layer_surface_v1_destroy(priv->layer_surface);
    priv->layer_surface = NULL;
  }
  priv->wl_surface = NULL;
}
323
324
325
326
327
328
329
330
331
332
333
334

static void
phosh_layer_surface_constructed (GObject *object)
{
  PhoshLayerSurface *self = PHOSH_LAYER_SURFACE (object);

  g_signal_connect (self, "realize",
                    G_CALLBACK (on_phosh_layer_surface_realized),
                    NULL);
  g_signal_connect (self, "map",
                    G_CALLBACK (on_phosh_layer_surface_mapped),
                    NULL);
335
336
337
  g_signal_connect (self, "unmap",
                    G_CALLBACK (on_phosh_layer_surface_unmapped),
                    NULL);
338
339

  G_OBJECT_CLASS (phosh_layer_surface_parent_class)->constructed (object);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
340
341
}

342

Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
static void
phosh_layer_surface_dispose (GObject *object)
{
  PhoshLayerSurface *self = PHOSH_LAYER_SURFACE (object);
  PhoshLayerSurfacePrivate *priv = phosh_layer_surface_get_instance_private (self);

  if (priv->layer_surface) {
    zwlr_layer_surface_v1_destroy(priv->layer_surface);
    priv->layer_surface = NULL;
  }
  g_clear_pointer (&priv->namespace, g_free);

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


static void
phosh_layer_surface_class_init (PhoshLayerSurfaceClass *klass)
{
  GObjectClass *object_class = (GObjectClass *)klass;

  object_class->constructed = phosh_layer_surface_constructed;
  object_class->dispose = phosh_layer_surface_dispose;

  object_class->set_property = phosh_layer_surface_set_property;
  object_class->get_property = phosh_layer_surface_get_property;

  props[PHOSH_LAYER_SURFACE_PROP_LAYER_SHELL] =
    g_param_spec_pointer (
      "layer-shell",
      "Wayland Layer Shell Global",
      "The layer shell wayland global",
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  props[PHOSH_LAYER_SURFACE_PROP_WL_OUTPUT] =
    g_param_spec_pointer (
      "wl-output",
      "Wayland Output",
      "The wl_output associated with this surface",
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  props[PHOSH_LAYER_SURFACE_PROP_ANCHOR] =
    g_param_spec_uint (
      "anchor",
      "Anchor edges",
      "The edges to anchor the surface to",
      0,
      G_MAXUINT,
      0,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  props[PHOSH_LAYER_SURFACE_PROP_LAYER] =
    g_param_spec_uint (
      "layer",
      "Layer",
      "The layer the surface should be attached to",
      0,
      G_MAXUINT,
      0,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  props[PHOSH_LAYER_SURFACE_PROP_KBD_INTERACTIVITY] =
    g_param_spec_boolean (
      "kbd-interactivity",
      "Keyboard interactivity",
      "Whether the surface interacts with the keyboard",
      FALSE,
410
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
411
412
413
414
415
416
417
418
419

  props[PHOSH_LAYER_SURFACE_PROP_EXCLUSIVE_ZONE] =
    g_param_spec_int (
      "exclusive-zone",
      "Exclusive Zone",
      "Set area that is not occluded with other surfaces",
      -1,
      G_MAXINT,
      0,
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PHOSH_LAYER_SURFACE_PROP_MARGIN_LEFT] =
    g_param_spec_int (
      "margin-left",
      "Left margin",
      "Distance away from the left anchor point",
      G_MININT,
      G_MAXINT,
      0,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PHOSH_LAYER_SURFACE_PROP_MARGIN_RIGHT] =
    g_param_spec_int (
      "margin-right",
      "Right margin",
      "Distance away from the right anchor point",
      G_MININT,
      G_MAXINT,
      0,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PHOSH_LAYER_SURFACE_PROP_MARGIN_TOP] =
    g_param_spec_int (
      "margin-top",
      "Top margin",
      "Distance away from the top anchor point",
      G_MININT,
      G_MAXINT,
      0,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PHOSH_LAYER_SURFACE_PROP_MARGIN_BOTTOM] =
    g_param_spec_int (
      "margin-bottom",
      "Bottom margin",
      "Distance away from the bottom anchor point",
      G_MININT,
      G_MAXINT,
      0,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
461
462
463
464
465
466
467
468
469

  props[PHOSH_LAYER_SURFACE_PROP_LAYER_WIDTH] =
    g_param_spec_uint (
      "width",
      "Width",
      "The width of the layer surface",
      0,
      G_MAXUINT,
      0,
470
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
471
472
473
474
475
476
477
478
479

  props[PHOSH_LAYER_SURFACE_PROP_LAYER_HEIGHT] =
    g_param_spec_uint (
      "height",
      "Height",
      "The height of the layer surface",
      0,
      G_MAXUINT,
      0,
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);


  props[PHOSH_LAYER_SURFACE_PROP_CONFIGURED_WIDTH] =
    g_param_spec_uint (
      "configured-width",
      "Configured width",
      "The width of the layer surface set by the compositor",
      0,
      G_MAXUINT,
      0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PHOSH_LAYER_SURFACE_PROP_CONFIGURED_HEIGHT] =
    g_param_spec_uint (
      "configured-height",
      "Configured height",
      "The height of the layer surface set by the compositor",
      0,
      G_MAXUINT,
      0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544

  props[PHOSH_LAYER_SURFACE_PROP_NAMESPACE] =
    g_param_spec_string (
      "namespace",
      "Namespace",
      "Namespace of the layer surface",
      "",
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, PHOSH_LAYER_SURFACE_PROP_LAST_PROP, props);

  /**
   * PhoshLayersurface::configured
   * @self: The #PhoshLayersurface instance.
   *
   * This signal is emitted once we received the configure event from the
   * compositor.
   */
  signals[CONFIGURED] =
    g_signal_new ("configured",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PhoshLayerSurfaceClass, configured),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
}


static void
phosh_layer_surface_init (PhoshLayerSurface *self)
{
}


GtkWidget *
phosh_layer_surface_new (gpointer layer_shell,
                         gpointer wl_output)
{
  return g_object_new (PHOSH_TYPE_LAYER_SURFACE,
                       "layer-shell", layer_shell,
                       "wl-output", wl_output);
}

545
546
547
548
549
550
/**
 * phosh_layer_surface_get_surface:
 *
 * Get the layer layer surface or #NULL if the window
 * is not yet realized.
 */
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
551
552
553
554
555
556
557
558
559
560
561
struct zwlr_layer_surface_v1 *
phosh_layer_surface_get_layer_surface(PhoshLayerSurface *self)
{
  PhoshLayerSurfacePrivate *priv;

  g_return_val_if_fail (PHOSH_IS_LAYER_SURFACE (self), NULL);
  priv = phosh_layer_surface_get_instance_private (self);
  return priv->layer_surface;
}


562
563
564
565
566
567
/**
 * phosh_layer_surface_get_wl_surface:
 *
 * Get the layer wayland surface or #NULL if the window
 * is not yet realized.
 */
Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
568
569
570
571
572
573
574
575
576
struct wl_surface *
phosh_layer_surface_get_wl_surface(PhoshLayerSurface *self)
{
  PhoshLayerSurfacePrivate *priv;

  g_return_val_if_fail (PHOSH_IS_LAYER_SURFACE (self), NULL);
  priv = phosh_layer_surface_get_instance_private (self);
  return priv->wl_surface;
}
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591

/**
 * phosh_layer_surface_set_size:
 *
 * Set the size of a layer surface. A value of '-1' indicates 'use old value'
 */
void
phosh_layer_surface_set_size(PhoshLayerSurface *self, gint width, gint height)
{
  PhoshLayerSurfacePrivate *priv;
  gint old_width, old_height;

  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));
  priv = phosh_layer_surface_get_instance_private (self);

592
  if (priv->height == height && priv->width == width) {
593
    return;
594
  }
595
596
597
598

  old_width = priv->width;
  old_height = priv->height;

599
  if (width != -1) {
600
    priv->width = width;
601
  }
602

603
  if (height != -1) {
604
    priv->height = height;
605
  }
606
607
608
609
610

  if (gtk_widget_get_mapped (GTK_WIDGET (self))) {
    zwlr_layer_surface_v1_set_size(priv->layer_surface, priv->width, priv->height);
  }

611
  if (priv->height != old_height) {
612
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_LAYER_HEIGHT]);
613
  }
614

615
  if (priv->width != old_width) {
616
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_LAYER_WIDTH]);
617
  }
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
}

/**
 * phosh_layer_surface_set_margins:
 *
 * Set anchor margins of a layer surface.
 */
void
phosh_layer_surface_set_margins(PhoshLayerSurface *self, gint top, gint right, gint bottom, gint left)
{
  PhoshLayerSurfacePrivate *priv;
  gint old_top, old_bottom, old_left, old_right;

  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));
  priv = phosh_layer_surface_get_instance_private (self);

  old_top = priv->margin_top;
  old_left = priv->margin_left;
  old_right = priv->margin_right;
  old_bottom = priv->margin_bottom;

639
  if (old_top == top && old_left == left && old_right == right && old_bottom == bottom) {
640
    return;
641
  }
642
643
644
645
646
647

  priv->margin_top = top;
  priv->margin_left = left;
  priv->margin_right = right;
  priv->margin_bottom = bottom;

648
  if (priv->layer_surface) {
649
    zwlr_layer_surface_v1_set_margin(priv->layer_surface, top, right, bottom, left);
650
  }
651

652
  if (old_top != top) {
653
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_MARGIN_TOP]);
654
655
  }
  if (old_bottom != bottom) {
656
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_MARGIN_BOTTOM]);
657
658
  }
  if (old_left != left) {
659
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_MARGIN_LEFT]);
660
661
  }
  if (old_right != right) {
662
    g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_MARGIN_RIGHT]);
663
  }
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
}

/**
 * phosh_layer_surface_set_exclusive_zone:
 *
 * Set exclusive zone of a layer surface.
 */
void
phosh_layer_surface_set_exclusive_zone(PhoshLayerSurface *self, gint zone)
{
  PhoshLayerSurfacePrivate *priv;
  gint old_zone;

  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));
  priv = phosh_layer_surface_get_instance_private (self);

  old_zone = priv->exclusive_zone;

682
  if (old_zone == zone) {
683
    return;
684
  }
685
686
687

  priv->exclusive_zone = zone;

688
  if (priv->layer_surface) {
689
    zwlr_layer_surface_v1_set_exclusive_zone(priv->layer_surface, zone);
690
  }
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707

  g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_EXCLUSIVE_ZONE]);
}

/**
 * phosh_layer_surface_set_keyboard_interactivity:
 *
 * Set keyboard ineractivity a layer surface.
 */
void
phosh_layer_surface_set_kbd_interactivity (PhoshLayerSurface *self, gboolean interactivity)
{
  PhoshLayerSurfacePrivate *priv;

  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));
  priv = phosh_layer_surface_get_instance_private (self);

708
  if (priv->kbd_interactivity == interactivity) {
709
    return;
710
  }
711
712
  priv->kbd_interactivity = interactivity;

713
  if (priv->layer_surface) {
714
    zwlr_layer_surface_v1_set_keyboard_interactivity (priv->layer_surface, interactivity);
715
  }
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732

  g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_LAYER_SURFACE_PROP_KBD_INTERACTIVITY]);
}

/**
 * phosh_layer_surface_wl_surface_commit:
 *
 * Forces a commit of layer surface's state.
 */
void
phosh_layer_surface_wl_surface_commit (PhoshLayerSurface *self)
{
  PhoshLayerSurfacePrivate *priv;

  g_return_if_fail (PHOSH_IS_LAYER_SURFACE (self));
  priv = phosh_layer_surface_get_instance_private (self);

733
  if (priv->wl_surface) {
734
    wl_surface_commit (priv->wl_surface);
735
  }
736
}