aboutsummaryrefslogtreecommitdiffstats
path: root/src/r-x11.c
blob: 5563178f61f0ae7e990836916cd194e337a54deb (plain)
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
/***
	This file is part of LockPassword
	Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
***/

#if defined(DISPLAY)

#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>

#include "constants.h"
#include "r-x11.h"
#include "output.h"

static Atom X_utf8;

static void send_data(Display *dpy, XSelectionRequestEvent *sev,
	const char *data)
{
	XSelectionEvent ssev;
	Atom target = sev->target;
	char *an;

	an = XGetAtomName(dpy, target);
	if(an) {
		dbgprint("Request of type '%s'\n", an);
		XFree(an);
	}

	/* All of these should match the values of the request. */
	ssev.type = SelectionNotify;
	ssev.requestor = sev->requestor;
	ssev.selection = sev->selection;
	ssev.target = target;
	ssev.property = None; // means refusal
	ssev.time = sev->time;

	if(target == X_utf8) {
		an = XGetAtomName(dpy, sev->property);
		if(an) {
			dbgprint("Sending data, property '%s'\n", an);
			XFree(an);
		}

		ssev.property = sev->property;
		XChangeProperty(dpy, sev->requestor, sev->property, X_utf8, 8,
			PropModeReplace, (unsigned char *)data, strlen(data));
	}
	else {
		dbgprint("No valid target. Refuse request.\n");
	}

	XSendEvent(dpy, sev->requestor, True, NoEventMask, (XEvent *)&ssev);
}

int run_clipboard(const char *data)
{
	Display *dpy;
	Window owner, root;
	int screen;
	Atom sel;
	XEvent ev;
	XSelectionRequestEvent *sev;

	dpy = XOpenDisplay(NULL); // means use env $DISPLAY
	if(!dpy) {
		print_error("Error: Open X display failed\n");
		return 1;
	}

	screen = DefaultScreen(dpy);
	root = RootWindow(dpy, screen);
	owner = XCreateSimpleWindow(dpy, root, -10, -10, 1, 1, 0, 0, 0);

	sel = XInternAtom(dpy, "CLIPBOARD", False);
	X_utf8 = XInternAtom(dpy, "UTF8_STRING", False);

	XSetSelectionOwner(dpy, sel, owner, CurrentTime);
	for(;;)
	{
		XNextEvent(dpy, &ev);
		switch(ev.type) {
		case SelectionClear:
			dbgprint("Lost 'CLIPBOARD' ownership\n");
			return 2;
		case SelectionRequest:
			sev = (XSelectionRequestEvent *)&ev.xselectionrequest;
			dbgprint("SelectionRequest by 0x%lx\n", sev->requestor);

			send_data(dpy, sev, data);
		}
	}
}

#endif /* defined(DISPLAY) */