cairo/
xcb.rs

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