Obtains clipboard.

This commit is contained in:
Nicolás Ortega Froysa 2019-04-17 15:03:42 +02:00
parent 8f8e68a83b
commit 4405557a32

View File

@ -19,6 +19,8 @@
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
int run;
@ -29,7 +31,40 @@ void quit() {
run = 0;
}
void send_selection(Display *display, XSelectionRequestEvent *sre, Atom utf8) {
void get_selection(Display *display, Window window, Atom property) {
Atom da, incr, type;
int di;
unsigned long size, dul;
unsigned char *prop_ret = NULL;
XGetWindowProperty(display, window, property, 0, 0, 0, AnyPropertyType,
&type, &di, &dul, &size, &prop_ret);
XFree(prop_ret);
incr = XInternAtom(display, "INCR", 0);
if(type == incr)
{
// increment size of buffer
return;
}
#ifdef DEBUG
printf("Property size: %lu\n", size);
#endif
XGetWindowProperty(display, window, property, 0, size, 0, AnyPropertyType,
&da, &di, &dul, &dul, &prop_ret);
free(data);
data = malloc(size);
strcpy(data, (void*)prop_ret);
data_size = size;
#ifdef DEBUG
printf("Received data: %s\n", (char*)data);
#endif
XFree(prop_ret);
XDeleteProperty(display, window, property);
}
/*void send_selection(Display *display, XSelectionRequestEvent *sre, Atom utf8) {
XSelectionEvent se;
time_t now_tm;
char *now;
@ -46,7 +81,7 @@ void send_selection(Display *display, XSelectionRequestEvent *sre, Atom utf8) {
XChangeProperty(display, sre->requestor, sre->property, utf8, 8, PropModeReplace, data, data_size);
}
}*/
int main() {
Display *display;
@ -54,6 +89,8 @@ int main() {
int screen;
Atom selection, utf8, clip_property;
XEvent event;
data = NULL;
data_size = 0;
// get current X display
display = XOpenDisplay(NULL);
@ -81,7 +118,7 @@ int main() {
{
// take ownership of clipboard while copying contents
#ifdef DEBUG
printf("Current owner: 0x%1X\n", owner);
printf("Current owner: 0x%1X\n", (unsigned int)owner);
#endif
XConvertSelection(display, selection, utf8,
clip_property, clip_win, CurrentTime);
@ -97,36 +134,31 @@ int main() {
while(run)
{
XNextEvent(display, &event);
XSelectionRequestEvent *sre;
/*XSelectionRequestEvent *sre;*/
XSelectionEvent *se;
switch(event.type)
{
case SelectionClear:
// if someone else took the clipboard, regain ownership
#ifdef DEBUG
printf("Lost selection.\n");
#endif
XConvertSelection(display, selection, utf8,
clip_property, clip_win, CurrentTime);
break;
case SelectionNotify:
// we've been sent the new CLIPBOARD, now we take ownership of it
se = (XSelectionEvent*)&event.xselection;
if(se->property == None)
fprintf(stderr, "Conversion impossible.");
else
{
// TODO: handle new clipboard information
}
get_selection(display, clip_win, clip_property);
XSetSelectionOwner(display, selection, clip_win, CurrentTime);
break;
case SelectionRequest:
// respond to request for clipboard
sre = (XSelectionRequestEvent*) &event.xselectionrequest;
/*sre = (XSelectionRequestEvent*) &event.xselectionrequest;*/
#ifdef DEBUG
printf("New selection request:\n"
" requestor: 0x%1x\n", sre->requestor);
/*printf("New selection request:\n"
" requestor: 0x%1x\n", (unsigned int)sre->requestor);*/
#endif
send_selection(display, sre, utf8);
break;
}
}