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