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
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/

//! Traits implementing interfaces to `Coordinator` functionality used to decouple crates using
//! `Coordinator` from its implementation (mainly useful for mocking in unit test).

use std::os::unix::io::RawFd;

use dharma::{EventHandler, EventHandlerId, EventKind};
use graphics::egl_tools::HwImage;
use graphics::attributes::{EglAttributes, DmabufAttributes};

use defs::{DrmBundle, WorkspaceState};
use defs::{DmabufId, EglImageId, MemoryPoolId, MemoryViewId, SignalId, SurfaceId};
use image::PixelFormat;
use memory::{Buffer, Memory};
use perceptron::Perceptron;
use surface::{SurfaceManagement, SurfaceControl, SurfaceViewer};
use surface::{SurfaceAccess, SurfaceListing, SurfaceFocusing};
use transfer::Transfer;

// -------------------------------------------------------------------------------------------------

/// Managing visual appearance;
pub trait AppearanceManagement {
    /// Sets given surface as cursor.
    fn set_surface_as_cursor(&self, sid: SurfaceId);

    /// Sets given surface as background.
    fn set_surface_as_background(&self, sid: SurfaceId);
}

// -------------------------------------------------------------------------------------------------

/// Offering and requesting data transfers (e.g. copy-paste) between clients.
pub trait DataTransferring {
    /// Sets transfer offer.
    fn set_transfer(&mut self, transfer: Option<Transfer>);

    /// Returns transfer offer.
    fn get_transfer(&self) -> Option<Transfer>;

    /// Requests start of data transfer to requesting client.
    fn request_transfer(&mut self, mime_type: String, fd: RawFd);
}

// -------------------------------------------------------------------------------------------------

/// Managing event sources (input devices, notifications from output devices, etc.)
pub trait EventHandling {
    /// Adds new event handler.
    fn add_event_handler(&mut self,
                         event_handler: Box<EventHandler + Send>,
                         event_kind: EventKind)
                         -> EventHandlerId;

    /// Removes new event handler.
    fn remove_event_handler(&mut self, EventHandlerId);
}

// -------------------------------------------------------------------------------------------------

/// Generic communication with the rest of application.
pub trait StatePublishing {
    /// Emits given signal.
    /// TODO: Remove `emit` method.
    fn emit(&mut self, id: SignalId, package: Perceptron);

    /// Notifies about suspending drawing on screen. Probably virtual terminal was switched and GPU
    /// is not available to us.
    fn suspend(&mut self);

    /// Sends request to revoke application from suspension.
    fn wakeup(&mut self);

    /// Sends notification about changing of state of input devices.
    fn input_devices_changed(&mut self);

    /// Sends notification about changing of state of output devices.
    fn output_devices_changed(&mut self);

    /// Notifies application about event that requires screen to be refreshed.
    /// TODO: Rename `notify` to `refresh`.
    fn notify(&mut self);

    /// Publishes newly found output.
    fn publish_output(&mut self, drm_budle: DrmBundle);

    /// Notifies about V-blank.
    fn emit_vblank(&mut self, display_id: i32);

    /// Notifies about page flip.
    fn emit_page_flip(&mut self, display_id: i32);
}

// -------------------------------------------------------------------------------------------------

/// Managing memory pools and views.
pub trait MemoryManagement {
    /// Creates new memory pool from mapped memory. Returns ID of newly created pool.
    fn create_memory_pool(&mut self, memory: Memory) -> MemoryPoolId;

    /// Schedules destruction of memory pool identified by given ID. The pool will be destructed
    /// when all its views go out of the scope.
    ///
    /// If the poll was created from mapped memory, returns this memory.
    fn destroy_memory_pool(&mut self, mpid: MemoryPoolId) -> Option<Memory>;

    /// Replaces mapped memory with other memory reusing its ID. This method may be used when
    /// client requests memory map resize.
    fn replace_memory_pool(&mut self, mpid: MemoryPoolId, memory: Memory);

    /// Creates new memory view from mapped memory.
    fn create_memory_view(&mut self,
                          mpid: MemoryPoolId,
                          format: PixelFormat,
                          offset: usize,
                          width: usize,
                          height: usize,
                          stride: usize)
                          -> Option<MemoryViewId>;

    /// Destroys memory view.
    fn destroy_memory_view(&mut self, mpid: MemoryViewId);
}

// -------------------------------------------------------------------------------------------------

/// Hardware accelerated graphics functionality.
pub trait HwGraphics {
    /// Sets graphics manager.
    fn set_graphics_manager(&mut self, graphics_manager: Box<GraphicsManagement + Send>);

    /// Checks if hardware acceleration support is available.
    fn has_hardware_acceleration_support(&self) -> bool;

    /// Makes request to create EGL buffer.
    fn create_egl_image(&mut self, attrs: EglAttributes) -> Option<EglImageId>;

    /// Requests destruction of hardware image.
    fn destroy_egl_image(&mut self, eiid: EglImageId);

    /// Makes request to create EGL buffer from dmabuf.
    fn import_dmabuf(&mut self, attrs: DmabufAttributes) -> Option<DmabufId>;

    /// Requests destruction of dmabuf.
    fn destroy_dmabuf(&mut self, dbid: DmabufId);
}

// -------------------------------------------------------------------------------------------------

/// Setting or getting window management info.
pub trait WindowManagement {
    /// Sets workspace state.
    fn set_workspace_state(&mut self, state: WorkspaceState);

    /// Returns workspace state.
    fn get_workspace_state(&self) -> WorkspaceState;
}

// -------------------------------------------------------------------------------------------------

/// Screenshooting related functionality.
pub trait Screenshooting {
    /// Makes screenshot request.
    fn take_screenshot(&mut self, id: i32);

    /// Sets given buffer as results of screenshot.
    fn set_screenshot_buffer(&mut self, buffer: Buffer);

    /// Returns and forgets screenshot buffer.
    fn take_screenshot_buffer(&mut self) -> Option<Buffer>;
}

// -------------------------------------------------------------------------------------------------

/// Trait every graphics manager should implement.
///
/// Graphics manager is peace of code abstracting hardware image creation.
pub trait GraphicsManagement {
    /// Creates EGL image from given parameters.
    fn create_egl_image(&mut self, attrs: &EglAttributes) -> Option<HwImage>;

    /// Imports dmabuf as EGL image.
    fn import_dmabuf(&mut self, attrs: &DmabufAttributes) -> Option<HwImage>;

    /// Destroys given hardware image.
    fn destroy_hw_image(&mut self, image: HwImage) -> Result<(), ()>;
}

// -------------------------------------------------------------------------------------------------

/// Helper trait gathering traits used by `Aesthetics`.
pub trait AestheticsCoordinationTrait: SurfaceControl +
                                       SurfaceManagement +
                                       AppearanceManagement +
                                       MemoryManagement +
                                       WindowManagement {}

// -------------------------------------------------------------------------------------------------

/// Helper trait gathering traits used by `Exhibitor`. Keeping list of all traits in all
/// implementations is too verbose so this trait was introduced as best for now solution.
pub trait ExhibitorCoordinationTrait: SurfaceControl +
                                      SurfaceViewer +
                                      SurfaceAccess +
                                      SurfaceListing +
                                      SurfaceFocusing +
                                      StatePublishing +
                                      Screenshooting +
                                      WindowManagement {}

// -------------------------------------------------------------------------------------------------

/// Helper trait gathering traits used by frontends. Keeping list of all traits in all
/// implementations is too verbose so this trait was introduced as best for now solution.
pub trait FrontendsCoordinationTrait: SurfaceManagement +
                                      SurfaceControl +
                                      SurfaceViewer +
                                      SurfaceFocusing +
                                      AppearanceManagement +
                                      DataTransferring +
                                      HwGraphics +
                                      Screenshooting +
                                      MemoryManagement {}

// -------------------------------------------------------------------------------------------------