winecfg: Fix list of configurable dlls to account for PE files.
[wine.git] / programs / winecfg / libraries.c
blob15c308c77e9a493bb5c42deda4b7fe2fd246874d
1 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #include <commdlg.h>
29 #include <wine/library.h>
30 #include <wine/debug.h>
31 #include <stdio.h>
32 #include <dirent.h>
33 #include <assert.h>
34 #include <stdlib.h>
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
42 #include "winecfg.h"
43 #include "resource.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[] =
50 "advapi32",
51 "capi2032",
52 "dbghelp",
53 "ddraw",
54 "gdi32",
55 "gphoto2.ds",
56 "icmp",
57 "iphlpapi",
58 "kernel32",
59 "l3codeca.acm",
60 "mountmgr.sys",
61 "mswsock",
62 "ntdll",
63 "ntoskrnl.exe",
64 "opengl32",
65 "sane.ds",
66 "secur32",
67 "twain_32",
68 "unicows",
69 "user32",
70 "vdmdbg",
71 "w32skrnl",
72 "winmm",
73 "wintab32",
74 "wnaspi32",
75 "wow32",
76 "ws2_32",
77 "wsock32",
80 enum dllmode
82 BUILTIN_NATIVE,
83 NATIVE_BUILTIN,
84 BUILTIN,
85 NATIVE,
86 DISABLE,
87 UNKNOWN /* Special value indicating an erroneous DLL override mode */
90 struct dll
92 char *name;
93 enum dllmode mode;
96 static const WCHAR emptyW[1];
98 /* Convert a registry string to a dllmode */
99 static enum dllmode string_to_mode(char *in)
101 int i, j, len;
102 char *out;
103 enum dllmode res;
105 len = strlen(in);
106 out = HeapAlloc(GetProcessHeap(), 0, len + 1);
108 /* remove the spaces */
109 for (i = j = 0; i <= len; ++i) {
110 if (in[i] != ' ') {
111 out[j++] = in[i];
115 /* parse the string */
116 res = UNKNOWN;
117 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
118 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
119 if (strcmp(out, "builtin") == 0) res = BUILTIN;
120 if (strcmp(out, "native") == 0) res = NATIVE;
121 if (strcmp(out, "") == 0) res = DISABLE;
123 HeapFree(GetProcessHeap(), 0, out);
124 return res;
127 /* Convert a dllmode to a registry string. */
128 static const char* mode_to_string(enum dllmode mode)
130 switch( mode )
132 case NATIVE: return "native";
133 case BUILTIN: return "builtin";
134 case NATIVE_BUILTIN: return "native,builtin";
135 case BUILTIN_NATIVE: return "builtin,native";
136 case DISABLE: return "";
137 default: return "";
141 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
142 static const char* mode_to_label(enum dllmode mode)
144 static char buffer[256];
145 UINT id = 0;
147 switch( mode )
149 case NATIVE: id = IDS_DLL_NATIVE; break;
150 case BUILTIN: id = IDS_DLL_BUILTIN; break;
151 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
152 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
153 case DISABLE: id = IDS_DLL_DISABLED; break;
154 default: return "??";
156 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
157 return buffer;
160 /* Convert a control id (IDC_ constant) to a dllmode */
161 static enum dllmode id_to_mode(DWORD id)
163 switch( id )
165 case IDC_RAD_BUILTIN: return BUILTIN;
166 case IDC_RAD_NATIVE: return NATIVE;
167 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
168 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
169 case IDC_RAD_DISABLE: return DISABLE;
170 default: assert( FALSE ); return 0; /* should not be reached */
174 /* Convert a dllmode to a control id (IDC_ constant) */
175 static DWORD mode_to_id(enum dllmode mode)
177 switch( mode )
179 case BUILTIN: return IDC_RAD_BUILTIN;
180 case NATIVE: return IDC_RAD_NATIVE;
181 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
182 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
183 case DISABLE: return IDC_RAD_DISABLE;
184 default: return IDC_RAD_BUILTIN_NATIVE;
188 /* helper for is_builtin_only */
189 static int compare_dll( const void *ptr1, const void *ptr2 )
191 const char * const *name1 = ptr1;
192 const char * const *name2 = ptr2;
193 return strcmp( *name1, *name2 );
196 /* check if dll is recommended as builtin only */
197 static inline BOOL is_builtin_only( const char *name )
199 const char *ext = strrchr( name, '.' );
201 if (ext)
203 if (!strcmp( ext, ".vxd" ) ||
204 !strcmp( ext, ".drv" ) ||
205 !strcmp( ext, ".tlb" ))
206 return TRUE;
208 if (!strncmp( name, "wine", 4 )) return TRUE;
209 return bsearch( &name, builtin_only, ARRAY_SIZE(builtin_only),
210 sizeof(builtin_only[0]), compare_dll ) != NULL;
213 /* check if dll should be offered in the drop-down list */
214 static BOOL show_dll_in_list( const char *name )
216 const char *ext = strrchr( name, '.' );
218 if (ext)
220 /* skip 16-bit dlls */
221 if (strlen(ext) > 2 && !strcmp( ext + strlen(ext) - 2, "16" )) return FALSE;
222 /* skip exes */
223 if (!strcmp( ext, ".exe" )) return FALSE;
225 /* skip api set placeholders */
226 if (!strncmp( name, "api-ms-", 7 ) || !strncmp( name, "ext-ms-", 7 )) return FALSE;
227 /* skip dlls that should always be builtin */
228 return !is_builtin_only( name );
231 static void set_controls_from_selection(HWND dialog)
233 /* FIXME: display/update some information about the selected dll (purpose, recommended load order) maybe? */
236 static void clear_settings(HWND dialog)
238 int count = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
239 int i;
241 WINE_TRACE("count=%d\n", count);
243 for (i = 0; i < count; i++)
245 struct dll *dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
247 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
248 HeapFree(GetProcessHeap(), 0, dll->name);
249 HeapFree(GetProcessHeap(), 0, dll);
253 /* load the list of available libraries from a given dir */
254 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
256 static const char * const ext[] = { ".dll", ".dll.so", ".so", "" };
257 char *buffer = NULL, name[256];
258 unsigned int i;
259 struct dirent *de;
260 DIR *dir = opendir( dir_path );
262 if (!dir) return;
264 if (check_subdirs)
265 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
267 while ((de = readdir( dir )))
269 size_t len = strlen(de->d_name);
270 if (len > sizeof(name)) continue;
271 if (check_subdirs)
273 struct stat st;
275 if (!strcmp( de->d_name, "." )) continue;
276 if (!strcmp( de->d_name, ".." )) continue;
277 if (!show_dll_in_list( de->d_name )) continue;
278 for (i = 0; i < ARRAY_SIZE( ext ); i++)
280 sprintf( buffer, "%s/%s/%s%s", dir_path, de->d_name, de->d_name, ext[i] );
281 if (!stat( buffer, &st ))
283 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
284 break;
288 else
290 for (i = 0; i < ARRAY_SIZE( ext ); i++)
292 if (!ext[i][0]) continue;
293 if (len > strlen(ext[i]) && !strcmp( de->d_name + len - strlen(ext[i]), ext[i]))
295 len -= strlen( ext[i] );
296 memcpy( name, de->d_name, len );
297 name[len] = 0;
298 if (!show_dll_in_list( name )) continue;
299 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
304 closedir( dir );
305 HeapFree( GetProcessHeap(), 0, buffer );
308 /* load the list of available libraries */
309 static void load_library_list( HWND dialog )
311 unsigned int i = 0;
312 const char *path, *build_dir = wine_get_build_dir();
313 char item1[256], item2[256];
314 HCURSOR old_cursor = SetCursor( LoadCursorW(0, (LPWSTR)IDC_WAIT) );
316 if (build_dir)
318 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
319 strcpy( dir, build_dir );
320 strcat( dir, "/dlls" );
321 load_library_list_from_dir( dialog, dir, TRUE );
322 HeapFree( GetProcessHeap(), 0, dir );
325 while ((path = wine_dll_enum_load_path( i++ )))
326 load_library_list_from_dir( dialog, path, FALSE );
328 /* get rid of duplicate entries */
330 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
331 i = 1;
332 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
334 if (!strcmp( item1, item2 ))
336 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
338 else
340 strcpy( item1, item2 );
341 i++;
344 SetCursor( old_cursor );
347 static void load_library_settings(HWND dialog)
349 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
350 char **p;
351 int sel, count = 0;
353 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
355 WINE_TRACE("sel=%d\n", sel);
357 clear_settings(dialog);
359 if (!overrides || *overrides == NULL)
361 set_controls_from_selection(dialog);
362 disable(IDC_DLLS_EDITDLL);
363 disable(IDC_DLLS_REMOVEDLL);
364 HeapFree(GetProcessHeap(), 0, overrides);
365 return;
368 enable(IDC_DLLS_EDITDLL);
369 enable(IDC_DLLS_REMOVEDLL);
371 for (p = overrides; *p != NULL; p++)
373 int index;
374 char *str, *value;
375 const char *label;
376 struct dll *dll;
378 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
380 label = mode_to_label(string_to_mode(value));
382 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
383 strcpy(str, *p);
384 strcat(str, " (");
385 strcat(str, label);
386 strcat(str, ")");
388 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
389 dll->name = *p;
390 dll->mode = string_to_mode(value);
392 index = SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
393 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
395 HeapFree(GetProcessHeap(), 0, str);
397 count++;
400 HeapFree(GetProcessHeap(), 0, overrides);
402 /* restore the previous selection, if possible */
403 if (sel >= count - 1) sel = count - 1;
404 else if (sel == -1) sel = 0;
406 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
408 set_controls_from_selection(dialog);
411 /* Called when the application is initialized (cannot reinit!) */
412 static void init_libsheet(HWND dialog)
414 /* clear the add dll controls */
415 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM)emptyW);
416 load_library_list( dialog );
417 disable(IDC_DLLS_ADDDLL);
420 static void on_add_combo_change(HWND dialog)
422 WCHAR buffer[1024];
423 int sel, len;
425 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_GETTEXT, ARRAY_SIZE(buffer), (LPARAM)buffer);
426 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
427 sel=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
428 len=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
430 if (buffer[0] || len>0)
432 enable(IDC_DLLS_ADDDLL)
433 SendMessageW(GetParent(dialog), DM_SETDEFID, IDC_DLLS_ADDDLL, 0);
435 else
437 disable(IDC_DLLS_ADDDLL);
438 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
442 static void set_dllmode(HWND dialog, DWORD id)
444 enum dllmode mode;
445 struct dll *dll;
446 int sel;
447 const char *str;
449 mode = id_to_mode(id);
451 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
452 if (sel == -1) return;
454 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
456 str = mode_to_string(mode);
457 WINE_TRACE("Setting %s to %s\n", dll->name, str);
459 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
460 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
462 load_library_settings(dialog); /* ... and refresh */
465 static void on_add_click(HWND dialog)
467 static const char dotDll[] = ".dll";
468 char buffer[1024], *ptr;
470 ZeroMemory(buffer, sizeof(buffer));
472 SendDlgItemMessageA(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
473 if (lstrlenA(buffer) >= sizeof(dotDll))
475 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
476 if (!lstrcmpiA(ptr, dotDll))
478 WINE_TRACE("Stripping dll extension\n");
479 *ptr = '\0';
483 /* check if dll is in the builtin-only list */
484 if (!(ptr = strrchr( buffer, '\\' )))
486 ptr = buffer;
487 if (*ptr == '*') ptr++;
489 else ptr++;
490 if (is_builtin_only( ptr ))
492 MSGBOXPARAMSA params;
493 params.cbSize = sizeof(params);
494 params.hwndOwner = dialog;
495 params.hInstance = GetModuleHandleA( NULL );
496 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
497 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
498 params.dwStyle = MB_ICONWARNING | MB_YESNO;
499 params.lpszIcon = NULL;
500 params.dwContextHelpId = 0;
501 params.lpfnMsgBoxCallback = NULL;
502 params.dwLanguageId = 0;
503 if (MessageBoxIndirectA( &params ) != IDYES) return;
506 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM)emptyW);
507 disable(IDC_DLLS_ADDDLL);
508 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
510 WINE_TRACE("Adding %s as native, builtin\n", buffer);
512 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
513 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
515 load_library_settings(dialog);
517 SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
519 set_controls_from_selection(dialog);
522 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
524 static WORD sel;
526 switch(uMsg)
528 case WM_INITDIALOG:
529 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
530 sel = lParam;
531 return TRUE;
533 case WM_COMMAND:
534 if(HIWORD(wParam) != BN_CLICKED) break;
535 switch (LOWORD(wParam))
537 case IDC_RAD_BUILTIN:
538 case IDC_RAD_NATIVE:
539 case IDC_RAD_BUILTIN_NATIVE:
540 case IDC_RAD_NATIVE_BUILTIN:
541 case IDC_RAD_DISABLE:
542 sel = LOWORD(wParam);
543 return TRUE;
544 case IDOK:
545 EndDialog(hwndDlg, sel);
546 return TRUE;
547 case IDCANCEL:
548 EndDialog(hwndDlg, wParam);
549 return TRUE;
552 return FALSE;
555 static void on_edit_click(HWND hwnd)
557 INT_PTR ret;
558 int index = SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
559 struct dll *dll;
560 DWORD id;
562 /* if no override is selected the edit button should be disabled... */
563 assert(index != -1);
565 dll = (struct dll *) SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
566 id = mode_to_id(dll->mode);
568 ret = DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
570 if(ret != IDCANCEL)
571 set_dllmode(hwnd, ret);
574 static void on_remove_click(HWND dialog)
576 int sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
577 struct dll *dll;
579 if (sel == LB_ERR) return;
581 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
583 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
585 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
586 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
588 HeapFree(GetProcessHeap(), 0, dll->name);
589 HeapFree(GetProcessHeap(), 0, dll);
591 if (SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
592 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
593 else
595 disable(IDC_DLLS_EDITDLL);
596 disable(IDC_DLLS_REMOVEDLL);
599 set_controls_from_selection(dialog);
602 INT_PTR CALLBACK
603 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
605 switch (uMsg)
607 case WM_INITDIALOG:
608 init_libsheet(hDlg);
609 break;
610 case WM_SHOWWINDOW:
611 set_window_title(hDlg);
612 break;
613 case WM_NOTIFY:
614 switch (((LPNMHDR)lParam)->code) {
615 case PSN_SETACTIVE:
616 load_library_settings(hDlg);
617 break;
619 break;
620 case WM_COMMAND:
621 switch(HIWORD(wParam)) {
622 case CBN_EDITCHANGE:
623 if (LOWORD(wParam) == IDC_DLLCOMBO)
624 on_add_combo_change(hDlg);
625 break;
626 case CBN_SETFOCUS:
627 if (LOWORD(wParam) == IDC_DLLCOMBO)
628 on_add_combo_change(hDlg);
629 break;
630 case CBN_KILLFOCUS:
631 if (LOWORD(wParam) == IDC_DLLCOMBO)
632 SendMessageW(GetParent(hDlg), DM_SETDEFID, IDOK, 0);
633 break;
634 case BN_CLICKED:
635 switch(LOWORD(wParam)) {
636 case IDC_DLLS_ADDDLL:
637 on_add_click(hDlg);
638 break;
639 case IDC_DLLS_EDITDLL:
640 on_edit_click(hDlg);
641 break;
642 case IDC_DLLS_REMOVEDLL:
643 on_remove_click(hDlg);
644 break;
646 break;
647 case LBN_SELCHANGE:
648 if(LOWORD(wParam) == IDC_DLLCOMBO)
649 on_add_combo_change(hDlg);
650 else
651 set_controls_from_selection(hDlg);
652 break;
654 break;
657 return 0;