calloop-wayland-source-0.3.0/.cargo_vcs_info.json0000644000000001360000000000100154200ustar { "git": { "sha1": "47ce61c2629c102bb37e893640037610e29dfd38" }, "path_in_vcs": "" }calloop-wayland-source-0.3.0/.github/workflows/ci.yml000064400000000000000000000017011046102023000207220ustar 00000000000000name: CI on: pull_request: push: branches: [main] env: RUST_BACKTRACE: 1 CARGO_INCREMENTAL: 0 RUSTFLAGS: "-Cdebuginfo=0 --deny=warnings" RUSTDOCFLAGS: "--deny=warnings" jobs: fmt: name: Check Formatting runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: hecrj/setup-rust-action@v1 with: rust-version: nightly components: rustfmt - name: Check Formatting run: cargo +nightly fmt --all -- --check tests: name: Tests runs-on: ubuntu-latest strategy: matrix: rust_version: ["1.65", stable, nightly] steps: - uses: actions/checkout@v3 - uses: hecrj/setup-rust-action@v1 with: rust-version: ${{ matrix.rust_version }} - name: Check documentation run: cargo doc --features=log --no-deps --document-private-items - name: Run tests run: cargo test --verbose --features=log calloop-wayland-source-0.3.0/.gitignore000064400000000000000000000000241046102023000161740ustar 00000000000000/target /Cargo.lock calloop-wayland-source-0.3.0/Cargo.lock0000644000000301760000000000100134020ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "calloop" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b99da2f8558ca23c71f4fd15dc57c906239752dd27ff3c00a1d56b685b7cbfec" dependencies = [ "bitflags 2.4.0", "log", "polling", "rustix", "slab", "thiserror", ] [[package]] name = "calloop-wayland-source" version = "0.3.0" dependencies = [ "calloop", "log", "rustix", "tempfile", "wayland-backend", "wayland-client", "wayland-protocols", ] [[package]] name = "cc" version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "concurrent-queue" version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62ec6771ecfa0762d24683ee5a32ad78487a3d3afdc0fb8cae19d2c5deb50b7c" dependencies = [ "crossbeam-utils", ] [[package]] name = "crossbeam-utils" version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" dependencies = [ "cfg-if", ] [[package]] name = "dlib" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412" dependencies = [ "libloading", ] [[package]] name = "downcast-rs" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" [[package]] name = "errno" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" dependencies = [ "errno-dragonfly", "libc", "windows-sys", ] [[package]] name = "errno-dragonfly" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" dependencies = [ "cc", "libc", ] [[package]] name = "fastrand" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" [[package]] name = "libc" version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "libloading" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d580318f95776505201b28cf98eb1fa5e4be3b689633ba6a3e6cd880ff22d8cb" dependencies = [ "cfg-if", "windows-sys", ] [[package]] name = "linux-raw-sys" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" [[package]] name = "log" version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "memoffset" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" dependencies = [ "autocfg", ] [[package]] name = "nix" version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" dependencies = [ "bitflags 1.3.2", "cfg-if", "libc", "memoffset", "static_assertions", ] [[package]] name = "pin-project-lite" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pkg-config" version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "polling" version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51348b98db9d4a18ada4fdf7ff5274666e7e6c5a50c42a7d77c5e5c0cb6b036b" dependencies = [ "cfg-if", "concurrent-queue", "pin-project-lite", "rustix", "tracing", "windows-sys", ] [[package]] name = "proc-macro2" version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] [[package]] name = "quick-xml" version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eff6510e86862b57b210fd8cbe8ed3f0d7d600b9c2863cd4549a2e033c66e956" dependencies = [ "memchr", ] [[package]] name = "quote" version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" dependencies = [ "proc-macro2", ] [[package]] name = "redox_syscall" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ "bitflags 1.3.2", ] [[package]] name = "rustix" version = "0.38.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662" dependencies = [ "bitflags 2.4.0", "errno", "libc", "linux-raw-sys", "windows-sys", ] [[package]] name = "scoped-tls" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" [[package]] name = "slab" version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" dependencies = [ "autocfg", ] [[package]] name = "smallvec" version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "syn" version = "2.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tempfile" version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" dependencies = [ "cfg-if", "fastrand", "redox_syscall", "rustix", "windows-sys", ] [[package]] name = "thiserror" version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a35fc5b8971143ca348fa6df4f024d4d55264f3468c71ad1c2f365b0a4d58c42" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "tracing" version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if", "pin-project-lite", "tracing-core", ] [[package]] name = "tracing-core" version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" [[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" [[package]] name = "wayland-backend" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abfc134185f589b9cc8f3d6a562e5764a8daa219238e75c3e4d36ff5c919164d" dependencies = [ "cc", "downcast-rs", "nix", "scoped-tls", "smallvec", "wayland-sys", ] [[package]] name = "wayland-client" version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ca7d52347346f5473bf2f56705f360e8440873052e575e55890c4fa57843ed3" dependencies = [ "bitflags 2.4.0", "nix", "wayland-backend", "wayland-scanner", ] [[package]] name = "wayland-protocols" version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e253d7107ba913923dc253967f35e8561a3c65f914543e46843c88ddd729e21c" dependencies = [ "bitflags 2.4.0", "wayland-backend", "wayland-client", "wayland-scanner", ] [[package]] name = "wayland-scanner" version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb8e28403665c9f9513202b7e1ed71ec56fde5c107816843fb14057910b2c09c" dependencies = [ "proc-macro2", "quick-xml", "quote", ] [[package]] name = "wayland-sys" version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15a0c8eaff5216d07f226cb7a549159267f3467b289d9a2e52fd3ef5aae2b7af" dependencies = [ "dlib", "log", "pkg-config", ] [[package]] name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] name = "windows_aarch64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] name = "windows_i686_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] name = "windows_i686_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" [[package]] name = "windows_x86_64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" calloop-wayland-source-0.3.0/Cargo.toml0000644000000023330000000000100134170ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.65.0" name = "calloop-wayland-source" version = "0.3.0" authors = ["Kirill Chibisov "] description = "A wayland-rs client event source for callloop" readme = "README.md" keywords = [ "wayland", "windowing", ] license = "MIT" repository = "https://github.com/smithay/calloop-wayland-source" [dependencies.calloop] version = "0.13.0" [dependencies.log] version = "0.4.19" optional = true [dependencies.rustix] version = "0.38.4" features = ["std"] default-features = false [dependencies.wayland-backend] version = "0.3.0" [dependencies.wayland-client] version = "0.31.1" [dev-dependencies.tempfile] version = "3.8.0" [dev-dependencies.wayland-protocols] version = "0.31.0" features = ["client"] calloop-wayland-source-0.3.0/Cargo.toml.orig000064400000000000000000000012371046102023000171020ustar 00000000000000[package] name = "calloop-wayland-source" description = "A wayland-rs client event source for callloop" repository = "https://github.com/smithay/calloop-wayland-source" version = "0.3.0" edition = "2021" authors = ["Kirill Chibisov "] license = "MIT" readme = "README.md" keywords = ["wayland", "windowing"] rust-version = "1.65.0" [dependencies] wayland-client = "0.31.1" wayland-backend = "0.3.0" calloop = "0.13.0" log = { version = "0.4.19", optional = true } rustix = { version = "0.38.4", default-features = false, features = ["std"] } [dev-dependencies] tempfile = "3.8.0" wayland-protocols = { version = "0.31.0", features = ["client"] } calloop-wayland-source-0.3.0/LICENSE.txt000064400000000000000000000020421046102023000160310ustar 00000000000000Copyright (c) 2023 Kirill Chibisov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.calloop-wayland-source-0.3.0/README.md000064400000000000000000000004051046102023000154660ustar 00000000000000# calloop-wayland-source Use [`EventQueue`] from the [`wayland-client`](https://crates.io/crates/wayland-client) with the [`calloop`](https://crates.io/crates/calloop). [`EventQueue`]: https://docs.rs/wayland-client/0.30/wayland_client/struct.EventQueue.html calloop-wayland-source-0.3.0/examples/simplest-window.rs000064400000000000000000000170321046102023000215440ustar 00000000000000use std::fs::File; use std::os::unix::prelude::AsFd; use calloop::EventLoop; use calloop_wayland_source::WaylandSource; use wayland_client::protocol::{ wl_buffer, wl_compositor, wl_keyboard, wl_registry, wl_seat, wl_shm, wl_shm_pool, wl_surface, }; use wayland_client::{delegate_noop, Connection, Dispatch, QueueHandle, WEnum}; use wayland_protocols::xdg::shell::client::{xdg_surface, xdg_toplevel, xdg_wm_base}; fn main() { let conn = Connection::connect_to_env().unwrap(); let event_queue = conn.new_event_queue(); let qhandle = event_queue.handle(); let display = conn.display(); display.get_registry(&qhandle, ()); let mut event_loop: EventLoop = EventLoop::try_new().expect("Failed to initialize the event loop!"); let loop_handle = event_loop.handle(); WaylandSource::new(conn, event_queue).insert(loop_handle).unwrap(); let stop_handle = event_loop.get_signal(); let mut state = State { running: true, base_surface: None, buffer: None, wm_base: None, xdg_surface: None, configured: false, stop_handle, }; println!("Starting the example window app, press to quit."); event_loop.run(None, &mut state, |_| {}).unwrap(); } struct State { running: bool, base_surface: Option, buffer: Option, wm_base: Option, xdg_surface: Option<(xdg_surface::XdgSurface, xdg_toplevel::XdgToplevel)>, configured: bool, stop_handle: calloop::LoopSignal, } impl Dispatch for State { fn event( state: &mut Self, registry: &wl_registry::WlRegistry, event: wl_registry::Event, _: &(), _: &Connection, qh: &QueueHandle, ) { if let wl_registry::Event::Global { name, interface, .. } = event { match &interface[..] { "wl_compositor" => { let compositor = registry.bind::(name, 1, qh, ()); let surface = compositor.create_surface(qh, ()); state.base_surface = Some(surface); if state.wm_base.is_some() && state.xdg_surface.is_none() { state.init_xdg_surface(qh); } }, "wl_shm" => { let shm = registry.bind::(name, 1, qh, ()); let (init_w, init_h) = (320, 240); let mut file = tempfile::tempfile().unwrap(); draw(&mut file, (init_w, init_h)); let pool = shm.create_pool(file.as_fd(), (init_w * init_h * 4) as i32, qh, ()); let buffer = pool.create_buffer( 0, init_w as i32, init_h as i32, (init_w * 4) as i32, wl_shm::Format::Argb8888, qh, (), ); state.buffer = Some(buffer.clone()); if state.configured { let surface = state.base_surface.as_ref().unwrap(); surface.attach(Some(&buffer), 0, 0); surface.commit(); } }, "wl_seat" => { registry.bind::(name, 1, qh, ()); }, "xdg_wm_base" => { let wm_base = registry.bind::(name, 1, qh, ()); state.wm_base = Some(wm_base); if state.base_surface.is_some() && state.xdg_surface.is_none() { state.init_xdg_surface(qh); } }, _ => {}, } } } } // Ignore events from these object types in this example. delegate_noop!(State: ignore wl_compositor::WlCompositor); delegate_noop!(State: ignore wl_surface::WlSurface); delegate_noop!(State: ignore wl_shm::WlShm); delegate_noop!(State: ignore wl_shm_pool::WlShmPool); delegate_noop!(State: ignore wl_buffer::WlBuffer); fn draw(tmp: &mut File, (buf_x, buf_y): (u32, u32)) { use std::cmp::min; use std::io::Write; let mut buf = std::io::BufWriter::new(tmp); for y in 0..buf_y { for x in 0..buf_x { let a = 0xFF; let r = min(((buf_x - x) * 0xFF) / buf_x, ((buf_y - y) * 0xFF) / buf_y); let g = min((x * 0xFF) / buf_x, ((buf_y - y) * 0xFF) / buf_y); let b = min(((buf_x - x) * 0xFF) / buf_x, (y * 0xFF) / buf_y); let color = (a << 24) + (r << 16) + (g << 8) + b; buf.write_all(&color.to_ne_bytes()).unwrap(); } } buf.flush().unwrap(); } impl State { fn init_xdg_surface(&mut self, qh: &QueueHandle) { let wm_base = self.wm_base.as_ref().unwrap(); let base_surface = self.base_surface.as_ref().unwrap(); let xdg_surface = wm_base.get_xdg_surface(base_surface, qh, ()); let toplevel = xdg_surface.get_toplevel(qh, ()); toplevel.set_title("A fantastic window!".into()); base_surface.commit(); self.xdg_surface = Some((xdg_surface, toplevel)); } } impl Dispatch for State { fn event( _: &mut Self, wm_base: &xdg_wm_base::XdgWmBase, event: xdg_wm_base::Event, _: &(), _: &Connection, _: &QueueHandle, ) { if let xdg_wm_base::Event::Ping { serial } = event { wm_base.pong(serial); } } } impl Dispatch for State { fn event( state: &mut Self, xdg_surface: &xdg_surface::XdgSurface, event: xdg_surface::Event, _: &(), _: &Connection, _: &QueueHandle, ) { if let xdg_surface::Event::Configure { serial, .. } = event { xdg_surface.ack_configure(serial); state.configured = true; let surface = state.base_surface.as_ref().unwrap(); if let Some(ref buffer) = state.buffer { surface.attach(Some(buffer), 0, 0); surface.commit(); } } } } impl Dispatch for State { fn event( state: &mut Self, _: &xdg_toplevel::XdgToplevel, event: xdg_toplevel::Event, _: &(), _: &Connection, _: &QueueHandle, ) { if let xdg_toplevel::Event::Close {} = event { state.running = false; } } } impl Dispatch for State { fn event( _: &mut Self, seat: &wl_seat::WlSeat, event: wl_seat::Event, _: &(), _: &Connection, qh: &QueueHandle, ) { if let wl_seat::Event::Capabilities { capabilities: WEnum::Value(capabilities) } = event { if capabilities.contains(wl_seat::Capability::Keyboard) { seat.get_keyboard(qh, ()); } } } } impl Dispatch for State { fn event( state: &mut Self, _: &wl_keyboard::WlKeyboard, event: wl_keyboard::Event, _: &(), _: &Connection, _: &QueueHandle, ) { if let wl_keyboard::Event::Key { key, .. } = event { if key == 1 { // ESC key state.stop_handle.stop(); } } } } calloop-wayland-source-0.3.0/rustfmt.toml000064400000000000000000000006651046102023000166200ustar 00000000000000format_code_in_doc_comments = true match_block_trailing_comma = true condense_wildcard_suffixes = true use_field_init_shorthand = true normalize_doc_attributes = true overflow_delimited_expr = true imports_granularity = "Module" use_small_heuristics = "Max" normalize_comments = true reorder_impl_items = true use_try_shorthand = true newline_style = "Unix" format_strings = true wrap_comments = true comment_width = 80 edition = "2021" calloop-wayland-source-0.3.0/src/lib.rs000064400000000000000000000241211046102023000161130ustar 00000000000000// SPDX-License-Identifier: MIT //! Utilities for using an [`EventQueue`] from wayland-client with an event loop //! that performs polling with [`calloop`](https://crates.io/crates/calloop). //! //! # Example //! //! ```no_run,rust //! use calloop::EventLoop; //! use calloop_wayland_source::WaylandSource; //! use wayland_client::{Connection, QueueHandle}; //! //! // Create a Wayland connection and a queue. //! let connection = Connection::connect_to_env().unwrap(); //! let event_queue = connection.new_event_queue(); //! let queue_handle = event_queue.handle(); //! //! // Create the calloop event loop to drive everytihng. //! let mut event_loop: EventLoop<()> = EventLoop::try_new().unwrap(); //! let loop_handle = event_loop.handle(); //! //! // Insert the wayland source into the calloop's event loop. //! WaylandSource::new(connection, event_queue).insert(loop_handle).unwrap(); //! //! // This will start dispatching the event loop and processing pending wayland requests. //! while let Ok(_) = event_loop.dispatch(None, &mut ()) { //! // Your logic here. //! } //! ``` #![deny(unsafe_op_in_unsafe_fn)] use std::io; use calloop::generic::Generic; use calloop::{ EventSource, InsertError, Interest, LoopHandle, Mode, Poll, PostAction, Readiness, RegistrationToken, Token, TokenFactory, }; use rustix::io::Errno; use wayland_backend::client::{ReadEventsGuard, WaylandError}; use wayland_client::{Connection, DispatchError, EventQueue}; #[cfg(feature = "log")] use log::error as log_error; #[cfg(not(feature = "log"))] use std::eprintln as log_error; /// An adapter to insert an [`EventQueue`] into a calloop /// [`EventLoop`](calloop::EventLoop). /// /// This type implements [`EventSource`] which generates an event whenever /// events on the event queue need to be dispatched. The event queue available /// in the callback calloop registers may be used to dispatch pending /// events using [`EventQueue::dispatch_pending`]. /// /// [`WaylandSource::insert`] can be used to insert this source into an event /// loop and automatically dispatch pending events on the event queue. #[derive(Debug)] pub struct WaylandSource { // In theory, we could use the same event queue inside `connection_source` // However, calloop's safety requirements mean that we cannot then give // mutable access to the queue, which is incompatible with our current interface // Additionally, `Connection` is cheaply cloneable, so it's not a huge burden queue: EventQueue, connection_source: Generic, read_guard: Option, /// Calloop's before_will_sleep method allows /// skipping the sleeping by returning a `Token`. /// We cannot produce this on the fly, so store it here instead fake_token: Option, // Some calloop event handlers don't support error handling, so we have to store the error // for a short time until we reach a method which allows it stored_error: Result<(), io::Error>, } impl WaylandSource { /// Wrap an [`EventQueue`] as a [`WaylandSource`]. /// /// `queue` must be from the connection `Connection`. /// This is not a safety invariant, but not following this may cause /// freezes or hangs pub fn new(connection: Connection, queue: EventQueue) -> WaylandSource { let connection_source = Generic::new(connection, Interest::READ, Mode::Level); WaylandSource { queue, connection_source, read_guard: None, fake_token: None, stored_error: Ok(()), } } /// Access the underlying event queue /// /// Note that you should not replace this queue with a queue from a /// different `Connection`, as that may cause freezes or other hangs. pub fn queue(&mut self) -> &mut EventQueue { &mut self.queue } /// Access the connection to the Wayland server pub fn connection(&self) -> &Connection { self.connection_source.get_ref() } /// Insert this source into the given event loop. /// /// This adapter will pass the event loop's shared data as the `D` type for /// the event loop. pub fn insert(self, handle: LoopHandle) -> Result> where D: 'static, { handle.insert_source(self, |_, queue, data| queue.dispatch_pending(data)) } } impl EventSource for WaylandSource { type Error = calloop::Error; type Event = (); /// The underlying event queue. /// /// You should call [`EventQueue::dispatch_pending`] inside your callback /// using this queue. type Metadata = EventQueue; type Ret = Result; const NEEDS_EXTRA_LIFECYCLE_EVENTS: bool = true; fn process_events( &mut self, _: Readiness, _: Token, mut callback: F, ) -> Result where F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret, { debug_assert!(self.read_guard.is_none()); // Take the stored error std::mem::replace(&mut self.stored_error, Ok(()))?; // We know that the event will either be a fake event // produced in `before_will_sleep`, or a "real" event from the underlying // source (self.queue_events). Our behaviour in both cases is the same. // In theory we might want to call the process_events handler on the underlying // event source. However, we know that Generic's `process_events` call is a // no-op, so we just handle the event ourselves. let queue = &mut self.queue; // Dispatch any pending events in the queue Self::loop_callback_pending(queue, &mut callback)?; // Once dispatching is finished, flush the responses to the compositor flush_queue(queue)?; Ok(PostAction::Continue) } fn register( &mut self, poll: &mut Poll, token_factory: &mut TokenFactory, ) -> calloop::Result<()> { self.fake_token = Some(token_factory.token()); self.connection_source.register(poll, token_factory) } fn reregister( &mut self, poll: &mut Poll, token_factory: &mut TokenFactory, ) -> calloop::Result<()> { self.connection_source.reregister(poll, token_factory) } fn unregister(&mut self, poll: &mut Poll) -> calloop::Result<()> { self.connection_source.unregister(poll) } fn before_sleep(&mut self) -> calloop::Result> { debug_assert!(self.read_guard.is_none()); flush_queue(&mut self.queue)?; self.read_guard = self.queue.prepare_read(); match self.read_guard { Some(_) => Ok(None), // If getting the guard failed, that means that there are // events in the queue, and so we need to handle the events instantly // rather than waiting on an event in polling. We tell calloop this // by returning Some here. Note that the readiness value is // never used, so we just need some marker None => Ok(Some((Readiness::EMPTY, self.fake_token.unwrap()))), } } fn before_handle_events(&mut self, events: calloop::EventIterator<'_>) { // It's important that the guard isn't held whilst process_events calls occur // This can use arbitrary user-provided code, which may want to use the wayland // socket For example, creating a Vulkan surface needs access to the // connection let guard = self.read_guard.take(); if events.count() > 0 { // Read events from the socket if any are available if let Some(Err(WaylandError::Io(err))) = guard.map(ReadEventsGuard::read) { // If some other thread read events before us, concurrently, that's an expected // case, so this error isn't an issue. Other error kinds do need to be returned, // however if err.kind() != io::ErrorKind::WouldBlock { // before_handle_events doesn't allow returning errors // For now, cache it in self until process_events is called self.stored_error = Err(err); } } } } } fn flush_queue(queue: &mut EventQueue) -> Result<(), calloop::Error> { if let Err(WaylandError::Io(err)) = queue.flush() { // WouldBlock error means the compositor could not process all // our messages quickly. Either it is slowed // down or we are a spammer. Should not really // happen, if it does we do nothing and will flush again later if err.kind() != io::ErrorKind::WouldBlock { // in case of error, forward it and fast-exit log_error!("Error trying to flush the wayland display: {}", err); return Err(err.into()); } } Ok(()) } impl WaylandSource { /// Loop over the callback until all pending messages have been dispatched. fn loop_callback_pending(queue: &mut EventQueue, callback: &mut F) -> io::Result<()> where F: FnMut((), &mut EventQueue) -> Result, { // Loop on the callback until no pending events are left. loop { match callback((), queue) { // No more pending events. Ok(0) => break Ok(()), Ok(_) => continue, Err(DispatchError::Backend(WaylandError::Io(err))) => { return Err(err); }, Err(DispatchError::Backend(WaylandError::Protocol(err))) => { log_error!("Protocol error received on display: {}", err); break Err(Errno::PROTO.into()); }, Err(DispatchError::BadMessage { interface, sender_id, opcode }) => { log_error!( "Bad message on interface \"{}\": (sender_id: {}, opcode: {})", interface, sender_id, opcode, ); break Err(Errno::PROTO.into()); }, } } } }