gdi32: Support the gamma correction for the subpixel rendering.
[wine.git] / programs / winecfg / libraries.c
blobee60978e9f6a733188f94336dc67e6352a823f5d
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 "secur32",
68 "twain_32",
69 "unicows",
70 "user32",
71 "vdmdbg",
72 "w32skrnl",
73 "wined3d",
74 "winedos",
75 "wineps",
76 "winmm",
77 "wintab32",
78 "wnaspi32",
79 "wow32",
80 "ws2_32",
81 "wsock32",
84 enum dllmode
86 BUILTIN_NATIVE,
87 NATIVE_BUILTIN,
88 BUILTIN,
89 NATIVE,
90 DISABLE,
91 UNKNOWN /* Special value indicating an erroneous DLL override mode */
94 struct dll
96 char *name;
97 enum dllmode mode;
100 static const WCHAR emptyW[1];
102 /* Convert a registry string to a dllmode */
103 static enum dllmode string_to_mode(char *in)
105 int i, j, len;
106 char *out;
107 enum dllmode res;
109 len = strlen(in);
110 out = HeapAlloc(GetProcessHeap(), 0, len + 1);
112 /* remove the spaces */
113 for (i = j = 0; i <= len; ++i) {
114 if (in[i] != ' ') {
115 out[j++] = in[i];
119 /* parse the string */
120 res = UNKNOWN;
121 if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
122 if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
123 if (strcmp(out, "builtin") == 0) res = BUILTIN;
124 if (strcmp(out, "native") == 0) res = NATIVE;
125 if (strcmp(out, "") == 0) res = DISABLE;
127 HeapFree(GetProcessHeap(), 0, out);
128 return res;
131 /* Convert a dllmode to a registry string. */
132 static const char* mode_to_string(enum dllmode mode)
134 switch( mode )
136 case NATIVE: return "native";
137 case BUILTIN: return "builtin";
138 case NATIVE_BUILTIN: return "native,builtin";
139 case BUILTIN_NATIVE: return "builtin,native";
140 case DISABLE: return "";
141 default: return "";
145 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
146 static const char* mode_to_label(enum dllmode mode)
148 static char buffer[256];
149 UINT id = 0;
151 switch( mode )
153 case NATIVE: id = IDS_DLL_NATIVE; break;
154 case BUILTIN: id = IDS_DLL_BUILTIN; break;
155 case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
156 case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
157 case DISABLE: id = IDS_DLL_DISABLED; break;
158 default: return "??";
160 if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
161 return buffer;
164 /* Convert a control id (IDC_ constant) to a dllmode */
165 static enum dllmode id_to_mode(DWORD id)
167 switch( id )
169 case IDC_RAD_BUILTIN: return BUILTIN;
170 case IDC_RAD_NATIVE: return NATIVE;
171 case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
172 case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
173 case IDC_RAD_DISABLE: return DISABLE;
174 default: assert( FALSE ); return 0; /* should not be reached */
178 /* Convert a dllmode to a control id (IDC_ constant) */
179 static DWORD mode_to_id(enum dllmode mode)
181 switch( mode )
183 case BUILTIN: return IDC_RAD_BUILTIN;
184 case NATIVE: return IDC_RAD_NATIVE;
185 case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
186 case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
187 case DISABLE: return IDC_RAD_DISABLE;
188 default: return IDC_RAD_BUILTIN_NATIVE;
192 /* helper for is_builtin_only */
193 static int compare_dll( const void *ptr1, const void *ptr2 )
195 const char * const *name1 = ptr1;
196 const char * const *name2 = ptr2;
197 return strcmp( *name1, *name2 );
200 /* check if dll is recommended as builtin only */
201 static inline BOOL is_builtin_only( const char *name )
203 const char *ext = strrchr( name, '.' );
205 if (ext)
207 if (!strcmp( ext, ".vxd" ) ||
208 !strcmp( ext, ".drv" ) ||
209 !strcmp( ext, ".tlb" ))
210 return TRUE;
212 return bsearch( &name, builtin_only, ARRAY_SIZE(builtin_only),
213 sizeof(builtin_only[0]), compare_dll ) != NULL;
216 /* check if dll should be offered in the drop-down list */
217 static BOOL show_dll_in_list( const char *name )
219 const char *ext = strrchr( name, '.' );
221 if (ext)
223 /* skip 16-bit dlls */
224 if (strlen(ext) > 2 && !strcmp( ext + strlen(ext) - 2, "16" )) return FALSE;
225 /* skip exes */
226 if (!strcmp( ext, ".exe" )) return FALSE;
228 /* skip dlls that should always be builtin */
229 return !is_builtin_only( name );
232 static void set_controls_from_selection(HWND dialog)
234 /* FIXME: display/update some information about the selected dll (purpose, recommended load order) maybe? */
237 static void clear_settings(HWND dialog)
239 int count = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
240 int i;
242 WINE_TRACE("count=%d\n", count);
244 for (i = 0; i < count; i++)
246 struct dll *dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
248 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
249 HeapFree(GetProcessHeap(), 0, dll->name);
250 HeapFree(GetProcessHeap(), 0, dll);
254 /* load the list of available libraries from a given dir */
255 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
257 char *buffer = NULL, name[256];
258 struct dirent *de;
259 DIR *dir = opendir( dir_path );
261 if (!dir) return;
263 if (check_subdirs)
264 buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
266 while ((de = readdir( dir )))
268 size_t len = strlen(de->d_name);
269 if (len > sizeof(name)) continue;
270 if (len > 3 && !strcmp( de->d_name + len - 3, ".so"))
272 len -= 3;
273 if (len > 4 && !strcmp( de->d_name + len - 4, ".dll.so")) len -= 4;
274 memcpy( name, de->d_name, len );
275 name[len] = 0;
276 if (!show_dll_in_list( name )) continue;
277 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
279 else if (check_subdirs)
281 struct stat st;
282 if (!show_dll_in_list( de->d_name )) continue;
283 sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
284 if (!stat( buffer, &st ))
286 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
287 continue;
289 sprintf( buffer, "%s/%s/%s.so", dir_path, de->d_name, de->d_name );
290 if (!stat( buffer, &st ))
292 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
293 continue;
297 closedir( dir );
298 HeapFree( GetProcessHeap(), 0, buffer );
301 /* load the list of available libraries */
302 static void load_library_list( HWND dialog )
304 unsigned int i = 0;
305 const char *path, *build_dir = wine_get_build_dir();
306 char item1[256], item2[256];
307 HCURSOR old_cursor = SetCursor( LoadCursorW(0, (LPWSTR)IDC_WAIT) );
309 if (build_dir)
311 char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
312 strcpy( dir, build_dir );
313 strcat( dir, "/dlls" );
314 load_library_list_from_dir( dialog, dir, TRUE );
315 HeapFree( GetProcessHeap(), 0, dir );
318 while ((path = wine_dll_enum_load_path( i++ )))
319 load_library_list_from_dir( dialog, path, FALSE );
321 /* get rid of duplicate entries */
323 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
324 i = 1;
325 while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
327 if (!strcmp( item1, item2 ))
329 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
331 else
333 strcpy( item1, item2 );
334 i++;
337 SetCursor( old_cursor );
340 static void load_library_settings(HWND dialog)
342 char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
343 char **p;
344 int sel, count = 0;
346 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
348 WINE_TRACE("sel=%d\n", sel);
350 clear_settings(dialog);
352 if (!overrides || *overrides == NULL)
354 set_controls_from_selection(dialog);
355 disable(IDC_DLLS_EDITDLL);
356 disable(IDC_DLLS_REMOVEDLL);
357 HeapFree(GetProcessHeap(), 0, overrides);
358 return;
361 enable(IDC_DLLS_EDITDLL);
362 enable(IDC_DLLS_REMOVEDLL);
364 for (p = overrides; *p != NULL; p++)
366 int index;
367 char *str, *value;
368 const char *label;
369 struct dll *dll;
371 value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
373 label = mode_to_label(string_to_mode(value));
375 str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
376 strcpy(str, *p);
377 strcat(str, " (");
378 strcat(str, label);
379 strcat(str, ")");
381 dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
382 dll->name = *p;
383 dll->mode = string_to_mode(value);
385 index = SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
386 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
388 HeapFree(GetProcessHeap(), 0, str);
390 count++;
393 HeapFree(GetProcessHeap(), 0, overrides);
395 /* restore the previous selection, if possible */
396 if (sel >= count - 1) sel = count - 1;
397 else if (sel == -1) sel = 0;
399 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
401 set_controls_from_selection(dialog);
404 /* Called when the application is initialized (cannot reinit!) */
405 static void init_libsheet(HWND dialog)
407 /* clear the add dll controls */
408 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM)emptyW);
409 load_library_list( dialog );
410 disable(IDC_DLLS_ADDDLL);
413 static void on_add_combo_change(HWND dialog)
415 WCHAR buffer[1024];
416 int sel, len;
418 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_GETTEXT, ARRAY_SIZE(buffer), (LPARAM)buffer);
419 /* if lib was chosen from combobox, we receive an empty buffer, check manually */
420 sel=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
421 len=SendDlgItemMessageW(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
423 if (buffer[0] || len>0)
425 enable(IDC_DLLS_ADDDLL)
426 SendMessageW(GetParent(dialog), DM_SETDEFID, IDC_DLLS_ADDDLL, 0);
428 else
430 disable(IDC_DLLS_ADDDLL);
431 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
435 static void set_dllmode(HWND dialog, DWORD id)
437 enum dllmode mode;
438 struct dll *dll;
439 int sel;
440 const char *str;
442 mode = id_to_mode(id);
444 sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
445 if (sel == -1) return;
447 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
449 str = mode_to_string(mode);
450 WINE_TRACE("Setting %s to %s\n", dll->name, str);
452 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
453 set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
455 load_library_settings(dialog); /* ... and refresh */
458 static void on_add_click(HWND dialog)
460 static const char dotDll[] = ".dll";
461 char buffer[1024], *ptr;
463 ZeroMemory(buffer, sizeof(buffer));
465 SendDlgItemMessageA(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
466 if (lstrlenA(buffer) >= sizeof(dotDll))
468 ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
469 if (!lstrcmpiA(ptr, dotDll))
471 WINE_TRACE("Stripping dll extension\n");
472 *ptr = '\0';
476 /* check if dll is in the builtin-only list */
477 if (!(ptr = strrchr( buffer, '\\' )))
479 ptr = buffer;
480 if (*ptr == '*') ptr++;
482 else ptr++;
483 if (is_builtin_only( ptr ))
485 MSGBOXPARAMSA params;
486 params.cbSize = sizeof(params);
487 params.hwndOwner = dialog;
488 params.hInstance = GetModuleHandleA( NULL );
489 params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
490 params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
491 params.dwStyle = MB_ICONWARNING | MB_YESNO;
492 params.lpszIcon = NULL;
493 params.dwContextHelpId = 0;
494 params.lpfnMsgBoxCallback = NULL;
495 params.dwLanguageId = 0;
496 if (MessageBoxIndirectA( &params ) != IDYES) return;
499 SendDlgItemMessageW(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM)emptyW);
500 disable(IDC_DLLS_ADDDLL);
501 SendMessageW(GetParent(dialog), DM_SETDEFID, IDOK, 0);
503 WINE_TRACE("Adding %s as native, builtin\n", buffer);
505 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
506 set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
508 load_library_settings(dialog);
510 SendDlgItemMessageA(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
512 set_controls_from_selection(dialog);
515 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
517 static WORD sel;
519 switch(uMsg)
521 case WM_INITDIALOG:
522 CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
523 sel = lParam;
524 return TRUE;
526 case WM_COMMAND:
527 if(HIWORD(wParam) != BN_CLICKED) break;
528 switch (LOWORD(wParam))
530 case IDC_RAD_BUILTIN:
531 case IDC_RAD_NATIVE:
532 case IDC_RAD_BUILTIN_NATIVE:
533 case IDC_RAD_NATIVE_BUILTIN:
534 case IDC_RAD_DISABLE:
535 sel = LOWORD(wParam);
536 return TRUE;
537 case IDOK:
538 EndDialog(hwndDlg, sel);
539 return TRUE;
540 case IDCANCEL:
541 EndDialog(hwndDlg, wParam);
542 return TRUE;
545 return FALSE;
548 static void on_edit_click(HWND hwnd)
550 INT_PTR ret;
551 int index = SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
552 struct dll *dll;
553 DWORD id;
555 /* if no override is selected the edit button should be disabled... */
556 assert(index != -1);
558 dll = (struct dll *) SendDlgItemMessageW(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
559 id = mode_to_id(dll->mode);
561 ret = DialogBoxParamW(0, MAKEINTRESOURCEW(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
563 if(ret != IDCANCEL)
564 set_dllmode(hwnd, ret);
567 static void on_remove_click(HWND dialog)
569 int sel = SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
570 struct dll *dll;
572 if (sel == LB_ERR) return;
574 dll = (struct dll *) SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
576 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
578 SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
579 set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
581 HeapFree(GetProcessHeap(), 0, dll->name);
582 HeapFree(GetProcessHeap(), 0, dll);
584 if (SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
585 SendDlgItemMessageW(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
586 else
588 disable(IDC_DLLS_EDITDLL);
589 disable(IDC_DLLS_REMOVEDLL);
592 set_controls_from_selection(dialog);
595 INT_PTR CALLBACK
596 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
598 switch (uMsg)
600 case WM_INITDIALOG:
601 init_libsheet(hDlg);
602 break;
603 case WM_SHOWWINDOW:
604 set_window_title(hDlg);
605 break;
606 case WM_NOTIFY:
607 switch (((LPNMHDR)lParam)->code) {
608 case PSN_SETACTIVE:
609 load_library_settings(hDlg);
610 break;
612 break;
613 case WM_COMMAND:
614 switch(HIWORD(wParam)) {
615 case CBN_EDITCHANGE:
616 if (LOWORD(wParam) == IDC_DLLCOMBO)
617 on_add_combo_change(hDlg);
618 break;
619 case CBN_SETFOCUS:
620 if (LOWORD(wParam) == IDC_DLLCOMBO)
621 on_add_combo_change(hDlg);
622 break;
623 case CBN_KILLFOCUS:
624 if (LOWORD(wParam) == IDC_DLLCOMBO)
625 SendMessageW(GetParent(hDlg), DM_SETDEFID, IDOK, 0);
626 break;
627 case BN_CLICKED:
628 switch(LOWORD(wParam)) {
629 case IDC_DLLS_ADDDLL:
630 on_add_click(hDlg);
631 break;
632 case IDC_DLLS_EDITDLL:
633 on_edit_click(hDlg);
634 break;
635 case IDC_DLLS_REMOVEDLL:
636 on_remove_click(hDlg);
637 break;
639 break;
640 case LBN_SELCHANGE:
641 if(LOWORD(wParam) == IDC_DLLCOMBO)
642 on_add_combo_change(hDlg);
643 else
644 set_controls_from_selection(hDlg);
645 break;
647 break;
650 return 0;