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
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
* Copyright 2009-2017 Pierre Ossman for Cendio AB
* Copyright 2014 Brian P. Hinz
*
* 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.
*/
//
// XserverDesktop.cxx
//
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <network/Socket.h>
#include <rfb/Exception.h>
#include <rfb/VNCServerST.h>
#include <rfb/LogWriter.h>
#include <rfb/Configuration.h>
#include <rfb/ServerCore.h>
#include "XserverDesktop.h"
#include "vncBlockHandler.h"
#include "vncExtInit.h"
#include "vncHooks.h"
#include "vncSelection.h"
#include "XorgGlue.h"
#include "Input.h"
extern "C" {
void vncSetGlueContext(int screenIndex);
}
using namespace rfb;
using namespace network;
static LogWriter vlog("XserverDesktop");
BoolParameter rawKeyboard("RawKeyboard",
"Send keyboard events straight through and "
"avoid mapping them to the current keyboard "
"layout", false);
IntParameter queryConnectTimeout("QueryConnectTimeout",
"Number of seconds to show the "
"Accept Connection dialog before "
"rejecting the connection",
10);
XserverDesktop::XserverDesktop(int screenIndex_,
std::list<network::SocketListener*> listeners_,
const char* name, const rfb::PixelFormat &pf,
int width, int height,
void* fbptr, int stride)
: screenIndex(screenIndex_),
server(0), listeners(listeners_),
directFbptr(true),
queryConnectId(0), queryConnectTimer(this)
{
format = pf;
server = new VNCServerST(name, this);
setFramebuffer(width, height, fbptr, stride);
for (std::list<SocketListener*>::iterator i = listeners.begin();
i != listeners.end();
i++) {
vncSetNotifyFd((*i)->getFd(), screenIndex, true, false);
}
}
XserverDesktop::~XserverDesktop()
{
while (!listeners.empty()) {
vncRemoveNotifyFd(listeners.back()->getFd());
delete listeners.back();
listeners.pop_back();
}
if (!directFbptr)
delete [] data;
delete server;
}
void XserverDesktop::blockUpdates()
{
server->blockUpdates();
}
void XserverDesktop::unblockUpdates()
{
server->unblockUpdates();
}
void XserverDesktop::setFramebuffer(int w, int h, void* fbptr, int stride_)
{
ScreenSet layout;
width_ = w;
height_ = h;
if (!directFbptr) {
delete [] data;
directFbptr = true;
}
if (!fbptr) {
fbptr = new rdr::U8[w * h * (format.bpp/8)];
stride_ = w;
directFbptr = false;
}
data = (rdr::U8*)fbptr;
stride = stride_;
vncSetGlueContext(screenIndex);
layout = ::computeScreenLayout(&outputIdMap);
server->setPixelBuffer(this, layout);
}
void XserverDesktop::refreshScreenLayout()
{
vncSetGlueContext(screenIndex);
server->setScreenLayout(::computeScreenLayout(&outputIdMap));
}
void XserverDesktop::start(rfb::VNCServer* vs)
{
// We already own the server object, and we always keep it in a
// ready state
assert(vs == server);
}
void XserverDesktop::stop()
{
}
void XserverDesktop::queryConnection(network::Socket* sock,
const char* userName)
{
int count;
if (queryConnectTimer.isStarted()) {
server->approveConnection(sock, false, "Another connection is currently being queried.");
return;
}
count = vncNotifyQueryConnect();
if (count == 0) {
server->approveConnection(sock, false, "Unable to query the local user to accept the connection.");
return;
}
queryConnectAddress.replaceBuf(sock->getPeerAddress());
if (!userName)
userName = "(anonymous)";
queryConnectUsername.replaceBuf(strDup(userName));
queryConnectId = (uint32_t)(intptr_t)sock;
queryConnectSocket = sock;
queryConnectTimer.start(queryConnectTimeout * 1000);
}
void XserverDesktop::bell()
{
server->bell();
}
void XserverDesktop::setLEDState(unsigned int state)
{
server->setLEDState(state);
}
void XserverDesktop::serverCutText(const char* str, int len)
{
try {
server->serverCutText(str, len);
} catch (rdr::Exception& e) {
vlog.error("XserverDesktop::serverCutText: %s",e.str());
}
}
void XserverDesktop::setDesktopName(const char* name)
{
try {
server->setName(name);
} catch (rdr::Exception& e) {
vlog.error("XserverDesktop::setDesktopName: %s",e.str());
}
}
void XserverDesktop::setCursor(int width, int height, int hotX, int hotY,
const unsigned char *rgbaData)
{
rdr::U8* cursorData;
rdr::U8 *out;
const unsigned char *in;
cursorData = new rdr::U8[width * height * 4];
// Un-premultiply alpha
in = rgbaData;
out = cursorData;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
rdr::U8 alpha;
alpha = in[3];
if (alpha == 0)
alpha = 1; // Avoid division by zero
*out++ = (unsigned)*in++ * 255/alpha;
*out++ = (unsigned)*in++ * 255/alpha;
*out++ = (unsigned)*in++ * 255/alpha;
*out++ = *in++;
}
}
try {
server->setCursor(width, height, Point(hotX, hotY), cursorData);
} catch (rdr::Exception& e) {
vlog.error("XserverDesktop::setCursor: %s",e.str());
}
delete [] cursorData;
}
void XserverDesktop::add_changed(const rfb::Region ®ion)
{
try {
server->add_changed(region);
} catch (rdr::Exception& e) {
vlog.error("XserverDesktop::add_changed: %s",e.str());
}
}
void XserverDesktop::add_copied(const rfb::Region &dest, const rfb::Point &delta)
{
try {
server->add_copied(dest, delta);
} catch (rdr::Exception& e) {
vlog.error("XserverDesktop::add_copied: %s",e.str());
}
}
void XserverDesktop::handleSocketEvent(int fd, bool read, bool write)
{
try {
if (read) {
if (handleListenerEvent(fd, &listeners, server))
return;
}
if (handleSocketEvent(fd, server, read, write))
return;
vlog.error("Cannot find file descriptor for socket event");
} catch (rdr::Exception& e) {
vlog.error("XserverDesktop::handleSocketEvent: %s",e.str());
}
}
bool XserverDesktop::handleListenerEvent(int fd,
std::list<SocketListener*>* sockets,
SocketServer* sockserv)
{
std::list<SocketListener*>::iterator i;
for (i = sockets->begin(); i != sockets->end(); i++) {
if ((*i)->getFd() == fd)
break;
}
if (i == sockets->end())
return false;
Socket* sock = (*i)->accept();
sock->outStream().setBlocking(false);
vlog.debug("new client, sock %d", sock->getFd());
sockserv->addSocket(sock);
vncSetNotifyFd(sock->getFd(), screenIndex, true, false);
return true;
}
bool XserverDesktop::handleSocketEvent(int fd,
SocketServer* sockserv,
bool read, bool write)
{
std::list<Socket*> sockets;
std::list<Socket*>::iterator i;
sockserv->getSockets(&sockets);
for (i = sockets.begin(); i != sockets.end(); i++) {
if ((*i)->getFd() == fd)
break;
}
if (i == sockets.end())
return false;
if (read)
sockserv->processSocketReadEvent(*i);
if (write)
sockserv->processSocketWriteEvent(*i);
return true;
}
void XserverDesktop::blockHandler(int* timeout)
{
// We don't have a good callback for when we can init input devices[1],
// so we abuse the fact that this routine will be called first thing
// once the dix is done initialising.
// [1] Technically Xvnc has InitInput(), but libvnc.so has nothing.
vncInitInputDevice();
try {
std::list<Socket*> sockets;
std::list<Socket*>::iterator i;
server->getSockets(&sockets);
for (i = sockets.begin(); i != sockets.end(); i++) {
int fd = (*i)->getFd();
if ((*i)->isShutdown()) {
vlog.debug("client gone, sock %d",fd);
vncRemoveNotifyFd(fd);
server->removeSocket(*i);
vncClientGone(fd);
delete (*i);
} else {
/* Update existing NotifyFD to listen for write (or not) */
vncSetNotifyFd(fd, screenIndex, true, (*i)->outStream().bufferUsage() > 0);
}
}
// We are responsible for propagating mouse movement between clients
int cursorX, cursorY;
vncGetPointerPos(&cursorX, &cursorY);
cursorX -= vncGetScreenX(screenIndex);
cursorY -= vncGetScreenY(screenIndex);
if (oldCursorPos.x != cursorX || oldCursorPos.y != cursorY) {
oldCursorPos.x = cursorX;
oldCursorPos.y = cursorY;
server->setCursorPos(oldCursorPos);
}
// Trigger timers and check when the next will expire
int nextTimeout = Timer::checkTimeouts();
if (nextTimeout > 0 && (*timeout == -1 || nextTimeout < *timeout))
*timeout = nextTimeout;
} catch (rdr::Exception& e) {
vlog.error("XserverDesktop::blockHandler: %s",e.str());
}
}
void XserverDesktop::addClient(Socket* sock, bool reverse)
{
vlog.debug("new client, sock %d reverse %d",sock->getFd(),reverse);
sock->outStream().setBlocking(false);
server->addSocket(sock, reverse);
vncSetNotifyFd(sock->getFd(), screenIndex, true, false);
}
void XserverDesktop::disconnectClients()
{
vlog.debug("disconnecting all clients");
return server->closeClients("Disconnection from server end");
}
void XserverDesktop::getQueryConnect(uint32_t* opaqueId,
const char** address,
const char** username,
int *timeout)
{
*opaqueId = queryConnectId;
if (!queryConnectTimer.isStarted()) {
*address = "";
*username = "";
*timeout = 0;
} else {
*address = queryConnectAddress.buf;
*username = queryConnectUsername.buf;
*timeout = queryConnectTimeout;
}
}
void XserverDesktop::approveConnection(uint32_t opaqueId, bool accept,
const char* rejectMsg)
{
if (queryConnectId == opaqueId) {
server->approveConnection(queryConnectSocket, accept, rejectMsg);
queryConnectId = 0;
queryConnectTimer.stop();
}
}
///////////////////////////////////////////////////////////////////////////
//
// SDesktop callbacks
void XserverDesktop::terminate()
{
kill(getpid(), SIGTERM);
}
void XserverDesktop::pointerEvent(const Point& pos, int buttonMask)
{
vncPointerMove(pos.x + vncGetScreenX(screenIndex),
pos.y + vncGetScreenY(screenIndex));
vncPointerButtonAction(buttonMask);
}
void XserverDesktop::clientCutText(const char* str, int len)
{
vncClientCutText(str, len);
}
unsigned int XserverDesktop::setScreenLayout(int fb_width, int fb_height,
const rfb::ScreenSet& layout)
{
unsigned int result;
char buffer[2048];
vlog.debug("Got request for framebuffer resize to %dx%d",
fb_width, fb_height);
layout.print(buffer, sizeof(buffer));
vlog.debug("%s", buffer);
vncSetGlueContext(screenIndex);
result = ::setScreenLayout(fb_width, fb_height, layout, &outputIdMap);
// Explicitly update the server state with the result as there
// can be corner cases where we don't get feedback from the X core
refreshScreenLayout();
return result;
}
void XserverDesktop::grabRegion(const rfb::Region& region)
{
if (directFbptr)
return;
std::vector<rfb::Rect> rects;
std::vector<rfb::Rect>::iterator i;
region.get_rects(&rects);
for (i = rects.begin(); i != rects.end(); i++) {
rdr::U8 *buffer;
int stride;
buffer = getBufferRW(*i, &stride);
vncGetScreenImage(screenIndex, i->tl.x, i->tl.y, i->width(), i->height(),
(char*)buffer, stride * format.bpp/8);
commitBufferRW(*i);
}
}
void XserverDesktop::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down)
{
if (!rawKeyboard)
keycode = 0;
vncKeyboardEvent(keysym, keycode, down);
}
bool XserverDesktop::handleTimeout(Timer* t)
{
if (t == &queryConnectTimer) {
server->approveConnection(queryConnectSocket, false,
"The attempt to prompt the user to "
"accept the connection failed");
}
return false;
}
|