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