1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2016-2017 The Perceptia Project Developers
//
// 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.

//! This crate supplements `skylane` crate with bindings `Wayland` protocol automatically
//! generated from XML protocol description files.
//!
//! This crate is planed to provide implementation for all (stable and unstable) protocols. If
//! something is missing, let us know.
//!
//! ## Implementation
//!
//! Each protocol description file contains requests (from client to server) and events (from
//! server to client). In server part requests are translated to:
//! - interfaces - traits describing methods of a Wayland protocol object
//! - dispatchers - structures translating socket data to calls to methods on objects implementing
//!   appropriate interface.
//! while events to stand-alone functions. On client side vice-versa.
//!
//! `Handler` structure helps bind `Dispatcher` with implementation of its `Interface` to
//! register it in `server::Connection` from `skylane` crate.
//!
//! ## Server examples
//!
//! TODO: Add examples for server.
//!
//! ## Client examples
//!
//! TODO: Add examples for client.

#![warn(missing_docs)]

extern crate skylane;
extern crate byteorder;

// -------------------------------------------------------------------------------------------------

mod private {
    use std::io::Cursor;
    use skylane::server::{Bundle, Header, Object, SkylaneError, Task};

    /// This trait is implemented by `Dispatcher`s in protocol definitions generated from XML
    /// files. Every object defined in protocol has its own `Dispatcher` which takes buffer
    /// data, parses it and calls appropriate method of given object which implements attached
    /// interface.
    pub trait Dispatcher<I> {
        /// Constructs new `Dispatcher`.
        fn new() -> Self;

        /// Demarshals message and call appropriate callback in passed `object`.
        fn dispatch(&mut self,
                    object: &mut I,
                    bundle: &mut Bundle,
                    header: &Header,
                    bytes_buf: &mut Cursor<&[u8]>,
                    fds_buf: &mut Cursor<&[u8]>)
                    -> Result<Task, SkylaneError>;
    }

    /// Binds `Dispatcher` with object implementing `Interface` trait from protocol definition.
    /// Only objects implementing `Interfaces` corresponding to `Dispatcher` can be bound
    /// together.
    pub struct Handler<I, D>
        where D: Dispatcher<I>
    {
        object: I,
        dispatcher: D,
    }

    impl<I, D> Handler<I, D>
        where D: Dispatcher<I>
    {
        /// Constructs new `Handler`.
        pub fn new(object: I) -> Self {
            Handler {
                object: object,
                dispatcher: D::new(),
            }
        }
    }

    impl<I, D> Object for Handler<I, D>
        where D: Dispatcher<I>
    {
        fn dispatch(&mut self,
                    bundle: &mut Bundle,
                    header: &Header,
                    bytes_buf: &mut Cursor<&[u8]>,
                    fds_buf: &mut Cursor<&[u8]>)
                    -> Result<Task, SkylaneError> {
            self.dispatcher.dispatch(&mut self.object, bundle, header, bytes_buf, fds_buf)
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Server-side protocols.
pub mod server {
    use skylane::server::{Bundle, Header, ObjectId, SkylaneError, Socket, Task};
    use private::Dispatcher;
    pub use private::Handler;

    /// Protocol generated from `wayland.xml`
    pub mod wayland {
        include!(concat!(env!("OUT_DIR"), "/wayland_server.rs"));
    }

    /// Protocol generated from `xdg-shell-unstable-v6.xml`
    pub mod xdg_shell_unstable_v6 {
        include!(concat!(env!("OUT_DIR"), "/xdg_shell_unstable_v6_server.rs"));
    }

    /// Protocol generated from `weston-screenshooter.xml`
    pub mod weston_screenshooter {
        include!(concat!(env!("OUT_DIR"), "/weston_screenshooter_server.rs"));
    }

    /// Protocol generated from `linux-dmabuf-unstable-v1.xml`
    pub mod linux_dmabuf_unstable_v1 {
        include!(concat!(env!("OUT_DIR"), "/linux_dmabuf_unstable_v1_server.rs"));
    }

    /// Protocol generated from `wayland-drm.xml`
    pub mod drm {
        include!(concat!(env!("OUT_DIR"), "/drm_server.rs"));
    }
}

// -------------------------------------------------------------------------------------------------

/// Client-side protocols.
pub mod client {
    use skylane::server::{Bundle, Header, ObjectId, SkylaneError, Socket, Task};
    use private::Dispatcher;
    pub use private::Handler;

    /// Protocol generated from `wayland.xml`
    pub mod wayland {
        include!(concat!(env!("OUT_DIR"), "/wayland_client.rs"));
    }

    /// Protocol generated from `xdg-shell-unstable-v6.xml`
    pub mod xdg_shell_unstable_v6 {
        include!(concat!(env!("OUT_DIR"), "/xdg_shell_unstable_v6_client.rs"));
    }

    /// Protocol generated from `weston-screenshooter.xml`
    pub mod weston_screenshooter {
        include!(concat!(env!("OUT_DIR"), "/weston_screenshooter_client.rs"));
    }

    /// Protocol generated from `linux-dmabuf-unstable-v1.xml`
    pub mod linux_dmabuf_unstable_v1 {
        include!(concat!(env!("OUT_DIR"), "/linux_dmabuf_unstable_v1_client.rs"));
    }

    /// Protocol generated from `wayland-drm.xml`
    pub mod drm {
        include!(concat!(env!("OUT_DIR"), "/drm_client.rs"));
    }
}

// -------------------------------------------------------------------------------------------------