winecfg: Update German translation.
[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 #include "wine/port.h"
25
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
42
43 #include "winecfg.h"
44 #include "resource.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
47
48 /* dlls that shouldn't be configured anything other than builtin; list must be sorted*/
49 static const char * const builtin_only[] =
50 {
51     "advapi32",
52     "capi2032",
53     "dbghelp",
54     "ddraw",
55     "gdi32",
56     "glu32",
57     "gphoto2.ds",
58     "icmp",
59     "iphlpapi",
60     "kernel32",
61     "mountmgr.sys",
62     "mswsock",
63     "ntdll",
64     "ntoskrnl.exe",
65     "opengl32",
66     "sane.ds",
67     "twain_32",
68     "unicows",
69     "user32",
70     "vdmdbg",
71     "w32skrnl",
72     "wined3d",
73     "winedos",
74     "winemp3.acm",
75     "wineps",
76     "winmm",
77     "wintab32",
78     "wnaspi32",
79     "wow32",
80     "ws2_32",
81     "wsock32",
82 };
83
84 enum dllmode
85 {
86         BUILTIN_NATIVE,
87         NATIVE_BUILTIN,
88         BUILTIN,
89         NATIVE,
90         DISABLE,
91         UNKNOWN /* Special value indicating an erroneous DLL override mode */
92 };
93
94 struct dll
95 {
96         char *name;
97         enum dllmode mode;
98 };
99
100 /* Convert a registry string to a dllmode */
101 static enum dllmode string_to_mode(char *in)
102 {
103     int i, j, len;
104     char *out;
105     enum dllmode res;
106
107     len = strlen(in);
108     out = HeapAlloc(GetProcessHeap(), 0, len);
109
110     /* remove the spaces */
111     for (i = j = 0; i <= len; ++i) {
112         if (in[i] != ' ') {
113             out[j++] = in[i];
114         }
115     }
116
117     /* parse the string */
118     res = UNKNOWN;
119     if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
120     if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
121     if (strcmp(out, "builtin") == 0) res = BUILTIN;
122     if (strcmp(out, "native") == 0) res = NATIVE;
123     if (strcmp(out, "") == 0) res = DISABLE;
124
125     HeapFree(GetProcessHeap(), 0, out);
126     return res;
127 }
128
129 /* Convert a dllmode to a registry string. */
130 static const char* mode_to_string(enum dllmode mode)
131 {
132     switch( mode )
133     {
134         case NATIVE: return "native";
135         case BUILTIN: return "builtin";
136         case NATIVE_BUILTIN: return "native,builtin";
137         case BUILTIN_NATIVE: return "builtin,native";
138         case DISABLE: return "";
139         default: return "";
140     }
141 }
142
143 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
144 static const char* mode_to_label(enum dllmode mode)
145 {
146     static char buffer[256];
147     UINT id = 0;
148
149     switch( mode )
150     {
151     case NATIVE: id = IDS_DLL_NATIVE; break;
152     case BUILTIN: id = IDS_DLL_BUILTIN; break;
153     case NATIVE_BUILTIN: id = IDS_DLL_NATIVE_BUILTIN; break;
154     case BUILTIN_NATIVE: id = IDS_DLL_BUILTIN_NATIVE; break;
155     case DISABLE: id = IDS_DLL_DISABLED; break;
156     default: return "??";
157     }
158     if (!LoadStringA( GetModuleHandleA(NULL), id, buffer, sizeof(buffer) )) buffer[0] = 0;
159     return buffer;
160 }
161
162 /* Convert a control id (IDC_ constant) to a dllmode */
163 static enum dllmode id_to_mode(DWORD id)
164 {
165     switch( id )
166     {
167         case IDC_RAD_BUILTIN: return BUILTIN;
168         case IDC_RAD_NATIVE: return NATIVE;
169         case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
170         case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
171         case IDC_RAD_DISABLE: return DISABLE;
172         default: assert( FALSE ); return 0; /* should not be reached  */
173     }
174 }
175
176 /* Convert a dllmode to a control id (IDC_ constant) */
177 static DWORD mode_to_id(enum dllmode mode)
178 {
179     switch( mode )
180     {
181         case BUILTIN: return IDC_RAD_BUILTIN;
182         case NATIVE: return IDC_RAD_NATIVE;
183         case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
184         case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
185         case DISABLE: return IDC_RAD_DISABLE;
186         default: return IDC_RAD_BUILTIN_NATIVE;
187     }
188 }
189
190 /* helper for is_builtin_only */
191 static int compare_dll( const void *ptr1, const void *ptr2 )
192 {
193     const char * const *name1 = ptr1;
194     const char * const *name2 = ptr2;
195     return strcmp( *name1, *name2 );
196 }
197
198 /* check if dll is recommended as builtin only */
199 static inline int is_builtin_only( const char *name )
200 {
201     const char *ext = strrchr( name, '.' );
202
203     if (ext)
204     {
205         if (!strcmp( ext, ".vxd" ) ||
206             !strcmp( ext, ".drv" ) ||
207             !strcmp( ext, ".tlb" ))
208             return TRUE;
209     }
210     return bsearch( &name, builtin_only, sizeof(builtin_only)/sizeof(builtin_only[0]),
211                     sizeof(builtin_only[0]), compare_dll ) != NULL;
212 }
213
214 /* check if dll should be offered in the drop-down list */
215 static int show_dll_in_list( const char *name )
216 {
217     const char *ext = strrchr( name, '.' );
218
219     if (ext)
220     {
221         /* skip 16-bit dlls */
222         if (strlen(ext) > 2 && !strcmp( ext + strlen(ext) - 2, "16" )) return FALSE;
223         /* skip exes */
224         if (!strcmp( ext, ".exe" )) return FALSE;
225     }
226     /* skip dlls that should always be builtin */
227     return !is_builtin_only( name );
228 }
229
230 static void set_controls_from_selection(HWND dialog)
231 {
232     /* FIXME: display/update some information about the selected dll (purpose, recommended loadorder) maybe? */
233 }
234
235 static void clear_settings(HWND dialog)
236 {
237     int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
238     int i;
239
240     WINE_TRACE("count=%d\n", count);
241     
242     for (i = 0; i < count; i++)
243     {
244         struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
245         
246         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
247         
248         HeapFree(GetProcessHeap(), 0, dll->name);
249         HeapFree(GetProcessHeap(), 0, dll);
250     }
251 }
252
253 /* load the list of available libraries from a given dir */
254 static void load_library_list_from_dir( HWND dialog, const char *dir_path, int check_subdirs )
255 {
256     char *buffer = NULL, name[256];
257     struct dirent *de;
258     DIR *dir = opendir( dir_path );
259
260     if (!dir) return;
261
262     if (check_subdirs)
263         buffer = HeapAlloc( GetProcessHeap(), 0, strlen(dir_path) + 2 * sizeof(name) + 10 );
264
265     while ((de = readdir( dir )))
266     {
267         size_t len = strlen(de->d_name);
268         if (len > sizeof(name)) continue;
269         if (len > 3 && !strcmp( de->d_name + len - 3, ".so"))
270         {
271             len -= 3;
272             if (len > 4 && !strcmp( de->d_name + len - 4, ".dll.so")) len -= 4;
273             memcpy( name, de->d_name, len );
274             name[len] = 0;
275             if (!show_dll_in_list( name )) continue;
276             SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)name );
277         }
278         else if (check_subdirs)
279         {
280             struct stat st;
281             if (!show_dll_in_list( de->d_name )) continue;
282             sprintf( buffer, "%s/%s/%s.dll.so", dir_path, de->d_name, de->d_name );
283             if (!stat( buffer, &st ))
284             {
285                 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
286                 continue;
287             }
288             sprintf( buffer, "%s/%s/%s.so", dir_path, de->d_name, de->d_name );
289             if (!stat( buffer, &st ))
290             {
291                 SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_ADDSTRING, 0, (LPARAM)de->d_name );
292                 continue;
293             }
294         }
295     }
296     closedir( dir );
297     HeapFree( GetProcessHeap(), 0, buffer );
298 }
299
300 /* load the list of available libraries */
301 static void load_library_list( HWND dialog )
302 {
303     unsigned int i = 0;
304     const char *path, *build_dir = wine_get_build_dir();
305     char item1[256], item2[256];
306     HCURSOR old_cursor = SetCursor( LoadCursor(0, IDC_WAIT) );
307
308     if (build_dir)
309     {
310         char *dir = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/dlls") );
311         strcpy( dir, build_dir );
312         strcat( dir, "/dlls" );
313         load_library_list_from_dir( dialog, dir, TRUE );
314         HeapFree( GetProcessHeap(), 0, dir );
315     }
316
317     while ((path = wine_dll_enum_load_path( i++ )))
318         load_library_list_from_dir( dialog, path, FALSE );
319
320     /* get rid of duplicate entries */
321
322     SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, 0, (LPARAM)item1 );
323     i = 1;
324     while (SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_GETLBTEXT, i, (LPARAM)item2 ) >= 0)
325     {
326         if (!strcmp( item1, item2 ))
327         {
328             SendDlgItemMessageA( dialog, IDC_DLLCOMBO, CB_DELETESTRING, i, 0 );
329         }
330         else
331         {
332             strcpy( item1, item2 );
333             i++;
334         }
335     }
336     SetCursor( old_cursor );
337 }
338
339 static void load_library_settings(HWND dialog)
340 {
341     char **overrides = enumerate_values(config_key, keypath("DllOverrides"));
342     char **p;
343     int sel, count = 0;
344
345     sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
346
347     WINE_TRACE("sel=%d\n", sel);
348
349     clear_settings(dialog);
350     
351     if (!overrides || *overrides == NULL)
352     {
353         set_controls_from_selection(dialog);
354         disable(IDC_DLLS_EDITDLL);
355         disable(IDC_DLLS_REMOVEDLL);
356         HeapFree(GetProcessHeap(), 0, overrides);
357         return;
358     }
359
360     enable(IDC_DLLS_EDITDLL);
361     enable(IDC_DLLS_REMOVEDLL);
362     
363     for (p = overrides; *p != NULL; p++)
364     {
365         int index;
366         char *str, *value;
367         const char *label;
368         struct dll *dll;
369
370         value = get_reg_key(config_key, keypath("DllOverrides"), *p, NULL);
371
372         label = mode_to_label(string_to_mode(value));
373         
374         str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
375         strcpy(str, *p);
376         strcat(str, " (");
377         strcat(str, label);
378         strcat(str, ")");
379
380         dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
381         dll->name = *p;
382         dll->mode = string_to_mode(value);
383
384         index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
385         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
386
387         HeapFree(GetProcessHeap(), 0, str);
388
389         count++;
390     }
391
392     HeapFree(GetProcessHeap(), 0, overrides);
393
394     /* restore the previous selection, if possible  */
395     if (sel >= count - 1) sel = count - 1;
396     else if (sel == -1) sel = 0;
397     
398     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
399
400     set_controls_from_selection(dialog);
401 }
402
403 /* Called when the application is initialized (cannot reinit!)  */
404 static void init_libsheet(HWND dialog)
405 {
406     /* clear the add dll controls  */
407     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
408     load_library_list( dialog );
409     disable(IDC_DLLS_ADDDLL);
410 }
411
412 static void on_add_combo_change(HWND dialog)
413 {
414     char buffer[1024];
415     int sel, len;
416
417     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
418     /* if lib was chosen from combobox, we receive an empty buffer, check manually */
419     sel=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETCURSEL, 0, 0);
420     len=SendDlgItemMessage(dialog, IDC_DLLCOMBO, CB_GETLBTEXTLEN, sel, 0);
421
422     if (strlen(buffer)>0 || len>0)
423         enable(IDC_DLLS_ADDDLL)
424     else
425         disable(IDC_DLLS_ADDDLL);
426 }
427
428 static void set_dllmode(HWND dialog, DWORD id)
429 {
430     enum dllmode mode;
431     struct dll *dll;
432     int sel;
433     const char *str;
434
435     mode = id_to_mode(id);
436
437     sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
438     if (sel == -1) return;
439     
440     dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
441
442     str = mode_to_string(mode);
443     WINE_TRACE("Setting %s to %s\n", dll->name, str);
444     
445     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
446     set_reg_key(config_key, keypath("DllOverrides"), dll->name, str);
447
448     load_library_settings(dialog);  /* ... and refresh  */
449 }
450
451 static void on_add_click(HWND dialog)
452 {
453     static const char dotDll[] = ".dll";
454     char buffer[1024], *ptr;
455
456     ZeroMemory(buffer, sizeof(buffer));
457
458     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
459     if (lstrlenA(buffer) >= sizeof(dotDll))
460     {
461         ptr = buffer + lstrlenA(buffer) - sizeof(dotDll) + 1;
462         if (!lstrcmpiA(ptr, dotDll))
463         {
464             WINE_TRACE("Stripping dll extension\n");
465             *ptr = '\0';
466         }
467     }
468
469     /* check if dll is in the builtin-only list */
470     if (!(ptr = strrchr( buffer, '\\' )))
471     {
472         ptr = buffer;
473         if (*ptr == '*') ptr++;
474     }
475     else ptr++;
476     if (is_builtin_only( ptr ))
477     {
478         MSGBOXPARAMSA params;
479         params.cbSize = sizeof(params);
480         params.hwndOwner = dialog;
481         params.hInstance = GetModuleHandleA( NULL );
482         params.lpszText = MAKEINTRESOURCEA( IDS_DLL_WARNING );
483         params.lpszCaption = MAKEINTRESOURCEA( IDS_DLL_WARNING_CAPTION );
484         params.dwStyle = MB_ICONWARNING | MB_YESNO;
485         params.lpszIcon = NULL;
486         params.dwContextHelpId = 0;
487         params.lpfnMsgBoxCallback = NULL;
488         params.dwLanguageId = 0;
489         if (MessageBoxIndirectA( &params ) != IDYES) return;
490     }
491
492     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
493     disable(IDC_DLLS_ADDDLL);
494     
495     WINE_TRACE("Adding %s as native, builtin\n", buffer);
496     
497     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
498     set_reg_key(config_key, keypath("DllOverrides"), buffer, "native,builtin");
499
500     load_library_settings(dialog);
501
502     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, 0, (LPARAM) buffer);
503
504     set_controls_from_selection(dialog);
505 }
506
507 static INT_PTR CALLBACK loadorder_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
508 {
509     static WORD sel;
510
511     switch(uMsg) 
512     {
513     case WM_INITDIALOG:
514         CheckRadioButton(hwndDlg, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, lParam);
515         sel = lParam;
516         return TRUE;
517
518     case WM_COMMAND:
519         if(HIWORD(wParam) != BN_CLICKED) break;
520         switch (LOWORD(wParam))
521         {
522         case IDC_RAD_BUILTIN:
523         case IDC_RAD_NATIVE:
524         case IDC_RAD_BUILTIN_NATIVE:
525         case IDC_RAD_NATIVE_BUILTIN:
526         case IDC_RAD_DISABLE:
527             sel = LOWORD(wParam);
528             return TRUE;
529         case IDOK:
530             EndDialog(hwndDlg, sel);
531             return TRUE;
532         case IDCANCEL:
533             EndDialog(hwndDlg, wParam);
534             return TRUE;
535         }
536     }
537     return FALSE;
538 }
539
540 static void on_edit_click(HWND hwnd)
541 {
542     INT_PTR ret; 
543     int index = SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
544     struct dll *dll;
545     DWORD id;
546
547     /* if no override is selected the edit button should be disabled... */
548     assert(index != -1);
549
550     dll = (struct dll *) SendDlgItemMessage(hwnd, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
551     id = mode_to_id(dll->mode);
552     
553     ret = DialogBoxParam(0, MAKEINTRESOURCE(IDD_LOADORDER), hwnd, loadorder_dlgproc, id);
554     
555     if(ret != IDCANCEL)
556         set_dllmode(hwnd, ret);
557 }
558
559 static void on_remove_click(HWND dialog)
560 {
561     int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
562     struct dll *dll;
563
564     if (sel == LB_ERR) return;
565     
566     dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
567     
568     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
569
570     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
571     set_reg_key(config_key, keypath("DllOverrides"), dll->name, NULL);
572
573     HeapFree(GetProcessHeap(), 0, dll->name);
574     HeapFree(GetProcessHeap(), 0, dll);
575
576     if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
577         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
578     else
579     {
580         disable(IDC_DLLS_EDITDLL);
581         disable(IDC_DLLS_REMOVEDLL);
582     }
583
584     set_controls_from_selection(dialog);
585 }
586
587 INT_PTR CALLBACK
588 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
589 {
590         switch (uMsg)
591         {
592         case WM_INITDIALOG:
593                 init_libsheet(hDlg);
594                 break;
595         case WM_SHOWWINDOW:
596                 set_window_title(hDlg);
597                 break;
598         case WM_NOTIFY:
599                 switch (((LPNMHDR)lParam)->code) {
600                 case PSN_SETACTIVE:
601                     load_library_settings(hDlg);
602                     break;
603                 }
604                 break;
605         case WM_COMMAND:
606                 switch(HIWORD(wParam)) {
607
608                     /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
609                      * add button, rather than the propsheet OK button. But I don't know how to do that!
610                      */
611                     
612                 case CBN_EDITCHANGE:
613                         if(LOWORD(wParam) == IDC_DLLCOMBO)
614                         {
615                             on_add_combo_change(hDlg);
616                             break;
617                         }
618
619                 case BN_CLICKED:
620                         switch(LOWORD(wParam)) {
621                         case IDC_DLLS_ADDDLL:
622                             on_add_click(hDlg);
623                             break;
624                         case IDC_DLLS_EDITDLL:
625                             on_edit_click(hDlg);
626                             break;
627                         case IDC_DLLS_REMOVEDLL:
628                             on_remove_click(hDlg);
629                             break;
630                         }
631                         break;
632                 case LBN_SELCHANGE:
633                         if(LOWORD(wParam) == IDC_DLLCOMBO)
634                             on_add_combo_change(hDlg);
635                         else
636                             set_controls_from_selection(hDlg);
637                         break;
638                 }
639                 break;
640         }
641
642         return 0;
643 }