dummy_rustwlc/
handle.rs

1//! Contains definitions for wlc handle types.
2//!
3//! # Implementations
4//! - **Debug**: pointer-prints the underlying `uintptr_t` handle
5//! - **Eq, Ord**: compare the underlying `uintptr_t` handle
6//! - **Clone**: View handles can safely be cloned.
7
8use libc;
9use libc::*;
10use wayland_sys::common::*;
11use wayland_sys::server::*;
12
13extern "C" {
14    pub fn wlc_get_outputs(memb: *mut libc::size_t) -> *const libc::uintptr_t;
15
16    pub fn wlc_get_focused_output() -> uintptr_t;
17
18    pub fn wlc_output_get_name(output: uintptr_t) -> *const c_char;
19
20    pub fn wlc_handle_get_user_data(handle: uintptr_t) -> *mut c_void;
21
22    pub fn wlc_handle_set_user_data(handle: uintptr_t, userdata: *const c_void);
23
24    pub fn wlc_output_get_sleep(output: uintptr_t) -> bool;
25
26    pub fn wlc_output_set_sleep(output: uintptr_t, sleep: bool);
27
28    pub fn wlc_output_get_resolution(output: uintptr_t) -> *const Size;
29
30    pub fn wlc_output_set_resolution(output: uintptr_t, resolution: *const Size, scale: uint32_t);
31
32    pub fn wlc_output_get_scale(output: uintptr_t) -> uint32_t;
33
34    pub fn wlc_output_get_virtual_resolution(output: uintptr_t) -> *const Size;
35
36    pub fn wlc_output_get_mask(output: uintptr_t) -> u32;
37
38    pub fn wlc_output_set_mask(output: uintptr_t, mask: u32);
39
40    // TODO tricky definition here
41    //pub fn wlc_output_get_pixels(output: WlcHandle) -> ();
42
43    pub fn wlc_output_get_views(output: uintptr_t,
44                            out_memb: *mut libc::size_t) -> *const uintptr_t;
45
46    pub fn wlc_output_set_views(output: uintptr_t, views: *const uintptr_t, memb: libc::size_t) -> bool;
47
48    pub fn wlc_output_focus(output: uintptr_t);
49
50    // View API
51
52    pub fn wlc_view_focus(view: uintptr_t);
53
54    pub fn wlc_view_close(view: uintptr_t);
55
56    // View -> Output
57    pub fn wlc_view_get_output(view: uintptr_t) -> uintptr_t;
58
59    // "set output. Alternatively you can use wlc_output_set_views"
60    pub fn wlc_view_set_output(view: uintptr_t, output: uintptr_t);
61
62    pub fn wlc_view_send_to_back(view: uintptr_t);
63
64    pub fn wlc_view_send_below(view: uintptr_t, other: uintptr_t);
65
66    pub fn wlc_view_bring_above(view: uintptr_t, other: uintptr_t);
67
68    pub fn wlc_view_bring_to_front(view: uintptr_t);
69
70    pub fn wlc_view_get_mask(view: uintptr_t) -> u32;
71
72    pub fn wlc_view_set_mask(view: uintptr_t, mask: u32);
73
74    pub fn wlc_view_get_geometry(view: uintptr_t) -> *const Geometry;
75
76    pub fn wlc_view_get_visible_geometry(view: uintptr_t, geo: *mut Geometry);
77
78    pub fn wlc_view_set_geometry(view: uintptr_t, edges: u32, geo: *const Geometry);
79
80    pub fn wlc_view_get_type(view: uintptr_t) -> ViewType;
81
82    pub fn wlc_view_set_type(view: uintptr_t, view_type: ViewType, toggle: bool);
83
84    pub fn wlc_view_get_state(view: uintptr_t) -> ViewState;
85
86    pub fn wlc_view_set_state(view: uintptr_t, state: ViewState, toggle: bool);
87
88    // Parent is Option<View>
89    pub fn wlc_view_get_parent(view: uintptr_t) -> uintptr_t;
90
91    // Parent is Option<View>
92    pub fn wlc_view_set_parent(view: uintptr_t, parent: uintptr_t);
93
94    pub fn wlc_view_get_title(view: uintptr_t) -> *const c_char;
95
96    pub fn wlc_view_get_class(view: uintptr_t) -> *const c_char;
97
98    pub fn wlc_view_get_app_id(view: uintptr_t) -> *const c_char;
99
100    pub fn wlc_view_get_pid(view: uintptr_t)-> pid_t;
101
102    pub fn wlc_handle_from_wl_surface_resource(resource: *const wl_resource) -> uintptr_t;
103
104    pub fn wlc_handle_from_wl_output_resource(resource: *const wl_resource) -> uintptr_t;
105
106    pub fn wlc_view_get_surface(view: uintptr_t) -> uintptr_t;
107
108    pub fn wlc_view_get_wl_client(view: uintptr_t) -> *mut wl_client;
109
110    pub fn wlc_view_get_role(view: uintptr_t) -> *mut wl_resource;
111
112    pub fn wlc_view_from_surface(surface: uintptr_t, client: *const wl_client, interface: *const wl_interface,
113                             implementation: *const c_void, version: uint32_t, id: uint32_t, userdata: *mut c_void)
114                             -> uintptr_t;
115}
116use libc::{uintptr_t, pid_t};
117
118use super::types::{Geometry, ResizeEdge, Point, Size, ViewType, ViewState};
119
120#[repr(C)]
121#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
122/// Represents a handle to a wlc view.
123///
124pub struct WlcView(pub uintptr_t);
125
126#[repr(C)]
127#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
128/// Represents a handle to a wlc output.
129pub struct WlcOutput(pub uintptr_t);
130
131impl From<WlcView> for WlcOutput {
132    fn from(view: WlcView) -> Self {
133        WlcOutput(view.0)
134    }
135}
136
137impl From<WlcOutput> for WlcView {
138    fn from(output: WlcOutput) -> Self {
139        WlcView(output.0)
140    }
141}
142
143static ZERO_RES: Size = Size { w: 0, h: 0 };
144
145impl WlcOutput {
146
147    /// Compatability/debugging function.
148    ///
149    /// wlc internally stores views and outputs under the same type.
150    /// If for some reason a conversion between the two was required,
151    /// this function could be called. If this is the case please submit
152    /// a bug report.
153    pub fn as_view(self) -> WlcView {
154        return WlcView::from(self)
155    }
156
157    /// Determines if this is a null a output (invalid).
158    #[inline]
159    pub fn is_null(self) -> bool {
160        self.0 == 0
161    }
162
163    /// Create a dummy WlcOutput for testing purposes.
164    ///
165    /// # Unsafety
166    /// The following operations on a dummy WlcOutput will cause crashes:
167    ///
168    /// - `WlcOutput::focused` when wlc is not running
169    /// - `WlcOutput::list` when wlc is not running
170    /// - `WlcOutput::set_resolution` on a dummy output
171    ///
172    /// In addition, `WlcOutput::set_views` will return an error.
173    ///
174    /// All other methods can be used on dummy outputs.
175    ///
176    /// # Example
177    /// ```rust
178    /// # use rustwlc::WlcOutput;
179    /// let output = WlcOutput::dummy(0u32);
180    /// let output2 = WlcOutput::dummy(1u32);
181    /// assert!(output < output2);
182    /// assert!(output != output2);
183    /// ```
184    pub fn dummy(code: u32) -> WlcOutput {
185        WlcOutput(code as uintptr_t)
186    }
187
188    /// Gets user-specified data.
189    ///
190    /// # Unsafety
191    /// The wlc implementation of this method uses `void*` pointers
192    /// for raw C data. This function will internaly do a conversion
193    /// between the input `T` and a `libc::c_void`.
194    ///
195    /// This is a highly unsafe conversion with no guarantees. As
196    /// such, usage of these functions requires an understanding of
197    /// what data they will have. Please review wlc's usage of these
198    /// functions before attempting to use them yourself.
199    pub unsafe fn get_user_data<T>(&self) -> &mut T {
200        unimplemented!()
201    }
202
203    /// Sets user-specified data.
204    ///
205    /// # Unsafety
206    /// The wlc implementation of this method uses `void*` pointers
207    /// for raw C data. This function will internaly do a conversion
208    /// between the input `T` and a `libc::c_void`.
209    ///
210    /// This is a highly unsafe conversion with no guarantees. As
211    /// such, usage of these functions requires an understanding of
212    /// what data they will have. Please review wlc's usage of these
213    /// functions before attempting to use them yourself.
214    pub unsafe fn set_user_data<T>(&self, data: &T) {
215        unimplemented!()
216    }
217
218    /// Schedules output for rendering next frame.
219    ///
220    /// If the output was already scheduled, this is
221    /// a no-op; if output is currently rendering,
222    /// it will render immediately after.
223    pub fn schedule_render(&self) {
224    }
225
226    /// Gets a list of the current outputs.
227    ///
228    /// # Safety
229    /// This function will crash the program if run when wlc is not running.
230    pub fn list() -> Vec<WlcOutput> {
231        vec![]
232    }
233
234    /// Gets the currently focused output.
235    ///
236    /// # Safety
237    /// This function will crash the program if run when wlc is not running.
238    pub fn focused() -> WlcOutput {
239        WlcOutput(0)
240    }
241
242    /// Gets the name of the WlcOutput.
243    ///
244    /// Names are usually assigned in the format WLC-n,
245    /// where the first output is WLC-1.
246    pub fn get_name(&self) -> String {
247        "".to_string()
248    }
249
250    /// Gets the sleep status of the output.
251    ///
252    /// Returns `true` if the monitor is sleeping,
253    /// such as having been set with `set_sleep`.
254    pub fn get_sleep(&self) -> bool {
255        false
256    }
257
258    /// Sets the sleep status of the output.
259    pub fn set_sleep(&self, sleep: bool) {
260    }
261
262    /// Gets the output resolution in pixels.
263    pub fn get_resolution(&self) -> Option<Size> {
264        Some(ZERO_RES)
265    }
266
267    /// Sets the resolution of the output.
268    ///
269    /// # Safety
270    /// This method will crash the program if use when wlc is not running.
271    pub fn set_resolution(&self, size: Size, scaling: u32) {
272    }
273
274    /// Get views in stack order.
275    ///
276    /// This is mainly useful for wm's who need another view stack for inplace sorting.
277    /// For example tiling wms, may want to use this to keep their tiling order separated
278    /// from floating order.
279    /// This handles `wlc_output_get_views` and `wlc_output_get_mutable_views`.
280    pub fn get_views(&self) -> Vec<WlcView> {
281        Vec::new()
282    }
283
284    /// Gets the mask of this output
285    pub fn get_mask(&self) -> u32 {
286        0
287    }
288
289    /// Sets the mask for this output
290    pub fn set_mask(&self, mask: u32) {
291    }
292
293    /// # Deprecated
294    /// This function is equivalent to simply calling get_views
295    pub fn get_mutable_views(&self) -> Vec<WlcView> {
296        self.get_views()
297    }
298
299    /// Attempts to set the views of a given output.
300    ///
301    /// Returns success if operation succeeded. An error will be returned
302    /// if something went wrong or if wlc isn't running.
303    pub fn set_views(&self, views: &mut Vec<WlcView>) -> Result<(), &'static str> {
304        Err("Currently running dummy-rustwlc")
305    }
306
307    /// Focuses compositor on a specific output.
308    ///
309    /// Pass in Option::None for no focus.
310    pub fn focus(output: Option<WlcOutput>) {
311    }
312}
313
314impl WlcView {
315
316    /// Compatability/debugging function.
317    ///
318    /// wlc internally stores views and outputs under the same type.
319    /// If for some reason a conversion between the two was required,
320    /// this function could be called. If this is the case please submit
321    /// a bug report.
322    pub fn as_output(self) -> WlcOutput {
323        WlcOutput::from(self)
324    }
325
326    /// Create a dummy WlcView for testing purposes.
327    ///
328    /// # Unsafety
329    /// The following methods on views may crash the program:
330    ///
331    /// - `WlcView::focus` if wlc is not running
332    /// - `WlcView::send_to_back` if wlc is not running
333    /// - `WlcView::send_below` if wlc is not running
334    /// - `WlcView::bring_above` if wlc is not running
335    /// - `WlcView::bring_to_font` if wlc is not running
336    ///
337    /// All other methods can be used on dummy views.
338    ///
339    /// # Note
340    /// `WlcView::root()` is equivalent to `WlcView::dummy(0)`.
341    ///
342    /// ```rust
343    /// # use rustwlc::WlcView;
344    /// assert!(WlcView::root() == WlcView::dummy(0))
345    /// ```
346    /// # Example
347    /// ```rust
348    /// # use rustwlc::WlcView;
349    /// let view = WlcView::dummy(0u32);
350    /// let view2 = WlcView::dummy(1u32);
351    /// assert!(view < view2);
352    /// assert!(view != view2);
353    /// ```
354    pub fn dummy(code: u32) -> WlcView {
355        WlcView(code as uintptr_t)
356    }
357
358    /// Returns a reference to the root window (desktop background).
359    ///
360    /// # Example
361    /// ```
362    /// # use rustwlc::WlcView;
363    /// let view = WlcView::root();
364    /// assert!(view.is_root());
365    /// ```
366    pub fn root() -> WlcView {
367        WlcView(0)
368    }
369
370    /// Whether this view is the root window (desktop background).
371    ///
372    /// # Example
373    /// ```rust
374    /// # use rustwlc::WlcView;
375    /// # // This example can be run because WlcView::root() does not interact with wlc
376    /// let view = WlcView::root();
377    /// assert!(view.is_root());
378    /// ```
379    #[inline]
380    pub fn is_root(&self) -> bool {
381        self.0 == 0
382    }
383
384    /// Whether this view is not the root window (desktop background).
385    ///
386    /// # Usage
387    /// A convenience method, the opposite of `view.is_root()`.
388    ///
389    /// # Example
390    /// ```rust
391    /// # use rustwlc::WlcView;
392    /// let view = WlcView::root();
393    /// assert!(view.is_root());
394    /// assert!(!view.is_window());
395    /// ```
396    #[inline]
397    pub fn is_window(&self) -> bool {
398        self.0 != 0
399    }
400
401    /// Gets user-specified data.
402    ///
403    /// # Unsafety
404    /// The wlc implementation of this method uses `void*` pointers
405    /// for raw C data. This function will internaly do a conversion
406    /// between the input `T` and a `libc::c_void`.
407    ///
408    /// This is a highly unsafe conversion with no guarantees. As
409    /// such, usage of these functions requires an understanding of
410    /// what data they will have. Please review wlc's usage of these
411    /// functions before attempting to use them yourself.
412    pub unsafe fn get_user_data<T>(&self) -> &mut T {
413        unimplemented!()
414    }
415
416    /// Sets user-specified data.
417    ///
418    /// # Unsafety
419    /// The wlc implementation of this method uses `void*` pointers
420    /// for raw C data. This function will internaly do a conversion
421    /// between the input `T` and a `libc::c_void`.
422    ///
423    /// This is a highly unsafe conversion with no guarantees. As
424    /// such, usage of these functions requires an understanding of
425    /// what data they will have. Please review wlc's usage of these
426    /// functions before attempting to use them yourself.
427    pub unsafe fn set_user_data<T>(&self, data: &T) {
428        unimplemented!()
429    }
430
431    /// Closes this view.
432    ///
433    /// For the main windows of most programs, this should close the program where applicable.
434    ///
435    /// # Behavior
436    /// This function will not do anything if `view.is_root()`.
437    pub fn close(self) {
438    }
439
440    /// Gets the WlcOutput this view is currently part of.
441    pub fn get_output(&self) -> WlcOutput {
442        WlcOutput::dummy(0)
443    }
444
445    /// Sets the output that the view renders on.
446    ///
447    /// This may not be supported by wlc at this time.
448    pub fn set_output(&self, output: WlcOutput) {
449    }
450
451    /// Brings this view to focus.
452    ///
453    /// Can be called on `WlcView::root()` to lose all focus.
454    pub fn focus(self) {
455    }
456
457    /// Sends the view to the back of the compositor
458    pub fn send_to_back(&self) {
459    }
460
461    /// Sends this view underneath another.
462    pub fn send_below(&self, other: WlcView) {
463    }
464
465    /// Brings this view above another.
466    pub fn bring_above(&self, other: WlcView) {
467    }
468
469    /// Brings this view to the front of the stack
470    /// within its WlcOutput.
471    pub fn bring_to_front(&self) {
472    }
473
474    // TODO Get masks enum working properly
475    /// Gets the current visibilty bitmask for the view.
476    pub fn get_mask(&self) -> u32 {
477        0
478    }
479
480    // TODO Get masks enum working properly
481    /// Sets the visibilty bitmask for the view.
482    pub fn set_mask(&self, mask: u32) {
483    }
484
485    /// Gets the geometry of the view.
486    pub fn get_geometry(&self) -> Option<Geometry> {
487        Some(Geometry {
488            origin: Point { x: 0, y: 0},
489            size:   Size  { w: 0, h: 0}
490        })
491    }
492
493    /// Gets the geometry of the view (that wlc displays).
494    pub fn get_visible_geometry(&self) -> Geometry {
495        let geo = Geometry { origin: Point { x: 0, y: 0}, size: Size { w: 0, h: 0 }};
496        return geo;
497    }
498
499    /// Sets the geometry of the view.
500    ///
501    /// Set edges if geometry is caused by interactive resize.
502    pub fn set_geometry(&self, edges: ResizeEdge, geometry: Geometry) {
503    }
504
505    /// Gets the type bitfield of the curent view
506    pub fn get_type(&self) -> ViewType {
507        ViewType::empty()
508    }
509
510    /// Set flag in the type field. Toggle indicates whether it is set.
511    pub fn set_type(&self, view_type: ViewType, toggle: bool) {
512    }
513
514    // TODO get bitflags enums
515    /// Get the current ViewState bitfield.
516    pub fn get_state(&self) -> ViewState {
517        ViewState::empty()
518    }
519
520    /// Set ViewState bit. Toggle indicates whether it is set or not.
521    pub fn set_state(&self, state: ViewState, toggle: bool) {
522    }
523
524    /// Gets parent view, returns `WlcView::root()` if this view has no parent.
525    pub fn get_parent(&self) -> WlcView {
526        WlcView::root()
527    }
528
529    /// Set the parent of this view.
530    ///
531    /// Call with `WlcView::root()` to make its parent the root window.
532    pub fn set_parent(&self, parent: WlcView) {
533    }
534
535    /// Get the title of the view
536    pub fn get_title(&self) -> String {
537        "".to_string()
538    }
539
540    /// Get class (shell surface only).
541    pub fn get_class(&self) -> String {
542        "".to_string()
543    }
544
545    pub fn get_pid(self) -> pid_t {
546        0 as pid_t
547    }
548
549    /// Get app id (xdg-surface only).
550    pub fn get_app_id(&self) -> String {
551        "".to_string()
552    }
553}
554
555#[cfg(test)]
556mod tests {
557    use super::super::*;
558
559    #[test]
560    fn dummy_views() {
561        let dummy = WlcView::dummy(1);
562        assert!(!dummy.is_root(), "Dummy(1) is root");
563        assert!(dummy.is_window(), "Dummy(1) is root");
564        let _title = dummy.get_title();
565        let _class = dummy.get_class();
566        let _app_id = dummy.get_app_id();
567        // Let's do some stuff with views
568        dummy.close(); // works
569        let output = dummy.get_output();
570        assert!(output == WlcOutput::dummy(0));
571        dummy.set_output(output);
572        // dummy.focus(); // SEGFAULTS
573        // dummy.send_to_back();
574        // dummy.send_below(&dummy);
575        // dummy.bring_above(&dummy);
576        // dummy.bring_to_front();
577        let mask = dummy.get_mask();
578        dummy.set_mask(mask);
579        let geometry = dummy.get_geometry();
580        dummy.set_geometry(EDGE_NONE, &Geometry {
581            origin: Point { x: 0, y: 0 },
582            size: Size { w: 0, h: 0 }
583        });
584        let view_type = dummy.get_type();
585        assert!(view_type.is_empty(), "Dummy had a view type");
586        dummy.set_type(ViewType::empty(), true);
587        let view_state = dummy.get_state();
588        assert!(view_state.is_empty(), "Dummu had a view state");
589        dummy.set_state(view_state, true);
590        let parent = dummy.get_parent();
591        assert!(parent.is_root(), "Dummy had real parent");
592        dummy.set_parent(parent);
593    }
594
595    #[test]
596    fn dummy_outputs() {
597        let dummy = WlcOutput::dummy(1);
598        //let _current = WlcOutput::focused();
599        //let _outputs = WlcOutput::list();
600        //dummy.set_resolution(resolution.clone());
601        dummy.schedule_render();
602        let _name = dummy.get_name();
603        let sleep = dummy.get_sleep();
604        dummy.set_sleep(sleep);
605        let _resolution = dummy.get_resolution();
606        let mut views = dummy.get_views();
607        dummy.set_views(&mut views).unwrap_err();
608        let mask = dummy.get_mask();
609        dummy.set_mask(mask);
610        WlcOutput::focus(Some(dummy));
611    }
612}