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
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
//
// VNC server configuration utility
//
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include "vncExt.h"
#include <rdr/Exception.h>
#include <rfb/Configuration.h>
#include <rfb/Logger_stdio.h>
#include <rfb/LogWriter.h>
#include <rfb/Timer.h>
#include "TXWindow.h"
#include "TXCheckbox.h"
#include "TXLabel.h"
#include "QueryConnectDialog.h"
using namespace rfb;
LogWriter vlog("vncconfig");
StringParameter displayname("display", "The X display", "");
BoolParameter noWindow("nowin", "Don't display a window", 0);
BoolParameter iconic("iconic", "Start with window iconified", 0);
BoolParameter sendPrimary("SendPrimary", "Send the PRIMARY as well as the "
"CLIPBOARD selection", true);
IntParameter pollTime("poll",
"How often to poll for clipboard changes in ms", 0);
inline const char* selectionName(Atom sel) {
if (sel == xaCLIPBOARD) return "CLIPBOARD";
if (sel == XA_PRIMARY) return "PRIMARY";
return "unknown";
}
#define ACCEPT_CUT_TEXT "AcceptCutText"
#define SEND_CUT_TEXT "SendCutText"
char* programName = 0;
Display* dpy;
int vncExtEventBase, vncExtErrorBase;
static bool getBoolParam(Display* dpy, const char* param) {
char* data;
int len;
if (XVncExtGetParam(dpy, param, &data, &len)) {
if (strcmp(data,"1") == 0) return true;
}
return false;
}
class VncConfigWindow : public TXWindow, public TXEventHandler,
public TXDeleteWindowCallback,
public TXCheckboxCallback,
public rfb::Timer::Callback,
public QueryResultCallback {
public:
VncConfigWindow(Display* dpy)
: TXWindow(dpy, 300, 100), cutText(0), cutTextLen(0),
acceptClipboard(dpy, "Accept clipboard from viewers", this, false, this),
sendClipboard(dpy, "Send clipboard to viewers", this, false, this),
sendPrimaryCB(dpy, "Send primary selection to viewers", this,false,this),
pollTimer(this),
queryConnectDialog(0)
{
selection[0] = selection[1] = 0;
selectionLen[0] = selectionLen[1] = 0;
int y = yPad;
acceptClipboard.move(xPad, y);
acceptClipboard.checked(getBoolParam(dpy, ACCEPT_CUT_TEXT));
y += acceptClipboard.height();
sendClipboard.move(xPad, y);
sendClipboard.checked(getBoolParam(dpy, SEND_CUT_TEXT));
y += sendClipboard.height();
sendPrimaryCB.move(xPad, y);
sendPrimaryCB.checked(sendPrimary);
sendPrimaryCB.disabled(!sendClipboard.checked());
y += sendPrimaryCB.height();
setEventHandler(this);
toplevel("VNC config", this, 0, 0, 0, iconic);
XVncExtSelectInput(dpy, win(),
VncExtClientCutTextMask|
VncExtSelectionChangeMask|
VncExtQueryConnectMask);
XConvertSelection(dpy, XA_PRIMARY, XA_STRING,
XA_PRIMARY, win(), CurrentTime);
XConvertSelection(dpy, xaCLIPBOARD, XA_STRING,
xaCLIPBOARD, win(), CurrentTime);
if (pollTime != 0)
pollTimer.start(pollTime);
}
// handleEvent(). If we get a ClientCutTextNotify event from Xvnc, set the
// primary and clipboard selections to the clientCutText. If we get a
// SelectionChangeNotify event from Xvnc, set the serverCutText to the value
// of the new selection.
virtual void handleEvent(TXWindow* w, XEvent* ev) {
if (acceptClipboard.checked()) {
if (ev->type == vncExtEventBase + VncExtClientCutTextNotify) {
XVncExtClientCutTextEvent* cutEv = (XVncExtClientCutTextEvent*)ev;
if (cutText)
XFree(cutText);
cutText = 0;
if (XVncExtGetClientCutText(dpy, &cutText, &cutTextLen)) {
vlog.debug("Got client cut text: '%.*s%s'",
cutTextLen<9?cutTextLen:8, cutText,
cutTextLen<9?"":"...");
XStoreBytes(dpy, cutText, cutTextLen);
ownSelection(XA_PRIMARY, cutEv->time);
ownSelection(xaCLIPBOARD, cutEv->time);
delete [] selection[0];
delete [] selection[1];
selection[0] = selection[1] = 0;
selectionLen[0] = selectionLen[1] = 0;
}
}
}
if (sendClipboard.checked()) {
if (ev->type == vncExtEventBase + VncExtSelectionChangeNotify) {
vlog.debug("selection change event");
XVncExtSelectionChangeEvent* selEv = (XVncExtSelectionChangeEvent*)ev;
if (selEv->selection == xaCLIPBOARD ||
(selEv->selection == XA_PRIMARY && sendPrimaryCB.checked())) {
if (!selectionOwner(selEv->selection))
XConvertSelection(dpy, selEv->selection, XA_STRING,
selEv->selection, win(), CurrentTime);
}
}
}
if (ev->type == vncExtEventBase + VncExtQueryConnectNotify) {
vlog.debug("query connection event");
if (queryConnectDialog)
delete queryConnectDialog;
queryConnectDialog = 0;
char* qcAddress;
char* qcUser;
int qcTimeout;
if (XVncExtGetQueryConnect(dpy, &qcAddress, &qcUser,
&qcTimeout, &queryConnectId)) {
if (qcTimeout)
queryConnectDialog = new QueryConnectDialog(dpy, qcAddress,
qcUser, qcTimeout,
this);
if (queryConnectDialog)
queryConnectDialog->map();
XFree(qcAddress);
XFree(qcUser);
}
}
}
// selectionRequest() is called when we are the selection owner and another X
// client has requested the selection. We simply put the server's cut text
// into the requested property. TXWindow will handle the rest.
bool selectionRequest(Window requestor, Atom selection, Atom property)
{
if (cutText)
XChangeProperty(dpy, requestor, property, XA_STRING, 8,
PropModeReplace, (unsigned char*)cutText,
cutTextLen);
return cutText;
}
// selectionNotify() is called when we have requested the selection from the
// selection owner.
void selectionNotify(XSelectionEvent* ev, Atom type, int format,
int nitems, void* data)
{
if (ev->requestor != win() || ev->target != XA_STRING)
return;
if (data && format == 8) {
int i = (ev->selection == XA_PRIMARY ? 0 : 1);
if (selectionLen[i] == nitems && memcmp(selection[i], data, nitems) == 0)
return;
delete [] selection[i];
selection[i] = new char[nitems];
memcpy(selection[i], data, nitems);
selectionLen[i] = nitems;
if (cutTextLen == nitems && memcmp(cutText, data, nitems) == 0) {
vlog.debug("ignoring duplicate cut text");
return;
}
if (cutText)
XFree(cutText);
cutText = (char*)malloc(nitems); // assuming XFree() same as free()
if (!cutText) {
vlog.error("unable to allocate selection buffer");
return;
}
memcpy(cutText, data, nitems);
cutTextLen = nitems;
vlog.debug("sending %s selection as server cut text: '%.*s%s'",
selectionName(ev->selection),cutTextLen<9?cutTextLen:8,
cutText, cutTextLen<9?"":"...");
XVncExtSetServerCutText(dpy, cutText, cutTextLen);
}
}
// TXDeleteWindowCallback method
virtual void deleteWindow(TXWindow* w) {
exit(1);
}
// TXCheckboxCallback method
virtual void checkboxSelect(TXCheckbox* checkbox) {
if (checkbox == &acceptClipboard) {
XVncExtSetParam(dpy, (acceptClipboard.checked()
? ACCEPT_CUT_TEXT "=1" : ACCEPT_CUT_TEXT "=0"));
} else if (checkbox == &sendClipboard) {
XVncExtSetParam(dpy, (sendClipboard.checked()
? SEND_CUT_TEXT "=1" : SEND_CUT_TEXT "=0"));
sendPrimaryCB.disabled(!sendClipboard.checked());
}
}
// rfb::Timer::Callback interface
virtual bool handleTimeout(rfb::Timer* timer) {
if (sendPrimaryCB.checked() && !selectionOwner(XA_PRIMARY))
XConvertSelection(dpy, XA_PRIMARY, XA_STRING,
XA_PRIMARY, win(), CurrentTime);
if (!selectionOwner(xaCLIPBOARD))
XConvertSelection(dpy, xaCLIPBOARD, XA_STRING,
xaCLIPBOARD, win(), CurrentTime);
return true;
}
// QueryResultCallback interface
virtual void queryApproved() {
XVncExtApproveConnect(dpy, queryConnectId, 1);
}
virtual void queryRejected() {
XVncExtApproveConnect(dpy, queryConnectId, 0);
}
private:
char* cutText;
int cutTextLen;
char* selection[2];
int selectionLen[2];
TXCheckbox acceptClipboard, sendClipboard, sendPrimaryCB;
rfb::Timer pollTimer;
QueryConnectDialog* queryConnectDialog;
void* queryConnectId;
};
static void usage()
{
fprintf(stderr,"usage: %s [parameters]\n",
programName);
fprintf(stderr," %s [parameters] -connect <host>[:<port>]\n",
programName);
fprintf(stderr," %s [parameters] -disconnect\n", programName);
fprintf(stderr," %s [parameters] [-set] <Xvnc-param>=<value> ...\n",
programName);
fprintf(stderr," %s [parameters] -list\n", programName);
fprintf(stderr," %s [parameters] -get <param>\n", programName);
fprintf(stderr," %s [parameters] -desc <param>\n",programName);
fprintf(stderr,"\n"
"Parameters can be turned on with -<param> or off with -<param>=0\n"
"Parameters which take a value can be specified as "
"-<param> <value>\n"
"Other valid forms are <param>=<value> -<param>=<value> "
"--<param>=<value>\n"
"Parameter names are case-insensitive. The parameters are:\n\n");
Configuration::listParams(79, 14);
exit(1);
}
void removeArgs(int* argc, char** argv, int first, int n)
{
if (first + n > *argc) return;
for (int i = first + n; i < *argc; i++)
argv[i-n] = argv[i];
*argc -= n;
}
int main(int argc, char** argv)
{
programName = argv[0];
rfb::initStdIOLoggers();
rfb::LogWriter::setLogParams("*:stderr:30");
// Process vncconfig's own parameters first, then we process the
// other arguments when we have the X display.
int i;
for (i = 1; i < argc; i++) {
if (Configuration::setParam(argv[i]))
continue;
if (argv[i][0] == '-' && i+1 < argc &&
Configuration::setParam(&argv[i][1], argv[i+1])) {
i++;
continue;
}
break;
}
CharArray displaynameStr(displayname.getData());
if (!(dpy = XOpenDisplay(displaynameStr.buf))) {
fprintf(stderr,"%s: unable to open display \"%s\"\n",
programName, XDisplayName(displaynameStr.buf));
exit(1);
}
if (!XVncExtQueryExtension(dpy, &vncExtEventBase, &vncExtErrorBase)) {
fprintf(stderr,"No VNC extension on display %s\n",
XDisplayName(displaynameStr.buf));
exit(1);
}
if (i < argc) {
for (; i < argc; i++) {
if (strcmp(argv[i], "-connect") == 0) {
i++;
if (i >= argc) usage();
if (!XVncExtConnect(dpy, argv[i])) {
fprintf(stderr,"connecting to %s failed\n",argv[i]);
}
} else if (strcmp(argv[i], "-disconnect") == 0) {
if (!XVncExtConnect(dpy, "")) {
fprintf(stderr,"disconnecting all clients failed\n");
}
} else if (strcmp(argv[i], "-get") == 0) {
i++;
if (i >= argc) usage();
char* data;
int len;
if (XVncExtGetParam(dpy, argv[i], &data, &len)) {
printf("%.*s\n",len,data);
} else {
fprintf(stderr,"getting param %s failed\n",argv[i]);
}
XFree(data);
} else if (strcmp(argv[i], "-desc") == 0) {
i++;
if (i >= argc) usage();
char* desc = XVncExtGetParamDesc(dpy, argv[i]);
if (desc) {
printf("%s\n",desc);
} else {
fprintf(stderr,"getting description for param %s failed\n",argv[i]);
}
XFree(desc);
} else if (strcmp(argv[i], "-list") == 0) {
int nParams;
char** list = XVncExtListParams(dpy, &nParams);
for (int i = 0; i < nParams; i++) {
printf("%s\n",list[i]);
}
XVncExtFreeParamList(list);
} else if (strcmp(argv[i], "-set") == 0) {
i++;
if (i >= argc) usage();
if (!XVncExtSetParam(dpy, argv[i])) {
fprintf(stderr,"setting param %s failed\n",argv[i]);
}
} else if (XVncExtSetParam(dpy, argv[i])) {
fprintf(stderr,"set parameter %s\n",argv[i]);
} else {
usage();
}
}
return 0;
}
try {
TXWindow::init(dpy,"Vncconfig");
VncConfigWindow w(dpy);
if (!noWindow) w.map();
while (true) {
struct timeval tv;
struct timeval* tvp = 0;
// Process any incoming X events
TXWindow::handleXEvents(dpy);
// Process expired timers and get the time until the next one
int timeoutMs = Timer::checkTimeouts();
if (timeoutMs) {
tv.tv_sec = timeoutMs / 1000;
tv.tv_usec = (timeoutMs % 1000) * 1000;
tvp = &tv;
}
// If there are X requests pending then poll, don't wait!
if (XPending(dpy)) {
tv.tv_usec = tv.tv_sec = 0;
tvp = &tv;
}
// Wait for X events, VNC traffic, or the next timer expiry
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(ConnectionNumber(dpy), &rfds);
int n = select(FD_SETSIZE, &rfds, 0, 0, tvp);
if (n < 0) throw rdr::SystemException("select",errno);
}
XCloseDisplay(dpy);
} catch (rdr::Exception &e) {
vlog.error("%s", e.str());
}
return 0;
}
|