push 337eb2e2d902d84a5d689451984c5832d7e04fc4
[wine/hacks.git] / programs / winecfg / libraries.c
blob2ef9a5368403aba75b88301db03fbab180c0174b
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 "iphlpapi",
59 "kernel32",
60 "mswsock",
61 "ntdll",
62 "opengl32",
63 "stdole2.tlb",
64 "stdole32.tlb",
65 "twain_32",
66 "unicows",
67 "user32",
68 "vdmdbg",
69 "w32skrnl",
70 "winealsa.drv",
71 "wineaudioio.drv",
72 "wined3d",
73 "winedos",
74 "wineesd.drv",
75 "winejack.drv",
76 "winejoystick.drv",
77 "winemp3.acm",
78 "winenas.drv",
79 "wineoss.drv",
80 "wineps",
81 "wineps.drv",
82 "winepulse.drv",
83 "winex11.drv",
84 "winmm",
85 "wintab32",
86 "wnaspi32",
87 "wow32",
88 "ws2_32",
89 "wsock32",
92 enum dllmode
94 BUILTIN_NATIVE,
95 NATIVE_BUILTIN,
96 BUILTIN,
97 NATIVE,
98 DISABLE,
99 UNKNOWN /* Special value indicating an erroneous DLL override mode */
102 struct dll
104 char *name;
105 enum dllmode mode;
108 /* Convert a registry string to a dllmode */
109 static enum dllmode string_to_mode(char *in)
111 int i, j, len;
112 char *out;
113 enum dllmode res;
115 len = strlen(in);
116 out = HeapAlloc(GetProcessHeap(), 0, len);
118 /* remove the spaces */
119 for (i = j = 0; i <= len; ++i) {
120 if (in[i] != ' ') {
121 out[j++] = in[i];
125 /* parse the string */
126 res = UNKNOWN;
127 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
128 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
129 if (strcmp(out, "builtin") == 0) res = BUILTIN;
130 if (strcmp(out, "native") == 0) res = NATIVE;
131 if (strcmp(out, "") == 0) res = DISABLE;
133 HeapFree(GetProcessHeap(), 0, out);
134 return res;
137 /* Convert a dllmode to a registry string. */
138 static const char* mode_to_string(enum dllmode mode)
140 switch( mode )
142 case NATIVE: return "native";
143 case BUILTIN: return "builtin";
144 case NATIVE_BUILTIN: return "native,builtin";
145 case BUILTIN_NATIVE: return "builtin,native";
146 case DISABLE: return "";
147 default: assert(FALSE); return "";
151 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
152 static const char* mode_to_label(enum dllmode mode)
154 static char buffer[256];
155 UINT id = 0;
157 switch( mode )
159 case NATIVE: id = IDS_DLL_NATIVE; break;
160 case BUILTIN: id = IDS_DLL_BUILTIN; break;
161 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
162 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
163 case DISABLE: id = IDS_DLL_DISABLED; break;
164 default: assert(FALSE);
166 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
167 return buffer;
170 /* Convert a control id (IDC_ constant) to a dllmode */
171 static enum dllmode id_to_mode(DWORD id)
173 switch( id )
175 case IDC_RAD_BUILTIN: return BUILTIN;
176 case IDC_RAD_NATIVE: return NATIVE;
177 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
178 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
179 case IDC_RAD_DISABLE: return DISABLE;
180 default: assert( FALSE ); return 0; /* should not be reached */
184 /* Convert a dllmode to a control id (IDC_ constant) */
185 static DWORD mode_to_id(enum dllmode mode)
187 switch( mode )
189 case BUILTIN: return IDC_RAD_BUILTIN;
190 case NATIVE: return IDC_RAD_NATIVE;
191 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
192 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
193 case DISABLE: return IDC_RAD_DISABLE;
194 default: assert( FALSE ); return 0; /* should not be reached */
198 /* helper for is_builtin_only */
199 static int compare_dll( const void *ptr1, const void *ptr2 )
201 const char * const *name1 = ptr1;
202 const char * const *name2 = ptr2;
203 return strcmp( *name1, *name2 );
206 /* check if dll is recommended as builtin only */
207 static inline int is_builtin_only( const char *name )
209 return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
210 sizeof(builtin_only[0]), compare_dll ) != NULL;
213 static void set_controls_from_selection(HWND dialog)
215 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
218 static void clear_settings(HWND dialog)
220 int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
221 int i;
223 WINE_TRACE("count=%d\n", count);
225 for (i = 0; i < count; i++)
227 struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
229 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
231 HeapFree(GetProcessHeap(), 0, dll->name);
232 HeapFree(GetProcessHeap(), 0, dll);
236 /* check if a given dll is 16-bit */
237 static int is_16bit_dll( const char *dir, const char *name )
239 char buffer[64];
240 int res;
241 size_t len = strlen(dir) + strlen(name) + 2;
242 char *path = HeapAlloc( GetProcessHeap(), 0, len );
244 strcpy( path, dir );
245 strcat( path, "/" );
246 strcat( path, name );
247 res = readlink( path, buffer, sizeof(buffer) );
248 HeapFree( GetProcessHeap(), 0, path );
250 if (res == -1) return 0; /* not a symlink */
251 if (res < 4 || res >= sizeof(buffer)) return 0;
252 buffer[res] = 0;
253 if (strchr( buffer, '/' )) return 0; /* contains a path, not valid */
254 if (strcmp( buffer + res - 3, ".so" )) return 0; /* does not end in .so, not valid */
255 return 1;
258 /* load the list of available libraries from a given dir */
259 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
261 char *buffer = NULL, name[256];
262 struct dirent *de;
263 DIR *dir = opendir( dir_path );
265 if (!dir) return;
267 if (check_subdirs)
268 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
270 while ((de = readdir( dir )))
272 size_t len = strlen(de->d_name);
273 if (len > sizeof(name)) continue;
274 if (len > 7 && !strcmp( de->d_name + len - 7, ".dll.so"))
276 if (is_16bit_dll( dir_path, de->d_name )) continue; /* 16-bit dlls can't be configured */
277 len -= 7;
278 memcpy( name, de->d_name, len );
279 name[len] = 0;
280 /* skip dlls that should always be builtin */
281 if (is_builtin_only( name )) continue;
282 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
284 else if (check_subdirs)
286 struct stat st;
287 if (is_builtin_only( de->d_name )) continue;
288 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
289 if (!stat( buffer, &st ))
290 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
293 closedir( dir );
294 HeapFree( GetProcessHeap(), 0, buffer );
297 /* load the list of available libraries */
298 static void load_library_list( HWND dialog )
300 unsigned int i = 0;
301 const char *path, *build_dir = wine_get_build_dir();
302 char item1[256], item2[256];
303 HCURSOR old_cursor = SetCursor( LoadCursor(0, IDC_WAIT) );
305 if (build_dir)
307 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
308 strcpy( dir, build_dir );
309 strcat( dir, "/dlls" );
310 load_library_list_from_dir( dialog, dir, TRUE );
311 HeapFree( GetProcessHeap(), 0, dir );
314 while ((path = wine_dll_enum_load_path( i++ )))
315 load_library_list_from_dir( dialog, path, FALSE );
317 /* get rid of duplicate entries */
319 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
320 i = 1;
321 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
323 if (!strcmp( item1, item2 ))
325 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
327 else
329 strcpy( item1, item2 );
330 i++;
333 SetCursor( old_cursor );
336 static void load_library_settings(HWND dialog)
338 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
339 char **p;
340 int sel, count = 0;
342 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
344 WINE_TRACE("sel=%d\n", sel);
346 clear_settings(dialog);
348 if (!overrides || *overrides == NULL)
350 set_controls_from_selection(dialog);
351 disable(IDC_DLLS_EDITDLL);
352 disable(IDC_DLLS_REMOVEDLL);
353 HeapFree(GetProcessHeap(), 0, overrides);
354 return;
357 enable(IDC_DLLS_EDITDLL);
358 enable(IDC_DLLS_REMOVEDLL);
360 for (p = overrides; *p != NULL; p++)
362 int index;
363 char *str, *value;
364 const char *label;
365 struct dll *dll;
367 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
369 label = mode_to_label(string_to_mode(value));
371 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
372 strcpy(str, *p);
373 strcat(str, " (");
374 strcat(str, label);
375 strcat(str, ")");
377 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
378 dll->name = *p;
379 dll->mode = string_to_mode(value);
381 index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
382 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
384 HeapFree(GetProcessHeap(), 0, str);
386 count++;
389 HeapFree(GetProcessHeap(), 0, overrides);
391 /* restore the previous selection, if possible */
392 if (sel >= count - 1) sel = count - 1;
393 else if (sel == -1) sel = 0;
395 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
397 set_controls_from_selection(dialog);
400 /* Called when the application is initialized (cannot reinit!) */
401 static void init_libsheet(HWND dialog)
403 /* clear the add dll controls */
404 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
405 load_library_list( dialog );
406 disable(IDC_DLLS_ADDDLL);
409 static void on_add_combo_change(HWND dialog)
411 char buffer[1024];
412 int sel, len;
414 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
415 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
416 sel=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
417 len=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
419 if (strlen(buffer)>0 || len>0)
420 enable(IDC_DLLS_ADDDLL)
421 else
422 disable(IDC_DLLS_ADDDLL);
425 static void set_dllmode(HWND dialog, DWORD id)
427 enum dllmode mode;
428 struct dll *dll;
429 int sel;
430 const char *str;
432 mode = id_to_mode(id);
434 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
435 if (sel == -1) return;
437 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
439 str = mode_to_string(mode);
440 WINE_TRACE("Setting %s to %s\n", dll->name, str);
442 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
443 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
445 load_library_settings(dialog); /* ... and refresh */
448 static void on_add_click(HWND dialog)
450 static const char dotDll[] = ".dll";
451 char buffer[1024], *ptr;
453 ZeroMemory(buffer, sizeof(buffer));
455 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
456 if (lstrlenA(buffer) >= sizeof(dotDll))
458 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
459 if (!lstrcmpiA(ptr, dotDll))
461 WINE_TRACE("Stripping dll extension\n");
462 *ptr = '\0';
466 /* check if dll is in the builtin-only list */
467 if (!(ptr = strrchr( buffer, '\\' )))
469 ptr = buffer;
470 if (*ptr == '*') ptr++;
472 else ptr++;
473 if (is_builtin_only( ptr ))
475 MSGBOXPARAMSA params;
476 params.cbSize = sizeof(params);
477 params.hwndOwner = dialog;
478 params.hInstance = GetModuleHandleA( NULL );
479 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
480 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
481 params.dwStyle = MB_ICONWARNING | MB_YESNO;
482 params.lpszIcon = NULL;
483 params.dwContextHelpId = 0;
484 params.lpfnMsgBoxCallback = NULL;
485 params.dwLanguageId = 0;
486 if (MessageBoxIndirectA( &params ) != IDYES) return;
489 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
490 disable(IDC_DLLS_ADDDLL);
492 WINE_TRACE("Adding %s as native, builtin\n", buffer);
494 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
495 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
497 load_library_settings(dialog);
499 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
501 set_controls_from_selection(dialog);
504 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
506 static WORD sel;
508 switch(uMsg)
510 case WM_INITDIALOG:
511 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
512 sel = lParam;
513 return TRUE;
515 case WM_COMMAND:
516 if(HIWORD(wParam) != BN_CLICKED) break;
517 switch (LOWORD(wParam))
519 case IDC_RAD_BUILTIN:
520 case IDC_RAD_NATIVE:
521 case IDC_RAD_BUILTIN_NATIVE:
522 case IDC_RAD_NATIVE_BUILTIN:
523 case IDC_RAD_DISABLE:
524 sel = LOWORD(wParam);
525 return TRUE;
526 case IDOK:
527 EndDialog(hwndDlg, sel);
528 return TRUE;
529 case IDCANCEL:
530 EndDialog(hwndDlg, wParam);
531 return TRUE;
534 return FALSE;
537 static void on_edit_click(HWND hwnd)
539 INT_PTR ret;
540 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
541 struct dll *dll;
542 DWORD id;
544 /* if no override is selected the edit button should be disabled... */
545 assert(index != -1);
547 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
548 id = mode_to_id(dll->mode);
550 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
552 if(ret != IDCANCEL)
553 set_dllmode(hwnd, ret);
556 static void on_remove_click(HWND dialog)
558 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
559 struct dll *dll;
561 if (sel == LB_ERR) return;
563 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
565 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
567 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
568 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
570 HeapFree(GetProcessHeap(), 0, dll->name);
571 HeapFree(GetProcessHeap(), 0, dll);
573 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
574 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
575 else
577 disable(IDC_DLLS_EDITDLL);
578 disable(IDC_DLLS_REMOVEDLL);
581 set_controls_from_selection(dialog);
584 INT_PTR CALLBACK
585 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
587 switch (uMsg)
589 case WM_INITDIALOG:
590 init_libsheet(hDlg);
591 break;
592 case WM_SHOWWINDOW:
593 set_window_title(hDlg);
594 break;
595 case WM_NOTIFY:
596 switch (((LPNMHDR)lParam)->code) {
597 case PSN_SETACTIVE:
598 load_library_settings(hDlg);
599 break;
601 break;
602 case WM_COMMAND:
603 switch(HIWORD(wParam)) {
605 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
606 * add button, rather than the propsheet OK button. But I don't know how to do that!
609 case CBN_EDITCHANGE:
610 if(LOWORD(wParam) == IDC_DLLCOMBO)
612 on_add_combo_change(hDlg);
613 break;
616 case BN_CLICKED:
617 switch(LOWORD(wParam)) {
618 case IDC_DLLS_ADDDLL:
619 on_add_click(hDlg);
620 break;
621 case IDC_DLLS_EDITDLL:
622 on_edit_click(hDlg);
623 break;
624 case IDC_DLLS_REMOVEDLL:
625 on_remove_click(hDlg);
626 break;
628 break;
629 case LBN_SELCHANGE:
630 if(LOWORD(wParam) == IDC_DLLCOMBO)
631 on_add_combo_change(hDlg);
632 else
633 set_controls_from_selection(hDlg);
634 break;
636 break;
639 return 0;