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