Move XBase to Rust

This commit is contained in:
Alex Kotov 2022-09-09 19:20:30 +04:00
parent 392c60dc9b
commit 1a356b10c5
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 27 additions and 75 deletions

View file

@ -24,7 +24,8 @@ RUST_APIS = \
src/constraints.h \ src/constraints.h \
src/geom.h \ src/geom.h \
src/helpers.h \ src/helpers.h \
src/settings.h src/settings.h \
src/xbase.h
MODULES_SRC = \ MODULES_SRC = \
src/drw.c \ src/drw.c \
@ -35,8 +36,7 @@ MODULES_SRC = \
src/spawn.c \ src/spawn.c \
src/state.c \ src/state.c \
src/unit.c \ src/unit.c \
src/util.c \ src/util.c
src/xbase.c
DWM_SRC = \ DWM_SRC = \
src/dwm/bar.c \ src/dwm/bar.c \

View file

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

View file

@ -0,0 +1,23 @@
use crate::*;
use std::alloc::{alloc, dealloc, Layout};
use std::ffi::CStr;
use std::os::raw::*;
#[no_mangle]
unsafe extern "C" fn xbase_new(
program_title: *const c_char,
x_error_handler: xbase::ErrorHandler,
) -> *mut Xbase {
let program_title = CStr::from_ptr(program_title).to_str().unwrap();
let layout = Layout::new::<Xbase>();
let ptr = alloc(layout) as *mut Xbase;
*ptr = Xbase::new(program_title.to_string(), x_error_handler).unwrap();
ptr
}
#[no_mangle]
unsafe extern "C" fn xbase_delete(xbase: *mut Xbase) {
let layout = Layout::new::<Xbase>();
dealloc(xbase as *mut u8, layout);
}

View file

@ -1,72 +0,0 @@
#include "xbase.h"
#include "logger.h"
#include <stdlib.h>
#include <string.h>
/* Startup Error handler to check if another
* window manager is already running. */
static int x_error_wm_check(Display*, XErrorEvent*);
Xbase xbase_new(
const char *const program_title,
const XErrorHandler x_error_handler
) {
if (!XSupportsLocale()) warning("no locale support in X");
Xbase xbase = malloc(sizeof(struct Xbase));
if (!xbase) fatal_perror("cannot allocate xbase");
if (!(xbase->program_title = strdup(program_title))) {
fatal_perror("no program title is given");
}
if (!(xbase->x_display = XOpenDisplay(NULL))) {
fatal("cannot open X display");
}
xbase->x_screen = DefaultScreen(xbase->x_display);
xbase->x_root = RootWindow(xbase->x_display, xbase->x_screen);
xbase->screen_sizes = sizes_create_from_args(
DisplayWidth(xbase->x_display, xbase->x_screen),
DisplayHeight(xbase->x_display, xbase->x_screen)
);
if (!(xbase->x_error = XSetErrorHandler(x_error_wm_check))) {
fatal("no X error handler is given");
}
// This causes an error if some other window manager is running
XSelectInput(
xbase->x_display,
DefaultRootWindow(xbase->x_display),
SubstructureRedirectMask
);
XSync(xbase->x_display, False);
XSetErrorHandler(x_error_handler);
XSync(xbase->x_display, False);
if (!(xbase->atoms = atoms_create(xbase->x_display))) {
fatal("cannot create atoms");
}
return xbase;
}
void xbase_delete(const Xbase xbase)
{
// TODO: maybe we should assert here
if (xbase == NULL) return;
ATOMS_DELETE(xbase->atoms);
XCloseDisplay(xbase->x_display);
free(xbase);
}
int x_error_wm_check(
__attribute__((unused)) Display *const x_display,
__attribute__((unused)) XErrorEvent *const x_error_event
) {
fatal("another window manager is already running");
}