selection.c (1330B)
1 /* Copyright ©2010 Kris Maglione <maglione.k at Gmail> 2 * See LICENSE file for license details. 3 */ 4 #include "x11.h" 5 6 static Handlers handlers; 7 8 typedef struct Data Data; 9 10 struct Data { 11 long selection; 12 void (*callback)(void*, char*); 13 void* aux; 14 }; 15 16 static bool 17 _getselection(Window *w, long selection, char *type) { 18 XConvertSelection(display, selection, xatom(type), 19 selection, w->xid, CurrentTime); 20 return true; 21 } 22 23 void 24 getselection(char *selection, void (*callback)(void*, char*), void *aux) { 25 Window *w; 26 Data *d; 27 28 d = emallocz(sizeof *d); 29 d->selection = xatom(selection); 30 d->callback = callback; 31 d->aux = aux; 32 33 w = createwindow(&scr.root, Rect(0, 0, 1, 1), 0, InputOnly, nil, 0); 34 w->aux = d; 35 sethandler(w, &handlers); 36 37 _getselection(w, d->selection, "UTF8_STRING"); 38 } 39 40 static bool 41 selection_event(Window *w, void *aux, XSelectionEvent *ev) { 42 Data *d; 43 char **ret; 44 45 d = aux; 46 if(ev->property == None && ev->target != xatom("STRING")) 47 return _getselection(w, d->selection, "STRING"); 48 else if(ev->property == None) 49 d->callback(d->aux, nil); 50 else { 51 getprop_textlist(w, atomname(ev->property), &ret); 52 delproperty(w, atomname(ev->property)); 53 d->callback(d->aux, ret ? *ret : nil); 54 free(ret); 55 } 56 destroywindow(w); 57 return false; 58 } 59 60 static Handlers handlers = { 61 .selection = selection_event, 62 }; 63