Avoid importing _strlwr/_strupr from ntdll.
[wine/multimedia.git] / programs / winecfg / winecfg.c
blobdf2b05ffa196747fc516f1318a0f72af5263be36
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 #define WIN32_LEAN_AND_MEAN
32 #include <assert.h>
33 #include <stdio.h>
34 #include <limits.h>
35 #include <windows.h>
36 #include <winreg.h>
37 #include <wine/debug.h>
38 #include <wine/list.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
42 #include "winecfg.h"
43 #include "resource.h"
45 HKEY config_key = NULL;
46 HMENU hPopupMenus = 0;
49 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
51 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
52 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
54 void set_window_title(HWND dialog)
56 char *newtitle;
58 /* update the window title */
59 if (current_app)
61 const char *template = "Wine Configuration for %s";
62 newtitle = HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(current_app) + 1);
63 sprintf(newtitle, template, current_app);
65 else
67 newtitle = strdupA("Wine Configuration");
70 WINE_TRACE("setting title to %s\n", newtitle);
71 SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
72 HeapFree(GetProcessHeap(), 0, newtitle);
76 /**
77 * get_config_key: Retrieves a configuration value from the registry
79 * char *subkey : the name of the config section
80 * char *name : the name of the config value
81 * char *default : if the key isn't found, return this value instead
83 * Returns a buffer holding the value if successful, NULL if
84 * not. Caller is responsible for releasing the result.
87 static char *get_config_key (HKEY root, const char *subkey, const char *name, const char *def)
89 LPSTR buffer = NULL;
90 DWORD len;
91 HKEY hSubKey = NULL;
92 DWORD res;
94 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
96 res = RegOpenKey(root, subkey, &hSubKey);
97 if (res != ERROR_SUCCESS)
99 if (res == ERROR_FILE_NOT_FOUND)
101 WINE_TRACE("Section key not present - using default\n");
102 return def ? strdupA(def) : NULL;
104 else
106 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
108 goto end;
111 res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
112 if (res == ERROR_FILE_NOT_FOUND)
114 WINE_TRACE("Value not present - using default\n");
115 buffer = def ? strdupA(def) : NULL;
116 goto end;
117 } else if (res != ERROR_SUCCESS)
119 WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
120 goto end;
123 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
125 RegQueryValueEx(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
127 WINE_TRACE("buffer=%s\n", buffer);
128 end:
129 if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
131 return buffer;
135 * set_config_key: convenience wrapper to set a key/value pair
137 * const char *subKey : the name of the config section
138 * const char *valueName : the name of the config value
139 * const char *value : the value to set the configuration key to
141 * Returns 0 on success, non-zero otherwise
143 * If valueName or value is NULL, an empty section will be created
145 static int set_config_key(HKEY root, const char *subkey, const char *name, const void *value, DWORD type) {
146 DWORD res = 1;
147 HKEY key = NULL;
149 WINE_TRACE("subkey=%s: name=%s, value=%p, type=%ld\n", subkey, name, value, type);
151 assert( subkey != NULL );
153 if (subkey[0])
155 res = RegCreateKey(root, subkey, &key);
156 if (res != ERROR_SUCCESS) goto end;
158 else key = root;
159 if (name == NULL || value == NULL) goto end;
161 switch (type)
163 case REG_SZ: res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1); break;
164 case REG_DWORD: res = RegSetValueEx(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
166 if (res != ERROR_SUCCESS) goto end;
168 res = 0;
169 end:
170 if (key && key != root) RegCloseKey(key);
171 if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s, res=%ld\n", name, subkey, res);
172 return res;
175 /* removes the requested value from the registry, however, does not
176 * remove the section if empty. Returns S_OK (0) on success.
178 static HRESULT remove_value(HKEY root, const char *subkey, const char *name)
180 HRESULT hr;
181 HKEY key;
183 WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
185 hr = RegOpenKey(root, subkey, &key);
186 if (hr != S_OK) return hr;
188 hr = RegDeleteValue(key, name);
189 if (hr != ERROR_SUCCESS) return hr;
191 return S_OK;
194 /* removes the requested subkey from the registry, assuming it exists */
195 static LONG remove_path(HKEY root, char *section) {
196 HKEY branch_key;
197 DWORD max_sub_key_len;
198 DWORD subkeys;
199 DWORD curr_len;
200 LONG ret = ERROR_SUCCESS;
201 long int i;
202 char *buffer;
204 WINE_TRACE("section=%s\n", section);
206 if ((ret = RegOpenKey(root, section, &branch_key)) != ERROR_SUCCESS)
207 return ret;
209 /* get size information and resize the buffers if necessary */
210 if ((ret = RegQueryInfoKey(branch_key, NULL, NULL, NULL,
211 &subkeys, &max_sub_key_len,
212 NULL, NULL, NULL, NULL, NULL, NULL
213 )) != ERROR_SUCCESS)
214 return ret;
216 curr_len = strlen(section);
217 buffer = HeapAlloc(GetProcessHeap(), 0, max_sub_key_len + curr_len + 1);
218 strcpy(buffer, section);
220 buffer[curr_len] = '\\';
221 for (i = subkeys - 1; i >= 0; i--)
223 DWORD buf_len = max_sub_key_len - curr_len - 1;
225 ret = RegEnumKeyEx(branch_key, i, buffer + curr_len + 1,
226 &buf_len, NULL, NULL, NULL, NULL);
227 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
228 ret != ERROR_NO_MORE_ITEMS)
229 break;
230 else
231 remove_path(root, buffer);
233 HeapFree(GetProcessHeap(), 0, buffer);
234 RegCloseKey(branch_key);
236 return RegDeleteKey(root, section);
240 /* ========================================================================= */
242 /* This code exists for the following reasons:
244 * - It makes working with the registry easier
245 * - By storing a mini cache of the registry, we can more easily implement
246 * cancel/revert and apply. The 'settings list' is an overlay on top of
247 * the actual registry data that we can write out at will.
249 * Rather than model a tree in memory, we simply store each absolute (rooted
250 * at the config key) path.
254 struct setting
256 struct list entry;
257 HKEY root; /* the key on which path is rooted */
258 char *path; /* path in the registry rooted at root */
259 char *name; /* name of the registry value. if null, this means delete the key */
260 char *value; /* contents of the registry value. if null, this means delete the value */
261 DWORD type; /* type of registry value. REG_SZ or REG_DWORD for now */
264 struct list *settings;
266 static void free_setting(struct setting *setting)
268 assert( setting != NULL );
269 assert( setting->path );
271 WINE_TRACE("destroying %p: %s\n", setting, setting->path);
273 HeapFree(GetProcessHeap(), 0, setting->path);
274 HeapFree(GetProcessHeap(), 0, setting->name);
275 HeapFree(GetProcessHeap(), 0, setting->value);
277 list_remove(&setting->entry);
279 HeapFree(GetProcessHeap(), 0, setting);
283 * Returns the contents of the value at path. If not in the settings
284 * list, it will be fetched from the registry - failing that, the
285 * default will be used.
287 * If already in the list, the contents as given there will be
288 * returned. You are expected to HeapFree the result.
290 char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
292 struct list *cursor;
293 struct setting *s;
294 char *val;
296 WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
298 /* check if it's in the list */
299 LIST_FOR_EACH( cursor, settings )
301 s = LIST_ENTRY(cursor, struct setting, entry);
303 if (root != s->root) continue;
304 if (strcasecmp(path, s->path) != 0) continue;
305 if (strcasecmp(name, s->name) != 0) continue;
307 WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
308 return s->value ? strdupA(s->value) : NULL;
311 /* no, so get from the registry */
312 val = get_config_key(root, path, name, def);
314 WINE_TRACE("returning %s\n", val);
316 return val;
320 * Used to set a registry key.
322 * path is rooted at the config key, ie use "Version" or
323 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
324 * to get such a string.
326 * name is the value name, or NULL to delete the path.
328 * value is what to set the value to, or NULL to delete it.
330 * type is REG_SZ or REG_DWORD.
332 * These values will be copied when necessary.
334 static void set_reg_key_ex(HKEY root, const char *path, const char *name, const void *value, DWORD type)
336 struct list *cursor;
337 struct setting *s;
339 assert( path != NULL );
341 WINE_TRACE("path=%s, name=%s, value=%p\n", path, name, value);
343 /* firstly, see if we already set this setting */
344 LIST_FOR_EACH( cursor, settings )
346 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
348 if (root != s->root) continue;
349 if (strcasecmp(s->path, path) != 0) continue;
350 if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;
352 /* are we attempting a double delete? */
353 if (!s->name && !name) return;
355 /* do we want to undelete this key? */
356 if (!s->name && name) s->name = strdupA(name);
358 /* yes, we have already set it, so just replace the content and return */
359 HeapFree(GetProcessHeap(), 0, s->value);
360 s->type = type;
361 switch (type)
363 case REG_SZ:
364 s->value = value ? strdupA(value) : NULL;
365 break;
366 case REG_DWORD:
367 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
368 memcpy( s->value, value, sizeof(DWORD) );
369 break;
372 /* are we deleting this key? this won't remove any of the
373 * children from the overlay so if the user adds it again in
374 * that session it will appear to undelete the settings, but
375 * in reality only the settings actually modified by the user
376 * in that session will be restored. we might want to fix this
377 * corner case in future by actually deleting all the children
378 * here so that once it's gone, it's gone.
380 if (!name) s->name = NULL;
382 return;
385 /* otherwise add a new setting for it */
386 s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
387 s->root = root;
388 s->path = strdupA(path);
389 s->name = name ? strdupA(name) : NULL;
390 s->type = type;
391 switch (type)
393 case REG_SZ:
394 s->value = value ? strdupA(value) : NULL;
395 break;
396 case REG_DWORD:
397 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
398 memcpy( s->value, value, sizeof(DWORD) );
399 break;
402 list_add_tail(settings, &s->entry);
405 void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
407 set_reg_key_ex(root, path, name, value, REG_SZ);
410 void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
412 set_reg_key_ex(root, path, name, &value, REG_DWORD);
416 * enumerates the value names at the given path, taking into account
417 * the changes in the settings list.
419 * you are expected to HeapFree each element of the array, which is null
420 * terminated, as well as the array itself.
422 char **enumerate_values(HKEY root, char *path)
424 HKEY key;
425 DWORD res, i = 0;
426 char **values = NULL;
427 int valueslen = 0;
428 struct list *cursor;
430 res = RegOpenKey(root, path, &key);
431 if (res == ERROR_SUCCESS)
433 while (TRUE)
435 char name[1024];
436 DWORD namesize = sizeof(name);
437 BOOL removed = FALSE;
439 /* find out the needed size, allocate a buffer, read the value */
440 if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
441 break;
443 WINE_TRACE("name=%s\n", name);
445 /* check if this value name has been removed in the settings list */
446 LIST_FOR_EACH( cursor, settings )
448 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
449 if (strcasecmp(s->path, path) != 0) continue;
450 if (strcasecmp(s->name, name) != 0) continue;
452 if (!s->value)
454 WINE_TRACE("this key has been removed, so skipping\n");
455 removed = TRUE;
456 break;
460 if (removed) /* this value was deleted by the user, so don't include it */
462 HeapFree(GetProcessHeap(), 0, name);
463 i++;
464 continue;
467 /* grow the array if necessary, add buffer to it, iterate */
468 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
469 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
471 values[valueslen++] = strdupA(name);
472 WINE_TRACE("valueslen is now %d\n", valueslen);
473 i++;
476 else
478 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
481 WINE_TRACE("adding settings in list but not registry\n");
483 /* now we have to add the values that aren't in the registry but are in the settings list */
484 LIST_FOR_EACH( cursor, settings )
486 struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
487 BOOL found = FALSE;
489 if (strcasecmp(setting->path, path) != 0) continue;
491 if (!setting->value) continue;
493 for (i = 0; i < valueslen; i++)
495 if (strcasecmp(setting->name, values[i]) == 0)
497 found = TRUE;
498 break;
502 if (found) continue;
504 WINE_TRACE("%s in list but not registry\n", setting->name);
506 /* otherwise it's been set by the user but isn't in the registry */
507 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
508 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
510 values[valueslen++] = strdupA(setting->name);
513 WINE_TRACE("adding null terminator\n");
514 if (values)
516 values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
517 values[valueslen] = NULL;
520 RegCloseKey(key);
522 return values;
526 * returns true if the given key/value pair exists in the registry or
527 * has been written to.
529 BOOL reg_key_exists(HKEY root, const char *path, const char *name)
531 char *val = get_reg_key(root, path, name, NULL);
533 if (val)
535 HeapFree(GetProcessHeap(), 0, val);
536 return TRUE;
539 return FALSE;
542 static void process_setting(struct setting *s)
544 if (s->value)
546 WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
547 set_config_key(s->root, s->path, s->name, s->value, s->type);
549 else
551 /* NULL name means remove that path/section entirely */
552 if (s->path && s->name) remove_value(s->root, s->path, s->name);
553 else if (s->path && !s->name) remove_path(s->root, s->path);
557 void apply(void)
559 if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
561 WINE_TRACE("()\n");
563 while (!list_empty(settings))
565 struct setting *s = (struct setting *) list_head(settings);
566 process_setting(s);
567 free_setting(s);
571 /* ================================== utility functions ============================ */
573 char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
575 /* returns a registry key path suitable for passing to addTransaction */
576 char *keypath(const char *section)
578 static char *result = NULL;
580 HeapFree(GetProcessHeap(), 0, result);
582 if (current_app)
584 result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
585 sprintf(result, "AppDefaults\\%s", current_app);
586 if (section[0]) sprintf( result + strlen(result), "\\%s", section );
588 else
590 result = strdupA(section);
593 return result;
596 void PRINTERROR(void)
598 LPSTR msg;
600 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
601 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
602 (LPSTR)&msg, 0, NULL);
604 /* eliminate trailing newline, is this a Wine bug? */
605 *(strrchr(msg, '\r')) = '\0';
607 WINE_TRACE("error: '%s'\n", msg);
610 int initialize(HINSTANCE hInstance)
612 DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
614 if (res != ERROR_SUCCESS) {
615 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
616 return 1;
619 /* load any menus */
620 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));
622 /* we could probably just have the list as static data */
623 settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
624 list_init(settings);
626 return 0;