explorer: Work around the latest HAL binary compatibility breakage.
[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 <windows.h>
23 #include <wine/debug.h>
24 #include "explorer_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
27
28 #define DESKTOP_CLASS_ATOM MAKEINTATOMW(32769)
29 #define DESKTOP_ALL_ACCESS 0x01ff
30
31 static BOOL using_root;
32
33 /* window procedure for the desktop window */
34 static LRESULT WINAPI desktop_wnd_proc( HWND hwnd, UINT message, WPARAM wp, LPARAM lp )
35 {
36     WINE_TRACE( "got msg %x wp %x lp %lx\n", message, wp, lp );
37
38     switch(message)
39     {
40     case WM_SYSCOMMAND:
41         if ((wp & 0xfff0) == SC_CLOSE) ExitWindows( 0, 0 );
42         return 0;
43
44     case WM_CLOSE:
45         PostQuitMessage(0);
46         return 0;
47
48     case WM_SETCURSOR:
49         return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
50
51     case WM_NCHITTEST:
52         return HTCLIENT;
53
54     case WM_ERASEBKGND:
55         if (!using_root) PaintDesktop( (HDC)wp );
56         return TRUE;
57
58     case WM_PAINT:
59         {
60             PAINTSTRUCT ps;
61             BeginPaint( hwnd, &ps );
62             if (!using_root && ps.fErase) PaintDesktop( ps.hdc );
63             EndPaint( hwnd, &ps );
64         }
65         return 0;
66
67     default:
68         return DefWindowProcW( hwnd, message, wp, lp );
69     }
70 }
71
72 /* create the desktop and the associated X11 window, and make it the current desktop */
73 static unsigned long create_desktop( const char *name, unsigned int width, unsigned int height )
74 {
75     HMODULE x11drv = GetModuleHandleA( "winex11.drv" );
76     HDESK desktop;
77     unsigned long xwin = 0;
78     unsigned long (*create_desktop_func)(unsigned int, unsigned int);
79
80     desktop = CreateDesktopA( name, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
81     if (!desktop)
82     {
83         WINE_ERR( "failed to create desktop %s error %ld\n", wine_dbgstr_a(name), GetLastError() );
84         ExitProcess( 1 );
85     }
86     /* magic: desktop "root" means use the X11 root window */
87     if (x11drv && strcasecmp( name, "root" ))
88     {
89         create_desktop_func = (void *)GetProcAddress( x11drv, "wine_create_desktop" );
90         if (create_desktop_func) xwin = create_desktop_func( width, height );
91     }
92     SetThreadDesktop( desktop );
93     return xwin;
94 }
95
96 /* retrieve the default desktop size from the X11 driver config */
97 /* FIXME: this is for backwards compatibility, should probably be changed */
98 static BOOL get_default_desktop_size( unsigned int *width, unsigned int *height )
99 {
100     HKEY hkey;
101     char buffer[64];
102     DWORD size = sizeof(buffer);
103     BOOL ret = FALSE;
104
105     /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
106     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver", &hkey )) return FALSE;
107     if (!RegQueryValueExA( hkey, "Desktop", 0, NULL, (LPBYTE)buffer, &size ))
108         ret = (sscanf( buffer, "%ux%u", width, height ) == 2);
109     RegCloseKey( hkey );
110     return ret;
111 }
112
113 /* main desktop management function */
114 void manage_desktop( char *arg )
115 {
116     MSG msg;
117     HWND hwnd;
118     unsigned long xwin = 0;
119     unsigned int width, height;
120     char *cmdline = NULL;
121     char *p = arg;
122     static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
123
124     /* get the rest of the command line (if any) */
125     while (*p && !isspace(*p)) p++;
126     if (*p)
127     {
128         *p++ = 0;
129         while (*p && isspace(*p)) p++;
130         if (*p) cmdline = p;
131     }
132
133     /* parse the desktop option */
134     /* the option is of the form /desktop=name[,widthxheight] */
135     if (*arg == '=' || *arg == ',')
136     {
137         arg++;
138         if ((p = strchr( arg, ',' ))) *p++ = 0;
139         if (!p || sscanf( p, "%ux%u", &width, &height ) != 2)
140         {
141             width = 800;
142             height = 600;
143         }
144         xwin = create_desktop( arg, width, height );
145     }
146     else if (get_default_desktop_size( &width, &height ))
147     {
148         xwin = create_desktop( "Default", width, height );
149     }
150
151     if (!xwin)  /* using the root window */
152     {
153         using_root = TRUE;
154         width = GetSystemMetrics(SM_CXSCREEN);
155         height = GetSystemMetrics(SM_CYSCREEN);
156     }
157
158     /* create the desktop window */
159     hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
160                             WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
161                             0, 0, width, height, 0, 0, 0, NULL );
162     if (hwnd == GetDesktopWindow())
163     {
164         SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
165         SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
166         SetWindowTextW( hwnd, desktop_nameW );
167         SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
168         SetDeskWallPaper( (LPSTR)-1 );
169         initialize_hal();
170         initialize_systray();
171     }
172     else
173     {
174         DestroyWindow( hwnd );  /* someone beat us to it */
175         hwnd = 0;
176     }
177
178     /* if we have a command line, execute it */
179     if (cmdline)
180     {
181         STARTUPINFOA si;
182         PROCESS_INFORMATION pi;
183
184         memset( &si, 0, sizeof(si) );
185         si.cb = sizeof(si);
186         WINE_TRACE( "starting %s\n", wine_dbgstr_a(cmdline) );
187         if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
188         {
189             CloseHandle( pi.hThread );
190             CloseHandle( pi.hProcess );
191         }
192     }
193
194     /* run the desktop message loop */
195     if (hwnd)
196     {
197         WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
198         while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
199         WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
200     }
201
202     ExitProcess( 0 );
203 }