forked from lv2/pugl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.c
224 lines (185 loc) · 5.04 KB
/
internal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2012-2023 David Robillard <[email protected]>
// SPDX-License-Identifier: ISC
#include "internal.h"
#include "types.h"
#include "pugl/pugl.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
bool
puglIsValidSize(const PuglViewSize size)
{
return size.width && size.height;
}
void
puglEnsureHint(PuglView* const view, const PuglViewHint hint, const int value)
{
if (view->hints[hint] == PUGL_DONT_CARE) {
view->hints[hint] = value;
}
}
PuglStatus
puglSetBlob(PuglBlob* const dest, const void* const data, const size_t len)
{
if (data) {
void* const newData = realloc(dest->data, len + 1);
if (!newData) {
free(dest->data);
dest->len = 0;
return PUGL_NO_MEMORY;
}
memcpy(newData, data, len);
((char*)newData)[len] = 0;
dest->len = len;
dest->data = newData;
} else {
dest->len = 0;
dest->data = NULL;
}
return PUGL_SUCCESS;
}
void
puglSetString(char** dest, const char* string)
{
if (*dest == string) {
return;
}
const size_t len = string ? strlen(string) : 0U;
if (!len) {
free(*dest);
*dest = NULL;
} else {
*dest = (char*)realloc(*dest, len + 1U);
strncpy(*dest, string, len + 1U);
}
}
uint32_t
puglDecodeUTF8(const uint8_t* buf)
{
#define FAIL_IF(cond) \
do { \
if (cond) \
return 0xFFFD; \
} while (0)
// http://en.wikipedia.org/wiki/UTF-8
if (buf[0] < 0x80) {
return buf[0];
}
if (buf[0] < 0xC2) {
return 0xFFFD;
}
if (buf[0] < 0xE0) {
FAIL_IF((buf[1] & 0xC0U) != 0x80);
return ((uint32_t)buf[0] << 6U) + buf[1] - 0x3080U;
}
if (buf[0] < 0xF0) {
FAIL_IF((buf[1] & 0xC0U) != 0x80);
FAIL_IF(buf[0] == 0xE0 && buf[1] < 0xA0);
FAIL_IF((buf[2] & 0xC0U) != 0x80);
return ((uint32_t)buf[0] << 12U) + //
((uint32_t)buf[1] << 6U) + //
((uint32_t)buf[2] - 0xE2080U);
}
if (buf[0] < 0xF5) {
FAIL_IF((buf[1] & 0xC0U) != 0x80);
FAIL_IF(buf[0] == 0xF0 && buf[1] < 0x90);
FAIL_IF(buf[0] == 0xF4 && buf[1] >= 0x90);
FAIL_IF((buf[2] & 0xC0U) != 0x80U);
FAIL_IF((buf[3] & 0xC0U) != 0x80U);
return (((uint32_t)buf[0] << 18U) + //
((uint32_t)buf[1] << 12U) + //
((uint32_t)buf[2] << 6U) + //
((uint32_t)buf[3] - 0x3C82080U));
}
return 0xFFFD;
}
PuglStatus
puglPreRealize(PuglView* const view)
{
// Ensure that a backend with at least a configure method has been set
if (!view->backend || !view->backend->configure) {
return PUGL_BAD_BACKEND;
}
// Ensure that the view has an event handler
if (!view->eventFunc) {
return PUGL_BAD_CONFIGURATION;
}
// Ensure that the default size is set to a valid size
if (!puglIsValidSize(view->sizeHints[PUGL_DEFAULT_SIZE])) {
return PUGL_BAD_CONFIGURATION;
}
return PUGL_SUCCESS;
}
PuglStatus
puglDispatchSimpleEvent(PuglView* view, const PuglEventType type)
{
assert(type == PUGL_REALIZE || type == PUGL_UNREALIZE ||
type == PUGL_UPDATE || type == PUGL_CLOSE || type == PUGL_LOOP_ENTER ||
type == PUGL_LOOP_LEAVE);
const PuglEvent event = {{type, 0}};
return puglDispatchEvent(view, &event);
}
static inline bool
puglMustConfigure(PuglView* view, const PuglConfigureEvent* configure)
{
return !!memcmp(configure, &view->lastConfigure, sizeof(PuglConfigureEvent));
}
PuglStatus
puglConfigure(PuglView* view, const PuglEvent* event)
{
PuglStatus st = PUGL_SUCCESS;
assert(event->type == PUGL_CONFIGURE);
if (puglMustConfigure(view, &event->configure)) {
st = view->eventFunc(view, event);
view->lastConfigure = event->configure;
}
return st;
}
PuglStatus
puglDispatchEvent(PuglView* view, const PuglEvent* event)
{
PuglStatus st0 = PUGL_SUCCESS;
PuglStatus st1 = PUGL_SUCCESS;
switch (event->type) {
case PUGL_NOTHING:
break;
case PUGL_REALIZE:
assert(view->stage == PUGL_VIEW_STAGE_ALLOCATED);
if (!(st0 = view->backend->enter(view, NULL))) {
st0 = view->eventFunc(view, event);
st1 = view->backend->leave(view, NULL);
}
view->stage = PUGL_VIEW_STAGE_REALIZED;
break;
case PUGL_UNREALIZE:
assert(view->stage >= PUGL_VIEW_STAGE_REALIZED);
if (!(st0 = view->backend->enter(view, NULL))) {
st0 = view->eventFunc(view, event);
st1 = view->backend->leave(view, NULL);
}
view->stage = PUGL_VIEW_STAGE_ALLOCATED;
break;
case PUGL_CONFIGURE:
if (puglMustConfigure(view, &event->configure)) {
if (!(st0 = view->backend->enter(view, NULL))) {
st0 = puglConfigure(view, event);
st1 = view->backend->leave(view, NULL);
}
}
if (view->stage == PUGL_VIEW_STAGE_REALIZED) {
view->stage = PUGL_VIEW_STAGE_CONFIGURED;
}
break;
case PUGL_EXPOSE:
assert(view->stage == PUGL_VIEW_STAGE_CONFIGURED);
if (!(st0 = view->backend->enter(view, &event->expose))) {
st0 = view->eventFunc(view, event);
st1 = view->backend->leave(view, &event->expose);
}
break;
default:
st0 = view->eventFunc(view, event);
}
return st0 ? st0 : st1;
}