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

3
/*
4 5
 * SECTION:window-props
 * @short_description: #MetaWindow property handling
Thomas Thurman's avatar
Thomas Thurman committed
6 7 8 9 10 11 12
 *
 * A system which can inspect sets of properties of given windows
 * and take appropriate action given their values.
 *
 * Note that all the meta_window_reload_propert* functions require a
 * round trip to the server.
 *
13 14 15
 * The guts of this system are in meta_display_init_window_prop_hooks().
 * Reading this function will give you insight into how this all fits
 * together.
Thomas Thurman's avatar
Thomas Thurman committed
16
 */
17

18
/*
19
 * Copyright (C) 2001, 2002, 2003 Red Hat, Inc.
20
 * Copyright (C) 2004, 2005 Elijah Newren
21
 * Copyright (C) 2009 Thomas Thurman
22
 *
23 24 25 26 27 28 29 30 31
 * 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.
32
 *
33
 * You should have received a copy of the GNU General Public License
34
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
35 36
 */

37
#define _XOPEN_SOURCE 500 /* for gethostname() */
38

39 40 41 42
#include "config.h"

#include "x11/window-props.h"

43
#include <X11/Xatom.h>
44 45
#include <unistd.h>
#include <string.h>
46 47 48 49 50 51 52 53 54 55

#include "core/frame.h"
#include "core/meta-workspace-manager-private.h"
#include "core/util-private.h"
#include "meta/group.h"
#include "meta/meta-x11-errors.h"
#include "x11/meta-x11-display-private.h"
#include "x11/window-x11-private.h"
#include "x11/window-x11.h"
#include "x11/xprops.h"
56

57 58 59 60 61
#ifndef HOST_NAME_MAX
/* Solaris headers apparently don't define this so do so manually; #326745 */
#define HOST_NAME_MAX 255
#endif

62
typedef void (* ReloadValueFunc) (MetaWindow    *window,
63 64
                                  MetaPropValue *value,
                                  gboolean       initial);
65

66 67 68 69
typedef enum {
  NONE       = 0,
  LOAD_INIT  = (1 << 0),
  INCLUDE_OR = (1 << 1),
70
  INIT_ONLY  = (1 << 2),
71
  FORCE_INIT = (1 << 3),
72 73
} MetaPropHookFlags;

74
struct _MetaWindowPropHooks
75 76
{
  Atom property;
77
  MetaPropValueType type;
78
  ReloadValueFunc reload_func;
79
  MetaPropHookFlags flags;
80
};
81

82
static void init_prop_value            (MetaWindow          *window,
83 84 85 86 87 88
                                        MetaWindowPropHooks *hooks,
                                        MetaPropValue       *value);
static void reload_prop_value          (MetaWindow          *window,
                                        MetaWindowPropHooks *hooks,
                                        MetaPropValue       *value,
                                        gboolean             initial);
89 90
static MetaWindowPropHooks *find_hooks (MetaX11Display *x11_display,
                                        Atom            property);
91 92 93


void
94 95 96 97
meta_window_reload_property_from_xwindow (MetaWindow      *window,
                                          Window           xwindow,
                                          Atom             property,
                                          gboolean         initial)
98
{
99 100
  MetaPropValue value = { 0, };
  MetaWindowPropHooks *hooks;
101

102
  hooks = find_hooks (window->display->x11_display, property);
103 104
  if (!hooks)
    return;
105

106 107 108
  if ((hooks->flags & INIT_ONLY) && !initial)
    return;

109
  init_prop_value (window, hooks, &value);
110

111
  meta_prop_get_values (window->display->x11_display, xwindow,
112
                        &value, 1);
113

114 115 116 117 118
  reload_prop_value (window, hooks, &value,
                     initial);

  meta_prop_free_values (&value, 1);
}
119

120 121 122 123 124 125 126 127 128
static void
meta_window_reload_property (MetaWindow      *window,
                             Atom             property,
                             gboolean         initial)
{
  meta_window_reload_property_from_xwindow (window,
                                            window->xwindow,
                                            property,
                                            initial);
129 130
}

131 132 133 134 135 136
void
meta_window_load_initial_properties (MetaWindow *window)
{
  int i, j;
  MetaPropValue *values;
  int n_properties = 0;
137
  MetaX11Display *x11_display = window->display->x11_display;
138

139
  values = g_new0 (MetaPropValue, x11_display->n_prop_hooks);
140 141

  j = 0;
142
  for (i = 0; i < x11_display->n_prop_hooks; i++)
143
    {
144
      MetaWindowPropHooks *hooks = &x11_display->prop_hooks_table[i];
145
      if (hooks->flags & LOAD_INIT)
146 147 148 149 150 151 152
        {
          init_prop_value (window, hooks, &values[j]);
          ++j;
        }
    }
  n_properties = j;

153
  meta_prop_get_values (window->display->x11_display, window->xwindow,
154 155 156
                        values, n_properties);

  j = 0;
157
  for (i = 0; i < x11_display->n_prop_hooks; i++)
158
    {
159
      MetaWindowPropHooks *hooks = &x11_display->prop_hooks_table[i];
160
      if (hooks->flags & LOAD_INIT)
161 162 163 164 165
        {
          /* If we didn't actually manage to load anything then we don't need
           * to call the reload function; this is different from a notification
           * where disappearance of a previously present value is significant.
           */
166 167
          if (values[j].type != META_PROP_VALUE_INVALID ||
              hooks->flags & FORCE_INIT)
168 169 170 171 172 173 174 175 176 177
            reload_prop_value (window, hooks, &values[j], TRUE);
          ++j;
        }
    }

  meta_prop_free_values (values, n_properties);

  g_free (values);
}

178 179
/* Fill in the MetaPropValue used to get the value of "property" */
static void
180
init_prop_value (MetaWindow          *window,
181 182
                 MetaWindowPropHooks *hooks,
                 MetaPropValue       *value)
183
{
184
  if (!hooks || hooks->type == META_PROP_VALUE_INVALID ||
185
      (window->override_redirect && !(hooks->flags & INCLUDE_OR)))
186
    {
187 188
      value->type = META_PROP_VALUE_INVALID;
      value->atom = None;
189 190 191 192
    }
  else
    {
      value->type = hooks->type;
193
      value->atom = hooks->property;
194
    }
195 196 197
}

static void
198 199 200 201
reload_prop_value (MetaWindow          *window,
                   MetaWindowPropHooks *hooks,
                   MetaPropValue       *value,
                   gboolean             initial)
202
{
203
  if (!(window->override_redirect && !(hooks->flags & INCLUDE_OR)))
204
    (* hooks->reload_func) (window, value, initial);
205 206 207 208
}

static void
reload_wm_client_machine (MetaWindow    *window,
209 210
                          MetaPropValue *value,
                          gboolean       initial)
211 212 213
{
  g_free (window->wm_client_machine);
  window->wm_client_machine = NULL;
214

215 216 217 218 219
  if (value->type != META_PROP_VALUE_INVALID)
    window->wm_client_machine = g_strdup (value->v.str);

  meta_verbose ("Window has client machine \"%s\"\n",
                window->wm_client_machine ? window->wm_client_machine : "unset");
220 221 222 223 224 225 226 227 228 229 230 231 232

  if (window->wm_client_machine == NULL)
    {
      window->is_remote = FALSE;
    }
  else
    {
      char hostname[HOST_NAME_MAX + 1] = "";

      gethostname (hostname, HOST_NAME_MAX + 1);

      window->is_remote = g_strcmp0 (window->wm_client_machine, hostname) != 0;
    }
233 234 235
}

static void
236 237 238 239 240 241 242 243 244 245 246 247 248
complain_about_broken_client (MetaWindow    *window,
                              MetaPropValue *value,
                              gboolean       initial)
{
  meta_warning ("Broken client! Window %s changed client leader window or SM client ID\n",
                window->desc);
}

static void
reload_net_wm_window_type (MetaWindow    *window,
                           MetaPropValue *value,
                           gboolean       initial)
{
249
  MetaX11Display *x11_display = window->display->x11_display;
250 251 252 253 254 255 256 257 258 259 260 261 262 263
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

  if (value->type != META_PROP_VALUE_INVALID)
    {
      int i;

      for (i = 0; i < value->v.atom_list.n_atoms; i++)
        {
          Atom atom = value->v.atom_list.atoms[i];

          /* We break as soon as we find one we recognize,
           * supposed to prefer those near the front of the list
           */
264 265 266 267 268 269 270 271 272 273 274 275 276 277
          if (atom == x11_display->atom__NET_WM_WINDOW_TYPE_DESKTOP ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_DOCK ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_TOOLBAR ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_MENU ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_UTILITY ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_SPLASH ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_DIALOG ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_DROPDOWN_MENU ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_POPUP_MENU ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_TOOLTIP ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_NOTIFICATION ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_COMBO ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_DND ||
              atom == x11_display->atom__NET_WM_WINDOW_TYPE_NORMAL)
278 279
            {
              priv->type_atom = atom;
280
              break;
281 282 283
            }
        }
    }
284 285

  meta_window_x11_recalc_window_type (window);
286 287 288 289 290 291
}

static void
reload_icon (MetaWindow    *window,
             Atom           atom)
{
292 293 294 295
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

  meta_icon_cache_property_changed (&priv->icon_cache,
296
                                    window->display->x11_display,
297 298 299 300 301 302 303 304 305
                                    atom);
  meta_window_queue(window, META_QUEUE_UPDATE_ICON);
}

static void
reload_net_wm_icon (MetaWindow    *window,
                    MetaPropValue *value,
                    gboolean       initial)
{
306
  reload_icon (window, window->display->x11_display->atom__NET_WM_ICON);
307 308 309 310 311 312 313
}

static void
reload_kwm_win_icon (MetaWindow    *window,
                     MetaPropValue *value,
                     gboolean       initial)
{
314
  reload_icon (window, window->display->x11_display->atom__KWM_WIN_ICON);
315 316
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330
static void
reload_icon_geometry (MetaWindow    *window,
                      MetaPropValue *value,
                      gboolean       initial)
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
      if (value->v.cardinal_list.n_cardinals != 4)
        {
          meta_verbose ("_NET_WM_ICON_GEOMETRY on %s has %d values instead of 4\n",
                        window->desc, value->v.cardinal_list.n_cardinals);
        }
      else
        {
331 332 333 334 335 336 337 338
          MetaRectangle geometry;

          geometry.x = (int)value->v.cardinal_list.cardinals[0];
          geometry.y = (int)value->v.cardinal_list.cardinals[1];
          geometry.width = (int)value->v.cardinal_list.cardinals[2];
          geometry.height = (int)value->v.cardinal_list.cardinals[3];

          meta_window_set_icon_geometry (window, &geometry);
339 340 341 342
        }
    }
  else
    {
343
      meta_window_set_icon_geometry (window, NULL);
344 345 346
    }
}

347 348
static void
meta_window_set_custom_frame_extents (MetaWindow *window,
349 350
                                      GtkBorder  *extents,
                                      gboolean    is_initial)
351 352 353
{
  if (extents)
    {
354 355
      if (window->has_custom_frame_extents &&
          memcmp (&window->custom_frame_extents, extents, sizeof (GtkBorder)) == 0)
356 357
        return;

358 359
      window->has_custom_frame_extents = TRUE;
      window->custom_frame_extents = *extents;
360 361 362 363 364 365 366 367 368 369 370

      /* If we're setting the frame extents on map, then this is telling
       * us to adjust our understanding of the frame rect to match what
       * GTK+ thinks it is. Future changes to the frame extents should
       * trigger a resize and send a ConfigureRequest to the application.
       */
      if (is_initial)
        {
          meta_window_client_rect_to_frame_rect (window, &window->rect, &window->rect);
          meta_window_client_rect_to_frame_rect (window, &window->unconstrained_rect, &window->unconstrained_rect);
        }
371 372 373
    }
  else
    {
374 375 376
      if (!window->has_custom_frame_extents)
        return;

377 378 379 380 381 382 383
      window->has_custom_frame_extents = FALSE;
      memset (&window->custom_frame_extents, 0, sizeof (window->custom_frame_extents));
    }

  meta_window_queue (window, META_QUEUE_MOVE_RESIZE);
}

384 385 386 387 388 389 390 391 392 393 394 395 396 397
static void
reload_gtk_frame_extents (MetaWindow    *window,
                          MetaPropValue *value,
                          gboolean       initial)
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
      if (value->v.cardinal_list.n_cardinals != 4)
        {
          meta_verbose ("_GTK_FRAME_EXTENTS on %s has %d values instead of 4\n",
                        window->desc, value->v.cardinal_list.n_cardinals);
        }
      else
        {
398 399 400 401 402
          GtkBorder extents;
          extents.left   = (int)value->v.cardinal_list.cardinals[0];
          extents.right  = (int)value->v.cardinal_list.cardinals[1];
          extents.top    = (int)value->v.cardinal_list.cardinals[2];
          extents.bottom = (int)value->v.cardinal_list.cardinals[3];
403
          meta_window_set_custom_frame_extents (window, &extents, initial);
404 405 406 407
        }
    }
  else
    {
408
      meta_window_set_custom_frame_extents (window, NULL, initial);
409 410 411
    }
}

412 413 414 415 416 417 418 419 420 421 422 423
static void
reload_struts (MetaWindow    *window,
               MetaPropValue *value,
               gboolean       initial)
{
  meta_window_update_struts (window);
}

static void
reload_wm_window_role (MetaWindow    *window,
                       MetaPropValue *value,
                       gboolean       initial)
424
{
425 426 427
  g_clear_pointer (&window->role, g_free);
  if (value->type != META_PROP_VALUE_INVALID)
    window->role = g_strdup (value->v.str);
428 429 430 431
}

static void
reload_net_wm_pid (MetaWindow    *window,
432 433
                   MetaPropValue *value,
                   gboolean       initial)
434 435 436
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
437
      uint32_t cardinal = (int) value->v.cardinal;
438

439
      if (cardinal <= 0)
440
        meta_warning ("Application set a bogus _NET_WM_PID %u\n",
441 442 443 444 445 446 447 448 449 450
                      cardinal);
      else
        {
          window->net_wm_pid = cardinal;
          meta_verbose ("Window has _NET_WM_PID %d\n",
                        window->net_wm_pid);
        }
    }
}

451 452
static void
reload_net_wm_user_time (MetaWindow    *window,
453 454
                         MetaPropValue *value,
                         gboolean       initial)
455 456 457
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
458
      uint32_t cardinal = value->v.cardinal;
459
      meta_window_set_user_time (window, cardinal);
460 461 462
    }
}

463 464
static void
reload_net_wm_user_time_window (MetaWindow    *window,
465 466
                                MetaPropValue *value,
                                gboolean       initial)
467 468 469
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
470 471
      MetaWindow *prev_owner;

472 473 474 475
      /* Unregister old NET_WM_USER_TIME_WINDOW */
      if (window->user_time_window != None)
        {
          /* See the comment to the meta_display_register_x_window call below. */
476 477
          meta_x11_display_unregister_x_window (window->display->x11_display,
                                                window->user_time_window);
478
          /* Don't get events on not-managed windows */
479
          XSelectInput (window->display->x11_display->xdisplay,
480 481 482 483
                        window->user_time_window,
                        NoEventMask);
        }

484 485 486
      /* Ensure the new user time window is not used on another MetaWindow,
       * and unset its user time window if that is the case.
       */
487 488
      prev_owner = meta_x11_display_lookup_x_window (window->display->x11_display,
                                                     value->v.xwindow);
489 490
      if (prev_owner && prev_owner->user_time_window == value->v.xwindow)
        {
491 492
          meta_x11_display_unregister_x_window (window->display->x11_display,
                                               value->v.xwindow);
493 494
          prev_owner->user_time_window = None;
        }
495 496 497 498 499 500 501 502 503 504 505 506 507 508

      /* Obtain the new NET_WM_USER_TIME_WINDOW and register it */
      window->user_time_window = value->v.xwindow;
      if (window->user_time_window != None)
        {
          /* Kind of a hack; display.c:event_callback() ignores events
           * for unknown windows.  We make window->user_time_window
           * known by registering it with window (despite the fact
           * that window->xwindow is already registered with window).
           * This basically means that property notifies to either the
           * window->user_time_window or window->xwindow will be
           * treated identically and will result in functions for
           * window being called to update it.  Maybe we should ignore
           * any property notifies to window->user_time_window other
509
           * than atom__NET_WM_USER_TIME ones, but I just don't care
510 511
           * and it's not specified in the spec anyway.
           */
512 513 514
          meta_x11_display_register_x_window (window->display->x11_display,
                                              &window->user_time_window,
                                              window);
515
          /* Just listen for property notify events */
516
          XSelectInput (window->display->x11_display->xdisplay,
517 518 519 520 521 522 523 524 525
                        window->user_time_window,
                        PropertyChangeMask);

          /* Manually load the _NET_WM_USER_TIME field from the given window
           * at this time as well.  If the user_time_window ever broadens in
           * scope, we'll probably want to load all relevant properties here.
           */
          meta_window_reload_property_from_xwindow (
            window,
526
            window->user_time_window,
527
            window->display->x11_display->atom__NET_WM_USER_TIME,
528
            initial);
529 530 531 532
        }
    }
}

533 534
#define MAX_TITLE_LENGTH 512

535
/**
536 537
 * set_title_text:
 *
538 539
 * Called by set_window_title() to set the value of @target to @title.
 * If required and @atom is set, it will update the appropriate property.
540
 *
541
 * Returns: %TRUE if a new title was set.
542 543
 */
static gboolean
544 545 546 547 548
set_title_text (MetaWindow  *window,
                gboolean     previous_was_modified,
                const char  *title,
                Atom         atom,
                char       **target)
549
{
550
  gboolean modified = FALSE;
551

552 553
  if (!target)
    return FALSE;
554

555
  g_free (*target);
556

557 558 559
  if (!title)
    *target = g_strdup ("");
  else if (g_utf8_strlen (title, MAX_TITLE_LENGTH + 1) > MAX_TITLE_LENGTH)
560
    {
561 562 563 564 565
      *target = meta_g_utf8_strndup (title, MAX_TITLE_LENGTH);
      modified = TRUE;
    }
  /* if WM_CLIENT_MACHINE indicates this machine is on a remote host
   * lets place that hostname in the title */
566
  else if (meta_window_is_remote (window))
567
    {
568
      *target = g_strdup_printf (_("%s (on %s)"),
569 570
                      title, window->wm_client_machine);
      modified = TRUE;
571
    }
572 573 574 575
  else
    *target = g_strdup (title);

  if (modified && atom != None)
576
    meta_prop_set_utf8_string_hint (window->display->x11_display,
577 578 579 580 581
                                    window->xwindow,
                                    atom, *target);

  /* Bug 330671 -- Don't forget to clear _NET_WM_VISIBLE_(ICON_)NAME */
  if (!modified && previous_was_modified)
582
    {
583
      meta_x11_error_trap_push (window->display->x11_display);
584
      XDeleteProperty (window->display->x11_display->xdisplay,
585 586
                       window->xwindow,
                       atom);
587
      meta_x11_error_trap_pop (window->display->x11_display);
588
    }
589 590 591 592 593 594 595 596

  return modified;
}

static void
set_window_title (MetaWindow *window,
                  const char *title)
{
597 598 599
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

600
  char *new_title = NULL;
601

602 603
  gboolean modified =
    set_title_text (window,
604
                    priv->using_net_wm_visible_name,
605
                    title,
606
                    window->display->x11_display->atom__NET_WM_VISIBLE_NAME,
607
                    &new_title);
608
  priv->using_net_wm_visible_name = modified;
609

610 611 612
  meta_window_set_title (window, new_title);

  g_free (new_title);
613 614 615 616
}

static void
reload_net_wm_name (MetaWindow    *window,
617 618
                    MetaPropValue *value,
                    gboolean       initial)
619
{
620 621 622
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

623 624 625
  if (value->type != META_PROP_VALUE_INVALID)
    {
      set_window_title (window, value->v.str);
626
      priv->using_net_wm_name = TRUE;
627 628 629 630 631 632 633

      meta_verbose ("Using _NET_WM_NAME for new title of %s: \"%s\"\n",
                    window->desc, window->title);
    }
  else
    {
      set_window_title (window, NULL);
634
      priv->using_net_wm_name = FALSE;
635 636
      if (!initial)
        meta_window_reload_property (window, XA_WM_NAME, FALSE);
637 638 639 640 641
    }
}

static void
reload_wm_name (MetaWindow    *window,
642 643
                MetaPropValue *value,
                gboolean       initial)
644
{
645 646 647 648
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

  if (priv->using_net_wm_name)
649 650 651 652 653
    {
      meta_verbose ("Ignoring WM_NAME \"%s\" as _NET_WM_NAME is set\n",
                    value->v.str);
      return;
    }
654

655 656
  if (value->type != META_PROP_VALUE_INVALID)
    {
657
      set_window_title (window, value->v.str);
658 659 660 661 662 663 664 665 666 667

      meta_verbose ("Using WM_NAME for new title of %s: \"%s\"\n",
                    window->desc, window->title);
    }
  else
    {
      set_window_title (window, NULL);
    }
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
static void
meta_window_set_opaque_region (MetaWindow     *window,
                               cairo_region_t *region)
{
  if (cairo_region_equal (window->opaque_region, region))
    return;

  g_clear_pointer (&window->opaque_region, cairo_region_destroy);

  if (region != NULL)
    window->opaque_region = cairo_region_reference (region);

  meta_compositor_window_shape_changed (window->display->compositor, window);
}

683 684 685 686 687
static void
reload_opaque_region (MetaWindow    *window,
                      MetaPropValue *value,
                      gboolean       initial)
{
688 689 690 691
  cairo_region_t *opaque_region = NULL;

  if (value->type != META_PROP_VALUE_INVALID)
    {
692
      uint32_t *region = value->v.cardinal_list.cardinals;
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
      int nitems = value->v.cardinal_list.n_cardinals;

      cairo_rectangle_int_t *rects;
      int i, rect_index, nrects;

      if (nitems % 4 != 0)
        {
          meta_verbose ("_NET_WM_OPAQUE_REGION does not have a list of 4-tuples.");
          goto out;
        }

      /* empty region */
      if (nitems == 0)
        goto out;

      nrects = nitems / 4;

      rects = g_new (cairo_rectangle_int_t, nrects);

      rect_index = 0;
      i = 0;
      while (i < nitems)
        {
          cairo_rectangle_int_t *rect = &rects[rect_index];

          rect->x = region[i++];
          rect->y = region[i++];
          rect->width = region[i++];
          rect->height = region[i++];

          rect_index++;
        }

      opaque_region = cairo_region_create_rectangles (rects, nrects);

      g_free (rects);
    }

 out:
  meta_window_set_opaque_region (window, opaque_region);
  cairo_region_destroy (opaque_region);
734 735
}

Tomas Frydrych's avatar
Tomas Frydrych committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
static void
reload_mutter_hints (MetaWindow    *window,
                     MetaPropValue *value,
                     gboolean       initial)
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
      char     *new_hints = value->v.str;
      char     *old_hints = window->mutter_hints;
      gboolean  changed   = FALSE;

      if (new_hints)
        {
          if (!old_hints || strcmp (new_hints, old_hints))
            changed = TRUE;
        }
      else
        {
          if (old_hints)
            changed = TRUE;
        }

      if (changed)
        {
          g_free (old_hints);

          if (new_hints)
            window->mutter_hints = g_strdup (new_hints);
          else
            window->mutter_hints = NULL;

          g_object_notify (G_OBJECT (window), "mutter-hints");
        }
    }
  else if (window->mutter_hints)
    {
      g_free (window->mutter_hints);
      window->mutter_hints = NULL;

      g_object_notify (G_OBJECT (window), "mutter-hints");
    }
}

779 780
static void
reload_net_wm_state (MetaWindow    *window,
781 782
                     MetaPropValue *value,
                     gboolean       initial)
783
{
784
  MetaX11Display *x11_display = window->display->x11_display;
785 786 787
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

788 789 790 791 792 793
  int i;

  /* We know this is only an initial window creation,
   * clients don't change the property.
   */

794 795 796 797 798 799 800
  if (!initial) {
    /* no, they DON'T change the property */
    meta_verbose ("Ignoring _NET_WM_STATE: we should be the one who set "
                  "the property in the first place\n");
    return;
  }

801 802 803
  window->shaded = FALSE;
  window->maximized_horizontally = FALSE;
  window->maximized_vertically = FALSE;
804
  window->fullscreen = FALSE;
805
  priv->wm_state_modal = FALSE;
806 807
  priv->wm_state_skip_taskbar = FALSE;
  priv->wm_state_skip_pager = FALSE;
808 809 810 811 812 813 814 815 816 817
  window->wm_state_above = FALSE;
  window->wm_state_below = FALSE;
  window->wm_state_demands_attention = FALSE;

  if (value->type == META_PROP_VALUE_INVALID)
    return;

  i = 0;
  while (i < value->v.atom_list.n_atoms)
    {
818
      if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_SHADED)
819
        window->shaded = TRUE;
820
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_MAXIMIZED_HORZ)
821
        window->maximize_horizontally_after_placement = TRUE;
822
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_MAXIMIZED_VERT)
823
        window->maximize_vertically_after_placement = TRUE;
824
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_HIDDEN)
825
        window->minimize_after_placement = TRUE;
826
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_MODAL)
827
        priv->wm_state_modal = TRUE;
828
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_SKIP_TASKBAR)
829
        priv->wm_state_skip_taskbar = TRUE;
830
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_SKIP_PAGER)
831
        priv->wm_state_skip_pager = TRUE;
832
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_FULLSCREEN)
833 834 835 836
        {
          window->fullscreen = TRUE;
          g_object_notify (G_OBJECT (window), "fullscreen");
        }
837
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_ABOVE)
838
        window->wm_state_above = TRUE;
839
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_BELOW)
840
        window->wm_state_below = TRUE;
841
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_DEMANDS_ATTENTION)
842
        window->wm_state_demands_attention = TRUE;
843
      else if (value->v.atom_list.atoms[i] == x11_display->atom__NET_WM_STATE_STICKY)
844
        window->on_all_workspaces_requested = TRUE;
845 846 847 848 849 850 851

      ++i;
    }

  meta_verbose ("Reloaded _NET_WM_STATE for %s\n",
                window->desc);

852
  meta_window_x11_recalc_window_type (window);
853
  meta_window_recalc_features (window);
854 855 856 857
}

static void
reload_mwm_hints (MetaWindow    *window,
858 859
                  MetaPropValue *value,
                  gboolean       initial)
860 861
{
  MotifWmHints *hints;
862
  gboolean old_decorated = window->decorated;
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887

  window->mwm_decorated = TRUE;
  window->mwm_border_only = FALSE;
  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;

  if (value->type == META_PROP_VALUE_INVALID)
    {
      meta_verbose ("Window %s has no MWM hints\n", window->desc);
      meta_window_recalc_features (window);
      return;
    }

  hints = value->v.motif_hints;

  /* We support those MWM hints deemed non-stupid */

  meta_verbose ("Window %s has MWM hints\n",
                window->desc);

  if (hints->flags & MWM_HINTS_DECORATIONS)
    {
888
      meta_verbose ("Window %s sets MWM_HINTS_DECORATIONS 0x%x\n",
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
          window->desc, hints->decorations);

      if (hints->decorations == 0)
        window->mwm_decorated = FALSE;
      /* some input methods use this */
      else if (hints->decorations == MWM_DECOR_BORDER)
        window->mwm_border_only = TRUE;
    }
  else
    meta_verbose ("Decorations flag unset\n");

  if (hints->flags & MWM_HINTS_FUNCTIONS)
    {
      gboolean toggle_value;

904
      meta_verbose ("Window %s sets MWM_HINTS_FUNCTIONS 0x%x\n",
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
                    window->desc, hints->functions);

      /* If _ALL is specified, then other flags indicate what to turn off;
       * if ALL is not specified, flags are what to turn on.
       * at least, I think so
       */

      if ((hints->functions & MWM_FUNC_ALL) == 0)
        {
          toggle_value = TRUE;

          meta_verbose ("Window %s disables all funcs then reenables some\n",
                        window->desc);
          window->mwm_has_close_func = FALSE;
          window->mwm_has_minimize_func = FALSE;
          window->mwm_has_maximize_func = FALSE;
          window->mwm_has_move_func = FALSE;
          window->mwm_has_resize_func = FALSE;
        }
      else
        {
          meta_verbose ("Window %s enables all funcs then disables some\n",
                        window->desc);
          toggle_value = FALSE;
        }

      if ((hints->functions & MWM_FUNC_CLOSE) != 0)
        {
          meta_verbose ("Window %s toggles close via MWM hints\n",
                        window->desc);
          window->mwm_has_close_func = toggle_value;
        }
      if ((hints->functions & MWM_FUNC_MINIMIZE) != 0)
        {
          meta_verbose ("Window %s toggles minimize via MWM hints\n",
                        window->desc);
          window->mwm_has_minimize_func = toggle_value;
        }
      if ((hints->functions & MWM_FUNC_MAXIMIZE) != 0)
        {
          meta_verbose ("Window %s toggles maximize via MWM hints\n",
                        window->desc);
          window->mwm_has_maximize_func = toggle_value;
        }
      if ((hints->functions & MWM_FUNC_MOVE) != 0)
        {
          meta_verbose ("Window %s toggles move via MWM hints\n",
                        window->desc);
          window->mwm_has_move_func = toggle_value;
        }
      if ((hints->functions & MWM_FUNC_RESIZE) != 0)
        {
          meta_verbose ("Window %s toggles resize via MWM hints\n",
                        window->desc);
          window->mwm_has_resize_func = toggle_value;
        }
    }
  else
    meta_verbose ("Functions flag unset\n");

  meta_window_recalc_features (window);
966

967
  /* We do all this anyhow at the end of meta_window_x11_new() */
968 969 970 971 972 973
  if (!window->constructing)
    {
      if (window->decorated)
        meta_window_ensure_frame (window);
      else
        meta_window_destroy_frame (window);
974

975 976 977 978
      meta_window_queue (window,
                         META_QUEUE_MOVE_RESIZE |
                         /* because ensure/destroy frame may unmap: */
                         META_QUEUE_CALC_SHOWING);
979 980 981

      if (old_decorated != window->decorated)
        g_object_notify (G_OBJECT (window), "decorated");
982 983 984 985 986
    }
}

static void
reload_wm_class (MetaWindow    *window,
987 988
                 MetaPropValue *value,
                 gboolean       initial)
989 990
{
  if (value->type != META_PROP_VALUE_INVALID)
991
    {
992 993 994 995 996 997 998
      g_autofree gchar *res_class = g_convert (value->v.class_hint.res_class, -1,
                                               "UTF-8", "LATIN1",
                                               NULL, NULL, NULL);
      g_autofree gchar *res_name = g_convert (value->v.class_hint.res_name, -1,
                                              "UTF-8", "LATIN1",
                                              NULL, NULL, NULL);
      meta_window_set_wm_class (window, res_class, res_name);
999 1000 1001 1002
    }
  else
    {
      meta_window_set_wm_class (window, NULL, NULL);
1003 1004 1005 1006 1007 1008 1009 1010
    }

  meta_verbose ("Window %s class: '%s' name: '%s'\n",
      window->desc,
      window->res_class ? window->res_class : "none",
      window->res_name ? window->res_name : "none");
}

1011 1012
static void
reload_net_wm_desktop (MetaWindow    *window,
1013 1014
                       MetaPropValue *value,
                       gboolean       initial)
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
      window->initial_workspace_set = TRUE;
      window->initial_workspace = value->v.cardinal;
      meta_topic (META_DEBUG_PLACEMENT,
                  "Read initial workspace prop %d for %s\n",
                  window->initial_workspace, window->desc);
    }
}

1026 1027
static void
reload_net_startup_id (MetaWindow    *window,
1028 1029
                       MetaPropValue *value,
                       gboolean       initial)
1030
{
1031
  MetaWorkspaceManager *workspace_manager = window->display->workspace_manager;
1032 1033
  guint32 timestamp = window->net_wm_user_time;
  MetaWorkspace *workspace = NULL;
1034

1035
  g_free (window->startup_id);
1036

1037 1038 1039 1040
  if (value->type != META_PROP_VALUE_INVALID)
    window->startup_id = g_strdup (value->v.str);
  else
    window->startup_id = NULL;
1041

1042 1043 1044
  /* Update timestamp and workspace on a running window */
  if (!window->constructing)
  {
1045
    window->initial_timestamp_set = 0;
1046
    window->initial_workspace_set = 0;
1047

1048
    if (meta_display_apply_startup_properties (window->display, window))
1049
      {
1050

1051 1052 1053
        if (window->initial_timestamp_set)
          timestamp = window->initial_timestamp;
        if (window->initial_workspace_set)
1054 1055
          workspace = meta_workspace_manager_get_workspace_by_index (workspace_manager,
                                                                     window->initial_workspace);
1056

1057 1058
        meta_window_activate_with_workspace (window, timestamp, workspace);
      }
1059
  }
1060

1061 1062 1063 1064 1065
  meta_verbose ("New _NET_STARTUP_ID \"%s\" for %s\n",
                window->startup_id ? window->startup_id : "unset",
                window->desc);
}

1066 1067
static void
reload_update_counter (MetaWindow    *window,
1068 1069
                       MetaPropValue *value,
                       gboolean       initial)
1070 1071 1072
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
1073
      meta_window_x11_destroy_sync_request_alarm (window);
1074
      window->sync_request_counter = None;
1075

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
      if (value->v.xcounter_list.n_counters == 0)
        {
          meta_warning ("_NET_WM_SYNC_REQUEST_COUNTER is empty\n");
          return;
        }

      if (value->v.xcounter_list.n_counters == 1)
        {
          window->sync_request_counter = value->v.xcounter_list.counters[0];
          window->extended_sync_request_counter = FALSE;
        }
      else
        {
          window->sync_request_counter = value->v.xcounter_list.counters[1];
          window->extended_sync_request_counter = TRUE;
        }
      meta_verbose ("Window has _NET_WM_SYNC_REQUEST_COUNTER 0x%lx (extended=%s)\n",
                    window->sync_request_counter,
                    window->extended_sync_request_counter ? "true" : "false");
1095

1096
      if (window->extended_sync_request_counter)
1097
        meta_window_x11_create_sync_request_alarm (window);
1098 1099 1100
    }
}

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
#define FLAG_TOGGLED_ON(old,new,flag) \
 (((old)->flags & (flag)) == 0 &&     \
  ((new)->flags & (flag)) != 0)

#define FLAG_TOGGLED_OFF(old,new,flag) \
 (((old)->flags & (flag)) != 0 &&      \
  ((new)->flags & (flag)) == 0)

#define FLAG_CHANGED(old,new,flag) \
  (FLAG_TOGGLED_ON(old,new,flag) || FLAG_TOGGLED_OFF(old,new,flag))

static void
spew_size_hints_differences (const XSizeHints *old,
                             const XSizeHints *new)
{
  if (FLAG_CHANGED (old, new, USPosition))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: USPosition now %s\n",
                FLAG_TOGGLED_ON (old, new, USPosition) ? "set" : "unset");
  if (FLAG_CHANGED (old, new, USSize))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: USSize now %s\n",
                FLAG_TOGGLED_ON (old, new, USSize) ? "set" : "unset");
  if (FLAG_CHANGED (old, new, PPosition))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PPosition now %s\n",
                FLAG_TOGGLED_ON (old, new, PPosition) ? "set" : "unset");
  if (FLAG_CHANGED (old, new, PSize))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PSize now %s\n",
                FLAG_TOGGLED_ON (old, new, PSize) ? "set" : "unset");
  if (FLAG_CHANGED (old, new, PMinSize))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PMinSize now %s (%d x %d -> %d x %d)\n",
                FLAG_TOGGLED_ON (old, new, PMinSize) ? "set" : "unset",
                old->min_width, old->min_height,
                new->min_width, new->min_height);
  if (FLAG_CHANGED (old, new, PMaxSize))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PMaxSize now %s (%d x %d -> %d x %d)\n",
                FLAG_TOGGLED_ON (old, new, PMaxSize) ? "set" : "unset",
                old->max_width, old->max_height,
                new->max_width, new->max_height);
  if (FLAG_CHANGED (old, new, PResizeInc))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PResizeInc now %s (width_inc %d -> %d height_inc %d -> %d)\n",
                FLAG_TOGGLED_ON (old, new, PResizeInc) ? "set" : "unset",
                old->width_inc, new->width_inc,
                old->height_inc, new->height_inc);
  if (FLAG_CHANGED (old, new, PAspect))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PAspect now %s (min %d/%d -> %d/%d max %d/%d -> %d/%d)\n",
                FLAG_TOGGLED_ON (old, new, PAspect) ? "set" : "unset",
                old->min_aspect.x, old->min_aspect.y,
                new->min_aspect.x, new->min_aspect.y,
                old->max_aspect.x, old->max_aspect.y,
                new->max_aspect.x, new->max_aspect.y);
  if (FLAG_CHANGED (old, new, PBaseSize))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PBaseSize now %s (%d x %d -> %d x %d)\n",
                FLAG_TOGGLED_ON (old, new, PBaseSize) ? "set" : "unset",
                old->base_width, old->base_height,
                new->base_width, new->base_height);
  if (FLAG_CHANGED (old, new, PWinGravity))
    meta_topic (META_DEBUG_GEOMETRY, "XSizeHints: PWinGravity now %s  (%d -> %d)\n",
                FLAG_TOGGLED_ON (old, new, PWinGravity) ? "set" : "unset",
1158
                old->win_gravity, new->win_gravity);
1159 1160
}

1161 1162
void
meta_set_normal_hints (MetaWindow *window,
1163
                       XSizeHints *hints)
1164 1165
{
  int x, y, w, h;
1166 1167 1168 1169 1170
  double minr, maxr;
  /* Some convenience vars */
  int minw, minh, maxw, maxh;   /* min/max width/height                      */
  int basew, baseh, winc, hinc; /* base width/height, width/height increment */

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
  /* Save the last ConfigureRequest, which we put here.
   * Values here set in the hints are supposed to
   * be ignored.
   */
  x = window->size_hints.x;
  y = window->size_hints.y;
  w = window->size_hints.width;
  h = window->size_hints.height;

  /* as far as I can tell, value->v.size_hints.flags is just to
   * check whether we had old-style normal hints without gravity,
   * base size as returned by XGetNormalHints(), so we don't
   * really use it as we fixup window->size_hints to have those
   * fields if they're missing.
   */

  /*
   * When the window is first created, NULL hints will
   * be passed in which will initialize all of the fields
   * as if flags were zero
   */
  if (hints)
    window->size_hints = *hints;
  else
    window->size_hints.flags = 0;

  /* Put back saved ConfigureRequest. */
  window->size_hints.x = x;
  window->size_hints.y = y;
  window->size_hints.width = w;
  window->size_hints.height = h;

1203
  /* Get base size hints */
1204 1205 1206
  if (window->size_hints.flags & PBaseSize)
    {
      meta_topic (META_DEBUG_GEOMETRY, "Window %s sets base size %d x %d\n",
1207 1208 1209
                  window->desc,
                  window->size_hints.base_width,
                  window->size_hints.base_height);
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    }
  else if (window->size_hints.flags & PMinSize)
    {
      window->size_hints.base_width = window->size_hints.min_width;
      window->size_hints.base_height = window->size_hints.min_height;
    }
  else
    {
      window->size_hints.base_width = 0;
      window->size_hints.base_height = 0;
    }
  window->size_hints.flags |= PBaseSize;

1223
  /* Get min size hints */
1224 1225 1226
  if (window->size_hints.flags & PMinSize)
    {
      meta_topic (META_DEBUG_GEOMETRY, "Window %s sets min size %d x %d\n",
1227 1228 1229
                  window->desc,
                  window->size_hints.min_width,
                  window->size_hints.min_height);
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    }
  else if (window->size_hints.flags & PBaseSize)
    {
      window->size_hints.min_width = window->size_hints.base_width;
      window->size_hints.min_height = window->size_hints.base_height;
    }
  else
    {
      window->size_hints.min_width = 0;
      window->size_hints.min_height = 0;
    }
  window->size_hints.flags |= PMinSize;

1243
  /* Get max size hints */
1244 1245 1246
  if (window->size_hints.flags & PMaxSize)
    {
      meta_topic (META_DEBUG_GEOMETRY, "Window %s sets max size %d x %d\n",
1247 1248 1249
                  window->desc,
                  window->size_hints.max_width,
                  window->size_hints.max_height);
1250 1251 1252 1253 1254 1255 1256 1257
    }
  else
    {
      window->size_hints.max_width = G_MAXINT;
      window->size_hints.max_height = G_MAXINT;
      window->size_hints.flags |= PMaxSize;
    }

1258 1259
  /* Get resize increment hints */
  if (window->size_hints.flags & PResizeInc)
1260 1261
    {
      meta_topic (META_DEBUG_GEOMETRY,
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
                  "Window %s sets resize width inc: %d height inc: %d\n",
                  window->desc,
                  window->size_hints.width_inc,
                  window->size_hints.height_inc);
    }
  else
    {
      window->size_hints.width_inc = 1;
      window->size_hints.height_inc = 1;
      window->size_hints.flags |= PResizeInc;
1272 1273
    }

1274 1275
  /* Get aspect ratio hints */
  if (window->size_hints.flags & PAspect)
1276 1277
    {
      meta_topic (META_DEBUG_GEOMETRY,
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
                  "Window %s sets min_aspect: %d/%d max_aspect: %d/%d\n",
                  window->desc,
                  window->size_hints.min_aspect.x,
                  window->size_hints.min_aspect.y,
                  window->size_hints.max_aspect.x,
                  window->size_hints.max_aspect.y);
    }
  else
    {
      window->size_hints.min_aspect.x = 1;
      window->size_hints.min_aspect.y = G_MAXINT;
      window->size_hints.max_aspect.x = G_MAXINT;
      window->size_hints.max_aspect.y = 1;
      window->size_hints.flags |= PAspect;
1292 1293
    }

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  /* Get gravity hint */
  if (window->size_hints.flags & PWinGravity)
    {
      meta_topic (META_DEBUG_GEOMETRY, "Window %s sets gravity %d\n",
                  window->desc,
                  window->size_hints.win_gravity);
    }
  else
    {
      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s doesn't set gravity, using NW\n",
                  window->desc);
      window->size_hints.win_gravity = NorthWestGravity;
      window->size_hints.flags |= PWinGravity;
    }

  /*** Lots of sanity checking ***/

  /* Verify all min & max hints are at least 1 pixel */
1313 1314
  if (window->size_hints.min_width < 1)
    {
1315
      /* someone is on crack */
1316
      meta_topic (META_DEBUG_GEOMETRY,
1317 1318
                  "Window %s sets min width to 0, which makes no sense\n",
                  window->desc);
1319 1320 1321 1322 1323 1324
      window->size_hints.min_width = 1;
    }
  if (window->size_hints.max_width < 1)
    {
      /* another cracksmoker */
      meta_topic (META_DEBUG_GEOMETRY,
1325 1326
                  "Window %s sets max width to 0, which makes no sense\n",
                  window->desc);
1327 1328 1329 1330 1331 1332
      window->size_hints.max_width = 1;
    }
  if (window->size_hints.min_height < 1)
    {
      /* another cracksmoker */
      meta_topic (META_DEBUG_GEOMETRY,
1333 1334
                  "Window %s sets min height to 0, which makes no sense\n",
                  window->desc);
1335 1336 1337 1338 1339 1340
      window->size_hints.min_height = 1;
    }
  if (window->size_hints.max_height < 1)
    {
      /* another cracksmoker */
      meta_topic (META_DEBUG_GEOMETRY,
1341 1342
                  "Window %s sets max height to 0, which makes no sense\n",
                  window->desc);
1343 1344 1345
      window->size_hints.max_height = 1;
    }

1346 1347
  /* Verify size increment hints are at least 1 pixel */
  if (window->size_hints.width_inc < 1)
1348
    {
1349 1350 1351
      /* app authors find so many ways to smoke crack */
      window->size_hints.width_inc = 1;
      meta_topic (META_DEBUG_GEOMETRY, "Corrected 0 width_inc to 1\n");
1352
    }
1353
  if (window->size_hints.height_inc < 1)
1354
    {
1355
      /* another cracksmoker */
1356
      window->size_hints.height_inc = 1;
1357 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
      meta_topic (META_DEBUG_GEOMETRY, "Corrected 0 height_inc to 1\n");
    }
  /* divide by 0 cracksmokers; note that x & y in (min|max)_aspect are
   * numerator & denominator
   */
  if (window->size_hints.min_aspect.y < 1)
    window->size_hints.min_aspect.y = 1;
  if (window->size_hints.max_aspect.y < 1)
    window->size_hints.max_aspect.y = 1;

  minw  = window->size_hints.min_width;  minh  = window->size_hints.min_height;
  maxw  = window->size_hints.max_width;  maxh  = window->size_hints.max_height;
  basew = window->size_hints.base_width; baseh = window->size_hints.base_height;
  winc  = window->size_hints.width_inc;  hinc  = window->size_hints.height_inc;

  /* Make sure min and max size hints are consistent with the base + increment
   * size hints.  If they're not, it's not a real big deal, but it means the
   * effective min and max size are more restrictive than the application
   * specified values.
   */
  if ((minw - basew) % winc != 0)
    {
      /* Take advantage of integer division throwing away the remainder... */
      window->size_hints.min_width = basew + ((minw - basew)/winc + 1)*winc;

      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s has width_inc (%d) that does not evenly divide "
                  "min_width - base_width (%d - %d); thus effective "
                  "min_width is really %d\n",
                  window->desc,
                  winc, minw, basew, window->size_hints.min_width);
      minw = window->size_hints.min_width;
1389
    }
1390 1391 1392 1393
  if (maxw != G_MAXINT && (maxw - basew) % winc != 0)
    {
      /* Take advantage of integer division throwing away the remainder... */
      window->size_hints.max_width = basew + ((maxw - basew)/winc)*winc;
1394

1395 1396 1397 1398 1399 1400 1401 1402 1403
      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s has width_inc (%d) that does not evenly divide "
                  "max_width - base_width (%d - %d); thus effective "
                  "max_width is really %d\n",
                  window->desc,
                  winc, maxw, basew, window->size_hints.max_width);
      maxw = window->size_hints.max_width;
    }
  if ((minh - baseh) % hinc != 0)
1404
    {
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
      /* Take advantage of integer division throwing away the remainder... */
      window->size_hints.min_height = baseh + ((minh - baseh)/hinc + 1)*hinc;

      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s has height_inc (%d) that does not evenly divide "
                  "min_height - base_height (%d - %d); thus effective "
                  "min_height is really %d\n",
                  window->desc,
                  hinc, minh, baseh, window->size_hints.min_height);
      minh = window->size_hints.min_height;
1415
    }
1416
  if (maxh != G_MAXINT && (maxh - baseh) % hinc != 0)
1417
    {
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
      /* Take advantage of integer division throwing away the remainder... */
      window->size_hints.max_height = baseh + ((maxh - baseh)/hinc)*hinc;

      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s has height_inc (%d) that does not evenly divide "
                  "max_height - base_height (%d - %d); thus effective "
                  "max_height is really %d\n",
                  window->desc,
                  hinc, maxh, baseh, window->size_hints.max_height);
      maxh = window->size_hints.max_height;
    }

  /* make sure maximum size hints are compatible with minimum size hints; min
   * size hints take precedence.
   */
  if (window->size_hints.max_width < window->size_hints.min_width)
    {
      /* another cracksmoker */
      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s sets max width %d less than min width %d, "
                  "disabling resize\n",
                  window->desc,
                  window->size_hints.max_width,
                  window->size_hints.min_width);
      maxw = window->size_hints.max_width = window->size_hints.min_width;
    }
  if (window->size_hints.max_height < window->size_hints.min_height)
    {
      /* another cracksmoker */
      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s sets max height %d less than min height %d, "
                  "disabling resize\n",
                  window->desc,
                  window->size_hints.max_height,
                  window->size_hints.min_height);
      maxh = window->size_hints.max_height = window->size_hints.min_height;
    }

  /* Make sure the aspect ratio hints are sane. */
  minr =         window->size_hints.min_aspect.x /
         (double)window->size_hints.min_aspect.y;
  maxr =         window->size_hints.max_aspect.x /
         (double)window->size_hints.max_aspect.y;
  if (minr > maxr)
    {
      /* another cracksmoker; not even minimally (self) consistent */
      meta_topic (META_DEBUG_GEOMETRY,
                  "Window %s sets min aspect ratio larger than max aspect "
                  "ratio; disabling aspect ratio constraints.\n",
                  window->desc);
1468 1469 1470 1471 1472
      window->size_hints.min_aspect.x = 1;
      window->size_hints.min_aspect.y = G_MAXINT;
      window->size_hints.max_aspect.x = G_MAXINT;
      window->size_hints.max_aspect.y = 1;
    }
1473
  else /* check consistency of aspect ratio hints with other hints */
1474
    {
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
      if (minh > 0 && minr > (maxw / (double)minh))
        {
          /* another cracksmoker */
          meta_topic (META_DEBUG_GEOMETRY,
                      "Window %s sets min aspect ratio larger than largest "
                      "aspect ratio possible given min/max size constraints; "
                      "disabling min aspect ratio constraint.\n",
                      window->desc);
          window->size_hints.min_aspect.x = 1;
          window->size_hints.min_aspect.y = G_MAXINT;
        }
      if (maxr < (minw / (double)maxh))
        {
          /* another cracksmoker */
          meta_topic (META_DEBUG_GEOMETRY,
                      "Window %s sets max aspect ratio smaller than smallest "
                      "aspect ratio possible given min/max size constraints; "
                      "disabling max aspect ratio constraint.\n",
                      window->desc);
          window->size_hints.max_aspect.x = G_MAXINT;
          window->size_hints.max_aspect.y = 1;
        }
      /* FIXME: Would be nice to check that aspect ratios are
       * consistent with base and size increment constraints.
       */
1500 1501 1502
    }
}

1503 1504
static void
reload_normal_hints (MetaWindow    *window,
1505 1506
                     MetaPropValue *value,
                     gboolean       initial)
1507 1508 1509 1510
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
      XSizeHints old_hints;
1511

1512 1513 1514
      meta_topic (META_DEBUG_GEOMETRY, "Updating WM_NORMAL_HINTS for %s\n", window->desc);

      old_hints = window->size_hints;
1515

1516
      meta_set_normal_hints (window, value->v.size_hints.hints);
1517

1518
      spew_size_hints_differences (&old_hints, &window->size_hints);
1519

1520 1521
      meta_window_recalc_features (window);

1522 1523
      if (!initial)
        meta_window_queue(window, META_QUEUE_MOVE_RESIZE);
1524 1525 1526 1527 1528
    }
}

static void
reload_wm_protocols (MetaWindow    *window,
1529 1530
                     MetaPropValue *value,
                     gboolean       initial)
1531 1532
{
  int i;
1533

1534
  window->can_ping = FALSE;
1535
  meta_window_x11_set_wm_take_focus (window, FALSE);
1536
  meta_window_x11_set_wm_delete_window (window, FALSE);
1537 1538

  if (value->type == META_PROP_VALUE_INVALID)
1539 1540 1541 1542 1543 1544
    return;

  i = 0;
  while (i < value->v.atom_list.n_atoms)
    {
      if (value->v.atom_list.atoms[i] ==
1545
          window->display->x11_display->atom_WM_TAKE_FOCUS)
1546
        meta_window_x11_set_wm_take_focus (window, TRUE);
1547
      else if (value->v.atom_list.atoms[i] ==
1548
               window->display->x11_display->atom_WM_DELETE_WINDOW)