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[] =
100 UNKNOWN /* Special value indicating an erroneous DLL override mode */
109 /* Convert a registry string to a dllmode */
110 static enum dllmode string_to_mode(char *in)
117 out = HeapAlloc(GetProcessHeap(), 0, len);
119 /* remove the spaces */
120 for (i = j = 0; i <= len; ++i) {
126 /* parse the string */
128 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
129 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
130 if (strcmp(out, "builtin") == 0) res = BUILTIN;
131 if (strcmp(out, "native") == 0) res = NATIVE;
132 if (strcmp(out, "") == 0) res = DISABLE;
134 HeapFree(GetProcessHeap(), 0, out);
138 /* Convert a dllmode to a registry string. */
139 static const char* mode_to_string(enum dllmode mode)
143 case NATIVE: return "native";
144 case BUILTIN: return "builtin";
145 case NATIVE_BUILTIN: return "native,builtin";
146 case BUILTIN_NATIVE: return "builtin,native";
147 case DISABLE: return "";
148 default: assert(FALSE); return "";
152 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
153 static const char* mode_to_label(enum dllmode mode)
155 static char buffer[256];
160 case NATIVE: id = IDS_DLL_NATIVE; break;
161 case BUILTIN: id = IDS_DLL_BUILTIN; break;
162 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
163 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
164 case DISABLE: id = IDS_DLL_DISABLED; break;
165 default: assert(FALSE);
167 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
171 /* Convert a control id (IDC_ constant) to a dllmode */
172 static enum dllmode id_to_mode(DWORD id)
176 case IDC_RAD_BUILTIN: return BUILTIN;
177 case IDC_RAD_NATIVE: return NATIVE;
178 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
179 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
180 case IDC_RAD_DISABLE: return DISABLE;
181 default: assert( FALSE ); return 0; /* should not be reached */
185 /* Convert a dllmode to a control id (IDC_ constant) */
186 static DWORD mode_to_id(enum dllmode mode)
190 case BUILTIN: return IDC_RAD_BUILTIN;
191 case NATIVE: return IDC_RAD_NATIVE;
192 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
193 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
194 case DISABLE: return IDC_RAD_DISABLE;
195 default: assert( FALSE ); return 0; /* should not be reached */
199 /* helper for is_builtin_only */
200 static int compare_dll( const void *ptr1, const void *ptr2 )
202 const char * const *name1 = ptr1;
203 const char * const *name2 = ptr2;
204 return strcmp( *name1, *name2 );
207 /* check if dll is recommended as builtin only */
208 static inline int is_builtin_only( const char *name )
210 const char *ext = strrchr( name, '.' );
214 if (!strcmp( ext, ".vxd" ) ||
215 !strcmp( ext, ".drv" ) ||
216 !strcmp( ext, ".tlb" ))
219 return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
220 sizeof(builtin_only[0]), compare_dll ) != NULL;
223 /* check if dll should be offered in the drop-down list */
224 static int show_dll_in_list( const char *name )
226 const char *ext = strrchr( name, '.' );
230 /* skip 16-bit dlls */
231 if (strlen(ext) > 2 && !strcmp( ext + strlen(ext) - 2, "16" )) return FALSE;
233 if (!strcmp( ext, ".exe" )) return FALSE;
235 /* skip dlls that should always be builtin */
236 return !is_builtin_only( name );
239 static void set_controls_from_selection(HWND dialog)
241 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
244 static void clear_settings(HWND dialog)
246 int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
249 WINE_TRACE("count=%d\n", count);
251 for (i = 0; i < count; i++)
253 struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
255 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
257 HeapFree(GetProcessHeap(), 0, dll->name);
258 HeapFree(GetProcessHeap(), 0, dll);
262 /* load the list of available libraries from a given dir */
263 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
265 char *buffer = NULL, name[256];
267 DIR *dir = opendir( dir_path );
272 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
274 while ((de = readdir( dir )))
276 size_t len = strlen(de->d_name);
277 if (len > sizeof(name)) continue;
278 if (len > 3 && !strcmp( de->d_name + len - 3, ".so"))
281 if (len > 4 && !strcmp( de->d_name + len - 4, ".dll.so")) len -= 4;
282 memcpy( name, de->d_name, len );
284 if (!show_dll_in_list( name )) continue;
285 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
287 else if (check_subdirs)
290 if (!show_dll_in_list( de->d_name )) continue;
291 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
292 if (!stat( buffer, &st ))
294 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
297 sprintf( buffer, "%s/%s/%s.so", dir_path, de->d_name, de->d_name );
298 if (!stat( buffer, &st ))
300 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
306 HeapFree( GetProcessHeap(), 0, buffer );
309 /* load the list of available libraries */
310 static void load_library_list( HWND dialog )
313 const char *path, *build_dir = wine_get_build_dir();
314 char item1[256], item2[256];
315 HCURSOR old_cursor = SetCursor( LoadCursor(0, IDC_WAIT) );
319 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
320 strcpy( dir, build_dir );
321 strcat( dir, "/dlls" );
322 load_library_list_from_dir( dialog, dir, TRUE );
323 HeapFree( GetProcessHeap(), 0, dir );
326 while ((path = wine_dll_enum_load_path( i++ )))
327 load_library_list_from_dir( dialog, path, FALSE );
329 /* get rid of duplicate entries */
331 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
333 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
335 if (!strcmp( item1, item2 ))
337 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
341 strcpy( item1, item2 );
345 SetCursor( old_cursor );
348 static void load_library_settings(HWND dialog)
350 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
354 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
356 WINE_TRACE("sel=%d\n", sel);
358 clear_settings(dialog);
360 if (!overrides || *overrides == NULL)
362 set_controls_from_selection(dialog);
363 disable(IDC_DLLS_EDITDLL);
364 disable(IDC_DLLS_REMOVEDLL);
365 HeapFree(GetProcessHeap(), 0, overrides);
369 enable(IDC_DLLS_EDITDLL);
370 enable(IDC_DLLS_REMOVEDLL);
372 for (p = overrides; *p != NULL; p++)
379 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
381 label = mode_to_label(string_to_mode(value));
383 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
389 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
391 dll->mode = string_to_mode(value);
393 index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
394 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
396 HeapFree(GetProcessHeap(), 0, str);
401 HeapFree(GetProcessHeap(), 0, overrides);
403 /* restore the previous selection, if possible */
404 if (sel >= count - 1) sel = count - 1;
405 else if (sel == -1) sel = 0;
407 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
409 set_controls_from_selection(dialog);
412 /* Called when the application is initialized (cannot reinit!) */
413 static void init_libsheet(HWND dialog)
415 /* clear the add dll controls */
416 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
417 load_library_list( dialog );
418 disable(IDC_DLLS_ADDDLL);
421 static void on_add_combo_change(HWND dialog)
426 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
427 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
428 sel=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
429 len=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
431 if (strlen(buffer)>0 || len>0)
432 enable(IDC_DLLS_ADDDLL)
434 disable(IDC_DLLS_ADDDLL);
437 static void set_dllmode(HWND dialog, DWORD id)
444 mode = id_to_mode(id);
446 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
447 if (sel == -1) return;
449 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
451 str = mode_to_string(mode);
452 WINE_TRACE("Setting %s to %s\n", dll->name, str);
454 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
455 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
457 load_library_settings(dialog); /* ... and refresh */
460 static void on_add_click(HWND dialog)
462 static const char dotDll[] = ".dll";
463 char buffer[1024], *ptr;
465 ZeroMemory(buffer, sizeof(buffer));
467 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
468 if (lstrlenA(buffer) >= sizeof(dotDll))
470 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
471 if (!lstrcmpiA(ptr, dotDll))
473 WINE_TRACE("Stripping dll extension\n");
478 /* check if dll is in the builtin-only list */
479 if (!(ptr = strrchr( buffer, '\\' )))
482 if (*ptr == '*') ptr++;
485 if (is_builtin_only( ptr ))
487 MSGBOXPARAMSA params;
488 params.cbSize = sizeof(params);
489 params.hwndOwner = dialog;
490 params.hInstance = GetModuleHandleA( NULL );
491 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
492 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
493 params.dwStyle = MB_ICONWARNING | MB_YESNO;
494 params.lpszIcon = NULL;
495 params.dwContextHelpId = 0;
496 params.lpfnMsgBoxCallback = NULL;
497 params.dwLanguageId = 0;
498 if (MessageBoxIndirectA( ¶ms ) != IDYES) return;
501 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
502 disable(IDC_DLLS_ADDDLL);
504 WINE_TRACE("Adding %s as native, builtin\n", buffer);
506 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
507 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
509 load_library_settings(dialog);
511 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
513 set_controls_from_selection(dialog);
516 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
523 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
528 if(HIWORD(wParam) != BN_CLICKED) break;
529 switch (LOWORD(wParam))
531 case IDC_RAD_BUILTIN:
533 case IDC_RAD_BUILTIN_NATIVE:
534 case IDC_RAD_NATIVE_BUILTIN:
535 case IDC_RAD_DISABLE:
536 sel = LOWORD(wParam);
539 EndDialog(hwndDlg, sel);
542 EndDialog(hwndDlg, wParam);
549 static void on_edit_click(HWND hwnd)
552 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
556 /* if no override is selected the edit button should be disabled... */
559 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
560 id = mode_to_id(dll->mode);
562 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
565 set_dllmode(hwnd, ret);
568 static void on_remove_click(HWND dialog)
570 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
573 if (sel == LB_ERR) return;
575 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
577 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
579 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
580 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
582 HeapFree(GetProcessHeap(), 0, dll->name);
583 HeapFree(GetProcessHeap(), 0, dll);
585 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
586 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
589 disable(IDC_DLLS_EDITDLL);
590 disable(IDC_DLLS_REMOVEDLL);
593 set_controls_from_selection(dialog);
597 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
605 set_window_title(hDlg);
608 switch (((LPNMHDR)lParam)->code) {
610 load_library_settings(hDlg);
615 switch(HIWORD(wParam)) {
617 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
618 * add button, rather than the propsheet OK button. But I don't know how to do that!
622 if(LOWORD(wParam) == IDC_DLLCOMBO)
624 on_add_combo_change(hDlg);
629 switch(LOWORD(wParam)) {
630 case IDC_DLLS_ADDDLL:
633 case IDC_DLLS_EDITDLL:
636 case IDC_DLLS_REMOVEDLL:
637 on_remove_click(hDlg);
642 if(LOWORD(wParam) == IDC_DLLCOMBO)
643 on_add_combo_change(hDlg);
645 set_controls_from_selection(hDlg);