explorer: Add a registry setting to always show the systray.
[wine/multimedia.git] / programs / explorer / desktop.c
blob51791c7a9605302717cc3e2eaf0a3edf91cdbd67
1 /*
2 * Explorer desktop support
4 * Copyright 2006 Alexandre Julliard
5 * Copyright 2013 Hans Leidekker for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
24 #include <stdio.h>
26 #define COBJMACROS
27 #define OEMRESOURCE
28 #include <windows.h>
29 #include <rpc.h>
30 #include <shlobj.h>
31 #include <shellapi.h>
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "explorer_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
39 #define DESKTOP_CLASS_ATOM ((LPCWSTR)MAKEINTATOM(32769))
40 #define DESKTOP_ALL_ACCESS 0x01ff
42 #ifdef __APPLE__
43 static const WCHAR default_driver[] = {'m','a','c',',','x','1','1',0};
44 #else
45 static const WCHAR default_driver[] = {'x','1','1',0};
46 #endif
48 static BOOL using_root;
50 struct launcher
52 WCHAR *path;
53 HICON icon;
54 WCHAR *title;
57 static WCHAR *desktop_folder;
58 static WCHAR *desktop_folder_public;
60 static int icon_cx, icon_cy, icon_offset_cx, icon_offset_cy;
61 static int title_cx, title_cy, title_offset_cx, title_offset_cy;
62 static int desktop_width, launcher_size, launchers_per_row;
64 static struct launcher **launchers;
65 static unsigned int nb_launchers, nb_allocated;
67 static RECT get_icon_rect( unsigned int index )
69 RECT rect;
70 unsigned int row = index / launchers_per_row;
71 unsigned int col = index % launchers_per_row;
73 rect.left = col * launcher_size + icon_offset_cx;
74 rect.right = rect.left + icon_cx;
75 rect.top = row * launcher_size + icon_offset_cy;
76 rect.bottom = rect.top + icon_cy;
77 return rect;
80 static RECT get_title_rect( unsigned int index )
82 RECT rect;
83 unsigned int row = index / launchers_per_row;
84 unsigned int col = index % launchers_per_row;
86 rect.left = col * launcher_size + title_offset_cx;
87 rect.right = rect.left + title_cx;
88 rect.top = row * launcher_size + title_offset_cy;
89 rect.bottom = rect.top + title_cy;
90 return rect;
93 static const struct launcher *launcher_from_point( int x, int y )
95 RECT icon, title;
96 unsigned int index;
98 if (!nb_launchers) return NULL;
99 index = x / launcher_size + (y / launcher_size) * launchers_per_row;
100 if (index >= nb_launchers) return NULL;
102 icon = get_icon_rect( index );
103 title = get_title_rect( index );
104 if ((x < icon.left || x > icon.right || y < icon.top || y > icon.bottom) &&
105 (x < title.left || x > title.right || y < title.top || y > title.bottom)) return NULL;
106 return launchers[index];
109 static void draw_launchers( HDC hdc, RECT update_rect )
111 COLORREF color = SetTextColor( hdc, RGB(255,255,255) ); /* FIXME: depends on background color */
112 int mode = SetBkMode( hdc, TRANSPARENT );
113 unsigned int i;
114 LOGFONTW lf;
115 HFONT font;
117 SystemParametersInfoW( SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0 );
118 font = SelectObject( hdc, CreateFontIndirectW( &lf ) );
120 for (i = 0; i < nb_launchers; i++)
122 RECT dummy, icon = get_icon_rect( i ), title = get_title_rect( i );
124 if (IntersectRect( &dummy, &icon, &update_rect ))
125 DrawIconEx( hdc, icon.left, icon.top, launchers[i]->icon, icon_cx, icon_cy,
126 0, 0, DI_DEFAULTSIZE|DI_NORMAL );
128 if (IntersectRect( &dummy, &title, &update_rect ))
129 DrawTextW( hdc, launchers[i]->title, -1, &title,
130 DT_CENTER|DT_WORDBREAK|DT_EDITCONTROL|DT_END_ELLIPSIS );
133 SelectObject( hdc, font );
134 SetTextColor( hdc, color );
135 SetBkMode( hdc, mode );
138 static void do_launch( const struct launcher *launcher )
140 static const WCHAR openW[] = {'o','p','e','n',0};
141 ShellExecuteW( NULL, openW, launcher->path, NULL, NULL, 0 );
144 static WCHAR *append_path( const WCHAR *path, const WCHAR *filename, int len_filename )
146 int len_path = strlenW( path );
147 WCHAR *ret;
149 if (len_filename == -1) len_filename = strlenW( filename );
150 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (len_path + len_filename + 2) * sizeof(WCHAR) )))
151 return NULL;
152 memcpy( ret, path, len_path * sizeof(WCHAR) );
153 ret[len_path] = '\\';
154 memcpy( ret + len_path + 1, filename, len_filename * sizeof(WCHAR) );
155 ret[len_path + 1 + len_filename] = 0;
156 return ret;
159 static IShellLinkW *load_shelllink( const WCHAR *path )
161 HRESULT hr;
162 IShellLinkW *link;
163 IPersistFile *file;
165 hr = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW,
166 (void **)&link );
167 if (FAILED( hr )) return NULL;
169 hr = IShellLinkW_QueryInterface( link, &IID_IPersistFile, (void **)&file );
170 if (FAILED( hr ))
172 IShellLinkW_Release( link );
173 return NULL;
175 hr = IPersistFile_Load( file, path, 0 );
176 IPersistFile_Release( file );
177 if (FAILED( hr ))
179 IShellLinkW_Release( link );
180 return NULL;
182 return link;
185 static HICON extract_icon( IShellLinkW *link )
187 WCHAR tmp_path[MAX_PATH], icon_path[MAX_PATH], target_path[MAX_PATH];
188 HICON icon = NULL;
189 int index;
191 tmp_path[0] = 0;
192 IShellLinkW_GetIconLocation( link, tmp_path, MAX_PATH, &index );
193 ExpandEnvironmentStringsW( tmp_path, icon_path, MAX_PATH );
195 if (icon_path[0]) ExtractIconExW( icon_path, index, &icon, NULL, 1 );
196 if (!icon)
198 tmp_path[0] = 0;
199 IShellLinkW_GetPath( link, tmp_path, MAX_PATH, NULL, SLGP_RAWPATH );
200 ExpandEnvironmentStringsW( tmp_path, target_path, MAX_PATH );
201 ExtractIconExW( target_path, index, &icon, NULL, 1 );
203 return icon;
206 static WCHAR *build_title( const WCHAR *filename, int len )
208 const WCHAR *p;
209 WCHAR *ret;
211 if (len == -1) len = strlenW( filename );
212 for (p = filename + len - 1; p >= filename; p--)
214 if (*p == '.')
216 len = p - filename;
217 break;
220 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
221 memcpy( ret, filename, len * sizeof(WCHAR) );
222 ret[len] = 0;
223 return ret;
226 static BOOL add_launcher( const WCHAR *folder, const WCHAR *filename, int len_filename )
228 struct launcher *launcher;
229 IShellLinkW *link;
231 if (nb_launchers == nb_allocated)
233 unsigned int count = nb_allocated * 2;
234 struct launcher **tmp = HeapReAlloc( GetProcessHeap(), 0, launchers, count * sizeof(*tmp) );
235 if (!tmp) return FALSE;
236 launchers = tmp;
237 nb_allocated = count;
240 if (!(launcher = HeapAlloc( GetProcessHeap(), 0, sizeof(*launcher) ))) return FALSE;
241 if (!(launcher->path = append_path( folder, filename, len_filename ))) goto error;
242 if (!(link = load_shelllink( launcher->path ))) goto error;
244 launcher->icon = extract_icon( link );
245 launcher->title = build_title( filename, len_filename );
246 IShellLinkW_Release( link );
247 if (launcher->icon && launcher->title)
249 launchers[nb_launchers++] = launcher;
250 return TRUE;
252 HeapFree( GetProcessHeap(), 0, launcher->title );
253 DestroyIcon( launcher->icon );
255 error:
256 HeapFree( GetProcessHeap(), 0, launcher->path );
257 HeapFree( GetProcessHeap(), 0, launcher );
258 return FALSE;
261 static void free_launcher( struct launcher *launcher )
263 DestroyIcon( launcher->icon );
264 HeapFree( GetProcessHeap(), 0, launcher->path );
265 HeapFree( GetProcessHeap(), 0, launcher->title );
266 HeapFree( GetProcessHeap(), 0, launcher );
269 static BOOL remove_launcher( const WCHAR *folder, const WCHAR *filename, int len_filename )
271 UINT i;
272 WCHAR *path;
273 BOOL ret = FALSE;
275 if (!(path = append_path( folder, filename, len_filename ))) return FALSE;
276 for (i = 0; i < nb_launchers; i++)
278 if (!strcmpiW( launchers[i]->path, path ))
280 free_launcher( launchers[i] );
281 if (--nb_launchers)
282 memmove( &launchers[i], &launchers[i + 1], sizeof(launchers[i]) * (nb_launchers - i) );
283 ret = TRUE;
284 break;
287 HeapFree( GetProcessHeap(), 0, path );
288 return ret;
291 static BOOL get_icon_text_metrics( HWND hwnd, TEXTMETRICW *tm )
293 BOOL ret;
294 HDC hdc;
295 LOGFONTW lf;
296 HFONT hfont;
298 hdc = GetDC( hwnd );
299 SystemParametersInfoW( SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0 );
300 hfont = SelectObject( hdc, CreateFontIndirectW( &lf ) );
301 ret = GetTextMetricsW( hdc, tm );
302 SelectObject( hdc, hfont );
303 ReleaseDC( hwnd, hdc );
304 return ret;
307 static BOOL process_changes( const WCHAR *folder, char *buf )
309 FILE_NOTIFY_INFORMATION *info = (FILE_NOTIFY_INFORMATION *)buf;
310 BOOL ret = FALSE;
312 for (;;)
314 switch (info->Action)
316 case FILE_ACTION_ADDED:
317 case FILE_ACTION_RENAMED_NEW_NAME:
318 if (add_launcher( folder, info->FileName, info->FileNameLength / sizeof(WCHAR) ))
319 ret = TRUE;
320 break;
322 case FILE_ACTION_REMOVED:
323 case FILE_ACTION_RENAMED_OLD_NAME:
324 if (remove_launcher( folder, info->FileName, info->FileNameLength / sizeof(WCHAR) ))
325 ret = TRUE;
326 break;
328 default:
329 WARN( "unexpected action %u\n", info->Action );
330 break;
332 if (!info->NextEntryOffset) break;
333 info = (FILE_NOTIFY_INFORMATION *)((char *)info + info->NextEntryOffset);
335 return ret;
338 static DWORD CALLBACK watch_desktop_folders( LPVOID param )
340 HWND hwnd = param;
341 HRESULT init = CoInitialize( NULL );
342 HANDLE dir0, dir1, events[2];
343 OVERLAPPED ovl0, ovl1;
344 char *buf0 = NULL, *buf1 = NULL;
345 DWORD count, size = 4096, error = ERROR_OUTOFMEMORY;
346 BOOL ret, redraw;
348 dir0 = CreateFileW( desktop_folder, FILE_LIST_DIRECTORY|SYNCHRONIZE, FILE_SHARE_READ|FILE_SHARE_WRITE,
349 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED, NULL );
350 if (dir0 == INVALID_HANDLE_VALUE) return GetLastError();
351 dir1 = CreateFileW( desktop_folder_public, FILE_LIST_DIRECTORY|SYNCHRONIZE, FILE_SHARE_READ|FILE_SHARE_WRITE,
352 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED, NULL );
353 if (dir1 == INVALID_HANDLE_VALUE)
355 CloseHandle( dir0 );
356 return GetLastError();
358 if (!(ovl0.hEvent = events[0] = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
359 if (!(ovl1.hEvent = events[1] = CreateEventW( NULL, FALSE, FALSE, NULL ))) goto error;
360 if (!(buf0 = HeapAlloc( GetProcessHeap(), 0, size ))) goto error;
361 if (!(buf1 = HeapAlloc( GetProcessHeap(), 0, size ))) goto error;
363 for (;;)
365 ret = ReadDirectoryChangesW( dir0, buf0, size, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME, NULL, &ovl0, NULL );
366 if (!ret)
368 error = GetLastError();
369 goto error;
371 ret = ReadDirectoryChangesW( dir1, buf1, size, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME, NULL, &ovl1, NULL );
372 if (!ret)
374 error = GetLastError();
375 goto error;
378 redraw = FALSE;
379 switch ((error = WaitForMultipleObjects( 2, events, FALSE, INFINITE )))
381 case WAIT_OBJECT_0:
382 if (!GetOverlappedResult( dir0, &ovl0, &count, FALSE ) || !count) break;
383 if (process_changes( desktop_folder, buf0 )) redraw = TRUE;
384 break;
386 case WAIT_OBJECT_0 + 1:
387 if (!GetOverlappedResult( dir1, &ovl1, &count, FALSE ) || !count) break;
388 if (process_changes( desktop_folder_public, buf1 )) redraw = TRUE;
389 break;
391 default:
392 goto error;
394 if (redraw) InvalidateRect( hwnd, NULL, TRUE );
397 error:
398 CloseHandle( dir0 );
399 CloseHandle( dir1 );
400 CloseHandle( events[0] );
401 CloseHandle( events[1] );
402 HeapFree( GetProcessHeap(), 0, buf0 );
403 HeapFree( GetProcessHeap(), 0, buf1 );
404 if (SUCCEEDED( init )) CoUninitialize();
405 return error;
408 static void add_folder( const WCHAR *folder )
410 static const WCHAR lnkW[] = {'\\','*','.','l','n','k',0};
411 int len = strlenW( folder ) + strlenW( lnkW );
412 WIN32_FIND_DATAW data;
413 HANDLE handle;
414 WCHAR *glob;
416 if (!(glob = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return;
417 strcpyW( glob, folder );
418 strcatW( glob, lnkW );
420 if ((handle = FindFirstFileW( glob, &data )) != INVALID_HANDLE_VALUE)
422 do { add_launcher( folder, data.cFileName, -1 ); } while (FindNextFileW( handle, &data ));
423 FindClose( handle );
425 HeapFree( GetProcessHeap(), 0, glob );
428 #define BORDER_SIZE 4
429 #define PADDING_SIZE 4
430 #define TITLE_CHARS 14
432 static void initialize_launchers( HWND hwnd )
434 HRESULT hr, init;
435 TEXTMETRICW tm;
436 int icon_size;
438 if (!(get_icon_text_metrics( hwnd, &tm ))) return;
440 icon_cx = GetSystemMetrics( SM_CXICON );
441 icon_cy = GetSystemMetrics( SM_CYICON );
442 icon_size = max( icon_cx, icon_cy );
443 title_cy = tm.tmHeight * 2;
444 title_cx = max( tm.tmAveCharWidth * TITLE_CHARS, icon_size + PADDING_SIZE + title_cy );
445 launcher_size = BORDER_SIZE + title_cx + BORDER_SIZE;
446 icon_offset_cx = (launcher_size - icon_cx) / 2;
447 icon_offset_cy = BORDER_SIZE + (icon_size - icon_cy) / 2;
448 title_offset_cx = BORDER_SIZE;
449 title_offset_cy = BORDER_SIZE + icon_size + PADDING_SIZE;
450 desktop_width = GetSystemMetrics( SM_CXSCREEN );
451 launchers_per_row = desktop_width / launcher_size;
453 hr = SHGetKnownFolderPath( &FOLDERID_Desktop, KF_FLAG_CREATE, NULL, &desktop_folder );
454 if (FAILED( hr ))
456 WINE_ERR("Could not get user desktop folder\n");
457 return;
459 hr = SHGetKnownFolderPath( &FOLDERID_PublicDesktop, KF_FLAG_CREATE, NULL, &desktop_folder_public );
460 if (FAILED( hr ))
462 WINE_ERR("Could not get public desktop folder\n");
463 CoTaskMemFree( desktop_folder );
464 return;
466 if ((launchers = HeapAlloc( GetProcessHeap(), 0, 2 * sizeof(launchers[0]) )))
468 nb_allocated = 2;
470 init = CoInitialize( NULL );
471 add_folder( desktop_folder );
472 add_folder( desktop_folder_public );
473 if (SUCCEEDED( init )) CoUninitialize();
475 CreateThread( NULL, 0, watch_desktop_folders, hwnd, 0, NULL );
479 /* screen saver handler */
480 static BOOL start_screensaver( void )
482 if (using_root)
484 const char *argv[3] = { "xdg-screensaver", "activate", NULL };
485 int pid = _spawnvp( _P_DETACH, argv[0], argv );
486 if (pid > 0)
488 WINE_TRACE( "started process %d\n", pid );
489 return TRUE;
492 return FALSE;
495 /* window procedure for the desktop window */
496 static LRESULT WINAPI desktop_wnd_proc( HWND hwnd, UINT message, WPARAM wp, LPARAM lp )
498 WINE_TRACE( "got msg %04x wp %lx lp %lx\n", message, wp, lp );
500 switch(message)
502 case WM_SYSCOMMAND:
503 switch(wp & 0xfff0)
505 case SC_CLOSE:
506 ExitWindows( 0, 0 );
507 break;
508 case SC_SCREENSAVE:
509 return start_screensaver();
511 return 0;
513 case WM_CLOSE:
514 PostQuitMessage(0);
515 return 0;
517 case WM_SETCURSOR:
518 return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
520 case WM_NCHITTEST:
521 return HTCLIENT;
523 case WM_ERASEBKGND:
524 if (!using_root) PaintDesktop( (HDC)wp );
525 return TRUE;
527 case WM_SETTINGCHANGE:
528 if (wp == SPI_SETDESKWALLPAPER)
529 SystemParametersInfoW( SPI_SETDESKWALLPAPER, 0, NULL, FALSE );
530 return 0;
532 case WM_LBUTTONDBLCLK:
533 if (!using_root)
535 const struct launcher *launcher = launcher_from_point( (short)LOWORD(lp), (short)HIWORD(lp) );
536 if (launcher) do_launch( launcher );
538 return 0;
540 case WM_PAINT:
542 PAINTSTRUCT ps;
543 BeginPaint( hwnd, &ps );
544 if (!using_root)
546 if (ps.fErase) PaintDesktop( ps.hdc );
547 draw_launchers( ps.hdc, ps.rcPaint );
549 EndPaint( hwnd, &ps );
551 return 0;
553 default:
554 return DefWindowProcW( hwnd, message, wp, lp );
558 /* create the desktop and the associated driver window, and make it the current desktop */
559 static BOOL create_desktop( HMODULE driver, const WCHAR *name, unsigned int width, unsigned int height )
561 static const WCHAR rootW[] = {'r','o','o','t',0};
562 BOOL ret = FALSE;
563 BOOL (CDECL *create_desktop_func)(unsigned int, unsigned int);
565 /* magic: desktop "root" means use the root window */
566 if (driver && strcmpiW( name, rootW ))
568 create_desktop_func = (void *)GetProcAddress( driver, "wine_create_desktop" );
569 if (create_desktop_func) ret = create_desktop_func( width, height );
571 return ret;
574 /* parse the desktop size specification */
575 static BOOL parse_size( const WCHAR *size, unsigned int *width, unsigned int *height )
577 WCHAR *end;
579 *width = strtoulW( size, &end, 10 );
580 if (end == size) return FALSE;
581 if (*end != 'x') return FALSE;
582 size = end + 1;
583 *height = strtoulW( size, &end, 10 );
584 return !*end;
587 /* retrieve the desktop name to use if not specified on the command line */
588 static const WCHAR *get_default_desktop_name(void)
590 static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
591 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
592 static const WCHAR explorer_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
593 'E','x','p','l','o','r','e','r',0};
594 static WCHAR buffer[MAX_PATH];
595 DWORD size = sizeof(buffer);
596 HDESK desk = GetThreadDesktop( GetCurrentThreadId() );
597 WCHAR *ret = NULL;
598 HKEY hkey;
600 if (desk && GetUserObjectInformationW( desk, UOI_NAME, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
602 if (strcmpiW( buffer, defaultW )) return buffer;
605 /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
606 if (!RegOpenKeyW( HKEY_CURRENT_USER, explorer_keyW, &hkey ))
608 if (!RegQueryValueExW( hkey, desktopW, 0, NULL, (LPBYTE)buffer, &size )) ret = buffer;
609 RegCloseKey( hkey );
611 return ret;
614 /* retrieve the default desktop size from the registry */
615 static BOOL get_default_desktop_size( const WCHAR *name, unsigned int *width, unsigned int *height )
617 static const WCHAR desktop_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
618 'E','x','p','l','o','r','e','r','\\',
619 'D','e','s','k','t','o','p','s',0};
620 HKEY hkey;
621 WCHAR buffer[64];
622 DWORD size = sizeof(buffer);
623 BOOL found = FALSE;
625 *width = 800;
626 *height = 600;
628 /* @@ Wine registry key: HKCU\Software\Wine\Explorer\Desktops */
629 if (!RegOpenKeyW( HKEY_CURRENT_USER, desktop_keyW, &hkey ))
631 if (!RegQueryValueExW( hkey, name, 0, NULL, (LPBYTE)buffer, &size ))
633 found = TRUE;
634 if (!parse_size( buffer, width, height )) *width = *height = 0;
636 RegCloseKey( hkey );
638 return found;
641 static BOOL get_default_enable_shell( const WCHAR *name )
643 static const WCHAR desktop_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
644 'E','x','p','l','o','r','e','r','\\',
645 'D','e','s','k','t','o','p','s',0};
646 static const WCHAR enable_shellW[] = {'E','n','a','b','l','e','S','h','e','l','l',0};
647 HKEY hkey;
648 BOOL result = FALSE;
649 DWORD size = sizeof(result);
651 /* @@ Wine registry key: HKCU\Software\Wine\Explorer\Desktops */
652 if (!RegOpenKeyW( HKEY_CURRENT_USER, desktop_keyW, &hkey ))
654 if (RegGetValueW( hkey, name, enable_shellW, RRF_RT_REG_DWORD, NULL, &result, &size ))
655 result = FALSE;
656 RegCloseKey( hkey );
658 return result;
661 static HMODULE load_graphics_driver( const WCHAR *driver, const GUID *guid )
663 static const WCHAR device_keyW[] = {
664 'S','y','s','t','e','m','\\',
665 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
666 'C','o','n','t','r','o','l','\\',
667 'V','i','d','e','o','\\',
668 '{','%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
669 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x','%','0','2','x',
670 '%','0','2','x','%','0','2','x','%','0','2','x','}','\\','0','0','0','0',0};
671 static const WCHAR graphics_driverW[] = {'G','r','a','p','h','i','c','s','D','r','i','v','e','r',0};
672 static const WCHAR driversW[] = {'S','o','f','t','w','a','r','e','\\',
673 'W','i','n','e','\\','D','r','i','v','e','r','s',0};
674 static const WCHAR graphicsW[] = {'G','r','a','p','h','i','c','s',0};
675 static const WCHAR drv_formatW[] = {'w','i','n','e','%','s','.','d','r','v',0};
677 WCHAR buffer[MAX_PATH], libname[32], *name, *next;
678 WCHAR key[sizeof(device_keyW)/sizeof(WCHAR) + 39];
679 HMODULE module = 0;
680 HKEY hkey;
681 char error[80];
683 if (!driver)
685 strcpyW( buffer, default_driver );
687 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
688 if (!RegOpenKeyW( HKEY_CURRENT_USER, driversW, &hkey ))
690 DWORD count = sizeof(buffer);
691 RegQueryValueExW( hkey, graphicsW, 0, NULL, (LPBYTE)buffer, &count );
692 RegCloseKey( hkey );
695 else lstrcpynW( buffer, driver, sizeof(buffer)/sizeof(WCHAR) );
697 name = buffer;
698 while (name)
700 next = strchrW( name, ',' );
701 if (next) *next++ = 0;
703 snprintfW( libname, sizeof(libname)/sizeof(WCHAR), drv_formatW, name );
704 if ((module = LoadLibraryW( libname )) != 0) break;
705 switch (GetLastError())
707 case ERROR_MOD_NOT_FOUND:
708 strcpy( error, "The graphics driver is missing. Check your build!" );
709 break;
710 case ERROR_DLL_INIT_FAILED:
711 strcpy( error, "Make sure that your X server is running and that $DISPLAY is set correctly." );
712 break;
713 default:
714 sprintf( error, "Unknown error (%u).", GetLastError() );
715 break;
717 name = next;
720 if (module)
722 GetModuleFileNameW( module, buffer, MAX_PATH );
723 TRACE( "display %s driver %s\n", debugstr_guid(guid), debugstr_w(buffer) );
726 sprintfW( key, device_keyW, guid->Data1, guid->Data2, guid->Data3,
727 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
728 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
730 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, key, 0, NULL,
731 REG_OPTION_VOLATILE, KEY_SET_VALUE, NULL, &hkey, NULL ))
733 if (module)
734 RegSetValueExW( hkey, graphics_driverW, 0, REG_SZ,
735 (BYTE *)buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
736 else
737 RegSetValueExA( hkey, "DriverError", 0, REG_SZ, (BYTE *)error, strlen(error) + 1 );
738 RegCloseKey( hkey );
741 return module;
744 static void initialize_display_settings(void)
746 DEVMODEW dmW;
748 /* Store current display mode in the registry */
749 if (EnumDisplaySettingsExW( NULL, ENUM_CURRENT_SETTINGS, &dmW, 0 ))
751 WINE_TRACE( "Current display mode %ux%u %u bpp %u Hz\n", dmW.dmPelsWidth,
752 dmW.dmPelsHeight, dmW.dmBitsPerPel, dmW.dmDisplayFrequency );
753 ChangeDisplaySettingsExW( NULL, &dmW, 0,
754 CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY,
755 NULL );
759 static void set_desktop_window_title( HWND hwnd, const WCHAR *name )
761 static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
762 static const WCHAR desktop_name_separatorW[] = {' ', '-', ' ', 0};
763 WCHAR *window_titleW = NULL;
764 int window_title_len;
766 if (!name[0])
768 SetWindowTextW( hwnd, desktop_nameW );
769 return;
772 window_title_len = strlenW(name) * sizeof(WCHAR)
773 + sizeof(desktop_name_separatorW)
774 + sizeof(desktop_nameW);
775 window_titleW = HeapAlloc( GetProcessHeap(), 0, window_title_len );
776 if (!window_titleW)
778 SetWindowTextW( hwnd, desktop_nameW );
779 return;
782 strcpyW( window_titleW, name );
783 strcatW( window_titleW, desktop_name_separatorW );
784 strcatW( window_titleW, desktop_nameW );
786 SetWindowTextW( hwnd, window_titleW );
787 HeapFree( GetProcessHeap(), 0, window_titleW );
790 /* main desktop management function */
791 void manage_desktop( WCHAR *arg )
793 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
794 HDESK desktop = 0;
795 GUID guid;
796 MSG msg;
797 HWND hwnd, msg_hwnd;
798 HMODULE graphics_driver;
799 unsigned int width, height;
800 WCHAR *cmdline = NULL, *driver = NULL;
801 WCHAR *p = arg;
802 const WCHAR *name = NULL;
803 BOOL enable_shell = FALSE;
805 /* get the rest of the command line (if any) */
806 while (*p && !isspace(*p)) p++;
807 if (*p)
809 *p++ = 0;
810 while (*p && isspace(*p)) p++;
811 if (*p) cmdline = p;
814 /* parse the desktop option */
815 /* the option is of the form /desktop=name[,widthxheight[,driver]] */
816 if (*arg == '=' || *arg == ',')
818 arg++;
819 name = arg;
820 if ((p = strchrW( arg, ',' )))
822 *p++ = 0;
823 if ((driver = strchrW( p, ',' ))) *driver++ = 0;
825 if (!p || !parse_size( p, &width, &height ))
826 get_default_desktop_size( name, &width, &height );
828 else if ((name = get_default_desktop_name()))
830 if (!get_default_desktop_size( name, &width, &height )) width = height = 0;
833 if (name)
834 enable_shell = get_default_enable_shell( name );
836 if (name && width && height)
838 if (!(desktop = CreateDesktopW( name, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL )))
840 WINE_ERR( "failed to create desktop %s error %d\n", wine_dbgstr_w(name), GetLastError() );
841 ExitProcess( 1 );
843 SetThreadDesktop( desktop );
846 UuidCreate( &guid );
847 TRACE( "display guid %s\n", debugstr_guid(&guid) );
848 graphics_driver = load_graphics_driver( driver, &guid );
850 /* create the desktop window */
851 hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
852 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 0, 0, 0, 0, 0, &guid );
854 /* create the HWND_MESSAGE parent */
855 msg_hwnd = CreateWindowExW( 0, messageW, NULL, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
856 0, 0, 100, 100, 0, 0, 0, NULL );
858 if (hwnd == GetDesktopWindow())
860 using_root = !desktop || !create_desktop( graphics_driver, name, width, height );
861 SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
862 SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
863 if (name) set_desktop_window_title( hwnd, name );
864 SetWindowPos( hwnd, 0, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN),
865 GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN),
866 SWP_SHOWWINDOW );
867 SystemParametersInfoW( SPI_SETDESKWALLPAPER, 0, NULL, FALSE );
868 ClipCursor( NULL );
869 initialize_display_settings();
870 initialize_appbar();
872 if (graphics_driver)
874 HMODULE shell32;
875 void (WINAPI *pShellDDEInit)( BOOL );
877 if (using_root) enable_shell = FALSE;
879 initialize_systray( graphics_driver, using_root, enable_shell );
880 if (!using_root) initialize_launchers( hwnd );
882 if ((shell32 = LoadLibraryA( "shell32.dll" )) &&
883 (pShellDDEInit = (void *)GetProcAddress( shell32, (LPCSTR)188)))
885 pShellDDEInit( TRUE );
889 else
891 DestroyWindow( hwnd ); /* someone beat us to it */
892 hwnd = 0;
895 if (GetAncestor( msg_hwnd, GA_PARENT )) DestroyWindow( msg_hwnd ); /* someone beat us to it */
897 /* if we have a command line, execute it */
898 if (cmdline)
900 STARTUPINFOW si;
901 PROCESS_INFORMATION pi;
903 memset( &si, 0, sizeof(si) );
904 si.cb = sizeof(si);
905 WINE_TRACE( "starting %s\n", wine_dbgstr_w(cmdline) );
906 if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
908 CloseHandle( pi.hThread );
909 CloseHandle( pi.hProcess );
913 /* run the desktop message loop */
914 if (hwnd)
916 WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
917 while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
918 WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
921 ExitProcess( 0 );