Rename GP_Context -> GP_Pixmap
[gfxprim.git] / include / backends / GP_Backend.h
blobf71b0f0a4e98c0d4230109542f321fceb91a3782
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
28 The GP_Backend is overall structure for API for managing
29 connection/mmaped memory/... to xserver window/framebuffer/... .
31 In contrast to other graphics libraries we do not try to create unified
32 initalization interface that would match specialities for every possible
33 backend. Rather than that we are trying to create API that is the same
34 for all backends, once initalization is done.
36 So once you initalize, for example, framebuffer driver, the GP_Backend
37 structure is returned, which then could be used with generic code for
38 backend drawing.
42 #ifndef BACKENDS_GP_BACKEND_H
43 #define BACKENDS_GP_BACKEND_H
45 #include "core/GP_Pixmap.h"
47 #include "input/GP_EventQueue.h"
48 #include "input/GP_Timer.h"
50 typedef struct GP_Backend {
52 * Backend name.
54 const char *name;
57 * Pointer to pixmap app should draw to.
59 * This MAY change upon a flip operation.
61 GP_Pixmap *pixmap;
64 * If display is buffered, this copies content
65 * of pixmap onto display.
67 * If display is not buffered, this is no-op (set to NULL).
69 void (*Flip)(struct GP_Backend *self);
72 * Updates display rectangle.
74 * In contrast to flip operation, the pixmap
75 * must not change (this is intended for updating very small areas).
77 * If display is not buffered, this is no-op (set to NULL).
79 void (*UpdateRect)(struct GP_Backend *self,
80 GP_Coord x0, GP_Coord y0,
81 GP_Coord x1, GP_Coord y1);
84 * Callback to change attributes.
86 * If w and h are zero, only caption is changed.
88 * If w is set and h is zero, only w is changed and vice versa.
90 * If caption is NULL only w and h are changed.
92 * Use the inline wrappers instead.
94 int (*SetAttributes)(struct GP_Backend *self,
95 uint32_t w, uint32_t h,
96 const char *caption);
99 * Resize acknowledge callback. This must be called
100 * after you got resize event in order to resize
101 * backend buffers.
103 int (*ResizeAck)(struct GP_Backend *self);
106 * Exits the backend.
108 void (*Exit)(struct GP_Backend *self);
111 * Connection fd. Set to -1 if not available
113 int fd;
116 * Non-blocking event loop.
118 * The events are filled into the event queue see GP_Input.h.
120 void (*Poll)(struct GP_Backend *self);
123 * Blocking event loop. Blocks until events are ready.
125 * Note that events received by a backend are not necessarily
126 * translated to input events. So input queue may be empty
127 * after Wait has returned.
129 * The events are filled into the event queue see GP_Input.h.
131 void (*Wait)(struct GP_Backend *self);
134 * Queue to store input events.
136 struct GP_EventQueue event_queue;
139 * Priority queue for timers.
141 struct GP_Timer *timers;
143 /* Backed private data */
144 char priv[];
145 } GP_Backend;
147 #define GP_BACKEND_PRIV(backend) ((void*)(backend)->priv)
150 * Calls backend->Flip().
152 void GP_BackendFlip(GP_Backend *backend);
155 * Calls backend->UpdateRect().
157 void GP_BackendUpdateRectXYXY(GP_Backend *backend,
158 GP_Coord x0, GP_Coord y0,
159 GP_Coord x1, GP_Coord y1);
161 static inline void GP_BackendUpdateRect(GP_Backend *backend,
162 GP_Coord x0, GP_Coord y0,
163 GP_Coord x1, GP_Coord y1)
165 return GP_BackendUpdateRectXYXY(backend, x0, y0, x1, y1);
168 static inline void GP_BackendUpdateRectXYWH(GP_Backend *backend,
169 GP_Coord x, GP_Coord y,
170 GP_Size w, GP_Size h)
172 GP_BackendUpdateRectXYXY(backend, x, y, x + w - 1, y + h - 1);
176 * Calls backend->Exit().
178 static inline void GP_BackendExit(GP_Backend *backend)
180 if (backend)
181 backend->Exit(backend);
185 * Polls backend, the events are filled into event queue.
187 void GP_BackendPoll(GP_Backend *self);
190 * Poll and GetEvent combined.
192 int GP_BackendPollEvent(GP_Backend *self, GP_Event *ev);
195 * Waits for backend events.
197 void GP_BackendWait(GP_Backend *self);
200 * Wait and GetEvent combined.
202 int GP_BackendWaitEvent(GP_Backend *self, GP_Event *ev);
205 * Adds timer to backend.
207 * If timer Callback is NULL a timer event is pushed into
208 * the backend event queue once timer expires.
210 * See input/GP_Timer.h for more information about timers.
212 void GP_BackendAddTimer(GP_Backend *self, GP_Timer *timer);
215 * Removes timer from backend timer queue.
217 void GP_BackendRemTimer(GP_Backend *self, GP_Timer *timer);
220 * Returns number of timers scheduled in backend.
222 static inline unsigned int GP_BackendTimersInQueue(GP_Backend *self)
224 return GP_TimerQueueSize(self->timers);
228 * Sets backend caption, if supported.
230 * When setting caption is not possible/implemented non zero is returned.
232 static inline int GP_BackendSetCaption(GP_Backend *backend,
233 const char *caption)
235 if (backend->SetAttributes == NULL)
236 return 1;
238 return backend->SetAttributes(backend, 0, 0, caption);
242 * Resize backend, if supported.
244 * When resizing is not possible/implemented non zero is returned.
246 * When the backend size matches the passed width and height,
247 * no action is done.
249 * Note that after calling this, the backend->pixmap pointer may change.
251 int GP_BackendResize(GP_Backend *backend, uint32_t w, uint32_t h);
255 * Resize acknowledge. You must call this right after you application has
256 * received resize event.
258 * This will resize backend buffers. After this call returns the backend width
259 * height and pixmap pointer are most likely different.
261 * This function returns zero on succes. Non zero on failure. If it fails the
262 * best action to take is to save application data and exit (as the backend
263 * may be in undefined state).
265 int GP_BackendResizeAck(GP_Backend *self);
268 * Event Queue functions.
270 static inline unsigned int GP_BackendEventsQueued(GP_Backend *self)
272 return GP_EventQueueEventsQueued(&self->event_queue);
275 static inline int GP_BackendGetEvent(GP_Backend *self, GP_Event *ev)
277 return GP_EventQueueGet(&self->event_queue, ev);
280 static inline int GP_BackendPeekEvent(GP_Backend *self, GP_Event *ev)
282 return GP_EventQueuePeek(&self->event_queue, ev);
285 static inline void GP_BackendPutEventBack(GP_Backend *self, GP_Event *ev)
287 GP_EventQueuePutBack(&self->event_queue, ev);
290 #endif /* BACKENDS_GP_BACKEND_H */