winecfg.c 18.3 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 20 21
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
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
HKEY config_key = NULL;
46
HMENU hPopupMenus = 0;
47

48 49 50 51 52 53 54 55 56 57 58

/* 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)
{
    char *newtitle;

    /* update the window title  */
Mike Hearn's avatar
Mike Hearn committed
59
    if (current_app)
60
    {
61
        const char *template = "Wine Configuration for %s";
Mike Hearn's avatar
Mike Hearn committed
62 63
        newtitle = HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(current_app) + 1);
        sprintf(newtitle, template, current_app);
64
    }
65 66 67 68 69 70 71 72
    else
    {
        newtitle = strdupA("Wine Configuration");
    }

    WINE_TRACE("setting title to %s\n", newtitle);
    SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
    HeapFree(GetProcessHeap(), 0, newtitle);
73 74
}

75

76
/**
77
 * get_config_key: Retrieves a configuration value from the registry
78
 *
79 80 81
 * 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
82
 *
83 84
 * Returns a buffer holding the value if successful, NULL if
 * not. Caller is responsible for releasing the result.
85
 *
86
 */
87
static char *get_config_key (HKEY root, const char *subkey, const char *name, const char *def)
88
{
Mike McCormack's avatar
Mike McCormack committed
89
    LPSTR buffer = NULL;
90
    DWORD len;
91
    HKEY hSubKey = NULL;
92
    DWORD res;
93

94
    WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
95

96
    res = RegOpenKey(root, subkey, &hSubKey);
97 98 99
    if (res != ERROR_SUCCESS)
    {
        if (res == ERROR_FILE_NOT_FOUND)
100
        {
101
            WINE_TRACE("Section key not present - using default\n");
102
            return def ? strdupA(def) : NULL;
103 104 105
        }
        else
        {
106
            WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
107 108 109
        }
        goto end;
    }
110

111 112 113
    res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
    if (res == ERROR_FILE_NOT_FOUND)
    {
114
        WINE_TRACE("Value not present - using default\n");
115
        buffer = def ? strdupA(def) : NULL;
116
	goto end;
117
    } else if (res != ERROR_SUCCESS)
118
    {
119
        WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
120 121
        goto end;
    }
122 123 124

    buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);

Mike McCormack's avatar
Mike McCormack committed
125
    RegQueryValueEx(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
126 127

    WINE_TRACE("buffer=%s\n", buffer);
128
end:
129
    if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
130

Mike McCormack's avatar
Mike McCormack committed
131
    return buffer;
132 133
}

134
/**
135
 * set_config_key: convenience wrapper to set a key/value pair
136
 *
137 138 139
 * 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
140 141
 *
 * Returns 0 on success, non-zero otherwise
142 143
 *
 * If valueName or value is NULL, an empty section will be created
144
 */
Mike McCormack's avatar
Mike McCormack committed
145
static int set_config_key(HKEY root, const char *subkey, const char *name, const void *value, DWORD type) {
146 147 148
    DWORD res = 1;
    HKEY key = NULL;

Mike McCormack's avatar
Mike McCormack committed
149
    WINE_TRACE("subkey=%s: name=%s, value=%p, type=%ld\n", subkey, name, value, type);
150 151

    assert( subkey != NULL );
152

153 154
    if (subkey[0])
    {
155
        res = RegCreateKey(root, subkey, &key);
156 157
        if (res != ERROR_SUCCESS) goto end;
    }
158
    else key = root;
159
    if (name == NULL || value == NULL) goto end;
160

161 162 163 164 165
    switch (type)
    {
        case REG_SZ: res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1); break;
        case REG_DWORD: res = RegSetValueEx(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
    }
166 167 168 169
    if (res != ERROR_SUCCESS) goto end;

    res = 0;
end:
170
    if (key && key != root) RegCloseKey(key);
Mike McCormack's avatar
Mike McCormack committed
171
    if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s, res=%ld\n", name, subkey, res);
172 173 174
    return res;
}

175 176 177
/* removes the requested value from the registry, however, does not
 * remove the section if empty. Returns S_OK (0) on success.
 */
178
static HRESULT remove_value(HKEY root, const char *subkey, const char *name)
179
{
180 181 182
    HRESULT hr;
    HKEY key;

183
    WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
184

185
    hr = RegOpenKey(root, subkey, &key);
186 187
    if (hr != S_OK) return hr;

188
    hr = RegDeleteValue(key, name);
189 190 191 192 193
    if (hr != ERROR_SUCCESS) return hr;

    return S_OK;
}

194
/* removes the requested subkey from the registry, assuming it exists */
195 196 197 198 199 200 201 202 203
static LONG remove_path(HKEY root, char *section) {
    HKEY branch_key;
    DWORD max_sub_key_len;
    DWORD subkeys;
    DWORD curr_len;
    LONG ret = ERROR_SUCCESS;
    long int i;
    char *buffer;

204 205
    WINE_TRACE("section=%s\n", section);

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    if ((ret = RegOpenKey(root, section, &branch_key)) != ERROR_SUCCESS)
        return ret;

    /* get size information and resize the buffers if necessary */
    if ((ret = RegQueryInfoKey(branch_key, NULL, NULL, NULL,
                               &subkeys, &max_sub_key_len,
                               NULL, NULL, NULL, NULL, NULL, NULL
                              )) != ERROR_SUCCESS)
        return ret;

    curr_len = strlen(section);
    buffer = HeapAlloc(GetProcessHeap(), 0, max_sub_key_len + curr_len + 1);
    strcpy(buffer, section);

    buffer[curr_len] = '\\';
    for (i = subkeys - 1; i >= 0; i--)
    {
        DWORD buf_len = max_sub_key_len - curr_len - 1;

        ret = RegEnumKeyEx(branch_key, i, buffer + curr_len + 1,
                           &buf_len, NULL, NULL, NULL, NULL);
        if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
            ret != ERROR_NO_MORE_ITEMS)
            break;
        else
            remove_path(root, buffer);
    }
    HeapFree(GetProcessHeap(), 0, buffer);
    RegCloseKey(branch_key);

236
    return RegDeleteKey(root, section);
237 238
}

239

240
/* ========================================================================= */
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

/* 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;
257 258
    HKEY root;    /* the key on which path is rooted */
    char *path;   /* path in the registry rooted at root  */
259 260
    char *name;   /* name of the registry value. if null, this means delete the key  */
    char *value;  /* contents of the registry value. if null, this means delete the value  */
261
    DWORD type;   /* type of registry value. REG_SZ or REG_DWORD for now */
262 263 264 265 266 267 268
};

struct list *settings;

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

271 272
    WINE_TRACE("destroying %p: %s\n", setting, setting->path);
    
273 274
    HeapFree(GetProcessHeap(), 0, setting->path);
    HeapFree(GetProcessHeap(), 0, setting->name);
275
    HeapFree(GetProcessHeap(), 0, setting->value);
276 277 278 279 280 281 282 283 284 285 286 287 288 289

    list_remove(&setting->entry);

    HeapFree(GetProcessHeap(), 0, setting);
}

/**
 * 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
 * returned. You are expected to HeapFree the result.
 */
290
char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
291 292 293 294 295 296 297 298 299 300 301 302
{
    struct list *cursor;
    struct setting *s;
    char *val;

    WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);

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

303
        if (root != s->root) continue;
304 305 306 307
        if (strcasecmp(path, s->path) != 0) continue;
        if (strcasecmp(name, s->name) != 0) continue;

        WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
308
        return s->value ? strdupA(s->value) : NULL;
309 310 311
    }

    /* no, so get from the registry */
312
    val = get_config_key(root, path, name, def);
313 314 315 316 317 318 319 320 321 322 323 324 325

    WINE_TRACE("returning %s\n", val);

    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.
 *
326
 * name is the value name, or NULL to delete the path.
327 328 329
 *
 * value is what to set the value to, or NULL to delete it.
 *
330 331
 * type is REG_SZ or REG_DWORD.
 *
332 333
 * These values will be copied when necessary.
 */
334
static void set_reg_key_ex(HKEY root, const char *path, const char *name, const void *value, DWORD type)
335 336 337 338 339 340
{
    struct list *cursor;
    struct setting *s;

    assert( path != NULL );

341
    WINE_TRACE("path=%s, name=%s, value=%p\n", path, name, value);
342 343 344 345 346 347

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

348
        if (root != s->root) continue;
349
        if (strcasecmp(s->path, path) != 0) continue;
350 351 352 353 354 355 356
        if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;

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

        /* do we want to undelete this key? */
        if (!s->name && name) s->name = strdupA(name);
357 358

        /* yes, we have already set it, so just replace the content and return  */
359
        HeapFree(GetProcessHeap(), 0, s->value);
360 361 362 363 364 365 366 367 368 369 370
        s->type = type;
        switch (type)
        {
            case REG_SZ:
                s->value = value ? strdupA(value) : NULL;
                break;
            case REG_DWORD:
                s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
                memcpy( s->value, value, sizeof(DWORD) );
                break;
        }
371

372 373 374 375 376 377 378 379 380 381
        /* 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;

382 383 384 385 386
        return;
    }

    /* otherwise add a new setting for it  */
    s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
387
    s->root  = root;
388 389
    s->path  = strdupA(path);
    s->name  = name  ? strdupA(name)  : NULL;
390 391 392 393 394 395 396 397 398 399 400
    s->type  = type;
    switch (type)
    {
        case REG_SZ:
            s->value = value ? strdupA(value) : NULL;
            break;
        case REG_DWORD:
            s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
            memcpy( s->value, value, sizeof(DWORD) );
            break;
    }
401 402

    list_add_tail(settings, &s->entry);
403
}
404

405 406 407 408 409 410 411 412 413 414
void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
{
    set_reg_key_ex(root, path, name, value, REG_SZ);
}

void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
{
    set_reg_key_ex(root, path, name, &value, REG_DWORD);
}

415 416 417 418 419 420 421
/**
 * enumerates the value names at the given path, taking into account
 * the changes in the settings list.
 *
 * you are expected to HeapFree each element of the array, which is null
 * terminated, as well as the array itself.
 */
422
char **enumerate_values(HKEY root, char *path)
423 424 425 426 427 428 429
{
    HKEY key;
    DWORD res, i = 0;
    char **values = NULL;
    int valueslen = 0;
    struct list *cursor;

430
    res = RegOpenKey(root, path, &key);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    if (res == ERROR_SUCCESS)
    {
        while (TRUE)
        {
            char name[1024];
            DWORD namesize = sizeof(name);
            BOOL removed = FALSE;

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

            WINE_TRACE("name=%s\n", name);

            /* check if this value name has been removed in the settings list  */
            LIST_FOR_EACH( cursor, settings )
            {
                struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
                if (strcasecmp(s->path, path) != 0) continue;
                if (strcasecmp(s->name, name) != 0) continue;

                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 */
            {
                HeapFree(GetProcessHeap(), 0, name);
                i++;
                continue;
            }

            /* grow the array if necessary, add buffer to it, iterate  */
            if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
            else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));

            values[valueslen++] = strdupA(name);
            WINE_TRACE("valueslen is now %d\n", valueslen);
            i++;
        }
    }
    else
    {
        WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
    }

    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 */
    LIST_FOR_EACH( cursor, settings )
    {
        struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
        BOOL found = FALSE;

        if (strcasecmp(setting->path, path) != 0) continue;

        if (!setting->value) continue;

        for (i = 0; i < valueslen; i++)
        {
            if (strcasecmp(setting->name, values[i]) == 0)
            {
                found = TRUE;
                break;
            }
        }

        if (found) continue;

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

        /* otherwise it's been set by the user but isn't in the registry */
        if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
        else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));

        values[valueslen++] = strdupA(setting->name);
511 512
    }

513 514 515 516 517
    WINE_TRACE("adding null terminator\n");
    if (values)
    {
        values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
        values[valueslen] = NULL;
518
    }
519 520 521 522

    RegCloseKey(key);

    return values;
523 524
}

525 526 527 528
/**
 * returns true if the given key/value pair exists in the registry or
 * has been written to.
 */
529
BOOL reg_key_exists(HKEY root, const char *path, const char *name)
530
{
531
    char *val = get_reg_key(root, path, name, NULL);
532 533 534 535 536

    if (val)
    {
        HeapFree(GetProcessHeap(), 0, val);
        return TRUE;
537
    }
538 539

    return FALSE;
540 541
}

542
static void process_setting(struct setting *s)
543
{
544 545 546
    if (s->value)
    {
	WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
547
        set_config_key(s->root, s->path, s->name, s->value, s->type);
548 549 550 551
    }
    else
    {
        /* NULL name means remove that path/section entirely */
552 553
	if (s->path && s->name) remove_value(s->root, s->path, s->name);
        else if (s->path && !s->name) remove_path(s->root, s->path);
554 555 556 557 558 559 560 561 562 563 564 565 566 567
    }
}

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

    WINE_TRACE("()\n");

    while (!list_empty(settings))
    {
        struct setting *s = (struct setting *) list_head(settings);
        process_setting(s);
        free_setting(s);
568
    }
569
}
570 571 572

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

Mike Hearn's avatar
Mike Hearn committed
573
char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
574 575

/* returns a registry key path suitable for passing to addTransaction  */
576
char *keypath(const char *section)
577 578
{
    static char *result = NULL;
579

580
    HeapFree(GetProcessHeap(), 0, result);
581

Mike Hearn's avatar
Mike Hearn committed
582
    if (current_app)
583
    {
Mike Hearn's avatar
Mike Hearn committed
584
        result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
585 586
        sprintf(result, "AppDefaults\\%s", current_app);
        if (section[0]) sprintf( result + strlen(result), "\\%s", section );
587 588 589 590 591
    }
    else
    {
        result = strdupA(section);
    }
592

593 594 595
    return result;
}

596 597 598 599 600 601 602
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
603 604

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

610 611
int initialize(HINSTANCE hInstance)
{
612
    DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
613 614 615 616 617 618

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

619 620 621
    /* load any menus */
    hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));

622 623 624 625 626 627
    /* we could probably just have the list as static data  */
    settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
    list_init(settings);

    return 0;
}