aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/x11.c9
-rw-r--r--test/meson.build2
-rw-r--r--test/test_local_copy_paste.c124
-rw-r--r--test/test_remote_copy_paste.c170
4 files changed, 300 insertions, 5 deletions
diff --git a/src/x11.c b/src/x11.c
index 3c3059a..3ead696 100644
--- a/src/x11.c
+++ b/src/x11.c
@@ -1,5 +1,5 @@
/*
- Copyright 2012-2020 David Robillard <d@drobilla.net>
+ Copyright 2012-2021 David Robillard <d@drobilla.net>
Copyright 2013 Robin Gareus <robin@gareus.org>
Copyright 2011-2012 Ben Loftis, Harrison Consoles
@@ -1357,12 +1357,11 @@ puglSetClipboard(PuglView* const view,
const PuglX11Atoms* const atoms = &view->world->impl->atoms;
PuglStatus st = puglSetInternalClipboard(view, type, data, len);
- if (st) {
- return st;
+ if (!st) {
+ XSetSelectionOwner(impl->display, atoms->CLIPBOARD, impl->win, CurrentTime);
}
- XSetSelectionOwner(impl->display, atoms->CLIPBOARD, impl->win, CurrentTime);
- return PUGL_SUCCESS;
+ return st;
}
#ifdef HAVE_XCURSOR
diff --git a/test/meson.build b/test/meson.build
index 47ef87f..35da4c8 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -1,6 +1,8 @@
basic_tests = [
+ 'local_copy_paste',
'realize',
'redisplay',
+ 'remote_copy_paste',
'show_hide',
'size',
'strerror',
diff --git a/test/test_local_copy_paste.c b/test/test_local_copy_paste.c
new file mode 100644
index 0000000..47beaff
--- /dev/null
+++ b/test/test_local_copy_paste.c
@@ -0,0 +1,124 @@
+/*
+ Copyright 2020-2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+// Tests copy and paste within the same view
+
+#undef NDEBUG
+
+#include "test_utils.h"
+
+#include "pugl/pugl.h"
+#include "pugl/stub.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+static const uintptr_t timerId = 1u;
+
+typedef enum {
+ START,
+ EXPOSED,
+ FINISHED,
+} State;
+
+typedef struct {
+ PuglWorld* world;
+ PuglView* view;
+ PuglTestOptions opts;
+ size_t iteration;
+ State state;
+} PuglTest;
+
+static PuglStatus
+onEvent(PuglView* view, const PuglEvent* event)
+{
+ PuglTest* test = (PuglTest*)puglGetHandle(view);
+
+ if (test->opts.verbose) {
+ printEvent(event, "Event: ", true);
+ }
+
+ switch (event->type) {
+ case PUGL_EXPOSE:
+ if (test->state < EXPOSED) {
+ // Start timer on first expose
+ assert(!puglStartTimer(view, timerId, 1 / 60.0));
+ test->state = EXPOSED;
+ }
+ break;
+
+ case PUGL_TIMER:
+ assert(event->timer.id == timerId);
+
+ if (test->iteration == 0) {
+ puglSetClipboard(
+ view, "text/plain", "Copied Text", strlen("Copied Text") + 1);
+
+ } else if (test->iteration == 1) {
+ const char* type = NULL;
+ size_t len = 0;
+ const char* text = (const char*)puglGetClipboard(view, &type, &len);
+
+ assert(!strcmp(type, "text/plain"));
+ assert(!strcmp(text, "Copied Text"));
+
+ test->state = FINISHED;
+ }
+
+ ++test->iteration;
+ break;
+
+ default:
+ break;
+ }
+
+ return PUGL_SUCCESS;
+}
+
+int
+main(int argc, char** argv)
+{
+ PuglTest app = {puglNewWorld(PUGL_PROGRAM, 0),
+ NULL,
+ puglParseTestOptions(&argc, &argv),
+ 0,
+ START};
+
+ // Set up view
+ app.view = puglNewView(app.world);
+ puglSetClassName(app.world, "Pugl Test");
+ puglSetBackend(app.view, puglStubBackend());
+ puglSetHandle(app.view, &app);
+ puglSetEventFunc(app.view, onEvent);
+ puglSetDefaultSize(app.view, 512, 512);
+
+ // Create and show window
+ assert(!puglRealize(app.view));
+ assert(!puglShow(app.view));
+
+ // Run until the test is finished
+ while (app.state != FINISHED) {
+ assert(!puglUpdate(app.world, 1 / 15.0));
+ }
+
+ puglFreeView(app.view);
+ puglFreeWorld(app.world);
+
+ return 0;
+}
diff --git a/test/test_remote_copy_paste.c b/test/test_remote_copy_paste.c
new file mode 100644
index 0000000..92faca1
--- /dev/null
+++ b/test/test_remote_copy_paste.c
@@ -0,0 +1,170 @@
+/*
+ Copyright 2020-2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+// Tests copy and paste from one view to another
+
+#undef NDEBUG
+
+#include "test_utils.h"
+
+#include "pugl/pugl.h"
+#include "pugl/stub.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+static const uintptr_t copierTimerId = 1u;
+static const uintptr_t pasterTimerId = 2u;
+
+typedef enum {
+ START,
+ EXPOSED,
+ COPIED,
+ FINISHED,
+} State;
+
+typedef struct {
+ PuglWorld* world;
+ PuglView* copierView;
+ PuglView* pasterView;
+ PuglTestOptions opts;
+ State state;
+ bool copierStarted;
+ bool pasterStarted;
+} PuglTest;
+
+static PuglStatus
+onCopierEvent(PuglView* const view, const PuglEvent* const event)
+{
+ PuglTest* const test = (PuglTest*)puglGetHandle(view);
+
+ if (test->opts.verbose) {
+ printEvent(event, "Copier Event: ", true);
+ }
+
+ switch (event->type) {
+ case PUGL_EXPOSE:
+ if (!test->copierStarted) {
+ // Start timer on first expose
+ assert(!puglStartTimer(view, copierTimerId, 1 / 15.0));
+ test->copierStarted = true;
+ }
+ break;
+
+ case PUGL_TIMER:
+ assert(event->timer.id == copierTimerId);
+
+ if (test->state < COPIED) {
+ puglSetClipboard(
+ view, "text/plain", "Copied Text", strlen("Copied Text") + 1);
+ test->state = COPIED;
+ }
+
+ break;
+
+ default:
+ break;
+ }
+
+ return PUGL_SUCCESS;
+}
+
+static PuglStatus
+onPasterEvent(PuglView* const view, const PuglEvent* const event)
+{
+ PuglTest* const test = (PuglTest*)puglGetHandle(view);
+
+ if (test->opts.verbose) {
+ printEvent(event, "Paster Event: ", true);
+ }
+
+ switch (event->type) {
+ case PUGL_EXPOSE:
+ if (!test->pasterStarted) {
+ // Start timer on first expose
+ assert(!puglStartTimer(view, pasterTimerId, 1 / 60.0));
+ test->pasterStarted = true;
+ }
+ break;
+
+ case PUGL_TIMER:
+ assert(event->timer.id == pasterTimerId);
+
+ if (test->state == COPIED) {
+ const char* type = NULL;
+ size_t len = 0;
+ const char* text = (const char*)puglGetClipboard(view, &type, &len);
+
+ assert(!strcmp(type, "text/plain"));
+ assert(!strcmp(text, "Copied Text"));
+
+ test->state = FINISHED;
+ }
+
+ break;
+
+ default:
+ break;
+ }
+
+ return PUGL_SUCCESS;
+}
+
+int
+main(int argc, char** argv)
+{
+ PuglTest app = {puglNewWorld(PUGL_PROGRAM, 0),
+ NULL,
+ NULL,
+ puglParseTestOptions(&argc, &argv),
+ START,
+ false,
+ false};
+
+ // Set up copier view
+ app.copierView = puglNewView(app.world);
+ puglSetClassName(app.world, "Pugl Test Copier");
+ puglSetBackend(app.copierView, puglStubBackend());
+ puglSetHandle(app.copierView, &app);
+ puglSetEventFunc(app.copierView, onCopierEvent);
+ puglSetDefaultSize(app.copierView, 256, 256);
+
+ // Set up paster view
+ app.pasterView = puglNewView(app.world);
+ puglSetClassName(app.world, "Pugl Test Paster");
+ puglSetBackend(app.pasterView, puglStubBackend());
+ puglSetHandle(app.pasterView, &app);
+ puglSetEventFunc(app.pasterView, onPasterEvent);
+ puglSetDefaultSize(app.pasterView, 256, 256);
+
+ // Create and show both views
+ assert(!puglShow(app.copierView));
+ assert(!puglShow(app.pasterView));
+
+ // Run until the test is finished
+ while (app.state != FINISHED) {
+ assert(!puglUpdate(app.world, 1 / 60.0));
+ }
+
+ puglFreeView(app.copierView);
+ puglFreeView(app.pasterView);
+ puglFreeWorld(app.world);
+
+ return 0;
+}