ranma 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ use std::cell::RefCell;
2
+ use magnus::{method, prelude::*, Error, RArray, Ruby};
3
+ use tao::monitor::MonitorHandle;
4
+ use crate::dpi::{RbPhysicalSize, RbPhysicalPosition};
5
+
6
+ thread_local! {
7
+ static VIDEO_MODES: RefCell<Vec<tao::monitor::VideoMode>> = RefCell::new(Vec::new());
8
+ }
9
+
10
+ fn store_video_mode(vm: tao::monitor::VideoMode) -> usize {
11
+ VIDEO_MODES.with(|cell| {
12
+ let mut modes = cell.borrow_mut();
13
+ let index = modes.len();
14
+ modes.push(vm);
15
+ index
16
+ })
17
+ }
18
+
19
+ pub fn get_tao_video_mode(index: usize) -> Option<tao::monitor::VideoMode> {
20
+ VIDEO_MODES.with(|cell| {
21
+ let modes = cell.borrow();
22
+ modes.get(index).cloned()
23
+ })
24
+ }
25
+
26
+ pub fn clear_video_modes() {
27
+ VIDEO_MODES.with(|cell| {
28
+ cell.borrow_mut().clear();
29
+ });
30
+ }
31
+
32
+ #[magnus::wrap(class = "Ranma::VideoMode", free_immediately, size)]
33
+ pub struct RbVideoMode {
34
+ width: u32,
35
+ height: u32,
36
+ bit_depth: u16,
37
+ refresh_rate: u16,
38
+ index: usize,
39
+ }
40
+
41
+ impl RbVideoMode {
42
+ pub fn width(&self) -> u32 {
43
+ self.width
44
+ }
45
+
46
+ pub fn height(&self) -> u32 {
47
+ self.height
48
+ }
49
+
50
+ pub fn bit_depth(&self) -> u16 {
51
+ self.bit_depth
52
+ }
53
+
54
+ pub fn refresh_rate(&self) -> u16 {
55
+ self.refresh_rate
56
+ }
57
+
58
+ pub fn index(&self) -> usize {
59
+ self.index
60
+ }
61
+
62
+ pub fn inspect(&self) -> String {
63
+ format!(
64
+ "#<Ranma::VideoMode {}x{} {}bit @{}Hz>",
65
+ self.width, self.height, self.bit_depth, self.refresh_rate
66
+ )
67
+ }
68
+ }
69
+
70
+ #[magnus::wrap(class = "Ranma::MonitorHandle", free_immediately, size)]
71
+ pub struct RbMonitorHandle {
72
+ inner: MonitorHandle,
73
+ }
74
+
75
+ impl RbMonitorHandle {
76
+ pub fn new(handle: MonitorHandle) -> Self {
77
+ Self { inner: handle }
78
+ }
79
+
80
+ pub fn inner_clone(&self) -> MonitorHandle {
81
+ self.inner.clone()
82
+ }
83
+
84
+ pub fn name(&self) -> Option<String> {
85
+ self.inner.name()
86
+ }
87
+
88
+ pub fn size(&self) -> RbPhysicalSize {
89
+ self.inner.size().into()
90
+ }
91
+
92
+ pub fn position(&self) -> RbPhysicalPosition {
93
+ self.inner.position().into()
94
+ }
95
+
96
+ pub fn scale_factor(&self) -> f64 {
97
+ self.inner.scale_factor()
98
+ }
99
+
100
+ pub fn video_modes(&self) -> RArray {
101
+ let ruby = unsafe { Ruby::get_unchecked() };
102
+ let arr = ruby.ary_new();
103
+ for vm in self.inner.video_modes() {
104
+ let size = vm.size();
105
+ let bit_depth = vm.bit_depth();
106
+ let refresh_rate = vm.refresh_rate();
107
+ let index = store_video_mode(vm);
108
+ let rb_vm = RbVideoMode {
109
+ width: size.width,
110
+ height: size.height,
111
+ bit_depth,
112
+ refresh_rate,
113
+ index,
114
+ };
115
+ let _ = arr.push(rb_vm);
116
+ }
117
+ arr
118
+ }
119
+
120
+ pub fn inspect(&self) -> String {
121
+ format!(
122
+ "#<Ranma::MonitorHandle name={:?} size={:?} scale_factor={}>",
123
+ self.inner.name(),
124
+ self.inner.size(),
125
+ self.inner.scale_factor()
126
+ )
127
+ }
128
+ }
129
+
130
+ pub fn define_monitor_class(ruby: &Ruby, module: &magnus::RModule) -> Result<(), Error> {
131
+ let class = module.define_class("MonitorHandle", ruby.class_object())?;
132
+ class.define_method("name", method!(RbMonitorHandle::name, 0))?;
133
+ class.define_method("size", method!(RbMonitorHandle::size, 0))?;
134
+ class.define_method("position", method!(RbMonitorHandle::position, 0))?;
135
+ class.define_method("scale_factor", method!(RbMonitorHandle::scale_factor, 0))?;
136
+ class.define_method("video_modes", method!(RbMonitorHandle::video_modes, 0))?;
137
+ class.define_method("inspect", method!(RbMonitorHandle::inspect, 0))?;
138
+ class.define_method("to_s", method!(RbMonitorHandle::inspect, 0))?;
139
+
140
+ let vm_class = module.define_class("VideoMode", ruby.class_object())?;
141
+ vm_class.define_method("width", method!(RbVideoMode::width, 0))?;
142
+ vm_class.define_method("height", method!(RbVideoMode::height, 0))?;
143
+ vm_class.define_method("bit_depth", method!(RbVideoMode::bit_depth, 0))?;
144
+ vm_class.define_method("refresh_rate", method!(RbVideoMode::refresh_rate, 0))?;
145
+ vm_class.define_method("inspect", method!(RbVideoMode::inspect, 0))?;
146
+ vm_class.define_method("to_s", method!(RbVideoMode::inspect, 0))?;
147
+
148
+ Ok(())
149
+ }