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-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 = \ MODULES_SRC = \
src/atoms.c \
src/drw.c \ src/drw.c \
src/dwm.c \ src/dwm.c \
src/geom.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 constraints;
mod geom; mod geom;
mod settings; 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 api;
mod atoms;
mod constraints; mod constraints;
pub mod geom; pub mod geom;
pub mod settings; pub mod settings;
pub mod unit; pub mod unit;
pub use atoms::Atoms;
pub use settings::Settings; 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 */ /* EWMH atoms */
enum { #define NetSupported 0
NetSupported, NetWMName, NetWMState, NetWMCheck, #define NetWMName 1
NetWMFullscreen, NetActiveWindow, NetWMWindowType, NetWMWindowTypeDialog, #define NetWMState 2
NetClientList, NetLast, #define NetWMCheck 3
}; #define NetWMFullscreen 4
#define NetActiveWindow 5
#define NetWMWindowType 6
#define NetWMWindowTypeDialog 7
#define NetClientList 8
#define NetLast 9
/* default atoms */ /* 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 { typedef struct Atoms {
Atom wmatom[WMLast], netatom[NetLast], utf8string; Atom wmatom[WMLast], netatom[NetLast], utf8string;