log_gaze/
log_gaze.rs

1extern crate tobii_sys;
2use tobii_sys::*;
3use std::ptr;
4use std::mem;
5use std::os::raw;
6use std::ffi::{CStr, CString};
7
8use tobii_sys::helpers::{self, PtrWrapper, status_to_result, TobiiError};
9
10unsafe extern "C"
11fn custom_log_fn(_log_context: *mut ::std::os::raw::c_void, level: LogLevel, text: *const raw::c_char) {
12    if level > TOBII_LOG_LEVEL_WARN { return; }
13    let s = CStr::from_ptr(text);
14    println!("LOG {}: {}", level, s.to_str().unwrap());
15}
16
17unsafe extern "C"
18fn gaze_callback(gaze_point: *const GazePoint, _user_data: *mut ::std::os::raw::c_void) {
19    let pt = &*gaze_point;
20    println!("GAZE {}: {}, {}", pt.timestamp_us, pt.position_xy[0], pt.position_xy[1]);
21}
22
23fn run_demo() -> Result<(), TobiiError> {
24    unsafe{
25        let custom_log = CustomLog {
26            log_context: ptr::null_mut(),
27            log_func: Some(custom_log_fn)
28        };
29
30        println!("Initializing API!");
31        let mut api_ptr: *mut Api = mem::zeroed();
32        let status = tobii_api_create( &mut api_ptr as *mut *mut Api, ptr::null_mut(), &custom_log as *const _);
33        status_to_result(status)?;
34        let api = PtrWrapper::new(api_ptr, tobii_api_destroy);
35
36        let devices = helpers::list_devices(api.ptr())?;
37        println!("{:?}", devices);
38
39        if devices.len() < 1 {
40            println!("No devices");
41            return Ok(());
42        }
43
44        let url_c_string = CString::new(devices[0].clone()).unwrap();
45        let url_c = url_c_string.as_c_str();
46        let mut device_ptr: *mut Device = mem::zeroed();
47        let status = tobii_device_create(api.ptr(), url_c.as_ptr(), &mut device_ptr as *mut *mut Device);
48        status_to_result(status)?;
49        let device = PtrWrapper::new(device_ptr, tobii_device_destroy);
50
51        let status = tobii_gaze_point_subscribe(device.ptr(), Some(gaze_callback), ptr::null_mut());
52        let _subscription = PtrWrapper::new(device.ptr(), tobii_gaze_point_unsubscribe);
53        status_to_result(status)?;
54        for _i in 1..1000 {
55            let status = helpers::wait_for_device_callbacks(device.ptr());
56            match status_to_result(status) {
57                Err(TobiiError::TimedOut) => continue,
58                Err(TobiiError::ConnectionFailed) => {
59                    status_to_result(helpers::reconnect(device.ptr()))?;
60                    continue;
61                },
62                Err(e) => return Err(e),
63                Ok(()) => (),
64            }
65
66            let status = tobii_device_process_callbacks(device.ptr());
67            if status == TOBII_ERROR_CONNECTION_FAILED {
68                status_to_result(helpers::reconnect(device.ptr()))?;
69                continue;
70            }
71            status_to_result(status)?;
72        }
73    }
74    Ok(())
75}
76
77fn main() {
78    match run_demo() {
79        Ok(()) => (),
80        Err(e) => {
81            println!("Error: {:?}", e);
82        }
83    }
84}