Move atoms to Rust

This commit is contained in:
Alex Kotov 2022-09-09 19:01:37 +04:00
parent 333b3163d3
commit 41211e3f93
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
7 changed files with 80 additions and 55 deletions

View File

@ -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 \

View 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);
}

View File

@ -1,3 +1,4 @@
mod atoms;
mod constraints;
mod geom;
mod settings;

View 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"),
}
}
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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;