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