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
24 #include "wine/port.h"
26 #define WIN32_LEAN_AND_MEAN
29 #include <wine/debug.h>
34 #ifdef HAVE_SYS_STAT_H
44 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
46 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
47 static const char * const builtin_only
[] =
86 UNKNOWN
/* Special value indicating an erroneous DLL override mode */
95 static const WCHAR emptyW
[1];
97 /* Convert a registry string to a dllmode */
98 static enum dllmode
string_to_mode(char *in
)
105 out
= HeapAlloc(GetProcessHeap(), 0, len
+ 1);
107 /* remove the spaces */
108 for (i
= j
= 0; i
<= len
; ++i
) {
114 /* parse the string */
116 if (strcmp(out
, "builtin,native") == 0) res
= BUILTIN_NATIVE
;
117 if (strcmp(out
, "native,builtin") == 0) res
= NATIVE_BUILTIN
;
118 if (strcmp(out
, "builtin") == 0) res
= BUILTIN
;
119 if (strcmp(out
, "native") == 0) res
= NATIVE
;
120 if (strcmp(out
, "") == 0) res
= DISABLE
;
122 HeapFree(GetProcessHeap(), 0, out
);
126 /* Convert a dllmode to a registry string. */
127 static const char* mode_to_string(enum dllmode mode
)
131 case NATIVE
: return "native";
132 case BUILTIN
: return "builtin";
133 case NATIVE_BUILTIN
: return "native,builtin";
134 case BUILTIN_NATIVE
: return "builtin,native";
135 case DISABLE
: return "";
140 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
141 static const char* mode_to_label(enum dllmode mode
)
143 static char buffer
[256];
148 case NATIVE
: id
= IDS_DLL_NATIVE
; break;
149 case BUILTIN
: id
= IDS_DLL_BUILTIN
; break;
150 case NATIVE_BUILTIN
: id
= IDS_DLL_NATIVE_BUILTIN
; break;
151 case BUILTIN_NATIVE
: id
= IDS_DLL_BUILTIN_NATIVE
; break;
152 case DISABLE
: id
= IDS_DLL_DISABLED
; break;
153 default: return "??";
155 if (!LoadStringA( GetModuleHandleA(NULL
), id
, buffer
, sizeof(buffer
) )) buffer
[0] = 0;
159 /* Convert a control id (IDC_ constant) to a dllmode */
160 static enum dllmode
id_to_mode(DWORD id
)
164 case IDC_RAD_BUILTIN
: return BUILTIN
;
165 case IDC_RAD_NATIVE
: return NATIVE
;
166 case IDC_RAD_NATIVE_BUILTIN
: return NATIVE_BUILTIN
;
167 case IDC_RAD_BUILTIN_NATIVE
: return BUILTIN_NATIVE
;
168 case IDC_RAD_DISABLE
: return DISABLE
;
169 default: assert( FALSE
); return 0; /* should not be reached */
173 /* Convert a dllmode to a control id (IDC_ constant) */
174 static DWORD
mode_to_id(enum dllmode mode
)
178 case BUILTIN
: return IDC_RAD_BUILTIN
;
179 case NATIVE
: return IDC_RAD_NATIVE
;
180 case NATIVE_BUILTIN
: return IDC_RAD_NATIVE_BUILTIN
;
181 case BUILTIN_NATIVE
: return IDC_RAD_BUILTIN_NATIVE
;
182 case DISABLE
: return IDC_RAD_DISABLE
;
183 default: return IDC_RAD_BUILTIN_NATIVE
;
187 /* helper for is_builtin_only */
188 static int compare_dll( const void *ptr1
, const void *ptr2
)
190 const char * const *name1
= ptr1
;
191 const char * const *name2
= ptr2
;
192 return strcmp( *name1
, *name2
);
195 /* check if dll is recommended as builtin only */
196 static inline BOOL
is_builtin_only( const char *name
)
198 const char *ext
= strrchr( name
, '.' );
202 if (!strcmp( ext
, ".vxd" ) ||
203 !strcmp( ext
, ".drv" ) ||
204 !strcmp( ext
, ".tlb" ))
207 if (!strncmp( name
, "wine", 4 )) return TRUE
;
208 return bsearch( &name
, builtin_only
, ARRAY_SIZE(builtin_only
),
209 sizeof(builtin_only
[0]), compare_dll
) != NULL
;
212 /* check if dll should be offered in the drop-down list */
213 static BOOL
show_dll_in_list( const char *name
)
215 const char *ext
= strrchr( name
, '.' );
219 /* skip 16-bit dlls */
220 if (strlen(ext
) > 2 && !strcmp( ext
+ strlen(ext
) - 2, "16" )) return FALSE
;
222 if (!strcmp( ext
, ".exe" )) return FALSE
;
224 /* skip api set placeholders */
225 if (!strncmp( name
, "api-ms-", 7 ) || !strncmp( name
, "ext-ms-", 7 )) 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);
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 HeapFree(GetProcessHeap(), 0, dll
->name
);
248 HeapFree(GetProcessHeap(), 0, dll
);
252 /* load the list of available libraries from a given dir */
253 static void load_library_list_from_dir( HWND dialog
, const char *dir_path
, int check_subdirs
)
255 static const char * const ext
[] = { ".dll", ".dll.so", ".so", "" };
256 char *buffer
, *p
, name
[256];
259 WIN32_FIND_DATAA data
;
261 buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(dir_path
) + 2 * sizeof(name
) + 10 );
263 strcpy( buffer
, dir_path
);
264 strcat( buffer
, "\\*" );
265 buffer
[1] = '\\'; /* change \??\ to \\?\ */
266 p
= buffer
+ strlen(buffer
) - 1;
268 if ((handle
= FindFirstFileA( buffer
, &data
)) == INVALID_HANDLE_VALUE
)
270 HeapFree( GetProcessHeap(), 0, buffer
);
276 size_t len
= strlen(data
.cFileName
);
277 if (len
> sizeof(name
)) continue;
280 if (!strcmp( data
.cFileName
, "." )) continue;
281 if (!strcmp( data
.cFileName
, ".." )) continue;
282 if (!show_dll_in_list( data
.cFileName
)) continue;
283 for (i
= 0; i
< ARRAY_SIZE( ext
); i
++)
285 sprintf( p
, "%s\\%s%s", data
.cFileName
, data
.cFileName
, ext
[i
] );
286 if (GetFileAttributesA( buffer
) != INVALID_FILE_ATTRIBUTES
)
288 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_ADDSTRING
, 0, (LPARAM
)data
.cFileName
);
295 for (i
= 0; i
< ARRAY_SIZE( ext
); i
++)
297 if (!ext
[i
][0]) continue;
298 if (len
> strlen(ext
[i
]) && !strcmp( data
.cFileName
+ len
- strlen(ext
[i
]), ext
[i
]))
300 len
-= strlen( ext
[i
] );
301 memcpy( name
, data
.cFileName
, len
);
303 if (!show_dll_in_list( name
)) continue;
304 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_ADDSTRING
, 0, (LPARAM
)name
);
308 } while (FindNextFileA( handle
, &data
));
311 HeapFree( GetProcessHeap(), 0, buffer
);
314 /* load the list of available libraries */
315 static void load_library_list( HWND dialog
)
318 char item1
[256], item2
[256], var
[32], path
[MAX_PATH
];
319 HCURSOR old_cursor
= SetCursor( LoadCursorW(0, (LPWSTR
)IDC_WAIT
) );
321 if (GetEnvironmentVariableA( "WINEBUILDDIR", path
, MAX_PATH
))
323 char *dir
= HeapAlloc( GetProcessHeap(), 0, strlen(path
) + sizeof("\\dlls") );
325 strcat( dir
, "\\dlls" );
326 load_library_list_from_dir( dialog
, dir
, TRUE
);
327 HeapFree( GetProcessHeap(), 0, dir
);
332 sprintf( var
, "WINEDLLDIR%u", i
++ );
333 if (!GetEnvironmentVariableA( var
, path
, MAX_PATH
)) break;
334 load_library_list_from_dir( dialog
, path
, FALSE
);
337 /* get rid of duplicate entries */
339 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, 0, (LPARAM
)item1
);
341 while (SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, i
, (LPARAM
)item2
) >= 0)
343 if (!strcmp( item1
, item2
))
345 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_DELETESTRING
, i
, 0 );
349 strcpy( item1
, item2
);
353 SetCursor( old_cursor
);
356 static void load_library_settings(HWND dialog
)
358 char **overrides
= enumerate_values(config_key
, keypath("DllOverrides"));
362 sel
= SendDlgItemMessageW(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
364 WINE_TRACE("sel=%d\n", sel
);
366 clear_settings(dialog
);
368 if (!overrides
|| *overrides
== NULL
)
370 set_controls_from_selection(dialog
);
371 disable(IDC_DLLS_EDITDLL
);
372 disable(IDC_DLLS_REMOVEDLL
);
373 HeapFree(GetProcessHeap(), 0, overrides
);
377 enable(IDC_DLLS_EDITDLL
);
378 enable(IDC_DLLS_REMOVEDLL
);
380 for (p
= overrides
; *p
!= NULL
; p
++)
387 value
= get_reg_key(config_key
, keypath("DllOverrides"), *p
, NULL
);
389 label
= mode_to_label(string_to_mode(value
));
391 str
= HeapAlloc(GetProcessHeap(), 0, strlen(*p
) + 2 + strlen(label
) + 2);
397 dll
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll
));
399 dll
->mode
= string_to_mode(value
);
401 index
= SendDlgItemMessageA(dialog
, IDC_DLLS_LIST
, LB_ADDSTRING
, (WPARAM
) -1, (LPARAM
) str
);
402 SendDlgItemMessageW(dialog
, IDC_DLLS_LIST
, LB_SETITEMDATA
, index
, (LPARAM
) dll
);
404 HeapFree(GetProcessHeap(), 0, str
);
409 HeapFree(GetProcessHeap(), 0, overrides
);
411 /* restore the previous selection, if possible */
412 if (sel
>= count
- 1) sel
= count
- 1;
413 else if (sel
== -1) sel
= 0;
415 SendDlgItemMessageW(dialog
, IDC_DLLS_LIST
, LB_SETCURSEL
, sel
, 0);
417 set_controls_from_selection(dialog
);
420 /* Called when the application is initialized (cannot reinit!) */
421 static void init_libsheet(HWND dialog
)
423 /* clear the add dll controls */
424 SendDlgItemMessageW(dialog
, IDC_DLLCOMBO
, WM_SETTEXT
, 1, (LPARAM
)emptyW
);
425 load_library_list( dialog
);
426 disable(IDC_DLLS_ADDDLL
);
429 static void on_add_combo_change(HWND dialog
)
434 SendDlgItemMessageW(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, ARRAY_SIZE(buffer
), (LPARAM
)buffer
);
435 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
436 sel
=SendDlgItemMessageW(dialog
, IDC_DLLCOMBO
, CB_GETCURSEL
, 0, 0);
437 len
=SendDlgItemMessageW(dialog
, IDC_DLLCOMBO
, CB_GETLBTEXTLEN
, sel
, 0);
439 if (buffer
[0] || len
>0)
441 enable(IDC_DLLS_ADDDLL
)
442 SendMessageW(GetParent(dialog
), DM_SETDEFID
, IDC_DLLS_ADDDLL
, 0);
446 disable(IDC_DLLS_ADDDLL
);
447 SendMessageW(GetParent(dialog
), DM_SETDEFID
, IDOK
, 0);
451 static void set_dllmode(HWND dialog
, DWORD id
)
458 mode
= id_to_mode(id
);
460 sel
= SendDlgItemMessageW(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
461 if (sel
== -1) return;
463 dll
= (struct dll
*) SendDlgItemMessageW(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, sel
, 0);
465 str
= mode_to_string(mode
);
466 WINE_TRACE("Setting %s to %s\n", dll
->name
, str
);
468 SendMessageW(GetParent(dialog
), PSM_CHANGED
, 0, 0);
469 set_reg_key(config_key
, keypath("DllOverrides"), dll
->name
, str
);
471 load_library_settings(dialog
); /* ... and refresh */
474 static void on_add_click(HWND dialog
)
476 static const char dotDll
[] = ".dll";
477 char buffer
[1024], *ptr
;
479 ZeroMemory(buffer
, sizeof(buffer
));
481 SendDlgItemMessageA(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, sizeof(buffer
), (LPARAM
) buffer
);
482 if (lstrlenA(buffer
) >= sizeof(dotDll
))
484 ptr
= buffer
+ lstrlenA(buffer
) - sizeof(dotDll
) + 1;
485 if (!lstrcmpiA(ptr
, dotDll
))
487 WINE_TRACE("Stripping dll extension\n");
492 /* check if dll is in the builtin-only list */
493 if (!(ptr
= strrchr( buffer
, '\\' )))
496 if (*ptr
== '*') ptr
++;
499 if (is_builtin_only( ptr
))
501 MSGBOXPARAMSA params
;
502 params
.cbSize
= sizeof(params
);
503 params
.hwndOwner
= dialog
;
504 params
.hInstance
= GetModuleHandleA( NULL
);
505 params
.lpszText
= MAKEINTRESOURCEA( IDS_DLL_WARNING
);
506 params
.lpszCaption
= MAKEINTRESOURCEA( 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 (MessageBoxIndirectA( ¶ms
) != 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", buffer
);
521 SendMessageW(GetParent(dialog
), PSM_CHANGED
, 0, 0);
522 set_reg_key(config_key
, keypath("DllOverrides"), buffer
, "native,builtin");
524 load_library_settings(dialog
);
526 SendDlgItemMessageA(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
)
538 CheckRadioButton(hwndDlg
, IDC_RAD_BUILTIN
, IDC_RAD_DISABLE
, lParam
);
543 if(HIWORD(wParam
) != BN_CLICKED
) break;
544 switch (LOWORD(wParam
))
546 case IDC_RAD_BUILTIN
:
548 case IDC_RAD_BUILTIN_NATIVE
:
549 case IDC_RAD_NATIVE_BUILTIN
:
550 case IDC_RAD_DISABLE
:
551 sel
= LOWORD(wParam
);
554 EndDialog(hwndDlg
, sel
);
557 EndDialog(hwndDlg
, wParam
);
564 static void on_edit_click(HWND hwnd
)
567 int index
= SendDlgItemMessageW(hwnd
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
571 /* if no override is selected the edit button should be disabled... */
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
);
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);
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("DllOverrides"), dll
->name
, NULL
);
597 HeapFree(GetProcessHeap(), 0, dll
->name
);
598 HeapFree(GetProcessHeap(), 0, 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);
604 disable(IDC_DLLS_EDITDLL
);
605 disable(IDC_DLLS_REMOVEDLL
);
608 set_controls_from_selection(dialog
);
612 LibrariesDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
620 set_window_title(hDlg
);
623 switch (((LPNMHDR
)lParam
)->code
) {
625 load_library_settings(hDlg
);
630 switch(HIWORD(wParam
)) {
632 if (LOWORD(wParam
) == IDC_DLLCOMBO
)
633 on_add_combo_change(hDlg
);
636 if (LOWORD(wParam
) == IDC_DLLCOMBO
)
637 on_add_combo_change(hDlg
);
640 if (LOWORD(wParam
) == IDC_DLLCOMBO
)
641 SendMessageW(GetParent(hDlg
), DM_SETDEFID
, IDOK
, 0);
644 switch(LOWORD(wParam
)) {
645 case IDC_DLLS_ADDDLL
:
648 case IDC_DLLS_EDITDLL
:
651 case IDC_DLLS_REMOVEDLL
:
652 on_remove_click(hDlg
);
657 if(LOWORD(wParam
) == IDC_DLLCOMBO
)
658 on_add_combo_change(hDlg
);
660 set_controls_from_selection(hDlg
);