forked from sigmike/autocutsel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocutsel.c
390 lines (335 loc) · 10.8 KB
/
autocutsel.c
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
/*
* autocutsel by Michael Witrant <mike @ lepton . fr>
* Synchronizes the cutbuffer and the selection
* Copyright (c) 2001-2006 Michael Witrant.
*
* Most code taken from:
* * clear-cut-buffers.c by "E. Jay Berkenbilt" <ejb @ ql . org>
* in this messages:
* http://boudicca.tux.org/mhonarc/ma-linux/2001-Feb/msg00824.html
*
* * xcutsel.c by Ralph Swick, DEC/Project Athena
* from the XFree86 project: http://www.xfree86.org/
*
* 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
*
* This program is distributed under the terms
* of the GNU General Public License (read the COPYING file)
*
*/
#include "common.h"
static XrmOptionDescRec optionDesc[] = {
{"-selection", "selection", XrmoptionSepArg, NULL},
{"-select", "selection", XrmoptionSepArg, NULL},
{"-sel", "selection", XrmoptionSepArg, NULL},
{"-s", "selection", XrmoptionSepArg, NULL},
{"-cutbuffer", "cutBuffer", XrmoptionSepArg, NULL},
{"-cut", "cutBuffer", XrmoptionSepArg, NULL},
{"-c", "cutBuffer", XrmoptionSepArg, NULL},
{"-debug", "debug", XrmoptionNoArg, "on"},
{"-d", "debug", XrmoptionNoArg, "on"},
{"-verbose", "verbose", XrmoptionNoArg, "on"},
{"-v", "verbose", XrmoptionNoArg, "on"},
{"-fork", "fork", XrmoptionNoArg, "on"},
{"-f", "fork", XrmoptionNoArg, "on"},
{"-pause", "pause", XrmoptionSepArg, NULL},
{"-p", "pause", XrmoptionSepArg, NULL},
{"-buttonup", "buttonup", XrmoptionNoArg, "on"},
};
int Syntax(char *call)
{
fprintf (stderr,
"usage: %s [-selection <name>] [-cutbuffer <number>]"
" [-pause <milliseconds>] [-debug] [-verbose] [-fork] [-buttonup]\n",
call);
exit (1);
}
#define Offset(field) XtOffsetOf(OptionsRec, field)
static XtResource resources[] = {
{"selection", "Selection", XtRString, sizeof(String),
Offset(selection_name), XtRString, "CLIPBOARD"},
{"cutBuffer", "CutBuffer", XtRInt, sizeof(int),
Offset(buffer), XtRImmediate, (XtPointer)0},
{"debug", "Debug", XtRString, sizeof(String),
Offset(debug_option), XtRString, "off"},
{"verbose", "Verbose", XtRString, sizeof(String),
Offset(verbose_option), XtRString, "off"},
{"fork", "Fork", XtRString, sizeof(String),
Offset(fork_option), XtRString, "off"},
{"kill", "kill", XtRString, sizeof(String),
Offset(kill), XtRString, "off"},
{"pause", "Pause", XtRInt, sizeof(int),
Offset(pause), XtRImmediate, (XtPointer)500},
{"buttonup", "ButtonUp", XtRString, sizeof(String),
Offset(buttonup_option), XtRString, "off"},
};
#undef Offset
static void CloseStdFds()
{
int fd;
for (fd = 0; fd < 3; fd++)
close (fd);
}
static RETSIGTYPE Terminate(int caught)
{
exit (0);
}
static void TrapSignals()
{
int catch;
struct sigaction action;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
for (catch = 1; catch < NSIG; catch++) {
if (catch != SIGKILL) {
action.sa_handler = Terminate;
sigaction (catch, &action, (struct sigaction *) 0);
}
}
}
// Called when we no longer own the selection
static void LoseSelection(Widget w, Atom *selection)
{
if (options.debug)
printf("Selection lost\n");
options.own_selection = 0;
}
// Returns true if value (or length) is different
// than current ones.
static int ValueDiffers(char *value, int length)
{
return (!options.value ||
length != options.length ||
memcmp(options.value, value, options.length));
}
// Update the current value
static void ChangeValue(char *value, int length)
{
if (options.value)
XtFree(options.value);
options.length = length;
options.value = XtMalloc(options.length);
if (!options.value)
printf("WARNING: Unable to allocate memory to store the new value\n");
else {
memcpy(options.value, value, options.length);
if (options.debug) {
printf("New value saved: ");
PrintValue(options.value, options.length);
printf("\n");
}
}
}
// Called just before owning the selection, to ensure we don't
// do it if the selection already has the same value
static void OwnSelectionIfDiffers(Widget w, XtPointer client_data,
Atom *selection, Atom *type, XtPointer value,
unsigned long *received_length, int *format)
{
int length = *received_length;
if (*type == 0 ||
*type == XT_CONVERT_FAIL ||
length == 0 ||
ValueDiffers(value, length)) {
if (options.debug)
printf("Selection is out of date. Owning it\n");
if (options.verbose)
{
printf("cut -> sel: ");
PrintValue(options.value, options.length);
printf("\n");
}
if (XtOwnSelection(box, options.selection,
0, //XtLastTimestampProcessed(dpy),
ConvertSelection, LoseSelection, NULL) == True) {
if (options.debug)
printf("Selection owned\n");
options.own_selection = 1;
}
else
printf("WARNING: Unable to own selection!\n");
}
XtFree(value);
}
// Look for change in the buffer, and update the selection if necessary
static void CheckBuffer()
{
char *value;
int length;
value = XFetchBuffer(dpy, &length, buffer);
if (length > 0 && ValueDiffers(value, length)) {
if (options.debug) {
printf("Buffer changed: ");
PrintValue(value, length);
printf("\n");
}
ChangeValue(value, length);
XtGetSelectionValue(box, selection, XInternAtom(dpy, "UTF8_STRING", False),
OwnSelectionIfDiffers, NULL,
CurrentTime);
}
XFree(value);
}
// Called when the requested selection value is availiable
static void SelectionReceived(Widget w, XtPointer client_data, Atom *selection,
Atom *type, XtPointer value,
unsigned long *received_length, int *format)
{
int length = *received_length;
if (*type != 0 && *type != XT_CONVERT_FAIL) {
if (length > 0 && ValueDiffers(value, length)) {
if (options.debug) {
printf("Selection changed: ");
PrintValue((char*)value, length);
printf("\n");
}
ChangeValue((char*)value, length);
if (options.verbose) {
printf("sel -> cut: ");
PrintValue(options.value, options.length);
printf("\n");
}
if (options.debug)
printf("Updating buffer\n");
XStoreBuffer(XtDisplay(w),
(char*)options.value,
(int)(options.length),
buffer );
XtFree(value);
return;
}
}
XtFree(value);
// Unless a new selection value is found, check the buffer value
CheckBuffer();
}
// Called each <pause arg=500> milliseconds
void timeout(XtPointer p, XtIntervalId* i)
{
if (options.own_selection)
CheckBuffer();
else {
int get_value = 1;
if (options.buttonup) {
int screen_num = DefaultScreen(dpy);
int root_x, root_y, win_x, win_y;
unsigned int mask;
Window root_wnd, child_wnd;
XQueryPointer(dpy, RootWindow(dpy,screen_num), &root_wnd, &child_wnd,
&root_x, &root_y, &win_x, &win_y, &mask);
if (mask & (ShiftMask | Button1Mask))
get_value = 0;
}
if (get_value)
XtGetSelectionValue(box, selection, XInternAtom(dpy, "UTF8_STRING", False),
SelectionReceived, NULL,
CurrentTime);
}
XtAppAddTimeOut(context, options.pause, timeout, 0);
}
int main(int argc, char* argv[])
{
Widget top;
top = XtVaAppInitialize(&context, "AutoCutSel",
optionDesc, XtNumber(optionDesc), &argc, argv, NULL,
XtNoverrideRedirect, True,
XtNgeometry, "-10-10",
NULL);
if (argc != 1) Syntax(argv[0]);
XtGetApplicationResources(top, (XtPointer)&options,
resources, XtNumber(resources),
NULL, ZERO );
if (strcmp(options.debug_option, "on") == 0)
options.debug = 1;
else
options.debug = 0;
if (strcmp(options.verbose_option, "on") == 0)
options.verbose = 1;
else
options.verbose = 0;
if (options.debug || options.verbose)
printf("autocutsel v%s\n", VERSION);
if (strcmp(options.buttonup_option, "on") == 0)
options.buttonup = 1;
else
options.buttonup = 0;
if (strcmp(options.fork_option, "on") == 0) {
options.fork = 1;
options.verbose = 0;
options.debug = 0;
}
else
options.fork = 0;
if (options.fork) {
if (getppid () != 1) {
#ifdef SETPGRP_VOID
setpgrp();
#else
setpgrp(0, 0);
#endif
switch (fork()) {
case -1:
fprintf (stderr, "could not fork, exiting\n");
return errno;
case 0:
sleep(3); /* Wait for father to exit */
chdir("/");
TrapSignals();
CloseStdFds();
break;
default:
return 0;
}
}
}
options.value = NULL;
options.length = 0;
options.own_selection = 0;
box = XtCreateManagedWidget("box", boxWidgetClass, top, NULL, 0);
dpy = XtDisplay(top);
selection = XInternAtom(dpy, options.selection_name, 0);
options.selection = selection;
buffer = 0;
options.value = XFetchBuffer(dpy, &options.length, buffer);
XtAppAddTimeOut(context, options.pause, timeout, 0);
XtRealizeWidget(top);
XUnmapWindow(XtDisplay(top), XtWindow(top));
/* Set up window properties so that the PID of the relevant autocutsel
* process is known and autocutsel windows can be identified as such when
* debugging. */
const int _NET_WM_NAME = 0;
const int UTF8_STRING = 1;
const int WM_NAME = 2;
const int STRING = 3;
const int _NET_WM_PID = 4;
const int CARDINAL = 5;
Atom atoms[6];
char *names[6] = {
"_NET_WM_NAME",
"UTF8_STRING",
"WM_NAME",
"STRING",
"_NET_WM_PID",
"CARDINAL",
};
XInternAtoms(dpy, names, 6, 0, atoms);
const char *window_name = "autocutsel";
pid_t pid = getpid();
XChangeProperty(dpy, XtWindow(top), atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, PropModeReplace, (const unsigned char*)window_name, strlen(window_name));
XChangeProperty(dpy, XtWindow(top), atoms[WM_NAME], atoms[STRING], 8, PropModeReplace, (const unsigned char*)window_name, strlen(window_name));
XChangeProperty(dpy, XtWindow(top), atoms[_NET_WM_PID], atoms[CARDINAL], 32, PropModeReplace, (const unsigned char*)&pid, 1);
XtAppMainLoop(context);
return 0;
}