Improved closing application.

This commit is contained in:
Nicolás Ortega Froysa 2019-04-17 17:09:40 +02:00
parent 25a68d31cc
commit e1dbccf7f6

View File

@ -23,21 +23,28 @@
#include <string.h> #include <string.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
Display *display;
Window clip_win;
int run; int run;
void *data; void *data;
size_t data_size; size_t data_size;
void quit() { void quit() {
run = 0; 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; Atom da, incr, type;
int di; int di;
unsigned long size, dul; unsigned long size, dul;
unsigned char *prop_ret = NULL; 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); &type, &di, &dul, &size, &prop_ret);
XFree(prop_ret); XFree(prop_ret);
@ -51,7 +58,7 @@ void get_selection(Display *display, Window window, Atom property) {
#ifdef DEBUG #ifdef DEBUG
printf("Property size: %lu\n", size); printf("Property size: %lu\n", size);
#endif #endif
XGetWindowProperty(display, window, property, 0, size, 0, AnyPropertyType, XGetWindowProperty(display, clip_win, property, 0, size, 0, AnyPropertyType,
&da, &di, &dul, &dul, &prop_ret); &da, &di, &dul, &dul, &prop_ret);
free(data); free(data);
data = malloc(size); data = malloc(size);
@ -61,10 +68,10 @@ void get_selection(Display *display, Window window, Atom property) {
printf("Received data: %s\n", (char*)data); printf("Received data: %s\n", (char*)data);
#endif #endif
XFree(prop_ret); 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; XSelectionEvent reply_se;
#ifdef DEBUG #ifdef DEBUG
@ -86,8 +93,7 @@ void send_reject(Display *display, XSelectionRequestEvent *sre) {
NoEventMask, (XEvent*)&reply_se); NoEventMask, (XEvent*)&reply_se);
} }
void send_selection(Display *display, void send_selection(XSelectionRequestEvent *sre, Atom utf8)
XSelectionRequestEvent *sre, Atom utf8)
{ {
XSelectionEvent reply_se; XSelectionEvent reply_se;
@ -114,11 +120,9 @@ void send_selection(Display *display,
} }
int main() { int main() {
Display *display; Window root, owner;
Window clip_win, root, owner;
int screen; int screen;
Atom selection, utf8, clip_property; Atom selection, utf8, clip_property;
XEvent event;
data = NULL; data = NULL;
data_size = 0; data_size = 0;
@ -135,7 +139,7 @@ int main() {
// create a dummy window for tinyclip // create a dummy window for tinyclip
clip_win = XCreateSimpleWindow(display, root, -10, -10, 1, 1, 0, 0, 0); clip_win = XCreateSimpleWindow(display, root, -10, -10, 1, 1, 0, 0, 0);
XSelectInput(display, clip_win, XSelectInput(display, clip_win,
SelectionClear | SelectionRequest | SelectionNotify); SelectionClear | SelectionRequest | SelectionNotify | ClientMessage);
// create atoms for selections // create atoms for selections
selection = XInternAtom(display, "CLIPBOARD", 0); selection = XInternAtom(display, "CLIPBOARD", 0);
@ -161,11 +165,15 @@ int main() {
run = 1; run = 1;
signal(SIGINT, quit); signal(SIGINT, quit);
Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", 0);
XSetWMProtocols(display, clip_win, &wmDeleteMessage, 1);
while(run) while(run)
{ {
XNextEvent(display, &event); XEvent event;
XSelectionRequestEvent *sre; XSelectionRequestEvent *sre;
XSelectionEvent *se; XSelectionEvent *se;
XNextEvent(display, &event);
switch(event.type) switch(event.type)
{ {
case SelectionClear: case SelectionClear:
@ -179,7 +187,7 @@ int main() {
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); get_selection(clip_property);
XSetSelectionOwner(display, selection, clip_win, CurrentTime); XSetSelectionOwner(display, selection, clip_win, CurrentTime);
break; break;
case SelectionRequest: case SelectionRequest:
@ -190,9 +198,11 @@ int main() {
" requestor: 0x%1x\n", (unsigned int)sre->requestor); " requestor: 0x%1x\n", (unsigned int)sre->requestor);
#endif #endif
if(sre->target != utf8 || sre->property == None) if(sre->target != utf8 || sre->property == None)
send_reject(display, sre); send_reject(sre);
else else
send_selection(display, sre, utf8); send_selection(sre, utf8);
break;
case ClientMessage:
break; break;
} }
} }