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
25 #define NONAMELESSUNION
26 #define WIN32_LEAN_AND_MEAN
29 #include <wine/library.h>
30 #include <wine/debug.h>
35 #ifdef HAVE_SYS_STAT_H
45 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
47 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
48 static const char * const builtin_only[] =
97 UNKNOWN /* Special value indicating an erronous DLL override mode */
106 /* Convert a registry string to a dllmode */
107 static enum dllmode string_to_mode(char *in)
114 out = HeapAlloc(GetProcessHeap(), 0, len);
116 /* remove the spaces */
117 for (i = j = 0; i <= len; ++i) {
123 /* parse the string */
125 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
126 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
127 if (strcmp(out, "builtin") == 0) res = BUILTIN;
128 if (strcmp(out, "native") == 0) res = NATIVE;
129 if (strcmp(out, "") == 0) res = DISABLE;
131 HeapFree(GetProcessHeap(), 0, out);
135 /* Convert a dllmode to a registry string. */
136 static const char* mode_to_string(enum dllmode mode)
140 case NATIVE: return "native";
141 case BUILTIN: return "builtin";
142 case NATIVE_BUILTIN: return "native,builtin";
143 case BUILTIN_NATIVE: return "builtin,native";
144 case DISABLE: return "";
145 default: assert(FALSE); return "";
149 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
150 static const char* mode_to_label(enum dllmode mode)
152 static char buffer[256];
157 case NATIVE: id = IDS_DLL_NATIVE; break;
158 case BUILTIN: id = IDS_DLL_BUILTIN; break;
159 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
160 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
161 case DISABLE: id = IDS_DLL_DISABLED; break;
162 default: assert(FALSE);
164 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
168 /* Convert a control id (IDC_ constant) to a dllmode */
169 static enum dllmode id_to_mode(DWORD id)
173 case IDC_RAD_BUILTIN: return BUILTIN;
174 case IDC_RAD_NATIVE: return NATIVE;
175 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
176 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
177 case IDC_RAD_DISABLE: return DISABLE;
178 default: assert( FALSE ); return 0; /* should not be reached */
182 /* Convert a dllmode to a control id (IDC_ constant) */
183 static DWORD mode_to_id(enum dllmode mode)
187 case BUILTIN: return IDC_RAD_BUILTIN;
188 case NATIVE: return IDC_RAD_NATIVE;
189 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
190 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
191 case DISABLE: return IDC_RAD_DISABLE;
192 default: assert( FALSE ); return 0; /* should not be reached */
196 /* helper for is_builtin_only */
197 static int compare_dll( const void *ptr1, const void *ptr2 )
199 const char * const *name1 = ptr1;
200 const char * const *name2 = ptr2;
201 return strcmp( *name1, *name2 );
204 /* check if dll is recommended as builtin only */
205 static inline int is_builtin_only( const char *name )
207 return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
208 sizeof(builtin_only[0]), compare_dll ) != NULL;
211 static void set_controls_from_selection(HWND dialog)
213 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
216 static void clear_settings(HWND dialog)
218 int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
221 WINE_TRACE("count=%d\n", count);
223 for (i = 0; i < count; i++)
225 struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
227 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
229 HeapFree(GetProcessHeap(), 0, dll->name);
230 HeapFree(GetProcessHeap(), 0, dll);
234 /* check if a given dll is 16-bit */
235 static int is_16bit_dll( const char *dir, const char *name )
239 size_t len = strlen(dir) + strlen(name) + 2;
240 char *path = HeapAlloc( GetProcessHeap(), 0, len );
244 strcat( path, name );
245 res = readlink( path, buffer, sizeof(buffer) );
246 HeapFree( GetProcessHeap(), 0, path );
248 if (res == -1) return 0; /* not a symlink */
249 if (res < 4 || res >= sizeof(buffer)) return 0;
251 if (strchr( buffer, '/' )) return 0; /* contains a path, not valid */
252 if (strcmp( buffer + res - 3, ".so" )) return 0; /* does not end in .so, not valid */
256 /* load the list of available libraries from a given dir */
257 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
259 char *buffer = NULL, name[256];
261 DIR *dir = opendir( dir_path );
266 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
268 while ((de = readdir( dir )))
270 size_t len = strlen(de->d_name);
271 if (len > sizeof(name)) continue;
272 if (len > 7 && !strcmp( de->d_name + len - 7, ".dll.so"))
274 if (is_16bit_dll( dir_path, de->d_name )) continue; /* 16-bit dlls can't be configured */
276 memcpy( name, de->d_name, len );
278 /* skip dlls that should always be builtin */
279 if (is_builtin_only( name )) continue;
280 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
282 else if (check_subdirs)
285 if (is_builtin_only( de->d_name )) continue;
286 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
287 if (!stat( buffer, &st ))
288 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
292 HeapFree( GetProcessHeap(), 0, buffer );
295 /* load the list of available libraries */
296 static void load_library_list( HWND dialog )
299 const char *path, *build_dir = wine_get_build_dir();
300 char item1[256], item2[256];
301 HCURSOR old_cursor = SetCursor( LoadCursor(0, IDC_WAIT) );
305 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
306 strcpy( dir, build_dir );
307 strcat( dir, "/dlls" );
308 load_library_list_from_dir( dialog, dir, TRUE );
309 HeapFree( GetProcessHeap(), 0, dir );
312 while ((path = wine_dll_enum_load_path( i++ )))
313 load_library_list_from_dir( dialog, path, FALSE );
315 /* get rid of duplicate entries */
317 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
319 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
321 if (!strcmp( item1, item2 ))
323 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
327 strcpy( item1, item2 );
331 SetCursor( old_cursor );
334 static void load_library_settings(HWND dialog)
336 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
340 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
342 WINE_TRACE("sel=%d\n", sel);
344 clear_settings(dialog);
346 if (!overrides || *overrides == NULL)
348 set_controls_from_selection(dialog);
349 disable(IDC_DLLS_EDITDLL);
350 disable(IDC_DLLS_REMOVEDLL);
351 HeapFree(GetProcessHeap(), 0, overrides);
355 enable(IDC_DLLS_EDITDLL);
356 enable(IDC_DLLS_REMOVEDLL);
358 for (p = overrides; *p != NULL; p++)
365 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
367 label = mode_to_label(string_to_mode(value));
369 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
375 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
377 dll->mode = string_to_mode(value);
379 index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
380 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
382 HeapFree(GetProcessHeap(), 0, str);
387 HeapFree(GetProcessHeap(), 0, overrides);
389 /* restore the previous selection, if possible */
390 if (sel >= count - 1) sel = count - 1;
391 else if (sel == -1) sel = 0;
393 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
395 set_controls_from_selection(dialog);
398 /* Called when the application is initialized (cannot reinit!) */
399 static void init_libsheet(HWND dialog)
401 /* clear the add dll controls */
402 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
403 load_library_list( dialog );
404 disable(IDC_DLLS_ADDDLL);
407 static void on_add_combo_change(HWND dialog)
411 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
414 enable(IDC_DLLS_ADDDLL)
416 disable(IDC_DLLS_ADDDLL);
419 static void set_dllmode(HWND dialog, DWORD id)
426 mode = id_to_mode(id);
428 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
429 if (sel == -1) return;
431 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
433 str = mode_to_string(mode);
434 WINE_TRACE("Setting %s to %s\n", dll->name, str);
436 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
437 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
439 load_library_settings(dialog); /* ... and refresh */
442 static void on_add_click(HWND dialog)
444 static const char dotDll[] = ".dll";
445 char buffer[1024], *ptr;
447 ZeroMemory(buffer, sizeof(buffer));
449 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
450 if (lstrlenA(buffer) >= sizeof(dotDll))
452 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
453 if (!lstrcmpiA(ptr, dotDll))
455 WINE_TRACE("Stripping dll extension\n");
460 /* check if dll is in the builtin-only list */
461 if (!(ptr = strrchr( buffer, '\\' )))
464 if (*ptr == '*') ptr++;
467 if (is_builtin_only( ptr ))
469 MSGBOXPARAMSA params;
470 params.cbSize = sizeof(params);
471 params.hwndOwner = dialog;
472 params.hInstance = GetModuleHandleA( NULL );
473 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
474 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
475 params.dwStyle = MB_ICONWARNING | MB_YESNO;
476 params.lpszIcon = NULL;
477 params.dwContextHelpId = 0;
478 params.lpfnMsgBoxCallback = NULL;
479 params.dwLanguageId = 0;
480 if (MessageBoxIndirectA( ¶ms ) != IDYES) return;
483 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
484 disable(IDC_DLLS_ADDDLL);
486 WINE_TRACE("Adding %s as native, builtin\n", buffer);
488 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
489 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
491 load_library_settings(dialog);
493 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, (WPARAM) 0, (LPARAM) buffer);
495 set_controls_from_selection(dialog);
498 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
505 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
510 if(HIWORD(wParam) != BN_CLICKED) break;
511 switch (LOWORD(wParam))
513 case IDC_RAD_BUILTIN:
515 case IDC_RAD_BUILTIN_NATIVE:
516 case IDC_RAD_NATIVE_BUILTIN:
517 case IDC_RAD_DISABLE:
518 sel = LOWORD(wParam);
521 EndDialog(hwndDlg, sel);
524 EndDialog(hwndDlg, wParam);
531 static void on_edit_click(HWND hwnd)
534 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
538 /* if no override is selected the edit button should be disabled... */
541 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
542 id = mode_to_id(dll->mode);
544 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
547 set_dllmode(hwnd, ret);
550 static void on_remove_click(HWND dialog)
552 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
555 if (sel == LB_ERR) return;
557 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
559 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
561 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
562 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
564 HeapFree(GetProcessHeap(), 0, dll->name);
565 HeapFree(GetProcessHeap(), 0, dll);
567 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
568 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
571 disable(IDC_DLLS_EDITDLL);
572 disable(IDC_DLLS_REMOVEDLL);
575 set_controls_from_selection(dialog);
579 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
587 set_window_title(hDlg);
590 switch (((LPNMHDR)lParam)->code) {
592 load_library_settings(hDlg);
597 switch(HIWORD(wParam)) {
599 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
600 * add button, rather than the propsheet OK button. But I don't know how to do that!
604 if(LOWORD(wParam) == IDC_DLLCOMBO)
606 on_add_combo_change(hDlg);
611 switch(LOWORD(wParam)) {
612 case IDC_DLLS_ADDDLL:
615 case IDC_DLLS_EDITDLL:
618 case IDC_DLLS_REMOVEDLL:
619 on_remove_click(hDlg);
624 if(LOWORD(wParam) == IDC_DLLCOMBO)
625 on_add_combo_change(hDlg);
627 set_controls_from_selection(hDlg);