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 NONAMELESSUNION
27 #define WIN32_LEAN_AND_MEAN
30 #include <wine/library.h>
31 #include <wine/debug.h>
36 #ifdef HAVE_SYS_STAT_H
46 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
48 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
49 static const char * const builtin_only
[] =
98 UNKNOWN
/* Special value indicating an erronous DLL override mode */
107 /* Convert a registry string to a dllmode */
108 static enum dllmode
string_to_mode(char *in
)
115 out
= HeapAlloc(GetProcessHeap(), 0, len
);
117 /* remove the spaces */
118 for (i
= j
= 0; i
<= len
; ++i
) {
124 /* parse the string */
126 if (strcmp(out
, "builtin,native") == 0) res
= BUILTIN_NATIVE
;
127 if (strcmp(out
, "native,builtin") == 0) res
= NATIVE_BUILTIN
;
128 if (strcmp(out
, "builtin") == 0) res
= BUILTIN
;
129 if (strcmp(out
, "native") == 0) res
= NATIVE
;
130 if (strcmp(out
, "") == 0) res
= DISABLE
;
132 HeapFree(GetProcessHeap(), 0, out
);
136 /* Convert a dllmode to a registry string. */
137 static const char* mode_to_string(enum dllmode mode
)
141 case NATIVE
: return "native";
142 case BUILTIN
: return "builtin";
143 case NATIVE_BUILTIN
: return "native,builtin";
144 case BUILTIN_NATIVE
: return "builtin,native";
145 case DISABLE
: return "";
146 default: assert(FALSE
); return "";
150 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
151 static const char* mode_to_label(enum dllmode mode
)
153 static char buffer
[256];
158 case NATIVE
: id
= IDS_DLL_NATIVE
; break;
159 case BUILTIN
: id
= IDS_DLL_BUILTIN
; break;
160 case NATIVE_BUILTIN
: id
= IDS_DLL_NATIVE_BUILTIN
; break;
161 case BUILTIN_NATIVE
: id
= IDS_DLL_BUILTIN_NATIVE
; break;
162 case DISABLE
: id
= IDS_DLL_DISABLED
; break;
163 default: assert(FALSE
);
165 if (!LoadStringA( GetModuleHandleA(NULL
), id
, buffer
, sizeof(buffer
) )) buffer
[0] = 0;
169 /* Convert a control id (IDC_ constant) to a dllmode */
170 static enum dllmode
id_to_mode(DWORD id
)
174 case IDC_RAD_BUILTIN
: return BUILTIN
;
175 case IDC_RAD_NATIVE
: return NATIVE
;
176 case IDC_RAD_NATIVE_BUILTIN
: return NATIVE_BUILTIN
;
177 case IDC_RAD_BUILTIN_NATIVE
: return BUILTIN_NATIVE
;
178 case IDC_RAD_DISABLE
: return DISABLE
;
179 default: assert( FALSE
); return 0; /* should not be reached */
183 /* Convert a dllmode to a control id (IDC_ constant) */
184 static DWORD
mode_to_id(enum dllmode mode
)
188 case BUILTIN
: return IDC_RAD_BUILTIN
;
189 case NATIVE
: return IDC_RAD_NATIVE
;
190 case NATIVE_BUILTIN
: return IDC_RAD_NATIVE_BUILTIN
;
191 case BUILTIN_NATIVE
: return IDC_RAD_BUILTIN_NATIVE
;
192 case DISABLE
: return IDC_RAD_DISABLE
;
193 default: assert( FALSE
); return 0; /* should not be reached */
197 /* helper for is_builtin_only */
198 static int compare_dll( const void *ptr1
, const void *ptr2
)
200 const char * const *name1
= ptr1
;
201 const char * const *name2
= ptr2
;
202 return strcmp( *name1
, *name2
);
205 /* check if dll is recommended as builtin only */
206 static inline int is_builtin_only( const char *name
)
208 return bsearch( &name
, builtin_only
, sizeof(builtin_only
)/sizeof(builtin_only
[0]),
209 sizeof(builtin_only
[0]), compare_dll
) != NULL
;
212 static void set_controls_from_selection(HWND dialog
)
214 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
217 static void clear_settings(HWND dialog
)
219 int count
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCOUNT
, 0, 0);
222 WINE_TRACE("count=%d\n", count
);
224 for (i
= 0; i
< count
; i
++)
226 struct dll
*dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, 0, 0);
228 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_DELETESTRING
, 0, 0);
230 HeapFree(GetProcessHeap(), 0, dll
->name
);
231 HeapFree(GetProcessHeap(), 0, dll
);
235 /* check if a given dll is 16-bit */
236 static int is_16bit_dll( const char *dir
, const char *name
)
240 size_t len
= strlen(dir
) + strlen(name
) + 2;
241 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
245 strcat( path
, name
);
246 res
= readlink( path
, buffer
, sizeof(buffer
) );
247 HeapFree( GetProcessHeap(), 0, path
);
249 if (res
== -1) return 0; /* not a symlink */
250 if (res
< 4 || res
>= sizeof(buffer
)) return 0;
252 if (strchr( buffer
, '/' )) return 0; /* contains a path, not valid */
253 if (strcmp( buffer
+ res
- 3, ".so" )) return 0; /* does not end in .so, not valid */
257 /* load the list of available libraries from a given dir */
258 static void load_library_list_from_dir( HWND dialog
, const char *dir_path
, int check_subdirs
)
260 char *buffer
= NULL
, name
[256];
262 DIR *dir
= opendir( dir_path
);
267 buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(dir_path
) + 2 * sizeof(name
) + 10 );
269 while ((de
= readdir( dir
)))
271 size_t len
= strlen(de
->d_name
);
272 if (len
> sizeof(name
)) continue;
273 if (len
> 7 && !strcmp( de
->d_name
+ len
- 7, ".dll.so"))
275 if (is_16bit_dll( dir_path
, de
->d_name
)) continue; /* 16-bit dlls can't be configured */
277 memcpy( name
, de
->d_name
, len
);
279 /* skip dlls that should always be builtin */
280 if (is_builtin_only( name
)) continue;
281 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_ADDSTRING
, 0, (LPARAM
)name
);
283 else if (check_subdirs
)
286 if (is_builtin_only( de
->d_name
)) continue;
287 sprintf( buffer
, "%s/%s/%s.dll.so", dir_path
, de
->d_name
, de
->d_name
);
288 if (!stat( buffer
, &st
))
289 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_ADDSTRING
, 0, (LPARAM
)de
->d_name
);
293 HeapFree( GetProcessHeap(), 0, buffer
);
296 /* load the list of available libraries */
297 static void load_library_list( HWND dialog
)
300 const char *path
, *build_dir
= wine_get_build_dir();
301 char item1
[256], item2
[256];
302 HCURSOR old_cursor
= SetCursor( LoadCursor(0, IDC_WAIT
) );
306 char *dir
= HeapAlloc( GetProcessHeap(), 0, strlen(build_dir
) + sizeof("/dlls") );
307 strcpy( dir
, build_dir
);
308 strcat( dir
, "/dlls" );
309 load_library_list_from_dir( dialog
, dir
, TRUE
);
310 HeapFree( GetProcessHeap(), 0, dir
);
313 while ((path
= wine_dll_enum_load_path( i
++ )))
314 load_library_list_from_dir( dialog
, path
, FALSE
);
316 /* get rid of duplicate entries */
318 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, 0, (LPARAM
)item1
);
320 while (SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, i
, (LPARAM
)item2
) >= 0)
322 if (!strcmp( item1
, item2
))
324 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_DELETESTRING
, i
, 0 );
328 strcpy( item1
, item2
);
332 SetCursor( old_cursor
);
335 static void load_library_settings(HWND dialog
)
337 char **overrides
= enumerate_values(config_key
, keypath("DllOverrides"));
341 sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
343 WINE_TRACE("sel=%d\n", sel
);
345 clear_settings(dialog
);
347 if (!overrides
|| *overrides
== NULL
)
349 set_controls_from_selection(dialog
);
350 disable(IDC_DLLS_EDITDLL
);
351 disable(IDC_DLLS_REMOVEDLL
);
352 HeapFree(GetProcessHeap(), 0, overrides
);
356 enable(IDC_DLLS_EDITDLL
);
357 enable(IDC_DLLS_REMOVEDLL
);
359 for (p
= overrides
; *p
!= NULL
; p
++)
366 value
= get_reg_key(config_key
, keypath("DllOverrides"), *p
, NULL
);
368 label
= mode_to_label(string_to_mode(value
));
370 str
= HeapAlloc(GetProcessHeap(), 0, strlen(*p
) + 2 + strlen(label
) + 2);
376 dll
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll
));
378 dll
->mode
= string_to_mode(value
);
380 index
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_ADDSTRING
, (WPARAM
) -1, (LPARAM
) str
);
381 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETITEMDATA
, index
, (LPARAM
) dll
);
383 HeapFree(GetProcessHeap(), 0, str
);
388 HeapFree(GetProcessHeap(), 0, overrides
);
390 /* restore the previous selection, if possible */
391 if (sel
>= count
- 1) sel
= count
- 1;
392 else if (sel
== -1) sel
= 0;
394 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETCURSEL
, sel
, 0);
396 set_controls_from_selection(dialog
);
399 /* Called when the application is initialized (cannot reinit!) */
400 static void init_libsheet(HWND dialog
)
402 /* clear the add dll controls */
403 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_SETTEXT
, 1, (LPARAM
) "");
404 load_library_list( dialog
);
405 disable(IDC_DLLS_ADDDLL
);
408 static void on_add_combo_change(HWND dialog
)
412 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, sizeof(buffer
), (LPARAM
) buffer
);
415 enable(IDC_DLLS_ADDDLL
)
417 disable(IDC_DLLS_ADDDLL
);
420 static void set_dllmode(HWND dialog
, DWORD id
)
427 mode
= id_to_mode(id
);
429 sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
430 if (sel
== -1) return;
432 dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, sel
, 0);
434 str
= mode_to_string(mode
);
435 WINE_TRACE("Setting %s to %s\n", dll
->name
, str
);
437 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
438 set_reg_key(config_key
, keypath("DllOverrides"), dll
->name
, str
);
440 load_library_settings(dialog
); /* ... and refresh */
443 static void on_add_click(HWND dialog
)
445 static const char dotDll
[] = ".dll";
446 char buffer
[1024], *ptr
;
448 ZeroMemory(buffer
, sizeof(buffer
));
450 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, sizeof(buffer
), (LPARAM
) buffer
);
451 if (lstrlenA(buffer
) >= sizeof(dotDll
))
453 ptr
= buffer
+ lstrlenA(buffer
) - sizeof(dotDll
) + 1;
454 if (!lstrcmpiA(ptr
, dotDll
))
456 WINE_TRACE("Stripping dll extension\n");
461 /* check if dll is in the builtin-only list */
462 if (!(ptr
= strrchr( buffer
, '\\' )))
465 if (*ptr
== '*') ptr
++;
468 if (is_builtin_only( ptr
))
470 MSGBOXPARAMSA params
;
471 params
.cbSize
= sizeof(params
);
472 params
.hwndOwner
= dialog
;
473 params
.hInstance
= GetModuleHandleA( NULL
);
474 params
.lpszText
= MAKEINTRESOURCEA( IDS_DLL_WARNING
);
475 params
.lpszCaption
= MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION
);
476 params
.dwStyle
= MB_ICONWARNING
| MB_YESNO
;
477 params
.lpszIcon
= NULL
;
478 params
.dwContextHelpId
= 0;
479 params
.lpfnMsgBoxCallback
= NULL
;
480 params
.dwLanguageId
= 0;
481 if (MessageBoxIndirectA( ¶ms
) != IDYES
) return;
484 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_SETTEXT
, 0, (LPARAM
) "");
485 disable(IDC_DLLS_ADDDLL
);
487 WINE_TRACE("Adding %s as native, builtin\n", buffer
);
489 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
490 set_reg_key(config_key
, keypath("DllOverrides"), buffer
, "native,builtin");
492 load_library_settings(dialog
);
494 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SELECTSTRING
, (WPARAM
) 0, (LPARAM
) buffer
);
496 set_controls_from_selection(dialog
);
499 static INT_PTR CALLBACK
loadorder_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
506 CheckRadioButton(hwndDlg
, IDC_RAD_BUILTIN
, IDC_RAD_DISABLE
, lParam
);
511 if(HIWORD(wParam
) != BN_CLICKED
) break;
512 switch (LOWORD(wParam
))
514 case IDC_RAD_BUILTIN
:
516 case IDC_RAD_BUILTIN_NATIVE
:
517 case IDC_RAD_NATIVE_BUILTIN
:
518 case IDC_RAD_DISABLE
:
519 sel
= LOWORD(wParam
);
522 EndDialog(hwndDlg
, sel
);
525 EndDialog(hwndDlg
, wParam
);
532 static void on_edit_click(HWND hwnd
)
535 int index
= SendDlgItemMessage(hwnd
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
539 /* if no override is selected the edit button should be disabled... */
542 dll
= (struct dll
*) SendDlgItemMessage(hwnd
, IDC_DLLS_LIST
, LB_GETITEMDATA
, index
, 0);
543 id
= mode_to_id(dll
->mode
);
545 ret
= DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER
), hwnd
, loadorder_dlgproc
, id
);
548 set_dllmode(hwnd
, ret
);
551 static void on_remove_click(HWND dialog
)
553 int sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
556 if (sel
== LB_ERR
) return;
558 dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, sel
, 0);
560 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_DELETESTRING
, sel
, 0);
562 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
563 set_reg_key(config_key
, keypath("DllOverrides"), dll
->name
, NULL
);
565 HeapFree(GetProcessHeap(), 0, dll
->name
);
566 HeapFree(GetProcessHeap(), 0, dll
);
568 if (SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCOUNT
, 0, 0) > 0)
569 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETCURSEL
, max(sel
- 1, 0), 0);
572 disable(IDC_DLLS_EDITDLL
);
573 disable(IDC_DLLS_REMOVEDLL
);
576 set_controls_from_selection(dialog
);
580 LibrariesDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
588 set_window_title(hDlg
);
591 switch (((LPNMHDR
)lParam
)->code
) {
593 load_library_settings(hDlg
);
598 switch(HIWORD(wParam
)) {
600 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
601 * add button, rather than the propsheet OK button. But I don't know how to do that!
605 if(LOWORD(wParam
) == IDC_DLLCOMBO
)
607 on_add_combo_change(hDlg
);
612 switch(LOWORD(wParam
)) {
613 case IDC_DLLS_ADDDLL
:
616 case IDC_DLLS_EDITDLL
:
619 case IDC_DLLS_REMOVEDLL
:
620 on_remove_click(hDlg
);
625 if(LOWORD(wParam
) == IDC_DLLCOMBO
)
626 on_add_combo_change(hDlg
);
628 set_controls_from_selection(hDlg
);