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
use {Size, View, Visibility};
use ffi;
use libc;
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::CStr;
use std::fmt;
use std::mem;
use std::ptr;
use std::rc::{Rc, Weak};
use std::slice;
pub struct NotRequiredThreadSafe<T>(pub T);
unsafe impl<T> Send for NotRequiredThreadSafe<T> {}
unsafe impl<T> Sync for NotRequiredThreadSafe<T> {}
lazy_static! {
pub static ref OUTPUTS:
NotRequiredThreadSafe<RefCell<HashMap<libc::uintptr_t, Rc<()>>>>
= NotRequiredThreadSafe(RefCell::new(HashMap::new()));
}
#[repr(C)]
pub struct Output;
#[cfg(not(feature = "unsafe-stable"))]
impl !Sync for Output {}
#[cfg(not(feature = "unsafe-stable"))]
impl !Send for Output {}
impl fmt::Debug for Output {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Output {{ id: {} }}", handle(self))
}
}
impl Output {
pub unsafe fn all_outputs<'a>() -> &'a [Output] {
let mut size = 0usize;
let output_ptr = ffi::wlc_get_outputs(&mut size) as *mut _;
slice::from_raw_parts_mut(output_ptr, size)
}
pub fn with_all_outputs<F, R>(runner: F) -> R
where F: FnOnce(&[Output]) -> R
{
let outputs = unsafe { Output::all_outputs() };
runner(outputs)
}
pub unsafe fn focused_output<'a>() -> &'a Output {
from_handle(ffi::wlc_get_focused_output())
}
pub fn with_focused_output<F, R>(runner: F) -> R
where F: FnOnce(&Output) -> R
{
let output = unsafe { Output::focused_output() };
runner(output)
}
pub fn set_no_focus() {
unsafe { ffi::wlc_output_focus(mem::transmute::<*const libc::c_void, libc::uintptr_t>(ptr::null())) }
}
pub fn name(&self) -> Cow<str> {
unsafe { CStr::from_ptr(ffi::wlc_output_get_name(handle(self))).to_string_lossy() }
}
pub fn is_sleeping(&self) -> bool {
unsafe { ffi::wlc_output_get_sleep(handle(self)) }
}
pub fn set_sleeping(&self, sleep: bool) {
unsafe { ffi::wlc_output_set_sleep(handle(self), sleep) }
}
pub fn resolution(&self) -> Size {
Size::from_ffi(unsafe { &*ffi::wlc_output_get_resolution(handle(self)) })
}
pub fn virtual_resolution(&self) -> Size {
Size::from_ffi(unsafe { &*ffi::wlc_output_get_virtual_resolution(handle(self)) })
}
pub fn set_resolution(&self, resolution: Size, scale: u32) {
unsafe { ffi::wlc_output_set_resolution(handle(self), &resolution.into_ffi() as *const _, scale) }
}
pub fn scale(&self) -> u32 {
unsafe { ffi::wlc_output_get_scale(handle(self)) }
}
pub fn visibility(&self) -> Visibility::Flags {
Visibility::Flags::from_bits_truncate(unsafe { ffi::wlc_output_get_mask(handle(self)) })
}
pub fn set_visibility(&self, slots: Visibility::Flags) {
unsafe { ffi::wlc_output_set_mask(handle(self), slots.bits()) }
}
pub fn views(&self) -> Vec<&View> {
let mut size = 0usize;
let view_ptr = unsafe { ffi::wlc_output_get_views(handle(self), &mut size) as *const _ };
let slice = unsafe { slice::from_raw_parts(view_ptr, size) };
slice.to_vec()
}
pub fn set_views<'a, I: IntoIterator<Item = &'a View>>(&self, views: I) -> Result<(), Vec<&'a View>> {
let mut vec: Vec<ffi::wlc_handle> = views.into_iter()
.map(|x| unsafe { mem::transmute::<&View, ffi::wlc_handle>(x) })
.collect();
let len = vec.len();
if unsafe { ffi::wlc_output_set_views(handle(self), vec.as_mut_ptr(), len) } {
Ok(mem::forget(vec))
} else {
Err(vec.into_iter()
.map(|x| unsafe { mem::transmute::<ffi::wlc_handle, &View>(x) })
.collect())
}
}
pub fn focus(&self) {
unsafe { ffi::wlc_output_focus(handle(self)) }
}
pub fn gamma_size(&self) -> u16 {
unsafe { ffi::wlc_output_get_gamma_size(handle(self)) }
}
pub fn set_gamma(&self, r: &mut [u16], g: &mut [u16], b: &mut [u16]) {
if r.len() != g.len() || r.len() != b.len() {
panic!("Color ramps do not have the same size");
}
unsafe {
ffi::wlc_output_set_gamma(handle(self),
r.len() as u16,
r.as_mut_ptr(),
g.as_mut_ptr(),
b.as_mut_ptr())
}
}
#[cfg(feature = "render")]
pub fn schedule_render(&self) {
unsafe { ffi::wlc_output_schedule_render(handle(self)) }
}
pub fn weak_reference(&self) -> WeakOutput {
let mut outputs = OUTPUTS.0.borrow_mut();
let ref_counter = outputs.entry(handle(self)).or_insert_with(|| Rc::new(()));
WeakOutput(Rc::downgrade(ref_counter), handle(self))
}
}
#[derive(Clone)]
pub struct WeakOutput(Weak<()>, ffi::wlc_handle);
impl WeakOutput {
pub unsafe fn upgrade(&self) -> Option<&Output> {
let test = self.0.clone().upgrade();
match test {
Some(_) => Some(from_handle(self.1)),
None => None,
}
}
pub fn run<F, R>(&self, runner: F) -> Option<R>
where F: FnOnce(&Output) -> R
{
let output = unsafe { self.upgrade() };
match output {
Some(output) => Some(runner(output)),
None => None,
}
}
}
impl fmt::Debug for WeakOutput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "WeakOutput {{ id: {} }}", self.1)
}
}
#[cfg(not(feature = "unsafe-stable"))]
impl !Send for WeakOutput {}
#[cfg(not(feature = "unsafe-stable"))]
impl !Sync for WeakOutput {}
pub fn from_handle<'a>(handle: ffi::wlc_handle) -> &'a mut Output {
unsafe { &mut *(handle as *mut Output) }
}
pub fn handle(output: &Output) -> ffi::wlc_handle {
unsafe { mem::transmute(output) }
}
impl PartialEq for Output {
fn eq(&self, other: &Output) -> bool {
handle(self) == handle(other)
}
}
impl Eq for Output {}
impl PartialEq<WeakOutput> for Output {
fn eq(&self, other: &WeakOutput) -> bool {
handle(self) == other.1
}
}
impl PartialEq<Output> for WeakOutput {
fn eq(&self, other: &Output) -> bool {
self.1 == handle(other)
}
}
impl PartialEq for WeakOutput {
fn eq(&self, other: &WeakOutput) -> bool {
self.1 == other.1
}
}
impl Eq for WeakOutput {}
use std::hash::*;
impl Hash for Output {
fn hash<H: Hasher>(&self, state: &mut H) {
handle(self).hash(state);
}
}
impl Hash for WeakOutput {
fn hash<H: Hasher>(&self, state: &mut H) {
self.1.hash(state);
}
}