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