rustfmt
This commit is contained in:
parent
38708c4ead
commit
4adea0950a
|
@ -0,0 +1,11 @@
|
|||
error_on_line_overflow = false
|
||||
fn_args_density = "Compressed"
|
||||
fn_args_layout = "Visual"
|
||||
fn_arg_intent = "Tabbed"
|
||||
reorder_imports = true
|
||||
reorder_imported_names = true
|
||||
report_todo = "Always"
|
||||
report_fixme = "Always"
|
||||
normalize_comments = true
|
||||
use_try_shorthand = true
|
||||
max_width = 110
|
|
@ -24,9 +24,9 @@ branches:
|
|||
- master
|
||||
|
||||
before_script:
|
||||
- export PATH=$HOME/.local/bin:$HOME/.cargo/bin:$PATH
|
||||
- which rustfmt || cargo install rustfmt
|
||||
- pip install 'travis-cargo<0.2' --user
|
||||
- export PATH=$HOME/.local/bin:$HOME/.cargo/bin:$PATH
|
||||
- mkdir $(pwd)/socket
|
||||
- export XDG_RUNTIME_DIR="$(pwd)/socket"
|
||||
|
||||
|
|
|
@ -61,13 +61,13 @@
|
|||
//!
|
||||
//! If you are already using an handler for this signal, you probably don't want to use this handler.
|
||||
|
||||
|
||||
use self::pool::{Pool, ResizeError};
|
||||
use std::os::unix::io::RawFd;
|
||||
use std::sync::Arc;
|
||||
|
||||
use wayland_server::{GlobalHandler, EventLoopHandle, Client, Init, Resource, Destroy, resource_is_registered};
|
||||
use wayland_server::protocol::{wl_shm, wl_shm_pool, wl_buffer};
|
||||
|
||||
use self::pool::{Pool, ResizeError};
|
||||
use wayland_server::{Client, Destroy, EventLoopHandle, GlobalHandler, Init, Resource, resource_is_registered};
|
||||
use wayland_server::protocol::{wl_buffer, wl_shm, wl_shm_pool};
|
||||
|
||||
mod pool;
|
||||
|
||||
|
@ -78,7 +78,7 @@ mod pool;
|
|||
pub struct ShmGlobal {
|
||||
formats: Vec<wl_shm::Format>,
|
||||
handler_id: Option<usize>,
|
||||
log: ::slog::Logger
|
||||
log: ::slog::Logger,
|
||||
}
|
||||
|
||||
impl ShmGlobal {
|
||||
|
@ -94,7 +94,7 @@ impl ShmGlobal {
|
|||
where L: Into<Option<::slog::Logger>>
|
||||
{
|
||||
use slog::DrainExt;
|
||||
let log = logger.into().unwrap_or(::slog::Logger::root(::slog_stdlog::StdLog.fuse(), o!()));
|
||||
let log = logger.into().unwrap_or_else(|| ::slog::Logger::root(::slog_stdlog::StdLog.fuse(), o!()));
|
||||
|
||||
// always add the mandatory formats
|
||||
formats.push(wl_shm::Format::Argb8888);
|
||||
|
@ -102,7 +102,7 @@ impl ShmGlobal {
|
|||
ShmGlobal {
|
||||
formats: formats,
|
||||
handler_id: None,
|
||||
log: log.new(o!("smithay_module" => "shm_handler"))
|
||||
log: log.new(o!("smithay_module" => "shm_handler")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,18 +113,16 @@ impl ShmGlobal {
|
|||
///
|
||||
/// This is needed to retrieve the contents of the shm pools and buffers.
|
||||
pub fn get_token(&self) -> ShmGlobalToken {
|
||||
ShmGlobalToken {
|
||||
hid: self.handler_id.clone().expect("ShmGlobal was not initialized."),
|
||||
}
|
||||
ShmGlobalToken { hid: self.handler_id.expect("ShmGlobal was not initialized.") }
|
||||
}
|
||||
}
|
||||
|
||||
/// An SHM global token
|
||||
///
|
||||
/// It is needed to access the contents of the buffers & pools managed by the
|
||||
/// associated ShmGlobal.
|
||||
/// associated `ShmGlobal`.
|
||||
pub struct ShmGlobalToken {
|
||||
hid: usize
|
||||
hid: usize,
|
||||
}
|
||||
|
||||
/// Error that can occur when accessing an SHM buffer
|
||||
|
@ -137,7 +135,7 @@ pub enum BufferAccessError {
|
|||
/// for the memory map.
|
||||
///
|
||||
/// If this error occurs, the client has been killed as a result.
|
||||
BadMap
|
||||
BadMap,
|
||||
}
|
||||
|
||||
impl ShmGlobalToken {
|
||||
|
@ -155,14 +153,14 @@ impl ShmGlobalToken {
|
|||
where F: FnOnce(&[u8], BufferData)
|
||||
{
|
||||
if !resource_is_registered::<_, ShmHandler>(buffer, self.hid) {
|
||||
return Err(BufferAccessError::NotManaged)
|
||||
return Err(BufferAccessError::NotManaged);
|
||||
}
|
||||
let data = unsafe { &*(buffer.get_user_data() as *mut InternalBufferData) };
|
||||
|
||||
if data.pool.with_data_slice(|slice| f(slice, data.data.clone())).is_err() {
|
||||
if data.pool.with_data_slice(|slice| f(slice, data.data)).is_err() {
|
||||
// SIGBUS error occured
|
||||
buffer.post_error(wl_shm::Error::InvalidFd as u32, "Bad pool size.".into());
|
||||
return Err(BufferAccessError::BadMap)
|
||||
return Err(BufferAccessError::BadMap);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -173,7 +171,7 @@ impl Init for ShmGlobal {
|
|||
let id = evqh.add_handler_with_init(ShmHandler {
|
||||
my_id: ::std::usize::MAX,
|
||||
valid_formats: self.formats.clone(),
|
||||
log: self.log.clone()
|
||||
log: self.log.clone(),
|
||||
});
|
||||
self.handler_id = Some(id);
|
||||
}
|
||||
|
@ -181,7 +179,7 @@ impl Init for ShmGlobal {
|
|||
|
||||
impl GlobalHandler<wl_shm::WlShm> for ShmGlobal {
|
||||
fn bind(&mut self, evqh: &mut EventLoopHandle, _: &Client, global: wl_shm::WlShm) {
|
||||
let hid = self.handler_id.clone().expect("ShmGlobal was not initialized.");
|
||||
let hid = self.handler_id.expect("ShmGlobal was not initialized.");
|
||||
// register an handler for this shm
|
||||
evqh.register::<_, ShmHandler>(&global, hid);
|
||||
// and then the custom formats
|
||||
|
@ -194,7 +192,7 @@ impl GlobalHandler<wl_shm::WlShm> for ShmGlobal {
|
|||
struct ShmHandler {
|
||||
my_id: usize,
|
||||
valid_formats: Vec<wl_shm::Format>,
|
||||
log: ::slog::Logger
|
||||
log: ::slog::Logger,
|
||||
}
|
||||
|
||||
impl Init for ShmHandler {
|
||||
|
@ -208,8 +206,9 @@ impl wl_shm::Handler for ShmHandler {
|
|||
fn create_pool(&mut self, evqh: &mut EventLoopHandle, _client: &Client, shm: &wl_shm::WlShm,
|
||||
pool: wl_shm_pool::WlShmPool, fd: RawFd, size: i32) {
|
||||
if size <= 0 {
|
||||
shm.post_error(wl_shm::Error::InvalidFd as u32, "Invalid size for a new wl_shm_pool.".into());
|
||||
return
|
||||
shm.post_error(wl_shm::Error::InvalidFd as u32,
|
||||
"Invalid size for a new wl_shm_pool.".into());
|
||||
return;
|
||||
}
|
||||
let mmap_pool = match Pool::new(fd, size as usize, self.log.clone()) {
|
||||
Ok(p) => p,
|
||||
|
@ -245,22 +244,21 @@ pub struct BufferData {
|
|||
/// Stride of the buffer in bytes
|
||||
pub stride: i32,
|
||||
/// Format used by this buffer
|
||||
pub format: wl_shm::Format
|
||||
pub format: wl_shm::Format,
|
||||
}
|
||||
|
||||
struct InternalBufferData {
|
||||
pool: Arc<Pool>,
|
||||
data: BufferData
|
||||
data: BufferData,
|
||||
}
|
||||
|
||||
impl wl_shm_pool::Handler for ShmHandler {
|
||||
fn create_buffer(&mut self, evqh: &mut EventLoopHandle, _client: &Client,
|
||||
pool: &wl_shm_pool::WlShmPool, buffer: wl_buffer::WlBuffer, offset: i32,
|
||||
width: i32, height: i32, stride: i32, format: wl_shm::Format)
|
||||
{
|
||||
pool: &wl_shm_pool::WlShmPool, buffer: wl_buffer::WlBuffer, offset: i32, width: i32,
|
||||
height: i32, stride: i32, format: wl_shm::Format) {
|
||||
if !self.valid_formats.contains(&format) {
|
||||
buffer.post_error(wl_shm::Error::InvalidFormat as u32, String::new());
|
||||
return
|
||||
return;
|
||||
}
|
||||
let arc_pool = unsafe { &*(pool.get_user_data() as *mut Arc<Pool>) };
|
||||
let data = Box::into_raw(Box::new(InternalBufferData {
|
||||
|
@ -270,22 +268,22 @@ impl wl_shm_pool::Handler for ShmHandler {
|
|||
width: width,
|
||||
height: height,
|
||||
stride: stride,
|
||||
format: format
|
||||
}
|
||||
format: format,
|
||||
},
|
||||
}));
|
||||
evqh.register_with_destructor::<_, ShmHandler, ShmHandler>(&buffer, self.my_id);
|
||||
buffer.set_user_data(data as *mut ());
|
||||
}
|
||||
|
||||
fn resize(&mut self, _evqh: &mut EventLoopHandle, _client: &Client,
|
||||
pool: &wl_shm_pool::WlShmPool, size: i32)
|
||||
{
|
||||
fn resize(&mut self, _evqh: &mut EventLoopHandle, _client: &Client, pool: &wl_shm_pool::WlShmPool,
|
||||
size: i32) {
|
||||
let arc_pool = unsafe { &*(pool.get_user_data() as *mut Arc<Pool>) };
|
||||
match arc_pool.resize(size) {
|
||||
Ok(()) => {},
|
||||
Ok(()) => {}
|
||||
Err(ResizeError::InvalidSize) => {
|
||||
pool.post_error(wl_shm::Error::InvalidFd as u32, "Invalid new size for a wl_shm_pool.".into());
|
||||
},
|
||||
pool.post_error(wl_shm::Error::InvalidFd as u32,
|
||||
"Invalid new size for a wl_shm_pool.".into());
|
||||
}
|
||||
Err(ResizeError::MremapFailed) => {
|
||||
pool.post_error(wl_shm::Error::InvalidFd as u32, "mremap failed.".into());
|
||||
}
|
||||
|
@ -302,7 +300,6 @@ impl Destroy<wl_buffer::WlBuffer> for ShmHandler {
|
|||
|
||||
declare_handler!(ShmHandler, wl_shm_pool::Handler, wl_shm_pool::WlShmPool);
|
||||
|
||||
impl wl_buffer::Handler for ShmHandler {
|
||||
}
|
||||
impl wl_buffer::Handler for ShmHandler {}
|
||||
|
||||
declare_handler!(ShmHandler, wl_buffer::Handler, wl_buffer::WlBuffer);
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
use std::cell::Cell;
|
||||
use std::os::unix::io::RawFd;
|
||||
use std::sync::{RwLock, Once, ONCE_INIT};
|
||||
use std::ptr;
|
||||
|
||||
|
||||
use nix::{c_int, c_void, libc, unistd};
|
||||
use nix::sys::mman;
|
||||
use nix::sys::signal::{self, SigAction, Signal, SigHandler};
|
||||
use nix::sys::signal::{self, SigAction, SigHandler, Signal};
|
||||
use std::cell::Cell;
|
||||
use std::os::unix::io::RawFd;
|
||||
use std::ptr;
|
||||
use std::sync::{ONCE_INIT, Once, RwLock};
|
||||
|
||||
thread_local!(static SIGBUS_GUARD: Cell<(*const MemMap, bool)> = Cell::new((ptr::null_mut(), false)));
|
||||
|
||||
|
@ -15,12 +16,12 @@ static mut OLD_SIGBUS_HANDLER: *mut SigAction = 0 as *mut SigAction;
|
|||
pub struct Pool {
|
||||
map: RwLock<MemMap>,
|
||||
fd: RawFd,
|
||||
log: ::slog::Logger
|
||||
log: ::slog::Logger,
|
||||
}
|
||||
|
||||
pub enum ResizeError {
|
||||
InvalidSize,
|
||||
MremapFailed
|
||||
MremapFailed,
|
||||
}
|
||||
|
||||
impl Pool {
|
||||
|
@ -30,7 +31,7 @@ impl Pool {
|
|||
Ok(Pool {
|
||||
map: RwLock::new(memmap),
|
||||
fd: fd,
|
||||
log: log
|
||||
log: log,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -38,7 +39,7 @@ impl Pool {
|
|||
let mut guard = self.map.write().unwrap();
|
||||
let oldsize = guard.size();
|
||||
if newsize <= 0 || oldsize > (newsize as usize) {
|
||||
return Err(ResizeError::InvalidSize)
|
||||
return Err(ResizeError::InvalidSize);
|
||||
}
|
||||
trace!(self.log, "Resizing shm pool"; "fd" => self.fd as i32, "oldsize" => oldsize, "newsize" => newsize);
|
||||
guard.remap(newsize as usize).map_err(|()| {
|
||||
|
@ -49,8 +50,8 @@ impl Pool {
|
|||
|
||||
pub fn with_data_slice<F: FnOnce(&[u8])>(&self, f: F) -> Result<(), ()> {
|
||||
// Place the sigbus handler
|
||||
SIGBUS_INIT.call_once(|| {
|
||||
unsafe { place_sigbus_handler(); }
|
||||
SIGBUS_INIT.call_once(|| unsafe {
|
||||
place_sigbus_handler();
|
||||
});
|
||||
|
||||
let pool_guard = self.map.read().unwrap();
|
||||
|
@ -87,14 +88,14 @@ impl Pool {
|
|||
impl Drop for Pool {
|
||||
fn drop(&mut self) {
|
||||
trace!(self.log, "Deleting SHM pool"; "fd" => self.fd);
|
||||
let _ = unsafe { unistd::close(self.fd) };
|
||||
let _ = unistd::close(self.fd);
|
||||
}
|
||||
}
|
||||
|
||||
struct MemMap {
|
||||
ptr: *mut u8,
|
||||
fd: RawFd,
|
||||
size: usize
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl MemMap {
|
||||
|
@ -102,13 +103,13 @@ impl MemMap {
|
|||
Ok(MemMap {
|
||||
ptr: unsafe { map(fd, size) }?,
|
||||
fd: fd,
|
||||
size: size
|
||||
size: size,
|
||||
})
|
||||
}
|
||||
|
||||
fn remap(&mut self, newsize: usize) -> Result<(), ()> {
|
||||
if self.ptr.is_null() {
|
||||
return Err(())
|
||||
return Err(());
|
||||
}
|
||||
// memunmap cannot fail, as we are unmapping a pre-existing map
|
||||
let _ = unsafe { unmap(self.ptr, self.size) };
|
||||
|
@ -119,7 +120,7 @@ impl MemMap {
|
|||
self.ptr = ptr;
|
||||
self.size = newsize;
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
Err(()) => {
|
||||
// set ourselves in an empty state
|
||||
self.ptr = ptr::null_mut();
|
||||
|
@ -159,14 +160,12 @@ impl Drop for MemMap {
|
|||
|
||||
// mman::mmap should really be unsafe... why isn't it?
|
||||
unsafe fn map(fd: RawFd, size: usize) -> Result<*mut u8, ()> {
|
||||
let ret = mman::mmap(
|
||||
ptr::null_mut(),
|
||||
let ret = mman::mmap(ptr::null_mut(),
|
||||
size,
|
||||
mman::PROT_READ,
|
||||
mman::MAP_SHARED,
|
||||
fd,
|
||||
0
|
||||
);
|
||||
0);
|
||||
ret.map(|p| p as *mut u8).map_err(|_| ())
|
||||
}
|
||||
|
||||
|
@ -177,31 +176,25 @@ unsafe fn unmap(ptr: *mut u8, size: usize) -> Result<(),()> {
|
|||
}
|
||||
|
||||
unsafe fn nullify_map(ptr: *mut u8, size: usize) -> Result<(), ()> {
|
||||
let ret = mman::mmap(
|
||||
ptr as *mut _,
|
||||
let ret = mman::mmap(ptr as *mut _,
|
||||
size,
|
||||
mman::PROT_READ,
|
||||
mman::MAP_ANONYMOUS | mman::MAP_PRIVATE | mman::MAP_FIXED,
|
||||
-1,
|
||||
0
|
||||
);
|
||||
0);
|
||||
ret.map(|_| ()).map_err(|_| ())
|
||||
}
|
||||
|
||||
unsafe fn place_sigbus_handler() {
|
||||
// create our sigbus handler
|
||||
let action = SigAction::new(
|
||||
SigHandler::SigAction(sigbus_handler),
|
||||
let action = SigAction::new(SigHandler::SigAction(sigbus_handler),
|
||||
signal::SA_NODEFER,
|
||||
signal::SigSet::empty()
|
||||
);
|
||||
signal::SigSet::empty());
|
||||
match signal::sigaction(Signal::SIGBUS, &action) {
|
||||
Ok(old_signal) => {
|
||||
OLD_SIGBUS_HANDLER = Box::into_raw(Box::new(old_signal));
|
||||
},
|
||||
Err(e) => {
|
||||
panic!("sigaction failed sor SIGBUS handler: {:?}", e)
|
||||
}
|
||||
Err(e) => panic!("sigaction failed sor SIGBUS handler: {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -225,7 +218,7 @@ extern "C" fn sigbus_handler(_signum: c_int, info: *mut libc::siginfo_t, _contex
|
|||
// something terrible occured !
|
||||
unsafe { reraise_sigbus() }
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
// something else occured, let's die honorably
|
||||
unsafe { reraise_sigbus() }
|
||||
|
|
Loading…
Reference in New Issue