File: xclib.c

package info (click to toggle)
xclip 0.08-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 156 kB
  • ctags: 706
  • sloc: ansic: 896; makefile: 786; sh: 56
file content (549 lines) | stat: -rw-r--r-- 11,455 bytes parent folder | download | duplicates (3)
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 *  $Id: xclib.c,v 1.12 2001/12/17 06:14:40 kims Exp $
 * 
 *  xclib.c - xclip library to look after xlib mechanics for xclip
 *  Copyright (C) 2001 Kim Saunders
 *
 *  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 allocater 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);
}

/* Retrieves the contents of a selections. Arguments are:
 *
 * A display that has been opened.
 * 
 * A window
 * 
 * An event to process
 * 
 * The selection to return
 * 
 * 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,
	unsigned char **txt,
	unsigned long *len,
	unsigned int *context
)
{
	/* a property for other windows to put their selection into */
	Atom pty, inc, pty_type, targets;
	int pty_format;
		
	/* buffer for XGetWindowProperty to dump data into */
	unsigned char *buffer;
	unsigned long pty_size, pty_items;

	/* local buffer of text to return */
	unsigned char *ltxt = *txt;

	pty = XInternAtom(dpy, "XCLIP_OUT", False);
	targets = XInternAtom(dpy, "TARGETS", 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,
				XA_STRING,
				pty,
				win,
				CurrentTime
			);
			*context = XCLIB_XCOUT_SENTCONVSEL;
			return(0);
		
		case XCLIB_XCOUT_SENTCONVSEL:
			inc = XInternAtom(dpy, "INCR", False);

			if (evt.type != SelectionNotify)
				return(0);

			/* find the size and format of the data in property */
			XGetWindowProperty(
				dpy,
				win,
				pty,
				0,
				0,
				False,
				AnyPropertyType,
				&pty_type,
				&pty_format,
				&pty_items,
				&pty_size,
				&buffer
			);
			XFree(buffer);

			if (pty_type == inc)
			{
				/* start INCR mechanism by deleting property */
				XDeleteProperty(dpy, win, pty);
				XFlush(dpy);
				*context = XCLIB_XCOUT_INCR;
				return(0);
			}

			/* if it's not incr, and not format == 8, then there's
			 * nothing in the selection (that xclip understands,
			 * anyway)
			 */ 
			if (pty_format != 8)
			{
				*context = XCLIB_XCOUT_NONE;
				return(0);
			}

			/* not using INCR mechanism, just read the property */
			XGetWindowProperty(
				dpy,
				win,
				pty,
				0,
				(long)pty_size,
				False,
				AnyPropertyType,
				&pty_type,
				&pty_format,
				&pty_items,
				&pty_size,
				&buffer
			);
	
			/* finished with property, delete it */
			XDeleteProperty(dpy, win, pty);
		
			/* copy the buffer to the pointer for returned data */
			ltxt = (unsigned char *)xcmalloc(pty_items);
			memcpy(ltxt, buffer, pty_items);

			/* set the length of the returned data */
			*len = pty_items;
			*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,
				&pty_type,
				&pty_format,
				&pty_items,
				&pty_size,
				(unsigned char **)&buffer
			);

			if (pty_format != 8)
			{
				/* property does not contain text, delete it
				 * to tell the other X client that we have read
				 * it and to send the next property
				 */
				XFree(buffer);
				XDeleteProperty(dpy, win, pty);
				return(0);
			}

			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 propery contains
			 * text, we know the size.
			 */
			XGetWindowProperty(
				dpy,
				win,
				pty,
				0,
				(long)pty_size,
				False,
				AnyPropertyType,
				&pty_type,
				&pty_format,
				&pty_items,
				&pty_size,
				(unsigned char **)&buffer
			);
			
			/* allocate memory to accommodate data in *txt */
			if (*len == 0)
			{
				*len = pty_items;
				ltxt = (unsigned char *)xcmalloc(*len);
			} else
			{
				*len += pty_items;
				ltxt = (unsigned char *)xcrealloc(ltxt, *len);
			}

			/* add data to ltxt */
			memcpy(
				&ltxt[*len - pty_items],
				buffer,
				pty_items
			);

			*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 SelecionRequest 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.
 * 
 * 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,
	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 */
	Atom		inc, targets;

	targets = XInternAtom(dpy, "TARGETS", False);

	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 an property */
			if (evt.xselectionrequest.target == targets)
			{
				Atom types[2] = { targets, XA_STRING };
			
				/* send data all at once (not using INCR) */
				XChangeProperty(
					dpy,
					*win,
					*pty,
					targets,
					8,
					PropModeReplace,
					(unsigned char*) types,
					(int)sizeof(types)
			       );
			} else if (len > XC_CHUNK)
			{
				/* INCR Atom to set response property to */
				inc = XInternAtom(dpy, "INCR", False);
			
				/* 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,
					XA_STRING,
					8,
					PropModeReplace,
					(unsigned char*) txt,
					(int)len
			       );
			}
	
			/* 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);

			/* if len < XC_CHUNK, then the data was sent all at
			 * once and the transfer is now complete, return 1
			 */
			if (len > XC_CHUNK)
				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 = XC_CHUNK;

			/* 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,
					XA_STRING,
					8,
					PropModeReplace,
					&txt[*pos],
					(int)chunk_len
				);
			} else
			{
				/* make an empty property to show we've
				 * finished the transfer
				 */
				XChangeProperty(
					dpy,
					*win,
					*pty,
					XA_STRING,
					8,
					PropModeReplace,
					0,
					0	
				);
			}
			XFlush(dpy);

			/* all data has been sent, break out of the loop */
			if (!chunk_len)
				*context = XCLIB_XCIN_NONE;

			*pos += XC_CHUNK;

			/* if chunk_len == 0, we just finished the transfer,
			 * return 1
			 */
			if (chunk_len > 0)
				return(0);
			else
				return(1);
			break;
	}
	return(0);
}