1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use crate::enums::SurfaceType;
4 #[cfg(feature = "use_glib")]
5 use glib::translate::*;
6 use std::convert::TryFrom;
7 use std::fmt;
8 use std::ops::Deref;
9 use std::ptr;
10 
11 use crate::error::Error;
12 use crate::surface::Surface;
13 
14 #[derive(Debug)]
15 pub struct XCBDrawable(pub u32);
16 
17 impl XCBDrawable {
to_raw_none(&self) -> u3218     fn to_raw_none(&self) -> u32 {
19         self.0
20     }
21 }
22 
23 impl fmt::Display for XCBDrawable {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result24     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25         write!(f, "XCBDrawable")
26     }
27 }
28 
29 #[derive(Debug)]
30 pub struct XCBPixmap(pub u32);
31 
32 impl XCBPixmap {
to_raw_none(&self) -> u3233     fn to_raw_none(&self) -> u32 {
34         self.0
35     }
36 }
37 
38 impl fmt::Display for XCBPixmap {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result39     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40         write!(f, "XCBPixmap")
41     }
42 }
43 
44 #[derive(Debug)]
45 #[doc(alias = "xcb_connection_t")]
46 pub struct XCBConnection(pub ptr::NonNull<ffi::xcb_connection_t>);
47 
48 impl XCBConnection {
to_raw_none(&self) -> *mut ffi::xcb_connection_t49     pub fn to_raw_none(&self) -> *mut ffi::xcb_connection_t {
50         self.0.as_ptr()
51     }
52 
from_raw_none(ptr: *mut ffi::xcb_connection_t) -> XCBConnection53     pub unsafe fn from_raw_none(ptr: *mut ffi::xcb_connection_t) -> XCBConnection {
54         assert!(!ptr.is_null());
55         XCBConnection(ptr::NonNull::new_unchecked(ptr))
56     }
57 
from_raw_borrow(ptr: *mut ffi::xcb_connection_t) -> Borrowed<XCBConnection>58     pub unsafe fn from_raw_borrow(ptr: *mut ffi::xcb_connection_t) -> Borrowed<XCBConnection> {
59         assert!(!ptr.is_null());
60         Borrowed::new(XCBConnection(ptr::NonNull::new_unchecked(ptr)))
61     }
62 
from_raw_full(ptr: *mut ffi::xcb_connection_t) -> XCBConnection63     pub unsafe fn from_raw_full(ptr: *mut ffi::xcb_connection_t) -> XCBConnection {
64         assert!(!ptr.is_null());
65         XCBConnection(ptr::NonNull::new_unchecked(ptr))
66     }
67 }
68 
69 #[cfg(feature = "use_glib")]
70 impl<'a> ToGlibPtr<'a, *mut ffi::xcb_connection_t> for &'a XCBConnection {
71     type Storage = &'a XCBConnection;
72 
73     #[inline]
to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_connection_t, &'a XCBConnection>74     fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_connection_t, &'a XCBConnection> {
75         Stash(self.to_raw_none(), *self)
76     }
77 }
78 
79 #[cfg(feature = "use_glib")]
80 impl FromGlibPtrNone<*mut ffi::xcb_connection_t> for XCBConnection {
81     #[inline]
from_glib_none(ptr: *mut ffi::xcb_connection_t) -> XCBConnection82     unsafe fn from_glib_none(ptr: *mut ffi::xcb_connection_t) -> XCBConnection {
83         Self::from_raw_none(ptr)
84     }
85 }
86 
87 #[cfg(feature = "use_glib")]
88 impl FromGlibPtrBorrow<*mut ffi::xcb_connection_t> for XCBConnection {
89     #[inline]
from_glib_borrow(ptr: *mut ffi::xcb_connection_t) -> Borrowed<XCBConnection>90     unsafe fn from_glib_borrow(ptr: *mut ffi::xcb_connection_t) -> Borrowed<XCBConnection> {
91         Self::from_raw_borrow(ptr)
92     }
93 }
94 
95 #[cfg(feature = "use_glib")]
96 impl FromGlibPtrFull<*mut ffi::xcb_connection_t> for XCBConnection {
97     #[inline]
from_glib_full(ptr: *mut ffi::xcb_connection_t) -> XCBConnection98     unsafe fn from_glib_full(ptr: *mut ffi::xcb_connection_t) -> XCBConnection {
99         Self::from_raw_full(ptr)
100     }
101 }
102 
103 impl Clone for XCBConnection {
clone(&self) -> XCBConnection104     fn clone(&self) -> XCBConnection {
105         unsafe { Self::from_raw_none(self.to_raw_none()) }
106     }
107 }
108 
109 impl fmt::Display for XCBConnection {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result110     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111         write!(f, "XCBConnection")
112     }
113 }
114 
115 #[derive(Debug)]
116 #[doc(alias = "xcb_render_pictforminfo_t")]
117 pub struct XCBRenderPictFormInfo(pub ptr::NonNull<ffi::xcb_render_pictforminfo_t>);
118 
119 impl XCBRenderPictFormInfo {
to_raw_none(&self) -> *mut ffi::xcb_render_pictforminfo_t120     pub fn to_raw_none(&self) -> *mut ffi::xcb_render_pictforminfo_t {
121         self.0.as_ptr()
122     }
123 
from_raw_none(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo124     pub unsafe fn from_raw_none(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo {
125         assert!(!ptr.is_null());
126         XCBRenderPictFormInfo(ptr::NonNull::new_unchecked(ptr))
127     }
128 
from_raw_borrow( ptr: *mut ffi::xcb_render_pictforminfo_t, ) -> Borrowed<XCBRenderPictFormInfo>129     pub unsafe fn from_raw_borrow(
130         ptr: *mut ffi::xcb_render_pictforminfo_t,
131     ) -> Borrowed<XCBRenderPictFormInfo> {
132         assert!(!ptr.is_null());
133         Borrowed::new(XCBRenderPictFormInfo(ptr::NonNull::new_unchecked(ptr)))
134     }
135 
from_raw_full(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo136     pub unsafe fn from_raw_full(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo {
137         assert!(!ptr.is_null());
138         XCBRenderPictFormInfo(ptr::NonNull::new_unchecked(ptr))
139     }
140 }
141 
142 #[cfg(feature = "use_glib")]
143 impl<'a> ToGlibPtr<'a, *mut ffi::xcb_render_pictforminfo_t> for &'a XCBRenderPictFormInfo {
144     type Storage = &'a XCBRenderPictFormInfo;
145 
146     #[inline]
to_glib_none( &self, ) -> Stash<'a, *mut ffi::xcb_render_pictforminfo_t, &'a XCBRenderPictFormInfo>147     fn to_glib_none(
148         &self,
149     ) -> Stash<'a, *mut ffi::xcb_render_pictforminfo_t, &'a XCBRenderPictFormInfo> {
150         Stash(self.to_raw_none(), *self)
151     }
152 }
153 
154 #[cfg(feature = "use_glib")]
155 impl FromGlibPtrNone<*mut ffi::xcb_render_pictforminfo_t> for XCBRenderPictFormInfo {
156     #[inline]
from_glib_none(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo157     unsafe fn from_glib_none(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo {
158         Self::from_raw_none(ptr)
159     }
160 }
161 
162 #[cfg(feature = "use_glib")]
163 impl FromGlibPtrBorrow<*mut ffi::xcb_render_pictforminfo_t> for XCBRenderPictFormInfo {
164     #[inline]
from_glib_borrow( ptr: *mut ffi::xcb_render_pictforminfo_t, ) -> Borrowed<XCBRenderPictFormInfo>165     unsafe fn from_glib_borrow(
166         ptr: *mut ffi::xcb_render_pictforminfo_t,
167     ) -> Borrowed<XCBRenderPictFormInfo> {
168         Self::from_raw_borrow(ptr)
169     }
170 }
171 
172 #[cfg(feature = "use_glib")]
173 impl FromGlibPtrFull<*mut ffi::xcb_render_pictforminfo_t> for XCBRenderPictFormInfo {
174     #[inline]
from_glib_full(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo175     unsafe fn from_glib_full(ptr: *mut ffi::xcb_render_pictforminfo_t) -> XCBRenderPictFormInfo {
176         Self::from_raw_full(ptr)
177     }
178 }
179 
180 impl Clone for XCBRenderPictFormInfo {
clone(&self) -> XCBRenderPictFormInfo181     fn clone(&self) -> XCBRenderPictFormInfo {
182         unsafe { Self::from_raw_none(self.to_raw_none()) }
183     }
184 }
185 
186 impl fmt::Display for XCBRenderPictFormInfo {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result187     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188         write!(f, "XCBRenderPictFormInfo")
189     }
190 }
191 
192 #[derive(Debug)]
193 #[doc(alias = "xcb_screen_t")]
194 pub struct XCBScreen(pub ptr::NonNull<ffi::xcb_screen_t>);
195 
196 impl XCBScreen {
to_raw_none(&self) -> *mut ffi::xcb_screen_t197     pub fn to_raw_none(&self) -> *mut ffi::xcb_screen_t {
198         self.0.as_ptr()
199     }
200 
from_raw_none(ptr: *mut ffi::xcb_screen_t) -> XCBScreen201     pub unsafe fn from_raw_none(ptr: *mut ffi::xcb_screen_t) -> XCBScreen {
202         assert!(!ptr.is_null());
203         XCBScreen(ptr::NonNull::new_unchecked(ptr))
204     }
205 
from_raw_borrow(ptr: *mut ffi::xcb_screen_t) -> Borrowed<XCBScreen>206     pub unsafe fn from_raw_borrow(ptr: *mut ffi::xcb_screen_t) -> Borrowed<XCBScreen> {
207         assert!(!ptr.is_null());
208         Borrowed::new(XCBScreen(ptr::NonNull::new_unchecked(ptr)))
209     }
210 
from_raw_full(ptr: *mut ffi::xcb_screen_t) -> XCBScreen211     pub unsafe fn from_raw_full(ptr: *mut ffi::xcb_screen_t) -> XCBScreen {
212         assert!(!ptr.is_null());
213         XCBScreen(ptr::NonNull::new_unchecked(ptr))
214     }
215 }
216 
217 #[cfg(feature = "use_glib")]
218 impl<'a> ToGlibPtr<'a, *mut ffi::xcb_screen_t> for &'a XCBScreen {
219     type Storage = &'a XCBScreen;
220 
221     #[inline]
to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_screen_t, &'a XCBScreen>222     fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_screen_t, &'a XCBScreen> {
223         Stash(self.to_raw_none(), *self)
224     }
225 }
226 
227 #[cfg(feature = "use_glib")]
228 impl FromGlibPtrNone<*mut ffi::xcb_screen_t> for XCBScreen {
229     #[inline]
from_glib_none(ptr: *mut ffi::xcb_screen_t) -> XCBScreen230     unsafe fn from_glib_none(ptr: *mut ffi::xcb_screen_t) -> XCBScreen {
231         Self::from_raw_none(ptr)
232     }
233 }
234 
235 #[cfg(feature = "use_glib")]
236 impl FromGlibPtrBorrow<*mut ffi::xcb_screen_t> for XCBScreen {
237     #[inline]
from_glib_borrow(ptr: *mut ffi::xcb_screen_t) -> Borrowed<XCBScreen>238     unsafe fn from_glib_borrow(ptr: *mut ffi::xcb_screen_t) -> Borrowed<XCBScreen> {
239         Self::from_raw_borrow(ptr)
240     }
241 }
242 
243 #[cfg(feature = "use_glib")]
244 impl FromGlibPtrFull<*mut ffi::xcb_screen_t> for XCBScreen {
245     #[inline]
from_glib_full(ptr: *mut ffi::xcb_screen_t) -> XCBScreen246     unsafe fn from_glib_full(ptr: *mut ffi::xcb_screen_t) -> XCBScreen {
247         Self::from_raw_full(ptr)
248     }
249 }
250 
251 impl Clone for XCBScreen {
clone(&self) -> XCBScreen252     fn clone(&self) -> XCBScreen {
253         unsafe { Self::from_raw_none(self.to_raw_none()) }
254     }
255 }
256 
257 impl fmt::Display for XCBScreen {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result258     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
259         write!(f, "XCBScreen")
260     }
261 }
262 
263 declare_surface!(XCBSurface, SurfaceType::Xcb);
264 
265 impl XCBSurface {
266     #[doc(alias = "cairo_xcb_surface_create")]
create( connection: &XCBConnection, drawable: &XCBDrawable, visual: &XCBVisualType, width: i32, height: i32, ) -> Result<Self, Error>267     pub fn create(
268         connection: &XCBConnection,
269         drawable: &XCBDrawable,
270         visual: &XCBVisualType,
271         width: i32,
272         height: i32,
273     ) -> Result<Self, Error> {
274         unsafe {
275             Self::from_raw_full(ffi::cairo_xcb_surface_create(
276                 connection.to_raw_none(),
277                 drawable.to_raw_none(),
278                 visual.to_raw_none(),
279                 width,
280                 height,
281             ))
282         }
283     }
284 
285     #[doc(alias = "cairo_xcb_surface_create_for_bitmap")]
create_for_bitmap( connection: &XCBConnection, screen: &XCBScreen, bitmap: &XCBPixmap, width: i32, height: i32, ) -> Result<Self, Error>286     pub fn create_for_bitmap(
287         connection: &XCBConnection,
288         screen: &XCBScreen,
289         bitmap: &XCBPixmap,
290         width: i32,
291         height: i32,
292     ) -> Result<Self, Error> {
293         unsafe {
294             Ok(Self(Surface::from_raw_full(
295                 ffi::cairo_xcb_surface_create_for_bitmap(
296                     connection.to_raw_none(),
297                     screen.to_raw_none(),
298                     bitmap.to_raw_none(),
299                     width,
300                     height,
301                 ),
302             )?))
303         }
304     }
305 
306     #[doc(alias = "cairo_xcb_surface_create_with_xrender_format")]
create_with_xrender_format( connection: &XCBConnection, screen: &XCBScreen, bitmap: &XCBPixmap, format: &XCBRenderPictFormInfo, width: i32, height: i32, ) -> Result<Self, Error>307     pub fn create_with_xrender_format(
308         connection: &XCBConnection,
309         screen: &XCBScreen,
310         bitmap: &XCBPixmap,
311         format: &XCBRenderPictFormInfo,
312         width: i32,
313         height: i32,
314     ) -> Result<Self, Error> {
315         unsafe {
316             Ok(Self(Surface::from_raw_full(
317                 ffi::cairo_xcb_surface_create_with_xrender_format(
318                     connection.to_raw_none(),
319                     screen.to_raw_none(),
320                     bitmap.to_raw_none(),
321                     format.to_raw_none(),
322                     width,
323                     height,
324                 ),
325             )?))
326         }
327     }
328 
329     #[doc(alias = "cairo_xcb_surface_set_size")]
set_size(&self, width: i32, height: i32) -> Result<(), Error>330     pub fn set_size(&self, width: i32, height: i32) -> Result<(), Error> {
331         unsafe { ffi::cairo_xcb_surface_set_size(self.to_raw_none(), width, height) }
332         self.status()
333     }
334 
335     #[doc(alias = "cairo_xcb_surface_set_drawable")]
set_drawable( &self, drawable: &XCBDrawable, width: i32, height: i32, ) -> Result<(), Error>336     pub fn set_drawable(
337         &self,
338         drawable: &XCBDrawable,
339         width: i32,
340         height: i32,
341     ) -> Result<(), Error> {
342         unsafe {
343             ffi::cairo_xcb_surface_set_drawable(
344                 self.to_raw_none(),
345                 drawable.to_raw_none(),
346                 width,
347                 height,
348             )
349         }
350         self.status()
351     }
352 }
353 
354 #[derive(Debug)]
355 #[doc(alias = "xcb_visualtype_t")]
356 pub struct XCBVisualType(pub ptr::NonNull<ffi::xcb_visualtype_t>);
357 
358 impl XCBVisualType {
to_raw_none(&self) -> *mut ffi::xcb_visualtype_t359     pub fn to_raw_none(&self) -> *mut ffi::xcb_visualtype_t {
360         self.0.as_ptr()
361     }
362 
from_raw_none(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType363     pub unsafe fn from_raw_none(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType {
364         assert!(!ptr.is_null());
365         XCBVisualType(ptr::NonNull::new_unchecked(ptr))
366     }
367 
from_raw_borrow(ptr: *mut ffi::xcb_visualtype_t) -> Borrowed<XCBVisualType>368     pub unsafe fn from_raw_borrow(ptr: *mut ffi::xcb_visualtype_t) -> Borrowed<XCBVisualType> {
369         assert!(!ptr.is_null());
370         Borrowed::new(XCBVisualType(ptr::NonNull::new_unchecked(ptr)))
371     }
372 
from_raw_full(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType373     pub unsafe fn from_raw_full(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType {
374         assert!(!ptr.is_null());
375         XCBVisualType(ptr::NonNull::new_unchecked(ptr))
376     }
377 }
378 
379 #[cfg(feature = "use_glib")]
380 impl<'a> ToGlibPtr<'a, *mut ffi::xcb_visualtype_t> for &'a XCBVisualType {
381     type Storage = &'a XCBVisualType;
382 
383     #[inline]
to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_visualtype_t, &'a XCBVisualType>384     fn to_glib_none(&self) -> Stash<'a, *mut ffi::xcb_visualtype_t, &'a XCBVisualType> {
385         Stash(self.to_raw_none(), *self)
386     }
387 }
388 
389 #[cfg(feature = "use_glib")]
390 impl FromGlibPtrNone<*mut ffi::xcb_visualtype_t> for XCBVisualType {
391     #[inline]
from_glib_none(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType392     unsafe fn from_glib_none(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType {
393         Self::from_raw_none(ptr)
394     }
395 }
396 
397 #[cfg(feature = "use_glib")]
398 impl FromGlibPtrBorrow<*mut ffi::xcb_visualtype_t> for XCBVisualType {
399     #[inline]
from_glib_borrow(ptr: *mut ffi::xcb_visualtype_t) -> Borrowed<XCBVisualType>400     unsafe fn from_glib_borrow(ptr: *mut ffi::xcb_visualtype_t) -> Borrowed<XCBVisualType> {
401         Self::from_raw_borrow(ptr)
402     }
403 }
404 
405 #[cfg(feature = "use_glib")]
406 impl FromGlibPtrFull<*mut ffi::xcb_visualtype_t> for XCBVisualType {
407     #[inline]
from_glib_full(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType408     unsafe fn from_glib_full(ptr: *mut ffi::xcb_visualtype_t) -> XCBVisualType {
409         Self::from_raw_full(ptr)
410     }
411 }
412 
413 impl Clone for XCBVisualType {
clone(&self) -> XCBVisualType414     fn clone(&self) -> XCBVisualType {
415         unsafe { Self::from_raw_none(self.to_raw_none()) }
416     }
417 }
418 
419 impl fmt::Display for XCBVisualType {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result420     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
421         write!(f, "XCBVisualType")
422     }
423 }
424 
425 impl crate::device::Device {
426     #[doc(alias = "cairo_xcb_device_get_connection")]
427     #[doc(alias = "get_connection")]
connection(&self) -> XCBConnection428     pub fn connection(&self) -> XCBConnection {
429         unsafe {
430             XCBConnection::from_raw_full(ffi::cairo_xcb_device_get_connection(self.to_raw_none()))
431         }
432     }
433 
434     #[doc(alias = "cairo_xcb_device_debug_cap_xshm_version")]
debug_cap_xshm_version(&self, major_version: i32, minor_version: i32)435     pub fn debug_cap_xshm_version(&self, major_version: i32, minor_version: i32) {
436         unsafe {
437             ffi::cairo_xcb_device_debug_cap_xshm_version(
438                 self.to_raw_none(),
439                 major_version,
440                 minor_version,
441             )
442         }
443     }
444 }
445