2017-06-13 14:52:43 +00:00
|
|
|
#[macro_use]
|
2017-06-02 13:26:50 +00:00
|
|
|
extern crate glium;
|
2017-09-05 19:23:40 +00:00
|
|
|
extern crate rand;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate slog;
|
|
|
|
extern crate slog_async;
|
|
|
|
extern crate slog_term;
|
2017-09-05 17:51:05 +00:00
|
|
|
#[macro_use(define_roles)]
|
|
|
|
extern crate smithay;
|
|
|
|
extern crate wayland_server;
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2017-06-13 14:52:43 +00:00
|
|
|
mod helpers;
|
2017-06-11 21:03:25 +00:00
|
|
|
|
2017-06-02 13:26:50 +00:00
|
|
|
use glium::Surface;
|
2018-01-07 21:30:38 +00:00
|
|
|
use helpers::{init_shell, Buffer, GliumDrawer, MyWindowMap};
|
2017-09-03 17:53:29 +00:00
|
|
|
use slog::{Drain, Logger};
|
2017-09-13 20:51:35 +00:00
|
|
|
use smithay::backend::graphics::egl::EGLGraphicsBackend;
|
2018-01-05 19:04:46 +00:00
|
|
|
use smithay::backend::graphics::egl::wayland::{EGLWaylandExtensions, Format};
|
2017-10-31 10:28:41 +00:00
|
|
|
use smithay::backend::input::{self, Event, InputBackend, InputHandler, KeyboardKeyEvent, PointerAxisEvent,
|
|
|
|
PointerButtonEvent, PointerMotionAbsoluteEvent};
|
2017-05-21 20:51:38 +00:00
|
|
|
use smithay::backend::winit;
|
2017-09-28 19:57:02 +00:00
|
|
|
use smithay::wayland::compositor::{SubsurfaceRole, TraversalAction};
|
|
|
|
use smithay::wayland::compositor::roles::Role;
|
2017-10-01 17:14:25 +00:00
|
|
|
use smithay::wayland::output::{Mode, Output, PhysicalProperties};
|
2017-09-28 19:57:02 +00:00
|
|
|
use smithay::wayland::seat::{KeyboardHandle, PointerHandle, Seat};
|
|
|
|
use smithay::wayland::shm::init_shm_global;
|
2017-09-22 12:57:11 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2018-01-12 14:49:55 +00:00
|
|
|
use wayland_server::EventLoopHandle;
|
2017-10-01 17:14:25 +00:00
|
|
|
use wayland_server::protocol::{wl_output, wl_pointer};
|
2017-09-22 12:57:11 +00:00
|
|
|
|
2018-01-05 19:04:46 +00:00
|
|
|
struct WinitInputHandler {
|
2017-09-22 12:57:11 +00:00
|
|
|
log: Logger,
|
|
|
|
pointer: PointerHandle,
|
2017-09-22 16:49:58 +00:00
|
|
|
keyboard: KeyboardHandle,
|
2018-01-05 19:04:46 +00:00
|
|
|
window_map: Rc<RefCell<MyWindowMap>>,
|
2017-09-22 12:57:11 +00:00
|
|
|
pointer_location: (f64, f64),
|
|
|
|
serial: u32,
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:04:46 +00:00
|
|
|
impl WinitInputHandler {
|
2017-09-22 12:57:11 +00:00
|
|
|
fn next_serial(&mut self) -> u32 {
|
|
|
|
self.serial += 1;
|
|
|
|
self.serial
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:04:46 +00:00
|
|
|
impl InputHandler<winit::WinitInputBackend> for WinitInputHandler {
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_seat_created(&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* never happens with winit */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_seat_destroyed(&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* never happens with winit */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_seat_changed(&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* never happens with winit */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_keyboard_key(
|
|
|
|
&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, evt: winit::WinitKeyboardInputEvent
|
|
|
|
) {
|
2017-09-28 17:34:31 +00:00
|
|
|
let keycode = evt.key_code();
|
2017-09-22 12:57:11 +00:00
|
|
|
let state = evt.state();
|
|
|
|
debug!(self.log, "key"; "keycode" => keycode, "state" => format!("{:?}", state));
|
|
|
|
let serial = self.next_serial();
|
|
|
|
self.keyboard.input(keycode, state, serial, |_, _| true);
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_pointer_move(&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, _: input::UnusedEvent) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* never happens with winit */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_pointer_move_absolute(
|
|
|
|
&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, evt: winit::WinitMouseMovedEvent
|
|
|
|
) {
|
2017-09-22 12:57:11 +00:00
|
|
|
// on winit, mouse events are already in pixel coordinates
|
|
|
|
let (x, y) = evt.position();
|
|
|
|
self.pointer_location = (x, y);
|
|
|
|
let serial = self.next_serial();
|
|
|
|
let under = self.window_map.borrow().get_surface_under((x, y));
|
|
|
|
self.pointer.motion(
|
|
|
|
under.as_ref().map(|&(ref s, (x, y))| (s, x, y)),
|
|
|
|
serial,
|
|
|
|
evt.time(),
|
|
|
|
);
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_pointer_button(
|
|
|
|
&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, evt: winit::WinitMouseInputEvent
|
|
|
|
) {
|
2017-09-22 12:57:11 +00:00
|
|
|
let serial = self.next_serial();
|
|
|
|
let button = match evt.button() {
|
|
|
|
input::MouseButton::Left => 0x110,
|
|
|
|
input::MouseButton::Right => 0x111,
|
|
|
|
input::MouseButton::Middle => 0x112,
|
|
|
|
input::MouseButton::Other(b) => b as u32,
|
|
|
|
};
|
|
|
|
let state = match evt.state() {
|
|
|
|
input::MouseButtonState::Pressed => {
|
|
|
|
// change the keyboard focus
|
|
|
|
let under = self.window_map
|
|
|
|
.borrow_mut()
|
|
|
|
.get_surface_and_bring_to_top(self.pointer_location);
|
|
|
|
self.keyboard
|
|
|
|
.set_focus(under.as_ref().map(|&(ref s, _)| s), serial);
|
|
|
|
wl_pointer::ButtonState::Pressed
|
|
|
|
}
|
|
|
|
input::MouseButtonState::Released => wl_pointer::ButtonState::Released,
|
|
|
|
};
|
|
|
|
self.pointer.button(button, state, serial, evt.time());
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_pointer_axis(
|
|
|
|
&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, evt: winit::WinitMouseWheelEvent
|
|
|
|
) {
|
2017-10-15 00:11:04 +00:00
|
|
|
let axis = match evt.axis() {
|
|
|
|
input::Axis::Vertical => wayland_server::protocol::wl_pointer::Axis::VerticalScroll,
|
|
|
|
input::Axis::Horizontal => wayland_server::protocol::wl_pointer::Axis::HorizontalScroll,
|
|
|
|
};
|
|
|
|
self.pointer.axis(axis, evt.amount(), evt.time());
|
2017-09-22 12:57:11 +00:00
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_touch_down(
|
|
|
|
&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, _: winit::WinitTouchStartedEvent
|
|
|
|
) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* not done in this example */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_touch_motion(
|
|
|
|
&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, _: winit::WinitTouchMovedEvent
|
|
|
|
) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* not done in this example */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_touch_up(&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, _: winit::WinitTouchEndedEvent) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* not done in this example */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_touch_cancel(
|
|
|
|
&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, _: winit::WinitTouchCancelledEvent
|
|
|
|
) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* not done in this example */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_touch_frame(&mut self, _evlh: &mut EventLoopHandle, _: &input::Seat, _: input::UnusedEvent) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* never happens with winit */
|
|
|
|
}
|
2018-01-12 14:49:55 +00:00
|
|
|
fn on_input_config_changed(&mut self, _evlh: &mut EventLoopHandle, _: &mut ()) {
|
2017-09-22 12:57:11 +00:00
|
|
|
/* never happens with winit */
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 17:53:29 +00:00
|
|
|
|
2017-03-07 10:53:57 +00:00
|
|
|
fn main() {
|
2017-06-11 21:03:25 +00:00
|
|
|
// A logger facility, here we use the terminal for this example
|
2017-06-23 13:40:28 +00:00
|
|
|
let log = Logger::root(
|
|
|
|
slog_async::Async::default(slog_term::term_full().fuse()).fuse(),
|
|
|
|
o!(),
|
|
|
|
);
|
2017-06-11 21:03:25 +00:00
|
|
|
|
2017-05-21 20:40:15 +00:00
|
|
|
// Initialize a simple backend for testing
|
2017-06-11 21:03:25 +00:00
|
|
|
let (renderer, mut input) = winit::init(log.clone()).unwrap();
|
2017-05-21 20:40:15 +00:00
|
|
|
|
2017-06-13 14:52:43 +00:00
|
|
|
let (mut display, mut event_loop) = wayland_server::create_display();
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2018-01-05 19:04:46 +00:00
|
|
|
let egl_display = Rc::new(RefCell::new(
|
|
|
|
if let Ok(egl_display) = renderer.bind_wl_display(&display) {
|
|
|
|
info!(log, "EGL hardware-acceleration enabled");
|
|
|
|
Some(egl_display)
|
|
|
|
} else {
|
|
|
|
None
|
2018-01-07 21:30:38 +00:00
|
|
|
},
|
2018-01-05 19:04:46 +00:00
|
|
|
));
|
2017-12-21 15:01:16 +00:00
|
|
|
|
2017-12-28 14:28:15 +00:00
|
|
|
let (w, h) = renderer.get_framebuffer_dimensions();
|
2018-01-05 19:04:46 +00:00
|
|
|
let drawer = GliumDrawer::from(renderer);
|
2017-12-21 15:01:16 +00:00
|
|
|
|
2017-06-11 21:03:25 +00:00
|
|
|
/*
|
2017-09-20 13:03:58 +00:00
|
|
|
* Initialize the globals
|
2017-06-11 21:03:25 +00:00
|
|
|
*/
|
2017-06-13 14:52:43 +00:00
|
|
|
|
2017-09-20 13:03:58 +00:00
|
|
|
init_shm_global(&mut event_loop, vec![], log.clone());
|
2017-06-11 21:03:25 +00:00
|
|
|
|
2018-01-07 21:30:38 +00:00
|
|
|
let (compositor_token, _shell_state_token, window_map) =
|
|
|
|
init_shell(&mut event_loop, log.clone(), egl_display);
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2017-09-22 12:57:11 +00:00
|
|
|
let (seat_token, _) = Seat::new(&mut event_loop, "winit".into(), log.clone());
|
|
|
|
|
|
|
|
let pointer = event_loop.state().get_mut(&seat_token).add_pointer();
|
|
|
|
let keyboard = event_loop
|
|
|
|
.state()
|
|
|
|
.get_mut(&seat_token)
|
|
|
|
.add_keyboard("", "fr", "oss", None, 1000, 500)
|
|
|
|
.expect("Failed to initialize the keyboard");
|
2017-06-13 14:52:43 +00:00
|
|
|
|
2017-10-01 17:14:25 +00:00
|
|
|
let (output_token, _output_global) = Output::new(
|
|
|
|
&mut event_loop,
|
|
|
|
"Winit".into(),
|
|
|
|
PhysicalProperties {
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
subpixel: wl_output::Subpixel::Unknown,
|
|
|
|
maker: "Smithay".into(),
|
|
|
|
model: "Winit".into(),
|
|
|
|
},
|
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
event_loop
|
|
|
|
.state()
|
|
|
|
.get_mut(&output_token)
|
|
|
|
.change_current_state(
|
|
|
|
Some(Mode {
|
|
|
|
width: w as i32,
|
|
|
|
height: h as i32,
|
|
|
|
refresh: 60_000,
|
|
|
|
}),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
event_loop
|
|
|
|
.state()
|
|
|
|
.get_mut(&output_token)
|
|
|
|
.set_preferred(Mode {
|
|
|
|
width: w as i32,
|
|
|
|
height: h as i32,
|
|
|
|
refresh: 60_000,
|
|
|
|
});
|
|
|
|
|
2018-01-12 14:49:55 +00:00
|
|
|
input.set_handler(
|
|
|
|
&mut event_loop,
|
|
|
|
WinitInputHandler {
|
|
|
|
log: log.clone(),
|
|
|
|
pointer,
|
|
|
|
keyboard,
|
|
|
|
window_map: window_map.clone(),
|
|
|
|
pointer_location: (0.0, 0.0),
|
|
|
|
serial: 0,
|
|
|
|
},
|
|
|
|
);
|
2017-09-22 12:57:11 +00:00
|
|
|
|
2017-06-13 14:52:43 +00:00
|
|
|
/*
|
|
|
|
* Add a listening socket:
|
|
|
|
*/
|
2017-06-13 15:39:25 +00:00
|
|
|
let name = display.add_socket_auto().unwrap().into_string().unwrap();
|
2017-06-13 14:52:43 +00:00
|
|
|
println!("Listening on socket: {}", name);
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2017-06-02 13:26:50 +00:00
|
|
|
loop {
|
2018-01-12 14:49:55 +00:00
|
|
|
input.dispatch_new_events(&mut event_loop).unwrap();
|
2017-06-02 13:26:50 +00:00
|
|
|
|
2017-09-13 20:51:35 +00:00
|
|
|
let mut frame = drawer.draw();
|
2017-12-28 14:28:15 +00:00
|
|
|
frame.clear(None, Some((0.8, 0.8, 0.9, 1.0)), false, Some(1.0), None);
|
2017-06-13 14:52:43 +00:00
|
|
|
// redraw the frame, in a simple but inneficient way
|
|
|
|
{
|
2017-09-13 20:51:35 +00:00
|
|
|
let screen_dimensions = drawer.get_framebuffer_dimensions();
|
2017-09-22 12:56:59 +00:00
|
|
|
window_map
|
|
|
|
.borrow()
|
|
|
|
.with_windows_from_bottom_to_top(|toplevel_surface, initial_place| {
|
|
|
|
if let Some(wl_surface) = toplevel_surface.get_surface() {
|
|
|
|
// this surface is a root of a subsurface tree that needs to be drawn
|
|
|
|
compositor_token
|
|
|
|
.with_surface_tree_upward(
|
|
|
|
wl_surface,
|
|
|
|
initial_place,
|
|
|
|
|_surface, attributes, role, &(mut x, mut y)| {
|
2018-01-05 19:04:46 +00:00
|
|
|
// there is actually something to draw !
|
|
|
|
if attributes.user_data.texture.is_none() {
|
|
|
|
let mut remove = false;
|
|
|
|
match attributes.user_data.buffer {
|
|
|
|
Some(Buffer::Egl { ref images }) => {
|
|
|
|
match images.format {
|
|
|
|
Format::RGB | Format::RGBA => {
|
2018-01-07 21:30:38 +00:00
|
|
|
attributes.user_data.texture =
|
|
|
|
drawer.texture_from_egl(&images);
|
|
|
|
}
|
2018-01-05 19:04:46 +00:00
|
|
|
_ => {
|
|
|
|
// we don't handle the more complex formats here.
|
|
|
|
attributes.user_data.texture = None;
|
|
|
|
remove = true;
|
2018-01-07 21:30:38 +00:00
|
|
|
}
|
2018-01-05 19:04:46 +00:00
|
|
|
};
|
2018-01-07 21:30:38 +00:00
|
|
|
}
|
2018-01-05 19:04:46 +00:00
|
|
|
Some(Buffer::Shm { ref data, ref size }) => {
|
2018-01-07 21:30:38 +00:00
|
|
|
attributes.user_data.texture =
|
|
|
|
Some(drawer.texture_from_mem(data, *size));
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-01-05 19:04:46 +00:00
|
|
|
}
|
|
|
|
if remove {
|
|
|
|
attributes.user_data.buffer = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 14:28:15 +00:00
|
|
|
if let Some(ref texture) = attributes.user_data.texture {
|
2017-09-22 12:56:59 +00:00
|
|
|
if let Ok(subdata) = Role::<SubsurfaceRole>::data(role) {
|
|
|
|
x += subdata.x;
|
|
|
|
y += subdata.y;
|
|
|
|
}
|
2017-12-21 15:01:16 +00:00
|
|
|
drawer.render_texture(
|
2017-09-22 12:56:59 +00:00
|
|
|
&mut frame,
|
2017-12-21 15:01:16 +00:00
|
|
|
texture,
|
|
|
|
match *attributes.user_data.buffer.as_ref().unwrap() {
|
2017-12-28 14:28:15 +00:00
|
|
|
Buffer::Egl { ref images } => images.y_inverted,
|
2017-12-21 15:01:16 +00:00
|
|
|
Buffer::Shm { .. } => false,
|
|
|
|
},
|
|
|
|
match *attributes.user_data.buffer.as_ref().unwrap() {
|
2017-12-28 14:28:15 +00:00
|
|
|
Buffer::Egl { ref images } => (images.width, images.height),
|
2017-12-21 15:01:16 +00:00
|
|
|
Buffer::Shm { ref size, .. } => *size,
|
2017-12-10 21:09:17 +00:00
|
|
|
},
|
2017-09-22 12:56:59 +00:00
|
|
|
(x, y),
|
|
|
|
screen_dimensions,
|
2018-01-05 19:04:46 +00:00
|
|
|
glium::Blend {
|
|
|
|
color: glium::BlendingFunction::Addition {
|
|
|
|
source: glium::LinearBlendingFactor::One,
|
2018-01-07 21:30:38 +00:00
|
|
|
destination:
|
|
|
|
glium::LinearBlendingFactor::OneMinusSourceAlpha,
|
2018-01-05 19:04:46 +00:00
|
|
|
},
|
|
|
|
alpha: glium::BlendingFunction::Addition {
|
|
|
|
source: glium::LinearBlendingFactor::One,
|
2018-01-07 21:30:38 +00:00
|
|
|
destination:
|
|
|
|
glium::LinearBlendingFactor::OneMinusSourceAlpha,
|
2018-01-05 19:04:46 +00:00
|
|
|
},
|
|
|
|
..Default::default()
|
2018-01-07 21:30:38 +00:00
|
|
|
},
|
2017-09-22 12:56:59 +00:00
|
|
|
);
|
|
|
|
TraversalAction::DoChildren((x, y))
|
|
|
|
} else {
|
|
|
|
// we are not display, so our children are neither
|
|
|
|
TraversalAction::SkipChildren
|
2017-09-06 14:33:35 +00:00
|
|
|
}
|
2017-09-22 12:56:59 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
});
|
2017-06-13 14:52:43 +00:00
|
|
|
}
|
2017-06-02 13:26:50 +00:00
|
|
|
frame.finish().unwrap();
|
|
|
|
|
|
|
|
event_loop.dispatch(Some(16)).unwrap();
|
2017-06-13 14:52:43 +00:00
|
|
|
display.flush_clients();
|
2017-09-22 12:56:59 +00:00
|
|
|
|
|
|
|
window_map.borrow_mut().refresh();
|
2017-06-02 13:26:50 +00:00
|
|
|
}
|
2017-03-07 10:53:57 +00:00
|
|
|
}
|