Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server decorations #20

Merged
merged 3 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ dlib = "0.4"
lazy_static = "1"
memmap = "0.6"
tempfile = "3.0"
wayland-commons = { version = "0.20.7" }
wayland-client = { version = "0.20.7", features = ["cursor"] }
wayland-protocols = { version = "0.20.7", features = ["client", "unstable_protocols"] }
wayland-commons = { version = "0.20.11" }
wayland-client = { version = "0.20.11", features = ["cursor"] }
wayland-protocols = { version = "0.20.11", features = ["client", "unstable_protocols"] }

[dev-dependencies]
image = "0.19"
wayland-client = { version = "0.20.7", features = ["dlopen"] }
wayland-client = { version = "0.20.11", features = ["dlopen"] }
byteorder = "1.0"
6 changes: 6 additions & 0 deletions examples/compositor_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ fn main() {
}
}
});

if env.decorations_mgr.is_some() {
println!("-> Compositor supports server-side decorations.")
} else {
println!("-> Compositor does not support server-side decorations.")
}
}
3 changes: 2 additions & 1 deletion examples/image_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ fn main() {
// only provide this one, but if you don't like the (arguably ugly)
// borders it draws, you just need to implement the appropriate trait
// to create your own.
let mut window = Window::<BasicFrame>::init(
let mut window = Window::<BasicFrame>::init_with_decorations(
surface, // the wl_surface that serves as the basis of this window
image.dimensions(), // the initial internal dimensions of the window
&env.compositor, // -+
&env.subcompositor, // | The Window constructor needs access to these globals
&env.shm, // | to initialize the window
&env.shell, // -+
env.decorations_mgr.as_ref(), // to use native decorations if available
move |evt, ()| {
// This is the closure that process the Window events.
// There are 3 possible events:
Expand Down
87 changes: 45 additions & 42 deletions examples/kbd_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::sync::{Arc, Mutex};

use byteorder::{NativeEndian, WriteBytesExt};

use sctk::keyboard::{map_keyboard_auto_with_repeat, Event as KbEvent, KeyRepeatKind, KeyRepeatEvent};
use sctk::keyboard::{
map_keyboard_auto_with_repeat, Event as KbEvent, KeyRepeatEvent, KeyRepeatKind,
};
use sctk::reexports::client::protocol::wl_buffer::RequestsTrait as BufferRequests;
use sctk::reexports::client::protocol::wl_compositor::RequestsTrait as CompositorRequests;
use sctk::reexports::client::protocol::wl_display::RequestsTrait as DisplayRequests;
Expand Down Expand Up @@ -42,13 +44,14 @@ fn main() {
let next_action = Arc::new(Mutex::new(None::<WEvent>));

let waction = next_action.clone();
let mut window = Window::<BasicFrame>::init(
let mut window = Window::<BasicFrame>::init_with_decorations(
surface,
dimensions,
&env.compositor,
&env.subcompositor,
&env.shm,
&env.shell,
env.decorations_mgr.as_ref(),
move |evt, ()| {
let mut next_action = waction.lock().unwrap();
// Keep last event in priority order : Close > Configure > Refresh
Expand Down Expand Up @@ -80,49 +83,49 @@ fn main() {

window.new_seat(&seat);

let _keyboard = map_keyboard_auto_with_repeat(seat.get_keyboard().unwrap(), KeyRepeatKind::System,
move |event: KbEvent, _| {
match event {
KbEvent::Enter {
modifiers, keysyms, ..
} => {
println!(
"Gained focus while {} keys pressed and modifiers are {:?}.",
keysyms.len(),
modifiers
);
}
KbEvent::Leave { .. } => {
println!("Lost focus.");
}
KbEvent::Key {
keysym,
state,
utf8,
modifiers,
..
} => {
println!("Key {:?}: {:x}.", state, keysym);
println!(" -> Modifers are {:?}", modifiers);
if let Some(txt) = utf8 {
println!(" -> Received text \"{}\".", txt);
}
}
KbEvent::RepeatInfo { rate, delay } => {
println!(
let _keyboard = map_keyboard_auto_with_repeat(
seat.get_keyboard().unwrap(),
KeyRepeatKind::System,
move |event: KbEvent, _| match event {
KbEvent::Enter {
modifiers, keysyms, ..
} => {
println!(
"Gained focus while {} keys pressed and modifiers are {:?}.",
keysyms.len(),
modifiers
);
}
KbEvent::Leave { .. } => {
println!("Lost focus.");
}
KbEvent::Key {
keysym,
state,
utf8,
modifiers,
..
} => {
println!("Key {:?}: {:x}.", state, keysym);
println!(" -> Modifers are {:?}", modifiers);
if let Some(txt) = utf8 {
println!(" -> Received text \"{}\".", txt);
}
}
KbEvent::RepeatInfo { rate, delay } => {
println!(
"Received repeat info: start repeating every {}ms after an initial delay of {}ms",
rate, delay
);
}
}
},
move |repeat_event: KeyRepeatEvent, _| {
println!("Repeated key {:x}.", repeat_event.keysym);
println!(" -> Modifers are {:?}", repeat_event.modifiers);
if let Some(txt) = repeat_event.utf8 {
println!(" -> Received text \"{}\".", txt);
}
}
},
move |repeat_event: KeyRepeatEvent, _| {
println!("Repeated key {:x}.", repeat_event.keysym);
println!(" -> Modifers are {:?}", repeat_event.modifiers);
if let Some(txt) = repeat_event.utf8 {
println!(" -> Received text \"{}\".", txt);
}
},
);

if !env.shell.needs_configure() {
Expand All @@ -138,7 +141,7 @@ fn main() {
Some(WEvent::Refresh) => {
window.refresh();
window.surface().commit();
},
}
Some(WEvent::Configure { new_size, states }) => {
if let Some((w, h)) = new_size {
window.resize(w, h);
Expand Down
6 changes: 4 additions & 2 deletions examples/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ fn main() {
let next_action = Arc::new(Mutex::new(None::<WEvent>));

let waction = next_action.clone();
let mut window = Window::<BasicFrame>::init(
let mut window = Window::<BasicFrame>::init_with_decorations(
surface,
dimensions,
&env.compositor,
&env.subcompositor,
&env.shm,
&env.shell,
env.decorations_mgr.as_ref(),
move |evt, ()| {
let mut next_action = waction.lock().unwrap();
// Keep last event in priority order : Close > Configure > Refresh
Expand Down Expand Up @@ -88,7 +89,8 @@ fn main() {
match event {
KbEvent::Key {
state,
utf8: Some(text), ..
utf8: Some(text),
..
} => {
if text == "p" && state == KeyState::Pressed {
// pressed the 'p' key, try to read contents !
Expand Down
5 changes: 3 additions & 2 deletions src/data_device/data_device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use wayland_client::commons::Implementation;
use wayland_client::protocol::{wl_data_device, wl_data_device_manager, wl_data_offer, wl_seat,
wl_surface};
use wayland_client::protocol::{
wl_data_device, wl_data_device_manager, wl_data_offer, wl_seat, wl_surface,
};
use wayland_client::{NewProxy, Proxy, QueueToken};

use wayland_client::protocol::wl_data_device::RequestsTrait as DeviceRequests;
Expand Down
20 changes: 18 additions & 2 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use std::io;
use std::sync::{Arc, Mutex};

use wayland_client::commons::Implementation;
use wayland_client::protocol::{wl_compositor, wl_data_device_manager, wl_output, wl_registry,
wl_shell, wl_shm, wl_subcompositor};
use wayland_client::protocol::{
wl_compositor, wl_data_device_manager, wl_output, wl_registry, wl_shell, wl_shm,
wl_subcompositor,
};
use wayland_client::{EventQueue, GlobalEvent, GlobalManager, NewProxy, Proxy};
use wayland_protocols::unstable::xdg_decoration::v1::client::zxdg_decoration_manager_v1;
use wayland_protocols::unstable::xdg_shell::v6::client::zxdg_shell_v6;
use wayland_protocols::xdg_shell::client::xdg_wm_base;

Expand Down Expand Up @@ -60,6 +63,8 @@ pub struct Environment {
pub data_device_manager: Proxy<wl_data_device_manager::WlDataDeviceManager>,
/// A manager for handling the advertized outputs
pub outputs: ::output::OutputMgr,
/// The decoration manager, if the server supports server-side decorations
pub decorations_mgr: Option<Proxy<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>>,
shm_formats: Arc<Mutex<Vec<wl_shm::Format>>>,
}

Expand Down Expand Up @@ -170,6 +175,16 @@ impl Environment {
panic!("Server didn't advertize neither `xdg_wm_base` nor `wl_shell`?!");
};

// try to retrieve the decoration manager
let decorations_mgr = if let Shell::Xdg(_) = shell {
manager
.instantiate_auto::<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>()
.ok()
.map(|mgr| mgr.implement(|evt, _| match evt {}))
} else {
None
};

// sync to retrieve the global events
evq.sync_roundtrip()?;

Expand All @@ -181,6 +196,7 @@ impl Environment {
shm,
shm_formats,
data_device_manager,
decorations_mgr,
outputs,
})
}
Expand Down
86 changes: 84 additions & 2 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use std::sync::{Arc, Mutex};

use wayland_client::commons::Implementation;
use wayland_client::protocol::{wl_compositor, wl_output, wl_seat, wl_shm, wl_subcompositor,
wl_surface};
use wayland_client::protocol::{
wl_compositor, wl_output, wl_seat, wl_shm, wl_subcompositor, wl_surface,
};
use wayland_client::Proxy;

use wayland_protocols::xdg_shell::client::xdg_toplevel::ResizeEdge;
pub use wayland_protocols::xdg_shell::client::xdg_toplevel::State;

use self::zxdg_decoration_manager_v1::RequestsTrait as DecorationMgrRequests;
use self::zxdg_toplevel_decoration_v1::RequestsTrait as DecorationRequests;
use wayland_protocols::unstable::xdg_decoration::v1::client::{
zxdg_decoration_manager_v1, zxdg_toplevel_decoration_v1,
};

use Shell;

mod basic_frame;
Expand Down Expand Up @@ -54,6 +61,7 @@ struct WindowInner<F> {
max_size: Option<(u32, u32)>,
current_size: (u32, u32),
old_size: Option<(u32, u32)>,
decorated: bool,
}

/// A window
Expand All @@ -74,6 +82,7 @@ struct WindowInner<F> {
pub struct Window<F: Frame> {
frame: Arc<Mutex<F>>,
surface: Proxy<wl_surface::WlSurface>,
decoration: Option<Proxy<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>>,
shell_surface: Arc<Box<shell::ShellSurface>>,
inner: Arc<Mutex<Option<WindowInner<F>>>>,
}
Expand All @@ -92,6 +101,36 @@ impl<F: Frame + 'static> Window<F> {
shell: &Shell,
implementation: Impl,
) -> Result<Window<F>, F::Error>
where
Impl: Implementation<(), Event> + Send,
{
Self::init_with_decorations(
surface,
initial_dims,
compositor,
subcompositor,
shm,
shell,
None,
implementation,
)
}

/// Create a new window wrapping a given wayland surface as its main content and using
/// server decorations if available

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set the client's preference to SSD if you prefer to use SSD. Right now the client preference is not set, so the compositor decides.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My goal with that was mostly to aim for "follow the compositor's preference". But yeah, I need to clarify what policy to actually follow.

///
/// It can fail if the initialization of the frame fails (for example if the
/// frame class fails to initialize its SHM).
pub fn init_with_decorations<Impl>(
surface: Proxy<wl_surface::WlSurface>,
initial_dims: (u32, u32),
compositor: &Proxy<wl_compositor::WlCompositor>,
subcompositor: &Proxy<wl_subcompositor::WlSubcompositor>,
shm: &Proxy<wl_shm::WlShm>,
shell: &Shell,
decoration_mgr: Option<&Proxy<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>>,
implementation: Impl,
) -> Result<Window<F>, F::Error>
where
Impl: Implementation<(), Event> + Send,
{
Expand Down Expand Up @@ -192,10 +231,42 @@ impl<F: Frame + 'static> Window<F> {
max_size: None,
current_size: initial_dims,
old_size: None,
decorated: true,
});

let decoration_frame = frame.clone();
let decoration_inner = inner.clone();
// init decoration if applicable
let decoration = match (shell_surface.get_xdg(), decoration_mgr) {
(Some(toplevel), Some(mgr)) => mgr.get_toplevel_decoration(toplevel).ok().map(
move |newdec| {
newdec.implement(move |event, _| {
use self::zxdg_toplevel_decoration_v1::{Event, Mode};
let Event::Configure { mode } = event;
match mode {
Mode::ServerSide => {
decoration_frame.lock().unwrap().set_hidden(true);
}
Mode::ClientSide => {
let want_decorate = decoration_inner
.lock()
.unwrap()
.as_ref()
.map(|inner| inner.decorated)
.unwrap_or(false);
decoration_frame.lock().unwrap().set_hidden(!want_decorate);
}
}
})
},
),
_ => None,
};

Ok(Window {
frame,
shell_surface,
decoration,
surface,
inner,
})
Expand Down Expand Up @@ -253,7 +324,18 @@ impl<F: Frame + 'static> Window<F> {
/// You need to call `refresh()` afterwards for this to properly
/// take effect.
pub fn set_decorate(&self, decorate: bool) {
use self::zxdg_toplevel_decoration_v1::Mode;
self.frame.lock().unwrap().set_hidden(!decorate);
if let Some(ref dec) = self.decoration {
if decorate {
// let the server decide decorations
dec.unset_mode();
} else {
// we must be the decorators, to be able to not
// show any decoration
dec.set_mode(Mode::ClientSide);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be more complicated to implement, but if you don't want to be decorated, you should destroy the decoration object. The compositor always has the last word wrt decoration mode.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, indeed. This will be more convoluted to implement though.

}
}
}

/// Set whether the window should be resizeable by the user
Expand Down
1 change: 1 addition & 0 deletions src/window/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ pub(crate) trait ShellSurface: Send + Sync {
fn set_geometry(&self, x: i32, y: i32, width: i32, height: i32);
fn set_min_size(&self, size: Option<(i32, i32)>);
fn set_max_size(&self, size: Option<(i32, i32)>);
fn get_xdg(&self) -> Option<&Proxy<xdg_toplevel::XdgToplevel>>;
}
Loading