LMMS
Loading...
Searching...
No Matches
common.c
Go to the documentation of this file.
1// Copyright 2012-2022 David Robillard <d@drobilla.net>
2// SPDX-License-Identifier: ISC
3
4// Common implementations of public API functions in the core library
5
6#include "internal.h"
7
8#include "platform.h"
9#include "types.h"
10
11#include "pugl/pugl.h"
12
13#include <stdbool.h>
14#include <stdlib.h>
15#include <string.h>
16
17const char*
19{
20 // clang-format off
21 switch (status) {
22 case PUGL_SUCCESS: return "Success";
23 case PUGL_FAILURE: return "Non-fatal failure";
24 case PUGL_UNKNOWN_ERROR: return "Unknown system error";
25 case PUGL_BAD_BACKEND: return "Invalid or missing backend";
26 case PUGL_BAD_CONFIGURATION: return "Invalid view configuration";
27 case PUGL_BAD_PARAMETER: return "Invalid parameter";
28 case PUGL_BACKEND_FAILED: return "Backend initialisation failed";
29 case PUGL_REGISTRATION_FAILED: return "Class registration failed";
30 case PUGL_REALIZE_FAILED: return "View creation failed";
31 case PUGL_SET_FORMAT_FAILED: return "Failed to set pixel format";
32 case PUGL_CREATE_CONTEXT_FAILED: return "Failed to create drawing context";
33 case PUGL_UNSUPPORTED: return "Unsupported operation";
34 case PUGL_NO_MEMORY: return "Failed to allocate memory";
35 }
36 // clang-format on
37
38 return "Unknown error";
39}
40
43{
44 PuglWorld* world = (PuglWorld*)calloc(1, sizeof(PuglWorld));
45 if (!world || !(world->impl = puglInitWorldInternals(type, flags))) {
46 free(world);
47 return NULL;
48 }
49
50 world->startTime = puglGetTime(world);
51
52#ifdef __EMSCRIPTEN__
53 puglSetString(&world->className, "canvas");
54#else
55 puglSetString(&world->className, "Pugl");
56#endif
57
58 return world;
59}
60
61void
63{
65 free(world->className);
66 free(world->views);
67 free(world);
68}
69
70void
72{
73 world->handle = handle;
74}
75
78{
79 return world->handle;
80}
81
83puglSetClassName(PuglWorld* const world, const char* const name)
84{
85 puglSetString(&world->className, name);
86 return PUGL_SUCCESS;
87}
88
89const char*
91{
92 return world->className;
93}
94
95static void
97{
101 hints[PUGL_RED_BITS] = 8;
102 hints[PUGL_GREEN_BITS] = 8;
103 hints[PUGL_BLUE_BITS] = 8;
104 hints[PUGL_ALPHA_BITS] = 8;
105 hints[PUGL_DEPTH_BITS] = 0;
106 hints[PUGL_STENCIL_BITS] = 0;
107 hints[PUGL_SAMPLES] = 0;
110 hints[PUGL_RESIZABLE] = PUGL_FALSE;
113}
114
117{
118 PuglView* view = (PuglView*)calloc(1, sizeof(PuglView));
119 if (!view || !(view->impl = puglInitViewInternals(world))) {
120 free(view);
121 return NULL;
122 }
123
124 view->world = world;
125 view->sizeHints[PUGL_MIN_SIZE].width = 1;
126 view->sizeHints[PUGL_MIN_SIZE].height = 1;
127
129
130 // Add to world view list
131 ++world->numViews;
132 world->views =
133 (PuglView**)realloc(world->views, world->numViews * sizeof(PuglView*));
134
135 world->views[world->numViews - 1] = view;
136
137 return view;
138}
139
140void
142{
143 if (view->eventFunc && view->backend) {
145 }
146
147 // Remove from world view list
148 PuglWorld* world = view->world;
149 for (size_t i = 0; i < world->numViews; ++i) {
150 if (world->views[i] == view) {
151 if (i == world->numViews - 1) {
152 world->views[i] = NULL;
153 } else {
154 memmove(world->views + i,
155 world->views + i + 1,
156 sizeof(PuglView*) * (world->numViews - i - 1));
157 world->views[world->numViews - 1] = NULL;
158 }
159 --world->numViews;
160 }
161 }
162
163 free(view->title);
165 free(view);
166}
167
170{
171 return view->world;
172}
173
174void
176{
177 view->handle = handle;
178}
179
182{
183 return view->handle;
184}
185
188{
189 view->backend = backend;
190 return PUGL_SUCCESS;
191}
192
193const PuglBackend*
195{
196 return view->backend;
197}
198
201{
202 view->eventFunc = eventFunc;
203 return PUGL_SUCCESS;
204}
205
208{
209 if (value == PUGL_DONT_CARE) {
210 switch (hint) {
216 return PUGL_BAD_PARAMETER;
217 default:
218 break;
219 }
220 }
221
222 view->hints[hint] = value;
223 return PUGL_SUCCESS;
224}
225
226int
228{
229 return view->hints[hint];
230}
231
234{
235 return view->frame;
236}
237
238const char*
239puglGetWindowTitle(const PuglView* const view)
240{
241 return view->title;
242}
243
246{
247 view->parent = parent;
248 return PUGL_SUCCESS;
249}
250
253{
254 return view->parent;
255}
256
259{
260 return view->transientParent;
261}
262
263bool
265{
266 return view->visible;
267}
268
269void*
271{
272 return view->backend->getContext(view);
273}
#define NULL
Definition CarlaBridgeFormat.cpp:30
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
static void puglSetDefaultHints(PuglHints hints)
Definition common.c:96
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
static PuglViewHint hint
Definition pugl.h:1707
static const PuglBackend * backend
Definition pugl.h:1732
static const char * name
Definition pugl.h:1582
static uintptr_t parent
Definition pugl.h:1644
@ PUGL_DESTROY
View destroyed, a PuglDestroyEvent.
Definition pugl.h:182
PuglRect puglGetFrame(const PuglView *view)
Definition common.c:233
void * puglGetContext(PuglView *view)
Definition common.c:270
PuglStatus puglSetBackend(PuglView *view, const PuglBackend *backend)
Definition common.c:187
PuglHandle puglGetHandle(PuglView *view)
Get the user data for a view.
Definition common.c:181
void puglSetHandle(PuglView *view, PuglHandle handle)
Definition common.c:175
PuglStatus puglSetEventFunc(PuglView *view, PuglEventFunc eventFunc)
Set the function to call when an event occurs.
Definition common.c:200
PuglWorld * puglGetWorld(PuglView *view)
Return the world that view is a part of.
Definition common.c:169
PuglView * puglNewView(PuglWorld *const world)
Definition common.c:116
PuglStatus puglSetViewHint(PuglView *view, PuglViewHint hint, int value)
Definition common.c:207
int puglGetViewHint(const PuglView *view, PuglViewHint hint)
Definition common.c:227
void puglFreeView(PuglView *view)
Free a view created with puglNewView().
Definition common.c:141
const PuglBackend * puglGetBackend(const PuglView *view)
Return the graphics backend used by a view.
Definition common.c:194
const char * puglStrerror(const PuglStatus status)
Return a string describing a status code.
Definition common.c:18
PuglStatus
Return status code.
Definition pugl.h:627
@ PUGL_SUCCESS
Success.
Definition pugl.h:628
@ PUGL_FAILURE
Non-fatal failure.
Definition pugl.h:629
@ PUGL_REALIZE_FAILED
System view realization failed.
Definition pugl.h:636
@ PUGL_REGISTRATION_FAILED
Class registration failed.
Definition pugl.h:635
@ PUGL_SET_FORMAT_FAILED
Failed to set pixel format.
Definition pugl.h:637
@ PUGL_BAD_BACKEND
Invalid or missing backend.
Definition pugl.h:631
@ PUGL_BAD_PARAMETER
Invalid parameter.
Definition pugl.h:633
@ PUGL_BACKEND_FAILED
Backend initialization failed.
Definition pugl.h:634
@ PUGL_UNKNOWN_ERROR
Unknown system error.
Definition pugl.h:630
@ PUGL_UNSUPPORTED
Unsupported operation.
Definition pugl.h:639
@ PUGL_NO_MEMORY
Failed to allocate memory.
Definition pugl.h:640
@ PUGL_BAD_CONFIGURATION
Invalid view configuration.
Definition pugl.h:632
@ PUGL_CREATE_CONTEXT_FAILED
Failed to create drawing context.
Definition pugl.h:638
void * PuglHandle
Handle for a view's opaque user data.
Definition pugl.h:837
struct PuglViewImpl PuglView
A drawable region that receives events.
Definition pugl.h:810
uintptr_t PuglNativeView
Definition pugl.h:834
PuglStatus(* PuglEventFunc)(PuglView *view, const PuglEvent *event)
A function called when an event occurs.
Definition pugl.h:910
PuglViewHint
A hint for configuring a view.
Definition pugl.h:840
struct PuglBackendImpl PuglBackend
Definition pugl.h:823
@ PUGL_DONT_CARE
Use best available value.
Definition pugl.h:864
@ PUGL_FALSE
Explicitly false.
Definition pugl.h:865
@ PUGL_TRUE
Explicitly true.
Definition pugl.h:866
@ PUGL_ALPHA_BITS
Number of bits for alpha channel.
Definition pugl.h:848
@ PUGL_DOUBLE_BUFFER
True if double buffering should be used.
Definition pugl.h:852
@ PUGL_CONTEXT_VERSION_MINOR
OpenGL context minor version.
Definition pugl.h:844
@ PUGL_USE_DEBUG_CONTEXT
True to use a debug OpenGL context.
Definition pugl.h:842
@ PUGL_BLUE_BITS
Number of bits for blue channel.
Definition pugl.h:847
@ PUGL_RED_BITS
Number of bits for red channel.
Definition pugl.h:845
@ PUGL_IGNORE_KEY_REPEAT
True if key repeat events are ignored.
Definition pugl.h:855
@ PUGL_DEPTH_BITS
Number of bits for depth buffer.
Definition pugl.h:849
@ PUGL_REFRESH_RATE
Refresh rate in Hz.
Definition pugl.h:856
@ PUGL_USE_COMPAT_PROFILE
Use compatible (not core) OpenGL profile.
Definition pugl.h:841
@ PUGL_SAMPLES
Number of samples per pixel (AA).
Definition pugl.h:851
@ PUGL_SWAP_INTERVAL
Number of frames between buffer swaps.
Definition pugl.h:853
@ PUGL_CONTEXT_VERSION_MAJOR
OpenGL context major version.
Definition pugl.h:843
@ PUGL_STENCIL_BITS
Number of bits for stencil buffer.
Definition pugl.h:850
@ PUGL_RESIZABLE
True if view should be resizable.
Definition pugl.h:854
@ PUGL_GREEN_BITS
Number of bits for green channel.
Definition pugl.h:846
@ PUGL_MIN_SIZE
Minimum size.
Definition pugl.h:878
PuglNativeView puglGetParentWindow(const PuglView *const view)
Return the parent window this view is embedded in, or null.
Definition common.c:252
const char * puglGetWindowTitle(const PuglView *const view)
Return the title of the window, or null.
Definition common.c:239
bool puglGetVisible(const PuglView *view)
Return true iff the view is currently visible.
Definition common.c:264
PuglStatus puglSetParentWindow(PuglView *view, PuglNativeView parent)
Definition common.c:245
PuglNativeView puglGetTransientParent(const PuglView *const view)
Definition common.c:258
PuglWorld * puglNewWorld(PuglWorldType type, PuglWorldFlags flags)
Definition common.c:42
PuglStatus puglSetClassName(PuglWorld *const world, const char *const name)
Definition common.c:83
uint32_t PuglWorldFlags
Bitwise OR of PuglWorldFlag values.
Definition pugl.h:692
PuglWorldType
The type of a World.
Definition pugl.h:676
struct PuglWorldImpl PuglWorld
Definition pugl.h:670
PUGL_API double puglGetTime(const PuglWorld *world)
Definition wasm.c:536
void puglSetWorldHandle(PuglWorld *world, PuglWorldHandle handle)
Definition common.c:71
void * PuglWorldHandle
Handle for the world's opaque user data.
Definition pugl.h:673
PuglWorldHandle puglGetWorldHandle(PuglWorld *world)
Get the user data for the world.
Definition common.c:77
const char * puglGetClassName(const PuglWorld *world)
Get the class name of the application, or null.
Definition common.c:90
void puglFreeWorld(PuglWorld *const world)
Free a world allocated with puglNewWorld().
Definition common.c:62
PuglStatus puglDispatchSimpleEvent(PuglView *view, const PuglEventType type)
Dispatch an event with a simple type to view.
Definition internal.c:99
void puglSetString(char **dest, const char *string)
Reallocate and set *dest to string.
Definition internal.c:40
static LilvWorld * world
Definition lilv_test.c:64
void puglFreeViewInternals(PuglView *view)
Destroy and free view internals (implemented once per platform).
Definition wasm.c:473
void puglFreeWorldInternals(PuglWorld *world)
Destroy and free world internals (implemented once per platform).
Definition wasm.c:486
PUGL_BEGIN_DECLS PuglWorldInternals * puglInitWorldInternals(PuglWorldType type, PuglWorldFlags flags)
Allocate and initialise world internals (implemented once per platform).
Definition wasm.c:24
PuglInternals * puglInitViewInternals(PuglWorld *world)
Allocate and initialise view internals (implemented once per platform).
Definition wasm.c:44
void *(* getContext)(PuglView *)
Return the puglGetContext() handle for the application, if any.
Definition types.h:88
Definition pugl.h:87
PuglInternals * impl
Definition types.h:40
PuglEventFunc eventFunc
Definition types.h:42
PuglRect frame
Definition types.h:46
PuglHandle handle
Definition types.h:41
PuglNativeView parent
Definition types.h:44
const PuglBackend * backend
Definition types.h:39
uintptr_t transientParent
Definition types.h:45
bool visible
Definition types.h:50
char * title
Definition types.h:43
PuglWorld * world
Definition types.h:38
PuglHints hints
Definition types.h:48
PuglViewSize sizeHints[PUGL_NUM_SIZE_HINTS]
Definition types.h:49
PuglSpan width
Definition types.h:26
PuglSpan height
Definition types.h:27
int PuglHints[PUGL_NUM_VIEW_HINTS]
View hints.
Definition types.h:22