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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! Note: At this time any API call that returns a value needs a callback assigned to return the value to, the 'callback' value will just be the string name of a function.
//!
//! Any API call that returns a value also had an additional string optional parameter, this can be used to share data back to the callback function.

pub mod bindings;
pub mod callbacks;

use super::types::{self, Uid};
use crate::cmd::{sink::submit_cmd, Notification};
use crate::{log, v};
use bindings as b;

pub fn submit(notification: crate::cmd::Notification) {
    submit_cmd(crate::cmd::Command::Notification(notification))
}

/// Spawn a new overlay.
///
// TODO: check accordingly to reference.
// reference: window.SpawnOverlay(JSON.stringify(transform), "ovrtWinSpawned");
pub fn spawn_overlay(
    transform_info: &types::OVROverlayTransform,
) -> Uid {
    let transform_info = serde_json::to_string(transform_info)
        .expect("serialization failed");
    log!("transform_str:", &transform_info);
    submit(Notification::StartSpawnOverlay);
    Uid(unsafe { b::spawn_overlay(transform_info) })
}

/// Spawn a new overlay.
///
// TODO: check accordingly to reference.
// reference: window.SpawnOverlay(JSON.stringify(transform), "ovrtWinSpawned");
pub fn spawn_overlay_with_callback(
    transform_info: &types::OVROverlayTransform,
) -> Uid {
    let transform_info = serde_json::to_string(transform_info)
        .expect("serialization failed");
    log!("transform_str:", &transform_info);
    submit(Notification::StartSpawnOverlay);
    Uid(unsafe {
        b::spawn_overlay_with_callback(
            transform_info,
            "SpawnOverlayOvrtSysCallback".into(),
        )
    })
}

/// Set contents of an overlay.
// TODO: check accordingly to reference.
// reference: window.SetContents(String(uid), Number(winData.type), normalizedContents);
pub fn set_contents_website(
    uid: Uid,
    contents: &types::OVRWebContents,
) {
    let contents = serde_json::to_string(contents)
        .expect("serialization failed");
    log!("contents", v(&contents));
    //
    // TODO: check if a notification should be sent
    //
    unsafe {
        b::set_contents_website(
            uid.0.to_string(),
            crate::consts::WindowType::WebPage as i32,
            contents,
        )
    }
}

/// Set contents of an overlay.
// TODO: check accordingly to reference.
// reference: window.SetContents(String(uid), Number(winData.type), normalizedContents);
pub fn set_contents_desktop(uid: Uid, monitor_id: i32) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe {
        b::set_contents_desktop(
            uid.0.to_string(),
            crate::consts::WindowType::DesktopCapture as i32,
            monitor_id,
        )
    }
}

/// Set contents of an overlay.
// TODO: check accordingly to reference.
// reference: window.SetContents(String(uid), Number(winData.type), normalizedContents);
pub fn set_contents_window(uid: Uid, window_handle: i32) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe {
        b::set_contents_window(
            uid.0.to_string(),
            crate::consts::WindowType::WindowCapture as i32,
            window_handle,
        )
    }
}

/// Returns a list of open windows and their handles.
/// (If user has this option enabled).
///
/// Returns `windowTitles`.
// TODO: check accordingly to reference.
// reference: window.GetWindowTitles("completeIntervalWinTitles");
// window.GetWindowTitles("ovrtWinTitles");
pub fn get_window_titles() -> types::KeyValuePairI32String {
    submit(Notification::StartGetWindowTitles);
    let window_titles = unsafe {
        b::get_window_titles(
            "GetWindowTitlesOvrtSysCallback".into(),
        )
    };
    serde_json::from_str::<types::KeyValuePairI32String>(
        &window_titles,
    )
    .expect("deserialization failed")
}

/// (Used for SetContents monitorId).
///
/// Returns `monitorCount` (how many monitors are connected).
// TODO: check accordingly to reference.
// reference: GetMonitorCount("ovrtMonitorTotal");
// window.GetMonitorCount(callback, data);
pub fn get_monitor_count() -> i32 {
    submit(Notification::StartGetMonitorCount);
    unsafe {
        b::get_monitor_count(
            "GetMonitorCountOvrtSysCallback".into(),
        )
    }
}

/// Refresh a browser page.
// TODO: check accordingly to reference.
// reference: window.Refresh(uid);
pub fn refresh(uid: Uid) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe { b::refresh(uid.0) }
}

/// Get `OVROverlayTransform` of a specified overlay.
///
/// Returns `transformInfo`.
// TODO: check accordingly to reference.
// reference: window.GetOverlayTransform(String(uid), "ovrtWinDetailed");
pub fn get_overlay_transform(
    uid: Uid,
) -> types::OVROverlayTransform {
    submit(Notification::StartGetOverlayTransform(uid.clone()));
    let transform_info =
        unsafe { b::get_overlay_transform(uid.0) };
    serde_json::from_str::<types::OVROverlayTransform>(
        &transform_info,
    )
    .expect("deserialization failed")
}

/// Get `OVROverlayTransform` of a specified overlay.
///
/// Returns `transformInfo`.
// TODO: check accordingly to reference.
// reference: window.GetOverlayTransform(String(uid), "ovrtWinDetailed");
pub fn get_overlay_transform_with_callback(
    uid: Uid,
) -> types::OVROverlayTransform {
    submit(Notification::StartGetOverlayTransform(uid.clone()));
    let transform_info = unsafe {
        b::get_overlay_transform_with_callback(
            uid.0,
            "GetOverlayTransformOvrtSysCallback".into(),
        )
    };
    serde_json::from_str::<types::OVROverlayTransform>(
        &transform_info,
    )
    .expect("deserialization failed")
}

/// Get type of overlay.
/// (Browser, window capture, desktop capture).
///
/// Returns `type`.
// TODO: check accordingly to reference.
// reference: window.GetOverlayType(uid, callback);
pub fn get_overlay_type(uid: Uid) -> i32 {
    submit(Notification::StartGetOverlayType(uid.clone()));
    unsafe {
        b::get_overlay_type(
            uid.0,
            "GetOverlayTypeOvrtSysCallback".into(),
        )
    }
}

/// Get bounds of overlay bounding box.
/// (Refer to Unity documentation 'Bounds' section).
///
/// Returns `bounds`.
// TODO: check accordingly to reference.
// reference: window.GetOverlayBounds(uid, callback);
pub fn get_overlay_bounds(uid: Uid) -> types::OVROverlayBounds {
    submit(Notification::StartGetOverlayBounds(uid.clone()));
    let bounds = unsafe {
        b::get_overlay_bounds(
            uid.0,
            "GetOverlayBoundsOvrtSysCallback".into(),
        )
    };
    serde_json::from_str::<types::OVROverlayBounds>(&bounds)
        .expect("deserialization failed")
}

/// Get finger curl positions.
///
/// Returns `curls`.
/// (Returns 0 for all values if user is in Simulator Mode).
// TODO: check accordingly to reference.
// reference: window.GetFingerCurls("completeFingerCurls");
pub fn get_finger_curls() -> types::OVRFingerCurls {
    submit(Notification::StartGetFingerCurls);
    let curls = unsafe {
        b::get_finger_curls(
            "GetFingerCurlsOvrtSysCallback".into(),
        )
    };
    serde_json::from_str::<types::OVRFingerCurls>(&curls)
        .expect("deserialization failed")
}

/// Set position of an overlay.
// TODO: check accordingly to reference.
// reference: window.SetOverlayPosition(uid, pos.x, pos.y, pos.z);
pub fn set_overlay_position(uid: Uid, pos: types::Pos) {
    //
    // TODO: check if a notification should be sent
    //
    let pos = pos.0;
    unsafe {
        b::set_overlay_position(uid.0, pos.x, pos.y, pos.z)
    }
}

/// Set rotation of an overlay.
/// (EulerAngles).
// TODO: check accordingly to reference.
// reference: window.SetOverlayRotation(uid, rot.x, rot.y, rot.z);
pub fn set_overlay_rotation(uid: Uid, rot: types::Rot) {
    //
    // TODO: check if a notification should be sent
    //
    let rot = rot.0;
    unsafe {
        b::set_overlay_rotation(uid.0, rot.x, rot.y, rot.z)
    }
}

/// Set overlay setting.
// TODO: check accordingly to reference.
// reference: window.SetOverlaySetting(uid, setting, value);
pub fn set_overlay_setting_i32(
    uid: Uid,
    setting: i32,
    new_value: i32,
) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe {
        b::set_overlay_setting_i32(uid.0, setting, new_value)
    }
}

/// Set overlay setting.
pub fn set_overlay_setting_f64(
    uid: Uid,
    setting: i32,
    new_value: f64,
) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe {
        b::set_overlay_setting_f64(uid.0, setting, new_value)
    }
}

/// Set overlay setting.
pub fn set_overlay_setting_bool(
    uid: Uid,
    setting: i32,
    new_value: bool,
) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe {
        b::set_overlay_setting_bool(uid.0, setting, new_value)
    }
}

/// Close the specified overlay.
// TODO: check accordingly to reference.
// reference: window.CloseOverlay(uid);
pub fn close_overlay(uid: Uid) {
    submit(Notification::StartCloseOverlay(uid.clone()));

    // https://github.com/swfsql/ovrt-sys/issues/6
    // unsafe { b::close_overlay(uid.0) }

    // https://github.com/swfsql/ovrt-sys/issues/6
    unsafe { b::close_overlay_str(uid.0.to_string()) }
}

/// Send device position/rotation data to the calling overlay.
/// (Refer to 'Events' below).
// TODO: check accordingly to reference.
// reference: window.SendDeviceData(enable);
pub fn send_device_data(enabled: bool) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe { b::send_device_data(enabled) }
}

/// Send overlay position/rotation data to the calling overlay.
/// (Refer to 'Events' below).
// TODO: check accordingly to reference.
// reference: window.SendOverlayPositions(enable);
pub fn send_overlay_positions(enabled: bool) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe { b::send_overlay_positions(enabled) }
}

/// Send message all other open browser instances.
/// (Calls 'ReceiveMessage').
// TODO: check accordingly to reference.
// reference: window.BroadcastMessage(JSON.stringify({broadcast: true,event: event,data: data,}));
// window.BroadcastMessage(JSON.stringify({broadcast: false,event: event,data: data,senderId: senderId,targetId: targetId,}));
//
// check send_message below
pub fn broadcast_message(message: String) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe { b::broadcast_message(message) }
}

/// Send message to specific browser instance.
/// (Calls 'ReceiveMessage').
// TODO: check accordingly to reference.
// reference: none
// TODO: actually, it appears that this was merged into broadcast_message
// but now it has its own api call
pub fn send_message(uid: Uid, message: String) {
    unsafe { b::send_message(uid.0, message) }
}

/// Set if this overlay should receive keyboard inputs.
/// (This will block the keyboard working on OVR Toolkit windows!).
// TODO: check accordingly to reference.
// reference: none
pub fn set_keyboard_focus(enabled: bool) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe { b::set_keyboard_focus(enabled) }
}

/// Sets the title of the browser that is visible in the Window List of OVR Toolkit.
// TODO: check accordingly to reference.
// reference: none
pub fn set_browser_title(title: String) {
    //
    // TODO: check if a notification should be sent
    //
    unsafe { b::set_browser_title(title) }
}