From 25a68d31ccdd82a271f8249ad4fbe35f94ab27a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Wed, 17 Apr 2019 15:20:47 +0200 Subject: [PATCH] Sends UTF-8 of clipboard. --- src/main.c | 70 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/src/main.c b/src/main.c index 0b52af4..0c85f1e 100644 --- a/src/main.c +++ b/src/main.c @@ -44,7 +44,7 @@ void get_selection(Display *display, Window window, Atom property) { incr = XInternAtom(display, "INCR", 0); if(type == incr) { - // increment size of buffer + // TODO: increment size of buffer return; } @@ -64,24 +64,54 @@ void get_selection(Display *display, Window window, Atom property) { XDeleteProperty(display, window, property); } -/*void send_selection(Display *display, XSelectionRequestEvent *sre, Atom utf8) { - XSelectionEvent se; - time_t now_tm; - char *now; - - now_tm = time(NULL); - now = ctime(&now_tm); +void send_reject(Display *display, XSelectionRequestEvent *sre) { + XSelectionEvent reply_se; #ifdef DEBUG - char *an = XGetAtomName(display, sre->property); - printf("Sending CLIPBOARD to window 0x%1x, property '%s'\n", sre->requestor, an); - if(an) - XFree(an); + char *target_name; + target_name = XGetAtomName(display, sre->target); + printf("Denying request for type `%s'\n", target_name); + if(target_name) + XFree(target_name); #endif - XChangeProperty(display, sre->requestor, sre->property, utf8, 8, PropModeReplace, data, data_size); + reply_se.type = SelectionNotify; + reply_se.requestor = sre->requestor; + reply_se.selection = sre->selection; + reply_se.target = sre->target; + reply_se.property = None; + reply_se.time = sre->time; -}*/ + XSendEvent(display, sre->requestor, 1, + NoEventMask, (XEvent*)&reply_se); +} + +void send_selection(Display *display, + XSelectionRequestEvent *sre, Atom utf8) +{ + XSelectionEvent reply_se; + +#ifdef DEBUG + char *target_name = XGetAtomName(display, sre->property); + printf("Sending CLIPBOARD to window 0x%1x, property '%s'\n", + (unsigned int)sre->requestor, target_name); + if(target_name) + XFree(target_name); +#endif + + XChangeProperty(display, sre->requestor, sre->property, + utf8, 8, PropModeReplace, (unsigned char*)data, data_size); + + reply_se.type = SelectionNotify; + reply_se.requestor = sre->requestor; + reply_se.selection = sre->selection; + reply_se.target = sre->target; + reply_se.property = sre->property; + reply_se.time = sre->time; + + XSendEvent(display, sre->requestor, 1, + NoEventMask, (XEvent*)&reply_se); +} int main() { Display *display; @@ -134,7 +164,7 @@ int main() { while(run) { XNextEvent(display, &event); - /*XSelectionRequestEvent *sre;*/ + XSelectionRequestEvent *sre; XSelectionEvent *se; switch(event.type) { @@ -154,11 +184,15 @@ int main() { 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", (unsigned int)sre->requestor);*/ + printf("New selection request:\n" + " requestor: 0x%1x\n", (unsigned int)sre->requestor); #endif + if(sre->target != utf8 || sre->property == None) + send_reject(display, sre); + else + send_selection(display, sre, utf8); break; } }