Implemented an array of 26 drives and interface functions for adding,
[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  *   - Implement autodetect for drive configuration
26  *   - Figure out whether we need the virtual vs real drive selection stuff at the top of the property page
27  *   - Implement explicit mode vs instant-apply mode
28  *   - DLL editing
29  *   - Multimedia page
30  *   - Settings migration code (from old configs)
31  *   - Clean up resource.h, it's a bog
32  *
33  *   Minor things that should be done someday:
34  *   - Make the desktop size UI a combo box, with a Custom option, so it's more obvious what you might want to choose here
35  *
36  * BUGS:
37  *   - x11drv page triggers key writes on entry
38  *
39  */
40
41 #include <assert.h>
42 #include <stdio.h>
43 #include <limits.h>
44 #include <windows.h>
45 #include <winreg.h>
46 #include <wine/debug.h>
47
48 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
49
50 #include "winecfg.h"
51
52 HKEY configKey = NULL;
53
54
55 int initialize(void) {
56     DWORD res = RegCreateKey(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT, &configKey);
57     if (res != ERROR_SUCCESS) {
58         WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
59         return 1;
60     }
61     return 0;
62 }
63
64
65 /*****************************************************************************
66  * getConfigValue: Retrieves a configuration value from the registry
67  *
68  * const char *subKey : the name of the config section
69  * const char *valueName : the name of the config value
70  * const char *defaultResult : if the key isn't found, return this value instead
71  *
72  * Returns a buffer holding the value if successful, NULL if not. Caller is responsible for freeing the result.
73  *
74  */
75 char *getConfigValue (const char *subkey, const char *valueName, const char *defaultResult)
76 {
77     char *buffer = NULL;
78     DWORD dataLength;
79     HKEY hSubKey = NULL;
80     DWORD res;
81
82     WINE_TRACE("subkey=%s, valueName=%s, defaultResult=%s\n", subkey, valueName, defaultResult);
83
84     res = RegOpenKeyEx( configKey, subkey, 0, KEY_ALL_ACCESS, &hSubKey );
85     if(res != ERROR_SUCCESS)  {
86         if( res==ERROR_FILE_NOT_FOUND )
87         {
88             WINE_TRACE("Section key not present - using default\n");
89             return strdup(defaultResult);
90         }
91         else
92         {
93             WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
94         }
95         goto end;
96     }
97
98     res = RegQueryValueExA( hSubKey, valueName, NULL, NULL, NULL, &dataLength);
99     if( res == ERROR_FILE_NOT_FOUND ) {
100         WINE_TRACE("Value not present - using default\n");
101         buffer = strdup(defaultResult);
102         goto end;
103     } else if( res!=ERROR_SUCCESS )  {
104         WINE_ERR("Couldn't query value's length (res=%ld)\n", res );
105         goto end;
106     }
107
108     buffer = malloc(dataLength);
109     if( buffer==NULL )
110     {
111         WINE_ERR("Couldn't allocate %lu bytes for the value\n", dataLength );
112         goto end;
113     }
114     
115     RegQueryValueEx(hSubKey, valueName, NULL, NULL, (LPBYTE)buffer, &dataLength);
116     
117 end:
118     if( hSubKey!=NULL )
119         RegCloseKey( hSubKey );
120
121     return buffer;
122     
123 }
124
125 /*****************************************************************************
126  * setConfigValue : Sets a configuration key in the registry. Section will be created if it doesn't already exist
127  *
128  * HKEY  hCurrent : the registry key that the configuration is rooted at
129  * const char *subKey : the name of the config section
130  * const char *valueName : the name of the config value
131  * const char *value : the value to set the configuration key to
132  *
133  * Returns 0 on success, non-zero otherwise
134  * 
135  * If *valueName or *value is NULL, an empty section will be created
136  */
137 int setConfigValue (const char *subkey, const char *valueName, const char *value) {
138     DWORD res = 1;
139     HKEY key = NULL;
140
141     WINE_TRACE("subkey=%s, valueName=%s, value=%s\n", subkey, valueName, value);
142
143     assert( subkey != NULL );
144     
145     res = RegCreateKey(configKey, subkey, &key);
146     if (res != ERROR_SUCCESS) goto end;
147     if (value == NULL || valueName == NULL) goto end;
148
149     res = RegSetValueEx(key, valueName, 0, REG_SZ, value, strlen(value) + 1);
150     if (res != ERROR_SUCCESS) goto end;
151
152     res = 0;
153 end:
154     if (key) RegCloseKey(key);
155     if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", valueName, subkey, value, res);
156     return res;
157 }
158
159 /* returns 0 on success, an HRESULT from the registry funtions otherwise */
160 HRESULT doesConfigValueExist(const char *subkey, const char *valueName) {
161     HRESULT hr;
162     HKEY key;
163
164     WINE_TRACE("subkey=%s, valueName=%s - ", subkey, valueName);
165     
166     hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
167     if (hr != S_OK) {
168         WINE_TRACE("no: subkey does not exist\n");
169         return hr;
170     }
171
172     hr = RegQueryValueEx(key, valueName, NULL, NULL, NULL, NULL);
173     if (hr != S_OK) {
174         WINE_TRACE("no: key does not exist\n");
175         return hr;
176     }
177
178     RegCloseKey(key);
179     WINE_TRACE("yes\n");
180     return S_OK;
181 }
182
183 /* removes the requested value from the registry, however, does not remove the section if empty. Returns S_OK (0) on success. */
184 HRESULT removeConfigValue(const char *subkey, const char *valueName) {
185     HRESULT hr;
186     HKEY key;
187     WINE_TRACE("subkey=%s, valueName=%s\n", subkey, valueName);
188     
189     hr = RegOpenKeyEx(configKey, subkey, 0, KEY_READ, &key);
190     if (hr != S_OK) return hr;
191
192     hr = RegDeleteValue(key, valueName);
193     if (hr != ERROR_SUCCESS) return hr;
194
195     return S_OK;
196 }
197
198 /* removes the requested configuration section (subkey) from the registry, assuming it exists */
199 /* this function might be slightly pointless, but in future we may wish to treat recursion specially etc, so we'll keep it for now */
200 HRESULT removeConfigSection(char *section) {
201     HRESULT hr;
202     WINE_TRACE("section=%s\n", section);
203
204     return hr = RegDeleteKey(configKey, section);
205 }
206
207
208 /* ========================================================================= */
209 /* Transaction management code */
210
211 struct transaction *tqhead, *tqtail;
212 int instantApply = 1;
213
214 void destroyTransaction(struct transaction *trans) {
215     assert( trans != NULL );
216     
217     WINE_TRACE("destroying %p\n", trans);
218     
219     free(trans->section);
220     if (trans->key) free(trans->key);
221     if (trans->newValue) free(trans->newValue);
222     
223     if (trans->next) trans->next->prev = trans->prev;
224     if (trans->prev) trans->prev->next = trans->next;
225     if (trans == tqhead) tqhead = NULL;
226     if (trans == tqtail) tqtail = NULL;
227     
228     free(trans);
229 }
230
231 void addTransaction(const char *section, const char *key, enum transaction_action action, const char *newValue) {
232     struct transaction *trans = calloc(sizeof(struct transaction),1);
233     
234     assert( section != NULL );
235     if (action == ACTION_SET) assert( newValue != NULL );
236     if (action == ACTION_SET) assert( key != NULL );
237                                      
238     trans->section = strdup(section);
239     if (key) trans->key = strdup(key);
240     if (newValue) trans->newValue = strdup(newValue);
241     trans->action = action;
242     
243     if (tqtail == NULL) {
244         tqtail = trans;
245         tqhead = tqtail;
246     } else {
247         tqhead->next = trans;
248         trans->prev = tqhead;
249         tqhead = trans;
250     }
251
252     if (instantApply) {
253         processTransaction(trans);
254         destroyTransaction(trans);
255     }
256 }
257
258 void processTransaction(struct transaction *trans) {
259     if (trans->action == ACTION_SET) {
260         WINE_TRACE("Setting %s\\%s to '%s'\n", trans->section, trans->key, trans->newValue);
261         setConfigValue(trans->section, trans->key, trans->newValue);
262     } else if (trans->action == ACTION_REMOVE) {
263         if (trans->key) {
264             WINE_TRACE("Removing %s\\%s\n", trans->section, trans->key);
265             removeConfigValue(trans->section, trans->key);
266         } else {
267             /* NULL key means remove that section entirely */
268             WINE_TRACE("Removing section %s\n", trans->section);
269             removeConfigSection(trans->section);
270         }
271     }
272     /* TODO: implement notifications here */
273 }
274
275 void processTransQueue(void)
276 {
277     WINE_TRACE("\n");
278     while (tqtail != NULL) {
279         struct transaction *next = tqtail->next;
280         processTransaction(tqtail);
281         destroyTransaction(tqtail);
282         tqtail = next;  
283     }
284 }
285
286 /* ================================== utility functions ============================ */
287
288 /* returns a string with the window text of the dialog item. user is responsible for freeing the result */
289 char *getDialogItemText(HWND hDlg, WORD controlID) {
290     HWND item = GetDlgItem(hDlg, controlID);
291     int len = GetWindowTextLength(item) + 1;
292     char *result = malloc(len);
293     if (GetWindowText(item, result, len) == 0) return NULL;
294     return result;
295 }
296
297 void PRINTERROR(void)
298 {
299         LPSTR msg;
300
301         FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
302                        0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
303                        (LPSTR)&msg, 0, NULL);
304         WINE_TRACE("error: '%s'\n", msg);
305 }