summaryrefslogtreecommitdiffstats
path: root/xsettings/xsettings-manager.c
blob: 2741c97b8008dc3553e0f95463be2f981c20cb58 (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
#include <stdio.h>
#include <stdlib.h>

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

#include "xsettings-manager.h"

struct _XSettingsManager
{
  Display *display;
  int screen;

  Window window;
  Atom manager_atom;
  Atom selection_atom;
  Atom xsettings_atom;

  XSettingsTerminateFunc terminate;
  void *cb_data;

  XSettingsList *settings;
  unsigned long serial;
};

XSettingsList *settings;

typedef struct 
{
  Window window;
  Atom timestamp_prop_atom;
} TimeStampInfo;

static Bool
timestamp_predicate (Display *display,
		     XEvent  *xevent,
		     XPointer arg)
{
  TimeStampInfo *info = (TimeStampInfo *)arg;

  if (xevent->type == PropertyNotify &&
      xevent->xproperty.window == info->window &&
      xevent->xproperty.atom == info->timestamp_prop_atom)
    return True;

  return False;
}

/**
 * get_server_time:
 * @display: display from which to get the time
 * @window: a #Window, used for communication with the server.
 *          The window must have PropertyChangeMask in its
 *          events mask or a hang will result.
 * 
 * Routine to get the current X server time stamp. 
 * 
 * Return value: the time stamp.
 **/
static Time
get_server_time (Display *display,
		 Window   window)
{
  unsigned char c = 'a';
  XEvent xevent;
  TimeStampInfo info;

  info.timestamp_prop_atom = XInternAtom  (display, "_TIMESTAMP_PROP", False);
  info.window = window;

  XChangeProperty (display, window,
		   info.timestamp_prop_atom, info.timestamp_prop_atom,
		   8, PropModeReplace, &c, 1);

  XIfEvent (display, &xevent,
	    timestamp_predicate, (XPointer)&info);

  return xevent.xproperty.time;
}

Bool
xsettings_manager_check_running (Display *display,
				 int      screen)
{
  char buffer[256];
  Atom selection_atom;
  
  sprintf(buffer, "_XSETTINGS_S%d", screen);
  selection_atom = XInternAtom (display, buffer, False);

  if (XGetSelectionOwner (display, selection_atom))
    return True;
  else
    return False;
}

XSettingsManager *
xsettings_manager_new (Display                *display,
		       int                     screen,
		       XSettingsTerminateFunc  terminate,
		       void                   *cb_data)
{
  XSettingsManager *manager;
  Time timestamp;
  XClientMessageEvent xev;

  char buffer[256];
  
  manager = malloc (sizeof *manager);
  if (!manager)
    return NULL;

  manager->display = display;
  manager->screen = screen;

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

  manager->terminate = terminate;
  manager->cb_data = cb_data;

  manager->settings = NULL;
  manager->serial = 0;

  manager->window = XCreateSimpleWindow (display,
					 RootWindow (display, screen),
					 0, 0, 10, 10, 0,
					 WhitePixel (display, screen),
					 WhitePixel (display, screen));

  XSelectInput (display, manager->window, PropertyChangeMask);
  timestamp = get_server_time (display, manager->window);

  XSetSelectionOwner (display, manager->selection_atom,
		      manager->window, timestamp);

  /* Check to see if we managed to claim the selection. If not,
   * we treat it as if we got it then immediately lost it
   */

  if (XGetSelectionOwner (display, manager->selection_atom) ==
      manager->window)
    {
      xev.type = ClientMessage;
      xev.window = RootWindow (display, screen);
      xev.message_type = manager->manager_atom;
      xev.format = 32;
      xev.data.l[0] = timestamp;
      xev.data.l[1] = manager->selection_atom;
      xev.data.l[2] = manager->window;
      xev.data.l[3] = 0;	/* manager specific data */
      xev.data.l[4] = 0;	/* manager specific data */
      
      XSendEvent (display, RootWindow (display, screen),
		  False, StructureNotifyMask, (XEvent *)&xev);
    }
  else
    {
      manager->terminate (manager->cb_data);
    }
  
  return manager;
}

void
xsettings_manager_destroy (XSettingsManager *manager)
{
  XDestroyWindow (manager->display, manager->window);
  
  xsettings_list_free (manager->settings);
  free (manager);
}

Window
xsettings_manager_get_window (XSettingsManager *manager)
{
  return manager->window;
}

Bool
xsettings_manager_process_event (XSettingsManager *manager,
				 XEvent           *xev)
{
  if (xev->xany.window == manager->window &&
      xev->xany.type == SelectionClear &&
      xev->xselectionclear.selection == manager->selection_atom)
    {
      manager->terminate (manager->cb_data);
      return True;
    }

  return False;
}

XSettingsResult
xsettings_manager_set_setting (XSettingsManager *manager,
			       XSettingsSetting *setting)
{
  XSettingsSetting *old_setting = xsettings_list_lookup (settings, setting->name);
  XSettingsSetting *new_setting;
  XSettingsResult result;

  if (old_setting)
    {
      if (xsettings_setting_equal (old_setting, setting))
	return XSETTINGS_SUCCESS;

      xsettings_list_delete (&settings, setting->name);
    }

  new_setting = xsettings_setting_copy (setting);
  if (!new_setting)
    return XSETTINGS_NO_MEM;
  
  new_setting->last_change_serial = manager->serial;
  
  result = xsettings_list_insert (&settings, new_setting);

  if (result != XSETTINGS_SUCCESS)
    xsettings_setting_free (new_setting);

  return result;
}

XSettingsResult
xsettings_manager_set_int (XSettingsManager *manager,
			   const char       *name,
			   int               value)
{
  XSettingsSetting setting;

  setting.name = (char *)name;
  setting.type = XSETTINGS_TYPE_INT;
  setting.data.v_int = value;

  return xsettings_manager_set_setting (manager, &setting);
}

XSettingsResult
xsettings_manager_set_string (XSettingsManager *manager,
			      const char       *name,
			      const char       *value)
{
  XSettingsSetting setting;

  setting.name = (char *)name;
  setting.type = XSETTINGS_TYPE_STRING;
  setting.data.v_string = (char *)value;

  return xsettings_manager_set_setting (manager, &setting);
}

XSettingsResult
xsettings_manager_set_color (XSettingsManager *manager,
			     const char       *name,
			     XSettingsColor   *value)
{
  XSettingsSetting setting;

  setting.name = (char *)name;
  setting.type = XSETTINGS_TYPE_COLOR;
  setting.data.v_color = *value;

  return xsettings_manager_set_setting (manager, &setting);
}

size_t
setting_length (XSettingsSetting *setting)
{
  size_t length = 8;	/* type + pad + name-len + last-change-serial */
  length += XSETTINGS_PAD (strlen (setting->name), 4);

  switch (setting->type)
    {
    case XSETTINGS_TYPE_INT:
      length += 4;
      break;
    case XSETTINGS_TYPE_STRING:
      length += 4 + XSETTINGS_PAD (strlen (setting->data.v_string), 4);
      break;
    case XSETTINGS_TYPE_COLOR:
      length += 8;
      break;
    }

  return length;
}

void
setting_store (XSettingsSetting *setting,
	       XSettingsBuffer *buffer)
{
  size_t string_len;
  size_t length;

  *(buffer->pos++) = setting->type;
  *(buffer->pos++) = 0;

  string_len = strlen (setting->name);
  *(CARD16 *)(buffer->pos) = string_len;
  buffer->pos += 2;

  length = XSETTINGS_PAD (string_len, 4);
  memcpy (buffer->pos, setting->name, string_len);
  length -= string_len;
  buffer->pos += string_len;
  
  while (length > 0)
    {
      *(buffer->pos++) = 0;
      length--;
    }

  *(CARD32 *)(buffer->pos) = setting->last_change_serial;
  buffer->pos += 4;

  switch (setting->type)
    {
    case XSETTINGS_TYPE_INT:
      *(CARD32 *)(buffer->pos) = setting->data.v_int;
      buffer->pos += 4;
      break;
    case XSETTINGS_TYPE_STRING:
      string_len = strlen (setting->data.v_string);
      *(CARD32 *)(buffer->pos) = string_len;
      buffer->pos += 4;
      
      length = XSETTINGS_PAD (string_len, 4);
      memcpy (buffer->pos, setting->data.v_string, string_len);
      length -= string_len;
      buffer->pos += string_len;
      
      while (length > 0)
	{
	  *(buffer->pos++) = 0;
	  length--;
	}
      break;
    case XSETTINGS_TYPE_COLOR:
      *(CARD16 *)(buffer->pos) = setting->data.v_color.red;
      *(CARD16 *)(buffer->pos + 2) = setting->data.v_color.green;
      *(CARD16 *)(buffer->pos + 4) = setting->data.v_color.blue;
      *(CARD16 *)(buffer->pos + 6) = setting->data.v_color.alpha;
      buffer->pos += 8;
      break;
    }
}

XSettingsResult
xsettings_manager_notify (XSettingsManager *manager)
{
  XSettingsBuffer buffer;
  XSettingsList *iter;
  int n_settings = 0;

  buffer.len = 12;		/* byte-order + pad + SERIAL + N_SETTINGS */

  iter = settings;
  while (iter)
    {
      buffer.len += setting_length (iter->setting);
      n_settings++;
      iter = iter->next;
    }

  buffer.data = buffer.pos = malloc (buffer.len);
  if (!buffer.data)
    return XSETTINGS_NO_MEM;

  *buffer.pos = xsettings_byte_order ();

  buffer.pos += 4;
  *(CARD32 *)buffer.pos = manager->serial++;
  buffer.pos += 4;
  *(CARD32 *)buffer.pos = n_settings;
  buffer.pos += 4;

  iter = settings;
  while (iter)
    {
      setting_store (iter->setting, &buffer);
      iter = iter->next;
    }

  XChangeProperty (manager->display, manager->window,
		   manager->xsettings_atom, manager->xsettings_atom,
		   8, PropModeReplace, buffer.data, buffer.len);

  free (buffer.data);

  return XSETTINGS_SUCCESS;
}