summaryrefslogtreecommitdiffstats
path: root/krfb/krfb/rfbcontroller.h
blob: 3d3cd73c21bdc9cfc56bb6c6886171e1c8645330 (plain)
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
/***************************************************************************
                               rfbcontroller.h
                             -------------------
    begin                : Sun Dec 9 2001
    copyright            : (C) 2001 by Tim Jansen
    email                : tim@tjansen.de
 ***************************************************************************/

/***************************************************************************
 * Contains portions & concept from rfb's x0rfbserver.cc
 * Copyright (C) 2000 heXoNet Support GmbH, D-66424 Homburg.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef RFBCONTROLLER_H
#define RFBCONTROLLER_H

#include "configuration.h"
#include "connectiondialog.h"
#include "xupdatescanner.h"
#include <ksock.h>
#include <tqobject.h>
#include <tqtimer.h>
#include <tqmutex.h>

#define HAVE_PTHREADS
#include "rfb/rfb.h"

#include <X11/Xlib.h>

#define USE_MODIFIED_BUILTIN_LIBVNCSERVER


class TQCloseEvent;
class TQClipboard;
class RFBController;

typedef enum {
	RFB_STOPPED,
	RFB_WAITING,
	RFB_CONNECTING,
	RFB_CONNECTED
} RFBState;

class VNCEvent {
public:
	virtual void exec() = 0;
	virtual ~VNCEvent();
};


class KeyboardEvent : public VNCEvent {
	bool down;
	KeySym keySym;

	static Display *dpy;
	static signed char modifiers[0x100];
	static KeyCode keycodes[0x100], leftShiftCode, rightShiftCode, altGrCode;
	static const int LEFTSHIFT;
	static const int RIGHTSHIFT;
	static const int ALTGR;
	static char ModifierState;

	static void tweakModifiers(signed char mod, bool down);
public:
	static void initKeycodes();

	KeyboardEvent(bool d, KeySym k);
	int add_keysym( KeySym keysym );
	virtual void exec();
};

class PointerEvent : public VNCEvent {
	int button_mask, x, y;

	static bool initialized;
	static Display *dpy;
	static int buttonMask;
public:
	PointerEvent(int b, int _x, int _y);
	virtual void exec();
};

class ClipboardEvent : public VNCEvent {
	RFBController *controller;
	TQString text;
public:
	ClipboardEvent(RFBController *c, const TQString &text);
	virtual void exec();
};

class KNotifyEvent : public VNCEvent {
	TQString name;
	TQString desc;
public:
	KNotifyEvent(const TQString &n, const TQString &d);
	virtual ~KNotifyEvent();
	virtual void exec();
};

class SessionEstablishedEvent : public VNCEvent {
        RFBController *controller;
public:
	SessionEstablishedEvent(RFBController *c);
	virtual void exec();
};

/**
 * Manages sockets, drives the RGBConnection and triggers the connection
 * dialog.
 * The controller has three states: 'waiting for connection',
 * 'waiting for confirmation' and 'connected'. In the first state socket and
 * connection are null, in the second socket is set and in the last both are
 * set.
 * @author Tim Jansen
 */
class RFBController : public TQObject  {
	Q_OBJECT
	

	friend class SessionEstablishedEvent;
	friend class ClipboardEvent;
public:
	RFBController(Configuration *c);
	virtual ~RFBController();

	RFBState state;

	void acceptConnection(bool allowRemoteConnection);
	void connectionAccepted(bool allowRemoteConnection);
	void refuseConnection();
	void connectionClosed();
	bool handleCheckPassword(rfbClientPtr, const char *, int);
	void handleKeyEvent(bool down, KeySym keySym);
	void handlePointerEvent(int button_mask, int x, int y);
	enum rfbNewClientAction handleNewClient(rfbClientPtr cl);
	void clipboardToServer(const TQString &text);
	void handleClientGone();
#ifdef USE_MODIFIED_BUILTIN_LIBVNCSERVER
	void handleNegotiationFinished(rfbClientPtr cl);
#endif // USE_MODIFIED_BUILTIN_LIBVNCSERVER
	int getPort();
	void startServer(int inetdFd = -1, bool xtestGrab = true);

	static bool checkX11Capabilities();

public slots:
	void passwordChanged();
	void closeConnection();
	void enableDesktopControl(bool c);

signals:
        void sessionEstablished(TQString host);
	void sessionFinished();
	void sessionRefused();
	void quitApp();
	void desktopControlSettingChanged(bool);

private:
	void stopServer(bool xtestUngrab = true);
	void sendKNotifyEvent(const TQString &name, const TQString &desc);
	void sendSessionEstablished();
	void disableBackground(bool state);

	TQString remoteIp;
	volatile bool allowDesktopControl;

	TQTimer initIdleTimer;
	TQTimer idleTimer;

	enum {
		LAST_SYNC_TO_SERVER,
		LAST_SYNC_TO_CLIENT
	} lastClipboardDirection;
	TQString lastClipboardText;
	TQClipboard *clipboard;

	Configuration *configuration;
	XUpdateScanner *scanner;
	ConnectionDialog dialog;

	TQString desktopName;

	rfbScreenInfoPtr server;

	XImage *framebufferImage;

	TQMutex asyncMutex;
	TQPtrList<VNCEvent> asyncQueue;

	bool disableBackgroundPending; // background, as desired by libvncserver
	bool disableBackgroundState; // real background state
	bool closePending; // set when libvncserver detected close
	bool forcedClose;  // set when user closed connection
private slots:
	bool checkAsyncEvents();
	void idleSlot();
	void dialogAccepted();
	void dialogRefused();
	void selectionChanged();
	void clipboardChanged();
};

/*
 * Class to call XTestDiscard at idle time (because otherwise
 * it will not work with QT)
 */
class XTestDisabler : public TQObject {
	Q_OBJECT
	
public:
	XTestDisabler();
	bool disable;
	Display *dpy;
public slots:
	void exec();
};

#endif