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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#![allow(non_camel_case_types, non_snake_case)]

//! FFI bindings and loader for the Logitech LCD SDK
//!
//! [LogitechLcd](struct.LogitechLcd.html) will try to locate and load
//! `LogitechLcd.dll` at Runtime for dynamic linking.
//!

#[macro_use]
extern crate bitflags;

use std::os::raw::{c_int, c_uint};

/// Monochrome screen width in pixels.
pub const MONO_WIDTH:  usize = 160;

/// Monochrome screen hight in pixels.
pub const MONO_HEIGHT: usize = 43;

/// Color screen witdh in pixels.
pub const COLOR_WIDTH:  usize = 320;

/// Color screen hight in pixels.
pub const COLOR_HEIGHT: usize = 240;

bitflags! {
    /// Targeted lcd type.
    ///
    /// This library allows you to target either Mono or Color devices
    pub struct LcdType: u32 {
        /// Mono color.
        const MONO =  0x00000001;
        /// 32bit RGBA color.
        const COLOR = 0b00000010;
        /// Either mono or color.
        const EITHER = Self::MONO.bits | Self::COLOR.bits;
    }
}

bitflags! {
    /// Lcd Button bitmap.
    pub struct LcdButton: u32 {
        const MONO_BUTTON_0 = 0x00000001;
        const MONO_BUTTON_1 = 0x00000002;
        const MONO_BUTTON_2 = 0x00000004;
        const MONO_BUTTON_3 = 0x00000008;
        const MONO_BUTTON = Self::MONO_BUTTON_0.bits |
                            Self::MONO_BUTTON_1.bits |
                            Self::MONO_BUTTON_2.bits |
                            Self::MONO_BUTTON_3.bits;

        const COLOR_BUTTON_LEFT   = 0x00000100;
        const COLOR_BUTTON_RIGHT  = 0x00000200;
        const COLOR_BUTTON_OK     = 0x00000400;
        const COLOR_BUTTON_CANCEL = 0x00000800;
        const COLOR_BUTTON_UP     = 0x00001000;
        const COLOR_BUTTON_DOWN   = 0x00002000;
        const COLOR_BUTTON_MENU   = 0x00004000;
        const COLOR_BUTTON = Self::COLOR_BUTTON_LEFT.bits |
                             Self::COLOR_BUTTON_RIGHT.bits |
                             Self::COLOR_BUTTON_OK.bits |
                             Self::COLOR_BUTTON_CANCEL.bits |
                             Self::COLOR_BUTTON_UP.bits |
                             Self::COLOR_BUTTON_DOWN.bits |
                             Self::COLOR_BUTTON_MENU.bits;
    }
}

/// LogitechLcd library.
///
/// Contains library symbols/functions as fields. Will unload library when dropped.
pub struct LogitechLcd {
    // Main functions
    pub LogiLcdInit: unsafe extern "C" fn(friendlyName: *const u16, lcdType: c_uint) -> bool,
    pub LogiLcdIsConnected: unsafe extern "C" fn(lcdType: c_uint) -> bool,
    pub LogiLcdIsButtonPressed: unsafe extern "C" fn(button: c_uint) -> bool,
    pub LogiLcdUpdate: unsafe extern "C" fn(),
    pub LogiLcdShutdown: unsafe extern "C" fn(),

    // Monochrome LCD functions
    pub LogiLcdMonoSetBackground: unsafe extern "C" fn(monoBitmap: *const u8) -> bool,
    pub LogiLcdMonoSetText: unsafe extern "C" fn(lineNumber: c_int, text: *const u16) -> bool,

    // Color LCD functions
    pub LogiLcdColorSetBackground: unsafe extern "C" fn(colorBitmap: *const u8) -> bool,
    pub LogiLcdColorSetTitle: unsafe extern "C" fn(text: *const u16, red: c_int, green: c_int,
        blue: c_int) -> bool,
    pub LogiLcdColorSetText: unsafe extern "C" fn(lineNumber: c_int, text: *const u16, red: c_int,
        green: c_int, blue: c_int) -> bool,

    // UDK functions, use this only if working with UDK
    pub LogiLcdColorSetBackgroundUDK: unsafe extern "C" fn(partialBitmap: *const u8,
        arraySize: c_int) -> c_int,
    pub LogiLcdColorResetBackgroundUDK: unsafe extern "C" fn() -> c_int,
    pub LogiLcdMonoSetBackgroundUDK: unsafe extern "C" fn(partialBitmap: *const u8,
        arraySize: c_int) -> c_int,
    pub LogiLcdMonoResetBackgroundUDK: unsafe extern "C" fn() -> c_int,

    /// Library handle, will be freed on drop
    _library: platform::Library,
}

unsafe impl std::marker::Send for LogitechLcd {}

#[cfg(not(target_os = "windows"))]
mod platform {
    use super::LogitechLcd;
    use std::io::Error;

    pub struct Library;

    impl LogitechLcd {
        pub fn load() -> Result<LogitechLcd, Error> {
            unimplemented!();
        }
    }
}

#[cfg(target_os = "windows")]
mod platform {
    extern crate winapi;
    extern crate kernel32;
    extern crate winreg;

    use super::LogitechLcd;

    use self::winreg::RegKey;
    use self::winreg::enums::{HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT, KEY_READ};
    use self::winapi::minwindef::{HMODULE, FARPROC};

    use std::os::windows::ffi::OsStrExt;
    use std::ffi::OsStr;
    use std::io::Error;

    pub struct Library(HMODULE);

    const ERROR_MOD_NOT_FOUND: i32 = winapi::winerror::ERROR_MOD_NOT_FOUND as i32;

    /// Find `LogitechLcd.dll` in Windows registry using its CLSID
    fn dll_path_clsid() -> Result<Vec<u16>, Error> {
        let hkcl = RegKey::predef(HKEY_CLASSES_ROOT);
        let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);

        let mut dll_path = None;

        #[cfg(target_arch = "x86_64")]
        {
            match hkcl.open_subkey_with_flags(
                "CLSID\\{d0e790a5-01a7-49ae-ae0b-e986bdd0c21b}\\ServerBinary", KEY_READ)
            {
                Ok(key) => dll_path = key.get_value::<String, &str>("").ok(),
                Err(_) => {},
            }

            match hklm.open_subkey_with_flags(
                "SOFTWARE\\Classes\\CLSID\\{d0e790a5-01a7-49ae-ae0b-e986bdd0c21b}\\ServerBinary",
                KEY_READ)
            {
                Ok(key) => dll_path = key.get_value::<String, &str>("").ok(),
                Err(_) => {},
            }
        }

        #[cfg(target_arch = "x86")]
        {
            match hkcl.open_subkey_with_flags(
                "Wow6432Node\\CLSID\\{d0e790a5-01a7-49ae-ae0b-e986bdd0c21b}\\ServerBinary", KEY_READ)
            {
                Ok(key) => dll_path = key.get_value::<String, &str>("").ok(),
                Err(_) => {},
            }

            match hklm.open_subkey_with_flags(
                "SOFTWARE\\Classes\\Wow6432Node\\CLSID\\{d0e790a5-01a7-49ae-ae0b-e986bdd0c21b}\\ServerBinary",
                KEY_READ)
            {
                Ok(key) => dll_path = key.get_value::<String, &str>("").ok(),
                Err(_) => {},
            }

            match hklm.open_subkey_with_flags(
                "SOFTWARE\\Wow6432Node\\Classes\\CLSID\\{d0e790a5-01a7-49ae-ae0b-e986bdd0c21b}\\ServerBinary",
                KEY_READ)
            {
                Ok(key) => dll_path = key.get_value::<String, &str>("").ok(),
                Err(_) => {},
            }
        }

        match dll_path {
            // Convert to widestring and terminate with \0\0.
            Some(p) => Ok(OsStr::new(&p[..]).encode_wide().chain(Some(0)).collect::<Vec<u16>>()),
            None => Err(Error::from_raw_os_error(ERROR_MOD_NOT_FOUND)),
        }
    }

    unsafe fn load_lib() -> Result<HMODULE, Error> {
        match dll_path_clsid() {
            Ok(wide_path) => {
                let handle = kernel32::LoadLibraryW(wide_path.as_ptr());
                if handle.is_null() {
                    let error = Error::last_os_error();
                    let ecode = error.raw_os_error().unwrap();
                    // Fallthrough on ERROR_MOD_NOT_FOUND
                    if ecode != ERROR_MOD_NOT_FOUND {
                        return Err(error);
                    }
                } else {
                    return Ok(handle);
                }
            },
            Err(e) => {
                match e.raw_os_error() {
                    Some(ERROR_MOD_NOT_FOUND) => {},
                    _ => return Err(e),
                }
            },
        }

        // Convert to widestring and terminate with \0\0.
        let wide_name = OsStr::new("LogitechLcd.dll").encode_wide().chain(Some(0)).collect::<Vec<u16>>();
        let handle = kernel32::LoadLibraryW(wide_name.as_ptr());
        if handle.is_null() {
            Err(Error::last_os_error())
        } else {
            Ok(handle)
        }
    }

    impl LogitechLcd {
        /// Try to locate and load 'LogitechLcd.dll'.
        pub fn load() -> Result<LogitechLcd, Error> {
            use std::mem;

            unsafe {
                let handle = load_lib()?;

                let mut symbols = [
                    ("LogiLcdInit\0",                    0 as FARPROC),
                    ("LogiLcdIsConnected\0",             0 as FARPROC),
                    ("LogiLcdIsButtonPressed\0",         0 as FARPROC),
                    ("LogiLcdUpdate\0",                  0 as FARPROC),
                    ("LogiLcdShutdown\0",                0 as FARPROC),
                    ("LogiLcdMonoSetBackground\0",       0 as FARPROC),
                    ("LogiLcdMonoSetText\0",             0 as FARPROC),
                    ("LogiLcdColorSetBackground\0",      0 as FARPROC),
                    ("LogiLcdColorSetTitle\0",           0 as FARPROC),
                    ("LogiLcdColorSetText\0",            0 as FARPROC),
                    ("LogiLcdColorSetBackgroundUDK\0",   0 as FARPROC),
                    ("LogiLcdColorResetBackgroundUDK\0", 0 as FARPROC),
                    ("LogiLcdMonoSetBackgroundUDK\0",    0 as FARPROC),
                    ("LogiLcdMonoResetBackgroundUDK\0",  0 as FARPROC),
                ];

                for i in symbols.iter_mut() {
                    i.1 = kernel32::GetProcAddress(handle, i.0.as_ptr() as *const i8);
                    if i.1.is_null() {
                        let error = Error::last_os_error();
                        kernel32::FreeLibrary(handle);
                        return Err(error);
                    }
                }

                Ok(LogitechLcd {
                    LogiLcdInit:                    mem::transmute(symbols[0].1),
                    LogiLcdIsConnected:             mem::transmute(symbols[1].1),
                    LogiLcdIsButtonPressed:         mem::transmute(symbols[2].1),
                    LogiLcdUpdate:                  mem::transmute(symbols[3].1),
                    LogiLcdShutdown:                mem::transmute(symbols[4].1),
                    LogiLcdMonoSetBackground:       mem::transmute(symbols[5].1),
                    LogiLcdMonoSetText:             mem::transmute(symbols[6].1),
                    LogiLcdColorSetBackground:      mem::transmute(symbols[7].1),
                    LogiLcdColorSetTitle:           mem::transmute(symbols[8].1),
                    LogiLcdColorSetText:            mem::transmute(symbols[9].1),
                    LogiLcdColorSetBackgroundUDK:   mem::transmute(symbols[10].1),
                    LogiLcdColorResetBackgroundUDK: mem::transmute(symbols[11].1),
                    LogiLcdMonoSetBackgroundUDK:    mem::transmute(symbols[12].1),
                    LogiLcdMonoResetBackgroundUDK:  mem::transmute(symbols[13].1),
                    _library: Library(handle),
                })
            }
        }
    }

    impl Drop for Library {
        fn drop(&mut self) {
            unsafe {
                kernel32::FreeLibrary(self.0);
            }
        }
    }
}