1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
/*
*
*
* xclib.c - xclip library to look after xlib mechanics for xclip
* Copyright (C) 2001 Kim Saunders
* Copyright (C) 2007-2008 Peter Åstrand
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include "xcdef.h"
#include "xcprint.h"
#include "xclib.h"
/* check a pointer to allocated memory, print an error if it's null */
void
xcmemcheck(void *ptr)
{
if (ptr == NULL)
errmalloc();
}
/* wrapper for malloc that checks for errors */
void *
xcmalloc(size_t size)
{
void *mem;
mem = malloc(size);
xcmemcheck(mem);
return (mem);
}
/* wrapper for realloc that checks for errors */
void *
xcrealloc(void *ptr, size_t size)
{
void *mem;
mem = realloc(ptr, size);
xcmemcheck(mem);
return (mem);
}
/* a strdup() implementation since ANSI C doesn't include strdup() */
void *
xcstrdup(const char *string)
{
void *mem;
/* allocate a buffer big enough to hold the characters and the
* null terminator, then copy the string into the buffer
*/
mem = xcmalloc(strlen(string) + sizeof(char));
strcpy(mem, string);
return (mem);
}
/* Returns the machine-specific number of bytes per data element
* returned by XGetWindowProperty */
static size_t
mach_itemsize(int format)
{
if (format == 8)
return sizeof(char);
if (format == 16)
return sizeof(short);
if (format == 32)
return sizeof(long);
return 0;
}
/* Retrieves the contents of a selections. Arguments are:
*
* A display that has been opened.
*
* A window
*
* An event to process
*
* The selection to return
*
* The target(UTF8_STRING or XA_STRING) to return
*
* A pointer to an atom that receives the type of the data
*
* A pointer to a char array to put the selection into.
*
* A pointer to a long to record the length of the char array
*
* A pointer to an int to record the context in which to process the event
*
* Return value is 1 if the retrieval of the selection data is complete,
* otherwise it's 0.
*/
int
xcout(Display * dpy,
Window win,
XEvent evt, Atom sel, Atom target, Atom * type, unsigned char **txt, unsigned long *len,
unsigned int *context)
{
/* a property for other windows to put their selection into */
static Atom pty;
static Atom inc;
int pty_format;
/* buffer for XGetWindowProperty to dump data into */
unsigned char *buffer;
unsigned long pty_size, pty_items, pty_machsize;
/* local buffer of text to return */
unsigned char *ltxt = *txt;
if (!pty) {
pty = XInternAtom(dpy, "XCLIP_OUT", False);
}
if (!inc) {
inc = XInternAtom(dpy, "INCR", False);
}
switch (*context) {
/* there is no context, do an XConvertSelection() */
case XCLIB_XCOUT_NONE:
/* initialise return length to 0 */
if (*len > 0) {
free(*txt);
*len = 0;
}
/* send a selection request */
XConvertSelection(dpy, sel, target, pty, win, CurrentTime);
*context = XCLIB_XCOUT_SENTCONVSEL;
return (0);
case XCLIB_XCOUT_SENTCONVSEL:
if (evt.type != SelectionNotify)
return (0);
/* return failure when the current target failed */
if (evt.xselection.property == None) {
*context = XCLIB_XCOUT_BAD_TARGET;
return (0);
}
/* find the size and format of the data in property */
XGetWindowProperty(dpy,
win,
pty,
0,
0,
False,
AnyPropertyType, type, &pty_format, &pty_items, &pty_size, &buffer);
XFree(buffer);
if (*type == inc) {
/* start INCR mechanism by deleting property */
XDeleteProperty(dpy, win, pty);
XFlush(dpy);
*context = XCLIB_XCOUT_INCR;
return (0);
}
/* not using INCR mechanism, just read the property */
XGetWindowProperty(dpy,
win,
pty,
0,
(long) pty_size,
False,
AnyPropertyType, type, &pty_format, &pty_items, &pty_size, &buffer);
/* finished with property, delete it */
XDeleteProperty(dpy, win, pty);
/* compute the size of the data buffer we received */
pty_machsize = pty_items * mach_itemsize(pty_format);
/* copy the buffer to the pointer for returned data */
ltxt = (unsigned char *) xcmalloc(pty_machsize);
memcpy(ltxt, buffer, pty_machsize);
/* set the length of the returned data */
*len = pty_machsize;
*txt = ltxt;
/* free the buffer */
XFree(buffer);
*context = XCLIB_XCOUT_NONE;
/* complete contents of selection fetched, return 1 */
return (1);
case XCLIB_XCOUT_INCR:
/* To use the INCR method, we basically delete the
* property with the selection in it, wait for an
* event indicating that the property has been created,
* then read it, delete it, etc.
*/
/* make sure that the event is relevant */
if (evt.type != PropertyNotify)
return (0);
/* skip unless the property has a new value */
if (evt.xproperty.state != PropertyNewValue)
return (0);
/* check size and format of the property */
XGetWindowProperty(dpy,
win,
pty,
0,
0,
False,
AnyPropertyType,
type, &pty_format, &pty_items, &pty_size, (unsigned char **) &buffer);
if (pty_size == 0) {
/* no more data, exit from loop */
XFree(buffer);
XDeleteProperty(dpy, win, pty);
*context = XCLIB_XCOUT_NONE;
/* this means that an INCR transfer is now
* complete, return 1
*/
return (1);
}
XFree(buffer);
/* if we have come this far, the property contains
* text, we know the size.
*/
XGetWindowProperty(dpy,
win,
pty,
0,
(long) pty_size,
False,
AnyPropertyType,
type, &pty_format, &pty_items, &pty_size, (unsigned char **) &buffer);
/* compute the size of the data buffer we received */
pty_machsize = pty_items * mach_itemsize(pty_format);
/* allocate memory to accommodate data in *txt */
if (*len == 0) {
*len = pty_machsize;
ltxt = (unsigned char *) xcmalloc(*len);
}
else {
*len += pty_machsize;
ltxt = (unsigned char *) xcrealloc(ltxt, *len);
}
/* add data to ltxt */
memcpy(<xt[*len - pty_machsize], buffer, pty_machsize);
*txt = ltxt;
XFree(buffer);
/* delete property to get the next item */
XDeleteProperty(dpy, win, pty);
XFlush(dpy);
return (0);
}
return (0);
}
/* put data into a selection, in response to a SelectionRequest event from
* another window (and any subsequent events relating to an INCR transfer).
*
* Arguments are:
*
* A display
*
* A window
*
* The event to respond to
*
* A pointer to an Atom. This gets set to the property nominated by the other
* app in it's SelectionRequest. Things are likely to break if you change the
* value of this yourself.
*
* The target(UTF8_STRING or XA_STRING) to respond to
*
* A pointer to an array of chars to read selection data from.
*
* The length of the array of chars.
*
* In the case of an INCR transfer, the position within the array of chars
* that is being processed.
*
* The context that event is the be processed within.
*/
int
xcin(Display * dpy,
Window * win,
XEvent evt,
Atom * pty, Atom target, unsigned char *txt, unsigned long len, unsigned long *pos,
unsigned int *context)
{
unsigned long chunk_len; /* length of current chunk (for incr
* transfers only)
*/
XEvent res; /* response to event */
static Atom inc;
static Atom targets;
static long chunk_size;
if (!targets) {
targets = XInternAtom(dpy, "TARGETS", False);
}
if (!inc) {
inc = XInternAtom(dpy, "INCR", False);
}
/* We consider selections larger than a quarter of the maximum
request size to be "large". See ICCCM section 2.5 */
if (!chunk_size) {
chunk_size = XExtendedMaxRequestSize(dpy) / 4;
if (!chunk_size) {
chunk_size = XMaxRequestSize(dpy) / 4;
}
}
switch (*context) {
case XCLIB_XCIN_NONE:
if (evt.type != SelectionRequest)
return (0);
/* set the window and property that is being used */
*win = evt.xselectionrequest.requestor;
*pty = evt.xselectionrequest.property;
/* reset position to 0 */
*pos = 0;
/* put the data into a property */
if (evt.xselectionrequest.target == targets) {
Atom types[2] = { targets, target };
/* send data all at once (not using INCR) */
XChangeProperty(dpy,
*win,
*pty,
XA_ATOM,
32, PropModeReplace, (unsigned char *) types,
(int) (sizeof(types) / sizeof(Atom))
);
}
else if (len > chunk_size) {
/* send INCR response */
XChangeProperty(dpy, *win, *pty, inc, 32, PropModeReplace, 0, 0);
/* With the INCR mechanism, we need to know
* when the requestor window changes (deletes)
* its properties
*/
XSelectInput(dpy, *win, PropertyChangeMask);
*context = XCLIB_XCIN_INCR;
}
else {
/* send data all at once (not using INCR) */
XChangeProperty(dpy,
*win,
*pty, target, 8, PropModeReplace, (unsigned char *) txt, (int) len);
}
/* Perhaps FIXME: According to ICCCM section 2.5, we should
confirm that XChangeProperty succeeded without any Alloc
errors before replying with SelectionNotify. However, doing
so would require an error handler which modifies a global
variable, plus doing XSync after each XChangeProperty. */
/* set values for the response event */
res.xselection.property = *pty;
res.xselection.type = SelectionNotify;
res.xselection.display = evt.xselectionrequest.display;
res.xselection.requestor = *win;
res.xselection.selection = evt.xselectionrequest.selection;
res.xselection.target = evt.xselectionrequest.target;
res.xselection.time = evt.xselectionrequest.time;
/* send the response event */
XSendEvent(dpy, evt.xselectionrequest.requestor, 0, 0, &res);
XFlush(dpy);
/* don't treat TARGETS request as contents request */
if (evt.xselectionrequest.target == targets)
return 0;
/* if len < chunk_size, then the data was sent all at
* once and the transfer is now complete, return 1
*/
if (len > chunk_size)
return (0);
else
return (1);
break;
case XCLIB_XCIN_INCR:
/* length of current chunk */
/* ignore non-property events */
if (evt.type != PropertyNotify)
return (0);
/* ignore the event unless it's to report that the
* property has been deleted
*/
if (evt.xproperty.state != PropertyDelete)
return (0);
/* set the chunk length to the maximum size */
chunk_len = chunk_size;
/* if a chunk length of maximum size would extend
* beyond the end ot txt, set the length to be the
* remaining length of txt
*/
if ((*pos + chunk_len) > len)
chunk_len = len - *pos;
/* if the start of the chunk is beyond the end of txt,
* then we've already sent all the data, so set the
* length to be zero
*/
if (*pos > len)
chunk_len = 0;
if (chunk_len) {
/* put the chunk into the property */
XChangeProperty(dpy,
*win, *pty, target, 8, PropModeReplace, &txt[*pos], (int) chunk_len);
}
else {
/* make an empty property to show we've
* finished the transfer
*/
XChangeProperty(dpy, *win, *pty, target, 8, PropModeReplace, 0, 0);
}
XFlush(dpy);
/* all data has been sent, break out of the loop */
if (!chunk_len)
*context = XCLIB_XCIN_NONE;
*pos += chunk_size;
/* if chunk_len == 0, we just finished the transfer,
* return 1
*/
if (chunk_len > 0)
return (0);
else
return (1);
break;
}
return (0);
}
|