ntdll/tests: Use FAILED() instead of not SUCCEEDED().
[wine.git] / programs / winecfg / libraries.c
blob43cfe81a1048676a27e5728e0d83c6ddc94ec719
1 /*
2 * WineCfg libraries tabsheet
4 * Copyright 2004 Robert van Herk
5 * Copyright 2004 Mike Hearn
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define WIN32_LEAN_AND_MEAN
24 #include <windows.h>
25 #include <commdlg.h>
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
30 #include "winecfg.h"
31 #include "resource.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
36 #ifdef __i386__
37 static const WCHAR pe_dir[] = L"\\i386-windows";
38 #elif defined __x86_64__
39 static const WCHAR pe_dir[] = L"\\x86_64-windows";
40 #elif defined __arm__
41 static const WCHAR pe_dir[] = L"\\arm-windows";
42 #elif defined __aarch64__
43 static const WCHAR pe_dir[] = L"\\aarch64-windows";
44 #else
45 static const WCHAR pe_dir[] = L"";
46 #endif
48 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
49 static const WCHAR * const builtin_only[] =
51 L"advapi32",
52 L"capi2032",
53 L"dbghelp",
54 L"ddraw",
55 L"gdi32",
56 L"gphoto2.ds",
57 L"icmp",
58 L"iphlpapi",
59 L"kernel32",
60 L"l3codeca.acm",
61 L"mountmgr.sys",
62 L"mswsock",
63 L"ntdll",
64 L"ntoskrnl.exe",
65 L"opengl32",
66 L"sane.ds",
67 L"secur32",
68 L"twain_32",
69 L"unicows",
70 L"user32",
71 L"vdmdbg",
72 L"w32skrnl",
73 L"winmm",
74 L"wintab32",
75 L"wnaspi32",
76 L"wow32",
77 L"ws2_32",
78 L"wsock32",
81 enum dllmode
83 BUILTIN_NATIVE,
84 NATIVE_BUILTIN,
85 BUILTIN,
86 NATIVE,
87 DISABLE,
88 UNKNOWN /* Special value indicating an erroneous DLL override mode */
91 struct dll
93 WCHAR *name;
94 enum dllmode mode;
97 static const WCHAR emptyW[1];
99 /* Convert a registry string to a dllmode */
100 static enum dllmode string_to_mode(const WCHAR *in)
102 int i, j, len;
103 WCHAR *out;
104 enum dllmode res;
106 len = wcslen(in);
107 out = malloc((len + 1) * sizeof(WCHAR));
109 /* remove the spaces */
110 for (i = j = 0; i <= len; ++i) {
111 if (in[i] != ' ') {
112 out[j++] = in[i];
116 /* parse the string */
117 res = UNKNOWN;
118 if (wcscmp(out, L"builtin,native") == 0) res = BUILTIN_NATIVE;
119 if (wcscmp(out, L"native,builtin") == 0) res = NATIVE_BUILTIN;
120 if (wcscmp(out, L"builtin") == 0) res = BUILTIN;
121 if (wcscmp(out, L"native") == 0) res = NATIVE;
122 if (wcscmp(out, L"") == 0) res = DISABLE;
124 free(out);
125 return res;
128 /* Convert a dllmode to a registry string. */
129 static const WCHAR* mode_to_string(enum dllmode mode)
131 switch( mode )
133 case NATIVE: return L"native";
134 case BUILTIN: return L"builtin";
135 case NATIVE_BUILTIN: return L"native,builtin";
136 case BUILTIN_NATIVE: return L"builtin,native";
137 case DISABLE: return L"";
138 default: return L"";
142 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
143 static const WCHAR* mode_to_label(enum dllmode mode)
145 static WCHAR buffer[256];
146 UINT id = 0;
148 switch( mode )
150 case NATIVE: id = IDS_DLL_NATIVE; break;
151 case BUILTIN: id = IDS_DLL_BUILTIN; break;
152 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
153 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
154 case DISABLE: id = IDS_DLL_DISABLED; break;
155 default: return L"??";
157 if (!LoadStringW( GetModuleHandleW(NULL), id, buffer, ARRAY_SIZE(buffer) )) buffer[0] = 0;
158 return buffer;
161 /* Convert a control id (IDC_ constant) to a dllmode */
162 static enum dllmode id_to_mode(DWORD id)
164 switch( id )
166 case IDC_RAD_BUILTIN: return BUILTIN;
167 case IDC_RAD_NATIVE: return NATIVE;
168 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
169 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
170 case IDC_RAD_DISABLE: return DISABLE;
171 default: assert( FALSE ); return 0; /* should not be reached */
175 /* Convert a dllmode to a control id (IDC_ constant) */
176 static DWORD mode_to_id(enum dllmode mode)
178 switch( mode )
180 case BUILTIN: return IDC_RAD_BUILTIN;
181 case NATIVE: return IDC_RAD_NATIVE;
182 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
183 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
184 case DISABLE: return IDC_RAD_DISABLE;
185 default: return IDC_RAD_BUILTIN_NATIVE;
189 /* helper for is_builtin_only */
190 static int __cdecl compare_dll( const void *ptr1, const void *ptr2 )
192 const WCHAR * const *name1 = ptr1;
193 const WCHAR * const *name2 = ptr2;
194 return wcscmp( *name1, *name2 );
197 /* check if dll is recommended as builtin only */
198 static inline BOOL is_builtin_only( const WCHAR *name )
200 const WCHAR *ext = wcsrchr( name, '.' );
202 if (ext)
204 if (!wcscmp( ext, L".vxd" ) ||
205 !wcscmp( ext, L".drv" ) ||
206 !wcscmp( ext, L".tlb" ))
207 return TRUE;
209 if (!wcsncmp( name, L"wine", 4 )) return TRUE;
210 return bsearch( &name, builtin_only, ARRAY_SIZE(builtin_only),
211 sizeof(builtin_only[0]), compare_dll ) != NULL;
214 /* check if dll should be offered in the drop-down list */
215 static BOOL show_dll_in_list( const WCHAR *name )
217 const WCHAR *ext = wcsrchr( name, '.' );
219 if (ext)
221 /* skip 16-bit dlls */
222 if (wcslen(ext) > 2 && !wcscmp( ext + wcslen(ext) - 2, L"16" )) return FALSE;
223 /* skip exes */
224 if (!wcscmp( ext, L".exe" )) return FALSE;
226 /* skip dlls that should always be builtin */
227 return !is_builtin_only( name );
230 static void set_controls_from_selection(HWND dialog)
232 /* FIXME: display/update some information about the selected dll (purpose, recommended load order) maybe? */
235 static void clear_settings(HWND dialog)
237 int count = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
238 int i;
240 WINE_TRACE("count=%d\n", count);
242 for (i = 0; i < count; i++)
244 struct dll *dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
246 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
247 free(dll->name);
248 free(dll);
252 /* load the list of available libraries from a given dir */
253 static void load_library_list_from_dir( HWND dialog, const WCHAR *dir_path, int check_subdirs )
255 static const WCHAR * const ext[] = { L".dll", L"", L".dll.so", L".so" };
256 WCHAR *buffer, *p, name[256];
257 unsigned int i;
258 HANDLE handle;
259 WIN32_FIND_DATAW data;
260 ULONG maxlen = wcslen(dir_path) + wcslen(pe_dir) + 10 + 2 * ARRAY_SIZE(name);
262 buffer = malloc( maxlen * sizeof(WCHAR) );
263 wcscpy( buffer, dir_path );
264 wcscat( buffer, L"\\*" );
265 buffer[1] = '\\'; /* change \??\ to \\?\ */
266 p = buffer + wcslen(buffer) - 1;
268 if ((handle = FindFirstFileW( buffer, &data )) == INVALID_HANDLE_VALUE)
270 free( buffer );
271 return;
276 size_t len = wcslen(data.cFileName);
277 if (len > ARRAY_SIZE(name)) continue;
278 if (check_subdirs)
280 if (!wcscmp( data.cFileName, L"." )) continue;
281 if (!wcscmp( data.cFileName, L".." )) continue;
282 if (!show_dll_in_list( data.cFileName )) continue;
283 for (i = 0; i < ARRAY_SIZE( ext ); i++)
285 if (!ext[i][0] && !wcschr( data.cFileName, '.' )) continue;
286 swprintf( p, buffer + maxlen - p, L"%s%s\\%s%s", data.cFileName,
287 i > 1 ? L"" : pe_dir, data.cFileName, ext[i] );
288 if (GetFileAttributesW( buffer ) != INVALID_FILE_ATTRIBUTES)
290 SendDlgItemMessageW( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)data.cFileName );
291 break;
295 else
297 for (i = 0; i < ARRAY_SIZE( ext ); i++)
299 if (!ext[i][0]) continue;
300 if (len > wcslen(ext[i]) && !wcscmp( data.cFileName + len - wcslen(ext[i]), ext[i]))
302 len -= wcslen( ext[i] );
303 memcpy( name, data.cFileName, len * sizeof(WCHAR) );
304 name[len] = 0;
305 if (!show_dll_in_list( name )) continue;
306 SendDlgItemMessageW( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
310 } while (FindNextFileW( handle, &data ));
312 FindClose( handle );
313 free( buffer );
316 /* load the list of available libraries */
317 static void load_library_list( HWND dialog )
319 unsigned int i = 0;
320 WCHAR item1[256], item2[256], var[32], path[MAX_PATH];
321 HCURSOR old_cursor = SetCursor( LoadCursorW(0, (LPWSTR)IDC_WAIT) );
323 if (GetEnvironmentVariableW( L"WINEBUILDDIR", path, MAX_PATH ))
325 WCHAR *dir = malloc( wcslen(path) * sizeof(WCHAR) + sizeof(L"\\dlls") );
326 wcscpy( dir, path );
327 wcscat( dir, L"\\dlls" );
328 load_library_list_from_dir( dialog, dir, TRUE );
329 free( dir );
332 for (;;)
334 swprintf( var, ARRAY_SIZE(var), L"WINEDLLDIR%u", i++ );
335 if (!GetEnvironmentVariableW( var, path, MAX_PATH )) break;
336 load_library_list_from_dir( dialog, path, FALSE );
337 wcscat( path, pe_dir );
338 load_library_list_from_dir( dialog, path, FALSE );
341 /* get rid of duplicate entries */
343 SendDlgItemMessageW( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
344 i = 1;
345 while (SendDlgItemMessageW( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
347 if (!wcscmp( item1, item2 ))
349 SendDlgItemMessageW( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
351 else
353 wcscpy( item1, item2 );
354 i++;
357 SetCursor( old_cursor );
360 static void load_library_settings(HWND dialog)
362 WCHAR **overrides = enumerate_values(config_key, keypath(L"DllOverrides"));
363 WCHAR **p;
364 int sel, count = 0;
366 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
368 WINE_TRACE("sel=%d\n", sel);
370 clear_settings(dialog);
372 if (!overrides || *overrides == NULL)
374 set_controls_from_selection(dialog);
375 disable(IDC_DLLS_EDITDLL);
376 disable(IDC_DLLS_REMOVEDLL);
377 free(overrides);
378 return;
381 enable(IDC_DLLS_EDITDLL);
382 enable(IDC_DLLS_REMOVEDLL);
384 for (p = overrides; *p != NULL; p++)
386 int index, len;
387 WCHAR *str, *value;
388 const WCHAR *label;
389 struct dll *dll;
391 value = get_reg_key(config_key, keypath(L"DllOverrides"), *p, NULL);
393 label = mode_to_label(string_to_mode(value));
395 len = wcslen(*p) + 2 + wcslen(label) + 2;
396 str = malloc(len * sizeof(WCHAR));
397 swprintf( str, len, L"%s (%s)", *p, label );
399 dll = malloc(sizeof(struct dll));
400 dll->name = *p;
401 dll->mode = string_to_mode(value);
403 index = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
404 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
406 free(str);
408 count++;
411 free(overrides);
413 /* restore the previous selection, if possible */
414 if (sel >= count - 1) sel = count - 1;
415 else if (sel == -1) sel = 0;
417 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
419 set_controls_from_selection(dialog);
422 /* Called when the application is initialized (cannot reinit!) */
423 static void init_libsheet(HWND dialog)
425 /* clear the add dll controls */
426 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM)emptyW);
427 load_library_list( dialog );
428 disable(IDC_DLLS_ADDDLL);
431 static void on_add_combo_change(HWND dialog)
433 WCHAR buffer[1024];
434 int sel, len;
436 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_GETTEXT, ARRAY_SIZE(buffer), (LPARAM)buffer);
437 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
438 sel=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
439 len=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
441 if (buffer[0] || len>0)
443 enable(IDC_DLLS_ADDDLL)
444 SendMessageW(GetParent(dialog), DM_SETDEFID, IDC_DLLS_ADDDLL, 0);
446 else
448 disable(IDC_DLLS_ADDDLL);
449 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
453 static void set_dllmode(HWND dialog, DWORD id)
455 enum dllmode mode;
456 struct dll *dll;
457 int sel;
458 const WCHAR *str;
460 mode = id_to_mode(id);
462 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
463 if (sel == -1) return;
465 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
467 str = mode_to_string(mode);
468 WINE_TRACE("Setting %s to %s\n", debugstr_w(dll->name), debugstr_w(str));
470 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
471 set_reg_key(config_key, keypath(L"DllOverrides"), dll->name, str);
473 load_library_settings(dialog); /* ... and refresh */
476 static void on_add_click(HWND dialog)
478 WCHAR buffer[1024], *ptr;
480 buffer[0] = 0;
481 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_GETTEXT, ARRAY_SIZE(buffer), (LPARAM) buffer);
482 if (wcslen(buffer) > 4)
484 ptr = buffer + wcslen(buffer) - 4;
485 if (!wcsicmp(ptr, L".dll"))
487 WINE_TRACE("Stripping dll extension\n");
488 *ptr = '\0';
492 /* check if dll is in the builtin-only list */
493 if (!(ptr = wcsrchr( buffer, '\\' )))
495 ptr = buffer;
496 if (*ptr == '*') ptr++;
498 else ptr++;
499 if (is_builtin_only( ptr ))
501 MSGBOXPARAMSW params;
502 params.cbSize = sizeof(params);
503 params.hwndOwner = dialog;
504 params.hInstance = GetModuleHandleW( NULL );
505 params.lpszText = MAKEINTRESOURCEW( IDS_DLL_WARNING );
506 params.lpszCaption = MAKEINTRESOURCEW( IDS_DLL_WARNING_CAPTION );
507 params.dwStyle = MB_ICONWARNING | MB_YESNO;
508 params.lpszIcon = NULL;
509 params.dwContextHelpId = 0;
510 params.lpfnMsgBoxCallback = NULL;
511 params.dwLanguageId = 0;
512 if (MessageBoxIndirectW( &params ) != IDYES) return;
515 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM)emptyW);
516 disable(IDC_DLLS_ADDDLL);
517 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
519 WINE_TRACE("Adding %s as native, builtin\n", debugstr_w(buffer));
521 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
522 set_reg_key(config_key, keypath(L"DllOverrides"), buffer, L"native,builtin");
524 load_library_settings(dialog);
526 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
528 set_controls_from_selection(dialog);
531 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
533 static WORD sel;
535 switch(uMsg)
537 case WM_INITDIALOG:
538 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
539 sel = lParam;
540 return TRUE;
542 case WM_COMMAND:
543 if(HIWORD(wParam) != BN_CLICKED) break;
544 switch (LOWORD(wParam))
546 case IDC_RAD_BUILTIN:
547 case IDC_RAD_NATIVE:
548 case IDC_RAD_BUILTIN_NATIVE:
549 case IDC_RAD_NATIVE_BUILTIN:
550 case IDC_RAD_DISABLE:
551 sel = LOWORD(wParam);
552 return TRUE;
553 case IDOK:
554 EndDialog(hwndDlg, sel);
555 return TRUE;
556 case IDCANCEL:
557 EndDialog(hwndDlg, wParam);
558 return TRUE;
561 return FALSE;
564 static void on_edit_click(HWND hwnd)
566 INT_PTR ret;
567 int index = SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
568 struct dll *dll;
569 DWORD id;
571 /* if no override is selected the edit button should be disabled... */
572 assert(index != -1);
574 dll = (struct dll *) SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
575 id = mode_to_id(dll->mode);
577 ret = DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
579 if(ret != IDCANCEL)
580 set_dllmode(hwnd, ret);
583 static void on_remove_click(HWND dialog)
585 int sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
586 struct dll *dll;
588 if (sel == LB_ERR) return;
590 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
592 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
594 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
595 set_reg_key(config_key, keypath(L"DllOverrides"), dll->name, NULL);
597 free(dll->name);
598 free(dll);
600 if (SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
601 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
602 else
604 disable(IDC_DLLS_EDITDLL);
605 disable(IDC_DLLS_REMOVEDLL);
608 set_controls_from_selection(dialog);
611 INT_PTR CALLBACK
612 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
614 switch (uMsg)
616 case WM_INITDIALOG:
617 init_libsheet(hDlg);
618 break;
619 case WM_SHOWWINDOW:
620 set_window_title(hDlg);
621 break;
622 case WM_NOTIFY:
623 switch (((LPNMHDR)lParam)->code) {
624 case PSN_SETACTIVE:
625 load_library_settings(hDlg);
626 break;
628 break;
629 case WM_COMMAND:
630 switch(HIWORD(wParam)) {
631 case CBN_EDITCHANGE:
632 if (LOWORD(wParam) == IDC_DLLCOMBO)
633 on_add_combo_change(hDlg);
634 break;
635 case CBN_SETFOCUS:
636 if (LOWORD(wParam) == IDC_DLLCOMBO)
637 on_add_combo_change(hDlg);
638 break;
639 case CBN_KILLFOCUS:
640 if (LOWORD(wParam) == IDC_DLLCOMBO)
641 SendMessageW(GetParent(hDlg), DM_SETDEFID, IDOK, 0);
642 break;
643 case BN_CLICKED:
644 switch(LOWORD(wParam)) {
645 case IDC_DLLS_ADDDLL:
646 on_add_click(hDlg);
647 break;
648 case IDC_DLLS_EDITDLL:
649 on_edit_click(hDlg);
650 break;
651 case IDC_DLLS_REMOVEDLL:
652 on_remove_click(hDlg);
653 break;
655 break;
656 case LBN_SELCHANGE:
657 if(LOWORD(wParam) == IDC_DLLCOMBO)
658 on_add_combo_change(hDlg);
659 else
660 set_controls_from_selection(hDlg);
661 break;
663 break;
666 return 0;