summaryrefslogtreecommitdiffstats
path: root/xsettings/xsettings-client.c
blob: 7e1e35642f1a90d0584f7e36d98979457552b9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xmd.h>		/* For CARD16 */

#include "xsettings-client.h"

struct _XSettingsClient
{
  Display *display;
  int screen;
  XSettingsNotifyFunc notify;
  XSettingsWatchFunc watch;
  void *cb_data;

  Window manager_window;
  Atom manager_atom;
  Atom selection_atom;
  Atom xsettings_atom;

  XSettingsList *settings;
};

static void
notify_changes (XSettingsClient *client,
		XSettingsList   *old_list)
{
  XSettingsList *old_iter = old_list;
  XSettingsList *new_iter = client->settings;

  if (!client->notify)
    return;

  while (old_iter || new_iter)
    {
      int cmp;
      
      if (old_iter && new_iter)
	cmp = strcmp (old_iter->setting->name, new_iter->setting->name);
      else if (old_iter)
	cmp = -1;
      else
	cmp = 1;

      if (cmp < 0)
	{
	  client->notify (old_iter->setting->name,
			  XSETTINGS_ACTION_DELETED,
			  NULL,
			  client->cb_data);
	}
      else if (cmp == 0)
	{
	  if (!xsettings_setting_equal (old_iter->setting,
					new_iter->setting))
	    client->notify (old_iter->setting->name,
			    XSETTINGS_ACTION_CHANGED,
			    new_iter->setting,
			    client->cb_data);
	}
      else
	{
	  client->notify (new_iter->setting->name,
			  XSETTINGS_ACTION_NEW,
			  new_iter->setting,
			  client->cb_data);
	}

      if (old_iter)
	old_iter = old_iter->next;
      if (new_iter)
	new_iter = new_iter->next;
    }
}

static int
ignore_errors (Display *display, XErrorEvent *event)
{
  return True;
}

static char local_byte_order = '\0';

#define BYTES_LEFT(buffer) ((buffer)->data + (buffer)->len - (buffer)->pos)

static XSettingsResult
fetch_card16 (XSettingsBuffer *buffer,
	      CARD16          *result)
{
  CARD16 x;

  if (BYTES_LEFT (buffer) < 2)
    return XSETTINGS_ACCESS;

  x = *(CARD16 *)buffer->pos;
  buffer->pos += 2;
  
  if (buffer->byte_order == local_byte_order)
    *result = x;
  else
    *result = (x << 8) | (x >> 8);

  return XSETTINGS_SUCCESS;
}

static XSettingsResult
fetch_ushort (XSettingsBuffer *buffer,
	      unsigned short  *result) 
{
  CARD16 x;
  XSettingsResult r;  

  r = fetch_card16 (buffer, &x);
  if (r == XSETTINGS_SUCCESS)
    *result = x;

  return r;
}

static XSettingsResult
fetch_card32 (XSettingsBuffer *buffer,
	      CARD32          *result)
{
  CARD32 x;

  if (BYTES_LEFT (buffer) < 4)
    return XSETTINGS_ACCESS;

  x = *(CARD32 *)buffer->pos;
  buffer->pos += 4;
  
  if (buffer->byte_order == local_byte_order)
    *result = x;
  else
    *result = (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
  
  return XSETTINGS_SUCCESS;
}

static XSettingsResult
fetch_card8 (XSettingsBuffer *buffer,
	     CARD8           *result)
{
  if (BYTES_LEFT (buffer) < 1)
    return XSETTINGS_ACCESS;

  *result = *(CARD32 *)buffer->pos;
  buffer->pos += 1;

  return XSETTINGS_SUCCESS;
}

#define XSETTINGS_PAD(n,m) ((n + m - 1) & (~(m-1)))

static XSettingsList *
parse_settings (unsigned char *data,
		size_t         len)
{
  XSettingsBuffer buffer;
  XSettingsResult result = XSETTINGS_SUCCESS;
  XSettingsList *settings = NULL;
  CARD32 serial;
  CARD32 n_entries;
  CARD32 i;
  XSettingsSetting *setting;
  
  local_byte_order = xsettings_byte_order ();

  buffer.pos = buffer.data = data;
  buffer.len = len;
  
  result = fetch_card8 (&buffer, &buffer.byte_order);
  if (buffer.byte_order != MSBFirst &&
      buffer.byte_order != LSBFirst)
    {
      fprintf (stderr, "Invalid byte order in XSETTINGS property\n");
      result = XSETTINGS_FAILED;
      goto out;
    }

  buffer.pos += 3;

  result = fetch_card32 (&buffer, &serial);
  if (result != XSETTINGS_SUCCESS)
    goto out;

  result = fetch_card32 (&buffer, &n_entries);
  if (result != XSETTINGS_SUCCESS)
    goto out;

  for (i = 0; i < n_entries; i++)
    {
      CARD8 type;
      CARD16 name_len;
      CARD32 v_int;
      size_t pad_len;
      
      result = fetch_card8 (&buffer, &type);
      if (result != XSETTINGS_SUCCESS)
	goto out;

      buffer.pos += 1;

      result = fetch_card16 (&buffer, &name_len);
      if (result != XSETTINGS_SUCCESS)
	goto out;

      pad_len = XSETTINGS_PAD(name_len, 4);
      if (BYTES_LEFT (&buffer) < pad_len)
	{
	  result = XSETTINGS_ACCESS;
	  goto out;
	}

      setting = malloc (sizeof *setting);
      if (!setting)
	{
	  result = XSETTINGS_NO_MEM;
	  goto out;
	}
      setting->type = XSETTINGS_TYPE_INT; /* No allocated memory */

      setting->name = malloc (name_len + 1);
      if (!setting->name)
	{
	  result = XSETTINGS_NO_MEM;
	  goto out;
	}

      memcpy (setting->name, buffer.pos, name_len);
      setting->name[name_len] = '\0';
      buffer.pos += pad_len;

      result = fetch_card32 (&buffer, &v_int);
      if (result != XSETTINGS_SUCCESS)
	goto out;
      setting->last_change_serial = v_int;

      switch (type)
	{
	case XSETTINGS_TYPE_INT:
	  result = fetch_card32 (&buffer, &v_int);
	  if (result != XSETTINGS_SUCCESS)
	    goto out;

	  setting->data.v_int = (INT32)v_int;
	  break;
	case XSETTINGS_TYPE_STRING:
	  result = fetch_card32 (&buffer, &v_int);
	  if (result != XSETTINGS_SUCCESS)
	    goto out;

	  pad_len = XSETTINGS_PAD (v_int, 4);
	  if (v_int + 1 == 0 || /* Guard against wrap-around */
	      BYTES_LEFT (&buffer) < pad_len)
	    {
	      result = XSETTINGS_ACCESS;
	      goto out;
	    }

	  setting->data.v_string = malloc (v_int + 1);
	  if (!setting->data.v_string)
	    {
	      result = XSETTINGS_NO_MEM;
	      goto out;
	    }
	  
	  memcpy (setting->data.v_string, buffer.pos, v_int);
	  setting->data.v_string[v_int] = '\0';
	  buffer.pos += pad_len;

	  break;
	case XSETTINGS_TYPE_COLOR:
	  result = fetch_ushort (&buffer, &setting->data.v_color.red);
	  if (result != XSETTINGS_SUCCESS)
	    goto out;
	  result = fetch_ushort (&buffer, &setting->data.v_color.green);
	  if (result != XSETTINGS_SUCCESS)
	    goto out;
	  result = fetch_ushort (&buffer, &setting->data.v_color.blue);
	  if (result != XSETTINGS_SUCCESS)
	    goto out;
	  result = fetch_ushort (&buffer, &setting->data.v_color.alpha);
	  if (result != XSETTINGS_SUCCESS)
	    goto out;

	  break;
	default:
	  /* Quietly ignore unknown types */
	  break;
	}

      setting->type = type;

      result = xsettings_list_insert (&settings, setting);
      if (result != XSETTINGS_SUCCESS)
	goto out;

      setting = NULL;
    }

 out:

  if (result != XSETTINGS_SUCCESS)
    {
      switch (result)
	{
	case XSETTINGS_NO_MEM:
	  fprintf(stderr, "Out of memory reading XSETTINGS property\n");
	  break;
	case XSETTINGS_ACCESS:
	  fprintf(stderr, "Invalid XSETTINGS property (read off end)\n");
	  break;
	case XSETTINGS_DUPLICATE_ENTRY:
	  fprintf (stderr, "Duplicate XSETTINGS entry for '%s'\n", setting->name);
	case XSETTINGS_FAILED:
	case XSETTINGS_SUCCESS:
	case XSETTINGS_NO_ENTRY:
	  break;
	}

      if (setting)
	xsettings_setting_free (setting);

      xsettings_list_free (settings);
      settings = NULL;

    }

  return settings;
}

static void
read_settings (XSettingsClient *client)
{
  Atom type;
  int format;
  unsigned long n_items;
  unsigned long bytes_after;
  unsigned char *data;
  int result;

  int (*old_handler) (Display *, XErrorEvent *);
  
  XSettingsList *old_list = client->settings;

  client->settings = NULL;
  
  old_handler = XSetErrorHandler (ignore_errors);
  result = XGetWindowProperty (client->display, client->manager_window,
			       client->xsettings_atom, 0, LONG_MAX,
			       False, client->xsettings_atom,
			       &type, &format, &n_items, &bytes_after, &data);
  XSetErrorHandler (old_handler);

  if (result == Success && type == client->xsettings_atom)
    {
      if (format != 8)
	{
	  fprintf (stderr, "Invalid format for XSETTINGS property %d", format);
	}
      else
	client->settings = parse_settings (data, n_items);

      XFree (data);
    }

  notify_changes (client, old_list);
  xsettings_list_free (old_list);
}

static void
add_events (Display *display,
	    Window   window,
	    long     mask)
{
  XWindowAttributes attr;

  XGetWindowAttributes (display, window, &attr);
  XSelectInput (display, window, attr.your_event_mask | mask);
}

static void
check_manager_window (XSettingsClient *client)
{
  if (client->manager_window && client->watch)
    client->watch (client->manager_window, False, 0, client->cb_data);
  
  XGrabServer (client->display);

  client->manager_window = XGetSelectionOwner (client->display,
					       client->selection_atom);
  if (client->manager_window)
    XSelectInput (client->display, client->manager_window,
		  PropertyChangeMask | StructureNotifyMask);

  XUngrabServer (client->display);
  XFlush (client->display);

  if (client->manager_window && client->watch)
    client->watch (client->manager_window, True, 
		   PropertyChangeMask | StructureNotifyMask,
		   client->cb_data);
  
  read_settings (client);
}

XSettingsClient *
xsettings_client_new (Display             *display,
		      int                  screen,
		      XSettingsNotifyFunc  notify,
		      XSettingsWatchFunc   watch,
		      void                *cb_data)
{
  XSettingsClient *client;
  char buffer[256];

  client = malloc (sizeof *client);
  if (!client)
    return NULL;

  client->display = display;
  client->screen = screen;
  client->notify = notify;
  client->watch = watch;
  client->cb_data = cb_data;
  
  client->manager_window = None;
  client->settings = NULL;

  sprintf(buffer, "_XSETTINGS_S%d", screen);
  client->selection_atom = XInternAtom (display, buffer, False);
  client->xsettings_atom = XInternAtom (display, "_XSETTINGS_SETTINGS", False);
  client->manager_atom = XInternAtom (display, "MANAGER", False);

  /* Select on StructureNotify so we get MANAGER events
   */
  add_events (display, RootWindow (display, screen), StructureNotifyMask);

  if (client->watch)
    client->watch (RootWindow (display, screen), True, StructureNotifyMask,
		   client->cb_data);

  check_manager_window (client);

  return client;
}

void
xsettings_client_destroy (XSettingsClient *client)
{
  if (client->watch)
    client->watch (RootWindow (client->display, client->screen),
		   False, 0, client->cb_data);
  if (client->manager_window && client->watch)
    client->watch (client->manager_window, False, 0, client->cb_data);
  
  xsettings_list_free (client->settings);
  free (client);
}

XSettingsResult
xsettings_client_get_setting (XSettingsClient   *client,
			      const char        *name,
			      XSettingsSetting **setting)
{
  XSettingsSetting *search = xsettings_list_lookup (client->settings, name);
  if (search)
    {
      *setting = xsettings_setting_copy (search);
      return *setting ? XSETTINGS_SUCCESS : XSETTINGS_NO_MEM;
    }
  else
    return XSETTINGS_NO_ENTRY;
}

Bool
xsettings_client_process_event (XSettingsClient *client,
				XEvent          *xev)
{
  /* The checks here will not unlikely cause us to reread
   * the properties from the manager window a number of
   * times when the manager changes from A->B. But manager changes
   * are going to be pretty rare.
   */
  if (xev->xany.window == RootWindow (client->display, client->screen))
    {
      if (xev->xany.type == ClientMessage &&
	  xev->xclient.message_type == client->manager_atom)
	{
	  check_manager_window (client);
	  return True;
	}
    }
  else if (xev->xany.window == client->manager_window)
    {
      if (xev->xany.type == DestroyNotify)
	{
	  check_manager_window (client);
	  return True;
	}
      else if (xev->xany.type == PropertyNotify)
	{
	  read_settings (client);
	  return True;
	}
    }
  
  return False;
}