1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 29 авг. 2018 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <core/types.h>
23 
24 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
25 
26 #include <test/mtest.h>
27 #include <X11/X.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xatom.h>
30 
31 MTEST_BEGIN("x11", selection)
32 
show_targets(Display * dpy,Window w,Atom p)33     void show_targets(Display *dpy, Window w, Atom p)
34     {
35         Atom type, *targets;
36         int di;
37         unsigned long i, nitems, dul;
38         unsigned char *prop_ret = NULL;
39         char *an = NULL;
40 
41         /* Read the first 1024 atoms from this list of atoms. We don't
42          * expect the selection owner to be able to convert to more than
43          * 1024 different targets. :-) */
44         XGetWindowProperty(dpy, w, p, 0, 1024 * sizeof (Atom), False, XA_ATOM,
45                            &type, &di, &nitems, &dul, &prop_ret);
46 
47         printf("Targets (actual type = %d %s):\n", int(type), XGetAtomName(dpy, type));
48         targets = (Atom *)prop_ret;
49         for (i = 0; i < nitems; i++)
50         {
51             printf("    id = %d\n", int(targets[i]));
52             an = XGetAtomName(dpy, targets[i]);
53             printf("    name '%s'\n", an);
54             if (an)
55                 XFree(an);
56         }
57         XFree(prop_ret);
58 
59         XDeleteProperty(dpy, w, p);
60     }
61 
62     MTEST_MAIN
63     {
64         Display *dpy;
65         Window target_window, root;
66         int screen;
67         Atom sel, targets, target_property;
68         XEvent ev;
69         XSelectionEvent *sev;
70 
71         dpy = XOpenDisplay(NULL);
72         MTEST_ASSERT_MSG(dpy, "Could not open X display");
73 
74         screen = DefaultScreen(dpy);
75         root = RootWindow(dpy, screen);
76 
77         sel = XInternAtom(dpy, "CLIPBOARD", False);
78         targets = XInternAtom(dpy, "TARGETS", False);
79         target_property = XInternAtom(dpy, "PENGUIN", False);
80 
81         target_window = XCreateSimpleWindow(dpy, root, -10, -10, 1, 1, 0, 0, 0);
82         XSelectInput(dpy, target_window, SelectionNotify);
83 
84         XConvertSelection(dpy, sel, targets, target_property, target_window, CurrentTime);
85 
86         while (true)
87         {
88             XNextEvent(dpy, &ev);
89             switch (ev.type)
90             {
91                 case SelectionNotify:
92                     sev = (XSelectionEvent*)&ev.xselection;
93                     MTEST_ASSERT_MSG(sev->property != None, "Conversion could not be performed.");
94                     show_targets(dpy, target_window, target_property);
95                     return;
96             }
97         }
98     }
99 MTEST_END
100 
101 #endif /* PLATFORM_LINUX || PLATFORM_BSD */
102