diff --git a/Makefile b/Makefile index a57cacb..bba31e4 100644 --- a/Makefile +++ b/Makefile @@ -19,10 +19,14 @@ RUST_SRC = \ rust-polytreewm/src/*.rs \ rust-polytreewm/src/**/*.rs -RUST_APIS = src/constraints.h src/geom.h src/helpers.h src/settings.h +RUST_APIS = \ + src/atoms.h \ + src/constraints.h \ + src/geom.h \ + src/helpers.h \ + src/settings.h MODULES_SRC = \ - src/atoms.c \ src/drw.c \ src/dwm.c \ src/geom.c \ diff --git a/rust-polytreewm/src/api/atoms.rs b/rust-polytreewm/src/api/atoms.rs new file mode 100644 index 0000000..ea55613 --- /dev/null +++ b/rust-polytreewm/src/api/atoms.rs @@ -0,0 +1,19 @@ +use crate::*; + +use std::alloc::{alloc, dealloc, Layout}; + +use x11::xlib::Display; + +#[no_mangle] +unsafe extern "C" fn atoms_create(dpy: *mut Display) -> *mut Atoms { + let layout = Layout::new::(); + let ptr = alloc(layout); + *(ptr as *mut Atoms) = Atoms::create(dpy); + ptr as *mut Atoms +} + +#[no_mangle] +unsafe extern "C" fn atoms_delete(atoms: *mut Atoms) { + let layout = Layout::new::(); + dealloc(atoms as *mut u8, layout); +} diff --git a/rust-polytreewm/src/api/mod.rs b/rust-polytreewm/src/api/mod.rs index 2bef5a4..204bb83 100644 --- a/rust-polytreewm/src/api/mod.rs +++ b/rust-polytreewm/src/api/mod.rs @@ -1,3 +1,4 @@ +mod atoms; mod constraints; mod geom; mod settings; diff --git a/rust-polytreewm/src/atoms.rs b/rust-polytreewm/src/atoms.rs new file mode 100644 index 0000000..411ed62 --- /dev/null +++ b/rust-polytreewm/src/atoms.rs @@ -0,0 +1,37 @@ +use x11::xlib::{self, Atom, Display, False}; + +#[repr(C)] +pub struct Atoms { + wmatom: [Atom; 4], + netatom: [Atom; 9], + utf8string: Atom, +} + +fn atom(dpy: *mut Display, name: &str) -> Atom { + unsafe { xlib::XInternAtom(dpy, name.as_ptr() as *const i8, False) } +} + +impl Atoms { + pub fn create(dpy: *mut Display) -> Self { + Self { + wmatom: [ + atom(dpy, "WM_PROTOCOLS"), + atom(dpy, "WM_DELETE_WINDOW"), + atom(dpy, "WM_STATE"), + atom(dpy, "WM_TAKE_FOCUS"), + ], + netatom: [ + atom(dpy, "_NET_SUPPORTED"), + atom(dpy, "_NET_WM_NAME"), + atom(dpy, "_NET_WM_STATE"), + atom(dpy, "_NET_SUPPORTING_WM_CHECK"), + atom(dpy, "_NET_WM_STATE_FULLSCREEN"), + atom(dpy, "_NET_ACTIVE_WINDOW"), + atom(dpy, "_NET_WM_WINDOW_TYPE"), + atom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG"), + atom(dpy, " _NET_CLIENT_LIST"), + ], + utf8string: atom(dpy, "UTF8_STRING"), + } + } +} diff --git a/rust-polytreewm/src/lib.rs b/rust-polytreewm/src/lib.rs index 62f0b75..39948a3 100644 --- a/rust-polytreewm/src/lib.rs +++ b/rust-polytreewm/src/lib.rs @@ -1,8 +1,10 @@ mod api; +mod atoms; mod constraints; pub mod geom; pub mod settings; pub mod unit; +pub use atoms::Atoms; pub use settings::Settings; diff --git a/src/atoms.c b/src/atoms.c deleted file mode 100644 index f4c3f1b..0000000 --- a/src/atoms.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "atoms.h" - -#include - -static const char *const default_atoms[WMLast] = { - [WMProtocols] = "WM_PROTOCOLS", - [WMDelete] = "WM_DELETE_WINDOW", - [WMState] = "WM_STATE", - [WMTakeFocus] = "WM_TAKE_FOCUS", -}; - -static const char *const ewmh_atoms[NetLast] = { - [NetActiveWindow] = "_NET_ACTIVE_WINDOW", - [NetSupported] = "_NET_SUPPORTED", - [NetWMName] = "_NET_WM_NAME", - [NetWMState] = "_NET_WM_STATE", - [NetWMCheck] = "_NET_SUPPORTING_WM_CHECK", - [NetWMFullscreen] = "_NET_WM_STATE_FULLSCREEN", - [NetWMWindowType] = "_NET_WM_WINDOW_TYPE", - [NetWMWindowTypeDialog] = "_NET_WM_WINDOW_TYPE_DIALOG", - [NetClientList] = "_NET_CLIENT_LIST", -}; - -Atoms atoms_create(Display *const dpy) -{ - Atoms atoms = malloc(sizeof(struct Atoms)); - - for (int index = 0; index < WMLast; ++index) { - atoms->wmatom[index] = XInternAtom(dpy, default_atoms[index], False); - } - - for (int index = 0; index < NetLast; ++index) { - atoms->netatom[index] = XInternAtom(dpy, ewmh_atoms[index], False); - } - - atoms->utf8string = XInternAtom(dpy, "UTF8_STRING", False); - - return atoms; -} - -void atoms_delete(Atoms atoms) -{ - // TODO: maybe we should assert - if (atoms == NULL) return; - - free(atoms); -} diff --git a/src/atoms.h b/src/atoms.h index 316b60d..513277b 100644 --- a/src/atoms.h +++ b/src/atoms.h @@ -10,14 +10,23 @@ } /* EWMH atoms */ -enum { - NetSupported, NetWMName, NetWMState, NetWMCheck, - NetWMFullscreen, NetActiveWindow, NetWMWindowType, NetWMWindowTypeDialog, - NetClientList, NetLast, -}; +#define NetSupported 0 +#define NetWMName 1 +#define NetWMState 2 +#define NetWMCheck 3 +#define NetWMFullscreen 4 +#define NetActiveWindow 5 +#define NetWMWindowType 6 +#define NetWMWindowTypeDialog 7 +#define NetClientList 8 +#define NetLast 9 /* default atoms */ -enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; +#define WMProtocols 0 +#define WMDelete 1 +#define WMState 2 +#define WMTakeFocus 3 +#define WMLast 4 typedef struct Atoms { Atom wmatom[WMLast], netatom[NetLast], utf8string;