File: printbuffer.c

package info (click to toggle)
ding 1.0-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,520 kB
  • ctags: 44
  • sloc: tcl: 1,322; ansic: 159; sh: 85; makefile: 60; perl: 13
file content (238 lines) | stat: -rw-r--r-- 4,850 bytes parent folder | download
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
/* printbuffer is shamelessly stolen from the Steak 1.6 package
 * which can be found at
 * http://www.tm.informatik.uni-frankfurt.de/~razi/steak/steak.html
 *
 *  Xsteak is copyrighted by Razi Lotfi-Tabrizi and is licensed through
 *  the GNU General Public License. Read the COPYING file for the complete
 * license.
 *
 * Compile this with:
 * gcc -I/usr/X11R6/include -L/usr/X11R6/lib -lXt -lX11 printbuffer.c -o printbuffer
 */

#include <ctype.h>
#include <stdio.h>
#include <string.h>			/* for strcmp() */
#ifdef __STDC__
#include <stdlib.h>			/* for exit()... */
#endif
#ifndef VMS
#include <unistd.h>			/* for read(), write() */
#endif
#include <X11/Xatom.h>			/* for pre-defined atom names */
#include <X11/StringDefs.h>		/* for XtNforeground et.al.*/
/* #include <X11/Intrinsic.h> */
#include <X11/IntrinsicP.h>		/* needed for CorePart et.al.*/




#define	_printf		(void) printf
#define	_fprintf	(void) fprintf
#define	_sprintf	(void) sprintf

#define	eq(a,b)		(strcmp((a),(b)) == 0)
#define	min(a,b)	((a) < (b)? (a): (b))
#define	max(a,b)	((a) > (b)? (a): (b))

#define	XtNbufferCount	"bufferCount"		/* Application resources */
#define	XtCBufferCount	"BufferCount"
#define	XtNlayout	"layout"
#define	XtCLayout	"Layout"

#define	PGM_NAME	"printpuffer"
#define	PGM_CLASS	"Printpuffer"
#define	BUFINC		2048

static Display		*dpy;
static Window		root;
static XtAppContext	app;
static Widget		top, box, *wdg;
static Atom		delwin;
static Atom		*atom;
static int		natoms, nbuffs;
static Window		seln;



/*
 * Fetch the contents of cut buffer n from the root window.
 */
static char *
fetch_buffer(a, nb)
Atom	a;
int	*nb;
{
	Atom		actl_type;
	int		actl_format;
	unsigned long	nitems, after;
	char		*data;

	*nb = 0;
	if (XGetWindowProperty(dpy, root, a,
			0L, 10000000L, False, XA_STRING,
			&actl_type, &actl_format, &nitems,
			&after, (unsigned char **) &data) != Success)
		return NULL;

	if (actl_type == XA_STRING && actl_format != 32) {
		*nb = nitems;
		return data;
	}
	if (data != NULL)
		XFree(data);
	return NULL;
}


/*
 * Print the contents of a cut buffer on stdout.
 */
static void
doprint(n, ptr, nb)
int	n;
char	*ptr;
int	nb;
{
	char	tmp[32];

	if (!atom[0]) {
		_sprintf(tmp, "CUT_BUFFER%d", 0);
		atom[0] = XInternAtom(dpy, tmp, (Bool) True);
       }
	if (atom[0]) {
		ptr = fetch_buffer(atom[0], &nb);
		if (write(1, ptr, nb) != nb) {
			_fprintf(stderr, "Write error\n");
			exit(1);
		}
		XFree(ptr);
	}
}



static void
timeout(arg, id)
char	*arg;
XtIntervalId *id;
{
	exit(2);
}

/*
 * Copy the PRIMARY selection into a cut buffer.
 */
static void
dogetseln(n, ptr, nb)
int	n;
char	*ptr;
int	nb;
{
	char	*data;
	int	nbytes;
	XEvent	event;
	char	tmp[32];


	if (!atom[0]) {
		_sprintf(tmp, "CUT_BUFFER%d", 0);
		atom[0] = XInternAtom(dpy, tmp, (Bool) False);
	}

	if (XGetSelectionOwner(dpy, XA_PRIMARY) == None) {
		return;
	}

	XSelectInput(dpy, root, PropertyChangeMask);
	XConvertSelection(dpy, XA_PRIMARY, XA_STRING, atom[0], root, CurrentTime);
	XtAppAddTimeOut(app, 3000, timeout, (char *)0);

	for (;;) {
		XtAppNextEvent(app, &event);
		if (event.type == PropertyNotify
			&& event.xproperty.window == root
			&& event.xproperty.atom == atom[0]
			&& event.xproperty.state == PropertyNewValue) {
			return;
		}
		XtDispatchEvent(&event);	/* This cannot happen. !!?? */
	}
}


typedef struct {
	int	nbuffs;
	char	*layout;
} ares_t, *ares_ptr;

static ares_t	ares;

static XtResource	res[] = {
#define offset(field)		XtOffset(ares_ptr, field)
	{ XtNbufferCount, XtCBufferCount, XtRInt, sizeof(int), 
		offset(nbuffs), XtRImmediate, (XtPointer) 8 },
	{ XtNlayout, XtCLayout, XtRString, sizeof(char *),
		offset(layout), XtRImmediate, "horiz" },
#undef offset
};



static char		*def[] = {		/* default resource values */
	".bufferCount:		8",
	".layout:		horizontal",
	"*font:			fixed",
	"*Buffer.width:		60",
	"*Buffer.height:	60",
	0,
};

static XrmOptionDescRec opt[] = {
	{ "-n",	".bufferCount",	XrmoptionSepArg, (caddr_t) 8 },
	{ "-l",	".layout",	XrmoptionSepArg, (caddr_t) "horiz" },
};



main(argc, argv)
int	argc;
char	**argv;
{
	int	i, n;
	char	**p;
	char	name[16];
	Arg	args[3];

	/*
	 * Set up the atoms that we already know about.
	 */
	natoms = 1;
	atom = (Atom *) XtMalloc(natoms * sizeof(Atom));
	atom[0] = XA_CUT_BUFFER0;

	/*
	 * Initialize the toolkit, parse the command line,
	 * initialize the resources database, and find out
	 * how many buffers to deal with.
	 */
	top = XtAppInitialize(&app, PGM_CLASS, opt, 2, &argc, argv, def, 0, 0);
	dpy = XtDisplay(top);
	root = RootWindow(dpy, DefaultScreen(dpy));

	XtGetApplicationResources(top, &ares, res, XtNumber(res), 0, 0);
	nbuffs = max(ares.nbuffs, 1);


       /**********************************************************/
       
       dogetseln(0, (char *)0, 0); 
       doprint(0, (char *)0, 0);


/*        exit(0); */
	return 0;
}