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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! Contains definitions for wlc handle types.
//!
//! # Implementations
//! - **Debug**: pointer-prints the underlying `uintptr_t` handle
//! - **Eq, Ord**: compare the underlying `uintptr_t` handle
//! - **Clone**: View handles can safely be cloned.

use libc;
use libc::*;
use wayland_sys::common::*;
use wayland_sys::server::*;

extern "C" {
    pub fn wlc_get_outputs(memb: *mut libc::size_t) -> *const libc::uintptr_t;

    pub fn wlc_get_focused_output() -> uintptr_t;

    pub fn wlc_output_get_name(output: uintptr_t) -> *const c_char;

    pub fn wlc_handle_get_user_data(handle: uintptr_t) -> *mut c_void;

    pub fn wlc_handle_set_user_data(handle: uintptr_t, userdata: *const c_void);

    pub fn wlc_output_get_sleep(output: uintptr_t) -> bool;

    pub fn wlc_output_set_sleep(output: uintptr_t, sleep: bool);

    pub fn wlc_output_get_resolution(output: uintptr_t) -> *const Size;

    pub fn wlc_output_set_resolution(output: uintptr_t, resolution: *const Size, scale: uint32_t);

    pub fn wlc_output_get_scale(output: uintptr_t) -> uint32_t;

    pub fn wlc_output_get_virtual_resolution(output: uintptr_t) -> *const Size;

    pub fn wlc_output_get_mask(output: uintptr_t) -> u32;

    pub fn wlc_output_set_mask(output: uintptr_t, mask: u32);

    // TODO tricky definition here
    //pub fn wlc_output_get_pixels(output: WlcHandle) -> ();

    pub fn wlc_output_get_views(output: uintptr_t,
                            out_memb: *mut libc::size_t) -> *const uintptr_t;

    pub fn wlc_output_set_views(output: uintptr_t, views: *const uintptr_t, memb: libc::size_t) -> bool;

    pub fn wlc_output_focus(output: uintptr_t);

    // View API

    pub fn wlc_view_focus(view: uintptr_t);

    pub fn wlc_view_close(view: uintptr_t);

    // View -> Output
    pub fn wlc_view_get_output(view: uintptr_t) -> uintptr_t;

    // "set output. Alternatively you can use wlc_output_set_views"
    pub fn wlc_view_set_output(view: uintptr_t, output: uintptr_t);

    pub fn wlc_view_send_to_back(view: uintptr_t);

    pub fn wlc_view_send_below(view: uintptr_t, other: uintptr_t);

    pub fn wlc_view_bring_above(view: uintptr_t, other: uintptr_t);

    pub fn wlc_view_bring_to_front(view: uintptr_t);

    pub fn wlc_view_get_mask(view: uintptr_t) -> u32;

    pub fn wlc_view_set_mask(view: uintptr_t, mask: u32);

    pub fn wlc_view_get_geometry(view: uintptr_t) -> *const Geometry;

    pub fn wlc_view_get_visible_geometry(view: uintptr_t, geo: *mut Geometry);

    pub fn wlc_view_set_geometry(view: uintptr_t, edges: u32, geo: *const Geometry);

    pub fn wlc_view_get_type(view: uintptr_t) -> ViewType;

    pub fn wlc_view_set_type(view: uintptr_t, view_type: ViewType, toggle: bool);

    pub fn wlc_view_get_state(view: uintptr_t) -> ViewState;

    pub fn wlc_view_set_state(view: uintptr_t, state: ViewState, toggle: bool);

    // Parent is Option<View>
    pub fn wlc_view_get_parent(view: uintptr_t) -> uintptr_t;

    // Parent is Option<View>
    pub fn wlc_view_set_parent(view: uintptr_t, parent: uintptr_t);

    pub fn wlc_view_get_title(view: uintptr_t) -> *const c_char;

    pub fn wlc_view_get_class(view: uintptr_t) -> *const c_char;

    pub fn wlc_view_get_app_id(view: uintptr_t) -> *const c_char;

    pub fn wlc_view_get_pid(view: uintptr_t)-> pid_t;

    pub fn wlc_handle_from_wl_surface_resource(resource: *const wl_resource) -> uintptr_t;

    pub fn wlc_handle_from_wl_output_resource(resource: *const wl_resource) -> uintptr_t;

    pub fn wlc_view_get_surface(view: uintptr_t) -> uintptr_t;

    pub fn wlc_view_get_wl_client(view: uintptr_t) -> *mut wl_client;

    pub fn wlc_view_get_role(view: uintptr_t) -> *mut wl_resource;

    pub fn wlc_view_from_surface(surface: uintptr_t, client: *const wl_client, interface: *const wl_interface,
                             implementation: *const c_void, version: uint32_t, id: uint32_t, userdata: *mut c_void)
                             -> uintptr_t;
}
use libc::{uintptr_t, pid_t};

use super::types::{Geometry, ResizeEdge, Point, Size, ViewType, ViewState};

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Represents a handle to a wlc view.
///
pub struct WlcView(pub uintptr_t);

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Represents a handle to a wlc output.
pub struct WlcOutput(pub uintptr_t);

impl From<WlcView> for WlcOutput {
    fn from(view: WlcView) -> Self {
        WlcOutput(view.0)
    }
}

impl From<WlcOutput> for WlcView {
    fn from(output: WlcOutput) -> Self {
        WlcView(output.0)
    }
}

static ZERO_RES: Size = Size { w: 0, h: 0 };

impl WlcOutput {

    /// Compatability/debugging function.
    ///
    /// wlc internally stores views and outputs under the same type.
    /// If for some reason a conversion between the two was required,
    /// this function could be called. If this is the case please submit
    /// a bug report.
    pub fn as_view(self) -> WlcView {
        return WlcView::from(self)
    }

    /// Determines if this is a null a output (invalid).
    #[inline]
    pub fn is_null(self) -> bool {
        self.0 == 0
    }

    /// Create a dummy WlcOutput for testing purposes.
    ///
    /// # Unsafety
    /// The following operations on a dummy WlcOutput will cause crashes:
    ///
    /// - `WlcOutput::focused` when wlc is not running
    /// - `WlcOutput::list` when wlc is not running
    /// - `WlcOutput::set_resolution` on a dummy output
    ///
    /// In addition, `WlcOutput::set_views` will return an error.
    ///
    /// All other methods can be used on dummy outputs.
    ///
    /// # Example
    /// ```rust
    /// # use rustwlc::WlcOutput;
    /// let output = WlcOutput::dummy(0u32);
    /// let output2 = WlcOutput::dummy(1u32);
    /// assert!(output < output2);
    /// assert!(output != output2);
    /// ```
    pub fn dummy(code: u32) -> WlcOutput {
        WlcOutput(code as uintptr_t)
    }

    /// Gets user-specified data.
    ///
    /// # Unsafety
    /// The wlc implementation of this method uses `void*` pointers
    /// for raw C data. This function will internaly do a conversion
    /// between the input `T` and a `libc::c_void`.
    ///
    /// This is a highly unsafe conversion with no guarantees. As
    /// such, usage of these functions requires an understanding of
    /// what data they will have. Please review wlc's usage of these
    /// functions before attempting to use them yourself.
    pub unsafe fn get_user_data<T>(&self) -> &mut T {
        unimplemented!()
    }

    /// Sets user-specified data.
    ///
    /// # Unsafety
    /// The wlc implementation of this method uses `void*` pointers
    /// for raw C data. This function will internaly do a conversion
    /// between the input `T` and a `libc::c_void`.
    ///
    /// This is a highly unsafe conversion with no guarantees. As
    /// such, usage of these functions requires an understanding of
    /// what data they will have. Please review wlc's usage of these
    /// functions before attempting to use them yourself.
    pub unsafe fn set_user_data<T>(&self, data: &T) {
        unimplemented!()
    }

    /// Schedules output for rendering next frame.
    ///
    /// If the output was already scheduled, this is
    /// a no-op; if output is currently rendering,
    /// it will render immediately after.
    pub fn schedule_render(&self) {
    }

    /// Gets a list of the current outputs.
    ///
    /// # Safety
    /// This function will crash the program if run when wlc is not running.
    pub fn list() -> Vec<WlcOutput> {
        vec![]
    }

    /// Gets the currently focused output.
    ///
    /// # Safety
    /// This function will crash the program if run when wlc is not running.
    pub fn focused() -> WlcOutput {
        WlcOutput(0)
    }

    /// Gets the name of the WlcOutput.
    ///
    /// Names are usually assigned in the format WLC-n,
    /// where the first output is WLC-1.
    pub fn get_name(&self) -> String {
        "".to_string()
    }

    /// Gets the sleep status of the output.
    ///
    /// Returns `true` if the monitor is sleeping,
    /// such as having been set with `set_sleep`.
    pub fn get_sleep(&self) -> bool {
        false
    }

    /// Sets the sleep status of the output.
    pub fn set_sleep(&self, sleep: bool) {
    }

    /// Gets the output resolution in pixels.
    pub fn get_resolution(&self) -> Option<Size> {
        Some(ZERO_RES)
    }

    /// Sets the resolution of the output.
    ///
    /// # Safety
    /// This method will crash the program if use when wlc is not running.
    pub fn set_resolution(&self, size: Size, scaling: u32) {
    }

    /// Get views in stack order.
    ///
    /// This is mainly useful for wm's who need another view stack for inplace sorting.
    /// For example tiling wms, may want to use this to keep their tiling order separated
    /// from floating order.
    /// This handles `wlc_output_get_views` and `wlc_output_get_mutable_views`.
    pub fn get_views(&self) -> Vec<WlcView> {
        Vec::new()
    }

    /// Gets the mask of this output
    pub fn get_mask(&self) -> u32 {
        0
    }

    /// Sets the mask for this output
    pub fn set_mask(&self, mask: u32) {
    }

    /// # Deprecated
    /// This function is equivalent to simply calling get_views
    pub fn get_mutable_views(&self) -> Vec<WlcView> {
        self.get_views()
    }

    /// Attempts to set the views of a given output.
    ///
    /// Returns success if operation succeeded. An error will be returned
    /// if something went wrong or if wlc isn't running.
    pub fn set_views(&self, views: &mut Vec<WlcView>) -> Result<(), &'static str> {
        Err("Currently running dummy-rustwlc")
    }

    /// Focuses compositor on a specific output.
    ///
    /// Pass in Option::None for no focus.
    pub fn focus(output: Option<WlcOutput>) {
    }
}

impl WlcView {

    /// Compatability/debugging function.
    ///
    /// wlc internally stores views and outputs under the same type.
    /// If for some reason a conversion between the two was required,
    /// this function could be called. If this is the case please submit
    /// a bug report.
    pub fn as_output(self) -> WlcOutput {
        WlcOutput::from(self)
    }

    /// Create a dummy WlcView for testing purposes.
    ///
    /// # Unsafety
    /// The following methods on views may crash the program:
    ///
    /// - `WlcView::focus` if wlc is not running
    /// - `WlcView::send_to_back` if wlc is not running
    /// - `WlcView::send_below` if wlc is not running
    /// - `WlcView::bring_above` if wlc is not running
    /// - `WlcView::bring_to_font` if wlc is not running
    ///
    /// All other methods can be used on dummy views.
    ///
    /// # Note
    /// `WlcView::root()` is equivalent to `WlcView::dummy(0)`.
    ///
    /// ```rust
    /// # use rustwlc::WlcView;
    /// assert!(WlcView::root() == WlcView::dummy(0))
    /// ```
    /// # Example
    /// ```rust
    /// # use rustwlc::WlcView;
    /// let view = WlcView::dummy(0u32);
    /// let view2 = WlcView::dummy(1u32);
    /// assert!(view < view2);
    /// assert!(view != view2);
    /// ```
    pub fn dummy(code: u32) -> WlcView {
        WlcView(code as uintptr_t)
    }

    /// Returns a reference to the root window (desktop background).
    ///
    /// # Example
    /// ```
    /// # use rustwlc::WlcView;
    /// let view = WlcView::root();
    /// assert!(view.is_root());
    /// ```
    pub fn root() -> WlcView {
        WlcView(0)
    }

    /// Whether this view is the root window (desktop background).
    ///
    /// # Example
    /// ```rust
    /// # use rustwlc::WlcView;
    /// # // This example can be run because WlcView::root() does not interact with wlc
    /// let view = WlcView::root();
    /// assert!(view.is_root());
    /// ```
    #[inline]
    pub fn is_root(&self) -> bool {
        self.0 == 0
    }

    /// Whether this view is not the root window (desktop background).
    ///
    /// # Usage
    /// A convenience method, the opposite of `view.is_root()`.
    ///
    /// # Example
    /// ```rust
    /// # use rustwlc::WlcView;
    /// let view = WlcView::root();
    /// assert!(view.is_root());
    /// assert!(!view.is_window());
    /// ```
    #[inline]
    pub fn is_window(&self) -> bool {
        self.0 != 0
    }

    /// Gets user-specified data.
    ///
    /// # Unsafety
    /// The wlc implementation of this method uses `void*` pointers
    /// for raw C data. This function will internaly do a conversion
    /// between the input `T` and a `libc::c_void`.
    ///
    /// This is a highly unsafe conversion with no guarantees. As
    /// such, usage of these functions requires an understanding of
    /// what data they will have. Please review wlc's usage of these
    /// functions before attempting to use them yourself.
    pub unsafe fn get_user_data<T>(&self) -> &mut T {
        unimplemented!()
    }

    /// Sets user-specified data.
    ///
    /// # Unsafety
    /// The wlc implementation of this method uses `void*` pointers
    /// for raw C data. This function will internaly do a conversion
    /// between the input `T` and a `libc::c_void`.
    ///
    /// This is a highly unsafe conversion with no guarantees. As
    /// such, usage of these functions requires an understanding of
    /// what data they will have. Please review wlc's usage of these
    /// functions before attempting to use them yourself.
    pub unsafe fn set_user_data<T>(&self, data: &T) {
        unimplemented!()
    }

    /// Closes this view.
    ///
    /// For the main windows of most programs, this should close the program where applicable.
    ///
    /// # Behavior
    /// This function will not do anything if `view.is_root()`.
    pub fn close(self) {
    }

    /// Gets the WlcOutput this view is currently part of.
    pub fn get_output(&self) -> WlcOutput {
        WlcOutput::dummy(0)
    }

    /// Sets the output that the view renders on.
    ///
    /// This may not be supported by wlc at this time.
    pub fn set_output(&self, output: WlcOutput) {
    }

    /// Brings this view to focus.
    ///
    /// Can be called on `WlcView::root()` to lose all focus.
    pub fn focus(self) {
    }

    /// Sends the view to the back of the compositor
    pub fn send_to_back(&self) {
    }

    /// Sends this view underneath another.
    pub fn send_below(&self, other: WlcView) {
    }

    /// Brings this view above another.
    pub fn bring_above(&self, other: WlcView) {
    }

    /// Brings this view to the front of the stack
    /// within its WlcOutput.
    pub fn bring_to_front(&self) {
    }

    // TODO Get masks enum working properly
    /// Gets the current visibilty bitmask for the view.
    pub fn get_mask(&self) -> u32 {
        0
    }

    // TODO Get masks enum working properly
    /// Sets the visibilty bitmask for the view.
    pub fn set_mask(&self, mask: u32) {
    }

    /// Gets the geometry of the view.
    pub fn get_geometry(&self) -> Option<Geometry> {
        Some(Geometry {
            origin: Point { x: 0, y: 0},
            size:   Size  { w: 0, h: 0}
        })
    }

    /// Gets the geometry of the view (that wlc displays).
    pub fn get_visible_geometry(&self) -> Geometry {
        let geo = Geometry { origin: Point { x: 0, y: 0}, size: Size { w: 0, h: 0 }};
        return geo;
    }

    /// Sets the geometry of the view.
    ///
    /// Set edges if geometry is caused by interactive resize.
    pub fn set_geometry(&self, edges: ResizeEdge, geometry: Geometry) {
    }

    /// Gets the type bitfield of the curent view
    pub fn get_type(&self) -> ViewType {
        ViewType::empty()
    }

    /// Set flag in the type field. Toggle indicates whether it is set.
    pub fn set_type(&self, view_type: ViewType, toggle: bool) {
    }

    // TODO get bitflags enums
    /// Get the current ViewState bitfield.
    pub fn get_state(&self) -> ViewState {
        ViewState::empty()
    }

    /// Set ViewState bit. Toggle indicates whether it is set or not.
    pub fn set_state(&self, state: ViewState, toggle: bool) {
    }

    /// Gets parent view, returns `WlcView::root()` if this view has no parent.
    pub fn get_parent(&self) -> WlcView {
        WlcView::root()
    }

    /// Set the parent of this view.
    ///
    /// Call with `WlcView::root()` to make its parent the root window.
    pub fn set_parent(&self, parent: WlcView) {
    }

    /// Get the title of the view
    pub fn get_title(&self) -> String {
        "".to_string()
    }

    /// Get class (shell surface only).
    pub fn get_class(&self) -> String {
        "".to_string()
    }

    pub fn get_pid(self) -> pid_t {
        0 as pid_t
    }

    /// Get app id (xdg-surface only).
    pub fn get_app_id(&self) -> String {
        "".to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::super::*;

    #[test]
    fn dummy_views() {
        let dummy = WlcView::dummy(1);
        assert!(!dummy.is_root(), "Dummy(1) is root");
        assert!(dummy.is_window(), "Dummy(1) is root");
        let _title = dummy.get_title();
        let _class = dummy.get_class();
        let _app_id = dummy.get_app_id();
        // Let's do some stuff with views
        dummy.close(); // works
        let output = dummy.get_output();
        assert!(output == WlcOutput::dummy(0));
        dummy.set_output(output);
        // dummy.focus(); // SEGFAULTS
        // dummy.send_to_back();
        // dummy.send_below(&dummy);
        // dummy.bring_above(&dummy);
        // dummy.bring_to_front();
        let mask = dummy.get_mask();
        dummy.set_mask(mask);
        let geometry = dummy.get_geometry();
        dummy.set_geometry(EDGE_NONE, &Geometry {
            origin: Point { x: 0, y: 0 },
            size: Size { w: 0, h: 0 }
        });
        let view_type = dummy.get_type();
        assert!(view_type.is_empty(), "Dummy had a view type");
        dummy.set_type(ViewType::empty(), true);
        let view_state = dummy.get_state();
        assert!(view_state.is_empty(), "Dummu had a view state");
        dummy.set_state(view_state, true);
        let parent = dummy.get_parent();
        assert!(parent.is_root(), "Dummy had real parent");
        dummy.set_parent(parent);
    }

    #[test]
    fn dummy_outputs() {
        let dummy = WlcOutput::dummy(1);
        //let _current = WlcOutput::focused();
        //let _outputs = WlcOutput::list();
        //dummy.set_resolution(resolution.clone());
        dummy.schedule_render();
        let _name = dummy.get_name();
        let sleep = dummy.get_sleep();
        dummy.set_sleep(sleep);
        let _resolution = dummy.get_resolution();
        let mut views = dummy.get_views();
        dummy.set_views(&mut views).unwrap_err();
        let mask = dummy.get_mask();
        dummy.set_mask(mask);
        WlcOutput::focus(Some(dummy));
    }
}