- Use a separate icon for minimizing windows.
[reactos.git] / reactos / subsys / system / explorer / explorer.cpp
index 90e91ef..d2f1df1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003, 2004 Martin Fuchs
+ * Copyright 2003, 2004, 2005 Martin Fuchs
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -28,9 +28,9 @@
  //
 
 
-#include "precomp.h"
+#include <precomp.h>   // <precomp.h> instead of "precomp.h" because the ROS build system needs this to find the precompiled header file (*.gch) in the output directory tree
 
-#include "explorer_intres.h"
+#include "resource.h"
 
 #include <locale.h>    // for setlocale()
 
 
 #include "dialogs/settings.h"  // for MdiSdiDlg
 
+#include "services/shellservices.h"
+
 
 extern "C" int initialize_gdb_stub();  // start up GDB stub
 
 
+DynamicLoadLibFct<void(__stdcall*)(BOOL)> g_SHDOCVW_ShellDDEInit(TEXT("SHDOCVW"), 118);
+
+
 ExplorerGlobals g_Globals;
 
 
 ExplorerGlobals::ExplorerGlobals()
 {
        _hInstance = 0;
-       _hframeClass = 0;
        _cfStrFName = 0;
+
+#ifndef ROSSHELL
+       _hframeClass = 0;
        _hMainWnd = 0;
-       _prescan_nodes = false;
        _desktop_mode = false;
+       _prescan_nodes = false;
+#endif
+
        _log = NULL;
 #ifndef __MINGW32__    // SHRestricted() missing in MinGW (as of 29.10.2003)
        _SHRestricted = 0;
@@ -173,9 +182,7 @@ bool FileTypeManager::is_exe_file(LPCTSTR ext)
 
 const FileTypeInfo& FileTypeManager::operator[](String ext)
 {
-#ifndef __WINE__ ///@todo _tcslwr() for Wine
-       _tcslwr((LPTSTR)ext.c_str());
-#endif
+       ext.toLower();
 
        iterator found = find(ext);
        if (found != end())
@@ -221,8 +228,7 @@ LPCTSTR FileTypeManager::set_type(Entry* entry, bool dont_hide_ext)
                if (type._neverShowExt && !dont_hide_ext) {
                        int len = ext - entry->_data.cFileName;
                        entry->_display_name = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
-                       _tcsncpy(entry->_display_name, entry->_data.cFileName, len);
-                       entry->_display_name[len] = TEXT('\0');
+                       lstrcpyn(entry->_display_name, entry->_data.cFileName, len + 1);
                }
 
                if (is_exe_file(ext))
@@ -240,10 +246,10 @@ Icon::Icon()
 {
 }
 
-Icon::Icon(ICON_ID id, UINT nid)
+Icon::Icon(ICON_ID id, UINT nid)       //, int cx, int cy
  :     _id(id),
        _itype(IT_STATIC),
-       _hicon(SmallIcon(nid))
+       _hicon(ResIcon(nid))    // ResIconEx(nid, cx, cy)
 {
 }
 
@@ -316,7 +322,24 @@ int Icon::add_to_imagelist(HIMAGELIST himl, HDC hdc_wnd, COLORREF bk_color, HBRU
        return ret;
 }
 
-HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
+HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd/*, bool large_icons*/)
+{
+       int cx = GetSystemMetrics(SM_CXSMICON); //ICON_SIZE_X;
+       int cy = GetSystemMetrics(SM_CYSMICON); //ICON_SIZE_Y;
+       HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
+
+       MemCanvas canvas;
+       BitmapSelection sel(canvas, hbmp);
+
+       RECT rect = {0, 0, cx, cy};
+       FillRect(canvas, &rect, hbrush_bkgnd);
+
+       DrawIconEx(canvas, 0, 0, hIcon, cx, cy, 0, hbrush_bkgnd, DI_NORMAL);
+
+       return hbmp;
+}
+
+HBITMAP create_small_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
 {
        int cx = GetSystemMetrics(SM_CXSMICON);
        int cy = GetSystemMetrics(SM_CYSMICON);
@@ -370,49 +393,94 @@ void IconCache::init()
        _icons[ICID_COMPUTER]   = Icon(ICID_COMPUTER,   IDI_COMPUTER);
        _icons[ICID_LOGOFF]             = Icon(ICID_LOGOFF,             IDI_LOGOFF);
        _icons[ICID_BOOKMARK]   = Icon(ICID_BOOKMARK,   IDI_DOT_TRANS);
+    _icons[ICID_MINIMIZE]      = Icon(ICID_MINIMIZE,   IDI_MINIMIZE);
 }
 
 
-const Icon& IconCache::extract(const String& path)
+const Icon& IconCache::extract(LPCTSTR path, ICONCACHE_FLAGS flags)
 {
-       PathMap::iterator found = _pathMap.find(path);
+        // search for matching icon with unchanged flags in the cache
+       CacheKey mapkey(path, flags);
+       PathCacheMap::iterator found = _pathCache.find(mapkey);
 
-       if (found != _pathMap.end())
+       if (found != _pathCache.end())
                return _icons[found->second];
 
+        // search for matching icon with handle
+       CacheKey mapkey_hicon(path, flags|ICF_HICON);
+       if (flags != mapkey_hicon.second) {
+               found = _pathCache.find(mapkey_hicon);
+
+               if (found != _pathCache.end())
+                       return _icons[found->second];
+       }
+
+        // search for matching icon in the system image list cache
+       CacheKey mapkey_syscache(path, flags|ICF_SYSCACHE);
+       if (flags != mapkey_syscache.second) {
+               found = _pathCache.find(mapkey_syscache);
+
+               if (found != _pathCache.end())
+                       return _icons[found->second];
+       }
+
        SHFILEINFO sfi;
 
-#if 1  // use system image list
-       HIMAGELIST himlSys = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
+       int shgfi_flags = 0;
 
-       if (himlSys) {
-               _himlSys = himlSys;
+       if (flags & ICF_OPEN)
+               shgfi_flags |= SHGFI_OPENICON;
 
-               const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
-#else
-       if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_SMALLICON)) {
-               const Icon& icon = add(sfi.hIcon, IT_CACHED);
-#endif
+       if ((flags&(ICF_LARGE|ICF_OVERLAYS|ICF_HICON)) && !(flags&ICF_SYSCACHE)) {
+               shgfi_flags |= SHGFI_ICON;
 
-               ///@todo limit cache size
-               _pathMap[path] = icon;
+               if (!(flags & ICF_LARGE))
+                       shgfi_flags |= SHGFI_SMALLICON;
 
-               return icon;
-       } else
-               return _icons[ICID_NONE];
+               if (flags & ICF_OVERLAYS)
+                       shgfi_flags |= SHGFI_ADDOVERLAYS;
+
+                // get small/big icons with/without overlays
+               if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags)) {
+                       const Icon& icon = add(sfi.hIcon, IT_CACHED);
+
+                       ///@todo limit cache size
+                       _pathCache[mapkey_hicon] = icon;
+
+                       return icon;
+               }
+       } else {
+               assert(!(flags&ICF_OVERLAYS));
+
+               shgfi_flags |= SHGFI_SYSICONINDEX|SHGFI_SMALLICON;
+
+                // use system image list - the "search program dialog" needs it
+               HIMAGELIST himlSys_small = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), shgfi_flags);
+
+               if (himlSys_small) {
+                       _himlSys_small = himlSys_small;
+
+                       const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
+
+                       ///@todo limit cache size
+                       _pathCache[mapkey_syscache] = icon;
+
+                       return icon;
+               }
+       }
+
+       return _icons[ICID_NONE];
 }
 
-const Icon& IconCache::extract(LPCTSTR path, int idx)
+const Icon& IconCache::extract(LPCTSTR path, int idx, ICONCACHE_FLAGS flags)
 {
-       CachePair key(path, idx);
+       IdxCacheKey key(path, make_pair(idx, (flags|ICF_HICON)&~ICF_SYSCACHE));
 
-#ifndef __WINE__ ///@todo _tcslwr() for Wine
-       _tcslwr((LPTSTR)key.first.c_str());
-#endif
+       key.first.toLower();
 
-       PathIdxMap::iterator found = _pathIdxMap.find(key);
+       IdxCacheMap::iterator found = _idxCache.find(key);
 
-       if (found != _pathIdxMap.end())
+       if (found != _idxCache.end())
                return _icons[found->second];
 
        HICON hIcon;
@@ -420,7 +488,7 @@ const Icon& IconCache::extract(LPCTSTR path, int idx)
        if ((int)ExtractIconEx(path, idx, NULL, &hIcon, 1) > 0) {
                const Icon& icon = add(hIcon, IT_CACHED);
 
-               _pathIdxMap[key] = icon;
+               _idxCache[key] = icon;
 
                return icon;
        } else {
@@ -431,19 +499,27 @@ const Icon& IconCache::extract(LPCTSTR path, int idx)
        }
 }
 
-const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx)
+const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx, ICONCACHE_FLAGS flags)
 {
        HICON hIconLarge = 0;
        HICON hIcon;
 
-       HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
+       bool large_icons = flags & ICF_LARGE;
+       HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(GetSystemMetrics(SM_CXICON), ICON_SIZE_X));
 
        if (hr == NOERROR) {    //@@ oder SUCCEEDED(hr) ?
-               if (hIconLarge)
-                       DestroyIcon(hIconLarge);
+               if (large_icons) {      //@@ OK?
+                       if (hIcon)
+                               DestroyIcon(hIcon);
+
+                       hIcon = hIconLarge;
+               } else {
+                       if (hIconLarge)
+                               DestroyIcon(hIconLarge);
+               }
 
                if (hIcon)
-                       return add(hIcon);
+                       return add(hIcon);      //@@ When do we want not to free this icons?
        }
 
        return _icons[ICID_NONE];
@@ -510,10 +586,10 @@ ResIconEx::ResIconEx(UINT nid, int w, int h)
 void SetWindowIcon(HWND hwnd, UINT nid)
 {
        HICON hIcon = ResIcon(nid);
-       Window_SetIcon(hwnd, ICON_BIG, hIcon);
+       (void)Window_SetIcon(hwnd, ICON_BIG, hIcon);
 
        HICON hIconSmall = SmallIcon(nid);
-       Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
+       (void)Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
 }
 
 
@@ -523,8 +599,12 @@ ResBitmap::ResBitmap(UINT nid)
 }
 
 
-void explorer_show_frame(int cmdshow, LPTSTR lpCmdLine)
+#ifndef ROSSHELL
+
+void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
 {
+       ExplorerCmd cmd;
+
        if (g_Globals._hMainWnd) {
                if (IsIconic(g_Globals._hMainWnd))
                        ShowWindow(g_Globals._hMainWnd, SW_RESTORE);
@@ -539,15 +619,130 @@ void explorer_show_frame(int cmdshow, LPTSTR lpCmdLine)
        XMLPos explorer_options = g_Globals.get_cfg("general/explorer");
        XS_String mdiStr = XMLString(explorer_options, "mdi");
 
+        // If there isn't yet the "mdi" setting in the configuration, display MDI/SDI dialog.
        if (mdiStr.empty())
                Dialog::DoModal(IDD_MDI_SDI, WINDOW_CREATOR(MdiSdiDlg), g_Globals._hwndDesktop);
 
-       bool mdi = XMLBool(explorer_options, "mdi", true);
+        // Now read the MDI attribute again and interpret it as boolean value.
+       cmd._mdi = XMLBool(explorer_options, "mdi", true);
+
+       cmd._cmdShow = cmdShow;
+
+        // parse command line options, which may overwrite the MDI flag
+       if (lpCmdLine)
+               cmd.ParseCmdLine(lpCmdLine);
 
         // create main window
-       MainFrameBase::Create(lpCmdLine, mdi, cmdshow);
+       MainFrameBase::Create(cmd);
+}
+
+bool ExplorerCmd::ParseCmdLine(LPCTSTR lpCmdLine)
+{
+       bool ok = true;
+
+       LPCTSTR b = lpCmdLine;
+       LPCTSTR p = b;
+
+       while(*b) {
+                // remove leading space
+               while(_istspace((unsigned)*b))
+                       ++b;
+
+               p = b;
+
+               bool quote = false;
+
+                // options are separated by ','
+               for(; *p; ++p) {
+                       if (*p == '"')  // Quote characters may appear at any position in the command line.
+                               quote = !quote;
+                       else if (*p==',' && !quote)
+                               break;
+               }
+
+               if (p > b) {
+                       int l = p - b;
+
+                        // remove trailing space
+                       while(l>0 && _istspace((unsigned)b[l-1]))
+                               --l;
+
+                       if (!EvaluateOption(String(b, l)))
+                               ok = false;
+
+                       if (*p)
+                               ++p;
+
+                       b = p;
+               }
+       }
+
+       return ok;
+}
+
+bool ExplorerCmd::EvaluateOption(LPCTSTR option)
+{
+       String opt_str;
+
+        // Remove quote characters, as they are evaluated at this point.
+       for(; *option; ++option)
+               if (*option != '"')
+                       opt_str += *option;
+
+       option = opt_str;
+
+       if (option[0] == '/') {
+               ++option;
+
+                // option /e for windows in explorer mode
+               if (!_tcsicmp(option, TEXT("e")))
+                       _flags |= OWM_EXPLORE;
+                // option /root for rooted explorer windows
+               else if (!_tcsicmp(option, TEXT("root")))
+                       _flags |= OWM_ROOTED;
+                // non-standard options: /mdi, /sdi
+               else if (!_tcsicmp(option, TEXT("mdi")))
+                       _mdi = true;
+               else if (!_tcsicmp(option, TEXT("sdi")))
+                       _mdi = false;
+               else
+                       return false;
+       } else {
+               if (!_path.empty())
+                       return false;
+
+               _path = opt_str;
+       }
+
+       return true;
+}
+
+bool ExplorerCmd::IsValidPath() const
+{
+       if (!_path.empty()) {
+               DWORD attribs = GetFileAttributes(_path);
+
+               if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
+                       return true;    // file system path
+               else if (*_path==':' && _path.at(1)==':')
+                       return true;    // text encoded IDL
+       }
+
+       return false;
 }
 
+#else
+
+void explorer_show_frame(int cmdShow, LPTSTR lpCmdLine)
+{
+       if (!lpCmdLine)
+               lpCmdLine = TEXT("explorer.exe");
+
+       launch_file(GetDesktopWindow(), lpCmdLine, cmdShow);
+}
+
+#endif
+
 
 PopupMenu::PopupMenu(UINT nid)
 {
@@ -632,6 +827,7 @@ static void InitInstance(HINSTANCE hInstance)
 
        setlocale(LC_COLLATE, "");      // set collating rules to local settings for compareName
 
+#ifndef ROSSHELL
         // register frame window class
        g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
 
@@ -640,12 +836,13 @@ static void InitInstance(HINSTANCE hInstance)
 
         // register tree window class
        WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
+#endif
 
        g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
 }
 
 
-int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdshow)
+int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdShow)
 {
        CONTEXT("explorer_main");
 
@@ -659,15 +856,17 @@ int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdshow)
                return -1;
        }
 
-       if (cmdshow != SW_HIDE) {
+#ifndef ROSSHELL
+       if (cmdShow != SW_HIDE) {
 /*     // don't maximize if being called from the ROS desktop
-               if (cmdshow == SW_SHOWNORMAL)
+               if (cmdShow == SW_SHOWNORMAL)
                                ///@todo read window placement from registry
-                       cmdshow = SW_MAXIMIZE;
+                       cmdShow = SW_MAXIMIZE;
 */
 
-               explorer_show_frame(cmdshow, lpCmdLine);
+               explorer_show_frame(cmdShow, lpCmdLine);
        }
+#endif
 
        return Window::MessageLoop();
 }
@@ -693,10 +892,10 @@ int main(int argc, char* argv[])
 
        LPWSTR cmdline = GetCommandLineW();
 
-       while(*cmdline && !_istspace(*cmdline))
+       while(*cmdline && !_istspace((unsigned)*cmdline))
                ++cmdline;
 
-       while(_istspace(*cmdline))
+       while(_istspace((unsigned)*cmdline))
                ++cmdline;
 
        return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
@@ -705,6 +904,19 @@ int main(int argc, char* argv[])
 #endif // __MINGW && UNICODE
 
 
+static bool SetShellReadyEvent(LPCTSTR evtName)
+{
+       HANDLE hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, evtName);
+       if (!hEvent)
+               return false;
+
+       SetEvent(hEvent);
+       CloseHandle(hEvent);
+
+       return true;
+}
+
+
 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
 {
        CONTEXT("WinMain()");
@@ -713,12 +925,23 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
 
        BOOL startup_desktop;
 
+        // strip extended options from the front of the command line
+       String ext_options;
+
+       while(*lpCmdLine == '-') {
+               while(*lpCmdLine && !_istspace((unsigned)*lpCmdLine))
+                       ext_options += *lpCmdLine++;
+
+               while(_istspace((unsigned)*lpCmdLine))
+                       ++lpCmdLine;
+       }
+
         // command line option "-install" to replace previous shell application with ROS Explorer
-       if (_tcsstr(lpCmdLine,TEXT("-install"))) {
+       if (_tcsstr(ext_options,TEXT("-install"))) {
                 // install ROS Explorer into the registry
                TCHAR path[MAX_PATH];
 
-               int l = GetModuleFileName(0, path, MAX_PATH);
+               int l = GetModuleFileName(0, path, COUNTOF(path));
                if (l) {
                        HKEY hkey;
 
@@ -729,6 +952,14 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
                                RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)path, l*sizeof(TCHAR));
                                RegCloseKey(hkey);
                        }
+
+                       if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hkey)) {
+
+                               ///@todo save previous shell application in config file
+
+                               RegSetValueEx(hkey, TEXT("Shell"), 0, REG_SZ, (LPBYTE)TEXT(""), l*sizeof(TCHAR));
+                               RegCloseKey(hkey);
+                       }
                }
 
                HWND shellWindow = GetShellWindow();
@@ -750,10 +981,14 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
                }
 
                startup_desktop = TRUE;
-       } else
+       } else {
                 // create desktop window and task bar only, if there is no other shell and we are
                 // the first explorer instance
+                // MS Explorer looks additionally into the registry entry HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\shell,
+                // to decide wether it is currently configured as shell application.
                startup_desktop = !any_desktop_running;
+       }
+
 
        bool autostart = !any_desktop_running;
 
@@ -766,41 +1001,63 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
 #endif
 
         // If there is given the command line option "-desktop", create desktop window anyways
-       if (_tcsstr(lpCmdLine,TEXT("-desktop")))
+       if (_tcsstr(ext_options,TEXT("-desktop")))
                startup_desktop = TRUE;
-       else if (_tcsstr(lpCmdLine,TEXT("-nodesktop")))
+#ifndef ROSSHELL
+       else if (_tcsstr(ext_options,TEXT("-nodesktop")))
                startup_desktop = FALSE;
 
         // Don't display cabinet window in desktop mode
-       if (startup_desktop && !_tcsstr(lpCmdLine,TEXT("-explorer")))
+       if (startup_desktop && !_tcsstr(ext_options,TEXT("-explorer")))
                nShowCmd = SW_HIDE;
+#endif
 
-       if (_tcsstr(lpCmdLine,TEXT("-noautostart")))
+       if (_tcsstr(ext_options,TEXT("-noautostart")))
                autostart = false;
-       else if (_tcsstr(lpCmdLine,TEXT("-autostart")))
+       else if (_tcsstr(ext_options,TEXT("-autostart")))
                autostart = true;
 
 #ifndef __WINE__
-       if (_tcsstr(lpCmdLine,TEXT("-console"))) {
+       if (_tcsstr(ext_options,TEXT("-console"))) {
                AllocConsole();
 
                _dup2(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY), 0);
                _dup2(_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0), 1);
                _dup2(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), 0), 2);
 
-               g_Globals._log = fdopen(1, "w");
+               g_Globals._log = _fdopen(1, "w");
                setvbuf(g_Globals._log, 0, _IONBF, 0);
 
                LOG(TEXT("starting explorer debug log\n"));
        }
 #endif
 
+
+       if (startup_desktop) {
+                // hide the XP login screen (Credit to Nicolas Escuder)
+                // another undocumented event: "Global\\msgina: ReturnToWelcome"
+               if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
+                       SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
+       }
+#ifdef ROSSHELL
+       else
+               return 0;       // no shell to launch, so exit immediatelly
+#endif
+
+
+       if (!any_desktop_running) {
+                // launch the shell DDE server
+               if (g_SHDOCVW_ShellDDEInit)
+                       (*g_SHDOCVW_ShellDDEInit)(TRUE);
+       }
+
+
        bool use_gdb_stub = false;      // !IsDebuggerPresent();
 
-       if (_tcsstr(lpCmdLine,TEXT("-debug")))
+       if (_tcsstr(ext_options,TEXT("-debug")))
                use_gdb_stub = true;
 
-       if (_tcsstr(lpCmdLine,TEXT("-break"))) {
+       if (_tcsstr(ext_options,TEXT("-break"))) {
                LOG(TEXT("debugger breakpoint"));
 #ifdef _MSC_VER
                __asm int 3
@@ -827,32 +1084,52 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdL
        g_Globals.read_persistent();
 
        if (startup_desktop) {
+               WaitCursor wait;
+
                g_Globals._desktops.init();
 
                g_Globals._hwndDesktop = DesktopWindow::Create();
 #ifdef _USE_HDESK
                g_Globals._desktops.get_current_Desktop()->_hwndDesktop = g_Globals._hwndDesktop;
 #endif
+       }
 
-               if (autostart) {
-                       char* argv[] = {"", "s"};       // call startup routine in SESSION_START mode
-                       startup(2, argv);
-               }
+       Thread* pSSOThread = NULL;
+
+       if (startup_desktop) {
+                // launch SSO thread to allow message processing independent from the explorer main thread
+               pSSOThread = new SSOThread;
+               pSSOThread->Start();
        }
 
-       /**TODO fix command line handling */
-       if (*lpCmdLine=='"' && lpCmdLine[_tcslen(lpCmdLine)-1]=='"') {
-               ++lpCmdLine;
-               lpCmdLine[_tcslen(lpCmdLine)-1] = '\0';
+       /**TODO launching autostart programs can be moved into a background thread. */
+       if (autostart) {
+               char* argv[] = {"", "s"};       // call startup routine in SESSION_START mode
+               startup(2, argv);
        }
 
+#ifndef ROSSHELL
        if (g_Globals._hwndDesktop)
                g_Globals._desktop_mode = true;
+#endif
+
 
        int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
 
+
         // write configuration file
        g_Globals.write_persistent();
 
+       if (pSSOThread) {
+               pSSOThread->Stop();
+               delete pSSOThread;
+       }
+
+       if (!any_desktop_running) {
+                // shutdown the shell DDE server
+               if (g_SHDOCVW_ShellDDEInit)
+                       (*g_SHDOCVW_ShellDDEInit)(FALSE);
+       }
+
        return ret;
 }