blob: 74d681881ef68cb69e285aa4d5c446a08c9470c3 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <windows.h>
20#include <winsock2.h>
21#include <vncviewer/UserPasswdDialog.h>
22#include <vncviewer/CConn.h>
23#include <vncviewer/CConnThread.h>
24#include <vncviewer/resource.h>
25#include <rfb/encodings.h>
26#include <rfb/secTypes.h>
27#include <rfb/CSecurityNone.h>
28#include <rfb/CSecurityVncAuth.h>
29#include <rfb/CMsgWriter.h>
30#include <rfb/Configuration.h>
31#include <rfb/LogWriter.h>
32#include <rfb_win32/AboutDialog.h>
33
34using namespace rfb;
35using namespace rfb::win32;
36using namespace rdr;
37
38// - Statics & consts
39
40static LogWriter vlog("CConn");
41
42
43const int IDM_FULLSCREEN = ID_FULLSCREEN;
44const int IDM_SEND_MENU_KEY = ID_SEND_MENU_KEY;
45const int IDM_SEND_CAD = ID_SEND_CAD;
46const int IDM_SEND_CTLESC = ID_SEND_CTLESC;
47const int IDM_ABOUT = ID_ABOUT;
48const int IDM_OPTIONS = ID_OPTIONS;
49const int IDM_INFO = ID_INFO;
50const int IDM_NEWCONN = ID_NEW_CONNECTION;
51const int IDM_REQUEST_REFRESH = ID_REQUEST_REFRESH;
52const int IDM_CTRL_KEY = ID_CTRL_KEY;
53const int IDM_ALT_KEY = ID_ALT_KEY;
54const int IDM_FILE_TRANSFER = ID_FILE_TRANSFER;
55const int IDM_CONN_SAVE_AS = ID_CONN_SAVE_AS;
george824f72ab32006-09-10 05:13:45 +000056const int IDM_ZOOM_IN = ID_ZOOM_IN;
57const int IDM_ZOOM_OUT = ID_ZOOM_OUT;
58const int IDM_ACTUAL_SIZE = ID_ACTUAL_SIZE;
59const int IDM_AUTO_SIZE = ID_AUTO_SIZE;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000060
61
62static IntParameter debugDelay("DebugDelay","Milliseconds to display inverted "
63 "pixel data - a debugging feature", 0);
64
george82b6d87aa2006-09-11 07:00:59 +000065const int scaleValues[9] = {10, 25, 50, 75, 90, 100, 125, 150, 200};
66const int scaleCount = 9;
67
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000068
69//
70// -=- CConn implementation
71//
72
73RegKey CConn::userConfigKey;
74
75
76CConn::CConn()
Peter Åstrand2dd9eea2008-12-11 08:23:55 +000077 : window(0), sameMachine(false), encodingChange(false), formatChange(false),
78 lastUsedEncoding_(encodingRaw), sock(0), sockEvent(CreateEvent(0, TRUE, FALSE, 0)),
79 reverseConnection(false), requestUpdate(false), isClosed_(false) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000080}
81
82CConn::~CConn() {
83 delete window;
84}
85
86bool CConn::initialise(network::Socket* s, bool reverse) {
87 // Set the server's name for MRU purposes
88 CharArray endpoint(s->getPeerEndpoint());
89 setServerName(endpoint.buf);
90 if (!options.host.buf)
91 options.setHost(endpoint.buf);
92
93 // Initialise the underlying CConnection
94 setStreams(&s->inStream(), &s->outStream());
95
96 // Enable processing of window messages while blocked on I/O
97 s->inStream().setBlockCallback(this);
98
99 // Initialise the viewer options
100 applyOptions(options);
101
102 // - Set which auth schemes we support, in order of preference
103 addSecType(secTypeVncAuth);
104 addSecType(secTypeNone);
105
106 // Start the RFB protocol
107 sock = s;
108 reverseConnection = reverse;
109 initialiseProtocol();
110
111 m_fileTransfer.initialize(&s->inStream(), &s->outStream());
112
113 return true;
114}
115
116
117void
118CConn::applyOptions(CConnOptions& opt) {
119 // - If any encoding-related settings have changed then we must
120 // notify the server of the new settings
121 encodingChange |= ((options.useLocalCursor != opt.useLocalCursor) ||
122 (options.useDesktopResize != opt.useDesktopResize) ||
123 (options.customCompressLevel != opt.customCompressLevel) ||
124 (options.compressLevel != opt.compressLevel) ||
125 (options.noJpeg != opt.noJpeg) ||
126 (options.qualityLevel != opt.qualityLevel) ||
127 (options.preferredEncoding != opt.preferredEncoding));
128
129 // - If the preferred pixel format has changed then notify the server
130 formatChange |= (options.fullColour != opt.fullColour);
131 if (!opt.fullColour)
132 formatChange |= (options.lowColourLevel != opt.lowColourLevel);
133
134 // - Save the new set of options
135 options = opt;
136
137 // - Set optional features in ConnParams
138 cp.supportsLocalCursor = options.useLocalCursor;
139 cp.supportsDesktopResize = options.useDesktopResize;
140 cp.customCompressLevel = options.customCompressLevel;
141 cp.compressLevel = options.compressLevel;
142 cp.noJpeg = options.noJpeg;
143 cp.qualityLevel = options.qualityLevel;
144
145 // - Configure connection sharing on/off
146 setShared(options.shared);
147
148 // - Whether to use protocol 3.3 for legacy compatibility
149 setProtocol3_3(options.protocol3_3);
150
151 // - Apply settings that affect the window, if it is visible
152 if (window) {
153 window->setMonitor(options.monitor.buf);
154 window->setFullscreen(options.fullScreen);
155 window->setEmulate3(options.emulate3);
156 window->setPointerEventInterval(options.pointerEventInterval);
157 window->setMenuKey(options.menuKey);
158 window->setDisableWinKeys(options.disableWinKeys);
159 window->setShowToolbar(options.showToolbar);
george82770bbbc2007-03-12 10:48:09 +0000160 window->printScale();
george82ffc14a62006-09-05 06:51:41 +0000161 if (options.autoScaling) {
162 window->setAutoScaling(true);
163 } else {
164 window->setAutoScaling(false);
165 window->setDesktopScale(options.scale);
166 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000167 if (!options.useLocalCursor)
168 window->setCursor(0, 0, Point(), 0, 0);
169 }
170}
171
172
173void
174CConn::displayChanged() {
175 // Display format has changed - recalculate the full-colour pixel format
176 calculateFullColourPF();
177}
178
179void
180CConn::paintCompleted() {
181 // A repaint message has just completed - request next update if necessary
182 requestNewUpdate();
183}
184
185bool
186CConn::sysCommand(WPARAM wParam, LPARAM lParam) {
187 // - If it's one of our (F8 Menu) messages
188 switch (wParam) {
189 case IDM_FULLSCREEN:
190 options.fullScreen = !window->isFullscreen();
191 window->setFullscreen(options.fullScreen);
192 return true;
george824f72ab32006-09-10 05:13:45 +0000193 case IDM_ZOOM_IN:
george824f72ab32006-09-10 05:13:45 +0000194 case IDM_ZOOM_OUT:
george82b6d87aa2006-09-11 07:00:59 +0000195 {
196 if (options.autoScaling) {
197 options.scale = window->getDesktopScale();
198 options.autoScaling = false;
199 window->setAutoScaling(false);
200 }
201 if (wParam == IDM_ZOOM_IN) {
202 for (int i = 0; i < scaleCount; i++)
203 if (options.scale < scaleValues[i]) {
204 options.scale = scaleValues[i];
205 break;
206 }
207 } else {
208 for (int i = scaleCount-1; i >= 0; i--)
209 if (options.scale > scaleValues[i]) {
210 options.scale = scaleValues[i];
211 break;
212 }
213 }
214 if (options.scale != window->getDesktopScale())
215 window->setDesktopScale(options.scale);
216 }
george824f72ab32006-09-10 05:13:45 +0000217 return true;
218 case IDM_ACTUAL_SIZE:
219 if (options.autoScaling) {
220 options.autoScaling = false;
221 window->setAutoScaling(false);
222 }
george824f72ab32006-09-10 05:13:45 +0000223 options.scale = 100;
224 window->setDesktopScale(100);
225 return true;
226 case IDM_AUTO_SIZE:
george8274ea5f32006-09-11 11:40:12 +0000227 options.autoScaling = !options.autoScaling;
228 window->setAutoScaling(options.autoScaling);
229 if (!options.autoScaling) options.scale = window->getDesktopScale();
george824f72ab32006-09-10 05:13:45 +0000230 return true;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000231 case IDM_SHOW_TOOLBAR:
232 options.showToolbar = !window->isToolbarEnabled();
233 window->setShowToolbar(options.showToolbar);
234 return true;
235 case IDM_CTRL_KEY:
236 window->kbd.keyEvent(this, VK_CONTROL, 0, !window->kbd.keyPressed(VK_CONTROL));
237 return true;
238 case IDM_ALT_KEY:
239 window->kbd.keyEvent(this, VK_MENU, 0, !window->kbd.keyPressed(VK_MENU));
240 return true;
241 case IDM_SEND_MENU_KEY:
242 window->kbd.keyEvent(this, options.menuKey, 0, true);
243 window->kbd.keyEvent(this, options.menuKey, 0, false);
244 return true;
245 case IDM_SEND_CAD:
246 window->kbd.keyEvent(this, VK_CONTROL, 0, true);
247 window->kbd.keyEvent(this, VK_MENU, 0, true);
248 window->kbd.keyEvent(this, VK_DELETE, 0x1000000, true);
249 window->kbd.keyEvent(this, VK_DELETE, 0x1000000, false);
250 window->kbd.keyEvent(this, VK_MENU, 0, false);
251 window->kbd.keyEvent(this, VK_CONTROL, 0, false);
252 return true;
253 case IDM_SEND_CTLESC:
254 window->kbd.keyEvent(this, VK_CONTROL, 0, true);
255 window->kbd.keyEvent(this, VK_ESCAPE, 0, true);
256 window->kbd.keyEvent(this, VK_ESCAPE, 0, false);
257 window->kbd.keyEvent(this, VK_CONTROL, 0, false);
258 return true;
259 case IDM_REQUEST_REFRESH:
260 try {
261 writer()->writeFramebufferUpdateRequest(Rect(0,0,cp.width,cp.height), false);
262 requestUpdate = false;
263 } catch (rdr::Exception& e) {
264 close(e.str());
265 }
266 return true;
267 case IDM_NEWCONN:
268 {
269 Thread* newThread = new CConnThread;
270 }
271 return true;
272 case IDM_OPTIONS:
273 // Update the monitor device name in the CConnOptions instance
274 options.monitor.replaceBuf(window->getMonitor());
275 showOptionsDialog();
276 return true;
277 case IDM_INFO:
278 infoDialog.showDialog(this);
279 return true;
280 case IDM_ABOUT:
281 AboutDialog::instance.showDialog();
282 return true;
283 case IDM_FILE_TRANSFER:
284 m_fileTransfer.show(window->getHandle());
285 return true;
286 case IDM_CONN_SAVE_AS:
287 return true;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000288 };
289 return false;
290}
291
292
293void
294CConn::closeWindow() {
295 vlog.info("window closed");
296 close();
297}
298
299
300void
301CConn::refreshMenu(bool enableSysItems) {
302 HMENU menu = GetSystemMenu(window->getHandle(), FALSE);
303
304 if (!enableSysItems) {
305 // Gray out menu items that might cause a World Of Pain
306 EnableMenuItem(menu, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
307 EnableMenuItem(menu, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
308 EnableMenuItem(menu, SC_RESTORE, MF_BYCOMMAND | MF_ENABLED);
309 EnableMenuItem(menu, SC_MINIMIZE, MF_BYCOMMAND | MF_ENABLED);
310 EnableMenuItem(menu, SC_MAXIMIZE, MF_BYCOMMAND | MF_ENABLED);
311 }
312
313 // Update the modifier key menu items
314 UINT ctrlCheckFlags = window->kbd.keyPressed(VK_CONTROL) ? MF_CHECKED : MF_UNCHECKED;
315 UINT altCheckFlags = window->kbd.keyPressed(VK_MENU) ? MF_CHECKED : MF_UNCHECKED;
316 CheckMenuItem(menu, IDM_CTRL_KEY, MF_BYCOMMAND | ctrlCheckFlags);
317 CheckMenuItem(menu, IDM_ALT_KEY, MF_BYCOMMAND | altCheckFlags);
318
319 // Ensure that the Send <MenuKey> menu item has the correct text
320 if (options.menuKey) {
321 TCharArray menuKeyStr(options.menuKeyName());
322 TCharArray tmp(_tcslen(menuKeyStr.buf) + 6);
323 _stprintf(tmp.buf, _T("Send %s"), menuKeyStr.buf);
324 if (!ModifyMenu(menu, IDM_SEND_MENU_KEY, MF_BYCOMMAND | MF_STRING, IDM_SEND_MENU_KEY, tmp.buf))
325 InsertMenu(menu, IDM_SEND_CAD, MF_BYCOMMAND | MF_STRING, IDM_SEND_MENU_KEY, tmp.buf);
326 } else {
327 RemoveMenu(menu, IDM_SEND_MENU_KEY, MF_BYCOMMAND);
328 }
329
330 // Set the menu fullscreen option tick
331 CheckMenuItem(menu, IDM_FULLSCREEN, (window->isFullscreen() ? MF_CHECKED : 0) | MF_BYCOMMAND);
332
333 // Set the menu toolbar option tick
334 int toolbarFlags = window->isToolbarEnabled() ? MF_CHECKED : 0;
335 CheckMenuItem(menu, IDM_SHOW_TOOLBAR, MF_BYCOMMAND | toolbarFlags);
336
337 // In the full-screen mode, "Show toolbar" should be grayed.
338 toolbarFlags = window->isFullscreen() ? MF_GRAYED : MF_ENABLED;
339 EnableMenuItem(menu, IDM_SHOW_TOOLBAR, MF_BYCOMMAND | toolbarFlags);
340}
341
342
343void
344CConn::blockCallback() {
345 // - An InStream has blocked on I/O while processing an RFB message
346 // We re-enable socket event notifications, so we'll know when more
347 // data is available, then we sit and dispatch window events until
348 // the notification arrives.
349 if (!isClosed()) {
350 if (WSAEventSelect(sock->getFd(), sockEvent, FD_READ | FD_CLOSE) == SOCKET_ERROR)
351 throw rdr::SystemException("Unable to wait for sokcet data", WSAGetLastError());
352 }
353 while (true) {
354 // If we have closed then we can't block waiting for data
355 if (isClosed())
356 throw rdr::EndOfStream();
357
358 // Wait for socket data, or a message to process
359 DWORD result = MsgWaitForMultipleObjects(1, &sockEvent.h, FALSE, INFINITE, QS_ALLINPUT);
360 if (result == WAIT_OBJECT_0) {
361 // - Network event notification. Return control to I/O routine.
362 break;
363 } else if (result == WAIT_FAILED) {
364 // - The wait operation failed - raise an exception
365 throw rdr::SystemException("blockCallback wait error", GetLastError());
366 }
367
368 // - There should be a message in the message queue
369 MSG msg;
370 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
371 // IMPORTANT: We mustn't call TranslateMessage() here, because instead we
372 // call ToAscii() in CKeyboard::keyEvent(). ToAscii() stores dead key
373 // state from one call to the next, which would be messed up by calls to
374 // TranslateMessage() (actually it looks like TranslateMessage() calls
375 // ToAscii() internally).
376 DispatchMessage(&msg);
377 }
378 }
379
380 // Before we return control to the InStream, reset the network event
381 WSAEventSelect(sock->getFd(), sockEvent, 0);
382 ResetEvent(sockEvent);
383}
384
385
386void CConn::keyEvent(rdr::U32 key, bool down) {
387 if (!options.sendKeyEvents) return;
388 try {
389 writer()->keyEvent(key, down);
390 } catch (rdr::Exception& e) {
391 close(e.str());
392 }
393}
394void CConn::pointerEvent(const Point& pos, int buttonMask) {
395 if (!options.sendPtrEvents) return;
396 try {
397 writer()->pointerEvent(pos, buttonMask);
398 } catch (rdr::Exception& e) {
399 close(e.str());
400 }
401}
402void CConn::clientCutText(const char* str, int len) {
403 if (!options.clientCutText) return;
404 if (state() != RFBSTATE_NORMAL) return;
405 try {
406 writer()->clientCutText(str, len);
407 } catch (rdr::Exception& e) {
408 close(e.str());
409 }
410}
411
412
413CSecurity* CConn::getCSecurity(int secType)
414{
415 switch (secType) {
416 case secTypeNone:
417 return new CSecurityNone();
418 case secTypeVncAuth:
419 return new CSecurityVncAuth(this);
420 default:
421 throw Exception("Unsupported secType?");
422 }
423}
424
425
426void
427CConn::setColourMapEntries(int first, int count, U16* rgbs) {
428 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
429 int i;
430 for (i=0;i<count;i++)
431 window->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
432 // *** change to 0, 256?
433 window->refreshWindowPalette(first, count);
434}
435
436void
437CConn::bell() {
438 if (options.acceptBell)
439 MessageBeep(-1);
440}
441
442
443void
444CConn::setDesktopSize(int w, int h) {
445 vlog.debug("setDesktopSize %dx%d", w, h);
446
447 // Resize the window's buffer
448 if (window)
449 window->setSize(w, h);
450
451 // Tell the underlying CConnection
452 CConnection::setDesktopSize(w, h);
453}
454
455void
456CConn::setCursor(int w, int h, const Point& hotspot, void* data, void* mask) {
457 if (!options.useLocalCursor) return;
458
459 // Set the window to use the new cursor
460 window->setCursor(w, h, hotspot, data, mask);
461}
462
463
464void
465CConn::close(const char* reason) {
466 // If already closed then ignore this
467 if (isClosed())
468 return;
469
470 // Hide the window, if it exists
471 if (window)
472 ShowWindow(window->getHandle(), SW_HIDE);
473
474 // Save the reason & flag that we're closed & shutdown the socket
475 isClosed_ = true;
476 closeReason_.replaceBuf(strDup(reason));
477 sock->shutdown();
478}
479
480
481void
482CConn::showOptionsDialog() {
483 optionsDialog.showDialog(this);
484}
485
486
487void
488CConn::framebufferUpdateEnd() {
489 if (debugDelay != 0) {
490 vlog.debug("debug delay %d",(int)debugDelay);
491 UpdateWindow(window->getHandle());
492 Sleep(debugDelay);
493 std::list<rfb::Rect>::iterator i;
494 for (i = debugRects.begin(); i != debugRects.end(); i++) {
495 window->invertRect(*i);
496 }
497 debugRects.clear();
498 }
499 if (options.autoSelect)
500 autoSelectFormatAndEncoding();
501
502 // Always request the next update
503 requestUpdate = true;
504
505 // Check that at least part of the window has changed
506 if (!GetUpdateRect(window->getHandle(), 0, FALSE)) {
507 if (!(GetWindowLong(window->getHandle(), GWL_STYLE) & WS_MINIMIZE))
508 requestNewUpdate();
509 }
510
511 // Make sure the local cursor is shown
512 window->showCursor();
513}
514
515
516// Note: The method below is duplicated in vncviewer_unix/CConn.cxx!
517
518// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
519// to the connection speed:
520//
521// Above 16Mbps (timing for at least a second), switch to hextile
522// Otherwise, switch to ZRLE
523//
524// Above 256Kbps, use full colour mode
525//
526void
527CConn::autoSelectFormatAndEncoding() {
528 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
529 unsigned int newEncoding = options.preferredEncoding;
530
531 bool newFullColour = options.fullColour;
532 unsigned int timeWaited = sock->inStream().timeWaited();
533
534 // Select best encoding
535 if (kbitsPerSecond > 16000 && timeWaited >= 10000) {
536 newEncoding = encodingHextile;
537 } else {
538 newEncoding = encodingZRLE;
539 }
540
541 if (newEncoding != options.preferredEncoding) {
542 vlog.info("Throughput %d kbit/s - changing to %s encoding",
543 kbitsPerSecond, encodingName(newEncoding));
544 options.preferredEncoding = newEncoding;
545 encodingChange = true;
546 }
547
548 if (kbitsPerSecond == 0) {
549 return;
550 }
551
552 if (cp.beforeVersion(3, 8)) {
553 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
554 // cursors "asynchronously". If this happens in the middle of a
555 // pixel format change, the server will encode the cursor with
556 // the old format, but the client will try to decode it
557 // according to the new format. This will lead to a
558 // crash. Therefore, we do not allow automatic format change for
559 // old servers.
560 return;
561 }
562
563 // Select best color level
564 newFullColour = (kbitsPerSecond > 256);
565 if (newFullColour != options.fullColour) {
566 vlog.info("Throughput %d kbit/s - full color is now %s",
567 kbitsPerSecond,
568 newFullColour ? "enabled" : "disabled");
569 options.fullColour = newFullColour;
570 formatChange = true;
571 }
572}
573
574void
575CConn::requestNewUpdate() {
576 if (!requestUpdate) return;
577
578 if (formatChange) {
579 // Select the required pixel format
580 if (options.fullColour) {
581 window->setPF(fullColourPF);
582 } else {
583 switch (options.lowColourLevel) {
584 case 0:
585 window->setPF(PixelFormat(8,3,0,1,1,1,1,2,1,0));
586 break;
587 case 1:
588 window->setPF(PixelFormat(8,6,0,1,3,3,3,4,2,0));
589 break;
590 case 2:
591 window->setPF(PixelFormat(8,8,0,0,0,0,0,0,0,0));
592 break;
593 }
594 }
595
596 // Print the current pixel format
597 char str[256];
598 window->getPF().print(str, 256);
599 vlog.info("Using pixel format %s",str);
600
601 // Save the connection pixel format and tell server to use it
602 cp.setPF(window->getPF());
603 writer()->writeSetPixelFormat(cp.pf());
604
605 // Correct the local window's palette
606 if (!window->getNativePF().trueColour)
607 window->refreshWindowPalette(0, 1 << cp.pf().depth);
608 }
609
610 if (encodingChange) {
611 vlog.info("Using %s encoding",encodingName(options.preferredEncoding));
612 writer()->writeSetEncodings(options.preferredEncoding, true);
613 }
614
615 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
616 !formatChange);
617
618 encodingChange = formatChange = requestUpdate = false;
619}
620
621
622void
623CConn::calculateFullColourPF() {
624 // If the server is palette based then use palette locally
625 // Also, don't bother doing bgr222
626 if (!serverDefaultPF.trueColour || (serverDefaultPF.depth < 6)) {
627 fullColourPF = serverDefaultPF;
628 options.fullColour = true;
629 } else {
630 // If server is trueColour, use lowest depth PF
631 PixelFormat native = window->getNativePF();
632 if ((serverDefaultPF.bpp < native.bpp) ||
633 ((serverDefaultPF.bpp == native.bpp) &&
634 (serverDefaultPF.depth < native.depth)))
635 fullColourPF = serverDefaultPF;
636 else
637 fullColourPF = window->getNativePF();
638 }
639 formatChange = true;
640}
641
642
643void
644CConn::setName(const char* name) {
645 if (window)
646 window->setName(name);
647 CConnection::setName(name);
648}
649
650
651void CConn::serverInit() {
652 CConnection::serverInit();
653
654 // If using AutoSelect with old servers, start in FullColor
655 // mode. See comment in autoSelectFormatAndEncoding.
656 if (cp.beforeVersion(3, 8) && options.autoSelect) {
657 options.fullColour = true;
658 }
659
660 // Show the window
661 window = new DesktopWindow(this);
662 window->setName(cp.name());
george82486740f2007-03-20 05:59:44 +0000663 window->setShowToolbar(options.showToolbar);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000664 window->setSize(cp.width, cp.height);
george82d2c22522006-09-10 11:45:19 +0000665 applyOptions(options);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000666
667 // Save the server's current format
668 serverDefaultPF = cp.pf();
669
670 // Calculate the full-colour format to use
671 calculateFullColourPF();
672
673 // Request the initial update
674 vlog.info("requesting initial update");
675 formatChange = encodingChange = requestUpdate = true;
676 requestNewUpdate();
677
678 // Update the window menu
679 HMENU wndmenu = GetSystemMenu(window->getHandle(), FALSE);
680 int toolbarChecked = options.showToolbar ? MF_CHECKED : 0;
681
682 AppendMenu(wndmenu, MF_SEPARATOR, 0, 0);
683 AppendMenu(wndmenu, MF_STRING, IDM_FULLSCREEN, _T("&Full screen"));
684 AppendMenu(wndmenu, MF_STRING | toolbarChecked, IDM_SHOW_TOOLBAR,
685 _T("Show tool&bar"));
686 AppendMenu(wndmenu, MF_SEPARATOR, 0, 0);
687 AppendMenu(wndmenu, MF_STRING, IDM_CTRL_KEY, _T("Ctr&l"));
688 AppendMenu(wndmenu, MF_STRING, IDM_ALT_KEY, _T("Al&t"));
689 AppendMenu(wndmenu, MF_STRING, IDM_SEND_CAD, _T("Send Ctrl-Alt-&Del"));
690 AppendMenu(wndmenu, MF_STRING, IDM_SEND_CTLESC, _T("Send Ctrl-&Esc"));
691 AppendMenu(wndmenu, MF_STRING, IDM_REQUEST_REFRESH, _T("Refres&h Screen"));
692 AppendMenu(wndmenu, MF_SEPARATOR, 0, 0);
693 AppendMenu(wndmenu, MF_STRING, IDM_NEWCONN, _T("Ne&w Connection..."));
694 AppendMenu(wndmenu, MF_STRING, IDM_OPTIONS, _T("&Options..."));
695 AppendMenu(wndmenu, MF_STRING, IDM_INFO, _T("Connection &Info..."));
696 AppendMenu(wndmenu, MF_STRING, IDM_ABOUT, _T("&About..."));
697}
698
699void
700CConn::serverCutText(const char* str, int len) {
701 if (!options.serverCutText) return;
702 window->serverCutText(str, len);
703}
704
705
706void CConn::beginRect(const Rect& r, unsigned int encoding) {
707 sock->inStream().startTiming();
708}
709
710void CConn::endRect(const Rect& r, unsigned int encoding) {
711 sock->inStream().stopTiming();
712 lastUsedEncoding_ = encoding;
713 if (debugDelay != 0) {
714 window->invertRect(r);
715 debugRects.push_back(r);
716 }
717}
718
719void CConn::fillRect(const Rect& r, Pixel pix) {
720 window->fillRect(r, pix);
721}
722void CConn::imageRect(const Rect& r, void* pixels) {
723 window->imageRect(r, pixels);
724}
725void CConn::copyRect(const Rect& r, int srcX, int srcY) {
726 window->copyRect(r, srcX, srcY);
727}
728
729void CConn::getUserPasswd(char** user, char** password) {
730 if (!user && options.passwordFile.buf[0]) {
731 FILE* fp = fopen(options.passwordFile.buf, "rb");
732 if (fp) {
733 char data[256];
734 int datalen = fread(data, 1, 256, fp);
735 fclose(fp);
736 if (datalen == 8) {
737 ObfuscatedPasswd obfPwd;
738 obfPwd.buf = data;
739 obfPwd.length = datalen;
740 PlainPasswd passwd(obfPwd);
Peter Åstrand8ace9152008-12-03 11:57:18 +0000741 obfPwd.takeBuf();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000742 *password = strDup(passwd.buf);
Peter Åstrand8ace9152008-12-03 11:57:18 +0000743 memset(data, 0, sizeof(data));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000744 }
745 }
746 }
747 if (user && options.userName.buf)
748 *user = strDup(options.userName.buf);
749 if (password && options.password.buf)
750 *password = strDup(options.password.buf);
751 if ((user && !*user) || (password && !*password)) {
752 // Missing username or password - prompt the user
753 UserPasswdDialog userPasswdDialog;
754 userPasswdDialog.setCSecurity(getCurrentCSecurity());
755 userPasswdDialog.getUserPasswd(user, password);
756 }
757 if (user) options.setUserName(*user);
758 if (password) options.setPassword(*password);
759}
760
761bool CConn::processFTMsg(int type) {
762 return m_fileTransfer.processFTMsg(type);
763}