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