winspool: Use printenv_t for GetDriverInfoFromReg.
[wine/wine64.git] / programs / winecfg / libraries.c
blob78f694bcebc44a5be92ff87f68564783ddf48843
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"
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_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 "icmp",
57 "iphlpapi",
58 "kernel32",
59 "mswsock",
60 "ntdll",
61 "opengl32",
62 "stdole2.tlb",
63 "stdole32.tlb",
64 "twain_32",
65 "unicows",
66 "user32",
67 "vdmdbg",
68 "w32skrnl",
69 "winealsa.drv",
70 "wineaudioio.drv",
71 "wined3d",
72 "winedos",
73 "wineesd.drv",
74 "winejack.drv",
75 "winejoystick.drv",
76 "winemp3.acm",
77 "winenas.drv",
78 "wineoss.drv",
79 "wineps",
80 "wineps.drv",
81 "winex11.drv",
82 "winmm",
83 "wintab32",
84 "wnaspi32",
85 "wow32",
86 "ws2_32",
87 "wsock32",
90 enum dllmode
92 BUILTIN_NATIVE,
93 NATIVE_BUILTIN,
94 BUILTIN,
95 NATIVE,
96 DISABLE,
97 UNKNOWN /* Special value indicating an erronous DLL override mode */
100 struct dll
102 char *name;
103 enum dllmode mode;
106 /* Convert a registry string to a dllmode */
107 static enum dllmode string_to_mode(char *in)
109 int i, j, len;
110 char *out;
111 enum dllmode res;
113 len = strlen(in);
114 out = HeapAlloc(GetProcessHeap(), 0, len);
116 /* remove the spaces */
117 for (i = j = 0; i <= len; ++i) {
118 if (in[i] != ' ') {
119 out[j++] = in[i];
123 /* parse the string */
124 res = UNKNOWN;
125 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
126 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
127 if (strcmp(out, "builtin") == 0) res = BUILTIN;
128 if (strcmp(out, "native") == 0) res = NATIVE;
129 if (strcmp(out, "") == 0) res = DISABLE;
131 HeapFree(GetProcessHeap(), 0, out);
132 return res;
135 /* Convert a dllmode to a registry string. */
136 static const char* mode_to_string(enum dllmode mode)
138 switch( mode )
140 case NATIVE: return "native";
141 case BUILTIN: return "builtin";
142 case NATIVE_BUILTIN: return "native,builtin";
143 case BUILTIN_NATIVE: return "builtin,native";
144 case DISABLE: return "";
145 default: assert(FALSE); return "";
149 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
150 static const char* mode_to_label(enum dllmode mode)
152 static char buffer[256];
153 UINT id = 0;
155 switch( mode )
157 case NATIVE: id = IDS_DLL_NATIVE; break;
158 case BUILTIN: id = IDS_DLL_BUILTIN; break;
159 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
160 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
161 case DISABLE: id = IDS_DLL_DISABLED; break;
162 default: assert(FALSE);
164 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
165 return buffer;
168 /* Convert a control id (IDC_ constant) to a dllmode */
169 static enum dllmode id_to_mode(DWORD id)
171 switch( id )
173 case IDC_RAD_BUILTIN: return BUILTIN;
174 case IDC_RAD_NATIVE: return NATIVE;
175 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
176 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
177 case IDC_RAD_DISABLE: return DISABLE;
178 default: assert( FALSE ); return 0; /* should not be reached */
182 /* Convert a dllmode to a control id (IDC_ constant) */
183 static DWORD mode_to_id(enum dllmode mode)
185 switch( mode )
187 case BUILTIN: return IDC_RAD_BUILTIN;
188 case NATIVE: return IDC_RAD_NATIVE;
189 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
190 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
191 case DISABLE: return IDC_RAD_DISABLE;
192 default: assert( FALSE ); return 0; /* should not be reached */
196 /* helper for is_builtin_only */
197 static int compare_dll( const void *ptr1, const void *ptr2 )
199 const char * const *name1 = ptr1;
200 const char * const *name2 = ptr2;
201 return strcmp( *name1, *name2 );
204 /* check if dll is recommended as builtin only */
205 static inline int is_builtin_only( const char *name )
207 return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
208 sizeof(builtin_only[0]), compare_dll ) != NULL;
211 static void set_controls_from_selection(HWND dialog)
213 /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
216 static void clear_settings(HWND dialog)
218 int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
219 int i;
221 WINE_TRACE("count=%d\n", count);
223 for (i = 0; i < count; i++)
225 struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
227 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
229 HeapFree(GetProcessHeap(), 0, dll->name);
230 HeapFree(GetProcessHeap(), 0, dll);
234 /* check if a given dll is 16-bit */
235 static int is_16bit_dll( const char *dir, const char *name )
237 char buffer[64];
238 int res;
239 size_t len = strlen(dir) + strlen(name) + 2;
240 char *path = HeapAlloc( GetProcessHeap(), 0, len );
242 strcpy( path, dir );
243 strcat( path, "/" );
244 strcat( path, name );
245 res = readlink( path, buffer, sizeof(buffer) );
246 HeapFree( GetProcessHeap(), 0, path );
248 if (res == -1) return 0; /* not a symlink */
249 if (res < 4 || res >= sizeof(buffer)) return 0;
250 buffer[res] = 0;
251 if (strchr( buffer, '/' )) return 0; /* contains a path, not valid */
252 if (strcmp( buffer + res - 3, ".so" )) return 0; /* does not end in .so, not valid */
253 return 1;
256 /* load the list of available libraries from a given dir */
257 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
259 char *buffer = NULL, name[256];
260 struct dirent *de;
261 DIR *dir = opendir( dir_path );
263 if (!dir) return;
265 if (check_subdirs)
266 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
268 while ((de = readdir( dir )))
270 size_t len = strlen(de->d_name);
271 if (len > sizeof(name)) continue;
272 if (len > 7 && !strcmp( de->d_name + len - 7, ".dll.so"))
274 if (is_16bit_dll( dir_path, de->d_name )) continue; /* 16-bit dlls can't be configured */
275 len -= 7;
276 memcpy( name, de->d_name, len );
277 name[len] = 0;
278 /* skip dlls that should always be builtin */
279 if (is_builtin_only( name )) continue;
280 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
282 else if (check_subdirs)
284 struct stat st;
285 if (is_builtin_only( de->d_name )) continue;
286 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
287 if (!stat( buffer, &st ))
288 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
291 closedir( dir );
292 HeapFree( GetProcessHeap(), 0, buffer );
295 /* load the list of available libraries */
296 static void load_library_list( HWND dialog )
298 unsigned int i = 0;
299 const char *path, *build_dir = wine_get_build_dir();
300 char item1[256], item2[256];
301 HCURSOR old_cursor = SetCursor( LoadCursor(0, IDC_WAIT) );
303 if (build_dir)
305 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
306 strcpy( dir, build_dir );
307 strcat( dir, "/dlls" );
308 load_library_list_from_dir( dialog, dir, TRUE );
309 HeapFree( GetProcessHeap(), 0, dir );
312 while ((path = wine_dll_enum_load_path( i++ )))
313 load_library_list_from_dir( dialog, path, FALSE );
315 /* get rid of duplicate entries */
317 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
318 i = 1;
319 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
321 if (!strcmp( item1, item2 ))
323 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
325 else
327 strcpy( item1, item2 );
328 i++;
331 SetCursor( old_cursor );
334 static void load_library_settings(HWND dialog)
336 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
337 char **p;
338 int sel, count = 0;
340 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
342 WINE_TRACE("sel=%d\n", sel);
344 clear_settings(dialog);
346 if (!overrides || *overrides == NULL)
348 set_controls_from_selection(dialog);
349 disable(IDC_DLLS_EDITDLL);
350 disable(IDC_DLLS_REMOVEDLL);
351 HeapFree(GetProcessHeap(), 0, overrides);
352 return;
355 enable(IDC_DLLS_EDITDLL);
356 enable(IDC_DLLS_REMOVEDLL);
358 for (p = overrides; *p != NULL; p++)
360 int index;
361 char *str, *value;
362 const char *label;
363 struct dll *dll;
365 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
367 label = mode_to_label(string_to_mode(value));
369 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
370 strcpy(str, *p);
371 strcat(str, " (");
372 strcat(str, label);
373 strcat(str, ")");
375 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
376 dll->name = *p;
377 dll->mode = string_to_mode(value);
379 index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
380 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
382 HeapFree(GetProcessHeap(), 0, str);
384 count++;
387 HeapFree(GetProcessHeap(), 0, overrides);
389 /* restore the previous selection, if possible */
390 if (sel >= count - 1) sel = count - 1;
391 else if (sel == -1) sel = 0;
393 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
395 set_controls_from_selection(dialog);
398 /* Called when the application is initialized (cannot reinit!) */
399 static void init_libsheet(HWND dialog)
401 /* clear the add dll controls */
402 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
403 load_library_list( dialog );
404 disable(IDC_DLLS_ADDDLL);
407 static void on_add_combo_change(HWND dialog)
409 char buffer[1024];
411 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
413 if (strlen(buffer))
414 enable(IDC_DLLS_ADDDLL)
415 else
416 disable(IDC_DLLS_ADDDLL);
419 static void set_dllmode(HWND dialog, DWORD id)
421 enum dllmode mode;
422 struct dll *dll;
423 int sel;
424 const char *str;
426 mode = id_to_mode(id);
428 sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
429 if (sel == -1) return;
431 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
433 str = mode_to_string(mode);
434 WINE_TRACE("Setting %s to %s\n", dll->name, str);
436 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
437 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
439 load_library_settings(dialog); /* ... and refresh */
442 static void on_add_click(HWND dialog)
444 static const char dotDll[] = ".dll";
445 char buffer[1024], *ptr;
447 ZeroMemory(buffer, sizeof(buffer));
449 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
450 if (lstrlenA(buffer) >= sizeof(dotDll))
452 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
453 if (!lstrcmpiA(ptr, dotDll))
455 WINE_TRACE("Stripping dll extension\n");
456 *ptr = '\0';
460 /* check if dll is in the builtin-only list */
461 if (!(ptr = strrchr( buffer, '\\' )))
463 ptr = buffer;
464 if (*ptr == '*') ptr++;
466 else ptr++;
467 if (is_builtin_only( ptr ))
469 MSGBOXPARAMSA params;
470 params.cbSize = sizeof(params);
471 params.hwndOwner = dialog;
472 params.hInstance = GetModuleHandleA( NULL );
473 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
474 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
475 params.dwStyle = MB_ICONWARNING | MB_YESNO;
476 params.lpszIcon = NULL;
477 params.dwContextHelpId = 0;
478 params.lpfnMsgBoxCallback = NULL;
479 params.dwLanguageId = 0;
480 if (MessageBoxIndirectA( &params ) != IDYES) return;
483 SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
484 disable(IDC_DLLS_ADDDLL);
486 WINE_TRACE("Adding %s as native, builtin\n", buffer);
488 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
489 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
491 load_library_settings(dialog);
493 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, (WPARAM) 0, (LPARAM) buffer);
495 set_controls_from_selection(dialog);
498 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
500 static WORD sel;
502 switch(uMsg)
504 case WM_INITDIALOG:
505 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
506 sel = lParam;
507 return TRUE;
509 case WM_COMMAND:
510 if(HIWORD(wParam) != BN_CLICKED) break;
511 switch (LOWORD(wParam))
513 case IDC_RAD_BUILTIN:
514 case IDC_RAD_NATIVE:
515 case IDC_RAD_BUILTIN_NATIVE:
516 case IDC_RAD_NATIVE_BUILTIN:
517 case IDC_RAD_DISABLE:
518 sel = LOWORD(wParam);
519 return TRUE;
520 case IDOK:
521 EndDialog(hwndDlg, sel);
522 return TRUE;
523 case IDCANCEL:
524 EndDialog(hwndDlg, wParam);
525 return TRUE;
528 return FALSE;
531 static void on_edit_click(HWND hwnd)
533 INT_PTR ret;
534 int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
535 struct dll *dll;
536 DWORD id;
538 /* if no override is selected the edit button should be disabled... */
539 assert(index != -1);
541 dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
542 id = mode_to_id(dll->mode);
544 ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
546 if(ret != IDCANCEL)
547 set_dllmode(hwnd, ret);
550 static void on_remove_click(HWND dialog)
552 int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
553 struct dll *dll;
555 if (sel == LB_ERR) return;
557 dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
559 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
561 SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
562 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
564 HeapFree(GetProcessHeap(), 0, dll->name);
565 HeapFree(GetProcessHeap(), 0, dll);
567 if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
568 SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
569 else
571 disable(IDC_DLLS_EDITDLL);
572 disable(IDC_DLLS_REMOVEDLL);
575 set_controls_from_selection(dialog);
578 INT_PTR CALLBACK
579 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
581 switch (uMsg)
583 case WM_INITDIALOG:
584 init_libsheet(hDlg);
585 break;
586 case WM_SHOWWINDOW:
587 set_window_title(hDlg);
588 break;
589 case WM_NOTIFY:
590 switch (((LPNMHDR)lParam)->code) {
591 case PSN_SETACTIVE:
592 load_library_settings(hDlg);
593 break;
595 break;
596 case WM_COMMAND:
597 switch(HIWORD(wParam)) {
599 /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
600 * add button, rather than the propsheet OK button. But I don't know how to do that!
603 case CBN_EDITCHANGE:
604 if(LOWORD(wParam) == IDC_DLLCOMBO)
606 on_add_combo_change(hDlg);
607 break;
610 case BN_CLICKED:
611 switch(LOWORD(wParam)) {
612 case IDC_DLLS_ADDDLL:
613 on_add_click(hDlg);
614 break;
615 case IDC_DLLS_EDITDLL:
616 on_edit_click(hDlg);
617 break;
618 case IDC_DLLS_REMOVEDLL:
619 on_remove_click(hDlg);
620 break;
622 break;
623 case LBN_SELCHANGE:
624 if(LOWORD(wParam) == IDC_DLLCOMBO)
625 on_add_combo_change(hDlg);
626 else
627 set_controls_from_selection(hDlg);
628 break;
630 break;
633 return 0;