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 of GDK settings name => XSettingsSetting */
66 gdk_xsettings_notify (const char *name,
67 GdkSettingAction 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.action = action;
81 new_event.setting.name = (char*) name;
83 gdk_event_put (&new_event);
87 notify_changes (XSettingsClient *client,
91 XSettingsSetting *setting, *old_setting;
94 if (client->settings != NULL)
96 g_hash_table_iter_init (&iter, client->settings);
97 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer*) &setting))
99 old_setting = old_list ? g_hash_table_lookup (old_list, name) : NULL;
101 if (old_setting == NULL)
102 gdk_xsettings_notify (name, GDK_SETTING_ACTION_NEW, setting, client->screen);
103 else if (!xsettings_setting_equal (setting, old_setting))
104 gdk_xsettings_notify (name, GDK_SETTING_ACTION_CHANGED, setting, client->screen);
106 /* remove setting from old_list */
107 if (old_setting != NULL)
108 g_hash_table_remove (old_list, name);
112 if (old_list != NULL)
114 /* old_list now contains only deleted settings */
115 g_hash_table_iter_init (&iter, old_list);
116 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer*) &old_setting))
117 gdk_xsettings_notify (name, GDK_SETTING_ACTION_DELETED, NULL, client->screen);
121 #define BYTES_LEFT(buffer) ((buffer)->data + (buffer)->len - (buffer)->pos)
123 #define return_if_fail_bytes(buffer, n_bytes) G_STMT_START{ \
124 if (BYTES_LEFT (buffer) < (n_bytes)) \
126 g_warning ("Invalid XSETTINGS property (read off end: Expected %u bytes, only %ld left", \
127 (n_bytes), BYTES_LEFT (buffer)); \
133 fetch_card16 (XSettingsBuffer *buffer,
138 return_if_fail_bytes (buffer, 2);
140 x = *(CARD16 *)buffer->pos;
143 if (buffer->byte_order == MSBFirst)
144 *result = GUINT16_FROM_BE (x);
146 *result = GUINT16_FROM_LE (x);
152 fetch_ushort (XSettingsBuffer *buffer,
153 unsigned short *result)
158 r = fetch_card16 (buffer, &x);
166 fetch_card32 (XSettingsBuffer *buffer,
171 return_if_fail_bytes (buffer, 4);
173 x = *(CARD32 *)buffer->pos;
176 if (buffer->byte_order == MSBFirst)
177 *result = GUINT32_FROM_BE (x);
179 *result = GUINT32_FROM_LE (x);
185 fetch_card8 (XSettingsBuffer *buffer,
188 return_if_fail_bytes (buffer, 1);
190 *result = *(CARD8 *)buffer->pos;
196 #define XSETTINGS_PAD(n,m) ((n + m - 1) & (~(m-1)))
199 fetch_string (XSettingsBuffer *buffer,
205 pad_len = XSETTINGS_PAD (length, 4);
206 if (pad_len < length) /* guard against overflow */
208 g_warning ("Invalid XSETTINGS property (overflow in string length)");
212 return_if_fail_bytes (buffer, pad_len);
214 *result = g_strndup ((char *) buffer->pos, length);
215 buffer->pos += pad_len;
221 parse_settings (unsigned char *data,
224 XSettingsBuffer buffer;
225 GHashTable *settings = NULL;
229 XSettingsSetting *setting = NULL;
231 const char *gdk_name;
233 buffer.pos = buffer.data = data;
236 if (!fetch_card8 (&buffer, (unsigned char *)&buffer.byte_order))
239 if (buffer.byte_order != MSBFirst &&
240 buffer.byte_order != LSBFirst)
242 g_warning ("Invalid XSETTINGS property (unknown byte order %u)", buffer.byte_order);
248 if (!fetch_card32 (&buffer, &serial) ||
249 !fetch_card32 (&buffer, &n_entries))
252 GDK_NOTE(SETTINGS, g_print("reading %u settings (serial %u byte order %u)\n", n_entries, serial, buffer.byte_order));
254 for (i = 0; i < n_entries; i++)
260 if (!fetch_card8 (&buffer, &type))
265 if (!fetch_card16 (&buffer, &name_len))
268 setting = g_new (XSettingsSetting, 1);
269 setting->type = XSETTINGS_TYPE_INT; /* No allocated memory */
271 if (!fetch_string (&buffer, name_len, &x_name) ||
272 /* last change serial (we ignore it) */
273 !fetch_card32 (&buffer, &v_int))
278 case XSETTINGS_TYPE_INT:
279 if (!fetch_card32 (&buffer, &v_int))
282 setting->data.v_int = (INT32)v_int;
283 GDK_NOTE(SETTINGS, g_print(" %s = %d\n", x_name, (gint) setting->data.v_int));
285 case XSETTINGS_TYPE_STRING:
286 if (!fetch_card32 (&buffer, &v_int) ||
287 !fetch_string (&buffer, v_int, &setting->data.v_string))
290 GDK_NOTE(SETTINGS, g_print(" %s = \"%s\"\n", x_name, setting->data.v_string));
292 case XSETTINGS_TYPE_COLOR:
293 if (!fetch_ushort (&buffer, &setting->data.v_color.red) ||
294 !fetch_ushort (&buffer, &setting->data.v_color.green) ||
295 !fetch_ushort (&buffer, &setting->data.v_color.blue) ||
296 !fetch_ushort (&buffer, &setting->data.v_color.alpha))
299 GDK_NOTE(SETTINGS, g_print(" %s = #%02X%02X%02X%02X\n", x_name,
300 setting->data.v_color.alpha, setting->data.v_color.red,
301 setting->data.v_color.green, setting->data.v_color.blue));
304 /* Quietly ignore unknown types */
305 GDK_NOTE(SETTINGS, g_print(" %s = ignored (unknown type %u)\n", x_name, type));
309 setting->type = type;
311 gdk_name = gdk_from_xsettings_name (x_name);
315 if (gdk_name == NULL)
317 GDK_NOTE(SETTINGS, g_print(" ==> unknown to GTK\n"));
321 GDK_NOTE(SETTINGS, g_print(" ==> storing as '%s'\n", gdk_name));
323 if (settings == NULL)
324 settings = g_hash_table_new_full (g_str_hash, g_str_equal,
326 (GDestroyNotify) xsettings_setting_free);
328 if (g_hash_table_lookup (settings, gdk_name) != NULL)
330 g_warning ("Invalid XSETTINGS property (Duplicate entry for '%s')", gdk_name);
334 g_hash_table_insert (settings, (gpointer) gdk_name, setting);
345 xsettings_setting_free (setting);
348 g_hash_table_unref (settings);
356 read_settings (XSettingsClient *client)
360 unsigned long n_items;
361 unsigned long bytes_after;
365 GHashTable *old_list = client->settings;
367 client->settings = NULL;
369 if (client->manager_window)
371 gdk_x11_display_error_trap_push (gdk_screen_get_display (client->screen));
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 gdk_x11_display_error_trap_pop_ignored (gdk_screen_get_display (client->screen));
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 gdk_xsettings_watch (Window window,
417 check_manager_window (XSettingsClient *client)
419 if (client->manager_window)
420 gdk_xsettings_watch (client->manager_window, False, 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, client->screen))
438 /* Inability to watch the window probably means that it was destroyed
441 client->manager_window = None;
446 read_settings (client);
449 static GdkFilterReturn
450 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
454 GdkScreen *screen = data;
455 XSettingsClient *client = GDK_X11_SCREEN (screen)->xsettings_client;
456 XEvent *xev = xevent;
458 /* The checks here will not unlikely cause us to reread
459 * the properties from the manager window a number of
460 * times when the manager changes from A->B. But manager changes
461 * are going to be pretty rare.
463 if (xev->xany.window == gdk_x11_window_get_xid (gdk_screen_get_root_window (screen)))
465 if (xev->xany.type == ClientMessage &&
466 xev->xclient.message_type == client->manager_atom &&
467 xev->xclient.data.l[1] == client->selection_atom)
469 check_manager_window (client);
470 return GDK_FILTER_REMOVE;
473 else if (xev->xany.window == client->manager_window)
475 if (xev->xany.type == DestroyNotify)
477 check_manager_window (client);
478 /* let GDK do its cleanup */
479 return GDK_FILTER_CONTINUE;
481 else if (xev->xany.type == PropertyNotify)
483 read_settings (client);
484 return GDK_FILTER_REMOVE;
488 return GDK_FILTER_CONTINUE;;
492 gdk_xsettings_watch (Window window,
498 gdkwin = gdk_x11_window_lookup_for_display (gdk_screen_get_display (screen), window);
503 g_object_ref (gdkwin);
506 gdkwin = gdk_x11_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
508 /* gdk_window_foreign_new_for_display() can fail and return NULL if the
509 * window has already been destroyed.
515 gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
521 /* gdkwin should not be NULL here, since if starting the watch succeeded
522 * we have a reference on the window. It might mean that the caller didn't
523 * remove the watch when it got a DestroyNotify event. Or maybe the
524 * caller ignored the return value when starting the watch failed.
526 g_warning ("gdk_xsettings_watch_cb(): Couldn't find window to unwatch");
530 gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
531 g_object_unref (gdkwin);
538 xsettings_client_new (GdkScreen *screen)
540 XSettingsClient *client;
545 client = g_new (XSettingsClient, 1);
549 client->screen = screen;
550 client->display = gdk_x11_display_get_xdisplay (gdk_screen_get_display (screen));
551 client->screen = screen;
552 client->manager_window = None;
553 client->settings = NULL;
555 sprintf(buffer, "_XSETTINGS_S%d", gdk_x11_screen_get_screen_number (screen));
556 atom_names[0] = buffer;
557 atom_names[1] = "_XSETTINGS_SETTINGS";
558 atom_names[2] = "MANAGER";
560 XInternAtoms (client->display, atom_names, 3, False, atoms);
562 client->selection_atom = atoms[0];
563 client->xsettings_atom = atoms[1];
564 client->manager_atom = atoms[2];
566 /* Select on StructureNotify so we get MANAGER events
568 add_events (client->display, gdk_x11_window_get_xid (gdk_screen_get_root_window (screen)), StructureNotifyMask);
570 gdk_xsettings_watch (gdk_x11_window_get_xid (gdk_screen_get_root_window (screen)), True, client->screen);
572 check_manager_window (client);
578 xsettings_client_destroy (XSettingsClient *client)
580 gdk_xsettings_watch (gdk_x11_window_get_xid (gdk_screen_get_root_window (client->screen)), False, client->screen);
581 if (client->manager_window)
582 gdk_xsettings_watch (client->manager_window, False, client->screen);
584 if (client->settings)
585 g_hash_table_unref (client->settings);
589 const XSettingsSetting *
590 xsettings_client_get_setting (XSettingsClient *client,
593 return g_hash_table_lookup (client->settings, name);
597 xsettings_setting_equal (XSettingsSetting *setting_a,
598 XSettingsSetting *setting_b)
600 if (setting_a->type != setting_b->type)
603 switch (setting_a->type)
605 case XSETTINGS_TYPE_INT:
606 return setting_a->data.v_int == setting_b->data.v_int;
607 case XSETTINGS_TYPE_COLOR:
608 return (setting_a->data.v_color.red == setting_b->data.v_color.red &&
609 setting_a->data.v_color.green == setting_b->data.v_color.green &&
610 setting_a->data.v_color.blue == setting_b->data.v_color.blue &&
611 setting_a->data.v_color.alpha == setting_b->data.v_color.alpha);
612 case XSETTINGS_TYPE_STRING:
613 return strcmp (setting_a->data.v_string, setting_b->data.v_string) == 0;
620 xsettings_setting_free (XSettingsSetting *setting)
622 if (setting->type == XSETTINGS_TYPE_STRING)
623 g_free (setting->data.v_string);