blob: 8df1e5faa2cf83dfab4e4d51e3491ca96e6352c7 [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);
george8263ebbcc2005-02-12 12:09:13 +0000243 } else {
244 disableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000245 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000246}
247
248RfbPlayer::~RfbPlayer() {
249 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000250 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000251 delete rfbReader->join();
252 rfbReader = 0;
253 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000254 if (mainHwnd) {
255 setVisible(false);
256 DestroyWindow(mainHwnd);
257 mainHwnd = 0;
258 }
george825beb62a2005-02-09 13:04:32 +0000259 if (buffer) delete buffer;
260 if (cutText) delete [] cutText;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000261 vlog.debug("~RfbPlayer done");
262}
263
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000264LRESULT
265RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
266 switch (msg) {
267
268 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000269
270 case WM_CREATE:
271 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000272 // Create the frame window
273 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
274 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
275 hwnd, 0, frameClass.instance, this);
276
george82d070c692005-01-19 16:44:04 +0000277 createToolBar(hwnd);
278
george82006f2792005-02-05 07:40:47 +0000279 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000280
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000281 return 0;
282 }
283
george827214b822004-12-12 07:02:51 +0000284 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000285
286 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000287 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000288 case ID_OPENFILE:
289 {
290 char curDir[_MAX_DIR];
291 static char filename[_MAX_PATH];
292 OPENFILENAME ofn;
293 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
294 GetCurrentDirectory(sizeof(curDir), curDir);
295
296 ofn.lStructSize = sizeof(OPENFILENAME);
297 ofn.hwndOwner = getMainHandle();
298 ofn.lpstrFile = filename;
299 ofn.nMaxFile = sizeof(filename);
300 ofn.lpstrInitialDir = curDir;
301 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
302 "All files (*.*)\0*.*\0";
303 ofn.lpstrDefExt = "rfb";
304 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
305 if (GetOpenFileName(&ofn))
306 openSessionFile(filename);
307 }
308 break;
george825c13c662005-01-27 14:48:23 +0000309 case ID_PLAY:
310 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000311 break;
312 case ID_PAUSE:
313 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000314 break;
315 case ID_STOP:
316 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000317 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000318 }
george825c13c662005-01-27 14:48:23 +0000319 break;
320 case ID_PLAYPAUSE:
321 if (isPaused()) {
322 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000323 } else {
324 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000325 }
george825c13c662005-01-27 14:48:23 +0000326 break;
george827549df42005-02-08 16:31:02 +0000327 case ID_GOTO:
328 {
329 GotoPosDialog gotoPosDlg;
330 if (gotoPosDlg.showDialog()) {
331 setPos(gotoPosDlg.getPos());
332 updatePos(getTimeOffset());
333 }
334 }
335 break;
george825c13c662005-01-27 14:48:23 +0000336 case ID_FULLSCREEN:
337 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
338 break;
george8231a36332005-02-06 17:27:34 +0000339 case ID_LOOP:
340 loopPlayback = !loopPlayback;
341 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
342 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
343 break;
george824ea27f62005-01-29 15:03:06 +0000344 case ID_RETURN:
345 // Update the speed if return pressed in speedEdit
346 if (speedEdit == GetFocus()) {
347 char speedStr[20], *stopStr;
348 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
349 double speed = strtod(speedStr, &stopStr);
350 if (speed > 0) {
351 speed = min(MAX_SPEED, speed);
352 // Update speedUpDown position
353 SendMessage(speedUpDown, UDM_SETPOS,
354 0, MAKELONG((short)(speed / 0.5), 0));
355 } else {
356 speed = getSpeed();
357 }
358 setSpeed(speed);
359 sprintf(speedStr, "%.2f", speed);
360 SetWindowText(speedEdit, speedStr);
361 }
362 break;
george8201aa6732005-02-06 17:13:03 +0000363 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000364 PostQuitMessage(0);
365 break;
george82ef5f7262005-02-08 15:09:26 +0000366 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000367 MessageBox(getMainHandle(),
368 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
369 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000370 }
371 break;
372
373 // Update frame's window size and add scrollbars if required
374
375 case WM_SIZE:
376 {
george82d070c692005-01-19 16:44:04 +0000377
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000378 Point old_offset = bufferToClient(Point(0, 0));
379
380 // Update the cached sizing information
381 RECT r;
382 GetClientRect(getMainHandle(), &r);
383 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
384 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
385
386 GetWindowRect(getFrameHandle(), &r);
387 window_size = Rect(r.left, r.top, r.right, r.bottom);
388 GetClientRect(getFrameHandle(), &r);
389 client_size = Rect(r.left, r.top, r.right, r.bottom);
390
391 // Determine whether scrollbars are required
392 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000393
394 // Resize the ToolBar
395 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000396
397 // Redraw if required
398 if (!old_offset.equals(bufferToClient(Point(0, 0))))
399 InvalidateRect(getFrameHandle(), 0, TRUE);
400 }
401 break;
george828a471482005-02-06 07:15:53 +0000402
403 // Process messages from posTrackBar
404
405 case WM_HSCROLL:
406 {
407 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
408 Pos *= sliderStepMs;
409
410 switch (LOWORD(wParam)) {
411 case TB_PAGEUP:
412 case TB_PAGEDOWN:
413 case TB_LINEUP:
414 case TB_LINEDOWN:
415 case TB_THUMBTRACK:
416 sliderDraging = true;
417 updatePos(Pos);
418 return 0;
419 case TB_ENDTRACK:
420 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000421 sliderDraging = false;
422 return 0;
423 default:
424 break;
425 }
426 }
427 break;
george829e6e6cc2005-01-29 13:12:05 +0000428
429 case WM_NOTIFY:
430 switch (((NMHDR*)lParam)->code) {
431 case UDN_DELTAPOS:
432 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000433 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000434 char speedStr[20] = "\0";
435 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
436 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
437 double speed;
438
george824ea27f62005-01-29 15:03:06 +0000439 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000440 if (upDown->iDelta > 0) {
441 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
442 } else {
george824ea27f62005-01-29 15:03:06 +0000443 // It's need to round the UpDown position
444 if ((upDown->iPos * 0.5) != getSpeed()) {
445 upDown->iDelta = 0;
446 lResult = TRUE;
447 }
george829e6e6cc2005-01-29 13:12:05 +0000448 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
449 }
450 _gcvt(speed, 5, speedStr);
451 sprintf(speedStr, "%.2f", speed);
452 SetWindowText(speedEdit, speedStr);
453 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000454 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000455 }
george824ea27f62005-01-29 15:03:06 +0000456 }
george829e6e6cc2005-01-29 13:12:05 +0000457 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000458
459 case WM_CLOSE:
460 vlog.debug("WM_CLOSE %x", getMainHandle());
461 PostQuitMessage(0);
462 break;
463 }
464
465 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
466}
467
468LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
469 switch (msg) {
470
471 case WM_PAINT:
472 {
george825beb62a2005-02-09 13:04:32 +0000473 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000474 seekMode = true;
475 return 0;
476 } else {
477 if (seekMode) {
478 seekMode = false;
479 InvalidateRect(getFrameHandle(), 0, true);
480 UpdateWindow(getFrameHandle());
481 return 0;
482 }
483 }
484
485 PAINTSTRUCT ps;
486 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
487 if (!paintDC)
488 throw SystemException("unable to BeginPaint", GetLastError());
489 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
490
491 if (!pr.is_empty()) {
492
493 if (buffer->bitmap) {
494
495 // Get device context
496 BitmapDC bitmapDC(paintDC, buffer->bitmap);
497
498 // Blit the border if required
499 Rect bufpos = bufferToClient(buffer->getRect());
500 if (!pr.enclosed_by(bufpos)) {
501 vlog.debug("draw border");
502 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
503 RECT r;
504 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
505 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
506 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
507 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
508 }
509
510 // Do the blit
511 Point buf_pos = clientToBuffer(pr.tl);
512 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
513 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
514 throw SystemException("unable to BitBlt to window", GetLastError());
515
516 } else {
517 // Blit a load of black
518 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
519 0, 0, 0, BLACKNESS))
520 throw SystemException("unable to BitBlt to blank window", GetLastError());
521 }
522 }
523 EndPaint(getFrameHandle(), &ps);
524 }
525 return 0;
526
527 case WM_VSCROLL:
528 case WM_HSCROLL:
529 {
530 Point delta;
531 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
532
533 switch (LOWORD(wParam)) {
534 case SB_PAGEUP: newpos -= 50; break;
535 case SB_PAGEDOWN: newpos += 50; break;
536 case SB_LINEUP: newpos -= 5; break;
537 case SB_LINEDOWN: newpos += 5; break;
538 case SB_THUMBTRACK:
539 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
540 default: vlog.info("received unknown scroll message");
541 };
542
543 if (msg == WM_HSCROLL)
544 setViewportOffset(Point(newpos, scrolloffset.y));
545 else
546 setViewportOffset(Point(scrolloffset.x, newpos));
547
548 SCROLLINFO si;
549 si.cbSize = sizeof(si);
550 si.fMask = SIF_POS;
551 si.nPos = newpos;
552 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
553 }
554 break;
555 }
556
557 return DefWindowProc(hwnd, msg, wParam, lParam);
558}
559
560void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000561 bool _autoplay = false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000562 autoplay = _autoplay;
563 playbackSpeed = _playbackSpeed;
564 initTime = _initTime;
565}
566
567void RfbPlayer::applyOptions() {
568 if (initTime >= 0)
569 setPos(initTime);
570 setSpeed(playbackSpeed);
571 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000572}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000573
george82d070c692005-01-19 16:44:04 +0000574void RfbPlayer::createToolBar(HWND parentHwnd) {
575 RECT tRect;
576 InitCommonControls();
577
578 tb.create(ID_TOOLBAR, parentHwnd);
579 tb.addBitmap(4, IDB_TOOLBAR);
580
581 // Create the control buttons
582 tb.addButton(0, ID_PLAY);
583 tb.addButton(1, ID_PAUSE);
584 tb.addButton(2, ID_STOP);
585 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
586 tb.addButton(3, ID_FULLSCREEN);
587 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
588
589 // Create the static control for the time output
590 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
591 tb.getButtonRect(6, &tRect);
592 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
593 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
594 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
595 GetModuleHandle(0), 0);
596 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
597
598 // Create the trackbar control for the time position
599 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
600 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000601 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000602 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
603 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
604 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
605 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000606 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000607 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
608
609 // Create the label with "Speed:" caption
610 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
611 tb.getButtonRect(10, &tRect);
612 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
613 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
614 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
615
616 // Create the edit control and the spin for the speed managing
617 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
618 tb.getButtonRect(11, &tRect);
619 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
620 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
621 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
622 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
623 // It's need to send notify messages to toolbar parent window
624 SetParent(speedEdit, tb.getHandle());
625
626 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
627 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000628 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000629}
630
george82a21d2952005-02-12 11:30:03 +0000631void RfbPlayer::disableTBandMenuItems() {
632 // Disable the menu items
633 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
634 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
635 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
636 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
637 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
638 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
640 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
641 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
642
643 // Disable the toolbar buttons and child controls
644 tb.enableButton(ID_PLAY, false);
645 tb.enableButton(ID_PAUSE, false);
646 tb.enableButton(ID_STOP, false);
647 tb.enableButton(ID_FULLSCREEN, false);
648 EnableWindow(posTrackBar, false);
649 EnableWindow(speedEdit, false);
650}
651
george82f5043162005-02-12 11:37:18 +0000652void RfbPlayer::enableTBandMenuItems() {
653 // Enable the menu items
654 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
655 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
656 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
657 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
658 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
659 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
660 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
661 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
662 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
663
664 // Enable the toolbar buttons and child controls
665 tb.enableButton(ID_PLAY, true);
666 tb.enableButton(ID_PAUSE, true);
667 tb.enableButton(ID_STOP, true);
668 tb.enableButton(ID_FULLSCREEN, true);
669 EnableWindow(posTrackBar, true);
670 EnableWindow(speedEdit, true);
671}
672
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000673void RfbPlayer::setVisible(bool visible) {
674 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
675 if (visible) {
676 // When the window becomes visible, make it active
677 SetForegroundWindow(getMainHandle());
678 SetActiveWindow(getMainHandle());
679 }
680}
681
682void RfbPlayer::setTitle(const char *title) {
683 char _title[256];
684 strcpy(_title, AppName);
685 strcat(_title, " - ");
686 strcat(_title, title);
687 SetWindowText(getMainHandle(), _title);
688}
689
690void RfbPlayer::setFrameSize(int width, int height) {
691 // Calculate and set required size for main window
692 RECT r = {0, 0, width, height};
693 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
694 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
695 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
696 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
697 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
698 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
699
700 // Enable/disable scrollbars as appropriate
701 calculateScrollBars();
702}
703
704void RfbPlayer::calculateScrollBars() {
705 // Calculate the required size of window
706 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
707 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
708 DWORD old_style;
709 RECT r;
710 SetRect(&r, 0, 0, buffer->width(), buffer->height());
711 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
712 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
713
714 // Work out whether scroll bars are required
715 do {
716 old_style = style;
717
718 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
719 style |= WS_HSCROLL;
720 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
721 }
722 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
723 style |= WS_VSCROLL;
724 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
725 }
726 } while (style != old_style);
727
728 // Tell Windows to update the window style & cached settings
729 if (style != current_style) {
730 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
731 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
732 }
733
734 // Update the scroll settings
735 SCROLLINFO si;
736 if (style & WS_VSCROLL) {
737 si.cbSize = sizeof(si);
738 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
739 si.nMin = 0;
740 si.nMax = buffer->height();
741 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
742 maxscrolloffset.y = max(0, si.nMax-si.nPage);
743 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
744 si.nPos = scrolloffset.y;
745 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
746 }
747 if (style & WS_HSCROLL) {
748 si.cbSize = sizeof(si);
749 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
750 si.nMin = 0;
751 si.nMax = buffer->width();
752 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
753 maxscrolloffset.x = max(0, si.nMax-si.nPage);
754 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
755 si.nPos = scrolloffset.x;
756 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
757 }
758}
759
760bool RfbPlayer::setViewportOffset(const Point& tl) {
761/* ***
762 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
763 max(0, min(maxscrolloffset.y, tl.y)));
764 */
765 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
766 max(0, min(tl.y, buffer->height()-client_size.height())));
767 Point delta = np.translate(scrolloffset.negate());
768 if (!np.equals(scrolloffset)) {
769 scrolloffset = np;
770 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
771 UpdateWindow(getFrameHandle());
772 return true;
773 }
774 return false;
775}
776
777void RfbPlayer::close(const char* reason) {
778 setVisible(false);
779 if (reason) {
780 vlog.info("closing - %s", reason);
781 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
782 }
783 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
784}
785
786void RfbPlayer::blankBuffer() {
787 fillRect(buffer->getRect(), 0);
788}
789
790void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000791 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000792 blankBuffer();
793 newSession(fileName);
794 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000795 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000796 if (paused) is->pausePlayback();
797 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000798}
799
800void RfbPlayer::processMsg() {
801 static long update_time = GetTickCount();
802 try {
george828a471482005-02-06 07:15:53 +0000803 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
804 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000805 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000806 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000807 update_time = GetTickCount();
808 }
809 RfbProto::processMsg();
810 } catch (rdr::Exception e) {
811 if (strcmp(e.str(), "[End Of File]") == 0) {
812 rewind();
george8231a36332005-02-06 17:27:34 +0000813 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000814 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000815 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000816 return;
817 }
818 // It's a special exception to perform backward seeking.
819 // We only rewind the stream and seek the offset
820 if (strcmp(e.str(), "[REWIND]") == 0) {
821 long initTime = getSeekOffset();
822 rewind();
823 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000824 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000825 } else {
826 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
827 return;
828 }
829 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000830}
831
832void RfbPlayer::serverInit() {
833 RfbProto::serverInit();
834
835 // Save the server init time for using in setPos()
836 serverInitTime = getTimeOffset() / getSpeed();
837
838 // Resize the backing buffer
839 buffer->setSize(cp.width, cp.height);
840
841 // Check on the true colour mode
842 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000843 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000844
845 // Set the session pixel format
846 buffer->setPF(cp.pf());
847
848 // If the window is not maximised then resize it
849 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
850 setFrameSize(cp.width, cp.height);
851
852 // Set the window title and show it
853 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000854
george82d4d69e62005-02-05 09:23:18 +0000855 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000856 sessionTimeMs = calculateSessionTime(fileName);
857 sprintf(fullSessionTime, "%.2um:%.2us",
858 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000859 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000860 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
861 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000862 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000863
george82006f2792005-02-05 07:40:47 +0000864 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000865}
866
867void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
868 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
869 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
870/* int i;
871 for (i=0;i<count;i++) {
872 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
873 }
874 // *** change to 0, 256?
875 refreshWindowPalette(first, count);
876 palette_changed = true;
877 InvalidateRect(getFrameHandle(), 0, FALSE);*/
878}
879
880void RfbPlayer::bell() {
881 if (acceptBell)
882 MessageBeep(-1);
883}
884
885void RfbPlayer::serverCutText(const char* str, int len) {
886 if (cutText != NULL)
887 delete [] cutText;
888 cutText = new char[len + 1];
889 memcpy(cutText, str, len);
890 cutText[len] = '\0';
891}
892
893void RfbPlayer::frameBufferUpdateEnd() {
894};
895
896void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
897}
898
899void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
900}
901
902
903void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
904 buffer->fillRect(r, pix);
905 invalidateBufferRect(r);
906}
907
908void RfbPlayer::imageRect(const Rect& r, void* pixels) {
909 buffer->imageRect(r, pixels);
910 invalidateBufferRect(r);
911}
912
913void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
914 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
915 invalidateBufferRect(r);
916}
917
918bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
919 Rect rect = bufferToClient(crect);
920 if (rect.intersect(client_size).is_empty()) return false;
921 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
922 InvalidateRect(getFrameHandle(), &invalid, FALSE);
923 return true;
924}
925
george8257f13522005-02-05 08:48:22 +0000926long RfbPlayer::calculateSessionTime(char *filename) {
927 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000928 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000929 try {
930 while (TRUE) {
931 sessionFile.skip(1024);
932 }
933 } catch (rdr::Exception e) {
934 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000935 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000936 } else {
937 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
938 return 0;
939 }
940 }
941 return 0;
942}
943
george8217e92cb2005-01-31 16:01:02 +0000944void RfbPlayer::openSessionFile(char *_fileName) {
945 fileName = strDup(_fileName);
946
947 // Close the previous reading thread
948 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000949 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000950 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000951 }
952 blankBuffer();
953 newSession(fileName);
954 setSpeed(playbackSpeed);
955 rfbReader = new rfbSessionReader(this);
956 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000957 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +0000958 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000959}
960
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000961void RfbPlayer::setPaused(bool paused) {
962 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000963 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000964 tb.checkButton(ID_PAUSE, true);
965 tb.checkButton(ID_PLAY, false);
966 tb.checkButton(ID_STOP, false);
967 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
968 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000969 } else {
george825beb62a2005-02-09 13:04:32 +0000970 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000971 tb.checkButton(ID_PLAY, true);
972 tb.checkButton(ID_STOP, false);
973 tb.checkButton(ID_PAUSE, false);
974 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
975 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000976 }
977}
978
george82006f2792005-02-05 07:40:47 +0000979void RfbPlayer::stopPlayback() {
980 setPos(0);
george825beb62a2005-02-09 13:04:32 +0000981 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000982 tb.checkButton(ID_STOP, true);
983 tb.checkButton(ID_PLAY, false);
984 tb.checkButton(ID_PAUSE, false);
985 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
986 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000987 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000988}
989
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000990void RfbPlayer::setSpeed(double speed) {
991 serverInitTime = serverInitTime * getSpeed() / speed;
992 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000993 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000994}
995
996double RfbPlayer::getSpeed() {
997 return is->getSpeed();
998}
999
1000void RfbPlayer::setPos(long pos) {
1001 is->setTimeOffset(max(pos, serverInitTime));
1002}
1003
1004long RfbPlayer::getSeekOffset() {
1005 return is->getSeekOffset();
1006}
1007
1008bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001009 if (is) return is->isSeeking();
1010 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001011}
1012
1013bool RfbPlayer::isSeekMode() {
1014 return seekMode;
1015}
1016
1017bool RfbPlayer::isPaused() {
1018 return is->isPaused();
1019}
1020
1021long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001022 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001023}
1024
george828a471482005-02-06 07:15:53 +00001025void RfbPlayer::updatePos(long newPos) {
1026 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001027 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +00001028 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +00001029 newPos /= 1000;
george8244325492005-02-06 07:29:51 +00001030 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001031 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001032
1033 // Update the position of slider
1034 if (!sliderDraging) {
1035 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +00001036 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
1037 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +00001038 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001039}
1040
1041void RfbPlayer::skipHandshaking() {
1042 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1043 is->skip(skipBytes);
1044 state_ = RFBSTATE_NORMAL;
1045}
1046
1047void programInfo() {
1048 win32::FileVersionInfo inf;
1049 _tprintf(_T("%s - %s, Version %s\n"),
1050 inf.getVerString(_T("ProductName")),
1051 inf.getVerString(_T("FileDescription")),
1052 inf.getVerString(_T("FileVersion")));
1053 printf("%s\n", buildTime);
1054 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1055}
1056
1057void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001058 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001059}
1060
1061double playbackSpeed = 1.0;
1062long initTime = -1;
1063bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001064char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001065bool print_usage = false;
1066bool acceptBell = false;
1067
1068bool processParams(int argc, char* argv[]) {
1069 for (int i = 1; i < argc; i++) {
1070 if ((strcasecmp(argv[i], "-help") == 0) ||
1071 (strcasecmp(argv[i], "--help") == 0) ||
1072 (strcasecmp(argv[i], "/help") == 0) ||
1073 (strcasecmp(argv[i], "-h") == 0) ||
1074 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001075 (strcasecmp(argv[i], "/?") == 0) ||
1076 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001077 print_usage = true;
1078 return true;
1079 }
1080
1081 if ((strcasecmp(argv[i], "-speed") == 0) ||
1082 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1083 playbackSpeed = atof(argv[++i]);
1084 if (playbackSpeed <= 0) {
1085 return false;
1086 }
1087 continue;
1088 }
1089
1090 if ((strcasecmp(argv[i], "-pos") == 0) ||
1091 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1092 initTime = atol(argv[++i]);
1093 if (initTime <= 0)
1094 return false;
1095 continue;
1096 }
1097
1098 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1099 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001100 autoplay = true;
1101 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001102 }
1103
1104 if ((strcasecmp(argv[i], "-bell") == 0) ||
1105 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001106 acceptBell = true;
1107 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001108 }
1109
1110 if (i != argc - 1)
1111 return false;
1112 }
1113
1114 fileName = strDup(argv[argc-1]);
1115 return true;
1116}
1117
1118//
1119// -=- WinMain
1120//
1121
1122int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1123
1124 // - Process the command-line
1125
1126 int argc = __argc;
1127 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001128 if ((argc > 1) && (!processParams(argc, argv))) {
1129 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1130 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001131 }
george82e6883de2005-02-08 14:42:12 +00001132
1133 if (print_usage) {
1134 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001135 return 0;
george8267cbcd02005-01-16 15:39:56 +00001136 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137
george82e6883de2005-02-08 14:42:12 +00001138 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001139 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001140 try {
1141 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001142 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001143 } catch (rdr::Exception e) {
1144 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1145 delete player;
1146 return 0;
1147 }
1148
1149 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001150 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001151 MSG msg;
1152 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001153 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1154 TranslateMessage(&msg);
1155 DispatchMessage(&msg);
1156 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001157 }
1158
george82e6883de2005-02-08 14:42:12 +00001159 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001160 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001161 if (player) delete player;
1162 } catch (rdr::Exception e) {
1163 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1164 }
1165
1166 return 0;
1167};