Skip to main content

nokhwa/
init.rs

1/*
2 * Copyright 2022 l1npengtul <l1npengtul@protonmail.com> / The Nokhwa Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#[cfg(not(all(
18    feature = "input-avfoundation",
19    any(target_os = "macos", target_os = "ios")
20)))]
21fn init_avfoundation(callback: impl Fn(bool) + Send + 'static) {
22    callback(true);
23}
24
25#[cfg(all(
26    feature = "input-avfoundation",
27    any(target_os = "macos", target_os = "ios")
28))]
29fn init_avfoundation(callback: impl Fn(bool) + Send + Sync + 'static) {
30    use nokhwa_bindings_macos::request_permission_with_callback;
31
32    request_permission_with_callback(callback);
33}
34
35#[cfg(not(all(
36    feature = "input-avfoundation",
37    any(target_os = "macos", target_os = "ios")
38)))]
39fn status_avfoundation() -> bool {
40    true
41}
42
43#[cfg(all(
44    feature = "input-avfoundation",
45    any(target_os = "macos", target_os = "ios")
46))]
47fn status_avfoundation() -> bool {
48    use nokhwa_bindings_macos::{current_authorization_status, AVAuthorizationStatus};
49
50    matches!(
51        current_authorization_status(),
52        AVAuthorizationStatus::Authorized
53    )
54}
55
56// todo: make this work on browser code
57/// Initialize `nokhwa`
58/// It is your responsibility to call this function before anything else, but only on `MacOS`.
59///
60/// The `on_complete` is called after initialization (a.k.a User granted permission). The callback's argument
61/// is weather the initialization was successful or not
62pub fn nokhwa_initialize(on_complete: impl Fn(bool) + Send + Sync + 'static) {
63    init_avfoundation(on_complete);
64}
65
66/// Check the status if `nokhwa`
67/// True if the initialization is successful (ready-to-use)
68#[must_use]
69pub fn nokhwa_check() -> bool {
70    status_avfoundation()
71}