use bitflags::bitflags;
use windows::Win32::UI::WindowsAndMessaging::{
WINEVENT_INCONTEXT, WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS, WINEVENT_SKIPOWNTHREAD,
};
bitflags! {
/// Windows Event Hook flags.
/// See [SetWinEventHook](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook)
/// for more information.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Flags: u32 {
/// The DLL that contains the callback function is mapped into the address space of the process that generates the event. With this flag, the system sends event notifications to the callback function as they occur. The hook function must be in a DLL when this flag is specified. This flag has no effect when both the calling process and the generating process are not 32-bit or 64-bit processes, or when the generating process is a console application. For more information, see In-Context Hook Functions.
const IN_CONTEXT = WINEVENT_INCONTEXT;
/// The callback function is not mapped into the address space of the process that generates the event. Because the hook function is called across process boundaries, the system must queue events. Although this method is asynchronous, events are guaranteed to be in sequential order. For more information, see Out-of-Context Hook Functions.
const OUT_OF_CONTEXT = WINEVENT_OUTOFCONTEXT;
/// Prevents this instance of the hook from receiving the events that are generated by threads in this process. This flag does not prevent threads from generating events.
const SKIP_OWN_PROCESS = WINEVENT_SKIPOWNPROCESS;
/// Prevents this instance of the hook from receiving the events that are generated by the thread that is registering this hook.
const SKIP_OWN_THREAD = WINEVENT_SKIPOWNTHREAD;
}
}
impl Flags {
/// Resets the current state to the `Flags::default`.
pub fn reset(&mut self) {
*self.0.bits_mut() = Self::default().bits();
}
/// Determines if the given [`Flags`] are valid, as defined in
/// [the Windows API documentation](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook).
pub fn is_valid(self) -> bool {
self == Flags::IN_CONTEXT
|| self == Flags::OUT_OF_CONTEXT
|| self == (Flags::IN_CONTEXT | Flags::SKIP_OWN_PROCESS)
|| self == (Flags::IN_CONTEXT | Flags::SKIP_OWN_THREAD)
|| self == (Flags::OUT_OF_CONTEXT | Flags::SKIP_OWN_PROCESS)
|| self == (Flags::OUT_OF_CONTEXT | Flags::SKIP_OWN_THREAD)
}
}
impl Default for Flags {
fn default() -> Self {
Self::OUT_OF_CONTEXT | Self::SKIP_OWN_PROCESS
}
}