winecfg: Display a warning when the user tries to change the load
[wine/multimedia.git] / programs / winecfg / libraries.c
blob2d9e60db264b6b1b05dfcf46922b74ebd5465594
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #define NONAMELESSUNION
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_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #include "winecfg.h"
40 #include "resource.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
44 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
45 static const char * const builtin_only[] =
47 "advapi32",
48 "capi2032",
49 "dbghelp",
50 "ddraw",
51 "gdi32",
52 "glu32",
53 "glut32",
54 "icmp",
55 "iphlpapi",
56 "joystick.drv",
57 "kernel32",
58 "mswsock",
59 "ntdll",
60 "opengl32",
61 "stdole2.tlb",
62 "stdole32.tlb",
63 "twain_32",
64 "unicows",
65 "user32",
66 "vdmdbg",
67 "w32skrnl",
68 "winealsa.drv",
69 "winearts.drv",
70 "wineaudioio.drv",
71 "wined3d",
72 "winedos",
73 "wineesd.drv",
74 "winejack.drv",
75 "winemp3.acm",
76 "winenas.drv",
77 "wineoss.drv",
78 "wineps",
79 "wineps.drv",
80 "winex11.drv",
81 "winmm",
82 "wintab32",
83 "wnaspi32",
84 "wow32",
85 "ws2_32",
86 "wsock32",
89 enum dllmode
91 BUILTIN_NATIVE,
92 NATIVE_BUILTIN,
93 BUILTIN,
94 NATIVE,
95 DISABLE,
96 UNKNOWN /* Special value indicating an erronous DLL override mode */
99 struct dll
101 char *name;
102 enum dllmode mode;
105 /* Convert a registry string to a dllmode */
106 static enum dllmode string_to_mode(char *in)
108 int i, j, len;
109 char *out;
110 enum dllmode res;
112 len = strlen(in);
113 out = HeapAlloc(GetProcessHeap(), 0, len);
115 /* remove the spaces */
116 for (i = j = 0; i <= len; ++i) {
117 if (in[i] != ' ') {
118 out[j++] = in[i];
122 /* parse the string */
123 res = UNKNOWN;
124 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
125 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
126 if (strcmp(out, "builtin") == 0) res = BUILTIN;
127 if (strcmp(out, "native") == 0) res = NATIVE;
128 if (strcmp(out, "") == 0) res = DISABLE;
130 HeapFree(GetProcessHeap(), 0, out);
131 return res;
134 /* Convert a dllmode to a registry string. */
135 static const char* mode_to_string(enum dllmode mode)
137 switch( mode )
139 case NATIVE: return "native";
140 case BUILTIN: return "builtin";
141 case NATIVE_BUILTIN: return "native,builtin";
142 case BUILTIN_NATIVE: return "builtin,native";
143 case DISABLE: return "";
144 default: assert(FALSE); return "";
148 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
149 static const char* mode_to_label(enum dllmode mode)
151 static char buffer[256];
152 UINT id = 0;
154 switch( mode )
156 case NATIVE: id = IDS_DLL_NATIVE; break;
157 case BUILTIN: id = IDS_DLL_BUILTIN; break;
158 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
159 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
160 case DISABLE: id = IDS_DLL_DISABLED; break;
161 default: assert(FALSE);
163 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
164 return buffer;
167 /* Convert a control id (IDC_ constant) to a dllmode */
168 static enum dllmode id_to_mode(DWORD id)
170 switch( id )
172 case IDC_RAD_BUILTIN: return BUILTIN;
173 case IDC_RAD_NATIVE: return NATIVE;
174 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
175 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
176 case IDC_RAD_DISABLE: return DISABLE;
177 default: assert( FALSE ); return 0; /* should not be reached */
181 /* Convert a dllmode to a control id (IDC_ constant) */
182 static DWORD mode_to_id(enum dllmode mode)
184 switch( mode )
186 case BUILTIN: return IDC_RAD_BUILTIN;
187 case NATIVE: return IDC_RAD_NATIVE;
188 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
189 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
190 case DISABLE: return IDC_RAD_DISABLE;
191 default: assert( FALSE ); return 0; /* should not be reached */
195 /* helper for is_builtin_only */
196 static int compare_dll( const void *ptr1, const void *ptr2 )
198 const char * const *name1 = ptr1;
199 const char * const *name2 = ptr2;
200 return strcmp( *name1, *name2 );
203 /* check if dll is recommended as builtin only */
204 static inline int is_builtin_only( const char *name )
206 return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
207 sizeof(builtin_only[0]), compare_dll ) != NULL;
210 static void set_controls_from_selection(HWND dialog)
212 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
215 static void clear_settings(HWND dialog)
217 int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
218 int i;
220 WINE_TRACE("count=%d\n", count);
222 for (i = 0; i < count; i++)
224 struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
226 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
228 HeapFree(GetProcessHeap(), 0, dll->name);
229 HeapFree(GetProcessHeap(), 0, dll);
233 /* check if a given dll is 16-bit */
234 static int is_16bit_dll( const char *dir, const char *name )
236 char buffer[64];
237 int res;
238 size_t len = strlen(dir) + strlen(name) + 2;
239 char *path = HeapAlloc( GetProcessHeap(), 0, len );
241 strcpy( path, dir );
242 strcat( path, "/" );
243 strcat( path, name );
244 res = readlink( path, buffer, sizeof(buffer) );
245 HeapFree( GetProcessHeap(), 0, path );
247 if (res == -1) return 0; /* not a symlink */
248 if (res < 4 || res >= sizeof(buffer)) return 0;
249 buffer[res] = 0;
250 if (strchr( buffer, '/' )) return 0; /* contains a path, not valid */
251 if (strcmp( buffer + res - 3, ".so" )) return 0; /* does not end in .so, not valid */
252 return 1;
255 /* load the list of available libraries from a given dir */
256 static void load_library_list_from_dir( HWND dialog, const char *dir_path )
258 char name[256];
259 struct dirent *de;
260 DIR *dir = opendir( dir_path );
262 if (!dir) return;
264 while ((de = readdir( dir )))
266 size_t len = strlen(de->d_name);
267 if (len > sizeof(name) || len <= 7 || strcmp( de->d_name + len - 7, ".dll.so")) continue;
268 if (is_16bit_dll( dir_path, de->d_name )) continue; /* 16-bit dlls can't be configured */
269 len -= 7;
270 memcpy( name, de->d_name, len );
271 name[len] = 0;
272 /* skip dlls that should always be builtin */
273 if (is_builtin_only( name )) continue;
274 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
276 closedir( dir );
279 /* load the list of available libraries */
280 static void load_library_list( HWND dialog )
282 unsigned int i = 0;
283 const char *path;
284 char item1[256], item2[256];
286 while ((path = wine_dll_enum_load_path( i++ )))
287 load_library_list_from_dir( dialog, path );
289 /* get rid of duplicate entries */
291 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
292 i = 1;
293 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
295 if (!strcmp( item1, item2 ))
297 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
299 else
301 strcpy( item1, item2 );
302 i++;
307 static void load_library_settings(HWND dialog)
309 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
310 char **p;
311 int sel, count = 0;
313 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
315 WINE_TRACE("sel=%d\n", sel);
317 clear_settings(dialog);
319 if (!overrides || *overrides == NULL)
321 set_controls_from_selection(dialog);
322 disable(IDC_DLLS_EDITDLL);
323 disable(IDC_DLLS_REMOVEDLL);
324 HeapFree(GetProcessHeap(), 0, overrides);
325 return;
328 enable(IDC_DLLS_EDITDLL);
329 enable(IDC_DLLS_REMOVEDLL);
331 for (p = overrides; *p != NULL; p++)
333 int index;
334 char *str, *value;
335 const char *label;
336 struct dll *dll;
338 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
340 label = mode_to_label(string_to_mode(value));
342 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
343 strcpy(str, *p);
344 strcat(str, " (");
345 strcat(str, label);
346 strcat(str, ")");
348 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
349 dll->name = *p;
350 dll->mode = string_to_mode(value);
352 index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
353 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
355 HeapFree(GetProcessHeap(), 0, str);
357 count++;
360 HeapFree(GetProcessHeap(), 0, overrides);
362 /* restore the previous selection, if possible */
363 if (sel >= count - 1) sel = count - 1;
364 else if (sel == -1) sel = 0;
366 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
368 set_controls_from_selection(dialog);
371 /* Called when the application is initialized (cannot reinit!) */
372 static void init_libsheet(HWND dialog)
374 /* clear the add dll controls */
375 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
376 load_library_list( dialog );
377 disable(IDC_DLLS_ADDDLL);
380 static void on_add_combo_change(HWND dialog)
382 char buffer[1024];
384 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
386 if (strlen(buffer))
387 enable(IDC_DLLS_ADDDLL)
388 else
389 disable(IDC_DLLS_ADDDLL);
392 static void set_dllmode(HWND dialog, DWORD id)
394 enum dllmode mode;
395 struct dll *dll;
396 int sel;
397 const char *str;
399 mode = id_to_mode(id);
401 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
402 if (sel == -1) return;
404 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
406 str = mode_to_string(mode);
407 WINE_TRACE("Setting %s to %s\n", dll->name, str);
409 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
410 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
412 load_library_settings(dialog); /* ... and refresh */
415 static void on_add_click(HWND dialog)
417 static const char dotDll[] = ".dll";
418 char buffer[1024], *ptr;
420 ZeroMemory(buffer, sizeof(buffer));
422 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
423 if (lstrlenA(buffer) >= sizeof(dotDll))
425 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
426 if (!lstrcmpiA(ptr, dotDll))
428 WINE_TRACE("Stripping dll extension\n");
429 *ptr = '\0';
433 /* check if dll is in the builtin-only list */
434 if (!(ptr = strrchr( buffer, '\\' )))
436 ptr = buffer;
437 if (*ptr == '*') ptr++;
439 else ptr++;
440 if (is_builtin_only( ptr ))
442 MSGBOXPARAMSA params;
443 params.cbSize = sizeof(params);
444 params.hwndOwner = dialog;
445 params.hInstance = GetModuleHandleA( NULL );
446 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
447 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
448 params.dwStyle = MB_ICONWARNING | MB_YESNO;
449 params.lpszIcon = NULL;
450 params.dwContextHelpId = 0;
451 params.lpfnMsgBoxCallback = NULL;
452 params.dwLanguageId = 0;
453 if (MessageBoxIndirectA( &params ) != IDYES) return;
456 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
457 disable(IDC_DLLS_ADDDLL);
459 WINE_TRACE("Adding %s as native, builtin", buffer);
461 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
462 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
464 load_library_settings(dialog);
466 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, (WPARAM) 0, (LPARAM) buffer);
468 set_controls_from_selection(dialog);
471 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
473 static WORD sel;
475 switch(uMsg)
477 case WM_INITDIALOG:
478 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
479 sel = lParam;
480 return TRUE;
482 case WM_COMMAND:
483 if(HIWORD(wParam) != BN_CLICKED) break;
484 switch (LOWORD(wParam))
486 case IDC_RAD_BUILTIN:
487 case IDC_RAD_NATIVE:
488 case IDC_RAD_BUILTIN_NATIVE:
489 case IDC_RAD_NATIVE_BUILTIN:
490 case IDC_RAD_DISABLE:
491 sel = LOWORD(wParam);
492 return TRUE;
493 case IDOK:
494 EndDialog(hwndDlg, sel);
495 return TRUE;
496 case IDCANCEL:
497 EndDialog(hwndDlg, wParam);
498 return TRUE;
501 return FALSE;
504 static void on_edit_click(HWND hwnd)
506 INT_PTR ret;
507 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
508 struct dll *dll;
509 DWORD id;
511 /* if no override is selected the edit button should be disabled... */
512 assert(index != -1);
514 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
515 id = mode_to_id(dll->mode);
517 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
519 if(ret != IDCANCEL)
520 set_dllmode(hwnd, ret);
523 static void on_remove_click(HWND dialog)
525 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
526 struct dll *dll;
528 if (sel == LB_ERR) return;
530 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
532 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
534 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
535 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
537 HeapFree(GetProcessHeap(), 0, dll->name);
538 HeapFree(GetProcessHeap(), 0, dll);
540 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
541 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
542 else
544 disable(IDC_DLLS_EDITDLL);
545 disable(IDC_DLLS_REMOVEDLL);
548 set_controls_from_selection(dialog);
551 INT_PTR CALLBACK
552 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
554 switch (uMsg)
556 case WM_INITDIALOG:
557 init_libsheet(hDlg);
558 break;
559 case WM_SHOWWINDOW:
560 set_window_title(hDlg);
561 break;
562 case WM_NOTIFY:
563 switch (((LPNMHDR)lParam)->code) {
564 case PSN_SETACTIVE:
565 load_library_settings(hDlg);
566 break;
568 break;
569 case WM_COMMAND:
570 switch(HIWORD(wParam)) {
572 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
573 * add button, rather than the propsheet OK button. But I don't know how to do that!
576 case CBN_EDITCHANGE:
577 if(LOWORD(wParam) == IDC_DLLCOMBO)
579 on_add_combo_change(hDlg);
580 break;
583 case BN_CLICKED:
584 switch(LOWORD(wParam)) {
585 case IDC_DLLS_ADDDLL:
586 on_add_click(hDlg);
587 break;
588 case IDC_DLLS_EDITDLL:
589 on_edit_click(hDlg);
590 break;
591 case IDC_DLLS_REMOVEDLL:
592 on_remove_click(hDlg);
593 break;
595 break;
596 case LBN_SELCHANGE:
597 if(LOWORD(wParam) == IDC_DLLCOMBO)
598 on_add_combo_change(hDlg);
599 else
600 set_controls_from_selection(hDlg);
601 break;
603 break;
606 return 0;