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