blob: 855e402b2e0d5747b64d16ab1ca3d97b6aa0d119 [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>
george823d0c56e2005-03-09 08:30:27 +000032#include <rfbplayer/ChoosePixelFormatDialog.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000033
34using namespace rfb;
35using namespace rfb::win32;
36
37// -=- Variables & consts
38
39static LogWriter vlog("RfbPlayer");
40
41TStr rfb::win32::AppName("RfbPlayer");
42extern const char* buildTime;
43
george82e6883de2005-02-08 14:42:12 +000044char wrong_cmd_msg[] =
45 "Wrong command-line parameters!\n"
46 "Use for help: rfbplayer -help";
47
48char usage_msg[] =
49 "usage: rfbplayer <options> <filename>\n"
50 "Command-line options:\n"
51 " -help \t- Provide usage information.\n"
george82057a4472005-02-21 13:23:56 +000052 " -depth <bit> \t- Forces the color depth for the session.\n"
53 " \t Supports 8, 16 and 24 bit mode.\n"
george82e6883de2005-02-08 14:42:12 +000054 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
55 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
56 " -pos <ms> \t- Sets initial time position in the session file,\n"
57 " \t in milliseconds. Default: 0.\n"
58 " -autoplay \t- Runs the player in the playback mode.\n"
59 " -bell \t- Accepts the bell.\n";
60
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000061// -=- RfbPlayer's defines
62
63#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000064#define MAX_SPEED 10.00
65#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000066#define MAX_POS_TRACKBAR_RANGE 50
george8268d25142005-02-13 09:33:22 +000067#define DEFAULT_PLAYER_WIDTH 640
68#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000069
george82193d8e42005-02-20 16:47:01 +000070#define DEPTH_AUTO 0
71#define DEPTH8_RGB332 8
72#define DEPTH16_RGB655 16
73#define DEPTH24_RGB888 24
74
george82d070c692005-01-19 16:44:04 +000075#define ID_TOOLBAR 500
76#define ID_PLAY 510
77#define ID_PAUSE 520
78#define ID_TIME_STATIC 530
79#define ID_SPEED_STATIC 540
80#define ID_SPEED_EDIT 550
81#define ID_POS_TRACKBAR 560
82#define ID_SPEED_UPDOWN 570
83
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000084//
85// -=- RfbPlayerClass
86
87//
88// Window class used as the basis for RfbPlayer instance
89//
90
91class RfbPlayerClass {
92public:
93 RfbPlayerClass();
94 ~RfbPlayerClass();
95 ATOM classAtom;
96 HINSTANCE instance;
97};
98
99LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
100 LRESULT result;
101
102 if (msg == WM_CREATE)
103 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
104 else if (msg == WM_DESTROY) {
105 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000106 SetWindowLong(hwnd, GWL_USERDATA, 0);
107 }
108 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
109 if (!_this) {
110 vlog.info("null _this in %x, message %u", hwnd, msg);
111 return DefWindowProc(hwnd, msg, wParam, lParam);
112 }
113
114 try {
115 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
116 } catch (rdr::Exception& e) {
117 vlog.error("untrapped: %s", e.str());
118 }
119
120 return result;
121};
122
123RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
124 WNDCLASS wndClass;
125 wndClass.style = 0;
126 wndClass.lpfnWndProc = RfbPlayerProc;
127 wndClass.cbClsExtra = 0;
128 wndClass.cbWndExtra = 0;
129 wndClass.hInstance = instance = GetModuleHandle(0);
130 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000131 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000132 if (!wndClass.hIcon)
133 printf("unable to load icon:%ld", GetLastError());
134 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
135 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000136 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000137 wndClass.lpszClassName = _T("RfbPlayerClass");
138 classAtom = RegisterClass(&wndClass);
139 if (!classAtom) {
140 throw rdr::SystemException("unable to register RfbPlayer window class",
141 GetLastError());
142 }
143}
144
145RfbPlayerClass::~RfbPlayerClass() {
146 if (classAtom) {
147 UnregisterClass((const TCHAR*)classAtom, instance);
148 }
149}
150
151RfbPlayerClass baseClass;
152
153//
154// -=- RfbFrameClass
155
156//
157// Window class used to displaying the rfb data
158//
159
160class RfbFrameClass {
161public:
162 RfbFrameClass();
163 ~RfbFrameClass();
164 ATOM classAtom;
165 HINSTANCE instance;
166};
167
168LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
169 LRESULT result;
170
171 if (msg == WM_CREATE)
172 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
173 else if (msg == WM_DESTROY)
174 SetWindowLong(hwnd, GWL_USERDATA, 0);
175 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
176 if (!_this) {
177 vlog.info("null _this in %x, message %u", hwnd, msg);
178 return DefWindowProc(hwnd, msg, wParam, lParam);
179 }
180
181 try {
182 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
183 } catch (rdr::Exception& e) {
184 vlog.error("untrapped: %s", e.str());
185 }
186
187 return result;
188}
189
190RfbFrameClass::RfbFrameClass() : classAtom(0) {
191 WNDCLASS wndClass;
192 wndClass.style = 0;
193 wndClass.lpfnWndProc = FrameProc;
194 wndClass.cbClsExtra = 0;
195 wndClass.cbWndExtra = 0;
196 wndClass.hInstance = instance = GetModuleHandle(0);
197 wndClass.hIcon = 0;
198 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
199 wndClass.hbrBackground = 0;
200 wndClass.lpszMenuName = 0;
201 wndClass.lpszClassName = _T("RfbPlayerClass1");
202 classAtom = RegisterClass(&wndClass);
203 if (!classAtom) {
204 throw rdr::SystemException("unable to register RfbPlayer window class",
205 GetLastError());
206 }
207}
208
209RfbFrameClass::~RfbFrameClass() {
210 if (classAtom) {
211 UnregisterClass((const TCHAR*)classAtom, instance);
212 }
213}
214
215RfbFrameClass frameClass;
216
217//
218// -=- RfbPlayer instance implementation
219//
220
george82193d8e42005-02-20 16:47:01 +0000221RfbPlayer::RfbPlayer(char *_fileName, int _depth = DEPTH_AUTO,
222 long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000223 bool _autoplay = false, bool _acceptBell = false)
george82193d8e42005-02-20 16:47:01 +0000224: RfbProto(_fileName), colourDepth(_depth), initTime(_initTime),
225 playbackSpeed(_playbackSpeed), autoplay(_autoplay), buffer(0),
226 client_size(0, 0, 32, 32), window_size(0, 0, 32, 32), cutText(0),
george823104aec2005-02-21 13:20:56 +0000227 seekMode(false), fileName(_fileName), lastPos(0), timeStatic(0),
228 speedEdit(0), posTrackBar(0), speedUpDown(0), acceptBell(_acceptBell),
229 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george820d2e19d2005-03-03 15:47:55 +0000230 loopPlayback(false), imageDataStartTime(0), rewindFlag(false),
231 stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000232
george82e6883de2005-02-08 14:42:12 +0000233 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000234
george823c8fbbf2005-01-24 11:09:08 +0000235 // Reset the full session time
236 strcpy(fullSessionTime, "00m:00s");
237
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000238 // Create the main window
239 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000240 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
241 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000242 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000243 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000244 if (!mainHwnd) {
245 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
246 }
247 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
248
249 // Create the backing buffer
250 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000251 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000252
george8217e92cb2005-01-31 16:01:02 +0000253 // Open the session file
254 if (fileName) {
255 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000256 if (initTime > 0) setPos(initTime);
257 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000258 } else {
259 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000260 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000261 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000262}
263
264RfbPlayer::~RfbPlayer() {
265 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000266 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000267 delete rfbReader->join();
268 rfbReader = 0;
269 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000270 if (mainHwnd) {
271 setVisible(false);
272 DestroyWindow(mainHwnd);
273 mainHwnd = 0;
274 }
george825beb62a2005-02-09 13:04:32 +0000275 if (buffer) delete buffer;
276 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000277 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000278 vlog.debug("~RfbPlayer done");
279}
280
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000281LRESULT
282RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
283 switch (msg) {
284
285 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000286
287 case WM_CREATE:
288 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000289 // Create the frame window
290 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
291 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
292 hwnd, 0, frameClass.instance, this);
293
george82d070c692005-01-19 16:44:04 +0000294 createToolBar(hwnd);
295
george82006f2792005-02-05 07:40:47 +0000296 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000297
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000298 return 0;
299 }
300
george827214b822004-12-12 07:02:51 +0000301 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000302
303 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000304 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000305 case ID_OPENFILE:
306 {
307 char curDir[_MAX_DIR];
308 static char filename[_MAX_PATH];
309 OPENFILENAME ofn;
310 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
311 GetCurrentDirectory(sizeof(curDir), curDir);
312
313 ofn.lStructSize = sizeof(OPENFILENAME);
314 ofn.hwndOwner = getMainHandle();
315 ofn.lpstrFile = filename;
316 ofn.nMaxFile = sizeof(filename);
317 ofn.lpstrInitialDir = curDir;
318 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
319 "All files (*.*)\0*.*\0";
320 ofn.lpstrDefExt = "rfb";
321 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000322 if (GetOpenFileName(&ofn)) {
323 colourDepth = DEPTH_AUTO;
george826e51fcc2005-02-06 13:30:49 +0000324 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000325 }
george826e51fcc2005-02-06 13:30:49 +0000326 }
327 break;
george8271ca1772005-02-13 10:50:46 +0000328 case ID_CLOSEFILE:
329 closeSessionFile();
330 break;
george825c13c662005-01-27 14:48:23 +0000331 case ID_PLAY:
332 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000333 break;
334 case ID_PAUSE:
335 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000336 break;
337 case ID_STOP:
338 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000339 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000340 }
george825c13c662005-01-27 14:48:23 +0000341 break;
342 case ID_PLAYPAUSE:
343 if (isPaused()) {
344 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000345 } else {
346 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000347 }
george825c13c662005-01-27 14:48:23 +0000348 break;
george827549df42005-02-08 16:31:02 +0000349 case ID_GOTO:
350 {
351 GotoPosDialog gotoPosDlg;
352 if (gotoPosDlg.showDialog()) {
george821d5d40d2005-02-20 03:25:47 +0000353 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
354 setPos(gotoTime);
355 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000356 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000357 }
358 }
359 break;
george825c13c662005-01-27 14:48:23 +0000360 case ID_FULLSCREEN:
361 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
362 break;
george8231a36332005-02-06 17:27:34 +0000363 case ID_LOOP:
364 loopPlayback = !loopPlayback;
365 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
366 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
367 break;
george824ea27f62005-01-29 15:03:06 +0000368 case ID_RETURN:
369 // Update the speed if return pressed in speedEdit
370 if (speedEdit == GetFocus()) {
371 char speedStr[20], *stopStr;
372 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
373 double speed = strtod(speedStr, &stopStr);
374 if (speed > 0) {
375 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000376 } else {
377 speed = getSpeed();
378 }
379 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000380 }
381 break;
george8201aa6732005-02-06 17:13:03 +0000382 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000383 PostQuitMessage(0);
384 break;
george82ef5f7262005-02-08 15:09:26 +0000385 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000386 MessageBox(getMainHandle(),
387 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
388 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000389 }
390 break;
391
392 // Update frame's window size and add scrollbars if required
393
394 case WM_SIZE:
395 {
george82d070c692005-01-19 16:44:04 +0000396
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000397 Point old_offset = bufferToClient(Point(0, 0));
398
399 // Update the cached sizing information
400 RECT r;
401 GetClientRect(getMainHandle(), &r);
402 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
403 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
404
405 GetWindowRect(getFrameHandle(), &r);
406 window_size = Rect(r.left, r.top, r.right, r.bottom);
407 GetClientRect(getFrameHandle(), &r);
408 client_size = Rect(r.left, r.top, r.right, r.bottom);
409
410 // Determine whether scrollbars are required
411 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000412
413 // Resize the ToolBar
414 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000415
416 // Redraw if required
417 if (!old_offset.equals(bufferToClient(Point(0, 0))))
418 InvalidateRect(getFrameHandle(), 0, TRUE);
419 }
420 break;
george828a471482005-02-06 07:15:53 +0000421
422 // Process messages from posTrackBar
423
424 case WM_HSCROLL:
425 {
426 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
427 Pos *= sliderStepMs;
428
429 switch (LOWORD(wParam)) {
430 case TB_PAGEUP:
431 case TB_PAGEDOWN:
432 case TB_LINEUP:
433 case TB_LINEDOWN:
434 case TB_THUMBTRACK:
435 sliderDraging = true;
436 updatePos(Pos);
437 return 0;
438 case TB_ENDTRACK:
439 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000440 setPaused(isPaused());;
george828a471482005-02-06 07:15:53 +0000441 sliderDraging = false;
442 return 0;
443 default:
444 break;
445 }
446 }
447 break;
george829e6e6cc2005-01-29 13:12:05 +0000448
449 case WM_NOTIFY:
450 switch (((NMHDR*)lParam)->code) {
451 case UDN_DELTAPOS:
452 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000453 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000454 char speedStr[20] = "\0";
455 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
456 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
457 double speed;
458
george824ea27f62005-01-29 15:03:06 +0000459 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000460 if (upDown->iDelta > 0) {
461 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
462 } else {
george824ea27f62005-01-29 15:03:06 +0000463 // It's need to round the UpDown position
464 if ((upDown->iPos * 0.5) != getSpeed()) {
465 upDown->iDelta = 0;
466 lResult = TRUE;
467 }
george829e6e6cc2005-01-29 13:12:05 +0000468 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
469 }
george829e6e6cc2005-01-29 13:12:05 +0000470 sprintf(speedStr, "%.2f", speed);
471 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000472 is->setSpeed(speed);
473 playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000474 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000475 }
george824ea27f62005-01-29 15:03:06 +0000476 }
george829e6e6cc2005-01-29 13:12:05 +0000477 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000478
479 case WM_CLOSE:
480 vlog.debug("WM_CLOSE %x", getMainHandle());
481 PostQuitMessage(0);
482 break;
483 }
484
485 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
486}
487
488LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
489 switch (msg) {
490
491 case WM_PAINT:
492 {
george821e846ff2005-02-24 13:13:33 +0000493 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000494 seekMode = true;
495 return 0;
496 } else {
497 if (seekMode) {
498 seekMode = false;
499 InvalidateRect(getFrameHandle(), 0, true);
500 UpdateWindow(getFrameHandle());
501 return 0;
502 }
503 }
504
505 PAINTSTRUCT ps;
506 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
507 if (!paintDC)
508 throw SystemException("unable to BeginPaint", GetLastError());
509 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
510
511 if (!pr.is_empty()) {
512
513 if (buffer->bitmap) {
514
515 // Get device context
516 BitmapDC bitmapDC(paintDC, buffer->bitmap);
517
518 // Blit the border if required
519 Rect bufpos = bufferToClient(buffer->getRect());
520 if (!pr.enclosed_by(bufpos)) {
521 vlog.debug("draw border");
522 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
523 RECT r;
524 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
525 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
526 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
527 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
528 }
529
530 // Do the blit
531 Point buf_pos = clientToBuffer(pr.tl);
532 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
533 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
534 throw SystemException("unable to BitBlt to window", GetLastError());
535
536 } else {
537 // Blit a load of black
538 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
539 0, 0, 0, BLACKNESS))
540 throw SystemException("unable to BitBlt to blank window", GetLastError());
541 }
542 }
543 EndPaint(getFrameHandle(), &ps);
544 }
545 return 0;
546
547 case WM_VSCROLL:
548 case WM_HSCROLL:
549 {
550 Point delta;
551 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
552
553 switch (LOWORD(wParam)) {
554 case SB_PAGEUP: newpos -= 50; break;
555 case SB_PAGEDOWN: newpos += 50; break;
556 case SB_LINEUP: newpos -= 5; break;
557 case SB_LINEDOWN: newpos += 5; break;
558 case SB_THUMBTRACK:
559 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
560 default: vlog.info("received unknown scroll message");
561 };
562
563 if (msg == WM_HSCROLL)
564 setViewportOffset(Point(newpos, scrolloffset.y));
565 else
566 setViewportOffset(Point(scrolloffset.x, newpos));
567
568 SCROLLINFO si;
569 si.cbSize = sizeof(si);
570 si.fMask = SIF_POS;
571 si.nPos = newpos;
572 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
573 }
574 break;
575 }
576
577 return DefWindowProc(hwnd, msg, wParam, lParam);
578}
579
george82d070c692005-01-19 16:44:04 +0000580void RfbPlayer::createToolBar(HWND parentHwnd) {
581 RECT tRect;
582 InitCommonControls();
583
584 tb.create(ID_TOOLBAR, parentHwnd);
585 tb.addBitmap(4, IDB_TOOLBAR);
586
587 // Create the control buttons
588 tb.addButton(0, ID_PLAY);
589 tb.addButton(1, ID_PAUSE);
590 tb.addButton(2, ID_STOP);
591 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
592 tb.addButton(3, ID_FULLSCREEN);
593 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
594
595 // Create the static control for the time output
596 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
597 tb.getButtonRect(6, &tRect);
598 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
599 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
600 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
601 GetModuleHandle(0), 0);
602 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
603
604 // Create the trackbar control for the time position
605 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
606 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000607 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000608 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
609 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
610 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
611 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000612 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000613 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
614
615 // Create the label with "Speed:" caption
616 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
617 tb.getButtonRect(10, &tRect);
618 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
619 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
620 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
621
622 // Create the edit control and the spin for the speed managing
623 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
624 tb.getButtonRect(11, &tRect);
625 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
626 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
627 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
628 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
629 // It's need to send notify messages to toolbar parent window
630 SetParent(speedEdit, tb.getHandle());
631
632 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
633 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000634 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000635}
636
george82a21d2952005-02-12 11:30:03 +0000637void RfbPlayer::disableTBandMenuItems() {
638 // Disable the menu items
639 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
640 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
641 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
642 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
643 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
644 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
645 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
646 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
647 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
648
649 // Disable the toolbar buttons and child controls
650 tb.enableButton(ID_PLAY, false);
651 tb.enableButton(ID_PAUSE, false);
652 tb.enableButton(ID_STOP, false);
653 tb.enableButton(ID_FULLSCREEN, false);
654 EnableWindow(posTrackBar, false);
655 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000656 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000657}
658
george82f5043162005-02-12 11:37:18 +0000659void RfbPlayer::enableTBandMenuItems() {
660 // Enable the menu items
661 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
662 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
663 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
664 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
665 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
666 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
667 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
668 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
669 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
670
671 // Enable the toolbar buttons and child controls
672 tb.enableButton(ID_PLAY, true);
673 tb.enableButton(ID_PAUSE, true);
674 tb.enableButton(ID_STOP, true);
675 tb.enableButton(ID_FULLSCREEN, true);
676 EnableWindow(posTrackBar, true);
677 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000678 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000679}
680
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000681void RfbPlayer::setVisible(bool visible) {
682 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
683 if (visible) {
684 // When the window becomes visible, make it active
685 SetForegroundWindow(getMainHandle());
686 SetActiveWindow(getMainHandle());
687 }
688}
689
690void RfbPlayer::setTitle(const char *title) {
691 char _title[256];
692 strcpy(_title, AppName);
693 strcat(_title, " - ");
694 strcat(_title, title);
695 SetWindowText(getMainHandle(), _title);
696}
697
698void RfbPlayer::setFrameSize(int width, int height) {
699 // Calculate and set required size for main window
700 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000701 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000702 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
703 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
704 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000705 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
706 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
707 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000708 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
709
710 // Enable/disable scrollbars as appropriate
711 calculateScrollBars();
712}
713
714void RfbPlayer::calculateScrollBars() {
715 // Calculate the required size of window
716 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
717 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
718 DWORD old_style;
719 RECT r;
720 SetRect(&r, 0, 0, buffer->width(), buffer->height());
721 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
722 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
723
724 // Work out whether scroll bars are required
725 do {
726 old_style = style;
727
728 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
729 style |= WS_HSCROLL;
730 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
731 }
732 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
733 style |= WS_VSCROLL;
734 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
735 }
736 } while (style != old_style);
737
738 // Tell Windows to update the window style & cached settings
739 if (style != current_style) {
740 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
741 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
742 }
743
744 // Update the scroll settings
745 SCROLLINFO si;
746 if (style & WS_VSCROLL) {
747 si.cbSize = sizeof(si);
748 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
749 si.nMin = 0;
750 si.nMax = buffer->height();
751 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
752 maxscrolloffset.y = max(0, si.nMax-si.nPage);
753 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
754 si.nPos = scrolloffset.y;
755 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
756 }
757 if (style & WS_HSCROLL) {
758 si.cbSize = sizeof(si);
759 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
760 si.nMin = 0;
761 si.nMax = buffer->width();
762 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
763 maxscrolloffset.x = max(0, si.nMax-si.nPage);
764 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
765 si.nPos = scrolloffset.x;
766 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
767 }
768}
769
770bool RfbPlayer::setViewportOffset(const Point& tl) {
771/* ***
772 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
773 max(0, min(maxscrolloffset.y, tl.y)));
774 */
775 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
776 max(0, min(tl.y, buffer->height()-client_size.height())));
777 Point delta = np.translate(scrolloffset.negate());
778 if (!np.equals(scrolloffset)) {
779 scrolloffset = np;
780 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
781 UpdateWindow(getFrameHandle());
782 return true;
783 }
784 return false;
785}
786
787void RfbPlayer::close(const char* reason) {
788 setVisible(false);
789 if (reason) {
790 vlog.info("closing - %s", reason);
791 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
792 }
793 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
794}
795
796void RfbPlayer::blankBuffer() {
797 fillRect(buffer->getRect(), 0);
798}
799
800void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000801 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000802 blankBuffer();
803 newSession(fileName);
804 skipHandshaking();
george825f326fe2005-02-20 08:01:01 +0000805 is->setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000806 if (paused) is->pausePlayback();
807 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000808}
809
810void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000811 // Perform return if waitWhilePaused processed because
812 // rfbReader thread could receive the signal to close
813 if (waitWhilePaused()) return;
814
george8223e08562005-01-31 15:16:42 +0000815 static long update_time = GetTickCount();
816 try {
george828a471482005-02-06 07:15:53 +0000817 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
818 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000819 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000820 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000821 update_time = GetTickCount();
822 }
823 RfbProto::processMsg();
824 } catch (rdr::Exception e) {
825 if (strcmp(e.str(), "[End Of File]") == 0) {
826 rewind();
george8231a36332005-02-06 17:27:34 +0000827 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000828 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000829 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000830 return;
831 }
832 // It's a special exception to perform backward seeking.
833 // We only rewind the stream and seek the offset
834 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000835 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000836 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000837 rewind();
george820d2e19d2005-03-03 15:47:55 +0000838 if (!stopped) setPos(seekOffset);
839 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000840 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000841 rewindFlag = false;
george8223e08562005-01-31 15:16:42 +0000842 } else {
843 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
844 return;
845 }
846 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000847}
848
849void RfbPlayer::serverInit() {
850 RfbProto::serverInit();
851
george82b95503e2005-02-21 17:02:34 +0000852 // Save the image data start time
853 imageDataStartTime = is->getTimeOffset();
854
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000855 // Resize the backing buffer
856 buffer->setSize(cp.width, cp.height);
857
858 // Check on the true colour mode
859 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000860 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000861
862 // Set the session pixel format
george82193d8e42005-02-20 16:47:01 +0000863 switch (colourDepth) {
864 case DEPTH_AUTO:
865 break;
866 case DEPTH8_RGB332:
867 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
868 break;
869 case DEPTH16_RGB655:
870 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
871 break;
872 case DEPTH24_RGB888:
873 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
874 break;
875 default:
876 throw rdr::Exception("This color depth is not supported!");
877 }
878 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000879
880 // If the window is not maximised then resize it
881 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
882 setFrameSize(cp.width, cp.height);
883
884 // Set the window title and show it
885 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000886
george82d4d69e62005-02-05 09:23:18 +0000887 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000888 sessionTimeMs = calculateSessionTime(fileName);
889 sprintf(fullSessionTime, "%.2um:%.2us",
890 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000891 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000892 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
893 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000894 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000895
george82006f2792005-02-05 07:40:47 +0000896 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000897}
898
899void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
900 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
901 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
902/* int i;
903 for (i=0;i<count;i++) {
904 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
905 }
906 // *** change to 0, 256?
907 refreshWindowPalette(first, count);
908 palette_changed = true;
909 InvalidateRect(getFrameHandle(), 0, FALSE);*/
910}
911
912void RfbPlayer::bell() {
913 if (acceptBell)
914 MessageBeep(-1);
915}
916
917void RfbPlayer::serverCutText(const char* str, int len) {
918 if (cutText != NULL)
919 delete [] cutText;
920 cutText = new char[len + 1];
921 memcpy(cutText, str, len);
922 cutText[len] = '\0';
923}
924
925void RfbPlayer::frameBufferUpdateEnd() {
926};
927
928void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
929}
930
931void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
932}
933
934
935void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
936 buffer->fillRect(r, pix);
937 invalidateBufferRect(r);
938}
939
940void RfbPlayer::imageRect(const Rect& r, void* pixels) {
941 buffer->imageRect(r, pixels);
942 invalidateBufferRect(r);
943}
944
945void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
946 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
947 invalidateBufferRect(r);
948}
949
950bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
951 Rect rect = bufferToClient(crect);
952 if (rect.intersect(client_size).is_empty()) return false;
953 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
954 InvalidateRect(getFrameHandle(), &invalid, FALSE);
955 return true;
956}
957
george820d2e19d2005-03-03 15:47:55 +0000958bool RfbPlayer::waitWhilePaused() {
959 bool result = false;
960 while(isPaused() && !isSeeking()) {
961 Sleep(20);
962 result = true;
963 }
964 return result;
965}
966
george8257f13522005-02-05 08:48:22 +0000967long RfbPlayer::calculateSessionTime(char *filename) {
968 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000969 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000970 try {
971 while (TRUE) {
972 sessionFile.skip(1024);
973 }
974 } catch (rdr::Exception e) {
975 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000976 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000977 } else {
978 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
979 return 0;
980 }
981 }
982 return 0;
983}
984
george826b87aff2005-02-13 10:48:21 +0000985void RfbPlayer::closeSessionFile() {
986 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000987 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000988 RECT r;
989
990 // Uncheck all toolbar buttons
991 if (tb.getHandle()) {
992 tb.checkButton(ID_PLAY, false);
993 tb.checkButton(ID_PAUSE, false);
994 tb.checkButton(ID_STOP, false);
995 }
996
997 // Stop playback and update the player state
998 disableTBandMenuItems();
999 if (rfbReader) {
1000 delete rfbReader->join();
1001 rfbReader = 0;
1002 delete [] fileName;
1003 fileName = 0;
1004 }
1005 blankBuffer();
1006 setTitle("None");
george827009c892005-02-19 12:49:42 +00001007 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +00001008 playbackSpeed = 1.0;
1009 SendMessage(speedUpDown, UDM_SETPOS,
1010 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
1011 sprintf(speedStr, "%.2f", playbackSpeed);
1012 SetWindowText(speedEdit, speedStr);
1013 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1014
1015 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001016 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1017 dwStyle &= ~WS_MAXIMIZE;
1018 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1019 }
george822ff7a612005-02-19 17:05:24 +00001020 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1021 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1022 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001023 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001024 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001025 buffer->setSize(32, 32);
1026 calculateScrollBars();
1027
1028 // Update the cached sizing information and repaint the frame window
1029 GetWindowRect(getFrameHandle(), &r);
1030 window_size = Rect(r.left, r.top, r.right, r.bottom);
1031 GetClientRect(getFrameHandle(), &r);
1032 client_size = Rect(r.left, r.top, r.right, r.bottom);
1033 InvalidateRect(getFrameHandle(), 0, TRUE);
1034 UpdateWindow(getFrameHandle());
1035}
1036
george8217e92cb2005-01-31 16:01:02 +00001037void RfbPlayer::openSessionFile(char *_fileName) {
1038 fileName = strDup(_fileName);
1039
1040 // Close the previous reading thread
1041 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001042 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001043 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001044 }
1045 blankBuffer();
1046 newSession(fileName);
1047 setSpeed(playbackSpeed);
1048 rfbReader = new rfbSessionReader(this);
1049 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001050 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001051 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001052}
1053
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001054void RfbPlayer::setPaused(bool paused) {
1055 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001056 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001057 tb.checkButton(ID_PAUSE, true);
1058 tb.checkButton(ID_PLAY, false);
1059 tb.checkButton(ID_STOP, false);
1060 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1061 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001062 } else {
george825beb62a2005-02-09 13:04:32 +00001063 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001064 tb.checkButton(ID_PLAY, true);
1065 tb.checkButton(ID_STOP, false);
1066 tb.checkButton(ID_PAUSE, false);
1067 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1068 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001069 }
1070}
1071
george82006f2792005-02-05 07:40:47 +00001072void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001073 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001074 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001075 if (is) {
1076 is->pausePlayback();
1077 is->interruptFrameDelay();
1078 }
george82006f2792005-02-05 07:40:47 +00001079 tb.checkButton(ID_STOP, true);
1080 tb.checkButton(ID_PLAY, false);
1081 tb.checkButton(ID_PAUSE, false);
1082 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1083 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001084 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001085}
1086
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001087void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001088 if (speed > 0) {
1089 char speedStr[20] = "\0";
1090 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001091 is->setSpeed(newSpeed);
1092 playbackSpeed = newSpeed;
1093 SendMessage(speedUpDown, UDM_SETPOS,
1094 0, MAKELONG((short)(newSpeed / 0.5), 0));
1095 sprintf(speedStr, "%.2f", newSpeed);
1096 SetWindowText(speedEdit, speedStr);
1097 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001098}
1099
1100double RfbPlayer::getSpeed() {
1101 return is->getSpeed();
1102}
1103
1104void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001105 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001106}
1107
1108long RfbPlayer::getSeekOffset() {
1109 return is->getSeekOffset();
1110}
1111
1112bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001113 if (is) return is->isSeeking();
1114 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001115}
1116
1117bool RfbPlayer::isSeekMode() {
1118 return seekMode;
1119}
1120
1121bool RfbPlayer::isPaused() {
1122 return is->isPaused();
1123}
1124
1125long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001126 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001127}
1128
george828a471482005-02-06 07:15:53 +00001129void RfbPlayer::updatePos(long newPos) {
1130 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001131 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001132 long time = newPos / 1000;
1133 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001134 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001135
1136 // Update the position of slider
1137 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001138 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1139 sliderStepMs / double(newPos);
1140 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1141 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1142 }
george828a471482005-02-06 07:15:53 +00001143 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001144}
1145
1146void RfbPlayer::skipHandshaking() {
1147 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1148 is->skip(skipBytes);
1149 state_ = RFBSTATE_NORMAL;
1150}
1151
1152void programInfo() {
1153 win32::FileVersionInfo inf;
1154 _tprintf(_T("%s - %s, Version %s\n"),
1155 inf.getVerString(_T("ProductName")),
1156 inf.getVerString(_T("FileDescription")),
1157 inf.getVerString(_T("FileVersion")));
1158 printf("%s\n", buildTime);
1159 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1160}
1161
1162void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001163 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001164}
1165
1166double playbackSpeed = 1.0;
1167long initTime = -1;
george82193d8e42005-02-20 16:47:01 +00001168int depth = DEPTH_AUTO;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001169bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001170char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001171bool print_usage = false;
1172bool acceptBell = false;
1173
1174bool processParams(int argc, char* argv[]) {
1175 for (int i = 1; i < argc; i++) {
1176 if ((strcasecmp(argv[i], "-help") == 0) ||
1177 (strcasecmp(argv[i], "--help") == 0) ||
1178 (strcasecmp(argv[i], "/help") == 0) ||
1179 (strcasecmp(argv[i], "-h") == 0) ||
1180 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001181 (strcasecmp(argv[i], "/?") == 0) ||
1182 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001183 print_usage = true;
1184 return true;
1185 }
1186
george82193d8e42005-02-20 16:47:01 +00001187 if ((strcasecmp(argv[i], "-depth") == 0) ||
1188 (strcasecmp(argv[i], "/depth") == 0) && (i < argc-1)) {
1189 depth = atoi(argv[++i]);
1190 if (depth < 0) {
1191 return false;
1192 }
1193 continue;
1194 }
1195
1196
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001197 if ((strcasecmp(argv[i], "-speed") == 0) ||
1198 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1199 playbackSpeed = atof(argv[++i]);
1200 if (playbackSpeed <= 0) {
1201 return false;
1202 }
1203 continue;
1204 }
1205
1206 if ((strcasecmp(argv[i], "-pos") == 0) ||
1207 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1208 initTime = atol(argv[++i]);
1209 if (initTime <= 0)
1210 return false;
1211 continue;
1212 }
1213
1214 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1215 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001216 autoplay = true;
1217 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001218 }
1219
1220 if ((strcasecmp(argv[i], "-bell") == 0) ||
1221 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001222 acceptBell = true;
1223 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001224 }
1225
1226 if (i != argc - 1)
1227 return false;
1228 }
1229
1230 fileName = strDup(argv[argc-1]);
1231 return true;
1232}
1233
1234//
1235// -=- WinMain
1236//
1237
1238int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1239
1240 // - Process the command-line
1241
1242 int argc = __argc;
1243 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001244 if ((argc > 1) && (!processParams(argc, argv))) {
1245 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1246 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001247 }
george82e6883de2005-02-08 14:42:12 +00001248
1249 if (print_usage) {
1250 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001251 return 0;
george8267cbcd02005-01-16 15:39:56 +00001252 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001253
george82e6883de2005-02-08 14:42:12 +00001254 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001255 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001256 try {
george82193d8e42005-02-20 16:47:01 +00001257 player = new RfbPlayer(fileName, depth, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001258 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001259 } catch (rdr::Exception e) {
1260 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1261 delete player;
1262 return 0;
1263 }
1264
1265 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001266 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001267 MSG msg;
1268 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001269 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1270 TranslateMessage(&msg);
1271 DispatchMessage(&msg);
1272 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001273 }
1274
george82e6883de2005-02-08 14:42:12 +00001275 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001276 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001277 if (player) delete player;
1278 } catch (rdr::Exception e) {
1279 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1280 }
1281
1282 return 0;
1283};