kernel32: Stub out GetFileMUIPath.
[wine.git] / programs / explorer / desktop.c
blob29ef8d7bb7ca77beb95915cff8b23176ab2045e3
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_PARENTNOTIFY:
533 if (LOWORD(wp) == WM_DESTROY) cleanup_systray_window( (HWND)lp );
534 return 0;
536 case WM_LBUTTONDBLCLK:
537 if (!using_root)
539 const struct launcher *launcher = launcher_from_point( (short)LOWORD(lp), (short)HIWORD(lp) );
540 if (launcher) do_launch( launcher );
542 return 0;
544 case WM_PAINT:
546 PAINTSTRUCT ps;
547 BeginPaint( hwnd, &ps );
548 if (!using_root)
550 if (ps.fErase) PaintDesktop( ps.hdc );
551 draw_launchers( ps.hdc, ps.rcPaint );
553 EndPaint( hwnd, &ps );
555 return 0;
557 default:
558 return DefWindowProcW( hwnd, message, wp, lp );
562 /* create the desktop and the associated driver window, and make it the current desktop */
563 static BOOL create_desktop( HMODULE driver, const WCHAR *name, unsigned int width, unsigned int height )
565 static const WCHAR rootW[] = {'r','o','o','t',0};
566 BOOL ret = FALSE;
567 BOOL (CDECL *create_desktop_func)(unsigned int, unsigned int);
569 /* magic: desktop "root" means use the root window */
570 if (driver && strcmpiW( name, rootW ))
572 create_desktop_func = (void *)GetProcAddress( driver, "wine_create_desktop" );
573 if (create_desktop_func) ret = create_desktop_func( width, height );
575 return ret;
578 /* parse the desktop size specification */
579 static BOOL parse_size( const WCHAR *size, unsigned int *width, unsigned int *height )
581 WCHAR *end;
583 *width = strtoulW( size, &end, 10 );
584 if (end == size) return FALSE;
585 if (*end != 'x') return FALSE;
586 size = end + 1;
587 *height = strtoulW( size, &end, 10 );
588 return !*end;
591 /* retrieve the desktop name to use if not specified on the command line */
592 static const WCHAR *get_default_desktop_name(void)
594 static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
595 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
596 static const WCHAR explorer_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
597 'E','x','p','l','o','r','e','r',0};
598 static WCHAR buffer[MAX_PATH];
599 DWORD size = sizeof(buffer);
600 HDESK desk = GetThreadDesktop( GetCurrentThreadId() );
601 WCHAR *ret = NULL;
602 HKEY hkey;
604 if (desk && GetUserObjectInformationW( desk, UOI_NAME, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
606 if (strcmpiW( buffer, defaultW )) return buffer;
609 /* @@ Wine registry key: HKCU\Software\Wine\Explorer */
610 if (!RegOpenKeyW( HKEY_CURRENT_USER, explorer_keyW, &hkey ))
612 if (!RegQueryValueExW( hkey, desktopW, 0, NULL, (LPBYTE)buffer, &size )) ret = buffer;
613 RegCloseKey( hkey );
615 return ret;
618 /* retrieve the default desktop size from the registry */
619 static BOOL get_default_desktop_size( const WCHAR *name, unsigned int *width, unsigned int *height )
621 static const WCHAR desktop_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
622 'E','x','p','l','o','r','e','r','\\',
623 'D','e','s','k','t','o','p','s',0};
624 HKEY hkey;
625 WCHAR buffer[64];
626 DWORD size = sizeof(buffer);
627 BOOL found = FALSE;
629 *width = 800;
630 *height = 600;
632 /* @@ Wine registry key: HKCU\Software\Wine\Explorer\Desktops */
633 if (!RegOpenKeyW( HKEY_CURRENT_USER, desktop_keyW, &hkey ))
635 if (!RegQueryValueExW( hkey, name, 0, NULL, (LPBYTE)buffer, &size ))
637 found = TRUE;
638 if (!parse_size( buffer, width, height )) *width = *height = 0;
640 RegCloseKey( hkey );
642 return found;
645 static BOOL get_default_enable_shell( const WCHAR *name )
647 static const WCHAR desktop_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
648 'E','x','p','l','o','r','e','r','\\',
649 'D','e','s','k','t','o','p','s',0};
650 static const WCHAR enable_shellW[] = {'E','n','a','b','l','e','S','h','e','l','l',0};
651 static const WCHAR shellW[] = {'s','h','e','l','l',0};
652 HKEY hkey;
653 BOOL found = FALSE;
654 BOOL result;
655 DWORD size = sizeof(result);
657 /* @@ Wine registry key: HKCU\Software\Wine\Explorer\Desktops */
658 if (!RegOpenKeyW( HKEY_CURRENT_USER, desktop_keyW, &hkey ))
660 if (!RegGetValueW( hkey, name, enable_shellW, RRF_RT_REG_DWORD, NULL, &result, &size ))
661 found = TRUE;
662 RegCloseKey( hkey );
664 /* Default off, except for the magic desktop name "shell" */
665 if (!found)
666 result = (lstrcmpiW( name, shellW ) == 0);
667 return result;
670 static HMODULE load_graphics_driver( const WCHAR *driver, const GUID *guid )
672 static const WCHAR device_keyW[] = {
673 'S','y','s','t','e','m','\\',
674 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
675 'C','o','n','t','r','o','l','\\',
676 'V','i','d','e','o','\\',
677 '{','%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
678 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x','%','0','2','x',
679 '%','0','2','x','%','0','2','x','%','0','2','x','}','\\','0','0','0','0',0};
680 static const WCHAR graphics_driverW[] = {'G','r','a','p','h','i','c','s','D','r','i','v','e','r',0};
681 static const WCHAR driversW[] = {'S','o','f','t','w','a','r','e','\\',
682 'W','i','n','e','\\','D','r','i','v','e','r','s',0};
683 static const WCHAR graphicsW[] = {'G','r','a','p','h','i','c','s',0};
684 static const WCHAR drv_formatW[] = {'w','i','n','e','%','s','.','d','r','v',0};
686 WCHAR buffer[MAX_PATH], libname[32], *name, *next;
687 WCHAR key[sizeof(device_keyW)/sizeof(WCHAR) + 39];
688 HMODULE module = 0;
689 HKEY hkey;
690 char error[80];
692 if (!driver)
694 strcpyW( buffer, default_driver );
696 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
697 if (!RegOpenKeyW( HKEY_CURRENT_USER, driversW, &hkey ))
699 DWORD count = sizeof(buffer);
700 RegQueryValueExW( hkey, graphicsW, 0, NULL, (LPBYTE)buffer, &count );
701 RegCloseKey( hkey );
704 else lstrcpynW( buffer, driver, sizeof(buffer)/sizeof(WCHAR) );
706 name = buffer;
707 while (name)
709 next = strchrW( name, ',' );
710 if (next) *next++ = 0;
712 snprintfW( libname, sizeof(libname)/sizeof(WCHAR), drv_formatW, name );
713 if ((module = LoadLibraryW( libname )) != 0) break;
714 switch (GetLastError())
716 case ERROR_MOD_NOT_FOUND:
717 strcpy( error, "The graphics driver is missing. Check your build!" );
718 break;
719 case ERROR_DLL_INIT_FAILED:
720 strcpy( error, "Make sure that your X server is running and that $DISPLAY is set correctly." );
721 break;
722 default:
723 sprintf( error, "Unknown error (%u).", GetLastError() );
724 break;
726 name = next;
729 if (module)
731 GetModuleFileNameW( module, buffer, MAX_PATH );
732 TRACE( "display %s driver %s\n", debugstr_guid(guid), debugstr_w(buffer) );
735 sprintfW( key, device_keyW, guid->Data1, guid->Data2, guid->Data3,
736 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
737 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
739 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, key, 0, NULL,
740 REG_OPTION_VOLATILE, KEY_SET_VALUE, NULL, &hkey, NULL ))
742 if (module)
743 RegSetValueExW( hkey, graphics_driverW, 0, REG_SZ,
744 (BYTE *)buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
745 else
746 RegSetValueExA( hkey, "DriverError", 0, REG_SZ, (BYTE *)error, strlen(error) + 1 );
747 RegCloseKey( hkey );
750 return module;
753 static void initialize_display_settings(void)
755 DEVMODEW dmW;
757 /* Store current display mode in the registry */
758 if (EnumDisplaySettingsExW( NULL, ENUM_CURRENT_SETTINGS, &dmW, 0 ))
760 WINE_TRACE( "Current display mode %ux%u %u bpp %u Hz\n", dmW.dmPelsWidth,
761 dmW.dmPelsHeight, dmW.dmBitsPerPel, dmW.dmDisplayFrequency );
762 ChangeDisplaySettingsExW( NULL, &dmW, 0,
763 CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY,
764 NULL );
768 static void set_desktop_window_title( HWND hwnd, const WCHAR *name )
770 static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
771 static const WCHAR desktop_name_separatorW[] = {' ', '-', ' ', 0};
772 WCHAR *window_titleW = NULL;
773 int window_title_len;
775 if (!name[0])
777 SetWindowTextW( hwnd, desktop_nameW );
778 return;
781 window_title_len = strlenW(name) * sizeof(WCHAR)
782 + sizeof(desktop_name_separatorW)
783 + sizeof(desktop_nameW);
784 window_titleW = HeapAlloc( GetProcessHeap(), 0, window_title_len );
785 if (!window_titleW)
787 SetWindowTextW( hwnd, desktop_nameW );
788 return;
791 strcpyW( window_titleW, name );
792 strcatW( window_titleW, desktop_name_separatorW );
793 strcatW( window_titleW, desktop_nameW );
795 SetWindowTextW( hwnd, window_titleW );
796 HeapFree( GetProcessHeap(), 0, window_titleW );
799 /* main desktop management function */
800 void manage_desktop( WCHAR *arg )
802 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
803 HDESK desktop = 0;
804 GUID guid;
805 MSG msg;
806 HWND hwnd;
807 HMODULE graphics_driver;
808 unsigned int width, height;
809 WCHAR *cmdline = NULL, *driver = NULL;
810 WCHAR *p = arg;
811 const WCHAR *name = NULL;
812 BOOL enable_shell = FALSE;
814 /* get the rest of the command line (if any) */
815 while (*p && !isspace(*p)) p++;
816 if (*p)
818 *p++ = 0;
819 while (*p && isspace(*p)) p++;
820 if (*p) cmdline = p;
823 /* parse the desktop option */
824 /* the option is of the form /desktop=name[,widthxheight[,driver]] */
825 if (*arg == '=' || *arg == ',')
827 arg++;
828 name = arg;
829 if ((p = strchrW( arg, ',' )))
831 *p++ = 0;
832 if ((driver = strchrW( p, ',' ))) *driver++ = 0;
834 if (!p || !parse_size( p, &width, &height ))
835 get_default_desktop_size( name, &width, &height );
837 else if ((name = get_default_desktop_name()))
839 if (!get_default_desktop_size( name, &width, &height )) width = height = 0;
842 if (name)
843 enable_shell = get_default_enable_shell( name );
845 if (name && width && height)
847 if (!(desktop = CreateDesktopW( name, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL )))
849 WINE_ERR( "failed to create desktop %s error %d\n", wine_dbgstr_w(name), GetLastError() );
850 ExitProcess( 1 );
852 SetThreadDesktop( desktop );
855 UuidCreate( &guid );
856 TRACE( "display guid %s\n", debugstr_guid(&guid) );
857 graphics_driver = load_graphics_driver( driver, &guid );
859 /* create the desktop window */
860 hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
861 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 0, 0, 0, 0, 0, &guid );
863 if (hwnd)
865 /* create the HWND_MESSAGE parent */
866 CreateWindowExW( 0, messageW, NULL, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
867 0, 0, 100, 100, 0, 0, 0, NULL );
869 using_root = !desktop || !create_desktop( graphics_driver, name, width, height );
870 SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
871 SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
872 if (name) set_desktop_window_title( hwnd, name );
873 SetWindowPos( hwnd, 0, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN),
874 GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN),
875 SWP_SHOWWINDOW );
876 SystemParametersInfoW( SPI_SETDESKWALLPAPER, 0, NULL, FALSE );
877 ClipCursor( NULL );
878 initialize_display_settings();
879 initialize_appbar();
881 if (graphics_driver)
883 HMODULE shell32;
884 void (WINAPI *pShellDDEInit)( BOOL );
886 if (using_root) enable_shell = FALSE;
888 initialize_systray( graphics_driver, using_root, enable_shell );
889 if (!using_root) initialize_launchers( hwnd );
891 if ((shell32 = LoadLibraryA( "shell32.dll" )) &&
892 (pShellDDEInit = (void *)GetProcAddress( shell32, (LPCSTR)188)))
894 pShellDDEInit( TRUE );
899 /* if we have a command line, execute it */
900 if (cmdline)
902 STARTUPINFOW si;
903 PROCESS_INFORMATION pi;
905 memset( &si, 0, sizeof(si) );
906 si.cb = sizeof(si);
907 WINE_TRACE( "starting %s\n", wine_dbgstr_w(cmdline) );
908 if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
910 CloseHandle( pi.hThread );
911 CloseHandle( pi.hProcess );
915 /* run the desktop message loop */
916 if (hwnd)
918 WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
919 while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
920 WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
923 ExitProcess( 0 );