smithay/anvil/src/winit.rs

118 lines
3.2 KiB
Rust
Raw Normal View History

2018-10-02 21:37:24 +00:00
use std::{
cell::RefCell,
rc::Rc,
sync::{atomic::AtomicBool, Arc},
};
2017-06-11 21:03:25 +00:00
2018-10-02 21:37:24 +00:00
use smithay::{
2018-11-22 09:10:30 +00:00
backend::{egl::EGLGraphicsBackend, graphics::gl::GLGraphicsBackend, input::InputBackend, winit},
2018-10-02 21:37:24 +00:00
wayland::{
data_device::{default_action_chooser, init_data_device, set_data_device_focus},
2018-10-02 21:37:24 +00:00
output::{Mode, Output, PhysicalProperties},
seat::{Seat, XkbConfig},
shm::init_shm_global,
},
wayland_server::{calloop::EventLoop, protocol::wl_output, Display},
};
use slog::Logger;
use glium_drawer::GliumDrawer;
2018-05-08 10:47:09 +00:00
use input_handler::AnvilInputHandler;
2018-09-24 22:32:09 +00:00
use shell::init_shell;
2017-09-03 17:53:29 +00:00
2018-09-24 22:30:39 +00:00
pub fn run_winit(display: &mut Display, event_loop: &mut EventLoop<()>, log: Logger) -> Result<(), ()> {
let (renderer, mut input) = winit::init(log.clone()).map_err(|_| ())?;
2017-03-07 10:53:57 +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
},
));
2017-12-21 15:01:16 +00:00
2017-12-28 14:28:15 +00:00
let (w, h) = renderer.get_framebuffer_dimensions();
let drawer = GliumDrawer::init(renderer, egl_display, log.clone());
2017-12-21 15:01:16 +00:00
let name = display.add_socket_auto().unwrap().into_string().unwrap();
info!(log, "Listening on wayland socket"; "name" => name.clone());
::std::env::set_var("WAYLAND_DISPLAY", name);
2018-05-08 10:47:09 +00:00
let running = Arc::new(AtomicBool::new(true));
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
2018-09-24 22:30:39 +00:00
init_shm_global(display, vec![], log.clone());
2017-06-11 21:03:25 +00:00
2018-09-24 22:30:39 +00:00
let (compositor_token, _, _, window_map) = init_shell(display, log.clone());
2017-03-07 10:53:57 +00:00
init_data_device(display, |_| {}, default_action_chooser, log.clone());
2018-11-18 18:39:33 +00:00
2018-09-24 22:30:39 +00:00
let (mut seat, _) = Seat::new(display, "winit".into(), log.clone());
2017-09-22 12:57:11 +00:00
2018-04-22 09:58:39 +00:00
let pointer = seat.add_pointer();
2018-11-18 18:39:33 +00:00
2018-09-24 22:32:09 +00:00
let keyboard = seat
2018-11-18 18:39:33 +00:00
.add_keyboard(XkbConfig::default(), 1000, 500, |seat, focus| {
set_data_device_focus(seat, focus.and_then(|s| s.client()))
2018-12-08 17:31:08 +00:00
})
.expect("Failed to initialize the keyboard");
2017-06-13 14:52:43 +00:00
2018-04-22 09:58:39 +00:00
let (output, _) = Output::new(
display,
2017-10-01 17:14:25 +00:00
"Winit".into(),
PhysicalProperties {
width: 0,
height: 0,
subpixel: wl_output::Subpixel::Unknown,
2018-09-24 22:30:39 +00:00
make: "Smithay".into(),
2017-10-01 17:14:25 +00:00
model: "Winit".into(),
},
log.clone(),
);
2018-04-22 09:58:39 +00:00
output.change_current_state(
Some(Mode {
2017-10-01 17:14:25 +00:00
width: w as i32,
height: h as i32,
refresh: 60_000,
2018-04-22 09:58:39 +00:00
}),
None,
None,
);
2018-04-22 09:58:39 +00:00
output.set_preferred(Mode {
width: w as i32,
height: h as i32,
refresh: 60_000,
});
2018-05-08 10:47:09 +00:00
input.set_handler(AnvilInputHandler::new(
log.clone(),
2018-04-22 09:58:39 +00:00
pointer,
keyboard,
2018-05-08 10:47:09 +00:00
window_map.clone(),
(0, 0),
running.clone(),
Rc::new(RefCell::new((0.0, 0.0))),
));
2017-09-22 12:57:11 +00:00
info!(log, "Initialization completed, starting the main loop.");
2017-03-07 10:53:57 +00:00
loop {
2018-04-22 09:58:39 +00:00
input.dispatch_new_events().unwrap();
2018-05-08 18:08:17 +00:00
drawer.draw_windows(&*window_map.borrow(), compositor_token, &log);
2018-09-24 22:32:09 +00:00
event_loop
.dispatch(Some(::std::time::Duration::from_millis(16)), &mut ())
.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();
}
2018-05-08 10:47:09 +00:00
}