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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::enums::DeviceType;
use crate::error::Error;
use crate::utils::status_to_result;

use std::fmt;
use std::ptr;

#[cfg(feature = "use_glib")]
use glib::translate::*;

#[cfg(any(feature = "script", feature = "dox"))]
use crate::enums::Content;
#[cfg(any(feature = "script", feature = "dox"))]
use crate::enums::ScriptMode;
#[cfg(any(feature = "script", feature = "dox"))]
use crate::recording_surface::RecordingSurface;
#[cfg(any(feature = "script", feature = "dox"))]
use crate::surface::Surface;
#[cfg(any(feature = "script", feature = "dox"))]
use std::ffi::CString;
#[cfg(any(feature = "script", feature = "dox"))]
use std::path::Path;

#[derive(Debug)]
pub struct DeviceAcquireGuard<'a>(&'a Device);

impl<'a> Drop for DeviceAcquireGuard<'a> {
    fn drop(&mut self) {
        self.0.release();
    }
}

#[derive(Debug)]
#[doc(alias = "cairo_device_t")]
pub struct Device(ptr::NonNull<ffi::cairo_device_t>);

impl Device {
    pub unsafe fn from_raw_none(ptr: *mut ffi::cairo_device_t) -> Device {
        assert!(!ptr.is_null());
        ffi::cairo_device_reference(ptr);
        Device(ptr::NonNull::new_unchecked(ptr))
    }

    pub unsafe fn from_raw_borrow(ptr: *mut ffi::cairo_device_t) -> crate::Borrowed<Device> {
        assert!(!ptr.is_null());
        crate::Borrowed::new(Device(ptr::NonNull::new_unchecked(ptr)))
    }

    pub unsafe fn from_raw_full(ptr: *mut ffi::cairo_device_t) -> Device {
        assert!(!ptr.is_null());
        Device(ptr::NonNull::new_unchecked(ptr))
    }

    pub fn to_raw_none(&self) -> *mut ffi::cairo_device_t {
        self.0.as_ptr()
    }

    #[cfg(any(feature = "script", feature = "dox"))]
    #[doc(alias = "cairo_script_create")]
    pub fn create<P: AsRef<Path>>(filename: P) -> Option<Device> {
        unsafe {
            let filename = filename.as_ref().to_string_lossy().into_owned();
            let filename = CString::new(filename).unwrap();
            let p = ffi::cairo_script_create(filename.as_ptr());
            if p.is_null() {
                None
            } else {
                Some(Self::from_raw_full(p))
            }
        }
    }

    #[cfg(any(feature = "script", feature = "dox"))]
    #[doc(alias = "cairo_script_from_recording_surface")]
    pub fn from_recording_surface(&self, surface: &RecordingSurface) -> Result<(), Error> {
        unsafe {
            let status =
                ffi::cairo_script_from_recording_surface(self.to_raw_none(), surface.to_raw_none());
            status_to_result(status)
        }
    }

    #[cfg(any(feature = "script", feature = "dox"))]
    #[doc(alias = "cairo_script_get_mode")]
    #[doc(alias = "get_mode")]
    pub fn mode(&self) -> ScriptMode {
        unsafe { ScriptMode::from(ffi::cairo_script_get_mode(self.to_raw_none())) }
    }

    #[cfg(any(feature = "script", feature = "dox"))]
    #[doc(alias = "cairo_script_set_mode")]
    pub fn set_mode(&self, mode: ScriptMode) {
        unsafe { ffi::cairo_script_set_mode(self.to_raw_none(), mode.into()) }
    }

    #[cfg(any(feature = "script", feature = "dox"))]
    #[doc(alias = "cairo_script_surface_create")]
    pub fn surface_create(
        &self,
        content: Content,
        width: f64,
        height: f64,
    ) -> Result<Surface, Error> {
        unsafe {
            Surface::from_raw_full(ffi::cairo_script_surface_create(
                self.to_raw_none(),
                content.into(),
                width,
                height,
            ))
        }
    }

    #[cfg(any(feature = "script", feature = "dox"))]
    #[doc(alias = "cairo_script_surface_create_for_target")]
    pub fn surface_create_for_target(&self, target: &Surface) -> Result<Surface, Error> {
        target.status()?;
        unsafe {
            Surface::from_raw_full(ffi::cairo_script_surface_create_for_target(
                self.to_raw_none(),
                target.to_raw_none(),
            ))
        }
    }

    #[cfg(any(feature = "script", feature = "dox"))]
    #[doc(alias = "cairo_script_write_comment")]
    pub fn write_comment(&self, comment: &str) {
        unsafe {
            let len = comment.len();
            let comment = CString::new(comment).unwrap();
            ffi::cairo_script_write_comment(self.to_raw_none(), comment.as_ptr(), len as i32)
        }
    }

    #[doc(alias = "cairo_device_finish")]
    pub fn finish(&self) {
        unsafe { ffi::cairo_device_finish(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_flush")]
    pub fn flush(&self) {
        unsafe { ffi::cairo_device_flush(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_get_type")]
    #[doc(alias = "get_type")]
    pub fn type_(&self) -> DeviceType {
        unsafe { DeviceType::from(ffi::cairo_device_get_type(self.to_raw_none())) }
    }

    #[doc(alias = "cairo_device_acquire")]
    pub fn acquire(&self) -> Result<DeviceAcquireGuard, Error> {
        unsafe {
            let status = ffi::cairo_device_acquire(self.to_raw_none());
            status_to_result(status)?;
        }
        Ok(DeviceAcquireGuard(self))
    }

    #[doc(alias = "cairo_device_release")]
    fn release(&self) {
        unsafe { ffi::cairo_device_release(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_observer_elapsed")]
    pub fn observer_elapsed(&self) -> f64 {
        unsafe { ffi::cairo_device_observer_elapsed(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_observer_fill_elapsed")]
    pub fn observer_fill_elapsed(&self) -> f64 {
        unsafe { ffi::cairo_device_observer_fill_elapsed(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_observer_glyphs_elapsed")]
    pub fn observer_glyphs_elapsed(&self) -> f64 {
        unsafe { ffi::cairo_device_observer_glyphs_elapsed(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_observer_mask_elapsed")]
    pub fn observer_mask_elapsed(&self) -> f64 {
        unsafe { ffi::cairo_device_observer_mask_elapsed(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_observer_paint_elapsed")]
    pub fn observer_paint_elapsed(&self) -> f64 {
        unsafe { ffi::cairo_device_observer_paint_elapsed(self.to_raw_none()) }
    }

    #[doc(alias = "cairo_device_observer_stroke_elapsed")]
    pub fn observer_stroke_elapsed(&self) -> f64 {
        unsafe { ffi::cairo_device_observer_stroke_elapsed(self.to_raw_none()) }
    }

    #[cfg(any(feature = "xlib", feature = "xcb", feature = "dox"))]
    #[doc(alias = "cairo_xlib_device_debug_cap_xrender_version")]
    #[doc(alias = "cairo_xcb_device_debug_cap_xrender_version")]
    pub fn debug_cap_xrender_version(&self, major_version: i32, minor_version: i32) {
        unsafe {
            match self.type_() {
                DeviceType::Xlib => {
                    #[cfg(feature = "xlib")]
                    {
                        ffi::cairo_xlib_device_debug_cap_xrender_version(
                            self.to_raw_none(),
                            major_version,
                            minor_version,
                        )
                    }
                    #[cfg(not(feature = "xlib"))]
                    {
                        panic!("you need to enable \"xlib\" feature")
                    }
                }
                DeviceType::Xcb => {
                    #[cfg(feature = "xcb")]
                    {
                        ffi::cairo_xcb_device_debug_cap_xrender_version(
                            self.to_raw_none(),
                            major_version,
                            minor_version,
                        )
                    }
                    #[cfg(not(feature = "xcb"))]
                    {
                        panic!("you need to enable \"xcb\" feature")
                    }
                }
                d => panic!("invalid device type: {}", d),
            }
        }
    }

    #[cfg(any(feature = "xlib", feature = "xcb", feature = "dox"))]
    #[doc(alias = "cairo_xlib_device_debug_get_precision")]
    #[doc(alias = "cairo_xcb_device_debug_get_precision")]
    pub fn debug_get_precision(&self) -> i32 {
        unsafe {
            match self.type_() {
                DeviceType::Xlib => {
                    #[cfg(feature = "xlib")]
                    {
                        ffi::cairo_xlib_device_debug_get_precision(self.to_raw_none())
                    }
                    #[cfg(not(feature = "xlib"))]
                    {
                        panic!("you need to enable \"xlib\" feature")
                    }
                }
                DeviceType::Xcb => {
                    #[cfg(feature = "xcb")]
                    {
                        ffi::cairo_xcb_device_debug_get_precision(self.to_raw_none())
                    }
                    #[cfg(not(feature = "xcb"))]
                    {
                        panic!("you need to enable \"xcb\" feature")
                    }
                }
                d => panic!("invalid device type: {}", d),
            }
        }
    }

    #[cfg(any(feature = "xlib", feature = "xcb", feature = "dox"))]
    #[doc(alias = "cairo_xlib_device_debug_set_precision")]
    #[doc(alias = "cairo_xcb_device_debug_set_precision")]
    pub fn debug_set_precision(&self, precision: i32) {
        unsafe {
            match self.type_() {
                DeviceType::Xlib => {
                    #[cfg(feature = "xlib")]
                    {
                        ffi::cairo_xlib_device_debug_set_precision(self.to_raw_none(), precision)
                    }
                    #[cfg(not(feature = "xlib"))]
                    {
                        panic!("you need to enable \"xlib\" feature")
                    }
                }
                DeviceType::Xcb => {
                    #[cfg(feature = "xcb")]
                    {
                        ffi::cairo_xcb_device_debug_set_precision(self.to_raw_none(), precision)
                    }
                    #[cfg(not(feature = "xcb"))]
                    {
                        panic!("you need to enable \"xcb\" feature")
                    }
                }
                d => panic!("invalid device type: {}", d),
            }
        }
    }

    #[doc(alias = "cairo_device_status")]
    pub fn status(&self) -> Result<(), Error> {
        let status = unsafe { ffi::cairo_device_status(self.to_raw_none()) };
        status_to_result(status)
    }

    user_data_methods! {
        ffi::cairo_device_get_user_data,
        ffi::cairo_device_set_user_data,
    }
}

#[cfg(feature = "use_glib")]
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_device_t> for Device {
    type Storage = &'a Device;

    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::cairo_device_t, Self> {
        Stash(self.to_raw_none(), self)
    }

    #[inline]
    fn to_glib_full(&self) -> *mut ffi::cairo_device_t {
        unsafe { ffi::cairo_device_reference(self.to_raw_none()) }
    }
}

#[cfg(feature = "use_glib")]
impl FromGlibPtrNone<*mut ffi::cairo_device_t> for Device {
    #[inline]
    unsafe fn from_glib_none(ptr: *mut ffi::cairo_device_t) -> Device {
        Self::from_raw_none(ptr)
    }
}

#[cfg(feature = "use_glib")]
impl FromGlibPtrBorrow<*mut ffi::cairo_device_t> for Device {
    #[inline]
    unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_device_t) -> crate::Borrowed<Device> {
        Self::from_raw_borrow(ptr)
    }
}

#[cfg(feature = "use_glib")]
impl FromGlibPtrFull<*mut ffi::cairo_device_t> for Device {
    #[inline]
    unsafe fn from_glib_full(ptr: *mut ffi::cairo_device_t) -> Device {
        Self::from_raw_full(ptr)
    }
}

#[cfg(feature = "use_glib")]
gvalue_impl!(
    Device,
    ffi::cairo_device_t,
    ffi::gobject::cairo_gobject_device_get_type
);

impl Clone for Device {
    fn clone(&self) -> Device {
        unsafe { Self::from_raw_none(ffi::cairo_device_reference(self.0.as_ptr())) }
    }
}

impl Drop for Device {
    fn drop(&mut self) {
        unsafe {
            ffi::cairo_device_destroy(self.0.as_ptr());
        }
    }
}

impl fmt::Display for Device {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Device")
    }
}