blob: 388014ffe8ab134ce8ff4b0aff8f0bcdf49e4892 [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()
77 : window(0), sock(0), sockEvent(CreateEvent(0, TRUE, FALSE, 0)), requestUpdate(false),
78 sameMachine(false), encodingChange(false), formatChange(false),
79 reverseConnection(false), lastUsedEncoding_(encodingRaw), isClosed_(false) {
80}
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);
george82ffc14a62006-09-05 06:51:41 +0000160 if (options.autoScaling) {
161 window->setAutoScaling(true);
162 } else {
163 window->setAutoScaling(false);
164 window->setDesktopScale(options.scale);
165 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000166 if (!options.useLocalCursor)
167 window->setCursor(0, 0, Point(), 0, 0);
168 }
169}
170
171
172void
173CConn::displayChanged() {
174 // Display format has changed - recalculate the full-colour pixel format
175 calculateFullColourPF();
176}
177
178void
179CConn::paintCompleted() {
180 // A repaint message has just completed - request next update if necessary
181 requestNewUpdate();
182}
183
184bool
185CConn::sysCommand(WPARAM wParam, LPARAM lParam) {
186 // - If it's one of our (F8 Menu) messages
187 switch (wParam) {
188 case IDM_FULLSCREEN:
189 options.fullScreen = !window->isFullscreen();
190 window->setFullscreen(options.fullScreen);
191 return true;
george824f72ab32006-09-10 05:13:45 +0000192 case IDM_ZOOM_IN:
george824f72ab32006-09-10 05:13:45 +0000193 case IDM_ZOOM_OUT:
george82b6d87aa2006-09-11 07:00:59 +0000194 {
195 if (options.autoScaling) {
196 options.scale = window->getDesktopScale();
197 options.autoScaling = false;
198 window->setAutoScaling(false);
199 }
200 if (wParam == IDM_ZOOM_IN) {
201 for (int i = 0; i < scaleCount; i++)
202 if (options.scale < scaleValues[i]) {
203 options.scale = scaleValues[i];
204 break;
205 }
206 } else {
207 for (int i = scaleCount-1; i >= 0; i--)
208 if (options.scale > scaleValues[i]) {
209 options.scale = scaleValues[i];
210 break;
211 }
212 }
213 if (options.scale != window->getDesktopScale())
214 window->setDesktopScale(options.scale);
215 }
george824f72ab32006-09-10 05:13:45 +0000216 return true;
217 case IDM_ACTUAL_SIZE:
218 if (options.autoScaling) {
219 options.autoScaling = false;
220 window->setAutoScaling(false);
221 }
george824f72ab32006-09-10 05:13:45 +0000222 options.scale = 100;
223 window->setDesktopScale(100);
224 return true;
225 case IDM_AUTO_SIZE:
226 options.autoScaling = true;
227 window->setAutoScaling(true);
228 return true;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000229 case IDM_SHOW_TOOLBAR:
230 options.showToolbar = !window->isToolbarEnabled();
231 window->setShowToolbar(options.showToolbar);
232 return true;
233 case IDM_CTRL_KEY:
234 window->kbd.keyEvent(this, VK_CONTROL, 0, !window->kbd.keyPressed(VK_CONTROL));
235 return true;
236 case IDM_ALT_KEY:
237 window->kbd.keyEvent(this, VK_MENU, 0, !window->kbd.keyPressed(VK_MENU));
238 return true;
239 case IDM_SEND_MENU_KEY:
240 window->kbd.keyEvent(this, options.menuKey, 0, true);
241 window->kbd.keyEvent(this, options.menuKey, 0, false);
242 return true;
243 case IDM_SEND_CAD:
244 window->kbd.keyEvent(this, VK_CONTROL, 0, true);
245 window->kbd.keyEvent(this, VK_MENU, 0, true);
246 window->kbd.keyEvent(this, VK_DELETE, 0x1000000, true);
247 window->kbd.keyEvent(this, VK_DELETE, 0x1000000, false);
248 window->kbd.keyEvent(this, VK_MENU, 0, false);
249 window->kbd.keyEvent(this, VK_CONTROL, 0, false);
250 return true;
251 case IDM_SEND_CTLESC:
252 window->kbd.keyEvent(this, VK_CONTROL, 0, true);
253 window->kbd.keyEvent(this, VK_ESCAPE, 0, true);
254 window->kbd.keyEvent(this, VK_ESCAPE, 0, false);
255 window->kbd.keyEvent(this, VK_CONTROL, 0, false);
256 return true;
257 case IDM_REQUEST_REFRESH:
258 try {
259 writer()->writeFramebufferUpdateRequest(Rect(0,0,cp.width,cp.height), false);
260 requestUpdate = false;
261 } catch (rdr::Exception& e) {
262 close(e.str());
263 }
264 return true;
265 case IDM_NEWCONN:
266 {
267 Thread* newThread = new CConnThread;
268 }
269 return true;
270 case IDM_OPTIONS:
271 // Update the monitor device name in the CConnOptions instance
272 options.monitor.replaceBuf(window->getMonitor());
273 showOptionsDialog();
274 return true;
275 case IDM_INFO:
276 infoDialog.showDialog(this);
277 return true;
278 case IDM_ABOUT:
279 AboutDialog::instance.showDialog();
280 return true;
281 case IDM_FILE_TRANSFER:
282 m_fileTransfer.show(window->getHandle());
283 return true;
284 case IDM_CONN_SAVE_AS:
285 return true;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000286 };
287 return false;
288}
289
290
291void
292CConn::closeWindow() {
293 vlog.info("window closed");
294 close();
295}
296
297
298void
299CConn::refreshMenu(bool enableSysItems) {
300 HMENU menu = GetSystemMenu(window->getHandle(), FALSE);
301
302 if (!enableSysItems) {
303 // Gray out menu items that might cause a World Of Pain
304 EnableMenuItem(menu, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
305 EnableMenuItem(menu, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
306 EnableMenuItem(menu, SC_RESTORE, MF_BYCOMMAND | MF_ENABLED);
307 EnableMenuItem(menu, SC_MINIMIZE, MF_BYCOMMAND | MF_ENABLED);
308 EnableMenuItem(menu, SC_MAXIMIZE, MF_BYCOMMAND | MF_ENABLED);
309 }
310
311 // Update the modifier key menu items
312 UINT ctrlCheckFlags = window->kbd.keyPressed(VK_CONTROL) ? MF_CHECKED : MF_UNCHECKED;
313 UINT altCheckFlags = window->kbd.keyPressed(VK_MENU) ? MF_CHECKED : MF_UNCHECKED;
314 CheckMenuItem(menu, IDM_CTRL_KEY, MF_BYCOMMAND | ctrlCheckFlags);
315 CheckMenuItem(menu, IDM_ALT_KEY, MF_BYCOMMAND | altCheckFlags);
316
317 // Ensure that the Send <MenuKey> menu item has the correct text
318 if (options.menuKey) {
319 TCharArray menuKeyStr(options.menuKeyName());
320 TCharArray tmp(_tcslen(menuKeyStr.buf) + 6);
321 _stprintf(tmp.buf, _T("Send %s"), menuKeyStr.buf);
322 if (!ModifyMenu(menu, IDM_SEND_MENU_KEY, MF_BYCOMMAND | MF_STRING, IDM_SEND_MENU_KEY, tmp.buf))
323 InsertMenu(menu, IDM_SEND_CAD, MF_BYCOMMAND | MF_STRING, IDM_SEND_MENU_KEY, tmp.buf);
324 } else {
325 RemoveMenu(menu, IDM_SEND_MENU_KEY, MF_BYCOMMAND);
326 }
327
328 // Set the menu fullscreen option tick
329 CheckMenuItem(menu, IDM_FULLSCREEN, (window->isFullscreen() ? MF_CHECKED : 0) | MF_BYCOMMAND);
330
331 // Set the menu toolbar option tick
332 int toolbarFlags = window->isToolbarEnabled() ? MF_CHECKED : 0;
333 CheckMenuItem(menu, IDM_SHOW_TOOLBAR, MF_BYCOMMAND | toolbarFlags);
334
335 // In the full-screen mode, "Show toolbar" should be grayed.
336 toolbarFlags = window->isFullscreen() ? MF_GRAYED : MF_ENABLED;
337 EnableMenuItem(menu, IDM_SHOW_TOOLBAR, MF_BYCOMMAND | toolbarFlags);
338}
339
340
341void
342CConn::blockCallback() {
343 // - An InStream has blocked on I/O while processing an RFB message
344 // We re-enable socket event notifications, so we'll know when more
345 // data is available, then we sit and dispatch window events until
346 // the notification arrives.
347 if (!isClosed()) {
348 if (WSAEventSelect(sock->getFd(), sockEvent, FD_READ | FD_CLOSE) == SOCKET_ERROR)
349 throw rdr::SystemException("Unable to wait for sokcet data", WSAGetLastError());
350 }
351 while (true) {
352 // If we have closed then we can't block waiting for data
353 if (isClosed())
354 throw rdr::EndOfStream();
355
356 // Wait for socket data, or a message to process
357 DWORD result = MsgWaitForMultipleObjects(1, &sockEvent.h, FALSE, INFINITE, QS_ALLINPUT);
358 if (result == WAIT_OBJECT_0) {
359 // - Network event notification. Return control to I/O routine.
360 break;
361 } else if (result == WAIT_FAILED) {
362 // - The wait operation failed - raise an exception
363 throw rdr::SystemException("blockCallback wait error", GetLastError());
364 }
365
366 // - There should be a message in the message queue
367 MSG msg;
368 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
369 // IMPORTANT: We mustn't call TranslateMessage() here, because instead we
370 // call ToAscii() in CKeyboard::keyEvent(). ToAscii() stores dead key
371 // state from one call to the next, which would be messed up by calls to
372 // TranslateMessage() (actually it looks like TranslateMessage() calls
373 // ToAscii() internally).
374 DispatchMessage(&msg);
375 }
376 }
377
378 // Before we return control to the InStream, reset the network event
379 WSAEventSelect(sock->getFd(), sockEvent, 0);
380 ResetEvent(sockEvent);
381}
382
383
384void CConn::keyEvent(rdr::U32 key, bool down) {
385 if (!options.sendKeyEvents) return;
386 try {
387 writer()->keyEvent(key, down);
388 } catch (rdr::Exception& e) {
389 close(e.str());
390 }
391}
392void CConn::pointerEvent(const Point& pos, int buttonMask) {
393 if (!options.sendPtrEvents) return;
394 try {
395 writer()->pointerEvent(pos, buttonMask);
396 } catch (rdr::Exception& e) {
397 close(e.str());
398 }
399}
400void CConn::clientCutText(const char* str, int len) {
401 if (!options.clientCutText) return;
402 if (state() != RFBSTATE_NORMAL) return;
403 try {
404 writer()->clientCutText(str, len);
405 } catch (rdr::Exception& e) {
406 close(e.str());
407 }
408}
409
410
411CSecurity* CConn::getCSecurity(int secType)
412{
413 switch (secType) {
414 case secTypeNone:
415 return new CSecurityNone();
416 case secTypeVncAuth:
417 return new CSecurityVncAuth(this);
418 default:
419 throw Exception("Unsupported secType?");
420 }
421}
422
423
424void
425CConn::setColourMapEntries(int first, int count, U16* rgbs) {
426 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
427 int i;
428 for (i=0;i<count;i++)
429 window->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
430 // *** change to 0, 256?
431 window->refreshWindowPalette(first, count);
432}
433
434void
435CConn::bell() {
436 if (options.acceptBell)
437 MessageBeep(-1);
438}
439
440
441void
442CConn::setDesktopSize(int w, int h) {
443 vlog.debug("setDesktopSize %dx%d", w, h);
444
445 // Resize the window's buffer
446 if (window)
447 window->setSize(w, h);
448
449 // Tell the underlying CConnection
450 CConnection::setDesktopSize(w, h);
451}
452
453void
454CConn::setCursor(int w, int h, const Point& hotspot, void* data, void* mask) {
455 if (!options.useLocalCursor) return;
456
457 // Set the window to use the new cursor
458 window->setCursor(w, h, hotspot, data, mask);
459}
460
461
462void
463CConn::close(const char* reason) {
464 // If already closed then ignore this
465 if (isClosed())
466 return;
467
468 // Hide the window, if it exists
469 if (window)
470 ShowWindow(window->getHandle(), SW_HIDE);
471
472 // Save the reason & flag that we're closed & shutdown the socket
473 isClosed_ = true;
474 closeReason_.replaceBuf(strDup(reason));
475 sock->shutdown();
476}
477
478
479void
480CConn::showOptionsDialog() {
481 optionsDialog.showDialog(this);
482}
483
484
485void
486CConn::framebufferUpdateEnd() {
487 if (debugDelay != 0) {
488 vlog.debug("debug delay %d",(int)debugDelay);
489 UpdateWindow(window->getHandle());
490 Sleep(debugDelay);
491 std::list<rfb::Rect>::iterator i;
492 for (i = debugRects.begin(); i != debugRects.end(); i++) {
493 window->invertRect(*i);
494 }
495 debugRects.clear();
496 }
497 if (options.autoSelect)
498 autoSelectFormatAndEncoding();
499
500 // Always request the next update
501 requestUpdate = true;
502
503 // Check that at least part of the window has changed
504 if (!GetUpdateRect(window->getHandle(), 0, FALSE)) {
505 if (!(GetWindowLong(window->getHandle(), GWL_STYLE) & WS_MINIMIZE))
506 requestNewUpdate();
507 }
508
509 // Make sure the local cursor is shown
510 window->showCursor();
511}
512
513
514// Note: The method below is duplicated in vncviewer_unix/CConn.cxx!
515
516// autoSelectFormatAndEncoding() chooses the format and encoding appropriate
517// to the connection speed:
518//
519// Above 16Mbps (timing for at least a second), switch to hextile
520// Otherwise, switch to ZRLE
521//
522// Above 256Kbps, use full colour mode
523//
524void
525CConn::autoSelectFormatAndEncoding() {
526 int kbitsPerSecond = sock->inStream().kbitsPerSecond();
527 unsigned int newEncoding = options.preferredEncoding;
528
529 bool newFullColour = options.fullColour;
530 unsigned int timeWaited = sock->inStream().timeWaited();
531
532 // Select best encoding
533 if (kbitsPerSecond > 16000 && timeWaited >= 10000) {
534 newEncoding = encodingHextile;
535 } else {
536 newEncoding = encodingZRLE;
537 }
538
539 if (newEncoding != options.preferredEncoding) {
540 vlog.info("Throughput %d kbit/s - changing to %s encoding",
541 kbitsPerSecond, encodingName(newEncoding));
542 options.preferredEncoding = newEncoding;
543 encodingChange = true;
544 }
545
546 if (kbitsPerSecond == 0) {
547 return;
548 }
549
550 if (cp.beforeVersion(3, 8)) {
551 // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
552 // cursors "asynchronously". If this happens in the middle of a
553 // pixel format change, the server will encode the cursor with
554 // the old format, but the client will try to decode it
555 // according to the new format. This will lead to a
556 // crash. Therefore, we do not allow automatic format change for
557 // old servers.
558 return;
559 }
560
561 // Select best color level
562 newFullColour = (kbitsPerSecond > 256);
563 if (newFullColour != options.fullColour) {
564 vlog.info("Throughput %d kbit/s - full color is now %s",
565 kbitsPerSecond,
566 newFullColour ? "enabled" : "disabled");
567 options.fullColour = newFullColour;
568 formatChange = true;
569 }
570}
571
572void
573CConn::requestNewUpdate() {
574 if (!requestUpdate) return;
575
576 if (formatChange) {
577 // Select the required pixel format
578 if (options.fullColour) {
579 window->setPF(fullColourPF);
580 } else {
581 switch (options.lowColourLevel) {
582 case 0:
583 window->setPF(PixelFormat(8,3,0,1,1,1,1,2,1,0));
584 break;
585 case 1:
586 window->setPF(PixelFormat(8,6,0,1,3,3,3,4,2,0));
587 break;
588 case 2:
589 window->setPF(PixelFormat(8,8,0,0,0,0,0,0,0,0));
590 break;
591 }
592 }
593
594 // Print the current pixel format
595 char str[256];
596 window->getPF().print(str, 256);
597 vlog.info("Using pixel format %s",str);
598
599 // Save the connection pixel format and tell server to use it
600 cp.setPF(window->getPF());
601 writer()->writeSetPixelFormat(cp.pf());
602
603 // Correct the local window's palette
604 if (!window->getNativePF().trueColour)
605 window->refreshWindowPalette(0, 1 << cp.pf().depth);
606 }
607
608 if (encodingChange) {
609 vlog.info("Using %s encoding",encodingName(options.preferredEncoding));
610 writer()->writeSetEncodings(options.preferredEncoding, true);
611 }
612
613 writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
614 !formatChange);
615
616 encodingChange = formatChange = requestUpdate = false;
617}
618
619
620void
621CConn::calculateFullColourPF() {
622 // If the server is palette based then use palette locally
623 // Also, don't bother doing bgr222
624 if (!serverDefaultPF.trueColour || (serverDefaultPF.depth < 6)) {
625 fullColourPF = serverDefaultPF;
626 options.fullColour = true;
627 } else {
628 // If server is trueColour, use lowest depth PF
629 PixelFormat native = window->getNativePF();
630 if ((serverDefaultPF.bpp < native.bpp) ||
631 ((serverDefaultPF.bpp == native.bpp) &&
632 (serverDefaultPF.depth < native.depth)))
633 fullColourPF = serverDefaultPF;
634 else
635 fullColourPF = window->getNativePF();
636 }
637 formatChange = true;
638}
639
640
641void
642CConn::setName(const char* name) {
643 if (window)
644 window->setName(name);
645 CConnection::setName(name);
646}
647
648
649void CConn::serverInit() {
650 CConnection::serverInit();
651
652 // If using AutoSelect with old servers, start in FullColor
653 // mode. See comment in autoSelectFormatAndEncoding.
654 if (cp.beforeVersion(3, 8) && options.autoSelect) {
655 options.fullColour = true;
656 }
657
658 // Show the window
659 window = new DesktopWindow(this);
660 window->setName(cp.name());
661 window->setSize(cp.width, cp.height);
george82d2c22522006-09-10 11:45:19 +0000662 applyOptions(options);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000663
664 // Save the server's current format
665 serverDefaultPF = cp.pf();
666
667 // Calculate the full-colour format to use
668 calculateFullColourPF();
669
670 // Request the initial update
671 vlog.info("requesting initial update");
672 formatChange = encodingChange = requestUpdate = true;
673 requestNewUpdate();
674
675 // Update the window menu
676 HMENU wndmenu = GetSystemMenu(window->getHandle(), FALSE);
677 int toolbarChecked = options.showToolbar ? MF_CHECKED : 0;
678
679 AppendMenu(wndmenu, MF_SEPARATOR, 0, 0);
680 AppendMenu(wndmenu, MF_STRING, IDM_FULLSCREEN, _T("&Full screen"));
681 AppendMenu(wndmenu, MF_STRING | toolbarChecked, IDM_SHOW_TOOLBAR,
682 _T("Show tool&bar"));
683 AppendMenu(wndmenu, MF_SEPARATOR, 0, 0);
684 AppendMenu(wndmenu, MF_STRING, IDM_CTRL_KEY, _T("Ctr&l"));
685 AppendMenu(wndmenu, MF_STRING, IDM_ALT_KEY, _T("Al&t"));
686 AppendMenu(wndmenu, MF_STRING, IDM_SEND_CAD, _T("Send Ctrl-Alt-&Del"));
687 AppendMenu(wndmenu, MF_STRING, IDM_SEND_CTLESC, _T("Send Ctrl-&Esc"));
688 AppendMenu(wndmenu, MF_STRING, IDM_REQUEST_REFRESH, _T("Refres&h Screen"));
689 AppendMenu(wndmenu, MF_SEPARATOR, 0, 0);
690 AppendMenu(wndmenu, MF_STRING, IDM_NEWCONN, _T("Ne&w Connection..."));
691 AppendMenu(wndmenu, MF_STRING, IDM_OPTIONS, _T("&Options..."));
692 AppendMenu(wndmenu, MF_STRING, IDM_INFO, _T("Connection &Info..."));
693 AppendMenu(wndmenu, MF_STRING, IDM_ABOUT, _T("&About..."));
694}
695
696void
697CConn::serverCutText(const char* str, int len) {
698 if (!options.serverCutText) return;
699 window->serverCutText(str, len);
700}
701
702
703void CConn::beginRect(const Rect& r, unsigned int encoding) {
704 sock->inStream().startTiming();
705}
706
707void CConn::endRect(const Rect& r, unsigned int encoding) {
708 sock->inStream().stopTiming();
709 lastUsedEncoding_ = encoding;
710 if (debugDelay != 0) {
711 window->invertRect(r);
712 debugRects.push_back(r);
713 }
714}
715
716void CConn::fillRect(const Rect& r, Pixel pix) {
717 window->fillRect(r, pix);
718}
719void CConn::imageRect(const Rect& r, void* pixels) {
720 window->imageRect(r, pixels);
721}
722void CConn::copyRect(const Rect& r, int srcX, int srcY) {
723 window->copyRect(r, srcX, srcY);
724}
725
726void CConn::getUserPasswd(char** user, char** password) {
727 if (!user && options.passwordFile.buf[0]) {
728 FILE* fp = fopen(options.passwordFile.buf, "rb");
729 if (fp) {
730 char data[256];
731 int datalen = fread(data, 1, 256, fp);
732 fclose(fp);
733 if (datalen == 8) {
734 ObfuscatedPasswd obfPwd;
735 obfPwd.buf = data;
736 obfPwd.length = datalen;
737 PlainPasswd passwd(obfPwd);
738 *password = strDup(passwd.buf);
739 memset(data, 0, strlen(data));
740 }
741 }
742 }
743 if (user && options.userName.buf)
744 *user = strDup(options.userName.buf);
745 if (password && options.password.buf)
746 *password = strDup(options.password.buf);
747 if ((user && !*user) || (password && !*password)) {
748 // Missing username or password - prompt the user
749 UserPasswdDialog userPasswdDialog;
750 userPasswdDialog.setCSecurity(getCurrentCSecurity());
751 userPasswdDialog.getUserPasswd(user, password);
752 }
753 if (user) options.setUserName(*user);
754 if (password) options.setPassword(*password);
755}
756
757bool CConn::processFTMsg(int type) {
758 return m_fileTransfer.processFTMsg(type);
759}