blob: b2943bdcfd1162ce3c28f2cf1b2535c6c8b4f460 [file] [log] [blame]
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001/* Copyright (C) 2004 TightVNC Team. 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// -=- RFB Player for Win32
20
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000021#include <rfb/LogWriter.h>
22#include <rfb/Exception.h>
23#include <rfb/Threading.h>
24
25#include <rfb_win32/Win32Util.h>
26#include <rfb_win32/WMShatter.h>
27
28#include <rfbplayer/rfbplayer.h>
29#include <rfbplayer/utils.h>
30#include <rfbplayer/resource.h>
george827549df42005-02-08 16:31:02 +000031#include <rfbplayer/GotoPosDialog.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000032
33using namespace rfb;
34using namespace rfb::win32;
35
36// -=- Variables & consts
37
38static LogWriter vlog("RfbPlayer");
39
40TStr rfb::win32::AppName("RfbPlayer");
41extern const char* buildTime;
42
george82e6883de2005-02-08 14:42:12 +000043char wrong_cmd_msg[] =
44 "Wrong command-line parameters!\n"
45 "Use for help: rfbplayer -help";
46
47char usage_msg[] =
48 "usage: rfbplayer <options> <filename>\n"
49 "Command-line options:\n"
50 " -help \t- Provide usage information.\n"
george82057a4472005-02-21 13:23:56 +000051 " -depth <bit> \t- Forces the color depth for the session.\n"
52 " \t Supports 8, 16 and 24 bit mode.\n"
george82e6883de2005-02-08 14:42:12 +000053 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
54 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
55 " -pos <ms> \t- Sets initial time position in the session file,\n"
56 " \t in milliseconds. Default: 0.\n"
57 " -autoplay \t- Runs the player in the playback mode.\n"
58 " -bell \t- Accepts the bell.\n";
59
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000060// -=- RfbPlayer's defines
61
62#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000063#define MAX_SPEED 10.00
64#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000065#define MAX_POS_TRACKBAR_RANGE 50
george8268d25142005-02-13 09:33:22 +000066#define DEFAULT_PLAYER_WIDTH 640
67#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000068
george82193d8e42005-02-20 16:47:01 +000069#define DEPTH_AUTO 0
70#define DEPTH8_RGB332 8
71#define DEPTH16_RGB655 16
72#define DEPTH24_RGB888 24
73
george82d070c692005-01-19 16:44:04 +000074#define ID_TOOLBAR 500
75#define ID_PLAY 510
76#define ID_PAUSE 520
77#define ID_TIME_STATIC 530
78#define ID_SPEED_STATIC 540
79#define ID_SPEED_EDIT 550
80#define ID_POS_TRACKBAR 560
81#define ID_SPEED_UPDOWN 570
82
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000083//
84// -=- RfbPlayerClass
85
86//
87// Window class used as the basis for RfbPlayer instance
88//
89
90class RfbPlayerClass {
91public:
92 RfbPlayerClass();
93 ~RfbPlayerClass();
94 ATOM classAtom;
95 HINSTANCE instance;
96};
97
98LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
99 LRESULT result;
100
101 if (msg == WM_CREATE)
102 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
103 else if (msg == WM_DESTROY) {
104 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000105 SetWindowLong(hwnd, GWL_USERDATA, 0);
106 }
107 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
108 if (!_this) {
109 vlog.info("null _this in %x, message %u", hwnd, msg);
110 return DefWindowProc(hwnd, msg, wParam, lParam);
111 }
112
113 try {
114 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
115 } catch (rdr::Exception& e) {
116 vlog.error("untrapped: %s", e.str());
117 }
118
119 return result;
120};
121
122RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
123 WNDCLASS wndClass;
124 wndClass.style = 0;
125 wndClass.lpfnWndProc = RfbPlayerProc;
126 wndClass.cbClsExtra = 0;
127 wndClass.cbWndExtra = 0;
128 wndClass.hInstance = instance = GetModuleHandle(0);
129 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000130 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000131 if (!wndClass.hIcon)
132 printf("unable to load icon:%ld", GetLastError());
133 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
134 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000135 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000136 wndClass.lpszClassName = _T("RfbPlayerClass");
137 classAtom = RegisterClass(&wndClass);
138 if (!classAtom) {
139 throw rdr::SystemException("unable to register RfbPlayer window class",
140 GetLastError());
141 }
142}
143
144RfbPlayerClass::~RfbPlayerClass() {
145 if (classAtom) {
146 UnregisterClass((const TCHAR*)classAtom, instance);
147 }
148}
149
150RfbPlayerClass baseClass;
151
152//
153// -=- RfbFrameClass
154
155//
156// Window class used to displaying the rfb data
157//
158
159class RfbFrameClass {
160public:
161 RfbFrameClass();
162 ~RfbFrameClass();
163 ATOM classAtom;
164 HINSTANCE instance;
165};
166
167LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
168 LRESULT result;
169
170 if (msg == WM_CREATE)
171 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
172 else if (msg == WM_DESTROY)
173 SetWindowLong(hwnd, GWL_USERDATA, 0);
174 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
175 if (!_this) {
176 vlog.info("null _this in %x, message %u", hwnd, msg);
177 return DefWindowProc(hwnd, msg, wParam, lParam);
178 }
179
180 try {
181 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
182 } catch (rdr::Exception& e) {
183 vlog.error("untrapped: %s", e.str());
184 }
185
186 return result;
187}
188
189RfbFrameClass::RfbFrameClass() : classAtom(0) {
190 WNDCLASS wndClass;
191 wndClass.style = 0;
192 wndClass.lpfnWndProc = FrameProc;
193 wndClass.cbClsExtra = 0;
194 wndClass.cbWndExtra = 0;
195 wndClass.hInstance = instance = GetModuleHandle(0);
196 wndClass.hIcon = 0;
197 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
198 wndClass.hbrBackground = 0;
199 wndClass.lpszMenuName = 0;
200 wndClass.lpszClassName = _T("RfbPlayerClass1");
201 classAtom = RegisterClass(&wndClass);
202 if (!classAtom) {
203 throw rdr::SystemException("unable to register RfbPlayer window class",
204 GetLastError());
205 }
206}
207
208RfbFrameClass::~RfbFrameClass() {
209 if (classAtom) {
210 UnregisterClass((const TCHAR*)classAtom, instance);
211 }
212}
213
214RfbFrameClass frameClass;
215
216//
217// -=- RfbPlayer instance implementation
218//
219
george82193d8e42005-02-20 16:47:01 +0000220RfbPlayer::RfbPlayer(char *_fileName, int _depth = DEPTH_AUTO,
221 long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000222 bool _autoplay = false, bool _acceptBell = false)
george82193d8e42005-02-20 16:47:01 +0000223: RfbProto(_fileName), colourDepth(_depth), initTime(_initTime),
224 playbackSpeed(_playbackSpeed), autoplay(_autoplay), buffer(0),
225 client_size(0, 0, 32, 32), window_size(0, 0, 32, 32), cutText(0),
george823104aec2005-02-21 13:20:56 +0000226 seekMode(false), fileName(_fileName), lastPos(0), timeStatic(0),
227 speedEdit(0), posTrackBar(0), speedUpDown(0), acceptBell(_acceptBell),
228 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george820d2e19d2005-03-03 15:47:55 +0000229 loopPlayback(false), imageDataStartTime(0), rewindFlag(false),
230 stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000231
george82e6883de2005-02-08 14:42:12 +0000232 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000233
george823c8fbbf2005-01-24 11:09:08 +0000234 // Reset the full session time
235 strcpy(fullSessionTime, "00m:00s");
236
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000237 // Create the main window
238 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000239 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
240 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000241 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000242 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000243 if (!mainHwnd) {
244 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
245 }
246 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
247
248 // Create the backing buffer
249 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000250 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000251
george8217e92cb2005-01-31 16:01:02 +0000252 // Open the session file
253 if (fileName) {
254 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000255 if (initTime > 0) setPos(initTime);
256 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000257 } else {
258 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000259 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000260 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000261}
262
263RfbPlayer::~RfbPlayer() {
264 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000265 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000266 delete rfbReader->join();
267 rfbReader = 0;
268 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000269 if (mainHwnd) {
270 setVisible(false);
271 DestroyWindow(mainHwnd);
272 mainHwnd = 0;
273 }
george825beb62a2005-02-09 13:04:32 +0000274 if (buffer) delete buffer;
275 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000276 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000277 vlog.debug("~RfbPlayer done");
278}
279
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000280LRESULT
281RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
282 switch (msg) {
283
284 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000285
286 case WM_CREATE:
287 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000288 // Create the frame window
289 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
290 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
291 hwnd, 0, frameClass.instance, this);
292
george82d070c692005-01-19 16:44:04 +0000293 createToolBar(hwnd);
294
george82006f2792005-02-05 07:40:47 +0000295 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000296
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000297 return 0;
298 }
299
george827214b822004-12-12 07:02:51 +0000300 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000301
302 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000303 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000304 case ID_OPENFILE:
305 {
306 char curDir[_MAX_DIR];
307 static char filename[_MAX_PATH];
308 OPENFILENAME ofn;
309 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
310 GetCurrentDirectory(sizeof(curDir), curDir);
311
312 ofn.lStructSize = sizeof(OPENFILENAME);
313 ofn.hwndOwner = getMainHandle();
314 ofn.lpstrFile = filename;
315 ofn.nMaxFile = sizeof(filename);
316 ofn.lpstrInitialDir = curDir;
317 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
318 "All files (*.*)\0*.*\0";
319 ofn.lpstrDefExt = "rfb";
320 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000321 if (GetOpenFileName(&ofn)) {
322 colourDepth = DEPTH_AUTO;
george826e51fcc2005-02-06 13:30:49 +0000323 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000324 }
george826e51fcc2005-02-06 13:30:49 +0000325 }
326 break;
george8271ca1772005-02-13 10:50:46 +0000327 case ID_CLOSEFILE:
328 closeSessionFile();
329 break;
george825c13c662005-01-27 14:48:23 +0000330 case ID_PLAY:
331 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000332 break;
333 case ID_PAUSE:
334 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000335 break;
336 case ID_STOP:
337 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000338 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000339 }
george825c13c662005-01-27 14:48:23 +0000340 break;
341 case ID_PLAYPAUSE:
342 if (isPaused()) {
343 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000344 } else {
345 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000346 }
george825c13c662005-01-27 14:48:23 +0000347 break;
george827549df42005-02-08 16:31:02 +0000348 case ID_GOTO:
349 {
350 GotoPosDialog gotoPosDlg;
351 if (gotoPosDlg.showDialog()) {
george821d5d40d2005-02-20 03:25:47 +0000352 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
353 setPos(gotoTime);
354 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000355 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000356 }
357 }
358 break;
george825c13c662005-01-27 14:48:23 +0000359 case ID_FULLSCREEN:
360 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
361 break;
george8231a36332005-02-06 17:27:34 +0000362 case ID_LOOP:
363 loopPlayback = !loopPlayback;
364 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
365 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
366 break;
george824ea27f62005-01-29 15:03:06 +0000367 case ID_RETURN:
368 // Update the speed if return pressed in speedEdit
369 if (speedEdit == GetFocus()) {
370 char speedStr[20], *stopStr;
371 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
372 double speed = strtod(speedStr, &stopStr);
373 if (speed > 0) {
374 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000375 } else {
376 speed = getSpeed();
377 }
378 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000379 }
380 break;
george8201aa6732005-02-06 17:13:03 +0000381 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000382 PostQuitMessage(0);
383 break;
george82ef5f7262005-02-08 15:09:26 +0000384 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000385 MessageBox(getMainHandle(),
386 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
387 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000388 }
389 break;
390
391 // Update frame's window size and add scrollbars if required
392
393 case WM_SIZE:
394 {
george82d070c692005-01-19 16:44:04 +0000395
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000396 Point old_offset = bufferToClient(Point(0, 0));
397
398 // Update the cached sizing information
399 RECT r;
400 GetClientRect(getMainHandle(), &r);
401 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
402 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
403
404 GetWindowRect(getFrameHandle(), &r);
405 window_size = Rect(r.left, r.top, r.right, r.bottom);
406 GetClientRect(getFrameHandle(), &r);
407 client_size = Rect(r.left, r.top, r.right, r.bottom);
408
409 // Determine whether scrollbars are required
410 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000411
412 // Resize the ToolBar
413 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000414
415 // Redraw if required
416 if (!old_offset.equals(bufferToClient(Point(0, 0))))
417 InvalidateRect(getFrameHandle(), 0, TRUE);
418 }
419 break;
george828a471482005-02-06 07:15:53 +0000420
421 // Process messages from posTrackBar
422
423 case WM_HSCROLL:
424 {
425 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
426 Pos *= sliderStepMs;
427
428 switch (LOWORD(wParam)) {
429 case TB_PAGEUP:
430 case TB_PAGEDOWN:
431 case TB_LINEUP:
432 case TB_LINEDOWN:
433 case TB_THUMBTRACK:
434 sliderDraging = true;
435 updatePos(Pos);
436 return 0;
437 case TB_ENDTRACK:
438 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000439 setPaused(isPaused());;
george828a471482005-02-06 07:15:53 +0000440 sliderDraging = false;
441 return 0;
442 default:
443 break;
444 }
445 }
446 break;
george829e6e6cc2005-01-29 13:12:05 +0000447
448 case WM_NOTIFY:
449 switch (((NMHDR*)lParam)->code) {
450 case UDN_DELTAPOS:
451 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000452 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000453 char speedStr[20] = "\0";
454 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
455 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
456 double speed;
457
george824ea27f62005-01-29 15:03:06 +0000458 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000459 if (upDown->iDelta > 0) {
460 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
461 } else {
george824ea27f62005-01-29 15:03:06 +0000462 // It's need to round the UpDown position
463 if ((upDown->iPos * 0.5) != getSpeed()) {
464 upDown->iDelta = 0;
465 lResult = TRUE;
466 }
george829e6e6cc2005-01-29 13:12:05 +0000467 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
468 }
george829e6e6cc2005-01-29 13:12:05 +0000469 sprintf(speedStr, "%.2f", speed);
470 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000471 is->setSpeed(speed);
472 playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000473 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000474 }
george824ea27f62005-01-29 15:03:06 +0000475 }
george829e6e6cc2005-01-29 13:12:05 +0000476 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000477
478 case WM_CLOSE:
479 vlog.debug("WM_CLOSE %x", getMainHandle());
480 PostQuitMessage(0);
481 break;
482 }
483
484 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
485}
486
487LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
488 switch (msg) {
489
490 case WM_PAINT:
491 {
george821e846ff2005-02-24 13:13:33 +0000492 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000493 seekMode = true;
494 return 0;
495 } else {
496 if (seekMode) {
497 seekMode = false;
498 InvalidateRect(getFrameHandle(), 0, true);
499 UpdateWindow(getFrameHandle());
500 return 0;
501 }
502 }
503
504 PAINTSTRUCT ps;
505 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
506 if (!paintDC)
507 throw SystemException("unable to BeginPaint", GetLastError());
508 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
509
510 if (!pr.is_empty()) {
511
512 if (buffer->bitmap) {
513
514 // Get device context
515 BitmapDC bitmapDC(paintDC, buffer->bitmap);
516
517 // Blit the border if required
518 Rect bufpos = bufferToClient(buffer->getRect());
519 if (!pr.enclosed_by(bufpos)) {
520 vlog.debug("draw border");
521 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
522 RECT r;
523 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
524 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
525 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
526 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
527 }
528
529 // Do the blit
530 Point buf_pos = clientToBuffer(pr.tl);
531 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
532 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
533 throw SystemException("unable to BitBlt to window", GetLastError());
534
535 } else {
536 // Blit a load of black
537 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
538 0, 0, 0, BLACKNESS))
539 throw SystemException("unable to BitBlt to blank window", GetLastError());
540 }
541 }
542 EndPaint(getFrameHandle(), &ps);
543 }
544 return 0;
545
546 case WM_VSCROLL:
547 case WM_HSCROLL:
548 {
549 Point delta;
550 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
551
552 switch (LOWORD(wParam)) {
553 case SB_PAGEUP: newpos -= 50; break;
554 case SB_PAGEDOWN: newpos += 50; break;
555 case SB_LINEUP: newpos -= 5; break;
556 case SB_LINEDOWN: newpos += 5; break;
557 case SB_THUMBTRACK:
558 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
559 default: vlog.info("received unknown scroll message");
560 };
561
562 if (msg == WM_HSCROLL)
563 setViewportOffset(Point(newpos, scrolloffset.y));
564 else
565 setViewportOffset(Point(scrolloffset.x, newpos));
566
567 SCROLLINFO si;
568 si.cbSize = sizeof(si);
569 si.fMask = SIF_POS;
570 si.nPos = newpos;
571 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
572 }
573 break;
574 }
575
576 return DefWindowProc(hwnd, msg, wParam, lParam);
577}
578
george82d070c692005-01-19 16:44:04 +0000579void RfbPlayer::createToolBar(HWND parentHwnd) {
580 RECT tRect;
581 InitCommonControls();
582
583 tb.create(ID_TOOLBAR, parentHwnd);
584 tb.addBitmap(4, IDB_TOOLBAR);
585
586 // Create the control buttons
587 tb.addButton(0, ID_PLAY);
588 tb.addButton(1, ID_PAUSE);
589 tb.addButton(2, ID_STOP);
590 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
591 tb.addButton(3, ID_FULLSCREEN);
592 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
593
594 // Create the static control for the time output
595 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
596 tb.getButtonRect(6, &tRect);
597 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
598 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
599 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
600 GetModuleHandle(0), 0);
601 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
602
603 // Create the trackbar control for the time position
604 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
605 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000606 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000607 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
608 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
609 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
610 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000611 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000612 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
613
614 // Create the label with "Speed:" caption
615 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
616 tb.getButtonRect(10, &tRect);
617 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
618 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
619 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
620
621 // Create the edit control and the spin for the speed managing
622 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
623 tb.getButtonRect(11, &tRect);
624 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
625 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
626 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
627 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
628 // It's need to send notify messages to toolbar parent window
629 SetParent(speedEdit, tb.getHandle());
630
631 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
632 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000633 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000634}
635
george82a21d2952005-02-12 11:30:03 +0000636void RfbPlayer::disableTBandMenuItems() {
637 // Disable the menu items
638 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
640 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
641 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
642 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
643 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
644 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
645 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
646 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
647
648 // Disable the toolbar buttons and child controls
649 tb.enableButton(ID_PLAY, false);
650 tb.enableButton(ID_PAUSE, false);
651 tb.enableButton(ID_STOP, false);
652 tb.enableButton(ID_FULLSCREEN, false);
653 EnableWindow(posTrackBar, false);
654 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000655 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000656}
657
george82f5043162005-02-12 11:37:18 +0000658void RfbPlayer::enableTBandMenuItems() {
659 // Enable the menu items
660 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
661 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
662 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
663 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
664 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
665 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
666 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
667 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
668 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
669
670 // Enable the toolbar buttons and child controls
671 tb.enableButton(ID_PLAY, true);
672 tb.enableButton(ID_PAUSE, true);
673 tb.enableButton(ID_STOP, true);
674 tb.enableButton(ID_FULLSCREEN, true);
675 EnableWindow(posTrackBar, true);
676 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000677 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000678}
679
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000680void RfbPlayer::setVisible(bool visible) {
681 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
682 if (visible) {
683 // When the window becomes visible, make it active
684 SetForegroundWindow(getMainHandle());
685 SetActiveWindow(getMainHandle());
686 }
687}
688
689void RfbPlayer::setTitle(const char *title) {
690 char _title[256];
691 strcpy(_title, AppName);
692 strcat(_title, " - ");
693 strcat(_title, title);
694 SetWindowText(getMainHandle(), _title);
695}
696
697void RfbPlayer::setFrameSize(int width, int height) {
698 // Calculate and set required size for main window
699 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000700 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000701 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
702 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
703 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000704 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
705 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
706 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000707 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
708
709 // Enable/disable scrollbars as appropriate
710 calculateScrollBars();
711}
712
713void RfbPlayer::calculateScrollBars() {
714 // Calculate the required size of window
715 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
716 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
717 DWORD old_style;
718 RECT r;
719 SetRect(&r, 0, 0, buffer->width(), buffer->height());
720 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
721 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
722
723 // Work out whether scroll bars are required
724 do {
725 old_style = style;
726
727 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
728 style |= WS_HSCROLL;
729 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
730 }
731 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
732 style |= WS_VSCROLL;
733 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
734 }
735 } while (style != old_style);
736
737 // Tell Windows to update the window style & cached settings
738 if (style != current_style) {
739 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
740 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
741 }
742
743 // Update the scroll settings
744 SCROLLINFO si;
745 if (style & WS_VSCROLL) {
746 si.cbSize = sizeof(si);
747 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
748 si.nMin = 0;
749 si.nMax = buffer->height();
750 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
751 maxscrolloffset.y = max(0, si.nMax-si.nPage);
752 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
753 si.nPos = scrolloffset.y;
754 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
755 }
756 if (style & WS_HSCROLL) {
757 si.cbSize = sizeof(si);
758 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
759 si.nMin = 0;
760 si.nMax = buffer->width();
761 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
762 maxscrolloffset.x = max(0, si.nMax-si.nPage);
763 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
764 si.nPos = scrolloffset.x;
765 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
766 }
767}
768
769bool RfbPlayer::setViewportOffset(const Point& tl) {
770/* ***
771 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
772 max(0, min(maxscrolloffset.y, tl.y)));
773 */
774 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
775 max(0, min(tl.y, buffer->height()-client_size.height())));
776 Point delta = np.translate(scrolloffset.negate());
777 if (!np.equals(scrolloffset)) {
778 scrolloffset = np;
779 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
780 UpdateWindow(getFrameHandle());
781 return true;
782 }
783 return false;
784}
785
786void RfbPlayer::close(const char* reason) {
787 setVisible(false);
788 if (reason) {
789 vlog.info("closing - %s", reason);
790 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
791 }
792 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
793}
794
795void RfbPlayer::blankBuffer() {
796 fillRect(buffer->getRect(), 0);
797}
798
799void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000800 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000801 blankBuffer();
802 newSession(fileName);
803 skipHandshaking();
george825f326fe2005-02-20 08:01:01 +0000804 is->setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000805 if (paused) is->pausePlayback();
806 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000807}
808
809void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000810 // Perform return if waitWhilePaused processed because
811 // rfbReader thread could receive the signal to close
812 if (waitWhilePaused()) return;
813
george8223e08562005-01-31 15:16:42 +0000814 static long update_time = GetTickCount();
815 try {
george828a471482005-02-06 07:15:53 +0000816 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
817 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000818 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000819 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000820 update_time = GetTickCount();
821 }
822 RfbProto::processMsg();
823 } catch (rdr::Exception e) {
824 if (strcmp(e.str(), "[End Of File]") == 0) {
825 rewind();
george8231a36332005-02-06 17:27:34 +0000826 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000827 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000828 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000829 return;
830 }
831 // It's a special exception to perform backward seeking.
832 // We only rewind the stream and seek the offset
833 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000834 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000835 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000836 rewind();
george820d2e19d2005-03-03 15:47:55 +0000837 if (!stopped) setPos(seekOffset);
838 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000839 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000840 rewindFlag = false;
george8223e08562005-01-31 15:16:42 +0000841 } else {
842 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
843 return;
844 }
845 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000846}
847
848void RfbPlayer::serverInit() {
849 RfbProto::serverInit();
850
george82b95503e2005-02-21 17:02:34 +0000851 // Save the image data start time
852 imageDataStartTime = is->getTimeOffset();
853
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000854 // Resize the backing buffer
855 buffer->setSize(cp.width, cp.height);
856
857 // Check on the true colour mode
858 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000859 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000860
861 // Set the session pixel format
george82193d8e42005-02-20 16:47:01 +0000862 switch (colourDepth) {
863 case DEPTH_AUTO:
864 break;
865 case DEPTH8_RGB332:
866 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
867 break;
868 case DEPTH16_RGB655:
869 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
870 break;
871 case DEPTH24_RGB888:
872 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
873 break;
874 default:
875 throw rdr::Exception("This color depth is not supported!");
876 }
877 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000878
879 // If the window is not maximised then resize it
880 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
881 setFrameSize(cp.width, cp.height);
882
883 // Set the window title and show it
884 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000885
george82d4d69e62005-02-05 09:23:18 +0000886 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000887 sessionTimeMs = calculateSessionTime(fileName);
888 sprintf(fullSessionTime, "%.2um:%.2us",
889 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000890 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000891 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
892 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000893 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000894
george82006f2792005-02-05 07:40:47 +0000895 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000896}
897
898void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
899 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
900 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
901/* int i;
902 for (i=0;i<count;i++) {
903 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
904 }
905 // *** change to 0, 256?
906 refreshWindowPalette(first, count);
907 palette_changed = true;
908 InvalidateRect(getFrameHandle(), 0, FALSE);*/
909}
910
911void RfbPlayer::bell() {
912 if (acceptBell)
913 MessageBeep(-1);
914}
915
916void RfbPlayer::serverCutText(const char* str, int len) {
917 if (cutText != NULL)
918 delete [] cutText;
919 cutText = new char[len + 1];
920 memcpy(cutText, str, len);
921 cutText[len] = '\0';
922}
923
924void RfbPlayer::frameBufferUpdateEnd() {
925};
926
927void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
928}
929
930void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
931}
932
933
934void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
935 buffer->fillRect(r, pix);
936 invalidateBufferRect(r);
937}
938
939void RfbPlayer::imageRect(const Rect& r, void* pixels) {
940 buffer->imageRect(r, pixels);
941 invalidateBufferRect(r);
942}
943
944void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
945 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
946 invalidateBufferRect(r);
947}
948
949bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
950 Rect rect = bufferToClient(crect);
951 if (rect.intersect(client_size).is_empty()) return false;
952 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
953 InvalidateRect(getFrameHandle(), &invalid, FALSE);
954 return true;
955}
956
george820d2e19d2005-03-03 15:47:55 +0000957bool RfbPlayer::waitWhilePaused() {
958 bool result = false;
959 while(isPaused() && !isSeeking()) {
960 Sleep(20);
961 result = true;
962 }
963 return result;
964}
965
george8257f13522005-02-05 08:48:22 +0000966long RfbPlayer::calculateSessionTime(char *filename) {
967 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000968 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000969 try {
970 while (TRUE) {
971 sessionFile.skip(1024);
972 }
973 } catch (rdr::Exception e) {
974 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000975 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000976 } else {
977 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
978 return 0;
979 }
980 }
981 return 0;
982}
983
george826b87aff2005-02-13 10:48:21 +0000984void RfbPlayer::closeSessionFile() {
985 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000986 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000987 RECT r;
988
989 // Uncheck all toolbar buttons
990 if (tb.getHandle()) {
991 tb.checkButton(ID_PLAY, false);
992 tb.checkButton(ID_PAUSE, false);
993 tb.checkButton(ID_STOP, false);
994 }
995
996 // Stop playback and update the player state
997 disableTBandMenuItems();
998 if (rfbReader) {
999 delete rfbReader->join();
1000 rfbReader = 0;
1001 delete [] fileName;
1002 fileName = 0;
1003 }
1004 blankBuffer();
1005 setTitle("None");
george827009c892005-02-19 12:49:42 +00001006 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +00001007 playbackSpeed = 1.0;
1008 SendMessage(speedUpDown, UDM_SETPOS,
1009 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
1010 sprintf(speedStr, "%.2f", playbackSpeed);
1011 SetWindowText(speedEdit, speedStr);
1012 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1013
1014 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001015 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1016 dwStyle &= ~WS_MAXIMIZE;
1017 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1018 }
george822ff7a612005-02-19 17:05:24 +00001019 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1020 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1021 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001022 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001023 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001024 buffer->setSize(32, 32);
1025 calculateScrollBars();
1026
1027 // Update the cached sizing information and repaint the frame window
1028 GetWindowRect(getFrameHandle(), &r);
1029 window_size = Rect(r.left, r.top, r.right, r.bottom);
1030 GetClientRect(getFrameHandle(), &r);
1031 client_size = Rect(r.left, r.top, r.right, r.bottom);
1032 InvalidateRect(getFrameHandle(), 0, TRUE);
1033 UpdateWindow(getFrameHandle());
1034}
1035
george8217e92cb2005-01-31 16:01:02 +00001036void RfbPlayer::openSessionFile(char *_fileName) {
1037 fileName = strDup(_fileName);
1038
1039 // Close the previous reading thread
1040 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001041 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001042 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001043 }
1044 blankBuffer();
1045 newSession(fileName);
1046 setSpeed(playbackSpeed);
1047 rfbReader = new rfbSessionReader(this);
1048 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001049 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001050 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001051}
1052
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001053void RfbPlayer::setPaused(bool paused) {
1054 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001055 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001056 tb.checkButton(ID_PAUSE, true);
1057 tb.checkButton(ID_PLAY, false);
1058 tb.checkButton(ID_STOP, false);
1059 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1060 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001061 } else {
george825beb62a2005-02-09 13:04:32 +00001062 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001063 tb.checkButton(ID_PLAY, true);
1064 tb.checkButton(ID_STOP, false);
1065 tb.checkButton(ID_PAUSE, false);
1066 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1067 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001068 }
1069}
1070
george82006f2792005-02-05 07:40:47 +00001071void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001072 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001073 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001074 if (is) {
1075 is->pausePlayback();
1076 is->interruptFrameDelay();
1077 }
george82006f2792005-02-05 07:40:47 +00001078 tb.checkButton(ID_STOP, true);
1079 tb.checkButton(ID_PLAY, false);
1080 tb.checkButton(ID_PAUSE, false);
1081 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1082 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001083 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001084}
1085
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001086void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001087 if (speed > 0) {
1088 char speedStr[20] = "\0";
1089 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001090 is->setSpeed(newSpeed);
1091 playbackSpeed = newSpeed;
1092 SendMessage(speedUpDown, UDM_SETPOS,
1093 0, MAKELONG((short)(newSpeed / 0.5), 0));
1094 sprintf(speedStr, "%.2f", newSpeed);
1095 SetWindowText(speedEdit, speedStr);
1096 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001097}
1098
1099double RfbPlayer::getSpeed() {
1100 return is->getSpeed();
1101}
1102
1103void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001104 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001105}
1106
1107long RfbPlayer::getSeekOffset() {
1108 return is->getSeekOffset();
1109}
1110
1111bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001112 if (is) return is->isSeeking();
1113 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001114}
1115
1116bool RfbPlayer::isSeekMode() {
1117 return seekMode;
1118}
1119
1120bool RfbPlayer::isPaused() {
1121 return is->isPaused();
1122}
1123
1124long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001125 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001126}
1127
george828a471482005-02-06 07:15:53 +00001128void RfbPlayer::updatePos(long newPos) {
1129 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001130 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001131 long time = newPos / 1000;
1132 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001133 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001134
1135 // Update the position of slider
1136 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001137 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1138 sliderStepMs / double(newPos);
1139 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1140 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1141 }
george828a471482005-02-06 07:15:53 +00001142 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001143}
1144
1145void RfbPlayer::skipHandshaking() {
1146 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1147 is->skip(skipBytes);
1148 state_ = RFBSTATE_NORMAL;
1149}
1150
1151void programInfo() {
1152 win32::FileVersionInfo inf;
1153 _tprintf(_T("%s - %s, Version %s\n"),
1154 inf.getVerString(_T("ProductName")),
1155 inf.getVerString(_T("FileDescription")),
1156 inf.getVerString(_T("FileVersion")));
1157 printf("%s\n", buildTime);
1158 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1159}
1160
1161void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001162 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001163}
1164
1165double playbackSpeed = 1.0;
1166long initTime = -1;
george82193d8e42005-02-20 16:47:01 +00001167int depth = DEPTH_AUTO;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001168bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001169char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001170bool print_usage = false;
1171bool acceptBell = false;
1172
1173bool processParams(int argc, char* argv[]) {
1174 for (int i = 1; i < argc; i++) {
1175 if ((strcasecmp(argv[i], "-help") == 0) ||
1176 (strcasecmp(argv[i], "--help") == 0) ||
1177 (strcasecmp(argv[i], "/help") == 0) ||
1178 (strcasecmp(argv[i], "-h") == 0) ||
1179 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001180 (strcasecmp(argv[i], "/?") == 0) ||
1181 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001182 print_usage = true;
1183 return true;
1184 }
1185
george82193d8e42005-02-20 16:47:01 +00001186 if ((strcasecmp(argv[i], "-depth") == 0) ||
1187 (strcasecmp(argv[i], "/depth") == 0) && (i < argc-1)) {
1188 depth = atoi(argv[++i]);
1189 if (depth < 0) {
1190 return false;
1191 }
1192 continue;
1193 }
1194
1195
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001196 if ((strcasecmp(argv[i], "-speed") == 0) ||
1197 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1198 playbackSpeed = atof(argv[++i]);
1199 if (playbackSpeed <= 0) {
1200 return false;
1201 }
1202 continue;
1203 }
1204
1205 if ((strcasecmp(argv[i], "-pos") == 0) ||
1206 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1207 initTime = atol(argv[++i]);
1208 if (initTime <= 0)
1209 return false;
1210 continue;
1211 }
1212
1213 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1214 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001215 autoplay = true;
1216 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001217 }
1218
1219 if ((strcasecmp(argv[i], "-bell") == 0) ||
1220 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001221 acceptBell = true;
1222 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001223 }
1224
1225 if (i != argc - 1)
1226 return false;
1227 }
1228
1229 fileName = strDup(argv[argc-1]);
1230 return true;
1231}
1232
1233//
1234// -=- WinMain
1235//
1236
1237int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1238
1239 // - Process the command-line
1240
1241 int argc = __argc;
1242 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001243 if ((argc > 1) && (!processParams(argc, argv))) {
1244 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1245 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001246 }
george82e6883de2005-02-08 14:42:12 +00001247
1248 if (print_usage) {
1249 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001250 return 0;
george8267cbcd02005-01-16 15:39:56 +00001251 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001252
george82e6883de2005-02-08 14:42:12 +00001253 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001254 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001255 try {
george82193d8e42005-02-20 16:47:01 +00001256 player = new RfbPlayer(fileName, depth, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001257 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001258 } catch (rdr::Exception e) {
1259 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1260 delete player;
1261 return 0;
1262 }
1263
1264 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001265 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001266 MSG msg;
1267 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001268 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1269 TranslateMessage(&msg);
1270 DispatchMessage(&msg);
1271 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001272 }
1273
george82e6883de2005-02-08 14:42:12 +00001274 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001275 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001276 if (player) delete player;
1277 } catch (rdr::Exception e) {
1278 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1279 }
1280
1281 return 0;
1282};