summaryrefslogtreecommitdiff
path: root/src/x11/meta-x11-stack.c
blob: 6a370644ea149f9a791788049956e858f6c279a3 (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * Copyright (C) 2019 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "core/frame.h"
#include "core/stack.h"
#include "core/window-private.h"
#include "x11/meta-x11-display-private.h"
#include "x11/meta-x11-stack-private.h"

struct _MetaX11Stack
{
  GObject parent;
  MetaX11Display *x11_display;

  /*
   * A sequence of all the Windows (X handles, not MetaWindows) of the windows
   * we manage, sorted in order.  Suitable to be passed into _NET_CLIENT_LIST.
   */
  GArray *xwindows;

  /*
   * MetaWindows waiting to be added to the xwindows list, after
   * being added to the MetaStack.
   *
   * The order of the elements in this list is not important; what is important
   * is the stack_position element of each window.
   */
  GList *added;

  /*
   * Windows (X handles, not MetaWindows) waiting to be removed from the
   * xwindows list, after being removed from the MetaStack.
   *
   * The order of the elements in this list is not important.
   */
  GList *removed;
};

enum
{
  PROP_DISPLAY = 1,
  N_PROPS
};

static GParamSpec *pspecs[N_PROPS] = { 0 };

G_DEFINE_TYPE (MetaX11Stack, meta_x11_stack, G_TYPE_OBJECT)

static void
meta_x11_stack_init (MetaX11Stack *x11_stack)
{
  x11_stack->xwindows = g_array_new (FALSE, FALSE, sizeof (Window));
}

static void
meta_x11_stack_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  MetaX11Stack *x11_stack = META_X11_STACK (object);

  switch (prop_id)
    {
    case PROP_DISPLAY:
      x11_stack->x11_display = g_value_get_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
meta_x11_stack_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  MetaX11Stack *x11_stack = META_X11_STACK (object);

  switch (prop_id)
    {
    case PROP_DISPLAY:
      g_value_set_object (value, x11_stack->x11_display);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
stack_window_added_cb (MetaStack    *stack,
                       MetaWindow   *window,
                       MetaX11Stack *x11_stack)
{
  if (window->client_type != META_WINDOW_CLIENT_TYPE_X11)
    return;

  x11_stack->added = g_list_prepend (x11_stack->added, window);
}

static void
stack_window_removed_cb (MetaStack    *stack,
                         MetaWindow   *window,
                         MetaX11Stack *x11_stack)
{
  if (window->client_type != META_WINDOW_CLIENT_TYPE_X11)
    return;

  x11_stack->added = g_list_remove (x11_stack->added, window);

  x11_stack->removed = g_list_prepend (x11_stack->removed,
                                   GUINT_TO_POINTER (window->xwindow));
  if (window->frame)
    {
      x11_stack->removed = g_list_prepend (x11_stack->removed,
                                           GUINT_TO_POINTER (window->frame->xwindow));
    }
}

/**
 * stack_do_window_deletions:
 *
 * Go through "deleted" and take the matching windows
 * out of "windows".
 */
static void
x11_stack_do_window_deletions (MetaX11Stack *x11_stack)
{
  GList *tmp;
  int i;

  tmp = x11_stack->removed;
  while (tmp != NULL)
    {
      Window xwindow;
      xwindow = GPOINTER_TO_UINT (tmp->data);

      /* We go from the end figuring removals are more
       * likely to be recent.
       */
      i = x11_stack->xwindows->len;
      while (i > 0)
        {
          --i;

          /* there's no guarantee we'll actually find windows to
           * remove, e.g. the same xwindow could have been
           * added/removed before we ever synced, and we put
           * both the window->xwindow and window->frame->xwindow
           * in the removal list.
           */
          if (xwindow == g_array_index (x11_stack->xwindows, Window, i))
            {
              g_array_remove_index (x11_stack->xwindows, i);
              goto next;
            }
        }

    next:
      tmp = tmp->next;
    }

  g_clear_pointer (&x11_stack->removed, g_list_free);
}

static void
x11_stack_do_window_additions (MetaX11Stack *x11_stack)
{
  GList *tmp;
  gint n_added;

  n_added = g_list_length (x11_stack->added);
  if (n_added > 0)
    {
      meta_topic (META_DEBUG_STACK,
                  "Adding %d windows to sorted list",
                  n_added);

      /* stack->added has the most recent additions at the
       * front of the list, so we need to reverse it
       */
      x11_stack->added = g_list_reverse (x11_stack->added);

      tmp = x11_stack->added;
      while (tmp != NULL)
        {
          MetaWindow *w;

          w = tmp->data;
          g_array_append_val (x11_stack->xwindows, w->xwindow);
          tmp = tmp->next;
        }
    }

  g_clear_pointer (&x11_stack->added, g_list_free);
}

/**
 * x11_stack_sync_to_server:
 *
 * Order the windows on the X server to be the same as in our structure.
 * We do this using XRestackWindows if we don't know the previous order,
 * or XConfigureWindow on a few particular windows if we do and can figure
 * out the minimum set of changes.  After that, we set __NET_CLIENT_LIST
 * and __NET_CLIENT_LIST_STACKING.
 *
 * FIXME: Now that we have a good view of the stacking order on the server
 * with MetaStackTracker it should be possible to do a simpler and better
 * job of computing the minimal set of stacking requests needed.
 */
static void
x11_stack_sync_to_xserver (MetaX11Stack *x11_stack)
{
  MetaX11Display *x11_display = x11_stack->x11_display;
  MetaStack *stack = x11_display->display->stack;
  GArray *x11_stacked;
  GList *tmp;
  GList *sorted;

  meta_topic (META_DEBUG_STACK, "Syncing window stack to server");

  /* Create stacked xwindow arrays, in bottom-to-top order
   */
  x11_stacked = g_array_new (FALSE, FALSE, sizeof (Window));

  sorted = meta_stack_list_windows (stack, NULL);

  for (tmp = sorted; tmp; tmp = tmp->next)
    {
      MetaWindow *w = tmp->data;

      if (w->client_type == META_WINDOW_CLIENT_TYPE_X11)
        g_array_append_val (x11_stacked, w->xwindow);
    }

  /* Sync _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING */

  XChangeProperty (x11_stack->x11_display->xdisplay,
                   x11_stack->x11_display->xroot,
                   x11_stack->x11_display->atom__NET_CLIENT_LIST,
                   XA_WINDOW,
                   32, PropModeReplace,
                   (unsigned char *) x11_stack->xwindows->data,
                   x11_stack->xwindows->len);
  XChangeProperty (x11_stack->x11_display->xdisplay,
                   x11_stack->x11_display->xroot,
                   x11_stack->x11_display->atom__NET_CLIENT_LIST_STACKING,
                   XA_WINDOW,
                   32, PropModeReplace,
                   (unsigned char *) x11_stacked->data,
                   x11_stacked->len);

  g_array_free (x11_stacked, TRUE);
  g_list_free (sorted);
}

static void
stack_changed_cb (MetaX11Stack *x11_stack)
{
  /* Do removals before adds, with paranoid idea that we might re-add
   * the same window IDs.
   */
  x11_stack_do_window_deletions (x11_stack);
  x11_stack_do_window_additions (x11_stack);
  x11_stack_sync_to_xserver (x11_stack);
}

static void
meta_x11_stack_constructed (GObject *object)
{
  MetaX11Stack *x11_stack = META_X11_STACK (object);
  MetaX11Display *x11_display = x11_stack->x11_display;

  G_OBJECT_CLASS (meta_x11_stack_parent_class)->constructed (object);

  g_signal_connect (x11_display->display->stack,
                    "window-added",
                    G_CALLBACK (stack_window_added_cb),
                    x11_stack);
  g_signal_connect (x11_display->display->stack,
                    "window-removed",
                    G_CALLBACK (stack_window_removed_cb),
                    x11_stack);
  g_signal_connect_swapped (x11_display->display->stack,
                            "changed",
                            G_CALLBACK (stack_changed_cb),
                            x11_stack);
}

static void
meta_x11_stack_finalize (GObject *object)
{
  MetaX11Stack *x11_stack = META_X11_STACK (object);
  MetaX11Display *x11_display = x11_stack->x11_display;

  if (x11_display->display && x11_display->display->stack)
    {
      g_signal_handlers_disconnect_by_data (x11_display->display->stack,
                                            x11_stack);
    }

  g_array_free (x11_stack->xwindows, TRUE);
  g_list_free (x11_stack->added);
  g_list_free (x11_stack->removed);

  G_OBJECT_CLASS (meta_x11_stack_parent_class)->finalize (object);
}

static void
meta_x11_stack_class_init (MetaX11StackClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = meta_x11_stack_set_property;
  object_class->get_property = meta_x11_stack_get_property;
  object_class->constructed = meta_x11_stack_constructed;
  object_class->finalize = meta_x11_stack_finalize;

  pspecs[PROP_DISPLAY] =
    g_param_spec_object ("display",
                         "Display",
                         "Display",
                         META_TYPE_X11_DISPLAY,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

  g_object_class_install_properties (object_class, N_PROPS, pspecs);
}

MetaX11Stack *
meta_x11_stack_new (MetaX11Display *x11_display)
{
  return g_object_new (META_TYPE_X11_STACK,
                       "display", x11_display,
                       NULL);
}