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

rhp's avatar
rhp committed
3 4
/* Metacity X display handler */

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

25
/**
26
 * \file display.c Handles operations on an X display.
27 28 29 30
 *
 * The display is represented as a MetaDisplay struct.
 */

31 32
#include "config.h"

33
#include "display-private.h"
rhp's avatar
rhp committed
34 35
#include "util.h"
#include "main.h"
36 37
#include "screen-private.h"
#include "window-private.h"
38
#include "window-props.h"
39
#include "group-props.h"
40
#include "frame-private.h"
rhp's avatar
rhp committed
41
#include "errors.h"
rhp's avatar
rhp committed
42
#include "keybindings.h"
43
#include "prefs.h"
44
#include "resizepopup.h"
45
#include "xprops.h"
rhp's avatar
rhp committed
46
#include "workspace.h"
47
#include "bell.h"
48
#include "effects.h"
49
#include "meta-compositor.h"
50
#include <libmetacity/meta-frame-borders.h>
rhp's avatar
rhp committed
51
#include <X11/Xatom.h>
rhp's avatar
rhp committed
52
#include <X11/cursorfont.h>
53 54 55
#ifdef HAVE_RANDR
#include <X11/extensions/Xrandr.h>
#endif
56
#include <X11/extensions/shape.h>
57
#include <X11/extensions/Xrender.h>
58 59 60
#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif
61 62 63
#ifdef HAVE_XCURSOR
#include <X11/Xcursor/Xcursor.h>
#endif
Iain Holmes's avatar
Iain Holmes committed
64 65 66
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>
#include <X11/extensions/Xfixes.h>
rhp's avatar
rhp committed
67
#include <string.h>
rhp's avatar
rhp committed
68

69 70
#include "compositor/meta-compositor-none.h"
#include "compositor/meta-compositor-xrender.h"
71 72

#ifdef HAVE_VULKAN
73
#include "compositor/meta-compositor-vulkan.h"
74
#endif
75

76 77 78
#define GRAB_OP_IS_WINDOW_SWITCH(g)                     \
        (g == META_GRAB_OP_KEYBOARD_TABBING_NORMAL  ||  \
         g == META_GRAB_OP_KEYBOARD_TABBING_DOCK    ||  \
79
         g == META_GRAB_OP_KEYBOARD_TABBING_GROUP   ||  \
80
         g == META_GRAB_OP_KEYBOARD_ESCAPING_NORMAL ||  \
81 82
         g == META_GRAB_OP_KEYBOARD_ESCAPING_DOCK   ||  \
         g == META_GRAB_OP_KEYBOARD_ESCAPING_GROUP)
83

84 85
/**
 * \defgroup pings Pings
86
 *
87 88 89 90 91 92 93 94 95 96 97 98
 * Sometimes we want to see whether a window is responding,
 * so we send it a "ping" message and see whether it sends us back a "pong"
 * message within a reasonable time. Here we have a system which lets us
 * nominate one function to be called if we get the pong in time and another
 * function if we don't. The system is rather more complicated than it needs
 * to be, since we only ever use it to destroy windows which are asked to
 * close themselves and don't do so within a reasonable amount of time, and
 * therefore we always use the same callbacks. It's possible that we might
 * use it for other things in future, or on the other hand we might decide
 * that we're never going to do so and simplify it a bit.
 */

99 100
/**
 * Describes a ping on a window. When we send a ping to a window, we build
101 102 103 104 105 106 107
 * one of these structs, and it eventually gets passed to the timeout function
 * or to the function which handles the response from the window. If the window
 * does or doesn't respond to the ping, we use this information to deal with
 * these facts; we have a handler function for each.
 *
 * \ingroup pings
 */
108
typedef struct
Havoc Pennington's avatar
Havoc Pennington committed
109
{
110
  MetaDisplay *display;
111 112
  Window       xwindow;
  guint32      timestamp;
113 114
  MetaWindowPingFunc ping_reply_func;
  MetaWindowPingFunc ping_timeout_func;
115 116
  void        *user_data;
  guint        ping_timeout_id;
117 118
} MetaPingData;

119
typedef struct
Havoc Pennington's avatar
Havoc Pennington committed
120 121 122 123 124
{
  MetaDisplay *display;
  Window xwindow;
} MetaAutoRaiseData;

125 126 127 128 129 130 131
/**
 * The display we're managing.  This is a singleton object.  (Historically,
 * this was a list of displays, but there was never any way to add more
 * than one element to it.)  The goofy name is because we don't want it
 * to shadow the parameter in its object methods.
 */
static MetaDisplay *the_display = NULL;
132

133 134
static gboolean mousemods_disabled = FALSE;

rhp's avatar
rhp committed
135 136
static void   meta_spew_event           (MetaDisplay    *display,
                                         XEvent         *event);
137

rhp's avatar
rhp committed
138
static gboolean event_callback          (XEvent         *event,
rhp's avatar
rhp committed
139 140 141
                                         gpointer        data);
static Window event_get_modified_window (MetaDisplay    *display,
                                         XEvent         *event);
142 143
static guint32 event_get_time           (MetaDisplay    *display,
                                         XEvent         *event);
144 145
static void    process_request_frame_extents (MetaDisplay    *display,
                                              XEvent         *event);
146 147
static void    process_pong_message     (MetaDisplay    *display,
                                         XEvent         *event);
148 149 150 151
static void    process_selection_request (MetaDisplay   *display,
                                          XEvent        *event);
static void    process_selection_clear   (MetaDisplay   *display,
                                          XEvent        *event);
rhp's avatar
rhp committed
152

153 154
static void    update_window_grab_modifiers (MetaDisplay *display);

155 156 157
static void    set_mousemods_disabled (MetaDisplay *display,
                                       gboolean     setting);

158 159
static void    prefs_changed_callback    (MetaPreference pref,
                                          void          *data);
rhp's avatar
rhp committed
160

161
static void    sanity_check_timestamps   (MetaDisplay *display,
162
                                          guint32      known_good_timestamp);
163

164 165
MetaGroup*     get_focussed_group (MetaDisplay *display);

166 167
/**
 * Destructor for MetaPingData structs. Will destroy the
168 169 170 171
 * event source for the struct as well.
 *
 * \ingroup pings
 */
172 173 174 175 176 177 178 179 180 181
static void
ping_data_free (MetaPingData *ping_data)
{
  /* Remove the timeout */
  if (ping_data->ping_timeout_id != 0)
    g_source_remove (ping_data->ping_timeout_id);

  g_free (ping_data);
}

182 183
/**
 * Frees every pending ping structure for the given X window on the
184 185 186 187 188 189 190 191
 * given display. This means that we also destroy the timeouts.
 *
 * \param display The display the window appears on
 * \param xwindow The X ID of the window whose pings we should remove
 *
 * \ingroup pings
 *
 */
192 193 194 195 196 197 198
static void
remove_pending_pings_for_window (MetaDisplay *display, Window xwindow)
{
  GSList *tmp;
  GSList *dead;

  /* could obviously be more efficient, don't care */
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  /* build list to be removed */
  dead = NULL;
  for (tmp = display->pending_pings; tmp; tmp = tmp->next)
    {
      MetaPingData *ping_data = tmp->data;

      if (ping_data->xwindow == xwindow)
        dead = g_slist_prepend (dead, ping_data);
    }

  /* remove what we found */
  for (tmp = dead; tmp; tmp = tmp->next)
    {
      MetaPingData *ping_data = tmp->data;

      display->pending_pings = g_slist_remove (display->pending_pings, ping_data);
      ping_data_free (ping_data);
    }

  g_slist_free (dead);
}

222

Havoc Pennington's avatar
Havoc Pennington committed
223
#ifdef HAVE_STARTUP_NOTIFICATION
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
static void
sn_error_trap_push (SnDisplay *sn_display,
                    Display   *xdisplay)
{
  MetaDisplay *display;
  display = meta_display_for_x_display (xdisplay);
  if (display != NULL)
    meta_error_trap_push (display);
}

static void
sn_error_trap_pop (SnDisplay *sn_display,
                   Display   *xdisplay)
{
  MetaDisplay *display;
  display = meta_display_for_x_display (xdisplay);
  if (display != NULL)
241
    meta_error_trap_pop (display);
242
}
Havoc Pennington's avatar
Havoc Pennington committed
243
#endif
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
static void
reframe_func (MetaScreen *screen,
              MetaWindow *window,
              gpointer    user_data)
{
  meta_window_reframe (window);
}

static void
notify_composited_cb (MetaCompositor *compositor,
                      GParamSpec     *pspec,
                      MetaDisplay    *display)
{
  gboolean composited;

  meta_screen_foreach_window (display->screen, reframe_func, NULL);

  composited = meta_compositor_is_composited (display->compositor);
  meta_ui_set_composited (display->screen->ui, composited);
}

266 267
static MetaCompositorType
get_compositor_type (MetaDisplay *display)
Søren Sandmann's avatar
Søren Sandmann committed
268
{
269
  const gchar *compositor;
270 271
  MetaCompositorType type;

272
  compositor = g_getenv ("META_COMPOSITOR");
273

274 275 276 277 278 279 280 281 282
  if (compositor != NULL)
    {
      if (g_strcmp0 (compositor, "vulkan") == 0)
        type = META_COMPOSITOR_TYPE_VULKAN;
      else if (g_strcmp0 (compositor, "xrender") == 0)
        type = META_COMPOSITOR_TYPE_XRENDER;
      else
        type = META_COMPOSITOR_TYPE_NONE;
    }
283
  else
284 285 286 287 288 289
    {
      if (meta_prefs_get_compositing_manager ())
        type = META_COMPOSITOR_TYPE_XRENDER;
      else
        type = META_COMPOSITOR_TYPE_NONE;
    }
Søren Sandmann's avatar
Søren Sandmann committed
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  return type;
}

static MetaCompositor *
create_compositor (MetaDisplay         *display,
                   MetaCompositorType   type,
                   GError             **error)
{
  MetaCompositor *compositor;

  compositor = NULL;

  switch (type)
    {
      case META_COMPOSITOR_TYPE_NONE:
        compositor = meta_compositor_none_new (display, error);
        break;

      case META_COMPOSITOR_TYPE_XRENDER:
        compositor = meta_compositor_xrender_new (display, error);
        break;

      case META_COMPOSITOR_TYPE_VULKAN:
314
#ifdef HAVE_VULKAN
315
        compositor = meta_compositor_vulkan_new (display, error);
316 317 318 319
#else
        g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                             "Compiled without Vulkan support");
#endif
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        break;

      default:
        g_assert_not_reached ();
        break;
    }

  return compositor;
}

static void
update_compositor (MetaDisplay *display,
                   gboolean     composite_windows)
{
  GError *error;
  gboolean old_composited;
  gboolean composited;

  old_composited = FALSE;
  if (display->compositor != NULL)
    {
      old_composited = meta_compositor_is_composited (display->compositor);
      g_object_unref (display->compositor);
    }

  error = NULL;
  display->compositor = create_compositor (display,
                                           get_compositor_type (display),
                                           &error);

  if (error != NULL)
    {
      g_warning ("Failed to create compositor: %s", error->message);
      g_error_free (error);

      g_assert (display->compositor == NULL);
      display->compositor = create_compositor (display,
                                               META_COMPOSITOR_TYPE_NONE,
                                               NULL);

      g_assert (display->compositor != NULL);
    }
362 363 364 365

  g_signal_connect (display->compositor, "notify::composited",
                    G_CALLBACK (notify_composited_cb), display);

366 367 368
  composited = meta_compositor_is_composited (display->compositor);
  meta_ui_set_composited (display->screen->ui, composited);

369 370
  if (old_composited != composited)
    meta_screen_foreach_window (display->screen, reframe_func, NULL);
371

372 373
  if (composite_windows)
    meta_screen_composite_all_windows (display->screen);
Søren Sandmann's avatar
Søren Sandmann committed
374 375
}

376 377
/**
 * Opens a new display, sets it up, initialises all the X extensions
378 379 380 381 382 383 384 385
 * we will need, and adds it to the list of displays.
 *
 * \return True if the display was opened successfully, and False
 * otherwise-- that is, if the display doesn't exist or it already
 * has a window manager.
 *
 * \ingroup main
 */
rhp's avatar
rhp committed
386
gboolean
387
meta_display_open (void)
rhp's avatar
rhp committed
388 389
{
  Display *xdisplay;
390
  int i;
391
  guint32 timestamp;
392
  MetaScreen *screen;
393
  Window old_active_xwindow;
394 395

  /* A list of all atom names, so that we can intern them in one go. */
396
  const gchar *atom_names[] = {
397 398 399
#define item(x) #x,
#include "atomnames.h"
#undef item
rhp's avatar
rhp committed
400
  };
rhp's avatar
rhp committed
401
  Atom atoms[G_N_ELEMENTS(atom_names)];
402

403
  meta_verbose ("Opening display '%s'\n", XDisplayName (NULL));
rhp's avatar
rhp committed
404

405
  xdisplay = meta_ui_get_display ();
406

rhp's avatar
rhp committed
407 408
  if (xdisplay == NULL)
    {
409 410 411
      g_warning ("Failed to open X Window System display '%s'",
                 XDisplayName (NULL));

rhp's avatar
rhp committed
412 413 414
      return FALSE;
    }

rhp's avatar
rhp committed
415 416
  if (meta_is_syncing ())
    XSynchronize (xdisplay, True);
417

418 419
  g_assert (the_display == NULL);
  the_display = g_new (MetaDisplay, 1);
rhp's avatar
rhp committed
420

421
  the_display->closing = 0;
422

rhp's avatar
rhp committed
423 424 425 426
  /* here we use XDisplayName which is what the user
   * probably put in, vs. DisplayString(display) which is
   * canonicalized by XOpenDisplay()
   */
427 428 429 430 431 432 433 434 435 436 437 438
  the_display->name = g_strdup (XDisplayName (NULL));
  the_display->xdisplay = xdisplay;
  the_display->error_trap_synced_at_last_pop = TRUE;
  the_display->error_traps = 0;
  the_display->error_trap_handler = NULL;
  the_display->server_grab_count = 0;
  the_display->display_opening = TRUE;

  the_display->pending_pings = NULL;
  the_display->autoraise_timeout_id = 0;
  the_display->autoraise_window = NULL;
  the_display->focus_window = NULL;
439 440 441
  the_display->focus_serial = 0;
  the_display->server_focus_window = None;
  the_display->server_focus_serial = 0;
442 443 444 445
  the_display->grab_old_window_stacking = NULL;

  the_display->mouse_mode = TRUE; /* Only relevant for mouse or sloppy focus */
  the_display->allow_terminal_deactivation = TRUE; /* Only relevant for when a
446
                                                  terminal has the focus */
447

448
  meta_bell_init (the_display);
449

450
  meta_display_init_keys (the_display);
rhp's avatar
rhp committed
451

452
  update_window_grab_modifiers (the_display);
453

454
  meta_prefs_add_listener (prefs_changed_callback, the_display);
Iain Holmes's avatar
Iain Holmes committed
455

456
  meta_verbose ("Creating %d atoms\n", (int) G_N_ELEMENTS (atom_names));
457 458
  XInternAtoms (the_display->xdisplay, (gchar **) atom_names,
                G_N_ELEMENTS (atom_names), False, atoms);
459
  {
460 461
    int atom_i = 0;
#define item(x) the_display->atom_##x = atoms[atom_i++];
462 463 464
#include "atomnames.h"
#undef item
  }
465 466 467 468 469

  the_display->prop_hooks = NULL;
  meta_display_init_window_prop_hooks (the_display);
  the_display->group_prop_hooks = NULL;
  meta_display_init_group_prop_hooks (the_display);
470

rhp's avatar
rhp committed
471 472 473
  /* Offscreen unmapped window used for _NET_SUPPORTING_WM_CHECK,
   * created in screen_new
   */
474 475
  the_display->leader_window = None;
  the_display->timestamp_pinging_window = None;
Havoc Pennington's avatar
Havoc Pennington committed
476

477
  the_display->monitor_cache_invalidated = TRUE;
478

479
  the_display->groups_by_leader = NULL;
480

481 482
  the_display->window_with_menu = NULL;
  the_display->window_menu = NULL;
483

484
  the_display->screen = NULL;
485

486
#ifdef HAVE_STARTUP_NOTIFICATION
487
  the_display->sn_display = sn_display_new (the_display->xdisplay,
488 489 490
                                        sn_error_trap_push,
                                        sn_error_trap_pop);
#endif
491

rhp's avatar
rhp committed
492
  /* Get events */
493
  meta_ui_add_event_func (the_display->xdisplay,
rhp's avatar
rhp committed
494
                          event_callback,
495
                          the_display);
496

497
  the_display->window_ids = g_hash_table_new (meta_unsigned_long_hash,
498
                                          meta_unsigned_long_equal);
499

500 501 502
  i = 0;
  while (i < N_IGNORED_SERIALS)
    {
503
      the_display->ignored_serials[i] = 0;
504 505
      ++i;
    }
506
  the_display->ungrab_should_not_cause_focus_window = None;
507

508 509
  the_display->current_time = CurrentTime;
  the_display->sentinel_counter = 0;
510

511 512
  the_display->grab_resize_timeout_id = 0;
  the_display->grab_have_keyboard = FALSE;
513 514

#ifdef HAVE_XKB
515
  the_display->last_bell_time = 0;
516
#endif
517

518 519 520 521 522
  the_display->grab_op = META_GRAB_OP_NONE;
  the_display->grab_wireframe_active = FALSE;
  the_display->grab_window = NULL;
  the_display->grab_screen = NULL;
  the_display->grab_resize_popup = NULL;
523 524
  the_display->grab_tile_mode = META_TILE_NONE;
  the_display->grab_tile_monitor_number = -1;
525

526
  the_display->grab_edge_resistance_data = NULL;
527

528 529
  {
    int major, minor;
530

531
    the_display->have_xsync = FALSE;
532

533 534
    the_display->xsync_error_base = 0;
    the_display->xsync_event_base = 0;
535 536 537 538

    /* I don't think we really have to fill these in */
    major = SYNC_MAJOR_VERSION;
    minor = SYNC_MINOR_VERSION;
539

540 541 542 543
    if (!XSyncQueryExtension (the_display->xdisplay,
                              &the_display->xsync_event_base,
                              &the_display->xsync_error_base) ||
        !XSyncInitialize (the_display->xdisplay,
544 545
                          &major, &minor))
      {
546 547
        the_display->xsync_error_base = 0;
        the_display->xsync_event_base = 0;
548
      }
549
    else
Owen W. Taylor's avatar
Owen W. Taylor committed
550 551 552 553
      {
        the_display->have_xsync = TRUE;
        XSyncSetPriority (the_display->xdisplay, None, 10);
      }
554

555 556
    meta_verbose ("Attempted to init Xsync, found version %d.%d error base %d event base %d\n",
                  major, minor,
557 558
                  the_display->xsync_error_base,
                  the_display->xsync_event_base);
559
  }
560 561

  {
562
    the_display->have_shape = FALSE;
563

564 565
    the_display->shape_error_base = 0;
    the_display->shape_event_base = 0;
566

567 568 569
    if (!XShapeQueryExtension (the_display->xdisplay,
                               &the_display->shape_event_base,
                               &the_display->shape_error_base))
570
      {
571 572
        the_display->shape_error_base = 0;
        the_display->shape_event_base = 0;
573
      }
574
    else
575
      the_display->have_shape = TRUE;
576

577
    meta_verbose ("Attempted to init Shape, found error base %d event base %d\n",
578 579
                  the_display->shape_error_base,
                  the_display->shape_event_base);
580
  }
Havoc Pennington's avatar
Havoc Pennington committed
581

582
  {
583
    the_display->have_render = FALSE;
584

585 586
    the_display->render_error_base = 0;
    the_display->render_event_base = 0;
587

588 589 590
    if (!XRenderQueryExtension (the_display->xdisplay,
                                &the_display->render_event_base,
                                &the_display->render_error_base))
591
      {
592 593
        the_display->render_error_base = 0;
        the_display->render_event_base = 0;
594 595
      }
    else
596
      the_display->have_render = TRUE;
597

598
    meta_verbose ("Attempted to init Render, found error base %d event base %d\n",
599 600
                  the_display->render_error_base,
                  the_display->render_event_base);
601
  }
602

Iain Holmes's avatar
Iain Holmes committed
603
  {
604 605 606
    int composite_major_version = 0;
    int composite_minor_version = 0;

607
    the_display->have_composite = FALSE;
Iain Holmes's avatar
Iain Holmes committed
608

609 610
    the_display->composite_error_base = 0;
    the_display->composite_event_base = 0;
Iain Holmes's avatar
Iain Holmes committed
611

612 613 614
    if (!XCompositeQueryExtension (the_display->xdisplay,
                                   &the_display->composite_event_base,
                                   &the_display->composite_error_base))
Iain Holmes's avatar
Iain Holmes committed
615
      {
616 617
        the_display->composite_error_base = 0;
        the_display->composite_event_base = 0;
618
      }
Iain Holmes's avatar
Iain Holmes committed
619
    else
620
      {
621
        if (XCompositeQueryVersion (the_display->xdisplay,
622 623
                                    &composite_major_version,
                                    &composite_minor_version))
624
          {
625
            the_display->have_composite = TRUE;
626 627 628
          }
        else
          {
629 630
            composite_major_version = 0;
            composite_minor_version = 0;
631 632
          }
      }
Iain Holmes's avatar
Iain Holmes committed
633

634 635
    meta_verbose ("Attempted to init Composite, found error base %d event base %d "
                  "extn ver %d %d\n",
636
                  the_display->composite_error_base,
637
                  the_display->composite_event_base,
638 639
                  composite_major_version,
                  composite_minor_version);
Iain Holmes's avatar
Iain Holmes committed
640

641
    the_display->have_damage = FALSE;
Iain Holmes's avatar
Iain Holmes committed
642

643 644
    the_display->damage_error_base = 0;
    the_display->damage_event_base = 0;
Iain Holmes's avatar
Iain Holmes committed
645

646 647 648
    if (!XDamageQueryExtension (the_display->xdisplay,
                                &the_display->damage_event_base,
                                &the_display->damage_error_base))
Iain Holmes's avatar
Iain Holmes committed
649
      {
650 651
        the_display->damage_error_base = 0;
        the_display->damage_event_base = 0;
652
      }
Iain Holmes's avatar
Iain Holmes committed
653
    else
654
      the_display->have_damage = TRUE;
Iain Holmes's avatar
Iain Holmes committed
655 656

    meta_verbose ("Attempted to init Damage, found error base %d event base %d\n",
657
                  the_display->damage_error_base,
658
                  the_display->damage_event_base);
Iain Holmes's avatar
Iain Holmes committed
659

660
    the_display->have_xfixes = FALSE;
Iain Holmes's avatar
Iain Holmes committed
661

662 663
    the_display->xfixes_error_base = 0;
    the_display->xfixes_event_base = 0;
Iain Holmes's avatar
Iain Holmes committed
664

665 666 667
    if (!XFixesQueryExtension (the_display->xdisplay,
                               &the_display->xfixes_event_base,
                               &the_display->xfixes_error_base))
Iain Holmes's avatar
Iain Holmes committed
668
      {
669 670
        the_display->xfixes_error_base = 0;
        the_display->xfixes_event_base = 0;
671
      }
Iain Holmes's avatar
Iain Holmes committed
672
    else
673
      the_display->have_xfixes = TRUE;
Iain Holmes's avatar
Iain Holmes committed
674 675

    meta_verbose ("Attempted to init XFixes, found error base %d event base %d\n",
676
                  the_display->xfixes_error_base,
677
                  the_display->xfixes_event_base);
Iain Holmes's avatar
Iain Holmes committed
678
  }
679

680 681
#ifdef HAVE_XCURSOR
  {
682 683
    XcursorSetTheme (the_display->xdisplay, meta_prefs_get_cursor_theme ());
    XcursorSetDefaultSize (the_display->xdisplay, meta_prefs_get_cursor_size ());
684 685 686 687 688
  }
#else /* HAVE_XCURSOR */
  meta_verbose ("Not compiled with Xcursor support\n");
#endif /* !HAVE_XCURSOR */

689 690 691 692 693 694 695 696
  /* Create the leader window here. Set its properties and
   * use the timestamp from one of the PropertyNotify events
   * that will follow.
   */
  {
    gulong data[1];
    XEvent event;

697 698 699 700 701
    /* We only care about the PropertyChangeMask in the next 30 or so lines of
     * code.  Note that gdk will at some point unset the PropertyChangeMask for
     * this window, so we can't rely on it still being set later.  See bug
     * 354213 for details.
     */
702 703 704
    the_display->leader_window =
      meta_create_offscreen_window (the_display->xdisplay,
                                    DefaultRootWindow (the_display->xdisplay),
705
                                    PropertyChangeMask);
706

707 708
    meta_prop_set_utf8_string_hint (the_display,
                                    the_display->leader_window,
709
                                    the_display->atom__NET_WM_NAME,
710
                                    "Metacity");
711

712 713
    meta_prop_set_utf8_string_hint (the_display,
                                    the_display->leader_window,
714
                                    the_display->atom__METACITY_VERSION,
715
                                    VERSION);
716

717 718 719
    data[0] = the_display->leader_window;
    XChangeProperty (the_display->xdisplay,
                     the_display->leader_window,
720
                     the_display->atom__NET_SUPPORTING_WM_CHECK,
721 722 723
                     XA_WINDOW,
                     32, PropModeReplace, (guchar*) data, 1);

724 725
    XWindowEvent (the_display->xdisplay,
                  the_display->leader_window,
726 727 728 729
                  PropertyChangeMask,
                  &event);

    timestamp = event.xproperty.time;
730 731 732 733

    /* Make it painfully clear that we can't rely on PropertyNotify events on
     * this window, as per bug 354213.
     */
734 735
    XSelectInput(the_display->xdisplay,
                 the_display->leader_window,
736
                 NoEventMask);
737 738
  }

739 740 741
  /* Make a little window used only for pinging the server for timestamps; note
   * that meta_create_offscreen_window already selects for PropertyChangeMask.
   */
742 743 744
  the_display->timestamp_pinging_window =
    meta_create_offscreen_window (the_display->xdisplay,
                                  DefaultRootWindow (the_display->xdisplay),
745
                                  PropertyChangeMask);
746

747 748 749
  the_display->last_focus_time = timestamp;
  the_display->last_user_time = timestamp;
  the_display->compositor = NULL;
750

751
  i = XDefaultScreen (the_display->xdisplay);
752
  screen = meta_screen_new (the_display, i, timestamp);
753
  the_display->screen = screen;
754

755
  if (screen == NULL)
756
    {
757 758
      /* This would typically happen because the screen already
       * have window manager.
759
       */
760
      meta_display_close (the_display, timestamp);
761 762
      return FALSE;
    }
Iain Holmes's avatar
Iain Holmes committed
763

764 765 766 767 768
  old_active_xwindow = None;
  meta_prop_get_window (the_display, the_display->screen->xroot,
                        the_display->atom__NET_ACTIVE_WINDOW,
                        &old_active_xwindow);

769 770
  /* We don't composite the windows here because they will be composited
     faster with the call to meta_screen_manage_all_windows further down
Iain Holmes's avatar
Iain Holmes committed
771
     the code */
772
  update_compositor (the_display, FALSE);
773

rhp's avatar
rhp committed
774
  /* Now manage all existing windows */
775
  meta_screen_manage_all_windows (the_display->screen);
776

777 778 779
  if (old_active_xwindow != None)
    {
      MetaWindow *old_active_window;
780

781 782
      old_active_window = meta_display_lookup_x_window (the_display,
                                                        old_active_xwindow);
783

784 785 786 787 788 789
      if (old_active_window)
        {
          meta_window_focus (old_active_window, timestamp);
        }
      else
        {
790
          meta_display_focus_the_no_focus_window (the_display,
791
                                                  the_display->screen,
792
                                                  timestamp);
793 794 795 796 797 798 799 800
        }
    }
  else
    {
      meta_display_focus_the_no_focus_window (the_display,
                                              the_display->screen,
                                              timestamp);
    }
801

802
  /* Done opening new display */
803
  the_display->display_opening = FALSE;
804

rhp's avatar
rhp committed
805 806 807
  return TRUE;
}

rhp's avatar
rhp committed
808 809 810 811 812 813 814 815 816
static gint
ptrcmp (gconstpointer a, gconstpointer b)
{
  if (a < b)
    return -1;
  else if (a > b)
    return 1;
  else
    return 0;
rhp's avatar
rhp committed
817 818
}

rhp's avatar
rhp committed
819
GSList*
820 821
meta_display_list_windows (MetaDisplay          *display,
                           MetaListWindowsFlags  flags)
rhp's avatar
rhp committed
822
{
rhp's avatar
rhp committed
823 824
  GSList *winlist;
  GSList *tmp;
rhp's avatar
rhp committed
825
  GSList *prev;
826 827
  GHashTableIter iter;
  gpointer key, value;
828

rhp's avatar
rhp committed
829
  winlist = NULL;
830 831 832 833 834 835 836 837 838
  g_hash_table_iter_init (&iter, display->window_ids);
  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      MetaWindow *window = value;

      if (!window->override_redirect ||
         (flags & META_LIST_INCLUDE_OVERRIDE_REDIRECT) != 0)
        winlist = g_slist_prepend (winlist, window);
    }
rhp's avatar
rhp committed
839

rhp's avatar
rhp committed
840 841 842
  /* Uniquify the list, since both frame windows and plain
   * windows are in the hash
   */
rhp's avatar
rhp committed
843 844
  winlist = g_slist_sort (winlist, ptrcmp);

rhp's avatar
rhp committed
845 846 847 848 849 850 851
  prev = NULL;
  tmp = winlist;
  while (tmp != NULL)
    {
      GSList *next;

      next = tmp->next;
852

rhp's avatar
rhp committed
853 854 855 856 857 858 859 860 861 862
      if (next &&
          next->data == tmp->data)
        {
          /* Delete tmp from list */

          if (prev)
            prev->next = next;

          if (tmp == winlist)
            winlist = next;
863

rhp's avatar
rhp committed
864 865 866 867 868 869 870 871
          g_slist_free_1 (tmp);

          /* leave prev unchanged */
        }
      else
        {
          prev = tmp;
        }
872

rhp's avatar
rhp committed
873 874 875 876 877 878
      tmp = next;
    }

  return winlist;
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892
static void
meta_display_unmanage_windows (MetaDisplay *display,
                               guint32      timestamp)
{
  GSList *tmp;
  GSList *winlist;

  winlist = meta_display_list_windows (display, META_LIST_INCLUDE_OVERRIDE_REDIRECT);
  winlist = g_slist_sort (winlist, meta_display_stack_cmp);

  /* Unmanage all windows */
  tmp = winlist;
  while (tmp != NULL)
    {
893
      meta_window_unmanage (tmp->data, timestamp);
894 895 896 897 898 899

      tmp = tmp->next;
    }
  g_slist_free (winlist);
}

rhp's avatar
rhp committed
900
void
901 902
meta_display_close (MetaDisplay *display,
                    guint32      timestamp)
rhp's avatar
rhp committed
903
{
904 905
  g_assert (display != NULL);

906
  if (display->closing != 0)
907
    {
908
      /* The display's already been closed. */
909 910 911
      return;
    }

912
  if (display->error_traps > 0)
913
    g_error ("Display closed with error traps pending");
rhp's avatar
rhp committed
914

915
  display->closing += 1;
916 917

  meta_prefs_remove_listener (prefs_changed_callback, display);
918

919
  meta_display_remove_autoraise_callback (display);
920 921 922

  if (display->grab_old_window_stacking)
    g_list_free (display->grab_old_window_stacking);
923

rhp's avatar
rhp committed
924 925 926 927
  /* Stop caring about events */
  meta_ui_remove_event_func (display->xdisplay,
                             event_callback,
                             display);
928

929 930
  meta_display_unmanage_windows (display, timestamp);

931 932
  g_clear_object (&display->compositor);

933
  if (display->screen != NULL)
934
    {
935 936
      meta_screen_free (display->screen, timestamp);
      display->screen = NULL;
937 938
    }

939 940 941 942 943 944 945
#ifdef HAVE_STARTUP_NOTIFICATION
  if (display->sn_display)
    {
      sn_display_unref (display->sn_display);
      display->sn_display = NULL;
    }
#endif
946

947
  /* Must be after all calls to meta_window_unmanage() since they
rhp's avatar
rhp committed
948 949 950
   * unregister windows
   */
  g_hash_table_destroy (display->window_ids);
rhp's avatar
rhp committed
951

rhp's avatar
rhp committed
952 953
  if (display->leader_window != None)
    XDestroyWindow (display->xdisplay, display->leader_window);
rhp's avatar
rhp committed
954

955
  XFlush (display->xdisplay);
956 957

  meta_display_free_window_prop_hooks (display);
958
  meta_display_free_group_prop_hooks (display);
959

rhp's avatar
rhp committed
960 961
  g_free (display->name);

962
  meta_display_shutdown_keys (display);
Havoc Pennington's avatar
Havoc Pennington committed
963

rhp's avatar
rhp committed
964
  g_free (display);
965
  the_display = NULL;
rhp's avatar
rhp committed
966

967
  meta_quit ();
rhp's avatar
rhp committed
968 969
}

rhp's avatar
rhp committed
970 971 972 973 974 975 976 977 978
/* Grab/ungrab routines taken from fvwm */
void
meta_display_grab (MetaDisplay *display)
{
  if (display->server_grab_count == 0)
    {
      XGrabServer (display->xdisplay);
    }
  display->server_grab_count += 1;
rhp's avatar
rhp committed
979 980
  meta_verbose ("Grabbing display, grab count now %d\n",
                display->server_grab_count);
rhp's avatar
rhp committed
981 982 983 984 985 986
}

void
meta_display_ungrab (MetaDisplay *display)
{
  if (display->server_grab_count == 0)
987
    g_error ("Ungrabbed non-grabbed server");
988

rhp's avatar
rhp committed
989 990 991
  display->server_grab_count -= 1;
  if (display->server_grab_count == 0)
    {
rhp's avatar
rhp committed
992 993 994
      /* FIXME we want to purge all pending "queued" stuff
       * at this point, such as window hide/show
       */
rhp's avatar
rhp committed
995
      XUngrabServer (display->xdisplay);
996
      XFlush (display->xdisplay);
rhp's avatar
rhp committed
997
    }
rhp's avatar
rhp committed
998 999 1000

  meta_verbose ("Ungrabbing display, grab count now %d\n",
                display->server_grab_count);
rhp's avatar
rhp committed
1001 1002
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
/**
 * Returns the singleton MetaDisplay if "xdisplay" matches the X display it's
 * managing; otherwise gives a warning and returns NULL.  When we were claiming
 * to be able to manage multiple displays, this was supposed to find the
 * display out of the list which matched that display.  Now it's merely an
 * extra sanity check.
 *
 * \param xdisplay  An X display
 * \return  The singleton X display, or NULL if "xdisplay" isn't the one
 *          we're managing.
 */
rhp's avatar
rhp committed
1014 1015 1016
MetaDisplay*
meta_display_for_x_display (Display *xdisplay)
{
1017 1018
  if (the_display->xdisplay == xdisplay)
    return the_display;
rhp's avatar
rhp committed
1019

1020 1021
  g_warning ("Could not find display for X display %p, probably going to crash",
             xdisplay);
1022

rhp's avatar
rhp committed
1023 1024 1025
  return NULL;
}

1026 1027 1028 1029 1030 1031 1032 1033
/**
 * Accessor for the singleton MetaDisplay.
 *
 * \return  The only MetaDisplay there is.  This can be NULL, but only
 *          during startup.
 */
MetaDisplay*
meta_get_display (void)
rhp's avatar
rhp committed
1034
{
1035
  return the_display;
rhp's avatar
rhp committed
1036 1037
}

rhp's avatar
rhp committed
1038 1039
static gboolean dump_events = TRUE;

1040 1041 1042 1043 1044 1045 1046
static gboolean
grab_op_is_mouse_only (MetaGrabOp op)
{
  switch (op)
    {
    case META_GRAB_OP_MOVING:
    case META_GRAB_OP_RESIZING_SE:
1047 1048
    case META_GRAB_OP_RESIZING_S:
    case META_GRAB_OP_RESIZING_SW:
1049 1050 1051 1052 1053 1054 1055
    case META_GRAB_OP_RESIZING_N:
    case META_GRAB_OP_RESIZING_NE:
    case META_GRAB_OP_RESIZING_NW:
    case META_GRAB_OP_RESIZING_W:
    case META_GRAB_OP_RESIZING_E:
      return TRUE;

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    case META_GRAB_OP_NONE:
    case META_GRAB_OP_KEYBOARD_MOVING:
    case META_GRAB_OP_KEYBOARD_RESIZING_UNKNOWN:
    case META_GRAB_OP_KEYBOARD_RESIZING_S:
    case META_GRAB_OP_KEYBOARD_RESIZING_N:
    case META_GRAB_OP_KEYBOARD_RESIZING_W:
    case META_GRAB_OP_KEYBOARD_RESIZING_E:
    case META_GRAB_OP_KEYBOARD_RESIZING_SE:
    case META_GRAB_OP_KEYBOARD_RESIZING_NE:
    case META_GRAB_OP_KEYBOARD_RESIZING_SW:
    case META_GRAB_OP_KEYBOARD_RESIZING_NW:
    case META_GRAB_OP_KEYBOARD_TABBING_NORMAL:
    case META_GRAB_OP_KEYBOARD_TABBING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_NORMAL:
    case META_GRAB_OP_KEYBOARD_ESCAPING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_GROUP:
    case META_GRAB_OP_KEYBOARD_TABBING_GROUP:
    case META_GRAB_OP_KEYBOARD_WORKSPACE_SWITCHING:
    case META_GRAB_OP_CLICKING_MINIMIZE:
    case META_GRAB_OP_CLICKING_MAXIMIZE:
    case META_GRAB_OP_CLICKING_UNMAXIMIZE:
    case META_GRAB_OP_CLICKING_DELETE:
    case META_GRAB_OP_CLICKING_MENU:
    case META_GRAB_OP_CLICKING_APPMENU:
    case META_GRAB_OP_CLICKING_SHADE:
    case META_GRAB_OP_CLICKING_UNSHADE:
    case META_GRAB_OP_CLICKING_ABOVE:
    case META_GRAB_OP_CLICKING_UNABOVE:
    case META_GRAB_OP_CLICKING_STICK:
    case META_GRAB_OP_CLICKING_UNSTICK:
      return FALSE;

1088 1089 1090 1091 1092
    default:
      return FALSE;
    }
}

1093 1094
gboolean
meta_grab_op_is_mouse (MetaGrabOp op)
rhp's avatar
rhp committed
1095 1096 1097 1098 1099
{
  switch (op)
    {
    case META_GRAB_OP_MOVING:
    case META_GRAB_OP_RESIZING_SE:
1100 1101
    case META_GRAB_OP_RESIZING_S:
    case META_GRAB_OP_RESIZING_SW:
rhp's avatar
rhp committed
1102 1103 1104 1105 1106
    case META_GRAB_OP_RESIZING_N:
    case META_GRAB_OP_RESIZING_NE:
    case META_GRAB_OP_RESIZING_NW:
    case META_GRAB_OP_RESIZING_W:
    case META_GRAB_OP_RESIZING_E:
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    case META_GRAB_OP_KEYBOARD_RESIZING_UNKNOWN:
    case META_GRAB_OP_KEYBOARD_RESIZING_S:
    case META_GRAB_OP_KEYBOARD_RESIZING_N:
    case META_GRAB_OP_KEYBOARD_RESIZING_W:
    case META_GRAB_OP_KEYBOARD_RESIZING_E:
    case META_GRAB_OP_KEYBOARD_RESIZING_SE:
    case META_GRAB_OP_KEYBOARD_RESIZING_NE:
    case META_GRAB_OP_KEYBOARD_RESIZING_SW:
    case META_GRAB_OP_KEYBOARD_RESIZING_NW:
    case META_GRAB_OP_KEYBOARD_MOVING:
rhp's avatar
rhp committed
1117 1118
      return TRUE;

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    case META_GRAB_OP_NONE:
    case META_GRAB_OP_KEYBOARD_TABBING_NORMAL:
    case META_GRAB_OP_KEYBOARD_TABBING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_NORMAL:
    case META_GRAB_OP_KEYBOARD_ESCAPING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_GROUP:
    case META_GRAB_OP_KEYBOARD_TABBING_GROUP:
    case META_GRAB_OP_KEYBOARD_WORKSPACE_SWITCHING:
    case META_GRAB_OP_CLICKING_MINIMIZE:
    case META_GRAB_OP_CLICKING_MAXIMIZE:
    case META_GRAB_OP_CLICKING_UNMAXIMIZE:
    case META_GRAB_OP_CLICKING_DELETE:
    case META_GRAB_OP_CLICKING_MENU:
    case META_GRAB_OP_CLICKING_APPMENU:
    case META_GRAB_OP_CLICKING_SHADE:
    case META_GRAB_OP_CLICKING_UNSHADE:
    case META_GRAB_OP_CLICKING_ABOVE:
    case META_GRAB_OP_CLICKING_UNABOVE:
    case META_GRAB_OP_CLICKING_STICK:
    case META_GRAB_OP_CLICKING_UNSTICK:
      return FALSE;

rhp's avatar
rhp committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    default:
      return FALSE;
    }
}

static gboolean
grab_op_is_keyboard (MetaGrabOp op)
{
  switch (op)
    {
    case META_GRAB_OP_KEYBOARD_MOVING:
    case META_GRAB_OP_KEYBOARD_RESIZING_UNKNOWN:
    case META_GRAB_OP_KEYBOARD_RESIZING_S:
    case META_GRAB_OP_KEYBOARD_RESIZING_N:
    case META_GRAB_OP_KEYBOARD_RESIZING_W:
    case META_GRAB_OP_KEYBOARD_RESIZING_E:
1157 1158 1159 1160
    case META_GRAB_OP_KEYBOARD_RESIZING_SE:
    case META_GRAB_OP_KEYBOARD_RESIZING_NE:
    case META_GRAB_OP_KEYBOARD_RESIZING_SW:
    case META_GRAB_OP_KEYBOARD_RESIZING_NW:
1161 1162
    case META_GRAB_OP_KEYBOARD_TABBING_NORMAL:
    case META_GRAB_OP_KEYBOARD_TABBING_DOCK:
1163
    case META_GRAB_OP_KEYBOARD_TABBING_GROUP:
1164 1165
    case META_GRAB_OP_KEYBOARD_ESCAPING_NORMAL:
    case META_GRAB_OP_KEYBOARD_ESCAPING_DOCK:
1166
    case META_GRAB_OP_KEYBOARD_ESCAPING_GROUP:
1167
    case META_GRAB_OP_KEYBOARD_WORKSPACE_SWITCHING:
rhp's avatar
rhp committed
1168 1169
      return TRUE;

1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
    case META_GRAB_OP_NONE:
    case META_GRAB_OP_MOVING:
    case META_GRAB_OP_RESIZING_SE:
    case META_GRAB_OP_RESIZING_S:
    case META_GRAB_OP_RESIZING_SW:
    case META_GRAB_OP_RESIZING_N:
    case META_GRAB_OP_RESIZING_NE:
    case META_GRAB_OP_RESIZING_NW:
    case META_GRAB_OP_RESIZING_W:
    case META_GRAB_OP_RESIZING_E:
    case META_GRAB_OP_CLICKING_MINIMIZE:
    case META_GRAB_OP_CLICKING_MAXIMIZE:
    case META_GRAB_OP_CLICKING_UNMAXIMIZE:
    case META_GRAB_OP_CLICKING_DELETE:
    case META_GRAB_OP_CLICKING_MENU:
    case META_GRAB_OP_CLICKING_APPMENU:
    case META_GRAB_OP_CLICKING_SHADE:
    case META_GRAB_OP_CLICKING_UNSHADE:
    case META_GRAB_OP_CLICKING_ABOVE:
    case META_GRAB_OP_CLICKING_UNABOVE:
    case META_GRAB_OP_CLICKING_STICK:
    case META_GRAB_OP_CLICKING_UNSTICK:
      return FALSE;

rhp's avatar
rhp committed
1194 1195 1196 1197 1198
    default:
      return FALSE;
    }
}

1199 1200 1201 1202 1203 1204
gboolean
meta_grab_op_is_resizing (MetaGrabOp op)
{
  switch (op)
    {
    case META_GRAB_OP_RESIZING_SE:
1205 1206
    case META_GRAB_OP_RESIZING_S:
    case META_GRAB_OP_RESIZING_SW:
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    case META_GRAB_OP_RESIZING_N:
    case META_GRAB_OP_RESIZING_NE:
    case META_GRAB_OP_RESIZING_NW:
    case META_GRAB_OP_RESIZING_W:
    case META_GRAB_OP_RESIZING_E:
    case META_GRAB_OP_KEYBOARD_RESIZING_UNKNOWN:
    case META_GRAB_OP_KEYBOARD_RESIZING_S:
    case META_GRAB_OP_KEYBOARD_RESIZING_N:
    case META_GRAB_OP_KEYBOARD_RESIZING_W:
    case META_GRAB_OP_KEYBOARD_RESIZING_E:
    case META_GRAB_OP_KEYBOARD_RESIZING_SE:
    case META_GRAB_OP_KEYBOARD_RESIZING_NE:
    case META_GRAB_OP_KEYBOARD_RESIZING_SW:
    case META_GRAB_OP_KEYBOARD_RESIZING_NW:
      return TRUE;

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    case META_GRAB_OP_NONE:
    case META_GRAB_OP_MOVING:
    case META_GRAB_OP_KEYBOARD_MOVING:
    case META_GRAB_OP_KEYBOARD_TABBING_NORMAL:
    case META_GRAB_OP_KEYBOARD_TABBING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_NORMAL:
    case META_GRAB_OP_KEYBOARD_ESCAPING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_GROUP:
    case META_GRAB_OP_KEYBOARD_TABBING_GROUP:
    case META_GRAB_OP_KEYBOARD_WORKSPACE_SWITCHING:
    case META_GRAB_OP_CLICKING_MINIMIZE:
    case META_GRAB_OP_CLICKING_MAXIMIZE:
    case META_GRAB_OP_CLICKING_UNMAXIMIZE:
    case META_GRAB_OP_CLICKING_DELETE:
    case META_GRAB_OP_CLICKING_MENU:
    case META_GRAB_OP_CLICKING_APPMENU:
    case META_GRAB_OP_CLICKING_SHADE:
    case META_GRAB_OP_CLICKING_UNSHADE:
    case META_GRAB_OP_CLICKING_ABOVE:
    case META_GRAB_OP_CLICKING_UNABOVE:
    case META_GRAB_OP_CLICKING_STICK:
    case META_GRAB_OP_CLICKING_UNSTICK:
      return FALSE;

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    default:
      return FALSE;
    }
}

gboolean
meta_grab_op_is_moving (MetaGrabOp op)
{
  switch (op)
    {
    case META_GRAB_OP_MOVING:
    case META_GRAB_OP_KEYBOARD_MOVING:
      return TRUE;
1260

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
    case META_GRAB_OP_NONE:
    case META_GRAB_OP_RESIZING_SE:
    case META_GRAB_OP_RESIZING_S:
    case META_GRAB_OP_RESIZING_SW:
    case META_GRAB_OP_RESIZING_N:
    case META_GRAB_OP_RESIZING_NE:
    case META_GRAB_OP_RESIZING_NW:
    case META_GRAB_OP_RESIZING_W:
    case META_GRAB_OP_RESIZING_E:
    case META_GRAB_OP_KEYBOARD_RESIZING_UNKNOWN:
    case META_GRAB_OP_KEYBOARD_RESIZING_S:
    case META_GRAB_OP_KEYBOARD_RESIZING_N:
    case META_GRAB_OP_KEYBOARD_RESIZING_W:
    case META_GRAB_OP_KEYBOARD_RESIZING_E:
    case META_GRAB_OP_KEYBOARD_RESIZING_SE:
    case META_GRAB_OP_KEYBOARD_RESIZING_NE:
    case META_GRAB_OP_KEYBOARD_RESIZING_SW:
    case META_GRAB_OP_KEYBOARD_RESIZING_NW:
    case META_GRAB_OP_KEYBOARD_TABBING_NORMAL:
    case META_GRAB_OP_KEYBOARD_TABBING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_NORMAL:
    case META_GRAB_OP_KEYBOARD_ESCAPING_DOCK:
    case META_GRAB_OP_KEYBOARD_ESCAPING_GROUP:
    case META_GRAB_OP_KEYBOARD_TABBING_GROUP:
    case META_GRAB_OP_KEYBOARD_WORKSPACE_SWITCHING:
    case META_GRAB_OP_CLICKING_MINIMIZE:
    case META_GRAB_OP_CLICKING_MAXIMIZE:
    case META_GRAB_OP_CLICKING_UNMAXIMIZE:
    case META_GRAB_OP_CLICKING_DELETE:
    case META_GRAB_OP_CLICKING_MENU:
    case META_GRAB_OP_CLICKING_APPMENU:
    case META_GRAB_OP_CLICKING_SHADE:
    case META_GRAB_OP_CLICKING_UNSHADE:
    case META_GRAB_OP_CLICKING_ABOVE:
    case META_GRAB_OP_CLICKING_UNABOVE:
    case META_GRAB_OP_CLICKING_STICK:
    case META_GRAB_OP_CLICKING_UNSTICK:
      return FALSE;

1300 1301 1302 1303 1304
    default:
      return FALSE;
    }
}

1305 1306 1307 1308 1309 1310 1311
/* Get time of current event, or CurrentTime if none. */
guint32
meta_display_get_current_time (MetaDisplay *display)
{
  return display->current_time;
}

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
static Bool
find_timestamp_predicate (Display  *xdisplay,
                          XEvent   *ev,
                          XPointer  arg)
{
  MetaDisplay *display = (MetaDisplay *) arg;

  return (ev->type == PropertyNotify &&
          ev->xproperty.atom == display->atom__METACITY_TIMESTAMP_PING);
}

1323 1324 1325 1326 1327
/* Get a timestamp, even if it means a roundtrip */
guint32
meta_display_get_current_time_roundtrip (MetaDisplay *display)
{
  guint32 timestamp;
1328

1329 1330 1331 1332 1333
  timestamp = meta_display_get_current_time (display);
  if (timestamp == CurrentTime)
    {
      XEvent property_event;

1334 1335 1336 1337 1338 1339 1340
      XChangeProperty (display->xdisplay, display->timestamp_pinging_window,
                       display->atom__METACITY_TIMESTAMP_PING,
                       XA_STRING, 8, PropModeAppend, NULL, 0);
      XIfEvent (display->xdisplay,
                &property_event,
                find_timestamp_predicate,
                (XPointer) display);
1341 1342 1343
      timestamp = property_event.xproperty.time;
    }

1344 1345
  sanity_check_timestamps (display, timestamp);

1346 1347 1348
  return timestamp;
}

1349 1350 1351 1352 1353 1354 1355 1356 1357
static void
add_ignored_serial (MetaDisplay  *display,
                    unsigned long serial)
{
  int i;

  /* don't add the same serial more than once */
  if (display->ignored_serials[N_IGNORED_SERIALS-1] == serial)
    return;
1358

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
  /* shift serials to the left */
  i = 0;
  while (i < (N_IGNORED_SERIALS - 1))
    {
      display->ignored_serials[i] = display->ignored_serials[i+1];
      ++i;
    }
  /* put new one on the end */
  display->ignored_serials[i] = serial;
}

static gboolean
serial_is_ignored (MetaDisplay  *display,
                   unsigned long serial)
{
  int i;

  i = 0;
  while (i < N_IGNORED_SERIALS)
    {
      if (display->ignored_serials[i] == serial)
        return TRUE;
      ++i;
    }
  return FALSE;
}

static void
reset_ignores (MetaDisplay *display)
{
  int i;

  i = 0;
  while (i < N_IGNORED_SERIALS)
    {
      display->ignored_serials[i] = 0;
      ++i;
    }

  display->ungrab_should_not_cause_focus_window = None;
}

1401
static gboolean
Havoc Pennington's avatar
Havoc Pennington committed
1402 1403 1404 1405 1406 1407 1408
window_raise_with_delay_callback (void *data)
{
  MetaWindow *window;
  MetaAutoRaiseData *auto_raise;

  auto_raise = data;

1409
  meta_topic (META_DEBUG_FOCUS,
1410 1411
              "In autoraise callback for window 0x%lx\n",
              auto_raise->xwindow);
Havoc Pennington's avatar
Havoc Pennington committed
1412 1413

  auto_raise->display->autoraise_timeout_id = 0;
1414
  auto_raise->display->autoraise_window = NULL;
Havoc Pennington's avatar
Havoc Pennington committed
1415

1416
  window  = meta_display_lookup_x_window (auto_raise->display,
1417
                                          auto_raise->xwindow);
1418 1419

  if (window == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
1420 1421 1422 1423
    return FALSE;

  /* If we aren't already on top, check whether the pointer is inside
   * the window and raise the window if so.
1424 1425
   */
  if (meta_stack_get_top (window->screen->stack) != window)
Havoc Pennington's avatar
Havoc Pennington committed
1426 1427 1428 1429 1430
    {
      int x, y, root_x, root_y;
      Window root, child;
      unsigned int mask;
      gboolean same_screen;
1431
      gboolean point_in_window;
Havoc Pennington's avatar
Havoc Pennington committed
1432 1433 1434

      meta_error_trap_push (window->display);
      same_screen = XQueryPointer (window->display->xdisplay,
1435 1436 1437
                                   window->