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
[] =
99 UNKNOWN
/* Special value indicating an erroneous DLL override mode */
108 /* Convert a registry string to a dllmode */
109 static enum dllmode
string_to_mode(char *in
)
116 out
= HeapAlloc(GetProcessHeap(), 0, len
);
118 /* remove the spaces */
119 for (i
= j
= 0; i
<= len
; ++i
) {
125 /* parse the string */
127 if (strcmp(out
, "builtin,native") == 0) res
= BUILTIN_NATIVE
;
128 if (strcmp(out
, "native,builtin") == 0) res
= NATIVE_BUILTIN
;
129 if (strcmp(out
, "builtin") == 0) res
= BUILTIN
;
130 if (strcmp(out
, "native") == 0) res
= NATIVE
;
131 if (strcmp(out
, "") == 0) res
= DISABLE
;
133 HeapFree(GetProcessHeap(), 0, out
);
137 /* Convert a dllmode to a registry string. */
138 static const char* mode_to_string(enum dllmode mode
)
142 case NATIVE
: return "native";
143 case BUILTIN
: return "builtin";
144 case NATIVE_BUILTIN
: return "native,builtin";
145 case BUILTIN_NATIVE
: return "builtin,native";
146 case DISABLE
: return "";
147 default: assert(FALSE
); return "";
151 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
152 static const char* mode_to_label(enum dllmode mode
)
154 static char buffer
[256];
159 case NATIVE
: id
= IDS_DLL_NATIVE
; break;
160 case BUILTIN
: id
= IDS_DLL_BUILTIN
; break;
161 case NATIVE_BUILTIN
: id
= IDS_DLL_NATIVE_BUILTIN
; break;
162 case BUILTIN_NATIVE
: id
= IDS_DLL_BUILTIN_NATIVE
; break;
163 case DISABLE
: id
= IDS_DLL_DISABLED
; break;
164 default: assert(FALSE
);
166 if (!LoadStringA( GetModuleHandleA(NULL
), id
, buffer
, sizeof(buffer
) )) buffer
[0] = 0;
170 /* Convert a control id (IDC_ constant) to a dllmode */
171 static enum dllmode
id_to_mode(DWORD id
)
175 case IDC_RAD_BUILTIN
: return BUILTIN
;
176 case IDC_RAD_NATIVE
: return NATIVE
;
177 case IDC_RAD_NATIVE_BUILTIN
: return NATIVE_BUILTIN
;
178 case IDC_RAD_BUILTIN_NATIVE
: return BUILTIN_NATIVE
;
179 case IDC_RAD_DISABLE
: return DISABLE
;
180 default: assert( FALSE
); return 0; /* should not be reached */
184 /* Convert a dllmode to a control id (IDC_ constant) */
185 static DWORD
mode_to_id(enum dllmode mode
)
189 case BUILTIN
: return IDC_RAD_BUILTIN
;
190 case NATIVE
: return IDC_RAD_NATIVE
;
191 case NATIVE_BUILTIN
: return IDC_RAD_NATIVE_BUILTIN
;
192 case BUILTIN_NATIVE
: return IDC_RAD_BUILTIN_NATIVE
;
193 case DISABLE
: return IDC_RAD_DISABLE
;
194 default: assert( FALSE
); return 0; /* should not be reached */
198 /* helper for is_builtin_only */
199 static int compare_dll( const void *ptr1
, const void *ptr2
)
201 const char * const *name1
= ptr1
;
202 const char * const *name2
= ptr2
;
203 return strcmp( *name1
, *name2
);
206 /* check if dll is recommended as builtin only */
207 static inline int is_builtin_only( const char *name
)
209 return bsearch( &name
, builtin_only
, sizeof(builtin_only
)/sizeof(builtin_only
[0]),
210 sizeof(builtin_only
[0]), compare_dll
) != NULL
;
213 static void set_controls_from_selection(HWND dialog
)
215 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
218 static void clear_settings(HWND dialog
)
220 int count
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCOUNT
, 0, 0);
223 WINE_TRACE("count=%d\n", count
);
225 for (i
= 0; i
< count
; i
++)
227 struct dll
*dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, 0, 0);
229 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_DELETESTRING
, 0, 0);
231 HeapFree(GetProcessHeap(), 0, dll
->name
);
232 HeapFree(GetProcessHeap(), 0, dll
);
236 /* check if a given dll is 16-bit */
237 static int is_16bit_dll( const char *dir
, const char *name
)
241 size_t len
= strlen(dir
) + strlen(name
) + 2;
242 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
246 strcat( path
, name
);
247 res
= readlink( path
, buffer
, sizeof(buffer
) );
248 HeapFree( GetProcessHeap(), 0, path
);
250 if (res
== -1) return 0; /* not a symlink */
251 if (res
< 4 || res
>= sizeof(buffer
)) return 0;
253 if (strchr( buffer
, '/' )) return 0; /* contains a path, not valid */
254 if (strcmp( buffer
+ res
- 3, ".so" )) return 0; /* does not end in .so, not valid */
258 /* load the list of available libraries from a given dir */
259 static void load_library_list_from_dir( HWND dialog
, const char *dir_path
, int check_subdirs
)
261 char *buffer
= NULL
, name
[256];
263 DIR *dir
= opendir( dir_path
);
268 buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(dir_path
) + 2 * sizeof(name
) + 10 );
270 while ((de
= readdir( dir
)))
272 size_t len
= strlen(de
->d_name
);
273 if (len
> sizeof(name
)) continue;
274 if (len
> 7 && !strcmp( de
->d_name
+ len
- 7, ".dll.so"))
276 if (is_16bit_dll( dir_path
, de
->d_name
)) continue; /* 16-bit dlls can't be configured */
278 memcpy( name
, de
->d_name
, len
);
280 /* skip dlls that should always be builtin */
281 if (is_builtin_only( name
)) continue;
282 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_ADDSTRING
, 0, (LPARAM
)name
);
284 else if (check_subdirs
)
287 if (is_builtin_only( de
->d_name
)) continue;
288 sprintf( buffer
, "%s/%s/%s.dll.so", dir_path
, de
->d_name
, de
->d_name
);
289 if (!stat( buffer
, &st
))
290 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_ADDSTRING
, 0, (LPARAM
)de
->d_name
);
294 HeapFree( GetProcessHeap(), 0, buffer
);
297 /* load the list of available libraries */
298 static void load_library_list( HWND dialog
)
301 const char *path
, *build_dir
= wine_get_build_dir();
302 char item1
[256], item2
[256];
303 HCURSOR old_cursor
= SetCursor( LoadCursor(0, IDC_WAIT
) );
307 char *dir
= HeapAlloc( GetProcessHeap(), 0, strlen(build_dir
) + sizeof("/dlls") );
308 strcpy( dir
, build_dir
);
309 strcat( dir
, "/dlls" );
310 load_library_list_from_dir( dialog
, dir
, TRUE
);
311 HeapFree( GetProcessHeap(), 0, dir
);
314 while ((path
= wine_dll_enum_load_path( i
++ )))
315 load_library_list_from_dir( dialog
, path
, FALSE
);
317 /* get rid of duplicate entries */
319 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, 0, (LPARAM
)item1
);
321 while (SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, i
, (LPARAM
)item2
) >= 0)
323 if (!strcmp( item1
, item2
))
325 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_DELETESTRING
, i
, 0 );
329 strcpy( item1
, item2
);
333 SetCursor( old_cursor
);
336 static void load_library_settings(HWND dialog
)
338 char **overrides
= enumerate_values(config_key
, keypath("DllOverrides"));
342 sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
344 WINE_TRACE("sel=%d\n", sel
);
346 clear_settings(dialog
);
348 if (!overrides
|| *overrides
== NULL
)
350 set_controls_from_selection(dialog
);
351 disable(IDC_DLLS_EDITDLL
);
352 disable(IDC_DLLS_REMOVEDLL
);
353 HeapFree(GetProcessHeap(), 0, overrides
);
357 enable(IDC_DLLS_EDITDLL
);
358 enable(IDC_DLLS_REMOVEDLL
);
360 for (p
= overrides
; *p
!= NULL
; p
++)
367 value
= get_reg_key(config_key
, keypath("DllOverrides"), *p
, NULL
);
369 label
= mode_to_label(string_to_mode(value
));
371 str
= HeapAlloc(GetProcessHeap(), 0, strlen(*p
) + 2 + strlen(label
) + 2);
377 dll
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll
));
379 dll
->mode
= string_to_mode(value
);
381 index
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_ADDSTRING
, (WPARAM
) -1, (LPARAM
) str
);
382 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETITEMDATA
, index
, (LPARAM
) dll
);
384 HeapFree(GetProcessHeap(), 0, str
);
389 HeapFree(GetProcessHeap(), 0, overrides
);
391 /* restore the previous selection, if possible */
392 if (sel
>= count
- 1) sel
= count
- 1;
393 else if (sel
== -1) sel
= 0;
395 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETCURSEL
, sel
, 0);
397 set_controls_from_selection(dialog
);
400 /* Called when the application is initialized (cannot reinit!) */
401 static void init_libsheet(HWND dialog
)
403 /* clear the add dll controls */
404 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_SETTEXT
, 1, (LPARAM
) "");
405 load_library_list( dialog
);
406 disable(IDC_DLLS_ADDDLL
);
409 static void on_add_combo_change(HWND dialog
)
414 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, sizeof(buffer
), (LPARAM
) buffer
);
415 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
416 sel
=SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, CB_GETCURSEL
, 0, 0);
417 len
=SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, CB_GETLBTEXTLEN
, sel
, 0);
419 if (strlen(buffer
)>0 || len
>0)
420 enable(IDC_DLLS_ADDDLL
)
422 disable(IDC_DLLS_ADDDLL
);
425 static void set_dllmode(HWND dialog
, DWORD id
)
432 mode
= id_to_mode(id
);
434 sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
435 if (sel
== -1) return;
437 dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, sel
, 0);
439 str
= mode_to_string(mode
);
440 WINE_TRACE("Setting %s to %s\n", dll
->name
, str
);
442 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
443 set_reg_key(config_key
, keypath("DllOverrides"), dll
->name
, str
);
445 load_library_settings(dialog
); /* ... and refresh */
448 static void on_add_click(HWND dialog
)
450 static const char dotDll
[] = ".dll";
451 char buffer
[1024], *ptr
;
453 ZeroMemory(buffer
, sizeof(buffer
));
455 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, sizeof(buffer
), (LPARAM
) buffer
);
456 if (lstrlenA(buffer
) >= sizeof(dotDll
))
458 ptr
= buffer
+ lstrlenA(buffer
) - sizeof(dotDll
) + 1;
459 if (!lstrcmpiA(ptr
, dotDll
))
461 WINE_TRACE("Stripping dll extension\n");
466 /* check if dll is in the builtin-only list */
467 if (!(ptr
= strrchr( buffer
, '\\' )))
470 if (*ptr
== '*') ptr
++;
473 if (is_builtin_only( ptr
))
475 MSGBOXPARAMSA params
;
476 params
.cbSize
= sizeof(params
);
477 params
.hwndOwner
= dialog
;
478 params
.hInstance
= GetModuleHandleA( NULL
);
479 params
.lpszText
= MAKEINTRESOURCEA( IDS_DLL_WARNING
);
480 params
.lpszCaption
= MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION
);
481 params
.dwStyle
= MB_ICONWARNING
| MB_YESNO
;
482 params
.lpszIcon
= NULL
;
483 params
.dwContextHelpId
= 0;
484 params
.lpfnMsgBoxCallback
= NULL
;
485 params
.dwLanguageId
= 0;
486 if (MessageBoxIndirectA( ¶ms
) != IDYES
) return;
489 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_SETTEXT
, 0, (LPARAM
) "");
490 disable(IDC_DLLS_ADDDLL
);
492 WINE_TRACE("Adding %s as native, builtin\n", buffer
);
494 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
495 set_reg_key(config_key
, keypath("DllOverrides"), buffer
, "native,builtin");
497 load_library_settings(dialog
);
499 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SELECTSTRING
, (WPARAM
) 0, (LPARAM
) buffer
);
501 set_controls_from_selection(dialog
);
504 static INT_PTR CALLBACK
loadorder_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
511 CheckRadioButton(hwndDlg
, IDC_RAD_BUILTIN
, IDC_RAD_DISABLE
, lParam
);
516 if(HIWORD(wParam
) != BN_CLICKED
) break;
517 switch (LOWORD(wParam
))
519 case IDC_RAD_BUILTIN
:
521 case IDC_RAD_BUILTIN_NATIVE
:
522 case IDC_RAD_NATIVE_BUILTIN
:
523 case IDC_RAD_DISABLE
:
524 sel
= LOWORD(wParam
);
527 EndDialog(hwndDlg
, sel
);
530 EndDialog(hwndDlg
, wParam
);
537 static void on_edit_click(HWND hwnd
)
540 int index
= SendDlgItemMessage(hwnd
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
544 /* if no override is selected the edit button should be disabled... */
547 dll
= (struct dll
*) SendDlgItemMessage(hwnd
, IDC_DLLS_LIST
, LB_GETITEMDATA
, index
, 0);
548 id
= mode_to_id(dll
->mode
);
550 ret
= DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER
), hwnd
, loadorder_dlgproc
, id
);
553 set_dllmode(hwnd
, ret
);
556 static void on_remove_click(HWND dialog
)
558 int sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
561 if (sel
== LB_ERR
) return;
563 dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, sel
, 0);
565 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_DELETESTRING
, sel
, 0);
567 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
568 set_reg_key(config_key
, keypath("DllOverrides"), dll
->name
, NULL
);
570 HeapFree(GetProcessHeap(), 0, dll
->name
);
571 HeapFree(GetProcessHeap(), 0, dll
);
573 if (SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCOUNT
, 0, 0) > 0)
574 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETCURSEL
, max(sel
- 1, 0), 0);
577 disable(IDC_DLLS_EDITDLL
);
578 disable(IDC_DLLS_REMOVEDLL
);
581 set_controls_from_selection(dialog
);
585 LibrariesDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
593 set_window_title(hDlg
);
596 switch (((LPNMHDR
)lParam
)->code
) {
598 load_library_settings(hDlg
);
603 switch(HIWORD(wParam
)) {
605 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
606 * add button, rather than the propsheet OK button. But I don't know how to do that!
610 if(LOWORD(wParam
) == IDC_DLLCOMBO
)
612 on_add_combo_change(hDlg
);
617 switch(LOWORD(wParam
)) {
618 case IDC_DLLS_ADDDLL
:
621 case IDC_DLLS_EDITDLL
:
624 case IDC_DLLS_REMOVEDLL
:
625 on_remove_click(hDlg
);
630 if(LOWORD(wParam
) == IDC_DLLCOMBO
)
631 on_add_combo_change(hDlg
);
633 set_controls_from_selection(hDlg
);