blob: d2bd1d6c857a312df44a57affa9b7c0dd4240c8d [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
george820981b342005-03-19 11:19:00 +000028#include <rfbplayer/PixelFormatList.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000029#include <rfbplayer/rfbplayer.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
george820981b342005-03-19 11:19:00 +000064#define UPF_REGISTRY_PATH "Software\\TightVnc\\RfbPlayer\\UserDefinedPF"
george825457d412005-02-19 06:43:09 +000065#define MAX_SPEED 10.00
66#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000067#define MAX_POS_TRACKBAR_RANGE 50
george825e7af742005-03-10 14:26:00 +000068#define CTRL_BAR_HEIGHT 28
george8268d25142005-02-13 09:33:22 +000069#define DEFAULT_PLAYER_WIDTH 640
70#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000071
george82d070c692005-01-19 16:44:04 +000072#define ID_TOOLBAR 500
73#define ID_PLAY 510
74#define ID_PAUSE 520
75#define ID_TIME_STATIC 530
76#define ID_SPEED_STATIC 540
77#define ID_SPEED_EDIT 550
78#define ID_POS_TRACKBAR 560
79#define ID_SPEED_UPDOWN 570
80
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000081//
82// -=- RfbPlayerClass
83
84//
85// Window class used as the basis for RfbPlayer instance
86//
87
88class RfbPlayerClass {
89public:
90 RfbPlayerClass();
91 ~RfbPlayerClass();
92 ATOM classAtom;
93 HINSTANCE instance;
94};
95
96LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
97 LRESULT result;
98
99 if (msg == WM_CREATE)
100 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
101 else if (msg == WM_DESTROY) {
102 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000103 SetWindowLong(hwnd, GWL_USERDATA, 0);
104 }
105 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
106 if (!_this) {
107 vlog.info("null _this in %x, message %u", hwnd, msg);
108 return DefWindowProc(hwnd, msg, wParam, lParam);
109 }
110
111 try {
112 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
113 } catch (rdr::Exception& e) {
114 vlog.error("untrapped: %s", e.str());
115 }
116
117 return result;
118};
119
120RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
121 WNDCLASS wndClass;
122 wndClass.style = 0;
123 wndClass.lpfnWndProc = RfbPlayerProc;
124 wndClass.cbClsExtra = 0;
125 wndClass.cbWndExtra = 0;
126 wndClass.hInstance = instance = GetModuleHandle(0);
127 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000128 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000129 if (!wndClass.hIcon)
130 printf("unable to load icon:%ld", GetLastError());
131 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
132 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000133 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000134 wndClass.lpszClassName = _T("RfbPlayerClass");
135 classAtom = RegisterClass(&wndClass);
136 if (!classAtom) {
137 throw rdr::SystemException("unable to register RfbPlayer window class",
138 GetLastError());
139 }
140}
141
142RfbPlayerClass::~RfbPlayerClass() {
143 if (classAtom) {
144 UnregisterClass((const TCHAR*)classAtom, instance);
145 }
146}
147
148RfbPlayerClass baseClass;
149
150//
151// -=- RfbFrameClass
152
153//
154// Window class used to displaying the rfb data
155//
156
157class RfbFrameClass {
158public:
159 RfbFrameClass();
160 ~RfbFrameClass();
161 ATOM classAtom;
162 HINSTANCE instance;
163};
164
165LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
166 LRESULT result;
167
168 if (msg == WM_CREATE)
169 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
170 else if (msg == WM_DESTROY)
171 SetWindowLong(hwnd, GWL_USERDATA, 0);
172 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
173 if (!_this) {
174 vlog.info("null _this in %x, message %u", hwnd, msg);
175 return DefWindowProc(hwnd, msg, wParam, lParam);
176 }
177
178 try {
179 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
180 } catch (rdr::Exception& e) {
181 vlog.error("untrapped: %s", e.str());
182 }
183
184 return result;
185}
186
187RfbFrameClass::RfbFrameClass() : classAtom(0) {
188 WNDCLASS wndClass;
189 wndClass.style = 0;
190 wndClass.lpfnWndProc = FrameProc;
191 wndClass.cbClsExtra = 0;
192 wndClass.cbWndExtra = 0;
193 wndClass.hInstance = instance = GetModuleHandle(0);
194 wndClass.hIcon = 0;
195 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
196 wndClass.hbrBackground = 0;
197 wndClass.lpszMenuName = 0;
198 wndClass.lpszClassName = _T("RfbPlayerClass1");
199 classAtom = RegisterClass(&wndClass);
200 if (!classAtom) {
201 throw rdr::SystemException("unable to register RfbPlayer window class",
202 GetLastError());
203 }
204}
205
206RfbFrameClass::~RfbFrameClass() {
207 if (classAtom) {
208 UnregisterClass((const TCHAR*)classAtom, instance);
209 }
210}
211
212RfbFrameClass frameClass;
213
214//
215// -=- RfbPlayer instance implementation
216//
217
george825e7af742005-03-10 14:26:00 +0000218RfbPlayer::RfbPlayer(char *_fileName, PlayerOptions *_options)
219: RfbProto(_fileName), fileName(_fileName), buffer(0), client_size(0, 0, 32, 32),
220 window_size(0, 0, 32, 32), cutText(0), seekMode(false), lastPos(0),
221 timeStatic(0), speedEdit(0), posTrackBar(0), speedUpDown(0),
george823104aec2005-02-21 13:20:56 +0000222 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george825e7af742005-03-10 14:26:00 +0000223 imageDataStartTime(0), rewindFlag(false), stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000224
george825e7af742005-03-10 14:26:00 +0000225 // Save the player options
226 memcpy(&options, _options, sizeof(options));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000227
george823c8fbbf2005-01-24 11:09:08 +0000228 // Reset the full session time
229 strcpy(fullSessionTime, "00m:00s");
230
george820981b342005-03-19 11:19:00 +0000231 // Load the user defined pixel formats from the registry
232 supportedPF.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
233
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000234 // Create the main window
235 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000236 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
237 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000238 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000239 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000240 if (!mainHwnd) {
241 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
242 }
243 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
244
245 // Create the backing buffer
246 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000247 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000248
george825e7af742005-03-10 14:26:00 +0000249 // If run with command-line parameters,
250 // open the session file with default settings, otherwise
251 // restore player settings from the registry
george8217e92cb2005-01-31 16:01:02 +0000252 if (fileName) {
253 openSessionFile(fileName);
george825e7af742005-03-10 14:26:00 +0000254 if (options.initTime > 0) setPos(options.initTime);
255 setSpeed(options.playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000256 } else {
george825e7af742005-03-10 14:26:00 +0000257 options.readFromRegistry();
george8263ebbcc2005-02-12 12:09:13 +0000258 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000259 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000260 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000261}
262
263RfbPlayer::~RfbPlayer() {
george825e7af742005-03-10 14:26:00 +0000264 options.writeToRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000265 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000266 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000267 delete rfbReader->join();
268 rfbReader = 0;
269 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000270 if (mainHwnd) {
271 setVisible(false);
272 DestroyWindow(mainHwnd);
273 mainHwnd = 0;
274 }
george825beb62a2005-02-09 13:04:32 +0000275 if (buffer) delete buffer;
276 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000277 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000278 vlog.debug("~RfbPlayer done");
279}
280
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000281LRESULT
282RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
283 switch (msg) {
284
285 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000286
287 case WM_CREATE:
288 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000289 // Create the frame window
290 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
291 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
292 hwnd, 0, frameClass.instance, this);
293
george82d070c692005-01-19 16:44:04 +0000294 createToolBar(hwnd);
295
george82006f2792005-02-05 07:40:47 +0000296 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000297
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000298 return 0;
299 }
300
george827214b822004-12-12 07:02:51 +0000301 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000302
303 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000304 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000305 case ID_OPENFILE:
306 {
307 char curDir[_MAX_DIR];
308 static char filename[_MAX_PATH];
309 OPENFILENAME ofn;
310 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
311 GetCurrentDirectory(sizeof(curDir), curDir);
312
313 ofn.lStructSize = sizeof(OPENFILENAME);
314 ofn.hwndOwner = getMainHandle();
315 ofn.lpstrFile = filename;
316 ofn.nMaxFile = sizeof(filename);
317 ofn.lpstrInitialDir = curDir;
318 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
319 "All files (*.*)\0*.*\0";
320 ofn.lpstrDefExt = "rfb";
321 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000322 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000323 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000324 }
george826e51fcc2005-02-06 13:30:49 +0000325 }
326 break;
george8271ca1772005-02-13 10:50:46 +0000327 case ID_CLOSEFILE:
328 closeSessionFile();
329 break;
george825c13c662005-01-27 14:48:23 +0000330 case ID_PLAY:
331 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000332 break;
333 case ID_PAUSE:
334 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000335 break;
336 case ID_STOP:
337 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000338 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000339 }
george825c13c662005-01-27 14:48:23 +0000340 break;
341 case ID_PLAYPAUSE:
342 if (isPaused()) {
343 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000344 } else {
345 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000346 }
george825c13c662005-01-27 14:48:23 +0000347 break;
george827549df42005-02-08 16:31:02 +0000348 case ID_GOTO:
349 {
350 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000351 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000352 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
353 setPos(gotoTime);
354 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000355 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000356 }
357 }
358 break;
george825c13c662005-01-27 14:48:23 +0000359 case ID_FULLSCREEN:
360 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
361 break;
george8231a36332005-02-06 17:27:34 +0000362 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000363 options.loopPlayback = !options.loopPlayback;
364 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000365 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
366 break;
george824ea27f62005-01-29 15:03:06 +0000367 case ID_RETURN:
368 // Update the speed if return pressed in speedEdit
369 if (speedEdit == GetFocus()) {
370 char speedStr[20], *stopStr;
371 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
372 double speed = strtod(speedStr, &stopStr);
373 if (speed > 0) {
374 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000375 } else {
376 speed = getSpeed();
377 }
378 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000379 }
380 break;
george825e7af742005-03-10 14:26:00 +0000381 case ID_OPTIONS:
382 {
383 OptionsDialog optionsDialog(&options);
george82d9957b72005-03-11 14:22:14 +0000384 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000385 }
386 break;
george8201aa6732005-02-06 17:13:03 +0000387 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000388 PostQuitMessage(0);
389 break;
george82ef5f7262005-02-08 15:09:26 +0000390 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000391 MessageBox(getMainHandle(),
392 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
393 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000394 }
395 break;
396
397 // Update frame's window size and add scrollbars if required
398
399 case WM_SIZE:
400 {
george82d070c692005-01-19 16:44:04 +0000401
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000402 Point old_offset = bufferToClient(Point(0, 0));
403
404 // Update the cached sizing information
405 RECT r;
406 GetClientRect(getMainHandle(), &r);
407 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
408 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
409
410 GetWindowRect(getFrameHandle(), &r);
411 window_size = Rect(r.left, r.top, r.right, r.bottom);
412 GetClientRect(getFrameHandle(), &r);
413 client_size = Rect(r.left, r.top, r.right, r.bottom);
414
415 // Determine whether scrollbars are required
416 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000417
418 // Resize the ToolBar
419 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000420
421 // Redraw if required
422 if (!old_offset.equals(bufferToClient(Point(0, 0))))
423 InvalidateRect(getFrameHandle(), 0, TRUE);
424 }
425 break;
george828a471482005-02-06 07:15:53 +0000426
427 // Process messages from posTrackBar
428
429 case WM_HSCROLL:
430 {
431 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
432 Pos *= sliderStepMs;
433
434 switch (LOWORD(wParam)) {
435 case TB_PAGEUP:
436 case TB_PAGEDOWN:
437 case TB_LINEUP:
438 case TB_LINEDOWN:
439 case TB_THUMBTRACK:
440 sliderDraging = true;
441 updatePos(Pos);
442 return 0;
george82bcc129b2005-03-15 17:11:40 +0000443 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000444 case TB_ENDTRACK:
445 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000446 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000447 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000448 sliderDraging = false;
449 return 0;
450 default:
451 break;
452 }
453 }
454 break;
george829e6e6cc2005-01-29 13:12:05 +0000455
456 case WM_NOTIFY:
457 switch (((NMHDR*)lParam)->code) {
458 case UDN_DELTAPOS:
459 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000460 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000461 char speedStr[20] = "\0";
462 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
463 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
464 double speed;
465
george824ea27f62005-01-29 15:03:06 +0000466 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000467 if (upDown->iDelta > 0) {
468 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
469 } else {
george824ea27f62005-01-29 15:03:06 +0000470 // It's need to round the UpDown position
471 if ((upDown->iPos * 0.5) != getSpeed()) {
472 upDown->iDelta = 0;
473 lResult = TRUE;
474 }
george829e6e6cc2005-01-29 13:12:05 +0000475 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
476 }
george829e6e6cc2005-01-29 13:12:05 +0000477 sprintf(speedStr, "%.2f", speed);
478 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000479 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000480 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000481 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000482 }
george824ea27f62005-01-29 15:03:06 +0000483 }
george829e6e6cc2005-01-29 13:12:05 +0000484 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000485
486 case WM_CLOSE:
487 vlog.debug("WM_CLOSE %x", getMainHandle());
488 PostQuitMessage(0);
489 break;
490 }
491
492 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
493}
494
495LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
496 switch (msg) {
497
498 case WM_PAINT:
499 {
george821e846ff2005-02-24 13:13:33 +0000500 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000501 seekMode = true;
502 return 0;
503 } else {
504 if (seekMode) {
505 seekMode = false;
506 InvalidateRect(getFrameHandle(), 0, true);
507 UpdateWindow(getFrameHandle());
508 return 0;
509 }
510 }
511
512 PAINTSTRUCT ps;
513 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
514 if (!paintDC)
515 throw SystemException("unable to BeginPaint", GetLastError());
516 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
517
518 if (!pr.is_empty()) {
519
520 if (buffer->bitmap) {
521
522 // Get device context
523 BitmapDC bitmapDC(paintDC, buffer->bitmap);
524
525 // Blit the border if required
526 Rect bufpos = bufferToClient(buffer->getRect());
527 if (!pr.enclosed_by(bufpos)) {
528 vlog.debug("draw border");
529 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
530 RECT r;
531 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
532 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
533 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
534 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
535 }
536
537 // Do the blit
538 Point buf_pos = clientToBuffer(pr.tl);
539 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
540 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
541 throw SystemException("unable to BitBlt to window", GetLastError());
542
543 } else {
544 // Blit a load of black
545 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
546 0, 0, 0, BLACKNESS))
547 throw SystemException("unable to BitBlt to blank window", GetLastError());
548 }
549 }
550 EndPaint(getFrameHandle(), &ps);
551 }
552 return 0;
george8203c01da2005-03-16 12:36:53 +0000553
george82aff63ab2005-03-16 13:48:59 +0000554 // Process play/pause by the left mouse button
555 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000556 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
557 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000558
559 case WM_VSCROLL:
560 case WM_HSCROLL:
561 {
562 Point delta;
563 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
564
565 switch (LOWORD(wParam)) {
566 case SB_PAGEUP: newpos -= 50; break;
567 case SB_PAGEDOWN: newpos += 50; break;
568 case SB_LINEUP: newpos -= 5; break;
569 case SB_LINEDOWN: newpos += 5; break;
570 case SB_THUMBTRACK:
571 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
572 default: vlog.info("received unknown scroll message");
573 };
574
575 if (msg == WM_HSCROLL)
576 setViewportOffset(Point(newpos, scrolloffset.y));
577 else
578 setViewportOffset(Point(scrolloffset.x, newpos));
579
580 SCROLLINFO si;
581 si.cbSize = sizeof(si);
582 si.fMask = SIF_POS;
583 si.nPos = newpos;
584 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
585 }
586 break;
587 }
588
589 return DefWindowProc(hwnd, msg, wParam, lParam);
590}
591
george82d070c692005-01-19 16:44:04 +0000592void RfbPlayer::createToolBar(HWND parentHwnd) {
593 RECT tRect;
594 InitCommonControls();
595
596 tb.create(ID_TOOLBAR, parentHwnd);
597 tb.addBitmap(4, IDB_TOOLBAR);
598
599 // Create the control buttons
600 tb.addButton(0, ID_PLAY);
601 tb.addButton(1, ID_PAUSE);
602 tb.addButton(2, ID_STOP);
603 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
604 tb.addButton(3, ID_FULLSCREEN);
605 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
606
607 // Create the static control for the time output
608 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
609 tb.getButtonRect(6, &tRect);
610 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
611 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
612 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
613 GetModuleHandle(0), 0);
614 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
615
616 // Create the trackbar control for the time position
617 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
618 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000619 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000620 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
621 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
622 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
623 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000624 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000625 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
626
627 // Create the label with "Speed:" caption
628 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
629 tb.getButtonRect(10, &tRect);
630 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
631 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
632 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
633
634 // Create the edit control and the spin for the speed managing
635 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
636 tb.getButtonRect(11, &tRect);
637 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
638 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
639 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
640 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
641 // It's need to send notify messages to toolbar parent window
642 SetParent(speedEdit, tb.getHandle());
643
644 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
645 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000646 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000647}
648
george82a21d2952005-02-12 11:30:03 +0000649void RfbPlayer::disableTBandMenuItems() {
650 // Disable the menu items
651 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
652 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
653 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
654 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
655 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
656 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
657 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
658 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
659 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
660
661 // Disable the toolbar buttons and child controls
662 tb.enableButton(ID_PLAY, false);
663 tb.enableButton(ID_PAUSE, false);
664 tb.enableButton(ID_STOP, false);
665 tb.enableButton(ID_FULLSCREEN, false);
666 EnableWindow(posTrackBar, false);
667 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000668 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000669}
670
george82f5043162005-02-12 11:37:18 +0000671void RfbPlayer::enableTBandMenuItems() {
672 // Enable the menu items
673 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
674 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
675 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
676 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
677 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
678 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
679 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
680 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
681 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
682
683 // Enable the toolbar buttons and child controls
684 tb.enableButton(ID_PLAY, true);
685 tb.enableButton(ID_PAUSE, true);
686 tb.enableButton(ID_STOP, true);
687 tb.enableButton(ID_FULLSCREEN, true);
688 EnableWindow(posTrackBar, true);
689 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000690 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000691}
692
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000693void RfbPlayer::setVisible(bool visible) {
694 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
695 if (visible) {
696 // When the window becomes visible, make it active
697 SetForegroundWindow(getMainHandle());
698 SetActiveWindow(getMainHandle());
699 }
700}
701
702void RfbPlayer::setTitle(const char *title) {
703 char _title[256];
704 strcpy(_title, AppName);
705 strcat(_title, " - ");
706 strcat(_title, title);
707 SetWindowText(getMainHandle(), _title);
708}
709
710void RfbPlayer::setFrameSize(int width, int height) {
711 // Calculate and set required size for main window
712 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000713 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000714 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
715 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
716 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000717 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
718 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
719 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000720 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
721
722 // Enable/disable scrollbars as appropriate
723 calculateScrollBars();
724}
725
726void RfbPlayer::calculateScrollBars() {
727 // Calculate the required size of window
728 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
729 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
730 DWORD old_style;
731 RECT r;
732 SetRect(&r, 0, 0, buffer->width(), buffer->height());
733 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
734 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
735
736 // Work out whether scroll bars are required
737 do {
738 old_style = style;
739
740 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
741 style |= WS_HSCROLL;
742 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
743 }
744 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
745 style |= WS_VSCROLL;
746 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
747 }
748 } while (style != old_style);
749
750 // Tell Windows to update the window style & cached settings
751 if (style != current_style) {
752 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
753 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
754 }
755
756 // Update the scroll settings
757 SCROLLINFO si;
758 if (style & WS_VSCROLL) {
759 si.cbSize = sizeof(si);
760 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
761 si.nMin = 0;
762 si.nMax = buffer->height();
763 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
764 maxscrolloffset.y = max(0, si.nMax-si.nPage);
765 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
766 si.nPos = scrolloffset.y;
767 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
768 }
769 if (style & WS_HSCROLL) {
770 si.cbSize = sizeof(si);
771 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
772 si.nMin = 0;
773 si.nMax = buffer->width();
774 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
775 maxscrolloffset.x = max(0, si.nMax-si.nPage);
776 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
777 si.nPos = scrolloffset.x;
778 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
779 }
george82a758a7a2005-03-15 16:34:57 +0000780
781 // Update the cached client size
782 GetClientRect(getFrameHandle(), &r);
783 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000784}
785
786bool RfbPlayer::setViewportOffset(const Point& tl) {
787/* ***
788 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
789 max(0, min(maxscrolloffset.y, tl.y)));
790 */
791 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
792 max(0, min(tl.y, buffer->height()-client_size.height())));
793 Point delta = np.translate(scrolloffset.negate());
794 if (!np.equals(scrolloffset)) {
795 scrolloffset = np;
796 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
797 UpdateWindow(getFrameHandle());
798 return true;
799 }
800 return false;
801}
802
803void RfbPlayer::close(const char* reason) {
804 setVisible(false);
805 if (reason) {
806 vlog.info("closing - %s", reason);
807 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
808 }
809 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
810}
811
812void RfbPlayer::blankBuffer() {
813 fillRect(buffer->getRect(), 0);
814}
815
816void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000817 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000818 blankBuffer();
819 newSession(fileName);
820 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000821 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000822 if (paused) is->pausePlayback();
823 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000824}
825
826void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000827 // Perform return if waitWhilePaused processed because
828 // rfbReader thread could receive the signal to close
829 if (waitWhilePaused()) return;
830
george8223e08562005-01-31 15:16:42 +0000831 static long update_time = GetTickCount();
832 try {
george828a471482005-02-06 07:15:53 +0000833 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
834 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000835 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000836 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000837 update_time = GetTickCount();
838 }
839 RfbProto::processMsg();
840 } catch (rdr::Exception e) {
841 if (strcmp(e.str(), "[End Of File]") == 0) {
842 rewind();
george825e7af742005-03-10 14:26:00 +0000843 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000844 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000845 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000846 return;
847 }
848 // It's a special exception to perform backward seeking.
849 // We only rewind the stream and seek the offset
850 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000851 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000852 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000853 rewind();
george820d2e19d2005-03-03 15:47:55 +0000854 if (!stopped) setPos(seekOffset);
855 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000856 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000857 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000858 return;
859 }
860 // It's a special exception which is used to terminate the playback
861 if (strcmp(e.str(), "[TERMINATE]") == 0) {
862 sessionTerminateThread *terminate = new sessionTerminateThread(this);
863 terminate->start();
george8223e08562005-01-31 15:16:42 +0000864 } else {
george820e980cc2005-03-10 18:18:34 +0000865 // Show the exception message and close the session playback
866 is->pausePlayback();
867 char message[256] = "\0";
868 strcat(message, e.str());
869 strcat(message, "\nMaybe you force wrong the pixel format for this session");
870 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
871 sessionTerminateThread *terminate = new sessionTerminateThread(this);
872 terminate->start();
george8223e08562005-01-31 15:16:42 +0000873 return;
874 }
875 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000876}
877
878void RfbPlayer::serverInit() {
879 RfbProto::serverInit();
880
george82b95503e2005-02-21 17:02:34 +0000881 // Save the image data start time
882 imageDataStartTime = is->getTimeOffset();
883
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000884 // Resize the backing buffer
885 buffer->setSize(cp.width, cp.height);
886
887 // Check on the true colour mode
888 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000889 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000890
891 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000892 static long pixelFormat = PF_AUTO;
893 if (options.askPixelFormat) {
894 ChoosePixelFormatDialog choosePixelFormatDialog(pixelFormat);
george82d9957b72005-03-11 14:22:14 +0000895 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george825e7af742005-03-10 14:26:00 +0000896 pixelFormat = choosePixelFormatDialog.getPF();
george822c7634b2005-03-10 18:03:27 +0000897 } else {
898 is->pausePlayback();
899 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000900 }
901 } else {
george820981b342005-03-19 11:19:00 +0000902 pixelFormat = options.pixelFormatIndex;
george825e7af742005-03-10 14:26:00 +0000903 }
george825caee412005-03-09 09:52:10 +0000904 switch (pixelFormat) {
905 case PF_AUTO:
george82193d8e42005-02-20 16:47:01 +0000906 break;
george825caee412005-03-09 09:52:10 +0000907 case PF_D8_RGB332:
george82193d8e42005-02-20 16:47:01 +0000908 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
909 break;
george825caee412005-03-09 09:52:10 +0000910 case PF_D16_RGB655:
george82193d8e42005-02-20 16:47:01 +0000911 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
912 break;
george825caee412005-03-09 09:52:10 +0000913 case PF_D24_RGB888:
george82193d8e42005-02-20 16:47:01 +0000914 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
915 break;
916 default:
917 throw rdr::Exception("This color depth is not supported!");
918 }
919 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000920
921 // If the window is not maximised then resize it
922 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
923 setFrameSize(cp.width, cp.height);
924
925 // Set the window title and show it
926 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000927
george82d4d69e62005-02-05 09:23:18 +0000928 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000929 sessionTimeMs = calculateSessionTime(fileName);
930 sprintf(fullSessionTime, "%.2um:%.2us",
931 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000932 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000933 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
934 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000935 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000936
george825e7af742005-03-10 14:26:00 +0000937 setPaused(!options.autoPlay);
938 // Restore the parameters from registry,
939 // which was replaced by command-line parameters.
940 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000941}
942
943void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
944 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
945 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
946/* int i;
947 for (i=0;i<count;i++) {
948 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
949 }
950 // *** change to 0, 256?
951 refreshWindowPalette(first, count);
952 palette_changed = true;
953 InvalidateRect(getFrameHandle(), 0, FALSE);*/
954}
955
956void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000957 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000958 MessageBeep(-1);
959}
960
961void RfbPlayer::serverCutText(const char* str, int len) {
962 if (cutText != NULL)
963 delete [] cutText;
964 cutText = new char[len + 1];
965 memcpy(cutText, str, len);
966 cutText[len] = '\0';
967}
968
969void RfbPlayer::frameBufferUpdateEnd() {
970};
971
972void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
973}
974
975void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
976}
977
978
979void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
980 buffer->fillRect(r, pix);
981 invalidateBufferRect(r);
982}
983
984void RfbPlayer::imageRect(const Rect& r, void* pixels) {
985 buffer->imageRect(r, pixels);
986 invalidateBufferRect(r);
987}
988
989void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
990 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
991 invalidateBufferRect(r);
992}
993
994bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
995 Rect rect = bufferToClient(crect);
996 if (rect.intersect(client_size).is_empty()) return false;
997 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
998 InvalidateRect(getFrameHandle(), &invalid, FALSE);
999 return true;
1000}
1001
george820d2e19d2005-03-03 15:47:55 +00001002bool RfbPlayer::waitWhilePaused() {
1003 bool result = false;
1004 while(isPaused() && !isSeeking()) {
1005 Sleep(20);
1006 result = true;
1007 }
1008 return result;
1009}
1010
george8257f13522005-02-05 08:48:22 +00001011long RfbPlayer::calculateSessionTime(char *filename) {
1012 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001013 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001014 try {
1015 while (TRUE) {
1016 sessionFile.skip(1024);
1017 }
1018 } catch (rdr::Exception e) {
1019 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001020 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001021 } else {
1022 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1023 return 0;
1024 }
1025 }
1026 return 0;
1027}
1028
george826b87aff2005-02-13 10:48:21 +00001029void RfbPlayer::closeSessionFile() {
1030 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001031 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001032 RECT r;
1033
1034 // Uncheck all toolbar buttons
1035 if (tb.getHandle()) {
1036 tb.checkButton(ID_PLAY, false);
1037 tb.checkButton(ID_PAUSE, false);
1038 tb.checkButton(ID_STOP, false);
1039 }
1040
1041 // Stop playback and update the player state
1042 disableTBandMenuItems();
1043 if (rfbReader) {
1044 delete rfbReader->join();
1045 rfbReader = 0;
1046 delete [] fileName;
1047 fileName = 0;
1048 }
1049 blankBuffer();
1050 setTitle("None");
george827009c892005-02-19 12:49:42 +00001051 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001052 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001053 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001054 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1055 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001056 SetWindowText(speedEdit, speedStr);
1057 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1058
1059 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001060 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1061 dwStyle &= ~WS_MAXIMIZE;
1062 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1063 }
george822ff7a612005-02-19 17:05:24 +00001064 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1065 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1066 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001067 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001068 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001069 buffer->setSize(32, 32);
1070 calculateScrollBars();
1071
1072 // Update the cached sizing information and repaint the frame window
1073 GetWindowRect(getFrameHandle(), &r);
1074 window_size = Rect(r.left, r.top, r.right, r.bottom);
1075 GetClientRect(getFrameHandle(), &r);
1076 client_size = Rect(r.left, r.top, r.right, r.bottom);
1077 InvalidateRect(getFrameHandle(), 0, TRUE);
1078 UpdateWindow(getFrameHandle());
1079}
1080
george8217e92cb2005-01-31 16:01:02 +00001081void RfbPlayer::openSessionFile(char *_fileName) {
1082 fileName = strDup(_fileName);
1083
1084 // Close the previous reading thread
1085 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001086 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001087 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001088 }
1089 blankBuffer();
1090 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001091 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001092 rfbReader = new rfbSessionReader(this);
1093 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001094 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001095 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001096}
1097
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001098void RfbPlayer::setPaused(bool paused) {
1099 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001100 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001101 tb.checkButton(ID_PAUSE, true);
1102 tb.checkButton(ID_PLAY, false);
1103 tb.checkButton(ID_STOP, false);
1104 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1105 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001106 } else {
george825beb62a2005-02-09 13:04:32 +00001107 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001108 tb.checkButton(ID_PLAY, true);
1109 tb.checkButton(ID_STOP, false);
1110 tb.checkButton(ID_PAUSE, false);
1111 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1112 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001113 }
1114}
1115
george82006f2792005-02-05 07:40:47 +00001116void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001117 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001118 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001119 if (is) {
1120 is->pausePlayback();
1121 is->interruptFrameDelay();
1122 }
george82006f2792005-02-05 07:40:47 +00001123 tb.checkButton(ID_STOP, true);
1124 tb.checkButton(ID_PLAY, false);
1125 tb.checkButton(ID_PAUSE, false);
1126 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1127 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001128 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001129}
1130
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001131void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001132 if (speed > 0) {
1133 char speedStr[20] = "\0";
1134 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001135 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001136 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001137 SendMessage(speedUpDown, UDM_SETPOS,
1138 0, MAKELONG((short)(newSpeed / 0.5), 0));
1139 sprintf(speedStr, "%.2f", newSpeed);
1140 SetWindowText(speedEdit, speedStr);
1141 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001142}
1143
1144double RfbPlayer::getSpeed() {
1145 return is->getSpeed();
1146}
1147
1148void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001149 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001150}
1151
1152long RfbPlayer::getSeekOffset() {
1153 return is->getSeekOffset();
1154}
1155
1156bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001157 if (is) return is->isSeeking();
1158 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001159}
1160
1161bool RfbPlayer::isSeekMode() {
1162 return seekMode;
1163}
1164
1165bool RfbPlayer::isPaused() {
1166 return is->isPaused();
1167}
1168
1169long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001170 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001171}
1172
george828a471482005-02-06 07:15:53 +00001173void RfbPlayer::updatePos(long newPos) {
1174 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001175 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001176 long time = newPos / 1000;
1177 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001178 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001179
1180 // Update the position of slider
1181 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001182 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1183 sliderStepMs / double(newPos);
1184 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1185 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1186 }
george828a471482005-02-06 07:15:53 +00001187 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001188}
1189
1190void RfbPlayer::skipHandshaking() {
1191 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1192 is->skip(skipBytes);
1193 state_ = RFBSTATE_NORMAL;
1194}
1195
1196void programInfo() {
1197 win32::FileVersionInfo inf;
1198 _tprintf(_T("%s - %s, Version %s\n"),
1199 inf.getVerString(_T("ProductName")),
1200 inf.getVerString(_T("FileDescription")),
1201 inf.getVerString(_T("FileVersion")));
1202 printf("%s\n", buildTime);
1203 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1204}
1205
1206void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001207 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001208}
1209
george825beb62a2005-02-09 13:04:32 +00001210char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001211
1212// playerOptions is the player options with default parameters values,
1213// it is used only for run the player with command-line parameters
1214PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001215bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001216
1217bool processParams(int argc, char* argv[]) {
1218 for (int i = 1; i < argc; i++) {
1219 if ((strcasecmp(argv[i], "-help") == 0) ||
1220 (strcasecmp(argv[i], "--help") == 0) ||
1221 (strcasecmp(argv[i], "/help") == 0) ||
1222 (strcasecmp(argv[i], "-h") == 0) ||
1223 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001224 (strcasecmp(argv[i], "/?") == 0) ||
1225 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001226 print_usage = true;
1227 return true;
1228 }
1229
george825caee412005-03-09 09:52:10 +00001230 if ((strcasecmp(argv[i], "-pf") == 0) ||
1231 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001232 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001233 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001234 return false;
1235 }
george820981b342005-03-19 11:19:00 +00001236 playerOptions.pixelFormatIndex = pf;
george82193d8e42005-02-20 16:47:01 +00001237 continue;
1238 }
1239
1240
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001241 if ((strcasecmp(argv[i], "-speed") == 0) ||
1242 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001243 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001244 if (playbackSpeed <= 0) {
1245 return false;
1246 }
george825e7af742005-03-10 14:26:00 +00001247 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001248 continue;
1249 }
1250
1251 if ((strcasecmp(argv[i], "-pos") == 0) ||
1252 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001253 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001254 if (initTime <= 0)
1255 return false;
george825e7af742005-03-10 14:26:00 +00001256 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001257 continue;
1258 }
1259
1260 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1261 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001262 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001263 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001264 }
1265
1266 if (i != argc - 1)
1267 return false;
1268 }
1269
1270 fileName = strDup(argv[argc-1]);
1271 return true;
1272}
1273
1274//
1275// -=- WinMain
1276//
1277
1278int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1279
1280 // - Process the command-line
1281
1282 int argc = __argc;
1283 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001284 if ((argc > 1) && (!processParams(argc, argv))) {
1285 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1286 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001287 }
george82e6883de2005-02-08 14:42:12 +00001288
1289 if (print_usage) {
1290 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001291 return 0;
george8267cbcd02005-01-16 15:39:56 +00001292 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001293
george82e6883de2005-02-08 14:42:12 +00001294 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001295 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001296 try {
george825e7af742005-03-10 14:26:00 +00001297 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001298 } catch (rdr::Exception e) {
1299 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1300 delete player;
1301 return 0;
1302 }
1303
1304 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001305 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001306 MSG msg;
1307 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001308 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1309 TranslateMessage(&msg);
1310 DispatchMessage(&msg);
1311 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001312 }
1313
george82e6883de2005-02-08 14:42:12 +00001314 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001315 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001316 if (player) delete player;
1317 } catch (rdr::Exception e) {
1318 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1319 }
1320
1321 return 0;
1322};