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-2025 Cyril Hrubis <metan@ucw.cz>
7 */
8
25#ifndef BACKENDS_GP_BACKEND_H
26#define BACKENDS_GP_BACKEND_H
27
28#include <core/gp_types.h>
29#include <core/gp_compiler.h>
30#include <core/gp_pixmap.h>
31
32#include <utils/gp_timer.h>
33#include <utils/gp_list.h>
34#include <utils/gp_poll.h>
35
36#include <input/gp_ev_queue.h>
37#include <input/gp_task.h>
38
39#include <backends/gp_types.h>
40
77
108
127
148
170
206
208 const char *name;
209
218 void (*flip)(gp_backend *self);
219
234 void (*update)(gp_backend *self);
235
247 void (*update_rect)(gp_backend *self,
248 gp_coord x0, gp_coord y0,
249 gp_coord x1, gp_coord y1);
250
264 enum gp_backend_ret (*set_attr)(gp_backend *self,
265 enum gp_backend_attr attr,
266 const void *vals);
267
278
282 void (*exit)(gp_backend *self);
283
290 void (*poll)(gp_backend *self);
291
292 /*
293 * Clipboard handler.
294 */
295 int (*clipboard)(gp_backend *self, gp_clipboard *op);
296
307 void (*wait)(gp_backend *self, int timeout_ms);
308
311
314
321 void (*on_ev_ret)(gp_backend *self, gp_event *ev);
322
325
328
337
344
348 unsigned int dpi;
349
350 /* Backed private data */
351 char priv[] GP_ALIGNED;
352};
353
354#define GP_BACKEND_PRIV(backend) ((void*)(backend)->priv)
355
362static inline gp_size gp_backend_w(gp_backend *self)
363{
364 return gp_pixmap_w(self->pixmap);
365}
366
373static inline gp_size gp_backend_h(gp_backend *self)
374{
375 return gp_pixmap_h(self->pixmap);
376}
377
385{
386 return self->pixmap->pixel_type;
387}
388
413 gp_coord x0, gp_coord y0,
414 gp_coord x1, gp_coord y1);
415
427static inline void gp_backend_update_rect(gp_backend *self,
428 gp_coord x0, gp_coord y0,
429 gp_coord x1, gp_coord y1)
430{
431 return gp_backend_update_rect_xyxy(self, x0, y0, x1, y1);
432}
433
446 gp_coord x, gp_coord y,
447 gp_size w, gp_size h)
448{
449 gp_backend_update_rect_xyxy(self, x, y, x + w - 1, y + h - 1);
450}
451
467static inline void gp_backend_update(gp_backend *self)
468{
469 if (self->update)
470 self->update(self);
471}
472
492static inline void gp_backend_flip(gp_backend *self)
493{
494 if (self->flip)
495 self->flip(self);
496 else
497 gp_backend_update(self);
498}
499
510
517static inline void gp_backend_poll_rem(gp_backend *self, gp_fd *fd)
518{
519 gp_poll_rem(&self->fds, fd);
520}
521
528static inline gp_fd *gp_backend_poll_rem_by_fd(gp_backend *self, int fd)
529{
530 return gp_poll_rem_by_fd(&self->fds, fd);
531}
532
546 enum gp_backend_cursor_req cursor)
547{
548 if (self->set_attr)
549 return self->set_attr(self, GP_BACKEND_ATTR_CURSOR, &cursor);
550
551 return GP_BACKEND_NOTSUPP;
552}
553
564static inline int gp_backend_backlight_inc(gp_backend *self)
565{
567
568 if (self->set_attr)
569 return self->set_attr(self, GP_BACKEND_ATTR_BACKLIGHT, &req);
570
571 return GP_BACKEND_NOTSUPP;
572}
573
584static inline int gp_backend_backlight_dec(gp_backend *self)
585{
587
588 if (self->set_attr)
589 return self->set_attr(self, GP_BACKEND_ATTR_BACKLIGHT, &req);
590
591 return GP_BACKEND_NOTSUPP;
592}
593
604static inline int gp_backend_backlight_get(gp_backend *self)
605{
607
608 if (self->set_attr)
609 return self->set_attr(self, GP_BACKEND_ATTR_BACKLIGHT, &req);
610
611 return GP_BACKEND_NOTSUPP;
612}
613
627
637
664
690void gp_backend_wait_timeout(gp_backend *self, int timeout_ms);
691
715static inline void gp_backend_wait(gp_backend *self)
716{
717 gp_backend_wait_timeout(self, -1);
718}
719
744
768{
769 return gp_backend_ev_wait_timeout(self, -1);
770}
771
782
793void gp_backend_timer_reschedule(gp_backend *self, gp_timer *timer, uint32_t expires_ms);
794
802
808static inline unsigned int gp_backend_timers_queued(gp_backend *self)
809{
810 return gp_timer_queue_size(self->timers);
811}
812
822
832 const char *caption)
833{
834 if (!backend->set_attr)
835 return GP_BACKEND_NOTSUPP;
836
837 return backend->set_attr(backend, GP_BACKEND_ATTR_TITLE, caption);
838}
839
854int gp_backend_resize(gp_backend *backend, uint32_t w, uint32_t h);
855
869{
870 if (!backend->set_attr)
871 return GP_BACKEND_NOTSUPP;
872
873 return backend->set_attr(backend, GP_BACKEND_ATTR_FULLSCREEN, &req);
874}
875
902
916
924
936
942static inline void gp_backend_ev_flush(gp_backend *self)
943{
944 return gp_ev_queue_flush(self->event_queue);
945}
946
954static inline unsigned int gp_backend_ev_queued(gp_backend *self)
955{
956 return gp_ev_queue_events(self->event_queue);
957}
958
970
984{
985 return gp_ev_queue_peek(self->event_queue);
986}
987
1000static inline void gp_backend_ev_put_back(gp_backend *self, gp_event *ev)
1001{
1003}
1004
1005#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
int gp_backend_timer_timeout(gp_backend *self)
Returns time to the closest timer timeout.
static int gp_backend_backlight_inc(gp_backend *self)
Increases the backlight intensity.
Definition gp_backend.h:564
static void gp_backend_ev_put_back(gp_backend *self, gp_event *ev)
Puts back an event that has been just removed from the queue.
void gp_backend_poll_add(gp_backend *self, gp_fd *fd)
Add a file descriptor to the backend poll loop.
static gp_pixel_type gp_backend_pixel_type(gp_backend *self)
Returns backend pixel type.
Definition gp_backend.h:384
static void gp_backend_flip(gp_backend *self)
Flips backend and display buffers.
Definition gp_backend.h:492
gp_backend_fullscreen_req
Fullscreen request type.
Definition gp_backend.h:112
@ GP_BACKEND_FULLSCREEN_QUERY
Query fullscreen state.
Definition gp_backend.h:125
@ GP_BACKEND_FULLSCREEN_OFF
Request fullscreen to be turned off.
Definition gp_backend.h:114
@ GP_BACKEND_FULLSCREEN_ON
Request fullscreen to be turned on.
Definition gp_backend.h:116
@ GP_BACKEND_FULLSCREEN_TOGGLE
Toggle fullscreen state.
Definition gp_backend.h:118
gp_backend_cursor_req
Cursor types.
Definition gp_backend.h:131
@ GP_BACKEND_CURSOR_SHOW
Shows cursor.
Definition gp_backend.h:144
@ GP_BACKEND_CURSOR_TEXT_EDIT
Text edit cursor.
Definition gp_backend.h:135
@ GP_BACKEND_CURSOR_HIDE
Hides cursor.
Definition gp_backend.h:146
@ GP_BACKEND_CURSOR_MAX
Last cursor + 1.
Definition gp_backend.h:141
@ GP_BACKEND_CURSOR_HAND
Used typicaly while howering over links.
Definition gp_backend.h:139
@ GP_BACKEND_CURSOR_CROSSHAIR
Crosshair.
Definition gp_backend.h:137
@ GP_BACKEND_CURSOR_ARROW
Arrow default cursor type.
Definition gp_backend.h:133
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:427
static void gp_backend_wait(gp_backend *self)
Waits for a backend events.
Definition gp_backend.h:715
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:445
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_poll(gp_backend *self)
Polls a backend for events.
void gp_backend_exit(gp_backend *self)
Exits the backend.
gp_backend_ret
A return value from the backend set_attr callback.
Definition gp_backend.h:85
@ GP_BACKEND_NOTSUPP
Unsupported attribute.
Definition gp_backend.h:97
@ GP_BACKEND_OK
Atrribute set succesfully.
Definition gp_backend.h:87
@ GP_BACKEND_CONNERR
Connection error.
Definition gp_backend.h:106
@ GP_BACKEND_ON
Attribute is enabled.
Definition gp_backend.h:91
@ GP_BACKEND_EINVAL
Invalid value.
Definition gp_backend.h:104
@ GP_BACKEND_OFF
Attribute is disabled.
Definition gp_backend.h:89
static gp_event * gp_backend_ev_wait(gp_backend *self)
Returns an event from the queue, wait the backend for events if the queue is empty.
Definition gp_backend.h:767
void gp_backend_timer_reschedule(gp_backend *self, gp_timer *timer, uint32_t expires_ms)
Reschedules a timer.
int gp_backend_resize(gp_backend *backend, uint32_t w, uint32_t h)
Request backend resize.
static gp_event * gp_backend_ev_peek(gp_backend *self)
Returns a pointer to a first event in the queue.
Definition gp_backend.h:983
gp_backend_backlight_req
Backlight operations.
Definition gp_backend.h:152
@ GP_BACKEND_BACKLIGHT_DEC
Decreases the backlight brightness.
Definition gp_backend.h:164
@ GP_BACKEND_BACKLIGHT_GET
Returns current backlight intensity in percents.
Definition gp_backend.h:168
@ GP_BACKEND_BACKLIGHT_INC
Increases the backlight brightness.
Definition gp_backend.h:158
void gp_backend_task_queue_set(gp_backend *self, gp_task_queue *task_queue)
Sets the backend task_queue and starts task processing.
static gp_size gp_backend_h(gp_backend *self)
Returns backend height.
Definition gp_backend.h:373
static gp_size gp_backend_w(gp_backend *self)
Returns backend width.
Definition gp_backend.h:362
static int gp_backend_backlight_dec(gp_backend *self)
Decreases the backlight intensity.
Definition gp_backend.h:584
static enum gp_backend_ret gp_backend_cursor_set(gp_backend *self, enum gp_backend_cursor_req cursor)
Sets backend cursor type.
Definition gp_backend.h:545
void gp_backend_task_ins(gp_backend *self, gp_task *task)
Inserts a task into the task queue.
static void gp_backend_ev_flush(gp_backend *self)
Removes all events from the event queue.
Definition gp_backend.h:942
void gp_backend_task_rem(gp_backend *self, gp_task *task)
Removes a task from the task queue.
static enum gp_backend_ret gp_backend_set_caption(gp_backend *backend, const char *caption)
Sets backend caption, if supported.
Definition gp_backend.h:831
static unsigned int gp_backend_timers_queued(gp_backend *self)
Returns number of timers scheduled in backend.
Definition gp_backend.h:808
gp_event * gp_backend_ev_get(gp_backend *self)
Removes and returns a pointer to a first event in the queue.
gp_event * gp_backend_ev_wait_timeout(gp_backend *self, int timeout_ms)
Returns an event from the queue, wait for the backend events until a timeout if the queue is empty.
static enum gp_backend_ret gp_backend_fullscreen(gp_backend *backend, enum gp_backend_fullscreen_req req)
Reuqests to change fullscreen mode.
Definition gp_backend.h:867
gp_backend_attr
Backend attributes.
Definition gp_backend.h:44
@ GP_BACKEND_ATTR_SIZE
Window change request.
Definition gp_backend.h:50
@ GP_BACKEND_ATTR_TITLE
Window title change request.
Definition gp_backend.h:56
@ GP_BACKEND_ATTR_BACKLIGHT
Backlight support.
Definition gp_backend.h:75
@ GP_BACKEND_ATTR_CURSOR
Cursor modifications.
Definition gp_backend.h:68
@ GP_BACKEND_ATTR_FULLSCREEN
Fullscreen mode change request.
Definition gp_backend.h:62
gp_event * gp_backend_ev_poll(gp_backend *self)
Returns an event from the queue, polls the backend for events if the queue is empty.
static int gp_backend_backlight_get(gp_backend *self)
Returns backlight intensity in percents.
Definition gp_backend.h:604
int gp_backend_render_stopped(gp_backend *self)
Rendering stopped acknowledge.
static void gp_backend_poll_rem(gp_backend *self, gp_fd *fd)
Removes a file descriptor to the backend poll loop.
Definition gp_backend.h:517
static void gp_backend_update(gp_backend *self)
Copies data from backend buffer to display.
Definition gp_backend.h:467
void gp_backend_timer_stop(gp_backend *self, gp_timer *timer)
Removes timer from backend timer queue.
static unsigned int gp_backend_ev_queued(gp_backend *self)
Returns a number of events in the backend event queue.
Definition gp_backend.h:954
static gp_fd * gp_backend_poll_rem_by_fd(gp_backend *self, int fd)
Removes a file descriptor to the backend poll loop.
Definition gp_backend.h:528
void gp_backend_timer_start(gp_backend *self, gp_timer *timer)
Adds a timer to a backend timer queue.
void gp_backend_wait_timeout(gp_backend *self, int timeout_ms)
Waits for a backend events with a timeout.
A compiler dependent macros.
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.
unsigned int gp_ev_queue_events(gp_ev_queue *self)
Returns number of events queued in the queue.
void gp_ev_queue_flush(gp_ev_queue *self)
Removes all events from the queue.
A linked list implementation.
gp_pixel_type
List of all pixel types.
A pixel buffer.
static gp_size gp_pixmap_w(const gp_pixmap *self)
Returns pixmap width taking axes swap into account.
Definition gp_pixmap.h:571
static gp_size gp_pixmap_h(const gp_pixmap *self)
Returns pixmap height taking axes swap into account.
Definition gp_pixmap.h:585
A simple epoll wrapper.
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(const gp_timer *queue)
Returns size of the queue, i.e. number of timers.
Definition gp_timer.h:166
A backend.
Definition gp_backend.h:176
void(* wait)(gp_backend *self, int timeout_ms)
Blocking event loop. Blocks until events are ready.
Definition gp_backend.h:307
gp_poll fds
File descriptors to poll for.
Definition gp_backend.h:310
void(* poll)(gp_backend *self)
Non-blocking event loop.
Definition gp_backend.h:290
const char * name
Backend name, e.g. "X11".
Definition gp_backend.h:208
void(* on_ev_ret)(gp_backend *self, gp_event *ev)
Called on events to be returned.
Definition gp_backend.h:321
gp_timer * timers
Priority queue for timers.
Definition gp_backend.h:324
gp_task_queue * task_queue
Task queue.
Definition gp_backend.h:327
gp_dlist input_drivers
List of input drivers feeding the ev_queue.
Definition gp_backend.h:336
void(* exit)(gp_backend *self)
Exits the backend.
Definition gp_backend.h:282
void * clipboard_data
Clipboard data.
Definition gp_backend.h:343
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:247
int(* render_stopped)(gp_backend *self)
Rendering stopped callback.
Definition gp_backend.h:277
void(* update)(gp_backend *self)
Updates whole display buffer.
Definition gp_backend.h:234
void(* flip)(gp_backend *self)
Flips backend and display buffers.
Definition gp_backend.h:218
gp_pixmap * pixmap
Pointer to a pixmap an app should draw to.
Definition gp_backend.h:205
gp_ev_queue * event_queue
Queue to store input events.
Definition gp_backend.h:313
unsigned int dpi
A backend DPI if unknown it's set to 0.
Definition gp_backend.h:348
enum gp_backend_ret(* set_attr)(gp_backend *self, enum gp_backend_attr attr, const void *vals)
Attribute change callback.
Definition gp_backend.h:264
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:225
An epoll file descriptor.
Definition gp_poll.h:70
A pixmap buffer.
Definition gp_pixmap.h:33
enum gp_pixel_type pixel_type
A pixel format.
Definition gp_pixmap.h:63
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