blob: e8a63e4520f5bd50c2df79ad168a142b2f46057c [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
george825457d412005-02-19 06:43:09 +000061#define MAX_SPEED 10.00
62#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000063#define MAX_POS_TRACKBAR_RANGE 50
george8268d25142005-02-13 09:33:22 +000064#define DEFAULT_PLAYER_WIDTH 640
65#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000066
george82d070c692005-01-19 16:44:04 +000067#define ID_TOOLBAR 500
68#define ID_PLAY 510
69#define ID_PAUSE 520
70#define ID_TIME_STATIC 530
71#define ID_SPEED_STATIC 540
72#define ID_SPEED_EDIT 550
73#define ID_POS_TRACKBAR 560
74#define ID_SPEED_UPDOWN 570
75
76
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000077//
78// -=- RfbPlayerClass
79
80//
81// Window class used as the basis for RfbPlayer instance
82//
83
84class RfbPlayerClass {
85public:
86 RfbPlayerClass();
87 ~RfbPlayerClass();
88 ATOM classAtom;
89 HINSTANCE instance;
90};
91
92LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
93 LRESULT result;
94
95 if (msg == WM_CREATE)
96 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
97 else if (msg == WM_DESTROY) {
98 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000099 SetWindowLong(hwnd, GWL_USERDATA, 0);
100 }
101 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
102 if (!_this) {
103 vlog.info("null _this in %x, message %u", hwnd, msg);
104 return DefWindowProc(hwnd, msg, wParam, lParam);
105 }
106
107 try {
108 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
109 } catch (rdr::Exception& e) {
110 vlog.error("untrapped: %s", e.str());
111 }
112
113 return result;
114};
115
116RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
117 WNDCLASS wndClass;
118 wndClass.style = 0;
119 wndClass.lpfnWndProc = RfbPlayerProc;
120 wndClass.cbClsExtra = 0;
121 wndClass.cbWndExtra = 0;
122 wndClass.hInstance = instance = GetModuleHandle(0);
123 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000124 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000125 if (!wndClass.hIcon)
126 printf("unable to load icon:%ld", GetLastError());
127 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
128 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000129 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000130 wndClass.lpszClassName = _T("RfbPlayerClass");
131 classAtom = RegisterClass(&wndClass);
132 if (!classAtom) {
133 throw rdr::SystemException("unable to register RfbPlayer window class",
134 GetLastError());
135 }
136}
137
138RfbPlayerClass::~RfbPlayerClass() {
139 if (classAtom) {
140 UnregisterClass((const TCHAR*)classAtom, instance);
141 }
142}
143
144RfbPlayerClass baseClass;
145
146//
147// -=- RfbFrameClass
148
149//
150// Window class used to displaying the rfb data
151//
152
153class RfbFrameClass {
154public:
155 RfbFrameClass();
156 ~RfbFrameClass();
157 ATOM classAtom;
158 HINSTANCE instance;
159};
160
161LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
162 LRESULT result;
163
164 if (msg == WM_CREATE)
165 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
166 else if (msg == WM_DESTROY)
167 SetWindowLong(hwnd, GWL_USERDATA, 0);
168 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
169 if (!_this) {
170 vlog.info("null _this in %x, message %u", hwnd, msg);
171 return DefWindowProc(hwnd, msg, wParam, lParam);
172 }
173
174 try {
175 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
176 } catch (rdr::Exception& e) {
177 vlog.error("untrapped: %s", e.str());
178 }
179
180 return result;
181}
182
183RfbFrameClass::RfbFrameClass() : classAtom(0) {
184 WNDCLASS wndClass;
185 wndClass.style = 0;
186 wndClass.lpfnWndProc = FrameProc;
187 wndClass.cbClsExtra = 0;
188 wndClass.cbWndExtra = 0;
189 wndClass.hInstance = instance = GetModuleHandle(0);
190 wndClass.hIcon = 0;
191 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
192 wndClass.hbrBackground = 0;
193 wndClass.lpszMenuName = 0;
194 wndClass.lpszClassName = _T("RfbPlayerClass1");
195 classAtom = RegisterClass(&wndClass);
196 if (!classAtom) {
197 throw rdr::SystemException("unable to register RfbPlayer window class",
198 GetLastError());
199 }
200}
201
202RfbFrameClass::~RfbFrameClass() {
203 if (classAtom) {
204 UnregisterClass((const TCHAR*)classAtom, instance);
205 }
206}
207
208RfbFrameClass frameClass;
209
210//
211// -=- RfbPlayer instance implementation
212//
213
214RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000215 bool _autoplay = false, bool _acceptBell = false)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000216: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
george82e6883de2005-02-08 14:42:12 +0000217 autoplay(_autoplay), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000218 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000219 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000220 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000221 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000222
george82e6883de2005-02-08 14:42:12 +0000223 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000224
george823c8fbbf2005-01-24 11:09:08 +0000225 // Reset the full session time
226 strcpy(fullSessionTime, "00m:00s");
227
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000228 // Create the main window
229 const TCHAR* name = _T("RfbPlayer");
230 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8268d25142005-02-13 09:33:22 +0000231 0, 0, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000232 if (!mainHwnd) {
233 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
234 }
235 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
236
237 // Create the backing buffer
238 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000239 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000240
george8217e92cb2005-01-31 16:01:02 +0000241 // Open the session file
242 if (fileName) {
243 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000244 if (initTime > 0) setPos(initTime);
245 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000246 } else {
247 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000248 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000249 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000250}
251
252RfbPlayer::~RfbPlayer() {
253 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000254 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000255 delete rfbReader->join();
256 rfbReader = 0;
257 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000258 if (mainHwnd) {
259 setVisible(false);
260 DestroyWindow(mainHwnd);
261 mainHwnd = 0;
262 }
george825beb62a2005-02-09 13:04:32 +0000263 if (buffer) delete buffer;
264 if (cutText) delete [] cutText;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000265 vlog.debug("~RfbPlayer done");
266}
267
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000268LRESULT
269RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
270 switch (msg) {
271
272 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000273
274 case WM_CREATE:
275 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000276 // Create the frame window
277 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
278 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
279 hwnd, 0, frameClass.instance, this);
280
george82d070c692005-01-19 16:44:04 +0000281 createToolBar(hwnd);
282
george82006f2792005-02-05 07:40:47 +0000283 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000284
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000285 return 0;
286 }
287
george827214b822004-12-12 07:02:51 +0000288 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000289
290 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000291 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000292 case ID_OPENFILE:
293 {
294 char curDir[_MAX_DIR];
295 static char filename[_MAX_PATH];
296 OPENFILENAME ofn;
297 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
298 GetCurrentDirectory(sizeof(curDir), curDir);
299
300 ofn.lStructSize = sizeof(OPENFILENAME);
301 ofn.hwndOwner = getMainHandle();
302 ofn.lpstrFile = filename;
303 ofn.nMaxFile = sizeof(filename);
304 ofn.lpstrInitialDir = curDir;
305 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
306 "All files (*.*)\0*.*\0";
307 ofn.lpstrDefExt = "rfb";
308 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
309 if (GetOpenFileName(&ofn))
310 openSessionFile(filename);
311 }
312 break;
george8271ca1772005-02-13 10:50:46 +0000313 case ID_CLOSEFILE:
314 closeSessionFile();
315 break;
george825c13c662005-01-27 14:48:23 +0000316 case ID_PLAY:
317 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000318 break;
319 case ID_PAUSE:
320 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000321 break;
322 case ID_STOP:
323 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000324 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000325 }
george825c13c662005-01-27 14:48:23 +0000326 break;
327 case ID_PLAYPAUSE:
328 if (isPaused()) {
329 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000330 } else {
331 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000332 }
george825c13c662005-01-27 14:48:23 +0000333 break;
george827549df42005-02-08 16:31:02 +0000334 case ID_GOTO:
335 {
336 GotoPosDialog gotoPosDlg;
337 if (gotoPosDlg.showDialog()) {
338 setPos(gotoPosDlg.getPos());
george825457d412005-02-19 06:43:09 +0000339 updatePos(gotoPosDlg.getPos());
george827549df42005-02-08 16:31:02 +0000340 }
341 }
342 break;
george825c13c662005-01-27 14:48:23 +0000343 case ID_FULLSCREEN:
344 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
345 break;
george8231a36332005-02-06 17:27:34 +0000346 case ID_LOOP:
347 loopPlayback = !loopPlayback;
348 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
349 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
350 break;
george824ea27f62005-01-29 15:03:06 +0000351 case ID_RETURN:
352 // Update the speed if return pressed in speedEdit
353 if (speedEdit == GetFocus()) {
354 char speedStr[20], *stopStr;
355 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
356 double speed = strtod(speedStr, &stopStr);
357 if (speed > 0) {
358 speed = min(MAX_SPEED, speed);
359 // Update speedUpDown position
360 SendMessage(speedUpDown, UDM_SETPOS,
361 0, MAKELONG((short)(speed / 0.5), 0));
362 } else {
363 speed = getSpeed();
364 }
365 setSpeed(speed);
366 sprintf(speedStr, "%.2f", speed);
367 SetWindowText(speedEdit, speedStr);
368 }
369 break;
george8201aa6732005-02-06 17:13:03 +0000370 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000371 PostQuitMessage(0);
372 break;
george82ef5f7262005-02-08 15:09:26 +0000373 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000374 MessageBox(getMainHandle(),
375 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
376 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000377 }
378 break;
379
380 // Update frame's window size and add scrollbars if required
381
382 case WM_SIZE:
383 {
george82d070c692005-01-19 16:44:04 +0000384
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000385 Point old_offset = bufferToClient(Point(0, 0));
386
387 // Update the cached sizing information
388 RECT r;
389 GetClientRect(getMainHandle(), &r);
390 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
391 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
392
393 GetWindowRect(getFrameHandle(), &r);
394 window_size = Rect(r.left, r.top, r.right, r.bottom);
395 GetClientRect(getFrameHandle(), &r);
396 client_size = Rect(r.left, r.top, r.right, r.bottom);
397
398 // Determine whether scrollbars are required
399 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000400
401 // Resize the ToolBar
402 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000403
404 // Redraw if required
405 if (!old_offset.equals(bufferToClient(Point(0, 0))))
406 InvalidateRect(getFrameHandle(), 0, TRUE);
407 }
408 break;
george828a471482005-02-06 07:15:53 +0000409
410 // Process messages from posTrackBar
411
412 case WM_HSCROLL:
413 {
414 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
415 Pos *= sliderStepMs;
416
417 switch (LOWORD(wParam)) {
418 case TB_PAGEUP:
419 case TB_PAGEDOWN:
420 case TB_LINEUP:
421 case TB_LINEDOWN:
422 case TB_THUMBTRACK:
423 sliderDraging = true;
424 updatePos(Pos);
425 return 0;
426 case TB_ENDTRACK:
427 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000428 sliderDraging = false;
429 return 0;
430 default:
431 break;
432 }
433 }
434 break;
george829e6e6cc2005-01-29 13:12:05 +0000435
436 case WM_NOTIFY:
437 switch (((NMHDR*)lParam)->code) {
438 case UDN_DELTAPOS:
439 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000440 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000441 char speedStr[20] = "\0";
442 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
443 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
444 double speed;
445
george824ea27f62005-01-29 15:03:06 +0000446 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000447 if (upDown->iDelta > 0) {
448 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
449 } else {
george824ea27f62005-01-29 15:03:06 +0000450 // It's need to round the UpDown position
451 if ((upDown->iPos * 0.5) != getSpeed()) {
452 upDown->iDelta = 0;
453 lResult = TRUE;
454 }
george829e6e6cc2005-01-29 13:12:05 +0000455 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
456 }
457 _gcvt(speed, 5, speedStr);
458 sprintf(speedStr, "%.2f", speed);
459 SetWindowText(speedEdit, speedStr);
460 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000461 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000462 }
george824ea27f62005-01-29 15:03:06 +0000463 }
george829e6e6cc2005-01-29 13:12:05 +0000464 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000465
466 case WM_CLOSE:
467 vlog.debug("WM_CLOSE %x", getMainHandle());
468 PostQuitMessage(0);
469 break;
470 }
471
472 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
473}
474
475LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
476 switch (msg) {
477
478 case WM_PAINT:
479 {
george825beb62a2005-02-09 13:04:32 +0000480 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000481 seekMode = true;
482 return 0;
483 } else {
484 if (seekMode) {
485 seekMode = false;
486 InvalidateRect(getFrameHandle(), 0, true);
487 UpdateWindow(getFrameHandle());
488 return 0;
489 }
490 }
491
492 PAINTSTRUCT ps;
493 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
494 if (!paintDC)
495 throw SystemException("unable to BeginPaint", GetLastError());
496 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
497
498 if (!pr.is_empty()) {
499
500 if (buffer->bitmap) {
501
502 // Get device context
503 BitmapDC bitmapDC(paintDC, buffer->bitmap);
504
505 // Blit the border if required
506 Rect bufpos = bufferToClient(buffer->getRect());
507 if (!pr.enclosed_by(bufpos)) {
508 vlog.debug("draw border");
509 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
510 RECT r;
511 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
512 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
513 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
514 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
515 }
516
517 // Do the blit
518 Point buf_pos = clientToBuffer(pr.tl);
519 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
520 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
521 throw SystemException("unable to BitBlt to window", GetLastError());
522
523 } else {
524 // Blit a load of black
525 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
526 0, 0, 0, BLACKNESS))
527 throw SystemException("unable to BitBlt to blank window", GetLastError());
528 }
529 }
530 EndPaint(getFrameHandle(), &ps);
531 }
532 return 0;
533
534 case WM_VSCROLL:
535 case WM_HSCROLL:
536 {
537 Point delta;
538 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
539
540 switch (LOWORD(wParam)) {
541 case SB_PAGEUP: newpos -= 50; break;
542 case SB_PAGEDOWN: newpos += 50; break;
543 case SB_LINEUP: newpos -= 5; break;
544 case SB_LINEDOWN: newpos += 5; break;
545 case SB_THUMBTRACK:
546 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
547 default: vlog.info("received unknown scroll message");
548 };
549
550 if (msg == WM_HSCROLL)
551 setViewportOffset(Point(newpos, scrolloffset.y));
552 else
553 setViewportOffset(Point(scrolloffset.x, newpos));
554
555 SCROLLINFO si;
556 si.cbSize = sizeof(si);
557 si.fMask = SIF_POS;
558 si.nPos = newpos;
559 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
560 }
561 break;
562 }
563
564 return DefWindowProc(hwnd, msg, wParam, lParam);
565}
566
george82d070c692005-01-19 16:44:04 +0000567void RfbPlayer::createToolBar(HWND parentHwnd) {
568 RECT tRect;
569 InitCommonControls();
570
571 tb.create(ID_TOOLBAR, parentHwnd);
572 tb.addBitmap(4, IDB_TOOLBAR);
573
574 // Create the control buttons
575 tb.addButton(0, ID_PLAY);
576 tb.addButton(1, ID_PAUSE);
577 tb.addButton(2, ID_STOP);
578 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
579 tb.addButton(3, ID_FULLSCREEN);
580 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
581
582 // Create the static control for the time output
583 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
584 tb.getButtonRect(6, &tRect);
585 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
586 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
587 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
588 GetModuleHandle(0), 0);
589 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
590
591 // Create the trackbar control for the time position
592 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
593 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000594 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000595 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
596 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
597 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
598 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000599 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000600 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
601
602 // Create the label with "Speed:" caption
603 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
604 tb.getButtonRect(10, &tRect);
605 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
606 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
607 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
608
609 // Create the edit control and the spin for the speed managing
610 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
611 tb.getButtonRect(11, &tRect);
612 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
613 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
614 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
615 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
616 // It's need to send notify messages to toolbar parent window
617 SetParent(speedEdit, tb.getHandle());
618
619 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
620 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000621 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000622}
623
george82a21d2952005-02-12 11:30:03 +0000624void RfbPlayer::disableTBandMenuItems() {
625 // Disable the menu items
626 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
627 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
628 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
629 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
630 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
631 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
632 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
633 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
634 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
635
636 // Disable the toolbar buttons and child controls
637 tb.enableButton(ID_PLAY, false);
638 tb.enableButton(ID_PAUSE, false);
639 tb.enableButton(ID_STOP, false);
640 tb.enableButton(ID_FULLSCREEN, false);
641 EnableWindow(posTrackBar, false);
642 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000643 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000644}
645
george82f5043162005-02-12 11:37:18 +0000646void RfbPlayer::enableTBandMenuItems() {
647 // Enable the menu items
648 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
649 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
650 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
651 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
652 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
653 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
654 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
655 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
656 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
657
658 // Enable the toolbar buttons and child controls
659 tb.enableButton(ID_PLAY, true);
660 tb.enableButton(ID_PAUSE, true);
661 tb.enableButton(ID_STOP, true);
662 tb.enableButton(ID_FULLSCREEN, true);
663 EnableWindow(posTrackBar, true);
664 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000665 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000666}
667
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000668void RfbPlayer::setVisible(bool visible) {
669 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
670 if (visible) {
671 // When the window becomes visible, make it active
672 SetForegroundWindow(getMainHandle());
673 SetActiveWindow(getMainHandle());
674 }
675}
676
677void RfbPlayer::setTitle(const char *title) {
678 char _title[256];
679 strcpy(_title, AppName);
680 strcat(_title, " - ");
681 strcat(_title, title);
682 SetWindowText(getMainHandle(), _title);
683}
684
685void RfbPlayer::setFrameSize(int width, int height) {
686 // Calculate and set required size for main window
687 RECT r = {0, 0, width, height};
688 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
689 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
690 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
691 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
692 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
693 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
694
695 // Enable/disable scrollbars as appropriate
696 calculateScrollBars();
697}
698
699void RfbPlayer::calculateScrollBars() {
700 // Calculate the required size of window
701 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
702 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
703 DWORD old_style;
704 RECT r;
705 SetRect(&r, 0, 0, buffer->width(), buffer->height());
706 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
707 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
708
709 // Work out whether scroll bars are required
710 do {
711 old_style = style;
712
713 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
714 style |= WS_HSCROLL;
715 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
716 }
717 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
718 style |= WS_VSCROLL;
719 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
720 }
721 } while (style != old_style);
722
723 // Tell Windows to update the window style & cached settings
724 if (style != current_style) {
725 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
726 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
727 }
728
729 // Update the scroll settings
730 SCROLLINFO si;
731 if (style & WS_VSCROLL) {
732 si.cbSize = sizeof(si);
733 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
734 si.nMin = 0;
735 si.nMax = buffer->height();
736 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
737 maxscrolloffset.y = max(0, si.nMax-si.nPage);
738 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
739 si.nPos = scrolloffset.y;
740 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
741 }
742 if (style & WS_HSCROLL) {
743 si.cbSize = sizeof(si);
744 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
745 si.nMin = 0;
746 si.nMax = buffer->width();
747 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
748 maxscrolloffset.x = max(0, si.nMax-si.nPage);
749 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
750 si.nPos = scrolloffset.x;
751 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
752 }
753}
754
755bool RfbPlayer::setViewportOffset(const Point& tl) {
756/* ***
757 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
758 max(0, min(maxscrolloffset.y, tl.y)));
759 */
760 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
761 max(0, min(tl.y, buffer->height()-client_size.height())));
762 Point delta = np.translate(scrolloffset.negate());
763 if (!np.equals(scrolloffset)) {
764 scrolloffset = np;
765 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
766 UpdateWindow(getFrameHandle());
767 return true;
768 }
769 return false;
770}
771
772void RfbPlayer::close(const char* reason) {
773 setVisible(false);
774 if (reason) {
775 vlog.info("closing - %s", reason);
776 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
777 }
778 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
779}
780
781void RfbPlayer::blankBuffer() {
782 fillRect(buffer->getRect(), 0);
783}
784
785void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000786 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000787 blankBuffer();
788 newSession(fileName);
789 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000790 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000791 if (paused) is->pausePlayback();
792 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000793}
794
795void RfbPlayer::processMsg() {
796 static long update_time = GetTickCount();
797 try {
george828a471482005-02-06 07:15:53 +0000798 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
799 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000800 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000801 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000802 update_time = GetTickCount();
803 }
804 RfbProto::processMsg();
805 } catch (rdr::Exception e) {
806 if (strcmp(e.str(), "[End Of File]") == 0) {
807 rewind();
george8231a36332005-02-06 17:27:34 +0000808 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000809 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000810 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000811 return;
812 }
813 // It's a special exception to perform backward seeking.
814 // We only rewind the stream and seek the offset
815 if (strcmp(e.str(), "[REWIND]") == 0) {
816 long initTime = getSeekOffset();
817 rewind();
818 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000819 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000820 } else {
821 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
822 return;
823 }
824 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000825}
826
827void RfbPlayer::serverInit() {
828 RfbProto::serverInit();
829
830 // Save the server init time for using in setPos()
831 serverInitTime = getTimeOffset() / getSpeed();
832
833 // Resize the backing buffer
834 buffer->setSize(cp.width, cp.height);
835
836 // Check on the true colour mode
837 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000838 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000839
840 // Set the session pixel format
841 buffer->setPF(cp.pf());
842
843 // If the window is not maximised then resize it
844 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
845 setFrameSize(cp.width, cp.height);
846
847 // Set the window title and show it
848 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000849
george82d4d69e62005-02-05 09:23:18 +0000850 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000851 sessionTimeMs = calculateSessionTime(fileName);
852 sprintf(fullSessionTime, "%.2um:%.2us",
853 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000854 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000855 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
856 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000857 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000858
george82006f2792005-02-05 07:40:47 +0000859 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000860}
861
862void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
863 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
864 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
865/* int i;
866 for (i=0;i<count;i++) {
867 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
868 }
869 // *** change to 0, 256?
870 refreshWindowPalette(first, count);
871 palette_changed = true;
872 InvalidateRect(getFrameHandle(), 0, FALSE);*/
873}
874
875void RfbPlayer::bell() {
876 if (acceptBell)
877 MessageBeep(-1);
878}
879
880void RfbPlayer::serverCutText(const char* str, int len) {
881 if (cutText != NULL)
882 delete [] cutText;
883 cutText = new char[len + 1];
884 memcpy(cutText, str, len);
885 cutText[len] = '\0';
886}
887
888void RfbPlayer::frameBufferUpdateEnd() {
889};
890
891void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
892}
893
894void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
895}
896
897
898void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
899 buffer->fillRect(r, pix);
900 invalidateBufferRect(r);
901}
902
903void RfbPlayer::imageRect(const Rect& r, void* pixels) {
904 buffer->imageRect(r, pixels);
905 invalidateBufferRect(r);
906}
907
908void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
909 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
910 invalidateBufferRect(r);
911}
912
913bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
914 Rect rect = bufferToClient(crect);
915 if (rect.intersect(client_size).is_empty()) return false;
916 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
917 InvalidateRect(getFrameHandle(), &invalid, FALSE);
918 return true;
919}
920
george8257f13522005-02-05 08:48:22 +0000921long RfbPlayer::calculateSessionTime(char *filename) {
922 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000923 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000924 try {
925 while (TRUE) {
926 sessionFile.skip(1024);
927 }
928 } catch (rdr::Exception e) {
929 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000930 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000931 } else {
932 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
933 return 0;
934 }
935 }
936 return 0;
937}
938
george826b87aff2005-02-13 10:48:21 +0000939void RfbPlayer::closeSessionFile() {
940 char speedStr[10];
941 RECT r;
942
943 // Uncheck all toolbar buttons
944 if (tb.getHandle()) {
945 tb.checkButton(ID_PLAY, false);
946 tb.checkButton(ID_PAUSE, false);
947 tb.checkButton(ID_STOP, false);
948 }
949
950 // Stop playback and update the player state
951 disableTBandMenuItems();
952 if (rfbReader) {
953 delete rfbReader->join();
954 rfbReader = 0;
955 delete [] fileName;
956 fileName = 0;
957 }
958 blankBuffer();
959 setTitle("None");
960 playbackSpeed = 1.0;
961 SendMessage(speedUpDown, UDM_SETPOS,
962 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
963 sprintf(speedStr, "%.2f", playbackSpeed);
964 SetWindowText(speedEdit, speedStr);
965 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
966
967 // Change the player window size and frame size to default
968 SetWindowPos(getMainHandle(), 0, 0, 0,
969 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
970 SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
971 buffer->setSize(32, 32);
972 calculateScrollBars();
973
974 // Update the cached sizing information and repaint the frame window
975 GetWindowRect(getFrameHandle(), &r);
976 window_size = Rect(r.left, r.top, r.right, r.bottom);
977 GetClientRect(getFrameHandle(), &r);
978 client_size = Rect(r.left, r.top, r.right, r.bottom);
979 InvalidateRect(getFrameHandle(), 0, TRUE);
980 UpdateWindow(getFrameHandle());
981}
982
george8217e92cb2005-01-31 16:01:02 +0000983void RfbPlayer::openSessionFile(char *_fileName) {
984 fileName = strDup(_fileName);
985
986 // Close the previous reading thread
987 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000988 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000989 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000990 }
991 blankBuffer();
992 newSession(fileName);
993 setSpeed(playbackSpeed);
994 rfbReader = new rfbSessionReader(this);
995 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000996 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +0000997 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000998}
999
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001000void RfbPlayer::setPaused(bool paused) {
1001 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001002 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001003 tb.checkButton(ID_PAUSE, true);
1004 tb.checkButton(ID_PLAY, false);
1005 tb.checkButton(ID_STOP, false);
1006 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1007 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001008 } else {
george825beb62a2005-02-09 13:04:32 +00001009 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001010 tb.checkButton(ID_PLAY, true);
1011 tb.checkButton(ID_STOP, false);
1012 tb.checkButton(ID_PAUSE, false);
1013 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1014 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001015 }
1016}
1017
george82006f2792005-02-05 07:40:47 +00001018void RfbPlayer::stopPlayback() {
1019 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001020 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001021 tb.checkButton(ID_STOP, true);
1022 tb.checkButton(ID_PLAY, false);
1023 tb.checkButton(ID_PAUSE, false);
1024 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1025 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001026 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001027}
1028
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001029void RfbPlayer::setSpeed(double speed) {
1030 serverInitTime = serverInitTime * getSpeed() / speed;
1031 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +00001032 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001033}
1034
1035double RfbPlayer::getSpeed() {
1036 return is->getSpeed();
1037}
1038
1039void RfbPlayer::setPos(long pos) {
1040 is->setTimeOffset(max(pos, serverInitTime));
1041}
1042
1043long RfbPlayer::getSeekOffset() {
1044 return is->getSeekOffset();
1045}
1046
1047bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001048 if (is) return is->isSeeking();
1049 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001050}
1051
1052bool RfbPlayer::isSeekMode() {
1053 return seekMode;
1054}
1055
1056bool RfbPlayer::isPaused() {
1057 return is->isPaused();
1058}
1059
1060long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001061 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001062}
1063
george828a471482005-02-06 07:15:53 +00001064void RfbPlayer::updatePos(long newPos) {
1065 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001066 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001067 long time = newPos / 1000;
1068 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001069 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001070
1071 // Update the position of slider
1072 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001073 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1074 sliderStepMs / double(newPos);
1075 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1076 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1077 }
george828a471482005-02-06 07:15:53 +00001078 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001079}
1080
1081void RfbPlayer::skipHandshaking() {
1082 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1083 is->skip(skipBytes);
1084 state_ = RFBSTATE_NORMAL;
1085}
1086
1087void programInfo() {
1088 win32::FileVersionInfo inf;
1089 _tprintf(_T("%s - %s, Version %s\n"),
1090 inf.getVerString(_T("ProductName")),
1091 inf.getVerString(_T("FileDescription")),
1092 inf.getVerString(_T("FileVersion")));
1093 printf("%s\n", buildTime);
1094 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1095}
1096
1097void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001098 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001099}
1100
1101double playbackSpeed = 1.0;
1102long initTime = -1;
1103bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001104char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001105bool print_usage = false;
1106bool acceptBell = false;
1107
1108bool processParams(int argc, char* argv[]) {
1109 for (int i = 1; i < argc; i++) {
1110 if ((strcasecmp(argv[i], "-help") == 0) ||
1111 (strcasecmp(argv[i], "--help") == 0) ||
1112 (strcasecmp(argv[i], "/help") == 0) ||
1113 (strcasecmp(argv[i], "-h") == 0) ||
1114 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001115 (strcasecmp(argv[i], "/?") == 0) ||
1116 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001117 print_usage = true;
1118 return true;
1119 }
1120
1121 if ((strcasecmp(argv[i], "-speed") == 0) ||
1122 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1123 playbackSpeed = atof(argv[++i]);
1124 if (playbackSpeed <= 0) {
1125 return false;
1126 }
1127 continue;
1128 }
1129
1130 if ((strcasecmp(argv[i], "-pos") == 0) ||
1131 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1132 initTime = atol(argv[++i]);
1133 if (initTime <= 0)
1134 return false;
1135 continue;
1136 }
1137
1138 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1139 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001140 autoplay = true;
1141 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001142 }
1143
1144 if ((strcasecmp(argv[i], "-bell") == 0) ||
1145 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001146 acceptBell = true;
1147 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001148 }
1149
1150 if (i != argc - 1)
1151 return false;
1152 }
1153
1154 fileName = strDup(argv[argc-1]);
1155 return true;
1156}
1157
1158//
1159// -=- WinMain
1160//
1161
1162int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1163
1164 // - Process the command-line
1165
1166 int argc = __argc;
1167 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001168 if ((argc > 1) && (!processParams(argc, argv))) {
1169 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1170 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001171 }
george82e6883de2005-02-08 14:42:12 +00001172
1173 if (print_usage) {
1174 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001175 return 0;
george8267cbcd02005-01-16 15:39:56 +00001176 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001177
george82e6883de2005-02-08 14:42:12 +00001178 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001179 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001180 try {
1181 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001182 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001183 } catch (rdr::Exception e) {
1184 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1185 delete player;
1186 return 0;
1187 }
1188
1189 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001190 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001191 MSG msg;
1192 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001193 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1194 TranslateMessage(&msg);
1195 DispatchMessage(&msg);
1196 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001197 }
1198
george82e6883de2005-02-08 14:42:12 +00001199 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001200 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001201 if (player) delete player;
1202 } catch (rdr::Exception e) {
1203 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1204 }
1205
1206 return 0;
1207};