configure: Don't use a default soname if a library is not found, leave it undefined.
[wine] / programs / explorer / desktop.c
1 /*
2  * Explorer desktop support
3  *
4  * Copyright 2006 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22
23 #define OEMRESOURCE
24
25 #include <windows.h>
26 #include <wine/debug.h>
27 #include "explorer_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
30
31 #define DESKTOP_CLASS_ATOM ((LPCWSTR)MAKEINTATOM(32769))
32 #define DESKTOP_ALL_ACCESS 0x01ff
33
34 extern HANDLE __wine_make_process_system(void);
35
36 static BOOL using_root;
37
38 /* window procedure for the desktop window */
39 static LRESULT WINAPI desktop_wnd_proc( HWND hwnd, UINT message, WPARAM wp, LPARAM lp )
40 {
41     WINE_TRACE( "got msg %x wp %lx lp %lx\n", message, wp, lp );
42
43     switch(message)
44     {
45     case WM_SYSCOMMAND:
46         if ((wp & 0xfff0) == SC_CLOSE) ExitWindows( 0, 0 );
47         return 0;
48
49     case WM_CLOSE:
50         PostQuitMessage(0);
51         return 0;
52
53     case WM_SETCURSOR:
54         return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
55
56     case WM_NCHITTEST:
57         return HTCLIENT;
58
59     case WM_ERASEBKGND:
60         if (!using_root) PaintDesktop( (HDC)wp );
61         return TRUE;
62
63     case WM_PAINT:
64         {
65             PAINTSTRUCT ps;
66             BeginPaint( hwnd, &ps );
67             if (!using_root && ps.fErase) PaintDesktop( ps.hdc );
68             EndPaint( hwnd, &ps );
69         }
70         return 0;
71
72     default:
73         return DefWindowProcW( hwnd, message, wp, lp );
74     }
75 }
76
77 /* create the desktop and the associated X11 window, and make it the current desktop */
78 static unsigned long create_desktop( const char *name, unsigned int width, unsigned int height )
79 {
80     HMODULE x11drv = GetModuleHandleA( "winex11.drv" );
81     HDESK desktop;
82     unsigned long xwin = 0;
83     unsigned long (*create_desktop_func)(unsigned int, unsigned int);
84
85     desktop = CreateDesktopA( name, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
86     if (!desktop)
87     {
88         WINE_ERR( "failed to create desktop %s error %d\n", wine_dbgstr_a(name), GetLastError() );
89         ExitProcess( 1 );
90     }
91     /* magic: desktop "root" means use the X11 root window */
92     if (x11drv && strcasecmp( name, "root" ))
93     {
94         create_desktop_func = (void *)GetProcAddress( x11drv, "wine_create_desktop" );
95         if (create_desktop_func) xwin = create_desktop_func( width, height );
96     }
97     SetThreadDesktop( desktop );
98     return xwin;
99 }
100
101 /* retrieve the default desktop size from the X11 driver config */
102 /* FIXME: this is for backwards compatibility, should probably be changed */
103 static BOOL get_default_desktop_size( unsigned int *width, unsigned int *height )
104 {
105     HKEY hkey;
106     char buffer[64];
107     DWORD size = sizeof(buffer);
108     BOOL ret = FALSE;
109
110     /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
111     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver", &hkey )) return FALSE;
112     if (!RegQueryValueExA( hkey, "Desktop", 0, NULL, (LPBYTE)buffer, &size ))
113         ret = (sscanf( buffer, "%ux%u", width, height ) == 2);
114     RegCloseKey( hkey );
115     return ret;
116 }
117
118 /* main desktop management function */
119 void manage_desktop( char *arg )
120 {
121     MSG msg;
122     HWND hwnd;
123     unsigned long xwin = 0;
124     unsigned int width, height;
125     char *cmdline = NULL;
126     char *p = arg;
127     static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
128
129     /* get the rest of the command line (if any) */
130     while (*p && !isspace(*p)) p++;
131     if (*p)
132     {
133         *p++ = 0;
134         while (*p && isspace(*p)) p++;
135         if (*p) cmdline = p;
136     }
137
138     /* parse the desktop option */
139     /* the option is of the form /desktop=name[,widthxheight] */
140     if (*arg == '=' || *arg == ',')
141     {
142         arg++;
143         if ((p = strchr( arg, ',' ))) *p++ = 0;
144         if (!p || sscanf( p, "%ux%u", &width, &height ) != 2)
145         {
146             width = 800;
147             height = 600;
148         }
149         xwin = create_desktop( arg, width, height );
150     }
151     else if (get_default_desktop_size( &width, &height ))
152     {
153         xwin = create_desktop( "Default", width, height );
154     }
155
156     if (!xwin)  /* using the root window */
157     {
158         using_root = TRUE;
159         width = GetSystemMetrics(SM_CXSCREEN);
160         height = GetSystemMetrics(SM_CYSCREEN);
161     }
162
163     /* create the desktop window */
164     hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
165                             WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
166                             0, 0, width, height, 0, 0, 0, NULL );
167     if (hwnd == GetDesktopWindow())
168     {
169         SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
170         SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
171         SetWindowTextW( hwnd, desktop_nameW );
172         SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
173         SetDeskWallPaper( (LPSTR)-1 );
174         initialize_diskarbitration();
175         initialize_hal();
176         initialize_systray();
177     }
178     else
179     {
180         DestroyWindow( hwnd );  /* someone beat us to it */
181         hwnd = 0;
182     }
183
184     /* if we have a command line, execute it */
185     if (cmdline)
186     {
187         STARTUPINFOA si;
188         PROCESS_INFORMATION pi;
189
190         memset( &si, 0, sizeof(si) );
191         si.cb = sizeof(si);
192         WINE_TRACE( "starting %s\n", wine_dbgstr_a(cmdline) );
193         if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
194         {
195             CloseHandle( pi.hThread );
196             CloseHandle( pi.hProcess );
197         }
198     }
199
200     /* run the desktop message loop */
201     if (hwnd)
202     {
203         /* we don't use the system process event, the server
204          * posts a WM_CLOSE when the last desktop user is gone */
205         CloseHandle( __wine_make_process_system() );
206
207         WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
208         while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
209         WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
210     }
211
212     ExitProcess( 0 );
213 }