Browse Source

Start to add bad unfinished Rust shims

Getty Ritter 1 year ago
parent
commit
61d17519ac
5 changed files with 96 additions and 2 deletions
  1. 9 0
      build_rust.sh
  2. 9 2
      meson.build
  3. 10 0
      rust/Cargo.toml
  4. 1 0
      rust/src/lib.rs
  5. 67 0
      rust/src/util.rs

+ 9 - 0
build_rust.sh

@@ -0,0 +1,9 @@
+#!/bin/bash -e
+
+DIR="$(pwd)"
+(
+    cd "$1/rust"
+    cargo build --release
+    echo "Copying to $DIR"
+    cp target/release/libslim.a "$DIR/libslim.a"
+)

+ 9 - 2
meson.build

@@ -37,12 +37,19 @@ slimlock_srcs = 'slimlock.cpp'
 common_srcs = ['cfg.cpp', 'image.cpp', 'log.cpp', 'panel.cpp', 'util.cpp']
 use_pam = true
 
+build_rust = find_program('build_rust.sh')
+rusty = custom_target(
+  'rusty',
+  output: ['libslim.a'],
+  command: [build_rust, meson.current_source_dir()],
+)
+
 cc = meson.get_compiler('cpp')
 deps = [
   cc.find_library('m'),
   cc.find_library('crypt'),
   dependency('freetype2'),
-  dependency('X11'),
+  dependency('x11'),
   dependency('xmu'),
   dependency('xft'),
   dependency('xrandr'),
@@ -81,7 +88,7 @@ endif
 # #Set the custom CMake module directory where our include/lib finders are
 # cmake_module_path = '${CMAKE_SOURCE_DIR}/cmake/modules'
 
-slim_exe = executable('slim', slim_srcs, dependencies: deps, include_directories: [include_directories('.')], link_with: libslim_lib)
+slim_exe = executable('slim', slim_srcs, dependencies: deps, include_directories: [include_directories('.')], link_with: [libslim_lib, rusty])
 
 # # Fontconfig
 # fontconfig_dir = cmake_module_path

+ 10 - 0
rust/Cargo.toml

@@ -0,0 +1,10 @@
+[package]
+name = "slim"
+version = "0.1.0"
+edition = "2018"
+
+[lib]
+crate-type = ["staticlib"]
+
+[dependencies]
+subprocess = "*"

+ 1 - 0
rust/src/lib.rs

@@ -0,0 +1 @@
+pub mod util;

+ 67 - 0
rust/src/util.rs

@@ -0,0 +1,67 @@
+use std::ffi::CStr;
+use std::os::raw::{c_char};
+use std::io::Write;
+
+#[derive(Debug)] 
+enum Error {
+    IOError { error: std::io::Error },
+    StringError { error: std::str::Utf8Error },
+    PopenError { error: subprocess::PopenError },
+    Other { message: String },
+}
+
+impl From<std::str::Utf8Error> for Error {
+    fn from(err: std::str::Utf8Error) -> Error {
+        Error::StringError { error: err }
+    }
+}
+
+impl From<std::io::Error> for Error {
+    fn from(err: std::io::Error) -> Error {
+        Error::IOError { error: err }
+    }
+}
+
+impl From<subprocess::PopenError> for Error {
+    fn from(err: subprocess::PopenError) -> Error {
+        Error::PopenError { error: err }
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn rs_add_mcookie(
+    cookie: *const c_char,
+    display: *const c_char,
+    xauth_cmd: *const c_char,
+    authfile: *const c_char,
+) -> c_char {
+    let result = unsafe { add_mcookie(
+        CStr::from_ptr(cookie),
+        CStr::from_ptr(display),
+        CStr::from_ptr(xauth_cmd),
+        CStr::from_ptr(authfile),
+    ) };
+    match result {
+        Ok(()) => 1,
+        Err(err) => {
+            eprintln!("{:?}", err);
+            0
+        }
+    }
+}
+
+fn add_mcookie(cookie: &CStr, display: &CStr, xauth_cmd: &CStr,authfile: &CStr) -> Result<(), Error> {
+    let cmd = format!("{} -f {} -q", xauth_cmd.to_str()?, authfile.to_str()?);
+    let mut p = subprocess::Popen::create(&[cmd], subprocess::PopenConfig {
+        stdin: subprocess::Redirection::Pipe, ..Default::default()
+    })?;
+    if let Some(stdin) = &mut p.stdin {
+        stdin.write(format!("remove {}\n", display.to_str()?).as_bytes())?;
+        stdin.write(format!("add {} . {}\n", display.to_str()?, cookie.to_str()?).as_bytes())?;
+        stdin.write("exit\n".as_bytes())?;
+    } else {
+        return Err(Error::Other { message: format!("unable to get pipe for writing") });
+    }
+
+    Ok(())
+}