blob: 614f5c128567f9d9ba8b2c7f56bfe89aeb1241f5 [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:
george82bc999f42005-03-19 11:48:19 +0000342 if (rfbReader) {
343 if (isPaused()) {
344 setPaused(false);
345 } else {
346 setPaused(true);
347 }
george825c13c662005-01-27 14:48:23 +0000348 }
george825c13c662005-01-27 14:48:23 +0000349 break;
george827549df42005-02-08 16:31:02 +0000350 case ID_GOTO:
351 {
352 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000353 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000354 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
355 setPos(gotoTime);
356 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000357 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000358 }
359 }
360 break;
george825c13c662005-01-27 14:48:23 +0000361 case ID_FULLSCREEN:
362 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
363 break;
george8231a36332005-02-06 17:27:34 +0000364 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000365 options.loopPlayback = !options.loopPlayback;
366 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000367 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
368 break;
george824ea27f62005-01-29 15:03:06 +0000369 case ID_RETURN:
370 // Update the speed if return pressed in speedEdit
371 if (speedEdit == GetFocus()) {
372 char speedStr[20], *stopStr;
373 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
374 double speed = strtod(speedStr, &stopStr);
375 if (speed > 0) {
376 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000377 } else {
378 speed = getSpeed();
379 }
380 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000381 }
382 break;
george825e7af742005-03-10 14:26:00 +0000383 case ID_OPTIONS:
384 {
george825a6df072005-03-20 09:56:17 +0000385 OptionsDialog optionsDialog(&options, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000386 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000387 }
388 break;
george8201aa6732005-02-06 17:13:03 +0000389 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000390 PostQuitMessage(0);
391 break;
george82ef5f7262005-02-08 15:09:26 +0000392 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000393 MessageBox(getMainHandle(),
394 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
395 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000396 }
397 break;
398
399 // Update frame's window size and add scrollbars if required
400
401 case WM_SIZE:
402 {
george82d070c692005-01-19 16:44:04 +0000403
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000404 Point old_offset = bufferToClient(Point(0, 0));
405
406 // Update the cached sizing information
407 RECT r;
408 GetClientRect(getMainHandle(), &r);
409 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
410 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
411
412 GetWindowRect(getFrameHandle(), &r);
413 window_size = Rect(r.left, r.top, r.right, r.bottom);
414 GetClientRect(getFrameHandle(), &r);
415 client_size = Rect(r.left, r.top, r.right, r.bottom);
416
417 // Determine whether scrollbars are required
418 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000419
420 // Resize the ToolBar
421 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000422
423 // Redraw if required
424 if (!old_offset.equals(bufferToClient(Point(0, 0))))
425 InvalidateRect(getFrameHandle(), 0, TRUE);
426 }
427 break;
george828a471482005-02-06 07:15:53 +0000428
429 // Process messages from posTrackBar
430
431 case WM_HSCROLL:
432 {
433 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
434 Pos *= sliderStepMs;
435
436 switch (LOWORD(wParam)) {
437 case TB_PAGEUP:
438 case TB_PAGEDOWN:
439 case TB_LINEUP:
440 case TB_LINEDOWN:
441 case TB_THUMBTRACK:
442 sliderDraging = true;
443 updatePos(Pos);
444 return 0;
george82bcc129b2005-03-15 17:11:40 +0000445 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000446 case TB_ENDTRACK:
447 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000448 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000449 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000450 sliderDraging = false;
451 return 0;
452 default:
453 break;
454 }
455 }
456 break;
george829e6e6cc2005-01-29 13:12:05 +0000457
458 case WM_NOTIFY:
459 switch (((NMHDR*)lParam)->code) {
460 case UDN_DELTAPOS:
461 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000462 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000463 char speedStr[20] = "\0";
464 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
465 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
466 double speed;
467
george824ea27f62005-01-29 15:03:06 +0000468 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000469 if (upDown->iDelta > 0) {
470 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
471 } else {
george824ea27f62005-01-29 15:03:06 +0000472 // It's need to round the UpDown position
473 if ((upDown->iPos * 0.5) != getSpeed()) {
474 upDown->iDelta = 0;
475 lResult = TRUE;
476 }
george829e6e6cc2005-01-29 13:12:05 +0000477 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
478 }
george829e6e6cc2005-01-29 13:12:05 +0000479 sprintf(speedStr, "%.2f", speed);
480 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000481 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000482 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000483 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000484 }
george824ea27f62005-01-29 15:03:06 +0000485 }
george829e6e6cc2005-01-29 13:12:05 +0000486 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000487
488 case WM_CLOSE:
489 vlog.debug("WM_CLOSE %x", getMainHandle());
490 PostQuitMessage(0);
491 break;
492 }
493
494 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
495}
496
497LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
498 switch (msg) {
499
500 case WM_PAINT:
501 {
george821e846ff2005-02-24 13:13:33 +0000502 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000503 seekMode = true;
504 return 0;
505 } else {
506 if (seekMode) {
507 seekMode = false;
508 InvalidateRect(getFrameHandle(), 0, true);
509 UpdateWindow(getFrameHandle());
510 return 0;
511 }
512 }
513
514 PAINTSTRUCT ps;
515 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
516 if (!paintDC)
517 throw SystemException("unable to BeginPaint", GetLastError());
518 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
519
520 if (!pr.is_empty()) {
521
522 if (buffer->bitmap) {
523
524 // Get device context
525 BitmapDC bitmapDC(paintDC, buffer->bitmap);
526
527 // Blit the border if required
528 Rect bufpos = bufferToClient(buffer->getRect());
529 if (!pr.enclosed_by(bufpos)) {
530 vlog.debug("draw border");
531 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
532 RECT r;
533 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
534 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
535 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
536 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
537 }
538
539 // Do the blit
540 Point buf_pos = clientToBuffer(pr.tl);
541 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
542 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
543 throw SystemException("unable to BitBlt to window", GetLastError());
544
545 } else {
546 // Blit a load of black
547 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
548 0, 0, 0, BLACKNESS))
549 throw SystemException("unable to BitBlt to blank window", GetLastError());
550 }
551 }
552 EndPaint(getFrameHandle(), &ps);
553 }
554 return 0;
george8203c01da2005-03-16 12:36:53 +0000555
george82aff63ab2005-03-16 13:48:59 +0000556 // Process play/pause by the left mouse button
557 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000558 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
559 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000560
561 case WM_VSCROLL:
562 case WM_HSCROLL:
563 {
564 Point delta;
565 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
566
567 switch (LOWORD(wParam)) {
568 case SB_PAGEUP: newpos -= 50; break;
569 case SB_PAGEDOWN: newpos += 50; break;
570 case SB_LINEUP: newpos -= 5; break;
571 case SB_LINEDOWN: newpos += 5; break;
572 case SB_THUMBTRACK:
573 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
574 default: vlog.info("received unknown scroll message");
575 };
576
577 if (msg == WM_HSCROLL)
578 setViewportOffset(Point(newpos, scrolloffset.y));
579 else
580 setViewportOffset(Point(scrolloffset.x, newpos));
581
582 SCROLLINFO si;
583 si.cbSize = sizeof(si);
584 si.fMask = SIF_POS;
585 si.nPos = newpos;
586 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
587 }
588 break;
589 }
590
591 return DefWindowProc(hwnd, msg, wParam, lParam);
592}
593
george82d070c692005-01-19 16:44:04 +0000594void RfbPlayer::createToolBar(HWND parentHwnd) {
595 RECT tRect;
596 InitCommonControls();
597
598 tb.create(ID_TOOLBAR, parentHwnd);
599 tb.addBitmap(4, IDB_TOOLBAR);
600
601 // Create the control buttons
602 tb.addButton(0, ID_PLAY);
603 tb.addButton(1, ID_PAUSE);
604 tb.addButton(2, ID_STOP);
605 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
606 tb.addButton(3, ID_FULLSCREEN);
607 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
608
609 // Create the static control for the time output
610 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
611 tb.getButtonRect(6, &tRect);
612 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
613 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
614 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
615 GetModuleHandle(0), 0);
616 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
617
618 // Create the trackbar control for the time position
619 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
620 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000621 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000622 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
623 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
624 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
625 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000626 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000627 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
628
629 // Create the label with "Speed:" caption
630 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
631 tb.getButtonRect(10, &tRect);
632 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
633 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
634 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
635
636 // Create the edit control and the spin for the speed managing
637 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
638 tb.getButtonRect(11, &tRect);
639 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
640 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
641 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
642 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
643 // It's need to send notify messages to toolbar parent window
644 SetParent(speedEdit, tb.getHandle());
645
646 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
647 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000648 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000649}
650
george82a21d2952005-02-12 11:30:03 +0000651void RfbPlayer::disableTBandMenuItems() {
652 // Disable the menu items
653 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
654 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
655 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
656 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
657 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
658 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
659 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
660 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
661 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
662
663 // Disable the toolbar buttons and child controls
664 tb.enableButton(ID_PLAY, false);
665 tb.enableButton(ID_PAUSE, false);
666 tb.enableButton(ID_STOP, false);
667 tb.enableButton(ID_FULLSCREEN, false);
668 EnableWindow(posTrackBar, false);
669 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000670 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000671}
672
george82f5043162005-02-12 11:37:18 +0000673void RfbPlayer::enableTBandMenuItems() {
674 // Enable the menu items
675 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
676 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
677 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
678 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
679 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
680 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
681 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
682 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
683 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
684
685 // Enable the toolbar buttons and child controls
686 tb.enableButton(ID_PLAY, true);
687 tb.enableButton(ID_PAUSE, true);
688 tb.enableButton(ID_STOP, true);
689 tb.enableButton(ID_FULLSCREEN, true);
690 EnableWindow(posTrackBar, true);
691 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000692 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000693}
694
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000695void RfbPlayer::setVisible(bool visible) {
696 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
697 if (visible) {
698 // When the window becomes visible, make it active
699 SetForegroundWindow(getMainHandle());
700 SetActiveWindow(getMainHandle());
701 }
702}
703
704void RfbPlayer::setTitle(const char *title) {
705 char _title[256];
706 strcpy(_title, AppName);
707 strcat(_title, " - ");
708 strcat(_title, title);
709 SetWindowText(getMainHandle(), _title);
710}
711
712void RfbPlayer::setFrameSize(int width, int height) {
713 // Calculate and set required size for main window
714 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000715 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000716 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
717 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
718 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000719 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
720 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
721 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000722 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
723
724 // Enable/disable scrollbars as appropriate
725 calculateScrollBars();
726}
727
728void RfbPlayer::calculateScrollBars() {
729 // Calculate the required size of window
730 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
731 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
732 DWORD old_style;
733 RECT r;
734 SetRect(&r, 0, 0, buffer->width(), buffer->height());
735 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
736 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
737
738 // Work out whether scroll bars are required
739 do {
740 old_style = style;
741
742 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
743 style |= WS_HSCROLL;
744 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
745 }
746 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
747 style |= WS_VSCROLL;
748 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
749 }
750 } while (style != old_style);
751
752 // Tell Windows to update the window style & cached settings
753 if (style != current_style) {
754 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
755 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
756 }
757
758 // Update the scroll settings
759 SCROLLINFO si;
760 if (style & WS_VSCROLL) {
761 si.cbSize = sizeof(si);
762 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
763 si.nMin = 0;
764 si.nMax = buffer->height();
765 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
766 maxscrolloffset.y = max(0, si.nMax-si.nPage);
767 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
768 si.nPos = scrolloffset.y;
769 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
770 }
771 if (style & WS_HSCROLL) {
772 si.cbSize = sizeof(si);
773 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
774 si.nMin = 0;
775 si.nMax = buffer->width();
776 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
777 maxscrolloffset.x = max(0, si.nMax-si.nPage);
778 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
779 si.nPos = scrolloffset.x;
780 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
781 }
george82a758a7a2005-03-15 16:34:57 +0000782
783 // Update the cached client size
784 GetClientRect(getFrameHandle(), &r);
785 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000786}
787
788bool RfbPlayer::setViewportOffset(const Point& tl) {
789/* ***
790 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
791 max(0, min(maxscrolloffset.y, tl.y)));
792 */
793 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
794 max(0, min(tl.y, buffer->height()-client_size.height())));
795 Point delta = np.translate(scrolloffset.negate());
796 if (!np.equals(scrolloffset)) {
797 scrolloffset = np;
798 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
799 UpdateWindow(getFrameHandle());
800 return true;
801 }
802 return false;
803}
804
805void RfbPlayer::close(const char* reason) {
806 setVisible(false);
807 if (reason) {
808 vlog.info("closing - %s", reason);
809 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
810 }
811 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
812}
813
814void RfbPlayer::blankBuffer() {
815 fillRect(buffer->getRect(), 0);
816}
817
818void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000819 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000820 blankBuffer();
821 newSession(fileName);
822 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000823 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000824 if (paused) is->pausePlayback();
825 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000826}
827
828void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000829 // Perform return if waitWhilePaused processed because
830 // rfbReader thread could receive the signal to close
831 if (waitWhilePaused()) return;
832
george8223e08562005-01-31 15:16:42 +0000833 static long update_time = GetTickCount();
834 try {
george828a471482005-02-06 07:15:53 +0000835 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
836 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000837 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000838 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000839 update_time = GetTickCount();
840 }
841 RfbProto::processMsg();
842 } catch (rdr::Exception e) {
843 if (strcmp(e.str(), "[End Of File]") == 0) {
844 rewind();
george825e7af742005-03-10 14:26:00 +0000845 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000846 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000847 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000848 return;
849 }
850 // It's a special exception to perform backward seeking.
851 // We only rewind the stream and seek the offset
852 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000853 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000854 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000855 rewind();
george820d2e19d2005-03-03 15:47:55 +0000856 if (!stopped) setPos(seekOffset);
857 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000858 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000859 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000860 return;
861 }
862 // It's a special exception which is used to terminate the playback
863 if (strcmp(e.str(), "[TERMINATE]") == 0) {
864 sessionTerminateThread *terminate = new sessionTerminateThread(this);
865 terminate->start();
george8223e08562005-01-31 15:16:42 +0000866 } else {
george820e980cc2005-03-10 18:18:34 +0000867 // Show the exception message and close the session playback
868 is->pausePlayback();
869 char message[256] = "\0";
870 strcat(message, e.str());
871 strcat(message, "\nMaybe you force wrong the pixel format for this session");
872 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
873 sessionTerminateThread *terminate = new sessionTerminateThread(this);
874 terminate->start();
george8223e08562005-01-31 15:16:42 +0000875 return;
876 }
877 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000878}
879
880void RfbPlayer::serverInit() {
881 RfbProto::serverInit();
882
george82b95503e2005-02-21 17:02:34 +0000883 // Save the image data start time
884 imageDataStartTime = is->getTimeOffset();
885
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000886 // Resize the backing buffer
887 buffer->setSize(cp.width, cp.height);
888
889 // Check on the true colour mode
890 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000891 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000892
893 // Set the session pixel format
george825a6df072005-03-20 09:56:17 +0000894 static long pixelFormatIndex = -1;
george825e7af742005-03-10 14:26:00 +0000895 if (options.askPixelFormat) {
george825a6df072005-03-20 09:56:17 +0000896 ChoosePixelFormatDialog choosePixelFormatDialog(pixelFormatIndex, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000897 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george825a6df072005-03-20 09:56:17 +0000898 pixelFormatIndex = choosePixelFormatDialog.getPF();
899 if (pixelFormatIndex < 0) {
900 options.autoDetectPF = true;
901 options.setPF((PixelFormat *)&cp.pf());
902 } else {
903 options.autoDetectPF = false;
904 options.setPF(&supportedPF[pixelFormatIndex].PF);
905 }
george822c7634b2005-03-10 18:03:27 +0000906 } else {
907 is->pausePlayback();
908 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000909 }
910 } else {
george825a6df072005-03-20 09:56:17 +0000911 if (options.autoDetectPF) {
912 options.setPF((PixelFormat *)&cp.pf());
913 } else {
914 options.setPF(&supportedPF[options.pixelFormatIndex].PF);
915 }
george825e7af742005-03-10 14:26:00 +0000916 }
george825a6df072005-03-20 09:56:17 +0000917 cp.setPF(options.pixelFormat);
918 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000919
920 // If the window is not maximised then resize it
921 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
922 setFrameSize(cp.width, cp.height);
923
924 // Set the window title and show it
925 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000926
george82d4d69e62005-02-05 09:23:18 +0000927 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000928 sessionTimeMs = calculateSessionTime(fileName);
929 sprintf(fullSessionTime, "%.2um:%.2us",
930 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000931 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000932 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
933 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000934 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000935
george825e7af742005-03-10 14:26:00 +0000936 setPaused(!options.autoPlay);
937 // Restore the parameters from registry,
938 // which was replaced by command-line parameters.
939 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000940}
941
942void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
943 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
944 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
945/* int i;
946 for (i=0;i<count;i++) {
947 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
948 }
949 // *** change to 0, 256?
950 refreshWindowPalette(first, count);
951 palette_changed = true;
952 InvalidateRect(getFrameHandle(), 0, FALSE);*/
953}
954
955void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000956 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000957 MessageBeep(-1);
958}
959
960void RfbPlayer::serverCutText(const char* str, int len) {
961 if (cutText != NULL)
962 delete [] cutText;
963 cutText = new char[len + 1];
964 memcpy(cutText, str, len);
965 cutText[len] = '\0';
966}
967
968void RfbPlayer::frameBufferUpdateEnd() {
969};
970
971void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
972}
973
974void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
975}
976
977
978void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
979 buffer->fillRect(r, pix);
980 invalidateBufferRect(r);
981}
982
983void RfbPlayer::imageRect(const Rect& r, void* pixels) {
984 buffer->imageRect(r, pixels);
985 invalidateBufferRect(r);
986}
987
988void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
989 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
990 invalidateBufferRect(r);
991}
992
993bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
994 Rect rect = bufferToClient(crect);
995 if (rect.intersect(client_size).is_empty()) return false;
996 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
997 InvalidateRect(getFrameHandle(), &invalid, FALSE);
998 return true;
999}
1000
george820d2e19d2005-03-03 15:47:55 +00001001bool RfbPlayer::waitWhilePaused() {
1002 bool result = false;
1003 while(isPaused() && !isSeeking()) {
1004 Sleep(20);
1005 result = true;
1006 }
1007 return result;
1008}
1009
george8257f13522005-02-05 08:48:22 +00001010long RfbPlayer::calculateSessionTime(char *filename) {
1011 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001012 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001013 try {
1014 while (TRUE) {
1015 sessionFile.skip(1024);
1016 }
1017 } catch (rdr::Exception e) {
1018 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001019 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001020 } else {
1021 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1022 return 0;
1023 }
1024 }
1025 return 0;
1026}
1027
george826b87aff2005-02-13 10:48:21 +00001028void RfbPlayer::closeSessionFile() {
1029 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001030 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001031 RECT r;
1032
1033 // Uncheck all toolbar buttons
1034 if (tb.getHandle()) {
1035 tb.checkButton(ID_PLAY, false);
1036 tb.checkButton(ID_PAUSE, false);
1037 tb.checkButton(ID_STOP, false);
1038 }
1039
1040 // Stop playback and update the player state
1041 disableTBandMenuItems();
1042 if (rfbReader) {
1043 delete rfbReader->join();
1044 rfbReader = 0;
1045 delete [] fileName;
1046 fileName = 0;
1047 }
1048 blankBuffer();
1049 setTitle("None");
george827009c892005-02-19 12:49:42 +00001050 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001051 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001052 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001053 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1054 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001055 SetWindowText(speedEdit, speedStr);
1056 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1057
1058 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001059 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1060 dwStyle &= ~WS_MAXIMIZE;
1061 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1062 }
george822ff7a612005-02-19 17:05:24 +00001063 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1064 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1065 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001066 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001067 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001068 buffer->setSize(32, 32);
1069 calculateScrollBars();
1070
1071 // Update the cached sizing information and repaint the frame window
1072 GetWindowRect(getFrameHandle(), &r);
1073 window_size = Rect(r.left, r.top, r.right, r.bottom);
1074 GetClientRect(getFrameHandle(), &r);
1075 client_size = Rect(r.left, r.top, r.right, r.bottom);
1076 InvalidateRect(getFrameHandle(), 0, TRUE);
1077 UpdateWindow(getFrameHandle());
1078}
1079
george8217e92cb2005-01-31 16:01:02 +00001080void RfbPlayer::openSessionFile(char *_fileName) {
1081 fileName = strDup(_fileName);
1082
1083 // Close the previous reading thread
1084 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001085 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001086 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001087 }
1088 blankBuffer();
1089 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001090 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001091 rfbReader = new rfbSessionReader(this);
1092 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001093 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001094 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001095}
1096
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001097void RfbPlayer::setPaused(bool paused) {
1098 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001099 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001100 tb.checkButton(ID_PAUSE, true);
1101 tb.checkButton(ID_PLAY, false);
1102 tb.checkButton(ID_STOP, false);
1103 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1104 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001105 } else {
george825beb62a2005-02-09 13:04:32 +00001106 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001107 tb.checkButton(ID_PLAY, true);
1108 tb.checkButton(ID_STOP, false);
1109 tb.checkButton(ID_PAUSE, false);
1110 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1111 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001112 }
1113}
1114
george82006f2792005-02-05 07:40:47 +00001115void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001116 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001117 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001118 if (is) {
1119 is->pausePlayback();
1120 is->interruptFrameDelay();
1121 }
george82006f2792005-02-05 07:40:47 +00001122 tb.checkButton(ID_STOP, true);
1123 tb.checkButton(ID_PLAY, false);
1124 tb.checkButton(ID_PAUSE, false);
1125 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1126 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001127 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001128}
1129
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001130void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001131 if (speed > 0) {
1132 char speedStr[20] = "\0";
1133 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001134 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001135 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001136 SendMessage(speedUpDown, UDM_SETPOS,
1137 0, MAKELONG((short)(newSpeed / 0.5), 0));
1138 sprintf(speedStr, "%.2f", newSpeed);
1139 SetWindowText(speedEdit, speedStr);
1140 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001141}
1142
1143double RfbPlayer::getSpeed() {
1144 return is->getSpeed();
1145}
1146
1147void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001148 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001149}
1150
1151long RfbPlayer::getSeekOffset() {
1152 return is->getSeekOffset();
1153}
1154
1155bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001156 if (is) return is->isSeeking();
1157 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001158}
1159
1160bool RfbPlayer::isSeekMode() {
1161 return seekMode;
1162}
1163
1164bool RfbPlayer::isPaused() {
1165 return is->isPaused();
1166}
1167
1168long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001169 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001170}
1171
george828a471482005-02-06 07:15:53 +00001172void RfbPlayer::updatePos(long newPos) {
1173 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001174 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001175 long time = newPos / 1000;
1176 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001177 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001178
1179 // Update the position of slider
1180 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001181 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1182 sliderStepMs / double(newPos);
1183 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1184 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1185 }
george828a471482005-02-06 07:15:53 +00001186 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001187}
1188
1189void RfbPlayer::skipHandshaking() {
1190 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1191 is->skip(skipBytes);
1192 state_ = RFBSTATE_NORMAL;
1193}
1194
1195void programInfo() {
1196 win32::FileVersionInfo inf;
1197 _tprintf(_T("%s - %s, Version %s\n"),
1198 inf.getVerString(_T("ProductName")),
1199 inf.getVerString(_T("FileDescription")),
1200 inf.getVerString(_T("FileVersion")));
1201 printf("%s\n", buildTime);
1202 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1203}
1204
1205void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001206 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001207}
1208
george825beb62a2005-02-09 13:04:32 +00001209char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001210
1211// playerOptions is the player options with default parameters values,
1212// it is used only for run the player with command-line parameters
1213PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001214bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001215
1216bool processParams(int argc, char* argv[]) {
1217 for (int i = 1; i < argc; i++) {
1218 if ((strcasecmp(argv[i], "-help") == 0) ||
1219 (strcasecmp(argv[i], "--help") == 0) ||
1220 (strcasecmp(argv[i], "/help") == 0) ||
1221 (strcasecmp(argv[i], "-h") == 0) ||
1222 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001223 (strcasecmp(argv[i], "/?") == 0) ||
1224 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001225 print_usage = true;
1226 return true;
1227 }
1228
george825caee412005-03-09 09:52:10 +00001229 if ((strcasecmp(argv[i], "-pf") == 0) ||
1230 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001231 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001232 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001233 return false;
1234 }
george820981b342005-03-19 11:19:00 +00001235 playerOptions.pixelFormatIndex = pf;
george82193d8e42005-02-20 16:47:01 +00001236 continue;
1237 }
1238
1239
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001240 if ((strcasecmp(argv[i], "-speed") == 0) ||
1241 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001242 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001243 if (playbackSpeed <= 0) {
1244 return false;
1245 }
george825e7af742005-03-10 14:26:00 +00001246 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001247 continue;
1248 }
1249
1250 if ((strcasecmp(argv[i], "-pos") == 0) ||
1251 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001252 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001253 if (initTime <= 0)
1254 return false;
george825e7af742005-03-10 14:26:00 +00001255 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001256 continue;
1257 }
1258
1259 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1260 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001261 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001262 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001263 }
1264
1265 if (i != argc - 1)
1266 return false;
1267 }
1268
1269 fileName = strDup(argv[argc-1]);
1270 return true;
1271}
1272
1273//
1274// -=- WinMain
1275//
1276
1277int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1278
1279 // - Process the command-line
1280
1281 int argc = __argc;
1282 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001283 if ((argc > 1) && (!processParams(argc, argv))) {
1284 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1285 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001286 }
george82e6883de2005-02-08 14:42:12 +00001287
1288 if (print_usage) {
1289 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001290 return 0;
george8267cbcd02005-01-16 15:39:56 +00001291 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001292
george82e6883de2005-02-08 14:42:12 +00001293 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001294 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001295 try {
george825e7af742005-03-10 14:26:00 +00001296 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001297 } catch (rdr::Exception e) {
1298 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1299 delete player;
1300 return 0;
1301 }
1302
1303 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001304 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001305 MSG msg;
1306 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001307 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1308 TranslateMessage(&msg);
1309 DispatchMessage(&msg);
1310 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001311 }
1312
george82e6883de2005-02-08 14:42:12 +00001313 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001314 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001315 if (player) delete player;
1316 } catch (rdr::Exception e) {
1317 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1318 }
1319
1320 return 0;
1321};