blob: 38dd241a4c1d84f10d7ec0e47f6637cbaf5c04de [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>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000030
31using namespace rfb;
32using namespace rfb::win32;
33
34// -=- Variables & consts
35
36static LogWriter vlog("RfbPlayer");
37
38TStr rfb::win32::AppName("RfbPlayer");
39extern const char* buildTime;
40
george82e6883de2005-02-08 14:42:12 +000041char wrong_cmd_msg[] =
42 "Wrong command-line parameters!\n"
43 "Use for help: rfbplayer -help";
44
45char usage_msg[] =
46 "usage: rfbplayer <options> <filename>\n"
47 "Command-line options:\n"
48 " -help \t- Provide usage information.\n"
george825caee412005-03-09 09:52:10 +000049 " -pf <mode> \t- Forces the pixel format for the session.\n"
50 " \t List of the pixel formats:\n"
51 " \t 0 - Auto,\n"
52 " \t 1 - depth 8 (RGB332),\n"
53 " \t 2 - depth 16 (RGB655),\n"
54 " \t 3 - depth 24 (RGB888).\n"
george82e6883de2005-02-08 14:42:12 +000055 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
56 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
57 " -pos <ms> \t- Sets initial time position in the session file,\n"
58 " \t in milliseconds. Default: 0.\n"
george825e7af742005-03-10 14:26:00 +000059 " -autoplay \t- Runs the player in the playback mode.\n";
george82e6883de2005-02-08 14:42:12 +000060
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
george825e7af742005-03-10 14:26:00 +000067#define CTRL_BAR_HEIGHT 28
george8268d25142005-02-13 09:33:22 +000068#define DEFAULT_PLAYER_WIDTH 640
69#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000070
george82d070c692005-01-19 16:44:04 +000071#define ID_TOOLBAR 500
72#define ID_PLAY 510
73#define ID_PAUSE 520
74#define ID_TIME_STATIC 530
75#define ID_SPEED_STATIC 540
76#define ID_SPEED_EDIT 550
77#define ID_POS_TRACKBAR 560
78#define ID_SPEED_UPDOWN 570
79
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000080//
81// -=- RfbPlayerClass
82
83//
84// Window class used as the basis for RfbPlayer instance
85//
86
87class RfbPlayerClass {
88public:
89 RfbPlayerClass();
90 ~RfbPlayerClass();
91 ATOM classAtom;
92 HINSTANCE instance;
93};
94
95LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
96 LRESULT result;
97
98 if (msg == WM_CREATE)
99 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
100 else if (msg == WM_DESTROY) {
101 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000102 SetWindowLong(hwnd, GWL_USERDATA, 0);
103 }
104 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
105 if (!_this) {
106 vlog.info("null _this in %x, message %u", hwnd, msg);
107 return DefWindowProc(hwnd, msg, wParam, lParam);
108 }
109
110 try {
111 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
112 } catch (rdr::Exception& e) {
113 vlog.error("untrapped: %s", e.str());
114 }
115
116 return result;
117};
118
119RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
120 WNDCLASS wndClass;
121 wndClass.style = 0;
122 wndClass.lpfnWndProc = RfbPlayerProc;
123 wndClass.cbClsExtra = 0;
124 wndClass.cbWndExtra = 0;
125 wndClass.hInstance = instance = GetModuleHandle(0);
126 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000127 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000128 if (!wndClass.hIcon)
129 printf("unable to load icon:%ld", GetLastError());
130 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
131 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000132 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000133 wndClass.lpszClassName = _T("RfbPlayerClass");
134 classAtom = RegisterClass(&wndClass);
135 if (!classAtom) {
136 throw rdr::SystemException("unable to register RfbPlayer window class",
137 GetLastError());
138 }
139}
140
141RfbPlayerClass::~RfbPlayerClass() {
142 if (classAtom) {
143 UnregisterClass((const TCHAR*)classAtom, instance);
144 }
145}
146
147RfbPlayerClass baseClass;
148
149//
150// -=- RfbFrameClass
151
152//
153// Window class used to displaying the rfb data
154//
155
156class RfbFrameClass {
157public:
158 RfbFrameClass();
159 ~RfbFrameClass();
160 ATOM classAtom;
161 HINSTANCE instance;
162};
163
164LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
165 LRESULT result;
166
167 if (msg == WM_CREATE)
168 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
169 else if (msg == WM_DESTROY)
170 SetWindowLong(hwnd, GWL_USERDATA, 0);
171 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
172 if (!_this) {
173 vlog.info("null _this in %x, message %u", hwnd, msg);
174 return DefWindowProc(hwnd, msg, wParam, lParam);
175 }
176
177 try {
178 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
179 } catch (rdr::Exception& e) {
180 vlog.error("untrapped: %s", e.str());
181 }
182
183 return result;
184}
185
186RfbFrameClass::RfbFrameClass() : classAtom(0) {
187 WNDCLASS wndClass;
188 wndClass.style = 0;
189 wndClass.lpfnWndProc = FrameProc;
190 wndClass.cbClsExtra = 0;
191 wndClass.cbWndExtra = 0;
192 wndClass.hInstance = instance = GetModuleHandle(0);
193 wndClass.hIcon = 0;
194 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
195 wndClass.hbrBackground = 0;
196 wndClass.lpszMenuName = 0;
197 wndClass.lpszClassName = _T("RfbPlayerClass1");
198 classAtom = RegisterClass(&wndClass);
199 if (!classAtom) {
200 throw rdr::SystemException("unable to register RfbPlayer window class",
201 GetLastError());
202 }
203}
204
205RfbFrameClass::~RfbFrameClass() {
206 if (classAtom) {
207 UnregisterClass((const TCHAR*)classAtom, instance);
208 }
209}
210
211RfbFrameClass frameClass;
212
213//
214// -=- RfbPlayer instance implementation
215//
216
george825e7af742005-03-10 14:26:00 +0000217RfbPlayer::RfbPlayer(char *_fileName, PlayerOptions *_options)
218: RfbProto(_fileName), fileName(_fileName), buffer(0), client_size(0, 0, 32, 32),
219 window_size(0, 0, 32, 32), cutText(0), seekMode(false), lastPos(0),
220 timeStatic(0), speedEdit(0), posTrackBar(0), speedUpDown(0),
george823104aec2005-02-21 13:20:56 +0000221 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george825e7af742005-03-10 14:26:00 +0000222 imageDataStartTime(0), rewindFlag(false), stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000223
george825e7af742005-03-10 14:26:00 +0000224 // Save the player options
225 memcpy(&options, _options, sizeof(options));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000226
george823c8fbbf2005-01-24 11:09:08 +0000227 // Reset the full session time
228 strcpy(fullSessionTime, "00m:00s");
229
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000230 // Create the main window
231 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000232 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
233 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000234 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000235 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000236 if (!mainHwnd) {
237 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
238 }
239 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
240
241 // Create the backing buffer
242 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000243 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000244
george825e7af742005-03-10 14:26:00 +0000245 // If run with command-line parameters,
246 // open the session file with default settings, otherwise
247 // restore player settings from the registry
george8217e92cb2005-01-31 16:01:02 +0000248 if (fileName) {
249 openSessionFile(fileName);
george825e7af742005-03-10 14:26:00 +0000250 if (options.initTime > 0) setPos(options.initTime);
251 setSpeed(options.playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000252 } else {
george825e7af742005-03-10 14:26:00 +0000253 options.readFromRegistry();
george8263ebbcc2005-02-12 12:09:13 +0000254 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000255 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000256 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000257}
258
259RfbPlayer::~RfbPlayer() {
george825e7af742005-03-10 14:26:00 +0000260 options.writeToRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000261 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000262 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000263 delete rfbReader->join();
264 rfbReader = 0;
265 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000266 if (mainHwnd) {
267 setVisible(false);
268 DestroyWindow(mainHwnd);
269 mainHwnd = 0;
270 }
george825beb62a2005-02-09 13:04:32 +0000271 if (buffer) delete buffer;
272 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000273 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000274 vlog.debug("~RfbPlayer done");
275}
276
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000277LRESULT
278RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
279 switch (msg) {
280
281 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000282
283 case WM_CREATE:
284 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000285 // Create the frame window
286 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
287 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
288 hwnd, 0, frameClass.instance, this);
289
george82d070c692005-01-19 16:44:04 +0000290 createToolBar(hwnd);
291
george82006f2792005-02-05 07:40:47 +0000292 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000293
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000294 return 0;
295 }
296
george827214b822004-12-12 07:02:51 +0000297 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000298
299 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000300 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000301 case ID_OPENFILE:
302 {
303 char curDir[_MAX_DIR];
304 static char filename[_MAX_PATH];
305 OPENFILENAME ofn;
306 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
307 GetCurrentDirectory(sizeof(curDir), curDir);
308
309 ofn.lStructSize = sizeof(OPENFILENAME);
310 ofn.hwndOwner = getMainHandle();
311 ofn.lpstrFile = filename;
312 ofn.nMaxFile = sizeof(filename);
313 ofn.lpstrInitialDir = curDir;
314 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
315 "All files (*.*)\0*.*\0";
316 ofn.lpstrDefExt = "rfb";
317 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000318 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000319 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000320 }
george826e51fcc2005-02-06 13:30:49 +0000321 }
322 break;
george8271ca1772005-02-13 10:50:46 +0000323 case ID_CLOSEFILE:
324 closeSessionFile();
325 break;
george825c13c662005-01-27 14:48:23 +0000326 case ID_PLAY:
327 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000328 break;
329 case ID_PAUSE:
330 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000331 break;
332 case ID_STOP:
333 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000334 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000335 }
george825c13c662005-01-27 14:48:23 +0000336 break;
337 case ID_PLAYPAUSE:
338 if (isPaused()) {
339 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000340 } else {
341 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000342 }
george825c13c662005-01-27 14:48:23 +0000343 break;
george827549df42005-02-08 16:31:02 +0000344 case ID_GOTO:
345 {
346 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000347 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000348 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
349 setPos(gotoTime);
350 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000351 setPaused(isPaused());;
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:
george825e7af742005-03-10 14:26:00 +0000359 options.loopPlayback = !options.loopPlayback;
360 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000361 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;
george825e7af742005-03-10 14:26:00 +0000377 case ID_OPTIONS:
378 {
379 OptionsDialog optionsDialog(&options);
george82d9957b72005-03-11 14:22:14 +0000380 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000381 }
382 break;
george8201aa6732005-02-06 17:13:03 +0000383 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000384 PostQuitMessage(0);
385 break;
george82ef5f7262005-02-08 15:09:26 +0000386 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000387 MessageBox(getMainHandle(),
388 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
389 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000390 }
391 break;
392
393 // Update frame's window size and add scrollbars if required
394
395 case WM_SIZE:
396 {
george82d070c692005-01-19 16:44:04 +0000397
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000398 Point old_offset = bufferToClient(Point(0, 0));
399
400 // Update the cached sizing information
401 RECT r;
402 GetClientRect(getMainHandle(), &r);
403 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
404 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
405
406 GetWindowRect(getFrameHandle(), &r);
407 window_size = Rect(r.left, r.top, r.right, r.bottom);
408 GetClientRect(getFrameHandle(), &r);
409 client_size = Rect(r.left, r.top, r.right, r.bottom);
410
411 // Determine whether scrollbars are required
412 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000413
414 // Resize the ToolBar
415 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000416
417 // Redraw if required
418 if (!old_offset.equals(bufferToClient(Point(0, 0))))
419 InvalidateRect(getFrameHandle(), 0, TRUE);
420 }
421 break;
george828a471482005-02-06 07:15:53 +0000422
423 // Process messages from posTrackBar
424
425 case WM_HSCROLL:
426 {
427 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
428 Pos *= sliderStepMs;
429
430 switch (LOWORD(wParam)) {
431 case TB_PAGEUP:
432 case TB_PAGEDOWN:
433 case TB_LINEUP:
434 case TB_LINEDOWN:
435 case TB_THUMBTRACK:
436 sliderDraging = true;
437 updatePos(Pos);
438 return 0;
george82bcc129b2005-03-15 17:11:40 +0000439 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000440 case TB_ENDTRACK:
441 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000442 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000443 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000444 sliderDraging = false;
445 return 0;
446 default:
447 break;
448 }
449 }
450 break;
george829e6e6cc2005-01-29 13:12:05 +0000451
452 case WM_NOTIFY:
453 switch (((NMHDR*)lParam)->code) {
454 case UDN_DELTAPOS:
455 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000456 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000457 char speedStr[20] = "\0";
458 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
459 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
460 double speed;
461
george824ea27f62005-01-29 15:03:06 +0000462 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000463 if (upDown->iDelta > 0) {
464 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
465 } else {
george824ea27f62005-01-29 15:03:06 +0000466 // It's need to round the UpDown position
467 if ((upDown->iPos * 0.5) != getSpeed()) {
468 upDown->iDelta = 0;
469 lResult = TRUE;
470 }
george829e6e6cc2005-01-29 13:12:05 +0000471 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
472 }
george829e6e6cc2005-01-29 13:12:05 +0000473 sprintf(speedStr, "%.2f", speed);
474 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000475 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000476 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000477 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000478 }
george824ea27f62005-01-29 15:03:06 +0000479 }
george829e6e6cc2005-01-29 13:12:05 +0000480 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000481
482 case WM_CLOSE:
483 vlog.debug("WM_CLOSE %x", getMainHandle());
484 PostQuitMessage(0);
485 break;
486 }
487
488 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
489}
490
491LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
492 switch (msg) {
493
494 case WM_PAINT:
495 {
george821e846ff2005-02-24 13:13:33 +0000496 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000497 seekMode = true;
498 return 0;
499 } else {
500 if (seekMode) {
501 seekMode = false;
502 InvalidateRect(getFrameHandle(), 0, true);
503 UpdateWindow(getFrameHandle());
504 return 0;
505 }
506 }
507
508 PAINTSTRUCT ps;
509 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
510 if (!paintDC)
511 throw SystemException("unable to BeginPaint", GetLastError());
512 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
513
514 if (!pr.is_empty()) {
515
516 if (buffer->bitmap) {
517
518 // Get device context
519 BitmapDC bitmapDC(paintDC, buffer->bitmap);
520
521 // Blit the border if required
522 Rect bufpos = bufferToClient(buffer->getRect());
523 if (!pr.enclosed_by(bufpos)) {
524 vlog.debug("draw border");
525 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
526 RECT r;
527 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
528 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
529 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
530 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
531 }
532
533 // Do the blit
534 Point buf_pos = clientToBuffer(pr.tl);
535 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
536 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
537 throw SystemException("unable to BitBlt to window", GetLastError());
538
539 } else {
540 // Blit a load of black
541 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
542 0, 0, 0, BLACKNESS))
543 throw SystemException("unable to BitBlt to blank window", GetLastError());
544 }
545 }
546 EndPaint(getFrameHandle(), &ps);
547 }
548 return 0;
549
550 case WM_VSCROLL:
551 case WM_HSCROLL:
552 {
553 Point delta;
554 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
555
556 switch (LOWORD(wParam)) {
557 case SB_PAGEUP: newpos -= 50; break;
558 case SB_PAGEDOWN: newpos += 50; break;
559 case SB_LINEUP: newpos -= 5; break;
560 case SB_LINEDOWN: newpos += 5; break;
561 case SB_THUMBTRACK:
562 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
563 default: vlog.info("received unknown scroll message");
564 };
565
566 if (msg == WM_HSCROLL)
567 setViewportOffset(Point(newpos, scrolloffset.y));
568 else
569 setViewportOffset(Point(scrolloffset.x, newpos));
570
571 SCROLLINFO si;
572 si.cbSize = sizeof(si);
573 si.fMask = SIF_POS;
574 si.nPos = newpos;
575 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
576 }
577 break;
578 }
579
580 return DefWindowProc(hwnd, msg, wParam, lParam);
581}
582
george82d070c692005-01-19 16:44:04 +0000583void RfbPlayer::createToolBar(HWND parentHwnd) {
584 RECT tRect;
585 InitCommonControls();
586
587 tb.create(ID_TOOLBAR, parentHwnd);
588 tb.addBitmap(4, IDB_TOOLBAR);
589
590 // Create the control buttons
591 tb.addButton(0, ID_PLAY);
592 tb.addButton(1, ID_PAUSE);
593 tb.addButton(2, ID_STOP);
594 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
595 tb.addButton(3, ID_FULLSCREEN);
596 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
597
598 // Create the static control for the time output
599 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
600 tb.getButtonRect(6, &tRect);
601 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
602 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
603 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
604 GetModuleHandle(0), 0);
605 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
606
607 // Create the trackbar control for the time position
608 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
609 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000610 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000611 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
612 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
613 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
614 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000615 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000616 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
617
618 // Create the label with "Speed:" caption
619 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
620 tb.getButtonRect(10, &tRect);
621 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
622 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
623 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
624
625 // Create the edit control and the spin for the speed managing
626 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
627 tb.getButtonRect(11, &tRect);
628 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
629 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
630 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
631 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
632 // It's need to send notify messages to toolbar parent window
633 SetParent(speedEdit, tb.getHandle());
634
635 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
636 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000637 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000638}
639
george82a21d2952005-02-12 11:30:03 +0000640void RfbPlayer::disableTBandMenuItems() {
641 // Disable the menu items
642 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
643 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
644 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
645 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
646 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
647 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
648 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
649 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
650 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
651
652 // Disable the toolbar buttons and child controls
653 tb.enableButton(ID_PLAY, false);
654 tb.enableButton(ID_PAUSE, false);
655 tb.enableButton(ID_STOP, false);
656 tb.enableButton(ID_FULLSCREEN, false);
657 EnableWindow(posTrackBar, false);
658 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000659 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000660}
661
george82f5043162005-02-12 11:37:18 +0000662void RfbPlayer::enableTBandMenuItems() {
663 // Enable the menu items
664 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
665 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
666 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
667 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
668 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
669 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
670 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
671 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
672 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
673
674 // Enable the toolbar buttons and child controls
675 tb.enableButton(ID_PLAY, true);
676 tb.enableButton(ID_PAUSE, true);
677 tb.enableButton(ID_STOP, true);
678 tb.enableButton(ID_FULLSCREEN, true);
679 EnableWindow(posTrackBar, true);
680 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000681 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000682}
683
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000684void RfbPlayer::setVisible(bool visible) {
685 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
686 if (visible) {
687 // When the window becomes visible, make it active
688 SetForegroundWindow(getMainHandle());
689 SetActiveWindow(getMainHandle());
690 }
691}
692
693void RfbPlayer::setTitle(const char *title) {
694 char _title[256];
695 strcpy(_title, AppName);
696 strcat(_title, " - ");
697 strcat(_title, title);
698 SetWindowText(getMainHandle(), _title);
699}
700
701void RfbPlayer::setFrameSize(int width, int height) {
702 // Calculate and set required size for main window
703 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000704 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000705 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
706 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
707 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000708 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
709 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
710 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000711 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
712
713 // Enable/disable scrollbars as appropriate
714 calculateScrollBars();
715}
716
717void RfbPlayer::calculateScrollBars() {
718 // Calculate the required size of window
719 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
720 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
721 DWORD old_style;
722 RECT r;
723 SetRect(&r, 0, 0, buffer->width(), buffer->height());
724 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
725 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
726
727 // Work out whether scroll bars are required
728 do {
729 old_style = style;
730
731 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
732 style |= WS_HSCROLL;
733 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
734 }
735 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
736 style |= WS_VSCROLL;
737 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
738 }
739 } while (style != old_style);
740
741 // Tell Windows to update the window style & cached settings
742 if (style != current_style) {
743 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
744 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
745 }
746
747 // Update the scroll settings
748 SCROLLINFO si;
749 if (style & WS_VSCROLL) {
750 si.cbSize = sizeof(si);
751 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
752 si.nMin = 0;
753 si.nMax = buffer->height();
754 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
755 maxscrolloffset.y = max(0, si.nMax-si.nPage);
756 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
757 si.nPos = scrolloffset.y;
758 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
759 }
760 if (style & WS_HSCROLL) {
761 si.cbSize = sizeof(si);
762 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
763 si.nMin = 0;
764 si.nMax = buffer->width();
765 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
766 maxscrolloffset.x = max(0, si.nMax-si.nPage);
767 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
768 si.nPos = scrolloffset.x;
769 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
770 }
george82a758a7a2005-03-15 16:34:57 +0000771
772 // Update the cached client size
773 GetClientRect(getFrameHandle(), &r);
774 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000775}
776
777bool RfbPlayer::setViewportOffset(const Point& tl) {
778/* ***
779 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
780 max(0, min(maxscrolloffset.y, tl.y)));
781 */
782 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
783 max(0, min(tl.y, buffer->height()-client_size.height())));
784 Point delta = np.translate(scrolloffset.negate());
785 if (!np.equals(scrolloffset)) {
786 scrolloffset = np;
787 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
788 UpdateWindow(getFrameHandle());
789 return true;
790 }
791 return false;
792}
793
794void RfbPlayer::close(const char* reason) {
795 setVisible(false);
796 if (reason) {
797 vlog.info("closing - %s", reason);
798 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
799 }
800 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
801}
802
803void RfbPlayer::blankBuffer() {
804 fillRect(buffer->getRect(), 0);
805}
806
807void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000808 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000809 blankBuffer();
810 newSession(fileName);
811 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000812 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000813 if (paused) is->pausePlayback();
814 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000815}
816
817void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000818 // Perform return if waitWhilePaused processed because
819 // rfbReader thread could receive the signal to close
820 if (waitWhilePaused()) return;
821
george8223e08562005-01-31 15:16:42 +0000822 static long update_time = GetTickCount();
823 try {
george828a471482005-02-06 07:15:53 +0000824 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
825 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000826 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000827 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000828 update_time = GetTickCount();
829 }
830 RfbProto::processMsg();
831 } catch (rdr::Exception e) {
832 if (strcmp(e.str(), "[End Of File]") == 0) {
833 rewind();
george825e7af742005-03-10 14:26:00 +0000834 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000835 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000836 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000837 return;
838 }
839 // It's a special exception to perform backward seeking.
840 // We only rewind the stream and seek the offset
841 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000842 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000843 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000844 rewind();
george820d2e19d2005-03-03 15:47:55 +0000845 if (!stopped) setPos(seekOffset);
846 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000847 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000848 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000849 return;
850 }
851 // It's a special exception which is used to terminate the playback
852 if (strcmp(e.str(), "[TERMINATE]") == 0) {
853 sessionTerminateThread *terminate = new sessionTerminateThread(this);
854 terminate->start();
george8223e08562005-01-31 15:16:42 +0000855 } else {
george820e980cc2005-03-10 18:18:34 +0000856 // Show the exception message and close the session playback
857 is->pausePlayback();
858 char message[256] = "\0";
859 strcat(message, e.str());
860 strcat(message, "\nMaybe you force wrong the pixel format for this session");
861 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
862 sessionTerminateThread *terminate = new sessionTerminateThread(this);
863 terminate->start();
george8223e08562005-01-31 15:16:42 +0000864 return;
865 }
866 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000867}
868
869void RfbPlayer::serverInit() {
870 RfbProto::serverInit();
871
george82b95503e2005-02-21 17:02:34 +0000872 // Save the image data start time
873 imageDataStartTime = is->getTimeOffset();
874
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000875 // Resize the backing buffer
876 buffer->setSize(cp.width, cp.height);
877
878 // Check on the true colour mode
879 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000880 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000881
882 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000883 static long pixelFormat = PF_AUTO;
884 if (options.askPixelFormat) {
885 ChoosePixelFormatDialog choosePixelFormatDialog(pixelFormat);
george82d9957b72005-03-11 14:22:14 +0000886 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george825e7af742005-03-10 14:26:00 +0000887 pixelFormat = choosePixelFormatDialog.getPF();
george822c7634b2005-03-10 18:03:27 +0000888 } else {
889 is->pausePlayback();
890 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000891 }
892 } else {
893 pixelFormat = options.pixelFormat;
894 }
george825caee412005-03-09 09:52:10 +0000895 switch (pixelFormat) {
896 case PF_AUTO:
george82193d8e42005-02-20 16:47:01 +0000897 break;
george825caee412005-03-09 09:52:10 +0000898 case PF_D8_RGB332:
george82193d8e42005-02-20 16:47:01 +0000899 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
900 break;
george825caee412005-03-09 09:52:10 +0000901 case PF_D16_RGB655:
george82193d8e42005-02-20 16:47:01 +0000902 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
903 break;
george825caee412005-03-09 09:52:10 +0000904 case PF_D24_RGB888:
george82193d8e42005-02-20 16:47:01 +0000905 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
906 break;
907 default:
908 throw rdr::Exception("This color depth is not supported!");
909 }
910 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000911
912 // If the window is not maximised then resize it
913 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
914 setFrameSize(cp.width, cp.height);
915
916 // Set the window title and show it
917 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000918
george82d4d69e62005-02-05 09:23:18 +0000919 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000920 sessionTimeMs = calculateSessionTime(fileName);
921 sprintf(fullSessionTime, "%.2um:%.2us",
922 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000923 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000924 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
925 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000926 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000927
george825e7af742005-03-10 14:26:00 +0000928 setPaused(!options.autoPlay);
929 // Restore the parameters from registry,
930 // which was replaced by command-line parameters.
931 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000932}
933
934void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
935 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
936 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
937/* int i;
938 for (i=0;i<count;i++) {
939 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
940 }
941 // *** change to 0, 256?
942 refreshWindowPalette(first, count);
943 palette_changed = true;
944 InvalidateRect(getFrameHandle(), 0, FALSE);*/
945}
946
947void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000948 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000949 MessageBeep(-1);
950}
951
952void RfbPlayer::serverCutText(const char* str, int len) {
953 if (cutText != NULL)
954 delete [] cutText;
955 cutText = new char[len + 1];
956 memcpy(cutText, str, len);
957 cutText[len] = '\0';
958}
959
960void RfbPlayer::frameBufferUpdateEnd() {
961};
962
963void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
964}
965
966void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
967}
968
969
970void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
971 buffer->fillRect(r, pix);
972 invalidateBufferRect(r);
973}
974
975void RfbPlayer::imageRect(const Rect& r, void* pixels) {
976 buffer->imageRect(r, pixels);
977 invalidateBufferRect(r);
978}
979
980void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
981 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
982 invalidateBufferRect(r);
983}
984
985bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
986 Rect rect = bufferToClient(crect);
987 if (rect.intersect(client_size).is_empty()) return false;
988 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
989 InvalidateRect(getFrameHandle(), &invalid, FALSE);
990 return true;
991}
992
george820d2e19d2005-03-03 15:47:55 +0000993bool RfbPlayer::waitWhilePaused() {
994 bool result = false;
995 while(isPaused() && !isSeeking()) {
996 Sleep(20);
997 result = true;
998 }
999 return result;
1000}
1001
george8257f13522005-02-05 08:48:22 +00001002long RfbPlayer::calculateSessionTime(char *filename) {
1003 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001004 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001005 try {
1006 while (TRUE) {
1007 sessionFile.skip(1024);
1008 }
1009 } catch (rdr::Exception e) {
1010 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001011 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001012 } else {
1013 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1014 return 0;
1015 }
1016 }
1017 return 0;
1018}
1019
george826b87aff2005-02-13 10:48:21 +00001020void RfbPlayer::closeSessionFile() {
1021 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001022 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001023 RECT r;
1024
1025 // Uncheck all toolbar buttons
1026 if (tb.getHandle()) {
1027 tb.checkButton(ID_PLAY, false);
1028 tb.checkButton(ID_PAUSE, false);
1029 tb.checkButton(ID_STOP, false);
1030 }
1031
1032 // Stop playback and update the player state
1033 disableTBandMenuItems();
1034 if (rfbReader) {
1035 delete rfbReader->join();
1036 rfbReader = 0;
1037 delete [] fileName;
1038 fileName = 0;
1039 }
1040 blankBuffer();
1041 setTitle("None");
george827009c892005-02-19 12:49:42 +00001042 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001043 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001044 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001045 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1046 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001047 SetWindowText(speedEdit, speedStr);
1048 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1049
1050 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001051 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1052 dwStyle &= ~WS_MAXIMIZE;
1053 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1054 }
george822ff7a612005-02-19 17:05:24 +00001055 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1056 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1057 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001058 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001059 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001060 buffer->setSize(32, 32);
1061 calculateScrollBars();
1062
1063 // Update the cached sizing information and repaint the frame window
1064 GetWindowRect(getFrameHandle(), &r);
1065 window_size = Rect(r.left, r.top, r.right, r.bottom);
1066 GetClientRect(getFrameHandle(), &r);
1067 client_size = Rect(r.left, r.top, r.right, r.bottom);
1068 InvalidateRect(getFrameHandle(), 0, TRUE);
1069 UpdateWindow(getFrameHandle());
1070}
1071
george8217e92cb2005-01-31 16:01:02 +00001072void RfbPlayer::openSessionFile(char *_fileName) {
1073 fileName = strDup(_fileName);
1074
1075 // Close the previous reading thread
1076 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001077 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001078 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001079 }
1080 blankBuffer();
1081 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001082 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001083 rfbReader = new rfbSessionReader(this);
1084 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001085 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001086 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001087}
1088
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001089void RfbPlayer::setPaused(bool paused) {
1090 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001091 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001092 tb.checkButton(ID_PAUSE, true);
1093 tb.checkButton(ID_PLAY, false);
1094 tb.checkButton(ID_STOP, false);
1095 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1096 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001097 } else {
george825beb62a2005-02-09 13:04:32 +00001098 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001099 tb.checkButton(ID_PLAY, true);
1100 tb.checkButton(ID_STOP, false);
1101 tb.checkButton(ID_PAUSE, false);
1102 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1103 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001104 }
1105}
1106
george82006f2792005-02-05 07:40:47 +00001107void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001108 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001109 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001110 if (is) {
1111 is->pausePlayback();
1112 is->interruptFrameDelay();
1113 }
george82006f2792005-02-05 07:40:47 +00001114 tb.checkButton(ID_STOP, true);
1115 tb.checkButton(ID_PLAY, false);
1116 tb.checkButton(ID_PAUSE, false);
1117 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1118 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001119 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001120}
1121
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001122void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001123 if (speed > 0) {
1124 char speedStr[20] = "\0";
1125 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001126 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001127 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001128 SendMessage(speedUpDown, UDM_SETPOS,
1129 0, MAKELONG((short)(newSpeed / 0.5), 0));
1130 sprintf(speedStr, "%.2f", newSpeed);
1131 SetWindowText(speedEdit, speedStr);
1132 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001133}
1134
1135double RfbPlayer::getSpeed() {
1136 return is->getSpeed();
1137}
1138
1139void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001140 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001141}
1142
1143long RfbPlayer::getSeekOffset() {
1144 return is->getSeekOffset();
1145}
1146
1147bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001148 if (is) return is->isSeeking();
1149 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001150}
1151
1152bool RfbPlayer::isSeekMode() {
1153 return seekMode;
1154}
1155
1156bool RfbPlayer::isPaused() {
1157 return is->isPaused();
1158}
1159
1160long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001161 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001162}
1163
george828a471482005-02-06 07:15:53 +00001164void RfbPlayer::updatePos(long newPos) {
1165 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001166 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001167 long time = newPos / 1000;
1168 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001169 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001170
1171 // Update the position of slider
1172 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001173 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1174 sliderStepMs / double(newPos);
1175 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1176 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1177 }
george828a471482005-02-06 07:15:53 +00001178 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001179}
1180
1181void RfbPlayer::skipHandshaking() {
1182 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1183 is->skip(skipBytes);
1184 state_ = RFBSTATE_NORMAL;
1185}
1186
1187void programInfo() {
1188 win32::FileVersionInfo inf;
1189 _tprintf(_T("%s - %s, Version %s\n"),
1190 inf.getVerString(_T("ProductName")),
1191 inf.getVerString(_T("FileDescription")),
1192 inf.getVerString(_T("FileVersion")));
1193 printf("%s\n", buildTime);
1194 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1195}
1196
1197void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001198 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001199}
1200
george825beb62a2005-02-09 13:04:32 +00001201char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001202
1203// playerOptions is the player options with default parameters values,
1204// it is used only for run the player with command-line parameters
1205PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001206bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001207
1208bool processParams(int argc, char* argv[]) {
1209 for (int i = 1; i < argc; i++) {
1210 if ((strcasecmp(argv[i], "-help") == 0) ||
1211 (strcasecmp(argv[i], "--help") == 0) ||
1212 (strcasecmp(argv[i], "/help") == 0) ||
1213 (strcasecmp(argv[i], "-h") == 0) ||
1214 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001215 (strcasecmp(argv[i], "/?") == 0) ||
1216 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001217 print_usage = true;
1218 return true;
1219 }
1220
george825caee412005-03-09 09:52:10 +00001221 if ((strcasecmp(argv[i], "-pf") == 0) ||
1222 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001223 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001224 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001225 return false;
1226 }
george825e7af742005-03-10 14:26:00 +00001227 playerOptions.pixelFormat = pf;
george82193d8e42005-02-20 16:47:01 +00001228 continue;
1229 }
1230
1231
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001232 if ((strcasecmp(argv[i], "-speed") == 0) ||
1233 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001234 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001235 if (playbackSpeed <= 0) {
1236 return false;
1237 }
george825e7af742005-03-10 14:26:00 +00001238 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001239 continue;
1240 }
1241
1242 if ((strcasecmp(argv[i], "-pos") == 0) ||
1243 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001244 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001245 if (initTime <= 0)
1246 return false;
george825e7af742005-03-10 14:26:00 +00001247 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001248 continue;
1249 }
1250
1251 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1252 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001253 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001254 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001255 }
1256
1257 if (i != argc - 1)
1258 return false;
1259 }
1260
1261 fileName = strDup(argv[argc-1]);
1262 return true;
1263}
1264
1265//
1266// -=- WinMain
1267//
1268
1269int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1270
1271 // - Process the command-line
1272
1273 int argc = __argc;
1274 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001275 if ((argc > 1) && (!processParams(argc, argv))) {
1276 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1277 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001278 }
george82e6883de2005-02-08 14:42:12 +00001279
1280 if (print_usage) {
1281 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001282 return 0;
george8267cbcd02005-01-16 15:39:56 +00001283 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001284
george82e6883de2005-02-08 14:42:12 +00001285 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001286 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001287 try {
george825e7af742005-03-10 14:26:00 +00001288 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001289 } catch (rdr::Exception e) {
1290 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1291 delete player;
1292 return 0;
1293 }
1294
1295 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001296 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001297 MSG msg;
1298 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001299 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1300 TranslateMessage(&msg);
1301 DispatchMessage(&msg);
1302 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001303 }
1304
george82e6883de2005-02-08 14:42:12 +00001305 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001306 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001307 if (player) delete player;
1308 } catch (rdr::Exception e) {
1309 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1310 }
1311
1312 return 0;
1313};