2 * Copyright © 2001, 2007 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Red Hat not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. Red Hat makes no representations about the
11 * suitability of this software for any purpose. It is provided "as is"
12 * without express or implied warranty.
14 * RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL RED HAT
16 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
18 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Author: Owen Taylor, Red Hat, Inc.
26 #include "xsettings-client.h"
34 #include <X11/Xmd.h> /* For CARD16 */
36 struct _XSettingsClient
40 XSettingsNotifyFunc notify;
41 XSettingsWatchFunc watch;
44 XSettingsGrabFunc grab;
45 XSettingsGrabFunc ungrab;
47 Window manager_window;
52 GHashTable *settings; /* string => XSettingsSetting */
56 notify_changes (XSettingsClient *client,
60 XSettingsSetting *setting, *old_setting;
65 if (client->settings != NULL)
67 g_hash_table_iter_init (&iter, client->settings);
68 while (g_hash_table_iter_next (&iter, NULL, (gpointer*) &setting))
70 old_setting = old_list ? g_hash_table_lookup (old_list, setting->name) : NULL;
72 if (old_setting == NULL)
73 client->notify (setting->name, XSETTINGS_ACTION_NEW, setting, client->cb_data);
74 else if (!xsettings_setting_equal (setting, old_setting))
75 client->notify (setting->name, XSETTINGS_ACTION_CHANGED, setting, client->cb_data);
77 /* remove setting from old_list */
78 if (old_setting != NULL)
79 g_hash_table_remove (old_list, setting->name);
85 /* old_list now contains only deleted settings */
86 g_hash_table_iter_init (&iter, old_list);
87 while (g_hash_table_iter_next (&iter, NULL, (gpointer*) &old_setting))
88 client->notify (old_setting->name, XSETTINGS_ACTION_DELETED, NULL, client->cb_data);
93 ignore_errors (Display *display, XErrorEvent *event)
98 #define BYTES_LEFT(buffer) ((buffer)->data + (buffer)->len - (buffer)->pos)
100 static XSettingsResult
101 fetch_card16 (XSettingsBuffer *buffer,
106 if (BYTES_LEFT (buffer) < 2)
107 return XSETTINGS_ACCESS;
109 x = *(CARD16 *)buffer->pos;
112 if (buffer->byte_order == MSBFirst)
113 *result = GUINT16_FROM_BE (x);
115 *result = GUINT16_FROM_LE (x);
117 return XSETTINGS_SUCCESS;
120 static XSettingsResult
121 fetch_ushort (XSettingsBuffer *buffer,
122 unsigned short *result)
127 r = fetch_card16 (buffer, &x);
128 if (r == XSETTINGS_SUCCESS)
134 static XSettingsResult
135 fetch_card32 (XSettingsBuffer *buffer,
140 if (BYTES_LEFT (buffer) < 4)
141 return XSETTINGS_ACCESS;
143 x = *(CARD32 *)buffer->pos;
146 if (buffer->byte_order == MSBFirst)
147 *result = GUINT32_FROM_BE (x);
149 *result = GUINT32_FROM_LE (x);
151 return XSETTINGS_SUCCESS;
154 static XSettingsResult
155 fetch_card8 (XSettingsBuffer *buffer,
158 if (BYTES_LEFT (buffer) < 1)
159 return XSETTINGS_ACCESS;
161 *result = *(CARD8 *)buffer->pos;
164 return XSETTINGS_SUCCESS;
167 #define XSETTINGS_PAD(n,m) ((n + m - 1) & (~(m-1)))
170 parse_settings (unsigned char *data,
173 XSettingsBuffer buffer;
174 XSettingsResult result = XSETTINGS_SUCCESS;
175 GHashTable *settings = NULL;
179 XSettingsSetting *setting = NULL;
181 buffer.pos = buffer.data = data;
184 result = fetch_card8 (&buffer, (unsigned char *)&buffer.byte_order);
185 if (buffer.byte_order != MSBFirst &&
186 buffer.byte_order != LSBFirst)
188 fprintf (stderr, "Invalid byte order in XSETTINGS property\n");
189 result = XSETTINGS_FAILED;
195 result = fetch_card32 (&buffer, &serial);
196 if (result != XSETTINGS_SUCCESS)
199 result = fetch_card32 (&buffer, &n_entries);
200 if (result != XSETTINGS_SUCCESS)
203 for (i = 0; i < n_entries; i++)
210 result = fetch_card8 (&buffer, &type);
211 if (result != XSETTINGS_SUCCESS)
216 result = fetch_card16 (&buffer, &name_len);
217 if (result != XSETTINGS_SUCCESS)
220 pad_len = XSETTINGS_PAD(name_len, 4);
221 if (BYTES_LEFT (&buffer) < pad_len)
223 result = XSETTINGS_ACCESS;
227 setting = malloc (sizeof *setting);
230 result = XSETTINGS_NO_MEM;
233 setting->type = XSETTINGS_TYPE_INT; /* No allocated memory */
235 setting->name = malloc (name_len + 1);
238 result = XSETTINGS_NO_MEM;
242 memcpy (setting->name, buffer.pos, name_len);
243 setting->name[name_len] = '\0';
244 buffer.pos += pad_len;
246 /* last change serial (we ignore it) */
247 result = fetch_card32 (&buffer, &v_int);
248 if (result != XSETTINGS_SUCCESS)
253 case XSETTINGS_TYPE_INT:
254 result = fetch_card32 (&buffer, &v_int);
255 if (result != XSETTINGS_SUCCESS)
258 setting->data.v_int = (INT32)v_int;
260 case XSETTINGS_TYPE_STRING:
261 result = fetch_card32 (&buffer, &v_int);
262 if (result != XSETTINGS_SUCCESS)
265 pad_len = XSETTINGS_PAD (v_int, 4);
266 if (v_int + 1 == 0 || /* Guard against wrap-around */
267 BYTES_LEFT (&buffer) < pad_len)
269 result = XSETTINGS_ACCESS;
273 setting->data.v_string = malloc (v_int + 1);
274 if (!setting->data.v_string)
276 result = XSETTINGS_NO_MEM;
280 memcpy (setting->data.v_string, buffer.pos, v_int);
281 setting->data.v_string[v_int] = '\0';
282 buffer.pos += pad_len;
285 case XSETTINGS_TYPE_COLOR:
286 result = fetch_ushort (&buffer, &setting->data.v_color.red);
287 if (result != XSETTINGS_SUCCESS)
289 result = fetch_ushort (&buffer, &setting->data.v_color.green);
290 if (result != XSETTINGS_SUCCESS)
292 result = fetch_ushort (&buffer, &setting->data.v_color.blue);
293 if (result != XSETTINGS_SUCCESS)
295 result = fetch_ushort (&buffer, &setting->data.v_color.alpha);
296 if (result != XSETTINGS_SUCCESS)
301 /* Quietly ignore unknown types */
305 setting->type = type;
307 if (settings == NULL)
308 settings = g_hash_table_new_full (g_str_hash, g_str_equal,
310 (GDestroyNotify) xsettings_setting_free);
312 if (g_hash_table_lookup (settings, setting->name) != NULL)
314 result = XSETTINGS_DUPLICATE_ENTRY;
318 g_hash_table_insert (settings, setting->name, setting);
324 if (result != XSETTINGS_SUCCESS)
328 case XSETTINGS_NO_MEM:
329 fprintf(stderr, "Out of memory reading XSETTINGS property\n");
331 case XSETTINGS_ACCESS:
332 fprintf(stderr, "Invalid XSETTINGS property (read off end)\n");
334 case XSETTINGS_DUPLICATE_ENTRY:
335 fprintf (stderr, "Duplicate XSETTINGS entry for '%s'\n", setting->name);
336 case XSETTINGS_FAILED:
337 case XSETTINGS_SUCCESS:
338 case XSETTINGS_NO_ENTRY:
343 xsettings_setting_free (setting);
346 g_hash_table_unref (settings);
354 read_settings (XSettingsClient *client)
358 unsigned long n_items;
359 unsigned long bytes_after;
363 int (*old_handler) (Display *, XErrorEvent *);
365 GHashTable *old_list = client->settings;
367 client->settings = NULL;
369 if (client->manager_window)
371 old_handler = XSetErrorHandler (ignore_errors);
372 result = XGetWindowProperty (client->display, client->manager_window,
373 client->xsettings_atom, 0, LONG_MAX,
374 False, client->xsettings_atom,
375 &type, &format, &n_items, &bytes_after, &data);
376 XSetErrorHandler (old_handler);
378 if (result == Success && type != None)
380 if (type != client->xsettings_atom)
382 fprintf (stderr, "Invalid type for XSETTINGS property");
384 else if (format != 8)
386 fprintf (stderr, "Invalid format for XSETTINGS property %d", format);
389 client->settings = parse_settings (data, n_items);
395 notify_changes (client, old_list);
397 g_hash_table_unref (old_list);
401 add_events (Display *display,
405 XWindowAttributes attr;
407 XGetWindowAttributes (display, window, &attr);
408 XSelectInput (display, window, attr.your_event_mask | mask);
412 check_manager_window (XSettingsClient *client)
414 if (client->manager_window && client->watch)
415 client->watch (client->manager_window, False, 0, client->cb_data);
418 client->grab (client->display);
420 XGrabServer (client->display);
422 client->manager_window = XGetSelectionOwner (client->display,
423 client->selection_atom);
424 if (client->manager_window)
425 XSelectInput (client->display, client->manager_window,
426 PropertyChangeMask | StructureNotifyMask);
429 client->ungrab (client->display);
431 XUngrabServer (client->display);
433 XFlush (client->display);
435 if (client->manager_window && client->watch)
437 if (!client->watch (client->manager_window, True,
438 PropertyChangeMask | StructureNotifyMask,
441 /* Inability to watch the window probably means that it was destroyed
444 client->manager_window = None;
450 read_settings (client);
454 xsettings_client_new (Display *display,
456 XSettingsNotifyFunc notify,
457 XSettingsWatchFunc watch,
459 XSettingsGrabFunc grab,
460 XSettingsGrabFunc ungrab)
462 XSettingsClient *client;
467 client = malloc (sizeof *client);
471 client->display = display;
472 client->screen = screen;
473 client->notify = notify;
474 client->watch = watch;
475 client->cb_data = cb_data;
477 client->ungrab = ungrab;
478 client->manager_window = None;
479 client->settings = NULL;
481 sprintf(buffer, "_XSETTINGS_S%d", screen);
482 atom_names[0] = buffer;
483 atom_names[1] = "_XSETTINGS_SETTINGS";
484 atom_names[2] = "MANAGER";
486 XInternAtoms (display, atom_names, 3, False, atoms);
488 client->selection_atom = atoms[0];
489 client->xsettings_atom = atoms[1];
490 client->manager_atom = atoms[2];
492 /* Select on StructureNotify so we get MANAGER events
494 add_events (display, RootWindow (display, screen), StructureNotifyMask);
497 client->watch (RootWindow (display, screen), True, StructureNotifyMask,
500 check_manager_window (client);
507 xsettings_client_set_grab_func (XSettingsClient *client,
508 XSettingsGrabFunc grab)
514 xsettings_client_set_ungrab_func (XSettingsClient *client,
515 XSettingsGrabFunc ungrab)
517 client->ungrab = ungrab;
521 xsettings_client_destroy (XSettingsClient *client)
524 client->watch (RootWindow (client->display, client->screen),
525 False, 0, client->cb_data);
526 if (client->manager_window && client->watch)
527 client->watch (client->manager_window, False, 0, client->cb_data);
529 if (client->settings)
530 g_hash_table_unref (client->settings);
534 const XSettingsSetting *
535 xsettings_client_get_setting (XSettingsClient *client,
538 return g_hash_table_lookup (client->settings, name);
542 xsettings_client_process_event (XSettingsClient *client,
545 /* The checks here will not unlikely cause us to reread
546 * the properties from the manager window a number of
547 * times when the manager changes from A->B. But manager changes
548 * are going to be pretty rare.
550 if (xev->xany.window == RootWindow (client->display, client->screen))
552 if (xev->xany.type == ClientMessage &&
553 xev->xclient.message_type == client->manager_atom &&
554 xev->xclient.data.l[1] == client->selection_atom)
556 check_manager_window (client);
560 else if (xev->xany.window == client->manager_window)
562 if (xev->xany.type == DestroyNotify)
564 check_manager_window (client);
565 /* let GDK do its cleanup */
568 else if (xev->xany.type == PropertyNotify)
570 read_settings (client);