You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

winshutdownmonitor.cpp 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2014-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "winshutdownmonitor.h"
  5. #if defined(Q_OS_WIN) && QT_VERSION >= 0x050000
  6. #include "init.h"
  7. #include "util.h"
  8. #include <windows.h>
  9. #include <QDebug>
  10. #include <openssl/rand.h>
  11. // If we don't want a message to be processed by Qt, return true and set result to
  12. // the value that the window procedure should return. Otherwise return false.
  13. bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pnResult)
  14. {
  15. Q_UNUSED(eventType);
  16. MSG *pMsg = static_cast<MSG *>(pMessage);
  17. // Seed OpenSSL PRNG with Windows event data (e.g. mouse movements and other user interactions)
  18. if (RAND_event(pMsg->message, pMsg->wParam, pMsg->lParam) == 0) {
  19. // Warn only once as this is performance-critical
  20. static bool warned = false;
  21. if (!warned) {
  22. LogPrintf("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__);
  23. warned = true;
  24. }
  25. }
  26. switch(pMsg->message)
  27. {
  28. case WM_QUERYENDSESSION:
  29. {
  30. // Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
  31. // Windows session end until we have finished client shutdown.
  32. StartShutdown();
  33. *pnResult = FALSE;
  34. return true;
  35. }
  36. case WM_ENDSESSION:
  37. {
  38. *pnResult = FALSE;
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, const HWND& mainWinId)
  45. {
  46. typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
  47. PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
  48. if (shutdownBRCreate == nullptr) {
  49. qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
  50. return;
  51. }
  52. if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
  53. qWarning() << "registerShutdownBlockReason: Successfully registered: " + strReason;
  54. else
  55. qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
  56. }
  57. #endif