GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_backend.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos
4 * <jiri.bluebear.dluhos@gmail.com>
5 *
6 * Copyright (C) 2009-2024 Cyril Hrubis <metan@ucw.cz>
7 */
8
23#ifndef BACKENDS_GP_BACKEND_H
24#define BACKENDS_GP_BACKEND_H
25
26#include <core/gp_types.h>
27
28#include <utils/gp_timer.h>
29#include <utils/gp_list.h>
30#include <utils/gp_poll.h>
31
32#include <input/gp_ev_queue.h>
33#include <input/gp_task.h>
34
35#include <backends/gp_types.h>
36
37enum gp_backend_attrs {
38 /* window size */
39 GP_BACKEND_SIZE,
40 /* window title */
41 GP_BACKEND_TITLE,
42 /* fullscreen mode */
43 GP_BACKEND_FULLSCREEN,
44};
45
66
72struct gp_backend {
82
84 const char *name;
85
94 void (*flip)(gp_backend *self);
95
104 void (*update_rect)(gp_backend *self,
105 gp_coord x0, gp_coord y0,
106 gp_coord x1, gp_coord y1);
107
108 /*
109 * Attribute change callback.
110 *
111 * The vals is supposed to be:
112 *
113 * GP_BACKEND_SIZE uint32_t vals[2];
114 * GP_BACKEND_TITLE const char *title
115 * GP_BACKEND_FULLSCREEN int *fs
116 */
117 int (*set_attr)(gp_backend *self, enum gp_backend_attrs attr,
118 const void *vals);
119
125 int (*set_cursor)(gp_backend *self, enum gp_backend_cursors cursor);
126
127 /*
128 * Resize acknowledge callback. This must be called
129 * after you got resize event in order to resize
130 * backend buffers.
131 */
132 int (*resize_ack)(gp_backend *self);
133
137 void (*exit)(gp_backend *self);
138
145 void (*poll)(gp_backend *self);
146
147 /*
148 * Clipboard handler.
149 */
150 int (*clipboard)(gp_backend *self, gp_clipboard *op);
151
162 void (*wait)(gp_backend *self);
163
166
167 /* @brief Queue to store input events. */
168 gp_ev_queue *event_queue;
169
172
175
184
185 void *clipboard_data;
186
190 unsigned int dpi;
191
192 /* Backed private data */
193 char priv[];
194};
195
196#define GP_BACKEND_PRIV(backend) ((void*)(backend)->priv)
197
208static inline void gp_backend_flip(gp_backend *self)
209{
210 if (self->flip)
211 self->flip(self);
212}
213
229 gp_coord x0, gp_coord y0,
230 gp_coord x1, gp_coord y1);
231
243static inline void gp_backend_update_rect(gp_backend *self,
244 gp_coord x0, gp_coord y0,
245 gp_coord x1, gp_coord y1)
246{
247 return gp_backend_update_rect_xyxy(self, x0, y0, x1, y1);
248}
249
262 gp_coord x, gp_coord y,
263 gp_size w, gp_size h)
264{
265 gp_backend_update_rect_xyxy(self, x, y, x + w - 1, y + h - 1);
266}
267
268static inline void gp_backend_poll_add(gp_backend *self, gp_fd *fd)
269{
270 gp_poll_add(&self->fds, fd);
271}
272
273static inline void gp_backend_poll_rem(gp_backend *self, gp_fd *fd)
274{
275 gp_poll_rem(&self->fds, fd);
276}
277
278static inline gp_fd *gp_backend_poll_rem_by_fd(gp_backend *self, int fd)
279{
280 return gp_poll_rem_by_fd(&self->fds, fd);
281}
282
283static inline int gp_backend_cursor_set(gp_backend *self, enum gp_backend_cursors cursor)
284{
285 if (self->set_cursor)
286 return self->set_cursor(self, cursor);
287
288 return 1;
289}
290
302
303/*
304 * Polls backend, the events are filled into event queue.
305 */
306void gp_backend_poll(gp_backend *self);
307
308/*
309 * Poll and GetEvent combined.
310 */
311gp_event *gp_backend_poll_event(gp_backend *self);
312
313/*
314 * Waits for backend events.
315 */
316void gp_backend_wait(gp_backend *self);
317
318/*
319 * Wait and GetEvent combined.
320 */
321gp_event *gp_backend_wait_event(gp_backend *self);
322
323/*
324 * Adds timer to backend.
325 *
326 * If timer Callback is NULL a timer event is pushed into
327 * the backend event queue once timer expires.
328 *
329 * See input/GP_Timer.h for more information about timers.
330 */
331void gp_backend_add_timer(gp_backend *self, gp_timer *timer);
332
333/*
334 * Removes timer from backend timer queue.
335 */
336void gp_backend_rem_timer(gp_backend *self, gp_timer *timer);
337
338/*
339 * Returns number of timers scheduled in backend.
340 */
341static inline unsigned int gp_backend_timers_in_queue(gp_backend *self)
342{
343 return gp_timer_queue_size(self->timers);
344}
345
346/*
347 * Returns a timeout to a closest timer in ms or -1. Can be passed directly to
348 * poll(2).
349 */
350int gp_backend_timer_timeout(gp_backend *self);
351
352/*
353 * Sets backend caption, if supported.
354 *
355 * When setting caption is not possible/implemented non zero is returned.
356 */
357static inline int gp_backend_set_caption(gp_backend *backend,
358 const char *caption)
359{
360 if (!backend->set_attr)
361 return 1;
362
363 return backend->set_attr(backend, GP_BACKEND_TITLE, caption);
364}
365
366/*
367 * Resize backend, if supported.
368 *
369 * When resizing is not possible/implemented non zero is returned.
370 *
371 * When the backend size matches the passed width and height,
372 * no action is done.
373 *
374 * Note that after calling this, the backend->pixmap pointer may change.
375 */
376int gp_backend_resize(gp_backend *backend, uint32_t w, uint32_t h);
377
378/*
379 * Changes fullscreen mode.
380 *
381 * val 0 - turn off
382 * 1 - turn on
383 * 2 - toggle
384 * 3 - query
385 */
386static inline int gp_backend_fullscreen(gp_backend *backend, int val)
387{
388 if (!backend->set_attr)
389 return 0;
390
391 return backend->set_attr(backend, GP_BACKEND_FULLSCREEN, &val);
392}
393
414
425
433
445
446/*
447 * Event Queue functions.
448 */
449static inline unsigned int gp_backend_events(gp_backend *self)
450{
451 return gp_ev_queue_events(self->event_queue);
452}
453
454static inline gp_event *gp_backend_get_event(gp_backend *self)
455{
456 return gp_ev_queue_get(self->event_queue);
457}
458
459static inline gp_event *gp_backend_peek_event(gp_backend *self)
460{
461 return gp_ev_queue_peek(self->event_queue);
462}
463
464static inline void gp_backend_put_event_back(gp_backend *self, gp_event *ev)
465{
466 gp_ev_queue_put_back(self->event_queue, ev);
467}
468
469#endif /* BACKENDS_GP_BACKEND_H */
A common types.
int gp_coord
Integer type for coordinates i.e. x, y, ...
Definition gp_types.h:19
unsigned int gp_size
Integer type for sizes i.e. w, h, ...
Definition gp_types.h:24
static void gp_backend_flip(gp_backend *self)
Copies whole backend pixmap to a display.
Definition gp_backend.h:208
int gp_backend_resize_ack(gp_backend *self)
Resize acknowledge.
static void gp_backend_update_rect(gp_backend *self, gp_coord x0, gp_coord y0, gp_coord x1, gp_coord y1)
Copies a rectangle from backend pixmap to a display.
Definition gp_backend.h:243
gp_backend_cursors
Cursor types.
Definition gp_backend.h:49
@ GP_BACKEND_CURSOR_SHOW
Shows cursor.
Definition gp_backend.h:62
@ GP_BACKEND_CURSOR_TEXT_EDIT
Text edit cursor.
Definition gp_backend.h:53
@ GP_BACKEND_CURSOR_HIDE
Hides cursor.
Definition gp_backend.h:64
@ GP_BACKEND_CURSOR_MAX
Last cursor + 1.
Definition gp_backend.h:59
@ GP_BACKEND_CURSOR_HAND
Used typicaly while howering over links.
Definition gp_backend.h:57
@ GP_BACKEND_CURSOR_CROSSHAIR
Crosshair.
Definition gp_backend.h:55
@ GP_BACKEND_CURSOR_ARROW
Arrow default cursor type.
Definition gp_backend.h:51
static void gp_backend_update_rect_xywh(gp_backend *self, gp_coord x, gp_coord y, gp_size w, gp_size h)
Copies a rectangle from backend pixmap to a display.
Definition gp_backend.h:261
void gp_backend_update_rect_xyxy(gp_backend *self, gp_coord x0, gp_coord y0, gp_coord x1, gp_coord y1)
Copies a rectangle from backend pixmap to a display.
void gp_backend_exit(gp_backend *self)
Exits the backend.
void gp_backend_task_queue_set(gp_backend *self, gp_task_queue *task_queue)
Sets the backend task_queue and starts task processing.
void gp_backend_task_ins(gp_backend *self, gp_task *task)
Inserts a task into the task queue.
void gp_backend_task_rem(gp_backend *self, gp_task *task)
Removes a task from the task queue.
Event Queue.
void gp_ev_queue_put_back(gp_ev_queue *self, gp_event *ev)
Pushes back an event to the event queue.
gp_event * gp_ev_queue_peek(gp_ev_queue *self)
Peeks at a top event from the queue.
gp_event * gp_ev_queue_get(gp_ev_queue *self)
Remoes and returns pointer to a top event from the queue.
unsigned int gp_ev_queue_events(gp_ev_queue *self)
Returns number of events queued in the queue.
A linked list implementation.
A simple epoll wrapper.
int gp_poll_add(gp_poll *self, gp_fd *fd)
Adds a file descriptor.
gp_fd * gp_poll_rem_by_fd(gp_poll *self, int fd)
Looks up and removes a gp_fd by a fd.
int gp_poll_rem(gp_poll *self, gp_fd *fd)
Removes a file descriptor.
Application tasks.
Timers and timer queue implementation.
static unsigned int gp_timer_queue_size(gp_timer *queue)
Returns size of the queue, i.e. number of timers.
Definition gp_timer.h:139
A backend.
Definition gp_backend.h:72
gp_poll fds
File descriptors to poll for.
Definition gp_backend.h:165
void(* poll)(gp_backend *self)
Non-blocking event loop.
Definition gp_backend.h:145
const char * name
Backend name, e.g. "X11".
Definition gp_backend.h:84
int(* set_cursor)(gp_backend *self, enum gp_backend_cursors cursor)
Sets cursor shape and/or hides and shows cursor.
Definition gp_backend.h:125
gp_timer * timers
Priority queue for timers.
Definition gp_backend.h:171
gp_task_queue * task_queue
Task queue.
Definition gp_backend.h:174
gp_dlist input_drivers
List of input drivers feeding the ev_queue.
Definition gp_backend.h:183
void(* exit)(gp_backend *self)
Exits the backend.
Definition gp_backend.h:137
void(* update_rect)(gp_backend *self, gp_coord x0, gp_coord y0, gp_coord x1, gp_coord y1)
Updates display rectangle.
Definition gp_backend.h:104
void(* flip)(gp_backend *self)
Updates display.
Definition gp_backend.h:94
gp_pixmap * pixmap
Pointer to pixmap app should draw to.
Definition gp_backend.h:81
unsigned int dpi
A backend DPI if unknown it's set to 0.
Definition gp_backend.h:190
void(* wait)(gp_backend *self)
Blocking event loop. Blocks until events are ready.
Definition gp_backend.h:162
A double linked list pointers.
Definition gp_list.h:56
An event queue.
Definition gp_ev_queue.h:24
An input event.
Definition gp_event.h:153
An epoll file descriptor.
Definition gp_poll.h:70
A pixmap buffer.
Definition gp_pixmap.h:33
An epoll instance.
Definition gp_poll.h:46
A task queue.
Definition gp_task.h:28
A task.
Definition gp_task.h:48
A timer.
Definition gp_timer.h:26