gdiplus/metafile: Implement DrawArc() recording.
[wine/zf.git] / programs / winecfg / libraries.c
blobae66509c9d0e17a63b46b9056434f9b101a3634c
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/debug.h>
30 #include <stdio.h>
31 #include <dirent.h>
32 #include <assert.h>
33 #include <stdlib.h>
34 #ifdef HAVE_SYS_STAT_H
35 #include <sys/stat.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
41 #include "winecfg.h"
42 #include "resource.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
46 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
47 static const char * const builtin_only[] =
49 "advapi32",
50 "capi2032",
51 "dbghelp",
52 "ddraw",
53 "gdi32",
54 "gphoto2.ds",
55 "icmp",
56 "iphlpapi",
57 "kernel32",
58 "l3codeca.acm",
59 "mountmgr.sys",
60 "mswsock",
61 "ntdll",
62 "ntoskrnl.exe",
63 "opengl32",
64 "sane.ds",
65 "secur32",
66 "twain_32",
67 "unicows",
68 "user32",
69 "vdmdbg",
70 "w32skrnl",
71 "winmm",
72 "wintab32",
73 "wnaspi32",
74 "wow32",
75 "ws2_32",
76 "wsock32",
79 enum dllmode
81 BUILTIN_NATIVE,
82 NATIVE_BUILTIN,
83 BUILTIN,
84 NATIVE,
85 DISABLE,
86 UNKNOWN /* Special value indicating an erroneous DLL override mode */
89 struct dll
91 char *name;
92 enum dllmode mode;
95 static const WCHAR emptyW[1];
97 /* Convert a registry string to a dllmode */
98 static enum dllmode string_to_mode(char *in)
100 int i, j, len;
101 char *out;
102 enum dllmode res;
104 len = strlen(in);
105 out = HeapAlloc(GetProcessHeap(), 0, len + 1);
107 /* remove the spaces */
108 for (i = j = 0; i <= len; ++i) {
109 if (in[i] != ' ') {
110 out[j++] = in[i];
114 /* parse the string */
115 res = UNKNOWN;
116 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
117 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
118 if (strcmp(out, "builtin") == 0) res = BUILTIN;
119 if (strcmp(out, "native") == 0) res = NATIVE;
120 if (strcmp(out, "") == 0) res = DISABLE;
122 HeapFree(GetProcessHeap(), 0, out);
123 return res;
126 /* Convert a dllmode to a registry string. */
127 static const char* mode_to_string(enum dllmode mode)
129 switch( mode )
131 case NATIVE: return "native";
132 case BUILTIN: return "builtin";
133 case NATIVE_BUILTIN: return "native,builtin";
134 case BUILTIN_NATIVE: return "builtin,native";
135 case DISABLE: return "";
136 default: return "";
140 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
141 static const char* mode_to_label(enum dllmode mode)
143 static char buffer[256];
144 UINT id = 0;
146 switch( mode )
148 case NATIVE: id = IDS_DLL_NATIVE; break;
149 case BUILTIN: id = IDS_DLL_BUILTIN; break;
150 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
151 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
152 case DISABLE: id = IDS_DLL_DISABLED; break;
153 default: return "??";
155 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
156 return buffer;
159 /* Convert a control id (IDC_ constant) to a dllmode */
160 static enum dllmode id_to_mode(DWORD id)
162 switch( id )
164 case IDC_RAD_BUILTIN: return BUILTIN;
165 case IDC_RAD_NATIVE: return NATIVE;
166 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
167 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
168 case IDC_RAD_DISABLE: return DISABLE;
169 default: assert( FALSE ); return 0; /* should not be reached */
173 /* Convert a dllmode to a control id (IDC_ constant) */
174 static DWORD mode_to_id(enum dllmode mode)
176 switch( mode )
178 case BUILTIN: return IDC_RAD_BUILTIN;
179 case NATIVE: return IDC_RAD_NATIVE;
180 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
181 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
182 case DISABLE: return IDC_RAD_DISABLE;
183 default: return IDC_RAD_BUILTIN_NATIVE;
187 /* helper for is_builtin_only */
188 static int compare_dll( const void *ptr1, const void *ptr2 )
190 const char * const *name1 = ptr1;
191 const char * const *name2 = ptr2;
192 return strcmp( *name1, *name2 );
195 /* check if dll is recommended as builtin only */
196 static inline BOOL is_builtin_only( const char *name )
198 const char *ext = strrchr( name, '.' );
200 if (ext)
202 if (!strcmp( ext, ".vxd" ) ||
203 !strcmp( ext, ".drv" ) ||
204 !strcmp( ext, ".tlb" ))
205 return TRUE;
207 if (!strncmp( name, "wine", 4 )) return TRUE;
208 return bsearch( &name, builtin_only, ARRAY_SIZE(builtin_only),
209 sizeof(builtin_only[0]), compare_dll ) != NULL;
212 /* check if dll should be offered in the drop-down list */
213 static BOOL show_dll_in_list( const char *name )
215 const char *ext = strrchr( name, '.' );
217 if (ext)
219 /* skip 16-bit dlls */
220 if (strlen(ext) > 2 && !strcmp( ext + strlen(ext) - 2, "16" )) return FALSE;
221 /* skip exes */
222 if (!strcmp( ext, ".exe" )) return FALSE;
224 /* skip api set placeholders */
225 if (!strncmp( name, "api-ms-", 7 ) || !strncmp( name, "ext-ms-", 7 )) return FALSE;
226 /* skip dlls that should always be builtin */
227 return !is_builtin_only( name );
230 static void set_controls_from_selection(HWND dialog)
232 /* FIXME: display/update some information about the selected dll (purpose, recommended load order) maybe? */
235 static void clear_settings(HWND dialog)
237 int count = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
238 int i;
240 WINE_TRACE("count=%d\n", count);
242 for (i = 0; i < count; i++)
244 struct dll *dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
246 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
247 HeapFree(GetProcessHeap(), 0, dll->name);
248 HeapFree(GetProcessHeap(), 0, dll);
252 /* load the list of available libraries from a given dir */
253 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
255 static const char * const ext[] = { ".dll", ".dll.so", ".so", "" };
256 char *buffer, *p, name[256];
257 unsigned int i;
258 HANDLE handle;
259 WIN32_FIND_DATAA data;
261 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
263 strcpy( buffer, dir_path );
264 strcat( buffer, "\\*" );
265 buffer[1] = '\\'; /* change \??\ to \\?\ */
266 p = buffer + strlen(buffer) - 1;
268 if ((handle = FindFirstFileA( buffer, &data )) == INVALID_HANDLE_VALUE)
270 HeapFree( GetProcessHeap(), 0, buffer );
271 return;
276 size_t len = strlen(data.cFileName);
277 if (len > sizeof(name)) continue;
278 if (check_subdirs)
280 if (!strcmp( data.cFileName, "." )) continue;
281 if (!strcmp( data.cFileName, ".." )) continue;
282 if (!show_dll_in_list( data.cFileName )) continue;
283 for (i = 0; i < ARRAY_SIZE( ext ); i++)
285 sprintf( p, "%s\\%s%s", data.cFileName, data.cFileName, ext[i] );
286 if (GetFileAttributesA( buffer ) != INVALID_FILE_ATTRIBUTES)
288 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)data.cFileName );
289 break;
293 else
295 for (i = 0; i < ARRAY_SIZE( ext ); i++)
297 if (!ext[i][0]) continue;
298 if (len > strlen(ext[i]) && !strcmp( data.cFileName + len - strlen(ext[i]), ext[i]))
300 len -= strlen( ext[i] );
301 memcpy( name, data.cFileName, len );
302 name[len] = 0;
303 if (!show_dll_in_list( name )) continue;
304 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
308 } while (FindNextFileA( handle, &data ));
310 FindClose( handle );
311 HeapFree( GetProcessHeap(), 0, buffer );
314 /* load the list of available libraries */
315 static void load_library_list( HWND dialog )
317 unsigned int i = 0;
318 char item1[256], item2[256], var[32], path[MAX_PATH];
319 HCURSOR old_cursor = SetCursor( LoadCursorW(0, (LPWSTR)IDC_WAIT) );
321 if (GetEnvironmentVariableA( "WINEBUILDDIR", path, MAX_PATH ))
323 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(path) + sizeof("\\dlls") );
324 strcpy( dir, path );
325 strcat( dir, "\\dlls" );
326 load_library_list_from_dir( dialog, dir, TRUE );
327 HeapFree( GetProcessHeap(), 0, dir );
330 for (;;)
332 sprintf( var, "WINEDLLDIR%u", i++ );
333 if (!GetEnvironmentVariableA( var, path, MAX_PATH )) break;
334 load_library_list_from_dir( dialog, path, FALSE );
337 /* get rid of duplicate entries */
339 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
340 i = 1;
341 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
343 if (!strcmp( item1, item2 ))
345 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
347 else
349 strcpy( item1, item2 );
350 i++;
353 SetCursor( old_cursor );
356 static void load_library_settings(HWND dialog)
358 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
359 char **p;
360 int sel, count = 0;
362 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
364 WINE_TRACE("sel=%d\n", sel);
366 clear_settings(dialog);
368 if (!overrides || *overrides == NULL)
370 set_controls_from_selection(dialog);
371 disable(IDC_DLLS_EDITDLL);
372 disable(IDC_DLLS_REMOVEDLL);
373 HeapFree(GetProcessHeap(), 0, overrides);
374 return;
377 enable(IDC_DLLS_EDITDLL);
378 enable(IDC_DLLS_REMOVEDLL);
380 for (p = overrides; *p != NULL; p++)
382 int index;
383 char *str, *value;
384 const char *label;
385 struct dll *dll;
387 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
389 label = mode_to_label(string_to_mode(value));
391 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
392 strcpy(str, *p);
393 strcat(str, " (");
394 strcat(str, label);
395 strcat(str, ")");
397 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
398 dll->name = *p;
399 dll->mode = string_to_mode(value);
401 index = SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
402 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
404 HeapFree(GetProcessHeap(), 0, str);
406 count++;
409 HeapFree(GetProcessHeap(), 0, overrides);
411 /* restore the previous selection, if possible */
412 if (sel >= count - 1) sel = count - 1;
413 else if (sel == -1) sel = 0;
415 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
417 set_controls_from_selection(dialog);
420 /* Called when the application is initialized (cannot reinit!) */
421 static void init_libsheet(HWND dialog)
423 /* clear the add dll controls */
424 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM)emptyW);
425 load_library_list( dialog );
426 disable(IDC_DLLS_ADDDLL);
429 static void on_add_combo_change(HWND dialog)
431 WCHAR buffer[1024];
432 int sel, len;
434 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_GETTEXT, ARRAY_SIZE(buffer), (LPARAM)buffer);
435 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
436 sel=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
437 len=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
439 if (buffer[0] || len>0)
441 enable(IDC_DLLS_ADDDLL)
442 SendMessageW(GetParent(dialog), DM_SETDEFID, IDC_DLLS_ADDDLL, 0);
444 else
446 disable(IDC_DLLS_ADDDLL);
447 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
451 static void set_dllmode(HWND dialog, DWORD id)
453 enum dllmode mode;
454 struct dll *dll;
455 int sel;
456 const char *str;
458 mode = id_to_mode(id);
460 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
461 if (sel == -1) return;
463 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
465 str = mode_to_string(mode);
466 WINE_TRACE("Setting %s to %s\n", dll->name, str);
468 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
469 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
471 load_library_settings(dialog); /* ... and refresh */
474 static void on_add_click(HWND dialog)
476 static const char dotDll[] = ".dll";
477 char buffer[1024], *ptr;
479 ZeroMemory(buffer, sizeof(buffer));
481 SendDlgItemMessageA(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
482 if (lstrlenA(buffer) >= sizeof(dotDll))
484 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
485 if (!lstrcmpiA(ptr, dotDll))
487 WINE_TRACE("Stripping dll extension\n");
488 *ptr = '\0';
492 /* check if dll is in the builtin-only list */
493 if (!(ptr = strrchr( buffer, '\\' )))
495 ptr = buffer;
496 if (*ptr == '*') ptr++;
498 else ptr++;
499 if (is_builtin_only( ptr ))
501 MSGBOXPARAMSA params;
502 params.cbSize = sizeof(params);
503 params.hwndOwner = dialog;
504 params.hInstance = GetModuleHandleA( NULL );
505 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
506 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
507 params.dwStyle = MB_ICONWARNING | MB_YESNO;
508 params.lpszIcon = NULL;
509 params.dwContextHelpId = 0;
510 params.lpfnMsgBoxCallback = NULL;
511 params.dwLanguageId = 0;
512 if (MessageBoxIndirectA( &params ) != IDYES) return;
515 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM)emptyW);
516 disable(IDC_DLLS_ADDDLL);
517 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
519 WINE_TRACE("Adding %s as native, builtin\n", buffer);
521 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
522 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
524 load_library_settings(dialog);
526 SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
528 set_controls_from_selection(dialog);
531 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
533 static WORD sel;
535 switch(uMsg)
537 case WM_INITDIALOG:
538 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
539 sel = lParam;
540 return TRUE;
542 case WM_COMMAND:
543 if(HIWORD(wParam) != BN_CLICKED) break;
544 switch (LOWORD(wParam))
546 case IDC_RAD_BUILTIN:
547 case IDC_RAD_NATIVE:
548 case IDC_RAD_BUILTIN_NATIVE:
549 case IDC_RAD_NATIVE_BUILTIN:
550 case IDC_RAD_DISABLE:
551 sel = LOWORD(wParam);
552 return TRUE;
553 case IDOK:
554 EndDialog(hwndDlg, sel);
555 return TRUE;
556 case IDCANCEL:
557 EndDialog(hwndDlg, wParam);
558 return TRUE;
561 return FALSE;
564 static void on_edit_click(HWND hwnd)
566 INT_PTR ret;
567 int index = SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
568 struct dll *dll;
569 DWORD id;
571 /* if no override is selected the edit button should be disabled... */
572 assert(index != -1);
574 dll = (struct dll *) SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
575 id = mode_to_id(dll->mode);
577 ret = DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
579 if(ret != IDCANCEL)
580 set_dllmode(hwnd, ret);
583 static void on_remove_click(HWND dialog)
585 int sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
586 struct dll *dll;
588 if (sel == LB_ERR) return;
590 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
592 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
594 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
595 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
597 HeapFree(GetProcessHeap(), 0, dll->name);
598 HeapFree(GetProcessHeap(), 0, dll);
600 if (SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
601 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
602 else
604 disable(IDC_DLLS_EDITDLL);
605 disable(IDC_DLLS_REMOVEDLL);
608 set_controls_from_selection(dialog);
611 INT_PTR CALLBACK
612 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
614 switch (uMsg)
616 case WM_INITDIALOG:
617 init_libsheet(hDlg);
618 break;
619 case WM_SHOWWINDOW:
620 set_window_title(hDlg);
621 break;
622 case WM_NOTIFY:
623 switch (((LPNMHDR)lParam)->code) {
624 case PSN_SETACTIVE:
625 load_library_settings(hDlg);
626 break;
628 break;
629 case WM_COMMAND:
630 switch(HIWORD(wParam)) {
631 case CBN_EDITCHANGE:
632 if (LOWORD(wParam) == IDC_DLLCOMBO)
633 on_add_combo_change(hDlg);
634 break;
635 case CBN_SETFOCUS:
636 if (LOWORD(wParam) == IDC_DLLCOMBO)
637 on_add_combo_change(hDlg);
638 break;
639 case CBN_KILLFOCUS:
640 if (LOWORD(wParam) == IDC_DLLCOMBO)
641 SendMessageW(GetParent(hDlg), DM_SETDEFID, IDOK, 0);
642 break;
643 case BN_CLICKED:
644 switch(LOWORD(wParam)) {
645 case IDC_DLLS_ADDDLL:
646 on_add_click(hDlg);
647 break;
648 case IDC_DLLS_EDITDLL:
649 on_edit_click(hDlg);
650 break;
651 case IDC_DLLS_REMOVEDLL:
652 on_remove_click(hDlg);
653 break;
655 break;
656 case LBN_SELCHANGE:
657 if(LOWORD(wParam) == IDC_DLLCOMBO)
658 on_add_combo_change(hDlg);
659 else
660 set_controls_from_selection(hDlg);
661 break;
663 break;
666 return 0;