msvcrt: Require exact uppercase and lowercase format in printf routines.
[wine/dibdrv.git] / programs / winecfg / winecfg.c
blob96d8f7a5d94f93a12ad4b0a4826651bc60803ef0
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"
41 #include "resource.h"
43 HKEY config_key = NULL;
44 HMENU hPopupMenus = 0;
47 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
49 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
50 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
52 void set_window_title(HWND dialog)
54 char *newtitle;
56 /* update the window title */
57 if (current_app)
59 const char *template = "Wine Configuration for %s";
60 newtitle = HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(current_app) + 1);
61 sprintf(newtitle, template, current_app);
63 else
65 newtitle = strdupA("Wine Configuration");
68 WINE_TRACE("setting title to %s\n", newtitle);
69 SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
70 HeapFree(GetProcessHeap(), 0, newtitle);
74 /**
75 * get_config_key: Retrieves a configuration value from the registry
77 * char *subkey : the name of the config section
78 * char *name : the name of the config value
79 * char *default : if the key isn't found, return this value instead
81 * Returns a buffer holding the value if successful, NULL if
82 * not. Caller is responsible for releasing the result.
85 static char *get_config_key (HKEY root, const char *subkey, const char *name, const char *def)
87 LPSTR buffer = NULL;
88 DWORD len;
89 HKEY hSubKey = NULL;
90 DWORD res;
92 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
94 res = RegOpenKey(root, subkey, &hSubKey);
95 if (res != ERROR_SUCCESS)
97 if (res == ERROR_FILE_NOT_FOUND)
99 WINE_TRACE("Section key not present - using default\n");
100 return def ? strdupA(def) : NULL;
102 else
104 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
106 goto end;
109 res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
110 if (res == ERROR_FILE_NOT_FOUND)
112 WINE_TRACE("Value not present - using default\n");
113 buffer = def ? strdupA(def) : NULL;
114 goto end;
115 } else if (res != ERROR_SUCCESS)
117 WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
118 goto end;
121 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
123 RegQueryValueEx(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
125 WINE_TRACE("buffer=%s\n", buffer);
126 end:
127 if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
129 return buffer;
133 * set_config_key: convenience wrapper to set a key/value pair
135 * const char *subKey : the name of the config section
136 * const char *valueName : the name of the config value
137 * const char *value : the value to set the configuration key to
139 * Returns 0 on success, non-zero otherwise
141 * If valueName or value is NULL, an empty section will be created
143 static int set_config_key(HKEY root, const char *subkey, const char *name, const void *value, DWORD type) {
144 DWORD res = 1;
145 HKEY key = NULL;
147 WINE_TRACE("subkey=%s: name=%s, value=%p, type=%ld\n", subkey, name, value, type);
149 assert( subkey != NULL );
151 if (subkey[0])
153 res = RegCreateKey(root, subkey, &key);
154 if (res != ERROR_SUCCESS) goto end;
156 else key = root;
157 if (name == NULL || value == NULL) goto end;
159 switch (type)
161 case REG_SZ: res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1); break;
162 case REG_DWORD: res = RegSetValueEx(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
164 if (res != ERROR_SUCCESS) goto end;
166 res = 0;
167 end:
168 if (key && key != root) RegCloseKey(key);
169 if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s, res=%ld\n", name, subkey, res);
170 return res;
173 /* removes the requested value from the registry, however, does not
174 * remove the section if empty. Returns S_OK (0) on success.
176 static HRESULT remove_value(HKEY root, const char *subkey, const char *name)
178 HRESULT hr;
179 HKEY key;
181 WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
183 hr = RegOpenKey(root, subkey, &key);
184 if (hr != S_OK) return hr;
186 hr = RegDeleteValue(key, name);
187 if (hr != ERROR_SUCCESS) return hr;
189 return S_OK;
192 /* removes the requested subkey from the registry, assuming it exists */
193 static LONG remove_path(HKEY root, char *section) {
194 HKEY branch_key;
195 DWORD max_sub_key_len;
196 DWORD subkeys;
197 DWORD curr_len;
198 LONG ret = ERROR_SUCCESS;
199 long int i;
200 char *buffer;
202 WINE_TRACE("section=%s\n", section);
204 if ((ret = RegOpenKey(root, section, &branch_key)) != ERROR_SUCCESS)
205 return ret;
207 /* get size information and resize the buffers if necessary */
208 if ((ret = RegQueryInfoKey(branch_key, NULL, NULL, NULL,
209 &subkeys, &max_sub_key_len,
210 NULL, NULL, NULL, NULL, NULL, NULL
211 )) != ERROR_SUCCESS)
212 return ret;
214 curr_len = strlen(section);
215 buffer = HeapAlloc(GetProcessHeap(), 0, max_sub_key_len + curr_len + 1);
216 strcpy(buffer, section);
218 buffer[curr_len] = '\\';
219 for (i = subkeys - 1; i >= 0; i--)
221 DWORD buf_len = max_sub_key_len - curr_len - 1;
223 ret = RegEnumKeyEx(branch_key, i, buffer + curr_len + 1,
224 &buf_len, NULL, NULL, NULL, NULL);
225 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
226 ret != ERROR_NO_MORE_ITEMS)
227 break;
228 else
229 remove_path(root, buffer);
231 HeapFree(GetProcessHeap(), 0, buffer);
232 RegCloseKey(branch_key);
234 return RegDeleteKey(root, section);
238 /* ========================================================================= */
240 /* This code exists for the following reasons:
242 * - It makes working with the registry easier
243 * - By storing a mini cache of the registry, we can more easily implement
244 * cancel/revert and apply. The 'settings list' is an overlay on top of
245 * the actual registry data that we can write out at will.
247 * Rather than model a tree in memory, we simply store each absolute (rooted
248 * at the config key) path.
252 struct setting
254 struct list entry;
255 HKEY root; /* the key on which path is rooted */
256 char *path; /* path in the registry rooted at root */
257 char *name; /* name of the registry value. if null, this means delete the key */
258 char *value; /* contents of the registry value. if null, this means delete the value */
259 DWORD type; /* type of registry value. REG_SZ or REG_DWORD for now */
262 struct list *settings;
264 static void free_setting(struct setting *setting)
266 assert( setting != NULL );
267 assert( setting->path );
269 WINE_TRACE("destroying %p: %s\n", setting, setting->path);
271 HeapFree(GetProcessHeap(), 0, setting->path);
272 HeapFree(GetProcessHeap(), 0, setting->name);
273 HeapFree(GetProcessHeap(), 0, setting->value);
275 list_remove(&setting->entry);
277 HeapFree(GetProcessHeap(), 0, setting);
281 * Returns the contents of the value at path. If not in the settings
282 * list, it will be fetched from the registry - failing that, the
283 * default will be used.
285 * If already in the list, the contents as given there will be
286 * returned. You are expected to HeapFree the result.
288 char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
290 struct list *cursor;
291 struct setting *s;
292 char *val;
294 WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
296 /* check if it's in the list */
297 LIST_FOR_EACH( cursor, settings )
299 s = LIST_ENTRY(cursor, struct setting, entry);
301 if (root != s->root) continue;
302 if (strcasecmp(path, s->path) != 0) continue;
303 if (strcasecmp(name, s->name) != 0) continue;
305 WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
306 return s->value ? strdupA(s->value) : NULL;
309 /* no, so get from the registry */
310 val = get_config_key(root, path, name, def);
312 WINE_TRACE("returning %s\n", val);
314 return val;
318 * Used to set a registry key.
320 * path is rooted at the config key, ie use "Version" or
321 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
322 * to get such a string.
324 * name is the value name, or NULL to delete the path.
326 * value is what to set the value to, or NULL to delete it.
328 * type is REG_SZ or REG_DWORD.
330 * These values will be copied when necessary.
332 static void set_reg_key_ex(HKEY root, const char *path, const char *name, const void *value, DWORD type)
334 struct list *cursor;
335 struct setting *s;
337 assert( path != NULL );
339 WINE_TRACE("path=%s, name=%s, value=%p\n", path, name, value);
341 /* firstly, see if we already set this setting */
342 LIST_FOR_EACH( cursor, settings )
344 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
346 if (root != s->root) continue;
347 if (strcasecmp(s->path, path) != 0) continue;
348 if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;
350 /* are we attempting a double delete? */
351 if (!s->name && !name) return;
353 /* do we want to undelete this key? */
354 if (!s->name && name) s->name = strdupA(name);
356 /* yes, we have already set it, so just replace the content and return */
357 HeapFree(GetProcessHeap(), 0, s->value);
358 s->type = type;
359 switch (type)
361 case REG_SZ:
362 s->value = value ? strdupA(value) : NULL;
363 break;
364 case REG_DWORD:
365 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
366 memcpy( s->value, value, sizeof(DWORD) );
367 break;
370 /* are we deleting this key? this won't remove any of the
371 * children from the overlay so if the user adds it again in
372 * that session it will appear to undelete the settings, but
373 * in reality only the settings actually modified by the user
374 * in that session will be restored. we might want to fix this
375 * corner case in future by actually deleting all the children
376 * here so that once it's gone, it's gone.
378 if (!name) s->name = NULL;
380 return;
383 /* otherwise add a new setting for it */
384 s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
385 s->root = root;
386 s->path = strdupA(path);
387 s->name = name ? strdupA(name) : NULL;
388 s->type = type;
389 switch (type)
391 case REG_SZ:
392 s->value = value ? strdupA(value) : NULL;
393 break;
394 case REG_DWORD:
395 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
396 memcpy( s->value, value, sizeof(DWORD) );
397 break;
400 list_add_tail(settings, &s->entry);
403 void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
405 set_reg_key_ex(root, path, name, value, REG_SZ);
408 void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
410 set_reg_key_ex(root, path, name, &value, REG_DWORD);
414 * enumerates the value names at the given path, taking into account
415 * the changes in the settings list.
417 * you are expected to HeapFree each element of the array, which is null
418 * terminated, as well as the array itself.
420 char **enumerate_values(HKEY root, char *path)
422 HKEY key;
423 DWORD res, i = 0;
424 char **values = NULL;
425 int valueslen = 0;
426 struct list *cursor;
428 res = RegOpenKey(root, path, &key);
429 if (res == ERROR_SUCCESS)
431 while (TRUE)
433 char name[1024];
434 DWORD namesize = sizeof(name);
435 BOOL removed = FALSE;
437 /* find out the needed size, allocate a buffer, read the value */
438 if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
439 break;
441 WINE_TRACE("name=%s\n", name);
443 /* check if this value name has been removed in the settings list */
444 LIST_FOR_EACH( cursor, settings )
446 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
447 if (strcasecmp(s->path, path) != 0) continue;
448 if (strcasecmp(s->name, name) != 0) continue;
450 if (!s->value)
452 WINE_TRACE("this key has been removed, so skipping\n");
453 removed = TRUE;
454 break;
458 if (removed) /* this value was deleted by the user, so don't include it */
460 HeapFree(GetProcessHeap(), 0, name);
461 i++;
462 continue;
465 /* grow the array if necessary, add buffer to it, iterate */
466 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
467 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
469 values[valueslen++] = strdupA(name);
470 WINE_TRACE("valueslen is now %d\n", valueslen);
471 i++;
474 else
476 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
479 WINE_TRACE("adding settings in list but not registry\n");
481 /* now we have to add the values that aren't in the registry but are in the settings list */
482 LIST_FOR_EACH( cursor, settings )
484 struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
485 BOOL found = FALSE;
487 if (strcasecmp(setting->path, path) != 0) continue;
489 if (!setting->value) continue;
491 for (i = 0; i < valueslen; i++)
493 if (strcasecmp(setting->name, values[i]) == 0)
495 found = TRUE;
496 break;
500 if (found) continue;
502 WINE_TRACE("%s in list but not registry\n", setting->name);
504 /* otherwise it's been set by the user but isn't in the registry */
505 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
506 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
508 values[valueslen++] = strdupA(setting->name);
511 WINE_TRACE("adding null terminator\n");
512 if (values)
514 values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
515 values[valueslen] = NULL;
518 RegCloseKey(key);
520 return values;
524 * returns true if the given key/value pair exists in the registry or
525 * has been written to.
527 BOOL reg_key_exists(HKEY root, const char *path, const char *name)
529 char *val = get_reg_key(root, path, name, NULL);
531 if (val)
533 HeapFree(GetProcessHeap(), 0, val);
534 return TRUE;
537 return FALSE;
540 static void process_setting(struct setting *s)
542 if (s->value)
544 WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
545 set_config_key(s->root, s->path, s->name, s->value, s->type);
547 else
549 /* NULL name means remove that path/section entirely */
550 if (s->path && s->name) remove_value(s->root, s->path, s->name);
551 else if (s->path && !s->name) remove_path(s->root, s->path);
555 void apply(void)
557 if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
559 WINE_TRACE("()\n");
561 while (!list_empty(settings))
563 struct setting *s = (struct setting *) list_head(settings);
564 process_setting(s);
565 free_setting(s);
569 /* ================================== utility functions ============================ */
571 char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
573 /* returns a registry key path suitable for passing to addTransaction */
574 char *keypath(const char *section)
576 static char *result = NULL;
578 HeapFree(GetProcessHeap(), 0, result);
580 if (current_app)
582 result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
583 sprintf(result, "AppDefaults\\%s", current_app);
584 if (section[0]) sprintf( result + strlen(result), "\\%s", section );
586 else
588 result = strdupA(section);
591 return result;
594 void PRINTERROR(void)
596 LPSTR msg;
598 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
599 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
600 (LPSTR)&msg, 0, NULL);
602 /* eliminate trailing newline, is this a Wine bug? */
603 *(strrchr(msg, '\r')) = '\0';
605 WINE_TRACE("error: '%s'\n", msg);
608 int initialize(HINSTANCE hInstance)
610 DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
612 if (res != ERROR_SUCCESS) {
613 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
614 return 1;
617 /* load any menus */
618 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));
620 /* we could probably just have the list as static data */
621 settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
622 list_init(settings);
624 return 0;