Fixed registry paths to edit the real config, and removed the startup
[wine.git] / programs / winecfg / winecfg.c
blob712bb661e2c214590dc55174d8e09a25fd741357
1 /*
2 * WineCfg configuration management
4 * Copyright 2002 Jaco Greeff
5 * Copyright 2003 Dimitrie O. Paun
6 * Copyright 2003-2004 Mike Hearn
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * TODO:
23 * - Use unicode
24 * - Icons in listviews/icons
25 * - Better add app dialog, scan c: for EXE files and add to list in background
26 * - Use [GNOME] HIG style groupboxes rather than win32 style (looks nicer, imho)
30 #include <assert.h>
31 #include <stdio.h>
32 #include <limits.h>
33 #include <windows.h>
34 #include <winreg.h>
35 #include <wine/debug.h>
36 #include <wine/list.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
40 #include "winecfg.h"
42 HKEY config_key = NULL;
46 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
48 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
49 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
51 void set_window_title(HWND dialog)
53 char *newtitle;
55 /* update the window title */
56 if (current_app)
58 const char *template = "Wine Configuration for %s";
59 newtitle = HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(current_app) + 1);
60 sprintf(newtitle, template, current_app);
62 else
64 newtitle = strdupA("Wine Configuration");
67 WINE_TRACE("setting title to %s\n", newtitle);
68 SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
69 HeapFree(GetProcessHeap(), 0, newtitle);
73 /**
74 * get_config_key: Retrieves a configuration value from the registry
76 * char *subkey : the name of the config section
77 * char *name : the name of the config value
78 * char *default : if the key isn't found, return this value instead
80 * Returns a buffer holding the value if successful, NULL if
81 * not. Caller is responsible for releasing the result.
84 static char *get_config_key (const char *subkey, const char *name, const char *def)
86 LPBYTE buffer = NULL;
87 DWORD len;
88 HKEY hSubKey = NULL;
89 DWORD res;
91 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
93 res = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &hSubKey);
94 if (res != ERROR_SUCCESS)
96 if (res == ERROR_FILE_NOT_FOUND)
98 WINE_TRACE("Section key not present - using default\n");
99 return def ? strdupA(def) : NULL;
101 else
103 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
105 goto end;
108 res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
109 if (res == ERROR_FILE_NOT_FOUND)
111 WINE_TRACE("Value not present - using default\n");
112 buffer = def ? strdupA(def) : NULL;
113 goto end;
114 } else if (res != ERROR_SUCCESS)
116 WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
117 goto end;
120 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
122 RegQueryValueEx(hSubKey, name, NULL, NULL, buffer, &len);
124 WINE_TRACE("buffer=%s\n", buffer);
125 end:
126 if (hSubKey) RegCloseKey(hSubKey);
128 return (char*)buffer;
132 * set_config_key: convenience wrapper to set a key/value pair
134 * const char *subKey : the name of the config section
135 * const char *valueName : the name of the config value
136 * const char *value : the value to set the configuration key to
138 * Returns 0 on success, non-zero otherwise
140 * If valueName or value is NULL, an empty section will be created
142 static int set_config_key(const char *subkey, const char *name, const char *value) {
143 DWORD res = 1;
144 HKEY key = NULL;
146 WINE_TRACE("subkey=%s: name=%s, value=%s\n", subkey, name, value);
148 assert( subkey != NULL );
150 res = RegCreateKey(config_key, subkey, &key);
151 if (res != ERROR_SUCCESS) goto end;
152 if (name == NULL || value == NULL) goto end;
154 res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1);
155 if (res != ERROR_SUCCESS) goto end;
157 res = 0;
158 end:
159 if (key) RegCloseKey(key);
160 if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", name, subkey, value, res);
161 return res;
164 /* removes the requested value from the registry, however, does not
165 * remove the section if empty. Returns S_OK (0) on success.
167 static HRESULT remove_value(const char *subkey, const char *name)
169 HRESULT hr;
170 HKEY key;
172 WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
174 hr = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &key);
175 if (hr != S_OK) return hr;
177 hr = RegDeleteValue(key, name);
178 if (hr != ERROR_SUCCESS) return hr;
180 return S_OK;
183 /* removes the requested subkey from the registry, assuming it exists */
184 static HRESULT remove_path(char *section) {
185 WINE_TRACE("section=%s\n", section);
187 return RegDeleteKey(config_key, section);
191 /* ========================================================================= */
193 /* This code exists for the following reasons:
195 * - It makes working with the registry easier
196 * - By storing a mini cache of the registry, we can more easily implement
197 * cancel/revert and apply. The 'settings list' is an overlay on top of
198 * the actual registry data that we can write out at will.
200 * Rather than model a tree in memory, we simply store each absolute (rooted
201 * at the config key) path.
205 struct setting
207 struct list entry;
208 char *path; /* path in the registry rooted at the config key */
209 char *name; /* name of the registry value. if null, this means delete the key */
210 char *value; /* contents of the registry value. if null, this means delete the value */
213 struct list *settings;
215 static void free_setting(struct setting *setting)
217 assert( setting != NULL );
218 assert( setting->path );
220 WINE_TRACE("destroying %p: %s\n", setting, setting->path);
222 HeapFree(GetProcessHeap(), 0, setting->path);
223 HeapFree(GetProcessHeap(), 0, setting->name);
224 HeapFree(GetProcessHeap(), 0, setting->value);
226 list_remove(&setting->entry);
228 HeapFree(GetProcessHeap(), 0, setting);
232 * Returns the contents of the value at path. If not in the settings
233 * list, it will be fetched from the registry - failing that, the
234 * default will be used.
236 * If already in the list, the contents as given there will be
237 * returned. You are expected to HeapFree the result.
239 char *get_reg_key(const char *path, const char *name, const char *def)
241 struct list *cursor;
242 struct setting *s;
243 char *val;
245 WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
247 /* check if it's in the list */
248 LIST_FOR_EACH( cursor, settings )
250 s = LIST_ENTRY(cursor, struct setting, entry);
252 if (strcasecmp(path, s->path) != 0) continue;
253 if (strcasecmp(name, s->name) != 0) continue;
255 WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
256 return s->value ? strdupA(s->value) : NULL;
259 /* no, so get from the registry */
260 val = get_config_key(path, name, def);
262 WINE_TRACE("returning %s\n", val);
264 return val;
268 * Used to set a registry key.
270 * path is rooted at the config key, ie use "Version" or
271 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
272 * to get such a string.
274 * name is the value name, or NULL to delete the path.
276 * value is what to set the value to, or NULL to delete it.
278 * These values will be copied when necessary.
280 void set_reg_key(const char *path, const char *name, const char *value)
282 struct list *cursor;
283 struct setting *s;
285 assert( path != NULL );
287 WINE_TRACE("path=%s, name=%s, value=%s\n", path, name, value);
289 /* firstly, see if we already set this setting */
290 LIST_FOR_EACH( cursor, settings )
292 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
294 if (strcasecmp(s->path, path) != 0) continue;
295 if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;
297 /* are we attempting a double delete? */
298 if (!s->name && !name) return;
300 /* do we want to undelete this key? */
301 if (!s->name && name) s->name = strdupA(name);
303 /* yes, we have already set it, so just replace the content and return */
304 HeapFree(GetProcessHeap(), 0, s->value);
305 s->value = value ? strdupA(value) : NULL;
307 /* are we deleting this key? this won't remove any of the
308 * children from the overlay so if the user adds it again in
309 * that session it will appear to undelete the settings, but
310 * in reality only the settings actually modified by the user
311 * in that session will be restored. we might want to fix this
312 * corner case in future by actually deleting all the children
313 * here so that once it's gone, it's gone.
315 if (!name) s->name = NULL;
317 return;
320 /* otherwise add a new setting for it */
321 s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
322 s->path = strdupA(path);
323 s->name = name ? strdupA(name) : NULL;
324 s->value = value ? strdupA(value) : NULL;
326 list_add_tail(settings, &s->entry);
330 * enumerates the value names at the given path, taking into account
331 * the changes in the settings list.
333 * you are expected to HeapFree each element of the array, which is null
334 * terminated, as well as the array itself.
336 char **enumerate_values(char *path)
338 HKEY key;
339 DWORD res, i = 0;
340 char **values = NULL;
341 int valueslen = 0;
342 struct list *cursor;
344 res = RegOpenKeyEx(config_key, path, 0, KEY_READ, &key);
345 if (res == ERROR_SUCCESS)
347 while (TRUE)
349 char name[1024];
350 DWORD namesize = sizeof(name);
351 BOOL removed = FALSE;
353 /* find out the needed size, allocate a buffer, read the value */
354 if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
355 break;
357 WINE_TRACE("name=%s\n", name);
359 /* check if this value name has been removed in the settings list */
360 LIST_FOR_EACH( cursor, settings )
362 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
363 if (strcasecmp(s->path, path) != 0) continue;
364 if (strcasecmp(s->name, name) != 0) continue;
366 if (!s->value)
368 WINE_TRACE("this key has been removed, so skipping\n");
369 removed = TRUE;
370 break;
374 if (removed) /* this value was deleted by the user, so don't include it */
376 HeapFree(GetProcessHeap(), 0, name);
377 i++;
378 continue;
381 /* grow the array if necessary, add buffer to it, iterate */
382 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
383 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
385 values[valueslen++] = strdupA(name);
386 WINE_TRACE("valueslen is now %d\n", valueslen);
387 i++;
390 else
392 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
395 WINE_TRACE("adding settings in list but not registry\n");
397 /* now we have to add the values that aren't in the registry but are in the settings list */
398 LIST_FOR_EACH( cursor, settings )
400 struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
401 BOOL found = FALSE;
403 if (strcasecmp(setting->path, path) != 0) continue;
405 if (!setting->value) continue;
407 for (i = 0; i < valueslen; i++)
409 if (strcasecmp(setting->name, values[i]) == 0)
411 found = TRUE;
412 break;
416 if (found) continue;
418 WINE_TRACE("%s in list but not registry\n", setting->name);
420 /* otherwise it's been set by the user but isn't in the registry */
421 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
422 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
424 values[valueslen++] = strdupA(setting->name);
427 WINE_TRACE("adding null terminator\n");
428 if (values)
430 values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
431 values[valueslen] = NULL;
434 RegCloseKey(key);
436 return values;
440 * returns true if the given key/value pair exists in the registry or
441 * has been written to.
443 BOOL reg_key_exists(const char *path, const char *name)
445 char *val = get_reg_key(path, name, NULL);
447 if (val)
449 HeapFree(GetProcessHeap(), 0, val);
450 return TRUE;
453 return FALSE;
456 static void process_setting(struct setting *s)
458 if (s->value)
460 WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
461 set_config_key(s->path, s->name, s->value);
463 else
465 /* NULL name means remove that path/section entirely */
466 if (s->path && s->name) remove_value(s->path, s->name);
467 else if (s->path && !s->name) remove_path(s->path);
471 void apply(void)
473 if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
475 WINE_TRACE("()\n");
477 while (!list_empty(settings))
479 struct setting *s = (struct setting *) list_head(settings);
480 process_setting(s);
481 free_setting(s);
485 /* ================================== utility functions ============================ */
487 char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
489 /* returns a registry key path suitable for passing to addTransaction */
490 char *keypath(const char *section)
492 static char *result = NULL;
494 HeapFree(GetProcessHeap(), 0, result);
496 if (current_app)
498 result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
499 sprintf(result, "AppDefaults\\%s\\%s", current_app, section);
501 else
503 result = strdupA(section);
506 return result;
509 void PRINTERROR(void)
511 LPSTR msg;
513 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
514 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
515 (LPSTR)&msg, 0, NULL);
517 /* eliminate trailing newline, is this a Wine bug? */
518 *(strrchr(msg, '\r')) = '\0';
520 WINE_TRACE("error: '%s'\n", msg);
523 int initialize(void) {
524 DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
526 if (res != ERROR_SUCCESS) {
527 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
528 return 1;
531 /* we could probably just have the list as static data */
532 settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
533 list_init(settings);
535 return 0;