xkb_switch_api/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4
5use std::ffi::{CStr, CString};
6
7include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
8
9pub fn get_layout() -> String {
10    let default = CString::new("").expect("CString::new failed");
11    unsafe {
12        CStr::from_ptr(Xkb_Switch_getXkbLayout((&default).as_ptr()))
13            .to_string_lossy()
14            .into_owned()
15    }
16}
17
18pub fn set_layout(layout: &str) -> Option<&i8> {
19    let newgrp = CString::new(layout).expect("CString::new failed");
20    unsafe { Xkb_Switch_setXkbLayout((&newgrp).as_ptr()).as_ref() }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn get_layout_test() {
29        println!("Current layout: {}", get_layout());
30    }
31
32    #[test]
33    fn set_layout_test() {
34        match set_layout("ru") {
35            Some(_) => println!("Failed"),
36            None => println!("Layout is changed"),
37        }
38    }
39}