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"
28 #include <gdk/x11/gdkx11display.h>
29 #include <gdk/x11/gdkx11screen.h>
30 #include <gdk/x11/gdkx11window.h>
31 #include <gdk/x11/gdkscreen-x11.h>
33 #include <gdkinternals.h>
38 #include <X11/Xmd.h> /* For CARD16 */
40 #include "gdksettings.c"
42 typedef struct _XSettingsBuffer XSettingsBuffer;
44 struct _XSettingsBuffer
52 struct _XSettingsClient
57 Window manager_window;
62 GHashTable *settings; /* string => XSettingsSetting */
66 gdk_xsettings_notify (const char *name,
67 XSettingsAction action,
68 XSettingsSetting *setting,
72 GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
74 if (x11_screen->xsettings_in_init)
77 new_event.type = GDK_SETTING;
78 new_event.setting.window = gdk_screen_get_root_window (screen);
79 new_event.setting.send_event = FALSE;
80 new_event.setting.name = (char*) gdk_from_xsettings_name (name);
82 if (!new_event.setting.name)
87 case XSETTINGS_ACTION_NEW:
88 new_event.setting.action = GDK_SETTING_ACTION_NEW;
90 case XSETTINGS_ACTION_CHANGED:
91 new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
93 case XSETTINGS_ACTION_DELETED:
94 new_event.setting.action = GDK_SETTING_ACTION_DELETED;
98 gdk_event_put (&new_event);
102 notify_changes (XSettingsClient *client,
103 GHashTable *old_list)
106 XSettingsSetting *setting, *old_setting;
109 if (client->settings != NULL)
111 g_hash_table_iter_init (&iter, client->settings);
112 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer*) &setting))
114 old_setting = old_list ? g_hash_table_lookup (old_list, name) : NULL;
116 if (old_setting == NULL)
117 gdk_xsettings_notify (name, XSETTINGS_ACTION_NEW, setting, client->screen);
118 else if (!xsettings_setting_equal (setting, old_setting))
119 gdk_xsettings_notify (name, XSETTINGS_ACTION_CHANGED, setting, client->screen);
121 /* remove setting from old_list */
122 if (old_setting != NULL)
123 g_hash_table_remove (old_list, name);
127 if (old_list != NULL)
129 /* old_list now contains only deleted settings */
130 g_hash_table_iter_init (&iter, old_list);
131 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer*) &old_setting))
132 gdk_xsettings_notify (name, XSETTINGS_ACTION_DELETED, NULL, client->screen);
136 #define BYTES_LEFT(buffer) ((buffer)->data + (buffer)->len - (buffer)->pos)
138 #define return_if_fail_bytes(buffer, n_bytes) G_STMT_START{ \
139 if (BYTES_LEFT (buffer) < (n_bytes)) \
141 g_warning ("Invalid XSETTINGS property (read off end: Expected %u bytes, only %ld left", \
142 (n_bytes), BYTES_LEFT (buffer)); \
148 fetch_card16 (XSettingsBuffer *buffer,
153 return_if_fail_bytes (buffer, 2);
155 x = *(CARD16 *)buffer->pos;
158 if (buffer->byte_order == MSBFirst)
159 *result = GUINT16_FROM_BE (x);
161 *result = GUINT16_FROM_LE (x);
167 fetch_ushort (XSettingsBuffer *buffer,
168 unsigned short *result)
173 r = fetch_card16 (buffer, &x);
181 fetch_card32 (XSettingsBuffer *buffer,
186 return_if_fail_bytes (buffer, 4);
188 x = *(CARD32 *)buffer->pos;
191 if (buffer->byte_order == MSBFirst)
192 *result = GUINT32_FROM_BE (x);
194 *result = GUINT32_FROM_LE (x);
200 fetch_card8 (XSettingsBuffer *buffer,
203 return_if_fail_bytes (buffer, 1);
205 *result = *(CARD8 *)buffer->pos;
211 #define XSETTINGS_PAD(n,m) ((n + m - 1) & (~(m-1)))
214 fetch_string (XSettingsBuffer *buffer,
220 pad_len = XSETTINGS_PAD (length, 4);
221 if (pad_len < length) /* guard against overflow */
223 g_warning ("Invalid XSETTINGS property (overflow in string length)");
227 return_if_fail_bytes (buffer, pad_len);
229 *result = g_strndup ((char *) buffer->pos, length);
230 buffer->pos += pad_len;
236 parse_settings (unsigned char *data,
239 XSettingsBuffer buffer;
240 GHashTable *settings = NULL;
244 XSettingsSetting *setting = NULL;
247 buffer.pos = buffer.data = data;
250 if (!fetch_card8 (&buffer, (unsigned char *)&buffer.byte_order))
253 if (buffer.byte_order != MSBFirst &&
254 buffer.byte_order != LSBFirst)
256 g_warning ("Invalid XSETTINGS property (unknown byte order %u)", buffer.byte_order);
262 if (!fetch_card32 (&buffer, &serial) ||
263 !fetch_card32 (&buffer, &n_entries))
266 GDK_NOTE(SETTINGS, g_print("reading %u settings (serial %u byte order %u)\n", n_entries, serial, buffer.byte_order));
268 for (i = 0; i < n_entries; i++)
274 if (!fetch_card8 (&buffer, &type))
279 if (!fetch_card16 (&buffer, &name_len))
282 setting = g_new (XSettingsSetting, 1);
283 setting->type = XSETTINGS_TYPE_INT; /* No allocated memory */
285 if (!fetch_string (&buffer, name_len, &name) ||
286 /* last change serial (we ignore it) */
287 !fetch_card32 (&buffer, &v_int))
292 case XSETTINGS_TYPE_INT:
293 if (!fetch_card32 (&buffer, &v_int))
296 setting->data.v_int = (INT32)v_int;
297 GDK_NOTE(SETTINGS, g_print(" %s = %d\n", name, (gint) setting->data.v_int));
299 case XSETTINGS_TYPE_STRING:
300 if (!fetch_card32 (&buffer, &v_int) ||
301 !fetch_string (&buffer, v_int, &setting->data.v_string))
304 GDK_NOTE(SETTINGS, g_print(" %s = \"%s\"\n", name, setting->data.v_string));
306 case XSETTINGS_TYPE_COLOR:
307 if (!fetch_ushort (&buffer, &setting->data.v_color.red) ||
308 !fetch_ushort (&buffer, &setting->data.v_color.green) ||
309 !fetch_ushort (&buffer, &setting->data.v_color.blue) ||
310 !fetch_ushort (&buffer, &setting->data.v_color.alpha))
313 GDK_NOTE(SETTINGS, g_print(" %s = #%02X%02X%02X%02X\n", name,
314 setting->data.v_color.alpha, setting->data.v_color.red,
315 setting->data.v_color.green, setting->data.v_color.blue));
318 /* Quietly ignore unknown types */
319 GDK_NOTE(SETTINGS, g_print(" %s = ignored (unknown type %u)\n", name, type));
323 setting->type = type;
325 if (settings == NULL)
326 settings = g_hash_table_new_full (g_str_hash, g_str_equal,
328 (GDestroyNotify) xsettings_setting_free);
330 if (g_hash_table_lookup (settings, name) != NULL)
332 g_warning ("Invalid XSETTINGS property (Duplicate entry for '%s')", name);
336 g_hash_table_insert (settings, name, setting);
346 xsettings_setting_free (setting);
349 g_hash_table_unref (settings);
355 read_settings (XSettingsClient *client)
359 unsigned long n_items;
360 unsigned long bytes_after;
364 GHashTable *old_list = client->settings;
366 client->settings = NULL;
368 if (client->manager_window)
370 gdk_x11_display_error_trap_push (gdk_screen_get_display (client->screen));
371 result = XGetWindowProperty (client->display, client->manager_window,
372 client->xsettings_atom, 0, LONG_MAX,
373 False, client->xsettings_atom,
374 &type, &format, &n_items, &bytes_after, &data);
375 gdk_x11_display_error_trap_pop_ignored (gdk_screen_get_display (client->screen));
377 if (result == Success && type != None)
379 if (type != client->xsettings_atom)
381 fprintf (stderr, "Invalid type for XSETTINGS property");
383 else if (format != 8)
385 fprintf (stderr, "Invalid format for XSETTINGS property %d", format);
388 client->settings = parse_settings (data, n_items);
394 notify_changes (client, old_list);
396 g_hash_table_unref (old_list);
400 add_events (Display *display,
404 XWindowAttributes attr;
406 XGetWindowAttributes (display, window, &attr);
407 XSelectInput (display, window, attr.your_event_mask | mask);
411 gdk_xsettings_watch (Window window,
417 check_manager_window (XSettingsClient *client)
419 if (client->manager_window)
420 gdk_xsettings_watch (client->manager_window, False, 0, client->screen);
422 gdk_x11_display_grab (gdk_screen_get_display (client->screen));
424 client->manager_window = XGetSelectionOwner (client->display,
425 client->selection_atom);
426 if (client->manager_window)
427 XSelectInput (client->display, client->manager_window,
428 PropertyChangeMask | StructureNotifyMask);
430 gdk_x11_display_ungrab (gdk_screen_get_display (client->screen));
432 XFlush (client->display);
434 if (client->manager_window)
436 if (!gdk_xsettings_watch (client->manager_window, True,
437 PropertyChangeMask | StructureNotifyMask,
440 /* Inability to watch the window probably means that it was destroyed
443 client->manager_window = None;
448 read_settings (client);
451 static GdkFilterReturn
452 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
456 GdkScreen *screen = data;
457 XSettingsClient *client = GDK_X11_SCREEN (screen)->xsettings_client;
458 XEvent *xev = xevent;
460 /* The checks here will not unlikely cause us to reread
461 * the properties from the manager window a number of
462 * times when the manager changes from A->B. But manager changes
463 * are going to be pretty rare.
465 if (xev->xany.window == gdk_x11_window_get_xid (gdk_screen_get_root_window (screen)))
467 if (xev->xany.type == ClientMessage &&
468 xev->xclient.message_type == client->manager_atom &&
469 xev->xclient.data.l[1] == client->selection_atom)
471 check_manager_window (client);
472 return GDK_FILTER_REMOVE;
475 else if (xev->xany.window == client->manager_window)
477 if (xev->xany.type == DestroyNotify)
479 check_manager_window (client);
480 /* let GDK do its cleanup */
481 return GDK_FILTER_CONTINUE;
483 else if (xev->xany.type == PropertyNotify)
485 read_settings (client);
486 return GDK_FILTER_REMOVE;
490 return GDK_FILTER_CONTINUE;;
494 gdk_xsettings_watch (Window window,
501 gdkwin = gdk_x11_window_lookup_for_display (gdk_screen_get_display (screen), window);
506 g_object_ref (gdkwin);
509 gdkwin = gdk_x11_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
511 /* gdk_window_foreign_new_for_display() can fail and return NULL if the
512 * window has already been destroyed.
518 gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
524 /* gdkwin should not be NULL here, since if starting the watch succeeded
525 * we have a reference on the window. It might mean that the caller didn't
526 * remove the watch when it got a DestroyNotify event. Or maybe the
527 * caller ignored the return value when starting the watch failed.
529 g_warning ("gdk_xsettings_watch_cb(): Couldn't find window to unwatch");
533 gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
534 g_object_unref (gdkwin);
541 xsettings_client_new (GdkScreen *screen)
543 XSettingsClient *client;
548 client = g_new (XSettingsClient, 1);
552 client->screen = screen;
553 client->display = gdk_x11_display_get_xdisplay (gdk_screen_get_display (screen));
554 client->screen = screen;
555 client->manager_window = None;
556 client->settings = NULL;
558 sprintf(buffer, "_XSETTINGS_S%d", gdk_x11_screen_get_screen_number (screen));
559 atom_names[0] = buffer;
560 atom_names[1] = "_XSETTINGS_SETTINGS";
561 atom_names[2] = "MANAGER";
563 XInternAtoms (client->display, atom_names, 3, False, atoms);
565 client->selection_atom = atoms[0];
566 client->xsettings_atom = atoms[1];
567 client->manager_atom = atoms[2];
569 /* Select on StructureNotify so we get MANAGER events
571 add_events (client->display, gdk_x11_window_get_xid (gdk_screen_get_root_window (screen)), StructureNotifyMask);
573 gdk_xsettings_watch (gdk_x11_window_get_xid (gdk_screen_get_root_window (screen)), True, StructureNotifyMask,
576 check_manager_window (client);
582 xsettings_client_destroy (XSettingsClient *client)
584 gdk_xsettings_watch (gdk_x11_window_get_xid (gdk_screen_get_root_window (client->screen)),
585 False, 0, client->screen);
586 if (client->manager_window)
587 gdk_xsettings_watch (client->manager_window, False, 0, client->screen);
589 if (client->settings)
590 g_hash_table_unref (client->settings);
594 const XSettingsSetting *
595 xsettings_client_get_setting (XSettingsClient *client,
598 return g_hash_table_lookup (client->settings, name);
602 xsettings_setting_equal (XSettingsSetting *setting_a,
603 XSettingsSetting *setting_b)
605 if (setting_a->type != setting_b->type)
608 switch (setting_a->type)
610 case XSETTINGS_TYPE_INT:
611 return setting_a->data.v_int == setting_b->data.v_int;
612 case XSETTINGS_TYPE_COLOR:
613 return (setting_a->data.v_color.red == setting_b->data.v_color.red &&
614 setting_a->data.v_color.green == setting_b->data.v_color.green &&
615 setting_a->data.v_color.blue == setting_b->data.v_color.blue &&
616 setting_a->data.v_color.alpha == setting_b->data.v_color.alpha);
617 case XSETTINGS_TYPE_STRING:
618 return strcmp (setting_a->data.v_string, setting_b->data.v_string) == 0;
625 xsettings_setting_free (XSettingsSetting *setting)
627 if (setting->type == XSETTINGS_TYPE_STRING)
628 g_free (setting->data.v_string);