blob: 72614f176979aacb459e285da0e19b3bd40aee2a [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),
229 loopPlayback(false) {
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) {
george823104aec2005-02-21 13:20:56 +0000827 long seekOffset = getSeekOffset();
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
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000841 // Resize the backing buffer
842 buffer->setSize(cp.width, cp.height);
843
844 // Check on the true colour mode
845 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000846 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000847
848 // Set the session pixel format
george82193d8e42005-02-20 16:47:01 +0000849 switch (colourDepth) {
850 case DEPTH_AUTO:
851 break;
852 case DEPTH8_RGB332:
853 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
854 break;
855 case DEPTH16_RGB655:
856 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
857 break;
858 case DEPTH24_RGB888:
859 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
860 break;
861 default:
862 throw rdr::Exception("This color depth is not supported!");
863 }
864 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000865
866 // If the window is not maximised then resize it
867 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
868 setFrameSize(cp.width, cp.height);
869
870 // Set the window title and show it
871 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000872
george82d4d69e62005-02-05 09:23:18 +0000873 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000874 sessionTimeMs = calculateSessionTime(fileName);
875 sprintf(fullSessionTime, "%.2um:%.2us",
876 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000877 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000878 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
879 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000880 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000881
george82006f2792005-02-05 07:40:47 +0000882 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000883}
884
885void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
886 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
887 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
888/* int i;
889 for (i=0;i<count;i++) {
890 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
891 }
892 // *** change to 0, 256?
893 refreshWindowPalette(first, count);
894 palette_changed = true;
895 InvalidateRect(getFrameHandle(), 0, FALSE);*/
896}
897
898void RfbPlayer::bell() {
899 if (acceptBell)
900 MessageBeep(-1);
901}
902
903void RfbPlayer::serverCutText(const char* str, int len) {
904 if (cutText != NULL)
905 delete [] cutText;
906 cutText = new char[len + 1];
907 memcpy(cutText, str, len);
908 cutText[len] = '\0';
909}
910
911void RfbPlayer::frameBufferUpdateEnd() {
912};
913
914void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
915}
916
917void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
918}
919
920
921void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
922 buffer->fillRect(r, pix);
923 invalidateBufferRect(r);
924}
925
926void RfbPlayer::imageRect(const Rect& r, void* pixels) {
927 buffer->imageRect(r, pixels);
928 invalidateBufferRect(r);
929}
930
931void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
932 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
933 invalidateBufferRect(r);
934}
935
936bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
937 Rect rect = bufferToClient(crect);
938 if (rect.intersect(client_size).is_empty()) return false;
939 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
940 InvalidateRect(getFrameHandle(), &invalid, FALSE);
941 return true;
942}
943
george8257f13522005-02-05 08:48:22 +0000944long RfbPlayer::calculateSessionTime(char *filename) {
945 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000946 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000947 try {
948 while (TRUE) {
949 sessionFile.skip(1024);
950 }
951 } catch (rdr::Exception e) {
952 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000953 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000954 } else {
955 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
956 return 0;
957 }
958 }
959 return 0;
960}
961
george826b87aff2005-02-13 10:48:21 +0000962void RfbPlayer::closeSessionFile() {
963 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000964 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000965 RECT r;
966
967 // Uncheck all toolbar buttons
968 if (tb.getHandle()) {
969 tb.checkButton(ID_PLAY, false);
970 tb.checkButton(ID_PAUSE, false);
971 tb.checkButton(ID_STOP, false);
972 }
973
974 // Stop playback and update the player state
975 disableTBandMenuItems();
976 if (rfbReader) {
977 delete rfbReader->join();
978 rfbReader = 0;
979 delete [] fileName;
980 fileName = 0;
981 }
982 blankBuffer();
983 setTitle("None");
george827009c892005-02-19 12:49:42 +0000984 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +0000985 playbackSpeed = 1.0;
986 SendMessage(speedUpDown, UDM_SETPOS,
987 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
988 sprintf(speedStr, "%.2f", playbackSpeed);
989 SetWindowText(speedEdit, speedStr);
990 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
991
992 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +0000993 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
994 dwStyle &= ~WS_MAXIMIZE;
995 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
996 }
george822ff7a612005-02-19 17:05:24 +0000997 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
998 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
999 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001000 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001001 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001002 buffer->setSize(32, 32);
1003 calculateScrollBars();
1004
1005 // Update the cached sizing information and repaint the frame window
1006 GetWindowRect(getFrameHandle(), &r);
1007 window_size = Rect(r.left, r.top, r.right, r.bottom);
1008 GetClientRect(getFrameHandle(), &r);
1009 client_size = Rect(r.left, r.top, r.right, r.bottom);
1010 InvalidateRect(getFrameHandle(), 0, TRUE);
1011 UpdateWindow(getFrameHandle());
1012}
1013
george8217e92cb2005-01-31 16:01:02 +00001014void RfbPlayer::openSessionFile(char *_fileName) {
1015 fileName = strDup(_fileName);
1016
1017 // Close the previous reading thread
1018 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001019 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001020 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001021 }
1022 blankBuffer();
1023 newSession(fileName);
1024 setSpeed(playbackSpeed);
1025 rfbReader = new rfbSessionReader(this);
1026 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001027 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001028 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001029}
1030
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001031void RfbPlayer::setPaused(bool paused) {
1032 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001033 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001034 tb.checkButton(ID_PAUSE, true);
1035 tb.checkButton(ID_PLAY, false);
1036 tb.checkButton(ID_STOP, false);
1037 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1038 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001039 } else {
george825beb62a2005-02-09 13:04:32 +00001040 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001041 tb.checkButton(ID_PLAY, true);
1042 tb.checkButton(ID_STOP, false);
1043 tb.checkButton(ID_PAUSE, false);
1044 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1045 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001046 }
1047}
1048
george82006f2792005-02-05 07:40:47 +00001049void RfbPlayer::stopPlayback() {
1050 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001051 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001052 tb.checkButton(ID_STOP, true);
1053 tb.checkButton(ID_PLAY, false);
1054 tb.checkButton(ID_PAUSE, false);
1055 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1056 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001057 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001058}
1059
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001060void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001061 if (speed > 0) {
1062 char speedStr[20] = "\0";
1063 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001064 is->setSpeed(newSpeed);
1065 playbackSpeed = newSpeed;
1066 SendMessage(speedUpDown, UDM_SETPOS,
1067 0, MAKELONG((short)(newSpeed / 0.5), 0));
1068 sprintf(speedStr, "%.2f", newSpeed);
1069 SetWindowText(speedEdit, speedStr);
1070 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001071}
1072
1073double RfbPlayer::getSpeed() {
1074 return is->getSpeed();
1075}
1076
1077void RfbPlayer::setPos(long pos) {
george823104aec2005-02-21 13:20:56 +00001078 is->setTimeOffset(max(pos, 0));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001079}
1080
1081long RfbPlayer::getSeekOffset() {
1082 return is->getSeekOffset();
1083}
1084
1085bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001086 if (is) return is->isSeeking();
1087 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001088}
1089
1090bool RfbPlayer::isSeekMode() {
1091 return seekMode;
1092}
1093
1094bool RfbPlayer::isPaused() {
1095 return is->isPaused();
1096}
1097
1098long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001099 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001100}
1101
george828a471482005-02-06 07:15:53 +00001102void RfbPlayer::updatePos(long newPos) {
1103 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001104 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001105 long time = newPos / 1000;
1106 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001107 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001108
1109 // Update the position of slider
1110 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001111 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1112 sliderStepMs / double(newPos);
1113 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1114 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1115 }
george828a471482005-02-06 07:15:53 +00001116 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001117}
1118
1119void RfbPlayer::skipHandshaking() {
1120 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1121 is->skip(skipBytes);
1122 state_ = RFBSTATE_NORMAL;
1123}
1124
1125void programInfo() {
1126 win32::FileVersionInfo inf;
1127 _tprintf(_T("%s - %s, Version %s\n"),
1128 inf.getVerString(_T("ProductName")),
1129 inf.getVerString(_T("FileDescription")),
1130 inf.getVerString(_T("FileVersion")));
1131 printf("%s\n", buildTime);
1132 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1133}
1134
1135void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001136 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137}
1138
1139double playbackSpeed = 1.0;
1140long initTime = -1;
george82193d8e42005-02-20 16:47:01 +00001141int depth = DEPTH_AUTO;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001142bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001143char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001144bool print_usage = false;
1145bool acceptBell = false;
1146
1147bool processParams(int argc, char* argv[]) {
1148 for (int i = 1; i < argc; i++) {
1149 if ((strcasecmp(argv[i], "-help") == 0) ||
1150 (strcasecmp(argv[i], "--help") == 0) ||
1151 (strcasecmp(argv[i], "/help") == 0) ||
1152 (strcasecmp(argv[i], "-h") == 0) ||
1153 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001154 (strcasecmp(argv[i], "/?") == 0) ||
1155 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001156 print_usage = true;
1157 return true;
1158 }
1159
george82193d8e42005-02-20 16:47:01 +00001160 if ((strcasecmp(argv[i], "-depth") == 0) ||
1161 (strcasecmp(argv[i], "/depth") == 0) && (i < argc-1)) {
1162 depth = atoi(argv[++i]);
1163 if (depth < 0) {
1164 return false;
1165 }
1166 continue;
1167 }
1168
1169
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001170 if ((strcasecmp(argv[i], "-speed") == 0) ||
1171 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1172 playbackSpeed = atof(argv[++i]);
1173 if (playbackSpeed <= 0) {
1174 return false;
1175 }
1176 continue;
1177 }
1178
1179 if ((strcasecmp(argv[i], "-pos") == 0) ||
1180 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1181 initTime = atol(argv[++i]);
1182 if (initTime <= 0)
1183 return false;
1184 continue;
1185 }
1186
1187 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1188 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001189 autoplay = true;
1190 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001191 }
1192
1193 if ((strcasecmp(argv[i], "-bell") == 0) ||
1194 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001195 acceptBell = true;
1196 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001197 }
1198
1199 if (i != argc - 1)
1200 return false;
1201 }
1202
1203 fileName = strDup(argv[argc-1]);
1204 return true;
1205}
1206
1207//
1208// -=- WinMain
1209//
1210
1211int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1212
1213 // - Process the command-line
1214
1215 int argc = __argc;
1216 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001217 if ((argc > 1) && (!processParams(argc, argv))) {
1218 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1219 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001220 }
george82e6883de2005-02-08 14:42:12 +00001221
1222 if (print_usage) {
1223 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001224 return 0;
george8267cbcd02005-01-16 15:39:56 +00001225 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001226
george82e6883de2005-02-08 14:42:12 +00001227 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001228 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001229 try {
george82193d8e42005-02-20 16:47:01 +00001230 player = new RfbPlayer(fileName, depth, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001231 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001232 } catch (rdr::Exception e) {
1233 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1234 delete player;
1235 return 0;
1236 }
1237
1238 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001239 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001240 MSG msg;
1241 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001242 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1243 TranslateMessage(&msg);
1244 DispatchMessage(&msg);
1245 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001246 }
1247
george82e6883de2005-02-08 14:42:12 +00001248 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001249 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001250 if (player) delete player;
1251 } catch (rdr::Exception e) {
1252 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1253 }
1254
1255 return 0;
1256};