Move new lxsession with built-in settings daemon to trunk.
[lxde/lxsession.git] / lxsession / xsettings-common.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 "string.h"
24 #include "stdlib.h"
25
26 #include <X11/Xlib.h>
27 #include <X11/Xmd.h> /* For CARD32 */
28
29 #include "xsettings-common.h"
30
31 XSettingsSetting *
32 xsettings_setting_copy (XSettingsSetting *setting)
33 {
34 XSettingsSetting *result;
35 size_t str_len;
36
37 result = malloc (sizeof *result);
38 if (!result)
39 return NULL;
40
41 str_len = strlen (setting->name);
42 result->name = malloc (str_len + 1);
43 if (!result->name)
44 goto err;
45
46 memcpy (result->name, setting->name, str_len + 1);
47
48 result->type = setting->type;
49
50 switch (setting->type)
51 {
52 case XSETTINGS_TYPE_INT:
53 result->data.v_int = setting->data.v_int;
54 break;
55 case XSETTINGS_TYPE_COLOR:
56 result->data.v_color = setting->data.v_color;
57 break;
58 case XSETTINGS_TYPE_STRING:
59 str_len = strlen (setting->data.v_string);
60 result->data.v_string = malloc (str_len + 1);
61 if (!result->data.v_string)
62 goto err;
63
64 memcpy (result->data.v_string, setting->data.v_string, str_len + 1);
65 break;
66 }
67
68 result->last_change_serial = setting->last_change_serial;
69
70 return result;
71
72 err:
73 if (result->name)
74 free (result->name);
75 free (result);
76
77 return NULL;
78 }
79
80 XSettingsList *
81 xsettings_list_copy (XSettingsList *list)
82 {
83 XSettingsList *new = NULL;
84 XSettingsList *old_iter = list;
85 XSettingsList *new_iter = NULL;
86
87 while (old_iter)
88 {
89 XSettingsList *new_node;
90
91 new_node = malloc (sizeof *new_node);
92 if (!new_node)
93 goto error;
94
95 new_node->setting = xsettings_setting_copy (old_iter->setting);
96 if (!new_node->setting)
97 {
98 free (new_node);
99 goto error;
100 }
101
102 if (new_iter)
103 new_iter->next = new_node;
104 else
105 new = new_node;
106
107 new_iter = new_node;
108
109 old_iter = old_iter->next;
110 }
111
112 return new;
113
114 error:
115 xsettings_list_free (new);
116 return NULL;
117 }
118
119 int
120 xsettings_setting_equal (XSettingsSetting *setting_a,
121 XSettingsSetting *setting_b)
122 {
123 if (setting_a->type != setting_b->type)
124 return 0;
125
126 if (strcmp (setting_a->name, setting_b->name) != 0)
127 return 0;
128
129 switch (setting_a->type)
130 {
131 case XSETTINGS_TYPE_INT:
132 return setting_a->data.v_int == setting_b->data.v_int;
133 case XSETTINGS_TYPE_COLOR:
134 return (setting_a->data.v_color.red == setting_b->data.v_color.red &&
135 setting_a->data.v_color.green == setting_b->data.v_color.green &&
136 setting_a->data.v_color.blue == setting_b->data.v_color.blue &&
137 setting_a->data.v_color.alpha == setting_b->data.v_color.alpha);
138 case XSETTINGS_TYPE_STRING:
139 return strcmp (setting_a->data.v_string, setting_b->data.v_string) == 0;
140 }
141
142 return 0;
143 }
144
145 void
146 xsettings_setting_free (XSettingsSetting *setting)
147 {
148 if (setting->type == XSETTINGS_TYPE_STRING)
149 free (setting->data.v_string);
150
151 if (setting->name)
152 free (setting->name);
153
154 free (setting);
155 }
156
157 void
158 xsettings_list_free (XSettingsList *list)
159 {
160 while (list)
161 {
162 XSettingsList *next = list->next;
163
164 xsettings_setting_free (list->setting);
165 free (list);
166
167 list = next;
168 }
169 }
170
171 XSettingsResult
172 xsettings_list_insert (XSettingsList **list,
173 XSettingsSetting *setting)
174 {
175 XSettingsList *node;
176 XSettingsList *iter;
177 XSettingsList *last = NULL;
178
179 node = malloc (sizeof *node);
180 if (!node)
181 return XSETTINGS_NO_MEM;
182 node->setting = setting;
183
184 iter = *list;
185 while (iter)
186 {
187 int cmp = strcmp (setting->name, iter->setting->name);
188
189 if (cmp < 0)
190 break;
191 else if (cmp == 0)
192 {
193 free (node);
194 return XSETTINGS_DUPLICATE_ENTRY;
195 }
196
197 last = iter;
198 iter = iter->next;
199 }
200
201 if (last)
202 last->next = node;
203 else
204 *list = node;
205
206 node->next = iter;
207
208 return XSETTINGS_SUCCESS;
209 }
210
211 XSettingsResult
212 xsettings_list_delete (XSettingsList **list,
213 const char *name)
214 {
215 XSettingsList *iter;
216 XSettingsList *last = NULL;
217
218 iter = *list;
219 while (iter)
220 {
221 if (strcmp (name, iter->setting->name) == 0)
222 {
223 if (last)
224 last->next = iter->next;
225 else
226 *list = iter->next;
227
228 xsettings_setting_free (iter->setting);
229 free (iter);
230
231 return XSETTINGS_SUCCESS;
232 }
233
234 last = iter;
235 iter = iter->next;
236 }
237
238 return XSETTINGS_FAILED;
239 }
240
241 XSettingsSetting *
242 xsettings_list_lookup (XSettingsList *list,
243 const char *name)
244 {
245 XSettingsList *iter;
246
247 iter = list;
248 while (iter)
249 {
250 if (strcmp (name, iter->setting->name) == 0)
251 return iter->setting;
252
253 iter = iter->next;
254 }
255
256 return NULL;
257 }
258
259 char
260 xsettings_byte_order (void)
261 {
262 CARD32 myint = 0x01020304;
263 return (*(char *)&myint == 1) ? MSBFirst : LSBFirst;
264 }