blob: fac91815a4ec66044846bc15d05dc0f5ed62d12e [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),
george82b95503e2005-02-21 17:02:34 +0000229 loopPlayback(false), imageDataStartTime(0) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000230
george82e6883de2005-02-08 14:42:12 +0000231 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000232
george823c8fbbf2005-01-24 11:09:08 +0000233 // Reset the full session time
234 strcpy(fullSessionTime, "00m:00s");
235
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000236 // Create the main window
237 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000238 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
239 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000240 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000241 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000242 if (!mainHwnd) {
243 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
244 }
245 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
246
247 // Create the backing buffer
248 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000249 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000250
george8217e92cb2005-01-31 16:01:02 +0000251 // Open the session file
252 if (fileName) {
253 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000254 if (initTime > 0) setPos(initTime);
255 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000256 } else {
257 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000258 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000259 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000260}
261
262RfbPlayer::~RfbPlayer() {
263 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000264 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000265 delete rfbReader->join();
266 rfbReader = 0;
267 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000268 if (mainHwnd) {
269 setVisible(false);
270 DestroyWindow(mainHwnd);
271 mainHwnd = 0;
272 }
george825beb62a2005-02-09 13:04:32 +0000273 if (buffer) delete buffer;
274 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000275 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000276 vlog.debug("~RfbPlayer done");
277}
278
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000279LRESULT
280RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
281 switch (msg) {
282
283 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000284
285 case WM_CREATE:
286 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000287 // Create the frame window
288 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
289 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
290 hwnd, 0, frameClass.instance, this);
291
george82d070c692005-01-19 16:44:04 +0000292 createToolBar(hwnd);
293
george82006f2792005-02-05 07:40:47 +0000294 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000295
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000296 return 0;
297 }
298
george827214b822004-12-12 07:02:51 +0000299 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000300
301 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000302 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000303 case ID_OPENFILE:
304 {
305 char curDir[_MAX_DIR];
306 static char filename[_MAX_PATH];
307 OPENFILENAME ofn;
308 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
309 GetCurrentDirectory(sizeof(curDir), curDir);
310
311 ofn.lStructSize = sizeof(OPENFILENAME);
312 ofn.hwndOwner = getMainHandle();
313 ofn.lpstrFile = filename;
314 ofn.nMaxFile = sizeof(filename);
315 ofn.lpstrInitialDir = curDir;
316 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
317 "All files (*.*)\0*.*\0";
318 ofn.lpstrDefExt = "rfb";
319 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000320 if (GetOpenFileName(&ofn)) {
321 colourDepth = DEPTH_AUTO;
george826e51fcc2005-02-06 13:30:49 +0000322 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000323 }
george826e51fcc2005-02-06 13:30:49 +0000324 }
325 break;
george8271ca1772005-02-13 10:50:46 +0000326 case ID_CLOSEFILE:
327 closeSessionFile();
328 break;
george825c13c662005-01-27 14:48:23 +0000329 case ID_PLAY:
330 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000331 break;
332 case ID_PAUSE:
333 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000334 break;
335 case ID_STOP:
336 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000337 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000338 }
george825c13c662005-01-27 14:48:23 +0000339 break;
340 case ID_PLAYPAUSE:
341 if (isPaused()) {
342 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000343 } else {
344 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000345 }
george825c13c662005-01-27 14:48:23 +0000346 break;
george827549df42005-02-08 16:31:02 +0000347 case ID_GOTO:
348 {
349 GotoPosDialog gotoPosDlg;
350 if (gotoPosDlg.showDialog()) {
george821d5d40d2005-02-20 03:25:47 +0000351 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
352 setPos(gotoTime);
353 updatePos(gotoTime);
george827549df42005-02-08 16:31:02 +0000354 }
355 }
356 break;
george825c13c662005-01-27 14:48:23 +0000357 case ID_FULLSCREEN:
358 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
359 break;
george8231a36332005-02-06 17:27:34 +0000360 case ID_LOOP:
361 loopPlayback = !loopPlayback;
362 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
363 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
364 break;
george824ea27f62005-01-29 15:03:06 +0000365 case ID_RETURN:
366 // Update the speed if return pressed in speedEdit
367 if (speedEdit == GetFocus()) {
368 char speedStr[20], *stopStr;
369 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
370 double speed = strtod(speedStr, &stopStr);
371 if (speed > 0) {
372 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000373 } else {
374 speed = getSpeed();
375 }
376 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000377 }
378 break;
george8201aa6732005-02-06 17:13:03 +0000379 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000380 PostQuitMessage(0);
381 break;
george82ef5f7262005-02-08 15:09:26 +0000382 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000383 MessageBox(getMainHandle(),
384 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
385 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000386 }
387 break;
388
389 // Update frame's window size and add scrollbars if required
390
391 case WM_SIZE:
392 {
george82d070c692005-01-19 16:44:04 +0000393
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000394 Point old_offset = bufferToClient(Point(0, 0));
395
396 // Update the cached sizing information
397 RECT r;
398 GetClientRect(getMainHandle(), &r);
399 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
400 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
401
402 GetWindowRect(getFrameHandle(), &r);
403 window_size = Rect(r.left, r.top, r.right, r.bottom);
404 GetClientRect(getFrameHandle(), &r);
405 client_size = Rect(r.left, r.top, r.right, r.bottom);
406
407 // Determine whether scrollbars are required
408 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000409
410 // Resize the ToolBar
411 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000412
413 // Redraw if required
414 if (!old_offset.equals(bufferToClient(Point(0, 0))))
415 InvalidateRect(getFrameHandle(), 0, TRUE);
416 }
417 break;
george828a471482005-02-06 07:15:53 +0000418
419 // Process messages from posTrackBar
420
421 case WM_HSCROLL:
422 {
423 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
424 Pos *= sliderStepMs;
425
426 switch (LOWORD(wParam)) {
427 case TB_PAGEUP:
428 case TB_PAGEDOWN:
429 case TB_LINEUP:
430 case TB_LINEDOWN:
431 case TB_THUMBTRACK:
432 sliderDraging = true;
433 updatePos(Pos);
434 return 0;
435 case TB_ENDTRACK:
436 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000437 sliderDraging = false;
438 return 0;
439 default:
440 break;
441 }
442 }
443 break;
george829e6e6cc2005-01-29 13:12:05 +0000444
445 case WM_NOTIFY:
446 switch (((NMHDR*)lParam)->code) {
447 case UDN_DELTAPOS:
448 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000449 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000450 char speedStr[20] = "\0";
451 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
452 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
453 double speed;
454
george824ea27f62005-01-29 15:03:06 +0000455 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000456 if (upDown->iDelta > 0) {
457 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
458 } else {
george824ea27f62005-01-29 15:03:06 +0000459 // It's need to round the UpDown position
460 if ((upDown->iPos * 0.5) != getSpeed()) {
461 upDown->iDelta = 0;
462 lResult = TRUE;
463 }
george829e6e6cc2005-01-29 13:12:05 +0000464 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
465 }
george829e6e6cc2005-01-29 13:12:05 +0000466 sprintf(speedStr, "%.2f", speed);
467 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000468 is->setSpeed(speed);
469 playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000470 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000471 }
george824ea27f62005-01-29 15:03:06 +0000472 }
george829e6e6cc2005-01-29 13:12:05 +0000473 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000474
475 case WM_CLOSE:
476 vlog.debug("WM_CLOSE %x", getMainHandle());
477 PostQuitMessage(0);
478 break;
479 }
480
481 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
482}
483
484LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
485 switch (msg) {
486
487 case WM_PAINT:
488 {
george825beb62a2005-02-09 13:04:32 +0000489 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000490 seekMode = true;
491 return 0;
492 } else {
493 if (seekMode) {
494 seekMode = false;
495 InvalidateRect(getFrameHandle(), 0, true);
496 UpdateWindow(getFrameHandle());
497 return 0;
498 }
499 }
500
501 PAINTSTRUCT ps;
502 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
503 if (!paintDC)
504 throw SystemException("unable to BeginPaint", GetLastError());
505 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
506
507 if (!pr.is_empty()) {
508
509 if (buffer->bitmap) {
510
511 // Get device context
512 BitmapDC bitmapDC(paintDC, buffer->bitmap);
513
514 // Blit the border if required
515 Rect bufpos = bufferToClient(buffer->getRect());
516 if (!pr.enclosed_by(bufpos)) {
517 vlog.debug("draw border");
518 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
519 RECT r;
520 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
521 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
522 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
523 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
524 }
525
526 // Do the blit
527 Point buf_pos = clientToBuffer(pr.tl);
528 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
529 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
530 throw SystemException("unable to BitBlt to window", GetLastError());
531
532 } else {
533 // Blit a load of black
534 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
535 0, 0, 0, BLACKNESS))
536 throw SystemException("unable to BitBlt to blank window", GetLastError());
537 }
538 }
539 EndPaint(getFrameHandle(), &ps);
540 }
541 return 0;
542
543 case WM_VSCROLL:
544 case WM_HSCROLL:
545 {
546 Point delta;
547 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
548
549 switch (LOWORD(wParam)) {
550 case SB_PAGEUP: newpos -= 50; break;
551 case SB_PAGEDOWN: newpos += 50; break;
552 case SB_LINEUP: newpos -= 5; break;
553 case SB_LINEDOWN: newpos += 5; break;
554 case SB_THUMBTRACK:
555 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
556 default: vlog.info("received unknown scroll message");
557 };
558
559 if (msg == WM_HSCROLL)
560 setViewportOffset(Point(newpos, scrolloffset.y));
561 else
562 setViewportOffset(Point(scrolloffset.x, newpos));
563
564 SCROLLINFO si;
565 si.cbSize = sizeof(si);
566 si.fMask = SIF_POS;
567 si.nPos = newpos;
568 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
569 }
570 break;
571 }
572
573 return DefWindowProc(hwnd, msg, wParam, lParam);
574}
575
george82d070c692005-01-19 16:44:04 +0000576void RfbPlayer::createToolBar(HWND parentHwnd) {
577 RECT tRect;
578 InitCommonControls();
579
580 tb.create(ID_TOOLBAR, parentHwnd);
581 tb.addBitmap(4, IDB_TOOLBAR);
582
583 // Create the control buttons
584 tb.addButton(0, ID_PLAY);
585 tb.addButton(1, ID_PAUSE);
586 tb.addButton(2, ID_STOP);
587 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
588 tb.addButton(3, ID_FULLSCREEN);
589 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
590
591 // Create the static control for the time output
592 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
593 tb.getButtonRect(6, &tRect);
594 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
595 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
596 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
597 GetModuleHandle(0), 0);
598 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
599
600 // Create the trackbar control for the time position
601 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
602 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000603 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000604 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
605 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
606 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
607 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000608 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000609 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
610
611 // Create the label with "Speed:" caption
612 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
613 tb.getButtonRect(10, &tRect);
614 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
615 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
616 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
617
618 // Create the edit control and the spin for the speed managing
619 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
620 tb.getButtonRect(11, &tRect);
621 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
622 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
623 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
624 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
625 // It's need to send notify messages to toolbar parent window
626 SetParent(speedEdit, tb.getHandle());
627
628 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
629 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000630 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000631}
632
george82a21d2952005-02-12 11:30:03 +0000633void RfbPlayer::disableTBandMenuItems() {
634 // Disable the menu items
635 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
636 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
637 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
638 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
640 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
641 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
642 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
643 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
644
645 // Disable the toolbar buttons and child controls
646 tb.enableButton(ID_PLAY, false);
647 tb.enableButton(ID_PAUSE, false);
648 tb.enableButton(ID_STOP, false);
649 tb.enableButton(ID_FULLSCREEN, false);
650 EnableWindow(posTrackBar, false);
651 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000652 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000653}
654
george82f5043162005-02-12 11:37:18 +0000655void RfbPlayer::enableTBandMenuItems() {
656 // Enable the menu items
657 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
658 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
659 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
660 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
661 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
662 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
663 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
664 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
665 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
666
667 // Enable the toolbar buttons and child controls
668 tb.enableButton(ID_PLAY, true);
669 tb.enableButton(ID_PAUSE, true);
670 tb.enableButton(ID_STOP, true);
671 tb.enableButton(ID_FULLSCREEN, true);
672 EnableWindow(posTrackBar, true);
673 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000674 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000675}
676
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000677void RfbPlayer::setVisible(bool visible) {
678 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
679 if (visible) {
680 // When the window becomes visible, make it active
681 SetForegroundWindow(getMainHandle());
682 SetActiveWindow(getMainHandle());
683 }
684}
685
686void RfbPlayer::setTitle(const char *title) {
687 char _title[256];
688 strcpy(_title, AppName);
689 strcat(_title, " - ");
690 strcat(_title, title);
691 SetWindowText(getMainHandle(), _title);
692}
693
694void RfbPlayer::setFrameSize(int width, int height) {
695 // Calculate and set required size for main window
696 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000697 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000698 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
699 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
700 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000701 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
702 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
703 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000704 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
705
706 // Enable/disable scrollbars as appropriate
707 calculateScrollBars();
708}
709
710void RfbPlayer::calculateScrollBars() {
711 // Calculate the required size of window
712 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
713 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
714 DWORD old_style;
715 RECT r;
716 SetRect(&r, 0, 0, buffer->width(), buffer->height());
717 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
718 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
719
720 // Work out whether scroll bars are required
721 do {
722 old_style = style;
723
724 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
725 style |= WS_HSCROLL;
726 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
727 }
728 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
729 style |= WS_VSCROLL;
730 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
731 }
732 } while (style != old_style);
733
734 // Tell Windows to update the window style & cached settings
735 if (style != current_style) {
736 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
737 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
738 }
739
740 // Update the scroll settings
741 SCROLLINFO si;
742 if (style & WS_VSCROLL) {
743 si.cbSize = sizeof(si);
744 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
745 si.nMin = 0;
746 si.nMax = buffer->height();
747 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
748 maxscrolloffset.y = max(0, si.nMax-si.nPage);
749 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
750 si.nPos = scrolloffset.y;
751 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
752 }
753 if (style & WS_HSCROLL) {
754 si.cbSize = sizeof(si);
755 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
756 si.nMin = 0;
757 si.nMax = buffer->width();
758 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
759 maxscrolloffset.x = max(0, si.nMax-si.nPage);
760 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
761 si.nPos = scrolloffset.x;
762 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
763 }
764}
765
766bool RfbPlayer::setViewportOffset(const Point& tl) {
767/* ***
768 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
769 max(0, min(maxscrolloffset.y, tl.y)));
770 */
771 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
772 max(0, min(tl.y, buffer->height()-client_size.height())));
773 Point delta = np.translate(scrolloffset.negate());
774 if (!np.equals(scrolloffset)) {
775 scrolloffset = np;
776 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
777 UpdateWindow(getFrameHandle());
778 return true;
779 }
780 return false;
781}
782
783void RfbPlayer::close(const char* reason) {
784 setVisible(false);
785 if (reason) {
786 vlog.info("closing - %s", reason);
787 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
788 }
789 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
790}
791
792void RfbPlayer::blankBuffer() {
793 fillRect(buffer->getRect(), 0);
794}
795
796void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000797 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000798 blankBuffer();
799 newSession(fileName);
800 skipHandshaking();
george825f326fe2005-02-20 08:01:01 +0000801 is->setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000802 if (paused) is->pausePlayback();
803 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000804}
805
806void RfbPlayer::processMsg() {
807 static long update_time = GetTickCount();
808 try {
george828a471482005-02-06 07:15:53 +0000809 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
810 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000811 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000812 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000813 update_time = GetTickCount();
814 }
815 RfbProto::processMsg();
816 } catch (rdr::Exception e) {
817 if (strcmp(e.str(), "[End Of File]") == 0) {
818 rewind();
george8231a36332005-02-06 17:27:34 +0000819 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000820 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000821 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000822 return;
823 }
824 // It's a special exception to perform backward seeking.
825 // We only rewind the stream and seek the offset
826 if (strcmp(e.str(), "[REWIND]") == 0) {
george82b95503e2005-02-21 17:02:34 +0000827 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000828 rewind();
george823104aec2005-02-21 13:20:56 +0000829 setPos(seekOffset);
830 updatePos(seekOffset);
george8223e08562005-01-31 15:16:42 +0000831 } else {
832 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
833 return;
834 }
835 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000836}
837
838void RfbPlayer::serverInit() {
839 RfbProto::serverInit();
840
george82b95503e2005-02-21 17:02:34 +0000841 // Save the image data start time
842 imageDataStartTime = is->getTimeOffset();
843
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000844 // Resize the backing buffer
845 buffer->setSize(cp.width, cp.height);
846
847 // Check on the true colour mode
848 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000849 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000850
851 // Set the session pixel format
george82193d8e42005-02-20 16:47:01 +0000852 switch (colourDepth) {
853 case DEPTH_AUTO:
854 break;
855 case DEPTH8_RGB332:
856 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
857 break;
858 case DEPTH16_RGB655:
859 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
860 break;
861 case DEPTH24_RGB888:
862 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
863 break;
864 default:
865 throw rdr::Exception("This color depth is not supported!");
866 }
867 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000868
869 // If the window is not maximised then resize it
870 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
871 setFrameSize(cp.width, cp.height);
872
873 // Set the window title and show it
874 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000875
george82d4d69e62005-02-05 09:23:18 +0000876 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000877 sessionTimeMs = calculateSessionTime(fileName);
878 sprintf(fullSessionTime, "%.2um:%.2us",
879 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000880 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000881 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
882 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000883 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000884
george82006f2792005-02-05 07:40:47 +0000885 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000886}
887
888void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
889 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
890 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
891/* int i;
892 for (i=0;i<count;i++) {
893 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
894 }
895 // *** change to 0, 256?
896 refreshWindowPalette(first, count);
897 palette_changed = true;
898 InvalidateRect(getFrameHandle(), 0, FALSE);*/
899}
900
901void RfbPlayer::bell() {
902 if (acceptBell)
903 MessageBeep(-1);
904}
905
906void RfbPlayer::serverCutText(const char* str, int len) {
907 if (cutText != NULL)
908 delete [] cutText;
909 cutText = new char[len + 1];
910 memcpy(cutText, str, len);
911 cutText[len] = '\0';
912}
913
914void RfbPlayer::frameBufferUpdateEnd() {
915};
916
917void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
918}
919
920void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
921}
922
923
924void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
925 buffer->fillRect(r, pix);
926 invalidateBufferRect(r);
927}
928
929void RfbPlayer::imageRect(const Rect& r, void* pixels) {
930 buffer->imageRect(r, pixels);
931 invalidateBufferRect(r);
932}
933
934void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
935 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
936 invalidateBufferRect(r);
937}
938
939bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
940 Rect rect = bufferToClient(crect);
941 if (rect.intersect(client_size).is_empty()) return false;
942 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
943 InvalidateRect(getFrameHandle(), &invalid, FALSE);
944 return true;
945}
946
george8257f13522005-02-05 08:48:22 +0000947long RfbPlayer::calculateSessionTime(char *filename) {
948 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000949 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000950 try {
951 while (TRUE) {
952 sessionFile.skip(1024);
953 }
954 } catch (rdr::Exception e) {
955 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000956 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000957 } else {
958 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
959 return 0;
960 }
961 }
962 return 0;
963}
964
george826b87aff2005-02-13 10:48:21 +0000965void RfbPlayer::closeSessionFile() {
966 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000967 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000968 RECT r;
969
970 // Uncheck all toolbar buttons
971 if (tb.getHandle()) {
972 tb.checkButton(ID_PLAY, false);
973 tb.checkButton(ID_PAUSE, false);
974 tb.checkButton(ID_STOP, false);
975 }
976
977 // Stop playback and update the player state
978 disableTBandMenuItems();
979 if (rfbReader) {
980 delete rfbReader->join();
981 rfbReader = 0;
982 delete [] fileName;
983 fileName = 0;
984 }
985 blankBuffer();
986 setTitle("None");
george827009c892005-02-19 12:49:42 +0000987 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +0000988 playbackSpeed = 1.0;
989 SendMessage(speedUpDown, UDM_SETPOS,
990 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
991 sprintf(speedStr, "%.2f", playbackSpeed);
992 SetWindowText(speedEdit, speedStr);
993 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
994
995 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +0000996 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
997 dwStyle &= ~WS_MAXIMIZE;
998 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
999 }
george822ff7a612005-02-19 17:05:24 +00001000 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1001 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1002 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001003 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001004 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001005 buffer->setSize(32, 32);
1006 calculateScrollBars();
1007
1008 // Update the cached sizing information and repaint the frame window
1009 GetWindowRect(getFrameHandle(), &r);
1010 window_size = Rect(r.left, r.top, r.right, r.bottom);
1011 GetClientRect(getFrameHandle(), &r);
1012 client_size = Rect(r.left, r.top, r.right, r.bottom);
1013 InvalidateRect(getFrameHandle(), 0, TRUE);
1014 UpdateWindow(getFrameHandle());
1015}
1016
george8217e92cb2005-01-31 16:01:02 +00001017void RfbPlayer::openSessionFile(char *_fileName) {
1018 fileName = strDup(_fileName);
1019
1020 // Close the previous reading thread
1021 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001022 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001023 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001024 }
1025 blankBuffer();
1026 newSession(fileName);
1027 setSpeed(playbackSpeed);
1028 rfbReader = new rfbSessionReader(this);
1029 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001030 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001031 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001032}
1033
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001034void RfbPlayer::setPaused(bool paused) {
1035 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001036 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001037 tb.checkButton(ID_PAUSE, true);
1038 tb.checkButton(ID_PLAY, false);
1039 tb.checkButton(ID_STOP, false);
1040 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1041 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001042 } else {
george825beb62a2005-02-09 13:04:32 +00001043 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001044 tb.checkButton(ID_PLAY, true);
1045 tb.checkButton(ID_STOP, false);
1046 tb.checkButton(ID_PAUSE, false);
1047 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1048 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001049 }
1050}
1051
george82006f2792005-02-05 07:40:47 +00001052void RfbPlayer::stopPlayback() {
1053 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001054 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001055 tb.checkButton(ID_STOP, true);
1056 tb.checkButton(ID_PLAY, false);
1057 tb.checkButton(ID_PAUSE, false);
1058 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1059 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001060 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001061}
1062
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001063void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001064 if (speed > 0) {
1065 char speedStr[20] = "\0";
1066 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001067 is->setSpeed(newSpeed);
1068 playbackSpeed = newSpeed;
1069 SendMessage(speedUpDown, UDM_SETPOS,
1070 0, MAKELONG((short)(newSpeed / 0.5), 0));
1071 sprintf(speedStr, "%.2f", newSpeed);
1072 SetWindowText(speedEdit, speedStr);
1073 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001074}
1075
1076double RfbPlayer::getSpeed() {
1077 return is->getSpeed();
1078}
1079
1080void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001081 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001082}
1083
1084long RfbPlayer::getSeekOffset() {
1085 return is->getSeekOffset();
1086}
1087
1088bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001089 if (is) return is->isSeeking();
1090 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001091}
1092
1093bool RfbPlayer::isSeekMode() {
1094 return seekMode;
1095}
1096
1097bool RfbPlayer::isPaused() {
1098 return is->isPaused();
1099}
1100
1101long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001102 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001103}
1104
george828a471482005-02-06 07:15:53 +00001105void RfbPlayer::updatePos(long newPos) {
1106 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001107 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001108 long time = newPos / 1000;
1109 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001110 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001111
1112 // Update the position of slider
1113 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001114 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1115 sliderStepMs / double(newPos);
1116 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1117 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1118 }
george828a471482005-02-06 07:15:53 +00001119 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001120}
1121
1122void RfbPlayer::skipHandshaking() {
1123 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1124 is->skip(skipBytes);
1125 state_ = RFBSTATE_NORMAL;
1126}
1127
1128void programInfo() {
1129 win32::FileVersionInfo inf;
1130 _tprintf(_T("%s - %s, Version %s\n"),
1131 inf.getVerString(_T("ProductName")),
1132 inf.getVerString(_T("FileDescription")),
1133 inf.getVerString(_T("FileVersion")));
1134 printf("%s\n", buildTime);
1135 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1136}
1137
1138void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001139 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001140}
1141
1142double playbackSpeed = 1.0;
1143long initTime = -1;
george82193d8e42005-02-20 16:47:01 +00001144int depth = DEPTH_AUTO;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001145bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001146char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001147bool print_usage = false;
1148bool acceptBell = false;
1149
1150bool processParams(int argc, char* argv[]) {
1151 for (int i = 1; i < argc; i++) {
1152 if ((strcasecmp(argv[i], "-help") == 0) ||
1153 (strcasecmp(argv[i], "--help") == 0) ||
1154 (strcasecmp(argv[i], "/help") == 0) ||
1155 (strcasecmp(argv[i], "-h") == 0) ||
1156 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001157 (strcasecmp(argv[i], "/?") == 0) ||
1158 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001159 print_usage = true;
1160 return true;
1161 }
1162
george82193d8e42005-02-20 16:47:01 +00001163 if ((strcasecmp(argv[i], "-depth") == 0) ||
1164 (strcasecmp(argv[i], "/depth") == 0) && (i < argc-1)) {
1165 depth = atoi(argv[++i]);
1166 if (depth < 0) {
1167 return false;
1168 }
1169 continue;
1170 }
1171
1172
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001173 if ((strcasecmp(argv[i], "-speed") == 0) ||
1174 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1175 playbackSpeed = atof(argv[++i]);
1176 if (playbackSpeed <= 0) {
1177 return false;
1178 }
1179 continue;
1180 }
1181
1182 if ((strcasecmp(argv[i], "-pos") == 0) ||
1183 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1184 initTime = atol(argv[++i]);
1185 if (initTime <= 0)
1186 return false;
1187 continue;
1188 }
1189
1190 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1191 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001192 autoplay = true;
1193 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001194 }
1195
1196 if ((strcasecmp(argv[i], "-bell") == 0) ||
1197 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001198 acceptBell = true;
1199 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001200 }
1201
1202 if (i != argc - 1)
1203 return false;
1204 }
1205
1206 fileName = strDup(argv[argc-1]);
1207 return true;
1208}
1209
1210//
1211// -=- WinMain
1212//
1213
1214int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1215
1216 // - Process the command-line
1217
1218 int argc = __argc;
1219 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001220 if ((argc > 1) && (!processParams(argc, argv))) {
1221 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1222 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001223 }
george82e6883de2005-02-08 14:42:12 +00001224
1225 if (print_usage) {
1226 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001227 return 0;
george8267cbcd02005-01-16 15:39:56 +00001228 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001229
george82e6883de2005-02-08 14:42:12 +00001230 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001231 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001232 try {
george82193d8e42005-02-20 16:47:01 +00001233 player = new RfbPlayer(fileName, depth, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001234 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001235 } catch (rdr::Exception e) {
1236 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1237 delete player;
1238 return 0;
1239 }
1240
1241 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001242 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001243 MSG msg;
1244 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001245 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1246 TranslateMessage(&msg);
1247 DispatchMessage(&msg);
1248 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001249 }
1250
george82e6883de2005-02-08 14:42:12 +00001251 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001252 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001253 if (player) delete player;
1254 } catch (rdr::Exception e) {
1255 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1256 }
1257
1258 return 0;
1259};