explorer: Modify rects passed to SHAppBarMessage to not interfere with existing appbars.
[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 #include "wine/unicode.h"
23
24 #define OEMRESOURCE
25
26 #include <windows.h>
27 #include <wine/debug.h>
28 #include "explorer_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
31
32 #define DESKTOP_CLASS_ATOM ((LPCWSTR)MAKEINTATOM(32769))
33 #define DESKTOP_ALL_ACCESS 0x01ff
34
35 static BOOL using_root;
36
37 /* window procedure for the desktop window */
38 static LRESULT WINAPI desktop_wnd_proc( HWND hwnd, UINT message, WPARAM wp, LPARAM lp )
39 {
40     WINE_TRACE( "got msg %04x wp %lx lp %lx\n", message, wp, lp );
41
42     switch(message)
43     {
44     case WM_SYSCOMMAND:
45         if ((wp & 0xfff0) == SC_CLOSE) ExitWindows( 0, 0 );
46         return 0;
47
48     case WM_CLOSE:
49         PostQuitMessage(0);
50         return 0;
51
52     case WM_SETCURSOR:
53         return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
54
55     case WM_NCHITTEST:
56         return HTCLIENT;
57
58     case WM_ERASEBKGND:
59         if (!using_root) PaintDesktop( (HDC)wp );
60         return TRUE;
61
62     case WM_PAINT:
63         {
64             PAINTSTRUCT ps;
65             BeginPaint( hwnd, &ps );
66             if (!using_root && ps.fErase) PaintDesktop( ps.hdc );
67             EndPaint( hwnd, &ps );
68         }
69         return 0;
70
71     default:
72         return DefWindowProcW( hwnd, message, wp, lp );
73     }
74 }
75
76 /* create the desktop and the associated X11 window, and make it the current desktop */
77 static unsigned long create_desktop( const WCHAR *name, unsigned int width, unsigned int height )
78 {
79     static const WCHAR rootW[] = {'r','o','o','t',0};
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 = CreateDesktopW( 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_w(name), GetLastError() );
89         ExitProcess( 1 );
90     }
91     /* magic: desktop "root" means use the X11 root window */
92     if (x11drv && strcmpiW( name, rootW ))
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 /* parse the desktop size specification */
102 static BOOL parse_size( const WCHAR *size, unsigned int *width, unsigned int *height )
103 {
104     WCHAR *end;
105
106     *width = strtoulW( size, &end, 10 );
107     if (end == size) return FALSE;
108     if (*end != 'x') return FALSE;
109     size = end + 1;
110     *height = strtoulW( size, &end, 10 );
111     return !*end;
112 }
113
114 /* retrieve the desktop name to use if not specified on the command line */
115 static const WCHAR *get_default_desktop_name(void)
116 {
117     static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
118     static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
119     static const WCHAR explorer_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
120                                           'E','x','p','l','o','r','e','r',0};
121     static WCHAR buffer[MAX_PATH];
122     DWORD size = sizeof(buffer);
123     HDESK desk = GetThreadDesktop( GetCurrentThreadId() );
124     WCHAR *ret = NULL;
125     HKEY hkey;
126
127     if (desk && GetUserObjectInformationW( desk, UOI_NAME, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
128     {
129         if (strcmpiW( buffer, defaultW )) return buffer;
130     }
131
132     /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
133     if (!RegOpenKeyW( HKEY_CURRENT_USER, explorer_keyW, &hkey ))
134     {
135         if (!RegQueryValueExW( hkey, desktopW, 0, NULL, (LPBYTE)buffer, &size )) ret = buffer;
136         RegCloseKey( hkey );
137     }
138     return ret;
139 }
140
141 /* retrieve the default desktop size from the registry */
142 static BOOL get_default_desktop_size( const WCHAR *name, unsigned int *width, unsigned int *height )
143 {
144     static const WCHAR desktop_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
145                                          'E','x','p','l','o','r','e','r','\\',
146                                          'D','e','s','k','t','o','p','s',0};
147     HKEY hkey;
148     WCHAR buffer[64];
149     DWORD size = sizeof(buffer);
150     BOOL found = FALSE;
151
152     *width = 800;
153     *height = 600;
154
155     /* @@ Wine registry key: HKCU\Software\Wine\Explorer\Desktops */
156     if (!RegOpenKeyW( HKEY_CURRENT_USER, desktop_keyW, &hkey ))
157     {
158         if (!RegQueryValueExW( hkey, name, 0, NULL, (LPBYTE)buffer, &size ))
159         {
160             found = TRUE;
161             if (!parse_size( buffer, width, height )) *width = *height = 0;
162         }
163         RegCloseKey( hkey );
164     }
165     return found;
166 }
167
168 static void initialize_display_settings( HWND desktop )
169 {
170     static const WCHAR display_device_guid_propW[] = {
171         '_','_','w','i','n','e','_','d','i','s','p','l','a','y','_',
172         'd','e','v','i','c','e','_','g','u','i','d',0 };
173     GUID guid;
174     RPC_CSTR guid_str;
175     ATOM guid_atom;
176     DEVMODEW dmW;
177
178     UuidCreate( &guid );
179     UuidToStringA( &guid, &guid_str );
180     WINE_TRACE( "display guid %s\n", guid_str );
181
182     guid_atom = GlobalAddAtomA( (LPCSTR)guid_str );
183     SetPropW( desktop, display_device_guid_propW, ULongToHandle(guid_atom) );
184
185     RpcStringFreeA( &guid_str );
186
187     /* Store current display mode in the registry */
188     if (EnumDisplaySettingsExW( NULL, ENUM_CURRENT_SETTINGS, &dmW, 0 ))
189     {
190         WINE_TRACE( "Current display mode %ux%u %u bpp %u Hz\n", dmW.dmPelsWidth,
191                     dmW.dmPelsHeight, dmW.dmBitsPerPel, dmW.dmDisplayFrequency );
192         ChangeDisplaySettingsExW( NULL, &dmW, 0,
193                                   CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY,
194                                   NULL );
195     }
196 }
197
198 static void set_desktop_window_title( HWND hwnd, const WCHAR *name )
199 {
200     static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
201     static const WCHAR desktop_name_separatorW[] = {' ', '-', ' ', 0};
202     WCHAR *window_titleW = NULL;
203     int window_title_len;
204
205     if (!name[0])
206     {
207         SetWindowTextW( hwnd, desktop_nameW );
208         return;
209     }
210
211     window_title_len = strlenW(name) * sizeof(WCHAR)
212                      + sizeof(desktop_name_separatorW)
213                      + sizeof(desktop_nameW);
214     window_titleW = HeapAlloc( GetProcessHeap(), 0, window_title_len );
215     if (!window_titleW)
216     {
217         SetWindowTextW( hwnd, desktop_nameW );
218         return;
219     }
220
221     strcpyW( window_titleW, name );
222     strcatW( window_titleW, desktop_name_separatorW );
223     strcatW( window_titleW, desktop_nameW );
224
225     SetWindowTextW( hwnd, window_titleW );
226     HeapFree( GetProcessHeap(), 0, window_titleW );
227 }
228
229 /* main desktop management function */
230 void manage_desktop( WCHAR *arg )
231 {
232     static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
233     static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
234     MSG msg;
235     HWND hwnd, msg_hwnd;
236     unsigned long xwin = 0;
237     unsigned int width, height;
238     WCHAR *cmdline = NULL;
239     WCHAR *p = arg;
240     const WCHAR *name = NULL;
241
242     /* get the rest of the command line (if any) */
243     while (*p && !isspace(*p)) p++;
244     if (*p)
245     {
246         *p++ = 0;
247         while (*p && isspace(*p)) p++;
248         if (*p) cmdline = p;
249     }
250
251     /* parse the desktop option */
252     /* the option is of the form /desktop=name[,widthxheight] */
253     if (*arg == '=' || *arg == ',')
254     {
255         arg++;
256         name = arg;
257         if ((p = strchrW( arg, ',' ))) *p++ = 0;
258         if (!p || !parse_size( p, &width, &height ))
259             get_default_desktop_size( name, &width, &height );
260     }
261     else if ((name = get_default_desktop_name()))
262     {
263         if (!get_default_desktop_size( name, &width, &height )) width = height = 0;
264     }
265     else  /* check for the X11 driver key for backwards compatibility (to be removed) */
266     {
267         static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
268         static const WCHAR x11_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
269                                          'X','1','1',' ','D','r','i','v','e','r',0};
270         HKEY hkey;
271         WCHAR buffer[64];
272         DWORD size = sizeof(buffer);
273
274         width = height = 0;
275         /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
276         if (!RegOpenKeyW( HKEY_CURRENT_USER, x11_keyW, &hkey ))
277         {
278             if (!RegQueryValueExW( hkey, desktopW, 0, NULL, (LPBYTE)buffer, &size ))
279             {
280                 name = defaultW;
281                 if (!parse_size( buffer, &width, &height )) width = height = 0;
282             }
283             RegCloseKey( hkey );
284         }
285     }
286
287     if (name && width && height) xwin = create_desktop( name, width, height );
288
289     if (!xwin) using_root = TRUE; /* using the root window */
290
291     /* create the desktop window */
292     hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
293                             WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
294                             GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN),
295                             GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN),
296                             0, 0, 0, NULL );
297
298     /* create the HWND_MESSAGE parent */
299     msg_hwnd = CreateWindowExW( 0, messageW, NULL, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
300                                 0, 0, 100, 100, 0, 0, 0, NULL );
301
302     if (hwnd == GetDesktopWindow())
303     {
304         SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
305         SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
306         if (name) set_desktop_window_title( hwnd, name );
307         SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
308         SetDeskWallPaper( (LPSTR)-1 );
309         initialize_display_settings( hwnd );
310         initialize_appbar();
311         initialize_systray();
312     }
313     else
314     {
315         DestroyWindow( hwnd );  /* someone beat us to it */
316         hwnd = 0;
317     }
318
319     if (GetAncestor( msg_hwnd, GA_PARENT )) DestroyWindow( msg_hwnd );  /* someone beat us to it */
320
321     /* if we have a command line, execute it */
322     if (cmdline)
323     {
324         STARTUPINFOW si;
325         PROCESS_INFORMATION pi;
326
327         memset( &si, 0, sizeof(si) );
328         si.cb = sizeof(si);
329         WINE_TRACE( "starting %s\n", wine_dbgstr_w(cmdline) );
330         if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
331         {
332             CloseHandle( pi.hThread );
333             CloseHandle( pi.hProcess );
334         }
335     }
336
337     /* run the desktop message loop */
338     if (hwnd)
339     {
340         WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
341         while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
342         WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
343     }
344
345     ExitProcess( 0 );
346 }