blob: d232461b4c123d105169832305a5b17e5d2fb737 [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);
george82a6900d72005-02-21 17:24:26 +0000354 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000355 }
356 }
357 break;
george825c13c662005-01-27 14:48:23 +0000358 case ID_FULLSCREEN:
359 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
360 break;
george8231a36332005-02-06 17:27:34 +0000361 case ID_LOOP:
362 loopPlayback = !loopPlayback;
363 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
364 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
365 break;
george824ea27f62005-01-29 15:03:06 +0000366 case ID_RETURN:
367 // Update the speed if return pressed in speedEdit
368 if (speedEdit == GetFocus()) {
369 char speedStr[20], *stopStr;
370 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
371 double speed = strtod(speedStr, &stopStr);
372 if (speed > 0) {
373 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000374 } else {
375 speed = getSpeed();
376 }
377 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000378 }
379 break;
george8201aa6732005-02-06 17:13:03 +0000380 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000381 PostQuitMessage(0);
382 break;
george82ef5f7262005-02-08 15:09:26 +0000383 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000384 MessageBox(getMainHandle(),
385 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
386 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000387 }
388 break;
389
390 // Update frame's window size and add scrollbars if required
391
392 case WM_SIZE:
393 {
george82d070c692005-01-19 16:44:04 +0000394
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000395 Point old_offset = bufferToClient(Point(0, 0));
396
397 // Update the cached sizing information
398 RECT r;
399 GetClientRect(getMainHandle(), &r);
400 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
401 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
402
403 GetWindowRect(getFrameHandle(), &r);
404 window_size = Rect(r.left, r.top, r.right, r.bottom);
405 GetClientRect(getFrameHandle(), &r);
406 client_size = Rect(r.left, r.top, r.right, r.bottom);
407
408 // Determine whether scrollbars are required
409 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000410
411 // Resize the ToolBar
412 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000413
414 // Redraw if required
415 if (!old_offset.equals(bufferToClient(Point(0, 0))))
416 InvalidateRect(getFrameHandle(), 0, TRUE);
417 }
418 break;
george828a471482005-02-06 07:15:53 +0000419
420 // Process messages from posTrackBar
421
422 case WM_HSCROLL:
423 {
424 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
425 Pos *= sliderStepMs;
426
427 switch (LOWORD(wParam)) {
428 case TB_PAGEUP:
429 case TB_PAGEDOWN:
430 case TB_LINEUP:
431 case TB_LINEDOWN:
432 case TB_THUMBTRACK:
433 sliderDraging = true;
434 updatePos(Pos);
435 return 0;
436 case TB_ENDTRACK:
437 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000438 setPaused(isPaused());;
george828a471482005-02-06 07:15:53 +0000439 sliderDraging = false;
440 return 0;
441 default:
442 break;
443 }
444 }
445 break;
george829e6e6cc2005-01-29 13:12:05 +0000446
447 case WM_NOTIFY:
448 switch (((NMHDR*)lParam)->code) {
449 case UDN_DELTAPOS:
450 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000451 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000452 char speedStr[20] = "\0";
453 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
454 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
455 double speed;
456
george824ea27f62005-01-29 15:03:06 +0000457 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000458 if (upDown->iDelta > 0) {
459 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
460 } else {
george824ea27f62005-01-29 15:03:06 +0000461 // It's need to round the UpDown position
462 if ((upDown->iPos * 0.5) != getSpeed()) {
463 upDown->iDelta = 0;
464 lResult = TRUE;
465 }
george829e6e6cc2005-01-29 13:12:05 +0000466 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
467 }
george829e6e6cc2005-01-29 13:12:05 +0000468 sprintf(speedStr, "%.2f", speed);
469 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000470 is->setSpeed(speed);
471 playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000472 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000473 }
george824ea27f62005-01-29 15:03:06 +0000474 }
george829e6e6cc2005-01-29 13:12:05 +0000475 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000476
477 case WM_CLOSE:
478 vlog.debug("WM_CLOSE %x", getMainHandle());
479 PostQuitMessage(0);
480 break;
481 }
482
483 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
484}
485
486LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
487 switch (msg) {
488
489 case WM_PAINT:
490 {
george825beb62a2005-02-09 13:04:32 +0000491 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000492 seekMode = true;
493 return 0;
494 } else {
495 if (seekMode) {
496 seekMode = false;
497 InvalidateRect(getFrameHandle(), 0, true);
498 UpdateWindow(getFrameHandle());
499 return 0;
500 }
501 }
502
503 PAINTSTRUCT ps;
504 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
505 if (!paintDC)
506 throw SystemException("unable to BeginPaint", GetLastError());
507 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
508
509 if (!pr.is_empty()) {
510
511 if (buffer->bitmap) {
512
513 // Get device context
514 BitmapDC bitmapDC(paintDC, buffer->bitmap);
515
516 // Blit the border if required
517 Rect bufpos = bufferToClient(buffer->getRect());
518 if (!pr.enclosed_by(bufpos)) {
519 vlog.debug("draw border");
520 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
521 RECT r;
522 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
523 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
524 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
525 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
526 }
527
528 // Do the blit
529 Point buf_pos = clientToBuffer(pr.tl);
530 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
531 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
532 throw SystemException("unable to BitBlt to window", GetLastError());
533
534 } else {
535 // Blit a load of black
536 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
537 0, 0, 0, BLACKNESS))
538 throw SystemException("unable to BitBlt to blank window", GetLastError());
539 }
540 }
541 EndPaint(getFrameHandle(), &ps);
542 }
543 return 0;
544
545 case WM_VSCROLL:
546 case WM_HSCROLL:
547 {
548 Point delta;
549 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
550
551 switch (LOWORD(wParam)) {
552 case SB_PAGEUP: newpos -= 50; break;
553 case SB_PAGEDOWN: newpos += 50; break;
554 case SB_LINEUP: newpos -= 5; break;
555 case SB_LINEDOWN: newpos += 5; break;
556 case SB_THUMBTRACK:
557 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
558 default: vlog.info("received unknown scroll message");
559 };
560
561 if (msg == WM_HSCROLL)
562 setViewportOffset(Point(newpos, scrolloffset.y));
563 else
564 setViewportOffset(Point(scrolloffset.x, newpos));
565
566 SCROLLINFO si;
567 si.cbSize = sizeof(si);
568 si.fMask = SIF_POS;
569 si.nPos = newpos;
570 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
571 }
572 break;
573 }
574
575 return DefWindowProc(hwnd, msg, wParam, lParam);
576}
577
george82d070c692005-01-19 16:44:04 +0000578void RfbPlayer::createToolBar(HWND parentHwnd) {
579 RECT tRect;
580 InitCommonControls();
581
582 tb.create(ID_TOOLBAR, parentHwnd);
583 tb.addBitmap(4, IDB_TOOLBAR);
584
585 // Create the control buttons
586 tb.addButton(0, ID_PLAY);
587 tb.addButton(1, ID_PAUSE);
588 tb.addButton(2, ID_STOP);
589 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
590 tb.addButton(3, ID_FULLSCREEN);
591 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
592
593 // Create the static control for the time output
594 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
595 tb.getButtonRect(6, &tRect);
596 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
597 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
598 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
599 GetModuleHandle(0), 0);
600 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
601
602 // Create the trackbar control for the time position
603 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
604 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000605 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000606 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
607 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
608 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
609 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000610 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000611 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
612
613 // Create the label with "Speed:" caption
614 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
615 tb.getButtonRect(10, &tRect);
616 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
617 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
618 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
619
620 // Create the edit control and the spin for the speed managing
621 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
622 tb.getButtonRect(11, &tRect);
623 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
624 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
625 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
626 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
627 // It's need to send notify messages to toolbar parent window
628 SetParent(speedEdit, tb.getHandle());
629
630 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
631 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000632 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000633}
634
george82a21d2952005-02-12 11:30:03 +0000635void RfbPlayer::disableTBandMenuItems() {
636 // Disable the menu items
637 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
638 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
640 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
641 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
642 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
643 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
644 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
645 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
646
647 // Disable the toolbar buttons and child controls
648 tb.enableButton(ID_PLAY, false);
649 tb.enableButton(ID_PAUSE, false);
650 tb.enableButton(ID_STOP, false);
651 tb.enableButton(ID_FULLSCREEN, false);
652 EnableWindow(posTrackBar, false);
653 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000654 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000655}
656
george82f5043162005-02-12 11:37:18 +0000657void RfbPlayer::enableTBandMenuItems() {
658 // Enable the menu items
659 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
660 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
661 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
662 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
663 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
664 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
665 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
666 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
667 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
668
669 // Enable the toolbar buttons and child controls
670 tb.enableButton(ID_PLAY, true);
671 tb.enableButton(ID_PAUSE, true);
672 tb.enableButton(ID_STOP, true);
673 tb.enableButton(ID_FULLSCREEN, true);
674 EnableWindow(posTrackBar, true);
675 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000676 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000677}
678
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000679void RfbPlayer::setVisible(bool visible) {
680 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
681 if (visible) {
682 // When the window becomes visible, make it active
683 SetForegroundWindow(getMainHandle());
684 SetActiveWindow(getMainHandle());
685 }
686}
687
688void RfbPlayer::setTitle(const char *title) {
689 char _title[256];
690 strcpy(_title, AppName);
691 strcat(_title, " - ");
692 strcat(_title, title);
693 SetWindowText(getMainHandle(), _title);
694}
695
696void RfbPlayer::setFrameSize(int width, int height) {
697 // Calculate and set required size for main window
698 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000699 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000700 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
701 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
702 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000703 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
704 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
705 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000706 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
707
708 // Enable/disable scrollbars as appropriate
709 calculateScrollBars();
710}
711
712void RfbPlayer::calculateScrollBars() {
713 // Calculate the required size of window
714 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
715 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
716 DWORD old_style;
717 RECT r;
718 SetRect(&r, 0, 0, buffer->width(), buffer->height());
719 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
720 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
721
722 // Work out whether scroll bars are required
723 do {
724 old_style = style;
725
726 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
727 style |= WS_HSCROLL;
728 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
729 }
730 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
731 style |= WS_VSCROLL;
732 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
733 }
734 } while (style != old_style);
735
736 // Tell Windows to update the window style & cached settings
737 if (style != current_style) {
738 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
739 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
740 }
741
742 // Update the scroll settings
743 SCROLLINFO si;
744 if (style & WS_VSCROLL) {
745 si.cbSize = sizeof(si);
746 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
747 si.nMin = 0;
748 si.nMax = buffer->height();
749 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
750 maxscrolloffset.y = max(0, si.nMax-si.nPage);
751 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
752 si.nPos = scrolloffset.y;
753 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
754 }
755 if (style & WS_HSCROLL) {
756 si.cbSize = sizeof(si);
757 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
758 si.nMin = 0;
759 si.nMax = buffer->width();
760 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
761 maxscrolloffset.x = max(0, si.nMax-si.nPage);
762 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
763 si.nPos = scrolloffset.x;
764 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
765 }
766}
767
768bool RfbPlayer::setViewportOffset(const Point& tl) {
769/* ***
770 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
771 max(0, min(maxscrolloffset.y, tl.y)));
772 */
773 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
774 max(0, min(tl.y, buffer->height()-client_size.height())));
775 Point delta = np.translate(scrolloffset.negate());
776 if (!np.equals(scrolloffset)) {
777 scrolloffset = np;
778 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
779 UpdateWindow(getFrameHandle());
780 return true;
781 }
782 return false;
783}
784
785void RfbPlayer::close(const char* reason) {
786 setVisible(false);
787 if (reason) {
788 vlog.info("closing - %s", reason);
789 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
790 }
791 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
792}
793
794void RfbPlayer::blankBuffer() {
795 fillRect(buffer->getRect(), 0);
796}
797
798void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000799 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000800 blankBuffer();
801 newSession(fileName);
802 skipHandshaking();
george825f326fe2005-02-20 08:01:01 +0000803 is->setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000804 if (paused) is->pausePlayback();
805 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000806}
807
808void RfbPlayer::processMsg() {
809 static long update_time = GetTickCount();
810 try {
george828a471482005-02-06 07:15:53 +0000811 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
812 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000813 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000814 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000815 update_time = GetTickCount();
816 }
817 RfbProto::processMsg();
818 } catch (rdr::Exception e) {
819 if (strcmp(e.str(), "[End Of File]") == 0) {
820 rewind();
george8231a36332005-02-06 17:27:34 +0000821 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000822 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000823 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000824 return;
825 }
826 // It's a special exception to perform backward seeking.
827 // We only rewind the stream and seek the offset
828 if (strcmp(e.str(), "[REWIND]") == 0) {
george82b95503e2005-02-21 17:02:34 +0000829 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000830 rewind();
george823104aec2005-02-21 13:20:56 +0000831 setPos(seekOffset);
832 updatePos(seekOffset);
george8223e08562005-01-31 15:16:42 +0000833 } else {
834 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
835 return;
836 }
837 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000838}
839
840void RfbPlayer::serverInit() {
841 RfbProto::serverInit();
842
george82b95503e2005-02-21 17:02:34 +0000843 // Save the image data start time
844 imageDataStartTime = is->getTimeOffset();
845
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000846 // Resize the backing buffer
847 buffer->setSize(cp.width, cp.height);
848
849 // Check on the true colour mode
850 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000851 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000852
853 // Set the session pixel format
george82193d8e42005-02-20 16:47:01 +0000854 switch (colourDepth) {
855 case DEPTH_AUTO:
856 break;
857 case DEPTH8_RGB332:
858 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
859 break;
860 case DEPTH16_RGB655:
861 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
862 break;
863 case DEPTH24_RGB888:
864 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
865 break;
866 default:
867 throw rdr::Exception("This color depth is not supported!");
868 }
869 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000870
871 // If the window is not maximised then resize it
872 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
873 setFrameSize(cp.width, cp.height);
874
875 // Set the window title and show it
876 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000877
george82d4d69e62005-02-05 09:23:18 +0000878 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000879 sessionTimeMs = calculateSessionTime(fileName);
880 sprintf(fullSessionTime, "%.2um:%.2us",
881 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000882 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000883 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
884 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000885 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000886
george82006f2792005-02-05 07:40:47 +0000887 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000888}
889
890void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
891 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
892 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
893/* int i;
894 for (i=0;i<count;i++) {
895 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
896 }
897 // *** change to 0, 256?
898 refreshWindowPalette(first, count);
899 palette_changed = true;
900 InvalidateRect(getFrameHandle(), 0, FALSE);*/
901}
902
903void RfbPlayer::bell() {
904 if (acceptBell)
905 MessageBeep(-1);
906}
907
908void RfbPlayer::serverCutText(const char* str, int len) {
909 if (cutText != NULL)
910 delete [] cutText;
911 cutText = new char[len + 1];
912 memcpy(cutText, str, len);
913 cutText[len] = '\0';
914}
915
916void RfbPlayer::frameBufferUpdateEnd() {
917};
918
919void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
920}
921
922void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
923}
924
925
926void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
927 buffer->fillRect(r, pix);
928 invalidateBufferRect(r);
929}
930
931void RfbPlayer::imageRect(const Rect& r, void* pixels) {
932 buffer->imageRect(r, pixels);
933 invalidateBufferRect(r);
934}
935
936void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
937 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
938 invalidateBufferRect(r);
939}
940
941bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
942 Rect rect = bufferToClient(crect);
943 if (rect.intersect(client_size).is_empty()) return false;
944 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
945 InvalidateRect(getFrameHandle(), &invalid, FALSE);
946 return true;
947}
948
george8257f13522005-02-05 08:48:22 +0000949long RfbPlayer::calculateSessionTime(char *filename) {
950 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000951 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000952 try {
953 while (TRUE) {
954 sessionFile.skip(1024);
955 }
956 } catch (rdr::Exception e) {
957 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000958 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000959 } else {
960 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
961 return 0;
962 }
963 }
964 return 0;
965}
966
george826b87aff2005-02-13 10:48:21 +0000967void RfbPlayer::closeSessionFile() {
968 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000969 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000970 RECT r;
971
972 // Uncheck all toolbar buttons
973 if (tb.getHandle()) {
974 tb.checkButton(ID_PLAY, false);
975 tb.checkButton(ID_PAUSE, false);
976 tb.checkButton(ID_STOP, false);
977 }
978
979 // Stop playback and update the player state
980 disableTBandMenuItems();
981 if (rfbReader) {
982 delete rfbReader->join();
983 rfbReader = 0;
984 delete [] fileName;
985 fileName = 0;
986 }
987 blankBuffer();
988 setTitle("None");
george827009c892005-02-19 12:49:42 +0000989 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +0000990 playbackSpeed = 1.0;
991 SendMessage(speedUpDown, UDM_SETPOS,
992 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
993 sprintf(speedStr, "%.2f", playbackSpeed);
994 SetWindowText(speedEdit, speedStr);
995 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
996
997 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +0000998 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
999 dwStyle &= ~WS_MAXIMIZE;
1000 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1001 }
george822ff7a612005-02-19 17:05:24 +00001002 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1003 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1004 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001005 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001006 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001007 buffer->setSize(32, 32);
1008 calculateScrollBars();
1009
1010 // Update the cached sizing information and repaint the frame window
1011 GetWindowRect(getFrameHandle(), &r);
1012 window_size = Rect(r.left, r.top, r.right, r.bottom);
1013 GetClientRect(getFrameHandle(), &r);
1014 client_size = Rect(r.left, r.top, r.right, r.bottom);
1015 InvalidateRect(getFrameHandle(), 0, TRUE);
1016 UpdateWindow(getFrameHandle());
1017}
1018
george8217e92cb2005-01-31 16:01:02 +00001019void RfbPlayer::openSessionFile(char *_fileName) {
1020 fileName = strDup(_fileName);
1021
1022 // Close the previous reading thread
1023 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001024 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001025 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001026 }
1027 blankBuffer();
1028 newSession(fileName);
1029 setSpeed(playbackSpeed);
1030 rfbReader = new rfbSessionReader(this);
1031 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001032 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001033 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001034}
1035
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001036void RfbPlayer::setPaused(bool paused) {
1037 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001038 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001039 tb.checkButton(ID_PAUSE, true);
1040 tb.checkButton(ID_PLAY, false);
1041 tb.checkButton(ID_STOP, false);
1042 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1043 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001044 } else {
george825beb62a2005-02-09 13:04:32 +00001045 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001046 tb.checkButton(ID_PLAY, true);
1047 tb.checkButton(ID_STOP, false);
1048 tb.checkButton(ID_PAUSE, false);
1049 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1050 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001051 }
1052}
1053
george82006f2792005-02-05 07:40:47 +00001054void RfbPlayer::stopPlayback() {
1055 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001056 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001057 tb.checkButton(ID_STOP, true);
1058 tb.checkButton(ID_PLAY, false);
1059 tb.checkButton(ID_PAUSE, false);
1060 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1061 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001062 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001063}
1064
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001065void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001066 if (speed > 0) {
1067 char speedStr[20] = "\0";
1068 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001069 is->setSpeed(newSpeed);
1070 playbackSpeed = newSpeed;
1071 SendMessage(speedUpDown, UDM_SETPOS,
1072 0, MAKELONG((short)(newSpeed / 0.5), 0));
1073 sprintf(speedStr, "%.2f", newSpeed);
1074 SetWindowText(speedEdit, speedStr);
1075 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001076}
1077
1078double RfbPlayer::getSpeed() {
1079 return is->getSpeed();
1080}
1081
1082void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001083 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001084}
1085
1086long RfbPlayer::getSeekOffset() {
1087 return is->getSeekOffset();
1088}
1089
1090bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001091 if (is) return is->isSeeking();
1092 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001093}
1094
1095bool RfbPlayer::isSeekMode() {
1096 return seekMode;
1097}
1098
1099bool RfbPlayer::isPaused() {
1100 return is->isPaused();
1101}
1102
1103long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001104 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001105}
1106
george828a471482005-02-06 07:15:53 +00001107void RfbPlayer::updatePos(long newPos) {
1108 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001109 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001110 long time = newPos / 1000;
1111 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001112 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001113
1114 // Update the position of slider
1115 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001116 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1117 sliderStepMs / double(newPos);
1118 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1119 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1120 }
george828a471482005-02-06 07:15:53 +00001121 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001122}
1123
1124void RfbPlayer::skipHandshaking() {
1125 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1126 is->skip(skipBytes);
1127 state_ = RFBSTATE_NORMAL;
1128}
1129
1130void programInfo() {
1131 win32::FileVersionInfo inf;
1132 _tprintf(_T("%s - %s, Version %s\n"),
1133 inf.getVerString(_T("ProductName")),
1134 inf.getVerString(_T("FileDescription")),
1135 inf.getVerString(_T("FileVersion")));
1136 printf("%s\n", buildTime);
1137 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1138}
1139
1140void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001141 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001142}
1143
1144double playbackSpeed = 1.0;
1145long initTime = -1;
george82193d8e42005-02-20 16:47:01 +00001146int depth = DEPTH_AUTO;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001147bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001148char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001149bool print_usage = false;
1150bool acceptBell = false;
1151
1152bool processParams(int argc, char* argv[]) {
1153 for (int i = 1; i < argc; i++) {
1154 if ((strcasecmp(argv[i], "-help") == 0) ||
1155 (strcasecmp(argv[i], "--help") == 0) ||
1156 (strcasecmp(argv[i], "/help") == 0) ||
1157 (strcasecmp(argv[i], "-h") == 0) ||
1158 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001159 (strcasecmp(argv[i], "/?") == 0) ||
1160 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001161 print_usage = true;
1162 return true;
1163 }
1164
george82193d8e42005-02-20 16:47:01 +00001165 if ((strcasecmp(argv[i], "-depth") == 0) ||
1166 (strcasecmp(argv[i], "/depth") == 0) && (i < argc-1)) {
1167 depth = atoi(argv[++i]);
1168 if (depth < 0) {
1169 return false;
1170 }
1171 continue;
1172 }
1173
1174
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001175 if ((strcasecmp(argv[i], "-speed") == 0) ||
1176 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1177 playbackSpeed = atof(argv[++i]);
1178 if (playbackSpeed <= 0) {
1179 return false;
1180 }
1181 continue;
1182 }
1183
1184 if ((strcasecmp(argv[i], "-pos") == 0) ||
1185 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1186 initTime = atol(argv[++i]);
1187 if (initTime <= 0)
1188 return false;
1189 continue;
1190 }
1191
1192 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1193 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001194 autoplay = true;
1195 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001196 }
1197
1198 if ((strcasecmp(argv[i], "-bell") == 0) ||
1199 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001200 acceptBell = true;
1201 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001202 }
1203
1204 if (i != argc - 1)
1205 return false;
1206 }
1207
1208 fileName = strDup(argv[argc-1]);
1209 return true;
1210}
1211
1212//
1213// -=- WinMain
1214//
1215
1216int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1217
1218 // - Process the command-line
1219
1220 int argc = __argc;
1221 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001222 if ((argc > 1) && (!processParams(argc, argv))) {
1223 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1224 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001225 }
george82e6883de2005-02-08 14:42:12 +00001226
1227 if (print_usage) {
1228 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001229 return 0;
george8267cbcd02005-01-16 15:39:56 +00001230 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001231
george82e6883de2005-02-08 14:42:12 +00001232 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001233 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001234 try {
george82193d8e42005-02-20 16:47:01 +00001235 player = new RfbPlayer(fileName, depth, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001236 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001237 } catch (rdr::Exception e) {
1238 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1239 delete player;
1240 return 0;
1241 }
1242
1243 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001244 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001245 MSG msg;
1246 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001247 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1248 TranslateMessage(&msg);
1249 DispatchMessage(&msg);
1250 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001251 }
1252
george82e6883de2005-02-08 14:42:12 +00001253 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001254 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001255 if (player) delete player;
1256 } catch (rdr::Exception e) {
1257 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1258 }
1259
1260 return 0;
1261};