aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-05-22 12:24:59 -0400
committerDavid Robillard <d@drobilla.net>2022-05-23 16:50:43 -0400
commit2501218801437ea413091007b535d7c097801713 (patch)
treecf938dd335f8aa9b547b458f97f05e7e18d8b9d3 /examples
parent0093196a4c624da6d7f78a909a442f2e784c37aa (diff)
downloadpugl-2501218801437ea413091007b535d7c097801713.tar.gz
pugl-2501218801437ea413091007b535d7c097801713.tar.bz2
pugl-2501218801437ea413091007b535d7c097801713.zip
Add rich clipboard support
This implements a more powerful protocol for working with clipboards, which supports datatype negotiation, and fixes various issues by mapping more directly to how things work on X11.
Diffstat (limited to 'examples')
-rw-r--r--examples/pugl_clipboard_demo.c50
1 files changed, 46 insertions, 4 deletions
diff --git a/examples/pugl_clipboard_demo.c b/examples/pugl_clipboard_demo.c
index 76e42f8..9b5fb28 100644
--- a/examples/pugl_clipboard_demo.c
+++ b/examples/pugl_clipboard_demo.c
@@ -11,6 +11,7 @@
#include <math.h>
#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -64,15 +65,50 @@ onKeyPress(PuglView* const view, const PuglKeyEvent* const event)
if (event->key == 'q' || event->key == PUGL_KEY_ESCAPE) {
app->quit = 1;
} else if ((event->state & PUGL_MOD_CTRL) && event->key == 'c') {
- puglSetClipboard(view, NULL, copyString, strlen(copyString) + 1);
+ puglSetClipboard(view, "text/plain", copyString, strlen(copyString));
fprintf(stderr, "Copy \"%s\"\n", copyString);
} else if ((event->state & PUGL_MOD_CTRL) && event->key == 'v') {
- const char* type = NULL;
+ 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 char* text = (const char*)puglGetClipboard(view, &type, &len);
+ const void* data = puglGetClipboard(view, typeIndex, &len);
- fprintf(stderr, "Paste \"%s\"\n", text);
+ fprintf(stderr, "Data:\n%s\n", (const char*)data);
}
}
@@ -143,6 +179,12 @@ onEvent(PuglView* view, const PuglEvent* event)
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;
}