shell32/tests: Use the available ARRAY_SIZE() macro.
[wine.git] / programs / winecfg / libraries.c
blob22e9435e3286383a9ef8fea9187b41434ae95dd8
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 "glu32",
56 "gphoto2.ds",
57 "icmp",
58 "iphlpapi",
59 "kernel32",
60 "l3codeca.acm",
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 "wined3d",
73 "winedos",
74 "wineps",
75 "winmm",
76 "wintab32",
77 "wnaspi32",
78 "wow32",
79 "ws2_32",
80 "wsock32",
83 enum dllmode
85 BUILTIN_NATIVE,
86 NATIVE_BUILTIN,
87 BUILTIN,
88 NATIVE,
89 DISABLE,
90 UNKNOWN /* Special value indicating an erroneous DLL override mode */
93 struct dll
95 char *name;
96 enum dllmode mode;
99 static const WCHAR emptyW[1];
101 /* Convert a registry string to a dllmode */
102 static enum dllmode string_to_mode(char *in)
104 int i, j, len;
105 char *out;
106 enum dllmode res;
108 len = strlen(in);
109 out = HeapAlloc(GetProcessHeap(), 0, len + 1);
111 /* remove the spaces */
112 for (i = j = 0; i <= len; ++i) {
113 if (in[i] != ' ') {
114 out[j++] = in[i];
118 /* parse the string */
119 res = UNKNOWN;
120 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
121 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
122 if (strcmp(out, "builtin") == 0) res = BUILTIN;
123 if (strcmp(out, "native") == 0) res = NATIVE;
124 if (strcmp(out, "") == 0) res = DISABLE;
126 HeapFree(GetProcessHeap(), 0, out);
127 return res;
130 /* Convert a dllmode to a registry string. */
131 static const char* mode_to_string(enum dllmode mode)
133 switch( mode )
135 case NATIVE: return "native";
136 case BUILTIN: return "builtin";
137 case NATIVE_BUILTIN: return "native,builtin";
138 case BUILTIN_NATIVE: return "builtin,native";
139 case DISABLE: return "";
140 default: return "";
144 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
145 static const char* mode_to_label(enum dllmode mode)
147 static char buffer[256];
148 UINT id = 0;
150 switch( mode )
152 case NATIVE: id = IDS_DLL_NATIVE; break;
153 case BUILTIN: id = IDS_DLL_BUILTIN; break;
154 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
155 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
156 case DISABLE: id = IDS_DLL_DISABLED; break;
157 default: return "??";
159 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
160 return buffer;
163 /* Convert a control id (IDC_ constant) to a dllmode */
164 static enum dllmode id_to_mode(DWORD id)
166 switch( id )
168 case IDC_RAD_BUILTIN: return BUILTIN;
169 case IDC_RAD_NATIVE: return NATIVE;
170 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
171 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
172 case IDC_RAD_DISABLE: return DISABLE;
173 default: assert( FALSE ); return 0; /* should not be reached */
177 /* Convert a dllmode to a control id (IDC_ constant) */
178 static DWORD mode_to_id(enum dllmode mode)
180 switch( mode )
182 case BUILTIN: return IDC_RAD_BUILTIN;
183 case NATIVE: return IDC_RAD_NATIVE;
184 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
185 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
186 case DISABLE: return IDC_RAD_DISABLE;
187 default: return IDC_RAD_BUILTIN_NATIVE;
191 /* helper for is_builtin_only */
192 static int compare_dll( const void *ptr1, const void *ptr2 )
194 const char * const *name1 = ptr1;
195 const char * const *name2 = ptr2;
196 return strcmp( *name1, *name2 );
199 /* check if dll is recommended as builtin only */
200 static inline BOOL is_builtin_only( const char *name )
202 const char *ext = strrchr( name, '.' );
204 if (ext)
206 if (!strcmp( ext, ".vxd" ) ||
207 !strcmp( ext, ".drv" ) ||
208 !strcmp( ext, ".tlb" ))
209 return TRUE;
211 return bsearch( &name, builtin_only, ARRAY_SIZE(builtin_only),
212 sizeof(builtin_only[0]), compare_dll ) != NULL;
215 /* check if dll should be offered in the drop-down list */
216 static BOOL show_dll_in_list( const char *name )
218 const char *ext = strrchr( name, '.' );
220 if (ext)
222 /* skip 16-bit dlls */
223 if (strlen(ext) > 2 && !strcmp( ext + strlen(ext) - 2, "16" )) return FALSE;
224 /* skip exes */
225 if (!strcmp( ext, ".exe" )) 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 char *buffer = NULL, name[256];
257 struct dirent *de;
258 DIR *dir = opendir( dir_path );
260 if (!dir) return;
262 if (check_subdirs)
263 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
265 while ((de = readdir( dir )))
267 size_t len = strlen(de->d_name);
268 if (len > sizeof(name)) continue;
269 if (len > 3 && !strcmp( de->d_name + len - 3, ".so"))
271 len -= 3;
272 if (len > 4 && !strcmp( de->d_name + len - 4, ".dll.so")) len -= 4;
273 memcpy( name, de->d_name, len );
274 name[len] = 0;
275 if (!show_dll_in_list( name )) continue;
276 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
278 else if (check_subdirs)
280 struct stat st;
281 if (!show_dll_in_list( de->d_name )) continue;
282 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
283 if (!stat( buffer, &st ))
285 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
286 continue;
288 sprintf( buffer, "%s/%s/%s.so", dir_path, de->d_name, de->d_name );
289 if (!stat( buffer, &st ))
291 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
292 continue;
296 closedir( dir );
297 HeapFree( GetProcessHeap(), 0, buffer );
300 /* load the list of available libraries */
301 static void load_library_list( HWND dialog )
303 unsigned int i = 0;
304 const char *path, *build_dir = wine_get_build_dir();
305 char item1[256], item2[256];
306 HCURSOR old_cursor = SetCursor( LoadCursorW(0, (LPWSTR)IDC_WAIT) );
308 if (build_dir)
310 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
311 strcpy( dir, build_dir );
312 strcat( dir, "/dlls" );
313 load_library_list_from_dir( dialog, dir, TRUE );
314 HeapFree( GetProcessHeap(), 0, dir );
317 while ((path = wine_dll_enum_load_path( i++ )))
318 load_library_list_from_dir( dialog, path, FALSE );
320 /* get rid of duplicate entries */
322 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
323 i = 1;
324 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
326 if (!strcmp( item1, item2 ))
328 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
330 else
332 strcpy( item1, item2 );
333 i++;
336 SetCursor( old_cursor );
339 static void load_library_settings(HWND dialog)
341 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
342 char **p;
343 int sel, count = 0;
345 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
347 WINE_TRACE("sel=%d\n", sel);
349 clear_settings(dialog);
351 if (!overrides || *overrides == NULL)
353 set_controls_from_selection(dialog);
354 disable(IDC_DLLS_EDITDLL);
355 disable(IDC_DLLS_REMOVEDLL);
356 HeapFree(GetProcessHeap(), 0, overrides);
357 return;
360 enable(IDC_DLLS_EDITDLL);
361 enable(IDC_DLLS_REMOVEDLL);
363 for (p = overrides; *p != NULL; p++)
365 int index;
366 char *str, *value;
367 const char *label;
368 struct dll *dll;
370 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
372 label = mode_to_label(string_to_mode(value));
374 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
375 strcpy(str, *p);
376 strcat(str, " (");
377 strcat(str, label);
378 strcat(str, ")");
380 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
381 dll->name = *p;
382 dll->mode = string_to_mode(value);
384 index = SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
385 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
387 HeapFree(GetProcessHeap(), 0, str);
389 count++;
392 HeapFree(GetProcessHeap(), 0, overrides);
394 /* restore the previous selection, if possible */
395 if (sel >= count - 1) sel = count - 1;
396 else if (sel == -1) sel = 0;
398 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
400 set_controls_from_selection(dialog);
403 /* Called when the application is initialized (cannot reinit!) */
404 static void init_libsheet(HWND dialog)
406 /* clear the add dll controls */
407 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM)emptyW);
408 load_library_list( dialog );
409 disable(IDC_DLLS_ADDDLL);
412 static void on_add_combo_change(HWND dialog)
414 WCHAR buffer[1024];
415 int sel, len;
417 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_GETTEXT, ARRAY_SIZE(buffer), (LPARAM)buffer);
418 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
419 sel=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
420 len=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
422 if (buffer[0] || len>0)
424 enable(IDC_DLLS_ADDDLL)
425 SendMessageW(GetParent(dialog), DM_SETDEFID, IDC_DLLS_ADDDLL, 0);
427 else
429 disable(IDC_DLLS_ADDDLL);
430 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
434 static void set_dllmode(HWND dialog, DWORD id)
436 enum dllmode mode;
437 struct dll *dll;
438 int sel;
439 const char *str;
441 mode = id_to_mode(id);
443 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
444 if (sel == -1) return;
446 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
448 str = mode_to_string(mode);
449 WINE_TRACE("Setting %s to %s\n", dll->name, str);
451 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
452 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
454 load_library_settings(dialog); /* ... and refresh */
457 static void on_add_click(HWND dialog)
459 static const char dotDll[] = ".dll";
460 char buffer[1024], *ptr;
462 ZeroMemory(buffer, sizeof(buffer));
464 SendDlgItemMessageA(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
465 if (lstrlenA(buffer) >= sizeof(dotDll))
467 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
468 if (!lstrcmpiA(ptr, dotDll))
470 WINE_TRACE("Stripping dll extension\n");
471 *ptr = '\0';
475 /* check if dll is in the builtin-only list */
476 if (!(ptr = strrchr( buffer, '\\' )))
478 ptr = buffer;
479 if (*ptr == '*') ptr++;
481 else ptr++;
482 if (is_builtin_only( ptr ))
484 MSGBOXPARAMSA params;
485 params.cbSize = sizeof(params);
486 params.hwndOwner = dialog;
487 params.hInstance = GetModuleHandleA( NULL );
488 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
489 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
490 params.dwStyle = MB_ICONWARNING | MB_YESNO;
491 params.lpszIcon = NULL;
492 params.dwContextHelpId = 0;
493 params.lpfnMsgBoxCallback = NULL;
494 params.dwLanguageId = 0;
495 if (MessageBoxIndirectA( &params ) != IDYES) return;
498 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM)emptyW);
499 disable(IDC_DLLS_ADDDLL);
500 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
502 WINE_TRACE("Adding %s as native, builtin\n", buffer);
504 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
505 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
507 load_library_settings(dialog);
509 SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
511 set_controls_from_selection(dialog);
514 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
516 static WORD sel;
518 switch(uMsg)
520 case WM_INITDIALOG:
521 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
522 sel = lParam;
523 return TRUE;
525 case WM_COMMAND:
526 if(HIWORD(wParam) != BN_CLICKED) break;
527 switch (LOWORD(wParam))
529 case IDC_RAD_BUILTIN:
530 case IDC_RAD_NATIVE:
531 case IDC_RAD_BUILTIN_NATIVE:
532 case IDC_RAD_NATIVE_BUILTIN:
533 case IDC_RAD_DISABLE:
534 sel = LOWORD(wParam);
535 return TRUE;
536 case IDOK:
537 EndDialog(hwndDlg, sel);
538 return TRUE;
539 case IDCANCEL:
540 EndDialog(hwndDlg, wParam);
541 return TRUE;
544 return FALSE;
547 static void on_edit_click(HWND hwnd)
549 INT_PTR ret;
550 int index = SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
551 struct dll *dll;
552 DWORD id;
554 /* if no override is selected the edit button should be disabled... */
555 assert(index != -1);
557 dll = (struct dll *) SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
558 id = mode_to_id(dll->mode);
560 ret = DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
562 if(ret != IDCANCEL)
563 set_dllmode(hwnd, ret);
566 static void on_remove_click(HWND dialog)
568 int sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
569 struct dll *dll;
571 if (sel == LB_ERR) return;
573 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
575 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
577 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
578 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
580 HeapFree(GetProcessHeap(), 0, dll->name);
581 HeapFree(GetProcessHeap(), 0, dll);
583 if (SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
584 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
585 else
587 disable(IDC_DLLS_EDITDLL);
588 disable(IDC_DLLS_REMOVEDLL);
591 set_controls_from_selection(dialog);
594 INT_PTR CALLBACK
595 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
597 switch (uMsg)
599 case WM_INITDIALOG:
600 init_libsheet(hDlg);
601 break;
602 case WM_SHOWWINDOW:
603 set_window_title(hDlg);
604 break;
605 case WM_NOTIFY:
606 switch (((LPNMHDR)lParam)->code) {
607 case PSN_SETACTIVE:
608 load_library_settings(hDlg);
609 break;
611 break;
612 case WM_COMMAND:
613 switch(HIWORD(wParam)) {
614 case CBN_EDITCHANGE:
615 if (LOWORD(wParam) == IDC_DLLCOMBO)
616 on_add_combo_change(hDlg);
617 break;
618 case CBN_SETFOCUS:
619 if (LOWORD(wParam) == IDC_DLLCOMBO)
620 on_add_combo_change(hDlg);
621 break;
622 case CBN_KILLFOCUS:
623 if (LOWORD(wParam) == IDC_DLLCOMBO)
624 SendMessageW(GetParent(hDlg), DM_SETDEFID, IDOK, 0);
625 break;
626 case BN_CLICKED:
627 switch(LOWORD(wParam)) {
628 case IDC_DLLS_ADDDLL:
629 on_add_click(hDlg);
630 break;
631 case IDC_DLLS_EDITDLL:
632 on_edit_click(hDlg);
633 break;
634 case IDC_DLLS_REMOVEDLL:
635 on_remove_click(hDlg);
636 break;
638 break;
639 case LBN_SELCHANGE:
640 if(LOWORD(wParam) == IDC_DLLCOMBO)
641 on_add_combo_change(hDlg);
642 else
643 set_controls_from_selection(hDlg);
644 break;
646 break;
649 return 0;