blob: 5fb5b16119d01972a8da94dfd1b6bc475eb02b0e [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;
439 case TB_ENDTRACK:
440 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000441 setPaused(isPaused());;
george828a471482005-02-06 07:15:53 +0000442 sliderDraging = false;
443 return 0;
444 default:
445 break;
446 }
447 }
448 break;
george829e6e6cc2005-01-29 13:12:05 +0000449
450 case WM_NOTIFY:
451 switch (((NMHDR*)lParam)->code) {
452 case UDN_DELTAPOS:
453 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000454 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000455 char speedStr[20] = "\0";
456 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
457 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
458 double speed;
459
george824ea27f62005-01-29 15:03:06 +0000460 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000461 if (upDown->iDelta > 0) {
462 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
463 } else {
george824ea27f62005-01-29 15:03:06 +0000464 // It's need to round the UpDown position
465 if ((upDown->iPos * 0.5) != getSpeed()) {
466 upDown->iDelta = 0;
467 lResult = TRUE;
468 }
george829e6e6cc2005-01-29 13:12:05 +0000469 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
470 }
george829e6e6cc2005-01-29 13:12:05 +0000471 sprintf(speedStr, "%.2f", speed);
472 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000473 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000474 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000475 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000476 }
george824ea27f62005-01-29 15:03:06 +0000477 }
george829e6e6cc2005-01-29 13:12:05 +0000478 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000479
480 case WM_CLOSE:
481 vlog.debug("WM_CLOSE %x", getMainHandle());
482 PostQuitMessage(0);
483 break;
484 }
485
486 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
487}
488
489LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
490 switch (msg) {
491
492 case WM_PAINT:
493 {
george821e846ff2005-02-24 13:13:33 +0000494 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000495 seekMode = true;
496 return 0;
497 } else {
498 if (seekMode) {
499 seekMode = false;
500 InvalidateRect(getFrameHandle(), 0, true);
501 UpdateWindow(getFrameHandle());
502 return 0;
503 }
504 }
505
506 PAINTSTRUCT ps;
507 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
508 if (!paintDC)
509 throw SystemException("unable to BeginPaint", GetLastError());
510 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
511
512 if (!pr.is_empty()) {
513
514 if (buffer->bitmap) {
515
516 // Get device context
517 BitmapDC bitmapDC(paintDC, buffer->bitmap);
518
519 // Blit the border if required
520 Rect bufpos = bufferToClient(buffer->getRect());
521 if (!pr.enclosed_by(bufpos)) {
522 vlog.debug("draw border");
523 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
524 RECT r;
525 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
526 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
527 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
528 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
529 }
530
531 // Do the blit
532 Point buf_pos = clientToBuffer(pr.tl);
533 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
534 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
535 throw SystemException("unable to BitBlt to window", GetLastError());
536
537 } else {
538 // Blit a load of black
539 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
540 0, 0, 0, BLACKNESS))
541 throw SystemException("unable to BitBlt to blank window", GetLastError());
542 }
543 }
544 EndPaint(getFrameHandle(), &ps);
545 }
546 return 0;
547
548 case WM_VSCROLL:
549 case WM_HSCROLL:
550 {
551 Point delta;
552 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
553
554 switch (LOWORD(wParam)) {
555 case SB_PAGEUP: newpos -= 50; break;
556 case SB_PAGEDOWN: newpos += 50; break;
557 case SB_LINEUP: newpos -= 5; break;
558 case SB_LINEDOWN: newpos += 5; break;
559 case SB_THUMBTRACK:
560 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
561 default: vlog.info("received unknown scroll message");
562 };
563
564 if (msg == WM_HSCROLL)
565 setViewportOffset(Point(newpos, scrolloffset.y));
566 else
567 setViewportOffset(Point(scrolloffset.x, newpos));
568
569 SCROLLINFO si;
570 si.cbSize = sizeof(si);
571 si.fMask = SIF_POS;
572 si.nPos = newpos;
573 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
574 }
575 break;
576 }
577
578 return DefWindowProc(hwnd, msg, wParam, lParam);
579}
580
george82d070c692005-01-19 16:44:04 +0000581void RfbPlayer::createToolBar(HWND parentHwnd) {
582 RECT tRect;
583 InitCommonControls();
584
585 tb.create(ID_TOOLBAR, parentHwnd);
586 tb.addBitmap(4, IDB_TOOLBAR);
587
588 // Create the control buttons
589 tb.addButton(0, ID_PLAY);
590 tb.addButton(1, ID_PAUSE);
591 tb.addButton(2, ID_STOP);
592 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
593 tb.addButton(3, ID_FULLSCREEN);
594 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
595
596 // Create the static control for the time output
597 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
598 tb.getButtonRect(6, &tRect);
599 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
600 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
601 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
602 GetModuleHandle(0), 0);
603 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
604
605 // Create the trackbar control for the time position
606 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
607 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000608 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000609 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
610 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
611 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
612 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000613 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000614 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
615
616 // Create the label with "Speed:" caption
617 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
618 tb.getButtonRect(10, &tRect);
619 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
620 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
621 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
622
623 // Create the edit control and the spin for the speed managing
624 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
625 tb.getButtonRect(11, &tRect);
626 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
627 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
628 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
629 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
630 // It's need to send notify messages to toolbar parent window
631 SetParent(speedEdit, tb.getHandle());
632
633 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
634 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000635 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000636}
637
george82a21d2952005-02-12 11:30:03 +0000638void RfbPlayer::disableTBandMenuItems() {
639 // Disable the menu items
640 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
641 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
642 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
643 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
644 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
645 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
646 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
647 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
648 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
649
650 // Disable the toolbar buttons and child controls
651 tb.enableButton(ID_PLAY, false);
652 tb.enableButton(ID_PAUSE, false);
653 tb.enableButton(ID_STOP, false);
654 tb.enableButton(ID_FULLSCREEN, false);
655 EnableWindow(posTrackBar, false);
656 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000657 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000658}
659
george82f5043162005-02-12 11:37:18 +0000660void RfbPlayer::enableTBandMenuItems() {
661 // Enable the menu items
662 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
663 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
664 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
665 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
666 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
667 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
668 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
669 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
670 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
671
672 // Enable the toolbar buttons and child controls
673 tb.enableButton(ID_PLAY, true);
674 tb.enableButton(ID_PAUSE, true);
675 tb.enableButton(ID_STOP, true);
676 tb.enableButton(ID_FULLSCREEN, true);
677 EnableWindow(posTrackBar, true);
678 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000679 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000680}
681
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000682void RfbPlayer::setVisible(bool visible) {
683 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
684 if (visible) {
685 // When the window becomes visible, make it active
686 SetForegroundWindow(getMainHandle());
687 SetActiveWindow(getMainHandle());
688 }
689}
690
691void RfbPlayer::setTitle(const char *title) {
692 char _title[256];
693 strcpy(_title, AppName);
694 strcat(_title, " - ");
695 strcat(_title, title);
696 SetWindowText(getMainHandle(), _title);
697}
698
699void RfbPlayer::setFrameSize(int width, int height) {
700 // Calculate and set required size for main window
701 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000702 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000703 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
704 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
705 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000706 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
707 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
708 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000709 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
710
711 // Enable/disable scrollbars as appropriate
712 calculateScrollBars();
713}
714
715void RfbPlayer::calculateScrollBars() {
716 // Calculate the required size of window
717 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
718 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
719 DWORD old_style;
720 RECT r;
721 SetRect(&r, 0, 0, buffer->width(), buffer->height());
722 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
723 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
724
725 // Work out whether scroll bars are required
726 do {
727 old_style = style;
728
729 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
730 style |= WS_HSCROLL;
731 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
732 }
733 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
734 style |= WS_VSCROLL;
735 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
736 }
737 } while (style != old_style);
738
739 // Tell Windows to update the window style & cached settings
740 if (style != current_style) {
741 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
742 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
743 }
744
745 // Update the scroll settings
746 SCROLLINFO si;
747 if (style & WS_VSCROLL) {
748 si.cbSize = sizeof(si);
749 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
750 si.nMin = 0;
751 si.nMax = buffer->height();
752 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
753 maxscrolloffset.y = max(0, si.nMax-si.nPage);
754 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
755 si.nPos = scrolloffset.y;
756 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
757 }
758 if (style & WS_HSCROLL) {
759 si.cbSize = sizeof(si);
760 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
761 si.nMin = 0;
762 si.nMax = buffer->width();
763 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
764 maxscrolloffset.x = max(0, si.nMax-si.nPage);
765 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
766 si.nPos = scrolloffset.x;
767 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
768 }
george82a758a7a2005-03-15 16:34:57 +0000769
770 // Update the cached client size
771 GetClientRect(getFrameHandle(), &r);
772 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000773}
774
775bool RfbPlayer::setViewportOffset(const Point& tl) {
776/* ***
777 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
778 max(0, min(maxscrolloffset.y, tl.y)));
779 */
780 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
781 max(0, min(tl.y, buffer->height()-client_size.height())));
782 Point delta = np.translate(scrolloffset.negate());
783 if (!np.equals(scrolloffset)) {
784 scrolloffset = np;
785 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
786 UpdateWindow(getFrameHandle());
787 return true;
788 }
789 return false;
790}
791
792void RfbPlayer::close(const char* reason) {
793 setVisible(false);
794 if (reason) {
795 vlog.info("closing - %s", reason);
796 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
797 }
798 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
799}
800
801void RfbPlayer::blankBuffer() {
802 fillRect(buffer->getRect(), 0);
803}
804
805void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000806 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000807 blankBuffer();
808 newSession(fileName);
809 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000810 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000811 if (paused) is->pausePlayback();
812 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000813}
814
815void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000816 // Perform return if waitWhilePaused processed because
817 // rfbReader thread could receive the signal to close
818 if (waitWhilePaused()) return;
819
george8223e08562005-01-31 15:16:42 +0000820 static long update_time = GetTickCount();
821 try {
george828a471482005-02-06 07:15:53 +0000822 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
823 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000824 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000825 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000826 update_time = GetTickCount();
827 }
828 RfbProto::processMsg();
829 } catch (rdr::Exception e) {
830 if (strcmp(e.str(), "[End Of File]") == 0) {
831 rewind();
george825e7af742005-03-10 14:26:00 +0000832 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000833 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000834 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000835 return;
836 }
837 // It's a special exception to perform backward seeking.
838 // We only rewind the stream and seek the offset
839 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000840 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000841 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000842 rewind();
george820d2e19d2005-03-03 15:47:55 +0000843 if (!stopped) setPos(seekOffset);
844 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000845 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000846 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000847 return;
848 }
849 // It's a special exception which is used to terminate the playback
850 if (strcmp(e.str(), "[TERMINATE]") == 0) {
851 sessionTerminateThread *terminate = new sessionTerminateThread(this);
852 terminate->start();
george8223e08562005-01-31 15:16:42 +0000853 } else {
george820e980cc2005-03-10 18:18:34 +0000854 // Show the exception message and close the session playback
855 is->pausePlayback();
856 char message[256] = "\0";
857 strcat(message, e.str());
858 strcat(message, "\nMaybe you force wrong the pixel format for this session");
859 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
860 sessionTerminateThread *terminate = new sessionTerminateThread(this);
861 terminate->start();
george8223e08562005-01-31 15:16:42 +0000862 return;
863 }
864 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000865}
866
867void RfbPlayer::serverInit() {
868 RfbProto::serverInit();
869
george82b95503e2005-02-21 17:02:34 +0000870 // Save the image data start time
871 imageDataStartTime = is->getTimeOffset();
872
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000873 // Resize the backing buffer
874 buffer->setSize(cp.width, cp.height);
875
876 // Check on the true colour mode
877 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000878 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000879
880 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000881 static long pixelFormat = PF_AUTO;
882 if (options.askPixelFormat) {
883 ChoosePixelFormatDialog choosePixelFormatDialog(pixelFormat);
george82d9957b72005-03-11 14:22:14 +0000884 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george825e7af742005-03-10 14:26:00 +0000885 pixelFormat = choosePixelFormatDialog.getPF();
george822c7634b2005-03-10 18:03:27 +0000886 } else {
887 is->pausePlayback();
888 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000889 }
890 } else {
891 pixelFormat = options.pixelFormat;
892 }
george825caee412005-03-09 09:52:10 +0000893 switch (pixelFormat) {
894 case PF_AUTO:
george82193d8e42005-02-20 16:47:01 +0000895 break;
george825caee412005-03-09 09:52:10 +0000896 case PF_D8_RGB332:
george82193d8e42005-02-20 16:47:01 +0000897 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
898 break;
george825caee412005-03-09 09:52:10 +0000899 case PF_D16_RGB655:
george82193d8e42005-02-20 16:47:01 +0000900 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
901 break;
george825caee412005-03-09 09:52:10 +0000902 case PF_D24_RGB888:
george82193d8e42005-02-20 16:47:01 +0000903 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
904 break;
905 default:
906 throw rdr::Exception("This color depth is not supported!");
907 }
908 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000909
910 // If the window is not maximised then resize it
911 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
912 setFrameSize(cp.width, cp.height);
913
914 // Set the window title and show it
915 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000916
george82d4d69e62005-02-05 09:23:18 +0000917 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000918 sessionTimeMs = calculateSessionTime(fileName);
919 sprintf(fullSessionTime, "%.2um:%.2us",
920 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000921 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000922 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
923 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000924 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000925
george825e7af742005-03-10 14:26:00 +0000926 setPaused(!options.autoPlay);
927 // Restore the parameters from registry,
928 // which was replaced by command-line parameters.
929 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000930}
931
932void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
933 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
934 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
935/* int i;
936 for (i=0;i<count;i++) {
937 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
938 }
939 // *** change to 0, 256?
940 refreshWindowPalette(first, count);
941 palette_changed = true;
942 InvalidateRect(getFrameHandle(), 0, FALSE);*/
943}
944
945void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000946 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000947 MessageBeep(-1);
948}
949
950void RfbPlayer::serverCutText(const char* str, int len) {
951 if (cutText != NULL)
952 delete [] cutText;
953 cutText = new char[len + 1];
954 memcpy(cutText, str, len);
955 cutText[len] = '\0';
956}
957
958void RfbPlayer::frameBufferUpdateEnd() {
959};
960
961void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
962}
963
964void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
965}
966
967
968void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
969 buffer->fillRect(r, pix);
970 invalidateBufferRect(r);
971}
972
973void RfbPlayer::imageRect(const Rect& r, void* pixels) {
974 buffer->imageRect(r, pixels);
975 invalidateBufferRect(r);
976}
977
978void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
979 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
980 invalidateBufferRect(r);
981}
982
983bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
984 Rect rect = bufferToClient(crect);
985 if (rect.intersect(client_size).is_empty()) return false;
986 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
987 InvalidateRect(getFrameHandle(), &invalid, FALSE);
988 return true;
989}
990
george820d2e19d2005-03-03 15:47:55 +0000991bool RfbPlayer::waitWhilePaused() {
992 bool result = false;
993 while(isPaused() && !isSeeking()) {
994 Sleep(20);
995 result = true;
996 }
997 return result;
998}
999
george8257f13522005-02-05 08:48:22 +00001000long RfbPlayer::calculateSessionTime(char *filename) {
1001 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001002 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001003 try {
1004 while (TRUE) {
1005 sessionFile.skip(1024);
1006 }
1007 } catch (rdr::Exception e) {
1008 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001009 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001010 } else {
1011 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1012 return 0;
1013 }
1014 }
1015 return 0;
1016}
1017
george826b87aff2005-02-13 10:48:21 +00001018void RfbPlayer::closeSessionFile() {
1019 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001020 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001021 RECT r;
1022
1023 // Uncheck all toolbar buttons
1024 if (tb.getHandle()) {
1025 tb.checkButton(ID_PLAY, false);
1026 tb.checkButton(ID_PAUSE, false);
1027 tb.checkButton(ID_STOP, false);
1028 }
1029
1030 // Stop playback and update the player state
1031 disableTBandMenuItems();
1032 if (rfbReader) {
1033 delete rfbReader->join();
1034 rfbReader = 0;
1035 delete [] fileName;
1036 fileName = 0;
1037 }
1038 blankBuffer();
1039 setTitle("None");
george827009c892005-02-19 12:49:42 +00001040 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001041 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001042 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001043 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1044 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001045 SetWindowText(speedEdit, speedStr);
1046 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1047
1048 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001049 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1050 dwStyle &= ~WS_MAXIMIZE;
1051 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1052 }
george822ff7a612005-02-19 17:05:24 +00001053 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1054 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1055 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001056 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001057 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001058 buffer->setSize(32, 32);
1059 calculateScrollBars();
1060
1061 // Update the cached sizing information and repaint the frame window
1062 GetWindowRect(getFrameHandle(), &r);
1063 window_size = Rect(r.left, r.top, r.right, r.bottom);
1064 GetClientRect(getFrameHandle(), &r);
1065 client_size = Rect(r.left, r.top, r.right, r.bottom);
1066 InvalidateRect(getFrameHandle(), 0, TRUE);
1067 UpdateWindow(getFrameHandle());
1068}
1069
george8217e92cb2005-01-31 16:01:02 +00001070void RfbPlayer::openSessionFile(char *_fileName) {
1071 fileName = strDup(_fileName);
1072
1073 // Close the previous reading thread
1074 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001075 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001076 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001077 }
1078 blankBuffer();
1079 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001080 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001081 rfbReader = new rfbSessionReader(this);
1082 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001083 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001084 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001085}
1086
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001087void RfbPlayer::setPaused(bool paused) {
1088 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001089 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001090 tb.checkButton(ID_PAUSE, true);
1091 tb.checkButton(ID_PLAY, false);
1092 tb.checkButton(ID_STOP, false);
1093 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1094 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001095 } else {
george825beb62a2005-02-09 13:04:32 +00001096 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001097 tb.checkButton(ID_PLAY, true);
1098 tb.checkButton(ID_STOP, false);
1099 tb.checkButton(ID_PAUSE, false);
1100 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1101 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001102 }
1103}
1104
george82006f2792005-02-05 07:40:47 +00001105void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001106 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001107 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001108 if (is) {
1109 is->pausePlayback();
1110 is->interruptFrameDelay();
1111 }
george82006f2792005-02-05 07:40:47 +00001112 tb.checkButton(ID_STOP, true);
1113 tb.checkButton(ID_PLAY, false);
1114 tb.checkButton(ID_PAUSE, false);
1115 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1116 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001117 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001118}
1119
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001120void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001121 if (speed > 0) {
1122 char speedStr[20] = "\0";
1123 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001124 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001125 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001126 SendMessage(speedUpDown, UDM_SETPOS,
1127 0, MAKELONG((short)(newSpeed / 0.5), 0));
1128 sprintf(speedStr, "%.2f", newSpeed);
1129 SetWindowText(speedEdit, speedStr);
1130 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001131}
1132
1133double RfbPlayer::getSpeed() {
1134 return is->getSpeed();
1135}
1136
1137void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001138 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001139}
1140
1141long RfbPlayer::getSeekOffset() {
1142 return is->getSeekOffset();
1143}
1144
1145bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001146 if (is) return is->isSeeking();
1147 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001148}
1149
1150bool RfbPlayer::isSeekMode() {
1151 return seekMode;
1152}
1153
1154bool RfbPlayer::isPaused() {
1155 return is->isPaused();
1156}
1157
1158long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001159 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001160}
1161
george828a471482005-02-06 07:15:53 +00001162void RfbPlayer::updatePos(long newPos) {
1163 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001164 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001165 long time = newPos / 1000;
1166 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001167 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001168
1169 // Update the position of slider
1170 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001171 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1172 sliderStepMs / double(newPos);
1173 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1174 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1175 }
george828a471482005-02-06 07:15:53 +00001176 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001177}
1178
1179void RfbPlayer::skipHandshaking() {
1180 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1181 is->skip(skipBytes);
1182 state_ = RFBSTATE_NORMAL;
1183}
1184
1185void programInfo() {
1186 win32::FileVersionInfo inf;
1187 _tprintf(_T("%s - %s, Version %s\n"),
1188 inf.getVerString(_T("ProductName")),
1189 inf.getVerString(_T("FileDescription")),
1190 inf.getVerString(_T("FileVersion")));
1191 printf("%s\n", buildTime);
1192 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1193}
1194
1195void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001196 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001197}
1198
george825beb62a2005-02-09 13:04:32 +00001199char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001200
1201// playerOptions is the player options with default parameters values,
1202// it is used only for run the player with command-line parameters
1203PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001204bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001205
1206bool processParams(int argc, char* argv[]) {
1207 for (int i = 1; i < argc; i++) {
1208 if ((strcasecmp(argv[i], "-help") == 0) ||
1209 (strcasecmp(argv[i], "--help") == 0) ||
1210 (strcasecmp(argv[i], "/help") == 0) ||
1211 (strcasecmp(argv[i], "-h") == 0) ||
1212 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001213 (strcasecmp(argv[i], "/?") == 0) ||
1214 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001215 print_usage = true;
1216 return true;
1217 }
1218
george825caee412005-03-09 09:52:10 +00001219 if ((strcasecmp(argv[i], "-pf") == 0) ||
1220 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001221 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001222 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001223 return false;
1224 }
george825e7af742005-03-10 14:26:00 +00001225 playerOptions.pixelFormat = pf;
george82193d8e42005-02-20 16:47:01 +00001226 continue;
1227 }
1228
1229
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001230 if ((strcasecmp(argv[i], "-speed") == 0) ||
1231 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001232 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001233 if (playbackSpeed <= 0) {
1234 return false;
1235 }
george825e7af742005-03-10 14:26:00 +00001236 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001237 continue;
1238 }
1239
1240 if ((strcasecmp(argv[i], "-pos") == 0) ||
1241 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001242 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001243 if (initTime <= 0)
1244 return false;
george825e7af742005-03-10 14:26:00 +00001245 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001246 continue;
1247 }
1248
1249 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1250 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001251 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001252 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001253 }
1254
1255 if (i != argc - 1)
1256 return false;
1257 }
1258
1259 fileName = strDup(argv[argc-1]);
1260 return true;
1261}
1262
1263//
1264// -=- WinMain
1265//
1266
1267int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1268
1269 // - Process the command-line
1270
1271 int argc = __argc;
1272 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001273 if ((argc > 1) && (!processParams(argc, argv))) {
1274 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1275 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001276 }
george82e6883de2005-02-08 14:42:12 +00001277
1278 if (print_usage) {
1279 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001280 return 0;
george8267cbcd02005-01-16 15:39:56 +00001281 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001282
george82e6883de2005-02-08 14:42:12 +00001283 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001284 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001285 try {
george825e7af742005-03-10 14:26:00 +00001286 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001287 } catch (rdr::Exception e) {
1288 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1289 delete player;
1290 return 0;
1291 }
1292
1293 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001294 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001295 MSG msg;
1296 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001297 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1298 TranslateMessage(&msg);
1299 DispatchMessage(&msg);
1300 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001301 }
1302
george82e6883de2005-02-08 14:42:12 +00001303 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001304 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001305 if (player) delete player;
1306 } catch (rdr::Exception e) {
1307 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1308 }
1309
1310 return 0;
1311};