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