explorer: Remove SWP_NOACTIVATE to ensure that the foreground window is set to the...
[wine.git] / programs / explorer / desktop.c
blob1a8db99ab7b1fc23db15609561ca3d77d1d8e840
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 HMODULE load_graphics_driver( const WCHAR *driver, const GUID *guid )
643 static const WCHAR device_keyW[] = {
644 'S','y','s','t','e','m','\\',
645 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
646 'C','o','n','t','r','o','l','\\',
647 'V','i','d','e','o','\\',
648 '{','%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
649 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x','%','0','2','x',
650 '%','0','2','x','%','0','2','x','%','0','2','x','}','\\','0','0','0','0',0};
651 static const WCHAR graphics_driverW[] = {'G','r','a','p','h','i','c','s','D','r','i','v','e','r',0};
652 static const WCHAR driversW[] = {'S','o','f','t','w','a','r','e','\\',
653 'W','i','n','e','\\','D','r','i','v','e','r','s',0};
654 static const WCHAR graphicsW[] = {'G','r','a','p','h','i','c','s',0};
655 static const WCHAR drv_formatW[] = {'w','i','n','e','%','s','.','d','r','v',0};
657 WCHAR buffer[MAX_PATH], libname[32], *name, *next;
658 WCHAR key[sizeof(device_keyW)/sizeof(WCHAR) + 39];
659 HMODULE module = 0;
660 HKEY hkey;
661 char error[80];
663 if (!driver)
665 strcpyW( buffer, default_driver );
667 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
668 if (!RegOpenKeyW( HKEY_CURRENT_USER, driversW, &hkey ))
670 DWORD count = sizeof(buffer);
671 RegQueryValueExW( hkey, graphicsW, 0, NULL, (LPBYTE)buffer, &count );
672 RegCloseKey( hkey );
675 else lstrcpynW( buffer, driver, sizeof(buffer)/sizeof(WCHAR) );
677 name = buffer;
678 while (name)
680 next = strchrW( name, ',' );
681 if (next) *next++ = 0;
683 snprintfW( libname, sizeof(libname)/sizeof(WCHAR), drv_formatW, name );
684 if ((module = LoadLibraryW( libname )) != 0) break;
685 switch (GetLastError())
687 case ERROR_MOD_NOT_FOUND:
688 strcpy( error, "The graphics driver is missing. Check your build!" );
689 break;
690 case ERROR_DLL_INIT_FAILED:
691 strcpy( error, "Make sure that your X server is running and that $DISPLAY is set correctly." );
692 break;
693 default:
694 sprintf( error, "Unknown error (%u).", GetLastError() );
695 break;
697 name = next;
700 if (module)
702 GetModuleFileNameW( module, buffer, MAX_PATH );
703 TRACE( "display %s driver %s\n", debugstr_guid(guid), debugstr_w(buffer) );
706 sprintfW( key, device_keyW, guid->Data1, guid->Data2, guid->Data3,
707 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
708 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
710 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, key, 0, NULL,
711 REG_OPTION_VOLATILE, KEY_SET_VALUE, NULL, &hkey, NULL ))
713 if (module)
714 RegSetValueExW( hkey, graphics_driverW, 0, REG_SZ,
715 (BYTE *)buffer, (strlenW(buffer) + 1) * sizeof(WCHAR) );
716 else
717 RegSetValueExA( hkey, "DriverError", 0, REG_SZ, (BYTE *)error, strlen(error) + 1 );
718 RegCloseKey( hkey );
721 return module;
724 static void initialize_display_settings(void)
726 DEVMODEW dmW;
728 /* Store current display mode in the registry */
729 if (EnumDisplaySettingsExW( NULL, ENUM_CURRENT_SETTINGS, &dmW, 0 ))
731 WINE_TRACE( "Current display mode %ux%u %u bpp %u Hz\n", dmW.dmPelsWidth,
732 dmW.dmPelsHeight, dmW.dmBitsPerPel, dmW.dmDisplayFrequency );
733 ChangeDisplaySettingsExW( NULL, &dmW, 0,
734 CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY,
735 NULL );
739 static void set_desktop_window_title( HWND hwnd, const WCHAR *name )
741 static const WCHAR desktop_nameW[] = {'W','i','n','e',' ','d','e','s','k','t','o','p',0};
742 static const WCHAR desktop_name_separatorW[] = {' ', '-', ' ', 0};
743 WCHAR *window_titleW = NULL;
744 int window_title_len;
746 if (!name[0])
748 SetWindowTextW( hwnd, desktop_nameW );
749 return;
752 window_title_len = strlenW(name) * sizeof(WCHAR)
753 + sizeof(desktop_name_separatorW)
754 + sizeof(desktop_nameW);
755 window_titleW = HeapAlloc( GetProcessHeap(), 0, window_title_len );
756 if (!window_titleW)
758 SetWindowTextW( hwnd, desktop_nameW );
759 return;
762 strcpyW( window_titleW, name );
763 strcatW( window_titleW, desktop_name_separatorW );
764 strcatW( window_titleW, desktop_nameW );
766 SetWindowTextW( hwnd, window_titleW );
767 HeapFree( GetProcessHeap(), 0, window_titleW );
770 /* main desktop management function */
771 void manage_desktop( WCHAR *arg )
773 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
774 HDESK desktop = 0;
775 GUID guid;
776 MSG msg;
777 HWND hwnd, msg_hwnd;
778 HMODULE graphics_driver;
779 unsigned int width, height;
780 WCHAR *cmdline = NULL, *driver = NULL;
781 WCHAR *p = arg;
782 const WCHAR *name = NULL;
784 /* get the rest of the command line (if any) */
785 while (*p && !isspace(*p)) p++;
786 if (*p)
788 *p++ = 0;
789 while (*p && isspace(*p)) p++;
790 if (*p) cmdline = p;
793 /* parse the desktop option */
794 /* the option is of the form /desktop=name[,widthxheight[,driver]] */
795 if (*arg == '=' || *arg == ',')
797 arg++;
798 name = arg;
799 if ((p = strchrW( arg, ',' )))
801 *p++ = 0;
802 if ((driver = strchrW( p, ',' ))) *driver++ = 0;
804 if (!p || !parse_size( p, &width, &height ))
805 get_default_desktop_size( name, &width, &height );
807 else if ((name = get_default_desktop_name()))
809 if (!get_default_desktop_size( name, &width, &height )) width = height = 0;
812 if (name && width && height)
814 if (!(desktop = CreateDesktopW( name, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL )))
816 WINE_ERR( "failed to create desktop %s error %d\n", wine_dbgstr_w(name), GetLastError() );
817 ExitProcess( 1 );
819 SetThreadDesktop( desktop );
822 UuidCreate( &guid );
823 TRACE( "display guid %s\n", debugstr_guid(&guid) );
824 graphics_driver = load_graphics_driver( driver, &guid );
826 /* create the desktop window */
827 hwnd = CreateWindowExW( 0, DESKTOP_CLASS_ATOM, NULL,
828 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 0, 0, 0, 0, 0, &guid );
830 /* create the HWND_MESSAGE parent */
831 msg_hwnd = CreateWindowExW( 0, messageW, NULL, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
832 0, 0, 100, 100, 0, 0, 0, NULL );
834 if (hwnd == GetDesktopWindow())
836 using_root = !desktop || !create_desktop( graphics_driver, name, width, height );
837 SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)desktop_wnd_proc );
838 SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( 0, MAKEINTRESOURCEW(OIC_WINLOGO)));
839 if (name) set_desktop_window_title( hwnd, name );
840 SetWindowPos( hwnd, 0, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN),
841 GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN),
842 SWP_SHOWWINDOW );
843 SystemParametersInfoW( SPI_SETDESKWALLPAPER, 0, NULL, FALSE );
844 ClipCursor( NULL );
845 initialize_display_settings();
846 initialize_appbar();
848 if (graphics_driver)
850 HMODULE shell32;
851 void (WINAPI *pShellDDEInit)( BOOL );
853 initialize_systray( graphics_driver, using_root );
854 if (!using_root) initialize_launchers( hwnd );
856 if ((shell32 = LoadLibraryA( "shell32.dll" )) &&
857 (pShellDDEInit = (void *)GetProcAddress( shell32, (LPCSTR)188)))
859 pShellDDEInit( TRUE );
863 else
865 DestroyWindow( hwnd ); /* someone beat us to it */
866 hwnd = 0;
869 if (GetAncestor( msg_hwnd, GA_PARENT )) DestroyWindow( msg_hwnd ); /* someone beat us to it */
871 /* if we have a command line, execute it */
872 if (cmdline)
874 STARTUPINFOW si;
875 PROCESS_INFORMATION pi;
877 memset( &si, 0, sizeof(si) );
878 si.cb = sizeof(si);
879 WINE_TRACE( "starting %s\n", wine_dbgstr_w(cmdline) );
880 if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
882 CloseHandle( pi.hThread );
883 CloseHandle( pi.hProcess );
887 /* run the desktop message loop */
888 if (hwnd)
890 WINE_TRACE( "desktop message loop starting on hwnd %p\n", hwnd );
891 while (GetMessageW( &msg, 0, 0, 0 )) DispatchMessageW( &msg );
892 WINE_TRACE( "desktop message loop exiting for hwnd %p\n", hwnd );
895 ExitProcess( 0 );