PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
display.h
Go to the documentation of this file.
1#ifndef PATCHWORK_DISPLAY_H
2#define PATCHWORK_DISPLAY_H 1
3
4#include "cmd.h"
5#include "event.h"
6#include "rect.h"
7
8#include <stdbool.h>
9#include <stdint.h>
10#include <sys/proc.h>
11
12#if defined(__cplusplus)
13extern "C"
14{
15#endif
16
17/**
18 * @brief Display connection.
19 * @defgroup libpatchwork_display Display Connection
20 * @ingroup libpatchwork
21 *
22 * A display represents a connection to the Desktop Window Manager (DWM).
23 *
24 * The display system IS thread safe, but the rest of the Patchwork library may not be. The display connection can thus
25 * be used to synchronize multiple threads when working with windows, etc.
26 *
27 * There are two things of note for performance when using a display. First, commands are batched and sent together when
28 * `display_cmds_flush()` is called. Second, the display uses a internal pipe to store events that cant be processed
29 * immediately and this means that if a thread calls `display_push()` another thread blocking on `display_next()` will
30 * unblock since its blocking on both the displays DWM connection and the internal pipe.
31 *
32 * @{
33 */
34
35/**
36 * @brief Opaque display structure.
37 * @struct display_t
38 */
39typedef struct display display_t;
40
41/**
42 * @brief Create a new display connection.
43 *
44 * @return On success, a The display connection. On failure, `NULL` and `errno` is set.
45 */
47
48/**
49 * @brief Free a display connection.
50 *
51 * @param disp The display connection.
52 */
53void display_free(display_t* disp);
54
55/**
56 * @brief Check if the display connection is still connected.
57 *
58 * @param disp The display connection.
59 * @return `true` if connected, `false` otherwise.
60 */
62
63/**
64 * @brief Disconnect the display connection.
65 *
66 * After calling this function, the display connection will be marked as disconnected and no further commands or
67 * events will be processed.
68 *
69 * Will not free the display connection, use `display_free()` for that.
70 *
71 * @param disp The display connection.
72 */
74
75/**
76 * @brief Allocate a section of the displays command buffer.
77 *
78 * The display batches commands together in its command buffer, where each command is prefixed with a `cmd_header_t` and
79 * has a variable size.
80 *
81 * @param disp The display connection.
82 * @param type Type of command to allocate.
83 * @param size Size of the command data to allocate.
84 * @return Pointer to the allocated command data, or `NULL` on failure and `errno` is set.
85 */
86void* display_cmd_alloc(display_t* disp, cmd_type_t type, uint64_t size);
87
88/**
89 * @brief Flush the display's command buffer.
90 *
91 * This will send all queued commands to the DWM.
92 *
93 * @param disp The display connection.
94 */
96
97/**
98 * @brief Retrieve the next event from the display connection.
99 *
100 * @param disp The display connection.
101 * @param event Output pointer to store the retrieved event.
102 * @param timeout Maximum time to wait for an event, if `CLOCKS_NEVER` will wait indefinitely.
103 * @return On success, `0`. On failure, `ERR` and `errno` is set.
104 */
105uint64_t display_next(display_t* disp, event_t* event, clock_t timeout);
106
107/**
108 * @brief Poll the display connection for events together with other file descriptors.
109 *
110 * @param disp The display connection.
111 * @param fds Array of file descriptors to poll alongside the display connection.
112 * @param nfds Number of file descriptors in the array.
113 * @param timeout Maximum time to wait for an event, if `CLOCKS_NEVER` will wait indefinitely.
114 * @return On success, number of file descriptors with events (not including the display connection). On failure,
115 * returns `ERR` and `errno` is set.
116 */
117uint64_t display_poll(display_t* disp, pollfd_t* fds, uint64_t nfds, clock_t timeout);
118
119/**
120 * @brief Push an event to the display's internal event queue.
121 *
122 * This will not send the event to the DWM, instead it will be stored in the display's internal event queue and can be
123 * retrieved using `display_next()`.
124 *
125 * @param disp The display connection.
126 * @param target Target surface ID for the event.
127 * @param type Type of event.
128 * @param data Pointer to the event data, can be `NULL` if `size` is `0`.
129 * @param size Size of the event data, must be less than `EVENT_MAX_DATA`.
130 */
131void display_push(display_t* disp, surface_id_t target, event_type_t type, void* data, uint64_t size);
132
133/**
134 * @brief Wait for the display to receive an event of the expected type.
135 *
136 * This function will block until an event of the expected type is received.
137 *
138 * Any other events received while waiting will be pushed back to the display's internal event queue.
139 *
140 * @param disp The display connection.
141 * @param event Output pointer to store the retrieved event.
142 * @param expected The expected event type to wait for.
143 * @return On success, `0`. On failure, `ERR` and `errno` is set.
144 */
145uint64_t display_wait(display_t* disp, event_t* event, event_type_t expected);
146
147/**
148 * @brief Emit an event to a target surface.
149 *
150 * This function will construct an event and dispatch it to the target surface.
151 *
152 * @param disp The display connection.
153 * @param target Target surface ID for the event, if `SURFACE_ID_NONE` the event is sent to all surfaces.
154 * @param type Type of event.
155 * @param data Pointer to the event data, can be `NULL` if `size` is `0`.
156 * @param size Size of the event data, must be less than `EVENT_MAX_DATA`.
157 * @return On success, `0`. On failure, `ERR` and `errno` is set.
158 */
159uint64_t display_emit(display_t* disp, surface_id_t target, event_type_t type, void* data, uint64_t size);
160
161/**
162 * @brief Dispatch an event to the appropriate surface.
163 *
164 * Will flush the display's command buffer after dispatching the event.
165 *
166 * @param disp The display connection.
167 * @param event The event to dispatch.
168 * @return On success, `0`. On failure, `ERR` and `errno` is set.
169 */
170uint64_t display_dispatch(display_t* disp, const event_t* event);
171
172/**
173 * @brief Dispatch all events currently in the display's internal event queue of a specific type and target.
174 *
175 * @param disp The display connection.
176 * @param type The event type to check for.
177 * @param target The target surface ID for the events, if `SURFACE_ID_NONE` all events are dispatched.
178 * @return On success, `0`. On failure, `ERR` and `errno` is set.
179 */
181
182/**
183 * @brief Subscribe to events of a specific type.
184 *
185 * Should only be used for events sent by the DWM.
186 *
187 * @param disp The display connection.
188 * @param type The event type to subscribe to.
189 */
191
192/**
193 * @brief Unsubscribe from events of a specific type.
194 *
195 * Should only be used for events sent by the DWM.
196 *
197 * @param disp The display connection.
198 * @param type The event type to unsubscribe from.
199 */
201
202/**
203 * @brief Get information about a surface.
204 *
205 * Uses a `CMD_SURFACE_REPORT` command to request information about the specified surface from the DWM.
206 *
207 * @param disp The display connection.
208 * @param id The surface ID to query.
209 * @param info Output pointer to store the surface information.
210 * @return On success, `0`. On failure, `ERR` and `errno` is set.
211 */
213
214/**
215 * @brief Set the focus to a surface.
216 *
217 * Can apply to any surface, not just ones owned by the client.
218 *
219 * @param disp The display connection.
220 * @param id The surface ID to set focus to.
221 */
223
224/**
225 * @brief Set the visibility of a surface.
226 *
227 * Can apply to any surface, not just ones owned by the client.
228 *
229 * @param disp The display connection.
230 * @param id The surface ID to set visibility for.
231 * @param isVisible Whether the surface should be visible.
232 */
233uint64_t display_set_is_visible(display_t* disp, surface_id_t id, bool isVisible);
234
235/**
236 * @brief Get the rectangle of a screen.
237 *
238 * @param disp The display connection.
239 * @param rect Output pointer to store the rectangle of the screen.
240 * @param index Index of the screen to query, only `0` is supported currently.
241 * @return On success, `0`. On failure, `ERR` and `errno` is set.
242 */
244
245/** @} */
246
247#if defined(__cplusplus)
248}
249#endif
250
251#endif
cmd_type_t
Definition cmd.h:23
static fd_t data
Definition dwm.c:21
void display_disconnect(display_t *disp)
Disconnect the display connection.
Definition display.c:168
uint64_t display_poll(display_t *disp, pollfd_t *fds, uint64_t nfds, clock_t timeout)
Poll the display connection for events together with other file descriptors.
Definition display.c:295
void * display_cmd_alloc(display_t *disp, cmd_type_t type, uint64_t size)
Allocate a section of the displays command buffer.
Definition display.c:180
uint64_t display_next(display_t *disp, event_t *event, clock_t timeout)
Retrieve the next event from the display connection.
Definition display.c:220
uint64_t display_unsubscribe(display_t *disp, event_type_t type)
Unsubscribe from events of a specific type.
Definition display.c:535
void display_cmds_flush(display_t *disp)
Flush the display's command buffer.
Definition display.c:205
uint64_t display_dispatch(display_t *disp, const event_t *event)
Dispatch an event to the appropriate surface.
Definition display.c:447
void display_push(display_t *disp, surface_id_t target, event_type_t type, void *data, uint64_t size)
Push an event to the display's internal event queue.
Definition display.c:356
uint64_t display_wait(display_t *disp, event_t *event, event_type_t expected)
Wait for the display to receive an event of the expected type.
Definition display.c:374
uint64_t display_get_screen(display_t *disp, rect_t *rect, uint64_t index)
Get the rectangle of a screen.
Definition display.c:610
void display_free(display_t *disp)
Free a display connection.
Definition display.c:119
uint64_t display_set_focus(display_t *disp, surface_id_t id)
Set the focus to a surface.
Definition display.c:581
bool display_is_connected(display_t *disp)
Check if the display connection is still connected.
Definition display.c:155
uint64_t display_emit(display_t *disp, surface_id_t target, event_type_t type, void *data, uint64_t size)
Emit an event to a target surface.
Definition display.c:426
uint64_t display_dispatch_pending(display_t *disp, event_type_t type, surface_id_t target)
Dispatch all events currently in the display's internal event queue of a specific type and target.
Definition display.c:479
display_t * display_new(void)
Create a new display connection.
Definition display.c:36
uint64_t display_get_surface_info(display_t *disp, surface_id_t id, surface_info_t *info)
Get information about a surface.
Definition display.c:554
uint64_t display_subscribe(display_t *disp, event_type_t type)
Subscribe to events of a specific type.
Definition display.c:516
uint64_t display_set_is_visible(display_t *disp, surface_id_t id, bool isVisible)
Set the visibility of a surface.
Definition display.c:595
uint16_t event_type_t
Event type.
Definition event.h:72
uint64_t surface_id_t
Definition surface.h:53
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static fb_info_t info
Definition gop.c:51
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Opaque display structure.
Definition internal.h:61
Event structure.
Definition event.h:271
Poll file descriptor structure.
Definition io.h:276
Definition rect.h:13