blob: 5deb695f809a314a77b834a2bfb3c35042527ab0 [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
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000063
george82d070c692005-01-19 16:44:04 +000064#define ID_TOOLBAR 500
65#define ID_PLAY 510
66#define ID_PAUSE 520
67#define ID_TIME_STATIC 530
68#define ID_SPEED_STATIC 540
69#define ID_SPEED_EDIT 550
70#define ID_POS_TRACKBAR 560
71#define ID_SPEED_UPDOWN 570
72
73
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000074//
75// -=- RfbPlayerClass
76
77//
78// Window class used as the basis for RfbPlayer instance
79//
80
81class RfbPlayerClass {
82public:
83 RfbPlayerClass();
84 ~RfbPlayerClass();
85 ATOM classAtom;
86 HINSTANCE instance;
87};
88
89LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
90 LRESULT result;
91
92 if (msg == WM_CREATE)
93 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
94 else if (msg == WM_DESTROY) {
95 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000096 SetWindowLong(hwnd, GWL_USERDATA, 0);
97 }
98 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
99 if (!_this) {
100 vlog.info("null _this in %x, message %u", hwnd, msg);
101 return DefWindowProc(hwnd, msg, wParam, lParam);
102 }
103
104 try {
105 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
106 } catch (rdr::Exception& e) {
107 vlog.error("untrapped: %s", e.str());
108 }
109
110 return result;
111};
112
113RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
114 WNDCLASS wndClass;
115 wndClass.style = 0;
116 wndClass.lpfnWndProc = RfbPlayerProc;
117 wndClass.cbClsExtra = 0;
118 wndClass.cbWndExtra = 0;
119 wndClass.hInstance = instance = GetModuleHandle(0);
120 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000121 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000122 if (!wndClass.hIcon)
123 printf("unable to load icon:%ld", GetLastError());
124 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
125 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000126 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000127 wndClass.lpszClassName = _T("RfbPlayerClass");
128 classAtom = RegisterClass(&wndClass);
129 if (!classAtom) {
130 throw rdr::SystemException("unable to register RfbPlayer window class",
131 GetLastError());
132 }
133}
134
135RfbPlayerClass::~RfbPlayerClass() {
136 if (classAtom) {
137 UnregisterClass((const TCHAR*)classAtom, instance);
138 }
139}
140
141RfbPlayerClass baseClass;
142
143//
144// -=- RfbFrameClass
145
146//
147// Window class used to displaying the rfb data
148//
149
150class RfbFrameClass {
151public:
152 RfbFrameClass();
153 ~RfbFrameClass();
154 ATOM classAtom;
155 HINSTANCE instance;
156};
157
158LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
159 LRESULT result;
160
161 if (msg == WM_CREATE)
162 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
163 else if (msg == WM_DESTROY)
164 SetWindowLong(hwnd, GWL_USERDATA, 0);
165 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
166 if (!_this) {
167 vlog.info("null _this in %x, message %u", hwnd, msg);
168 return DefWindowProc(hwnd, msg, wParam, lParam);
169 }
170
171 try {
172 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
173 } catch (rdr::Exception& e) {
174 vlog.error("untrapped: %s", e.str());
175 }
176
177 return result;
178}
179
180RfbFrameClass::RfbFrameClass() : classAtom(0) {
181 WNDCLASS wndClass;
182 wndClass.style = 0;
183 wndClass.lpfnWndProc = FrameProc;
184 wndClass.cbClsExtra = 0;
185 wndClass.cbWndExtra = 0;
186 wndClass.hInstance = instance = GetModuleHandle(0);
187 wndClass.hIcon = 0;
188 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
189 wndClass.hbrBackground = 0;
190 wndClass.lpszMenuName = 0;
191 wndClass.lpszClassName = _T("RfbPlayerClass1");
192 classAtom = RegisterClass(&wndClass);
193 if (!classAtom) {
194 throw rdr::SystemException("unable to register RfbPlayer window class",
195 GetLastError());
196 }
197}
198
199RfbFrameClass::~RfbFrameClass() {
200 if (classAtom) {
201 UnregisterClass((const TCHAR*)classAtom, instance);
202 }
203}
204
205RfbFrameClass frameClass;
206
207//
208// -=- RfbPlayer instance implementation
209//
210
211RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000212 bool _autoplay = false, bool _acceptBell = false)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000213: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
george82e6883de2005-02-08 14:42:12 +0000214 autoplay(_autoplay), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000215 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000216 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000217 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000218 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000219
george82e6883de2005-02-08 14:42:12 +0000220 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000221
george823c8fbbf2005-01-24 11:09:08 +0000222 // Reset the full session time
223 strcpy(fullSessionTime, "00m:00s");
224
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000225 // Create the main window
226 const TCHAR* name = _T("RfbPlayer");
227 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000228 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000229 if (!mainHwnd) {
230 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
231 }
232 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
233
234 // Create the backing buffer
235 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000236 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000237
george8217e92cb2005-01-31 16:01:02 +0000238 // Open the session file
239 if (fileName) {
240 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000241 if (initTime > 0) setPos(initTime);
242 setSpeed(playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +0000243 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000244}
245
246RfbPlayer::~RfbPlayer() {
247 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000248 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000249 delete rfbReader->join();
250 rfbReader = 0;
251 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000252 if (mainHwnd) {
253 setVisible(false);
254 DestroyWindow(mainHwnd);
255 mainHwnd = 0;
256 }
george825beb62a2005-02-09 13:04:32 +0000257 if (buffer) delete buffer;
258 if (cutText) delete [] cutText;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000259 vlog.debug("~RfbPlayer done");
260}
261
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000262LRESULT
263RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
264 switch (msg) {
265
266 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000267
268 case WM_CREATE:
269 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000270 // Create the frame window
271 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
272 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
273 hwnd, 0, frameClass.instance, this);
274
george82d070c692005-01-19 16:44:04 +0000275 createToolBar(hwnd);
276
george82006f2792005-02-05 07:40:47 +0000277 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000278
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000279 return 0;
280 }
281
george827214b822004-12-12 07:02:51 +0000282 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000283
284 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000285 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000286 case ID_OPENFILE:
287 {
288 char curDir[_MAX_DIR];
289 static char filename[_MAX_PATH];
290 OPENFILENAME ofn;
291 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
292 GetCurrentDirectory(sizeof(curDir), curDir);
293
294 ofn.lStructSize = sizeof(OPENFILENAME);
295 ofn.hwndOwner = getMainHandle();
296 ofn.lpstrFile = filename;
297 ofn.nMaxFile = sizeof(filename);
298 ofn.lpstrInitialDir = curDir;
299 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
300 "All files (*.*)\0*.*\0";
301 ofn.lpstrDefExt = "rfb";
302 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
303 if (GetOpenFileName(&ofn))
304 openSessionFile(filename);
305 }
306 break;
george825c13c662005-01-27 14:48:23 +0000307 case ID_PLAY:
308 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000309 break;
310 case ID_PAUSE:
311 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000312 break;
313 case ID_STOP:
314 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000315 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000316 }
george825c13c662005-01-27 14:48:23 +0000317 break;
318 case ID_PLAYPAUSE:
319 if (isPaused()) {
320 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000321 } else {
322 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000323 }
george825c13c662005-01-27 14:48:23 +0000324 break;
george827549df42005-02-08 16:31:02 +0000325 case ID_GOTO:
326 {
327 GotoPosDialog gotoPosDlg;
328 if (gotoPosDlg.showDialog()) {
329 setPos(gotoPosDlg.getPos());
330 updatePos(getTimeOffset());
331 }
332 }
333 break;
george825c13c662005-01-27 14:48:23 +0000334 case ID_FULLSCREEN:
335 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
336 break;
george8231a36332005-02-06 17:27:34 +0000337 case ID_LOOP:
338 loopPlayback = !loopPlayback;
339 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
340 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
341 break;
george824ea27f62005-01-29 15:03:06 +0000342 case ID_RETURN:
343 // Update the speed if return pressed in speedEdit
344 if (speedEdit == GetFocus()) {
345 char speedStr[20], *stopStr;
346 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
347 double speed = strtod(speedStr, &stopStr);
348 if (speed > 0) {
349 speed = min(MAX_SPEED, speed);
350 // Update speedUpDown position
351 SendMessage(speedUpDown, UDM_SETPOS,
352 0, MAKELONG((short)(speed / 0.5), 0));
353 } else {
354 speed = getSpeed();
355 }
356 setSpeed(speed);
357 sprintf(speedStr, "%.2f", speed);
358 SetWindowText(speedEdit, speedStr);
359 }
360 break;
george8201aa6732005-02-06 17:13:03 +0000361 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000362 PostQuitMessage(0);
363 break;
george82ef5f7262005-02-08 15:09:26 +0000364 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000365 MessageBox(getMainHandle(),
366 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
367 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000368 }
369 break;
370
371 // Update frame's window size and add scrollbars if required
372
373 case WM_SIZE:
374 {
george82d070c692005-01-19 16:44:04 +0000375
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000376 Point old_offset = bufferToClient(Point(0, 0));
377
378 // Update the cached sizing information
379 RECT r;
380 GetClientRect(getMainHandle(), &r);
381 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
382 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
383
384 GetWindowRect(getFrameHandle(), &r);
385 window_size = Rect(r.left, r.top, r.right, r.bottom);
386 GetClientRect(getFrameHandle(), &r);
387 client_size = Rect(r.left, r.top, r.right, r.bottom);
388
389 // Determine whether scrollbars are required
390 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000391
392 // Resize the ToolBar
393 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000394
395 // Redraw if required
396 if (!old_offset.equals(bufferToClient(Point(0, 0))))
397 InvalidateRect(getFrameHandle(), 0, TRUE);
398 }
399 break;
george828a471482005-02-06 07:15:53 +0000400
401 // Process messages from posTrackBar
402
403 case WM_HSCROLL:
404 {
405 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
406 Pos *= sliderStepMs;
407
408 switch (LOWORD(wParam)) {
409 case TB_PAGEUP:
410 case TB_PAGEDOWN:
411 case TB_LINEUP:
412 case TB_LINEDOWN:
413 case TB_THUMBTRACK:
414 sliderDraging = true;
415 updatePos(Pos);
416 return 0;
417 case TB_ENDTRACK:
418 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000419 sliderDraging = false;
420 return 0;
421 default:
422 break;
423 }
424 }
425 break;
george829e6e6cc2005-01-29 13:12:05 +0000426
427 case WM_NOTIFY:
428 switch (((NMHDR*)lParam)->code) {
429 case UDN_DELTAPOS:
430 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000431 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000432 char speedStr[20] = "\0";
433 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
434 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
435 double speed;
436
george824ea27f62005-01-29 15:03:06 +0000437 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000438 if (upDown->iDelta > 0) {
439 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
440 } else {
george824ea27f62005-01-29 15:03:06 +0000441 // It's need to round the UpDown position
442 if ((upDown->iPos * 0.5) != getSpeed()) {
443 upDown->iDelta = 0;
444 lResult = TRUE;
445 }
george829e6e6cc2005-01-29 13:12:05 +0000446 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
447 }
448 _gcvt(speed, 5, speedStr);
449 sprintf(speedStr, "%.2f", speed);
450 SetWindowText(speedEdit, speedStr);
451 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000452 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000453 }
george824ea27f62005-01-29 15:03:06 +0000454 }
george829e6e6cc2005-01-29 13:12:05 +0000455 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000456
457 case WM_CLOSE:
458 vlog.debug("WM_CLOSE %x", getMainHandle());
459 PostQuitMessage(0);
460 break;
461 }
462
463 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
464}
465
466LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
467 switch (msg) {
468
469 case WM_PAINT:
470 {
george825beb62a2005-02-09 13:04:32 +0000471 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000472 seekMode = true;
473 return 0;
474 } else {
475 if (seekMode) {
476 seekMode = false;
477 InvalidateRect(getFrameHandle(), 0, true);
478 UpdateWindow(getFrameHandle());
479 return 0;
480 }
481 }
482
483 PAINTSTRUCT ps;
484 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
485 if (!paintDC)
486 throw SystemException("unable to BeginPaint", GetLastError());
487 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
488
489 if (!pr.is_empty()) {
490
491 if (buffer->bitmap) {
492
493 // Get device context
494 BitmapDC bitmapDC(paintDC, buffer->bitmap);
495
496 // Blit the border if required
497 Rect bufpos = bufferToClient(buffer->getRect());
498 if (!pr.enclosed_by(bufpos)) {
499 vlog.debug("draw border");
500 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
501 RECT r;
502 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
503 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
504 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
505 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
506 }
507
508 // Do the blit
509 Point buf_pos = clientToBuffer(pr.tl);
510 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
511 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
512 throw SystemException("unable to BitBlt to window", GetLastError());
513
514 } else {
515 // Blit a load of black
516 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
517 0, 0, 0, BLACKNESS))
518 throw SystemException("unable to BitBlt to blank window", GetLastError());
519 }
520 }
521 EndPaint(getFrameHandle(), &ps);
522 }
523 return 0;
524
525 case WM_VSCROLL:
526 case WM_HSCROLL:
527 {
528 Point delta;
529 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
530
531 switch (LOWORD(wParam)) {
532 case SB_PAGEUP: newpos -= 50; break;
533 case SB_PAGEDOWN: newpos += 50; break;
534 case SB_LINEUP: newpos -= 5; break;
535 case SB_LINEDOWN: newpos += 5; break;
536 case SB_THUMBTRACK:
537 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
538 default: vlog.info("received unknown scroll message");
539 };
540
541 if (msg == WM_HSCROLL)
542 setViewportOffset(Point(newpos, scrolloffset.y));
543 else
544 setViewportOffset(Point(scrolloffset.x, newpos));
545
546 SCROLLINFO si;
547 si.cbSize = sizeof(si);
548 si.fMask = SIF_POS;
549 si.nPos = newpos;
550 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
551 }
552 break;
553 }
554
555 return DefWindowProc(hwnd, msg, wParam, lParam);
556}
557
558void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000559 bool _autoplay = false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000560 autoplay = _autoplay;
561 playbackSpeed = _playbackSpeed;
562 initTime = _initTime;
563}
564
565void RfbPlayer::applyOptions() {
566 if (initTime >= 0)
567 setPos(initTime);
568 setSpeed(playbackSpeed);
569 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000570}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000571
george82d070c692005-01-19 16:44:04 +0000572void RfbPlayer::createToolBar(HWND parentHwnd) {
573 RECT tRect;
574 InitCommonControls();
575
576 tb.create(ID_TOOLBAR, parentHwnd);
577 tb.addBitmap(4, IDB_TOOLBAR);
578
579 // Create the control buttons
580 tb.addButton(0, ID_PLAY);
581 tb.addButton(1, ID_PAUSE);
582 tb.addButton(2, ID_STOP);
583 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
584 tb.addButton(3, ID_FULLSCREEN);
585 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
586
587 // Create the static control for the time output
588 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
589 tb.getButtonRect(6, &tRect);
590 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
591 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
592 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
593 GetModuleHandle(0), 0);
594 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
595
596 // Create the trackbar control for the time position
597 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
598 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000599 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000600 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
601 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
602 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
603 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000604 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000605 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
606
607 // Create the label with "Speed:" caption
608 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
609 tb.getButtonRect(10, &tRect);
610 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
611 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
612 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
613
614 // Create the edit control and the spin for the speed managing
615 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
616 tb.getButtonRect(11, &tRect);
617 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
618 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
619 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
620 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
621 // It's need to send notify messages to toolbar parent window
622 SetParent(speedEdit, tb.getHandle());
623
624 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
625 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000626 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000627}
628
george82a21d2952005-02-12 11:30:03 +0000629void RfbPlayer::disableTBandMenuItems() {
630 // Disable the menu items
631 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
632 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
633 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
634 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
635 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
636 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
637 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
638 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
640
641 // Disable the toolbar buttons and child controls
642 tb.enableButton(ID_PLAY, false);
643 tb.enableButton(ID_PAUSE, false);
644 tb.enableButton(ID_STOP, false);
645 tb.enableButton(ID_FULLSCREEN, false);
646 EnableWindow(posTrackBar, false);
647 EnableWindow(speedEdit, false);
648}
649
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000650void RfbPlayer::setVisible(bool visible) {
651 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
652 if (visible) {
653 // When the window becomes visible, make it active
654 SetForegroundWindow(getMainHandle());
655 SetActiveWindow(getMainHandle());
656 }
657}
658
659void RfbPlayer::setTitle(const char *title) {
660 char _title[256];
661 strcpy(_title, AppName);
662 strcat(_title, " - ");
663 strcat(_title, title);
664 SetWindowText(getMainHandle(), _title);
665}
666
667void RfbPlayer::setFrameSize(int width, int height) {
668 // Calculate and set required size for main window
669 RECT r = {0, 0, width, height};
670 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
671 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
672 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
673 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
674 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
675 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
676
677 // Enable/disable scrollbars as appropriate
678 calculateScrollBars();
679}
680
681void RfbPlayer::calculateScrollBars() {
682 // Calculate the required size of window
683 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
684 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
685 DWORD old_style;
686 RECT r;
687 SetRect(&r, 0, 0, buffer->width(), buffer->height());
688 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
689 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
690
691 // Work out whether scroll bars are required
692 do {
693 old_style = style;
694
695 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
696 style |= WS_HSCROLL;
697 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
698 }
699 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
700 style |= WS_VSCROLL;
701 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
702 }
703 } while (style != old_style);
704
705 // Tell Windows to update the window style & cached settings
706 if (style != current_style) {
707 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
708 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
709 }
710
711 // Update the scroll settings
712 SCROLLINFO si;
713 if (style & WS_VSCROLL) {
714 si.cbSize = sizeof(si);
715 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
716 si.nMin = 0;
717 si.nMax = buffer->height();
718 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
719 maxscrolloffset.y = max(0, si.nMax-si.nPage);
720 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
721 si.nPos = scrolloffset.y;
722 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
723 }
724 if (style & WS_HSCROLL) {
725 si.cbSize = sizeof(si);
726 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
727 si.nMin = 0;
728 si.nMax = buffer->width();
729 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
730 maxscrolloffset.x = max(0, si.nMax-si.nPage);
731 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
732 si.nPos = scrolloffset.x;
733 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
734 }
735}
736
737bool RfbPlayer::setViewportOffset(const Point& tl) {
738/* ***
739 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
740 max(0, min(maxscrolloffset.y, tl.y)));
741 */
742 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
743 max(0, min(tl.y, buffer->height()-client_size.height())));
744 Point delta = np.translate(scrolloffset.negate());
745 if (!np.equals(scrolloffset)) {
746 scrolloffset = np;
747 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
748 UpdateWindow(getFrameHandle());
749 return true;
750 }
751 return false;
752}
753
754void RfbPlayer::close(const char* reason) {
755 setVisible(false);
756 if (reason) {
757 vlog.info("closing - %s", reason);
758 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
759 }
760 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
761}
762
763void RfbPlayer::blankBuffer() {
764 fillRect(buffer->getRect(), 0);
765}
766
767void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000768 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000769 blankBuffer();
770 newSession(fileName);
771 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000772 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000773 if (paused) is->pausePlayback();
774 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000775}
776
777void RfbPlayer::processMsg() {
778 static long update_time = GetTickCount();
779 try {
george828a471482005-02-06 07:15:53 +0000780 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
781 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000782 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000783 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000784 update_time = GetTickCount();
785 }
786 RfbProto::processMsg();
787 } catch (rdr::Exception e) {
788 if (strcmp(e.str(), "[End Of File]") == 0) {
789 rewind();
george8231a36332005-02-06 17:27:34 +0000790 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000791 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000792 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000793 return;
794 }
795 // It's a special exception to perform backward seeking.
796 // We only rewind the stream and seek the offset
797 if (strcmp(e.str(), "[REWIND]") == 0) {
798 long initTime = getSeekOffset();
799 rewind();
800 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000801 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000802 } else {
803 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
804 return;
805 }
806 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000807}
808
809void RfbPlayer::serverInit() {
810 RfbProto::serverInit();
811
812 // Save the server init time for using in setPos()
813 serverInitTime = getTimeOffset() / getSpeed();
814
815 // Resize the backing buffer
816 buffer->setSize(cp.width, cp.height);
817
818 // Check on the true colour mode
819 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000820 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000821
822 // Set the session pixel format
823 buffer->setPF(cp.pf());
824
825 // If the window is not maximised then resize it
826 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
827 setFrameSize(cp.width, cp.height);
828
829 // Set the window title and show it
830 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000831
george82d4d69e62005-02-05 09:23:18 +0000832 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000833 sessionTimeMs = calculateSessionTime(fileName);
834 sprintf(fullSessionTime, "%.2um:%.2us",
835 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000836 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000837 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
838 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000839 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000840
george82006f2792005-02-05 07:40:47 +0000841 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000842}
843
844void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
845 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
846 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
847/* int i;
848 for (i=0;i<count;i++) {
849 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
850 }
851 // *** change to 0, 256?
852 refreshWindowPalette(first, count);
853 palette_changed = true;
854 InvalidateRect(getFrameHandle(), 0, FALSE);*/
855}
856
857void RfbPlayer::bell() {
858 if (acceptBell)
859 MessageBeep(-1);
860}
861
862void RfbPlayer::serverCutText(const char* str, int len) {
863 if (cutText != NULL)
864 delete [] cutText;
865 cutText = new char[len + 1];
866 memcpy(cutText, str, len);
867 cutText[len] = '\0';
868}
869
870void RfbPlayer::frameBufferUpdateEnd() {
871};
872
873void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
874}
875
876void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
877}
878
879
880void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
881 buffer->fillRect(r, pix);
882 invalidateBufferRect(r);
883}
884
885void RfbPlayer::imageRect(const Rect& r, void* pixels) {
886 buffer->imageRect(r, pixels);
887 invalidateBufferRect(r);
888}
889
890void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
891 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
892 invalidateBufferRect(r);
893}
894
895bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
896 Rect rect = bufferToClient(crect);
897 if (rect.intersect(client_size).is_empty()) return false;
898 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
899 InvalidateRect(getFrameHandle(), &invalid, FALSE);
900 return true;
901}
902
george8257f13522005-02-05 08:48:22 +0000903long RfbPlayer::calculateSessionTime(char *filename) {
904 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000905 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000906 try {
907 while (TRUE) {
908 sessionFile.skip(1024);
909 }
910 } catch (rdr::Exception e) {
911 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000912 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000913 } else {
914 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
915 return 0;
916 }
917 }
918 return 0;
919}
920
george8217e92cb2005-01-31 16:01:02 +0000921void RfbPlayer::openSessionFile(char *_fileName) {
922 fileName = strDup(_fileName);
923
924 // Close the previous reading thread
925 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000926 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000927 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000928 }
929 blankBuffer();
930 newSession(fileName);
931 setSpeed(playbackSpeed);
932 rfbReader = new rfbSessionReader(this);
933 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000934 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8217e92cb2005-01-31 16:01:02 +0000935}
936
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000937void RfbPlayer::setPaused(bool paused) {
938 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000939 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000940 tb.checkButton(ID_PAUSE, true);
941 tb.checkButton(ID_PLAY, false);
942 tb.checkButton(ID_STOP, false);
943 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
944 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000945 } else {
george825beb62a2005-02-09 13:04:32 +0000946 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000947 tb.checkButton(ID_PLAY, true);
948 tb.checkButton(ID_STOP, false);
949 tb.checkButton(ID_PAUSE, false);
950 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
951 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000952 }
953}
954
george82006f2792005-02-05 07:40:47 +0000955void RfbPlayer::stopPlayback() {
956 setPos(0);
george825beb62a2005-02-09 13:04:32 +0000957 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000958 tb.checkButton(ID_STOP, true);
959 tb.checkButton(ID_PLAY, false);
960 tb.checkButton(ID_PAUSE, false);
961 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
962 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000963 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000964}
965
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000966void RfbPlayer::setSpeed(double speed) {
967 serverInitTime = serverInitTime * getSpeed() / speed;
968 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000969 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000970}
971
972double RfbPlayer::getSpeed() {
973 return is->getSpeed();
974}
975
976void RfbPlayer::setPos(long pos) {
977 is->setTimeOffset(max(pos, serverInitTime));
978}
979
980long RfbPlayer::getSeekOffset() {
981 return is->getSeekOffset();
982}
983
984bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +0000985 if (is) return is->isSeeking();
986 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000987}
988
989bool RfbPlayer::isSeekMode() {
990 return seekMode;
991}
992
993bool RfbPlayer::isPaused() {
994 return is->isPaused();
995}
996
997long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +0000998 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000999}
1000
george828a471482005-02-06 07:15:53 +00001001void RfbPlayer::updatePos(long newPos) {
1002 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001003 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +00001004 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +00001005 newPos /= 1000;
george8244325492005-02-06 07:29:51 +00001006 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001007 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001008
1009 // Update the position of slider
1010 if (!sliderDraging) {
1011 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +00001012 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
1013 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +00001014 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001015}
1016
1017void RfbPlayer::skipHandshaking() {
1018 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1019 is->skip(skipBytes);
1020 state_ = RFBSTATE_NORMAL;
1021}
1022
1023void programInfo() {
1024 win32::FileVersionInfo inf;
1025 _tprintf(_T("%s - %s, Version %s\n"),
1026 inf.getVerString(_T("ProductName")),
1027 inf.getVerString(_T("FileDescription")),
1028 inf.getVerString(_T("FileVersion")));
1029 printf("%s\n", buildTime);
1030 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1031}
1032
1033void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001034 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001035}
1036
1037double playbackSpeed = 1.0;
1038long initTime = -1;
1039bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001040char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001041bool print_usage = false;
1042bool acceptBell = false;
1043
1044bool processParams(int argc, char* argv[]) {
1045 for (int i = 1; i < argc; i++) {
1046 if ((strcasecmp(argv[i], "-help") == 0) ||
1047 (strcasecmp(argv[i], "--help") == 0) ||
1048 (strcasecmp(argv[i], "/help") == 0) ||
1049 (strcasecmp(argv[i], "-h") == 0) ||
1050 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001051 (strcasecmp(argv[i], "/?") == 0) ||
1052 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001053 print_usage = true;
1054 return true;
1055 }
1056
1057 if ((strcasecmp(argv[i], "-speed") == 0) ||
1058 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1059 playbackSpeed = atof(argv[++i]);
1060 if (playbackSpeed <= 0) {
1061 return false;
1062 }
1063 continue;
1064 }
1065
1066 if ((strcasecmp(argv[i], "-pos") == 0) ||
1067 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1068 initTime = atol(argv[++i]);
1069 if (initTime <= 0)
1070 return false;
1071 continue;
1072 }
1073
1074 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1075 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001076 autoplay = true;
1077 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001078 }
1079
1080 if ((strcasecmp(argv[i], "-bell") == 0) ||
1081 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001082 acceptBell = true;
1083 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001084 }
1085
1086 if (i != argc - 1)
1087 return false;
1088 }
1089
1090 fileName = strDup(argv[argc-1]);
1091 return true;
1092}
1093
1094//
1095// -=- WinMain
1096//
1097
1098int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1099
1100 // - Process the command-line
1101
1102 int argc = __argc;
1103 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001104 if ((argc > 1) && (!processParams(argc, argv))) {
1105 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1106 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001107 }
george82e6883de2005-02-08 14:42:12 +00001108
1109 if (print_usage) {
1110 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001111 return 0;
george8267cbcd02005-01-16 15:39:56 +00001112 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001113
george82e6883de2005-02-08 14:42:12 +00001114 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001115 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001116 try {
1117 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001118 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001119 } catch (rdr::Exception e) {
1120 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1121 delete player;
1122 return 0;
1123 }
1124
1125 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001126 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001127 MSG msg;
1128 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001129 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1130 TranslateMessage(&msg);
1131 DispatchMessage(&msg);
1132 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001133 }
1134
george82e6883de2005-02-08 14:42:12 +00001135 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001136 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137 if (player) delete player;
1138 } catch (rdr::Exception e) {
1139 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1140 }
1141
1142 return 0;
1143};