window.c 244 KB
Newer Older
1
2
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

3
/*
4
 * Copyright (C) 2001 Havoc Pennington, Anders Carlsson
5
6
 * Copyright (C) 2002, 2003 Red Hat, Inc.
 * Copyright (C) 2003 Rob Adams
7
 * Copyright (C) 2004-2006 Elijah Newren
8
 *
rhp's avatar
...  
rhp committed
9
10
11
12
13
14
15
16
17
 * 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.
18
 *
rhp's avatar
...  
rhp committed
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
rhp's avatar
...  
rhp committed
21
22
 */

23
24
25
26
27
28
/**
 * SECTION:window
 * @title: MetaWindow
 * @short_description: Mutter X managed windows
 */

29
#include <config.h>
30
#include "window-private.h"
31
#include "boxes-private.h"
32
#include "edge-resistance.h"
33
#include "util-private.h"
34
35
#include "frame.h"
#include <meta/errors.h>
36
#include "workspace-private.h"
rhp's avatar
...    
rhp committed
37
#include "stack.h"
38
#include "keybindings-private.h"
rhp's avatar
...    
rhp committed
39
#include "ui.h"
rhp's avatar
...    
rhp committed
40
#include "place.h"
41
#include <meta/prefs.h>
42
#include <meta/group.h>
43
#include "constraints.h"
44
#include "mutter-enum-types.h"
Adel Gadllah's avatar
Adel Gadllah committed
45
#include "core.h"
rhp's avatar
...    
rhp committed
46

rhp's avatar
...    
rhp committed
47
#include <X11/Xatom.h>
48
#include <stdlib.h>
49
#include <string.h>
50
#include <math.h>
rhp's avatar
...    
rhp committed
51

52
#include "meta/compositor-mutter.h"
53

54
55
56
57
#include "x11/window-x11.h"
#include "x11/window-props.h"
#include "x11/xprops.h"

58
#include "wayland/window-wayland.h"
59
60
#include "wayland/meta-wayland-private.h"

61
62
#include "backends/meta-backend.h"

63
/* Windows that unmaximize to a size bigger than that fraction of the workarea
64
65
66
 * will be scaled down to that size (while maintaining aspect ratio).
 * Windows that cover an area greater then this size are automaximized on map.
 */
67
68
#define MAX_UNMAXIMIZED_WINDOW_AREA .8

69
70
static int destroying_windows_disallowed = 0;

rhp's avatar
...    
rhp committed
71

72
static void     invalidate_work_areas     (MetaWindow     *window);
73
static void     set_wm_state              (MetaWindow     *window);
74
static void     set_net_wm_state          (MetaWindow     *window);
75
76
static void     meta_window_set_above     (MetaWindow     *window,
                                           gboolean        new_value);
77

78
79
static void     meta_window_force_placement (MetaWindow     *window);

rhp's avatar
...    
rhp committed
80
81
82
static void     meta_window_show          (MetaWindow     *window);
static void     meta_window_hide          (MetaWindow     *window);

83
84
static void     meta_window_save_rect         (MetaWindow    *window);

85
86
87
static void     ensure_mru_position_after (MetaWindow *window,
                                           MetaWindow *after_this_one);

rhp's avatar
...    
rhp committed
88

89
static void meta_window_move_resize_now (MetaWindow  *window);
rhp's avatar
...    
rhp committed
90

91
static void meta_window_unqueue (MetaWindow *window, guint queuebits);
92

93
94
95
96
97
98
99
100
101
102
103
static void     update_move           (MetaWindow   *window,
                                       gboolean      snap,
                                       int           x,
                                       int           y);
static gboolean update_move_timeout   (gpointer data);
static void     update_resize         (MetaWindow   *window,
                                       gboolean      snap,
                                       int           x,
                                       int           y,
                                       gboolean      force);
static gboolean update_resize_timeout (gpointer data);
104
static gboolean should_be_on_all_workspaces (MetaWindow *window);
105

106
static void meta_window_flush_calc_showing   (MetaWindow *window);
rhp's avatar
...    
rhp committed
107

108
109
110
static gboolean queue_calc_showing_func (MetaWindow *window,
                                         void       *data);

111
112
113
static void meta_window_move_between_rects (MetaWindow          *window,
                                            const MetaRectangle *old_area,
                                            const MetaRectangle *new_area);
rhp's avatar
...    
rhp committed
114

115
116
117
static void unmaximize_window_before_freeing (MetaWindow        *window);
static void unminimize_window_and_all_transient_parents (MetaWindow *window);

118
119
static void meta_window_propagate_focus_appearance (MetaWindow *window,
                                                    gboolean    focused);
120
121
static void meta_window_update_icon_now (MetaWindow *window,
                                         gboolean    force);
122

123
124
125
/* Idle handlers for the three queues (run with meta_later_add()). The
 * "data" parameter in each case will be a GINT_TO_POINTER of the
 * index into the queue arrays to use.
126
127
128
129
130
131
132
 *
 * TODO: Possibly there is still some code duplication among these, which we
 * need to sort out at some point.
 */
static gboolean idle_calc_showing (gpointer data);
static gboolean idle_move_resize (gpointer data);
static gboolean idle_update_icon (gpointer data);
133

134
G_DEFINE_ABSTRACT_TYPE (MetaWindow, meta_window, G_TYPE_OBJECT);
Owen Taylor's avatar
Owen Taylor committed
135

136
137
138
139
140
enum {
  PROP_0,

  PROP_TITLE,
  PROP_ICON,
141
  PROP_MINI_ICON,
142
  PROP_DECORATED,
143
  PROP_FULLSCREEN,
144
145
  PROP_MAXIMIZED_HORIZONTALLY,
  PROP_MAXIMIZED_VERTICALLY,
146
  PROP_MINIMIZED,
147
  PROP_WINDOW_TYPE,
148
  PROP_USER_TIME,
149
  PROP_DEMANDS_ATTENTION,
Tomas Frydrych's avatar
Tomas Frydrych committed
150
  PROP_URGENT,
151
  PROP_SKIP_TASKBAR,
152
  PROP_MUTTER_HINTS,
153
  PROP_APPEARS_FOCUSED,
154
155
  PROP_RESIZEABLE,
  PROP_ABOVE,
156
  PROP_WM_CLASS,
157
158
159
160
161
  PROP_GTK_APPLICATION_ID,
  PROP_GTK_UNIQUE_BUS_NAME,
  PROP_GTK_APPLICATION_OBJECT_PATH,
  PROP_GTK_WINDOW_OBJECT_PATH,
  PROP_GTK_APP_MENU_OBJECT_PATH,
162
  PROP_GTK_MENUBAR_OBJECT_PATH,
163
164
165
  PROP_ON_ALL_WORKSPACES,

  LAST_PROP,
166
167
};

168
169
static GParamSpec *obj_props[LAST_PROP];

170
171
172
enum
{
  WORKSPACE_CHANGED,
Tomas Frydrych's avatar
Tomas Frydrych committed
173
  FOCUS,
Tomas Frydrych's avatar
Tomas Frydrych committed
174
  RAISED,
175
  UNMANAGED,
176
177
  SIZE_CHANGED,
  POSITION_CHANGED,
178
179
180
181
182
183

  LAST_SIGNAL
};

static guint window_signals[LAST_SIGNAL] = { 0 };

184
185
186
187
188
189
static void
prefs_changed_callback (MetaPreference pref,
                        gpointer       data)
{
  MetaWindow *window = data;

190
191
192
193
194
195
196
197
198
  if (pref == META_PREF_WORKSPACES_ONLY_ON_PRIMARY)
    {
      meta_window_update_on_all_workspaces (window);
      meta_window_queue (window, META_QUEUE_CALC_SHOWING);
    }
  else if (pref == META_PREF_ATTACH_MODAL_DIALOGS &&
           window->type == META_WINDOW_MODAL_DIALOG)
    {
      window->attached = meta_window_should_attach_to_parent (window);
Jasper St. Pierre's avatar
Jasper St. Pierre committed
199
      meta_window_recalc_features (window);
200
201
      meta_window_queue (window, META_QUEUE_MOVE_RESIZE);
    }
202
203
}

204
205
206
207
208
209
210
211
212
213
214
215
216
static void
meta_window_real_grab_op_began (MetaWindow *window,
                                MetaGrabOp  op)
{
}

static void
meta_window_real_grab_op_ended (MetaWindow *window,
                                MetaGrabOp  op)
{
  window->shaken_loose = FALSE;
}

217
218
219
220
221
static void
meta_window_real_current_workspace_changed (MetaWindow *window)
{
}

222
223
224
225
226
227
static gboolean
meta_window_real_update_struts (MetaWindow *window)
{
  return FALSE;
}

228
229
230
231
232
233
234
235
236
static void
meta_window_real_get_default_skip_hints (MetaWindow *window,
                                         gboolean   *skip_taskbar_out,
                                         gboolean   *skip_pager_out)
{
  *skip_taskbar_out = FALSE;
  *skip_pager_out = FALSE;
}

237
238
239
240
241
242
243
244
245
246
static gboolean
meta_window_real_update_icon (MetaWindow  *window,
                              GdkPixbuf  **icon,
                              GdkPixbuf  **mini_icon)
{
  *icon = NULL;
  *mini_icon = NULL;
  return FALSE;
}

Owen Taylor's avatar
Owen Taylor committed
247
248
249
250
static void
meta_window_finalize (GObject *object)
{
  MetaWindow *window = META_WINDOW (object);
251

Owen Taylor's avatar
Owen Taylor committed
252
253
254
255
256
257
  if (window->icon)
    g_object_unref (G_OBJECT (window->icon));

  if (window->mini_icon)
    g_object_unref (G_OBJECT (window->mini_icon));

258
259
260
  if (window->frame_bounds)
    cairo_region_destroy (window->frame_bounds);

Pavel Vasin's avatar
Pavel Vasin committed
261
262
263
  if (window->shape_region)
    cairo_region_destroy (window->shape_region);

264
265
266
  if (window->opaque_region)
    cairo_region_destroy (window->opaque_region);

267
268
269
  if (window->transient_for)
    g_object_unref (window->transient_for);

Owen Taylor's avatar
Owen Taylor committed
270
271
272
273
274
275
276
277
  g_free (window->sm_client_id);
  g_free (window->wm_client_machine);
  g_free (window->startup_id);
  g_free (window->role);
  g_free (window->res_class);
  g_free (window->res_name);
  g_free (window->title);
  g_free (window->desc);
278
  g_free (window->gtk_theme_variant);
279
280
281
282
283
284
  g_free (window->gtk_application_id);
  g_free (window->gtk_unique_bus_name);
  g_free (window->gtk_application_object_path);
  g_free (window->gtk_window_object_path);
  g_free (window->gtk_app_menu_object_path);
  g_free (window->gtk_menubar_object_path);
285
286

  G_OBJECT_CLASS (meta_window_parent_class)->finalize (object);
Owen Taylor's avatar
Owen Taylor committed
287
288
}

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
static void
meta_window_get_property(GObject         *object,
                         guint            prop_id,
                         GValue          *value,
                         GParamSpec      *pspec)
{
  MetaWindow *win = META_WINDOW (object);

  switch (prop_id)
    {
    case PROP_TITLE:
      g_value_set_string (value, win->title);
      break;
    case PROP_ICON:
      g_value_set_object (value, win->icon);
      break;
    case PROP_MINI_ICON:
      g_value_set_object (value, win->mini_icon);
307
      break;
308
309
310
    case PROP_DECORATED:
      g_value_set_boolean (value, win->decorated);
      break;
311
312
313
    case PROP_FULLSCREEN:
      g_value_set_boolean (value, win->fullscreen);
      break;
314
315
316
317
318
319
    case PROP_MAXIMIZED_HORIZONTALLY:
      g_value_set_boolean (value, win->maximized_horizontally);
      break;
    case PROP_MAXIMIZED_VERTICALLY:
      g_value_set_boolean (value, win->maximized_vertically);
      break;
320
321
322
    case PROP_MINIMIZED:
      g_value_set_boolean (value, win->minimized);
      break;
323
324
325
    case PROP_WINDOW_TYPE:
      g_value_set_enum (value, win->type);
      break;
326
327
328
    case PROP_USER_TIME:
      g_value_set_uint (value, win->net_wm_user_time);
      break;
329
330
331
    case PROP_DEMANDS_ATTENTION:
      g_value_set_boolean (value, win->wm_state_demands_attention);
      break;
332
    case PROP_URGENT:
333
      g_value_set_boolean (value, win->urgent);
334
      break;
335
336
337
    case PROP_SKIP_TASKBAR:
      g_value_set_boolean (value, win->skip_taskbar);
      break;
Tomas Frydrych's avatar
Tomas Frydrych committed
338
339
340
    case PROP_MUTTER_HINTS:
      g_value_set_string (value, win->mutter_hints);
      break;
341
342
343
    case PROP_APPEARS_FOCUSED:
      g_value_set_boolean (value, meta_window_appears_focused (win));
      break;
344
345
346
    case PROP_WM_CLASS:
      g_value_set_string (value, win->res_class);
      break;
347
348
349
350
351
352
    case PROP_RESIZEABLE:
      g_value_set_boolean (value, win->has_resize_func);
      break;
    case PROP_ABOVE:
      g_value_set_boolean (value, win->wm_state_above);
      break;
353
354
    case PROP_GTK_APPLICATION_ID:
      g_value_set_string (value, win->gtk_application_id);
355
      break;
356
357
    case PROP_GTK_UNIQUE_BUS_NAME:
      g_value_set_string (value, win->gtk_unique_bus_name);
358
      break;
359
360
361
362
363
364
365
366
367
368
369
    case PROP_GTK_APPLICATION_OBJECT_PATH:
      g_value_set_string (value, win->gtk_application_object_path);
      break;
    case PROP_GTK_WINDOW_OBJECT_PATH:
      g_value_set_string (value, win->gtk_window_object_path);
      break;
    case PROP_GTK_APP_MENU_OBJECT_PATH:
      g_value_set_string (value, win->gtk_app_menu_object_path);
      break;
    case PROP_GTK_MENUBAR_OBJECT_PATH:
      g_value_set_string (value, win->gtk_menubar_object_path);
370
      break;
371
372
373
    case PROP_ON_ALL_WORKSPACES:
      g_value_set_boolean (value, win->on_all_workspaces);
      break;
374
375
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376
      break;
377
378
379
380
381
382
383
384
385
386
387
388
389
    }
}

static void
meta_window_set_property(GObject         *object,
                         guint            prop_id,
                         const GValue    *value,
                         GParamSpec      *pspec)
{
  switch (prop_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390
      break;
391
392
393
    }
}

Owen Taylor's avatar
Owen Taylor committed
394
395
396
397
398
399
static void
meta_window_class_init (MetaWindowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = meta_window_finalize;
400

401
402
  object_class->get_property = meta_window_get_property;
  object_class->set_property = meta_window_set_property;
403

404
405
  klass->grab_op_began = meta_window_real_grab_op_began;
  klass->grab_op_ended = meta_window_real_grab_op_ended;
406
  klass->current_workspace_changed = meta_window_real_current_workspace_changed;
407
  klass->update_struts = meta_window_real_update_struts;
408
  klass->get_default_skip_hints = meta_window_real_get_default_skip_hints;
409
  klass->update_icon = meta_window_real_update_icon;
410

411
412
413
414
415
416
417
418
419
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  obj_props[PROP_TITLE] =
    g_param_spec_string ("title",
                         "Title",
                         "The title of the window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_ICON] =
    g_param_spec_object ("icon",
                         "Icon",
                         "96 pixel sized icon",
                         GDK_TYPE_PIXBUF,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_MINI_ICON] =
    g_param_spec_object ("mini-icon",
                         "Mini Icon",
                         "16 pixel sized icon",
                         GDK_TYPE_PIXBUF,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_DECORATED] =
    g_param_spec_boolean ("decorated",
                          "Decorated",
                          "Whether window is decorated",
                          TRUE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_FULLSCREEN] =
    g_param_spec_boolean ("fullscreen",
                          "Fullscreen",
                          "Whether window is fullscreened",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_MAXIMIZED_HORIZONTALLY] =
    g_param_spec_boolean ("maximized-horizontally",
                          "Maximized horizontally",
                          "Whether window is maximized horizontally",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_MAXIMIZED_VERTICALLY] =
    g_param_spec_boolean ("maximized-vertically",
                          "Maximizing vertically",
                          "Whether window is maximized vertically",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_MINIMIZED] =
    g_param_spec_boolean ("minimized",
                          "Minimizing",
                          "Whether window is minimized",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_WINDOW_TYPE] =
    g_param_spec_enum ("window-type",
                       "Window Type",
                       "The type of the window",
                       META_TYPE_WINDOW_TYPE,
                       META_WINDOW_NORMAL,
                       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_USER_TIME] =
    g_param_spec_uint ("user-time",
                       "User time",
                       "Timestamp of last user interaction",
                       0,
                       G_MAXUINT,
                       0,
                       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_DEMANDS_ATTENTION] =
    g_param_spec_boolean ("demands-attention",
                          "Demands Attention",
                          "Whether the window has _NET_WM_STATE_DEMANDS_ATTENTION set",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_URGENT] =
    g_param_spec_boolean ("urgent",
                          "Urgent",
                          "Whether the urgent flag of WM_HINTS is set",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_SKIP_TASKBAR] =
    g_param_spec_boolean ("skip-taskbar",
                          "Skip taskbar",
                          "Whether the skip-taskbar flag of WM_HINTS is set",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_MUTTER_HINTS] =
    g_param_spec_string ("mutter-hints",
                         "_MUTTER_HINTS",
                         "Contents of the _MUTTER_HINTS property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_APPEARS_FOCUSED] =
    g_param_spec_boolean ("appears-focused",
                          "Appears focused",
                          "Whether the window is drawn as being focused",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_RESIZEABLE] =
    g_param_spec_boolean ("resizeable",
                          "Resizeable",
                          "Whether the window can be resized",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_ABOVE] =
    g_param_spec_boolean ("above",
                          "Above",
                          "Whether the window is shown as always-on-top",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_WM_CLASS] =
    g_param_spec_string ("wm-class",
                         "WM_CLASS",
                         "Contents of the WM_CLASS property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_GTK_APPLICATION_ID] =
    g_param_spec_string ("gtk-application-id",
                         "_GTK_APPLICATION_ID",
                         "Contents of the _GTK_APPLICATION_ID property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_GTK_UNIQUE_BUS_NAME] =
    g_param_spec_string ("gtk-unique-bus-name",
                         "_GTK_UNIQUE_BUS_NAME",
                         "Contents of the _GTK_UNIQUE_BUS_NAME property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_GTK_APPLICATION_OBJECT_PATH] =
    g_param_spec_string ("gtk-application-object-path",
                         "_GTK_APPLICATION_OBJECT_PATH",
                         "Contents of the _GTK_APPLICATION_OBJECT_PATH property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_GTK_WINDOW_OBJECT_PATH] =
    g_param_spec_string ("gtk-window-object-path",
                         "_GTK_WINDOW_OBJECT_PATH",
                         "Contents of the _GTK_WINDOW_OBJECT_PATH property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_GTK_APP_MENU_OBJECT_PATH] =
    g_param_spec_string ("gtk-app-menu-object-path",
                         "_GTK_APP_MENU_OBJECT_PATH",
                         "Contents of the _GTK_APP_MENU_OBJECT_PATH property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_GTK_MENUBAR_OBJECT_PATH] =
    g_param_spec_string ("gtk-menubar-object-path",
                         "_GTK_MENUBAR_OBJECT_PATH",
                         "Contents of the _GTK_MENUBAR_OBJECT_PATH property of this window",
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  obj_props[PROP_ON_ALL_WORKSPACES] =
    g_param_spec_boolean ("on-all-workspaces",
                          "On all workspaces",
                          "Whether the window is set to appear on all workspaces",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, LAST_PROP, obj_props);
566

567
568
569
570
  window_signals[WORKSPACE_CHANGED] =
    g_signal_new ("workspace-changed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
571
                  0,
Jasper St. Pierre's avatar
Jasper St. Pierre committed
572
                  NULL, NULL, NULL,
573
574
                  G_TYPE_NONE, 1,
                  G_TYPE_INT);
Tomas Frydrych's avatar
Tomas Frydrych committed
575
576
577
578
579

  window_signals[FOCUS] =
    g_signal_new ("focus",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
580
                  0,
Jasper St. Pierre's avatar
Jasper St. Pierre committed
581
                  NULL, NULL, NULL,
Tomas Frydrych's avatar
Tomas Frydrych committed
582
                  G_TYPE_NONE, 0);
Tomas Frydrych's avatar
Tomas Frydrych committed
583
584
585
586
587

  window_signals[RAISED] =
    g_signal_new ("raised",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
588
                  0,
Jasper St. Pierre's avatar
Jasper St. Pierre committed
589
                  NULL, NULL, NULL,
Tomas Frydrych's avatar
Tomas Frydrych committed
590
                  G_TYPE_NONE, 0);
591
592
593
594
595

  window_signals[UNMANAGED] =
    g_signal_new ("unmanaged",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
596
                  0,
Jasper St. Pierre's avatar
Jasper St. Pierre committed
597
                  NULL, NULL, NULL,
598
                  G_TYPE_NONE, 0);
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632

  /**
   * MetaWindow::position-changed:
   * @window: a #MetaWindow
   *
   * This is emitted when the position of a window might
   * have changed. Specifically, this is emitted when the
   * position of the toplevel window has changed, or when
   * the position of the client window has changed.
   */
  window_signals[POSITION_CHANGED] =
    g_signal_new ("position-changed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  /**
   * MetaWindow::size-changed:
   * @window: a #MetaWindow
   *
   * This is emitted when the position of a window might
   * have changed. Specifically, this is emitted when the
   * size of the toplevel window has changed, or when the
   * size of the client window has changed.
   */
  window_signals[SIZE_CHANGED] =
    g_signal_new ("size-changed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
633
634
635
636
637
}

static void
meta_window_init (MetaWindow *self)
{
638
  meta_prefs_add_listener (prefs_changed_callback, self);
Owen Taylor's avatar
Owen Taylor committed
639
640
}

641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
static gboolean
is_desktop_or_dock_foreach (MetaWindow *window,
                            void       *data)
{
  gboolean *result = data;

  *result =
    window->type == META_WINDOW_DESKTOP ||
    window->type == META_WINDOW_DOCK;
  if (*result)
    return FALSE; /* stop as soon as we find one */
  else
    return TRUE;
}

/* window is the window that's newly mapped provoking
 * the possible change
 */
static void
maybe_leave_show_desktop_mode (MetaWindow *window)
{
  gboolean is_desktop_or_dock;

664
  if (!window->screen->active_workspace->showing_desktop)
665
666
667
668
669
670
671
672
    return;

  /* If the window is a transient for the dock or desktop, don't
   * leave show desktop mode when the window opens. That's
   * so you can e.g. hide all windows, manipulate a file on
   * the desktop via a dialog, then unshow windows again.
   */
  is_desktop_or_dock = FALSE;
673
674
  is_desktop_or_dock_foreach (window,
                              &is_desktop_or_dock);
675
676
677
678
679
680

  meta_window_foreach_ancestor (window, is_desktop_or_dock_foreach,
                                &is_desktop_or_dock);

  if (!is_desktop_or_dock)
    {
681
682
      meta_screen_minimize_all_on_active_workspace_except (window->screen,
                                                           window);
683
      meta_screen_unshow_desktop (window->screen);
684
685
686
    }
}

687
688
689
690
691
692
693
694
695
696
697
698
699
gboolean
meta_window_should_attach_to_parent (MetaWindow *window)
{
  MetaWindow *parent;

  if (!meta_prefs_get_attach_modal_dialogs () ||
      window->type != META_WINDOW_MODAL_DIALOG)
    return FALSE;

  parent = meta_window_get_transient_for (window);
  if (!parent)
    return FALSE;

700
701
702
703
704
705
706
707
708
709
  switch (parent->type)
    {
    case META_WINDOW_NORMAL:
    case META_WINDOW_DIALOG:
    case META_WINDOW_MODAL_DIALOG:
      return TRUE;

    default:
      return FALSE;
    }
710
711
}

712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
static gboolean
client_window_should_be_mapped (MetaWindow *window)
{
  return !window->shaded;
}

static void
sync_client_window_mapped (MetaWindow *window)
{
  gboolean should_be_mapped = client_window_should_be_mapped (window);

  if (window->mapped == should_be_mapped)
    return;

  window->mapped = should_be_mapped;

  meta_error_trap_push (window->display);
  if (should_be_mapped)
    {
      XMapWindow (window->display->xdisplay, window->xwindow);
    }
  else
    {
      XUnmapWindow (window->display->xdisplay, window->xwindow);
      window->unmaps_pending ++;
    }
  meta_error_trap_pop (window->display);
}

741
742
743
744
745
746
747
748
749
750
751
static void
meta_window_update_desc (MetaWindow *window)
{
  g_clear_pointer (&window->desc, g_free);

  if (window->title)
    window->desc = g_strdup_printf ("0x%lx (%.10s)", window->xwindow, window->title);
  else
    window->desc = g_strdup_printf ("0x%lx", window->xwindow);
}

752
753
754
755
756
757
758
759
760
MetaWindow *
_meta_window_shared_new (MetaDisplay         *display,
                         MetaScreen          *screen,
                         MetaWindowClientType client_type,
                         MetaWaylandSurface  *surface,
                         Window               xwindow,
                         gulong               existing_wm_state,
                         MetaCompEffect       effect,
                         XWindowAttributes   *attrs)
Havoc Pennington's avatar
Havoc Pennington committed
761
762
{
  MetaWindow *window;
rhp's avatar
...    
rhp committed
763
  MetaWorkspace *space;
Havoc Pennington's avatar
Havoc Pennington committed
764
765

  g_assert (attrs != NULL);
766

767
  meta_verbose ("attrs->map_state = %d (%s)\n",
Havoc Pennington's avatar
Havoc Pennington committed
768
769
                attrs->map_state,
                (attrs->map_state == IsUnmapped) ?
770
                "IsUnmapped" :
Havoc Pennington's avatar
Havoc Pennington committed
771
                (attrs->map_state == IsViewable) ?
772
                "IsViewable" :
Havoc Pennington's avatar
Havoc Pennington committed
773
                (attrs->map_state == IsUnviewable) ?
774
775
                "IsUnviewable" :
                "(unknown)");
776

777
778
779
  if (client_type == META_WINDOW_CLIENT_TYPE_X11)
    window = g_object_new (META_TYPE_WINDOW_X11, NULL);
  else
780
    window = g_object_new (META_TYPE_WINDOW_WAYLAND, NULL);
rhp's avatar
...  
rhp committed
781

782
  window->constructing = TRUE;
783

784
  window->dialog_pid = -1;
785

786
787
  window->client_type = client_type;
  window->surface = surface;
rhp's avatar
...  
rhp committed
788
  window->xwindow = xwindow;
789

rhp's avatar
...    
rhp committed
790
791
792
793
  /* this is in window->screen->display, but that's too annoying to
   * type
   */
  window->display = display;
794
  window->workspace = NULL;
795

796
797
  window->sync_request_counter = None;
  window->sync_request_serial = 0;
798
  window->sync_request_timeout_id = 0;
799
  window->sync_request_alarm = None;
800

801
  window->screen = screen;
rhp's avatar
...    
rhp committed
802

803
  meta_window_update_desc (window);
804

805
806
  window->override_redirect = attrs->override_redirect;

rhp's avatar
...    
rhp committed
807
808
  /* avoid tons of stack updates */
  meta_stack_freeze (window->screen->stack);
809

Havoc Pennington's avatar
Havoc Pennington committed
810
811
812
813
  window->rect.x = attrs->x;
  window->rect.y = attrs->y;
  window->rect.width = attrs->width;
  window->rect.height = attrs->height;
rhp's avatar
...    
rhp committed
814

815
  /* size_hints are the "request" */
Havoc Pennington's avatar
Havoc Pennington committed
816
817
818
819
  window->size_hints.x = attrs->x;
  window->size_hints.y = attrs->y;
  window->size_hints.width = attrs->width;
  window->size_hints.height = attrs->height;
820
821
  /* initialize the remaining size_hints as if size_hints.flags were zero */
  meta_set_normal_hints (window, NULL);
rhp's avatar
...    
rhp committed
822
823
824

  /* And this is our unmaximized size */
  window->saved_rect = window->rect;
825
  window->unconstrained_rect = window->rect;
826

Havoc Pennington's avatar
Havoc Pennington committed
827
828
  window->depth = attrs->depth;
  window->xvisual = attrs->visual;
829

rhp's avatar
...    
rhp committed
830
  window->title = NULL;
rhp's avatar
...    
rhp committed
831
  window->icon = NULL;
832
  window->mini_icon = NULL;
rhp's avatar
...    
rhp committed
833
834

  window->frame = NULL;
rhp's avatar
...    
rhp committed
835
  window->has_focus = FALSE;
836
  window->attached_focus_window = NULL;
rhp's avatar
...    
rhp committed
837

838
839
840
841
  window->maximized_horizontally = FALSE;
  window->maximized_vertically = FALSE;
  window->maximize_horizontally_after_placement = FALSE;
  window->maximize_vertically_after_placement = FALSE;
842
  window->minimize_after_placement = FALSE;
843
  window->fullscreen = FALSE;
844
  window->fullscreen_after_placement = FALSE;
845
  window->fullscreen_monitors[0] = -1;
846
  window->require_fully_onscreen = TRUE;
847
  window->require_on_single_monitor = TRUE;
848
  window->require_titlebar_visible = TRUE;
rhp's avatar
...    
rhp committed
849
  window->on_all_workspaces = FALSE;
850
  window->on_all_workspaces_requested = FALSE;
851
  window->tile_mode = META_TILE_NONE;
852
  window->tile_monitor_number = -1;
rhp's avatar
...    
rhp committed
853
  window->shaded = FALSE;
rhp's avatar
...    
rhp committed
854
855
  window->initially_iconic = FALSE;
  window->minimized = FALSE;
856
  window->tab_unminimized = FALSE;
rhp's avatar
...    
rhp committed
857
  window->iconic = FALSE;
Havoc Pennington's avatar
Havoc Pennington committed
858
  window->mapped = attrs->map_state != IsUnmapped;
859
  window->hidden = FALSE;
860
  window->known_to_compositor = FALSE;
861
862
  window->visible_to_compositor = FALSE;
  window->pending_compositor_effect = effect;
863
864
  /* if already mapped, no need to worry about focus-on-first-time-showing */
  window->showing_for_first_time = !window->mapped;
865
866
867
  /* if already mapped we don't want to do the placement thing;
   * override-redirect windows are placed by the app */
  window->placed = ((window->mapped && !window->hidden) || window->override_redirect);
868
  window->denied_focus_and_not_transient = FALSE;
rhp's avatar
...    
rhp committed
869
  window->unmanaging = FALSE;
870
  window->is_in_queues = 0;
rhp's avatar
...    
rhp committed
871
872
  window->keys_grabbed = FALSE;
  window->grab_on_frame = FALSE;
rhp's avatar
...    
rhp committed
873
  window->all_keys_grabbed = FALSE;
rhp's avatar
...    
rhp committed
874
  window->withdrawn = FALSE;
rhp's avatar
...    
rhp committed
875
  window->initial_workspace_set = FALSE;
876
877
  window->initial_timestamp_set = FALSE;
  window->net_wm_user_time_set = FALSE;
878
  window->user_time_window = None;
879
880
  window->take_focus = FALSE;
  window->delete_window = FALSE;
881
  window->can_ping = FALSE;
882
  window->input = TRUE;
rhp's avatar
...    
rhp committed
883
  window->calc_placement = FALSE;
884
  window->shaken_loose = FALSE;
885
  window->have_focus_click_grab = FALSE;
886
  window->disable_sync = FALSE;
887

rhp's avatar
...    
rhp committed
888
  window->unmaps_pending = 0;
889

rhp's avatar
...    
rhp committed
890
  window->mwm_decorated = TRUE;
891
  window->mwm_border_only = FALSE;
rhp's avatar
...    
rhp committed
892
893
894
895
896
  window->mwm_has_close_func = TRUE;
  window->mwm_has_minimize_func = TRUE;
  window->mwm_has_maximize_func = TRUE;
  window->mwm_has_move_func = TRUE;
  window->mwm_has_resize_func = TRUE;
897

898
899
900
901
902
  if (client_type == META_WINDOW_CLIENT_TYPE_X11)
    window->decorated = TRUE;
  else
    window->decorated = FALSE;

rhp's avatar
...    
rhp committed
903
904
905
  window->has_close_func = TRUE;
  window->has_minimize_func = TRUE;
  window->has_maximize_func = TRUE;
rhp's avatar
...    
rhp committed
906
907
908
  window->has_move_func = TRUE;
  window->has_resize_func = TRUE;

rhp's avatar
...    
rhp committed
909
  window->has_shade_func = TRUE;
910
911

  window->has_fullscreen_func = TRUE;
912
913

  window->always_sticky = FALSE;
914

915
916
  window->skip_taskbar = FALSE;
  window->skip_pager = FALSE;
Havoc Pennington's avatar
Havoc Pennington committed
917
918
  window->wm_state_above = FALSE;
  window->wm_state_below = FALSE;
919
  window->wm_state_demands_attention = FALSE;
920

rhp's avatar
...    
rhp committed
921
922
923
924
  window->res_class = NULL;
  window->res_name = NULL;
  window->role = NULL;
  window->sm_client_id = NULL;
925
  window->wm_client_machine = NULL;
926
  window->is_remote = FALSE;
Havoc Pennington's avatar
Havoc Pennington committed
927
  window->startup_id = NULL;
928

929
  window->net_wm_pid = -1;
930

rhp's avatar
...    
rhp committed
931
932
  window->xtransient_for = None;
  window->xclient_leader = None;
933
  window->transient_parent_is_root_window = FALSE;
934

rhp's avatar
...    
rhp committed
935
  window->type = META_WINDOW_NORMAL;
rhp's avatar
...    
rhp committed
936

937
  window->struts = NULL;
938

939
940
  window->layer = META_LAYER_LAST; /* invalid value */
  window->stack_position = -1;
rhp's avatar
...    
rhp committed
941
  window->initial_workspace = 0; /* not used */
942
  window->initial_timestamp = 0; /* not used */
943
944

  window->compositor_private = NULL;
945

946
  window->monitor = meta_screen_get_monitor_for_window (window->screen, window);
947
  window->preferred_output_winsys_id = window->monitor->winsys_id;
948

949
950
  window->tile_match = NULL;

951
952
953
954
  /* Assign this #MetaWindow a sequence number which can be used
   * for sorting.
   */
  window->stable_sequence = ++display->window_sequence_counter;
Havoc Pennington's avatar
Havoc Pennington committed
955

956
957
  window->opacity = 0xFF;

958
959
960
961
962
963
964
965
966
967
  if (window->override_redirect)
    {
      window->decorated = FALSE;
      window->always_sticky = TRUE;
      window->has_close_func = FALSE;
      window->has_shade_func = FALSE;
      window->has_move_func = FALSE;
      window->has_resize_func = FALSE;
    }

968
969
  META_WINDOW_GET_CLASS (window)->manage (window);

970
  if (!window->override_redirect)
971
    meta_window_update_icon_now (window, TRUE);
972

rhp's avatar
...    
rhp committed
973
974
  if (window->initially_iconic)
    {
rhp's avatar
...    
rhp committed
975
976
977
      /* WM_HINTS said minimized */
      window->minimized = TRUE;
      meta_verbose ("Window %s asked to start out minimized\n", window->desc);
rhp's avatar
...    
rhp committed
978
    }
rhp's avatar
...    
rhp committed
979
980
981
982
983
984
985
986
987
988
989
990
991

  if (existing_wm_state == IconicState)
    {
      /* WM_STATE said minimized */
      window->minimized = TRUE;
      meta_verbose ("Window %s had preexisting WM_STATE = IconicState, minimizing\n",
                    window->desc);

      /* Assume window was previously placed, though perhaps it's
       * been iconic its whole life, we have no way of knowing.
       */
      window->placed = TRUE;
    }
Havoc Pennington's avatar
Havoc Pennington committed
992
993
994
995
996

  /* Apply any window attributes such as initial workspace
   * based on startup notification
   */
  meta_screen_apply_startup_properties (window->screen, window);
997
998
999
1000
1001
1002
1003

  /* Try to get a "launch timestamp" for the window.  If the window is
   * a transient, we'd like to be able to get a last-usage timestamp
   * from the parent window.  If the window has no parent, there isn't
   * much we can do...except record the current time so that any children
   * can use this time as a fallback.
   */
1004
  if (!window->override_redirect && !window->net_wm_user_time_set) {
1005
1006
1007
    /* First, maybe the app was launched with startup notification using an
     * obsolete version of the spec; use that timestamp if it exists.
     */
1008
    if (window->initial_timestamp_set)
1009
1010
1011
      /* NOTE: Do NOT toggle net_wm_user_time_set to true; this is just
       * being recorded as a fallback for potential transients
       */
1012
      window->net_wm_user_time = window->initial_timestamp;
1013
1014
    else if (window->transient_for != NULL)
      meta_window_set_user_time (window, window->transient_for->net_wm_user_time);
1015
    else
1016
1017
1018
      /* NOTE: Do NOT toggle net_wm_user_time_set to true; this is just
       * being recorded as a fallback for potential transients
       */
1019
1020
1021
      window->net_wm_user_time =
        meta_display_get_current_time_roundtrip (window->display);
  }
1022

1023
1024
  window->attached = meta_window_should_attach_to_parent (window);
  if (window->attached)
Jasper St. Pierre's avatar
Jasper St. Pierre committed
1025
    meta_window_recalc_features (window);
1026

1027
  if (window->type == META_WINDOW_DESKTOP ||
1028
      window->type == META_WINDOW_DOCK)
1029
1030
1031
1032
1033
1034
    {
      /* Change the default, but don't enforce this if the user
       * focuses the dock/desktop and unsticks it using key shortcuts.
       * Need to set this before adding to the workspaces so the MRU
       * lists will be updated.
       */
1035
      window->on_all_workspaces_requested = TRUE;
1036
    }
1037

1038
1039
  window->on_all_workspaces = should_be_on_all_workspaces (window);

rhp's avatar
...    
rhp committed
1040
1041
1042
1043
  /* For the workspace, first honor hints,
   * if that fails put transients with parents,
   * otherwise put window on active space
   */
1044

rhp's avatar
...    
rhp committed
1045
1046
  if (window->initial_workspace_set)
    {
1047
      if (window->initial_workspace == (int) 0xFFFFFFFF)
1048
        {
1049
1050
1051
          meta_topic (META_DEBUG_PLACEMENT,
                      "Window %s is initially on all spaces\n",
                      window->desc);
1052

1053
1054
1055
	  /* need to set on_all_workspaces first so that it will be
	   * added to all the MRU lists
	   */
1056
          window->on_all_workspaces_requested = TRUE;
Rob Adams's avatar
Rob Adams committed
1057
          window->on_all_workspaces = TRUE;
1058
          meta_workspace_add_window (window->screen->active_workspace, window);
1059
1060
1061
        }
      else
        {
1062
1063
1064
1065
          meta_topic (META_DEBUG_PLACEMENT,
                      "Window %s is initially on space %d\n",
                      window->desc, window->initial_workspace);

1066
          space =
1067
1068
            meta_screen_get_workspace_by_index (window->screen,
                                                window->initial_workspace);
1069

1070
1071
1072
          if (space)
            meta_workspace_add_window (space, window);
        }
rhp's avatar
...    
rhp committed
1073
    }
1074

1075
1076
1077
1078
1079
1080
1081
  /* override-redirect windows are subtly different from other windows
   * with window->on_all_workspaces == TRUE. Other windows are part of
   * some workspace (so they can return to that if the flag is turned off),
   * but appear on other workspaces. override-redirect windows are part
   * of no workspace.
   */
  if (!window->override_redirect)
rhp's avatar
...    
rhp committed
1082
    {
1083
      if (window->workspace == NULL &&
1084
          window->transient_for != NULL)
1085
1086
        {
          /* Try putting dialog on parent's workspace */
1087
          if (window->transient_for->workspace)
1088
1089
1090
            {
              meta_topic (META_DEBUG_PLACEMENT,
                          "Putting window %s on same workspace as parent %s\n",
1091
                          window->desc, window->transient_for->desc);
1092

1093
              if (window->transient_for->on_all_workspaces_requested)
1094
1095
1096
1097
                {
                  window->on_all_workspaces_requested = TRUE;
                  window->on_all_workspaces = TRUE;
                }
1098

1099
1100
              /* this will implicitly add to the appropriate MRU lists
               */
1101
              meta_workspace_add_window (window->transient_for->workspace, window);
1102
            }
rhp's avatar
...    
rhp committed
1103
        }
1104

1105
1106
1107
1108
1109
      if (window->workspace == NULL)
        {
          meta_topic (META_DEBUG_PLACEMENT,
                      "Putting window %s on active workspace\n",
                      window->desc);
1110

1111
          space = window->screen->active_workspace;
rhp's avatar
...    
rhp committed
1112

1113
1114
          meta_workspace_add_window (space, window);
        }
1115

1116
      /* for the various on_all_workspaces = TRUE possible above */
1117
      meta_window_current_workspace_changed (window);
1118

1119
1120
      meta_window_update_struts (window);
    }
1121

1122
1123
  g_signal_emit_by_name (window->screen, "window-entered-monitor", window->monitor->number, window);

1124
1125
1126
1127
1128
1129
1130
  /* Must add window to stack before doing move/resize, since the
   * window might have fullscreen size (i.e. should have been
   * fullscreen'd; acrobat is one such braindead case; it withdraws
   * and remaps its window whenever trying to become fullscreen...)
   * and thus constraints may try to auto-fullscreen it which also
   * means restacking it.
   */
1131
1132
1133
1134
1135
  if (!window->override_redirect)
    meta_stack_add (window->screen->stack,
                    window);
  else
    window->layer = META_LAYER_OVERRIDE_REDIRECT; /* otherwise set by MetaStack */
1136

1137
1138
1139
1140
1141
  if (!window->override_redirect)
    {
      /* FIXME we have a tendency to set this then immediately
       * change it again.
       */
1142
      set_wm_state (window);