psapi: Remove unneeded cast.
[wine/hacks.git] / programs / winecfg / libraries.c
blob8e4e46bbb7c759b73da366cd9d03f7d78ab53a06
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 "winex11.drv",
83 "winmm",
84 "wintab32",
85 "wnaspi32",
86 "wow32",
87 "ws2_32",
88 "wsock32",
91 enum dllmode
93 BUILTIN_NATIVE,
94 NATIVE_BUILTIN,
95 BUILTIN,
96 NATIVE,
97 DISABLE,
98 UNKNOWN /* Special value indicating an erronous DLL override mode */
101 struct dll
103 char *name;
104 enum dllmode mode;
107 /* Convert a registry string to a dllmode */
108 static enum dllmode string_to_mode(char *in)
110 int i, j, len;
111 char *out;
112 enum dllmode res;
114 len = strlen(in);
115 out = HeapAlloc(GetProcessHeap(), 0, len);
117 /* remove the spaces */
118 for (i = j = 0; i <= len; ++i) {
119 if (in[i] != ' ') {
120 out[j++] = in[i];
124 /* parse the string */
125 res = UNKNOWN;
126 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
127 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
128 if (strcmp(out, "builtin") == 0) res = BUILTIN;
129 if (strcmp(out, "native") == 0) res = NATIVE;
130 if (strcmp(out, "") == 0) res = DISABLE;
132 HeapFree(GetProcessHeap(), 0, out);
133 return res;
136 /* Convert a dllmode to a registry string. */
137 static const char* mode_to_string(enum dllmode mode)
139 switch( mode )
141 case NATIVE: return "native";
142 case BUILTIN: return "builtin";
143 case NATIVE_BUILTIN: return "native,builtin";
144 case BUILTIN_NATIVE: return "builtin,native";
145 case DISABLE: return "";
146 default: assert(FALSE); return "";
150 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
151 static const char* mode_to_label(enum dllmode mode)
153 static char buffer[256];
154 UINT id = 0;
156 switch( mode )
158 case NATIVE: id = IDS_DLL_NATIVE; break;
159 case BUILTIN: id = IDS_DLL_BUILTIN; break;
160 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
161 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
162 case DISABLE: id = IDS_DLL_DISABLED; break;
163 default: assert(FALSE);
165 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
166 return buffer;
169 /* Convert a control id (IDC_ constant) to a dllmode */
170 static enum dllmode id_to_mode(DWORD id)
172 switch( id )
174 case IDC_RAD_BUILTIN: return BUILTIN;
175 case IDC_RAD_NATIVE: return NATIVE;
176 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
177 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
178 case IDC_RAD_DISABLE: return DISABLE;
179 default: assert( FALSE ); return 0; /* should not be reached */
183 /* Convert a dllmode to a control id (IDC_ constant) */
184 static DWORD mode_to_id(enum dllmode mode)
186 switch( mode )
188 case BUILTIN: return IDC_RAD_BUILTIN;
189 case NATIVE: return IDC_RAD_NATIVE;
190 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
191 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
192 case DISABLE: return IDC_RAD_DISABLE;
193 default: assert( FALSE ); return 0; /* should not be reached */
197 /* helper for is_builtin_only */
198 static int compare_dll( const void *ptr1, const void *ptr2 )
200 const char * const *name1 = ptr1;
201 const char * const *name2 = ptr2;
202 return strcmp( *name1, *name2 );
205 /* check if dll is recommended as builtin only */
206 static inline int is_builtin_only( const char *name )
208 return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
209 sizeof(builtin_only[0]), compare_dll ) != NULL;
212 static void set_controls_from_selection(HWND dialog)
214 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
217 static void clear_settings(HWND dialog)
219 int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
220 int i;
222 WINE_TRACE("count=%d\n", count);
224 for (i = 0; i < count; i++)
226 struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
228 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
230 HeapFree(GetProcessHeap(), 0, dll->name);
231 HeapFree(GetProcessHeap(), 0, dll);
235 /* check if a given dll is 16-bit */
236 static int is_16bit_dll( const char *dir, const char *name )
238 char buffer[64];
239 int res;
240 size_t len = strlen(dir) + strlen(name) + 2;
241 char *path = HeapAlloc( GetProcessHeap(), 0, len );
243 strcpy( path, dir );
244 strcat( path, "/" );
245 strcat( path, name );
246 res = readlink( path, buffer, sizeof(buffer) );
247 HeapFree( GetProcessHeap(), 0, path );
249 if (res == -1) return 0; /* not a symlink */
250 if (res < 4 || res >= sizeof(buffer)) return 0;
251 buffer[res] = 0;
252 if (strchr( buffer, '/' )) return 0; /* contains a path, not valid */
253 if (strcmp( buffer + res - 3, ".so" )) return 0; /* does not end in .so, not valid */
254 return 1;
257 /* load the list of available libraries from a given dir */
258 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
260 char *buffer = NULL, name[256];
261 struct dirent *de;
262 DIR *dir = opendir( dir_path );
264 if (!dir) return;
266 if (check_subdirs)
267 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
269 while ((de = readdir( dir )))
271 size_t len = strlen(de->d_name);
272 if (len > sizeof(name)) continue;
273 if (len > 7 && !strcmp( de->d_name + len - 7, ".dll.so"))
275 if (is_16bit_dll( dir_path, de->d_name )) continue; /* 16-bit dlls can't be configured */
276 len -= 7;
277 memcpy( name, de->d_name, len );
278 name[len] = 0;
279 /* skip dlls that should always be builtin */
280 if (is_builtin_only( name )) continue;
281 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
283 else if (check_subdirs)
285 struct stat st;
286 if (is_builtin_only( de->d_name )) continue;
287 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
288 if (!stat( buffer, &st ))
289 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
292 closedir( dir );
293 HeapFree( GetProcessHeap(), 0, buffer );
296 /* load the list of available libraries */
297 static void load_library_list( HWND dialog )
299 unsigned int i = 0;
300 const char *path, *build_dir = wine_get_build_dir();
301 char item1[256], item2[256];
302 HCURSOR old_cursor = SetCursor( LoadCursor(0, IDC_WAIT) );
304 if (build_dir)
306 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
307 strcpy( dir, build_dir );
308 strcat( dir, "/dlls" );
309 load_library_list_from_dir( dialog, dir, TRUE );
310 HeapFree( GetProcessHeap(), 0, dir );
313 while ((path = wine_dll_enum_load_path( i++ )))
314 load_library_list_from_dir( dialog, path, FALSE );
316 /* get rid of duplicate entries */
318 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
319 i = 1;
320 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
322 if (!strcmp( item1, item2 ))
324 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
326 else
328 strcpy( item1, item2 );
329 i++;
332 SetCursor( old_cursor );
335 static void load_library_settings(HWND dialog)
337 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
338 char **p;
339 int sel, count = 0;
341 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
343 WINE_TRACE("sel=%d\n", sel);
345 clear_settings(dialog);
347 if (!overrides || *overrides == NULL)
349 set_controls_from_selection(dialog);
350 disable(IDC_DLLS_EDITDLL);
351 disable(IDC_DLLS_REMOVEDLL);
352 HeapFree(GetProcessHeap(), 0, overrides);
353 return;
356 enable(IDC_DLLS_EDITDLL);
357 enable(IDC_DLLS_REMOVEDLL);
359 for (p = overrides; *p != NULL; p++)
361 int index;
362 char *str, *value;
363 const char *label;
364 struct dll *dll;
366 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
368 label = mode_to_label(string_to_mode(value));
370 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
371 strcpy(str, *p);
372 strcat(str, " (");
373 strcat(str, label);
374 strcat(str, ")");
376 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
377 dll->name = *p;
378 dll->mode = string_to_mode(value);
380 index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
381 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
383 HeapFree(GetProcessHeap(), 0, str);
385 count++;
388 HeapFree(GetProcessHeap(), 0, overrides);
390 /* restore the previous selection, if possible */
391 if (sel >= count - 1) sel = count - 1;
392 else if (sel == -1) sel = 0;
394 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
396 set_controls_from_selection(dialog);
399 /* Called when the application is initialized (cannot reinit!) */
400 static void init_libsheet(HWND dialog)
402 /* clear the add dll controls */
403 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
404 load_library_list( dialog );
405 disable(IDC_DLLS_ADDDLL);
408 static void on_add_combo_change(HWND dialog)
410 char buffer[1024];
412 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
414 if (strlen(buffer))
415 enable(IDC_DLLS_ADDDLL)
416 else
417 disable(IDC_DLLS_ADDDLL);
420 static void set_dllmode(HWND dialog, DWORD id)
422 enum dllmode mode;
423 struct dll *dll;
424 int sel;
425 const char *str;
427 mode = id_to_mode(id);
429 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
430 if (sel == -1) return;
432 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
434 str = mode_to_string(mode);
435 WINE_TRACE("Setting %s to %s\n", dll->name, str);
437 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
438 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
440 load_library_settings(dialog); /* ... and refresh */
443 static void on_add_click(HWND dialog)
445 static const char dotDll[] = ".dll";
446 char buffer[1024], *ptr;
448 ZeroMemory(buffer, sizeof(buffer));
450 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
451 if (lstrlenA(buffer) >= sizeof(dotDll))
453 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
454 if (!lstrcmpiA(ptr, dotDll))
456 WINE_TRACE("Stripping dll extension\n");
457 *ptr = '\0';
461 /* check if dll is in the builtin-only list */
462 if (!(ptr = strrchr( buffer, '\\' )))
464 ptr = buffer;
465 if (*ptr == '*') ptr++;
467 else ptr++;
468 if (is_builtin_only( ptr ))
470 MSGBOXPARAMSA params;
471 params.cbSize = sizeof(params);
472 params.hwndOwner = dialog;
473 params.hInstance = GetModuleHandleA( NULL );
474 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
475 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
476 params.dwStyle = MB_ICONWARNING | MB_YESNO;
477 params.lpszIcon = NULL;
478 params.dwContextHelpId = 0;
479 params.lpfnMsgBoxCallback = NULL;
480 params.dwLanguageId = 0;
481 if (MessageBoxIndirectA( &params ) != IDYES) return;
484 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
485 disable(IDC_DLLS_ADDDLL);
487 WINE_TRACE("Adding %s as native, builtin\n", buffer);
489 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
490 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
492 load_library_settings(dialog);
494 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, (WPARAM) 0, (LPARAM) buffer);
496 set_controls_from_selection(dialog);
499 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
501 static WORD sel;
503 switch(uMsg)
505 case WM_INITDIALOG:
506 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
507 sel = lParam;
508 return TRUE;
510 case WM_COMMAND:
511 if(HIWORD(wParam) != BN_CLICKED) break;
512 switch (LOWORD(wParam))
514 case IDC_RAD_BUILTIN:
515 case IDC_RAD_NATIVE:
516 case IDC_RAD_BUILTIN_NATIVE:
517 case IDC_RAD_NATIVE_BUILTIN:
518 case IDC_RAD_DISABLE:
519 sel = LOWORD(wParam);
520 return TRUE;
521 case IDOK:
522 EndDialog(hwndDlg, sel);
523 return TRUE;
524 case IDCANCEL:
525 EndDialog(hwndDlg, wParam);
526 return TRUE;
529 return FALSE;
532 static void on_edit_click(HWND hwnd)
534 INT_PTR ret;
535 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
536 struct dll *dll;
537 DWORD id;
539 /* if no override is selected the edit button should be disabled... */
540 assert(index != -1);
542 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
543 id = mode_to_id(dll->mode);
545 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
547 if(ret != IDCANCEL)
548 set_dllmode(hwnd, ret);
551 static void on_remove_click(HWND dialog)
553 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
554 struct dll *dll;
556 if (sel == LB_ERR) return;
558 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
560 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
562 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
563 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
565 HeapFree(GetProcessHeap(), 0, dll->name);
566 HeapFree(GetProcessHeap(), 0, dll);
568 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
569 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
570 else
572 disable(IDC_DLLS_EDITDLL);
573 disable(IDC_DLLS_REMOVEDLL);
576 set_controls_from_selection(dialog);
579 INT_PTR CALLBACK
580 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
582 switch (uMsg)
584 case WM_INITDIALOG:
585 init_libsheet(hDlg);
586 break;
587 case WM_SHOWWINDOW:
588 set_window_title(hDlg);
589 break;
590 case WM_NOTIFY:
591 switch (((LPNMHDR)lParam)->code) {
592 case PSN_SETACTIVE:
593 load_library_settings(hDlg);
594 break;
596 break;
597 case WM_COMMAND:
598 switch(HIWORD(wParam)) {
600 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
601 * add button, rather than the propsheet OK button. But I don't know how to do that!
604 case CBN_EDITCHANGE:
605 if(LOWORD(wParam) == IDC_DLLCOMBO)
607 on_add_combo_change(hDlg);
608 break;
611 case BN_CLICKED:
612 switch(LOWORD(wParam)) {
613 case IDC_DLLS_ADDDLL:
614 on_add_click(hDlg);
615 break;
616 case IDC_DLLS_EDITDLL:
617 on_edit_click(hDlg);
618 break;
619 case IDC_DLLS_REMOVEDLL:
620 on_remove_click(hDlg);
621 break;
623 break;
624 case LBN_SELCHANGE:
625 if(LOWORD(wParam) == IDC_DLLCOMBO)
626 on_add_combo_change(hDlg);
627 else
628 set_controls_from_selection(hDlg);
629 break;
631 break;
634 return 0;