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
28 #include <wine/debug.h>
30 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
34 HKEY configKey = NULL;
36 /*****************************************************************************
38 WINECFG_DESC* allocConfig(void)
40 WINECFG_DESC* pWineCfg = malloc (sizeof (WINECFG_DESC));
42 if (!pWineCfg) goto fail;
43 ZeroMemory(pWineCfg, sizeof(*pWineCfg));
45 pWineCfg->pDlls = DPA_Create(100);
46 if (!pWineCfg->pDlls) goto fail;
47 pWineCfg->pApps = DPA_Create(100);
48 if (!pWineCfg->pApps) goto fail;
53 /* FIXME: do something nice */
54 printf("Out of memory");
59 /*****************************************************************************
61 int freeConfig (WINECFG_DESC* pCfg)
65 for (i = 0; i < pCfg->pDlls->nItemCount; i++)
66 free (DPA_GetPtr(pCfg->pDlls, i));
67 DPA_Destroy(pCfg->pDlls);
69 for (i = 0; i < pCfg->pApps->nItemCount; i++)
70 free (DPA_GetPtr(pCfg->pApps, i));
71 DPA_Destroy(pCfg->pApps);
78 /*****************************************************************************
79 * getConfigValue: Retrieves a configuration value from the registry
81 * HKEY hCurrent : the registry key that the configuration is rooted at
82 * char *subKey : the name of the config section
83 * char *valueName : the name of the config value
84 * char *retVal : pointer to the start of a buffer that has room for >= length chars
85 * int length : length of the buffer pointed to by retVal
86 * char *defaultResult : if the key isn't found, return this value instead
88 * Returns 0 upon success, non-zero otherwise
91 int getConfigValue (HKEY hCurrent, char *subkey, char *valueName, char *retVal, int length, char *defaultResult)
96 DWORD res = 1; /* assume failure */
98 WINE_TRACE("subkey=%s, valueName=%s, defaultResult=%s\n", subkey, valueName, defaultResult);
100 if( (res=RegOpenKeyEx( hCurrent, subkey, 0, KEY_ALL_ACCESS, &hSubKey ))
103 if( res==ERROR_FILE_NOT_FOUND )
105 WINE_TRACE("Section key not present - using default\n");
106 strncpy(retVal, defaultResult, length);
110 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
115 res = RegQueryValueExA( hSubKey, valueName, NULL, NULL, NULL, &dataLength);
116 if( res == ERROR_FILE_NOT_FOUND )
118 WINE_TRACE("Value not present - using default\n");
119 strncpy(retVal, defaultResult, length);
123 if( res!=ERROR_SUCCESS )
125 WINE_ERR("Couldn't query value's length (res=%ld)\n", res );
129 buffer=malloc( dataLength );
132 WINE_ERR("Couldn't allocate %lu bytes for the value\n", dataLength );
136 RegQueryValueEx(hSubKey, valueName, NULL, NULL, (LPBYTE)buffer, &dataLength);
137 strncpy(retVal, buffer, length);
143 RegCloseKey( hSubKey );
149 /*****************************************************************************
150 * setConfigValue : Sets a configuration key in the registry
152 * HKEY hCurrent : the registry key that the configuration is rooted at
153 * char *subKey : the name of the config section
154 * char *valueName : the name of the config value
155 * char *value : the value to set the configuration key to
157 * Returns 0 on success, non-zero otherwise
160 int setConfigValue (HKEY hCurrent, char *subkey, char *valueName, const char *value) {
164 WINE_TRACE("subkey=%s, valueName=%s, value=%s\n", subkey, valueName, value);
166 res = RegCreateKey(hCurrent, subkey, &key);
167 if (res != ERROR_SUCCESS) goto end;
169 res = RegSetValueEx(key, valueName, 0, REG_SZ, value, strlen(value) + 1);
170 if (res != ERROR_SUCCESS) goto end;
174 if (key) RegCloseKey(key);
175 if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", valueName, subkey, value, res);
180 /*****************************************************************************
182 * Description: Loads and populates a configuration structure
184 * Returns : 0 on success, -1 otherwise
186 * FIXME: We are supposed to load these values from the registry.
187 * This is not active yet, so just setup some (hopefully)
190 int loadConfig (WINECFG_DESC* pCfg)
192 const DLL_DESC *pDllDefaults;
193 char buffer[MAX_PATH];
198 res = RegCreateKey(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT, &configKey);
199 if (res != ERROR_SUCCESS)
201 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
205 /* Windows and DOS versions */
206 getConfigValue(configKey, "Version", "Windows", pCfg->szWinVer, MAX_VERSION_LENGTH, "win95");
207 getConfigValue(configKey, "Version", "DOS", pCfg->szDOSVer, MAX_VERSION_LENGTH, "6.22");
208 getConfigValue(configKey, "Tweak.Layout", "WineLook", pCfg->szWinLook, MAX_VERSION_LENGTH, "win95");
211 getConfigValue(configKey, "Wine", "Windows", pCfg->szWinDir, MAX_PATH, "c:\\Windows");
212 getConfigValue(configKey, "Wine", "System", pCfg->szWinSysDir, MAX_PATH, "c:\\Windows\\System");
213 getConfigValue(configKey, "Wine", "Temp", pCfg->szWinTmpDir, MAX_PATH, "c:\\Windows\\Temp");
214 getConfigValue(configKey, "Wine", "Profile", pCfg->szWinProfDir, MAX_PATH, "c:\\Windows\\Profiles\\Administrator");
215 getConfigValue(configKey, "Wine", "Path", pCfg->szWinPath, MAX_PATH, "c:\\Windows;c:\\Windows\\System");
217 /* Graphics driver */
218 getConfigValue(configKey, "Wine", "GraphicsDriver", pCfg->szGraphDriver, MAX_NAME_LENGTH, "x11drv");
221 * DLL defaults for all applications is built using
222 * the default DLL structure
224 for (pDllDefaults = getDLLDefaults (); *pDllDefaults->szName; pDllDefaults++)
226 DLL_DESC *pDll = malloc(sizeof(DLL_DESC));
227 memcpy (pDll, pDllDefaults, sizeof(DLL_DESC));
228 DPA_InsertPtr(pCfg->pDlls, INT_MAX, pDll);
232 * Application defaults on a per application
233 * level (if not set, this defaults to what
237 /* FIXME: Finish these off. Do we actually need GUI for all of them? */
242 getConfigValue(configKey, "x11drv", "Display", pCfg->sX11Drv.szX11Display, sizeof(pCfg->sX11Drv.szX11Display), ":0.0");
244 getConfigValue(configKey, "x11drv", "AllocSystemColors", buffer, sizeof(buffer), "100");
245 pCfg->sX11Drv.nSysColors = atoi(buffer);
247 getConfigValue(configKey, "x11drv", "PrivateColorMap", buffer, sizeof(buffer), "N");
248 pCfg->sX11Drv.nPrivateMap = IS_OPTION_TRUE(buffer[0]);
250 getConfigValue(configKey, "x11drv", "PerfectGraphics", buffer, sizeof(buffer), "N");
251 pCfg->sX11Drv.nPerfect = IS_OPTION_TRUE(buffer[0]);
253 getConfigValue(configKey, "x11drv", "Desktop", buffer, sizeof(buffer), "640x480");
254 sscanf(buffer, "%dx%d", &pCfg->sX11Drv.nDesktopSizeX, &pCfg->sX11Drv.nDesktopSizeY);
256 pCfg->sX11Drv.nTextCP = 0;
257 pCfg->sX11Drv.nXVideoPort = 43;
258 pCfg->sX11Drv.nDepth = 16;
259 pCfg->sX11Drv.nManaged = 1;
260 pCfg->sX11Drv.nDesktopSizeX = 640;
261 pCfg->sX11Drv.nDesktopSizeY = 480;
262 pCfg->sX11Drv.nDGA = 1;
263 pCfg->sX11Drv.nXVidMode = 1;
264 pCfg->sX11Drv.nTakeFocus = 1;
265 pCfg->sX11Drv.nDXGrab = 0;
266 pCfg->sX11Drv.nDoubleBuffered = 0;
267 pCfg->sX11Drv.nSynchronous = 1;
269 RegCloseKey( configKey );
273 /*****************************************************************************
274 * saveConfig : Stores the configuration structure
276 * Returns : 0 on success, -1 otherwise
278 * FIXME: This is where we are to write the changes to the registry.
279 * This is not setup yet, so do nothing and say ok.
281 int saveConfig (const WINECFG_DESC* pCfg)
288 res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT, 0, KEY_ALL_ACCESS, &key);
289 if (res != ERROR_SUCCESS) {
290 WINE_ERR("Failed to open Wine config registry branch, res=%ld\n", res);
294 /* Windows and DOS versions */
295 setConfigValue(key, "Version", "Windows", pCfg->szWinVer);
297 WINE_FIXME("We don't write out the entire configuration yet\n");