blob: 7cdef8152957f14b9f05f96d27303f6906239c1d [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"
51 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
52 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
53 " -pos <ms> \t- Sets initial time position in the session file,\n"
54 " \t in milliseconds. Default: 0.\n"
55 " -autoplay \t- Runs the player in the playback mode.\n"
56 " -bell \t- Accepts the bell.\n";
57
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000058// -=- RfbPlayer's defines
59
60#define strcasecmp _stricmp
george824ea27f62005-01-29 15:03:06 +000061#define MAX_SPEED 10
george82d4d69e62005-02-05 09:23:18 +000062#define MAX_POS_TRACKBAR_RANGE 50
george8268d25142005-02-13 09:33:22 +000063#define DEFAULT_PLAYER_WIDTH 640
64#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000065
george82d070c692005-01-19 16:44:04 +000066#define ID_TOOLBAR 500
67#define ID_PLAY 510
68#define ID_PAUSE 520
69#define ID_TIME_STATIC 530
70#define ID_SPEED_STATIC 540
71#define ID_SPEED_EDIT 550
72#define ID_POS_TRACKBAR 560
73#define ID_SPEED_UPDOWN 570
74
75
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000076//
77// -=- RfbPlayerClass
78
79//
80// Window class used as the basis for RfbPlayer instance
81//
82
83class RfbPlayerClass {
84public:
85 RfbPlayerClass();
86 ~RfbPlayerClass();
87 ATOM classAtom;
88 HINSTANCE instance;
89};
90
91LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
92 LRESULT result;
93
94 if (msg == WM_CREATE)
95 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
96 else if (msg == WM_DESTROY) {
97 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000098 SetWindowLong(hwnd, GWL_USERDATA, 0);
99 }
100 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
101 if (!_this) {
102 vlog.info("null _this in %x, message %u", hwnd, msg);
103 return DefWindowProc(hwnd, msg, wParam, lParam);
104 }
105
106 try {
107 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
108 } catch (rdr::Exception& e) {
109 vlog.error("untrapped: %s", e.str());
110 }
111
112 return result;
113};
114
115RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
116 WNDCLASS wndClass;
117 wndClass.style = 0;
118 wndClass.lpfnWndProc = RfbPlayerProc;
119 wndClass.cbClsExtra = 0;
120 wndClass.cbWndExtra = 0;
121 wndClass.hInstance = instance = GetModuleHandle(0);
122 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000123 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000124 if (!wndClass.hIcon)
125 printf("unable to load icon:%ld", GetLastError());
126 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
127 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000128 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000129 wndClass.lpszClassName = _T("RfbPlayerClass");
130 classAtom = RegisterClass(&wndClass);
131 if (!classAtom) {
132 throw rdr::SystemException("unable to register RfbPlayer window class",
133 GetLastError());
134 }
135}
136
137RfbPlayerClass::~RfbPlayerClass() {
138 if (classAtom) {
139 UnregisterClass((const TCHAR*)classAtom, instance);
140 }
141}
142
143RfbPlayerClass baseClass;
144
145//
146// -=- RfbFrameClass
147
148//
149// Window class used to displaying the rfb data
150//
151
152class RfbFrameClass {
153public:
154 RfbFrameClass();
155 ~RfbFrameClass();
156 ATOM classAtom;
157 HINSTANCE instance;
158};
159
160LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
161 LRESULT result;
162
163 if (msg == WM_CREATE)
164 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
165 else if (msg == WM_DESTROY)
166 SetWindowLong(hwnd, GWL_USERDATA, 0);
167 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
168 if (!_this) {
169 vlog.info("null _this in %x, message %u", hwnd, msg);
170 return DefWindowProc(hwnd, msg, wParam, lParam);
171 }
172
173 try {
174 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
175 } catch (rdr::Exception& e) {
176 vlog.error("untrapped: %s", e.str());
177 }
178
179 return result;
180}
181
182RfbFrameClass::RfbFrameClass() : classAtom(0) {
183 WNDCLASS wndClass;
184 wndClass.style = 0;
185 wndClass.lpfnWndProc = FrameProc;
186 wndClass.cbClsExtra = 0;
187 wndClass.cbWndExtra = 0;
188 wndClass.hInstance = instance = GetModuleHandle(0);
189 wndClass.hIcon = 0;
190 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
191 wndClass.hbrBackground = 0;
192 wndClass.lpszMenuName = 0;
193 wndClass.lpszClassName = _T("RfbPlayerClass1");
194 classAtom = RegisterClass(&wndClass);
195 if (!classAtom) {
196 throw rdr::SystemException("unable to register RfbPlayer window class",
197 GetLastError());
198 }
199}
200
201RfbFrameClass::~RfbFrameClass() {
202 if (classAtom) {
203 UnregisterClass((const TCHAR*)classAtom, instance);
204 }
205}
206
207RfbFrameClass frameClass;
208
209//
210// -=- RfbPlayer instance implementation
211//
212
213RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000214 bool _autoplay = false, bool _acceptBell = false)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000215: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
george82e6883de2005-02-08 14:42:12 +0000216 autoplay(_autoplay), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000217 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000218 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000219 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000220 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000221
george82e6883de2005-02-08 14:42:12 +0000222 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000223
george823c8fbbf2005-01-24 11:09:08 +0000224 // Reset the full session time
225 strcpy(fullSessionTime, "00m:00s");
226
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000227 // Create the main window
228 const TCHAR* name = _T("RfbPlayer");
229 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8268d25142005-02-13 09:33:22 +0000230 0, 0, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000231 if (!mainHwnd) {
232 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
233 }
234 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
235
236 // Create the backing buffer
237 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000238 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000239
george8217e92cb2005-01-31 16:01:02 +0000240 // Open the session file
241 if (fileName) {
242 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000243 if (initTime > 0) setPos(initTime);
244 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000245 } else {
246 disableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000247 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000248}
249
250RfbPlayer::~RfbPlayer() {
251 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000252 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000253 delete rfbReader->join();
254 rfbReader = 0;
255 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000256 if (mainHwnd) {
257 setVisible(false);
258 DestroyWindow(mainHwnd);
259 mainHwnd = 0;
260 }
george825beb62a2005-02-09 13:04:32 +0000261 if (buffer) delete buffer;
262 if (cutText) delete [] cutText;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000263 vlog.debug("~RfbPlayer done");
264}
265
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000266LRESULT
267RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
268 switch (msg) {
269
270 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000271
272 case WM_CREATE:
273 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000274 // Create the frame window
275 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
276 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
277 hwnd, 0, frameClass.instance, this);
278
george82d070c692005-01-19 16:44:04 +0000279 createToolBar(hwnd);
280
george82006f2792005-02-05 07:40:47 +0000281 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000282
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000283 return 0;
284 }
285
george827214b822004-12-12 07:02:51 +0000286 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000287
288 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000289 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000290 case ID_OPENFILE:
291 {
292 char curDir[_MAX_DIR];
293 static char filename[_MAX_PATH];
294 OPENFILENAME ofn;
295 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
296 GetCurrentDirectory(sizeof(curDir), curDir);
297
298 ofn.lStructSize = sizeof(OPENFILENAME);
299 ofn.hwndOwner = getMainHandle();
300 ofn.lpstrFile = filename;
301 ofn.nMaxFile = sizeof(filename);
302 ofn.lpstrInitialDir = curDir;
303 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
304 "All files (*.*)\0*.*\0";
305 ofn.lpstrDefExt = "rfb";
306 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
307 if (GetOpenFileName(&ofn))
308 openSessionFile(filename);
309 }
310 break;
george8271ca1772005-02-13 10:50:46 +0000311 case ID_CLOSEFILE:
312 closeSessionFile();
313 break;
george825c13c662005-01-27 14:48:23 +0000314 case ID_PLAY:
315 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000316 break;
317 case ID_PAUSE:
318 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000319 break;
320 case ID_STOP:
321 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000322 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000323 }
george825c13c662005-01-27 14:48:23 +0000324 break;
325 case ID_PLAYPAUSE:
326 if (isPaused()) {
327 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000328 } else {
329 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000330 }
george825c13c662005-01-27 14:48:23 +0000331 break;
george827549df42005-02-08 16:31:02 +0000332 case ID_GOTO:
333 {
334 GotoPosDialog gotoPosDlg;
335 if (gotoPosDlg.showDialog()) {
336 setPos(gotoPosDlg.getPos());
337 updatePos(getTimeOffset());
338 }
339 }
340 break;
george825c13c662005-01-27 14:48:23 +0000341 case ID_FULLSCREEN:
342 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
343 break;
george8231a36332005-02-06 17:27:34 +0000344 case ID_LOOP:
345 loopPlayback = !loopPlayback;
346 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
347 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
348 break;
george824ea27f62005-01-29 15:03:06 +0000349 case ID_RETURN:
350 // Update the speed if return pressed in speedEdit
351 if (speedEdit == GetFocus()) {
352 char speedStr[20], *stopStr;
353 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
354 double speed = strtod(speedStr, &stopStr);
355 if (speed > 0) {
356 speed = min(MAX_SPEED, speed);
357 // Update speedUpDown position
358 SendMessage(speedUpDown, UDM_SETPOS,
359 0, MAKELONG((short)(speed / 0.5), 0));
360 } else {
361 speed = getSpeed();
362 }
363 setSpeed(speed);
364 sprintf(speedStr, "%.2f", speed);
365 SetWindowText(speedEdit, speedStr);
366 }
367 break;
george8201aa6732005-02-06 17:13:03 +0000368 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000369 PostQuitMessage(0);
370 break;
george82ef5f7262005-02-08 15:09:26 +0000371 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000372 MessageBox(getMainHandle(),
373 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
374 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000375 }
376 break;
377
378 // Update frame's window size and add scrollbars if required
379
380 case WM_SIZE:
381 {
george82d070c692005-01-19 16:44:04 +0000382
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000383 Point old_offset = bufferToClient(Point(0, 0));
384
385 // Update the cached sizing information
386 RECT r;
387 GetClientRect(getMainHandle(), &r);
388 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
389 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
390
391 GetWindowRect(getFrameHandle(), &r);
392 window_size = Rect(r.left, r.top, r.right, r.bottom);
393 GetClientRect(getFrameHandle(), &r);
394 client_size = Rect(r.left, r.top, r.right, r.bottom);
395
396 // Determine whether scrollbars are required
397 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000398
399 // Resize the ToolBar
400 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000401
402 // Redraw if required
403 if (!old_offset.equals(bufferToClient(Point(0, 0))))
404 InvalidateRect(getFrameHandle(), 0, TRUE);
405 }
406 break;
george828a471482005-02-06 07:15:53 +0000407
408 // Process messages from posTrackBar
409
410 case WM_HSCROLL:
411 {
412 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
413 Pos *= sliderStepMs;
414
415 switch (LOWORD(wParam)) {
416 case TB_PAGEUP:
417 case TB_PAGEDOWN:
418 case TB_LINEUP:
419 case TB_LINEDOWN:
420 case TB_THUMBTRACK:
421 sliderDraging = true;
422 updatePos(Pos);
423 return 0;
424 case TB_ENDTRACK:
425 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000426 sliderDraging = false;
427 return 0;
428 default:
429 break;
430 }
431 }
432 break;
george829e6e6cc2005-01-29 13:12:05 +0000433
434 case WM_NOTIFY:
435 switch (((NMHDR*)lParam)->code) {
436 case UDN_DELTAPOS:
437 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000438 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000439 char speedStr[20] = "\0";
440 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
441 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
442 double speed;
443
george824ea27f62005-01-29 15:03:06 +0000444 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000445 if (upDown->iDelta > 0) {
446 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
447 } else {
george824ea27f62005-01-29 15:03:06 +0000448 // It's need to round the UpDown position
449 if ((upDown->iPos * 0.5) != getSpeed()) {
450 upDown->iDelta = 0;
451 lResult = TRUE;
452 }
george829e6e6cc2005-01-29 13:12:05 +0000453 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
454 }
455 _gcvt(speed, 5, speedStr);
456 sprintf(speedStr, "%.2f", speed);
457 SetWindowText(speedEdit, speedStr);
458 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000459 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000460 }
george824ea27f62005-01-29 15:03:06 +0000461 }
george829e6e6cc2005-01-29 13:12:05 +0000462 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000463
464 case WM_CLOSE:
465 vlog.debug("WM_CLOSE %x", getMainHandle());
466 PostQuitMessage(0);
467 break;
468 }
469
470 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
471}
472
473LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
474 switch (msg) {
475
476 case WM_PAINT:
477 {
george825beb62a2005-02-09 13:04:32 +0000478 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000479 seekMode = true;
480 return 0;
481 } else {
482 if (seekMode) {
483 seekMode = false;
484 InvalidateRect(getFrameHandle(), 0, true);
485 UpdateWindow(getFrameHandle());
486 return 0;
487 }
488 }
489
490 PAINTSTRUCT ps;
491 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
492 if (!paintDC)
493 throw SystemException("unable to BeginPaint", GetLastError());
494 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
495
496 if (!pr.is_empty()) {
497
498 if (buffer->bitmap) {
499
500 // Get device context
501 BitmapDC bitmapDC(paintDC, buffer->bitmap);
502
503 // Blit the border if required
504 Rect bufpos = bufferToClient(buffer->getRect());
505 if (!pr.enclosed_by(bufpos)) {
506 vlog.debug("draw border");
507 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
508 RECT r;
509 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
510 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
511 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
512 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
513 }
514
515 // Do the blit
516 Point buf_pos = clientToBuffer(pr.tl);
517 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
518 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
519 throw SystemException("unable to BitBlt to window", GetLastError());
520
521 } else {
522 // Blit a load of black
523 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
524 0, 0, 0, BLACKNESS))
525 throw SystemException("unable to BitBlt to blank window", GetLastError());
526 }
527 }
528 EndPaint(getFrameHandle(), &ps);
529 }
530 return 0;
531
532 case WM_VSCROLL:
533 case WM_HSCROLL:
534 {
535 Point delta;
536 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
537
538 switch (LOWORD(wParam)) {
539 case SB_PAGEUP: newpos -= 50; break;
540 case SB_PAGEDOWN: newpos += 50; break;
541 case SB_LINEUP: newpos -= 5; break;
542 case SB_LINEDOWN: newpos += 5; break;
543 case SB_THUMBTRACK:
544 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
545 default: vlog.info("received unknown scroll message");
546 };
547
548 if (msg == WM_HSCROLL)
549 setViewportOffset(Point(newpos, scrolloffset.y));
550 else
551 setViewportOffset(Point(scrolloffset.x, newpos));
552
553 SCROLLINFO si;
554 si.cbSize = sizeof(si);
555 si.fMask = SIF_POS;
556 si.nPos = newpos;
557 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
558 }
559 break;
560 }
561
562 return DefWindowProc(hwnd, msg, wParam, lParam);
563}
564
565void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000566 bool _autoplay = false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000567 autoplay = _autoplay;
568 playbackSpeed = _playbackSpeed;
569 initTime = _initTime;
570}
571
572void RfbPlayer::applyOptions() {
573 if (initTime >= 0)
574 setPos(initTime);
575 setSpeed(playbackSpeed);
576 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000577}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000578
george82d070c692005-01-19 16:44:04 +0000579void RfbPlayer::createToolBar(HWND parentHwnd) {
580 RECT tRect;
581 InitCommonControls();
582
583 tb.create(ID_TOOLBAR, parentHwnd);
584 tb.addBitmap(4, IDB_TOOLBAR);
585
586 // Create the control buttons
587 tb.addButton(0, ID_PLAY);
588 tb.addButton(1, ID_PAUSE);
589 tb.addButton(2, ID_STOP);
590 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
591 tb.addButton(3, ID_FULLSCREEN);
592 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
593
594 // Create the static control for the time output
595 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
596 tb.getButtonRect(6, &tRect);
597 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
598 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
599 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
600 GetModuleHandle(0), 0);
601 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
602
603 // Create the trackbar control for the time position
604 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
605 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000606 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000607 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
608 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
609 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
610 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000611 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000612 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
613
614 // Create the label with "Speed:" caption
615 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
616 tb.getButtonRect(10, &tRect);
617 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
618 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
619 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
620
621 // Create the edit control and the spin for the speed managing
622 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
623 tb.getButtonRect(11, &tRect);
624 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
625 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
626 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
627 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
628 // It's need to send notify messages to toolbar parent window
629 SetParent(speedEdit, tb.getHandle());
630
631 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
632 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000633 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000634}
635
george82a21d2952005-02-12 11:30:03 +0000636void RfbPlayer::disableTBandMenuItems() {
637 // Disable the menu items
638 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
640 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
641 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
642 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
643 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
644 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
645 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
646 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
647
648 // Disable the toolbar buttons and child controls
649 tb.enableButton(ID_PLAY, false);
650 tb.enableButton(ID_PAUSE, false);
651 tb.enableButton(ID_STOP, false);
652 tb.enableButton(ID_FULLSCREEN, false);
653 EnableWindow(posTrackBar, false);
654 EnableWindow(speedEdit, false);
655}
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);
676}
677
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000678void RfbPlayer::setVisible(bool visible) {
679 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
680 if (visible) {
681 // When the window becomes visible, make it active
682 SetForegroundWindow(getMainHandle());
683 SetActiveWindow(getMainHandle());
684 }
685}
686
687void RfbPlayer::setTitle(const char *title) {
688 char _title[256];
689 strcpy(_title, AppName);
690 strcat(_title, " - ");
691 strcat(_title, title);
692 SetWindowText(getMainHandle(), _title);
693}
694
695void RfbPlayer::setFrameSize(int width, int height) {
696 // Calculate and set required size for main window
697 RECT r = {0, 0, width, height};
698 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
699 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
700 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
701 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
702 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
703 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
704
705 // Enable/disable scrollbars as appropriate
706 calculateScrollBars();
707}
708
709void RfbPlayer::calculateScrollBars() {
710 // Calculate the required size of window
711 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
712 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
713 DWORD old_style;
714 RECT r;
715 SetRect(&r, 0, 0, buffer->width(), buffer->height());
716 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
717 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
718
719 // Work out whether scroll bars are required
720 do {
721 old_style = style;
722
723 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
724 style |= WS_HSCROLL;
725 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
726 }
727 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
728 style |= WS_VSCROLL;
729 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
730 }
731 } while (style != old_style);
732
733 // Tell Windows to update the window style & cached settings
734 if (style != current_style) {
735 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
736 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
737 }
738
739 // Update the scroll settings
740 SCROLLINFO si;
741 if (style & WS_VSCROLL) {
742 si.cbSize = sizeof(si);
743 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
744 si.nMin = 0;
745 si.nMax = buffer->height();
746 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
747 maxscrolloffset.y = max(0, si.nMax-si.nPage);
748 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
749 si.nPos = scrolloffset.y;
750 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
751 }
752 if (style & WS_HSCROLL) {
753 si.cbSize = sizeof(si);
754 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
755 si.nMin = 0;
756 si.nMax = buffer->width();
757 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
758 maxscrolloffset.x = max(0, si.nMax-si.nPage);
759 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
760 si.nPos = scrolloffset.x;
761 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
762 }
763}
764
765bool RfbPlayer::setViewportOffset(const Point& tl) {
766/* ***
767 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
768 max(0, min(maxscrolloffset.y, tl.y)));
769 */
770 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
771 max(0, min(tl.y, buffer->height()-client_size.height())));
772 Point delta = np.translate(scrolloffset.negate());
773 if (!np.equals(scrolloffset)) {
774 scrolloffset = np;
775 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
776 UpdateWindow(getFrameHandle());
777 return true;
778 }
779 return false;
780}
781
782void RfbPlayer::close(const char* reason) {
783 setVisible(false);
784 if (reason) {
785 vlog.info("closing - %s", reason);
786 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
787 }
788 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
789}
790
791void RfbPlayer::blankBuffer() {
792 fillRect(buffer->getRect(), 0);
793}
794
795void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000796 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000797 blankBuffer();
798 newSession(fileName);
799 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000800 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000801 if (paused) is->pausePlayback();
802 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000803}
804
805void RfbPlayer::processMsg() {
806 static long update_time = GetTickCount();
807 try {
george828a471482005-02-06 07:15:53 +0000808 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
809 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000810 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000811 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000812 update_time = GetTickCount();
813 }
814 RfbProto::processMsg();
815 } catch (rdr::Exception e) {
816 if (strcmp(e.str(), "[End Of File]") == 0) {
817 rewind();
george8231a36332005-02-06 17:27:34 +0000818 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000819 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000820 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000821 return;
822 }
823 // It's a special exception to perform backward seeking.
824 // We only rewind the stream and seek the offset
825 if (strcmp(e.str(), "[REWIND]") == 0) {
826 long initTime = getSeekOffset();
827 rewind();
828 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000829 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000830 } else {
831 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
832 return;
833 }
834 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000835}
836
837void RfbPlayer::serverInit() {
838 RfbProto::serverInit();
839
840 // Save the server init time for using in setPos()
841 serverInitTime = getTimeOffset() / getSpeed();
842
843 // Resize the backing buffer
844 buffer->setSize(cp.width, cp.height);
845
846 // Check on the true colour mode
847 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000848 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000849
850 // Set the session pixel format
851 buffer->setPF(cp.pf());
852
853 // If the window is not maximised then resize it
854 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
855 setFrameSize(cp.width, cp.height);
856
857 // Set the window title and show it
858 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000859
george82d4d69e62005-02-05 09:23:18 +0000860 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000861 sessionTimeMs = calculateSessionTime(fileName);
862 sprintf(fullSessionTime, "%.2um:%.2us",
863 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000864 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000865 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
866 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000867 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000868
george82006f2792005-02-05 07:40:47 +0000869 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000870}
871
872void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
873 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
874 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
875/* int i;
876 for (i=0;i<count;i++) {
877 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
878 }
879 // *** change to 0, 256?
880 refreshWindowPalette(first, count);
881 palette_changed = true;
882 InvalidateRect(getFrameHandle(), 0, FALSE);*/
883}
884
885void RfbPlayer::bell() {
886 if (acceptBell)
887 MessageBeep(-1);
888}
889
890void RfbPlayer::serverCutText(const char* str, int len) {
891 if (cutText != NULL)
892 delete [] cutText;
893 cutText = new char[len + 1];
894 memcpy(cutText, str, len);
895 cutText[len] = '\0';
896}
897
898void RfbPlayer::frameBufferUpdateEnd() {
899};
900
901void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
902}
903
904void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
905}
906
907
908void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
909 buffer->fillRect(r, pix);
910 invalidateBufferRect(r);
911}
912
913void RfbPlayer::imageRect(const Rect& r, void* pixels) {
914 buffer->imageRect(r, pixels);
915 invalidateBufferRect(r);
916}
917
918void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
919 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
920 invalidateBufferRect(r);
921}
922
923bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
924 Rect rect = bufferToClient(crect);
925 if (rect.intersect(client_size).is_empty()) return false;
926 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
927 InvalidateRect(getFrameHandle(), &invalid, FALSE);
928 return true;
929}
930
george8257f13522005-02-05 08:48:22 +0000931long RfbPlayer::calculateSessionTime(char *filename) {
932 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000933 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000934 try {
935 while (TRUE) {
936 sessionFile.skip(1024);
937 }
938 } catch (rdr::Exception e) {
939 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000940 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000941 } else {
942 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
943 return 0;
944 }
945 }
946 return 0;
947}
948
george826b87aff2005-02-13 10:48:21 +0000949void RfbPlayer::closeSessionFile() {
950 char speedStr[10];
951 RECT r;
952
953 // Uncheck all toolbar buttons
954 if (tb.getHandle()) {
955 tb.checkButton(ID_PLAY, false);
956 tb.checkButton(ID_PAUSE, false);
957 tb.checkButton(ID_STOP, false);
958 }
959
960 // Stop playback and update the player state
961 disableTBandMenuItems();
962 if (rfbReader) {
963 delete rfbReader->join();
964 rfbReader = 0;
965 delete [] fileName;
966 fileName = 0;
967 }
968 blankBuffer();
969 setTitle("None");
970 playbackSpeed = 1.0;
971 SendMessage(speedUpDown, UDM_SETPOS,
972 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
973 sprintf(speedStr, "%.2f", playbackSpeed);
974 SetWindowText(speedEdit, speedStr);
975 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
976
977 // Change the player window size and frame size to default
978 SetWindowPos(getMainHandle(), 0, 0, 0,
979 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
980 SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
981 buffer->setSize(32, 32);
982 calculateScrollBars();
983
984 // Update the cached sizing information and repaint the frame window
985 GetWindowRect(getFrameHandle(), &r);
986 window_size = Rect(r.left, r.top, r.right, r.bottom);
987 GetClientRect(getFrameHandle(), &r);
988 client_size = Rect(r.left, r.top, r.right, r.bottom);
989 InvalidateRect(getFrameHandle(), 0, TRUE);
990 UpdateWindow(getFrameHandle());
991}
992
george8217e92cb2005-01-31 16:01:02 +0000993void RfbPlayer::openSessionFile(char *_fileName) {
994 fileName = strDup(_fileName);
995
996 // Close the previous reading thread
997 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000998 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000999 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001000 }
1001 blankBuffer();
1002 newSession(fileName);
1003 setSpeed(playbackSpeed);
1004 rfbReader = new rfbSessionReader(this);
1005 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001006 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001007 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001008}
1009
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001010void RfbPlayer::setPaused(bool paused) {
1011 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001012 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001013 tb.checkButton(ID_PAUSE, true);
1014 tb.checkButton(ID_PLAY, false);
1015 tb.checkButton(ID_STOP, false);
1016 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1017 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001018 } else {
george825beb62a2005-02-09 13:04:32 +00001019 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001020 tb.checkButton(ID_PLAY, true);
1021 tb.checkButton(ID_STOP, false);
1022 tb.checkButton(ID_PAUSE, false);
1023 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1024 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001025 }
1026}
1027
george82006f2792005-02-05 07:40:47 +00001028void RfbPlayer::stopPlayback() {
1029 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001030 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001031 tb.checkButton(ID_STOP, true);
1032 tb.checkButton(ID_PLAY, false);
1033 tb.checkButton(ID_PAUSE, false);
1034 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1035 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001036 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001037}
1038
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001039void RfbPlayer::setSpeed(double speed) {
1040 serverInitTime = serverInitTime * getSpeed() / speed;
1041 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +00001042 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001043}
1044
1045double RfbPlayer::getSpeed() {
1046 return is->getSpeed();
1047}
1048
1049void RfbPlayer::setPos(long pos) {
1050 is->setTimeOffset(max(pos, serverInitTime));
1051}
1052
1053long RfbPlayer::getSeekOffset() {
1054 return is->getSeekOffset();
1055}
1056
1057bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001058 if (is) return is->isSeeking();
1059 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001060}
1061
1062bool RfbPlayer::isSeekMode() {
1063 return seekMode;
1064}
1065
1066bool RfbPlayer::isPaused() {
1067 return is->isPaused();
1068}
1069
1070long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001071 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001072}
1073
george828a471482005-02-06 07:15:53 +00001074void RfbPlayer::updatePos(long newPos) {
1075 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001076 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +00001077 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +00001078 newPos /= 1000;
george8244325492005-02-06 07:29:51 +00001079 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001080 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001081
1082 // Update the position of slider
1083 if (!sliderDraging) {
1084 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +00001085 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
1086 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +00001087 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001088}
1089
1090void RfbPlayer::skipHandshaking() {
1091 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1092 is->skip(skipBytes);
1093 state_ = RFBSTATE_NORMAL;
1094}
1095
1096void programInfo() {
1097 win32::FileVersionInfo inf;
1098 _tprintf(_T("%s - %s, Version %s\n"),
1099 inf.getVerString(_T("ProductName")),
1100 inf.getVerString(_T("FileDescription")),
1101 inf.getVerString(_T("FileVersion")));
1102 printf("%s\n", buildTime);
1103 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1104}
1105
1106void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001107 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001108}
1109
1110double playbackSpeed = 1.0;
1111long initTime = -1;
1112bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001113char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001114bool print_usage = false;
1115bool acceptBell = false;
1116
1117bool processParams(int argc, char* argv[]) {
1118 for (int i = 1; i < argc; i++) {
1119 if ((strcasecmp(argv[i], "-help") == 0) ||
1120 (strcasecmp(argv[i], "--help") == 0) ||
1121 (strcasecmp(argv[i], "/help") == 0) ||
1122 (strcasecmp(argv[i], "-h") == 0) ||
1123 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001124 (strcasecmp(argv[i], "/?") == 0) ||
1125 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001126 print_usage = true;
1127 return true;
1128 }
1129
1130 if ((strcasecmp(argv[i], "-speed") == 0) ||
1131 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1132 playbackSpeed = atof(argv[++i]);
1133 if (playbackSpeed <= 0) {
1134 return false;
1135 }
1136 continue;
1137 }
1138
1139 if ((strcasecmp(argv[i], "-pos") == 0) ||
1140 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1141 initTime = atol(argv[++i]);
1142 if (initTime <= 0)
1143 return false;
1144 continue;
1145 }
1146
1147 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1148 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001149 autoplay = true;
1150 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001151 }
1152
1153 if ((strcasecmp(argv[i], "-bell") == 0) ||
1154 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001155 acceptBell = true;
1156 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001157 }
1158
1159 if (i != argc - 1)
1160 return false;
1161 }
1162
1163 fileName = strDup(argv[argc-1]);
1164 return true;
1165}
1166
1167//
1168// -=- WinMain
1169//
1170
1171int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1172
1173 // - Process the command-line
1174
1175 int argc = __argc;
1176 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001177 if ((argc > 1) && (!processParams(argc, argv))) {
1178 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1179 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001180 }
george82e6883de2005-02-08 14:42:12 +00001181
1182 if (print_usage) {
1183 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001184 return 0;
george8267cbcd02005-01-16 15:39:56 +00001185 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001186
george82e6883de2005-02-08 14:42:12 +00001187 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001188 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001189 try {
1190 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001191 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001192 } catch (rdr::Exception e) {
1193 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1194 delete player;
1195 return 0;
1196 }
1197
1198 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001199 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001200 MSG msg;
1201 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001202 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1203 TranslateMessage(&msg);
1204 DispatchMessage(&msg);
1205 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001206 }
1207
george82e6883de2005-02-08 14:42:12 +00001208 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001209 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001210 if (player) delete player;
1211 } catch (rdr::Exception e) {
1212 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1213 }
1214
1215 return 0;
1216};