Skip to content

Instantly share code, notes, and snippets.

@tamamu
Created March 20, 2021 14:51
Show Gist options
  • Save tamamu/e22900ea46239cddff150502dafbc91a to your computer and use it in GitHub Desktop.
Save tamamu/e22900ea46239cddff150502dafbc91a to your computer and use it in GitHub Desktop.
use std::ffi::CString;
use std::mem::zeroed;
use std::os::raw::{c_char, c_int, c_uint};
use std::ptr;
use x11::xlib;
fn main() {
let display = unsafe { xlib::XOpenDisplay(CString::default().as_ptr()) };
let mut attr: xlib::XWindowAttributes = unsafe { zeroed() };
let mut start: xlib::XButtonEvent = unsafe { zeroed() };
if display.is_null() {
std::process::exit(1);
}
unsafe {
xlib::XGrabKey(
display,
xlib::XKeysymToKeycode(
display,
xlib::XStringToKeysym(CString::new("F1").unwrap_or_default().as_ptr()),
) as c_int,
xlib::Mod1Mask,
xlib::XDefaultRootWindow(display),
xlib::True,
xlib::GrabModeAsync,
xlib::GrabModeAsync,
);
xlib::XGrabButton(
display,
1,
xlib::Mod1Mask,
xlib::XDefaultRootWindow(display),
xlib::True,
(xlib::ButtonPressMask | xlib::ButtonReleaseMask | xlib::PointerMotionMask) as c_uint,
xlib::GrabModeAsync,
xlib::GrabModeAsync,
0,
0,
);
xlib::XGrabButton(
display,
3,
xlib::Mod1Mask,
xlib::XDefaultRootWindow(display),
xlib::True,
(xlib::ButtonPressMask | xlib::ButtonReleaseMask | xlib::PointerMotionMask) as c_uint,
xlib::GrabModeAsync,
xlib::GrabModeAsync,
0,
0,
);
}
start.subwindow = 0;
let mut event: xlib::XEvent = unsafe { zeroed() };
loop {
unsafe {
xlib::XNextEvent(display, &mut event);
match event.get_type() {
xlib::KeyPress => {
if event.key.subwindow != 0 {
xlib::XRaiseWindow(display, event.key.subwindow);
}
eprintln!("KeyPress");
}
xlib::ButtonPress => {
if event.key.subwindow != 0 {
xlib::XGetWindowAttributes(display, event.key.subwindow, &mut attr);
start = event.button;
}
eprintln!("ButtonPress");
}
xlib::MotionNotify => {
if event.key.subwindow != 0 {
let xdiff = event.button.x_root - start.x_root;
let ydiff = event.button.y_root - start.y_root;
xlib::XMoveResizeWindow(
display,
start.subwindow,
attr.x + if start.button == 1 { xdiff } else { 0 },
attr.y + if start.button == 1 { ydiff } else { 0 },
1.max(attr.width + if start.button == 3 { xdiff } else { 0 }) as u32,
1.max(attr.height + if start.button == 3 { ydiff } else { 0 }) as u32,
);
}
}
xlib::ButtonRelease => {
start.subwindow = 0;
}
xlib::MapRequest => {
// let event: xlib::XCreateWindowEvent = From::from(event);
// let gc = xlib::XCreateGC(display, event.window, 0, ptr::null_mut());
// let white_color = xlib::XWhitePixel(display, xlib::XDefaultScreen(display));
// xlib::XSetForeground(display, gc, white_color);
// xlib::XDrawLine(display, event.window, gc, 10, 60, 180, 20);
eprintln!("MapRequest");
}
xlib::UnmapNotify => {
eprintln!("UnmapNotify");
}
xlib::ClientMessage => {
eprintln!("ClientMessage");
}
xlib::Expose => {
eprintln!("Expose");
}
xlib::FocusIn => {
eprintln!("FocusIn");
}
_ => {}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment