-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpugl_clipboard_demo.c
253 lines (213 loc) · 6.53 KB
/
pugl_clipboard_demo.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2012-2022 David Robillard <[email protected]>
// SPDX-License-Identifier: ISC
// A demonstration of using clipboards for copy/paste and drag and drop
#include "cube_view.h"
#include <puglutil/test_utils.h>
#include <pugl/gl.h>
#include <pugl/pugl.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef struct {
PuglView* view;
double xAngle;
double yAngle;
double lastMouseX;
double lastMouseY;
double lastDrawTime;
bool entered;
} CubeView;
typedef struct {
PuglWorld* world;
CubeView cube;
int quit;
bool continuous;
bool verbose;
} PuglTestApp;
static void
onDisplay(PuglView* view)
{
PuglWorld* world = puglGetWorld(view);
PuglTestApp* app = (PuglTestApp*)puglGetWorldHandle(world);
CubeView* cube = (CubeView*)puglGetHandle(view);
const double thisTime = puglGetTime(app->world);
if (app->continuous) {
const double dTime = thisTime - cube->lastDrawTime;
cube->xAngle = fmod(cube->xAngle + (dTime * 100.0), 360.0);
cube->yAngle = fmod(cube->yAngle + (dTime * 100.0), 360.0);
}
displayCube(
view, 10.0f, (float)cube->xAngle, (float)cube->yAngle, cube->entered);
cube->lastDrawTime = thisTime;
}
static void
onKeyPress(PuglView* const view, const PuglKeyEvent* const event)
{
static const char* const copyString = "Pugl test";
PuglWorld* const world = puglGetWorld(view);
PuglTestApp* const app = (PuglTestApp*)puglGetWorldHandle(world);
if (event->key == 'q' || event->key == PUGL_KEY_ESCAPE) {
app->quit = 1;
} else if ((event->state & PUGL_MOD_CTRL) && event->key == 'c') {
puglSetClipboard(view, "text/plain", copyString, strlen(copyString));
fprintf(stderr, "Copy \"%s\"\n", copyString);
} else if ((event->state & PUGL_MOD_CTRL) && event->key == 'v') {
puglPaste(view);
}
}
static void
onDataOffer(PuglView* view, const PuglDataOfferEvent* event)
{
const uint32_t numTypes = puglGetNumClipboardTypes(view);
// Print all offered types to be useful as a testing program
fprintf(stderr, "Offered %u types:\n", numTypes);
for (uint32_t t = 0; t < numTypes; ++t) {
const char* type = puglGetClipboardType(view, t);
fprintf(stderr, "\t%s\n", type);
}
// Accept the first type found that we support (namely text)
for (uint32_t t = 0; t < numTypes; ++t) {
const char* type = puglGetClipboardType(view, t);
if (!strncmp(type, "text/", 5)) {
puglAcceptOffer(view, event, t);
return;
}
}
}
static void
onData(PuglView* view, const PuglDataEvent* event)
{
const uint32_t typeIndex = event->typeIndex;
const char* const type = puglGetClipboardType(view, typeIndex);
fprintf(stderr, "Received data type: %s\n", type);
if (!strncmp(type, "text/", 5)) {
// Accept any text type
size_t len = 0;
const void* data = puglGetClipboard(view, typeIndex, &len);
fprintf(stderr, "Data:\n%s\n", (const char*)data);
}
}
static void
redisplayView(PuglTestApp* app, PuglView* view)
{
if (!app->continuous) {
puglObscureView(view);
}
}
static PuglStatus
onEvent(PuglView* view, const PuglEvent* event)
{
PuglWorld* world = puglGetWorld(view);
PuglTestApp* app = (PuglTestApp*)puglGetWorldHandle(world);
CubeView* cube = (CubeView*)puglGetHandle(view);
printEvent(event, "Event: ", app->verbose);
switch (event->type) {
case PUGL_CONFIGURE:
reshapeCube((float)event->configure.width, (float)event->configure.height);
break;
case PUGL_UPDATE:
if (app->continuous) {
puglObscureView(view);
}
break;
case PUGL_EXPOSE:
onDisplay(view);
break;
case PUGL_CLOSE:
app->quit = 1;
break;
case PUGL_KEY_PRESS:
onKeyPress(view, &event->key);
break;
case PUGL_MOTION:
#if defined(__GNUC__) && (__GNUC__ >= 5)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-conversion"
#endif
if (isnan(cube->lastMouseX)) {
cube->lastMouseX = event->motion.x;
}
if (isnan(cube->lastMouseY)) {
cube->lastMouseY = event->motion.y;
}
#if defined(__GNUC__) && (__GNUC__ >= 5)
# pragma GCC diagnostic pop
#endif
cube->xAngle -= (event->motion.x - cube->lastMouseX) / 2.0;
cube->yAngle += (event->motion.y - cube->lastMouseY) / 2.0;
cube->lastMouseX = event->motion.x;
cube->lastMouseY = event->motion.y;
redisplayView(app, view);
break;
case PUGL_POINTER_IN:
cube->entered = true;
redisplayView(app, view);
break;
case PUGL_POINTER_OUT:
cube->entered = false;
redisplayView(app, view);
break;
case PUGL_FOCUS_IN:
case PUGL_FOCUS_OUT:
redisplayView(app, view);
break;
case PUGL_DATA_OFFER:
onDataOffer(view, &event->offer);
break;
case PUGL_DATA:
onData(view, &event->data);
break;
default:
break;
}
return PUGL_SUCCESS;
}
int
main(int argc, char** argv)
{
// Parse command line options
const PuglTestOptions opts = puglParseTestOptions(&argc, &argv);
if (opts.help) {
puglPrintTestUsage(argv[0], "");
return 1;
}
PuglTestApp app = {0};
app.world = puglNewWorld(PUGL_PROGRAM, 0);
app.cube.view = puglNewView(app.world);
app.cube.xAngle = 30.0;
app.cube.yAngle = -30.0;
app.cube.lastMouseX = (double)NAN;
app.cube.lastMouseY = (double)NAN;
app.verbose = opts.verbose;
app.continuous = opts.continuous;
PuglStatus st = PUGL_SUCCESS;
PuglView* const view = app.cube.view;
puglSetWorldHandle(app.world, &app);
puglSetWorldString(app.world, PUGL_CLASS_NAME, "Pugl Test");
puglSetViewString(view, PUGL_WINDOW_TITLE, "Pugl Clipboard Demo");
puglSetSizeHint(view, PUGL_DEFAULT_SIZE, 512, 512);
puglSetSizeHint(view, PUGL_MIN_SIZE, 128, 128);
puglSetBackend(view, puglGlBackend());
puglSetViewHint(view, PUGL_CONTEXT_DEBUG, opts.errorChecking);
puglSetViewHint(view, PUGL_RESIZABLE, opts.resizable);
puglSetViewHint(view, PUGL_SAMPLES, opts.samples);
puglSetViewHint(view, PUGL_DOUBLE_BUFFER, opts.doubleBuffer);
puglSetViewHint(view, PUGL_SWAP_INTERVAL, opts.sync);
puglSetViewHint(view, PUGL_IGNORE_KEY_REPEAT, opts.ignoreKeyRepeat);
puglSetHandle(view, &app.cube);
puglSetEventFunc(view, onEvent);
if ((st = puglRealize(view))) {
return logError("Failed to realize view (%s)\n", puglStrerror(st));
}
if ((st = puglShow(view, PUGL_SHOW_RAISE))) {
return logError("Failed to show view (%s)\n", puglStrerror(st));
}
while (!app.quit) {
puglUpdate(app.world, app.continuous ? 0.0 : -1.0);
}
puglFreeView(app.cube.view);
puglFreeWorld(app.world);
return 0;
}