blob: 65c268ed520b28f5b5c806ac40a78653a8affc01 [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"
george82193d8e42005-02-20 16:47:01 +000051 " -depth <bit> \t- Forces the color depth for the session."
52 " \t Supports 8, 16 and 24 bit mode."
george82e6883de2005-02-08 14:42:12 +000053 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
54 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
55 " -pos <ms> \t- Sets initial time position in the session file,\n"
56 " \t in milliseconds. Default: 0.\n"
57 " -autoplay \t- Runs the player in the playback mode.\n"
58 " -bell \t- Accepts the bell.\n";
59
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000060// -=- RfbPlayer's defines
61
62#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000063#define MAX_SPEED 10.00
64#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000065#define MAX_POS_TRACKBAR_RANGE 50
george8268d25142005-02-13 09:33:22 +000066#define DEFAULT_PLAYER_WIDTH 640
67#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000068
george82193d8e42005-02-20 16:47:01 +000069#define DEPTH_AUTO 0
70#define DEPTH8_RGB332 8
71#define DEPTH16_RGB655 16
72#define DEPTH24_RGB888 24
73
george82d070c692005-01-19 16:44:04 +000074#define ID_TOOLBAR 500
75#define ID_PLAY 510
76#define ID_PAUSE 520
77#define ID_TIME_STATIC 530
78#define ID_SPEED_STATIC 540
79#define ID_SPEED_EDIT 550
80#define ID_POS_TRACKBAR 560
81#define ID_SPEED_UPDOWN 570
82
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000083//
84// -=- RfbPlayerClass
85
86//
87// Window class used as the basis for RfbPlayer instance
88//
89
90class RfbPlayerClass {
91public:
92 RfbPlayerClass();
93 ~RfbPlayerClass();
94 ATOM classAtom;
95 HINSTANCE instance;
96};
97
98LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
99 LRESULT result;
100
101 if (msg == WM_CREATE)
102 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
103 else if (msg == WM_DESTROY) {
104 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000105 SetWindowLong(hwnd, GWL_USERDATA, 0);
106 }
107 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
108 if (!_this) {
109 vlog.info("null _this in %x, message %u", hwnd, msg);
110 return DefWindowProc(hwnd, msg, wParam, lParam);
111 }
112
113 try {
114 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
115 } catch (rdr::Exception& e) {
116 vlog.error("untrapped: %s", e.str());
117 }
118
119 return result;
120};
121
122RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
123 WNDCLASS wndClass;
124 wndClass.style = 0;
125 wndClass.lpfnWndProc = RfbPlayerProc;
126 wndClass.cbClsExtra = 0;
127 wndClass.cbWndExtra = 0;
128 wndClass.hInstance = instance = GetModuleHandle(0);
129 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000130 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000131 if (!wndClass.hIcon)
132 printf("unable to load icon:%ld", GetLastError());
133 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
134 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000135 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000136 wndClass.lpszClassName = _T("RfbPlayerClass");
137 classAtom = RegisterClass(&wndClass);
138 if (!classAtom) {
139 throw rdr::SystemException("unable to register RfbPlayer window class",
140 GetLastError());
141 }
142}
143
144RfbPlayerClass::~RfbPlayerClass() {
145 if (classAtom) {
146 UnregisterClass((const TCHAR*)classAtom, instance);
147 }
148}
149
150RfbPlayerClass baseClass;
151
152//
153// -=- RfbFrameClass
154
155//
156// Window class used to displaying the rfb data
157//
158
159class RfbFrameClass {
160public:
161 RfbFrameClass();
162 ~RfbFrameClass();
163 ATOM classAtom;
164 HINSTANCE instance;
165};
166
167LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
168 LRESULT result;
169
170 if (msg == WM_CREATE)
171 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
172 else if (msg == WM_DESTROY)
173 SetWindowLong(hwnd, GWL_USERDATA, 0);
174 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
175 if (!_this) {
176 vlog.info("null _this in %x, message %u", hwnd, msg);
177 return DefWindowProc(hwnd, msg, wParam, lParam);
178 }
179
180 try {
181 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
182 } catch (rdr::Exception& e) {
183 vlog.error("untrapped: %s", e.str());
184 }
185
186 return result;
187}
188
189RfbFrameClass::RfbFrameClass() : classAtom(0) {
190 WNDCLASS wndClass;
191 wndClass.style = 0;
192 wndClass.lpfnWndProc = FrameProc;
193 wndClass.cbClsExtra = 0;
194 wndClass.cbWndExtra = 0;
195 wndClass.hInstance = instance = GetModuleHandle(0);
196 wndClass.hIcon = 0;
197 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
198 wndClass.hbrBackground = 0;
199 wndClass.lpszMenuName = 0;
200 wndClass.lpszClassName = _T("RfbPlayerClass1");
201 classAtom = RegisterClass(&wndClass);
202 if (!classAtom) {
203 throw rdr::SystemException("unable to register RfbPlayer window class",
204 GetLastError());
205 }
206}
207
208RfbFrameClass::~RfbFrameClass() {
209 if (classAtom) {
210 UnregisterClass((const TCHAR*)classAtom, instance);
211 }
212}
213
214RfbFrameClass frameClass;
215
216//
217// -=- RfbPlayer instance implementation
218//
219
george82193d8e42005-02-20 16:47:01 +0000220RfbPlayer::RfbPlayer(char *_fileName, int _depth = DEPTH_AUTO,
221 long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000222 bool _autoplay = false, bool _acceptBell = false)
george82193d8e42005-02-20 16:47:01 +0000223: RfbProto(_fileName), colourDepth(_depth), initTime(_initTime),
224 playbackSpeed(_playbackSpeed), autoplay(_autoplay), buffer(0),
225 client_size(0, 0, 32, 32), window_size(0, 0, 32, 32), cutText(0),
226 seekMode(false), fileName(_fileName), serverInitTime(0), lastPos(0),
227 timeStatic(0), speedEdit(0), posTrackBar(0), speedUpDown(0),
228 acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000229 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000230
george82e6883de2005-02-08 14:42:12 +0000231 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000232
george823c8fbbf2005-01-24 11:09:08 +0000233 // Reset the full session time
234 strcpy(fullSessionTime, "00m:00s");
235
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000236 // Create the main window
237 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000238 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
239 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000240 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000241 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000242 if (!mainHwnd) {
243 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
244 }
245 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
246
247 // Create the backing buffer
248 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000249 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000250
george8217e92cb2005-01-31 16:01:02 +0000251 // Open the session file
252 if (fileName) {
253 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000254 if (initTime > 0) setPos(initTime);
255 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000256 } else {
257 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000258 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000259 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000260}
261
262RfbPlayer::~RfbPlayer() {
263 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000264 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000265 delete rfbReader->join();
266 rfbReader = 0;
267 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000268 if (mainHwnd) {
269 setVisible(false);
270 DestroyWindow(mainHwnd);
271 mainHwnd = 0;
272 }
george825beb62a2005-02-09 13:04:32 +0000273 if (buffer) delete buffer;
274 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000275 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000276 vlog.debug("~RfbPlayer done");
277}
278
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000279LRESULT
280RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
281 switch (msg) {
282
283 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000284
285 case WM_CREATE:
286 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000287 // Create the frame window
288 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
289 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
290 hwnd, 0, frameClass.instance, this);
291
george82d070c692005-01-19 16:44:04 +0000292 createToolBar(hwnd);
293
george82006f2792005-02-05 07:40:47 +0000294 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000295
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000296 return 0;
297 }
298
george827214b822004-12-12 07:02:51 +0000299 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000300
301 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000302 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000303 case ID_OPENFILE:
304 {
305 char curDir[_MAX_DIR];
306 static char filename[_MAX_PATH];
307 OPENFILENAME ofn;
308 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
309 GetCurrentDirectory(sizeof(curDir), curDir);
310
311 ofn.lStructSize = sizeof(OPENFILENAME);
312 ofn.hwndOwner = getMainHandle();
313 ofn.lpstrFile = filename;
314 ofn.nMaxFile = sizeof(filename);
315 ofn.lpstrInitialDir = curDir;
316 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
317 "All files (*.*)\0*.*\0";
318 ofn.lpstrDefExt = "rfb";
319 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
320 if (GetOpenFileName(&ofn))
321 openSessionFile(filename);
322 }
323 break;
george8271ca1772005-02-13 10:50:46 +0000324 case ID_CLOSEFILE:
325 closeSessionFile();
326 break;
george825c13c662005-01-27 14:48:23 +0000327 case ID_PLAY:
328 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000329 break;
330 case ID_PAUSE:
331 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000332 break;
333 case ID_STOP:
334 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000335 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000336 }
george825c13c662005-01-27 14:48:23 +0000337 break;
338 case ID_PLAYPAUSE:
339 if (isPaused()) {
340 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000341 } else {
342 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000343 }
george825c13c662005-01-27 14:48:23 +0000344 break;
george827549df42005-02-08 16:31:02 +0000345 case ID_GOTO:
346 {
347 GotoPosDialog gotoPosDlg;
348 if (gotoPosDlg.showDialog()) {
george821d5d40d2005-02-20 03:25:47 +0000349 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
350 setPos(gotoTime);
351 updatePos(gotoTime);
george827549df42005-02-08 16:31:02 +0000352 }
353 }
354 break;
george825c13c662005-01-27 14:48:23 +0000355 case ID_FULLSCREEN:
356 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
357 break;
george8231a36332005-02-06 17:27:34 +0000358 case ID_LOOP:
359 loopPlayback = !loopPlayback;
360 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
361 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
362 break;
george824ea27f62005-01-29 15:03:06 +0000363 case ID_RETURN:
364 // Update the speed if return pressed in speedEdit
365 if (speedEdit == GetFocus()) {
366 char speedStr[20], *stopStr;
367 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
368 double speed = strtod(speedStr, &stopStr);
369 if (speed > 0) {
370 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000371 } else {
372 speed = getSpeed();
373 }
374 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000375 }
376 break;
george8201aa6732005-02-06 17:13:03 +0000377 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000378 PostQuitMessage(0);
379 break;
george82ef5f7262005-02-08 15:09:26 +0000380 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000381 MessageBox(getMainHandle(),
382 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
383 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000384 }
385 break;
386
387 // Update frame's window size and add scrollbars if required
388
389 case WM_SIZE:
390 {
george82d070c692005-01-19 16:44:04 +0000391
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000392 Point old_offset = bufferToClient(Point(0, 0));
393
394 // Update the cached sizing information
395 RECT r;
396 GetClientRect(getMainHandle(), &r);
397 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
398 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
399
400 GetWindowRect(getFrameHandle(), &r);
401 window_size = Rect(r.left, r.top, r.right, r.bottom);
402 GetClientRect(getFrameHandle(), &r);
403 client_size = Rect(r.left, r.top, r.right, r.bottom);
404
405 // Determine whether scrollbars are required
406 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000407
408 // Resize the ToolBar
409 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000410
411 // Redraw if required
412 if (!old_offset.equals(bufferToClient(Point(0, 0))))
413 InvalidateRect(getFrameHandle(), 0, TRUE);
414 }
415 break;
george828a471482005-02-06 07:15:53 +0000416
417 // Process messages from posTrackBar
418
419 case WM_HSCROLL:
420 {
421 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
422 Pos *= sliderStepMs;
423
424 switch (LOWORD(wParam)) {
425 case TB_PAGEUP:
426 case TB_PAGEDOWN:
427 case TB_LINEUP:
428 case TB_LINEDOWN:
429 case TB_THUMBTRACK:
430 sliderDraging = true;
431 updatePos(Pos);
432 return 0;
433 case TB_ENDTRACK:
434 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000435 sliderDraging = false;
436 return 0;
437 default:
438 break;
439 }
440 }
441 break;
george829e6e6cc2005-01-29 13:12:05 +0000442
443 case WM_NOTIFY:
444 switch (((NMHDR*)lParam)->code) {
445 case UDN_DELTAPOS:
446 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000447 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000448 char speedStr[20] = "\0";
449 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
450 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
451 double speed;
452
george824ea27f62005-01-29 15:03:06 +0000453 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000454 if (upDown->iDelta > 0) {
455 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
456 } else {
george824ea27f62005-01-29 15:03:06 +0000457 // It's need to round the UpDown position
458 if ((upDown->iPos * 0.5) != getSpeed()) {
459 upDown->iDelta = 0;
460 lResult = TRUE;
461 }
george829e6e6cc2005-01-29 13:12:05 +0000462 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
463 }
george829e6e6cc2005-01-29 13:12:05 +0000464 sprintf(speedStr, "%.2f", speed);
465 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000466 is->setSpeed(speed);
467 playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000468 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000469 }
george824ea27f62005-01-29 15:03:06 +0000470 }
george829e6e6cc2005-01-29 13:12:05 +0000471 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000472
473 case WM_CLOSE:
474 vlog.debug("WM_CLOSE %x", getMainHandle());
475 PostQuitMessage(0);
476 break;
477 }
478
479 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
480}
481
482LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
483 switch (msg) {
484
485 case WM_PAINT:
486 {
george825beb62a2005-02-09 13:04:32 +0000487 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000488 seekMode = true;
489 return 0;
490 } else {
491 if (seekMode) {
492 seekMode = false;
493 InvalidateRect(getFrameHandle(), 0, true);
494 UpdateWindow(getFrameHandle());
495 return 0;
496 }
497 }
498
499 PAINTSTRUCT ps;
500 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
501 if (!paintDC)
502 throw SystemException("unable to BeginPaint", GetLastError());
503 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
504
505 if (!pr.is_empty()) {
506
507 if (buffer->bitmap) {
508
509 // Get device context
510 BitmapDC bitmapDC(paintDC, buffer->bitmap);
511
512 // Blit the border if required
513 Rect bufpos = bufferToClient(buffer->getRect());
514 if (!pr.enclosed_by(bufpos)) {
515 vlog.debug("draw border");
516 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
517 RECT r;
518 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
519 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
520 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
521 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
522 }
523
524 // Do the blit
525 Point buf_pos = clientToBuffer(pr.tl);
526 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
527 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
528 throw SystemException("unable to BitBlt to window", GetLastError());
529
530 } else {
531 // Blit a load of black
532 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
533 0, 0, 0, BLACKNESS))
534 throw SystemException("unable to BitBlt to blank window", GetLastError());
535 }
536 }
537 EndPaint(getFrameHandle(), &ps);
538 }
539 return 0;
540
541 case WM_VSCROLL:
542 case WM_HSCROLL:
543 {
544 Point delta;
545 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
546
547 switch (LOWORD(wParam)) {
548 case SB_PAGEUP: newpos -= 50; break;
549 case SB_PAGEDOWN: newpos += 50; break;
550 case SB_LINEUP: newpos -= 5; break;
551 case SB_LINEDOWN: newpos += 5; break;
552 case SB_THUMBTRACK:
553 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
554 default: vlog.info("received unknown scroll message");
555 };
556
557 if (msg == WM_HSCROLL)
558 setViewportOffset(Point(newpos, scrolloffset.y));
559 else
560 setViewportOffset(Point(scrolloffset.x, newpos));
561
562 SCROLLINFO si;
563 si.cbSize = sizeof(si);
564 si.fMask = SIF_POS;
565 si.nPos = newpos;
566 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
567 }
568 break;
569 }
570
571 return DefWindowProc(hwnd, msg, wParam, lParam);
572}
573
george82d070c692005-01-19 16:44:04 +0000574void RfbPlayer::createToolBar(HWND parentHwnd) {
575 RECT tRect;
576 InitCommonControls();
577
578 tb.create(ID_TOOLBAR, parentHwnd);
579 tb.addBitmap(4, IDB_TOOLBAR);
580
581 // Create the control buttons
582 tb.addButton(0, ID_PLAY);
583 tb.addButton(1, ID_PAUSE);
584 tb.addButton(2, ID_STOP);
585 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
586 tb.addButton(3, ID_FULLSCREEN);
587 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
588
589 // Create the static control for the time output
590 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
591 tb.getButtonRect(6, &tRect);
592 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
593 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
594 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
595 GetModuleHandle(0), 0);
596 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
597
598 // Create the trackbar control for the time position
599 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
600 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000601 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000602 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
603 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
604 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
605 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000606 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000607 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
608
609 // Create the label with "Speed:" caption
610 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
611 tb.getButtonRect(10, &tRect);
612 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
613 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
614 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
615
616 // Create the edit control and the spin for the speed managing
617 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
618 tb.getButtonRect(11, &tRect);
619 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
620 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
621 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
622 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
623 // It's need to send notify messages to toolbar parent window
624 SetParent(speedEdit, tb.getHandle());
625
626 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
627 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000628 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000629}
630
george82a21d2952005-02-12 11:30:03 +0000631void RfbPlayer::disableTBandMenuItems() {
632 // Disable the menu items
633 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
634 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
635 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
636 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
637 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
638 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
639 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
640 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
641 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
642
643 // Disable the toolbar buttons and child controls
644 tb.enableButton(ID_PLAY, false);
645 tb.enableButton(ID_PAUSE, false);
646 tb.enableButton(ID_STOP, false);
647 tb.enableButton(ID_FULLSCREEN, false);
648 EnableWindow(posTrackBar, false);
649 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000650 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000651}
652
george82f5043162005-02-12 11:37:18 +0000653void RfbPlayer::enableTBandMenuItems() {
654 // Enable the menu items
655 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
656 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
657 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
658 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
659 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
660 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
661 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
662 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
663 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
664
665 // Enable the toolbar buttons and child controls
666 tb.enableButton(ID_PLAY, true);
667 tb.enableButton(ID_PAUSE, true);
668 tb.enableButton(ID_STOP, true);
669 tb.enableButton(ID_FULLSCREEN, true);
670 EnableWindow(posTrackBar, true);
671 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000672 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000673}
674
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000675void RfbPlayer::setVisible(bool visible) {
676 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
677 if (visible) {
678 // When the window becomes visible, make it active
679 SetForegroundWindow(getMainHandle());
680 SetActiveWindow(getMainHandle());
681 }
682}
683
684void RfbPlayer::setTitle(const char *title) {
685 char _title[256];
686 strcpy(_title, AppName);
687 strcat(_title, " - ");
688 strcat(_title, title);
689 SetWindowText(getMainHandle(), _title);
690}
691
692void RfbPlayer::setFrameSize(int width, int height) {
693 // Calculate and set required size for main window
694 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000695 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000696 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
697 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
698 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000699 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
700 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
701 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000702 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
703
704 // Enable/disable scrollbars as appropriate
705 calculateScrollBars();
706}
707
708void RfbPlayer::calculateScrollBars() {
709 // Calculate the required size of window
710 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
711 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
712 DWORD old_style;
713 RECT r;
714 SetRect(&r, 0, 0, buffer->width(), buffer->height());
715 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
716 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
717
718 // Work out whether scroll bars are required
719 do {
720 old_style = style;
721
722 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
723 style |= WS_HSCROLL;
724 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
725 }
726 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
727 style |= WS_VSCROLL;
728 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
729 }
730 } while (style != old_style);
731
732 // Tell Windows to update the window style & cached settings
733 if (style != current_style) {
734 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
735 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
736 }
737
738 // Update the scroll settings
739 SCROLLINFO si;
740 if (style & WS_VSCROLL) {
741 si.cbSize = sizeof(si);
742 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
743 si.nMin = 0;
744 si.nMax = buffer->height();
745 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
746 maxscrolloffset.y = max(0, si.nMax-si.nPage);
747 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
748 si.nPos = scrolloffset.y;
749 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
750 }
751 if (style & WS_HSCROLL) {
752 si.cbSize = sizeof(si);
753 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
754 si.nMin = 0;
755 si.nMax = buffer->width();
756 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
757 maxscrolloffset.x = max(0, si.nMax-si.nPage);
758 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
759 si.nPos = scrolloffset.x;
760 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
761 }
762}
763
764bool RfbPlayer::setViewportOffset(const Point& tl) {
765/* ***
766 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
767 max(0, min(maxscrolloffset.y, tl.y)));
768 */
769 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
770 max(0, min(tl.y, buffer->height()-client_size.height())));
771 Point delta = np.translate(scrolloffset.negate());
772 if (!np.equals(scrolloffset)) {
773 scrolloffset = np;
774 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
775 UpdateWindow(getFrameHandle());
776 return true;
777 }
778 return false;
779}
780
781void RfbPlayer::close(const char* reason) {
782 setVisible(false);
783 if (reason) {
784 vlog.info("closing - %s", reason);
785 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
786 }
787 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
788}
789
790void RfbPlayer::blankBuffer() {
791 fillRect(buffer->getRect(), 0);
792}
793
794void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000795 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000796 blankBuffer();
797 newSession(fileName);
798 skipHandshaking();
george825f326fe2005-02-20 08:01:01 +0000799 is->setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000800 if (paused) is->pausePlayback();
801 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000802}
803
804void RfbPlayer::processMsg() {
805 static long update_time = GetTickCount();
806 try {
george828a471482005-02-06 07:15:53 +0000807 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
808 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000809 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000810 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000811 update_time = GetTickCount();
812 }
813 RfbProto::processMsg();
814 } catch (rdr::Exception e) {
815 if (strcmp(e.str(), "[End Of File]") == 0) {
816 rewind();
george8231a36332005-02-06 17:27:34 +0000817 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000818 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000819 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000820 return;
821 }
822 // It's a special exception to perform backward seeking.
823 // We only rewind the stream and seek the offset
824 if (strcmp(e.str(), "[REWIND]") == 0) {
825 long initTime = getSeekOffset();
826 rewind();
827 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000828 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000829 } else {
830 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
831 return;
832 }
833 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000834}
835
836void RfbPlayer::serverInit() {
837 RfbProto::serverInit();
838
839 // Save the server init time for using in setPos()
840 serverInitTime = getTimeOffset() / getSpeed();
841
842 // Resize the backing buffer
843 buffer->setSize(cp.width, cp.height);
844
845 // Check on the true colour mode
846 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000847 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000848
849 // Set the session pixel format
george82193d8e42005-02-20 16:47:01 +0000850 switch (colourDepth) {
851 case DEPTH_AUTO:
852 break;
853 case DEPTH8_RGB332:
854 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
855 break;
856 case DEPTH16_RGB655:
857 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
858 break;
859 case DEPTH24_RGB888:
860 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
861 break;
862 default:
863 throw rdr::Exception("This color depth is not supported!");
864 }
865 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000866
867 // If the window is not maximised then resize it
868 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
869 setFrameSize(cp.width, cp.height);
870
871 // Set the window title and show it
872 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000873
george82d4d69e62005-02-05 09:23:18 +0000874 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000875 sessionTimeMs = calculateSessionTime(fileName);
876 sprintf(fullSessionTime, "%.2um:%.2us",
877 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000878 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000879 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
880 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000881 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000882
george82006f2792005-02-05 07:40:47 +0000883 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000884}
885
886void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
887 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
888 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
889/* int i;
890 for (i=0;i<count;i++) {
891 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
892 }
893 // *** change to 0, 256?
894 refreshWindowPalette(first, count);
895 palette_changed = true;
896 InvalidateRect(getFrameHandle(), 0, FALSE);*/
897}
898
899void RfbPlayer::bell() {
900 if (acceptBell)
901 MessageBeep(-1);
902}
903
904void RfbPlayer::serverCutText(const char* str, int len) {
905 if (cutText != NULL)
906 delete [] cutText;
907 cutText = new char[len + 1];
908 memcpy(cutText, str, len);
909 cutText[len] = '\0';
910}
911
912void RfbPlayer::frameBufferUpdateEnd() {
913};
914
915void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
916}
917
918void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
919}
920
921
922void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
923 buffer->fillRect(r, pix);
924 invalidateBufferRect(r);
925}
926
927void RfbPlayer::imageRect(const Rect& r, void* pixels) {
928 buffer->imageRect(r, pixels);
929 invalidateBufferRect(r);
930}
931
932void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
933 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
934 invalidateBufferRect(r);
935}
936
937bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
938 Rect rect = bufferToClient(crect);
939 if (rect.intersect(client_size).is_empty()) return false;
940 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
941 InvalidateRect(getFrameHandle(), &invalid, FALSE);
942 return true;
943}
944
george8257f13522005-02-05 08:48:22 +0000945long RfbPlayer::calculateSessionTime(char *filename) {
946 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000947 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000948 try {
949 while (TRUE) {
950 sessionFile.skip(1024);
951 }
952 } catch (rdr::Exception e) {
953 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000954 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000955 } else {
956 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
957 return 0;
958 }
959 }
960 return 0;
961}
962
george826b87aff2005-02-13 10:48:21 +0000963void RfbPlayer::closeSessionFile() {
964 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000965 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000966 RECT r;
967
968 // Uncheck all toolbar buttons
969 if (tb.getHandle()) {
970 tb.checkButton(ID_PLAY, false);
971 tb.checkButton(ID_PAUSE, false);
972 tb.checkButton(ID_STOP, false);
973 }
974
975 // Stop playback and update the player state
976 disableTBandMenuItems();
977 if (rfbReader) {
978 delete rfbReader->join();
979 rfbReader = 0;
980 delete [] fileName;
981 fileName = 0;
982 }
983 blankBuffer();
984 setTitle("None");
george827009c892005-02-19 12:49:42 +0000985 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +0000986 playbackSpeed = 1.0;
987 SendMessage(speedUpDown, UDM_SETPOS,
988 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
989 sprintf(speedStr, "%.2f", playbackSpeed);
990 SetWindowText(speedEdit, speedStr);
991 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
992
993 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +0000994 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
995 dwStyle &= ~WS_MAXIMIZE;
996 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
997 }
george822ff7a612005-02-19 17:05:24 +0000998 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
999 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1000 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001001 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001002 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001003 buffer->setSize(32, 32);
1004 calculateScrollBars();
1005
1006 // Update the cached sizing information and repaint the frame window
1007 GetWindowRect(getFrameHandle(), &r);
1008 window_size = Rect(r.left, r.top, r.right, r.bottom);
1009 GetClientRect(getFrameHandle(), &r);
1010 client_size = Rect(r.left, r.top, r.right, r.bottom);
1011 InvalidateRect(getFrameHandle(), 0, TRUE);
1012 UpdateWindow(getFrameHandle());
1013}
1014
george8217e92cb2005-01-31 16:01:02 +00001015void RfbPlayer::openSessionFile(char *_fileName) {
1016 fileName = strDup(_fileName);
1017
1018 // Close the previous reading thread
1019 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001020 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001021 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001022 }
1023 blankBuffer();
1024 newSession(fileName);
1025 setSpeed(playbackSpeed);
1026 rfbReader = new rfbSessionReader(this);
1027 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001028 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001029 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001030}
1031
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001032void RfbPlayer::setPaused(bool paused) {
1033 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001034 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001035 tb.checkButton(ID_PAUSE, true);
1036 tb.checkButton(ID_PLAY, false);
1037 tb.checkButton(ID_STOP, false);
1038 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1039 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001040 } else {
george825beb62a2005-02-09 13:04:32 +00001041 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001042 tb.checkButton(ID_PLAY, true);
1043 tb.checkButton(ID_STOP, false);
1044 tb.checkButton(ID_PAUSE, false);
1045 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1046 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001047 }
1048}
1049
george82006f2792005-02-05 07:40:47 +00001050void RfbPlayer::stopPlayback() {
1051 setPos(0);
george825beb62a2005-02-09 13:04:32 +00001052 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001053 tb.checkButton(ID_STOP, true);
1054 tb.checkButton(ID_PLAY, false);
1055 tb.checkButton(ID_PAUSE, false);
1056 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1057 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001058 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001059}
1060
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001061void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001062 if (speed > 0) {
1063 char speedStr[20] = "\0";
1064 double newSpeed = min(speed, MAX_SPEED);
1065 serverInitTime = serverInitTime * getSpeed() / newSpeed;
1066 is->setSpeed(newSpeed);
1067 playbackSpeed = newSpeed;
1068 SendMessage(speedUpDown, UDM_SETPOS,
1069 0, MAKELONG((short)(newSpeed / 0.5), 0));
1070 sprintf(speedStr, "%.2f", newSpeed);
1071 SetWindowText(speedEdit, speedStr);
1072 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001073}
1074
1075double RfbPlayer::getSpeed() {
1076 return is->getSpeed();
1077}
1078
1079void RfbPlayer::setPos(long pos) {
1080 is->setTimeOffset(max(pos, serverInitTime));
1081}
1082
1083long RfbPlayer::getSeekOffset() {
1084 return is->getSeekOffset();
1085}
1086
1087bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001088 if (is) return is->isSeeking();
1089 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001090}
1091
1092bool RfbPlayer::isSeekMode() {
1093 return seekMode;
1094}
1095
1096bool RfbPlayer::isPaused() {
1097 return is->isPaused();
1098}
1099
1100long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001101 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001102}
1103
george828a471482005-02-06 07:15:53 +00001104void RfbPlayer::updatePos(long newPos) {
1105 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001106 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001107 long time = newPos / 1000;
1108 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001109 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001110
1111 // Update the position of slider
1112 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001113 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1114 sliderStepMs / double(newPos);
1115 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1116 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1117 }
george828a471482005-02-06 07:15:53 +00001118 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001119}
1120
1121void RfbPlayer::skipHandshaking() {
1122 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1123 is->skip(skipBytes);
1124 state_ = RFBSTATE_NORMAL;
1125}
1126
1127void programInfo() {
1128 win32::FileVersionInfo inf;
1129 _tprintf(_T("%s - %s, Version %s\n"),
1130 inf.getVerString(_T("ProductName")),
1131 inf.getVerString(_T("FileDescription")),
1132 inf.getVerString(_T("FileVersion")));
1133 printf("%s\n", buildTime);
1134 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1135}
1136
1137void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001138 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001139}
1140
1141double playbackSpeed = 1.0;
1142long initTime = -1;
george82193d8e42005-02-20 16:47:01 +00001143int depth = DEPTH_AUTO;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001144bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001145char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001146bool print_usage = false;
1147bool acceptBell = false;
1148
1149bool processParams(int argc, char* argv[]) {
1150 for (int i = 1; i < argc; i++) {
1151 if ((strcasecmp(argv[i], "-help") == 0) ||
1152 (strcasecmp(argv[i], "--help") == 0) ||
1153 (strcasecmp(argv[i], "/help") == 0) ||
1154 (strcasecmp(argv[i], "-h") == 0) ||
1155 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001156 (strcasecmp(argv[i], "/?") == 0) ||
1157 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001158 print_usage = true;
1159 return true;
1160 }
1161
george82193d8e42005-02-20 16:47:01 +00001162 if ((strcasecmp(argv[i], "-depth") == 0) ||
1163 (strcasecmp(argv[i], "/depth") == 0) && (i < argc-1)) {
1164 depth = atoi(argv[++i]);
1165 if (depth < 0) {
1166 return false;
1167 }
1168 continue;
1169 }
1170
1171
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001172 if ((strcasecmp(argv[i], "-speed") == 0) ||
1173 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1174 playbackSpeed = atof(argv[++i]);
1175 if (playbackSpeed <= 0) {
1176 return false;
1177 }
1178 continue;
1179 }
1180
1181 if ((strcasecmp(argv[i], "-pos") == 0) ||
1182 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1183 initTime = atol(argv[++i]);
1184 if (initTime <= 0)
1185 return false;
1186 continue;
1187 }
1188
1189 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1190 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001191 autoplay = true;
1192 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001193 }
1194
1195 if ((strcasecmp(argv[i], "-bell") == 0) ||
1196 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001197 acceptBell = true;
1198 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001199 }
1200
1201 if (i != argc - 1)
1202 return false;
1203 }
1204
1205 fileName = strDup(argv[argc-1]);
1206 return true;
1207}
1208
1209//
1210// -=- WinMain
1211//
1212
1213int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1214
1215 // - Process the command-line
1216
1217 int argc = __argc;
1218 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001219 if ((argc > 1) && (!processParams(argc, argv))) {
1220 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1221 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001222 }
george82e6883de2005-02-08 14:42:12 +00001223
1224 if (print_usage) {
1225 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001226 return 0;
george8267cbcd02005-01-16 15:39:56 +00001227 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001228
george82e6883de2005-02-08 14:42:12 +00001229 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001230 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001231 try {
george82193d8e42005-02-20 16:47:01 +00001232 player = new RfbPlayer(fileName, depth, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001233 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001234 } catch (rdr::Exception e) {
1235 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1236 delete player;
1237 return 0;
1238 }
1239
1240 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001241 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001242 MSG msg;
1243 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001244 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1245 TranslateMessage(&msg);
1246 DispatchMessage(&msg);
1247 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001248 }
1249
george82e6883de2005-02-08 14:42:12 +00001250 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001251 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001252 if (player) delete player;
1253 } catch (rdr::Exception e) {
1254 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1255 }
1256
1257 return 0;
1258};