浏览代码

ca642dd4 Add PowerOff/Reboot/Suspend support to OSX, part of #283 long-osxpower (#287)

Lionel CHAZALLON 9 年之前
父节点
当前提交
3c0661f8e7
共有 5 个文件被更改,包括 79 次插入1 次删除
  1. 37 0
      src/power/PowerComponentMac.cpp
  2. 8 0
      src/power/PowerComponentMac.h
  3. 0 1
      src/utils/Utils.cpp
  4. 2 0
      src/utils/osx/OSXUtils.h
  5. 32 0
      src/utils/osx/OSXUtils.mm

+ 37 - 0
src/power/PowerComponentMac.cpp

@@ -22,3 +22,40 @@ void PowerComponentMac::doEnableScreensaver()
     m_assertion = 0;
   }
 }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+bool PowerComponentMac::PowerOff()
+{
+  OSErr error = OSXUtils::SendAppleEventToSystemProcess(kAEShutDown);
+  if (error == noErr)
+    QLOG_DEBUG() <<  "Computer is going to shutdown!";
+  else
+    QLOG_DEBUG() << "Computer wouldn't shutdown!";
+
+  return (error == noErr);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+bool PowerComponentMac::Reboot()
+{
+  OSErr error = OSXUtils::SendAppleEventToSystemProcess(kAERestart);
+  if (error == noErr)
+    QLOG_DEBUG() <<  "Computer is going to reboot!";
+  else
+    QLOG_DEBUG() << "Computer wouldn't reboot!";
+
+  return (error == noErr);
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+bool PowerComponentMac::Suspend()
+{
+  OSErr error = OSXUtils::SendAppleEventToSystemProcess(kAESleep);
+  if (error == noErr)
+    QLOG_DEBUG() <<  "Computer is going to sleep!";
+  else
+    QLOG_DEBUG() << "Computer wouldn't sleep!";
+
+  return (error == noErr);
+}

+ 8 - 0
src/power/PowerComponentMac.h

@@ -11,6 +11,14 @@ public:
   void doDisableScreensaver() override;
   void doEnableScreensaver() override;
 
+  virtual bool canPowerOff() override { return true; }
+  virtual bool canReboot() override { return true; }
+  virtual bool canSuspend() override { return IOPMSleepEnabled(); }
+
+  virtual bool PowerOff() override;
+  virtual bool Reboot() override;
+  virtual bool Suspend() override;
+
 private:
   IOPMAssertionID m_assertion = 0;
 };

+ 0 - 1
src/utils/Utils.cpp

@@ -15,7 +15,6 @@
 #include "settings/SettingsComponent.h"
 #include "settings/SettingsSection.h"
 
-#include "osx/OSXUtils.h"
 #include "QsLog.h"
 
 

+ 2 - 0
src/utils/osx/OSXUtils.h

@@ -2,11 +2,13 @@
 #define OSXUTILS_H
 
 #include <QString>
+#include <ApplicationServices/ApplicationServices.h>
 
 namespace OSXUtils
 {
   void SetMenuBarVisible(bool visible);
   QString ComputerName();
+  OSStatus SendAppleEventToSystemProcess(AEEventID eventToSendID);
 };
 
 #endif /* OSXUTILS_H */

+ 32 - 0
src/utils/osx/OSXUtils.mm

@@ -22,3 +22,35 @@ QString OSXUtils::ComputerName()
 {
   return QString::fromNSString([[NSHost currentHost] localizedName]);
 }
+
+/////////////////////////////////////////////////////////////////////////////////////////
+OSStatus OSXUtils::SendAppleEventToSystemProcess(AEEventID eventToSendID)
+{
+  AEAddressDesc targetDesc;
+  static const  ProcessSerialNumber kPSNOfSystemProcess = {0, kSystemProcess };
+  AppleEvent    eventReply  = {typeNull, nullptr};
+  AppleEvent    eventToSend = {typeNull, nullptr};
+
+  OSStatus status = AECreateDesc(typeProcessSerialNumber,
+    &kPSNOfSystemProcess, sizeof(kPSNOfSystemProcess), &targetDesc);
+
+  if (status != noErr)
+    return status;
+
+  status = AECreateAppleEvent(kCoreEventClass, eventToSendID,
+    &targetDesc, kAutoGenerateReturnID, kAnyTransactionID, &eventToSend);
+  AEDisposeDesc(&targetDesc);
+
+  if (status != noErr)
+    return status;
+
+  status = AESendMessage(&eventToSend, &eventReply, kAENormalPriority, kAEDefaultTimeout);
+  AEDisposeDesc(&eventToSend);
+
+  if (status != noErr)
+    return status;
+
+  AEDisposeDesc(&eventReply);
+
+  return status;
+}