blob: 1220f6be55abd8fe7b2dbf50f912244bf91e1674 [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");
george822ff7a612005-02-19 17:05:24 +0000230 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
231 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000232 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000233 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000234 if (!mainHwnd) {
235 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
236 }
237 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
238
239 // Create the backing buffer
240 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000241 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000242
george8217e92cb2005-01-31 16:01:02 +0000243 // Open the session file
244 if (fileName) {
245 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000246 if (initTime > 0) setPos(initTime);
247 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000248 } else {
249 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000250 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000251 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000252}
253
254RfbPlayer::~RfbPlayer() {
255 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000256 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000257 delete rfbReader->join();
258 rfbReader = 0;
259 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000260 if (mainHwnd) {
261 setVisible(false);
262 DestroyWindow(mainHwnd);
263 mainHwnd = 0;
264 }
george825beb62a2005-02-09 13:04:32 +0000265 if (buffer) delete buffer;
266 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000267 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000268 vlog.debug("~RfbPlayer done");
269}
270
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000271LRESULT
272RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
273 switch (msg) {
274
275 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000276
277 case WM_CREATE:
278 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000279 // Create the frame window
280 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
281 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
282 hwnd, 0, frameClass.instance, this);
283
george82d070c692005-01-19 16:44:04 +0000284 createToolBar(hwnd);
285
george82006f2792005-02-05 07:40:47 +0000286 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000287
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000288 return 0;
289 }
290
george827214b822004-12-12 07:02:51 +0000291 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000292
293 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000294 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000295 case ID_OPENFILE:
296 {
297 char curDir[_MAX_DIR];
298 static char filename[_MAX_PATH];
299 OPENFILENAME ofn;
300 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
301 GetCurrentDirectory(sizeof(curDir), curDir);
302
303 ofn.lStructSize = sizeof(OPENFILENAME);
304 ofn.hwndOwner = getMainHandle();
305 ofn.lpstrFile = filename;
306 ofn.nMaxFile = sizeof(filename);
307 ofn.lpstrInitialDir = curDir;
308 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
309 "All files (*.*)\0*.*\0";
310 ofn.lpstrDefExt = "rfb";
311 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
312 if (GetOpenFileName(&ofn))
313 openSessionFile(filename);
314 }
315 break;
george8271ca1772005-02-13 10:50:46 +0000316 case ID_CLOSEFILE:
317 closeSessionFile();
318 break;
george825c13c662005-01-27 14:48:23 +0000319 case ID_PLAY:
320 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000321 break;
322 case ID_PAUSE:
323 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000324 break;
325 case ID_STOP:
326 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000327 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000328 }
george825c13c662005-01-27 14:48:23 +0000329 break;
330 case ID_PLAYPAUSE:
331 if (isPaused()) {
332 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000333 } else {
334 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000335 }
george825c13c662005-01-27 14:48:23 +0000336 break;
george827549df42005-02-08 16:31:02 +0000337 case ID_GOTO:
338 {
339 GotoPosDialog gotoPosDlg;
340 if (gotoPosDlg.showDialog()) {
george821d5d40d2005-02-20 03:25:47 +0000341 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
342 setPos(gotoTime);
343 updatePos(gotoTime);
george827549df42005-02-08 16:31:02 +0000344 }
345 }
346 break;
george825c13c662005-01-27 14:48:23 +0000347 case ID_FULLSCREEN:
348 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
349 break;
george8231a36332005-02-06 17:27:34 +0000350 case ID_LOOP:
351 loopPlayback = !loopPlayback;
352 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
353 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
354 break;
george824ea27f62005-01-29 15:03:06 +0000355 case ID_RETURN:
356 // Update the speed if return pressed in speedEdit
357 if (speedEdit == GetFocus()) {
358 char speedStr[20], *stopStr;
359 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
360 double speed = strtod(speedStr, &stopStr);
361 if (speed > 0) {
362 speed = min(MAX_SPEED, speed);
363 // Update speedUpDown position
364 SendMessage(speedUpDown, UDM_SETPOS,
365 0, MAKELONG((short)(speed / 0.5), 0));
366 } else {
367 speed = getSpeed();
368 }
369 setSpeed(speed);
370 sprintf(speedStr, "%.2f", speed);
371 SetWindowText(speedEdit, speedStr);
372 }
373 break;
george8201aa6732005-02-06 17:13:03 +0000374 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000375 PostQuitMessage(0);
376 break;
george82ef5f7262005-02-08 15:09:26 +0000377 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000378 MessageBox(getMainHandle(),
379 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
380 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000381 }
382 break;
383
384 // Update frame's window size and add scrollbars if required
385
386 case WM_SIZE:
387 {
george82d070c692005-01-19 16:44:04 +0000388
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000389 Point old_offset = bufferToClient(Point(0, 0));
390
391 // Update the cached sizing information
392 RECT r;
393 GetClientRect(getMainHandle(), &r);
394 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
395 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
396
397 GetWindowRect(getFrameHandle(), &r);
398 window_size = Rect(r.left, r.top, r.right, r.bottom);
399 GetClientRect(getFrameHandle(), &r);
400 client_size = Rect(r.left, r.top, r.right, r.bottom);
401
402 // Determine whether scrollbars are required
403 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000404
405 // Resize the ToolBar
406 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000407
408 // Redraw if required
409 if (!old_offset.equals(bufferToClient(Point(0, 0))))
410 InvalidateRect(getFrameHandle(), 0, TRUE);
411 }
412 break;
george828a471482005-02-06 07:15:53 +0000413
414 // Process messages from posTrackBar
415
416 case WM_HSCROLL:
417 {
418 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
419 Pos *= sliderStepMs;
420
421 switch (LOWORD(wParam)) {
422 case TB_PAGEUP:
423 case TB_PAGEDOWN:
424 case TB_LINEUP:
425 case TB_LINEDOWN:
426 case TB_THUMBTRACK:
427 sliderDraging = true;
428 updatePos(Pos);
429 return 0;
430 case TB_ENDTRACK:
431 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000432 sliderDraging = false;
433 return 0;
434 default:
435 break;
436 }
437 }
438 break;
george829e6e6cc2005-01-29 13:12:05 +0000439
440 case WM_NOTIFY:
441 switch (((NMHDR*)lParam)->code) {
442 case UDN_DELTAPOS:
443 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000444 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000445 char speedStr[20] = "\0";
446 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
447 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
448 double speed;
449
george824ea27f62005-01-29 15:03:06 +0000450 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000451 if (upDown->iDelta > 0) {
452 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
453 } else {
george824ea27f62005-01-29 15:03:06 +0000454 // It's need to round the UpDown position
455 if ((upDown->iPos * 0.5) != getSpeed()) {
456 upDown->iDelta = 0;
457 lResult = TRUE;
458 }
george829e6e6cc2005-01-29 13:12:05 +0000459 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
460 }
461 _gcvt(speed, 5, speedStr);
462 sprintf(speedStr, "%.2f", speed);
463 SetWindowText(speedEdit, speedStr);
464 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000465 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000466 }
george824ea27f62005-01-29 15:03:06 +0000467 }
george829e6e6cc2005-01-29 13:12:05 +0000468 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000469
470 case WM_CLOSE:
471 vlog.debug("WM_CLOSE %x", getMainHandle());
472 PostQuitMessage(0);
473 break;
474 }
475
476 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
477}
478
479LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
480 switch (msg) {
481
482 case WM_PAINT:
483 {
george825beb62a2005-02-09 13:04:32 +0000484 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000485 seekMode = true;
486 return 0;
487 } else {
488 if (seekMode) {
489 seekMode = false;
490 InvalidateRect(getFrameHandle(), 0, true);
491 UpdateWindow(getFrameHandle());
492 return 0;
493 }
494 }
495
496 PAINTSTRUCT ps;
497 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
498 if (!paintDC)
499 throw SystemException("unable to BeginPaint", GetLastError());
500 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
501
502 if (!pr.is_empty()) {
503
504 if (buffer->bitmap) {
505
506 // Get device context
507 BitmapDC bitmapDC(paintDC, buffer->bitmap);
508
509 // Blit the border if required
510 Rect bufpos = bufferToClient(buffer->getRect());
511 if (!pr.enclosed_by(bufpos)) {
512 vlog.debug("draw border");
513 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
514 RECT r;
515 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
516 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
517 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
518 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
519 }
520
521 // Do the blit
522 Point buf_pos = clientToBuffer(pr.tl);
523 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
524 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
525 throw SystemException("unable to BitBlt to window", GetLastError());
526
527 } else {
528 // Blit a load of black
529 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
530 0, 0, 0, BLACKNESS))
531 throw SystemException("unable to BitBlt to blank window", GetLastError());
532 }
533 }
534 EndPaint(getFrameHandle(), &ps);
535 }
536 return 0;
537
538 case WM_VSCROLL:
539 case WM_HSCROLL:
540 {
541 Point delta;
542 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
543
544 switch (LOWORD(wParam)) {
545 case SB_PAGEUP: newpos -= 50; break;
546 case SB_PAGEDOWN: newpos += 50; break;
547 case SB_LINEUP: newpos -= 5; break;
548 case SB_LINEDOWN: newpos += 5; break;
549 case SB_THUMBTRACK:
550 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
551 default: vlog.info("received unknown scroll message");
552 };
553
554 if (msg == WM_HSCROLL)
555 setViewportOffset(Point(newpos, scrolloffset.y));
556 else
557 setViewportOffset(Point(scrolloffset.x, newpos));
558
559 SCROLLINFO si;
560 si.cbSize = sizeof(si);
561 si.fMask = SIF_POS;
562 si.nPos = newpos;
563 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
564 }
565 break;
566 }
567
568 return DefWindowProc(hwnd, msg, wParam, lParam);
569}
570
george82d070c692005-01-19 16:44:04 +0000571void RfbPlayer::createToolBar(HWND parentHwnd) {
572 RECT tRect;
573 InitCommonControls();
574
575 tb.create(ID_TOOLBAR, parentHwnd);
576 tb.addBitmap(4, IDB_TOOLBAR);
577
578 // Create the control buttons
579 tb.addButton(0, ID_PLAY);
580 tb.addButton(1, ID_PAUSE);
581 tb.addButton(2, ID_STOP);
582 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
583 tb.addButton(3, ID_FULLSCREEN);
584 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
585
586 // Create the static control for the time output
587 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
588 tb.getButtonRect(6, &tRect);
589 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
590 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
591 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
592 GetModuleHandle(0), 0);
593 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
594
595 // Create the trackbar control for the time position
596 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
597 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000598 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000599 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
600 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
601 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
602 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000603 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000604 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
605
606 // Create the label with "Speed:" caption
607 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
608 tb.getButtonRect(10, &tRect);
609 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
610 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
611 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
612
613 // Create the edit control and the spin for the speed managing
614 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
615 tb.getButtonRect(11, &tRect);
616 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
617 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
618 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
619 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
620 // It's need to send notify messages to toolbar parent window
621 SetParent(speedEdit, tb.getHandle());
622
623 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
624 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000625 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000626}
627
george82a21d2952005-02-12 11:30:03 +0000628void RfbPlayer::disableTBandMenuItems() {
629 // Disable the menu items
630 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
631 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
632 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
633 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
634 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
635 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
636 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
637 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
638 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
639
640 // Disable the toolbar buttons and child controls
641 tb.enableButton(ID_PLAY, false);
642 tb.enableButton(ID_PAUSE, false);
643 tb.enableButton(ID_STOP, false);
644 tb.enableButton(ID_FULLSCREEN, false);
645 EnableWindow(posTrackBar, false);
646 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000647 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000648}
649
george82f5043162005-02-12 11:37:18 +0000650void RfbPlayer::enableTBandMenuItems() {
651 // Enable the menu items
652 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
653 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
654 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
655 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
656 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
657 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
658 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
659 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
660 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
661
662 // Enable the toolbar buttons and child controls
663 tb.enableButton(ID_PLAY, true);
664 tb.enableButton(ID_PAUSE, true);
665 tb.enableButton(ID_STOP, true);
666 tb.enableButton(ID_FULLSCREEN, true);
667 EnableWindow(posTrackBar, true);
668 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000669 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000670}
671
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000672void RfbPlayer::setVisible(bool visible) {
673 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
674 if (visible) {
675 // When the window becomes visible, make it active
676 SetForegroundWindow(getMainHandle());
677 SetActiveWindow(getMainHandle());
678 }
679}
680
681void RfbPlayer::setTitle(const char *title) {
682 char _title[256];
683 strcpy(_title, AppName);
684 strcat(_title, " - ");
685 strcat(_title, title);
686 SetWindowText(getMainHandle(), _title);
687}
688
689void RfbPlayer::setFrameSize(int width, int height) {
690 // Calculate and set required size for main window
691 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000692 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000693 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
694 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
695 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000696 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
697 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
698 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000699 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
700
701 // Enable/disable scrollbars as appropriate
702 calculateScrollBars();
703}
704
705void RfbPlayer::calculateScrollBars() {
706 // Calculate the required size of window
707 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
708 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
709 DWORD old_style;
710 RECT r;
711 SetRect(&r, 0, 0, buffer->width(), buffer->height());
712 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
713 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
714
715 // Work out whether scroll bars are required
716 do {
717 old_style = style;
718
719 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
720 style |= WS_HSCROLL;
721 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
722 }
723 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
724 style |= WS_VSCROLL;
725 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
726 }
727 } while (style != old_style);
728
729 // Tell Windows to update the window style & cached settings
730 if (style != current_style) {
731 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
732 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
733 }
734
735 // Update the scroll settings
736 SCROLLINFO si;
737 if (style & WS_VSCROLL) {
738 si.cbSize = sizeof(si);
739 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
740 si.nMin = 0;
741 si.nMax = buffer->height();
742 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
743 maxscrolloffset.y = max(0, si.nMax-si.nPage);
744 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
745 si.nPos = scrolloffset.y;
746 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
747 }
748 if (style & WS_HSCROLL) {
749 si.cbSize = sizeof(si);
750 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
751 si.nMin = 0;
752 si.nMax = buffer->width();
753 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
754 maxscrolloffset.x = max(0, si.nMax-si.nPage);
755 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
756 si.nPos = scrolloffset.x;
757 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
758 }
759}
760
761bool RfbPlayer::setViewportOffset(const Point& tl) {
762/* ***
763 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
764 max(0, min(maxscrolloffset.y, tl.y)));
765 */
766 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
767 max(0, min(tl.y, buffer->height()-client_size.height())));
768 Point delta = np.translate(scrolloffset.negate());
769 if (!np.equals(scrolloffset)) {
770 scrolloffset = np;
771 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
772 UpdateWindow(getFrameHandle());
773 return true;
774 }
775 return false;
776}
777
778void RfbPlayer::close(const char* reason) {
779 setVisible(false);
780 if (reason) {
781 vlog.info("closing - %s", reason);
782 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
783 }
784 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
785}
786
787void RfbPlayer::blankBuffer() {
788 fillRect(buffer->getRect(), 0);
789}
790
791void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000792 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000793 blankBuffer();
794 newSession(fileName);
795 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000796 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000797 if (paused) is->pausePlayback();
798 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000799}
800
801void RfbPlayer::processMsg() {
802 static long update_time = GetTickCount();
803 try {
george828a471482005-02-06 07:15:53 +0000804 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
805 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000806 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000807 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000808 update_time = GetTickCount();
809 }
810 RfbProto::processMsg();
811 } catch (rdr::Exception e) {
812 if (strcmp(e.str(), "[End Of File]") == 0) {
813 rewind();
george8231a36332005-02-06 17:27:34 +0000814 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000815 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000816 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000817 return;
818 }
819 // It's a special exception to perform backward seeking.
820 // We only rewind the stream and seek the offset
821 if (strcmp(e.str(), "[REWIND]") == 0) {
822 long initTime = getSeekOffset();
823 rewind();
824 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000825 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000826 } else {
827 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
828 return;
829 }
830 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000831}
832
833void RfbPlayer::serverInit() {
834 RfbProto::serverInit();
835
836 // Save the server init time for using in setPos()
837 serverInitTime = getTimeOffset() / getSpeed();
838
839 // Resize the backing buffer
840 buffer->setSize(cp.width, cp.height);
841
842 // Check on the true colour mode
843 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000844 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000845
846 // Set the session pixel format
847 buffer->setPF(cp.pf());
848
849 // If the window is not maximised then resize it
850 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
851 setFrameSize(cp.width, cp.height);
852
853 // Set the window title and show it
854 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000855
george82d4d69e62005-02-05 09:23:18 +0000856 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000857 sessionTimeMs = calculateSessionTime(fileName);
858 sprintf(fullSessionTime, "%.2um:%.2us",
859 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000860 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000861 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
862 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000863 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000864
george82006f2792005-02-05 07:40:47 +0000865 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000866}
867
868void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
869 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
870 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
871/* int i;
872 for (i=0;i<count;i++) {
873 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
874 }
875 // *** change to 0, 256?
876 refreshWindowPalette(first, count);
877 palette_changed = true;
878 InvalidateRect(getFrameHandle(), 0, FALSE);*/
879}
880
881void RfbPlayer::bell() {
882 if (acceptBell)
883 MessageBeep(-1);
884}
885
886void RfbPlayer::serverCutText(const char* str, int len) {
887 if (cutText != NULL)
888 delete [] cutText;
889 cutText = new char[len + 1];
890 memcpy(cutText, str, len);
891 cutText[len] = '\0';
892}
893
894void RfbPlayer::frameBufferUpdateEnd() {
895};
896
897void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
898}
899
900void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
901}
902
903
904void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
905 buffer->fillRect(r, pix);
906 invalidateBufferRect(r);
907}
908
909void RfbPlayer::imageRect(const Rect& r, void* pixels) {
910 buffer->imageRect(r, pixels);
911 invalidateBufferRect(r);
912}
913
914void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
915 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
916 invalidateBufferRect(r);
917}
918
919bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
920 Rect rect = bufferToClient(crect);
921 if (rect.intersect(client_size).is_empty()) return false;
922 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
923 InvalidateRect(getFrameHandle(), &invalid, FALSE);
924 return true;
925}
926
george8257f13522005-02-05 08:48:22 +0000927long RfbPlayer::calculateSessionTime(char *filename) {
928 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000929 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000930 try {
931 while (TRUE) {
932 sessionFile.skip(1024);
933 }
934 } catch (rdr::Exception e) {
935 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000936 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000937 } else {
938 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
939 return 0;
940 }
941 }
942 return 0;
943}
944
george826b87aff2005-02-13 10:48:21 +0000945void RfbPlayer::closeSessionFile() {
946 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000947 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000948 RECT r;
949
950 // Uncheck all toolbar buttons
951 if (tb.getHandle()) {
952 tb.checkButton(ID_PLAY, false);
953 tb.checkButton(ID_PAUSE, false);
954 tb.checkButton(ID_STOP, false);
955 }
956
957 // Stop playback and update the player state
958 disableTBandMenuItems();
959 if (rfbReader) {
960 delete rfbReader->join();
961 rfbReader = 0;
962 delete [] fileName;
963 fileName = 0;
964 }
965 blankBuffer();
966 setTitle("None");
george827009c892005-02-19 12:49:42 +0000967 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +0000968 playbackSpeed = 1.0;
969 SendMessage(speedUpDown, UDM_SETPOS,
970 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
971 sprintf(speedStr, "%.2f", playbackSpeed);
972 SetWindowText(speedEdit, speedStr);
973 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
974
975 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +0000976 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
977 dwStyle &= ~WS_MAXIMIZE;
978 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
979 }
george822ff7a612005-02-19 17:05:24 +0000980 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
981 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
982 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +0000983 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +0000984 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +0000985 buffer->setSize(32, 32);
986 calculateScrollBars();
987
988 // Update the cached sizing information and repaint the frame window
989 GetWindowRect(getFrameHandle(), &r);
990 window_size = Rect(r.left, r.top, r.right, r.bottom);
991 GetClientRect(getFrameHandle(), &r);
992 client_size = Rect(r.left, r.top, r.right, r.bottom);
993 InvalidateRect(getFrameHandle(), 0, TRUE);
994 UpdateWindow(getFrameHandle());
995}
996
george8217e92cb2005-01-31 16:01:02 +0000997void RfbPlayer::openSessionFile(char *_fileName) {
998 fileName = strDup(_fileName);
999
1000 // Close the previous reading thread
1001 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001002 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001003 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001004 }
1005 blankBuffer();
1006 newSession(fileName);
1007 setSpeed(playbackSpeed);
1008 rfbReader = new rfbSessionReader(this);
1009 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001010 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001011 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001012}
1013
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001014void RfbPlayer::setPaused(bool paused) {
1015 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001016 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001017 tb.checkButton(ID_PAUSE, true);
1018 tb.checkButton(ID_PLAY, false);
1019 tb.checkButton(ID_STOP, false);
1020 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1021 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001022 } else {
george825beb62a2005-02-09 13:04:32 +00001023 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001024 tb.checkButton(ID_PLAY, true);
1025 tb.checkButton(ID_STOP, false);
1026 tb.checkButton(ID_PAUSE, false);
1027 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1028 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001029 }
1030}
1031
george82006f2792005-02-05 07:40:47 +00001032void RfbPlayer::stopPlayback() {
1033 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001034 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001035 tb.checkButton(ID_STOP, true);
1036 tb.checkButton(ID_PLAY, false);
1037 tb.checkButton(ID_PAUSE, false);
1038 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1039 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001040 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001041}
1042
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001043void RfbPlayer::setSpeed(double speed) {
1044 serverInitTime = serverInitTime * getSpeed() / speed;
1045 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +00001046 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001047}
1048
1049double RfbPlayer::getSpeed() {
1050 return is->getSpeed();
1051}
1052
1053void RfbPlayer::setPos(long pos) {
1054 is->setTimeOffset(max(pos, serverInitTime));
1055}
1056
1057long RfbPlayer::getSeekOffset() {
1058 return is->getSeekOffset();
1059}
1060
1061bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001062 if (is) return is->isSeeking();
1063 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001064}
1065
1066bool RfbPlayer::isSeekMode() {
1067 return seekMode;
1068}
1069
1070bool RfbPlayer::isPaused() {
1071 return is->isPaused();
1072}
1073
1074long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001075 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001076}
1077
george828a471482005-02-06 07:15:53 +00001078void RfbPlayer::updatePos(long newPos) {
1079 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001080 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001081 long time = newPos / 1000;
1082 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001083 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001084
1085 // Update the position of slider
1086 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001087 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1088 sliderStepMs / double(newPos);
1089 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1090 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1091 }
george828a471482005-02-06 07:15:53 +00001092 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001093}
1094
1095void RfbPlayer::skipHandshaking() {
1096 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1097 is->skip(skipBytes);
1098 state_ = RFBSTATE_NORMAL;
1099}
1100
1101void programInfo() {
1102 win32::FileVersionInfo inf;
1103 _tprintf(_T("%s - %s, Version %s\n"),
1104 inf.getVerString(_T("ProductName")),
1105 inf.getVerString(_T("FileDescription")),
1106 inf.getVerString(_T("FileVersion")));
1107 printf("%s\n", buildTime);
1108 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1109}
1110
1111void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001112 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001113}
1114
1115double playbackSpeed = 1.0;
1116long initTime = -1;
1117bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001118char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001119bool print_usage = false;
1120bool acceptBell = false;
1121
1122bool processParams(int argc, char* argv[]) {
1123 for (int i = 1; i < argc; i++) {
1124 if ((strcasecmp(argv[i], "-help") == 0) ||
1125 (strcasecmp(argv[i], "--help") == 0) ||
1126 (strcasecmp(argv[i], "/help") == 0) ||
1127 (strcasecmp(argv[i], "-h") == 0) ||
1128 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001129 (strcasecmp(argv[i], "/?") == 0) ||
1130 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001131 print_usage = true;
1132 return true;
1133 }
1134
1135 if ((strcasecmp(argv[i], "-speed") == 0) ||
1136 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1137 playbackSpeed = atof(argv[++i]);
1138 if (playbackSpeed <= 0) {
1139 return false;
1140 }
1141 continue;
1142 }
1143
1144 if ((strcasecmp(argv[i], "-pos") == 0) ||
1145 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1146 initTime = atol(argv[++i]);
1147 if (initTime <= 0)
1148 return false;
1149 continue;
1150 }
1151
1152 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1153 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001154 autoplay = true;
1155 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001156 }
1157
1158 if ((strcasecmp(argv[i], "-bell") == 0) ||
1159 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001160 acceptBell = true;
1161 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001162 }
1163
1164 if (i != argc - 1)
1165 return false;
1166 }
1167
1168 fileName = strDup(argv[argc-1]);
1169 return true;
1170}
1171
1172//
1173// -=- WinMain
1174//
1175
1176int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1177
1178 // - Process the command-line
1179
1180 int argc = __argc;
1181 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001182 if ((argc > 1) && (!processParams(argc, argv))) {
1183 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1184 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001185 }
george82e6883de2005-02-08 14:42:12 +00001186
1187 if (print_usage) {
1188 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001189 return 0;
george8267cbcd02005-01-16 15:39:56 +00001190 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001191
george82e6883de2005-02-08 14:42:12 +00001192 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001193 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001194 try {
1195 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001196 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001197 } catch (rdr::Exception e) {
1198 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1199 delete player;
1200 return 0;
1201 }
1202
1203 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001204 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001205 MSG msg;
1206 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001207 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1208 TranslateMessage(&msg);
1209 DispatchMessage(&msg);
1210 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001211 }
1212
george82e6883de2005-02-08 14:42:12 +00001213 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001214 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001215 if (player) delete player;
1216 } catch (rdr::Exception e) {
1217 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1218 }
1219
1220 return 0;
1221};