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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define NONAMELESSUNION
26 #define WIN32_LEAN_AND_MEAN
29 #include <wine/library.h>
30 #include <wine/debug.h>
42 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
44 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
45 static const char * const builtin_only
[] =
96 UNKNOWN
/* Special value indicating an erronous DLL override mode */
105 /* Convert a registry string to a dllmode */
106 static enum dllmode
string_to_mode(char *in
)
113 out
= HeapAlloc(GetProcessHeap(), 0, len
);
115 /* remove the spaces */
116 for (i
= j
= 0; i
<= len
; ++i
) {
122 /* parse the string */
124 if (strcmp(out
, "builtin,native") == 0) res
= BUILTIN_NATIVE
;
125 if (strcmp(out
, "native,builtin") == 0) res
= NATIVE_BUILTIN
;
126 if (strcmp(out
, "builtin") == 0) res
= BUILTIN
;
127 if (strcmp(out
, "native") == 0) res
= NATIVE
;
128 if (strcmp(out
, "") == 0) res
= DISABLE
;
130 HeapFree(GetProcessHeap(), 0, out
);
134 /* Convert a dllmode to a registry string. */
135 static const char* mode_to_string(enum dllmode mode
)
139 case NATIVE
: return "native";
140 case BUILTIN
: return "builtin";
141 case NATIVE_BUILTIN
: return "native,builtin";
142 case BUILTIN_NATIVE
: return "builtin,native";
143 case DISABLE
: return "";
144 default: assert(FALSE
); return "";
148 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
149 static const char* mode_to_label(enum dllmode mode
)
151 static char buffer
[256];
156 case NATIVE
: id
= IDS_DLL_NATIVE
; break;
157 case BUILTIN
: id
= IDS_DLL_BUILTIN
; break;
158 case NATIVE_BUILTIN
: id
= IDS_DLL_NATIVE_BUILTIN
; break;
159 case BUILTIN_NATIVE
: id
= IDS_DLL_BUILTIN_NATIVE
; break;
160 case DISABLE
: id
= IDS_DLL_DISABLED
; break;
161 default: assert(FALSE
);
163 if (!LoadStringA( GetModuleHandleA(NULL
), id
, buffer
, sizeof(buffer
) )) buffer
[0] = 0;
167 /* Convert a control id (IDC_ constant) to a dllmode */
168 static enum dllmode
id_to_mode(DWORD id
)
172 case IDC_RAD_BUILTIN
: return BUILTIN
;
173 case IDC_RAD_NATIVE
: return NATIVE
;
174 case IDC_RAD_NATIVE_BUILTIN
: return NATIVE_BUILTIN
;
175 case IDC_RAD_BUILTIN_NATIVE
: return BUILTIN_NATIVE
;
176 case IDC_RAD_DISABLE
: return DISABLE
;
177 default: assert( FALSE
); return 0; /* should not be reached */
181 /* Convert a dllmode to a control id (IDC_ constant) */
182 static DWORD
mode_to_id(enum dllmode mode
)
186 case BUILTIN
: return IDC_RAD_BUILTIN
;
187 case NATIVE
: return IDC_RAD_NATIVE
;
188 case NATIVE_BUILTIN
: return IDC_RAD_NATIVE_BUILTIN
;
189 case BUILTIN_NATIVE
: return IDC_RAD_BUILTIN_NATIVE
;
190 case DISABLE
: return IDC_RAD_DISABLE
;
191 default: assert( FALSE
); return 0; /* should not be reached */
195 /* helper for is_builtin_only */
196 static int compare_dll( const void *ptr1
, const void *ptr2
)
198 const char * const *name1
= ptr1
;
199 const char * const *name2
= ptr2
;
200 return strcmp( *name1
, *name2
);
203 /* check if dll is recommended as builtin only */
204 static inline int is_builtin_only( const char *name
)
206 return bsearch( &name
, builtin_only
, sizeof(builtin_only
)/sizeof(builtin_only
[0]),
207 sizeof(builtin_only
[0]), compare_dll
) != NULL
;
210 static void set_controls_from_selection(HWND dialog
)
212 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
215 static void clear_settings(HWND dialog
)
217 int count
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCOUNT
, 0, 0);
220 WINE_TRACE("count=%d\n", count
);
222 for (i
= 0; i
< count
; i
++)
224 struct dll
*dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, 0, 0);
226 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_DELETESTRING
, 0, 0);
228 HeapFree(GetProcessHeap(), 0, dll
->name
);
229 HeapFree(GetProcessHeap(), 0, dll
);
233 /* check if a given dll is 16-bit */
234 static int is_16bit_dll( const char *dir
, const char *name
)
238 size_t len
= strlen(dir
) + strlen(name
) + 2;
239 char *path
= HeapAlloc( GetProcessHeap(), 0, len
);
243 strcat( path
, name
);
244 res
= readlink( path
, buffer
, sizeof(buffer
) );
245 HeapFree( GetProcessHeap(), 0, path
);
247 if (res
== -1) return 0; /* not a symlink */
248 if (res
< 4 || res
>= sizeof(buffer
)) return 0;
250 if (strchr( buffer
, '/' )) return 0; /* contains a path, not valid */
251 if (strcmp( buffer
+ res
- 3, ".so" )) return 0; /* does not end in .so, not valid */
255 /* load the list of available libraries from a given dir */
256 static void load_library_list_from_dir( HWND dialog
, const char *dir_path
)
260 DIR *dir
= opendir( dir_path
);
264 while ((de
= readdir( dir
)))
266 size_t len
= strlen(de
->d_name
);
267 if (len
> sizeof(name
) || len
<= 7 || strcmp( de
->d_name
+ len
- 7, ".dll.so")) continue;
268 if (is_16bit_dll( dir_path
, de
->d_name
)) continue; /* 16-bit dlls can't be configured */
270 memcpy( name
, de
->d_name
, len
);
272 /* skip dlls that should always be builtin */
273 if (is_builtin_only( name
)) continue;
274 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_ADDSTRING
, 0, (LPARAM
)name
);
279 /* load the list of available libraries */
280 static void load_library_list( HWND dialog
)
284 char item1
[256], item2
[256];
286 while ((path
= wine_dll_enum_load_path( i
++ )))
287 load_library_list_from_dir( dialog
, path
);
289 /* get rid of duplicate entries */
291 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, 0, (LPARAM
)item1
);
293 while (SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_GETLBTEXT
, i
, (LPARAM
)item2
) >= 0)
295 if (!strcmp( item1
, item2
))
297 SendDlgItemMessageA( dialog
, IDC_DLLCOMBO
, CB_DELETESTRING
, i
, 0 );
301 strcpy( item1
, item2
);
307 static void load_library_settings(HWND dialog
)
309 char **overrides
= enumerate_values(config_key
, keypath("DllOverrides"));
313 sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
315 WINE_TRACE("sel=%d\n", sel
);
317 clear_settings(dialog
);
319 if (!overrides
|| *overrides
== NULL
)
321 set_controls_from_selection(dialog
);
322 disable(IDC_DLLS_EDITDLL
);
323 disable(IDC_DLLS_REMOVEDLL
);
324 HeapFree(GetProcessHeap(), 0, overrides
);
328 enable(IDC_DLLS_EDITDLL
);
329 enable(IDC_DLLS_REMOVEDLL
);
331 for (p
= overrides
; *p
!= NULL
; p
++)
338 value
= get_reg_key(config_key
, keypath("DllOverrides"), *p
, NULL
);
340 label
= mode_to_label(string_to_mode(value
));
342 str
= HeapAlloc(GetProcessHeap(), 0, strlen(*p
) + 2 + strlen(label
) + 2);
348 dll
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll
));
350 dll
->mode
= string_to_mode(value
);
352 index
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_ADDSTRING
, (WPARAM
) -1, (LPARAM
) str
);
353 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETITEMDATA
, index
, (LPARAM
) dll
);
355 HeapFree(GetProcessHeap(), 0, str
);
360 HeapFree(GetProcessHeap(), 0, overrides
);
362 /* restore the previous selection, if possible */
363 if (sel
>= count
- 1) sel
= count
- 1;
364 else if (sel
== -1) sel
= 0;
366 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETCURSEL
, sel
, 0);
368 set_controls_from_selection(dialog
);
371 /* Called when the application is initialized (cannot reinit!) */
372 static void init_libsheet(HWND dialog
)
374 /* clear the add dll controls */
375 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_SETTEXT
, 1, (LPARAM
) "");
376 load_library_list( dialog
);
377 disable(IDC_DLLS_ADDDLL
);
380 static void on_add_combo_change(HWND dialog
)
384 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, sizeof(buffer
), (LPARAM
) buffer
);
387 enable(IDC_DLLS_ADDDLL
)
389 disable(IDC_DLLS_ADDDLL
);
392 static void set_dllmode(HWND dialog
, DWORD id
)
399 mode
= id_to_mode(id
);
401 sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
402 if (sel
== -1) return;
404 dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, sel
, 0);
406 str
= mode_to_string(mode
);
407 WINE_TRACE("Setting %s to %s\n", dll
->name
, str
);
409 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
410 set_reg_key(config_key
, keypath("DllOverrides"), dll
->name
, str
);
412 load_library_settings(dialog
); /* ... and refresh */
415 static void on_add_click(HWND dialog
)
417 static const char dotDll
[] = ".dll";
418 char buffer
[1024], *ptr
;
420 ZeroMemory(buffer
, sizeof(buffer
));
422 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_GETTEXT
, sizeof(buffer
), (LPARAM
) buffer
);
423 if (lstrlenA(buffer
) >= sizeof(dotDll
))
425 ptr
= buffer
+ lstrlenA(buffer
) - sizeof(dotDll
) + 1;
426 if (!lstrcmpiA(ptr
, dotDll
))
428 WINE_TRACE("Stripping dll extension\n");
433 /* check if dll is in the builtin-only list */
434 if (!(ptr
= strrchr( buffer
, '\\' )))
437 if (*ptr
== '*') ptr
++;
440 if (is_builtin_only( ptr
))
442 MSGBOXPARAMSA params
;
443 params
.cbSize
= sizeof(params
);
444 params
.hwndOwner
= dialog
;
445 params
.hInstance
= GetModuleHandleA( NULL
);
446 params
.lpszText
= MAKEINTRESOURCEA( IDS_DLL_WARNING
);
447 params
.lpszCaption
= MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION
);
448 params
.dwStyle
= MB_ICONWARNING
| MB_YESNO
;
449 params
.lpszIcon
= NULL
;
450 params
.dwContextHelpId
= 0;
451 params
.lpfnMsgBoxCallback
= NULL
;
452 params
.dwLanguageId
= 0;
453 if (MessageBoxIndirectA( ¶ms
) != IDYES
) return;
456 SendDlgItemMessage(dialog
, IDC_DLLCOMBO
, WM_SETTEXT
, 0, (LPARAM
) "");
457 disable(IDC_DLLS_ADDDLL
);
459 WINE_TRACE("Adding %s as native, builtin", buffer
);
461 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
462 set_reg_key(config_key
, keypath("DllOverrides"), buffer
, "native,builtin");
464 load_library_settings(dialog
);
466 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SELECTSTRING
, (WPARAM
) 0, (LPARAM
) buffer
);
468 set_controls_from_selection(dialog
);
471 static INT_PTR CALLBACK
loadorder_dlgproc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
478 CheckRadioButton(hwndDlg
, IDC_RAD_BUILTIN
, IDC_RAD_DISABLE
, lParam
);
483 if(HIWORD(wParam
) != BN_CLICKED
) break;
484 switch (LOWORD(wParam
))
486 case IDC_RAD_BUILTIN
:
488 case IDC_RAD_BUILTIN_NATIVE
:
489 case IDC_RAD_NATIVE_BUILTIN
:
490 case IDC_RAD_DISABLE
:
491 sel
= LOWORD(wParam
);
494 EndDialog(hwndDlg
, sel
);
497 EndDialog(hwndDlg
, wParam
);
504 static void on_edit_click(HWND hwnd
)
507 int index
= SendDlgItemMessage(hwnd
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
511 /* if no override is selected the edit button should be disabled... */
514 dll
= (struct dll
*) SendDlgItemMessage(hwnd
, IDC_DLLS_LIST
, LB_GETITEMDATA
, index
, 0);
515 id
= mode_to_id(dll
->mode
);
517 ret
= DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER
), hwnd
, loadorder_dlgproc
, id
);
520 set_dllmode(hwnd
, ret
);
523 static void on_remove_click(HWND dialog
)
525 int sel
= SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCURSEL
, 0, 0);
528 if (sel
== LB_ERR
) return;
530 dll
= (struct dll
*) SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETITEMDATA
, sel
, 0);
532 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_DELETESTRING
, sel
, 0);
534 SendMessage(GetParent(dialog
), PSM_CHANGED
, 0, 0);
535 set_reg_key(config_key
, keypath("DllOverrides"), dll
->name
, NULL
);
537 HeapFree(GetProcessHeap(), 0, dll
->name
);
538 HeapFree(GetProcessHeap(), 0, dll
);
540 if (SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_GETCOUNT
, 0, 0) > 0)
541 SendDlgItemMessage(dialog
, IDC_DLLS_LIST
, LB_SETCURSEL
, max(sel
- 1, 0), 0);
544 disable(IDC_DLLS_EDITDLL
);
545 disable(IDC_DLLS_REMOVEDLL
);
548 set_controls_from_selection(dialog
);
552 LibrariesDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
560 set_window_title(hDlg
);
563 switch (((LPNMHDR
)lParam
)->code
) {
565 load_library_settings(hDlg
);
570 switch(HIWORD(wParam
)) {
572 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
573 * add button, rather than the propsheet OK button. But I don't know how to do that!
577 if(LOWORD(wParam
) == IDC_DLLCOMBO
)
579 on_add_combo_change(hDlg
);
584 switch(LOWORD(wParam
)) {
585 case IDC_DLLS_ADDDLL
:
588 case IDC_DLLS_EDITDLL
:
591 case IDC_DLLS_REMOVEDLL
:
592 on_remove_click(hDlg
);
597 if(LOWORD(wParam
) == IDC_DLLCOMBO
)
598 on_add_combo_change(hDlg
);
600 set_controls_from_selection(hDlg
);