configure: Make the libssl soname check depend on the header check.
[wine] / programs / winecfg / libraries.c
1 /*
2  * WineCfg libraries tabsheet
3  *
4  * Copyright 2004 Robert van Herk
5  * Copyright 2004 Mike Hearn
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #include "config.h"
24
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
41
42 #include "winecfg.h"
43 #include "resource.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
46
47 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
48 static const char * const builtin_only[] =
49 {
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",
88 };
89
90 enum dllmode
91 {
92         BUILTIN_NATIVE,
93         NATIVE_BUILTIN,
94         BUILTIN,
95         NATIVE,
96         DISABLE,
97         UNKNOWN /* Special value indicating an erronous DLL override mode */
98 };
99
100 struct dll
101 {
102         char *name;
103         enum dllmode mode;
104 };
105
106 /* Convert a registry string to a dllmode */
107 static enum dllmode string_to_mode(char *in)
108 {
109     int i, j, len;
110     char *out;
111     enum dllmode res;
112
113     len = strlen(in);
114     out = HeapAlloc(GetProcessHeap(), 0, len);
115
116     /* remove the spaces */
117     for (i = j = 0; i <= len; ++i) {
118         if (in[i] != ' ') {
119             out[j++] = in[i];
120         }
121     }
122
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;
130
131     HeapFree(GetProcessHeap(), 0, out);
132     return res;
133 }
134
135 /* Convert a dllmode to a registry string. */
136 static const char* mode_to_string(enum dllmode mode)
137 {
138     switch( mode )
139     {
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 "";
146     }
147 }
148
149 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
150 static const char* mode_to_label(enum dllmode mode)
151 {
152     static char buffer[256];
153     UINT id = 0;
154
155     switch( mode )
156     {
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);
163     }
164     if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
165     return buffer;
166 }
167
168 /* Convert a control id (IDC_ constant) to a dllmode */
169 static enum dllmode id_to_mode(DWORD id)
170 {
171     switch( id )
172     {
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  */
179     }
180 }
181
182 /* Convert a dllmode to a control id (IDC_ constant) */
183 static DWORD mode_to_id(enum dllmode mode)
184 {
185     switch( mode )
186     {
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  */
193     }
194 }
195
196 /* helper for is_builtin_only */
197 static int compare_dll( const void *ptr1, const void *ptr2 )
198 {
199     const char * const *name1 = ptr1;
200     const char * const *name2 = ptr2;
201     return strcmp( *name1, *name2 );
202 }
203
204 /* check if dll is recommended as builtin only */
205 static inline int is_builtin_only( const char *name )
206 {
207     return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
208                     sizeof(builtin_only[0]), compare_dll ) != NULL;
209 }
210
211 static void set_controls_from_selection(HWND dialog)
212 {
213     /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
214 }
215
216 static void clear_settings(HWND dialog)
217 {
218     int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
219     int i;
220
221     WINE_TRACE("count=%d\n", count);
222     
223     for (i = 0; i < count; i++)
224     {
225         struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
226         
227         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
228         
229         HeapFree(GetProcessHeap(), 0, dll->name);
230         HeapFree(GetProcessHeap(), 0, dll);
231     }
232 }
233
234 /* check if a given dll is 16-bit */
235 static int is_16bit_dll( const char *dir, const char *name )
236 {
237     char buffer[64];
238     int res;
239     size_t len = strlen(dir) + strlen(name) + 2;
240     char *path = HeapAlloc( GetProcessHeap(), 0, len );
241
242     strcpy( path, dir );
243     strcat( path, "/" );
244     strcat( path, name );
245     res = readlink( path, buffer, sizeof(buffer) );
246     HeapFree( GetProcessHeap(), 0, path );
247
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;
254 }
255
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 )
258 {
259     char *buffer = NULL, name[256];
260     struct dirent *de;
261     DIR *dir = opendir( dir_path );
262
263     if (!dir) return;
264
265     if (check_subdirs)
266         buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
267
268     while ((de = readdir( dir )))
269     {
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"))
273         {
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 );
281         }
282         else if (check_subdirs)
283         {
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 );
289         }
290     }
291     closedir( dir );
292     HeapFree( GetProcessHeap(), 0, buffer );
293 }
294
295 /* load the list of available libraries */
296 static void load_library_list( HWND dialog )
297 {
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) );
302
303     if (build_dir)
304     {
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 );
310     }
311
312     while ((path = wine_dll_enum_load_path( i++ )))
313         load_library_list_from_dir( dialog, path, FALSE );
314
315     /* get rid of duplicate entries */
316
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)
320     {
321         if (!strcmp( item1, item2 ))
322         {
323             SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
324         }
325         else
326         {
327             strcpy( item1, item2 );
328             i++;
329         }
330     }
331     SetCursor( old_cursor );
332 }
333
334 static void load_library_settings(HWND dialog)
335 {
336     char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
337     char **p;
338     int sel, count = 0;
339
340     sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
341
342     WINE_TRACE("sel=%d\n", sel);
343
344     clear_settings(dialog);
345     
346     if (!overrides || *overrides == NULL)
347     {
348         set_controls_from_selection(dialog);
349         disable(IDC_DLLS_EDITDLL);
350         disable(IDC_DLLS_REMOVEDLL);
351         HeapFree(GetProcessHeap(), 0, overrides);
352         return;
353     }
354
355     enable(IDC_DLLS_EDITDLL);
356     enable(IDC_DLLS_REMOVEDLL);
357     
358     for (p = overrides; *p != NULL; p++)
359     {
360         int index;
361         char *str, *value;
362         const char *label;
363         struct dll *dll;
364
365         value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
366
367         label = mode_to_label(string_to_mode(value));
368         
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, ")");
374
375         dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
376         dll->name = *p;
377         dll->mode = string_to_mode(value);
378
379         index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
380         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
381
382         HeapFree(GetProcessHeap(), 0, str);
383
384         count++;
385     }
386
387     HeapFree(GetProcessHeap(), 0, overrides);
388
389     /* restore the previous selection, if possible  */
390     if (sel >= count - 1) sel = count - 1;
391     else if (sel == -1) sel = 0;
392     
393     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
394
395     set_controls_from_selection(dialog);
396 }
397
398 /* Called when the application is initialized (cannot reinit!)  */
399 static void init_libsheet(HWND dialog)
400 {
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);
405 }
406
407 static void on_add_combo_change(HWND dialog)
408 {
409     char buffer[1024];
410
411     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
412
413     if (strlen(buffer))
414         enable(IDC_DLLS_ADDDLL)
415     else
416         disable(IDC_DLLS_ADDDLL);
417 }
418
419 static void set_dllmode(HWND dialog, DWORD id)
420 {
421     enum dllmode mode;
422     struct dll *dll;
423     int sel;
424     const char *str;
425
426     mode = id_to_mode(id);
427
428     sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
429     if (sel == -1) return;
430     
431     dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
432
433     str = mode_to_string(mode);
434     WINE_TRACE("Setting %s to %s\n", dll->name, str);
435     
436     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
437     set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
438
439     load_library_settings(dialog);  /* ... and refresh  */
440 }
441
442 static void on_add_click(HWND dialog)
443 {
444     static const char dotDll[] = ".dll";
445     char buffer[1024], *ptr;
446
447     ZeroMemory(buffer, sizeof(buffer));
448
449     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
450     if (lstrlenA(buffer) >= sizeof(dotDll))
451     {
452         ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
453         if (!lstrcmpiA(ptr, dotDll))
454         {
455             WINE_TRACE("Stripping dll extension\n");
456             *ptr = '\0';
457         }
458     }
459
460     /* check if dll is in the builtin-only list */
461     if (!(ptr = strrchr( buffer, '\\' )))
462     {
463         ptr = buffer;
464         if (*ptr == '*') ptr++;
465     }
466     else ptr++;
467     if (is_builtin_only( ptr ))
468     {
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;
481     }
482
483     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
484     disable(IDC_DLLS_ADDDLL);
485     
486     WINE_TRACE("Adding %s as native, builtin\n", buffer);
487     
488     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
489     set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
490
491     load_library_settings(dialog);
492
493     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, (WPARAM) 0, (LPARAM) buffer);
494
495     set_controls_from_selection(dialog);
496 }
497
498 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
499 {
500     static WORD sel;
501
502     switch(uMsg) 
503     {
504     case WM_INITDIALOG:
505         CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
506         sel = lParam;
507         return TRUE;
508
509     case WM_COMMAND:
510         if(HIWORD(wParam) != BN_CLICKED) break;
511         switch (LOWORD(wParam))
512         {
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;
526         }
527     }
528     return FALSE;
529 }
530
531 static void on_edit_click(HWND hwnd)
532 {
533     INT_PTR ret; 
534     int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
535     struct dll *dll;
536     DWORD id;
537
538     /* if no override is selected the edit button should be disabled... */
539     assert(index != -1);
540
541     dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
542     id = mode_to_id(dll->mode);
543     
544     ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
545     
546     if(ret != IDCANCEL)
547         set_dllmode(hwnd, ret);
548 }
549
550 static void on_remove_click(HWND dialog)
551 {
552     int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
553     struct dll *dll;
554
555     if (sel == LB_ERR) return;
556     
557     dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
558     
559     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
560
561     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
562     set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
563
564     HeapFree(GetProcessHeap(), 0, dll->name);
565     HeapFree(GetProcessHeap(), 0, dll);
566
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
570     {
571         disable(IDC_DLLS_EDITDLL);
572         disable(IDC_DLLS_REMOVEDLL);
573     }
574
575     set_controls_from_selection(dialog);
576 }
577
578 INT_PTR CALLBACK
579 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
580 {
581         switch (uMsg)
582         {
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;
594                 }
595                 break;
596         case WM_COMMAND:
597                 switch(HIWORD(wParam)) {
598
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!
601                      */
602                     
603                 case CBN_EDITCHANGE:
604                         if(LOWORD(wParam) == IDC_DLLCOMBO)
605                         {
606                             on_add_combo_change(hDlg);
607                             break;
608                         }
609
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;
621                         }
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;
629                 }
630                 break;
631         }
632
633         return 0;
634 }