2 * WineCfg configuration management
4 * Copyright 2002 Jaco Greeff
5 * Copyright 2003 Dimitrie O. Paun
6 * Copyright 2003 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: (in rough order of priority)
23 * - A mind bogglingly vast amount of stuff
25 * - Complete X11DRV page, so all controls are hooked up
26 * - Implement autodetect for drive configuration
27 * - Figure out whether we need the virtual vs real drive selection stuff at the top of the property page
28 * - Implement explicit mode vs instant-apply mode
29 * - AppDefaults handling
31 * - Make a list of prefs that have been dropped from the UI/should be eliminated
33 * - Settings migration code (from old configs)
42 #include <wine/debug.h>
44 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
48 HKEY configKey = NULL;
51 int initialize(void) {
52 DWORD res = RegCreateKey(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT, &configKey);
53 if (res != ERROR_SUCCESS) {
54 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
61 /*****************************************************************************
62 * getConfigValue: Retrieves a configuration value from the registry
64 * char *subKey : the name of the config section
65 * char *valueName : the name of the config value
66 * char *defaultResult : if the key isn't found, return this value instead
68 * Returns a buffer holding the value if successful, NULL if not. Caller is responsible for freeing the result.
71 char *getConfigValue (char *subkey, char *valueName, char *defaultResult)
78 WINE_TRACE("subkey=%s, valueName=%s, defaultResult=%s\n", subkey, valueName, defaultResult);
80 res = RegOpenKeyEx( configKey, subkey, 0, KEY_ALL_ACCESS, &hSubKey );
81 if(res != ERROR_SUCCESS) {
82 if( res==ERROR_FILE_NOT_FOUND )
84 WINE_TRACE("Section key not present - using default\n");
85 return strdup(defaultResult);
89 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
94 res = RegQueryValueExA( hSubKey, valueName, NULL, NULL, NULL, &dataLength);
95 if( res == ERROR_FILE_NOT_FOUND ) {
96 WINE_TRACE("Value not present - using default\n");
97 buffer = strdup(defaultResult);
99 } else if( res!=ERROR_SUCCESS ) {
100 WINE_ERR("Couldn't query value's length (res=%ld)\n", res );
104 buffer = malloc(dataLength);
107 WINE_ERR("Couldn't allocate %lu bytes for the value\n", dataLength );
111 RegQueryValueEx(hSubKey, valueName, NULL, NULL, (LPBYTE)buffer, &dataLength);
115 RegCloseKey( hSubKey );
121 /*****************************************************************************
122 * setConfigValue : Sets a configuration key in the registry. Section will be created if it doesn't already exist
124 * HKEY hCurrent : the registry key that the configuration is rooted at
125 * char *subKey : the name of the config section
126 * char *valueName : the name of the config value
127 * char *value : the value to set the configuration key to
129 * Returns 0 on success, non-zero otherwise
132 int setConfigValue (char *subkey, char *valueName, const char *value) {
136 WINE_TRACE("subkey=%s, valueName=%s, value=%s\n", subkey, valueName, value);
138 assert( subkey != NULL );
139 assert( valueName != NULL );
140 assert( value != NULL );
142 res = RegCreateKey(configKey, subkey, &key);
143 if (res != ERROR_SUCCESS) goto end;
145 res = RegSetValueEx(key, valueName, 0, REG_SZ, value, strlen(value) + 1);
146 if (res != ERROR_SUCCESS) goto end;
150 if (key) RegCloseKey(key);
151 if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", valueName, subkey, value, res);
155 /* returns 0 on success, an HRESULT from the registry funtions otherwise */
156 HRESULT doesConfigValueExist(char *subkey, char *valueName) {
160 WINE_TRACE("subkey=%s, valueName=%s - ", subkey, valueName);
162 hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
164 WINE_TRACE("no: subkey does not exist\n");
168 hr = RegQueryValueEx(key, valueName, NULL, NULL, NULL, NULL);
170 WINE_TRACE("no: key does not exist\n");
179 /* removes the requested value from the registry, however, does not remove the section if empty. Returns S_OK (0) on success. */
180 HRESULT removeConfigValue(char *subkey, char *valueName) {
183 WINE_TRACE("subkey=%s, valueName=%s\n", subkey, valueName);
185 hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
186 if (hr != S_OK) return hr;
188 hr = RegDeleteValue(key, valueName);
189 if (hr != ERROR_SUCCESS) return hr;
194 /* removes the requested configuration section (subkey) from the registry, assuming it exists */
195 /* this function might be slightly pointless, but in future we may wish to treat recursion specially etc, so we'll keep it for now */
196 HRESULT removeConfigSection(char *section) {
198 WINE_TRACE("section=%s\n", section);
200 return hr = RegDeleteKey(configKey, section);
204 /* ========================================================================= */
205 /* Transaction management code */
207 struct transaction *tqhead, *tqtail;
208 int instantApply = 1;
210 void destroyTransaction(struct transaction *trans) {
211 assert( trans != NULL );
213 WINE_TRACE("destroying %p\n", trans);
215 free(trans->section);
216 if (trans->key) free(trans->key);
217 if (trans->newValue) free(trans->newValue);
219 if (trans->next) trans->next->prev = trans->prev;
220 if (trans->prev) trans->prev->next = trans->next;
221 if (trans == tqhead) tqhead = NULL;
222 if (trans == tqtail) tqtail = NULL;
227 void addTransaction(char *section, char *key, enum transaction_action action, char *newValue) {
228 struct transaction *trans = calloc(sizeof(struct transaction),1);
230 assert( section != NULL );
231 if (action == ACTION_SET) assert( newValue != NULL );
232 if (action == ACTION_SET) assert( key != NULL );
234 trans->section = strdup(section);
235 if (key) trans->key = strdup(key);
236 if (newValue) trans->newValue = strdup(newValue);
237 trans->action = action;
239 if (tqtail == NULL) {
243 tqhead->next = trans;
244 trans->prev = tqhead;
249 processTransaction(trans);
250 destroyTransaction(trans);
254 void processTransaction(struct transaction *trans) {
255 if (trans->action == ACTION_SET) {
256 WINE_TRACE("Setting %s\\%s to '%s'\n", trans->section, trans->key, trans->newValue);
257 setConfigValue(trans->section, trans->key, trans->newValue);
258 } else if (trans->action == ACTION_REMOVE) {
260 WINE_TRACE("Removing %s\\%s\n", trans->section, trans->key);
261 removeConfigValue(trans->section, trans->key);
263 /* NULL key means remove that section entirely */
264 WINE_TRACE("Removing section %s\n", trans->section);
265 removeConfigSection(trans->section);
268 /* TODO: implement notifications here */
271 void processTransQueue(void)
274 while (tqtail != NULL) {
275 struct transaction *next = tqtail->next;
276 processTransaction(tqtail);
277 destroyTransaction(tqtail);
282 /* ================================== utility functions ============================ */
284 /* returns a string with the window text of the dialog item. user is responsible for freeing the result */
285 char *getDialogItemText(HWND hDlg, WORD controlID) {
286 HWND item = GetDlgItem(hDlg, controlID);
287 int len = GetWindowTextLength(item) + 1;
288 char *result = malloc(len);
289 if (GetWindowText(item, result, len) == 0) return NULL;