blob: f41a9ab48f58e2a3c511e09a33225d29a3aaa666 [file] [log] [blame]
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001/* Copyright (C) 2004 TightVNC Team. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- RFB Player for Win32
20
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000021#include <rfb/LogWriter.h>
22#include <rfb/Exception.h>
23#include <rfb/Threading.h>
24
25#include <rfb_win32/Win32Util.h>
26#include <rfb_win32/WMShatter.h>
27
28#include <rfbplayer/rfbplayer.h>
29#include <rfbplayer/utils.h>
30#include <rfbplayer/resource.h>
george827549df42005-02-08 16:31:02 +000031#include <rfbplayer/GotoPosDialog.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000032
33using namespace rfb;
34using namespace rfb::win32;
35
36// -=- Variables & consts
37
38static LogWriter vlog("RfbPlayer");
39
40TStr rfb::win32::AppName("RfbPlayer");
41extern const char* buildTime;
42
george82e6883de2005-02-08 14:42:12 +000043char wrong_cmd_msg[] =
44 "Wrong command-line parameters!\n"
45 "Use for help: rfbplayer -help";
46
47char usage_msg[] =
48 "usage: rfbplayer <options> <filename>\n"
49 "Command-line options:\n"
50 " -help \t- Provide usage information.\n"
51 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
52 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
53 " -pos <ms> \t- Sets initial time position in the session file,\n"
54 " \t in milliseconds. Default: 0.\n"
55 " -autoplay \t- Runs the player in the playback mode.\n"
56 " -bell \t- Accepts the bell.\n";
57
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000058// -=- RfbPlayer's defines
59
60#define strcasecmp _stricmp
george824ea27f62005-01-29 15:03:06 +000061#define MAX_SPEED 10
george82d4d69e62005-02-05 09:23:18 +000062#define MAX_POS_TRACKBAR_RANGE 50
george8268d25142005-02-13 09:33:22 +000063#define DEFAULT_PLAYER_WIDTH 640
64#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000065
george82d070c692005-01-19 16:44:04 +000066#define ID_TOOLBAR 500
67#define ID_PLAY 510
68#define ID_PAUSE 520
69#define ID_TIME_STATIC 530
70#define ID_SPEED_STATIC 540
71#define ID_SPEED_EDIT 550
72#define ID_POS_TRACKBAR 560
73#define ID_SPEED_UPDOWN 570
74
75
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000076//
77// -=- RfbPlayerClass
78
79//
80// Window class used as the basis for RfbPlayer instance
81//
82
83class RfbPlayerClass {
84public:
85 RfbPlayerClass();
86 ~RfbPlayerClass();
87 ATOM classAtom;
88 HINSTANCE instance;
89};
90
91LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
92 LRESULT result;
93
94 if (msg == WM_CREATE)
95 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
96 else if (msg == WM_DESTROY) {
97 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000098 SetWindowLong(hwnd, GWL_USERDATA, 0);
99 }
100 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
101 if (!_this) {
102 vlog.info("null _this in %x, message %u", hwnd, msg);
103 return DefWindowProc(hwnd, msg, wParam, lParam);
104 }
105
106 try {
107 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
108 } catch (rdr::Exception& e) {
109 vlog.error("untrapped: %s", e.str());
110 }
111
112 return result;
113};
114
115RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
116 WNDCLASS wndClass;
117 wndClass.style = 0;
118 wndClass.lpfnWndProc = RfbPlayerProc;
119 wndClass.cbClsExtra = 0;
120 wndClass.cbWndExtra = 0;
121 wndClass.hInstance = instance = GetModuleHandle(0);
122 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000123 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000124 if (!wndClass.hIcon)
125 printf("unable to load icon:%ld", GetLastError());
126 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
127 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000128 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000129 wndClass.lpszClassName = _T("RfbPlayerClass");
130 classAtom = RegisterClass(&wndClass);
131 if (!classAtom) {
132 throw rdr::SystemException("unable to register RfbPlayer window class",
133 GetLastError());
134 }
135}
136
137RfbPlayerClass::~RfbPlayerClass() {
138 if (classAtom) {
139 UnregisterClass((const TCHAR*)classAtom, instance);
140 }
141}
142
143RfbPlayerClass baseClass;
144
145//
146// -=- RfbFrameClass
147
148//
149// Window class used to displaying the rfb data
150//
151
152class RfbFrameClass {
153public:
154 RfbFrameClass();
155 ~RfbFrameClass();
156 ATOM classAtom;
157 HINSTANCE instance;
158};
159
160LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
161 LRESULT result;
162
163 if (msg == WM_CREATE)
164 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
165 else if (msg == WM_DESTROY)
166 SetWindowLong(hwnd, GWL_USERDATA, 0);
167 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
168 if (!_this) {
169 vlog.info("null _this in %x, message %u", hwnd, msg);
170 return DefWindowProc(hwnd, msg, wParam, lParam);
171 }
172
173 try {
174 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
175 } catch (rdr::Exception& e) {
176 vlog.error("untrapped: %s", e.str());
177 }
178
179 return result;
180}
181
182RfbFrameClass::RfbFrameClass() : classAtom(0) {
183 WNDCLASS wndClass;
184 wndClass.style = 0;
185 wndClass.lpfnWndProc = FrameProc;
186 wndClass.cbClsExtra = 0;
187 wndClass.cbWndExtra = 0;
188 wndClass.hInstance = instance = GetModuleHandle(0);
189 wndClass.hIcon = 0;
190 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
191 wndClass.hbrBackground = 0;
192 wndClass.lpszMenuName = 0;
193 wndClass.lpszClassName = _T("RfbPlayerClass1");
194 classAtom = RegisterClass(&wndClass);
195 if (!classAtom) {
196 throw rdr::SystemException("unable to register RfbPlayer window class",
197 GetLastError());
198 }
199}
200
201RfbFrameClass::~RfbFrameClass() {
202 if (classAtom) {
203 UnregisterClass((const TCHAR*)classAtom, instance);
204 }
205}
206
207RfbFrameClass frameClass;
208
209//
210// -=- RfbPlayer instance implementation
211//
212
213RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000214 bool _autoplay = false, bool _acceptBell = false)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000215: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
george82e6883de2005-02-08 14:42:12 +0000216 autoplay(_autoplay), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000217 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000218 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000219 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000220 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000221
george82e6883de2005-02-08 14:42:12 +0000222 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000223
george823c8fbbf2005-01-24 11:09:08 +0000224 // Reset the full session time
225 strcpy(fullSessionTime, "00m:00s");
226
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000227 // Create the main window
228 const TCHAR* name = _T("RfbPlayer");
229 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8268d25142005-02-13 09:33:22 +0000230 0, 0, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000231 if (!mainHwnd) {
232 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
233 }
234 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
235
236 // Create the backing buffer
237 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000238 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000239
george8217e92cb2005-01-31 16:01:02 +0000240 // Open the session file
241 if (fileName) {
242 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000243 if (initTime > 0) setPos(initTime);
244 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000245 } else {
246 disableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000247 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000248}
249
250RfbPlayer::~RfbPlayer() {
251 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000252 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000253 delete rfbReader->join();
254 rfbReader = 0;
255 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000256 if (mainHwnd) {
257 setVisible(false);
258 DestroyWindow(mainHwnd);
259 mainHwnd = 0;
260 }
george825beb62a2005-02-09 13:04:32 +0000261 if (buffer) delete buffer;
262 if (cutText) delete [] cutText;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000263 vlog.debug("~RfbPlayer done");
264}
265
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000266LRESULT
267RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
268 switch (msg) {
269
270 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000271
272 case WM_CREATE:
273 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000274 // Create the frame window
275 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
276 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
277 hwnd, 0, frameClass.instance, this);
278
george82d070c692005-01-19 16:44:04 +0000279 createToolBar(hwnd);
280
george82006f2792005-02-05 07:40:47 +0000281 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000282
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000283 return 0;
284 }
285
george827214b822004-12-12 07:02:51 +0000286 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000287
288 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000289 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000290 case ID_OPENFILE:
291 {
292 char curDir[_MAX_DIR];
293 static char filename[_MAX_PATH];
294 OPENFILENAME ofn;
295 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
296 GetCurrentDirectory(sizeof(curDir), curDir);
297
298 ofn.lStructSize = sizeof(OPENFILENAME);
299 ofn.hwndOwner = getMainHandle();
300 ofn.lpstrFile = filename;
301 ofn.nMaxFile = sizeof(filename);
302 ofn.lpstrInitialDir = curDir;
303 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
304 "All files (*.*)\0*.*\0";
305 ofn.lpstrDefExt = "rfb";
306 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
307 if (GetOpenFileName(&ofn))
308 openSessionFile(filename);
309 }
310 break;
george8271ca1772005-02-13 10:50:46 +0000311 case ID_CLOSEFILE:
312 closeSessionFile();
313 break;
george825c13c662005-01-27 14:48:23 +0000314 case ID_PLAY:
315 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000316 break;
317 case ID_PAUSE:
318 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000319 break;
320 case ID_STOP:
321 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000322 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000323 }
george825c13c662005-01-27 14:48:23 +0000324 break;
325 case ID_PLAYPAUSE:
326 if (isPaused()) {
327 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000328 } else {
329 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000330 }
george825c13c662005-01-27 14:48:23 +0000331 break;
george827549df42005-02-08 16:31:02 +0000332 case ID_GOTO:
333 {
334 GotoPosDialog gotoPosDlg;
335 if (gotoPosDlg.showDialog()) {
336 setPos(gotoPosDlg.getPos());
337 updatePos(getTimeOffset());
338 }
339 }
340 break;
george825c13c662005-01-27 14:48:23 +0000341 case ID_FULLSCREEN:
342 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
343 break;
george8231a36332005-02-06 17:27:34 +0000344 case ID_LOOP:
345 loopPlayback = !loopPlayback;
346 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
347 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
348 break;
george824ea27f62005-01-29 15:03:06 +0000349 case ID_RETURN:
350 // Update the speed if return pressed in speedEdit
351 if (speedEdit == GetFocus()) {
352 char speedStr[20], *stopStr;
353 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
354 double speed = strtod(speedStr, &stopStr);
355 if (speed > 0) {
356 speed = min(MAX_SPEED, speed);
357 // Update speedUpDown position
358 SendMessage(speedUpDown, UDM_SETPOS,
359 0, MAKELONG((short)(speed / 0.5), 0));
360 } else {
361 speed = getSpeed();
362 }
363 setSpeed(speed);
364 sprintf(speedStr, "%.2f", speed);
365 SetWindowText(speedEdit, speedStr);
366 }
367 break;
george8201aa6732005-02-06 17:13:03 +0000368 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000369 PostQuitMessage(0);
370 break;
george82ef5f7262005-02-08 15:09:26 +0000371 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000372 MessageBox(getMainHandle(),
373 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
374 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000375 }
376 break;
377
378 // Update frame's window size and add scrollbars if required
379
380 case WM_SIZE:
381 {
george82d070c692005-01-19 16:44:04 +0000382
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000383 Point old_offset = bufferToClient(Point(0, 0));
384
385 // Update the cached sizing information
386 RECT r;
387 GetClientRect(getMainHandle(), &r);
388 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
389 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
390
391 GetWindowRect(getFrameHandle(), &r);
392 window_size = Rect(r.left, r.top, r.right, r.bottom);
393 GetClientRect(getFrameHandle(), &r);
394 client_size = Rect(r.left, r.top, r.right, r.bottom);
395
396 // Determine whether scrollbars are required
397 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000398
399 // Resize the ToolBar
400 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000401
402 // Redraw if required
403 if (!old_offset.equals(bufferToClient(Point(0, 0))))
404 InvalidateRect(getFrameHandle(), 0, TRUE);
405 }
406 break;
george828a471482005-02-06 07:15:53 +0000407
408 // Process messages from posTrackBar
409
410 case WM_HSCROLL:
411 {
412 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
413 Pos *= sliderStepMs;
414
415 switch (LOWORD(wParam)) {
416 case TB_PAGEUP:
417 case TB_PAGEDOWN:
418 case TB_LINEUP:
419 case TB_LINEDOWN:
420 case TB_THUMBTRACK:
421 sliderDraging = true;
422 updatePos(Pos);
423 return 0;
424 case TB_ENDTRACK:
425 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000426 sliderDraging = false;
427 return 0;
428 default:
429 break;
430 }
431 }
432 break;
george829e6e6cc2005-01-29 13:12:05 +0000433
434 case WM_NOTIFY:
435 switch (((NMHDR*)lParam)->code) {
436 case UDN_DELTAPOS:
437 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000438 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000439 char speedStr[20] = "\0";
440 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
441 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
442 double speed;
443
george824ea27f62005-01-29 15:03:06 +0000444 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000445 if (upDown->iDelta > 0) {
446 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
447 } else {
george824ea27f62005-01-29 15:03:06 +0000448 // It's need to round the UpDown position
449 if ((upDown->iPos * 0.5) != getSpeed()) {
450 upDown->iDelta = 0;
451 lResult = TRUE;
452 }
george829e6e6cc2005-01-29 13:12:05 +0000453 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
454 }
455 _gcvt(speed, 5, speedStr);
456 sprintf(speedStr, "%.2f", speed);
457 SetWindowText(speedEdit, speedStr);
458 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000459 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000460 }
george824ea27f62005-01-29 15:03:06 +0000461 }
george829e6e6cc2005-01-29 13:12:05 +0000462 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000463
464 case WM_CLOSE:
465 vlog.debug("WM_CLOSE %x", getMainHandle());
466 PostQuitMessage(0);
467 break;
468 }
469
470 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
471}
472
473LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
474 switch (msg) {
475
476 case WM_PAINT:
477 {
george825beb62a2005-02-09 13:04:32 +0000478 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000479 seekMode = true;
480 return 0;
481 } else {
482 if (seekMode) {
483 seekMode = false;
484 InvalidateRect(getFrameHandle(), 0, true);
485 UpdateWindow(getFrameHandle());
486 return 0;
487 }
488 }
489
490 PAINTSTRUCT ps;
491 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
492 if (!paintDC)
493 throw SystemException("unable to BeginPaint", GetLastError());
494 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
495
496 if (!pr.is_empty()) {
497
498 if (buffer->bitmap) {
499
500 // Get device context
501 BitmapDC bitmapDC(paintDC, buffer->bitmap);
502
503 // Blit the border if required
504 Rect bufpos = bufferToClient(buffer->getRect());
505 if (!pr.enclosed_by(bufpos)) {
506 vlog.debug("draw border");
507 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
508 RECT r;
509 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
510 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
511 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
512 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
513 }
514
515 // Do the blit
516 Point buf_pos = clientToBuffer(pr.tl);
517 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
518 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
519 throw SystemException("unable to BitBlt to window", GetLastError());
520
521 } else {
522 // Blit a load of black
523 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
524 0, 0, 0, BLACKNESS))
525 throw SystemException("unable to BitBlt to blank window", GetLastError());
526 }
527 }
528 EndPaint(getFrameHandle(), &ps);
529 }
530 return 0;
531
532 case WM_VSCROLL:
533 case WM_HSCROLL:
534 {
535 Point delta;
536 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
537
538 switch (LOWORD(wParam)) {
539 case SB_PAGEUP: newpos -= 50; break;
540 case SB_PAGEDOWN: newpos += 50; break;
541 case SB_LINEUP: newpos -= 5; break;
542 case SB_LINEDOWN: newpos += 5; break;
543 case SB_THUMBTRACK:
544 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
545 default: vlog.info("received unknown scroll message");
546 };
547
548 if (msg == WM_HSCROLL)
549 setViewportOffset(Point(newpos, scrolloffset.y));
550 else
551 setViewportOffset(Point(scrolloffset.x, newpos));
552
553 SCROLLINFO si;
554 si.cbSize = sizeof(si);
555 si.fMask = SIF_POS;
556 si.nPos = newpos;
557 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
558 }
559 break;
560 }
561
562 return DefWindowProc(hwnd, msg, wParam, lParam);
563}
564
george82d070c692005-01-19 16:44:04 +0000565void RfbPlayer::createToolBar(HWND parentHwnd) {
566 RECT tRect;
567 InitCommonControls();
568
569 tb.create(ID_TOOLBAR, parentHwnd);
570 tb.addBitmap(4, IDB_TOOLBAR);
571
572 // Create the control buttons
573 tb.addButton(0, ID_PLAY);
574 tb.addButton(1, ID_PAUSE);
575 tb.addButton(2, ID_STOP);
576 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
577 tb.addButton(3, ID_FULLSCREEN);
578 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
579
580 // Create the static control for the time output
581 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
582 tb.getButtonRect(6, &tRect);
583 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
584 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
585 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
586 GetModuleHandle(0), 0);
587 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
588
589 // Create the trackbar control for the time position
590 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
591 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000592 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000593 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
594 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
595 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
596 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000597 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000598 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
599
600 // Create the label with "Speed:" caption
601 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
602 tb.getButtonRect(10, &tRect);
603 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
604 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
605 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
606
607 // Create the edit control and the spin for the speed managing
608 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
609 tb.getButtonRect(11, &tRect);
610 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
611 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
612 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
613 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
614 // It's need to send notify messages to toolbar parent window
615 SetParent(speedEdit, tb.getHandle());
616
617 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
618 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000619 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000620}
621
george82a21d2952005-02-12 11:30:03 +0000622void RfbPlayer::disableTBandMenuItems() {
623 // Disable the menu items
624 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
625 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
626 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
627 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
628 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
629 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
630 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
631 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
632 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
633
634 // Disable the toolbar buttons and child controls
635 tb.enableButton(ID_PLAY, false);
636 tb.enableButton(ID_PAUSE, false);
637 tb.enableButton(ID_STOP, false);
638 tb.enableButton(ID_FULLSCREEN, false);
639 EnableWindow(posTrackBar, false);
640 EnableWindow(speedEdit, false);
641}
642
george82f5043162005-02-12 11:37:18 +0000643void RfbPlayer::enableTBandMenuItems() {
644 // Enable the menu items
645 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
646 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
647 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
648 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
649 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
650 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
651 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
652 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
653 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
654
655 // Enable the toolbar buttons and child controls
656 tb.enableButton(ID_PLAY, true);
657 tb.enableButton(ID_PAUSE, true);
658 tb.enableButton(ID_STOP, true);
659 tb.enableButton(ID_FULLSCREEN, true);
660 EnableWindow(posTrackBar, true);
661 EnableWindow(speedEdit, true);
662}
663
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000664void RfbPlayer::setVisible(bool visible) {
665 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
666 if (visible) {
667 // When the window becomes visible, make it active
668 SetForegroundWindow(getMainHandle());
669 SetActiveWindow(getMainHandle());
670 }
671}
672
673void RfbPlayer::setTitle(const char *title) {
674 char _title[256];
675 strcpy(_title, AppName);
676 strcat(_title, " - ");
677 strcat(_title, title);
678 SetWindowText(getMainHandle(), _title);
679}
680
681void RfbPlayer::setFrameSize(int width, int height) {
682 // Calculate and set required size for main window
683 RECT r = {0, 0, width, height};
684 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
685 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
686 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
687 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
688 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
689 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
690
691 // Enable/disable scrollbars as appropriate
692 calculateScrollBars();
693}
694
695void RfbPlayer::calculateScrollBars() {
696 // Calculate the required size of window
697 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
698 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
699 DWORD old_style;
700 RECT r;
701 SetRect(&r, 0, 0, buffer->width(), buffer->height());
702 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
703 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
704
705 // Work out whether scroll bars are required
706 do {
707 old_style = style;
708
709 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
710 style |= WS_HSCROLL;
711 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
712 }
713 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
714 style |= WS_VSCROLL;
715 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
716 }
717 } while (style != old_style);
718
719 // Tell Windows to update the window style & cached settings
720 if (style != current_style) {
721 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
722 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
723 }
724
725 // Update the scroll settings
726 SCROLLINFO si;
727 if (style & WS_VSCROLL) {
728 si.cbSize = sizeof(si);
729 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
730 si.nMin = 0;
731 si.nMax = buffer->height();
732 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
733 maxscrolloffset.y = max(0, si.nMax-si.nPage);
734 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
735 si.nPos = scrolloffset.y;
736 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
737 }
738 if (style & WS_HSCROLL) {
739 si.cbSize = sizeof(si);
740 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
741 si.nMin = 0;
742 si.nMax = buffer->width();
743 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
744 maxscrolloffset.x = max(0, si.nMax-si.nPage);
745 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
746 si.nPos = scrolloffset.x;
747 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
748 }
749}
750
751bool RfbPlayer::setViewportOffset(const Point& tl) {
752/* ***
753 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
754 max(0, min(maxscrolloffset.y, tl.y)));
755 */
756 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
757 max(0, min(tl.y, buffer->height()-client_size.height())));
758 Point delta = np.translate(scrolloffset.negate());
759 if (!np.equals(scrolloffset)) {
760 scrolloffset = np;
761 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
762 UpdateWindow(getFrameHandle());
763 return true;
764 }
765 return false;
766}
767
768void RfbPlayer::close(const char* reason) {
769 setVisible(false);
770 if (reason) {
771 vlog.info("closing - %s", reason);
772 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
773 }
774 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
775}
776
777void RfbPlayer::blankBuffer() {
778 fillRect(buffer->getRect(), 0);
779}
780
781void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000782 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000783 blankBuffer();
784 newSession(fileName);
785 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000786 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000787 if (paused) is->pausePlayback();
788 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000789}
790
791void RfbPlayer::processMsg() {
792 static long update_time = GetTickCount();
793 try {
george828a471482005-02-06 07:15:53 +0000794 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
795 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000796 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000797 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000798 update_time = GetTickCount();
799 }
800 RfbProto::processMsg();
801 } catch (rdr::Exception e) {
802 if (strcmp(e.str(), "[End Of File]") == 0) {
803 rewind();
george8231a36332005-02-06 17:27:34 +0000804 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000805 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000806 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000807 return;
808 }
809 // It's a special exception to perform backward seeking.
810 // We only rewind the stream and seek the offset
811 if (strcmp(e.str(), "[REWIND]") == 0) {
812 long initTime = getSeekOffset();
813 rewind();
814 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000815 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000816 } else {
817 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
818 return;
819 }
820 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000821}
822
823void RfbPlayer::serverInit() {
824 RfbProto::serverInit();
825
826 // Save the server init time for using in setPos()
827 serverInitTime = getTimeOffset() / getSpeed();
828
829 // Resize the backing buffer
830 buffer->setSize(cp.width, cp.height);
831
832 // Check on the true colour mode
833 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000834 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000835
836 // Set the session pixel format
837 buffer->setPF(cp.pf());
838
839 // If the window is not maximised then resize it
840 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
841 setFrameSize(cp.width, cp.height);
842
843 // Set the window title and show it
844 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000845
george82d4d69e62005-02-05 09:23:18 +0000846 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000847 sessionTimeMs = calculateSessionTime(fileName);
848 sprintf(fullSessionTime, "%.2um:%.2us",
849 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000850 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000851 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
852 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000853 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000854
george82006f2792005-02-05 07:40:47 +0000855 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000856}
857
858void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
859 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
860 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
861/* int i;
862 for (i=0;i<count;i++) {
863 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
864 }
865 // *** change to 0, 256?
866 refreshWindowPalette(first, count);
867 palette_changed = true;
868 InvalidateRect(getFrameHandle(), 0, FALSE);*/
869}
870
871void RfbPlayer::bell() {
872 if (acceptBell)
873 MessageBeep(-1);
874}
875
876void RfbPlayer::serverCutText(const char* str, int len) {
877 if (cutText != NULL)
878 delete [] cutText;
879 cutText = new char[len + 1];
880 memcpy(cutText, str, len);
881 cutText[len] = '\0';
882}
883
884void RfbPlayer::frameBufferUpdateEnd() {
885};
886
887void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
888}
889
890void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
891}
892
893
894void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
895 buffer->fillRect(r, pix);
896 invalidateBufferRect(r);
897}
898
899void RfbPlayer::imageRect(const Rect& r, void* pixels) {
900 buffer->imageRect(r, pixels);
901 invalidateBufferRect(r);
902}
903
904void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
905 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
906 invalidateBufferRect(r);
907}
908
909bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
910 Rect rect = bufferToClient(crect);
911 if (rect.intersect(client_size).is_empty()) return false;
912 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
913 InvalidateRect(getFrameHandle(), &invalid, FALSE);
914 return true;
915}
916
george8257f13522005-02-05 08:48:22 +0000917long RfbPlayer::calculateSessionTime(char *filename) {
918 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000919 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000920 try {
921 while (TRUE) {
922 sessionFile.skip(1024);
923 }
924 } catch (rdr::Exception e) {
925 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000926 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000927 } else {
928 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
929 return 0;
930 }
931 }
932 return 0;
933}
934
george826b87aff2005-02-13 10:48:21 +0000935void RfbPlayer::closeSessionFile() {
936 char speedStr[10];
937 RECT r;
938
939 // Uncheck all toolbar buttons
940 if (tb.getHandle()) {
941 tb.checkButton(ID_PLAY, false);
942 tb.checkButton(ID_PAUSE, false);
943 tb.checkButton(ID_STOP, false);
944 }
945
946 // Stop playback and update the player state
947 disableTBandMenuItems();
948 if (rfbReader) {
949 delete rfbReader->join();
950 rfbReader = 0;
951 delete [] fileName;
952 fileName = 0;
953 }
954 blankBuffer();
955 setTitle("None");
956 playbackSpeed = 1.0;
957 SendMessage(speedUpDown, UDM_SETPOS,
958 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
959 sprintf(speedStr, "%.2f", playbackSpeed);
960 SetWindowText(speedEdit, speedStr);
961 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
962
963 // Change the player window size and frame size to default
964 SetWindowPos(getMainHandle(), 0, 0, 0,
965 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
966 SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
967 buffer->setSize(32, 32);
968 calculateScrollBars();
969
970 // Update the cached sizing information and repaint the frame window
971 GetWindowRect(getFrameHandle(), &r);
972 window_size = Rect(r.left, r.top, r.right, r.bottom);
973 GetClientRect(getFrameHandle(), &r);
974 client_size = Rect(r.left, r.top, r.right, r.bottom);
975 InvalidateRect(getFrameHandle(), 0, TRUE);
976 UpdateWindow(getFrameHandle());
977}
978
george8217e92cb2005-01-31 16:01:02 +0000979void RfbPlayer::openSessionFile(char *_fileName) {
980 fileName = strDup(_fileName);
981
982 // Close the previous reading thread
983 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000984 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000985 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000986 }
987 blankBuffer();
988 newSession(fileName);
989 setSpeed(playbackSpeed);
990 rfbReader = new rfbSessionReader(this);
991 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000992 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +0000993 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000994}
995
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000996void RfbPlayer::setPaused(bool paused) {
997 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000998 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000999 tb.checkButton(ID_PAUSE, true);
1000 tb.checkButton(ID_PLAY, false);
1001 tb.checkButton(ID_STOP, false);
1002 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1003 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001004 } else {
george825beb62a2005-02-09 13:04:32 +00001005 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001006 tb.checkButton(ID_PLAY, true);
1007 tb.checkButton(ID_STOP, false);
1008 tb.checkButton(ID_PAUSE, false);
1009 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1010 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001011 }
1012}
1013
george82006f2792005-02-05 07:40:47 +00001014void RfbPlayer::stopPlayback() {
1015 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001016 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001017 tb.checkButton(ID_STOP, true);
1018 tb.checkButton(ID_PLAY, false);
1019 tb.checkButton(ID_PAUSE, false);
1020 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1021 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001022 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001023}
1024
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001025void RfbPlayer::setSpeed(double speed) {
1026 serverInitTime = serverInitTime * getSpeed() / speed;
1027 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +00001028 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001029}
1030
1031double RfbPlayer::getSpeed() {
1032 return is->getSpeed();
1033}
1034
1035void RfbPlayer::setPos(long pos) {
1036 is->setTimeOffset(max(pos, serverInitTime));
1037}
1038
1039long RfbPlayer::getSeekOffset() {
1040 return is->getSeekOffset();
1041}
1042
1043bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001044 if (is) return is->isSeeking();
1045 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001046}
1047
1048bool RfbPlayer::isSeekMode() {
1049 return seekMode;
1050}
1051
1052bool RfbPlayer::isPaused() {
1053 return is->isPaused();
1054}
1055
1056long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001057 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001058}
1059
george828a471482005-02-06 07:15:53 +00001060void RfbPlayer::updatePos(long newPos) {
1061 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001062 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +00001063 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +00001064 newPos /= 1000;
george8244325492005-02-06 07:29:51 +00001065 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001066 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001067
1068 // Update the position of slider
1069 if (!sliderDraging) {
1070 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +00001071 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
1072 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +00001073 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001074}
1075
1076void RfbPlayer::skipHandshaking() {
1077 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1078 is->skip(skipBytes);
1079 state_ = RFBSTATE_NORMAL;
1080}
1081
1082void programInfo() {
1083 win32::FileVersionInfo inf;
1084 _tprintf(_T("%s - %s, Version %s\n"),
1085 inf.getVerString(_T("ProductName")),
1086 inf.getVerString(_T("FileDescription")),
1087 inf.getVerString(_T("FileVersion")));
1088 printf("%s\n", buildTime);
1089 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1090}
1091
1092void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001093 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001094}
1095
1096double playbackSpeed = 1.0;
1097long initTime = -1;
1098bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001099char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001100bool print_usage = false;
1101bool acceptBell = false;
1102
1103bool processParams(int argc, char* argv[]) {
1104 for (int i = 1; i < argc; i++) {
1105 if ((strcasecmp(argv[i], "-help") == 0) ||
1106 (strcasecmp(argv[i], "--help") == 0) ||
1107 (strcasecmp(argv[i], "/help") == 0) ||
1108 (strcasecmp(argv[i], "-h") == 0) ||
1109 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001110 (strcasecmp(argv[i], "/?") == 0) ||
1111 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001112 print_usage = true;
1113 return true;
1114 }
1115
1116 if ((strcasecmp(argv[i], "-speed") == 0) ||
1117 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1118 playbackSpeed = atof(argv[++i]);
1119 if (playbackSpeed <= 0) {
1120 return false;
1121 }
1122 continue;
1123 }
1124
1125 if ((strcasecmp(argv[i], "-pos") == 0) ||
1126 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1127 initTime = atol(argv[++i]);
1128 if (initTime <= 0)
1129 return false;
1130 continue;
1131 }
1132
1133 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1134 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001135 autoplay = true;
1136 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137 }
1138
1139 if ((strcasecmp(argv[i], "-bell") == 0) ||
1140 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001141 acceptBell = true;
1142 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001143 }
1144
1145 if (i != argc - 1)
1146 return false;
1147 }
1148
1149 fileName = strDup(argv[argc-1]);
1150 return true;
1151}
1152
1153//
1154// -=- WinMain
1155//
1156
1157int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1158
1159 // - Process the command-line
1160
1161 int argc = __argc;
1162 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001163 if ((argc > 1) && (!processParams(argc, argv))) {
1164 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1165 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001166 }
george82e6883de2005-02-08 14:42:12 +00001167
1168 if (print_usage) {
1169 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001170 return 0;
george8267cbcd02005-01-16 15:39:56 +00001171 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001172
george82e6883de2005-02-08 14:42:12 +00001173 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001174 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001175 try {
1176 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001177 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001178 } catch (rdr::Exception e) {
1179 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1180 delete player;
1181 return 0;
1182 }
1183
1184 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001185 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001186 MSG msg;
1187 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001188 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1189 TranslateMessage(&msg);
1190 DispatchMessage(&msg);
1191 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001192 }
1193
george82e6883de2005-02-08 14:42:12 +00001194 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001195 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001196 if (player) delete player;
1197 } catch (rdr::Exception e) {
1198 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1199 }
1200
1201 return 0;
1202};