How to distinguish between a clipboard daemon's request and a user's request?
I'm a newbie about X11 clipboard. When I run my demo of clipboard, I get SelectionRequest from other daemons program. how i distinguish between a clipboard daemon's request and a user's request,The user's request is what I ultimately want respond. Here is my code.
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <file_path>\n", argv[0]);
return 1;
}
char *file_path = argv[1];
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot open display\n");
return 1;
}
char* uri;
int uri_len;
uri_len = strlen(file_path) + 7; // 7 bytes for "file://"
uri = (char *)malloc(uri_len + 1);
snprintf(uri, uri_len + 1, "file://%s", file_path);
char* copyurl =(char *)malloc(256);
sprintf(copyurl,"copy\n%s",uri);
Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, 0, 0);
printf("===created window id is 0x%lX\n",window);
XStoreName(display, window, file_path);
Atom clipboard = XInternAtom(display, "CLIPBOARD", False);
Atom targets = XInternAtom(display, "TARGETS", False);
Atom utf8_string = XInternAtom(display, "UTF8_STRING", True);
Atom uri_list = XInternAtom(display, "text/uri-list", False);
Atom text_plain = XInternAtom(display, "text/plain;charset=utf-8", False);
Atom gnome = XInternAtom(display, "x-special/gnome-copied-files", False);
printf("===created clipboard id is 0x%lX\n",clipboard);
XSetSelectionOwner(display, clipboard, window, CurrentTime);
XSelectionRequestEvent* xsr;
XEvent event;
while (1) {
XNextEvent(display, &event);
printf("event.type=%d\n",event.type);
switch (event.type) {
case SelectionClear:
return 0;
case SelectionRequest:
xsr = &event.xselectionrequest;
char* an ;
an = XGetAtomName(display, event.xselectionrequest.target);
printf("xsr->window=0x%lX,xsr->requestor = 0x%lX,xsr->selection = 0x%lX,xsr->target=%s\n",xsr->owner,xsr->requestor,xsr->selection,an);
XSelectionEvent ev = {0};
int R = 0;
ev.type = SelectionNotify, ev.display = xsr->display, ev.requestor = xsr->requestor,
ev.selection = xsr->selection, ev.time = xsr->time, ev.target = xsr->target, ev.property = xsr->property;
if (event.xselectionrequest.target == targets) {
Atom supported_targets[] = {targets, uri_list,utf8_string,text_plain,gnome};
R = XChangeProperty(display, event.xselectionrequest.requestor, event.xselectionrequest.property,
XA_ATOM, 32, PropModeReplace, (unsigned char *) supported_targets, sizeof(supported_targets)/sizeof(supported_targets[0]));
}
else if (event.xselectionrequest.target == text_plain || event.xselectionrequest.target == utf8_string) {
printf("********target == text_plain or utf8_string*******\n");
R = XChangeProperty(display, event.xselectionrequest.requestor, event.xselectionrequest.property,
event.xselectionrequest.target, 8, PropModeReplace, (unsigned char *) file_path, strlen(file_path));
}
else if (event.xselectionrequest.target == gnome) {
printf("********target == gnome*******\n");
R =XChangeProperty(display, event.xselectionrequest.requestor, event.xselectionrequest.property,
gnome, 8, PropModeReplace, (unsigned char *) copyurl, strlen(copyurl));
}
else if (event.xselectionrequest.target == uri_list) {
printf("********target == uri_list*******\n");
R =XChangeProperty(display, event.xselectionrequest.requestor, event.xselectionrequest.property,
uri_list, 8, PropModeReplace, (unsigned char *) uri, strlen(uri));
}
printf("R = %d\n",R);
event.xselection.property = event.xselectionrequest.property;
XSendEvent (display, ev.requestor, 0, 0, (XEvent *)&ev);
break;
}
}
XCloseDisplay(display);
return 0;
}