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)",
"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)",
"x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View file

@ -14,8 +14,9 @@ failure = "0.1"
gl = "0.10"
glutin = "0.18"
imlib2 = { version = "0.1", path = "./imlib2" }
leanshot_xlib = { version = "0.1", path = "./xlib" }
nanovg = { version = "1.0.2", features = ["gl3"] }
png = "0.12"
structopt = "0.2"
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 xlib::{Display, Visual, Window};
use xlib::{self, Display, Visual, Window};
use errors::ScreenshotError;
use Options;
@ -116,7 +116,25 @@ impl GUI {
.with_multisampling(4)
.with_srgb(true);
let win = GlWindow::new(wb, ctx, &evl).expect("couldn't make window");
// 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;
unsafe {
win.make_current().expect("couldn't make window");

View file

@ -13,6 +13,7 @@ extern crate png;
extern crate structopt;
extern crate leanshot_xlib as xlib;
extern crate time;
extern crate x11;
mod capture;
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]
extern crate failure;
extern crate libc;
extern crate x11;
pub extern crate x11;
mod atom;
mod cursor;
mod display;
mod drawable;
@ -21,6 +22,7 @@ mod window;
#[allow(missing_docs)]
mod cursorfont;
pub use atom::Atom;
pub use cursor::Cursor;
pub use cursorfont::*;
pub use display::{Display, GetDisplay};

View file

@ -4,7 +4,7 @@ use std::mem;
use libc;
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.
#[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.
pub fn get_attributes(&self) -> Result<WindowAttributes, X11Error> {
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 {