From e1dbccf7f6880964559668e3ce7efb6cab15a2a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Wed, 17 Apr 2019 17:09:40 +0200 Subject: [PATCH] Improved closing application. --- src/main.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 0c85f1e..040b7d7 100644 --- a/src/main.c +++ b/src/main.c @@ -23,21 +23,28 @@ #include #include +Display *display; +Window clip_win; int run; void *data; size_t data_size; void quit() { run = 0; + XClientMessageEvent cme; + cme.type = ClientMessage; + cme.format = 32; + XSendEvent(display, clip_win, 0, 0, (XEvent*)&cme); + XFlush(display); } -void get_selection(Display *display, Window window, Atom property) { +void get_selection(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, + XGetWindowProperty(display, clip_win, property, 0, 0, 0, AnyPropertyType, &type, &di, &dul, &size, &prop_ret); XFree(prop_ret); @@ -51,7 +58,7 @@ void get_selection(Display *display, Window window, Atom property) { #ifdef DEBUG printf("Property size: %lu\n", size); #endif - XGetWindowProperty(display, window, property, 0, size, 0, AnyPropertyType, + XGetWindowProperty(display, clip_win, property, 0, size, 0, AnyPropertyType, &da, &di, &dul, &dul, &prop_ret); free(data); data = malloc(size); @@ -61,10 +68,10 @@ void get_selection(Display *display, Window window, Atom property) { printf("Received data: %s\n", (char*)data); #endif XFree(prop_ret); - XDeleteProperty(display, window, property); + XDeleteProperty(display, clip_win, property); } -void send_reject(Display *display, XSelectionRequestEvent *sre) { +void send_reject(XSelectionRequestEvent *sre) { XSelectionEvent reply_se; #ifdef DEBUG @@ -86,8 +93,7 @@ void send_reject(Display *display, XSelectionRequestEvent *sre) { NoEventMask, (XEvent*)&reply_se); } -void send_selection(Display *display, - XSelectionRequestEvent *sre, Atom utf8) +void send_selection(XSelectionRequestEvent *sre, Atom utf8) { XSelectionEvent reply_se; @@ -114,11 +120,9 @@ void send_selection(Display *display, } int main() { - Display *display; - Window clip_win, root, owner; + Window root, owner; int screen; Atom selection, utf8, clip_property; - XEvent event; data = NULL; data_size = 0; @@ -135,7 +139,7 @@ int main() { // create a dummy window for tinyclip clip_win = XCreateSimpleWindow(display, root, -10, -10, 1, 1, 0, 0, 0); XSelectInput(display, clip_win, - SelectionClear | SelectionRequest | SelectionNotify); + SelectionClear | SelectionRequest | SelectionNotify | ClientMessage); // create atoms for selections selection = XInternAtom(display, "CLIPBOARD", 0); @@ -161,11 +165,15 @@ int main() { run = 1; signal(SIGINT, quit); + Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", 0); + XSetWMProtocols(display, clip_win, &wmDeleteMessage, 1); + while(run) { - XNextEvent(display, &event); + XEvent event; XSelectionRequestEvent *sre; XSelectionEvent *se; + XNextEvent(display, &event); switch(event.type) { case SelectionClear: @@ -179,7 +187,7 @@ int main() { if(se->property == None) fprintf(stderr, "Conversion impossible."); else - get_selection(display, clip_win, clip_property); + get_selection(clip_property); XSetSelectionOwner(display, selection, clip_win, CurrentTime); break; case SelectionRequest: @@ -190,9 +198,11 @@ int main() { " requestor: 0x%1x\n", (unsigned int)sre->requestor); #endif if(sre->target != utf8 || sre->property == None) - send_reject(display, sre); + send_reject(sre); else - send_selection(display, sre, utf8); + send_selection(sre, utf8); + break; + case ClientMessage: break; } }