summaryrefslogtreecommitdiff
path: root/src/xprops.c
blob: f60b85e72dc5c0b8989219a956f68aa04d26cd82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Metacity X property convenience routines */

/* 
 * Copyright (C) 2001 Havoc Pennington
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <config.h>
#include "xprops.h"
#include "errors.h"
#include "util.h"
#include <X11/Xatom.h>

static gboolean
check_type_and_format (MetaDisplay *display,
                       Window       xwindow,
                       Atom         xatom,
                       int          expected_format,
                       Atom         expected_type,                           
                       int          format,
                       Atom         type)
{
  char *type_name;
  char *expected_name;
  char *prop_name;

  if (expected_format == format &&
      expected_type == type)
    return TRUE;
  
  meta_error_trap_push (display);
  type_name = XGetAtomName (display->xdisplay, type);
  expected_name = XGetAtomName (display->xdisplay, expected_type);
  prop_name = XGetAtomName (display->xdisplay, xatom);
  meta_error_trap_pop (display);

  meta_warning (_("Window 0x%lx has property %s that was expected to have type %s format %d and actually has type %s format %d\n"),
                xwindow,
                prop_name ? prop_name : "(bad atom)",
                expected_name ? expected_name : "(bad atom)",
                expected_format,
                type_name ? type_name : "(bad atom)",
                format);

  if (type_name)
    XFree (type_name);
  if (expected_name)
    XFree (expected_name);
  if (prop_name)
    XFree (prop_name);

  return FALSE;
}

gboolean
meta_prop_get_atom_list (MetaDisplay *display,
                         Window       xwindow,
                         Atom         xatom,
                         Atom       **atoms_p,
                         int         *n_atoms_p)
{
  Atom type;
  int format;
  gulong n_atoms;
  gulong bytes_after;
  Atom *atoms;

  *atoms_p = NULL;
  *n_atoms_p = 0;
  
  meta_error_trap_push (display);
  if (XGetWindowProperty (display->xdisplay, xwindow, xatom,
                          0, G_MAXLONG,
                          False, XA_ATOM, &type, &format, &n_atoms,
                          &bytes_after, (guchar **)&atoms) != Success)
    {
      meta_error_trap_pop (display);
      return FALSE;
    }

  if (meta_error_trap_pop (display) != Success)
    return FALSE;
    
  if (!check_type_and_format (display, xwindow, xatom, 32, XA_ATOM,
                              format, type))
    {
      XFree (atoms);
      return FALSE;
    }

  *atoms_p = atoms;
  *n_atoms_p = n_atoms;

  return TRUE;
}

gboolean
meta_prop_get_cardinal_list (MetaDisplay *display,
                             Window       xwindow,
                             Atom         xatom,
                             gulong     **cardinals_p,
                             int         *n_cardinals_p)
{
  Atom type;
  int format;
  gulong n_cardinals;
  gulong bytes_after;
  gulong *cardinals;

  *cardinals_p = NULL;
  *n_cardinals_p = 0;
  
  meta_error_trap_push (display);
  if (XGetWindowProperty (display->xdisplay, xwindow, xatom,
                          0, G_MAXLONG,
                          False, XA_CARDINAL, &type, &format, &n_cardinals,
                          &bytes_after, (guchar **)&cardinals) != Success)
    {
      meta_error_trap_pop (display);
      return FALSE;
    }

  if (meta_error_trap_pop (display) != Success)
    return FALSE;

  if (!check_type_and_format (display, xwindow, xatom, 32, XA_CARDINAL,
                              format, type))
    {
      XFree (cardinals);
      return FALSE;
    }

  *cardinals_p = cardinals;
  *n_cardinals_p = n_cardinals;

  return TRUE;
}

gboolean
meta_prop_get_motif_hints (MetaDisplay   *display,
                           Window         xwindow,
                           Atom           xatom,
                           MotifWmHints **hints_p)
{
  Atom type;
  int format;
  gulong bytes_after;
  MotifWmHints *hints;
  gulong n_items;

#define EXPECTED_ITEMS sizeof (MotifWmHints)/sizeof (gulong)
  
  *hints_p = NULL;
  
  meta_error_trap_push (display);
  if (XGetWindowProperty (display->xdisplay, xwindow, xatom,
                          0, EXPECTED_ITEMS,
                          False, AnyPropertyType, &type, &format, &n_items,
                          &bytes_after, (guchar **)&hints) != Success)
    {
      meta_error_trap_pop (display);
      return FALSE;
    }

  if (meta_error_trap_pop (display) != Success)
    return FALSE;
  
  if (type == None || n_items != EXPECTED_ITEMS)
    {
      meta_verbose ("Motif hints had unexpected type or n_items\n");
      XFree (hints);
      return FALSE;
    }

  *hints_p = hints;
  
  return TRUE;
}