winecfg.c 18.1 KB
Newer Older
1 2 3 4 5
/*
 * WineCfg configuration management
 *
 * Copyright 2002 Jaco Greeff
 * Copyright 2003 Dimitrie O. Paun
6
 * Copyright 2003-2004 Mike Hearn
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21
 *
Mike Hearn's avatar
Mike Hearn committed
22 23 24 25 26 27
 * TODO:
 *  - Use unicode
 *  - Icons in listviews/icons
 *  - Better add app dialog, scan c: for EXE files and add to list in background
 *  - Use [GNOME] HIG style groupboxes rather than win32 style (looks nicer, imho)
 *
28 29
 */

30 31
#define WIN32_LEAN_AND_MEAN

32
#include <assert.h>
33 34 35
#include <stdio.h>
#include <limits.h>
#include <windows.h>
36 37
#include <winreg.h>
#include <wine/debug.h>
38
#include <wine/list.h>
39 40

WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
41 42

#include "winecfg.h"
43
#include "resource.h"
44

45
static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
46

47
HKEY config_key = NULL;
48
HMENU hPopupMenus = 0;
49

50 51 52 53 54 55 56 57

/* this is called from the WM_SHOWWINDOW handlers of each tab page.
 *
 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
 */
void set_window_title(HWND dialog)
{
58
    WCHAR newtitle[256];
59 60

    /* update the window title  */
Mike Hearn's avatar
Mike Hearn committed
61
    if (current_app)
62
    {
63
        WCHAR apptitle[256];
64
        LoadStringW(GetModuleHandleW(NULL), IDS_WINECFG_TITLE_APP, apptitle, ARRAY_SIZE(apptitle));
65
        swprintf(newtitle, ARRAY_SIZE(newtitle), apptitle, current_app);
66
    }
67 68
    else
    {
69
        LoadStringW(GetModuleHandleW(NULL), IDS_WINECFG_TITLE, newtitle, ARRAY_SIZE(newtitle));
70 71
    }

72 73
    WINE_TRACE("setting title to %s\n", wine_dbgstr_w (newtitle));
    SendMessageW (GetParent(dialog), PSM_SETTITLEW, 0, (LPARAM) newtitle);
74 75
}

76

77 78
WCHAR* load_string (UINT id)
{
79
    WCHAR buf[1024];
80 81 82
    int len;
    WCHAR* newStr;

83
    LoadStringW(GetModuleHandleW(NULL), id, buf, ARRAY_SIZE(buf));
84

85 86 87
    len = wcslen(buf);
    newStr = malloc((len + 1) * sizeof(WCHAR));
    memcpy(newStr, buf, len * sizeof(WCHAR));
88 89 90 91
    newStr[len] = 0;
    return newStr;
}

92
/**
93
 * get_config_key: Retrieves a configuration value from the registry
94
 *
95 96 97
 * char *subkey : the name of the config section
 * char *name : the name of the config value
 * char *default : if the key isn't found, return this value instead
98
 *
99 100
 * Returns a buffer holding the value if successful, NULL if
 * not. Caller is responsible for releasing the result.
101
 *
102
 */
103
static WCHAR *get_config_key (HKEY root, const WCHAR *subkey, const WCHAR *name, const WCHAR *def)
104
{
105
    LPWSTR buffer = NULL;
106
    DWORD len;
107
    HKEY hSubKey = NULL;
108
    DWORD res;
109

110 111
    WINE_TRACE("subkey=%s, name=%s, def=%s\n", wine_dbgstr_w(subkey),
               wine_dbgstr_w(name), wine_dbgstr_w(def));
112

113
    res = RegOpenKeyExW(root, subkey, 0, MAXIMUM_ALLOWED, &hSubKey);
114 115 116
    if (res != ERROR_SUCCESS)
    {
        if (res == ERROR_FILE_NOT_FOUND)
117
        {
118
            WINE_TRACE("Section key not present - using default\n");
119
            return wcsdup(def);
120 121 122
        }
        else
        {
123
            WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
124 125 126
        }
        goto end;
    }
127

128
    res = RegQueryValueExW(hSubKey, name, NULL, NULL, NULL, &len);
129 130
    if (res == ERROR_FILE_NOT_FOUND)
    {
131
        WINE_TRACE("Value not present - using default\n");
132
        buffer = wcsdup(def);
133
	goto end;
134
    } else if (res != ERROR_SUCCESS)
135
    {
136
        WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
137 138
        goto end;
    }
139

140
    buffer = malloc(len + sizeof(WCHAR));
141

142
    RegQueryValueExW(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
143

144
    WINE_TRACE("buffer=%s\n", wine_dbgstr_w(buffer));
145
end:
146
    RegCloseKey(hSubKey);
147

Mike McCormack's avatar
Mike McCormack committed
148
    return buffer;
149 150
}

151
/**
152
 * set_config_key: convenience wrapper to set a key/value pair
153
 *
154 155 156
 * const char *subKey : the name of the config section
 * const char *valueName : the name of the config value
 * const char *value : the value to set the configuration key to
157 158
 *
 * Returns 0 on success, non-zero otherwise
159 160
 *
 * If valueName or value is NULL, an empty section will be created
161
 */
162 163
static int set_config_key(HKEY root, const WCHAR *subkey, REGSAM access, const WCHAR *name, const void *value, DWORD type)
{
164 165 166
    DWORD res = 1;
    HKEY key = NULL;

167
    WINE_TRACE("subkey=%s: name=%s, value=%p, type=%ld\n", wine_dbgstr_w(subkey),
168
               wine_dbgstr_w(name), value, type);
169 170

    assert( subkey != NULL );
171

172 173
    if (subkey[0])
    {
174 175
        res = RegCreateKeyExW( root, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
                               access, NULL, &key, NULL );
176 177
        if (res != ERROR_SUCCESS) goto end;
    }
178
    else key = root;
179
    if (name == NULL || value == NULL) goto end;
180

181 182
    switch (type)
    {
183 184
        case REG_SZ: res = RegSetValueExW(key, name, 0, REG_SZ, value, (lstrlenW(value)+1)*sizeof(WCHAR)); break;
        case REG_DWORD: res = RegSetValueExW(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
185
    }
186 187 188 189
    if (res != ERROR_SUCCESS) goto end;

    res = 0;
end:
190
    if (key && key != root) RegCloseKey(key);
191
    if (res != 0)
192
        WINE_ERR("Unable to set configuration key %s in section %s, res=%ld\n",
193
                 wine_dbgstr_w(name), wine_dbgstr_w(subkey), res);
194 195 196
    return res;
}

197
/* ========================================================================= */
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

/* This code exists for the following reasons:
 *
 * - It makes working with the registry easier
 * - By storing a mini cache of the registry, we can more easily implement
 *   cancel/revert and apply. The 'settings list' is an overlay on top of
 *   the actual registry data that we can write out at will.
 *
 * Rather than model a tree in memory, we simply store each absolute (rooted
 * at the config key) path.
 *
 */

struct setting
{
    struct list entry;
214
    HKEY root;    /* the key on which path is rooted */
215 216 217
    WCHAR *path;   /* path in the registry rooted at root  */
    WCHAR *name;   /* name of the registry value. if null, this means delete the key  */
    WCHAR *value;  /* contents of the registry value. if null, this means delete the value  */
218
    DWORD type;   /* type of registry value. REG_SZ or REG_DWORD for now */
219 220
};

221
static struct list settings = LIST_INIT(settings);
222 223 224 225

static void free_setting(struct setting *setting)
{
    assert( setting != NULL );
226
    assert( setting->path );
227

228 229 230
    WINE_TRACE("destroying %p: %s\n", setting,
               wine_dbgstr_w(setting->path));

231 232 233
    free(setting->path);
    free(setting->name);
    free(setting->value);
234 235 236

    list_remove(&setting->entry);

237
    free(setting);
238 239 240 241 242 243 244 245
}

/**
 * Returns the contents of the value at path. If not in the settings
 * list, it will be fetched from the registry - failing that, the
 * default will be used.
 *
 * If already in the list, the contents as given there will be
246
 * returned. You are expected to free the result.
247
 */
248
WCHAR *get_reg_key(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *def)
249 250 251
{
    struct list *cursor;
    struct setting *s;
252
    WCHAR *val;
253

254 255
    WINE_TRACE("path=%s, name=%s, def=%s\n", wine_dbgstr_w(path),
               wine_dbgstr_w(name), wine_dbgstr_w(def));
256 257

    /* check if it's in the list */
258
    LIST_FOR_EACH( cursor, &settings )
259 260 261
    {
        s = LIST_ENTRY(cursor, struct setting, entry);

262
        if (root != s->root) continue;
263
        if (lstrcmpiW(path, s->path) != 0) continue;
264
        if (!s->name) continue;
265
        if (lstrcmpiW(name, s->name) != 0) continue;
266

267 268 269
        WINE_TRACE("found %s:%s in settings list, returning %s\n",
                   wine_dbgstr_w(path), wine_dbgstr_w(name),
                   wine_dbgstr_w(s->value));
270
        return wcsdup(s->value);
271 272 273
    }

    /* no, so get from the registry */
274
    val = get_config_key(root, path, name, def);
275

276
    WINE_TRACE("returning %s\n", wine_dbgstr_w(val));
277 278 279 280 281 282 283 284 285 286 287

    return val;
}

/**
 * Used to set a registry key.
 *
 * path is rooted at the config key, ie use "Version" or
 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
 * to get such a string.
 *
288
 * name is the value name, or NULL to delete the path.
289 290 291
 *
 * value is what to set the value to, or NULL to delete it.
 *
292 293
 * type is REG_SZ or REG_DWORD.
 *
294 295
 * These values will be copied when necessary.
 */
296
static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, const void *value, DWORD type)
297 298 299 300 301 302
{
    struct list *cursor;
    struct setting *s;

    assert( path != NULL );

303
    WINE_TRACE("path=%s, name=%s, value=%s\n", wine_dbgstr_w(path),
304
               wine_dbgstr_w(name), wine_dbgstr_w(value));
305 306

    /* firstly, see if we already set this setting  */
307
    LIST_FOR_EACH( cursor, &settings )
308 309 310
    {
        struct setting *s = LIST_ENTRY(cursor, struct setting, entry);

311
        if (root != s->root) continue;
312 313
        if (lstrcmpiW(s->path, path) != 0) continue;
        if ((s->name && name) && lstrcmpiW(s->name, name) != 0) continue;
314 315 316 317 318

        /* are we attempting a double delete? */
        if (!s->name && !name) return;

        /* do we want to undelete this key? */
319
        if (!s->name && name) s->name = wcsdup(name);
320 321

        /* yes, we have already set it, so just replace the content and return  */
322
        free(s->value);
323 324 325 326
        s->type = type;
        switch (type)
        {
            case REG_SZ:
327
                s->value = wcsdup(value);
328 329
                break;
            case REG_DWORD:
330
                s->value = malloc(sizeof(DWORD));
331 332 333
                memcpy( s->value, value, sizeof(DWORD) );
                break;
        }
334

335 336 337 338 339 340 341 342 343 344
        /* are we deleting this key? this won't remove any of the
         * children from the overlay so if the user adds it again in
         * that session it will appear to undelete the settings, but
         * in reality only the settings actually modified by the user
         * in that session will be restored. we might want to fix this
         * corner case in future by actually deleting all the children
         * here so that once it's gone, it's gone.
         */
        if (!name) s->name = NULL;

345 346 347 348
        return;
    }

    /* otherwise add a new setting for it  */
349
    s = malloc(sizeof(struct setting));
350
    s->root  = root;
351 352
    s->path  = wcsdup(path);
    s->name  = wcsdup(name);
353 354 355 356
    s->type  = type;
    switch (type)
    {
        case REG_SZ:
357
            s->value = wcsdup(value);
358 359
            break;
        case REG_DWORD:
360
            s->value = malloc(sizeof(DWORD));
361 362 363
            memcpy( s->value, value, sizeof(DWORD) );
            break;
    }
364

365
    list_add_tail(&settings, &s->entry);
366
}
367

368
void set_reg_key(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *value)
369 370 371 372
{
    set_reg_key_ex(root, path, name, value, REG_SZ);
}

373
void set_reg_key_dword(HKEY root, const WCHAR *path, const WCHAR *name, DWORD value)
374 375 376 377
{
    set_reg_key_ex(root, path, name, &value, REG_DWORD);
}

378 379 380 381
/**
 * enumerates the value names at the given path, taking into account
 * the changes in the settings list.
 *
382
 * you are expected to free each element of the array, which is null
383 384
 * terminated, as well as the array itself.
 */
385
WCHAR **enumerate_values(HKEY root, const WCHAR *path)
386 387
{
    HKEY key;
388
    DWORD res, i = 0, valueslen = 0;
389
    WCHAR **values = NULL;
390 391
    struct list *cursor;

392
    res = RegOpenKeyExW(root, path, 0, MAXIMUM_ALLOWED, &key);
393 394 395 396
    if (res == ERROR_SUCCESS)
    {
        while (TRUE)
        {
397
            WCHAR name[1024];
398
            DWORD namesize = ARRAY_SIZE(name);
399 400 401
            BOOL removed = FALSE;

            /* find out the needed size, allocate a buffer, read the value  */
402
            if ((res = RegEnumValueW(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
403 404
                break;

405
            WINE_TRACE("name=%s\n", wine_dbgstr_w(name));
406 407

            /* check if this value name has been removed in the settings list  */
408
            LIST_FOR_EACH( cursor, &settings )
409 410
            {
                struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
411 412
                if (lstrcmpiW(s->path, path) != 0) continue;
                if (lstrcmpiW(s->name, name) != 0) continue;
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

                if (!s->value)
                {
                    WINE_TRACE("this key has been removed, so skipping\n");
                    removed = TRUE;
                    break;
                }
            }

            if (removed)            /* this value was deleted by the user, so don't include it */
            {
                i++;
                continue;
            }

            /* grow the array if necessary, add buffer to it, iterate  */
429
            values = realloc(values, sizeof(WCHAR*) * (valueslen + 1));
430

431
            values[valueslen++] = wcsdup(name);
432
            WINE_TRACE("valueslen is now %ld\n", valueslen);
433 434 435 436 437
            i++;
        }
    }
    else
    {
438
        WINE_WARN("failed opening registry key %s, res=0x%lx\n",
439
                  wine_dbgstr_w(path), res);
440 441 442 443 444
    }

    WINE_TRACE("adding settings in list but not registry\n");

    /* now we have to add the values that aren't in the registry but are in the settings list */
445
    LIST_FOR_EACH( cursor, &settings )
446 447 448 449
    {
        struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
        BOOL found = FALSE;

450
        if (lstrcmpiW(setting->path, path) != 0) continue;
451 452 453 454 455

        if (!setting->value) continue;

        for (i = 0; i < valueslen; i++)
        {
456
            if (lstrcmpiW(setting->name, values[i]) == 0)
457 458 459 460 461 462 463 464
            {
                found = TRUE;
                break;
            }
        }

        if (found) continue;

465
        WINE_TRACE("%s in list but not registry\n", wine_dbgstr_w(setting->name));
466 467

        /* otherwise it's been set by the user but isn't in the registry */
468
        values = realloc(values, sizeof(WCHAR*) * (valueslen + 1));
469

470
        values[valueslen++] = wcsdup(setting->name);
471 472
    }

473 474 475
    WINE_TRACE("adding null terminator\n");
    if (values)
    {
476
        values = realloc(values, sizeof(WCHAR*) * (valueslen + 1));
477
        values[valueslen] = NULL;
478
    }
479 480 481 482

    RegCloseKey(key);

    return values;
483 484
}

485 486 487 488
/**
 * returns true if the given key/value pair exists in the registry or
 * has been written to.
 */
489
BOOL reg_key_exists(HKEY root, const WCHAR *path, const WCHAR *name)
490
{
491
    WCHAR *val = get_reg_key(root, path, name, NULL);
492

493
    free(val);
494
    return val != NULL;
495 496
}

497
static void process_setting(struct setting *s)
498
{
499 500
    HKEY key;
    BOOL needs_wow64 = (is_win64 && s->root == HKEY_LOCAL_MACHINE && s->path &&
501
                        !wcsnicmp(s->path, L"Software\\", wcslen(L"Software\\")));
502

503 504
    if (s->value)
    {
505 506
	WINE_TRACE("Setting %s:%s to '%s'\n", wine_dbgstr_w(s->path),
                   wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
507 508 509 510 511 512 513
        set_config_key(s->root, s->path, MAXIMUM_ALLOWED, s->name, s->value, s->type);
        if (needs_wow64)
        {
            WINE_TRACE("Setting 32-bit %s:%s to '%s'\n", wine_dbgstr_w(s->path),
                       wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
            set_config_key(s->root, s->path, MAXIMUM_ALLOWED | KEY_WOW64_32KEY, s->name, s->value, s->type);
        }
514 515 516
    }
    else
    {
517
	WINE_TRACE("Removing %s:%s\n", wine_dbgstr_w(s->path), wine_dbgstr_w(s->name));
518
        if (!RegOpenKeyExW( s->root, s->path, 0, MAXIMUM_ALLOWED, &key ))
519 520 521
        {
            /* NULL name means remove that path/section entirely */
            if (s->name) RegDeleteValueW( key, s->name );
522 523 524 525 526
            else
            {
                RegDeleteTreeW( key, NULL );
                RegDeleteKeyW( s->root, s->path );
            }
527 528 529 530 531 532 533 534
            RegCloseKey( key );
        }
        if (needs_wow64)
        {
            WINE_TRACE("Removing 32-bit %s:%s\n", wine_dbgstr_w(s->path), wine_dbgstr_w(s->name));
            if (!RegOpenKeyExW( s->root, s->path, 0, MAXIMUM_ALLOWED | KEY_WOW64_32KEY, &key ))
            {
                if (s->name) RegDeleteValueW( key, s->name );
535 536 537 538 539
                else
                {
                    RegDeleteTreeW( key, NULL );
                    RegDeleteKeyExW( s->root, s->path, KEY_WOW64_32KEY, 0 );
                }
540 541 542
                RegCloseKey( key );
            }
        }
543 544 545 546 547
    }
}

void apply(void)
{
548
    if (list_empty(&settings)) return; /* we will be called for each page when the user clicks OK */
549 550 551

    WINE_TRACE("()\n");

552
    while (!list_empty(&settings))
553
    {
554
        struct setting *s = (struct setting *) list_head(&settings);
555 556
        process_setting(s);
        free_setting(s);
557
    }
558
}
559 560 561

/* ================================== utility functions ============================ */

562
WCHAR* current_app = NULL; /* the app we are currently editing, or NULL if editing global */
563 564

/* returns a registry key path suitable for passing to addTransaction  */
565
WCHAR *keypath(const WCHAR *section)
566 567 568
{
    static WCHAR *result = NULL;

569
    free(result);
570 571 572

    if (current_app)
    {
573
        DWORD len = sizeof(L"AppDefaults\\") + (lstrlenW(current_app) + lstrlenW(section) + 1) * sizeof(WCHAR);
574
        result = malloc(len);
575
        lstrcpyW( result, L"AppDefaults\\" );
576 577 578 579 580 581 582 583 584 585
        lstrcatW( result, current_app );
        if (section[0])
        {
            len = lstrlenW(result);
            result[len++] = '\\';
            lstrcpyW( result + len, section );
        }
    }
    else
    {
586
        result = wcsdup(section);
587 588 589 590 591
    }

    return result;
}

592 593 594 595 596 597 598
void PRINTERROR(void)
{
        LPSTR msg;

        FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
                       0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                       (LPSTR)&msg, 0, NULL);
Mike Hearn's avatar
Mike Hearn committed
599 600

        /* eliminate trailing newline, is this a Wine bug? */
601
        *(strrchr(msg, '\r')) = '\0';
Mike Hearn's avatar
Mike Hearn committed
602
        
603 604
        WINE_TRACE("error: '%s'\n", msg);
}
605

606
BOOL initialize(HINSTANCE hInstance)
607
{
608
    DWORD res = RegCreateKeyW(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
609 610

    if (res != ERROR_SUCCESS) {
611
	WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
612
        return TRUE;
613 614
    }

615
    return FALSE;
616
}