blob: ef64ba4fab38bdd727b6c264c2bc7f83e6536110 [file] [log] [blame]
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001/* Copyright (C) 2004 TightVNC Team. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- RFB Player for Win32
20
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000021#include <rfb/LogWriter.h>
22#include <rfb/Exception.h>
23#include <rfb/Threading.h>
24
25#include <rfb_win32/Win32Util.h>
26#include <rfb_win32/WMShatter.h>
27
28#include <rfbplayer/rfbplayer.h>
29#include <rfbplayer/utils.h>
30#include <rfbplayer/resource.h>
george827549df42005-02-08 16:31:02 +000031#include <rfbplayer/GotoPosDialog.h>
george823d0c56e2005-03-09 08:30:27 +000032#include <rfbplayer/ChoosePixelFormatDialog.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000033
34using namespace rfb;
35using namespace rfb::win32;
36
37// -=- Variables & consts
38
39static LogWriter vlog("RfbPlayer");
40
41TStr rfb::win32::AppName("RfbPlayer");
42extern const char* buildTime;
43
george82e6883de2005-02-08 14:42:12 +000044char wrong_cmd_msg[] =
45 "Wrong command-line parameters!\n"
46 "Use for help: rfbplayer -help";
47
48char usage_msg[] =
49 "usage: rfbplayer <options> <filename>\n"
50 "Command-line options:\n"
51 " -help \t- Provide usage information.\n"
george825caee412005-03-09 09:52:10 +000052 " -pf <mode> \t- Forces the pixel format for the session.\n"
53 " \t List of the pixel formats:\n"
54 " \t 0 - Auto,\n"
55 " \t 1 - depth 8 (RGB332),\n"
56 " \t 2 - depth 16 (RGB655),\n"
57 " \t 3 - depth 24 (RGB888).\n"
george82e6883de2005-02-08 14:42:12 +000058 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
59 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
60 " -pos <ms> \t- Sets initial time position in the session file,\n"
61 " \t in milliseconds. Default: 0.\n"
62 " -autoplay \t- Runs the player in the playback mode.\n"
63 " -bell \t- Accepts the bell.\n";
64
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000065// -=- RfbPlayer's defines
66
67#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000068#define MAX_SPEED 10.00
69#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000070#define MAX_POS_TRACKBAR_RANGE 50
george8268d25142005-02-13 09:33:22 +000071#define DEFAULT_PLAYER_WIDTH 640
72#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000073
george825caee412005-03-09 09:52:10 +000074#define PF_AUTO 0
75#define PF_D8_RGB332 1
76#define PF_D16_RGB655 2
77#define PF_D24_RGB888 3
78#define PF_MODES 3
george82193d8e42005-02-20 16:47:01 +000079
george82d070c692005-01-19 16:44:04 +000080#define ID_TOOLBAR 500
81#define ID_PLAY 510
82#define ID_PAUSE 520
83#define ID_TIME_STATIC 530
84#define ID_SPEED_STATIC 540
85#define ID_SPEED_EDIT 550
86#define ID_POS_TRACKBAR 560
87#define ID_SPEED_UPDOWN 570
88
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000089//
90// -=- RfbPlayerClass
91
92//
93// Window class used as the basis for RfbPlayer instance
94//
95
96class RfbPlayerClass {
97public:
98 RfbPlayerClass();
99 ~RfbPlayerClass();
100 ATOM classAtom;
101 HINSTANCE instance;
102};
103
104LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
105 LRESULT result;
106
107 if (msg == WM_CREATE)
108 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
109 else if (msg == WM_DESTROY) {
110 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000111 SetWindowLong(hwnd, GWL_USERDATA, 0);
112 }
113 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
114 if (!_this) {
115 vlog.info("null _this in %x, message %u", hwnd, msg);
116 return DefWindowProc(hwnd, msg, wParam, lParam);
117 }
118
119 try {
120 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
121 } catch (rdr::Exception& e) {
122 vlog.error("untrapped: %s", e.str());
123 }
124
125 return result;
126};
127
128RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
129 WNDCLASS wndClass;
130 wndClass.style = 0;
131 wndClass.lpfnWndProc = RfbPlayerProc;
132 wndClass.cbClsExtra = 0;
133 wndClass.cbWndExtra = 0;
134 wndClass.hInstance = instance = GetModuleHandle(0);
135 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000136 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000137 if (!wndClass.hIcon)
138 printf("unable to load icon:%ld", GetLastError());
139 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
140 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000141 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000142 wndClass.lpszClassName = _T("RfbPlayerClass");
143 classAtom = RegisterClass(&wndClass);
144 if (!classAtom) {
145 throw rdr::SystemException("unable to register RfbPlayer window class",
146 GetLastError());
147 }
148}
149
150RfbPlayerClass::~RfbPlayerClass() {
151 if (classAtom) {
152 UnregisterClass((const TCHAR*)classAtom, instance);
153 }
154}
155
156RfbPlayerClass baseClass;
157
158//
159// -=- RfbFrameClass
160
161//
162// Window class used to displaying the rfb data
163//
164
165class RfbFrameClass {
166public:
167 RfbFrameClass();
168 ~RfbFrameClass();
169 ATOM classAtom;
170 HINSTANCE instance;
171};
172
173LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
174 LRESULT result;
175
176 if (msg == WM_CREATE)
177 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
178 else if (msg == WM_DESTROY)
179 SetWindowLong(hwnd, GWL_USERDATA, 0);
180 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
181 if (!_this) {
182 vlog.info("null _this in %x, message %u", hwnd, msg);
183 return DefWindowProc(hwnd, msg, wParam, lParam);
184 }
185
186 try {
187 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
188 } catch (rdr::Exception& e) {
189 vlog.error("untrapped: %s", e.str());
190 }
191
192 return result;
193}
194
195RfbFrameClass::RfbFrameClass() : classAtom(0) {
196 WNDCLASS wndClass;
197 wndClass.style = 0;
198 wndClass.lpfnWndProc = FrameProc;
199 wndClass.cbClsExtra = 0;
200 wndClass.cbWndExtra = 0;
201 wndClass.hInstance = instance = GetModuleHandle(0);
202 wndClass.hIcon = 0;
203 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
204 wndClass.hbrBackground = 0;
205 wndClass.lpszMenuName = 0;
206 wndClass.lpszClassName = _T("RfbPlayerClass1");
207 classAtom = RegisterClass(&wndClass);
208 if (!classAtom) {
209 throw rdr::SystemException("unable to register RfbPlayer window class",
210 GetLastError());
211 }
212}
213
214RfbFrameClass::~RfbFrameClass() {
215 if (classAtom) {
216 UnregisterClass((const TCHAR*)classAtom, instance);
217 }
218}
219
220RfbFrameClass frameClass;
221
222//
223// -=- RfbPlayer instance implementation
224//
225
george825caee412005-03-09 09:52:10 +0000226RfbPlayer::RfbPlayer(char *_fileName, int _pixelFormat = PF_AUTO,
george82193d8e42005-02-20 16:47:01 +0000227 long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000228 bool _autoplay = false, bool _acceptBell = false)
george825caee412005-03-09 09:52:10 +0000229: RfbProto(_fileName), pixelFormat(_pixelFormat), initTime(_initTime),
george82193d8e42005-02-20 16:47:01 +0000230 playbackSpeed(_playbackSpeed), autoplay(_autoplay), buffer(0),
231 client_size(0, 0, 32, 32), window_size(0, 0, 32, 32), cutText(0),
george823104aec2005-02-21 13:20:56 +0000232 seekMode(false), fileName(_fileName), lastPos(0), timeStatic(0),
233 speedEdit(0), posTrackBar(0), speedUpDown(0), acceptBell(_acceptBell),
234 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george820d2e19d2005-03-03 15:47:55 +0000235 loopPlayback(false), imageDataStartTime(0), rewindFlag(false),
236 stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000237
george82e6883de2005-02-08 14:42:12 +0000238 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000239
george823c8fbbf2005-01-24 11:09:08 +0000240 // Reset the full session time
241 strcpy(fullSessionTime, "00m:00s");
242
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000243 // Create the main window
244 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000245 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
246 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000247 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000248 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000249 if (!mainHwnd) {
250 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
251 }
252 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
253
254 // Create the backing buffer
255 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000256 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000257
george8217e92cb2005-01-31 16:01:02 +0000258 // Open the session file
259 if (fileName) {
260 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000261 if (initTime > 0) setPos(initTime);
262 setSpeed(playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000263 } else {
264 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000265 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000266 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000267}
268
269RfbPlayer::~RfbPlayer() {
270 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000271 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000272 delete rfbReader->join();
273 rfbReader = 0;
274 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000275 if (mainHwnd) {
276 setVisible(false);
277 DestroyWindow(mainHwnd);
278 mainHwnd = 0;
279 }
george825beb62a2005-02-09 13:04:32 +0000280 if (buffer) delete buffer;
281 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000282 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000283 vlog.debug("~RfbPlayer done");
284}
285
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000286LRESULT
287RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
288 switch (msg) {
289
290 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000291
292 case WM_CREATE:
293 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000294 // Create the frame window
295 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
296 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
297 hwnd, 0, frameClass.instance, this);
298
george82d070c692005-01-19 16:44:04 +0000299 createToolBar(hwnd);
300
george82006f2792005-02-05 07:40:47 +0000301 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000302
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000303 return 0;
304 }
305
george827214b822004-12-12 07:02:51 +0000306 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000307
308 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000309 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000310 case ID_OPENFILE:
311 {
312 char curDir[_MAX_DIR];
313 static char filename[_MAX_PATH];
314 OPENFILENAME ofn;
315 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
316 GetCurrentDirectory(sizeof(curDir), curDir);
317
318 ofn.lStructSize = sizeof(OPENFILENAME);
319 ofn.hwndOwner = getMainHandle();
320 ofn.lpstrFile = filename;
321 ofn.nMaxFile = sizeof(filename);
322 ofn.lpstrInitialDir = curDir;
323 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
324 "All files (*.*)\0*.*\0";
325 ofn.lpstrDefExt = "rfb";
326 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000327 if (GetOpenFileName(&ofn)) {
george825caee412005-03-09 09:52:10 +0000328 pixelFormat = PF_AUTO;
george826e51fcc2005-02-06 13:30:49 +0000329 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000330 }
george826e51fcc2005-02-06 13:30:49 +0000331 }
332 break;
george8271ca1772005-02-13 10:50:46 +0000333 case ID_CLOSEFILE:
334 closeSessionFile();
335 break;
george825c13c662005-01-27 14:48:23 +0000336 case ID_PLAY:
337 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000338 break;
339 case ID_PAUSE:
340 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000341 break;
342 case ID_STOP:
343 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000344 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000345 }
george825c13c662005-01-27 14:48:23 +0000346 break;
347 case ID_PLAYPAUSE:
348 if (isPaused()) {
349 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000350 } else {
351 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000352 }
george825c13c662005-01-27 14:48:23 +0000353 break;
george827549df42005-02-08 16:31:02 +0000354 case ID_GOTO:
355 {
356 GotoPosDialog gotoPosDlg;
357 if (gotoPosDlg.showDialog()) {
george821d5d40d2005-02-20 03:25:47 +0000358 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
359 setPos(gotoTime);
360 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000361 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000362 }
363 }
364 break;
george825c13c662005-01-27 14:48:23 +0000365 case ID_FULLSCREEN:
366 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
367 break;
george8231a36332005-02-06 17:27:34 +0000368 case ID_LOOP:
369 loopPlayback = !loopPlayback;
370 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
371 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
372 break;
george824ea27f62005-01-29 15:03:06 +0000373 case ID_RETURN:
374 // Update the speed if return pressed in speedEdit
375 if (speedEdit == GetFocus()) {
376 char speedStr[20], *stopStr;
377 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
378 double speed = strtod(speedStr, &stopStr);
379 if (speed > 0) {
380 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000381 } else {
382 speed = getSpeed();
383 }
384 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000385 }
386 break;
george8201aa6732005-02-06 17:13:03 +0000387 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000388 PostQuitMessage(0);
389 break;
george82ef5f7262005-02-08 15:09:26 +0000390 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000391 MessageBox(getMainHandle(),
392 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
393 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000394 }
395 break;
396
397 // Update frame's window size and add scrollbars if required
398
399 case WM_SIZE:
400 {
george82d070c692005-01-19 16:44:04 +0000401
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000402 Point old_offset = bufferToClient(Point(0, 0));
403
404 // Update the cached sizing information
405 RECT r;
406 GetClientRect(getMainHandle(), &r);
407 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
408 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
409
410 GetWindowRect(getFrameHandle(), &r);
411 window_size = Rect(r.left, r.top, r.right, r.bottom);
412 GetClientRect(getFrameHandle(), &r);
413 client_size = Rect(r.left, r.top, r.right, r.bottom);
414
415 // Determine whether scrollbars are required
416 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000417
418 // Resize the ToolBar
419 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000420
421 // Redraw if required
422 if (!old_offset.equals(bufferToClient(Point(0, 0))))
423 InvalidateRect(getFrameHandle(), 0, TRUE);
424 }
425 break;
george828a471482005-02-06 07:15:53 +0000426
427 // Process messages from posTrackBar
428
429 case WM_HSCROLL:
430 {
431 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
432 Pos *= sliderStepMs;
433
434 switch (LOWORD(wParam)) {
435 case TB_PAGEUP:
436 case TB_PAGEDOWN:
437 case TB_LINEUP:
438 case TB_LINEDOWN:
439 case TB_THUMBTRACK:
440 sliderDraging = true;
441 updatePos(Pos);
442 return 0;
443 case TB_ENDTRACK:
444 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000445 setPaused(isPaused());;
george828a471482005-02-06 07:15:53 +0000446 sliderDraging = false;
447 return 0;
448 default:
449 break;
450 }
451 }
452 break;
george829e6e6cc2005-01-29 13:12:05 +0000453
454 case WM_NOTIFY:
455 switch (((NMHDR*)lParam)->code) {
456 case UDN_DELTAPOS:
457 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000458 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000459 char speedStr[20] = "\0";
460 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
461 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
462 double speed;
463
george824ea27f62005-01-29 15:03:06 +0000464 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000465 if (upDown->iDelta > 0) {
466 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
467 } else {
george824ea27f62005-01-29 15:03:06 +0000468 // It's need to round the UpDown position
469 if ((upDown->iPos * 0.5) != getSpeed()) {
470 upDown->iDelta = 0;
471 lResult = TRUE;
472 }
george829e6e6cc2005-01-29 13:12:05 +0000473 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
474 }
george829e6e6cc2005-01-29 13:12:05 +0000475 sprintf(speedStr, "%.2f", speed);
476 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000477 is->setSpeed(speed);
478 playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000479 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000480 }
george824ea27f62005-01-29 15:03:06 +0000481 }
george829e6e6cc2005-01-29 13:12:05 +0000482 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000483
484 case WM_CLOSE:
485 vlog.debug("WM_CLOSE %x", getMainHandle());
486 PostQuitMessage(0);
487 break;
488 }
489
490 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
491}
492
493LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
494 switch (msg) {
495
496 case WM_PAINT:
497 {
george821e846ff2005-02-24 13:13:33 +0000498 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000499 seekMode = true;
500 return 0;
501 } else {
502 if (seekMode) {
503 seekMode = false;
504 InvalidateRect(getFrameHandle(), 0, true);
505 UpdateWindow(getFrameHandle());
506 return 0;
507 }
508 }
509
510 PAINTSTRUCT ps;
511 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
512 if (!paintDC)
513 throw SystemException("unable to BeginPaint", GetLastError());
514 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
515
516 if (!pr.is_empty()) {
517
518 if (buffer->bitmap) {
519
520 // Get device context
521 BitmapDC bitmapDC(paintDC, buffer->bitmap);
522
523 // Blit the border if required
524 Rect bufpos = bufferToClient(buffer->getRect());
525 if (!pr.enclosed_by(bufpos)) {
526 vlog.debug("draw border");
527 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
528 RECT r;
529 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
530 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
531 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
532 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
533 }
534
535 // Do the blit
536 Point buf_pos = clientToBuffer(pr.tl);
537 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
538 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
539 throw SystemException("unable to BitBlt to window", GetLastError());
540
541 } else {
542 // Blit a load of black
543 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
544 0, 0, 0, BLACKNESS))
545 throw SystemException("unable to BitBlt to blank window", GetLastError());
546 }
547 }
548 EndPaint(getFrameHandle(), &ps);
549 }
550 return 0;
551
552 case WM_VSCROLL:
553 case WM_HSCROLL:
554 {
555 Point delta;
556 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
557
558 switch (LOWORD(wParam)) {
559 case SB_PAGEUP: newpos -= 50; break;
560 case SB_PAGEDOWN: newpos += 50; break;
561 case SB_LINEUP: newpos -= 5; break;
562 case SB_LINEDOWN: newpos += 5; break;
563 case SB_THUMBTRACK:
564 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
565 default: vlog.info("received unknown scroll message");
566 };
567
568 if (msg == WM_HSCROLL)
569 setViewportOffset(Point(newpos, scrolloffset.y));
570 else
571 setViewportOffset(Point(scrolloffset.x, newpos));
572
573 SCROLLINFO si;
574 si.cbSize = sizeof(si);
575 si.fMask = SIF_POS;
576 si.nPos = newpos;
577 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
578 }
579 break;
580 }
581
582 return DefWindowProc(hwnd, msg, wParam, lParam);
583}
584
george82d070c692005-01-19 16:44:04 +0000585void RfbPlayer::createToolBar(HWND parentHwnd) {
586 RECT tRect;
587 InitCommonControls();
588
589 tb.create(ID_TOOLBAR, parentHwnd);
590 tb.addBitmap(4, IDB_TOOLBAR);
591
592 // Create the control buttons
593 tb.addButton(0, ID_PLAY);
594 tb.addButton(1, ID_PAUSE);
595 tb.addButton(2, ID_STOP);
596 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
597 tb.addButton(3, ID_FULLSCREEN);
598 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
599
600 // Create the static control for the time output
601 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
602 tb.getButtonRect(6, &tRect);
603 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
604 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
605 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
606 GetModuleHandle(0), 0);
607 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
608
609 // Create the trackbar control for the time position
610 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
611 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000612 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000613 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
614 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
615 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
616 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000617 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000618 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
619
620 // Create the label with "Speed:" caption
621 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
622 tb.getButtonRect(10, &tRect);
623 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
624 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
625 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
626
627 // Create the edit control and the spin for the speed managing
628 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
629 tb.getButtonRect(11, &tRect);
630 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
631 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
632 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
633 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
634 // It's need to send notify messages to toolbar parent window
635 SetParent(speedEdit, tb.getHandle());
636
637 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
638 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000639 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000640}
641
george82a21d2952005-02-12 11:30:03 +0000642void RfbPlayer::disableTBandMenuItems() {
643 // Disable the menu items
644 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
645 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
646 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
647 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
648 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
649 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
650 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
651 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
652 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
653
654 // Disable the toolbar buttons and child controls
655 tb.enableButton(ID_PLAY, false);
656 tb.enableButton(ID_PAUSE, false);
657 tb.enableButton(ID_STOP, false);
658 tb.enableButton(ID_FULLSCREEN, false);
659 EnableWindow(posTrackBar, false);
660 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000661 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000662}
663
george82f5043162005-02-12 11:37:18 +0000664void RfbPlayer::enableTBandMenuItems() {
665 // Enable the menu items
666 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
667 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
668 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
669 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
670 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
671 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
672 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
673 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
674 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
675
676 // Enable the toolbar buttons and child controls
677 tb.enableButton(ID_PLAY, true);
678 tb.enableButton(ID_PAUSE, true);
679 tb.enableButton(ID_STOP, true);
680 tb.enableButton(ID_FULLSCREEN, true);
681 EnableWindow(posTrackBar, true);
682 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000683 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000684}
685
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000686void RfbPlayer::setVisible(bool visible) {
687 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
688 if (visible) {
689 // When the window becomes visible, make it active
690 SetForegroundWindow(getMainHandle());
691 SetActiveWindow(getMainHandle());
692 }
693}
694
695void RfbPlayer::setTitle(const char *title) {
696 char _title[256];
697 strcpy(_title, AppName);
698 strcat(_title, " - ");
699 strcat(_title, title);
700 SetWindowText(getMainHandle(), _title);
701}
702
703void RfbPlayer::setFrameSize(int width, int height) {
704 // Calculate and set required size for main window
705 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000706 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000707 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
708 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
709 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000710 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
711 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
712 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000713 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
714
715 // Enable/disable scrollbars as appropriate
716 calculateScrollBars();
717}
718
719void RfbPlayer::calculateScrollBars() {
720 // Calculate the required size of window
721 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
722 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
723 DWORD old_style;
724 RECT r;
725 SetRect(&r, 0, 0, buffer->width(), buffer->height());
726 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
727 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
728
729 // Work out whether scroll bars are required
730 do {
731 old_style = style;
732
733 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
734 style |= WS_HSCROLL;
735 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
736 }
737 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
738 style |= WS_VSCROLL;
739 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
740 }
741 } while (style != old_style);
742
743 // Tell Windows to update the window style & cached settings
744 if (style != current_style) {
745 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
746 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
747 }
748
749 // Update the scroll settings
750 SCROLLINFO si;
751 if (style & WS_VSCROLL) {
752 si.cbSize = sizeof(si);
753 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
754 si.nMin = 0;
755 si.nMax = buffer->height();
756 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
757 maxscrolloffset.y = max(0, si.nMax-si.nPage);
758 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
759 si.nPos = scrolloffset.y;
760 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
761 }
762 if (style & WS_HSCROLL) {
763 si.cbSize = sizeof(si);
764 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
765 si.nMin = 0;
766 si.nMax = buffer->width();
767 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
768 maxscrolloffset.x = max(0, si.nMax-si.nPage);
769 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
770 si.nPos = scrolloffset.x;
771 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
772 }
773}
774
775bool RfbPlayer::setViewportOffset(const Point& tl) {
776/* ***
777 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
778 max(0, min(maxscrolloffset.y, tl.y)));
779 */
780 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
781 max(0, min(tl.y, buffer->height()-client_size.height())));
782 Point delta = np.translate(scrolloffset.negate());
783 if (!np.equals(scrolloffset)) {
784 scrolloffset = np;
785 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
786 UpdateWindow(getFrameHandle());
787 return true;
788 }
789 return false;
790}
791
792void RfbPlayer::close(const char* reason) {
793 setVisible(false);
794 if (reason) {
795 vlog.info("closing - %s", reason);
796 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
797 }
798 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
799}
800
801void RfbPlayer::blankBuffer() {
802 fillRect(buffer->getRect(), 0);
803}
804
805void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000806 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000807 blankBuffer();
808 newSession(fileName);
809 skipHandshaking();
george825f326fe2005-02-20 08:01:01 +0000810 is->setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000811 if (paused) is->pausePlayback();
812 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000813}
814
815void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000816 // Perform return if waitWhilePaused processed because
817 // rfbReader thread could receive the signal to close
818 if (waitWhilePaused()) return;
819
george8223e08562005-01-31 15:16:42 +0000820 static long update_time = GetTickCount();
821 try {
george828a471482005-02-06 07:15:53 +0000822 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
823 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000824 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000825 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000826 update_time = GetTickCount();
827 }
828 RfbProto::processMsg();
829 } catch (rdr::Exception e) {
830 if (strcmp(e.str(), "[End Of File]") == 0) {
831 rewind();
george8231a36332005-02-06 17:27:34 +0000832 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000833 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000834 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000835 return;
836 }
837 // It's a special exception to perform backward seeking.
838 // We only rewind the stream and seek the offset
839 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000840 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000841 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000842 rewind();
george820d2e19d2005-03-03 15:47:55 +0000843 if (!stopped) setPos(seekOffset);
844 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000845 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000846 rewindFlag = false;
george8223e08562005-01-31 15:16:42 +0000847 } else {
848 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
849 return;
850 }
851 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000852}
853
854void RfbPlayer::serverInit() {
855 RfbProto::serverInit();
856
george82b95503e2005-02-21 17:02:34 +0000857 // Save the image data start time
858 imageDataStartTime = is->getTimeOffset();
859
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000860 // Resize the backing buffer
861 buffer->setSize(cp.width, cp.height);
862
863 // Check on the true colour mode
864 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000865 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000866
867 // Set the session pixel format
george825caee412005-03-09 09:52:10 +0000868 switch (pixelFormat) {
869 case PF_AUTO:
george82193d8e42005-02-20 16:47:01 +0000870 break;
george825caee412005-03-09 09:52:10 +0000871 case PF_D8_RGB332:
george82193d8e42005-02-20 16:47:01 +0000872 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
873 break;
george825caee412005-03-09 09:52:10 +0000874 case PF_D16_RGB655:
george82193d8e42005-02-20 16:47:01 +0000875 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
876 break;
george825caee412005-03-09 09:52:10 +0000877 case PF_D24_RGB888:
george82193d8e42005-02-20 16:47:01 +0000878 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
879 break;
880 default:
881 throw rdr::Exception("This color depth is not supported!");
882 }
883 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000884
885 // If the window is not maximised then resize it
886 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
887 setFrameSize(cp.width, cp.height);
888
889 // Set the window title and show it
890 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000891
george82d4d69e62005-02-05 09:23:18 +0000892 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000893 sessionTimeMs = calculateSessionTime(fileName);
894 sprintf(fullSessionTime, "%.2um:%.2us",
895 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000896 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000897 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
898 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000899 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000900
george82006f2792005-02-05 07:40:47 +0000901 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000902}
903
904void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
905 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
906 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
907/* int i;
908 for (i=0;i<count;i++) {
909 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
910 }
911 // *** change to 0, 256?
912 refreshWindowPalette(first, count);
913 palette_changed = true;
914 InvalidateRect(getFrameHandle(), 0, FALSE);*/
915}
916
917void RfbPlayer::bell() {
918 if (acceptBell)
919 MessageBeep(-1);
920}
921
922void RfbPlayer::serverCutText(const char* str, int len) {
923 if (cutText != NULL)
924 delete [] cutText;
925 cutText = new char[len + 1];
926 memcpy(cutText, str, len);
927 cutText[len] = '\0';
928}
929
930void RfbPlayer::frameBufferUpdateEnd() {
931};
932
933void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
934}
935
936void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
937}
938
939
940void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
941 buffer->fillRect(r, pix);
942 invalidateBufferRect(r);
943}
944
945void RfbPlayer::imageRect(const Rect& r, void* pixels) {
946 buffer->imageRect(r, pixels);
947 invalidateBufferRect(r);
948}
949
950void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
951 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
952 invalidateBufferRect(r);
953}
954
955bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
956 Rect rect = bufferToClient(crect);
957 if (rect.intersect(client_size).is_empty()) return false;
958 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
959 InvalidateRect(getFrameHandle(), &invalid, FALSE);
960 return true;
961}
962
george820d2e19d2005-03-03 15:47:55 +0000963bool RfbPlayer::waitWhilePaused() {
964 bool result = false;
965 while(isPaused() && !isSeeking()) {
966 Sleep(20);
967 result = true;
968 }
969 return result;
970}
971
george8257f13522005-02-05 08:48:22 +0000972long RfbPlayer::calculateSessionTime(char *filename) {
973 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000974 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000975 try {
976 while (TRUE) {
977 sessionFile.skip(1024);
978 }
979 } catch (rdr::Exception e) {
980 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000981 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000982 } else {
983 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
984 return 0;
985 }
986 }
987 return 0;
988}
989
george826b87aff2005-02-13 10:48:21 +0000990void RfbPlayer::closeSessionFile() {
991 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +0000992 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000993 RECT r;
994
995 // Uncheck all toolbar buttons
996 if (tb.getHandle()) {
997 tb.checkButton(ID_PLAY, false);
998 tb.checkButton(ID_PAUSE, false);
999 tb.checkButton(ID_STOP, false);
1000 }
1001
1002 // Stop playback and update the player state
1003 disableTBandMenuItems();
1004 if (rfbReader) {
1005 delete rfbReader->join();
1006 rfbReader = 0;
1007 delete [] fileName;
1008 fileName = 0;
1009 }
1010 blankBuffer();
1011 setTitle("None");
george827009c892005-02-19 12:49:42 +00001012 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george826b87aff2005-02-13 10:48:21 +00001013 playbackSpeed = 1.0;
1014 SendMessage(speedUpDown, UDM_SETPOS,
1015 0, MAKELONG((short)(playbackSpeed / 0.5), 0));
1016 sprintf(speedStr, "%.2f", playbackSpeed);
1017 SetWindowText(speedEdit, speedStr);
1018 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1019
1020 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001021 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1022 dwStyle &= ~WS_MAXIMIZE;
1023 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1024 }
george822ff7a612005-02-19 17:05:24 +00001025 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1026 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1027 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001028 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001029 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001030 buffer->setSize(32, 32);
1031 calculateScrollBars();
1032
1033 // Update the cached sizing information and repaint the frame window
1034 GetWindowRect(getFrameHandle(), &r);
1035 window_size = Rect(r.left, r.top, r.right, r.bottom);
1036 GetClientRect(getFrameHandle(), &r);
1037 client_size = Rect(r.left, r.top, r.right, r.bottom);
1038 InvalidateRect(getFrameHandle(), 0, TRUE);
1039 UpdateWindow(getFrameHandle());
1040}
1041
george8217e92cb2005-01-31 16:01:02 +00001042void RfbPlayer::openSessionFile(char *_fileName) {
1043 fileName = strDup(_fileName);
1044
1045 // Close the previous reading thread
1046 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001047 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001048 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001049 }
1050 blankBuffer();
1051 newSession(fileName);
1052 setSpeed(playbackSpeed);
1053 rfbReader = new rfbSessionReader(this);
1054 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001055 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001056 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001057}
1058
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001059void RfbPlayer::setPaused(bool paused) {
1060 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001061 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001062 tb.checkButton(ID_PAUSE, true);
1063 tb.checkButton(ID_PLAY, false);
1064 tb.checkButton(ID_STOP, false);
1065 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1066 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001067 } else {
george825beb62a2005-02-09 13:04:32 +00001068 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001069 tb.checkButton(ID_PLAY, true);
1070 tb.checkButton(ID_STOP, false);
1071 tb.checkButton(ID_PAUSE, false);
1072 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1073 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001074 }
1075}
1076
george82006f2792005-02-05 07:40:47 +00001077void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001078 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001079 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001080 if (is) {
1081 is->pausePlayback();
1082 is->interruptFrameDelay();
1083 }
george82006f2792005-02-05 07:40:47 +00001084 tb.checkButton(ID_STOP, true);
1085 tb.checkButton(ID_PLAY, false);
1086 tb.checkButton(ID_PAUSE, false);
1087 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1088 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001089 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001090}
1091
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001092void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001093 if (speed > 0) {
1094 char speedStr[20] = "\0";
1095 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001096 is->setSpeed(newSpeed);
1097 playbackSpeed = newSpeed;
1098 SendMessage(speedUpDown, UDM_SETPOS,
1099 0, MAKELONG((short)(newSpeed / 0.5), 0));
1100 sprintf(speedStr, "%.2f", newSpeed);
1101 SetWindowText(speedEdit, speedStr);
1102 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001103}
1104
1105double RfbPlayer::getSpeed() {
1106 return is->getSpeed();
1107}
1108
1109void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001110 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001111}
1112
1113long RfbPlayer::getSeekOffset() {
1114 return is->getSeekOffset();
1115}
1116
1117bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001118 if (is) return is->isSeeking();
1119 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001120}
1121
1122bool RfbPlayer::isSeekMode() {
1123 return seekMode;
1124}
1125
1126bool RfbPlayer::isPaused() {
1127 return is->isPaused();
1128}
1129
1130long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001131 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001132}
1133
george828a471482005-02-06 07:15:53 +00001134void RfbPlayer::updatePos(long newPos) {
1135 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001136 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001137 long time = newPos / 1000;
1138 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001139 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001140
1141 // Update the position of slider
1142 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001143 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1144 sliderStepMs / double(newPos);
1145 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1146 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1147 }
george828a471482005-02-06 07:15:53 +00001148 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001149}
1150
1151void RfbPlayer::skipHandshaking() {
1152 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1153 is->skip(skipBytes);
1154 state_ = RFBSTATE_NORMAL;
1155}
1156
1157void programInfo() {
1158 win32::FileVersionInfo inf;
1159 _tprintf(_T("%s - %s, Version %s\n"),
1160 inf.getVerString(_T("ProductName")),
1161 inf.getVerString(_T("FileDescription")),
1162 inf.getVerString(_T("FileVersion")));
1163 printf("%s\n", buildTime);
1164 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1165}
1166
1167void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001168 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001169}
1170
1171double playbackSpeed = 1.0;
1172long initTime = -1;
george825caee412005-03-09 09:52:10 +00001173int pf = PF_AUTO;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001174bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001175char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001176bool print_usage = false;
1177bool acceptBell = false;
1178
1179bool processParams(int argc, char* argv[]) {
1180 for (int i = 1; i < argc; i++) {
1181 if ((strcasecmp(argv[i], "-help") == 0) ||
1182 (strcasecmp(argv[i], "--help") == 0) ||
1183 (strcasecmp(argv[i], "/help") == 0) ||
1184 (strcasecmp(argv[i], "-h") == 0) ||
1185 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001186 (strcasecmp(argv[i], "/?") == 0) ||
1187 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001188 print_usage = true;
1189 return true;
1190 }
1191
george825caee412005-03-09 09:52:10 +00001192 if ((strcasecmp(argv[i], "-pf") == 0) ||
1193 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
1194 pf = atoi(argv[++i]);
1195 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001196 return false;
1197 }
1198 continue;
1199 }
1200
1201
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001202 if ((strcasecmp(argv[i], "-speed") == 0) ||
1203 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1204 playbackSpeed = atof(argv[++i]);
1205 if (playbackSpeed <= 0) {
1206 return false;
1207 }
1208 continue;
1209 }
1210
1211 if ((strcasecmp(argv[i], "-pos") == 0) ||
1212 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1213 initTime = atol(argv[++i]);
1214 if (initTime <= 0)
1215 return false;
1216 continue;
1217 }
1218
1219 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1220 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001221 autoplay = true;
1222 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001223 }
1224
1225 if ((strcasecmp(argv[i], "-bell") == 0) ||
1226 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001227 acceptBell = true;
1228 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001229 }
1230
1231 if (i != argc - 1)
1232 return false;
1233 }
1234
1235 fileName = strDup(argv[argc-1]);
1236 return true;
1237}
1238
1239//
1240// -=- WinMain
1241//
1242
1243int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1244
1245 // - Process the command-line
1246
1247 int argc = __argc;
1248 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001249 if ((argc > 1) && (!processParams(argc, argv))) {
1250 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1251 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001252 }
george82e6883de2005-02-08 14:42:12 +00001253
1254 if (print_usage) {
1255 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001256 return 0;
george8267cbcd02005-01-16 15:39:56 +00001257 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001258
george82e6883de2005-02-08 14:42:12 +00001259 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001260 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001261 try {
george825caee412005-03-09 09:52:10 +00001262 player = new RfbPlayer(fileName, pf, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001263 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001264 } catch (rdr::Exception e) {
1265 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1266 delete player;
1267 return 0;
1268 }
1269
1270 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001271 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001272 MSG msg;
1273 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001274 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1275 TranslateMessage(&msg);
1276 DispatchMessage(&msg);
1277 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001278 }
1279
george82e6883de2005-02-08 14:42:12 +00001280 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001281 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001282 if (player) delete player;
1283 } catch (rdr::Exception e) {
1284 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1285 }
1286
1287 return 0;
1288};