summaryrefslogtreecommitdiff
path: root/src/wayland/meta-wayland-inhibit-shortcuts.c
blob: 68e90eec28c947fa0806fef23f079971abed5d0f (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
/*
 * Copyright (C) 2017 Red Hat
 *
 * 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.
 *
 * Written by:
 *     Olivier Fourdan <ofourdan@redhat.com>
 */

#include "config.h"

#include <wayland-server.h>

#include "wayland/meta-wayland-private.h"
#include "wayland/meta-wayland-versions.h"
#include "wayland/meta-wayland-inhibit-shortcuts.h"
#include "wayland/meta-wayland-inhibit-shortcuts-dialog.h"

#include "keyboard-shortcuts-inhibit-unstable-v1-server-protocol.h"

struct _MetaWaylandKeyboardShotscutsInhibit
{
  MetaWaylandSurface      *surface;
  MetaWaylandSeat         *seat;
  gulong                   inhibit_shortcut_handler;
  gulong                   restore_shortcut_handler;
  gulong                   surface_destroyed_handler;
  struct wl_resource      *resource;
};

static void
zwp_keyboard_shortcuts_inhibit_destructor (struct wl_resource *resource)
{
  MetaWaylandKeyboardShotscutsInhibit *shortcut_inhibit;

  shortcut_inhibit = wl_resource_get_user_data (resource);
  if (shortcut_inhibit->surface)
    {
      meta_wayland_surface_cancel_inhibit_shortcuts_dialog (shortcut_inhibit->surface);

      g_clear_signal_handler (&shortcut_inhibit->surface_destroyed_handler,
                              shortcut_inhibit->surface);

      g_clear_signal_handler (&shortcut_inhibit->inhibit_shortcut_handler,
                              shortcut_inhibit->surface);

      g_clear_signal_handler (&shortcut_inhibit->restore_shortcut_handler,
                              shortcut_inhibit->surface);

      meta_wayland_surface_restore_shortcuts (shortcut_inhibit->surface,
                                              shortcut_inhibit->seat);
    }
  g_free (shortcut_inhibit);
}

static void
zwp_keyboard_shortcuts_inhibit_destroy (struct wl_client   *client,
                                        struct wl_resource *resource)
{
  wl_resource_destroy (resource);
}

static const struct zwp_keyboard_shortcuts_inhibit_manager_v1_interface
  meta_keyboard_shortcuts_inhibit_interface = {
    zwp_keyboard_shortcuts_inhibit_destroy,
  };

static void
surface_destroyed_cb (MetaWaylandSurface                  *surface,
                      MetaWaylandKeyboardShotscutsInhibit *shortcut_inhibit)
{
  shortcut_inhibit->surface = NULL;
  shortcut_inhibit->seat = NULL;
}

static void
shortcuts_inhibited_cb (MetaWaylandSurface                  *surface,
                        MetaWaylandKeyboardShotscutsInhibit *shortcut_inhibit)
{
  zwp_keyboard_shortcuts_inhibitor_v1_send_active (shortcut_inhibit->resource);
}

static void
shortcuts_restored_cb (MetaWaylandSurface                  *surface,
                       MetaWaylandKeyboardShotscutsInhibit *shortcut_inhibit)
{
  zwp_keyboard_shortcuts_inhibitor_v1_send_inactive (shortcut_inhibit->resource);
}

static void
zwp_keyboard_shortcuts_inhibit_manager_destroy (struct wl_client   *client,
                                                struct wl_resource *resource)
{
  wl_resource_destroy (resource);
}

static void
zwp_keyboard_shortcuts_inhibit_manager_inhibit_shortcuts (struct wl_client   *client,
                                                          struct wl_resource *resource,
                                                          uint32_t            id,
                                                          struct wl_resource *surface_resource,
                                                          struct wl_resource *seat_resource)
{
  MetaWaylandKeyboardShotscutsInhibit *shortcut_inhibit;
  MetaWaylandSurface *surface = wl_resource_get_user_data (surface_resource);
  MetaWaylandSeat *seat = wl_resource_get_user_data (seat_resource);
  struct wl_resource *keyboard_shortcuts_inhibit_resource;

  keyboard_shortcuts_inhibit_resource =
      wl_resource_create (client,
                          &zwp_keyboard_shortcuts_inhibitor_v1_interface,
                          META_ZWP_KEYBOARD_SHORTCUTS_INHIBIT_V1_VERSION,
                          id);

  shortcut_inhibit = g_new0 (MetaWaylandKeyboardShotscutsInhibit, 1);
  shortcut_inhibit->surface = surface;
  shortcut_inhibit->seat = seat;
  shortcut_inhibit->resource = keyboard_shortcuts_inhibit_resource;

  shortcut_inhibit->inhibit_shortcut_handler =
    g_signal_connect (surface, "shortcuts-inhibited",
                      G_CALLBACK (shortcuts_inhibited_cb),
                      shortcut_inhibit);
  shortcut_inhibit->restore_shortcut_handler =
    g_signal_connect (surface, "shortcuts-restored",
                      G_CALLBACK (shortcuts_restored_cb),
                      shortcut_inhibit);
  shortcut_inhibit->surface_destroyed_handler =
    g_signal_connect (surface, "destroy",
                      G_CALLBACK (surface_destroyed_cb),
                      shortcut_inhibit);

  /* Cannot grant shortcuts to a surface without any window */
  if (meta_wayland_surface_get_toplevel_window (surface))
    meta_wayland_surface_show_inhibit_shortcuts_dialog (surface, seat);

  wl_resource_set_implementation (keyboard_shortcuts_inhibit_resource,
                                  &meta_keyboard_shortcuts_inhibit_interface,
                                  shortcut_inhibit,
                                  zwp_keyboard_shortcuts_inhibit_destructor);
}

static const struct zwp_keyboard_shortcuts_inhibit_manager_v1_interface
  meta_keyboard_shortcuts_inhibit_manager_interface = {
    zwp_keyboard_shortcuts_inhibit_manager_destroy,
    zwp_keyboard_shortcuts_inhibit_manager_inhibit_shortcuts,
  };

static void
bind_keyboard_shortcuts_inhibit (struct wl_client *client,
                                 void             *data,
                                 uint32_t          version,
                                 uint32_t          id)
{
  struct wl_resource *resource;

  resource = wl_resource_create (client,
                                 &zwp_keyboard_shortcuts_inhibit_manager_v1_interface,
                                 META_ZWP_KEYBOARD_SHORTCUTS_INHIBIT_V1_VERSION,
                                 id);

  wl_resource_set_implementation (resource,
                                  &meta_keyboard_shortcuts_inhibit_manager_interface,
                                  NULL, NULL);
}

gboolean
meta_wayland_keyboard_shortcuts_inhibit_init (MetaWaylandCompositor *compositor)
{
  return (wl_global_create (compositor->wayland_display,
                            &zwp_keyboard_shortcuts_inhibit_manager_v1_interface,
                            META_ZWP_KEYBOARD_SHORTCUTS_INHIBIT_V1_VERSION,
                            NULL,
                            bind_keyboard_shortcuts_inhibit) != NULL);
}