]> Pileus Git - ~andy/gtk/blob - gdk/x11/xsettings-client.c
Check for selection_atom, not property name atom.
[~andy/gtk] / gdk / x11 / xsettings-client.c
1 /*
2  * Copyright © 2001 Red Hat, Inc.
3  *
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.
13  *
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.
20  *
21  * Author:  Owen Taylor, Red Hat, Inc.
22  */
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <X11/Xlib.h>
29 #include <X11/Xmd.h>            /* For CARD16 */
30
31 #include "xsettings-client.h"
32
33 struct _XSettingsClient
34 {
35   Display *display;
36   int screen;
37   XSettingsNotifyFunc notify;
38   XSettingsWatchFunc watch;
39   void *cb_data;
40
41   Window manager_window;
42   Atom manager_atom;
43   Atom selection_atom;
44   Atom xsettings_atom;
45
46   XSettingsList *settings;
47 };
48
49 static void
50 notify_changes (XSettingsClient *client,
51                 XSettingsList   *old_list)
52 {
53   XSettingsList *old_iter = old_list;
54   XSettingsList *new_iter = client->settings;
55
56   if (!client->notify)
57     return;
58
59   while (old_iter || new_iter)
60     {
61       int cmp;
62       
63       if (old_iter && new_iter)
64         cmp = strcmp (old_iter->setting->name, new_iter->setting->name);
65       else if (old_iter)
66         cmp = -1;
67       else
68         cmp = 1;
69
70       if (cmp < 0)
71         {
72           client->notify (old_iter->setting->name,
73                           XSETTINGS_ACTION_DELETED,
74                           NULL,
75                           client->cb_data);
76         }
77       else if (cmp == 0)
78         {
79           if (!xsettings_setting_equal (old_iter->setting,
80                                         new_iter->setting))
81             client->notify (old_iter->setting->name,
82                             XSETTINGS_ACTION_CHANGED,
83                             new_iter->setting,
84                             client->cb_data);
85         }
86       else
87         {
88           client->notify (new_iter->setting->name,
89                           XSETTINGS_ACTION_NEW,
90                           new_iter->setting,
91                           client->cb_data);
92         }
93
94       if (old_iter)
95         old_iter = old_iter->next;
96       if (new_iter)
97         new_iter = new_iter->next;
98     }
99 }
100
101 static int
102 ignore_errors (Display *display, XErrorEvent *event)
103 {
104   return True;
105 }
106
107 static char local_byte_order = '\0';
108
109 #define BYTES_LEFT(buffer) ((buffer)->data + (buffer)->len - (buffer)->pos)
110
111 static XSettingsResult
112 fetch_card16 (XSettingsBuffer *buffer,
113               CARD16          *result)
114 {
115   CARD16 x;
116
117   if (BYTES_LEFT (buffer) < 2)
118     return XSETTINGS_ACCESS;
119
120   x = *(CARD16 *)buffer->pos;
121   buffer->pos += 2;
122   
123   if (buffer->byte_order == local_byte_order)
124     *result = x;
125   else
126     *result = (x << 8) | (x >> 8);
127
128   return XSETTINGS_SUCCESS;
129 }
130
131 static XSettingsResult
132 fetch_ushort (XSettingsBuffer *buffer,
133               unsigned short  *result) 
134 {
135   CARD16 x;
136   XSettingsResult r;  
137
138   r = fetch_card16 (buffer, &x);
139   if (r == XSETTINGS_SUCCESS)
140     *result = x;
141
142   return r;
143 }
144
145 static XSettingsResult
146 fetch_card32 (XSettingsBuffer *buffer,
147               CARD32          *result)
148 {
149   CARD32 x;
150
151   if (BYTES_LEFT (buffer) < 4)
152     return XSETTINGS_ACCESS;
153
154   x = *(CARD32 *)buffer->pos;
155   buffer->pos += 4;
156   
157   if (buffer->byte_order == local_byte_order)
158     *result = x;
159   else
160     *result = (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
161   
162   return XSETTINGS_SUCCESS;
163 }
164
165 static XSettingsResult
166 fetch_card8 (XSettingsBuffer *buffer,
167              CARD8           *result)
168 {
169   if (BYTES_LEFT (buffer) < 1)
170     return XSETTINGS_ACCESS;
171
172   *result = *(CARD8 *)buffer->pos;
173   buffer->pos += 1;
174
175   return XSETTINGS_SUCCESS;
176 }
177
178 #define XSETTINGS_PAD(n,m) ((n + m - 1) & (~(m-1)))
179
180 static XSettingsList *
181 parse_settings (unsigned char *data,
182                 size_t         len)
183 {
184   XSettingsBuffer buffer;
185   XSettingsResult result = XSETTINGS_SUCCESS;
186   XSettingsList *settings = NULL;
187   CARD32 serial;
188   CARD32 n_entries;
189   CARD32 i;
190   XSettingsSetting *setting = NULL;
191   
192   local_byte_order = xsettings_byte_order ();
193
194   buffer.pos = buffer.data = data;
195   buffer.len = len;
196   
197   result = fetch_card8 (&buffer, &buffer.byte_order);
198   if (buffer.byte_order != MSBFirst &&
199       buffer.byte_order != LSBFirst)
200     {
201       fprintf (stderr, "Invalid byte order in XSETTINGS property\n");
202       result = XSETTINGS_FAILED;
203       goto out;
204     }
205
206   buffer.pos += 3;
207
208   result = fetch_card32 (&buffer, &serial);
209   if (result != XSETTINGS_SUCCESS)
210     goto out;
211
212   result = fetch_card32 (&buffer, &n_entries);
213   if (result != XSETTINGS_SUCCESS)
214     goto out;
215
216   for (i = 0; i < n_entries; i++)
217     {
218       CARD8 type;
219       CARD16 name_len;
220       CARD32 v_int;
221       size_t pad_len;
222       
223       result = fetch_card8 (&buffer, &type);
224       if (result != XSETTINGS_SUCCESS)
225         goto out;
226
227       buffer.pos += 1;
228
229       result = fetch_card16 (&buffer, &name_len);
230       if (result != XSETTINGS_SUCCESS)
231         goto out;
232
233       pad_len = XSETTINGS_PAD(name_len, 4);
234       if (BYTES_LEFT (&buffer) < pad_len)
235         {
236           result = XSETTINGS_ACCESS;
237           goto out;
238         }
239
240       setting = malloc (sizeof *setting);
241       if (!setting)
242         {
243           result = XSETTINGS_NO_MEM;
244           goto out;
245         }
246       setting->type = XSETTINGS_TYPE_INT; /* No allocated memory */
247
248       setting->name = malloc (name_len + 1);
249       if (!setting->name)
250         {
251           result = XSETTINGS_NO_MEM;
252           goto out;
253         }
254
255       memcpy (setting->name, buffer.pos, name_len);
256       setting->name[name_len] = '\0';
257       buffer.pos += pad_len;
258
259       result = fetch_card32 (&buffer, &v_int);
260       if (result != XSETTINGS_SUCCESS)
261         goto out;
262       setting->last_change_serial = v_int;
263
264       switch (type)
265         {
266         case XSETTINGS_TYPE_INT:
267           result = fetch_card32 (&buffer, &v_int);
268           if (result != XSETTINGS_SUCCESS)
269             goto out;
270
271           setting->data.v_int = (INT32)v_int;
272           break;
273         case XSETTINGS_TYPE_STRING:
274           result = fetch_card32 (&buffer, &v_int);
275           if (result != XSETTINGS_SUCCESS)
276             goto out;
277
278           pad_len = XSETTINGS_PAD (v_int, 4);
279           if (v_int + 1 == 0 || /* Guard against wrap-around */
280               BYTES_LEFT (&buffer) < pad_len)
281             {
282               result = XSETTINGS_ACCESS;
283               goto out;
284             }
285
286           setting->data.v_string = malloc (v_int + 1);
287           if (!setting->data.v_string)
288             {
289               result = XSETTINGS_NO_MEM;
290               goto out;
291             }
292           
293           memcpy (setting->data.v_string, buffer.pos, v_int);
294           setting->data.v_string[v_int] = '\0';
295           buffer.pos += pad_len;
296
297           break;
298         case XSETTINGS_TYPE_COLOR:
299           result = fetch_ushort (&buffer, &setting->data.v_color.red);
300           if (result != XSETTINGS_SUCCESS)
301             goto out;
302           result = fetch_ushort (&buffer, &setting->data.v_color.green);
303           if (result != XSETTINGS_SUCCESS)
304             goto out;
305           result = fetch_ushort (&buffer, &setting->data.v_color.blue);
306           if (result != XSETTINGS_SUCCESS)
307             goto out;
308           result = fetch_ushort (&buffer, &setting->data.v_color.alpha);
309           if (result != XSETTINGS_SUCCESS)
310             goto out;
311
312           break;
313         default:
314           /* Quietly ignore unknown types */
315           break;
316         }
317
318       setting->type = type;
319
320       result = xsettings_list_insert (&settings, setting);
321       if (result != XSETTINGS_SUCCESS)
322         goto out;
323
324       setting = NULL;
325     }
326
327  out:
328
329   if (result != XSETTINGS_SUCCESS)
330     {
331       switch (result)
332         {
333         case XSETTINGS_NO_MEM:
334           fprintf(stderr, "Out of memory reading XSETTINGS property\n");
335           break;
336         case XSETTINGS_ACCESS:
337           fprintf(stderr, "Invalid XSETTINGS property (read off end)\n");
338           break;
339         case XSETTINGS_DUPLICATE_ENTRY:
340           fprintf (stderr, "Duplicate XSETTINGS entry for '%s'\n", setting->name);
341         case XSETTINGS_FAILED:
342         case XSETTINGS_SUCCESS:
343         case XSETTINGS_NO_ENTRY:
344           break;
345         }
346
347       if (setting)
348         xsettings_setting_free (setting);
349
350       xsettings_list_free (settings);
351       settings = NULL;
352
353     }
354
355   return settings;
356 }
357
358 static void
359 read_settings (XSettingsClient *client)
360 {
361   Atom type;
362   int format;
363   unsigned long n_items;
364   unsigned long bytes_after;
365   unsigned char *data;
366   int result;
367
368   int (*old_handler) (Display *, XErrorEvent *);
369   
370   XSettingsList *old_list = client->settings;
371
372   client->settings = NULL;
373   
374   old_handler = XSetErrorHandler (ignore_errors);
375   result = XGetWindowProperty (client->display, client->manager_window,
376                                client->xsettings_atom, 0, LONG_MAX,
377                                False, client->xsettings_atom,
378                                &type, &format, &n_items, &bytes_after, &data);
379   XSetErrorHandler (old_handler);
380
381   if (result == Success && type == client->xsettings_atom)
382     {
383       if (format != 8)
384         {
385           fprintf (stderr, "Invalid format for XSETTINGS property %d", format);
386         }
387       else
388         client->settings = parse_settings (data, n_items);
389
390       XFree (data);
391     }
392
393   notify_changes (client, old_list);
394   xsettings_list_free (old_list);
395 }
396
397 static void
398 add_events (Display *display,
399             Window   window,
400             long     mask)
401 {
402   XWindowAttributes attr;
403
404   XGetWindowAttributes (display, window, &attr);
405   XSelectInput (display, window, attr.your_event_mask | mask);
406 }
407
408 static void
409 check_manager_window (XSettingsClient *client)
410 {
411   if (client->manager_window && client->watch)
412     client->watch (client->manager_window, False, 0, client->cb_data);
413   
414   XGrabServer (client->display);
415
416   client->manager_window = XGetSelectionOwner (client->display,
417                                                client->selection_atom);
418   if (client->manager_window)
419     XSelectInput (client->display, client->manager_window,
420                   PropertyChangeMask | StructureNotifyMask);
421
422   XUngrabServer (client->display);
423   XFlush (client->display);
424
425   if (client->manager_window && client->watch)
426     client->watch (client->manager_window, True, 
427                    PropertyChangeMask | StructureNotifyMask,
428                    client->cb_data);
429   
430   read_settings (client);
431 }
432
433 XSettingsClient *
434 xsettings_client_new (Display             *display,
435                       int                  screen,
436                       XSettingsNotifyFunc  notify,
437                       XSettingsWatchFunc   watch,
438                       void                *cb_data)
439 {
440   XSettingsClient *client;
441   char buffer[256];
442
443   client = malloc (sizeof *client);
444   if (!client)
445     return NULL;
446
447   client->display = display;
448   client->screen = screen;
449   client->notify = notify;
450   client->watch = watch;
451   client->cb_data = cb_data;
452   
453   client->manager_window = None;
454   client->settings = NULL;
455
456   sprintf(buffer, "_XSETTINGS_S%d", screen);
457   client->selection_atom = XInternAtom (display, buffer, False);
458   client->xsettings_atom = XInternAtom (display, "_XSETTINGS_SETTINGS", False);
459   client->manager_atom = XInternAtom (display, "MANAGER", False);
460
461   /* Select on StructureNotify so we get MANAGER events
462    */
463   add_events (display, RootWindow (display, screen), StructureNotifyMask);
464
465   if (client->watch)
466     client->watch (RootWindow (display, screen), True, StructureNotifyMask,
467                    client->cb_data);
468
469   check_manager_window (client);
470
471   return client;
472 }
473
474 void
475 xsettings_client_destroy (XSettingsClient *client)
476 {
477   if (client->watch)
478     client->watch (RootWindow (client->display, client->screen),
479                    False, 0, client->cb_data);
480   if (client->manager_window && client->watch)
481     client->watch (client->manager_window, False, 0, client->cb_data);
482   
483   xsettings_list_free (client->settings);
484   free (client);
485 }
486
487 XSettingsResult
488 xsettings_client_get_setting (XSettingsClient   *client,
489                               const char        *name,
490                               XSettingsSetting **setting)
491 {
492   XSettingsSetting *search = xsettings_list_lookup (client->settings, name);
493   if (search)
494     {
495       *setting = xsettings_setting_copy (search);
496       return *setting ? XSETTINGS_SUCCESS : XSETTINGS_NO_MEM;
497     }
498   else
499     return XSETTINGS_NO_ENTRY;
500 }
501
502 Bool
503 xsettings_client_process_event (XSettingsClient *client,
504                                 XEvent          *xev)
505 {
506   /* The checks here will not unlikely cause us to reread
507    * the properties from the manager window a number of
508    * times when the manager changes from A->B. But manager changes
509    * are going to be pretty rare.
510    */
511   if (xev->xany.window == RootWindow (client->display, client->screen))
512     {
513       if (xev->xany.type == ClientMessage &&
514           xev->xclient.message_type == client->manager_atom &&
515           xev->xclient.data.l[1] == client->selection_atom)
516         {
517           check_manager_window (client);
518           return True;
519         }
520     }
521   else if (xev->xany.window == client->manager_window)
522     {
523       if (xev->xany.type == DestroyNotify)
524         {
525           check_manager_window (client);
526           return True;
527         }
528       else if (xev->xany.type == PropertyNotify)
529         {
530           read_settings (client);
531           return True;
532         }
533     }
534   
535   return False;
536 }