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 erroneous 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)
413 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
414 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
415 sel=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
416 len=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
418 if (strlen(buffer)>0 || len>0)
419 enable(IDC_DLLS_ADDDLL)
421 disable(IDC_DLLS_ADDDLL);
424 static void set_dllmode(HWND dialog, DWORD id)
431 mode = id_to_mode(id);
433 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
434 if (sel == -1) return;
436 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
438 str = mode_to_string(mode);
439 WINE_TRACE("Setting %s to %s\n", dll->name, str);
441 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
442 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
444 load_library_settings(dialog); /* ... and refresh */
447 static void on_add_click(HWND dialog)
449 static const char dotDll[] = ".dll";
450 char buffer[1024], *ptr;
452 ZeroMemory(buffer, sizeof(buffer));
454 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
455 if (lstrlenA(buffer) >= sizeof(dotDll))
457 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
458 if (!lstrcmpiA(ptr, dotDll))
460 WINE_TRACE("Stripping dll extension\n");
465 /* check if dll is in the builtin-only list */
466 if (!(ptr = strrchr( buffer, '\\' )))
469 if (*ptr == '*') ptr++;
472 if (is_builtin_only( ptr ))
474 MSGBOXPARAMSA params;
475 params.cbSize = sizeof(params);
476 params.hwndOwner = dialog;
477 params.hInstance = GetModuleHandleA( NULL );
478 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
479 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
480 params.dwStyle = MB_ICONWARNING | MB_YESNO;
481 params.lpszIcon = NULL;
482 params.dwContextHelpId = 0;
483 params.lpfnMsgBoxCallback = NULL;
484 params.dwLanguageId = 0;
485 if (MessageBoxIndirectA( ¶ms ) != IDYES) return;
488 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
489 disable(IDC_DLLS_ADDDLL);
491 WINE_TRACE("Adding %s as native, builtin\n", buffer);
493 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
494 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
496 load_library_settings(dialog);
498 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, (WPARAM) 0, (LPARAM) buffer);
500 set_controls_from_selection(dialog);
503 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
510 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
515 if(HIWORD(wParam) != BN_CLICKED) break;
516 switch (LOWORD(wParam))
518 case IDC_RAD_BUILTIN:
520 case IDC_RAD_BUILTIN_NATIVE:
521 case IDC_RAD_NATIVE_BUILTIN:
522 case IDC_RAD_DISABLE:
523 sel = LOWORD(wParam);
526 EndDialog(hwndDlg, sel);
529 EndDialog(hwndDlg, wParam);
536 static void on_edit_click(HWND hwnd)
539 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
543 /* if no override is selected the edit button should be disabled... */
546 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
547 id = mode_to_id(dll->mode);
549 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
552 set_dllmode(hwnd, ret);
555 static void on_remove_click(HWND dialog)
557 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
560 if (sel == LB_ERR) return;
562 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
564 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
566 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
567 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
569 HeapFree(GetProcessHeap(), 0, dll->name);
570 HeapFree(GetProcessHeap(), 0, dll);
572 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
573 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
576 disable(IDC_DLLS_EDITDLL);
577 disable(IDC_DLLS_REMOVEDLL);
580 set_controls_from_selection(dialog);
584 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
592 set_window_title(hDlg);
595 switch (((LPNMHDR)lParam)->code) {
597 load_library_settings(hDlg);
602 switch(HIWORD(wParam)) {
604 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
605 * add button, rather than the propsheet OK button. But I don't know how to do that!
609 if(LOWORD(wParam) == IDC_DLLCOMBO)
611 on_add_combo_change(hDlg);
616 switch(LOWORD(wParam)) {
617 case IDC_DLLS_ADDDLL:
620 case IDC_DLLS_EDITDLL:
623 case IDC_DLLS_REMOVEDLL:
624 on_remove_click(hDlg);
629 if(LOWORD(wParam) == IDC_DLLCOMBO)
630 on_add_combo_change(hDlg);
632 set_controls_from_selection(hDlg);