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
use libcogcore_sys as sys;
use crate::{handlers::RequestHandler, object::ObjectPtr, raw};
/// Owned Cog shell.
#[derive(Clone)]
pub struct Shell {
ptr: ObjectPtr<sys::CogShell>,
}
impl Shell {
/// Creates a new shell.
pub fn new(name: &str, automated: bool) -> crate::Result<Self> {
let name = raw::cstring(name)?;
// SAFETY: `name` is a valid NUL-terminated string for the duration of
// the call, and Cog returns a transfer-full GObject pointer.
let ptr = unsafe { sys::cog_shell_new(name.as_ptr(), raw::bool_to_gboolean(automated)) };
Ok(Self {
ptr: ObjectPtr::from_owned(ptr, "cog_shell_new")?,
})
}
/// Returns the shell name.
pub fn name(&self) -> Option<String> {
// SAFETY: `self.ptr` owns a live CogShell and Cog returns a borrowed
// string pointer copied immediately by the wrapper.
let name = unsafe { sys::cog_shell_get_name(self.as_ptr()) };
raw::string_from_const(name)
}
/// Returns the configured device scale factor.
pub fn device_scale_factor(&self) -> f64 {
// SAFETY: `self.ptr` owns a live CogShell.
unsafe { sys::cog_shell_get_device_scale_factor(self.as_ptr()) }
}
/// Returns whether this shell was created in automated mode.
pub fn is_automated(&self) -> bool {
// SAFETY: `self.ptr` owns a live CogShell.
let automated = unsafe { sys::cog_shell_is_automated(self.as_ptr()) };
raw::gboolean_to_bool(automated)
}
/// Installs a request handler for a URI scheme.
pub fn set_request_handler(&self, scheme: &str, handler: &RequestHandler) -> crate::Result<()> {
let scheme = raw::cstring(scheme)?;
// SAFETY: The shell and handler are live GObjects, and `scheme` is a
// valid NUL-terminated string for the duration of the call.
unsafe {
sys::cog_shell_set_request_handler(self.as_ptr(), scheme.as_ptr(), handler.as_ptr())
};
Ok(())
}
/// Returns the borrowed WebKit context pointer for advanced integrations.
pub fn web_context_ptr(&self) -> *mut sys::WebKitWebContext {
// SAFETY: `self.ptr` owns a live CogShell. The returned pointer is
// borrowed from Cog and must not be unreffed by the caller.
unsafe { sys::cog_shell_get_web_context(self.as_ptr()) }
}
/// Returns the borrowed WebKit settings pointer for advanced integrations.
pub fn web_settings_ptr(&self) -> *mut sys::WebKitSettings {
// SAFETY: `self.ptr` owns a live CogShell. The returned pointer is
// borrowed from Cog and must not be unreffed by the caller.
unsafe { sys::cog_shell_get_web_settings(self.as_ptr()) }
}
/// Returns the borrowed GLib key file pointer for advanced integrations.
pub fn config_file_ptr(&self) -> *mut sys::GKeyFile {
// SAFETY: `self.ptr` owns a live CogShell. The returned pointer is
// borrowed from Cog and must not be freed by the caller.
unsafe { sys::cog_shell_get_config_file(self.as_ptr()) }
}
/// Returns the underlying Cog pointer.
pub fn as_ptr(&self) -> *mut sys::CogShell {
self.ptr.as_ptr()
}
}