push 8724397e7ede0f147b6e05994a72142d44d4fecc
[wine/hacks.git] / programs / winecfg / libraries.c
blob7c13faddbe0b041d6f80c60da167b2aeba11ff89
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 NONAMELESSUNION
27 #define WIN32_LEAN_AND_MEAN
28 #include <windows.h>
29 #include <commdlg.h>
30 #include <wine/library.h>
31 #include <wine/debug.h>
32 #include <stdio.h>
33 #include <dirent.h>
34 #include <assert.h>
35 #include <stdlib.h>
36 #ifdef HAVE_SYS_STAT_H
37 #include <sys/stat.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
43 #include "winecfg.h"
44 #include "resource.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[] =
51 "advapi32",
52 "capi2032",
53 "dbghelp",
54 "ddraw",
55 "gdi32",
56 "glu32",
57 "icmp",
58 "gphoto2.ds",
59 "iphlpapi",
60 "kernel32",
61 "mountmgr.sys",
62 "mswsock",
63 "ntdll",
64 "ntoskrnl.exe",
65 "opengl32",
66 "sane.ds",
67 "twain_32",
68 "unicows",
69 "user32",
70 "vdmdbg",
71 "w32skrnl",
72 "winealsa.drv",
73 "wineaudioio.drv",
74 "wined3d",
75 "winedos",
76 "wineesd.drv",
77 "winejack.drv",
78 "winejoystick.drv",
79 "winemp3.acm",
80 "winenas.drv",
81 "wineoss.drv",
82 "wineps",
83 "wineps.drv",
84 "winepulse.drv",
85 "winex11.drv",
86 "winmm",
87 "wintab32",
88 "wnaspi32",
89 "wow32",
90 "ws2_32",
91 "wsock32",
94 enum dllmode
96 BUILTIN_NATIVE,
97 NATIVE_BUILTIN,
98 BUILTIN,
99 NATIVE,
100 DISABLE,
101 UNKNOWN /* Special value indicating an erroneous DLL override mode */
104 struct dll
106 char *name;
107 enum dllmode mode;
110 /* Convert a registry string to a dllmode */
111 static enum dllmode string_to_mode(char *in)
113 int i, j, len;
114 char *out;
115 enum dllmode res;
117 len = strlen(in);
118 out = HeapAlloc(GetProcessHeap(), 0, len);
120 /* remove the spaces */
121 for (i = j = 0; i <= len; ++i) {
122 if (in[i] != ' ') {
123 out[j++] = in[i];
127 /* parse the string */
128 res = UNKNOWN;
129 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
130 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
131 if (strcmp(out, "builtin") == 0) res = BUILTIN;
132 if (strcmp(out, "native") == 0) res = NATIVE;
133 if (strcmp(out, "") == 0) res = DISABLE;
135 HeapFree(GetProcessHeap(), 0, out);
136 return res;
139 /* Convert a dllmode to a registry string. */
140 static const char* mode_to_string(enum dllmode mode)
142 switch( mode )
144 case NATIVE: return "native";
145 case BUILTIN: return "builtin";
146 case NATIVE_BUILTIN: return "native,builtin";
147 case BUILTIN_NATIVE: return "builtin,native";
148 case DISABLE: return "";
149 default: assert(FALSE); return "";
153 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
154 static const char* mode_to_label(enum dllmode mode)
156 static char buffer[256];
157 UINT id = 0;
159 switch( mode )
161 case NATIVE: id = IDS_DLL_NATIVE; break;
162 case BUILTIN: id = IDS_DLL_BUILTIN; break;
163 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
164 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
165 case DISABLE: id = IDS_DLL_DISABLED; break;
166 default: assert(FALSE);
168 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
169 return buffer;
172 /* Convert a control id (IDC_ constant) to a dllmode */
173 static enum dllmode id_to_mode(DWORD id)
175 switch( id )
177 case IDC_RAD_BUILTIN: return BUILTIN;
178 case IDC_RAD_NATIVE: return NATIVE;
179 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
180 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
181 case IDC_RAD_DISABLE: return DISABLE;
182 default: assert( FALSE ); return 0; /* should not be reached */
186 /* Convert a dllmode to a control id (IDC_ constant) */
187 static DWORD mode_to_id(enum dllmode mode)
189 switch( mode )
191 case BUILTIN: return IDC_RAD_BUILTIN;
192 case NATIVE: return IDC_RAD_NATIVE;
193 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
194 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
195 case DISABLE: return IDC_RAD_DISABLE;
196 default: assert( FALSE ); return 0; /* should not be reached */
200 /* helper for is_builtin_only */
201 static int compare_dll( const void *ptr1, const void *ptr2 )
203 const char * const *name1 = ptr1;
204 const char * const *name2 = ptr2;
205 return strcmp( *name1, *name2 );
208 /* check if dll is recommended as builtin only */
209 static inline int is_builtin_only( const char *name )
211 const char *ext = strrchr( name, '.' );
213 if (ext)
215 if (!strcmp( ext, ".vxd" ) ||
216 !strcmp( ext, ".drv" ) ||
217 !strcmp( ext, ".tlb" ))
218 return TRUE;
220 return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
221 sizeof(builtin_only[0]), compare_dll ) != NULL;
224 /* check if dll should be offered in the drop-down list */
225 static int show_dll_in_list( const char *name )
227 const char *ext = strrchr( name, '.' );
229 if (ext)
231 /* skip 16-bit dlls */
232 if (strlen(ext) > 2 && !strcmp( ext + strlen(ext) - 2, "16" )) return FALSE;
233 /* skip exes */
234 if (!strcmp( ext, ".exe" )) return FALSE;
236 /* skip dlls that should always be builtin */
237 return !is_builtin_only( name );
240 static void set_controls_from_selection(HWND dialog)
242 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
245 static void clear_settings(HWND dialog)
247 int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
248 int i;
250 WINE_TRACE("count=%d\n", count);
252 for (i = 0; i < count; i++)
254 struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
256 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
258 HeapFree(GetProcessHeap(), 0, dll->name);
259 HeapFree(GetProcessHeap(), 0, dll);
263 /* load the list of available libraries from a given dir */
264 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
266 char *buffer = NULL, name[256];
267 struct dirent *de;
268 DIR *dir = opendir( dir_path );
270 if (!dir) return;
272 if (check_subdirs)
273 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
275 while ((de = readdir( dir )))
277 size_t len = strlen(de->d_name);
278 if (len > sizeof(name)) continue;
279 if (len > 3 && !strcmp( de->d_name + len - 3, ".so"))
281 len -= 3;
282 if (len > 4 && !strcmp( de->d_name + len - 4, ".dll.so")) len -= 4;
283 memcpy( name, de->d_name, len );
284 name[len] = 0;
285 if (!show_dll_in_list( name )) continue;
286 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
288 else if (check_subdirs)
290 struct stat st;
291 if (!show_dll_in_list( de->d_name )) continue;
292 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
293 if (!stat( buffer, &st ))
295 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
296 continue;
298 sprintf( buffer, "%s/%s/%s.so", dir_path, de->d_name, de->d_name );
299 if (!stat( buffer, &st ))
301 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
302 continue;
306 closedir( dir );
307 HeapFree( GetProcessHeap(), 0, buffer );
310 /* load the list of available libraries */
311 static void load_library_list( HWND dialog )
313 unsigned int i = 0;
314 const char *path, *build_dir = wine_get_build_dir();
315 char item1[256], item2[256];
316 HCURSOR old_cursor = SetCursor( LoadCursor(0, IDC_WAIT) );
318 if (build_dir)
320 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
321 strcpy( dir, build_dir );
322 strcat( dir, "/dlls" );
323 load_library_list_from_dir( dialog, dir, TRUE );
324 HeapFree( GetProcessHeap(), 0, dir );
327 while ((path = wine_dll_enum_load_path( i++ )))
328 load_library_list_from_dir( dialog, path, FALSE );
330 /* get rid of duplicate entries */
332 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
333 i = 1;
334 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
336 if (!strcmp( item1, item2 ))
338 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
340 else
342 strcpy( item1, item2 );
343 i++;
346 SetCursor( old_cursor );
349 static void load_library_settings(HWND dialog)
351 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
352 char **p;
353 int sel, count = 0;
355 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
357 WINE_TRACE("sel=%d\n", sel);
359 clear_settings(dialog);
361 if (!overrides || *overrides == NULL)
363 set_controls_from_selection(dialog);
364 disable(IDC_DLLS_EDITDLL);
365 disable(IDC_DLLS_REMOVEDLL);
366 HeapFree(GetProcessHeap(), 0, overrides);
367 return;
370 enable(IDC_DLLS_EDITDLL);
371 enable(IDC_DLLS_REMOVEDLL);
373 for (p = overrides; *p != NULL; p++)
375 int index;
376 char *str, *value;
377 const char *label;
378 struct dll *dll;
380 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
382 label = mode_to_label(string_to_mode(value));
384 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
385 strcpy(str, *p);
386 strcat(str, " (");
387 strcat(str, label);
388 strcat(str, ")");
390 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
391 dll->name = *p;
392 dll->mode = string_to_mode(value);
394 index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
395 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
397 HeapFree(GetProcessHeap(), 0, str);
399 count++;
402 HeapFree(GetProcessHeap(), 0, overrides);
404 /* restore the previous selection, if possible */
405 if (sel >= count - 1) sel = count - 1;
406 else if (sel == -1) sel = 0;
408 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
410 set_controls_from_selection(dialog);
413 /* Called when the application is initialized (cannot reinit!) */
414 static void init_libsheet(HWND dialog)
416 /* clear the add dll controls */
417 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
418 load_library_list( dialog );
419 disable(IDC_DLLS_ADDDLL);
422 static void on_add_combo_change(HWND dialog)
424 char buffer[1024];
425 int sel, len;
427 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
428 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
429 sel=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
430 len=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
432 if (strlen(buffer)>0 || len>0)
433 enable(IDC_DLLS_ADDDLL)
434 else
435 disable(IDC_DLLS_ADDDLL);
438 static void set_dllmode(HWND dialog, DWORD id)
440 enum dllmode mode;
441 struct dll *dll;
442 int sel;
443 const char *str;
445 mode = id_to_mode(id);
447 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
448 if (sel == -1) return;
450 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
452 str = mode_to_string(mode);
453 WINE_TRACE("Setting %s to %s\n", dll->name, str);
455 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
456 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
458 load_library_settings(dialog); /* ... and refresh */
461 static void on_add_click(HWND dialog)
463 static const char dotDll[] = ".dll";
464 char buffer[1024], *ptr;
466 ZeroMemory(buffer, sizeof(buffer));
468 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
469 if (lstrlenA(buffer) >= sizeof(dotDll))
471 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
472 if (!lstrcmpiA(ptr, dotDll))
474 WINE_TRACE("Stripping dll extension\n");
475 *ptr = '\0';
479 /* check if dll is in the builtin-only list */
480 if (!(ptr = strrchr( buffer, '\\' )))
482 ptr = buffer;
483 if (*ptr == '*') ptr++;
485 else ptr++;
486 if (is_builtin_only( ptr ))
488 MSGBOXPARAMSA params;
489 params.cbSize = sizeof(params);
490 params.hwndOwner = dialog;
491 params.hInstance = GetModuleHandleA( NULL );
492 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
493 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
494 params.dwStyle = MB_ICONWARNING | MB_YESNO;
495 params.lpszIcon = NULL;
496 params.dwContextHelpId = 0;
497 params.lpfnMsgBoxCallback = NULL;
498 params.dwLanguageId = 0;
499 if (MessageBoxIndirectA( &params ) != IDYES) return;
502 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
503 disable(IDC_DLLS_ADDDLL);
505 WINE_TRACE("Adding %s as native, builtin\n", buffer);
507 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
508 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
510 load_library_settings(dialog);
512 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
514 set_controls_from_selection(dialog);
517 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
519 static WORD sel;
521 switch(uMsg)
523 case WM_INITDIALOG:
524 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
525 sel = lParam;
526 return TRUE;
528 case WM_COMMAND:
529 if(HIWORD(wParam) != BN_CLICKED) break;
530 switch (LOWORD(wParam))
532 case IDC_RAD_BUILTIN:
533 case IDC_RAD_NATIVE:
534 case IDC_RAD_BUILTIN_NATIVE:
535 case IDC_RAD_NATIVE_BUILTIN:
536 case IDC_RAD_DISABLE:
537 sel = LOWORD(wParam);
538 return TRUE;
539 case IDOK:
540 EndDialog(hwndDlg, sel);
541 return TRUE;
542 case IDCANCEL:
543 EndDialog(hwndDlg, wParam);
544 return TRUE;
547 return FALSE;
550 static void on_edit_click(HWND hwnd)
552 INT_PTR ret;
553 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
554 struct dll *dll;
555 DWORD id;
557 /* if no override is selected the edit button should be disabled... */
558 assert(index != -1);
560 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
561 id = mode_to_id(dll->mode);
563 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
565 if(ret != IDCANCEL)
566 set_dllmode(hwnd, ret);
569 static void on_remove_click(HWND dialog)
571 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
572 struct dll *dll;
574 if (sel == LB_ERR) return;
576 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
578 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
580 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
581 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
583 HeapFree(GetProcessHeap(), 0, dll->name);
584 HeapFree(GetProcessHeap(), 0, dll);
586 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
587 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
588 else
590 disable(IDC_DLLS_EDITDLL);
591 disable(IDC_DLLS_REMOVEDLL);
594 set_controls_from_selection(dialog);
597 INT_PTR CALLBACK
598 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
600 switch (uMsg)
602 case WM_INITDIALOG:
603 init_libsheet(hDlg);
604 break;
605 case WM_SHOWWINDOW:
606 set_window_title(hDlg);
607 break;
608 case WM_NOTIFY:
609 switch (((LPNMHDR)lParam)->code) {
610 case PSN_SETACTIVE:
611 load_library_settings(hDlg);
612 break;
614 break;
615 case WM_COMMAND:
616 switch(HIWORD(wParam)) {
618 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
619 * add button, rather than the propsheet OK button. But I don't know how to do that!
622 case CBN_EDITCHANGE:
623 if(LOWORD(wParam) == IDC_DLLCOMBO)
625 on_add_combo_change(hDlg);
626 break;
629 case BN_CLICKED:
630 switch(LOWORD(wParam)) {
631 case IDC_DLLS_ADDDLL:
632 on_add_click(hDlg);
633 break;
634 case IDC_DLLS_EDITDLL:
635 on_edit_click(hDlg);
636 break;
637 case IDC_DLLS_REMOVEDLL:
638 on_remove_click(hDlg);
639 break;
641 break;
642 case LBN_SELCHANGE:
643 if(LOWORD(wParam) == IDC_DLLCOMBO)
644 on_add_combo_change(hDlg);
645 else
646 set_controls_from_selection(hDlg);
647 break;
649 break;
652 return 0;