blob: 9a181a54d9c0baec39f37923712e74418220984c [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
george82c7e9f792005-03-20 12:52:46 +0000880long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
881bool ChoosePixelFormatDialog::bigEndian = false;
882
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000883void RfbPlayer::serverInit() {
884 RfbProto::serverInit();
885
george82b95503e2005-02-21 17:02:34 +0000886 // Save the image data start time
887 imageDataStartTime = is->getTimeOffset();
888
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000889 // Resize the backing buffer
890 buffer->setSize(cp.width, cp.height);
891
892 // Check on the true colour mode
893 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000894 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000895
896 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000897 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000898 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000899 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000900 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000901 if (pixelFormatIndex < 0) {
902 options.autoDetectPF = true;
903 options.setPF((PixelFormat *)&cp.pf());
904 } else {
905 options.autoDetectPF = false;
906 options.setPF(&supportedPF[pixelFormatIndex].PF);
george82c7e9f792005-03-20 12:52:46 +0000907 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000908 }
george822c7634b2005-03-10 18:03:27 +0000909 } else {
910 is->pausePlayback();
911 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000912 }
913 } else {
george825a6df072005-03-20 09:56:17 +0000914 if (options.autoDetectPF) {
915 options.setPF((PixelFormat *)&cp.pf());
916 } else {
917 options.setPF(&supportedPF[options.pixelFormatIndex].PF);
george82c7e9f792005-03-20 12:52:46 +0000918 options.pixelFormat.bigEndian = options.bigEndianFlag;
george825a6df072005-03-20 09:56:17 +0000919 }
george825e7af742005-03-10 14:26:00 +0000920 }
george825a6df072005-03-20 09:56:17 +0000921 cp.setPF(options.pixelFormat);
922 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000923
924 // If the window is not maximised then resize it
925 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
926 setFrameSize(cp.width, cp.height);
927
928 // Set the window title and show it
929 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000930
george82d4d69e62005-02-05 09:23:18 +0000931 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000932 sessionTimeMs = calculateSessionTime(fileName);
933 sprintf(fullSessionTime, "%.2um:%.2us",
934 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000935 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000936 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
937 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000938 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000939
george825e7af742005-03-10 14:26:00 +0000940 setPaused(!options.autoPlay);
941 // Restore the parameters from registry,
942 // which was replaced by command-line parameters.
943 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000944}
945
946void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
947 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
948 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
949/* int i;
950 for (i=0;i<count;i++) {
951 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
952 }
953 // *** change to 0, 256?
954 refreshWindowPalette(first, count);
955 palette_changed = true;
956 InvalidateRect(getFrameHandle(), 0, FALSE);*/
957}
958
959void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000960 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000961 MessageBeep(-1);
962}
963
964void RfbPlayer::serverCutText(const char* str, int len) {
965 if (cutText != NULL)
966 delete [] cutText;
967 cutText = new char[len + 1];
968 memcpy(cutText, str, len);
969 cutText[len] = '\0';
970}
971
972void RfbPlayer::frameBufferUpdateEnd() {
973};
974
975void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
976}
977
978void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
979}
980
981
982void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
983 buffer->fillRect(r, pix);
984 invalidateBufferRect(r);
985}
986
987void RfbPlayer::imageRect(const Rect& r, void* pixels) {
988 buffer->imageRect(r, pixels);
989 invalidateBufferRect(r);
990}
991
992void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
993 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
994 invalidateBufferRect(r);
995}
996
997bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
998 Rect rect = bufferToClient(crect);
999 if (rect.intersect(client_size).is_empty()) return false;
1000 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
1001 InvalidateRect(getFrameHandle(), &invalid, FALSE);
1002 return true;
1003}
1004
george820d2e19d2005-03-03 15:47:55 +00001005bool RfbPlayer::waitWhilePaused() {
1006 bool result = false;
1007 while(isPaused() && !isSeeking()) {
1008 Sleep(20);
1009 result = true;
1010 }
1011 return result;
1012}
1013
george8257f13522005-02-05 08:48:22 +00001014long RfbPlayer::calculateSessionTime(char *filename) {
1015 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001016 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001017 try {
1018 while (TRUE) {
1019 sessionFile.skip(1024);
1020 }
1021 } catch (rdr::Exception e) {
1022 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001023 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001024 } else {
1025 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1026 return 0;
1027 }
1028 }
1029 return 0;
1030}
1031
george826b87aff2005-02-13 10:48:21 +00001032void RfbPlayer::closeSessionFile() {
1033 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001034 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001035 RECT r;
1036
1037 // Uncheck all toolbar buttons
1038 if (tb.getHandle()) {
1039 tb.checkButton(ID_PLAY, false);
1040 tb.checkButton(ID_PAUSE, false);
1041 tb.checkButton(ID_STOP, false);
1042 }
1043
1044 // Stop playback and update the player state
1045 disableTBandMenuItems();
1046 if (rfbReader) {
1047 delete rfbReader->join();
1048 rfbReader = 0;
1049 delete [] fileName;
1050 fileName = 0;
1051 }
1052 blankBuffer();
1053 setTitle("None");
george827009c892005-02-19 12:49:42 +00001054 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001055 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001056 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001057 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1058 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001059 SetWindowText(speedEdit, speedStr);
1060 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1061
1062 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001063 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1064 dwStyle &= ~WS_MAXIMIZE;
1065 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1066 }
george822ff7a612005-02-19 17:05:24 +00001067 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1068 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1069 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001070 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001071 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001072 buffer->setSize(32, 32);
1073 calculateScrollBars();
1074
1075 // Update the cached sizing information and repaint the frame window
1076 GetWindowRect(getFrameHandle(), &r);
1077 window_size = Rect(r.left, r.top, r.right, r.bottom);
1078 GetClientRect(getFrameHandle(), &r);
1079 client_size = Rect(r.left, r.top, r.right, r.bottom);
1080 InvalidateRect(getFrameHandle(), 0, TRUE);
1081 UpdateWindow(getFrameHandle());
1082}
1083
george8217e92cb2005-01-31 16:01:02 +00001084void RfbPlayer::openSessionFile(char *_fileName) {
1085 fileName = strDup(_fileName);
1086
1087 // Close the previous reading thread
1088 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001089 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001090 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001091 }
1092 blankBuffer();
1093 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001094 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001095 rfbReader = new rfbSessionReader(this);
1096 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001097 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001098 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001099}
1100
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001101void RfbPlayer::setPaused(bool paused) {
1102 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001103 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001104 tb.checkButton(ID_PAUSE, true);
1105 tb.checkButton(ID_PLAY, false);
1106 tb.checkButton(ID_STOP, false);
1107 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1108 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001109 } else {
george825beb62a2005-02-09 13:04:32 +00001110 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001111 tb.checkButton(ID_PLAY, true);
1112 tb.checkButton(ID_STOP, false);
1113 tb.checkButton(ID_PAUSE, false);
1114 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1115 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001116 }
1117}
1118
george82006f2792005-02-05 07:40:47 +00001119void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001120 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001121 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001122 if (is) {
1123 is->pausePlayback();
1124 is->interruptFrameDelay();
1125 }
george82006f2792005-02-05 07:40:47 +00001126 tb.checkButton(ID_STOP, true);
1127 tb.checkButton(ID_PLAY, false);
1128 tb.checkButton(ID_PAUSE, false);
1129 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1130 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001131 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001132}
1133
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001134void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001135 if (speed > 0) {
1136 char speedStr[20] = "\0";
1137 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001138 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001139 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001140 SendMessage(speedUpDown, UDM_SETPOS,
1141 0, MAKELONG((short)(newSpeed / 0.5), 0));
1142 sprintf(speedStr, "%.2f", newSpeed);
1143 SetWindowText(speedEdit, speedStr);
1144 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001145}
1146
1147double RfbPlayer::getSpeed() {
1148 return is->getSpeed();
1149}
1150
1151void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001152 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001153}
1154
1155long RfbPlayer::getSeekOffset() {
1156 return is->getSeekOffset();
1157}
1158
1159bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001160 if (is) return is->isSeeking();
1161 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001162}
1163
1164bool RfbPlayer::isSeekMode() {
1165 return seekMode;
1166}
1167
1168bool RfbPlayer::isPaused() {
1169 return is->isPaused();
1170}
1171
1172long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001173 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001174}
1175
george828a471482005-02-06 07:15:53 +00001176void RfbPlayer::updatePos(long newPos) {
1177 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001178 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001179 long time = newPos / 1000;
1180 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001181 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001182
1183 // Update the position of slider
1184 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001185 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1186 sliderStepMs / double(newPos);
1187 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1188 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1189 }
george828a471482005-02-06 07:15:53 +00001190 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001191}
1192
1193void RfbPlayer::skipHandshaking() {
1194 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1195 is->skip(skipBytes);
1196 state_ = RFBSTATE_NORMAL;
1197}
1198
1199void programInfo() {
1200 win32::FileVersionInfo inf;
1201 _tprintf(_T("%s - %s, Version %s\n"),
1202 inf.getVerString(_T("ProductName")),
1203 inf.getVerString(_T("FileDescription")),
1204 inf.getVerString(_T("FileVersion")));
1205 printf("%s\n", buildTime);
1206 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1207}
1208
1209void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001210 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001211}
1212
george825beb62a2005-02-09 13:04:32 +00001213char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001214
1215// playerOptions is the player options with default parameters values,
1216// it is used only for run the player with command-line parameters
1217PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001218bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001219
1220bool processParams(int argc, char* argv[]) {
1221 for (int i = 1; i < argc; i++) {
1222 if ((strcasecmp(argv[i], "-help") == 0) ||
1223 (strcasecmp(argv[i], "--help") == 0) ||
1224 (strcasecmp(argv[i], "/help") == 0) ||
1225 (strcasecmp(argv[i], "-h") == 0) ||
1226 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001227 (strcasecmp(argv[i], "/?") == 0) ||
1228 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001229 print_usage = true;
1230 return true;
1231 }
1232
george825caee412005-03-09 09:52:10 +00001233 if ((strcasecmp(argv[i], "-pf") == 0) ||
1234 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001235 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001236 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001237 return false;
1238 }
george820981b342005-03-19 11:19:00 +00001239 playerOptions.pixelFormatIndex = pf;
george82193d8e42005-02-20 16:47:01 +00001240 continue;
1241 }
1242
1243
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001244 if ((strcasecmp(argv[i], "-speed") == 0) ||
1245 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001246 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001247 if (playbackSpeed <= 0) {
1248 return false;
1249 }
george825e7af742005-03-10 14:26:00 +00001250 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001251 continue;
1252 }
1253
1254 if ((strcasecmp(argv[i], "-pos") == 0) ||
1255 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001256 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001257 if (initTime <= 0)
1258 return false;
george825e7af742005-03-10 14:26:00 +00001259 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001260 continue;
1261 }
1262
1263 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1264 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001265 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001266 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001267 }
1268
1269 if (i != argc - 1)
1270 return false;
1271 }
1272
1273 fileName = strDup(argv[argc-1]);
1274 return true;
1275}
1276
1277//
1278// -=- WinMain
1279//
1280
1281int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1282
1283 // - Process the command-line
1284
1285 int argc = __argc;
1286 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001287 if ((argc > 1) && (!processParams(argc, argv))) {
1288 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1289 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001290 }
george82e6883de2005-02-08 14:42:12 +00001291
1292 if (print_usage) {
1293 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001294 return 0;
george8267cbcd02005-01-16 15:39:56 +00001295 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001296
george82e6883de2005-02-08 14:42:12 +00001297 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001298 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001299 try {
george825e7af742005-03-10 14:26:00 +00001300 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001301 } catch (rdr::Exception e) {
1302 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1303 delete player;
1304 return 0;
1305 }
1306
1307 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001308 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001309 MSG msg;
1310 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001311 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1312 TranslateMessage(&msg);
1313 DispatchMessage(&msg);
1314 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001315 }
1316
george82e6883de2005-02-08 14:42:12 +00001317 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001318 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001319 if (player) delete player;
1320 } catch (rdr::Exception e) {
1321 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1322 }
1323
1324 return 0;
1325};