diff options
author | Aurélien Aptel <aurelien.aptel@gmail.com> | 2010-11-08 19:49:13 +0100 |
---|---|---|
committer | Aurélien Aptel <aurelien.aptel@gmail.com> | 2010-11-08 19:49:13 +0100 |
commit | d581bfccd7c2987e12809383d8ce1f833529b715 (patch) | |
tree | 654049ff217fe8ef99a6bc230beace2b344eae02 /st.c | |
parent | b1e6099220f7e10d88adb8d960db27f63f1d8ccb (diff) | |
download | st-d581bfccd7c2987e12809383d8ce1f833529b715.tar.gz st-d581bfccd7c2987e12809383d8ce1f833529b715.zip |
selection code cleanup.
Diffstat (limited to 'st.c')
-rw-r--r-- | st.c | 97 |
1 files changed, 24 insertions, 73 deletions
@@ -286,35 +286,24 @@ static char *getseltext() { | |||
286 | 286 | ||
287 | static void selection_notify(XEvent *e) { | 287 | static void selection_notify(XEvent *e) { |
288 | unsigned long nitems; | 288 | unsigned long nitems; |
289 | unsigned long length; | 289 | unsigned long ofs, rem; |
290 | int format, res; | 290 | int format; |
291 | unsigned char *data; | 291 | unsigned char *data; |
292 | Atom type; | 292 | Atom type; |
293 | 293 | ||
294 | res = XGetWindowProperty(xw.dis, xw.win, XA_PRIMARY, 0, 0, False, | 294 | ofs = 0; |
295 | AnyPropertyType, &type, &format, &nitems, &length, &data); | 295 | do { |
296 | switch(res) { | 296 | if(XGetWindowProperty(xw.dis, xw.win, XA_PRIMARY, ofs, BUFSIZ/4, |
297 | case BadAtom: | 297 | False, AnyPropertyType, &type, &format, |
298 | case BadValue: | 298 | &nitems, &rem, &data)) { |
299 | case BadWindow: | 299 | fprintf(stderr, "Clipboard allocation failed\n"); |
300 | fprintf(stderr, "Invalid paste, XGetWindowProperty0"); | ||
301 | return; | 300 | return; |
302 | } | 301 | } |
303 | |||
304 | res = XGetWindowProperty(xw.dis, xw.win, XA_PRIMARY, 0, length, False, | ||
305 | AnyPropertyType, &type, &format, &nitems, &length, &data); | ||
306 | switch(res) { | ||
307 | case BadAtom: | ||
308 | case BadValue: | ||
309 | case BadWindow: | ||
310 | fprintf(stderr, "Invalid paste, XGetWindowProperty0"); | ||
311 | return; | ||
312 | } | ||
313 | |||
314 | if(data) { | ||
315 | ttywrite((const char *) data, nitems * format / 8); | 302 | ttywrite((const char *) data, nitems * format / 8); |
316 | XFree(data); | 303 | XFree(data); |
317 | } | 304 | /* number of 32-bit chunks returned */ |
305 | ofs += nitems * format / 32; | ||
306 | } while(rem > 0); | ||
318 | } | 307 | } |
319 | 308 | ||
320 | static void selpaste() { | 309 | static void selpaste() { |
@@ -325,7 +314,6 @@ static void selection_request(XEvent *e) | |||
325 | { | 314 | { |
326 | XSelectionRequestEvent *xsre; | 315 | XSelectionRequestEvent *xsre; |
327 | XSelectionEvent xev; | 316 | XSelectionEvent xev; |
328 | int res; | ||
329 | Atom xa_targets; | 317 | Atom xa_targets; |
330 | 318 | ||
331 | xsre = (XSelectionRequestEvent *) e; | 319 | xsre = (XSelectionRequestEvent *) e; |
@@ -341,70 +329,33 @@ static void selection_request(XEvent *e) | |||
341 | if(xsre->target == xa_targets) { | 329 | if(xsre->target == xa_targets) { |
342 | /* respond with the supported type */ | 330 | /* respond with the supported type */ |
343 | Atom string = XA_STRING; | 331 | Atom string = XA_STRING; |
344 | res = XChangeProperty(xsre->display, xsre->requestor, xsre->property, XA_ATOM, 32, | 332 | XChangeProperty(xsre->display, xsre->requestor, xsre->property, |
345 | PropModeReplace, (unsigned char *) &string, 1); | 333 | XA_ATOM, 32, PropModeReplace, |
346 | switch(res) { | 334 | (unsigned char *) &string, 1); |
347 | case BadAlloc: | 335 | xev.property = xsre->property; |
348 | case BadAtom: | ||
349 | case BadMatch: | ||
350 | case BadValue: | ||
351 | case BadWindow: | ||
352 | fprintf(stderr, "Error in selection_request, TARGETS"); | ||
353 | break; | ||
354 | default: | ||
355 | xev.property = xsre->property; | ||
356 | } | ||
357 | } else if(xsre->target == XA_STRING) { | 336 | } else if(xsre->target == XA_STRING) { |
358 | res = XChangeProperty(xsre->display, xsre->requestor, xsre->property, | 337 | XChangeProperty(xsre->display, xsre->requestor, xsre->property, |
359 | xsre->target, 8, PropModeReplace, (unsigned char *) sel.clip, | 338 | xsre->target, 8, PropModeReplace, |
360 | strlen(sel.clip)); | 339 | (unsigned char *) sel.clip, strlen(sel.clip)); |
361 | switch(res) { | 340 | xev.property = xsre->property; |
362 | case BadAlloc: | ||
363 | case BadAtom: | ||
364 | case BadMatch: | ||
365 | case BadValue: | ||
366 | case BadWindow: | ||
367 | fprintf(stderr, "Error in selection_request, XA_STRING"); | ||
368 | break; | ||
369 | default: | ||
370 | xev.property = xsre->property; | ||
371 | } | ||
372 | } | 341 | } |
373 | 342 | ||
374 | /* all done, send a notification to the listener */ | 343 | /* all done, send a notification to the listener */ |
375 | res = XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev); | 344 | if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev)) |
376 | switch(res) { | 345 | fprintf(stderr, "Error sending SelectionNotify event\n"); |
377 | case 0: | ||
378 | case BadValue: | ||
379 | case BadWindow: | ||
380 | fprintf(stderr, "Error in selection_requested, XSendEvent"); | ||
381 | } | ||
382 | } | 346 | } |
383 | 347 | ||
384 | static void selcopy(char *str) { | 348 | static void selcopy(char *str) { |
385 | /* register the selection for both the clipboard and the primary */ | 349 | /* register the selection for both the clipboard and the primary */ |
386 | Atom clipboard; | 350 | Atom clipboard; |
387 | int res; | ||
388 | 351 | ||
389 | free(sel.clip); | 352 | free(sel.clip); |
390 | sel.clip = str; | 353 | sel.clip = str; |
391 | 354 | ||
392 | res = XSetSelectionOwner(xw.dis, XA_PRIMARY, xw.win, CurrentTime); | 355 | XSetSelectionOwner(xw.dis, XA_PRIMARY, xw.win, CurrentTime); |
393 | switch(res) { | ||
394 | case BadAtom: | ||
395 | case BadWindow: | ||
396 | fprintf(stderr, "Invalid copy, XSetSelectionOwner"); | ||
397 | return; | ||
398 | } | ||
399 | 356 | ||
400 | clipboard = XInternAtom(xw.dis, "CLIPBOARD", 0); | 357 | clipboard = XInternAtom(xw.dis, "CLIPBOARD", 0); |
401 | res = XSetSelectionOwner(xw.dis, clipboard, xw.win, CurrentTime); | 358 | XSetSelectionOwner(xw.dis, clipboard, xw.win, CurrentTime); |
402 | switch(res) { | ||
403 | case BadAtom: | ||
404 | case BadWindow: | ||
405 | fprintf(stderr, "Invalid copy, XSetSelectionOwner"); | ||
406 | return; | ||
407 | } | ||
408 | 359 | ||
409 | XFlush(xw.dis); | 360 | XFlush(xw.dis); |
410 | } | 361 | } |