dummy_rustwlc/
lib.rs

1//! `rustwlc` is a wrapper for [wlc][], a library for writing a window
2//! manager using the [wayland][] protocol. Compositors using rustwlc will
3//! not need unsafe code for basic interaction with wayland.
4//!
5//! # wlc
6//!
7//! [wlc][] is a library written in C which acts as a wayland compositor. It
8//! provides abstractions over wayland via structs such as `WlcView`,
9//! `WlcOutput`, `Geometry`, or `KeyboardModifiers`. It provides callbacks
10//! (found in the `callback` module) for events such as `view_created`,
11//! `mouse_move`, or `view_focused`.
12//!
13//! # Example
14//!
15//! For more information on how to use rustwlc, see the `callbacks` module
16//! and the `run_wlc()` method.
17//!
18//! For a more complete example, see [the example program][] on
19//! [our GitHub page][].
20//!
21//! ```rust
22//! extern crate rustwlc;
23//! use rustwlc::callback;
24//! // VIEW_ACTIVATED is a bitflags enum variant, and those must be imported
25//! // manually, or using a wildcatd.
26//! use rustwlc::{WlcView, VIEW_ACTIVATED};
27//!
28//! // Callbacks must be labeled extern as they will be called from C
29//! extern "C" fn view_created(view: WlcView) -> bool {
30//!     view.bring_to_front();
31//!     view.focus();
32//!     return true;
33//! }
34//!
35//! extern "C" fn view_focus(view: WlcView, focused: bool) {
36//!     view.set_state(VIEW_ACTIVATED, focused);
37//! }
38//!
39//! // Entry point for a compositor
40//! fn compsoitor_main() {
41//!     callback::view_created(view_created);
42//!     callback::view_focus(view_focus);
43//!
44//!     // The default log handler will print wlc logs to stdout
45//!     rustwlc::log_set_default_handler();
46//!     let run_fn = rustwlc::init().expect("Unable to initialize!");
47//!     // This will run wlc's event loop and launch wayland.
48//!     run_fn();
49//! }
50//! # fn main() {}
51//! ```
52//! For a more full-featured compositor using rustwlc, see [way-cooler][].
53//! [wlc]: https://github.com/Cloudef/wlc
54//! [wayland]: https://wayland.freedesktop.org/
55//! [the example program]: https://github.com/Immington-Industries/rust-wlc/blob/master/example/src/main.rs
56//! [our GitHub page]: https://github.com/Immington-Industries/rustwlc
57//! [way-cooler]: https://github.com/Immington-Industries/way-cooler
58
59#![warn(missing_docs)]
60#![allow(unused_variables)]
61
62extern crate libc;
63extern crate wayland_sys;
64
65#[macro_use]
66extern crate bitflags;
67
68use std::ffi;
69
70pub mod handle;
71pub mod callback;
72pub mod types;
73pub mod input;
74pub mod wayland;
75pub mod xkb;
76pub mod render;
77
78pub use types::*;
79pub use handle::{WlcOutput, WlcView};
80
81/// Query backend wlc is using.
82///
83/// # Results
84/// * None: Unknown backend type
85/// * DRM: "Direct Rendering Manager" - running on tty
86/// * X11: Running inside an X server
87pub fn get_backend_type() -> BackendType {
88    BackendType::None
89}
90
91/// Initialize wlc's callbacks and logger with a `WlcInterface`.
92///
93/// # Deprecated
94/// wlc has deprecated this callback interface. They offer a new API with a
95/// series of methods found in the `callback` module
96///
97/// To initialize wlc, register your callbacks with the functions described in
98/// the `callbacks` module, and the logger using `log_set_handler` or
99/// `log_set_default_handler`. Then call `init2()`.
100///
101/// # Permissions
102/// If a compositor is initialized from the tty using suid or logind, it will
103/// drop extra permissions after a call to `init()` or `init2()`. It is strongly
104/// recommended to delay code which is not registering callbacks until after
105/// this call.
106///
107/// # wlc Example
108/// ```no_run
109/// use rustwlc;
110/// use rustwlc::callback;
111/// use rustwlc::WlcView;
112///
113/// // An example callback function
114/// // See the various functions in the callback module for more information
115/// extern "C" fn view_focus_callback(view: WlcView, focused: bool) {
116///     println!("A view came into focus!");
117/// }
118///
119/// // Set a default log callback
120/// rustwlc::log_set_default_handler();
121///
122/// // Register some callbacks
123/// callback::view_focus(view_focus_callback);
124/// // ... and additional callbacks
125///
126/// // The only thing your code should do before init2 is register callbacks
127/// // and log handlers.
128/// let run_wlc = rustwlc::init()
129///     .expect("Unable to initialize wlc!");
130///
131/// run_wlc();
132/// ```
133pub fn init() -> Option<fn() -> ()> {
134    Some(run_wlc)
135}
136
137/// Deprecated alias to init().
138///
139/// When wlc went to 0.0.1, they added an argumentless init2
140/// to replace the old init that took a WlcInterface. Now,
141/// init2 has been renamed init and init is removed.
142pub fn init2() -> Option<fn() -> ()> {
143    init()
144}
145
146/// Runs wlc's event loop.
147///
148/// The initialize functions will return this function in an Option.
149/// Only then can it be called to being wlc's main event loop.
150fn run_wlc() {
151    println!("Attempted to run wlc!");
152}
153
154/// Halts execution of wlc.
155pub fn terminate() {
156}
157
158/// Registers a C callback for wlc logging.
159///
160/// Note that `rustwlc::log_set_default_handler()` will register a simple callback
161/// that will print the type and text to the console.
162///
163/// # Parameters
164/// The `handler` callback has two parameters:
165/// * `type`: The `LogType` of the message being printed.
166/// * `text`: The text to be logged, currently in C form. One may call `rustwlc::pointer_to_string`
167/// to convert it to a Rust String.
168///
169/// # Safety
170/// The callback function (like other callbacks in `rustwlc`) must be marked as extern as it is called
171/// from C code.
172///
173/// In addition, `unsafe` will be required to convert the text into a Rust String.
174pub fn log_set_handler(handler: extern "C" fn(type_: LogType, text: *const libc::c_char)) {
175}
176
177/// Registers a Rust callback for wlc logging.
178
179/// This is a nice convenience function that should be used in place of
180/// `log_set_handler`. That way you can just pass a safe Rust `&str`
181/// and not depend on libc`.
182pub fn log_set_rust_handler(handler: fn(type_: LogType, text: &str)) {
183}
184
185#[allow(dead_code)]
186fn default_log_callback(log_type: LogType, text: &str) {
187    println!("wlc [{:?}] {}", log_type, text);
188}
189
190/// Sets the wlc log callback to a simple function that prints to console.
191///
192/// Not calling any `log_set_handler` will have no logging, use this or
193/// `log_set_handler` with a callback to use wlc logging.
194///
195/// # Example
196/// ```no_run
197/// use rustwlc;
198///
199/// // An example where only the default log handler is registered
200/// rustwlc::log_set_default_handler();
201///
202/// if let Some(run_wlc) = rustwlc::init2() {
203///      run_wlc();
204/// }
205/// else {
206///     panic!("Unable to initialize wlc!");
207/// }
208/// ```
209pub fn log_set_default_handler() {
210}
211
212/// Unsafe strings conversion function.
213///
214/// Converts a `*const libc::c_char` to an owned `String`.
215/// Useful for log callbacks.
216///
217/// # Example
218/// Standard usage may be for the log callbacks.
219/// ```rust
220/// extern "C" fn default_log_callback(log_type: LogType, text: *const libc::c_char) {
221///     let string = unsafe { pointer_to_string(text) };
222///     println!("wlc [{:?}]: {}", log_type, string);
223/// }
224/// ```
225pub unsafe fn pointer_to_string(pointer: *const libc::c_char) -> String {
226    if pointer.is_null() {
227        return "".to_string();
228    }
229    let slice = ffi::CStr::from_ptr(pointer);
230    slice.to_string_lossy().into_owned()
231}