winecfg.c 18.8 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
HKEY config_key = NULL;
46
HMENU hPopupMenus = 0;
47

48 49 50 51 52 53 54 55

/* 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)
{
56
    WCHAR newtitle[256];
57 58

    /* update the window title  */
Mike Hearn's avatar
Mike Hearn committed
59
    if (current_app)
60
    {
61 62 63 64
        WCHAR apptitle[256];
        LoadStringW (GetModuleHandle(NULL), IDS_WINECFG_TITLE_APP, apptitle,
            sizeof(apptitle)/sizeof(apptitle[0]));
        wsprintfW (newtitle, apptitle, current_app);
65
    }
66 67
    else
    {
68 69
        LoadStringW (GetModuleHandle(NULL), IDS_WINECFG_TITLE, newtitle,
            sizeof(newtitle)/sizeof(newtitle[0]));
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 83 84 85 86 87 88 89 90 91
    int len;
    WCHAR* newStr;

    LoadStringW (GetModuleHandle (NULL), id, buf, sizeof(buf)/sizeof(buf[0]));

    len = lstrlenW (buf);
    newStr = HeapAlloc (GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR));
    memcpy (newStr, buf, len * sizeof (WCHAR));
    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 char *get_config_key (HKEY root, const char *subkey, const char *name, const char *def)
104
{
Mike McCormack's avatar
Mike McCormack committed
105
    LPSTR buffer = NULL;
106
    DWORD len;
107
    HKEY hSubKey = NULL;
108
    DWORD res;
109

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

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

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

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

Mike McCormack's avatar
Mike McCormack committed
141
    RegQueryValueEx(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
142 143

    WINE_TRACE("buffer=%s\n", buffer);
144
end:
145
    if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
146

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

150
/**
151
 * set_config_key: convenience wrapper to set a key/value pair
152
 *
153 154 155
 * 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
156 157
 *
 * Returns 0 on success, non-zero otherwise
158 159
 *
 * If valueName or value is NULL, an empty section will be created
160
 */
Mike McCormack's avatar
Mike McCormack committed
161
static int set_config_key(HKEY root, const char *subkey, const char *name, const void *value, DWORD type) {
162 163 164
    DWORD res = 1;
    HKEY key = NULL;

165
    WINE_TRACE("subkey=%s: name=%s, value=%p, type=%d\n", subkey, name, value, type);
166 167

    assert( subkey != NULL );
168

169 170
    if (subkey[0])
    {
171
        res = RegCreateKey(root, subkey, &key);
172 173
        if (res != ERROR_SUCCESS) goto end;
    }
174
    else key = root;
175
    if (name == NULL || value == NULL) goto end;
176

177 178 179 180 181
    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;
    }
182 183 184 185
    if (res != ERROR_SUCCESS) goto end;

    res = 0;
end:
186
    if (key && key != root) RegCloseKey(key);
187
    if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s, res=%d\n", name, subkey, res);
188 189 190
    return res;
}

191 192 193
/* removes the requested value from the registry, however, does not
 * remove the section if empty. Returns S_OK (0) on success.
 */
194
static HRESULT remove_value(HKEY root, const char *subkey, const char *name)
195
{
196 197 198
    HRESULT hr;
    HKEY key;

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

201
    hr = RegOpenKey(root, subkey, &key);
202 203
    if (hr != S_OK) return hr;

204
    hr = RegDeleteValue(key, name);
205 206 207 208 209
    if (hr != ERROR_SUCCESS) return hr;

    return S_OK;
}

210
/* removes the requested subkey from the registry, assuming it exists */
211 212 213 214 215 216 217 218 219
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;

220 221
    WINE_TRACE("section=%s\n", section);

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    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);

252
    return RegDeleteKey(root, section);
253 254
}

255

256
/* ========================================================================= */
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

/* 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;
273 274
    HKEY root;    /* the key on which path is rooted */
    char *path;   /* path in the registry rooted at root  */
275 276
    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  */
277
    DWORD type;   /* type of registry value. REG_SZ or REG_DWORD for now */
278 279 280 281 282 283 284
};

struct list *settings;

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

287 288
    WINE_TRACE("destroying %p: %s\n", setting, setting->path);
    
289 290
    HeapFree(GetProcessHeap(), 0, setting->path);
    HeapFree(GetProcessHeap(), 0, setting->name);
291
    HeapFree(GetProcessHeap(), 0, setting->value);
292 293 294 295 296 297 298 299 300 301 302 303 304 305

    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.
 */
306
char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
307 308 309 310 311 312 313 314 315 316 317 318
{
    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);

319
        if (root != s->root) continue;
320
        if (strcasecmp(path, s->path) != 0) continue;
321
        if (!s->name) continue;
322 323 324
        if (strcasecmp(name, s->name) != 0) continue;

        WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
325
        return s->value ? strdupA(s->value) : NULL;
326 327 328
    }

    /* no, so get from the registry */
329
    val = get_config_key(root, path, name, def);
330 331 332 333 334 335 336 337 338 339 340 341 342

    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.
 *
343
 * name is the value name, or NULL to delete the path.
344 345 346
 *
 * value is what to set the value to, or NULL to delete it.
 *
347 348
 * type is REG_SZ or REG_DWORD.
 *
349 350
 * These values will be copied when necessary.
 */
351
static void set_reg_key_ex(HKEY root, const char *path, const char *name, const void *value, DWORD type)
352 353 354 355 356 357
{
    struct list *cursor;
    struct setting *s;

    assert( path != NULL );

358
    WINE_TRACE("path=%s, name=%s, value=%p\n", path, name, value);
359 360 361 362 363 364

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

365
        if (root != s->root) continue;
366
        if (strcasecmp(s->path, path) != 0) continue;
367 368 369 370 371 372 373
        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);
374 375

        /* yes, we have already set it, so just replace the content and return  */
376
        HeapFree(GetProcessHeap(), 0, s->value);
377 378 379 380 381 382 383 384 385 386 387
        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;
        }
388

389 390 391 392 393 394 395 396 397 398
        /* 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;

399 400 401 402 403
        return;
    }

    /* otherwise add a new setting for it  */
    s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
404
    s->root  = root;
405 406
    s->path  = strdupA(path);
    s->name  = name  ? strdupA(name)  : NULL;
407 408 409 410 411 412 413 414 415 416 417
    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;
    }
418 419

    list_add_tail(settings, &s->entry);
420
}
421

422 423 424 425 426 427 428 429 430 431
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);
}

432 433 434 435 436 437 438
/**
 * 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.
 */
439
char **enumerate_values(HKEY root, char *path)
440 441 442 443 444 445 446
{
    HKEY key;
    DWORD res, i = 0;
    char **values = NULL;
    int valueslen = 0;
    struct list *cursor;

447
    res = RegOpenKey(root, path, &key);
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
    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
    {
495
        WINE_WARN("failed opening registry key %s, res=0x%x\n", path, res);
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    }

    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);
528 529
    }

530 531 532 533 534
    WINE_TRACE("adding null terminator\n");
    if (values)
    {
        values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
        values[valueslen] = NULL;
535
    }
536 537 538 539

    RegCloseKey(key);

    return values;
540 541
}

542 543 544 545
/**
 * returns true if the given key/value pair exists in the registry or
 * has been written to.
 */
546
BOOL reg_key_exists(HKEY root, const char *path, const char *name)
547
{
548
    char *val = get_reg_key(root, path, name, NULL);
549 550 551 552 553

    if (val)
    {
        HeapFree(GetProcessHeap(), 0, val);
        return TRUE;
554
    }
555 556

    return FALSE;
557 558
}

559
static void process_setting(struct setting *s)
560
{
561 562 563
    if (s->value)
    {
	WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
564
        set_config_key(s->root, s->path, s->name, s->value, s->type);
565 566 567 568
    }
    else
    {
        /* NULL name means remove that path/section entirely */
569 570
	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);
571 572 573 574 575 576 577 578 579 580 581 582 583 584
    }
}

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);
585
    }
586
}
587 588 589

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

590
WCHAR* current_app = NULL; /* the app we are currently editing, or NULL if editing global */
591 592

/* returns a registry key path suitable for passing to addTransaction  */
593
char *keypath(const char *section)
594 595
{
    static char *result = NULL;
596

597
    HeapFree(GetProcessHeap(), 0, result);
598

Mike Hearn's avatar
Mike Hearn committed
599
    if (current_app)
600
    {
601 602
        result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + lstrlenW(current_app)*2 + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
        wsprintf(result, "AppDefaults\\%ls", current_app);
603
        if (section[0]) sprintf( result + strlen(result), "\\%s", section );
604 605 606 607 608
    }
    else
    {
        result = strdupA(section);
    }
609

610 611 612
    return result;
}

613 614 615 616 617 618 619
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
620 621

        /* eliminate trailing newline, is this a Wine bug? */
622
        *(strrchr(msg, '\r')) = '\0';
Mike Hearn's avatar
Mike Hearn committed
623
        
624 625
        WINE_TRACE("error: '%s'\n", msg);
}
626

627 628
int initialize(HINSTANCE hInstance)
{
629
    DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
630 631

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

636 637 638
    /* load any menus */
    hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));

639 640 641 642 643 644
    /* we could probably just have the list as static data  */
    settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
    list_init(settings);

    return 0;
}