blob: a4942076703f6247fd47ecba1934bc3fda0e4fc9 [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;
george8203c01da2005-03-16 12:36:53 +0000549
550 // Process play/pause by the right mouse button
551 case WM_RBUTTONDOWN:
552 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
553 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000554
555 case WM_VSCROLL:
556 case WM_HSCROLL:
557 {
558 Point delta;
559 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
560
561 switch (LOWORD(wParam)) {
562 case SB_PAGEUP: newpos -= 50; break;
563 case SB_PAGEDOWN: newpos += 50; break;
564 case SB_LINEUP: newpos -= 5; break;
565 case SB_LINEDOWN: newpos += 5; break;
566 case SB_THUMBTRACK:
567 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
568 default: vlog.info("received unknown scroll message");
569 };
570
571 if (msg == WM_HSCROLL)
572 setViewportOffset(Point(newpos, scrolloffset.y));
573 else
574 setViewportOffset(Point(scrolloffset.x, newpos));
575
576 SCROLLINFO si;
577 si.cbSize = sizeof(si);
578 si.fMask = SIF_POS;
579 si.nPos = newpos;
580 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
581 }
582 break;
583 }
584
585 return DefWindowProc(hwnd, msg, wParam, lParam);
586}
587
george82d070c692005-01-19 16:44:04 +0000588void RfbPlayer::createToolBar(HWND parentHwnd) {
589 RECT tRect;
590 InitCommonControls();
591
592 tb.create(ID_TOOLBAR, parentHwnd);
593 tb.addBitmap(4, IDB_TOOLBAR);
594
595 // Create the control buttons
596 tb.addButton(0, ID_PLAY);
597 tb.addButton(1, ID_PAUSE);
598 tb.addButton(2, ID_STOP);
599 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
600 tb.addButton(3, ID_FULLSCREEN);
601 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
602
603 // Create the static control for the time output
604 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
605 tb.getButtonRect(6, &tRect);
606 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
607 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
608 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
609 GetModuleHandle(0), 0);
610 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
611
612 // Create the trackbar control for the time position
613 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
614 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000615 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000616 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
617 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
618 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
619 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000620 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000621 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
622
623 // Create the label with "Speed:" caption
624 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
625 tb.getButtonRect(10, &tRect);
626 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
627 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
628 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
629
630 // Create the edit control and the spin for the speed managing
631 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
632 tb.getButtonRect(11, &tRect);
633 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
634 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
635 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
636 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
637 // It's need to send notify messages to toolbar parent window
638 SetParent(speedEdit, tb.getHandle());
639
640 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
641 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000642 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000643}
644
george82a21d2952005-02-12 11:30:03 +0000645void RfbPlayer::disableTBandMenuItems() {
646 // Disable the menu items
647 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
648 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
649 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
650 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
651 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
652 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
653 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
654 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
655 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
656
657 // Disable the toolbar buttons and child controls
658 tb.enableButton(ID_PLAY, false);
659 tb.enableButton(ID_PAUSE, false);
660 tb.enableButton(ID_STOP, false);
661 tb.enableButton(ID_FULLSCREEN, false);
662 EnableWindow(posTrackBar, false);
663 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000664 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000665}
666
george82f5043162005-02-12 11:37:18 +0000667void RfbPlayer::enableTBandMenuItems() {
668 // Enable the menu items
669 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
670 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
671 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
672 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
673 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
674 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
675 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
676 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
677 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
678
679 // Enable the toolbar buttons and child controls
680 tb.enableButton(ID_PLAY, true);
681 tb.enableButton(ID_PAUSE, true);
682 tb.enableButton(ID_STOP, true);
683 tb.enableButton(ID_FULLSCREEN, true);
684 EnableWindow(posTrackBar, true);
685 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000686 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000687}
688
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000689void RfbPlayer::setVisible(bool visible) {
690 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
691 if (visible) {
692 // When the window becomes visible, make it active
693 SetForegroundWindow(getMainHandle());
694 SetActiveWindow(getMainHandle());
695 }
696}
697
698void RfbPlayer::setTitle(const char *title) {
699 char _title[256];
700 strcpy(_title, AppName);
701 strcat(_title, " - ");
702 strcat(_title, title);
703 SetWindowText(getMainHandle(), _title);
704}
705
706void RfbPlayer::setFrameSize(int width, int height) {
707 // Calculate and set required size for main window
708 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000709 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000710 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
711 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
712 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000713 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
714 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
715 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000716 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
717
718 // Enable/disable scrollbars as appropriate
719 calculateScrollBars();
720}
721
722void RfbPlayer::calculateScrollBars() {
723 // Calculate the required size of window
724 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
725 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
726 DWORD old_style;
727 RECT r;
728 SetRect(&r, 0, 0, buffer->width(), buffer->height());
729 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
730 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
731
732 // Work out whether scroll bars are required
733 do {
734 old_style = style;
735
736 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
737 style |= WS_HSCROLL;
738 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
739 }
740 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
741 style |= WS_VSCROLL;
742 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
743 }
744 } while (style != old_style);
745
746 // Tell Windows to update the window style & cached settings
747 if (style != current_style) {
748 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
749 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
750 }
751
752 // Update the scroll settings
753 SCROLLINFO si;
754 if (style & WS_VSCROLL) {
755 si.cbSize = sizeof(si);
756 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
757 si.nMin = 0;
758 si.nMax = buffer->height();
759 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
760 maxscrolloffset.y = max(0, si.nMax-si.nPage);
761 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
762 si.nPos = scrolloffset.y;
763 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
764 }
765 if (style & WS_HSCROLL) {
766 si.cbSize = sizeof(si);
767 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
768 si.nMin = 0;
769 si.nMax = buffer->width();
770 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
771 maxscrolloffset.x = max(0, si.nMax-si.nPage);
772 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
773 si.nPos = scrolloffset.x;
774 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
775 }
george82a758a7a2005-03-15 16:34:57 +0000776
777 // Update the cached client size
778 GetClientRect(getFrameHandle(), &r);
779 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000780}
781
782bool RfbPlayer::setViewportOffset(const Point& tl) {
783/* ***
784 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
785 max(0, min(maxscrolloffset.y, tl.y)));
786 */
787 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
788 max(0, min(tl.y, buffer->height()-client_size.height())));
789 Point delta = np.translate(scrolloffset.negate());
790 if (!np.equals(scrolloffset)) {
791 scrolloffset = np;
792 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
793 UpdateWindow(getFrameHandle());
794 return true;
795 }
796 return false;
797}
798
799void RfbPlayer::close(const char* reason) {
800 setVisible(false);
801 if (reason) {
802 vlog.info("closing - %s", reason);
803 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
804 }
805 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
806}
807
808void RfbPlayer::blankBuffer() {
809 fillRect(buffer->getRect(), 0);
810}
811
812void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000813 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000814 blankBuffer();
815 newSession(fileName);
816 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000817 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000818 if (paused) is->pausePlayback();
819 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000820}
821
822void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000823 // Perform return if waitWhilePaused processed because
824 // rfbReader thread could receive the signal to close
825 if (waitWhilePaused()) return;
826
george8223e08562005-01-31 15:16:42 +0000827 static long update_time = GetTickCount();
828 try {
george828a471482005-02-06 07:15:53 +0000829 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
830 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000831 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000832 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000833 update_time = GetTickCount();
834 }
835 RfbProto::processMsg();
836 } catch (rdr::Exception e) {
837 if (strcmp(e.str(), "[End Of File]") == 0) {
838 rewind();
george825e7af742005-03-10 14:26:00 +0000839 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000840 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000841 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000842 return;
843 }
844 // It's a special exception to perform backward seeking.
845 // We only rewind the stream and seek the offset
846 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000847 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000848 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000849 rewind();
george820d2e19d2005-03-03 15:47:55 +0000850 if (!stopped) setPos(seekOffset);
851 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000852 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000853 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000854 return;
855 }
856 // It's a special exception which is used to terminate the playback
857 if (strcmp(e.str(), "[TERMINATE]") == 0) {
858 sessionTerminateThread *terminate = new sessionTerminateThread(this);
859 terminate->start();
george8223e08562005-01-31 15:16:42 +0000860 } else {
george820e980cc2005-03-10 18:18:34 +0000861 // Show the exception message and close the session playback
862 is->pausePlayback();
863 char message[256] = "\0";
864 strcat(message, e.str());
865 strcat(message, "\nMaybe you force wrong the pixel format for this session");
866 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
867 sessionTerminateThread *terminate = new sessionTerminateThread(this);
868 terminate->start();
george8223e08562005-01-31 15:16:42 +0000869 return;
870 }
871 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000872}
873
874void RfbPlayer::serverInit() {
875 RfbProto::serverInit();
876
george82b95503e2005-02-21 17:02:34 +0000877 // Save the image data start time
878 imageDataStartTime = is->getTimeOffset();
879
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000880 // Resize the backing buffer
881 buffer->setSize(cp.width, cp.height);
882
883 // Check on the true colour mode
884 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000885 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000886
887 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000888 static long pixelFormat = PF_AUTO;
889 if (options.askPixelFormat) {
890 ChoosePixelFormatDialog choosePixelFormatDialog(pixelFormat);
george82d9957b72005-03-11 14:22:14 +0000891 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george825e7af742005-03-10 14:26:00 +0000892 pixelFormat = choosePixelFormatDialog.getPF();
george822c7634b2005-03-10 18:03:27 +0000893 } else {
894 is->pausePlayback();
895 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000896 }
897 } else {
898 pixelFormat = options.pixelFormat;
899 }
george825caee412005-03-09 09:52:10 +0000900 switch (pixelFormat) {
901 case PF_AUTO:
george82193d8e42005-02-20 16:47:01 +0000902 break;
george825caee412005-03-09 09:52:10 +0000903 case PF_D8_RGB332:
george82193d8e42005-02-20 16:47:01 +0000904 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
905 break;
george825caee412005-03-09 09:52:10 +0000906 case PF_D16_RGB655:
george82193d8e42005-02-20 16:47:01 +0000907 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
908 break;
george825caee412005-03-09 09:52:10 +0000909 case PF_D24_RGB888:
george82193d8e42005-02-20 16:47:01 +0000910 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
911 break;
912 default:
913 throw rdr::Exception("This color depth is not supported!");
914 }
915 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000916
917 // If the window is not maximised then resize it
918 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
919 setFrameSize(cp.width, cp.height);
920
921 // Set the window title and show it
922 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000923
george82d4d69e62005-02-05 09:23:18 +0000924 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000925 sessionTimeMs = calculateSessionTime(fileName);
926 sprintf(fullSessionTime, "%.2um:%.2us",
927 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000928 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000929 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
930 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000931 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000932
george825e7af742005-03-10 14:26:00 +0000933 setPaused(!options.autoPlay);
934 // Restore the parameters from registry,
935 // which was replaced by command-line parameters.
936 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000937}
938
939void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
940 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
941 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
942/* int i;
943 for (i=0;i<count;i++) {
944 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
945 }
946 // *** change to 0, 256?
947 refreshWindowPalette(first, count);
948 palette_changed = true;
949 InvalidateRect(getFrameHandle(), 0, FALSE);*/
950}
951
952void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000953 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000954 MessageBeep(-1);
955}
956
957void RfbPlayer::serverCutText(const char* str, int len) {
958 if (cutText != NULL)
959 delete [] cutText;
960 cutText = new char[len + 1];
961 memcpy(cutText, str, len);
962 cutText[len] = '\0';
963}
964
965void RfbPlayer::frameBufferUpdateEnd() {
966};
967
968void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
969}
970
971void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
972}
973
974
975void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
976 buffer->fillRect(r, pix);
977 invalidateBufferRect(r);
978}
979
980void RfbPlayer::imageRect(const Rect& r, void* pixels) {
981 buffer->imageRect(r, pixels);
982 invalidateBufferRect(r);
983}
984
985void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
986 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
987 invalidateBufferRect(r);
988}
989
990bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
991 Rect rect = bufferToClient(crect);
992 if (rect.intersect(client_size).is_empty()) return false;
993 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
994 InvalidateRect(getFrameHandle(), &invalid, FALSE);
995 return true;
996}
997
george820d2e19d2005-03-03 15:47:55 +0000998bool RfbPlayer::waitWhilePaused() {
999 bool result = false;
1000 while(isPaused() && !isSeeking()) {
1001 Sleep(20);
1002 result = true;
1003 }
1004 return result;
1005}
1006
george8257f13522005-02-05 08:48:22 +00001007long RfbPlayer::calculateSessionTime(char *filename) {
1008 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001009 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001010 try {
1011 while (TRUE) {
1012 sessionFile.skip(1024);
1013 }
1014 } catch (rdr::Exception e) {
1015 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001016 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001017 } else {
1018 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1019 return 0;
1020 }
1021 }
1022 return 0;
1023}
1024
george826b87aff2005-02-13 10:48:21 +00001025void RfbPlayer::closeSessionFile() {
1026 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001027 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001028 RECT r;
1029
1030 // Uncheck all toolbar buttons
1031 if (tb.getHandle()) {
1032 tb.checkButton(ID_PLAY, false);
1033 tb.checkButton(ID_PAUSE, false);
1034 tb.checkButton(ID_STOP, false);
1035 }
1036
1037 // Stop playback and update the player state
1038 disableTBandMenuItems();
1039 if (rfbReader) {
1040 delete rfbReader->join();
1041 rfbReader = 0;
1042 delete [] fileName;
1043 fileName = 0;
1044 }
1045 blankBuffer();
1046 setTitle("None");
george827009c892005-02-19 12:49:42 +00001047 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001048 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001049 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001050 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1051 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001052 SetWindowText(speedEdit, speedStr);
1053 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1054
1055 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001056 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1057 dwStyle &= ~WS_MAXIMIZE;
1058 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1059 }
george822ff7a612005-02-19 17:05:24 +00001060 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1061 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1062 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001063 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001064 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001065 buffer->setSize(32, 32);
1066 calculateScrollBars();
1067
1068 // Update the cached sizing information and repaint the frame window
1069 GetWindowRect(getFrameHandle(), &r);
1070 window_size = Rect(r.left, r.top, r.right, r.bottom);
1071 GetClientRect(getFrameHandle(), &r);
1072 client_size = Rect(r.left, r.top, r.right, r.bottom);
1073 InvalidateRect(getFrameHandle(), 0, TRUE);
1074 UpdateWindow(getFrameHandle());
1075}
1076
george8217e92cb2005-01-31 16:01:02 +00001077void RfbPlayer::openSessionFile(char *_fileName) {
1078 fileName = strDup(_fileName);
1079
1080 // Close the previous reading thread
1081 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001082 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001083 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001084 }
1085 blankBuffer();
1086 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001087 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001088 rfbReader = new rfbSessionReader(this);
1089 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001090 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001091 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001092}
1093
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001094void RfbPlayer::setPaused(bool paused) {
1095 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001096 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001097 tb.checkButton(ID_PAUSE, true);
1098 tb.checkButton(ID_PLAY, false);
1099 tb.checkButton(ID_STOP, false);
1100 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1101 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001102 } else {
george825beb62a2005-02-09 13:04:32 +00001103 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001104 tb.checkButton(ID_PLAY, true);
1105 tb.checkButton(ID_STOP, false);
1106 tb.checkButton(ID_PAUSE, false);
1107 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1108 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001109 }
1110}
1111
george82006f2792005-02-05 07:40:47 +00001112void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001113 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001114 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001115 if (is) {
1116 is->pausePlayback();
1117 is->interruptFrameDelay();
1118 }
george82006f2792005-02-05 07:40:47 +00001119 tb.checkButton(ID_STOP, true);
1120 tb.checkButton(ID_PLAY, false);
1121 tb.checkButton(ID_PAUSE, false);
1122 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1123 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001124 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001125}
1126
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001127void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001128 if (speed > 0) {
1129 char speedStr[20] = "\0";
1130 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001131 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001132 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001133 SendMessage(speedUpDown, UDM_SETPOS,
1134 0, MAKELONG((short)(newSpeed / 0.5), 0));
1135 sprintf(speedStr, "%.2f", newSpeed);
1136 SetWindowText(speedEdit, speedStr);
1137 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001138}
1139
1140double RfbPlayer::getSpeed() {
1141 return is->getSpeed();
1142}
1143
1144void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001145 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001146}
1147
1148long RfbPlayer::getSeekOffset() {
1149 return is->getSeekOffset();
1150}
1151
1152bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001153 if (is) return is->isSeeking();
1154 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001155}
1156
1157bool RfbPlayer::isSeekMode() {
1158 return seekMode;
1159}
1160
1161bool RfbPlayer::isPaused() {
1162 return is->isPaused();
1163}
1164
1165long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001166 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001167}
1168
george828a471482005-02-06 07:15:53 +00001169void RfbPlayer::updatePos(long newPos) {
1170 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001171 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001172 long time = newPos / 1000;
1173 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001174 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001175
1176 // Update the position of slider
1177 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001178 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1179 sliderStepMs / double(newPos);
1180 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1181 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1182 }
george828a471482005-02-06 07:15:53 +00001183 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001184}
1185
1186void RfbPlayer::skipHandshaking() {
1187 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1188 is->skip(skipBytes);
1189 state_ = RFBSTATE_NORMAL;
1190}
1191
1192void programInfo() {
1193 win32::FileVersionInfo inf;
1194 _tprintf(_T("%s - %s, Version %s\n"),
1195 inf.getVerString(_T("ProductName")),
1196 inf.getVerString(_T("FileDescription")),
1197 inf.getVerString(_T("FileVersion")));
1198 printf("%s\n", buildTime);
1199 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1200}
1201
1202void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001203 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001204}
1205
george825beb62a2005-02-09 13:04:32 +00001206char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001207
1208// playerOptions is the player options with default parameters values,
1209// it is used only for run the player with command-line parameters
1210PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001211bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001212
1213bool processParams(int argc, char* argv[]) {
1214 for (int i = 1; i < argc; i++) {
1215 if ((strcasecmp(argv[i], "-help") == 0) ||
1216 (strcasecmp(argv[i], "--help") == 0) ||
1217 (strcasecmp(argv[i], "/help") == 0) ||
1218 (strcasecmp(argv[i], "-h") == 0) ||
1219 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001220 (strcasecmp(argv[i], "/?") == 0) ||
1221 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001222 print_usage = true;
1223 return true;
1224 }
1225
george825caee412005-03-09 09:52:10 +00001226 if ((strcasecmp(argv[i], "-pf") == 0) ||
1227 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001228 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001229 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001230 return false;
1231 }
george825e7af742005-03-10 14:26:00 +00001232 playerOptions.pixelFormat = pf;
george82193d8e42005-02-20 16:47:01 +00001233 continue;
1234 }
1235
1236
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001237 if ((strcasecmp(argv[i], "-speed") == 0) ||
1238 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001239 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001240 if (playbackSpeed <= 0) {
1241 return false;
1242 }
george825e7af742005-03-10 14:26:00 +00001243 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001244 continue;
1245 }
1246
1247 if ((strcasecmp(argv[i], "-pos") == 0) ||
1248 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001249 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001250 if (initTime <= 0)
1251 return false;
george825e7af742005-03-10 14:26:00 +00001252 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001253 continue;
1254 }
1255
1256 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1257 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001258 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001259 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001260 }
1261
1262 if (i != argc - 1)
1263 return false;
1264 }
1265
1266 fileName = strDup(argv[argc-1]);
1267 return true;
1268}
1269
1270//
1271// -=- WinMain
1272//
1273
1274int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1275
1276 // - Process the command-line
1277
1278 int argc = __argc;
1279 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001280 if ((argc > 1) && (!processParams(argc, argv))) {
1281 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1282 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001283 }
george82e6883de2005-02-08 14:42:12 +00001284
1285 if (print_usage) {
1286 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001287 return 0;
george8267cbcd02005-01-16 15:39:56 +00001288 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001289
george82e6883de2005-02-08 14:42:12 +00001290 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001291 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001292 try {
george825e7af742005-03-10 14:26:00 +00001293 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001294 } catch (rdr::Exception e) {
1295 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1296 delete player;
1297 return 0;
1298 }
1299
1300 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001301 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001302 MSG msg;
1303 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001304 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1305 TranslateMessage(&msg);
1306 DispatchMessage(&msg);
1307 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001308 }
1309
george82e6883de2005-02-08 14:42:12 +00001310 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001311 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001312 if (player) delete player;
1313 } catch (rdr::Exception e) {
1314 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1315 }
1316
1317 return 0;
1318};