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
use std::convert::TryInto;
use std::marker::PhantomData;
use super::cookie::VoidCookie;
use super::errors::{ConnectionError, ReplyError};
use super::generated::xproto::ConnectionExt as XProtoConnectionExt;
use super::x11_utils::TryParse;
#[derive(Debug, Clone)]
pub struct PropertyIterator<'a, T>(&'a [u8], PhantomData<T>);
impl<'a, T> PropertyIterator<'a, T> {
pub(crate) fn new(value: &'a [u8]) -> Self {
PropertyIterator(value, PhantomData)
}
}
impl<T> Iterator for PropertyIterator<'_, T>
where
T: TryParse,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match T::try_parse(self.0) {
Ok((value, remaining)) => {
self.0 = remaining;
Some(value)
}
Err(_) => {
self.0 = &[];
None
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.0.len() / std::mem::size_of::<T>();
(size, Some(size))
}
}
impl<T: TryParse> std::iter::FusedIterator for PropertyIterator<'_, T> {}
pub trait ConnectionExt: XProtoConnectionExt {
fn change_property8<A>(
&self,
mode: A,
window: u32,
property: u32,
type_: u32,
data: &[u8],
) -> Result<VoidCookie<'_, Self>, ConnectionError>
where
A: Into<u8>,
{
self.change_property(
mode,
window,
property,
type_,
8,
data.len().try_into()?,
data,
)
}
fn change_property16<A>(
&self,
mode: A,
window: u32,
property: u32,
type_: u32,
data: &[u16],
) -> Result<VoidCookie<'_, Self>, ConnectionError>
where
A: Into<u8>,
{
let mut data_u8 = Vec::with_capacity(data.len() * 2);
for item in data {
data_u8.extend(&item.to_ne_bytes());
}
self.change_property(
mode,
window,
property,
type_,
16,
data.len().try_into()?,
&data_u8,
)
}
fn change_property32<A>(
&self,
mode: A,
window: u32,
property: u32,
type_: u32,
data: &[u32],
) -> Result<VoidCookie<'_, Self>, ConnectionError>
where
A: Into<u8>,
{
let mut data_u8 = Vec::with_capacity(data.len() * 4);
for item in data {
data_u8.extend(&item.to_ne_bytes());
}
self.change_property(
mode,
window,
property,
type_,
32,
data.len().try_into()?,
&data_u8,
)
}
fn sync(&self) -> Result<(), ReplyError<Self::Buf>> {
self.get_input_focus()?.reply().and(Ok(()))
}
}
impl<C: XProtoConnectionExt + ?Sized> ConnectionExt for C {}