open a splash window instead

This commit is contained in:
Michael Zhang 2018-09-30 15:43:50 -05:00
parent eb4ed18c49
commit cffe227be9
No known key found for this signature in database
GPG key ID: A1B65B603268116B
7 changed files with 92 additions and 4 deletions

1
Cargo.lock generated
View file

@ -378,6 +378,7 @@ dependencies = [
"png 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "png 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]

View file

@ -14,8 +14,9 @@ failure = "0.1"
gl = "0.10" gl = "0.10"
glutin = "0.18" glutin = "0.18"
imlib2 = { version = "0.1", path = "./imlib2" } imlib2 = { version = "0.1", path = "./imlib2" }
leanshot_xlib = { version = "0.1", path = "./xlib" }
nanovg = { version = "1.0.2", features = ["gl3"] } nanovg = { version = "1.0.2", features = ["gl3"] }
png = "0.12" png = "0.12"
structopt = "0.2" structopt = "0.2"
time = "0.1" time = "0.1"
leanshot_xlib = { version = "0.1", path = "./xlib" } x11 = { version = "2.18", features = ["xlib"] }

View file

@ -1,5 +1,5 @@
use imlib2::{self, Image as Image2}; use imlib2::{self, Image as Image2};
use xlib::{Display, Visual, Window}; use xlib::{self, Display, Visual, Window};
use errors::ScreenshotError; use errors::ScreenshotError;
use Options; use Options;
@ -116,7 +116,25 @@ impl GUI {
.with_multisampling(4) .with_multisampling(4)
.with_srgb(true); .with_srgb(true);
let win = GlWindow::new(wb, ctx, &evl).expect("couldn't make window"); let win = GlWindow::new(wb, ctx, &evl).expect("couldn't make window");
// crosshair
win.set_cursor(MouseCursor::Crosshair); win.set_cursor(MouseCursor::Crosshair);
// change window type
{
use glutin::os::unix::WindowExt;
use xlib::Atom;
match win.get_xlib_window() {
Some(id) => {
let w = Window::create_from_handle(&self.display, id)?;
let key = Atom::new(&self.display, "_NET_WM_WINDOW_TYPE", false)?;
let val = Atom::new(&self.display, "_NET_WM_WINDOW_TYPE_SPLASH", false)?;
w.change_property(&key, &val);
}
_ => (),
}
}
let f = win.get_hidpi_factor() as f64; let f = win.get_hidpi_factor() as f64;
unsafe { unsafe {
win.make_current().expect("couldn't make window"); win.make_current().expect("couldn't make window");

View file

@ -13,6 +13,7 @@ extern crate png;
extern crate structopt; extern crate structopt;
extern crate leanshot_xlib as xlib; extern crate leanshot_xlib as xlib;
extern crate time; extern crate time;
extern crate x11;
mod capture; mod capture;
mod errors; mod errors;

38
xlib/src/atom.rs Normal file
View file

@ -0,0 +1,38 @@
use std::ffi::CString;
use x11::xlib as x;
use Display;
use X11Error;
/// A unique string or intger
pub struct Atom {
inner: x::Atom,
}
impl Atom {
/// Create a new atom using a string
pub fn new(
display: &Display,
val: impl AsRef<str>,
only_if_exists: bool,
) -> Result<Self, X11Error> {
let val = {
let v = val.as_ref();
let s = CString::new(v).unwrap();
s.as_ptr()
};
let inner =
unsafe { x::XInternAtom(display.as_raw(), val, if only_if_exists { 1 } else { 0 }) };
Ok(Atom { inner })
}
/// Create a new Atom object from an existing handle
pub fn from(handle: x::Atom) -> Self {
Atom { inner: handle }
}
/// Get the handle
pub fn as_raw(&self) -> x::Atom {
self.inner
}
}

View file

@ -5,8 +5,9 @@
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
extern crate libc; extern crate libc;
extern crate x11; pub extern crate x11;
mod atom;
mod cursor; mod cursor;
mod display; mod display;
mod drawable; mod drawable;
@ -21,6 +22,7 @@ mod window;
#[allow(missing_docs)] #[allow(missing_docs)]
mod cursorfont; mod cursorfont;
pub use atom::Atom;
pub use cursor::Cursor; pub use cursor::Cursor;
pub use cursorfont::*; pub use cursorfont::*;
pub use display::{Display, GetDisplay}; pub use display::{Display, GetDisplay};

View file

@ -4,7 +4,7 @@ use std::mem;
use libc; use libc;
use x11::xlib as x; use x11::xlib as x;
use {Display, Drawable, GetDisplay, Image, Rectangle, X11Error}; use {Atom, Display, Drawable, GetDisplay, Image, Rectangle, X11Error};
/// A wrapper around a window handle. /// A wrapper around a window handle.
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
@ -53,6 +53,14 @@ impl Window {
}) })
} }
/// Create a new Window instance from an existing ID
pub fn create_from_handle(display: &Display, id: u64) -> Result<Window, X11Error> {
Ok(Window {
display: display.as_raw(),
inner: id,
})
}
/// Get window attributes. /// Get window attributes.
pub fn get_attributes(&self) -> Result<WindowAttributes, X11Error> { pub fn get_attributes(&self) -> Result<WindowAttributes, X11Error> {
let attr = unsafe { let attr = unsafe {
@ -86,6 +94,25 @@ impl Window {
), ),
) )
} }
/// Change window property
// TODO: make it more general
pub fn change_property(&self, key: &Atom, val: &Atom) {
use std::mem::transmute;
let v = val.as_raw();
unsafe {
x::XChangeProperty(
self.display,
self.inner,
key.as_raw(),
x::XA_ATOM,
32,
x::PropModeReplace,
transmute(&v),
1,
);
}
}
} }
impl GetDisplay for Window { impl GetDisplay for Window {