api-ms-win-downlevel-user32-l1-1-0: Add version resource.
[wine.git] / programs / explorer / desktop.c
blob12888d84f2fdbc7741ec10b705b2279bd99e7579
1 /*
2 * Explorer desktop support
4 * Copyright 2006 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
23 #include <stdio.h>
24 #include "wine/unicode.h"
26 #define OEMRESOURCE
28 #include <windows.h>
29 #include <rpc.h>
30 #include <wine/debug.h>
31 #include "explorer_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
35 #define DESKTOP_CLASS_ATOM ((LPCWSTR)MAKEINTATOM(32769))
36 #define DESKTOP_ALL_ACCESS 0x01ff
38 static BOOL using_root;
40 /* screen saver handler */
41 static BOOL start_screensaver( void )
43 if (using_root)
45 const char *argv[3] = { "xdg-screensaver", "activate", NULL };
46 int pid = spawnvp( _P_DETACH, argv[0], argv );
47 if (pid > 0)
49 WINE_TRACE( "started process %d\n", pid );
50 return TRUE;
53 return FALSE;
56 /* window procedure for the desktop window */
57 static LRESULT WINAPI desktop_wnd_proc( HWND hwnd, UINT message, WPARAM wp, LPARAM lp )
59 WINE_TRACE( "got msg %04x wp %lx lp %lx\n", message, wp, lp );
61 switch(message)
63 case WM_SYSCOMMAND:
64 switch(wp & 0xfff0)
66 case SC_CLOSE:
67 ExitWindows( 0, 0 );
68 break;
69 case SC_SCREENSAVE:
70 return start_screensaver();
72 return 0;
74 case WM_CLOSE:
75 PostQuitMessage(0);
76 return 0;
78 case WM_SETCURSOR:
79 return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
81 case WM_NCHITTEST:
82 return HTCLIENT;
84 case WM_ERASEBKGND:
85 if (!using_root) PaintDesktop( (HDC)wp );
86 return TRUE;
88 case WM_SETTINGCHANGE:
89 if (wp == SPI_SETDESKWALLPAPER)
90 SystemParametersInfoW( SPI_SETDESKWALLPAPER, 0, NULL, FALSE );
91 return 0;
93 case WM_PAINT:
95 PAINTSTRUCT ps;
96 BeginPaint( hwnd, &ps );
97 if (!using_root && ps.fErase) PaintDesktop( ps.hdc );
98 EndPaint( hwnd, &ps );
100 return 0;
102 default:
103 return DefWindowProcW( hwnd, message, wp, lp );
107 /* create the desktop and the associated X11 window, and make it the current desktop */
108 static unsigned long create_desktop( const WCHAR *name, unsigned int width, unsigned int height )
110 static const WCHAR rootW[] = {'r','o','o','t',0};
111 HMODULE x11drv = GetModuleHandleA( "winex11.drv" );
112 HDESK desktop;
113 unsigned long xwin = 0;
114 unsigned long (CDECL *create_desktop_func)(unsigned int, unsigned int);
116 desktop = CreateDesktopW( name, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
117 if (!desktop)
119 WINE_ERR( "failed to create desktop %s error %d\n", wine_dbgstr_w(name), GetLastError() );
120 ExitProcess( 1 );
122 /* magic: desktop "root" means use the X11 root window */
123 if (x11drv && strcmpiW( name, rootW ))
125 create_desktop_func = (void *)GetProcAddress( x11drv, "wine_create_desktop" );
126 if (create_desktop_func) xwin = create_desktop_func( width, height );
128 SetThreadDesktop( desktop );
129 return xwin;
132 /* parse the desktop size specification */
133 static BOOL parse_size( const WCHAR *size, unsigned int *width, unsigned int *height )
135 WCHAR *end;
137 *width = strtoulW( size, &end, 10 );
138 if (end == size) return FALSE;
139 if (*end != 'x') return FALSE;
140 size = end + 1;
141 *height = strtoulW( size, &end, 10 );
142 return !*end;
145 /* retrieve the desktop name to use if not specified on the command line */
146 static const WCHAR *get_default_desktop_name(void)
148 static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
149 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
150 static const WCHAR explorer_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
151 'E','x','p','l','o','r','e','r',0};
152 static WCHAR buffer[MAX_PATH];
153 DWORD size = sizeof(buffer);
154 HDESK desk = GetThreadDesktop( GetCurrentThreadId() );
155 WCHAR *ret = NULL;
156 HKEY hkey;
158 if (desk && GetUserObjectInformationW( desk, UOI_NAME, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
160 if (strcmpiW( buffer, defaultW )) return buffer;
163 /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
164 if (!RegOpenKeyW( HKEY_CURRENT_USER, explorer_keyW, &hkey ))
166 if (!RegQueryValueExW( hkey, desktopW, 0, NULL, (LPBYTE)buffer, &size )) ret = buffer;
167 RegCloseKey( hkey );
169 return ret;
172 /* retrieve the default desktop size from the registry */
173 static BOOL get_default_desktop_size( const WCHAR *name, unsigned int *width, unsigned int *height )
175 static const WCHAR desktop_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
176 'E','x','p','l','o','r','e','r','\\',
177 'D','e','s','k','t','o','p','s',0};
178 HKEY hkey;
179 WCHAR buffer[64];
180 DWORD size = sizeof(buffer);
181 BOOL found = FALSE;
183 *width = 800;
184 *height = 600;
186 /* @@ Wine registry key: HKCU\Software\Wine\Explorer\Desktops */
187 if (!RegOpenKeyW( HKEY_CURRENT_USER, desktop_keyW, &hkey ))
189 if (!RegQueryValueExW( hkey, name, 0, NULL, (LPBYTE)buffer, &size ))
191 found = TRUE;
192 if (!parse_size( buffer, width, height )) *width = *height = 0;
194 RegCloseKey( hkey );
196 return found;
199 static void initialize_display_settings( HWND desktop )
201 static const WCHAR display_device_guid_propW[] = {
202 '_','_','w','i','n','e','_','d','i','s','p','l','a','y','_',
203 'd','e','v','i','c','e','_','g','u','i','d',0 };
204 GUID guid;
205 RPC_CSTR guid_str;
206 ATOM guid_atom;
207 DEVMODEW dmW;
209 UuidCreate( &guid );
210 UuidToStringA( &guid, &guid_str );
211 WINE_TRACE( "display guid %s\n", guid_str );
213 guid_atom = GlobalAddAtomA( (LPCSTR)guid_str );
214 SetPropW( desktop, display_device_guid_propW, ULongToHandle(guid_atom) );
216 RpcStringFreeA( &guid_str );
218 /* Store current display mode in the registry */
219 if (EnumDisplaySettingsExW( NULL, ENUM_CURRENT_SETTINGS, &dmW, 0 ))
221 WINE_TRACE( "Current display mode %ux%u %u bpp %u Hz\n", dmW.dmPelsWidth,
222 dmW.dmPelsHeight, dmW.dmBitsPerPel, dmW.dmDisplayFrequency );
223 ChangeDisplaySettingsExW( NULL, &dmW, 0,
224 CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY,
225 NULL );
229 static void set_desktop_window_title( HWND hwnd, const WCHAR *name )
231 static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
232 static const WCHAR desktop_name_separatorW[] = {' ', '-', ' ', 0};
233 WCHAR *window_titleW = NULL;
234 int window_title_len;
236 if (!name[0])
238 SetWindowTextW( hwnd, desktop_nameW );
239 return;
242 window_title_len = strlenW(name) * sizeof(WCHAR)
243 + sizeof(desktop_name_separatorW)
244 + sizeof(desktop_nameW);
245 window_titleW = HeapAlloc( GetProcessHeap(), 0, window_title_len );
246 if (!window_titleW)
248 SetWindowTextW( hwnd, desktop_nameW );
249 return;
252 strcpyW( window_titleW, name );
253 strcatW( window_titleW, desktop_name_separatorW );
254 strcatW( window_titleW, desktop_nameW );
256 SetWindowTextW( hwnd, window_titleW );
257 HeapFree( GetProcessHeap(), 0, window_titleW );
260 /* main desktop management function */
261 void manage_desktop( WCHAR *arg )
263 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
264 MSG msg;
265 HWND hwnd, msg_hwnd;
266 unsigned long xwin = 0;
267 unsigned int width, height;
268 WCHAR *cmdline = NULL;
269 WCHAR *p = arg;
270 const WCHAR *name = NULL;
272 /* get the rest of the command line (if any) */
273 while (*p && !isspace(*p)) p++;
274 if (*p)
276 *p++ = 0;
277 while (*p && isspace(*p)) p++;
278 if (*p) cmdline = p;
281 /* parse the desktop option */
282 /* the option is of the form /desktop=name[,widthxheight] */
283 if (*arg == '=' || *arg == ',')
285 arg++;
286 name = arg;
287 if ((p = strchrW( arg, ',' ))) *p++ = 0;
288 if (!p || !parse_size( p, &width, &height ))
289 get_default_desktop_size( name, &width, &height );
291 else if ((name = get_default_desktop_name()))
293 if (!get_default_desktop_size( name, &width, &height )) width = height = 0;
296 if (name && width && height) xwin = create_desktop( name, width, height );
298 if (!xwin) using_root = TRUE; /* using the root window */
300 /* create the desktop window */
301 hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
302 WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
303 GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN),
304 GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN),
305 0, 0, 0, NULL );
307 /* create the HWND_MESSAGE parent */
308 msg_hwnd = CreateWindowExW( 0, messageW, NULL, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
309 0, 0, 100, 100, 0, 0, 0, NULL );
311 if (hwnd == GetDesktopWindow())
313 HMODULE shell32;
314 void (WINAPI *pShellDDEInit)( BOOL );
316 SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
317 SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
318 if (name) set_desktop_window_title( hwnd, name );
319 SystemParametersInfoW( SPI_SETDESKWALLPAPER, 0, NULL, FALSE );
320 ClipCursor( NULL );
321 initialize_display_settings( hwnd );
322 initialize_appbar();
323 initialize_systray( using_root );
325 if ((shell32 = LoadLibraryA( "shell32.dll" )) &&
326 (pShellDDEInit = (void *)GetProcAddress( shell32, (LPCSTR)188)))
328 pShellDDEInit( TRUE );
331 else
333 DestroyWindow( hwnd ); /* someone beat us to it */
334 hwnd = 0;
337 if (GetAncestor( msg_hwnd, GA_PARENT )) DestroyWindow( msg_hwnd ); /* someone beat us to it */
339 /* if we have a command line, execute it */
340 if (cmdline)
342 STARTUPINFOW si;
343 PROCESS_INFORMATION pi;
345 memset( &si, 0, sizeof(si) );
346 si.cb = sizeof(si);
347 WINE_TRACE( "starting %s\n", wine_dbgstr_w(cmdline) );
348 if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
350 CloseHandle( pi.hThread );
351 CloseHandle( pi.hProcess );
355 /* run the desktop message loop */
356 if (hwnd)
358 WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
359 while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
360 WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
363 ExitProcess( 0 );