#wayland #codegen

build wayland-scanner

Wayland Scanner for generating rust APIs from XML wayland protocol files. Intented for use with wayland-sys. You should only need this crate if you are working on custom wayland protocol extensions. Look at the crate wayland-client for usable bindings.

98 releases

✓ Uses Rust 2018 edition

new 0.26.6 May 23, 2020
0.26.3 Apr 22, 2020
0.25.0 Feb 7, 2020
0.24.1 Dec 13, 2019
0.2.0 Nov 19, 2015

#133 in GUI

Download history 12013/week @ 2020-01-25 9810/week @ 2020-02-01 9562/week @ 2020-02-08 9765/week @ 2020-02-15 10001/week @ 2020-02-22 8835/week @ 2020-02-29 7341/week @ 2020-03-07 8957/week @ 2020-03-14 8948/week @ 2020-03-21 9064/week @ 2020-03-28 9577/week @ 2020-04-04 9773/week @ 2020-04-11 10006/week @ 2020-04-18 10857/week @ 2020-04-25 13989/week @ 2020-05-02 12862/week @ 2020-05-09

44,799 downloads per month
Used in 464 crates (7 directly)

MIT license

88KB
2K SLoC

crates.io docs.rs Continuous Integration codecov

wayland-scanner

Code-generation for Wayland protocols, to be used with wayland-client or wayland-server to integrate them with your own protocol extensions.

Most general protocol extensions are already exposed by the wayland-protocols crate, so you don't need to use wayland-scanner directly to support them.


lib.rs:

Wayland scanner crate

This crate is a rust equivalent of the wayland-scanner tool from the official wayland C library.

You can use in your build script to generate the rust code for any wayland protocol file, to use alongside the wayland_client and wayland_server crate to build your applications.

How to use this crate

This crate is to be used in a build script. It provides the function generate_code. It'll allow you to generate the code to use with the wayland_client or wayland_server crates for any XML wayland protocol file (NB: you don't need to do it for the core protocol, which is already included in both crates).

First, have the XML files you want to use in your project, somewhere the build script will be able to read them.

Then, you'll need to invoke generate_code for each of these files.

A sample build script:

extern crate wayland_scanner;

use std::env::var;
use std::path::Path;

use wayland_scanner::{Side, generate_code};

// Location of the xml file, relative to the `Cargo.toml`
let protocol_file = "./my_protocol.xml";

// Target directory for the generate files
let out_dir_str = var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir_str);

generate_code(
    protocol_file,
    out_dir.join("my_protocol_api.rs"),
    Side::Client, // Replace by `Side::Server` for server-side code
);

The above example will output a .rs file in the OUT_DIR defined by cargo. Then, you'll need to include this generated file (using the macro of the same name) to make this code available in your crate.

// The generated code will import stuff from wayland_commons
extern crate wayland_commons;
extern crate wayland_client;

// Re-export only the actual code, and then only use this re-export
// The `generated` module below is just some boilerplate to properly isolate stuff
// and avoid exposing internal details.
//
// You can use all the types from my_protocol as if they went from `wayland_client::protocol`.
pub use generated::client as my_protocol;

mod generated {
    // The generated code tends to trigger a lot of warnings
    // so we isolate it into a very permissive module
    #![allow(dead_code,non_camel_case_types,unused_unsafe,unused_variables)]
    #![allow(non_upper_case_globals,non_snake_case,unused_imports)]

    pub mod client {
        // These imports are used by the generated code
        pub(crate) use wayland_client::{Main, Attached, Proxy, ProxyMap, AnonymousObject};
        pub(crate) use wayland_commons::map::{Object, ObjectMetadata};
        pub(crate) use wayland_commons::{Interface, MessageGroup};
        pub(crate) use wayland_commons::wire::{Argument, MessageDesc, ArgumentType, Message};
        pub(crate) use wayland_commons::smallvec;
        pub(crate) use wayland_client::protocol::{$($import),*};
        pub(crate) use wayland_client::sys;
        // If you protocol interacts with objects from other protocols, you'll need to import
        // their modules, like so:
        pub(crate) use wayland_client::protocol::{wl_surface, wl_region};
        include!(concat!(env!("OUT_DIR"), "/my_protocol_code.rs"));
    }
}

Dependencies

~320KB