2 * ion/ioncore/selection.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
16 #include <libextl/extl.h>
19 static char *selection_data=NULL;
20 static int selection_length;
21 static bool continuation_set=FALSE;
22 static ExtlFn continuation;
24 #define CLIPATOM(X) XA_PRIMARY
26 static Atom XA_COMPOUND_TEXT(Display *unused)
31 a=XInternAtom(ioncore_g.dpy, "COMPOUND_TEXT", False);
37 void ioncore_handle_selection_request(XSelectionRequestEvent *ev)
47 if(selection_data==NULL || ev->property==None)
52 if(!ioncore_g.use_mb && ev->target==XA_STRING){
53 Status st=XStringListToTextProperty((char **)&p, 1, &prop);
55 }else if(ioncore_g.use_mb){
56 XICCEncodingStyle style;
58 if(ev->target==XA_STRING){
61 }else if(ev->target==XA_COMPOUND_TEXT(ioncore_g.dpy)){
62 style=XCompoundTextStyle;
67 Status st=XmbTextListToTextProperty(ioncore_g.dpy, (char **)p, 1,
74 XSetTextProperty(ioncore_g.dpy, ev->requestor, &prop, ev->property);
75 sev.target=prop.encoding;
76 sev.property=ev->property;
81 sev.type=SelectionNotify;
82 sev.requestor=ev->requestor;
83 sev.selection=ev->selection;
85 XSendEvent(ioncore_g.dpy, ev->requestor, False, 0L, (XEvent*)&sev);
89 static void ins(Window win, const char *str, int n)
91 if(!continuation_set){
92 WWindow *wwin=XWINDOW_REGION_OF_T(win, WWindow);
94 window_insstr(wwin, str, n);
96 char *tmp=scopyn(str, n);
98 extl_call(continuation, "s", NULL, tmp);
105 static void insert_selection(Window win, Atom prop)
107 char **p=xwindow_get_text_property(win, prop, NULL);
109 ins(win, p[0], strlen(p[0]));
115 static void insert_cutbuffer(Window win)
120 p=XFetchBytes(ioncore_g.dpy, &n);
129 void ioncore_handle_selection(XSelectionEvent *ev)
131 Atom prop=ev->property;
132 Window win=ev->requestor;
136 insert_cutbuffer(win);
138 insert_selection(win, prop);
139 XDeleteProperty(ioncore_g.dpy, win, prop);
142 if(continuation_set){
143 extl_unref_fn(continuation);
144 continuation_set=FALSE;
149 void ioncore_clear_selection()
151 if(selection_data!=NULL){
152 free(selection_data);
158 void ioncore_set_selection_n(const char *p, int n)
160 if(selection_data!=NULL)
161 free(selection_data);
163 selection_data=ALLOC_N(char, n+1);
165 if(selection_data==NULL)
168 memcpy(selection_data, p, n);
169 selection_data[n]='\0';
172 XStoreBytes(ioncore_g.dpy, p, n);
174 XSetSelectionOwner(ioncore_g.dpy, CLIPATOM(ioncore_g.dpy),
175 DefaultRootWindow(ioncore_g.dpy),
181 * Set primary selection and cutbuffer0 to \var{p}.
184 void ioncore_set_selection(const char *p)
187 ioncore_clear_selection();
189 ioncore_set_selection_n(p, strlen(p));
193 void ioncore_request_selection_for(Window win)
197 if(continuation_set){
198 extl_unref_fn(continuation);
199 continuation_set=FALSE;
203 a=XA_COMPOUND_TEXT(ioncore_g.dpy);
205 XConvertSelection(ioncore_g.dpy, CLIPATOM(ioncore_g.dpy), a,
206 ioncore_g.atom_selection, win, CurrentTime);
211 * Request (string) selection. The function \var{fn} will be called
212 * with the selection when and if it is received.
215 void ioncore_request_selection(ExtlFn fn)
217 assert(ioncore_g.rootwins!=NULL);
218 ioncore_request_selection_for(ioncore_g.rootwins->dummy_win);
219 continuation=extl_ref_fn(fn);
220 continuation_set=TRUE;