-
Notifications
You must be signed in to change notification settings - Fork 0
/
vruivnc.cpp
359 lines (265 loc) · 9.95 KB
/
vruivnc.cpp
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
/***********************************************************************
vruivnc - Simple Vrui application to connect to a VNC server.
Copyright (c) 2007,2008 Voltaic
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
***********************************************************************/
#include <stdlib.h>
#include <iostream>
#include <deque>
#include <GL/GLMaterial.h>
#include <GL/GLContextData.h>
#include <Images/RGBImage.h>
#include <Vrui/Vrui.h>
#include <Vrui/Application.h>
#include <Vrui/Geometry.h>
#include <Geometry/OrthogonalTransformation.h>
#include "vruivnc.h"
namespace Voltaic {
//----------------------------------------------------------------------
// vruivnc methods
vruivnc::vruivnc(int& argc, char**& argv, char**& appDefaults) :
Vrui::Application(argc, argv, appDefaults),
rfbProtocolStartupData(),
vncManager(new VncManager(*this, *this, !Vrui::isMaster(), Vrui::openPipe())),
scaleToEnvironment(true),
surfaceMaterial(GLMaterial::Color(1.0f, 1.0f, 1.0f, 0.333f), GLMaterial::Color(0.333f, 0.333f, 0.333f), 10.0f)
{
// Parse the command line:
for (int i = 1; i < argc; ++i)
{
if (argv[i][0] == '-')
{
if (strcasecmp(argv[i]+1, "host") == 0)
{
i++;
rfbProtocolStartupData.desktopHost = argv[i++];
rfbProtocolStartupData.initViaConnect = true;
}
else if (strcasecmp(argv[i]+1, "port") == 0)
{
i++;
rfbProtocolStartupData.rfbPort = atoi(argv[i++]);
}
else if (strcasecmp(argv[i]+1, "encodings") == 0)
{
i++;
rfbProtocolStartupData.requestedEncodings = argv[i++];
}
else if (strcasecmp(argv[i]+1, "shared") == 0)
{
i++;
rfbProtocolStartupData.sharedDesktopFlag = true;
}
else
{
std::cout << "Unrecognized switch " << argv[i] << std::endl;
}
}
}
// Initialize Vrui navigation transformation:
centerDisplayCallback(0);
}
vruivnc::~vruivnc()
{
if (vncManager)
{
vncManager->shutdown();
delete vncManager;
}
}
void vruivnc::initContext(GLContextData& contextData) const
{
if (vncManager)
vncManager->startup(rfbProtocolStartupData);
}
void vruivnc::frame()
{
(void)vncManager->performQueuedActions();
Vrui::requestUpdate();
}
void vruivnc::display(GLContextData& contextData) const
{
// Save OpenGL state:
glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_LIGHTING_BIT|GL_POLYGON_BIT);
// Save coordinate system:
glPushMatrix();
// Render all opaque surfaces:
glDisable(GL_CULL_FACE);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
// Set up OpenGL to render the surface:
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
glMaterial(GLMaterialEnums::FRONT_AND_BACK, surfaceMaterial);
// Display the graphics:
drawRemoteDisplaySurface();
// Go back to original coordinate system:
glPopMatrix();
// Restore OpenGL state:
glPopAttrib();
}
void vruivnc::centerDisplayCallback(Misc::CallbackData* cbData)
{
static const float dist = 400;
if (scaleToEnvironment)
{
// Center the model in the available display space:
Vrui::setNavigationTransformation(Vrui::Point::origin, Vrui::Scalar(3.0*dist));
}
else
{
// Center the model in the available display space, but do not scale it:
Vrui::NavTransform nav = Vrui::NavTransform::identity;
nav *= Vrui::NavTransform::translateFromOriginTo(Vrui::getDisplayCenter());
nav *= Vrui::NavTransform::scale(Vrui::Scalar(8)*Vrui::getInchFactor() / Vrui::Scalar(dist));
Vrui::setNavigationTransformation(nav);
}
}
void vruivnc::drawRemoteDisplaySurface() const
{
static const float depth = 10.0;
static const float bezelWidth = 3.0;
const float dw0 = vncManager->getRemoteDisplayWidth() / 2.0;
const float dh0 = vncManager->getRemoteDisplayHeight() / 2.0;
const float dd0 = depth / 2.0;
const float dw1 = dw0 + bezelWidth;
const float dh1 = dh0 + bezelWidth;
const float dd1 = dd0 + bezelWidth;
const float sqrtHalf = sqrt(0.5);
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 1.0);
// Bottom:
glNormal3f(0, 0, 1);
glVertex3f(-dw1, -dh1, -dd1);
glVertex3f( dw1, -dh1, -dd1);
glVertex3f( dw1, dh1, -dd1);
glVertex3f(-dw1, dh1, -dd1);
// Sides:
glNormal3f(-1, 0, 0);
glVertex3f(-dw1, -dh1, -dd1);
glVertex3f(-dw1, -dh1, dd0);
glVertex3f( dw1, -dh1, dd0);
glVertex3f( dw1, -dh1, -dd1);
glNormal3f(0, -1, 0);
glVertex3f(-dw1, -dh1, -dd1);
glVertex3f(-dw1, -dh1, dd0);
glVertex3f(-dw1, dh1, dd0);
glVertex3f(-dw1, dh1, -dd1);
glNormal3f(-1, 0, 0);
glVertex3f(-dw1, dh1, -dd1);
glVertex3f(-dw1, dh1, dd0);
glVertex3f( dw1, dh1, dd0);
glVertex3f( dw1, dh1, -dd1);
glNormal3f(0, -1, 0);
glVertex3f( dw1, -dh1, -dd1);
glVertex3f( dw1, -dh1, dd0);
glVertex3f( dw1, dh1, dd0);
glVertex3f( dw1, dh1, -dd1);
// Bezel:
glNormal3f(-sqrtHalf, 0, sqrtHalf);
glVertex3f(-dw1, -dh1, dd0);
glVertex3f(-dw0, -dh0, dd1);
glVertex3f( dw0, -dh0, dd1);
glVertex3f( dw1, -dh1, dd0);
glNormal3f(0, -sqrtHalf, sqrtHalf);
glVertex3f(-dw1, -dh1, dd0);
glVertex3f(-dw0, -dh0, dd1);
glVertex3f(-dw0, dh0, dd1);
glVertex3f(-dw1, dh1, dd0);
glNormal3f(-sqrtHalf, 0, sqrtHalf);
glVertex3f(-dw1, dh1, dd0);
glVertex3f(-dw0, dh0, dd1);
glVertex3f( dw0, dh0, dd1);
glVertex3f( dw1, dh1, dd0);
glNormal3f(0, -sqrtHalf, sqrtHalf);
glVertex3f( dw1, -dh1, dd0);
glVertex3f( dw0, -dh0, dd1);
glVertex3f( dw0, dh0, dd1);
glVertex3f( dw1, dh1, dd0);
glEnd();
// Top face: the video surface:
(void)vncManager->drawRemoteDisplaySurface( -dw0, -dh0, dd1,
dw0, -dh0, dd1,
dw0, dh0, dd1 );
}
//----------------------------------------------------------------------
// VncManager::MessageManager methods
void vruivnc::internalErrorMessage(const char* where, const char* message)
{
fprintf(stderr, "*** vruivnc: internal error at %s: %s", where, message);
perror("; errno"); // prints newline at end of line
}
void vruivnc::errorMessage(const char* where, const char* message)
{
fprintf(stderr, "*** vruivnc: %s: %s", where, message);
perror("; errno"); // prints newline at end of line
}
void vruivnc::errorMessageFromServer(const char* where, const char* message)
{
// Note: a malicious message from the server won't hurt us here, i.e., there
// are no known injection attacks for the way we're handling this message....
this->errorMessage(where, message);
}
void vruivnc::infoServerInitStarted()
{
fprintf(stderr, "vruivnc info: RFB protocol: server init started\n");
}
void vruivnc::infoProtocolVersion(int serverMajorVersion, int serverMinorVersion, int clientMajorVersion, int clientMinorVersion)
{
fprintf(stderr, "vruivnc info: RFB protocol version: server:%03d.%03d, client: %03d.%03d\n", (int)serverMajorVersion, (int)serverMinorVersion, (int)clientMajorVersion, (int)clientMinorVersion);
}
void vruivnc::infoAuthenticationResult(bool succeeded, rfbCARD32 authScheme, rfbCARD32 authResult)
{
fprintf(stderr, "vruivnc info: RFB protocol: authentication result: succeeded = %d, authScheme = %ld, authResult = %ld\n", (int)succeeded, (long)authScheme, (long)authResult);
}
void vruivnc::infoServerInitCompleted(bool succeeded)
{
fprintf(stderr, "vruivnc info: RFB protocol: server init completed: succeeded = %d\n", (int)succeeded);
}
void vruivnc::infoDesktopSizeReceived(rfbCARD16 newWidth, rfbCARD16 newHeight)
{
fprintf(stderr, "vruivnc info: RFB protocol: DesktopSize received: %ldx%ld\n", (long)newWidth, (long)newHeight);
}
void vruivnc::infoCloseStarted()
{
fprintf(stderr, "vruivnc info: RFB protocol: close started\n");
}
void vruivnc::infoCloseCompleted()
{
fprintf(stderr, "vruivnc info: RFB protocol: close completed\n");
}
//----------------------------------------------------------------------
// VncManager::PasswordRetrievalThunk method
void vruivnc::getPassword(VncManager::PasswordRetrievalCompletionThunk& passwordRetrievalCompletionThunk)
{
passwordRetrievalCompletionThunk.postPassword(getpass("Enter VNC password:"));
}
} // end of namespace Voltaic
//----------------------------------------------------------------------
int main(int argc, char* argv[])
{
try
{
// Create the Vrui application object:
char** appDefaults = 0;
Voltaic::vruivnc app(argc, argv, appDefaults);
// Run the Vrui application:
app.run();
// Return to the OS:
return 0;
}
catch(std::runtime_error err)
{
// Print an error message and return to the OS:
std::cerr << "Exception: " << err.what() << std::endl;
return 1;
}
}