2 * Copyright © 2009-10 Sam Thursfield
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Sam Thursfield <ssssam@gmail.com>
20 /* GRegistryBackend implementation notes:
22 * - All settings are stored under the path:
23 * HKEY_CURRENT_USER\Software\GSettings\
24 * This means all settings are per-user. Permissions and system-wide
25 * defaults are not implemented and will probably always be out of scope of
26 * the Windows port of GLib.
28 * - The registry type system is limited. Most GVariant types are stored as
29 * literals via g_variant_print/parse(). Strings are stored without the
30 * quotes that GVariant requires. Integer types are stored as native
31 * REG_DWORD or REG_QWORD. The REG_MULTI_SZ (string array) type could be
32 * used to avoid flattening container types.
34 * - Notifications are handled; the change event is watched for in a separate
35 * thread (Windows does not provide a callback API) which sends them with
36 * g_idle_add to the GLib main loop. The threading is done using Windows
37 * API functions, so there is no dependence on GThread.
39 * - Windows doesn't tell us which value has changed. This means we have to
40 * maintain a cache of every stored value so we can play spot the
41 * difference. This should not be a performance issue because if you are
42 * storing thousands of values in GSettings, you are probably using it
45 * - The cache stores the value as a registry type. Because many variants are
46 * stored as string representations, values which have changed equality but
47 * not equivalence may trigger spurious change notifications. GSettings
48 * users must already deal with this possibility and converting all data to
49 * GVariant values would be more effort.
51 * - Because we have to cache every registry value locally, reads are done
52 * from the cache rather than directly from the registry. Writes update
53 * both. This means that the backend will not work if the watch thread is
54 * not running. A GSettings object always subscribes to changes so we can
55 * be sure that the watch thread will be running, but if for some reason
56 * the backend is being used directly you should bear that in mind.
58 * - The registry is totally user-editable, so we are very forgiving about
59 * errors in the data we get.
61 * - The registry uses backslashes as path separators. GSettings keys only
62 * allow [A-Za-z\-] so no escaping is needed. No attempt is made to solve
63 * clashes between keys differing only in case.
65 * - RegCreateKeyA is used - Windows can also handle UTF16LE strings.
66 * GSettings doesn't pay any attention to encoding, so by using ANSI we
67 * hopefully avoid passing any invalid Unicode.
69 * - The Windows registry has the following limitations: a key may not exceed
70 * 255 characters, an entry's value may not exceed 16,383 characters, and
71 * all the values of a key may not exceed 65,535 characters.
74 * * in GSettings, a 'key' is eg. /desktop/gnome/background/primary-color
75 * * in the registry, the 'key' is path, which contains some 'values'.
76 * * in this file, any GSettings key is a 'key', while a registry key is
77 * termed a 'path', which contains 'values'.
79 * - My set of tests for this backend are currently at:
80 * http://gitorious.org/gsettings-gtk/gsettings-test.git
82 * - There is an undocumented function in ntdll.dll which might be more
83 * than RegNotifyChangeKeyValue(), NtNotifyChangeKey:
84 * http://source.winehq.org/source/dlls/ntdll/reg.c#L618
85 * http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/Key/NtNotifyChangeKey.html
87 * - If updating the cache ever becomes a performance issue it may make sense
88 * to use a red-black tree, but I don't currently think it's worth the time
93 #include "gregistrysettingsbackend.h"
94 #include "gsimplepermission.h"
95 #include "gsettingsbackend.h"
96 #include "giomodule.h"
102 /* GSettings' limit */
103 #define MAX_KEY_NAME_LENGTH 32
105 /* Testing (on Windows XP SP3) shows that WaitForMultipleObjects fails with
106 * "The parameter is incorrect" after 64 watches. We need one for the
107 * message_sent cond, which is allowed for in the way the watches_remaining
110 #define MAX_WATCHES 64
112 /* A watch on one registry path and its subkeys */
122 /* Simple message passing for the watch thread. Not enough traffic to
128 WATCH_THREAD_ADD_WATCH,
129 WATCH_THREAD_REMOVE_WATCH,
131 } WatchThreadMessageType;
135 WatchThreadMessageType type;
137 } WatchThreadMessage;
142 GSettingsBackend *owner;
145 /* Details of the things we are watching. */
146 int watches_remaining;
147 GPtrArray *events, *handles, *prefixes, *cache_nodes;
149 /* Communication with the main thread. Only one message is stored at a time,
150 * to make sure that messages are acknowledged before being overwritten we
151 * create two events - one is signalled when a new message is set, the
152 * other is signalled by the thread when it has processed the message.
154 WatchThreadMessage message;
155 CRITICAL_SECTION *message_lock;
156 HANDLE message_sent_event, message_received_event;
160 #define G_TYPE_REGISTRY_BACKEND (g_registry_backend_get_type ())
161 #define G_REGISTRY_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
162 G_TYPE_REGISTRY_BACKEND, GRegistryBackend))
163 #define G_IS_REGISTRY_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
164 G_TYPE_REGISTRY_BACKEND))
167 typedef GSettingsBackendClass GRegistryBackendClass;
170 GSettingsBackend parent_instance;
174 /* A stored copy of the whole tree being watched. When we receive a change notification
175 * we have to check against this to see what has changed ... every time ...*/
176 CRITICAL_SECTION *cache_lock;
179 WatchThreadState *watch;
182 G_DEFINE_TYPE_WITH_CODE (GRegistryBackend,
184 G_TYPE_SETTINGS_BACKEND,
185 g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
186 g_define_type_id, "registry", 90))
189 /**********************************************************************************
191 **********************************************************************************/
195 trace (const char *format, ...)
198 va_list va; va_start (va, format);
199 vprintf (format, va); fflush (stdout);
204 /* g_message including a windows error message. It is not useful to have an
205 * equivalent function for g_warning because none of the registry errors can
206 * result from programmer error (Microsoft programmers don't count), instead
207 * they will mostly occur from people messing with the registry by hand. */
209 g_message_win32_error (DWORD result_code,
215 gchar win32_message[1024];
217 if (result_code == 0)
218 result_code = GetLastError ();
220 va_start (va, format);
221 pos = g_vsnprintf (win32_message, 512, format, va);
223 win32_message[pos++] = ':'; win32_message[pos++] = ' ';
225 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, result_code, 0, (LPTSTR)(win32_message+pos),
228 if (result_code == ERROR_KEY_DELETED)
229 trace ("(%s)", win32_message);
231 g_message (win32_message);
235 /* Make gsettings key into a registry path & value pair.
237 * Note that the return value *only* needs freeing - registry_value_name
238 * is a pointer to further inside the same block of memory.
241 parse_key (const gchar *key_name,
242 const gchar *registry_prefix,
245 gchar *path_name, *c;
247 /* All key paths are treated as absolute; gsettings doesn't seem to enforce a
250 if (key_name[0] == '/')
253 if (registry_prefix == NULL)
254 path_name = g_strdup (key_name);
256 path_name = g_strjoin ("/", registry_prefix, key_name, NULL);
258 /* Prefix is expected to be in registry format (\ separators) so don't escape that. */
259 for (c=path_name+(registry_prefix?strlen(registry_prefix):0); *c!=0; c++)
266 **value_name = 0; (*value_name)++;
272 g_variant_get_as_dword (GVariant *variant)
274 switch (g_variant_get_type_string (variant)[0])
276 case 'b': return g_variant_get_boolean (variant);
277 case 'y': return g_variant_get_byte (variant);
278 case 'n': return g_variant_get_int16 (variant);
279 case 'q': return g_variant_get_uint16 (variant);
280 case 'i': return g_variant_get_int32 (variant);
281 case 'u': return g_variant_get_uint32 (variant);
282 default: g_warn_if_reached ();
288 g_variant_get_as_qword (GVariant *variant)
290 switch (g_variant_get_type_string (variant)[0])
292 case 'x': return g_variant_get_int64 (variant);
293 case 't': return g_variant_get_uint64 (variant);
294 default: g_warn_if_reached ();
301 handle_read_error (LONG result,
302 const gchar *path_name,
303 const gchar *value_name)
305 /* file not found means key value not set, this isn't an error for us. */
306 if (result != ERROR_FILE_NOT_FOUND)
307 g_message_win32_error (result, "Unable to query value %s/%s: %s.\n",
308 path_name, value_name);
311 /***************************************************************************
312 * Cache of registry values
313 ***************************************************************************/
315 /* Generic container for registry values */
320 gint dword; /* FIXME: could inline QWORD on 64-bit systems too */
326 registry_value_dump (RegistryValue value)
328 if (value.type == REG_DWORD)
329 return g_strdup_printf ("%i", value.dword);
330 else if (value.type == REG_QWORD)
331 return g_strdup_printf ("%I64i", value.ptr==NULL? 0: *(DWORDLONG *)value.ptr);
332 else if (value.type == REG_SZ)
333 return g_strdup_printf ("%s", (char *)value.ptr);
334 else if (value.type == REG_NONE)
335 return g_strdup_printf ("<empty>");
337 return g_strdup_printf ("<invalid>");
341 registry_value_free (RegistryValue value)
343 if (value.type == REG_SZ || value.type == REG_QWORD)
345 value.type = REG_NONE;
350 /* The registry cache is stored as a tree, for easy traversal. Right now we
351 * don't sort it in a clever way. Each node corresponds to a path element
352 * ('key' in registry terms) or a value.
354 * Each subscription uses the same cache. Because GSettings can subscribe to
355 * the tree at any node any number of times, we need to reference count the
360 /* Component of path that this node represents */
363 /* If a watch is subscribed at this point (subscription_count > 0) we can
364 * block its next notification. This is useful because if two watches cover
365 * the same path, both will trigger when it changes. It also allows changes
366 * done by the application to be ignored by the watch thread.
368 gint32 block_count : 8;
370 /* Number of times g_settings_subscribe has been called for this location
371 * (I guess you can't subscribe more than 16383 times) */
372 gint32 subscription_count : 14;
374 gint32 ref_count : 9;
383 registry_cache_add_item (GNode *parent,
388 RegistryCacheItem *item = g_slice_new (RegistryCacheItem);
391 g_return_val_if_fail (name != NULL, NULL);
392 g_return_val_if_fail (parent != NULL, NULL);
394 /* Ref count should be the number of watch points above this node */
395 item->ref_count = ref_count;
397 item->name = g_strdup (name);
399 item->subscription_count = 0;
400 item->block_count = 0;
401 item->touched = FALSE;
402 trace ("\treg cache: adding %s to %s\n", name, ((RegistryCacheItem *)parent->data)->name);
404 cache_node = g_node_new (item);
405 g_node_append (parent, cache_node);
409 /* The reference counting of cache tree nodes works like this: when a node is
410 * subscribed to (GSettings wants us to watch that path and everything below
411 * it) the reference count of that node and everything below is increased, as
412 * well as each parent up to the root.
417 _ref_down (GNode *node)
419 RegistryCacheItem *item = node->data;
420 g_node_children_foreach (node, G_TRAVERSE_ALL,
421 (GNodeForeachFunc)_ref_down, NULL);
425 registry_cache_ref_tree (GNode *tree)
427 RegistryCacheItem *item = tree->data;
428 GNode *node = tree->parent;
430 g_return_if_fail (tree != NULL);
434 g_node_children_foreach (tree, G_TRAVERSE_ALL,
435 (GNodeForeachFunc)_ref_down, NULL);
437 for (node=tree->parent; node; node=node->parent)
445 _free_cache_item (RegistryCacheItem *item)
447 trace ("\t -- Free node %s\n", item->name);
449 registry_value_free (item->value);
450 g_slice_free (RegistryCacheItem, item);
453 /* Unreferencing has to be done bottom-up */
455 _unref_node (GNode *node)
457 RegistryCacheItem *item = node->data;
461 g_warn_if_fail (item->ref_count >= 0);
463 if (item->ref_count == 0)
465 _free_cache_item (item);
466 g_node_destroy (node);
471 _unref_down (GNode *node)
473 g_node_children_foreach (node, G_TRAVERSE_ALL,
474 (GNodeForeachFunc)_unref_down, NULL);
479 registry_cache_unref_tree (GNode *tree)
481 GNode *parent = tree->parent, *next_parent;
487 next_parent = parent->parent;
488 _unref_node (parent);
489 parent = next_parent;
495 registry_cache_dump (GNode *cache_node,
498 RegistryCacheItem *item = cache_node->data;
500 int depth = GPOINTER_TO_INT(data),
504 g_return_if_fail (cache_node != NULL);
506 for (i=0; i<depth; i++)
509 g_print ("*root*\n");
511 g_print ("'%s' [%i] @ %x = %s\n", item->name, item->ref_count, (guint)cache_node,
512 registry_value_dump (item->value));
513 g_node_children_foreach (cache_node, G_TRAVERSE_ALL, registry_cache_dump,
514 GINT_TO_POINTER (new_depth));
522 } RegistryCacheSearch;
525 registry_cache_find_compare (GNode *node,
528 RegistryCacheSearch *search = data;
529 RegistryCacheItem *item = node->data;
531 if (item == NULL) /* root node */
534 g_return_val_if_fail (search->name != NULL, FALSE);
535 g_return_val_if_fail (item->name != NULL, FALSE);
537 if (strcmp (search->name, item->name) == 0)
539 search->result = node;
546 registry_cache_find_immediate_child (GNode *node,
549 RegistryCacheSearch search;
550 search.result = NULL;
552 g_node_traverse (node, G_POST_ORDER, G_TRAVERSE_ALL, 2,
553 registry_cache_find_compare, &search);
554 return search.result;
559 registry_cache_get_node_for_key_recursive (GNode *node,
561 gboolean create_if_not_found,
562 gint n_parent_watches)
564 RegistryCacheItem *item;
565 gchar *component = key_name,
566 *c = strchr (component, '/');
572 /* We count up how many watch points we travel through finding this node,
573 * because a new node should have as many references as there are watches at
574 * points above it in the tree.
577 if (item->subscription_count > 0)
580 child = registry_cache_find_immediate_child (node, component);
581 if (child == NULL && create_if_not_found)
583 item = g_slice_new (RegistryCacheItem);
584 item->name = g_strdup (component);
585 item->value.type = REG_NONE;
586 item->value.ptr = NULL;
587 item->ref_count = n_parent_watches;
588 child = g_node_new (item);
589 g_node_append (node, child);
590 trace ("\tget node for key recursive: new %x = %s.\n", node, item->name);
593 /* We are done if there are no more path components. Allow for a trailing /. */
594 if (child==NULL || c == NULL || *(c+1)==0)
598 trace ("get node for key recursive: next: %s.\n", c+1);
599 return registry_cache_get_node_for_key_recursive
600 (child, c+1, create_if_not_found, n_parent_watches);
604 /* Look up a GSettings key in the cache. */
606 registry_cache_get_node_for_key (GNode *root,
607 const gchar *key_name,
608 gboolean create_if_not_found)
612 gchar *component, *c;
614 g_return_val_if_fail (key_name != NULL, NULL);
616 if (key_name[0] == '/')
619 /* Ignore preceding / */
620 component = g_strdup (key_name);
621 c = strchr (component, '/');
625 child = registry_cache_find_immediate_child (root, component);
626 if (child == NULL && create_if_not_found)
628 /* Reference count is set to 0, tree should be referenced by the caller */
629 RegistryCacheItem *item = g_slice_new (RegistryCacheItem);
630 item->value.type = REG_NONE;
631 item->value.ptr = NULL;
632 item->name = g_strdup (component);
634 trace ("get_node_for_key: New node for component '%s'\n", item->name);
635 child = g_node_new (item);
636 g_node_append (root, child);
643 else if (child != NULL)
644 result = registry_cache_get_node_for_key_recursive (child, c+1, create_if_not_found, 0);
651 /* Check the cache node against the registry key it represents. Return TRUE if
652 * they differ, and update the cache with the new value.
655 registry_cache_update_node (GNode *cache_node,
656 RegistryValue registry_value)
658 RegistryCacheItem *cache_item = cache_node->data;
660 g_return_val_if_fail (cache_node != NULL, FALSE);
661 g_return_val_if_fail (cache_item != NULL, FALSE);
663 if (registry_value.type != cache_item->value.type)
665 /* The type has changed. Update cache item and register it as changed.
666 * Either the schema has changed and this is entirely legitimate, or
667 * whenever the app reads the key it will get the default value due to
670 cache_item->value = registry_value;
674 switch (registry_value.type)
678 if (cache_item->value.dword == registry_value.dword)
682 cache_item->value.dword = registry_value.dword;
688 g_return_val_if_fail (registry_value.ptr != NULL &&
689 cache_item->value.ptr != NULL, FALSE);
691 if (memcmp (registry_value.ptr, cache_item->value.ptr, 8)==0)
693 g_free (registry_value.ptr);
698 g_free (cache_item->value.ptr);
699 cache_item->value.ptr = registry_value.ptr;
705 /* Value should not exist if it is NULL, an empty string is "" */
706 g_return_val_if_fail (cache_item->value.ptr != NULL, FALSE);
707 g_return_val_if_fail (registry_value.ptr != NULL, FALSE);
709 if (strcmp (registry_value.ptr, cache_item->value.ptr) == 0)
711 g_free (registry_value.ptr);
716 g_free (cache_item->value.ptr);
717 cache_item->value.ptr = registry_value.ptr;
722 g_warning ("gregistrybackend: registry_cache_update_node: Unhandled value type :(");
727 /* Blocking notifications is a useful optimisation. When a change is made
728 * through GSettings we update the cache manually, but a notifcation is
729 * triggered as well. This function is also used for nested notifications,
730 * eg. if /test and /test/foo are watched, and /test/foo/value is changed then
731 * we will get notified both for /test/foo and /test and it is helpful to block
735 registry_cache_block_notification (GNode *node)
737 RegistryCacheItem *item = node->data;
739 g_return_if_fail (node != NULL);
741 if (item->subscription_count > 0)
742 item->block_count ++;
744 if (node->parent != NULL)
745 registry_cache_block_notification (node->parent);
749 registry_cache_destroy_tree (GNode *node,
750 WatchThreadState *self);
752 /***************************************************************************
753 * Reading and writing
754 ***************************************************************************/
757 registry_read (HKEY hpath,
758 const gchar *path_name,
759 const gchar *value_name,
760 RegistryValue *p_value)
763 DWORD value_data_size;
766 g_return_val_if_fail (p_value != NULL, FALSE);
768 p_value->type = REG_NONE;
771 result = RegQueryValueExA (hpath, value_name, 0, &p_value->type, NULL, &value_data_size);
772 if (result != ERROR_SUCCESS)
774 handle_read_error (result, path_name, value_name);
778 if (p_value->type == REG_SZ && value_data_size == 0)
780 p_value->ptr = g_strdup ("");
784 if (p_value->type == REG_DWORD)
785 /* REG_DWORD is inlined */
786 buffer = (void *)&p_value->dword;
788 buffer = p_value->ptr = g_malloc (value_data_size);
790 result = RegQueryValueExA (hpath, value_name, 0, NULL, (LPBYTE)buffer, &value_data_size);
791 if (result != ERROR_SUCCESS)
793 handle_read_error (result, path_name, value_name);
802 g_registry_backend_read (GSettingsBackend *backend,
803 const gchar *key_name,
804 const GVariantType *expected_type,
805 gboolean default_value)
807 GRegistryBackend *self = G_REGISTRY_BACKEND (backend);
810 RegistryValue registry_value;
811 GVariant *gsettings_value = NULL;
812 gchar *gsettings_type;
814 g_return_val_if_fail (expected_type != NULL, NULL);
819 /* Simply read from the cache, which is updated from the registry by the
820 * watch thread as soon as changes can propagate. Any changes not yet in the
821 * cache will have the 'changed' signal emitted after this function returns.
823 EnterCriticalSection (self->cache_lock);
824 cache_node = registry_cache_get_node_for_key (self->cache_root, key_name, FALSE);
825 LeaveCriticalSection (self->cache_lock);
827 trace ("Reading key %s, cache node %x\n", key_name, cache_node);
829 /* Maybe it's not set, we can return to default */
830 if (cache_node == NULL)
833 trace ("\t- cached value %s\n", registry_value_dump (((RegistryCacheItem *)cache_node->data)->value));
835 registry_value = ((RegistryCacheItem *)cache_node->data)->value;
837 gsettings_type = g_variant_type_dup_string (expected_type);
839 /* The registry is user-editable, so we need to be fault-tolerant here. */
840 switch (gsettings_type[0])
842 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
843 if (registry_value.type == REG_DWORD)
844 gsettings_value = g_variant_new (gsettings_type, registry_value.dword);
848 if (registry_value.type == REG_QWORD)
850 DWORDLONG qword_value = *(DWORDLONG *)registry_value.ptr;
851 gsettings_value = g_variant_new (gsettings_type, qword_value);
856 if (registry_value.type == REG_SZ)
858 if (gsettings_type[0]=='s')
859 gsettings_value = g_variant_new_string ((char *)registry_value.ptr);
862 GError *error = NULL;
863 gsettings_value = g_variant_parse (expected_type, registry_value.ptr, NULL, NULL, &error);
866 g_message ("gregistrysettingsbackend: error parsing key %s: %s\n",
867 key_name, error->message);
873 g_free (gsettings_type);
875 return gsettings_value;
881 GRegistryBackend *self;
886 g_registry_backend_write_one (const char *key_name,
890 GRegistryBackend *self;
891 RegistryWrite *action;
895 gchar *path_name, *value_name = NULL;
896 DWORD value_data_size;
903 const gchar *type_string = g_variant_get_type_string (variant);
906 self = G_REGISTRY_BACKEND (action->self);
907 hroot = action->hroot;
909 value.type = REG_NONE;
912 switch (type_string[0])
914 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
915 value.type = REG_DWORD;
916 value.dword = g_variant_get_as_dword (variant);
918 value_data = &value.dword;
922 value.type = REG_QWORD;
923 value.ptr = g_malloc (8);
924 *(DWORDLONG *)value.ptr = g_variant_get_as_qword (variant);
926 value_data = value.ptr;
931 if (type_string[0]=='s')
934 value.ptr = g_strdup (g_variant_get_string (variant, &length));
935 value_data_size = length + 1;
936 value_data = value.ptr;
940 GString *value_string;
941 value_string = g_variant_print_string (variant, NULL, FALSE);
942 value_data_size = value_string->len+1;
943 value.ptr = value_data = g_string_free (value_string, FALSE);
948 /* First update the cache, because the value may not have changed and we can
951 * If 'value' has changed then its memory will not be freed by update_node(),
952 * because it will be stored in the node.
954 EnterCriticalSection (self->cache_lock);
955 node = registry_cache_get_node_for_key (self->cache_root, key_name, TRUE);
956 changed = registry_cache_update_node (node, value);
957 LeaveCriticalSection (self->cache_lock);
962 /* Block the next notification to any watch points above this location,
963 * because they will each get triggered on a change that is already updated
966 registry_cache_block_notification (node);
968 path_name = parse_key (key_name, NULL, &value_name);
970 trace ("Set key: %s / %s\n", path_name, value_name);
972 /* Store the value in the registry */
973 result = RegCreateKeyExA (hroot, path_name, 0, NULL, 0, KEY_WRITE, NULL, &hpath, NULL);
974 if (result != ERROR_SUCCESS)
976 g_message_win32_error (result, "gregistrybackend: opening key %s failed", path_name+1);
977 registry_value_free (value);
982 result = RegSetValueExA (hpath, value_name, 0, value.type, value_data, value_data_size);
983 if (result != ERROR_SUCCESS)
984 g_message_win32_error (result, "gregistrybackend: setting value %s\\%s\\%s failed.\n",
985 self->base_path, path_name, value_name);
987 /* If the write fails then it will seem like the value has changed until the
988 * next execution (because we wrote to the cache first). There's no reason
989 * for it to fail unless something is weirdly broken, however.
998 /* The dconf write policy is to do the write while making out it succeeded,
999 * and then backtrack if it didn't. The registry functions are synchronous so
1000 * we can't do that. */
1003 g_registry_backend_write (GSettingsBackend *backend,
1004 const gchar *key_name,
1006 gpointer origin_tag)
1008 GRegistryBackend *self = G_REGISTRY_BACKEND (backend);
1011 RegistryWrite action;
1013 result = RegCreateKeyExA (HKEY_CURRENT_USER, self->base_path, 0, NULL, 0,
1014 KEY_WRITE, NULL, &hroot, NULL);
1015 if (result != ERROR_SUCCESS) {
1016 trace ("Error opening/creating key %s.\n", self->base_path);
1021 action.hroot = hroot;
1022 g_registry_backend_write_one (key_name, value, &action);
1023 g_settings_backend_changed (backend, key_name, origin_tag);
1025 RegCloseKey (hroot);
1031 g_registry_backend_write_tree (GSettingsBackend *backend,
1033 gpointer origin_tag)
1035 GRegistryBackend *self = G_REGISTRY_BACKEND (backend);
1038 RegistryWrite action;
1040 result = RegCreateKeyExA (HKEY_CURRENT_USER, self->base_path, 0, NULL, 0,
1041 KEY_WRITE, NULL, &hroot, NULL);
1042 if (result != ERROR_SUCCESS) {
1043 trace ("Error opening/creating key %s.\n", self->base_path);
1048 action.hroot = hroot;
1049 g_tree_foreach (values, (GTraverseFunc)g_registry_backend_write_one,
1052 g_settings_backend_changed_tree (backend, values, origin_tag);
1053 RegCloseKey (hroot);
1059 g_registry_backend_reset (GSettingsBackend *backend,
1060 const gchar *key_name,
1061 gpointer origin_tag)
1063 GRegistryBackend *self = G_REGISTRY_BACKEND (backend);
1064 gchar *path_name, *value_name = NULL;
1069 /* Remove from cache */
1070 EnterCriticalSection (self->cache_lock);
1071 cache_node = registry_cache_get_node_for_key (self->cache_root, key_name, FALSE);
1073 registry_cache_destroy_tree (cache_node, self->watch);
1074 LeaveCriticalSection (self->cache_lock);
1076 /* Remove from the registry */
1077 path_name = parse_key (key_name, self->base_path, &value_name);
1079 result = RegOpenKeyExA (HKEY_CURRENT_USER, path_name, 0, KEY_SET_VALUE, &hpath);
1080 if (result != ERROR_SUCCESS)
1082 g_message_win32_error (result, "Registry: resetting key '%s'", path_name);
1087 result = RegDeleteValueA (hpath, value_name);
1088 RegCloseKey (hpath);
1090 if (result != ERROR_SUCCESS)
1092 g_message_win32_error (result, "Registry: resetting key '%s'", path_name);
1100 g_settings_backend_changed (backend, key_name, origin_tag);
1103 /* Not implemented and probably beyond the scope of this backend */
1105 g_registry_backend_get_writable (GSettingsBackend *backend,
1106 const gchar *key_name)
1111 static GPermission *
1112 g_registry_backend_get_permission (GSettingsBackend *backend,
1113 const gchar *key_name)
1115 return g_simple_permission_new (TRUE);
1119 /********************************************************************************
1120 * Spot-the-difference engine
1121 ********************************************************************************/
1124 _free_watch (WatchThreadState *self,
1129 registry_cache_item_reset_touched (GNode *node,
1132 RegistryCacheItem *item = node->data;
1133 item->touched = FALSE;
1136 /* Delete a node and any children, for when it has been deleted from the registry */
1138 registry_cache_destroy_tree (GNode *node,
1139 WatchThreadState *self)
1141 RegistryCacheItem *item = node->data;
1143 g_node_children_foreach (node, G_TRAVERSE_ALL,
1144 (GNodeForeachFunc)registry_cache_destroy_tree, self);
1146 if (item->subscription_count > 0)
1149 /* There must be some watches active if this node is a watch point */
1150 g_warn_if_fail (self->cache_nodes->len > 1);
1152 /* This is a watch point that has been deleted. Let's free the watch! */
1153 for (i=1; i<self->cache_nodes->len; i++)
1154 if (g_ptr_array_index (self->cache_nodes, i) == node)
1156 if (i >= self->cache_nodes->len)
1157 g_warning ("watch thread: a watch point was deleted, but unable to "
1158 "find '%s' in the list of %i watch nodes\n", item->name,
1159 self->cache_nodes->len-1);
1162 _free_watch (self, i, node);
1163 g_atomic_int_inc (&self->watches_remaining);
1166 _free_cache_item (node->data);
1167 g_node_destroy (node);
1171 registry_cache_remove_deleted (GNode *node,
1174 RegistryCacheItem *item = node->data;
1177 registry_cache_destroy_tree (node, data);
1180 /* Update cache from registry, and optionally report on the changes.
1182 * This function is sometimes called from the watch thread, with no locking. It
1183 * does call g_registry_backend functions, but this is okay because they only
1184 * access self->base which is constant.
1186 * When looking at this code bear in mind the terminology: in the registry, keys
1187 * are containers that contain values, and other keys. Keys have a 'default'
1188 * value which we always ignore.
1190 * n_parent_watches: a counter used to set the reference count of any new nodes
1191 * that are created - they should have as many references as
1192 * there are notifications that are watching them.
1195 registry_cache_update (GRegistryBackend *self,
1197 const gchar *prefix,
1198 const gchar *partial_key_name,
1203 gchar buffer[MAX_KEY_NAME_LENGTH + 1];
1208 RegistryCacheItem *item = cache_node->data;
1210 if (item->subscription_count > 0)
1213 /* prefix is the level that all changes occur below; partial_key_name should
1214 * be NULL on the first call to this function */
1215 key_name = g_build_path ("/", prefix, partial_key_name, NULL);
1217 trace ("registry cache update: %s. Node %x has %i children\n", key_name,
1218 cache_node, g_node_n_children (cache_node));
1220 /* Start by zeroing 'touched' flag. When the registry traversal is done, any untouched nodes
1221 * must have been deleted from the registry.
1223 g_node_children_foreach (cache_node, G_TRAVERSE_ALL,
1224 registry_cache_item_reset_touched, NULL);
1226 /* Recurse into each subpath at the current level, if any */
1230 DWORD buffer_size = MAX_KEY_NAME_LENGTH;
1233 result = RegEnumKeyEx (hpath, i++, buffer, &buffer_size, NULL, NULL, NULL, NULL);
1234 if (result != ERROR_SUCCESS)
1237 result = RegOpenKeyEx (hpath, buffer, 0, KEY_READ, &hsubpath);
1238 if (result == ERROR_SUCCESS)
1241 RegistryCacheItem *child_item;
1243 subkey_node = registry_cache_find_immediate_child (cache_node, buffer);
1244 if (subkey_node == NULL)
1246 RegistryValue null_value = {REG_NONE, {0}};
1247 subkey_node = registry_cache_add_item (cache_node, buffer,
1248 null_value, n_watches);
1252 registry_cache_update (self, hsubpath, prefix, buffer, subkey_node,
1253 n_watches, changes);
1254 child_item = subkey_node->data;
1255 child_item->touched = TRUE;
1257 RegCloseKey (hsubpath);
1260 if (result != ERROR_NO_MORE_ITEMS)
1261 g_message_win32_error (result, "gregistrybackend: error enumerating subkeys for cache.");
1263 /* Enumerate each value at 'path' and check if it has changed */
1267 DWORD buffer_size = MAX_KEY_NAME_LENGTH;
1268 GNode *cache_child_node;
1269 RegistryCacheItem *child_item;
1270 RegistryValue value;
1271 gboolean changed = FALSE;
1273 result = RegEnumValue (hpath, i++, buffer, &buffer_size, NULL, NULL, NULL, NULL);
1274 if (result != ERROR_SUCCESS)
1278 /* This is the key's 'default' value, for which we have no use. */
1281 cache_child_node = registry_cache_find_immediate_child (cache_node, buffer);
1283 if (!registry_read (hpath, key_name, buffer, &value))
1286 trace ("\tgot value %s for %s, node %x\n", registry_value_dump (value), buffer, cache_child_node);
1288 if (cache_child_node == NULL)
1290 /* This is a new value */
1291 cache_child_node = registry_cache_add_item (cache_node, buffer, value,
1297 /* For efficiency, instead of converting every value back to a GVariant to
1298 * compare it, we compare them as registry values (integers, or string
1299 * representations of the variant). The spurious change notifications that may
1300 * result should not be a big issue.
1302 * Note that 'value' is swallowed or freed.
1304 changed = registry_cache_update_node (cache_child_node, value);
1307 child_item = cache_child_node->data;
1308 child_item->touched = TRUE;
1309 if (changed == TRUE && changes != NULL)
1312 if (partial_key_name == NULL)
1313 item = g_strdup (buffer);
1315 item = g_build_path ("/", partial_key_name, buffer, NULL);
1316 g_ptr_array_add (changes, item);
1320 if (result != ERROR_NO_MORE_ITEMS)
1321 g_message_win32_error (result, "gregistrybackend: error enumerating values for cache");
1323 /* Any nodes now left untouched must have been deleted, remove them from cache */
1324 g_node_children_foreach (cache_node, G_TRAVERSE_ALL,
1325 registry_cache_remove_deleted, self->watch);
1327 trace ("registry cache update complete.\n");
1333 /***********************************************************************************
1334 * Thread to watch for registry change events
1335 ***********************************************************************************/
1337 /* Called by watch thread. Apply for notifications on a registry key and its subkeys. */
1339 registry_watch_key (HKEY hpath, HANDLE event)
1341 return RegNotifyChangeKeyValue (hpath, TRUE,
1342 REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET,
1347 /* One of these is sent down the pipe when something happens in the registry. */
1350 GRegistryBackend *self;
1351 gchar *prefix; /* prefix is a gsettings path, all items are subkeys of this. */
1352 GPtrArray *items; /* each item is a subkey below prefix that has changed. */
1355 /* This handler runs in the main thread to emit the changed signals */
1357 watch_handler (RegistryEvent *event)
1360 trace ("Watch handler: got event in %s, items %i.\n", event->prefix, event->items->len);
1362 /* GSettings requires us to NULL-terminate the array. */
1363 g_ptr_array_add (event->items, NULL);
1364 g_settings_backend_keys_changed (G_SETTINGS_BACKEND (event->self), event->prefix,
1365 (gchar const **)event->items->pdata, NULL);
1367 for (i=0; i<event->items->len; i++)
1368 g_free (g_ptr_array_index (event->items, i));
1369 g_ptr_array_free (event->items, TRUE);
1371 g_free (event->prefix);
1372 g_object_unref (event->self);
1374 g_slice_free (RegistryEvent, event);
1375 return G_SOURCE_REMOVE;
1380 _free_watch (WatchThreadState *self,
1388 g_return_if_fail (index > 0 && index < self->events->len);
1390 cond = g_ptr_array_index (self->events, index);
1391 hpath = g_ptr_array_index (self->handles, index);
1392 prefix = g_ptr_array_index (self->prefixes, index);
1394 trace ("Freeing watch %i [%s]\n", index, prefix);
1396 /* These can be NULL if the watch was already dead, this can happen when eg.
1397 * a key is deleted but GSettings is still subscribed to it - the watch is
1398 * kept alive so that the unsubscribe function works properly, but does not
1402 RegCloseKey (hpath);
1404 if (cache_node != NULL)
1406 //registry_cache_dump (G_REGISTRY_BACKEND (self->owner)->cache_root, NULL);
1407 registry_cache_unref_tree (cache_node);
1413 /* As long as we remove from each array at the same time, it doesn't matter that
1414 * their orders get messed up - they all get messed up the same.
1416 g_ptr_array_remove_index_fast (self->handles, index);
1417 g_ptr_array_remove_index_fast (self->events, index);
1418 g_ptr_array_remove_index_fast (self->prefixes, index);
1419 g_ptr_array_remove_index_fast (self->cache_nodes, index);
1423 watch_thread_handle_message (WatchThreadState *self)
1425 switch (self->message.type)
1427 case WATCH_THREAD_NONE:
1428 trace ("watch thread: you woke me up for nothin', man!");
1431 case WATCH_THREAD_ADD_WATCH:
1433 RegistryWatch *watch = &self->message.watch;
1435 result = registry_watch_key (watch->hpath, watch->event);
1436 if (result == ERROR_SUCCESS)
1438 g_ptr_array_add (self->events, watch->event);
1439 g_ptr_array_add (self->handles, watch->hpath);
1440 g_ptr_array_add (self->prefixes, watch->prefix);
1441 g_ptr_array_add (self->cache_nodes, watch->cache_node);
1442 trace ("watch thread: new watch on %s, %i total\n", watch->prefix,
1447 g_message_win32_error (result, "watch thread: could not watch %s", watch->prefix);
1448 CloseHandle (watch->event);
1449 RegCloseKey (watch->hpath);
1450 g_free (watch->prefix);
1451 registry_cache_unref_tree (watch->cache_node);
1456 case WATCH_THREAD_REMOVE_WATCH:
1459 RegistryCacheItem *cache_item;
1462 for (i=1; i<self->prefixes->len; i++)
1463 if (strcmp (g_ptr_array_index (self->prefixes, i),
1464 self->message.watch.prefix) == 0)
1467 if (i >= self->prefixes->len)
1469 /* Don't make a fuss if the prefix is not being watched because
1470 * maybe the path was deleted so we removed the watch.
1472 trace ("unsubscribe: prefix %s is not being watched [%i things are]!\n",
1473 self->message.watch.prefix, self->prefixes->len);
1474 g_free (self->message.watch.prefix);
1478 cache_node = g_ptr_array_index (self->cache_nodes, i);
1480 trace ("watch thread: unsubscribe: freeing node %x, prefix %s, index %i\n",
1481 (guint)cache_node, self->message.watch.prefix, i);
1482 if (cache_node != NULL)
1484 cache_item = cache_node->data;
1486 /* There may be more than one GSettings object subscribed to this
1487 * path, only free the watch when the last one unsubscribes.
1489 cache_item->subscription_count --;
1490 if (cache_item->subscription_count > 0)
1494 _free_watch (self, i, cache_node);
1495 g_free (self->message.watch.prefix);
1497 g_atomic_int_inc (&self->watches_remaining);
1501 case WATCH_THREAD_STOP:
1505 /* Free any remaining cache and watch handles */
1506 for (i=1; i<self->events->len; i++)
1507 _free_watch (self, i, g_ptr_array_index (self->cache_nodes, i));
1509 SetEvent (self->message_received_event);
1514 self->message.type = WATCH_THREAD_NONE;
1515 SetEvent (self->message_received_event);
1519 /* Thread which watches for win32 registry events */
1521 watch_thread_function (LPVOID parameter)
1523 WatchThreadState *self = (WatchThreadState *)parameter;
1526 self->events = g_ptr_array_new ();
1527 self->handles = g_ptr_array_new ();
1528 self->prefixes = g_ptr_array_new ();
1529 self->cache_nodes = g_ptr_array_new ();
1530 g_ptr_array_add (self->events, self->message_sent_event);
1531 g_ptr_array_add (self->handles, NULL);
1532 g_ptr_array_add (self->prefixes, NULL);
1533 g_ptr_array_add (self->cache_nodes, NULL);
1537 trace ("watch thread: going to sleep; %i events watched.\n", self->events->len);
1538 result = WaitForMultipleObjects (self->events->len, self->events->pdata, FALSE, INFINITE);
1540 if (result == WAIT_OBJECT_0)
1542 /* A message to you. The sender (main thread) will block until we signal the received
1543 * event, so there should be no danger of it sending another before we receive the
1546 watch_thread_handle_message (self);
1548 else if (result > WAIT_OBJECT_0 && result <= WAIT_OBJECT_0 + self->events->len)
1554 RegistryCacheItem *cache_item;
1555 RegistryEvent *event;
1557 /* One of our notifications has triggered. All we know is which one, and which key
1558 * this is for. We do most of the processing here, because we may as well. If the
1559 * registry changes further while we are processing it doesn't matter - we will then
1560 * receive another change notification from the OS anyway.
1562 gint notify_index = result - WAIT_OBJECT_0;
1563 hpath = g_ptr_array_index (self->handles, notify_index);
1564 cond = g_ptr_array_index (self->events, notify_index);
1565 prefix = g_ptr_array_index (self->prefixes, notify_index);
1566 cache_node = g_ptr_array_index (self->cache_nodes, notify_index);
1568 trace ("Watch thread: notify received on prefix %i: %s.\n", notify_index, prefix);
1570 if (cache_node == NULL)
1572 /* This path has been deleted */
1573 trace ("Notify received on a path that was deleted :(\n");
1577 /* Firstly we need to reapply for the notification, because (what a
1578 * sensible API) we won't receive any more. MSDN is pretty
1579 * inconsistent on this matter:
1580 * http://msdn.microsoft.com/en-us/library/ms724892%28VS.85%29.aspx
1581 * http://support.microsoft.com/kb/236570
1582 * But my tests (on Windows XP SP3) show that we need to reapply
1585 result = registry_watch_key (hpath, cond);
1587 if (result != ERROR_SUCCESS)
1589 /* Watch failed, most likely because the key has just been
1590 * deleted. Free the watch and unref the cache nodes.
1592 if (result != ERROR_KEY_DELETED)
1593 g_message_win32_error (result, "watch thread: failed to watch %s", prefix);
1594 _free_watch (self, notify_index, cache_node);
1595 g_atomic_int_inc (&self->watches_remaining);
1599 /* The notification may have been blocked because we just changed
1600 * some data ourselves.
1602 cache_item = cache_node->data;
1603 if (cache_item->block_count)
1605 cache_item->block_count --;
1606 trace ("Watch thread: notify blocked at %s\n", prefix);
1610 /* Now we update our stored cache from registry data, and find which keys have
1611 * actually changed. If more changes happen while we are processing, we will get
1612 * another event because we have reapplied for change notifications already.
1614 * Working here rather than in the main thread is preferable because the UI is less
1615 * likely to block (only when changing notification subscriptions).
1617 event = g_slice_new (RegistryEvent);
1619 event->self = G_REGISTRY_BACKEND (self->owner);
1620 g_object_ref (self->owner);
1622 event->items = g_ptr_array_new ();
1624 EnterCriticalSection (G_REGISTRY_BACKEND (self->owner)->cache_lock);
1625 registry_cache_update (G_REGISTRY_BACKEND (self->owner), hpath,
1626 prefix, NULL, cache_node, 0, event->items);
1627 LeaveCriticalSection (G_REGISTRY_BACKEND (self->owner)->cache_lock);
1629 if (event->items->len > 0)
1631 event->prefix = g_strdup (prefix);
1632 g_idle_add ((GSourceFunc) watch_handler, event);
1636 g_ptr_array_free (event->items, TRUE);
1637 g_slice_free (RegistryEvent, event);
1642 /* God knows what has happened */
1643 g_message_win32_error (GetLastError(), "watch thread: WaitForMultipleObjects error");
1651 watch_start (GRegistryBackend *self)
1653 WatchThreadState *watch;
1655 g_return_val_if_fail (self->watch == NULL, FALSE);
1657 self->cache_lock = g_slice_new (CRITICAL_SECTION);
1658 InitializeCriticalSection (self->cache_lock);
1660 watch = g_slice_new (WatchThreadState);
1661 watch->owner = G_SETTINGS_BACKEND (self);
1663 watch->watches_remaining = MAX_WATCHES;
1665 watch->message_lock = g_slice_new (CRITICAL_SECTION);
1666 InitializeCriticalSection (watch->message_lock);
1667 watch->message_sent_event = CreateEvent (NULL, FALSE, FALSE, NULL);
1668 watch->message_received_event = CreateEvent (NULL, FALSE, FALSE, NULL);
1669 if (watch->message_sent_event == NULL || watch->message_received_event == NULL)
1671 g_message_win32_error (0, "gregistrybackend: Failed to create sync objects.");
1675 /* Use a small stack to make the thread more lightweight. */
1676 watch->thread = CreateThread (NULL, 1024, watch_thread_function, watch, 0, NULL);
1677 if (watch->thread == NULL)
1679 g_message_win32_error (0, "gregistrybackend: Failed to create notify watch thread.");
1683 self->watch = watch;
1688 DeleteCriticalSection (self->cache_lock);
1689 g_slice_free (CRITICAL_SECTION, self->cache_lock);
1690 DeleteCriticalSection (watch->message_lock);
1691 g_slice_free (CRITICAL_SECTION, watch->message_lock);
1692 CloseHandle (watch->message_sent_event);
1693 CloseHandle (watch->message_received_event);
1695 g_slice_free (WatchThreadState, watch);
1699 /* This function assumes you hold the message lock! */
1701 watch_stop_unlocked (GRegistryBackend *self)
1703 WatchThreadState *watch = self->watch;
1705 g_return_if_fail (watch != NULL);
1707 watch->message.type = WATCH_THREAD_STOP;
1708 SetEvent (watch->message_sent_event);
1710 /* This is signalled as soon as the message is received. We must not return
1711 * while the watch thread is still firing off callbacks. Freeing all of the
1712 * memory is done in the watch thread after this is signalled.
1714 result = WaitForSingleObject (watch->message_received_event, INFINITE);
1715 if (result != WAIT_OBJECT_0)
1717 g_warning ("gregistrybackend: unable to stop watch thread.");
1721 LeaveCriticalSection (watch->message_lock);
1722 DeleteCriticalSection (watch->message_lock);
1723 DeleteCriticalSection (self->cache_lock);
1724 g_slice_free (CRITICAL_SECTION, watch->message_lock);
1725 g_slice_free (CRITICAL_SECTION, self->cache_lock);
1726 CloseHandle (watch->message_sent_event);
1727 CloseHandle (watch->message_received_event);
1728 CloseHandle (watch->thread);
1729 g_slice_free (WatchThreadState, watch);
1731 trace ("\nwatch thread: %x: all data freed.\n", self);
1736 watch_add_notify (GRegistryBackend *self,
1739 gchar *gsettings_prefix)
1741 WatchThreadState *watch = self->watch;
1743 RegistryCacheItem *cache_item;
1748 g_return_val_if_fail (watch != NULL, FALSE);
1749 trace ("watch_add_notify: prefix %s.\n", gsettings_prefix);
1751 /* Duplicate tree into the cache in the main thread, before we add the notify: if we do it in the
1752 * thread we can miss changes while we are caching.
1754 EnterCriticalSection (self->cache_lock);
1755 cache_node = registry_cache_get_node_for_key (self->cache_root, gsettings_prefix, TRUE);
1757 g_return_val_if_fail (cache_node != NULL, FALSE);
1758 g_return_val_if_fail (cache_node->data != NULL, FALSE);
1760 cache_item = cache_node->data;
1762 cache_item->subscription_count ++;
1763 if (cache_item->subscription_count > 1)
1765 trace ("watch_add_notify: prefix %s already watched, %i subscribers.\n",
1766 gsettings_prefix, cache_item->subscription_count);
1770 registry_cache_ref_tree (cache_node);
1771 registry_cache_update (self, hpath, gsettings_prefix, NULL, cache_node, 0, NULL);
1772 //registry_cache_dump (self->cache_root, NULL);
1773 LeaveCriticalSection (self->cache_lock);
1775 EnterCriticalSection (watch->message_lock);
1776 watch->message.type = WATCH_THREAD_ADD_WATCH;
1777 watch->message.watch.event = event;
1778 watch->message.watch.hpath = hpath;
1779 watch->message.watch.prefix = gsettings_prefix;
1780 watch->message.watch.cache_node = cache_node;
1782 SetEvent (watch->message_sent_event);
1784 /* Wait for the received event in return, to avoid sending another message before the first
1785 * one was received. If it takes > 200ms there is a possible race but the worst outcome is
1786 * a notification is ignored.
1791 WaitForSingleObject (watch->message_received_event, 200);
1793 if (result != WAIT_OBJECT_0)
1794 trace ("watch thread is slow to respond - notification may not be added.");
1797 LeaveCriticalSection (watch->message_lock);
1804 watch_remove_notify (GRegistryBackend *self,
1805 const gchar *key_name)
1807 WatchThreadState *watch = self->watch;
1810 if (self->watch == NULL)
1811 /* Here we assume that the unsubscribe message is for somewhere that was
1812 * deleted, and so it has already been removed and the watch thread has
1817 EnterCriticalSection (watch->message_lock);
1818 watch->message.type = WATCH_THREAD_REMOVE_WATCH;
1819 watch->message.watch.prefix = g_strdup (key_name);
1821 SetEvent (watch->message_sent_event);
1823 /* Wait for the received event in return, to avoid sending another message before the first
1826 result = WaitForSingleObject (watch->message_received_event, INFINITE);
1828 if (result != ERROR_SUCCESS)
1829 g_warning ("unsubscribe from %s: message not acknowledged\n", key_name);
1831 if (g_atomic_int_get (&watch->watches_remaining) >= MAX_WATCHES)
1832 /* Stop it before any new ones can get added and confuse things */
1833 watch_stop_unlocked (self);
1835 LeaveCriticalSection (watch->message_lock);
1838 /* dconf semantics are: if the key ends in /, watch the keys underneath it - if not, watch that
1839 * key. Our job is easier because keys and values are separate.
1842 g_registry_backend_subscribe (GSettingsBackend *backend,
1843 const char *key_name)
1845 GRegistryBackend *self = G_REGISTRY_BACKEND (backend);
1846 gchar *path_name, *value_name = NULL;
1851 if (self->watch == NULL)
1852 if (!watch_start (self))
1855 if (g_atomic_int_dec_and_test (&self->watch->watches_remaining))
1857 g_atomic_int_inc (&self->watch->watches_remaining);
1858 g_warning ("subscribe() failed: only %i different paths may be watched.\n", MAX_WATCHES);
1862 path_name = parse_key (key_name, self->base_path, &value_name);
1864 /* Must check for this, otherwise strange crashes occur because the cache
1865 * node that is being watched gets freed. All path names to subscribe must
1868 if (value_name != NULL && *value_name != 0)
1869 g_warning ("subscribe() failed: path must end in a /, got %s\n", key_name);
1871 trace ("Subscribing to %s [registry %s / %s] - watch %x\n", key_name, path_name, value_name, self->watch);
1874 /* Give the caller the benefit of the doubt if the key doesn't exist and create it. The caller
1875 * is almost certainly a new g_settings with this path as base path. */
1876 result = RegCreateKeyExA (HKEY_CURRENT_USER, path_name, 0, NULL, 0, KEY_READ, NULL, &hpath,
1880 if (result != ERROR_SUCCESS)
1882 g_message_win32_error (result, "gregistrybackend: Unable to subscribe to key %s.", key_name);
1883 g_atomic_int_inc (&self->watch->watches_remaining);
1887 event = CreateEvent (NULL, FALSE, FALSE, NULL);
1890 g_message_win32_error (result, "gregistrybackend: CreateEvent failed.\n");
1891 g_atomic_int_inc (&self->watch->watches_remaining);
1892 RegCloseKey (hpath);
1896 /* The actual watch is added by the thread, which has to re-subscribe each time it
1897 * receives a change. */
1898 if (!watch_add_notify (self, event, hpath, g_strdup (key_name)))
1899 g_atomic_int_inc (&self->watch->watches_remaining);
1903 g_registry_backend_unsubscribe (GSettingsBackend *backend,
1904 const char *key_name)
1906 trace ("unsubscribe: %s.\n", key_name);
1908 watch_remove_notify (G_REGISTRY_BACKEND (backend), key_name);
1912 /********************************************************************************
1913 * Object management junk
1914 ********************************************************************************/
1917 g_registry_backend_finalize (GObject *object)
1919 GRegistryBackend *self = G_REGISTRY_BACKEND (object);
1920 RegistryCacheItem *item;
1922 item = self->cache_root->data;
1923 g_warn_if_fail (item->ref_count == 1);
1925 _free_cache_item (item);
1926 g_node_destroy (self->cache_root);
1928 if (self->watch != NULL)
1930 EnterCriticalSection (self->watch->message_lock);
1931 watch_stop_unlocked (self);
1934 g_free (self->base_path);
1938 g_registry_backend_class_init (GRegistryBackendClass *class)
1940 GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
1941 GObjectClass *object_class = G_OBJECT_CLASS (class);
1943 object_class->finalize = g_registry_backend_finalize;
1945 backend_class->read = g_registry_backend_read;
1946 backend_class->write = g_registry_backend_write;
1947 backend_class->write_tree = g_registry_backend_write_tree;
1948 backend_class->reset = g_registry_backend_reset;
1949 backend_class->get_writable = g_registry_backend_get_writable;
1950 backend_class->get_permission = g_registry_backend_get_permission;
1951 backend_class->subscribe = g_registry_backend_subscribe;
1952 backend_class->unsubscribe = g_registry_backend_unsubscribe;
1956 g_registry_backend_init (GRegistryBackend *self)
1958 RegistryCacheItem *item;
1959 self->base_path = g_strdup_printf ("Software\\GSettings");
1961 item = g_slice_new (RegistryCacheItem);
1962 item->value.type = REG_NONE;
1963 item->value.ptr = NULL;
1964 item->name = g_strdup ("<root>");
1965 item->ref_count = 1;
1966 self->cache_root = g_node_new (item);