1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*! Locale-specific functions. 
 * 
 * This file is intended as a library:
 * it must pass errors upwards
 * and panicking is allowed only when
 * this code encounters an internal inconsistency.
 */

use std::cmp;
use std::ffi::{ CStr, CString };
use std::fmt;
use std::os::raw::c_char;
use std::ptr;
use std::str::Utf8Error;

mod c {
    use super::*;
    use std::os::raw::c_void;

    #[allow(non_camel_case_types)]
    pub type c_int = i32;

    #[derive(Clone, Copy)]
    #[repr(C)]
    pub struct GnomeXkbInfo(*const c_void);

    extern "C" {
        // from libc
        pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
        // from gnome-desktop3
        pub fn gnome_xkb_info_new() -> GnomeXkbInfo;
        pub fn gnome_xkb_info_get_layout_info (
            info: GnomeXkbInfo,
            id: *const c_char,
            display_name: *mut *const c_char,
            short_name: *const *const c_char,
            xkb_layout: *const *const c_char,
            xkb_variant: *const *const c_char
        ) -> c_int;
        pub fn g_object_unref(o: GnomeXkbInfo);
    }
}

#[derive(Debug)]
pub enum Error {
    StringConversion(Utf8Error),
    NoInfo,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self, f)
    }
}

pub struct XkbInfo(c::GnomeXkbInfo);

impl XkbInfo {
    pub fn new() -> XkbInfo {
        XkbInfo(unsafe { c::gnome_xkb_info_new() })
    }
    pub fn get_display_name(&self, id: &str) -> Result<String, Error> {
        let id = cstring_safe(id);
        let id_ref = id.as_ptr();
        let mut display_name: *const c_char = ptr::null();
        let found = unsafe {
            c::gnome_xkb_info_get_layout_info(
                self.0,
                id_ref,
                &mut display_name as *mut *const c_char,
                ptr::null(), ptr::null(), ptr::null(),
            )
        };
        if found != 0 && !display_name.is_null() {
            let display_name = unsafe { CStr::from_ptr(display_name) };
            display_name.to_str()
                .map(str::to_string)
                .map_err(Error::StringConversion)
        } else {
            Err(Error::NoInfo)
        }
    }
}

impl Drop for XkbInfo {
    fn drop(&mut self) {
        unsafe { c::g_object_unref(self.0) }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct OwnedTranslation(pub String);

fn cstring_safe(s: &str) -> CString {
    CString::new(s)
        .unwrap_or(CString::new("").unwrap())
}

pub fn compare_current_locale(a: &str, b: &str) -> cmp::Ordering {
    let a = cstring_safe(a);
    let b = cstring_safe(b);
    let a = a.as_ptr();
    let b = b.as_ptr();
    let result = unsafe { c::strcoll(a, b) };
    if result == 0 {
        cmp::Ordering::Equal
    } else if result > 0 {
        cmp::Ordering::Greater
    } else if result < 0 {
        cmp::Ordering::Less
    } else {
        unreachable!()
    }
}