ltk_webkit/wpe/
autoplay.rs

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
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Whether media may start playing by itself: WebKit's autoplay website policy, which a view takes at construction.

use std::ffi::{ c_char, c_int };
use std::ptr;
use std::sync::atomic::{ AtomicI32, Ordering };

use wpe_webkit_sys::*;

use crate::types::Autoplay;

static POLICY: AtomicI32 = AtomicI32::new( WEBKIT_AUTOPLAY_ALLOW_WITHOUT_SOUND );

/// For the views created from now on; an existing view keeps the policy it was built with.
pub fn set_autoplay( policy: Autoplay )
{
	let policy = match policy
	{
		Autoplay::Allow             => WEBKIT_AUTOPLAY_ALLOW,
		Autoplay::AllowWithoutSound => WEBKIT_AUTOPLAY_ALLOW_WITHOUT_SOUND,
		Autoplay::Deny              => WEBKIT_AUTOPLAY_DENY,
	};
	POLICY.store( policy, Ordering::Relaxed );
}

/// A full reference, for the caller to release once the view holds its own.
pub( crate ) fn website_policies() -> *mut WebKitWebsitePolicies
{
	// SAFETY: the property name is NUL-terminated and the list NULL-terminated.
	unsafe
	{
		webkit_website_policies_new_with_policies(
			b"autoplay\0".as_ptr() as *const c_char,
			POLICY.load( Ordering::Relaxed ) as c_int,
			ptr::null::<c_char>(),
		)
	}
}