diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
commit | 9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /widget/windows/WinEventObserver.h | |
parent | Initial commit. (diff) | |
download | thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.tar.xz thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | widget/windows/WinEventObserver.h | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/widget/windows/WinEventObserver.h b/widget/windows/WinEventObserver.h new file mode 100644 index 0000000000..6a267871dd --- /dev/null +++ b/widget/windows/WinEventObserver.h @@ -0,0 +1,115 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef widget_windows_WinEventObserver_h +#define widget_windows_WinEventObserver_h + +#include <windows.h> + +#include "mozilla/Maybe.h" +#include "nsISupportsImpl.h" +#include "nsTHashSet.h" + +namespace mozilla { + +namespace widget { + +class WinEventObserver { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventObserver) + public: + virtual void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam) {} + virtual void Destroy(); + + protected: + virtual ~WinEventObserver(); + + bool mDestroyed = false; +}; + +// Uses singleton window to observe events like display status and session +// change. +class WinEventHub final { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WinEventHub) + + public: + // Returns true if the singleton exists (or was created). Will return + // false if the singleton couldn't be created, in which case a call + // to Get() will return a nullptr. It is safe to call this function + // repeatedly. + static bool Ensure(); + static RefPtr<WinEventHub> Get() { return sInstance; } + + void AddObserver(WinEventObserver* aObserver); + void RemoveObserver(WinEventObserver* aObserver); + + HWND GetWnd() { return mHWnd; } + + private: + WinEventHub(); + ~WinEventHub(); + + static LRESULT CALLBACK WinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam); + + bool Initialize(); + void ProcessWinEventProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); + + HWND mHWnd = nullptr; + nsTHashSet<nsRefPtrHashKey<WinEventObserver>> mObservers; + + static StaticRefPtr<WinEventHub> sInstance; +}; + +class DisplayStatusListener { + public: + virtual void OnDisplayStateChanged(bool aDisplayOn) = 0; +}; + +// Observe Display on/off event +class DisplayStatusObserver final : public WinEventObserver { + public: + static already_AddRefed<DisplayStatusObserver> Create( + DisplayStatusListener* aListener); + + private: + explicit DisplayStatusObserver(DisplayStatusListener* aListener); + virtual ~DisplayStatusObserver(); + void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam) override; + + DisplayStatusListener* mListener; + + HPOWERNOTIFY mDisplayStatusHandle = nullptr; +}; + +class SessionChangeListener { + public: + virtual void OnSessionChange(WPARAM aStatusCode, + Maybe<bool> aIsCurrentSession) = 0; +}; + +// Observe session lock/unlock event +class SessionChangeObserver : public WinEventObserver { + public: + static already_AddRefed<SessionChangeObserver> Create( + SessionChangeListener* aListener); + + private: + explicit SessionChangeObserver(SessionChangeListener* aListener); + virtual ~SessionChangeObserver(); + + void Initialize(); + void OnWinEventProc(HWND aHwnd, UINT aMsg, WPARAM aWParam, + LPARAM aLParam) override; + + SessionChangeListener* mListener; +}; + +} // namespace widget +} // namespace mozilla + +#endif // widget_windows_WinEventObserver_h |