- All settings in the drive edit dialog are now instant apply.
[wine] / programs / winecfg / winecfg.c
1 /*
2  * WineCfg configuration management
3  *
4  * Copyright 2002 Jaco Greeff
5  * Copyright 2003 Dimitrie O. Paun
6  * Copyright 2003 Mike Hearn
7  *
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.
12  *
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.
17  *
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
21  *
22  * TODO:   (in rough order of priority)
23  *   - A mind bogglingly vast amount of stuff
24  *
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
30  *   - DLL editing
31  *   - Make a list of prefs that have been dropped from the UI/should be eliminated
32  *   - Multimedia page
33  *   - Settings migration code (from old configs)
34  *
35  */
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <limits.h>
40 #include <windows.h>
41 #include <winreg.h>
42 #include <wine/debug.h>
43
44 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
45
46 #include "winecfg.h"
47
48 HKEY configKey = NULL;
49
50
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);
55         return 1;
56     }
57     return 0;
58 }
59
60
61 /*****************************************************************************
62  * getConfigValue: Retrieves a configuration value from the registry
63  *
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
67  *
68  * Returns a buffer holding the value if successful, NULL if not. Caller is responsible for freeing the result.
69  *
70  */
71 char *getConfigValue (char *subkey, char *valueName, char *defaultResult)
72 {
73     char *buffer = NULL;
74     DWORD dataLength;
75     HKEY hSubKey = NULL;
76     DWORD res;
77
78     WINE_TRACE("subkey=%s, valueName=%s, defaultResult=%s\n", subkey, valueName, defaultResult);
79
80     res = RegOpenKeyEx( configKey, subkey, 0, KEY_ALL_ACCESS, &hSubKey );
81     if(res != ERROR_SUCCESS)  {
82         if( res==ERROR_FILE_NOT_FOUND )
83         {
84             WINE_TRACE("Section key not present - using default\n");
85             return strdup(defaultResult);
86         }
87         else
88         {
89             WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
90         }
91         goto end;
92     }
93     
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);
98         goto end;
99     } else if( res!=ERROR_SUCCESS )  {
100         WINE_ERR("Couldn't query value's length (res=%ld)\n", res );
101         goto end;
102     }
103
104     buffer = malloc(dataLength);
105     if( buffer==NULL )
106     {
107         WINE_ERR("Couldn't allocate %lu bytes for the value\n", dataLength );
108         goto end;
109     }
110     
111     RegQueryValueEx(hSubKey, valueName, NULL, NULL, (LPBYTE)buffer, &dataLength);
112     
113 end:
114     if( hSubKey!=NULL )
115         RegCloseKey( hSubKey );
116
117     return buffer;
118     
119 }
120
121 /*****************************************************************************
122  * setConfigValue : Sets a configuration key in the registry. Section will be created if it doesn't already exist
123  *
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
128  *
129  * Returns 0 on success, non-zero otherwise
130  *
131  */
132 int setConfigValue (char *subkey, char *valueName, const char *value) {
133     DWORD res = 1;
134     HKEY key = NULL;
135
136     WINE_TRACE("subkey=%s, valueName=%s, value=%s\n", subkey, valueName, value);
137
138     assert( subkey != NULL );
139     assert( valueName != NULL );
140     assert( value != NULL );
141     
142     res = RegCreateKey(configKey, subkey, &key);
143     if (res != ERROR_SUCCESS) goto end;
144
145     res = RegSetValueEx(key, valueName, 0, REG_SZ, value, strlen(value) + 1);
146     if (res != ERROR_SUCCESS) goto end;
147
148     res = 0;
149 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);
152     return res;
153 }
154
155 /* returns 0 on success, an HRESULT from the registry funtions otherwise */
156 HRESULT doesConfigValueExist(char *subkey, char *valueName) {
157     HRESULT hr;
158     HKEY key;
159
160     WINE_TRACE("subkey=%s, valueName=%s - ", subkey, valueName);
161     
162     hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
163     if (hr != S_OK) {
164         WINE_TRACE("no: subkey does not exist\n");
165         return hr;
166     }
167
168     hr = RegQueryValueEx(key, valueName, NULL, NULL, NULL, NULL);
169     if (hr != S_OK) {
170         WINE_TRACE("no: key does not exist\n");
171         return hr;
172     }
173
174     RegCloseKey(key);
175     WINE_TRACE("yes\n");
176     return S_OK;
177 }
178
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) {
181     HRESULT hr;
182     HKEY key;
183     WINE_TRACE("subkey=%s, valueName=%s\n", subkey, valueName);
184     
185     hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
186     if (hr != S_OK) return hr;
187
188     hr = RegDeleteValue(key, valueName);
189     if (hr != ERROR_SUCCESS) return hr;
190
191     return S_OK;
192 }
193
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) {
197     HRESULT hr;
198     WINE_TRACE("section=%s\n", section);
199
200     return hr = RegDeleteKey(configKey, section);
201 }
202
203
204 /* ========================================================================= */
205 /* Transaction management code */
206
207 struct transaction *tqhead, *tqtail;
208 int instantApply = 1;
209
210 void destroyTransaction(struct transaction *trans) {
211     assert( trans != NULL );
212     
213     WINE_TRACE("destroying %p\n", trans);
214     
215     free(trans->section);
216     if (trans->key) free(trans->key);
217     if (trans->newValue) free(trans->newValue);
218     
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;
223     
224     free(trans);
225 }
226
227 void addTransaction(char *section, char *key, enum transaction_action action, char *newValue) {
228     struct transaction *trans = calloc(sizeof(struct transaction),1);
229     
230     assert( section != NULL );
231     if (action == ACTION_SET) assert( newValue != NULL );
232     if (action == ACTION_SET) assert( key != NULL );
233                                      
234     trans->section = strdup(section);
235     if (key) trans->key = strdup(key);
236     if (newValue) trans->newValue = strdup(newValue);
237     trans->action = action;
238     
239     if (tqtail == NULL) {
240         tqtail = trans;
241         tqhead = tqtail;
242     } else {
243         tqhead->next = trans;
244         trans->prev = tqhead;
245         tqhead = trans;
246     }
247
248     if (instantApply) {
249         processTransaction(trans);
250         destroyTransaction(trans);
251     }
252 }
253
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) {
259         if (trans->key) {
260             WINE_TRACE("Removing %s\\%s\n", trans->section, trans->key);
261             removeConfigValue(trans->section, trans->key);
262         } else {
263             /* NULL key means remove that section entirely */
264             WINE_TRACE("Removing section %s\n", trans->section);
265             removeConfigSection(trans->section);
266         }
267     }
268     /* TODO: implement notifications here */
269 }
270
271 void processTransQueue(void)
272 {
273     WINE_TRACE("\n");
274     while (tqtail != NULL) {
275         struct transaction *next = tqtail->next;
276         processTransaction(tqtail);
277         destroyTransaction(tqtail);
278         tqtail = next;  
279     }
280 }
281
282 /* ================================== utility functions ============================ */
283
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;
290     return result;
291 }