breadx/
lib.rs

1//               Copyright John Nunley, 2022.
2// Distributed under the Boost Software License, Version 1.0.
3//       (See accompanying file LICENSE or copy at
4//         https://www.boost.org/LICENSE_1_0.txt)
5
6//! `breadx` is a comprehensive implementation of the [X11 client protocol]
7//! with an aim to be featureful and powerful, but also easy to use.
8//!
9//! `breadx` aims to be a minimal implementation of the X11 protocol that
10//! can be used in any case where a client is needed. `breadx` comes built
11//! in with the following features:
12//!
13//! - **Comprehensive:** `breadx` has first class support for all X11 protocol
14//!   extensions. These extensions can be enabled and disabled as features.
15//! - **Lock-free**: The [default connection implementation] uses no locks and
16//!   no waiting outside of standard I/O primitives. The goal is to ensure that
17//!   there are as few layers as possible between the user's intended goal and
18//!   actually sending data to the server.
19//! - **Safe**: `breadx` has `#[forbid(unsafe_code)]`, which means that there never
20//!   will be any unsafe code in `breadx`. This means that `breadx` will never be
21//!   the cause of any undefined behavior.
22//! - **Versatile:** For cases where sharing the connection is necessary, `breadx`
23//!   provides [thread unsafe] and [thread safe] variants.
24//! - **`no_std`:** By disabling the `std` feature, `breadx` can be used
25//!   without depending on the standard library.
26//! - **Asynchronous:** With the `async` feature enabled, `breadx`'s primitives
27//!   can be used in asynchronous contexts. By default, `breadx` is runtime-agnostic,
28//!   but support can be enabled for [`tokio`] and [`async-std`].
29//! - **Simple:** With all of this, a client can be created and used in `breadx`
30//!   in very few lines of code.
31//!
32//! Features that `breadx` does not provide:
33//!
34//! - **Data Manipulation** - APIs to make image manipulation/ICCCM/etc easier are
35//!   located in other crates.
36//! - **Interfacing with Xlib/XCB** - `breadx` does not provide a way to interact
37//!   with Xlib/XCB directly.
38//!
39//! [X11 client protocol]: https://en.wikipedia.org/wiki/X_Window_System
40//! [default connection implementation]: crate::display::BasicDisplay
41//! [thread unsafe]: crate::display::CellDisplay
42//! [thread safe]: crate::display::SyncDisplay
43//! [`tokio`]: crate::rt_support::tokio_support
44//! [`async-std`]: crate::rt_support::async_std_support
45//!
46//! # Usage
47//!
48//! All functions in `breadx` exist in the context of a [`Display`]. There are
49//! many ways to create a [`Display`], but in most cases, [`DisplayConnection::connect()`]
50//! will connect to the currently running X11 server without any fuss.
51//!
52//! From there, most functions that are actually used in `breadx` exist on the
53//! [`DisplayFunctionsExt`] extension trait.
54//!
55//! [`Display`]: crate::display::Display
56//! [`DisplayConnection::connect()`]: crate::display::DisplayConnection::connect
57//! [`DisplayFunctionsExt`]: crate::display::DisplayFunctionsExt
58//!
59//! ```rust,no_run
60//! use breadx::{prelude::*, display::DisplayConnection, protocol::xproto};
61//!
62//! # fn main() -> breadx::Result<()> {
63//! // establish a connection to the X11 server
64//! let mut connection = DisplayConnection::connect(None)?;
65//!
66//! // create a window
67//! // note the "_checked" suffix, this indicates that the result of the
68//! // function will be checked by the server after it is run
69//! // also note that we need to create an XID for the window ahead of time
70//! let wid = connection.generate_xid()?;
71//! connection.create_window_checked(
72//!     0, // depth
73//!     wid,
74//!     connection.default_screen().root, // parent
75//!     0, // x
76//!     0, // y
77//!     600, // width
78//!     400, // height
79//!     0, // border width
80//!     xproto::WindowClass::COPY_FROM_PARENT,
81//!     0, // visual
82//!     xproto::CreateWindowAux::new()
83//!         .background_pixel(connection.default_screen().white_pixel)
84//! )?;
85//!
86//! // map the window to the screen
87//! // note the lack of _checked here
88//! connection.map_window(wid)?;
89//!
90//! // primary event loop
91//! loop {
92//!     let event = connection.wait_for_event()?;
93//!
94//!     match event {
95//!         // match on the Event struct in here
96//!         # _ => {},
97//!     }
98//! }
99//! # Ok(()) }
100//! ```
101//!
102//! See the [tutorial](crate::tutorials::introduction) for more information
103//! on the usage of `breadx`.
104
105#![no_std]
106#![forbid(future_incompatible, unsafe_code, rust_2018_idioms)]
107#![warn(clippy::pedantic)]
108#![allow(
109    clippy::module_name_repetitions,
110    clippy::missing_errors_doc,
111    clippy::wildcard_imports
112)]
113
114extern crate alloc;
115
116#[cfg(feature = "std")]
117extern crate std;
118
119#[macro_use]
120mod gates;
121
122mod error;
123pub use error::*;
124
125mod utils;
126pub(crate) use utils::*;
127
128cfg_async! {
129    pub mod rt_support;
130}
131
132mod void;
133pub use void::Void;
134
135cfg_std! {
136    mod name;
137    pub use name::*;
138}
139
140cfg_sync! {
141    mod mutex;
142}
143
144cfg_async! {
145    pub mod futures;
146}
147
148// inline some of x11rb_protocol's docs into ours
149#[doc(inline)]
150pub use x11rb_protocol::{connect, x11_utils};
151
152/// The protocol used during communication.
153pub mod protocol {
154    #[doc(inline)]
155    pub use x11rb_protocol::{connection::ReplyFdKind, protocol::*, x11_utils::*, DiscardMode};
156}
157
158cfg_std! {
159    #[doc(inline)]
160    pub use x11rb_protocol::resource_manager;
161}
162
163#[allow(
164    clippy::cast_possible_truncation,
165    clippy::let_and_return,
166    clippy::needless_lifetimes,
167    clippy::too_many_arguments
168)]
169pub(crate) mod automatically_generated;
170
171#[doc(inline)]
172pub use x11rb_protocol::RawFdContainer as Fd;
173
174pub mod connection;
175pub mod display;
176
177mod mutlireply;
178pub use mutlireply::*;
179
180/// Contains a set of traits to be automatically imported for full
181/// functionality.
182pub mod prelude {
183    pub use super::display::{
184        Display, DisplayBase, DisplayBaseExt, DisplayExt, DisplayFunctionsExt,
185    };
186
187    cfg_async! {
188        pub use super::display::{
189            AsyncDisplay, AsyncDisplayExt, AsyncDisplayFunctionsExt
190        };
191    }
192}
193
194pub mod tutorials;