gdiplus: Add a stub for GdipSetImageAttributesRemapTable.
[wine/dcerpc.git] / programs / explorer / desktop.c
blobddca914a1e7199efd92f3afd7d0c250d7056e31e
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 <stdio.h>
22 #include "wine/unicode.h"
24 #define OEMRESOURCE
26 #include <windows.h>
27 #include <wine/debug.h>
28 #include "explorer_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
32 #define DESKTOP_CLASS_ATOM ((LPCWSTR)MAKEINTATOM(32769))
33 #define DESKTOP_ALL_ACCESS 0x01ff
35 static BOOL using_root;
37 /* window procedure for the desktop window */
38 static LRESULT WINAPI desktop_wnd_proc( HWND hwnd, UINT message, WPARAM wp, LPARAM lp )
40 WINE_TRACE( "got msg %04x wp %lx lp %lx\n", message, wp, lp );
42 switch(message)
44 case WM_SYSCOMMAND:
45 if ((wp & 0xfff0) == SC_CLOSE) ExitWindows( 0, 0 );
46 return 0;
48 case WM_CLOSE:
49 PostQuitMessage(0);
50 return 0;
52 case WM_SETCURSOR:
53 return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
55 case WM_NCHITTEST:
56 return HTCLIENT;
58 case WM_ERASEBKGND:
59 if (!using_root) PaintDesktop( (HDC)wp );
60 return TRUE;
62 case WM_PAINT:
64 PAINTSTRUCT ps;
65 BeginPaint( hwnd, &ps );
66 if (!using_root && ps.fErase) PaintDesktop( ps.hdc );
67 EndPaint( hwnd, &ps );
69 return 0;
71 default:
72 return DefWindowProcW( hwnd, message, wp, lp );
76 /* create the desktop and the associated X11 window, and make it the current desktop */
77 static unsigned long create_desktop( const char *name, unsigned int width, unsigned int height )
79 HMODULE x11drv = GetModuleHandleA( "winex11.drv" );
80 HDESK desktop;
81 unsigned long xwin = 0;
82 unsigned long (*create_desktop_func)(unsigned int, unsigned int);
84 desktop = CreateDesktopA( name, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
85 if (!desktop)
87 WINE_ERR( "failed to create desktop %s error %d\n", wine_dbgstr_a(name), GetLastError() );
88 ExitProcess( 1 );
90 /* magic: desktop "root" means use the X11 root window */
91 if (x11drv && strcasecmp( name, "root" ))
93 create_desktop_func = (void *)GetProcAddress( x11drv, "wine_create_desktop" );
94 if (create_desktop_func) xwin = create_desktop_func( width, height );
96 SetThreadDesktop( desktop );
97 return xwin;
100 /* retrieve the default desktop size from the X11 driver config */
101 /* FIXME: this is for backwards compatibility, should probably be changed */
102 static BOOL get_default_desktop_size( unsigned int *width, unsigned int *height )
104 HKEY hkey;
105 char buffer[64];
106 DWORD size = sizeof(buffer);
107 BOOL ret = FALSE;
109 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
110 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver", &hkey )) return FALSE;
111 if (!RegQueryValueExA( hkey, "Desktop", 0, NULL, (LPBYTE)buffer, &size ))
112 ret = (sscanf( buffer, "%ux%u", width, height ) == 2);
113 RegCloseKey( hkey );
114 return ret;
117 static void initialize_display_settings( HWND desktop )
119 static const WCHAR display_device_guid_propW[] = {
120 '_','_','w','i','n','e','_','d','i','s','p','l','a','y','_',
121 'd','e','v','i','c','e','_','g','u','i','d',0 };
122 GUID guid;
123 RPC_CSTR guid_str;
124 ATOM guid_atom;
125 DEVMODEW dmW;
127 UuidCreate( &guid );
128 UuidToStringA( &guid, &guid_str );
129 WINE_TRACE( "display guid %s\n", guid_str );
131 guid_atom = GlobalAddAtomA( (LPCSTR)guid_str );
132 SetPropW( desktop, display_device_guid_propW, ULongToHandle(guid_atom) );
134 RpcStringFreeA( &guid_str );
136 /* Store current display mode in the registry */
137 if (EnumDisplaySettingsExW( NULL, ENUM_CURRENT_SETTINGS, &dmW, 0 ))
139 WINE_TRACE( "Current display mode %ux%u %u bpp %u Hz\n", dmW.dmPelsWidth,
140 dmW.dmPelsHeight, dmW.dmBitsPerPel, dmW.dmDisplayFrequency );
141 ChangeDisplaySettingsExW( NULL, &dmW, 0,
142 CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY,
143 NULL );
147 static void set_desktop_window_title( HWND hwnd, const char *name )
149 static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
150 static const WCHAR desktop_name_separatorW[] = {' ', '-', ' ', 0};
151 WCHAR *window_titleW = NULL;
152 int window_title_len;
153 int name_len;
155 if (!name[0])
157 SetWindowTextW( hwnd, desktop_nameW );
158 return;
161 name_len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
162 window_title_len = name_len * sizeof(WCHAR)
163 + sizeof(desktop_name_separatorW)
164 + sizeof(desktop_nameW);
165 window_titleW = HeapAlloc( GetProcessHeap(), 0, window_title_len );
166 if (!window_titleW)
168 SetWindowTextW( hwnd, desktop_nameW );
169 return;
172 MultiByteToWideChar( CP_ACP, 0, name, -1,
173 window_titleW, name_len );
174 strcatW( window_titleW, desktop_name_separatorW );
175 strcatW( window_titleW, desktop_nameW );
177 SetWindowTextW( hwnd, window_titleW );
178 HeapFree( GetProcessHeap(), 0, window_titleW );
181 /* main desktop management function */
182 void manage_desktop( char *arg )
184 MSG msg;
185 HWND hwnd;
186 unsigned long xwin = 0;
187 unsigned int width, height;
188 char *cmdline = NULL;
189 char *p = arg;
190 const char *name = NULL;
192 /* get the rest of the command line (if any) */
193 while (*p && !isspace(*p)) p++;
194 if (*p)
196 *p++ = 0;
197 while (*p && isspace(*p)) p++;
198 if (*p) cmdline = p;
201 /* parse the desktop option */
202 /* the option is of the form /desktop=name[,widthxheight] */
203 if (*arg == '=' || *arg == ',')
205 arg++;
206 if ((p = strchr( arg, ',' ))) *p++ = 0;
207 if (!p || sscanf( p, "%ux%u", &width, &height ) != 2)
209 width = 800;
210 height = 600;
212 name = arg;
213 xwin = create_desktop( name, width, height );
215 else if (get_default_desktop_size( &width, &height ))
217 name = "Default";
218 xwin = create_desktop( name, width, height );
221 if (!xwin) using_root = TRUE; /* using the root window */
223 /* create the desktop window */
224 hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
225 WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
226 GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN),
227 GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN),
228 0, 0, 0, NULL );
229 if (hwnd == GetDesktopWindow())
231 SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
232 SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
233 if (name) set_desktop_window_title( hwnd, name );
234 SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
235 SetDeskWallPaper( (LPSTR)-1 );
236 initialize_display_settings( hwnd );
237 initialize_diskarbitration();
238 initialize_hal();
239 initialize_systray();
241 else
243 DestroyWindow( hwnd ); /* someone beat us to it */
244 hwnd = 0;
247 /* if we have a command line, execute it */
248 if (cmdline)
250 STARTUPINFOA si;
251 PROCESS_INFORMATION pi;
253 memset( &si, 0, sizeof(si) );
254 si.cb = sizeof(si);
255 WINE_TRACE( "starting %s\n", wine_dbgstr_a(cmdline) );
256 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
258 CloseHandle( pi.hThread );
259 CloseHandle( pi.hProcess );
263 /* run the desktop message loop */
264 if (hwnd)
266 WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
267 while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
268 WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
271 ExitProcess( 0 );