x11rb/protocol/
record.rs

1// This file contains generated code. Do not edit directly.
2// To regenerate this, run 'make'.
3
4//! Bindings to the `Record` X11 extension.
5
6#![allow(clippy::too_many_arguments)]
7
8#[allow(unused_imports)]
9use std::borrow::Cow;
10#[allow(unused_imports)]
11use std::convert::TryInto;
12#[allow(unused_imports)]
13use crate::utils::RawFdContainer;
14#[allow(unused_imports)]
15use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
16use std::io::IoSlice;
17use crate::connection::RequestConnection;
18#[allow(unused_imports)]
19use crate::connection::Connection as X11Connection;
20#[allow(unused_imports)]
21use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
22use crate::cookie::RecordEnableContextCookie;
23use crate::errors::ConnectionError;
24#[allow(unused_imports)]
25use crate::errors::ReplyOrIdError;
26
27pub use x11rb_protocol::protocol::record::*;
28
29/// Get the major opcode of this extension
30fn major_opcode<Conn: RequestConnection + ?Sized>(conn: &Conn) -> Result<u8, ConnectionError> {
31    let info = conn.extension_information(X11_EXTENSION_NAME)?;
32    let info = info.ok_or(ConnectionError::UnsupportedExtension)?;
33    Ok(info.major_opcode)
34}
35
36pub fn query_version<Conn>(conn: &Conn, major_version: u16, minor_version: u16) -> Result<Cookie<'_, Conn, QueryVersionReply>, ConnectionError>
37where
38    Conn: RequestConnection + ?Sized,
39{
40    let request0 = QueryVersionRequest {
41        major_version,
42        minor_version,
43    };
44    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
45    let slices = [IoSlice::new(&bytes[0])];
46    assert_eq!(slices.len(), bytes.len());
47    conn.send_request_with_reply(&slices, fds)
48}
49
50pub fn create_context<'c, 'input, Conn>(conn: &'c Conn, context: Context, element_header: ElementHeader, client_specs: &'input [ClientSpec], ranges: &'input [Range]) -> Result<VoidCookie<'c, Conn>, ConnectionError>
51where
52    Conn: RequestConnection + ?Sized,
53{
54    let request0 = CreateContextRequest {
55        context,
56        element_header,
57        client_specs: Cow::Borrowed(client_specs),
58        ranges: Cow::Borrowed(ranges),
59    };
60    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
61    let slices = [IoSlice::new(&bytes[0]), IoSlice::new(&bytes[1]), IoSlice::new(&bytes[2]), IoSlice::new(&bytes[3])];
62    assert_eq!(slices.len(), bytes.len());
63    conn.send_request_without_reply(&slices, fds)
64}
65
66pub fn register_clients<'c, 'input, Conn>(conn: &'c Conn, context: Context, element_header: ElementHeader, client_specs: &'input [ClientSpec], ranges: &'input [Range]) -> Result<VoidCookie<'c, Conn>, ConnectionError>
67where
68    Conn: RequestConnection + ?Sized,
69{
70    let request0 = RegisterClientsRequest {
71        context,
72        element_header,
73        client_specs: Cow::Borrowed(client_specs),
74        ranges: Cow::Borrowed(ranges),
75    };
76    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
77    let slices = [IoSlice::new(&bytes[0]), IoSlice::new(&bytes[1]), IoSlice::new(&bytes[2]), IoSlice::new(&bytes[3])];
78    assert_eq!(slices.len(), bytes.len());
79    conn.send_request_without_reply(&slices, fds)
80}
81
82pub fn unregister_clients<'c, 'input, Conn>(conn: &'c Conn, context: Context, client_specs: &'input [ClientSpec]) -> Result<VoidCookie<'c, Conn>, ConnectionError>
83where
84    Conn: RequestConnection + ?Sized,
85{
86    let request0 = UnregisterClientsRequest {
87        context,
88        client_specs: Cow::Borrowed(client_specs),
89    };
90    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
91    let slices = [IoSlice::new(&bytes[0]), IoSlice::new(&bytes[1]), IoSlice::new(&bytes[2])];
92    assert_eq!(slices.len(), bytes.len());
93    conn.send_request_without_reply(&slices, fds)
94}
95
96pub fn get_context<Conn>(conn: &Conn, context: Context) -> Result<Cookie<'_, Conn, GetContextReply>, ConnectionError>
97where
98    Conn: RequestConnection + ?Sized,
99{
100    let request0 = GetContextRequest {
101        context,
102    };
103    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
104    let slices = [IoSlice::new(&bytes[0])];
105    assert_eq!(slices.len(), bytes.len());
106    conn.send_request_with_reply(&slices, fds)
107}
108
109pub fn enable_context<Conn>(conn: &Conn, context: Context) -> Result<RecordEnableContextCookie<'_, Conn>, ConnectionError>
110where
111    Conn: RequestConnection + ?Sized,
112{
113    let request0 = EnableContextRequest {
114        context,
115    };
116    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
117    let slices = [IoSlice::new(&bytes[0])];
118    assert_eq!(slices.len(), bytes.len());
119    Ok(RecordEnableContextCookie::new(conn.send_request_with_reply(&slices, fds)?))
120}
121
122pub fn disable_context<Conn>(conn: &Conn, context: Context) -> Result<VoidCookie<'_, Conn>, ConnectionError>
123where
124    Conn: RequestConnection + ?Sized,
125{
126    let request0 = DisableContextRequest {
127        context,
128    };
129    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
130    let slices = [IoSlice::new(&bytes[0])];
131    assert_eq!(slices.len(), bytes.len());
132    conn.send_request_without_reply(&slices, fds)
133}
134
135pub fn free_context<Conn>(conn: &Conn, context: Context) -> Result<VoidCookie<'_, Conn>, ConnectionError>
136where
137    Conn: RequestConnection + ?Sized,
138{
139    let request0 = FreeContextRequest {
140        context,
141    };
142    let (bytes, fds) = request0.serialize(major_opcode(conn)?);
143    let slices = [IoSlice::new(&bytes[0])];
144    assert_eq!(slices.len(), bytes.len());
145    conn.send_request_without_reply(&slices, fds)
146}
147
148/// Extension trait defining the requests of this extension.
149pub trait ConnectionExt: RequestConnection {
150    fn record_query_version(&self, major_version: u16, minor_version: u16) -> Result<Cookie<'_, Self, QueryVersionReply>, ConnectionError>
151    {
152        query_version(self, major_version, minor_version)
153    }
154    fn record_create_context<'c, 'input>(&'c self, context: Context, element_header: ElementHeader, client_specs: &'input [ClientSpec], ranges: &'input [Range]) -> Result<VoidCookie<'c, Self>, ConnectionError>
155    {
156        create_context(self, context, element_header, client_specs, ranges)
157    }
158    fn record_register_clients<'c, 'input>(&'c self, context: Context, element_header: ElementHeader, client_specs: &'input [ClientSpec], ranges: &'input [Range]) -> Result<VoidCookie<'c, Self>, ConnectionError>
159    {
160        register_clients(self, context, element_header, client_specs, ranges)
161    }
162    fn record_unregister_clients<'c, 'input>(&'c self, context: Context, client_specs: &'input [ClientSpec]) -> Result<VoidCookie<'c, Self>, ConnectionError>
163    {
164        unregister_clients(self, context, client_specs)
165    }
166    fn record_get_context(&self, context: Context) -> Result<Cookie<'_, Self, GetContextReply>, ConnectionError>
167    {
168        get_context(self, context)
169    }
170    fn record_enable_context(&self, context: Context) -> Result<RecordEnableContextCookie<'_, Self>, ConnectionError>
171    {
172        enable_context(self, context)
173    }
174    fn record_disable_context(&self, context: Context) -> Result<VoidCookie<'_, Self>, ConnectionError>
175    {
176        disable_context(self, context)
177    }
178    fn record_free_context(&self, context: Context) -> Result<VoidCookie<'_, Self>, ConnectionError>
179    {
180        free_context(self, context)
181    }
182}
183
184impl<C: RequestConnection + ?Sized> ConnectionExt for C {}
185
186/// A RAII-like wrapper around a [Context].
187///
188/// Instances of this struct represent a Context that is freed in `Drop`.
189///
190/// Any errors during `Drop` are silently ignored. Most likely an error here means that your
191/// X11 connection is broken and later requests will also fail.
192#[derive(Debug)]
193pub struct ContextWrapper<C: RequestConnection>(C, Context);
194
195impl<C: RequestConnection> ContextWrapper<C>
196{
197    /// Assume ownership of the given resource and destroy it in `Drop`.
198    pub fn for_context(conn: C, id: Context) -> Self {
199        ContextWrapper(conn, id)
200    }
201
202    /// Get the XID of the wrapped resource
203    pub fn context(&self) -> Context {
204        self.1
205    }
206
207    /// Assume ownership of the XID of the wrapped resource
208    ///
209    /// This function destroys this wrapper without freeing the underlying resource.
210    pub fn into_context(self) -> Context {
211        let id = self.1;
212        std::mem::forget(self);
213        id
214    }
215}
216
217impl<'c, C: X11Connection> ContextWrapper<&'c C>
218{
219    /// Create a new Context and return a Context wrapper and a cookie.
220    ///
221    /// This is a thin wrapper around [create_context] that allocates an id for the Context.
222    /// This function returns the resulting `ContextWrapper` that owns the created Context and frees
223    /// it in `Drop`. This also returns a `VoidCookie` that comes from the call to
224    /// [create_context].
225    ///
226    /// Errors can come from the call to [X11Connection::generate_id] or [create_context].
227    pub fn create_context_and_get_cookie(conn: &'c C, element_header: ElementHeader, client_specs: &[ClientSpec], ranges: &[Range]) -> Result<(Self, VoidCookie<'c, C>), ReplyOrIdError>
228    {
229        let context = conn.generate_id()?;
230        let cookie = create_context(conn, context, element_header, client_specs, ranges)?;
231        Ok((Self::for_context(conn, context), cookie))
232    }
233}
234impl<C: X11Connection> ContextWrapper<C>
235{
236    /// Create a new Context and return a Context wrapper
237    ///
238    /// This is a thin wrapper around [create_context] that allocates an id for the Context.
239    /// This function returns the resulting `ContextWrapper` that owns the created Context and frees
240    /// it in `Drop`.
241    ///
242    /// Errors can come from the call to [X11Connection::generate_id] or [create_context].
243    pub fn create_context(conn: C, element_header: ElementHeader, client_specs: &[ClientSpec], ranges: &[Range]) -> Result<Self, ReplyOrIdError>
244    {
245        let context = conn.generate_id()?;
246        let _ = create_context(&conn, context, element_header, client_specs, ranges)?;
247        Ok(Self::for_context(conn, context))
248    }
249}
250
251impl<C: RequestConnection> From<&ContextWrapper<C>> for Context {
252    fn from(from: &ContextWrapper<C>) -> Self {
253        from.1
254    }
255}
256
257impl<C: RequestConnection> Drop for ContextWrapper<C> {
258    fn drop(&mut self) {
259        let _ = free_context(&self.0, self.1);
260    }
261}