Skip to main content

asdf_overlay_common/
request.rs

1//! IPC request types sent from client to server.
2//!
3//! [`Request`] is the top-level enum representing all possible requests.
4
5use core::{fmt::Debug, num::NonZeroU32};
6
7use bincode::{Decode, Encode};
8
9use crate::{cursor::Cursor, size::PercentLength};
10
11/// Describes a request.
12#[derive(Debug, Encode, Decode, Clone)]
13pub enum Request {
14    /// Request to a specific window.
15    Window {
16        /// Window identifier.
17        id: u32,
18
19        /// The underlying window request.
20        request: WindowRequest,
21    },
22}
23
24/// Describes a request to a specific window.
25#[derive(Debug, Encode, Decode, Clone, derive_more::From)]
26pub enum WindowRequest {
27    /// Set overlay surface position.
28    SetPosition(SetPosition),
29
30    /// Set overlay surface positioning anchor.
31    SetAnchor(SetAnchor),
32
33    /// Set overlay surface margin.
34    SetMargin(SetMargin),
35
36    /// Change whether to listen input events.
37    ListenInput(ListenInput),
38
39    /// Whether to block input events from reaching window and listen all input events.
40    BlockInput(BlockInput),
41
42    /// Set cursor of a window when being input blocked.
43    SetBlockingCursor(SetBlockingCursor),
44
45    /// Set overlay surface shared handle.
46    UpdateSharedHandle(UpdateSharedHandle),
47}
48
49mod __sealed {
50    pub trait Sealed {}
51}
52
53/// Trait implemented to sub types of [`WindowRequest`] enum.
54pub trait WindowRequestItem: __sealed::Sealed + Into<WindowRequest> {}
55
56macro_rules! impl_WindowRequestItem {
57    ($ty:ty) => {
58        impl __sealed::Sealed for $ty {}
59        impl WindowRequestItem for $ty {}
60    };
61}
62
63#[derive(Debug, Default, Encode, Decode, Clone, PartialEq)]
64/// Set overlay surface position relative to the window client area.
65pub struct SetPosition {
66    /// X position of percent or absolute length relative to the window's width.
67    pub x: PercentLength,
68
69    /// Y position of percent or absolute length relative to the window's height.
70    pub y: PercentLength,
71}
72impl_WindowRequestItem!(SetPosition);
73
74#[derive(Debug, Default, Encode, Decode, Clone, PartialEq)]
75/// Set overlay surface positioning anchor relative to the top-left of the overlay surface.
76pub struct SetAnchor {
77    /// Anchor of X axis as percent relative to the window's width.
78    pub x: PercentLength,
79
80    /// Anchor of Y axis as percent relative to the window's height.
81    pub y: PercentLength,
82}
83impl_WindowRequestItem!(SetAnchor);
84
85#[derive(Debug, Default, Encode, Decode, Clone, PartialEq)]
86/// Set overlay surface margin relative to the overlay surface's size.
87pub struct SetMargin {
88    /// Margin of top side as percent or absolute length relative to the overlay surface's height.
89    pub top: PercentLength,
90
91    /// Margin of right side as percent or absolute length relative to the overlay surface's width.
92    pub right: PercentLength,
93
94    /// Margin of bottom side as percent or absolute length relative to the overlay surface's height.
95    pub bottom: PercentLength,
96
97    /// Margin of left side as percent or absolute length relative to the overlay surface's width.
98    pub left: PercentLength,
99}
100impl_WindowRequestItem!(SetMargin);
101
102impl SetMargin {
103    /// Utility function to set same margin for axis.
104    pub const fn xy(x: PercentLength, y: PercentLength) -> Self {
105        Self {
106            top: y,
107            right: x,
108            bottom: y,
109            left: x,
110        }
111    }
112}
113
114#[derive(Debug, Default, Encode, Decode, Clone, PartialEq, Eq)]
115/// Listen input events.
116pub struct ListenInput {
117    /// Whether to listen cursor related events.
118    pub cursor: bool,
119
120    /// Whether to listen keyboard related events.
121    pub keyboard: bool,
122}
123impl_WindowRequestItem!(ListenInput);
124
125#[derive(Debug, Default, Encode, Decode, Clone, PartialEq, Eq)]
126/// Block input events from reaching window and listen all input events
127pub struct BlockInput {
128    /// Whether to block input events from reaching to window.
129    pub block: bool,
130}
131impl_WindowRequestItem!(BlockInput);
132
133#[derive(Debug, Default, Encode, Decode, Clone, PartialEq, Eq)]
134/// Set cursor of a window being input captured
135pub struct SetBlockingCursor {
136    /// Cursor to be set.
137    /// If [`None`] is given, the cursor will be hidden.
138    pub cursor: Option<Cursor>,
139}
140impl_WindowRequestItem!(SetBlockingCursor);
141
142#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)]
143/// Update overlay surface
144pub struct UpdateSharedHandle {
145    /// DirectX KMT shared handle to the overlay surface texture.
146    ///
147    /// ## Note
148    /// * The texture is either a 32-bit BGRA(integer) or 32-bit RGBA(integer) or 64-bit RGBA(integer, float) format texture.
149    /// * The texture must be created with `D3D11_RESOURCE_MISC_SHARED` flag.
150    /// * If the texture is created with `D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX` flag, the `key` of the `IDXGIKeyedMutex` must be `0`.
151    ///
152    /// If [`None`] is given, the overlay surface will be removed.
153    pub handle: Option<NonZeroU32>,
154}
155impl_WindowRequestItem!(UpdateSharedHandle);