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

Tomeu Vizoso's avatar
Tomeu Vizoso committed
3
/*
Jasper St. Pierre's avatar
Jasper St. Pierre committed
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

Jasper St. Pierre's avatar
Jasper St. Pierre committed
18
/*
19
 * Copyright (C) 2001, 2002, 2003 Red Hat, Inc.
20
 * Copyright (C) 2004, 2005 Elijah Newren
21
 * Copyright (C) 2009 Thomas Thurman
Jasper St. Pierre's avatar
Jasper St. Pierre committed
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.
Jasper St. Pierre's avatar
Jasper St. Pierre committed
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
#include <config.h>
#include "window-props.h"
41
#include "window-x11.h"
42
#include "window-x11-private.h"
43
#include "x11/meta-x11-display-private.h"
44
#include <meta/meta-x11-errors.h>
45
#include "xprops.h"
46
#include "frame.h"
47
#include <meta/group.h>
48
#include <X11/Xatom.h>
49 50
#include <unistd.h>
#include <string.h>
51
#include "util-private.h"
52
#include "meta-workspace-manager-private.h"
53

54 55 56 57 58
#ifndef HOST_NAME_MAX
/* Solaris headers apparently don't define this so do so manually; #326745 */
#define HOST_NAME_MAX 255
#endif

59
typedef void (* ReloadValueFunc) (MetaWindow    *window,
60 61
                                  MetaPropValue *value,
                                  gboolean       initial);
62

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

71
struct _MetaWindowPropHooks
72 73
{
  Atom property;
74
  MetaPropValueType type;
75
  ReloadValueFunc reload_func;
76
  MetaPropHookFlags flags;
77
};
78

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


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

99
  hooks = find_hooks (window->display->x11_display, property);
100 101
  if (!hooks)
    return;
102

103 104 105
  if ((hooks->flags & INIT_ONLY) && !initial)
    return;

106
  init_prop_value (window, hooks, &value);
107

108
  meta_prop_get_values (window->display->x11_display, xwindow,
109
                        &value, 1);
110

111 112 113 114 115
  reload_prop_value (window, hooks, &value,
                     initial);

  meta_prop_free_values (&value, 1);
}
116

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

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

136
  values = g_new0 (MetaPropValue, x11_display->n_prop_hooks);
137 138

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

150
  meta_prop_get_values (window->display->x11_display, window->xwindow,
151 152 153
                        values, n_properties);

  j = 0;
154
  for (i = 0; i < x11_display->n_prop_hooks; i++)
155
    {
156
      MetaWindowPropHooks *hooks = &x11_display->prop_hooks_table[i];
157
      if (hooks->flags & LOAD_INIT)
158 159 160 161 162
        {
          /* 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.
           */
163 164
          if (values[j].type != META_PROP_VALUE_INVALID ||
              hooks->flags & FORCE_INIT)
165 166 167 168 169 170 171 172 173 174
            reload_prop_value (window, hooks, &values[j], TRUE);
          ++j;
        }
    }

  meta_prop_free_values (values, n_properties);

  g_free (values);
}

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

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

static void
reload_wm_client_machine (MetaWindow    *window,
206 207
                          MetaPropValue *value,
                          gboolean       initial)
208 209 210
{
  g_free (window->wm_client_machine);
  window->wm_client_machine = NULL;
Jasper St. Pierre's avatar
Jasper St. Pierre committed
211

212 213 214 215 216
  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");
217 218 219 220 221 222 223 224 225 226 227 228 229

  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;
    }
230 231 232
}

static void
233 234 235 236 237 238 239 240 241 242 243 244 245
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)
{
246
  MetaX11Display *x11_display = window->display->x11_display;
247 248 249 250 251 252 253 254 255 256 257 258 259 260
  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
           */
261 262 263 264 265 266 267 268 269 270 271 272 273 274
          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)
275 276
            {
              priv->type_atom = atom;
277
              break;
278 279 280
            }
        }
    }
281 282

  meta_window_x11_recalc_window_type (window);
283 284 285 286 287 288
}

static void
reload_icon (MetaWindow    *window,
             Atom           atom)
{
289 290 291 292
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

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

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

static void
reload_kwm_win_icon (MetaWindow    *window,
                     MetaPropValue *value,
                     gboolean       initial)
{
311
  reload_icon (window, window->display->x11_display->atom__KWM_WIN_ICON);
312 313
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327
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
        {
328 329 330 331 332 333 334 335
          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);
336 337 338 339
        }
    }
  else
    {
340
      meta_window_set_icon_geometry (window, NULL);
341 342 343
    }
}

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

355 356
      window->has_custom_frame_extents = TRUE;
      window->custom_frame_extents = *extents;
357 358 359 360 361 362 363 364 365 366 367

      /* 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);
        }
368 369 370
    }
  else
    {
371 372 373
      if (!window->has_custom_frame_extents)
        return;

374 375 376 377 378 379 380
      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);
}

Jasper St. Pierre's avatar
Jasper St. Pierre committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394
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
        {
395 396 397 398 399
          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];
400
          meta_window_set_custom_frame_extents (window, &extents, initial);
Jasper St. Pierre's avatar
Jasper St. Pierre committed
401 402 403 404
        }
    }
  else
    {
405
      meta_window_set_custom_frame_extents (window, NULL, initial);
Jasper St. Pierre's avatar
Jasper St. Pierre committed
406 407 408
    }
}

409 410 411 412 413 414 415 416 417 418 419 420
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)
421
{
422 423 424
  g_clear_pointer (&window->role, g_free);
  if (value->type != META_PROP_VALUE_INVALID)
    window->role = g_strdup (value->v.str);
425 426 427 428
}

static void
reload_net_wm_pid (MetaWindow    *window,
429 430
                   MetaPropValue *value,
                   gboolean       initial)
431 432 433
{
  if (value->type != META_PROP_VALUE_INVALID)
    {
434
      uint32_t cardinal = (int) value->v.cardinal;
Jasper St. Pierre's avatar
Jasper St. Pierre committed
435

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

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

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

469 470 471 472
      /* 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. */
473 474
          meta_x11_display_unregister_x_window (window->display->x11_display,
                                                window->user_time_window);
475
          /* Don't get events on not-managed windows */
476
          XSelectInput (window->display->x11_display->xdisplay,
477 478 479 480
                        window->user_time_window,
                        NoEventMask);
        }

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

      /* 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
506
           * than atom__NET_WM_USER_TIME ones, but I just don't care
507 508
           * and it's not specified in the spec anyway.
           */
509 510 511
          meta_x11_display_register_x_window (window->display->x11_display,
                                              &window->user_time_window,
                                              window);
512
          /* Just listen for property notify events */
513
          XSelectInput (window->display->x11_display->xdisplay,
514 515 516 517 518 519 520 521 522
                        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,
523
            window->user_time_window,
524
            window->display->x11_display->atom__NET_WM_USER_TIME,
525
            initial);
526 527 528 529
        }
    }
}

530 531
#define MAX_TITLE_LENGTH 512

532
/**
Jasper St. Pierre's avatar
Jasper St. Pierre committed
533 534
 * set_title_text:
 *
535 536
 * 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.
537
 *
Jasper St. Pierre's avatar
Jasper St. Pierre committed
538
 * Returns: %TRUE if a new title was set.
539 540
 */
static gboolean
541 542 543 544 545
set_title_text (MetaWindow  *window,
                gboolean     previous_was_modified,
                const char  *title,
                Atom         atom,
                char       **target)
546
{
547
  gboolean modified = FALSE;
Jasper St. Pierre's avatar
Jasper St. Pierre committed
548

549 550
  if (!target)
    return FALSE;
Jasper St. Pierre's avatar
Jasper St. Pierre committed
551

552
  g_free (*target);
Jasper St. Pierre's avatar
Jasper St. Pierre committed
553

554 555 556
  if (!title)
    *target = g_strdup ("");
  else if (g_utf8_strlen (title, MAX_TITLE_LENGTH + 1) > MAX_TITLE_LENGTH)
557
    {
558 559 560 561 562
      *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 */
563
  else if (meta_window_is_remote (window))
564
    {
565
      *target = g_strdup_printf (_("%s (on %s)"),
566 567
                      title, window->wm_client_machine);
      modified = TRUE;
568
    }
569 570 571 572
  else
    *target = g_strdup (title);

  if (modified && atom != None)
573
    meta_prop_set_utf8_string_hint (window->display->x11_display,
574 575 576 577 578
                                    window->xwindow,
                                    atom, *target);

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

  return modified;
}

static void
set_window_title (MetaWindow *window,
                  const char *title)
{
594 595 596
  MetaWindowX11 *window_x11 = META_WINDOW_X11 (window);
  MetaWindowX11Private *priv = window_x11->priv;

597
  char *new_title = NULL;
598

599 600
  gboolean modified =
    set_title_text (window,
601
                    priv->using_net_wm_visible_name,
602
                    title,
603
                    window->display->x11_display->atom__NET_WM_VISIBLE_NAME,
604
                    &new_title);
605
  priv->using_net_wm_visible_name = modified;
Jasper St. Pierre's avatar
Jasper St. Pierre committed
606

607 608 609
  meta_window_set_title (window, new_title);

  g_free (new_title);
610 611 612 613
}

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

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

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

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

  if (priv->using_net_wm_name)
646 647 648 649 650
    {
      meta_verbose ("Ignoring WM_NAME \"%s\" as _NET_WM_NAME is set\n",
                    value->v.str);
      return;
    }
Jasper St. Pierre's avatar
Jasper St. Pierre committed
651

652 653
  if (value->type != META_PROP_VALUE_INVALID)
    {
654 655 656 657
      g_autofree gchar *title = g_convert (value->v.str, -1,
                                           "UTF-8", "LATIN1",
                                           NULL, NULL, NULL);
      set_window_title (window, title);
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);
Jasper St. Pierre's avatar
Jasper St. Pierre committed
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);
Jasper St. Pierre's avatar
Jasper St. Pierre committed
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);
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
1026 1027
static void
reload_net_startup_id (MetaWindow    *window,
1028 1029
                       MetaPropValue *value,
                       gboolean       initial)
Havoc Pennington's avatar
Havoc Pennington committed
1030
{
1031
  MetaWorkspaceManager *workspace_manager = window->display->workspace_manager;
1032 1033
  guint32 timestamp = window->net_wm_user_time;
  MetaWorkspace *workspace = NULL;
Jasper St. Pierre's avatar
Jasper St. Pierre committed
1034

Havoc Pennington's avatar
Havoc Pennington committed
1035
  g_free (window->startup_id);
Jasper St. Pierre's avatar
Jasper St. Pierre committed
1036

Havoc Pennington's avatar
Havoc Pennington committed
1037 1038 1039 1040
  if (value->type != META_PROP_VALUE_INVALID)
    window->startup_id = g_strdup (value->v.str);
  else
    window->startup_id = NULL;
Jasper St. Pierre's avatar
Jasper St. Pierre committed
1041

1042 1043 1044
  /* Update timestamp and workspace on a running window */
  if (!window->constructing)
  {
Jasper St. Pierre's avatar
Jasper St. Pierre committed
1045
    window->initial_timestamp_set = 0;
Elijah Newren's avatar