blob: 89e9d5dcf8d17347eb25ef3548a78355483368dd [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;
george825c13c662005-01-27 14:48:23 +0000311 case ID_PLAY:
312 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000313 break;
314 case ID_PAUSE:
315 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000316 break;
317 case ID_STOP:
318 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000319 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000320 }
george825c13c662005-01-27 14:48:23 +0000321 break;
322 case ID_PLAYPAUSE:
323 if (isPaused()) {
324 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000325 } else {
326 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000327 }
george825c13c662005-01-27 14:48:23 +0000328 break;
george827549df42005-02-08 16:31:02 +0000329 case ID_GOTO:
330 {
331 GotoPosDialog gotoPosDlg;
332 if (gotoPosDlg.showDialog()) {
333 setPos(gotoPosDlg.getPos());
334 updatePos(getTimeOffset());
335 }
336 }
337 break;
george825c13c662005-01-27 14:48:23 +0000338 case ID_FULLSCREEN:
339 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
340 break;
george8231a36332005-02-06 17:27:34 +0000341 case ID_LOOP:
342 loopPlayback = !loopPlayback;
343 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
344 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
345 break;
george824ea27f62005-01-29 15:03:06 +0000346 case ID_RETURN:
347 // Update the speed if return pressed in speedEdit
348 if (speedEdit == GetFocus()) {
349 char speedStr[20], *stopStr;
350 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
351 double speed = strtod(speedStr, &stopStr);
352 if (speed > 0) {
353 speed = min(MAX_SPEED, speed);
354 // Update speedUpDown position
355 SendMessage(speedUpDown, UDM_SETPOS,
356 0, MAKELONG((short)(speed / 0.5), 0));
357 } else {
358 speed = getSpeed();
359 }
360 setSpeed(speed);
361 sprintf(speedStr, "%.2f", speed);
362 SetWindowText(speedEdit, speedStr);
363 }
364 break;
george8201aa6732005-02-06 17:13:03 +0000365 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000366 PostQuitMessage(0);
367 break;
george82ef5f7262005-02-08 15:09:26 +0000368 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000369 MessageBox(getMainHandle(),
370 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
371 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000372 }
373 break;
374
375 // Update frame's window size and add scrollbars if required
376
377 case WM_SIZE:
378 {
george82d070c692005-01-19 16:44:04 +0000379
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000380 Point old_offset = bufferToClient(Point(0, 0));
381
382 // Update the cached sizing information
383 RECT r;
384 GetClientRect(getMainHandle(), &r);
385 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
386 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
387
388 GetWindowRect(getFrameHandle(), &r);
389 window_size = Rect(r.left, r.top, r.right, r.bottom);
390 GetClientRect(getFrameHandle(), &r);
391 client_size = Rect(r.left, r.top, r.right, r.bottom);
392
393 // Determine whether scrollbars are required
394 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000395
396 // Resize the ToolBar
397 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000398
399 // Redraw if required
400 if (!old_offset.equals(bufferToClient(Point(0, 0))))
401 InvalidateRect(getFrameHandle(), 0, TRUE);
402 }
403 break;
george828a471482005-02-06 07:15:53 +0000404
405 // Process messages from posTrackBar
406
407 case WM_HSCROLL:
408 {
409 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
410 Pos *= sliderStepMs;
411
412 switch (LOWORD(wParam)) {
413 case TB_PAGEUP:
414 case TB_PAGEDOWN:
415 case TB_LINEUP:
416 case TB_LINEDOWN:
417 case TB_THUMBTRACK:
418 sliderDraging = true;
419 updatePos(Pos);
420 return 0;
421 case TB_ENDTRACK:
422 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000423 sliderDraging = false;
424 return 0;
425 default:
426 break;
427 }
428 }
429 break;
george829e6e6cc2005-01-29 13:12:05 +0000430
431 case WM_NOTIFY:
432 switch (((NMHDR*)lParam)->code) {
433 case UDN_DELTAPOS:
434 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000435 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000436 char speedStr[20] = "\0";
437 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
438 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
439 double speed;
440
george824ea27f62005-01-29 15:03:06 +0000441 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000442 if (upDown->iDelta > 0) {
443 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
444 } else {
george824ea27f62005-01-29 15:03:06 +0000445 // It's need to round the UpDown position
446 if ((upDown->iPos * 0.5) != getSpeed()) {
447 upDown->iDelta = 0;
448 lResult = TRUE;
449 }
george829e6e6cc2005-01-29 13:12:05 +0000450 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
451 }
452 _gcvt(speed, 5, speedStr);
453 sprintf(speedStr, "%.2f", speed);
454 SetWindowText(speedEdit, speedStr);
455 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000456 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000457 }
george824ea27f62005-01-29 15:03:06 +0000458 }
george829e6e6cc2005-01-29 13:12:05 +0000459 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000460
461 case WM_CLOSE:
462 vlog.debug("WM_CLOSE %x", getMainHandle());
463 PostQuitMessage(0);
464 break;
465 }
466
467 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
468}
469
470LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
471 switch (msg) {
472
473 case WM_PAINT:
474 {
george825beb62a2005-02-09 13:04:32 +0000475 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000476 seekMode = true;
477 return 0;
478 } else {
479 if (seekMode) {
480 seekMode = false;
481 InvalidateRect(getFrameHandle(), 0, true);
482 UpdateWindow(getFrameHandle());
483 return 0;
484 }
485 }
486
487 PAINTSTRUCT ps;
488 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
489 if (!paintDC)
490 throw SystemException("unable to BeginPaint", GetLastError());
491 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
492
493 if (!pr.is_empty()) {
494
495 if (buffer->bitmap) {
496
497 // Get device context
498 BitmapDC bitmapDC(paintDC, buffer->bitmap);
499
500 // Blit the border if required
501 Rect bufpos = bufferToClient(buffer->getRect());
502 if (!pr.enclosed_by(bufpos)) {
503 vlog.debug("draw border");
504 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
505 RECT r;
506 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
507 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
508 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
509 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
510 }
511
512 // Do the blit
513 Point buf_pos = clientToBuffer(pr.tl);
514 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
515 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
516 throw SystemException("unable to BitBlt to window", GetLastError());
517
518 } else {
519 // Blit a load of black
520 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
521 0, 0, 0, BLACKNESS))
522 throw SystemException("unable to BitBlt to blank window", GetLastError());
523 }
524 }
525 EndPaint(getFrameHandle(), &ps);
526 }
527 return 0;
528
529 case WM_VSCROLL:
530 case WM_HSCROLL:
531 {
532 Point delta;
533 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
534
535 switch (LOWORD(wParam)) {
536 case SB_PAGEUP: newpos -= 50; break;
537 case SB_PAGEDOWN: newpos += 50; break;
538 case SB_LINEUP: newpos -= 5; break;
539 case SB_LINEDOWN: newpos += 5; break;
540 case SB_THUMBTRACK:
541 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
542 default: vlog.info("received unknown scroll message");
543 };
544
545 if (msg == WM_HSCROLL)
546 setViewportOffset(Point(newpos, scrolloffset.y));
547 else
548 setViewportOffset(Point(scrolloffset.x, newpos));
549
550 SCROLLINFO si;
551 si.cbSize = sizeof(si);
552 si.fMask = SIF_POS;
553 si.nPos = newpos;
554 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
555 }
556 break;
557 }
558
559 return DefWindowProc(hwnd, msg, wParam, lParam);
560}
561
562void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000563 bool _autoplay = false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000564 autoplay = _autoplay;
565 playbackSpeed = _playbackSpeed;
566 initTime = _initTime;
567}
568
569void RfbPlayer::applyOptions() {
570 if (initTime >= 0)
571 setPos(initTime);
572 setSpeed(playbackSpeed);
573 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000574}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000575
george82d070c692005-01-19 16:44:04 +0000576void RfbPlayer::createToolBar(HWND parentHwnd) {
577 RECT tRect;
578 InitCommonControls();
579
580 tb.create(ID_TOOLBAR, parentHwnd);
581 tb.addBitmap(4, IDB_TOOLBAR);
582
583 // Create the control buttons
584 tb.addButton(0, ID_PLAY);
585 tb.addButton(1, ID_PAUSE);
586 tb.addButton(2, ID_STOP);
587 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
588 tb.addButton(3, ID_FULLSCREEN);
589 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
590
591 // Create the static control for the time output
592 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
593 tb.getButtonRect(6, &tRect);
594 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
595 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
596 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
597 GetModuleHandle(0), 0);
598 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
599
600 // Create the trackbar control for the time position
601 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
602 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000603 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000604 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
605 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
606 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
607 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000608 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000609 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
610
611 // Create the label with "Speed:" caption
612 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
613 tb.getButtonRect(10, &tRect);
614 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
615 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
616 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
617
618 // Create the edit control and the spin for the speed managing
619 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
620 tb.getButtonRect(11, &tRect);
621 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
622 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
623 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
624 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
625 // It's need to send notify messages to toolbar parent window
626 SetParent(speedEdit, tb.getHandle());
627
628 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
629 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000630 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000631}
632
george82a21d2952005-02-12 11:30:03 +0000633void RfbPlayer::disableTBandMenuItems() {
634 // Disable the menu items
635 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
636 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
637 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
638 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
640 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
641 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
642 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
643 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
644
645 // Disable the toolbar buttons and child controls
646 tb.enableButton(ID_PLAY, false);
647 tb.enableButton(ID_PAUSE, false);
648 tb.enableButton(ID_STOP, false);
649 tb.enableButton(ID_FULLSCREEN, false);
650 EnableWindow(posTrackBar, false);
651 EnableWindow(speedEdit, false);
652}
653
george82f5043162005-02-12 11:37:18 +0000654void RfbPlayer::enableTBandMenuItems() {
655 // Enable the menu items
656 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
657 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
658 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
659 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
660 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
661 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
662 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
663 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
664 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
665
666 // Enable the toolbar buttons and child controls
667 tb.enableButton(ID_PLAY, true);
668 tb.enableButton(ID_PAUSE, true);
669 tb.enableButton(ID_STOP, true);
670 tb.enableButton(ID_FULLSCREEN, true);
671 EnableWindow(posTrackBar, true);
672 EnableWindow(speedEdit, true);
673}
674
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000675void RfbPlayer::setVisible(bool visible) {
676 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
677 if (visible) {
678 // When the window becomes visible, make it active
679 SetForegroundWindow(getMainHandle());
680 SetActiveWindow(getMainHandle());
681 }
682}
683
684void RfbPlayer::setTitle(const char *title) {
685 char _title[256];
686 strcpy(_title, AppName);
687 strcat(_title, " - ");
688 strcat(_title, title);
689 SetWindowText(getMainHandle(), _title);
690}
691
692void RfbPlayer::setFrameSize(int width, int height) {
693 // Calculate and set required size for main window
694 RECT r = {0, 0, width, height};
695 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
696 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
697 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
698 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
699 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
700 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
701
702 // Enable/disable scrollbars as appropriate
703 calculateScrollBars();
704}
705
706void RfbPlayer::calculateScrollBars() {
707 // Calculate the required size of window
708 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
709 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
710 DWORD old_style;
711 RECT r;
712 SetRect(&r, 0, 0, buffer->width(), buffer->height());
713 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
714 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
715
716 // Work out whether scroll bars are required
717 do {
718 old_style = style;
719
720 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
721 style |= WS_HSCROLL;
722 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
723 }
724 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
725 style |= WS_VSCROLL;
726 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
727 }
728 } while (style != old_style);
729
730 // Tell Windows to update the window style & cached settings
731 if (style != current_style) {
732 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
733 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
734 }
735
736 // Update the scroll settings
737 SCROLLINFO si;
738 if (style & WS_VSCROLL) {
739 si.cbSize = sizeof(si);
740 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
741 si.nMin = 0;
742 si.nMax = buffer->height();
743 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
744 maxscrolloffset.y = max(0, si.nMax-si.nPage);
745 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
746 si.nPos = scrolloffset.y;
747 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
748 }
749 if (style & WS_HSCROLL) {
750 si.cbSize = sizeof(si);
751 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
752 si.nMin = 0;
753 si.nMax = buffer->width();
754 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
755 maxscrolloffset.x = max(0, si.nMax-si.nPage);
756 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
757 si.nPos = scrolloffset.x;
758 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
759 }
760}
761
762bool RfbPlayer::setViewportOffset(const Point& tl) {
763/* ***
764 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
765 max(0, min(maxscrolloffset.y, tl.y)));
766 */
767 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
768 max(0, min(tl.y, buffer->height()-client_size.height())));
769 Point delta = np.translate(scrolloffset.negate());
770 if (!np.equals(scrolloffset)) {
771 scrolloffset = np;
772 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
773 UpdateWindow(getFrameHandle());
774 return true;
775 }
776 return false;
777}
778
779void RfbPlayer::close(const char* reason) {
780 setVisible(false);
781 if (reason) {
782 vlog.info("closing - %s", reason);
783 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
784 }
785 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
786}
787
788void RfbPlayer::blankBuffer() {
789 fillRect(buffer->getRect(), 0);
790}
791
792void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000793 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000794 blankBuffer();
795 newSession(fileName);
796 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000797 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000798 if (paused) is->pausePlayback();
799 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000800}
801
802void RfbPlayer::processMsg() {
803 static long update_time = GetTickCount();
804 try {
george828a471482005-02-06 07:15:53 +0000805 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
806 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000807 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000808 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000809 update_time = GetTickCount();
810 }
811 RfbProto::processMsg();
812 } catch (rdr::Exception e) {
813 if (strcmp(e.str(), "[End Of File]") == 0) {
814 rewind();
george8231a36332005-02-06 17:27:34 +0000815 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000816 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000817 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000818 return;
819 }
820 // It's a special exception to perform backward seeking.
821 // We only rewind the stream and seek the offset
822 if (strcmp(e.str(), "[REWIND]") == 0) {
823 long initTime = getSeekOffset();
824 rewind();
825 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000826 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000827 } else {
828 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
829 return;
830 }
831 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000832}
833
834void RfbPlayer::serverInit() {
835 RfbProto::serverInit();
836
837 // Save the server init time for using in setPos()
838 serverInitTime = getTimeOffset() / getSpeed();
839
840 // Resize the backing buffer
841 buffer->setSize(cp.width, cp.height);
842
843 // Check on the true colour mode
844 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000845 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000846
847 // Set the session pixel format
848 buffer->setPF(cp.pf());
849
850 // If the window is not maximised then resize it
851 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
852 setFrameSize(cp.width, cp.height);
853
854 // Set the window title and show it
855 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000856
george82d4d69e62005-02-05 09:23:18 +0000857 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000858 sessionTimeMs = calculateSessionTime(fileName);
859 sprintf(fullSessionTime, "%.2um:%.2us",
860 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000861 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000862 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
863 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000864 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000865
george82006f2792005-02-05 07:40:47 +0000866 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000867}
868
869void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
870 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
871 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
872/* int i;
873 for (i=0;i<count;i++) {
874 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
875 }
876 // *** change to 0, 256?
877 refreshWindowPalette(first, count);
878 palette_changed = true;
879 InvalidateRect(getFrameHandle(), 0, FALSE);*/
880}
881
882void RfbPlayer::bell() {
883 if (acceptBell)
884 MessageBeep(-1);
885}
886
887void RfbPlayer::serverCutText(const char* str, int len) {
888 if (cutText != NULL)
889 delete [] cutText;
890 cutText = new char[len + 1];
891 memcpy(cutText, str, len);
892 cutText[len] = '\0';
893}
894
895void RfbPlayer::frameBufferUpdateEnd() {
896};
897
898void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
899}
900
901void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
902}
903
904
905void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
906 buffer->fillRect(r, pix);
907 invalidateBufferRect(r);
908}
909
910void RfbPlayer::imageRect(const Rect& r, void* pixels) {
911 buffer->imageRect(r, pixels);
912 invalidateBufferRect(r);
913}
914
915void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
916 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
917 invalidateBufferRect(r);
918}
919
920bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
921 Rect rect = bufferToClient(crect);
922 if (rect.intersect(client_size).is_empty()) return false;
923 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
924 InvalidateRect(getFrameHandle(), &invalid, FALSE);
925 return true;
926}
927
george8257f13522005-02-05 08:48:22 +0000928long RfbPlayer::calculateSessionTime(char *filename) {
929 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000930 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000931 try {
932 while (TRUE) {
933 sessionFile.skip(1024);
934 }
935 } catch (rdr::Exception e) {
936 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000937 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000938 } else {
939 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
940 return 0;
941 }
942 }
943 return 0;
944}
945
george8217e92cb2005-01-31 16:01:02 +0000946void RfbPlayer::openSessionFile(char *_fileName) {
947 fileName = strDup(_fileName);
948
949 // Close the previous reading thread
950 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000951 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000952 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000953 }
954 blankBuffer();
955 newSession(fileName);
956 setSpeed(playbackSpeed);
957 rfbReader = new rfbSessionReader(this);
958 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000959 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +0000960 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000961}
962
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000963void RfbPlayer::setPaused(bool paused) {
964 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000965 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000966 tb.checkButton(ID_PAUSE, true);
967 tb.checkButton(ID_PLAY, false);
968 tb.checkButton(ID_STOP, false);
969 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
970 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000971 } else {
george825beb62a2005-02-09 13:04:32 +0000972 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000973 tb.checkButton(ID_PLAY, true);
974 tb.checkButton(ID_STOP, false);
975 tb.checkButton(ID_PAUSE, false);
976 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
977 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000978 }
979}
980
george82006f2792005-02-05 07:40:47 +0000981void RfbPlayer::stopPlayback() {
982 setPos(0);
george825beb62a2005-02-09 13:04:32 +0000983 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000984 tb.checkButton(ID_STOP, true);
985 tb.checkButton(ID_PLAY, false);
986 tb.checkButton(ID_PAUSE, false);
987 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
988 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000989 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000990}
991
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000992void RfbPlayer::setSpeed(double speed) {
993 serverInitTime = serverInitTime * getSpeed() / speed;
994 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000995 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000996}
997
998double RfbPlayer::getSpeed() {
999 return is->getSpeed();
1000}
1001
1002void RfbPlayer::setPos(long pos) {
1003 is->setTimeOffset(max(pos, serverInitTime));
1004}
1005
1006long RfbPlayer::getSeekOffset() {
1007 return is->getSeekOffset();
1008}
1009
1010bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001011 if (is) return is->isSeeking();
1012 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001013}
1014
1015bool RfbPlayer::isSeekMode() {
1016 return seekMode;
1017}
1018
1019bool RfbPlayer::isPaused() {
1020 return is->isPaused();
1021}
1022
1023long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001024 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001025}
1026
george828a471482005-02-06 07:15:53 +00001027void RfbPlayer::updatePos(long newPos) {
1028 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001029 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +00001030 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +00001031 newPos /= 1000;
george8244325492005-02-06 07:29:51 +00001032 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001033 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001034
1035 // Update the position of slider
1036 if (!sliderDraging) {
1037 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +00001038 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
1039 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +00001040 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001041}
1042
1043void RfbPlayer::skipHandshaking() {
1044 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1045 is->skip(skipBytes);
1046 state_ = RFBSTATE_NORMAL;
1047}
1048
1049void programInfo() {
1050 win32::FileVersionInfo inf;
1051 _tprintf(_T("%s - %s, Version %s\n"),
1052 inf.getVerString(_T("ProductName")),
1053 inf.getVerString(_T("FileDescription")),
1054 inf.getVerString(_T("FileVersion")));
1055 printf("%s\n", buildTime);
1056 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1057}
1058
1059void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001060 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001061}
1062
1063double playbackSpeed = 1.0;
1064long initTime = -1;
1065bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001066char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001067bool print_usage = false;
1068bool acceptBell = false;
1069
1070bool processParams(int argc, char* argv[]) {
1071 for (int i = 1; i < argc; i++) {
1072 if ((strcasecmp(argv[i], "-help") == 0) ||
1073 (strcasecmp(argv[i], "--help") == 0) ||
1074 (strcasecmp(argv[i], "/help") == 0) ||
1075 (strcasecmp(argv[i], "-h") == 0) ||
1076 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001077 (strcasecmp(argv[i], "/?") == 0) ||
1078 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001079 print_usage = true;
1080 return true;
1081 }
1082
1083 if ((strcasecmp(argv[i], "-speed") == 0) ||
1084 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1085 playbackSpeed = atof(argv[++i]);
1086 if (playbackSpeed <= 0) {
1087 return false;
1088 }
1089 continue;
1090 }
1091
1092 if ((strcasecmp(argv[i], "-pos") == 0) ||
1093 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1094 initTime = atol(argv[++i]);
1095 if (initTime <= 0)
1096 return false;
1097 continue;
1098 }
1099
1100 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1101 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001102 autoplay = true;
1103 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001104 }
1105
1106 if ((strcasecmp(argv[i], "-bell") == 0) ||
1107 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001108 acceptBell = true;
1109 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001110 }
1111
1112 if (i != argc - 1)
1113 return false;
1114 }
1115
1116 fileName = strDup(argv[argc-1]);
1117 return true;
1118}
1119
1120//
1121// -=- WinMain
1122//
1123
1124int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1125
1126 // - Process the command-line
1127
1128 int argc = __argc;
1129 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001130 if ((argc > 1) && (!processParams(argc, argv))) {
1131 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1132 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001133 }
george82e6883de2005-02-08 14:42:12 +00001134
1135 if (print_usage) {
1136 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137 return 0;
george8267cbcd02005-01-16 15:39:56 +00001138 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001139
george82e6883de2005-02-08 14:42:12 +00001140 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001141 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001142 try {
1143 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001144 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001145 } catch (rdr::Exception e) {
1146 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1147 delete player;
1148 return 0;
1149 }
1150
1151 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001152 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001153 MSG msg;
1154 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001155 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1156 TranslateMessage(&msg);
1157 DispatchMessage(&msg);
1158 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001159 }
1160
george82e6883de2005-02-08 14:42:12 +00001161 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001162 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001163 if (player) delete player;
1164 } catch (rdr::Exception e) {
1165 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1166 }
1167
1168 return 0;
1169};