Move atoms to Rust
This commit is contained in:
parent
333b3163d3
commit
41211e3f93
7 changed files with 80 additions and 55 deletions
8
Makefile
8
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 \
|
||||
|
|
19
rust-polytreewm/src/api/atoms.rs
Normal file
19
rust-polytreewm/src/api/atoms.rs
Normal file
|
@ -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::<Atoms>();
|
||||
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::<Atoms>();
|
||||
dealloc(atoms as *mut u8, layout);
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
mod atoms;
|
||||
mod constraints;
|
||||
mod geom;
|
||||
mod settings;
|
||||
|
|
37
rust-polytreewm/src/atoms.rs
Normal file
37
rust-polytreewm/src/atoms.rs
Normal file
|
@ -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"),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
47
src/atoms.c
47
src/atoms.c
|
@ -1,47 +0,0 @@
|
|||
#include "atoms.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
21
src/atoms.h
21
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;
|
||||
|
|
Loading…
Reference in a new issue