Index: trunk/src/VBox/Main/src-server/HostPower.cpp
===================================================================
--- trunk/src/VBox/Main/src-server/HostPower.cpp	(revision 46754)
+++ trunk/src/VBox/Main/src-server/HostPower.cpp	(revision 46775)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2011 Oracle Corporation
+ * Copyright (C) 2006-2013 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -27,8 +27,9 @@
 
 #include "VirtualBoxImpl.h"
+#include "MachineImpl.h"
 
 #include <iprt/mem.h>
 
-HostPowerService::HostPowerService (VirtualBox *aVirtualBox)
+HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
 {
     Assert(aVirtualBox != NULL);
@@ -40,5 +41,5 @@
 }
 
-void HostPowerService::notify(HostPowerEvent aEvent)
+void HostPowerService::notify(Reason_T aReason)
 {
     SessionMachinesList machines;
@@ -47,9 +48,9 @@
     HRESULT rc = S_OK;
 
-    switch (aEvent)
+    switch (aReason)
     {
-        case HostPowerEvent_Suspend:
+        case Reason_HostSuspend:
         {
-            LogFunc (("SUSPEND\n"));
+            LogFunc(("SUSPEND\n"));
 
 #ifdef VBOX_WITH_RESOURCE_USAGE_API
@@ -69,34 +70,27 @@
                 ComPtr<IInternalSessionControl> pControl = *it;
 
-                /* get the remote console */
-                ComPtr<IConsole> console;
-                rc = pControl->GetRemoteConsole(console.asOutParam());
-                /* the VM could have been powered down and closed or whatever */
-                if (FAILED(rc))
-                    continue;
-
-                /* note that Pause() will simply return a failure if the VM is
-                 * in an inappropriate state */
-                rc = console->Pause();
+                /* PauseWithReason() will simply return a failure if
+                 * the VM is in an inappropriate state */
+                rc = pControl->PauseWithReason(Reason_HostSuspend);
                 if (FAILED(rc))
                     continue;
 
                 /* save the control to un-pause the VM later */
-                mConsoles.push_back(console);
+                mSessionControls.push_back(pControl);
             }
 
-            LogFunc (("Suspended %d VMs\n", mConsoles.size()));
+            LogFunc(("Suspended %d VMs\n", mSessionControls.size()));
 
             break;
         }
 
-        case HostPowerEvent_Resume:
+        case Reason_HostResume:
         {
-            LogFunc (("RESUME\n"));
+            LogFunc(("RESUME\n"));
 
             size_t resumed = 0;
 
             /* go through VMs we paused on Suspend */
-            for (size_t i = 0; i < mConsoles.size(); ++i)
+            for (size_t i = 0; i < mSessionControls.size(); ++i)
             {
                 /* note that Resume() will simply return a failure if the VM is
@@ -104,12 +98,12 @@
                  * been somehow closed by this time already so that the
                  * console reference we have is dead) */
-                rc = mConsoles[i]->Resume();
+                rc = mSessionControls[i]->ResumeWithReason(Reason_HostResume);
                 if (FAILED(rc))
                     continue;
 
-                ++ resumed;
+                ++resumed;
             }
 
-            LogFunc (("Resumed %d VMs\n", resumed));
+            LogFunc(("Resumed %d VMs\n", resumed));
 
 #ifdef VBOX_WITH_RESOURCE_USAGE_API
@@ -121,12 +115,12 @@
 #endif
 
-            mConsoles.clear();
+            mSessionControls.clear();
 
             break;
         }
 
-        case HostPowerEvent_BatteryLow:
+        case Reason_HostBatteryLow:
         {
-            LogFunc (("BATTERY LOW\n"));
+            LogFunc(("BATTERY LOW\n"));
 
             mVirtualBox->getOpenedMachines(machines, &controls);
@@ -140,16 +134,10 @@
             {
                 ComPtr<IInternalSessionControl> pControl = *it;
-                /* get the remote console */
-                ComPtr<IConsole> console;
-                rc = pControl->GetRemoteConsole (console.asOutParam());
-                /* the VM could have been powered down and closed or whatever */
-                if (FAILED(rc))
-                    continue;
 
                 ComPtr<IProgress> progress;
 
-                /* note that SaveState() will simply return a failure if the VM
-                 * is in an inappropriate state */
-                rc = console->SaveState (progress.asOutParam());
+                /* note that SaveStateWithReason() will simply return a failure
+                 * if the VM is in an inappropriate state */
+                rc = pControl->SaveStateWithReason(Reason_HostBatteryLow, progress.asOutParam());
                 if (FAILED(rc))
                     continue;
@@ -164,15 +152,17 @@
                 }
 
-                AssertMsg (SUCCEEDED(rc), ("SaveState WaitForCompletion "
-                                            "failed with %Rhrc (%#08X)\n", rc, rc));
+                AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %Rhrc (%#08X)\n", rc, rc));
 
                 if (SUCCEEDED(rc))
-                    ++ saved;
+                    ++saved;
             }
 
-            LogFunc (("Saved %d VMs\n", saved));
+            LogFunc(("Saved %d VMs\n", saved));
 
             break;
         }
+
+        default:
+            /* nothing */;
     }
 }
Index: trunk/src/VBox/Main/src-server/darwin/HostPowerDarwin.cpp
===================================================================
--- trunk/src/VBox/Main/src-server/darwin/HostPowerDarwin.cpp	(revision 46754)
+++ trunk/src/VBox/Main/src-server/darwin/HostPowerDarwin.cpp	(revision 46775)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2008-2010 Oracle Corporation
+ * Copyright (C) 2008-2013 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -26,18 +26,18 @@
 #define POWER_SOURCE_BATTERY 2
 
-HostPowerServiceDarwin::HostPowerServiceDarwin (VirtualBox *aVirtualBox)
-  : HostPowerService (aVirtualBox)
-  , mThread (NULL)
-  , mRootPort (MACH_PORT_NULL)
-  , mNotifyPort (nil)
-  , mRunLoop (nil)
-  , mCritical (false)
+HostPowerServiceDarwin::HostPowerServiceDarwin(VirtualBox *aVirtualBox)
+  : HostPowerService(aVirtualBox)
+  , mThread(NULL)
+  , mRootPort(MACH_PORT_NULL)
+  , mNotifyPort(nil)
+  , mRunLoop(nil)
+  , mCritical(false)
 {
     /* Create the new worker thread. */
-    int rc = RTThreadCreate (&mThread, HostPowerServiceDarwin::powerChangeNotificationThread, this, 65536,
-                             RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "MainPower");
+    int rc = RTThreadCreate(&mThread, HostPowerServiceDarwin::powerChangeNotificationThread, this, 65536,
+                            RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "MainPower");
 
     if (RT_FAILURE(rc))
-        LogFlow (("RTThreadCreate failed with %Rrc\n", rc));
+        LogFlow(("RTThreadCreate failed with %Rrc\n", rc));
 }
 
@@ -45,22 +45,22 @@
 {
     /* Jump out of the run loop. */
-    CFRunLoopStop (mRunLoop);
+    CFRunLoopStop(mRunLoop);
     /* Remove the sleep notification port from the application runloop. */
-    CFRunLoopRemoveSource (CFRunLoopGetCurrent(),
-                           IONotificationPortGetRunLoopSource (mNotifyPort),
-                           kCFRunLoopCommonModes);
+    CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
+                          IONotificationPortGetRunLoopSource(mNotifyPort),
+                          kCFRunLoopCommonModes);
     /* Deregister for system sleep notifications. */
-    IODeregisterForSystemPower (&mNotifierObject);
+    IODeregisterForSystemPower(&mNotifierObject);
     /* IORegisterForSystemPower implicitly opens the Root Power Domain
      * IOService so we close it here. */
-    IOServiceClose (mRootPort);
+    IOServiceClose(mRootPort);
     /* Destroy the notification port allocated by IORegisterForSystemPower */
-    IONotificationPortDestroy (mNotifyPort);
-}
-
-
-DECLCALLBACK(int) HostPowerServiceDarwin::powerChangeNotificationThread (RTTHREAD /* ThreadSelf */, void *pInstance)
-{
-    HostPowerServiceDarwin *pPowerObj = static_cast<HostPowerServiceDarwin *> (pInstance);
+    IONotificationPortDestroy(mNotifyPort);
+}
+
+
+DECLCALLBACK(int) HostPowerServiceDarwin::powerChangeNotificationThread(RTTHREAD /* ThreadSelf */, void *pInstance)
+{
+    HostPowerServiceDarwin *pPowerObj = static_cast<HostPowerServiceDarwin *>(pInstance);
 
     /* We have to initial set the critical state of the battery, cause we want
@@ -70,17 +70,17 @@
 
     /* Register to receive system sleep notifications */
-    pPowerObj->mRootPort = IORegisterForSystemPower (pPowerObj, &pPowerObj->mNotifyPort,
-                                                     HostPowerServiceDarwin::powerChangeNotificationHandler,
-                                                     &pPowerObj->mNotifierObject);
+    pPowerObj->mRootPort = IORegisterForSystemPower(pPowerObj, &pPowerObj->mNotifyPort,
+                                                    HostPowerServiceDarwin::powerChangeNotificationHandler,
+                                                    &pPowerObj->mNotifierObject);
     if (pPowerObj->mRootPort == MACH_PORT_NULL)
     {
-        LogFlow (("IORegisterForSystemPower failed\n"));
+        LogFlow(("IORegisterForSystemPower failed\n"));
         return VERR_NOT_SUPPORTED;
     }
     pPowerObj->mRunLoop = CFRunLoopGetCurrent();
     /* Add the notification port to the application runloop */
-    CFRunLoopAddSource (pPowerObj->mRunLoop,
-                        IONotificationPortGetRunLoopSource (pPowerObj->mNotifyPort),
-                        kCFRunLoopCommonModes);
+    CFRunLoopAddSource(pPowerObj->mRunLoop,
+                       IONotificationPortGetRunLoopSource(pPowerObj->mNotifyPort),
+                       kCFRunLoopCommonModes);
 
     /* Register for all battery change events. The handler will check for low
@@ -97,8 +97,8 @@
 }
 
-void HostPowerServiceDarwin::powerChangeNotificationHandler (void *pvData, io_service_t /* service */, natural_t messageType, void *pMessageArgument)
-{
-    HostPowerServiceDarwin *pPowerObj = static_cast<HostPowerServiceDarwin *> (pvData);
-    Log (( "powerChangeNotificationHandler: messageType %08lx, arg %08lx\n", (long unsigned int)messageType, (long unsigned int)pMessageArgument));
+void HostPowerServiceDarwin::powerChangeNotificationHandler(void *pvData, io_service_t /* service */, natural_t messageType, void *pMessageArgument)
+{
+    HostPowerServiceDarwin *pPowerObj = static_cast<HostPowerServiceDarwin *>(pvData);
+    Log(( "powerChangeNotificationHandler: messageType %08lx, arg %08lx\n", (long unsigned int)messageType, (long unsigned int)pMessageArgument));
 
     switch (messageType)
@@ -114,5 +114,5 @@
                  * IOAllowPowerChange or IOCancelPowerChange, the system will
                  * wait 30 seconds then go to sleep. */
-                IOAllowPowerChange (pPowerObj->mRootPort, reinterpret_cast<long> (pMessageArgument));
+                IOAllowPowerChange(pPowerObj->mRootPort, reinterpret_cast<long>(pMessageArgument));
                 break;
             }
@@ -120,10 +120,10 @@
             {
                 /* The system will go for sleep. */
-                pPowerObj->notify (HostPowerEvent_Suspend);
+                pPowerObj->notify(Reason_HostSuspend);
                 /* If you do not call IOAllowPowerChange or IOCancelPowerChange to
                  * acknowledge this message, sleep will be delayed by 30 seconds.
                  * NOTE: If you call IOCancelPowerChange to deny sleep it returns
                  * kIOReturnSuccess, however the system WILL still go to sleep. */
-                IOAllowPowerChange (pPowerObj->mRootPort, reinterpret_cast<long> (pMessageArgument));
+                IOAllowPowerChange(pPowerObj->mRootPort, reinterpret_cast<long>(pMessageArgument));
                 break;
             }
@@ -136,5 +136,5 @@
             {
                 /* System has finished the wake up process. */
-                pPowerObj->notify (HostPowerEvent_Resume);
+                pPowerObj->notify(Reason_HostResume);
                 break;
             }
@@ -144,9 +144,9 @@
 }
 
-void HostPowerServiceDarwin::lowPowerHandler (void *pvData)
-{
-    HostPowerServiceDarwin *pPowerObj = static_cast<HostPowerServiceDarwin *> (pvData);
-
-    /* Following role for sending the BatteryLow event (5% is critical):
+void HostPowerServiceDarwin::lowPowerHandler(void *pvData)
+{
+    HostPowerServiceDarwin *pPowerObj = static_cast<HostPowerServiceDarwin *>(pvData);
+
+    /* Following role for sending the BatteryLow event(5% is critical):
      * - Not at VM start even if the battery is in an critical state already.
      * - When the power cord is removed so the power supply change from AC to
@@ -157,13 +157,13 @@
      *   normal triggers nothing. */
     bool fCriticalStateChanged = false;
-    pPowerObj->checkBatteryCriticalLevel (&fCriticalStateChanged);
+    pPowerObj->checkBatteryCriticalLevel(&fCriticalStateChanged);
     if (fCriticalStateChanged)
-        pPowerObj->notify (HostPowerEvent_BatteryLow);
-}
-
-void HostPowerServiceDarwin::checkBatteryCriticalLevel (bool *pfCriticalChanged)
+        pPowerObj->notify(Reason_HostBatteryLow);
+}
+
+void HostPowerServiceDarwin::checkBatteryCriticalLevel(bool *pfCriticalChanged)
 {
     CFTypeRef pBlob = IOPSCopyPowerSourcesInfo();
-    CFArrayRef pSources = IOPSCopyPowerSourcesList (pBlob);
+    CFArrayRef pSources = IOPSCopyPowerSourcesList(pBlob);
 
     CFDictionaryRef pSource = NULL;
@@ -173,9 +173,9 @@
     bool critical = false;
 
-    if (CFArrayGetCount (pSources) > 0)
+    if (CFArrayGetCount(pSources) > 0)
     {
-        for (int i = 0; i < CFArrayGetCount (pSources); ++i)
+        for (int i = 0; i < CFArrayGetCount(pSources); ++i)
         {
-            pSource = IOPSGetPowerSourceDescription (pBlob, CFArrayGetValueAtIndex (pSources, i));
+            pSource = IOPSGetPowerSourceDescription(pBlob, CFArrayGetValueAtIndex(pSources, i));
             /* If the source is empty skip over to the next one. */
             if (!pSource)
@@ -183,18 +183,18 @@
             /* Skip all power sources which are currently not present like a
              * second battery. */
-            if (CFDictionaryGetValue (pSource, CFSTR (kIOPSIsPresentKey)) == kCFBooleanFalse)
+            if (CFDictionaryGetValue(pSource, CFSTR(kIOPSIsPresentKey)) == kCFBooleanFalse)
                 continue;
             /* Only internal power types are of interest. */
-            result = CFDictionaryGetValueIfPresent (pSource, CFSTR (kIOPSTransportTypeKey), &psValue);
+            result = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSTransportTypeKey), &psValue);
             if (result &&
-                CFStringCompare ((CFStringRef)psValue, CFSTR (kIOPSInternalType), 0) == kCFCompareEqualTo)
+                CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSInternalType), 0) == kCFCompareEqualTo)
             {
                 /* First check which power source we are connect on. */
-                result = CFDictionaryGetValueIfPresent (pSource, CFSTR (kIOPSPowerSourceStateKey), &psValue);
+                result = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSPowerSourceStateKey), &psValue);
                 if (result &&
-                    CFStringCompare ((CFStringRef)psValue, CFSTR (kIOPSACPowerValue), 0) == kCFCompareEqualTo)
+                    CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo)
                     powerSource = POWER_SOURCE_OUTLET;
                 else if (result &&
-                         CFStringCompare ((CFStringRef)psValue, CFSTR (kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo)
+                         CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo)
                     powerSource = POWER_SOURCE_BATTERY;
 
@@ -204,11 +204,11 @@
 
                 /* Fetch the current capacity value of the power source */
-                result = CFDictionaryGetValueIfPresent (pSource, CFSTR (kIOPSCurrentCapacityKey), &psValue);
+                result = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSCurrentCapacityKey), &psValue);
                 if (result)
-                    CFNumberGetValue ((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
+                    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
                 /* Fetch the maximum capacity value of the power source */
-                result = CFDictionaryGetValueIfPresent (pSource, CFSTR (kIOPSMaxCapacityKey), &psValue);
+                result = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSMaxCapacityKey), &psValue);
                 if (result)
-                    CFNumberGetValue ((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
+                    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
 
                 /* Calculate the remaining capacity in percent */
@@ -217,7 +217,7 @@
                 /* Check for critical. 5 percent is default. */
                 int criticalValue = 5;
-                result = CFDictionaryGetValueIfPresent (pSource, CFSTR (kIOPSDeadWarnLevelKey), &psValue);
+                result = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSDeadWarnLevelKey), &psValue);
                 if (result)
-                    CFNumberGetValue ((CFNumberRef)psValue, kCFNumberSInt32Type, &criticalValue);
+                    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &criticalValue);
                 critical = (remCapacity < criticalValue);
                 /* We have to take action only if we are on battery, the
@@ -229,5 +229,5 @@
                     pfCriticalChanged)
                     *pfCriticalChanged = true;
-                Log (("checkBatteryCriticalLevel: Remains: %d.%d%% Critical: %d Critical State Changed: %d\n", (int)remCapacity, (int)(remCapacity * 10) % 10, critical, pfCriticalChanged?*pfCriticalChanged:-1));
+                Log(("checkBatteryCriticalLevel: Remains: %d.%d%% Critical: %d Critical State Changed: %d\n", (int)remCapacity, (int)(remCapacity * 10) % 10, critical, pfCriticalChanged?*pfCriticalChanged:-1));
             }
         }
@@ -236,6 +236,6 @@
     mCritical = critical;
 
-    CFRelease (pBlob);
-    CFRelease (pSources);
-}
-
+    CFRelease(pBlob);
+    CFRelease(pSources);
+}
+
Index: trunk/src/VBox/Main/src-server/win/HostPowerWin.cpp
===================================================================
--- trunk/src/VBox/Main/src-server/win/HostPowerWin.cpp	(revision 46754)
+++ trunk/src/VBox/Main/src-server/win/HostPowerWin.cpp	(revision 46775)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2006-2010 Oracle Corporation
+ * Copyright (C) 2006-2013 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -36,6 +36,6 @@
     mHwnd = 0;
 
-    int rc = RTThreadCreate (&mThread, HostPowerServiceWin::NotificationThread, this, 65536,
-                             RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "MainPower");
+    int rc = RTThreadCreate(&mThread, HostPowerServiceWin::NotificationThread, this, 65536,
+                            RTTHREADTYPE_GUI, RTTHREADFLAGS_WAITABLE, "MainPower");
 
     if (RT_FAILURE(rc))
@@ -63,5 +63,5 @@
 
 
-DECLCALLBACK(int) HostPowerServiceWin::NotificationThread (RTTHREAD ThreadSelf, void *pInstance)
+DECLCALLBACK(int) HostPowerServiceWin::NotificationThread(RTTHREAD ThreadSelf, void *pInstance)
 {
     HostPowerServiceWin *pPowerObj = (HostPowerServiceWin *)pInstance;
@@ -71,5 +71,5 @@
     int rc = VINF_SUCCESS;
 
-    HINSTANCE hInstance = (HINSTANCE)GetModuleHandle (NULL);
+    HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
 
     /* Register the Window Class. */
@@ -97,8 +97,8 @@
     {
         /* Create the window. */
-        hwnd = pPowerObj->mHwnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
-                                                  gachWindowClassName, gachWindowClassName,
-                                                  WS_POPUPWINDOW,
-                                                 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
+        hwnd = pPowerObj->mHwnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
+                                                 gachWindowClassName, gachWindowClassName,
+                                                 WS_POPUPWINDOW,
+                                                -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
 
         if (hwnd == NULL)
@@ -124,9 +124,9 @@
     Log(("HostPowerServiceWin::NotificationThread: exit thread\n"));
     if (hwnd)
-        DestroyWindow (hwnd);
+        DestroyWindow(hwnd);
 
     if (atomWindowClass != 0)
     {
-        UnregisterClass (gachWindowClassName, hInstance);
+        UnregisterClass(gachWindowClassName, hInstance);
         atomWindowClass = 0;
     }
@@ -149,9 +149,9 @@
                 {
                 case PBT_APMSUSPEND:
-                    pPowerObj->notify(HostPowerEvent_Suspend);
+                    pPowerObj->notify(Reason_HostSuspend);
                     break;
 
                 case PBT_APMRESUMEAUTOMATIC:
-                    pPowerObj->notify(HostPowerEvent_Resume);
+                    pPowerObj->notify(Reason_HostResume);
                     break;
 
@@ -180,5 +180,5 @@
                                     &&  BatteryState.EstimatedTime < 60*5)
                                 {
-                                    pPowerObj->notify(HostPowerEvent_BatteryLow);
+                                    pPowerObj->notify(Reason_HostBatteryLow);
                                 }
                             }
@@ -187,5 +187,5 @@
                             if (SystemPowerStatus.BatteryFlag == 4      /* critical battery status; less than 5% */)
                             {
-                                pPowerObj->notify(HostPowerEvent_BatteryLow);
+                                pPowerObj->notify(Reason_HostBatteryLow);
                             }
                         }
@@ -194,5 +194,5 @@
                 }
                 default:
-                    return DefWindowProc (hwnd, msg, wParam, lParam);
+                    return DefWindowProc(hwnd, msg, wParam, lParam);
                 }
             }
@@ -201,5 +201,5 @@
 
         default:
-            return DefWindowProc (hwnd, msg, wParam, lParam);
-    }
-}
+            return DefWindowProc(hwnd, msg, wParam, lParam);
+    }
+}
