blob: 6de2810306f807f7d3e366b69d8ca2e66c78dbf0 [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>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000029
30using namespace rfb;
31using namespace rfb::win32;
32
33// -=- Variables & consts
34
35static LogWriter vlog("RfbPlayer");
36
37TStr rfb::win32::AppName("RfbPlayer");
38extern const char* buildTime;
39
george82e6883de2005-02-08 14:42:12 +000040char wrong_cmd_msg[] =
41 "Wrong command-line parameters!\n"
42 "Use for help: rfbplayer -help";
43
44char usage_msg[] =
45 "usage: rfbplayer <options> <filename>\n"
46 "Command-line options:\n"
47 " -help \t- Provide usage information.\n"
george825caee412005-03-09 09:52:10 +000048 " -pf <mode> \t- Forces the pixel format for the session.\n"
49 " \t List of the pixel formats:\n"
50 " \t 0 - Auto,\n"
51 " \t 1 - depth 8 (RGB332),\n"
52 " \t 2 - depth 16 (RGB655),\n"
53 " \t 3 - depth 24 (RGB888).\n"
george82e6883de2005-02-08 14:42:12 +000054 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
55 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
56 " -pos <ms> \t- Sets initial time position in the session file,\n"
57 " \t in milliseconds. Default: 0.\n"
george825e7af742005-03-10 14:26:00 +000058 " -autoplay \t- Runs the player in the playback mode.\n";
george82e6883de2005-02-08 14:42:12 +000059
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000060// -=- RfbPlayer's defines
61
62#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000063#define MAX_SPEED 10.00
64#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000065#define MAX_POS_TRACKBAR_RANGE 50
george825e7af742005-03-10 14:26:00 +000066#define CTRL_BAR_HEIGHT 28
george8268d25142005-02-13 09:33:22 +000067#define DEFAULT_PLAYER_WIDTH 640
68#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000069
george82d070c692005-01-19 16:44:04 +000070#define ID_TOOLBAR 500
71#define ID_PLAY 510
72#define ID_PAUSE 520
73#define ID_TIME_STATIC 530
74#define ID_SPEED_STATIC 540
75#define ID_SPEED_EDIT 550
76#define ID_POS_TRACKBAR 560
77#define ID_SPEED_UPDOWN 570
78
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000079//
80// -=- RfbPlayerClass
81
82//
83// Window class used as the basis for RfbPlayer instance
84//
85
86class RfbPlayerClass {
87public:
88 RfbPlayerClass();
89 ~RfbPlayerClass();
90 ATOM classAtom;
91 HINSTANCE instance;
92};
93
94LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
95 LRESULT result;
96
97 if (msg == WM_CREATE)
98 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
99 else if (msg == WM_DESTROY) {
100 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000101 SetWindowLong(hwnd, GWL_USERDATA, 0);
102 }
103 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
104 if (!_this) {
105 vlog.info("null _this in %x, message %u", hwnd, msg);
106 return DefWindowProc(hwnd, msg, wParam, lParam);
107 }
108
109 try {
110 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
111 } catch (rdr::Exception& e) {
112 vlog.error("untrapped: %s", e.str());
113 }
114
115 return result;
116};
117
118RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
119 WNDCLASS wndClass;
120 wndClass.style = 0;
121 wndClass.lpfnWndProc = RfbPlayerProc;
122 wndClass.cbClsExtra = 0;
123 wndClass.cbWndExtra = 0;
124 wndClass.hInstance = instance = GetModuleHandle(0);
125 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000126 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000127 if (!wndClass.hIcon)
128 printf("unable to load icon:%ld", GetLastError());
129 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
130 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000131 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000132 wndClass.lpszClassName = _T("RfbPlayerClass");
133 classAtom = RegisterClass(&wndClass);
134 if (!classAtom) {
135 throw rdr::SystemException("unable to register RfbPlayer window class",
136 GetLastError());
137 }
138}
139
140RfbPlayerClass::~RfbPlayerClass() {
141 if (classAtom) {
142 UnregisterClass((const TCHAR*)classAtom, instance);
143 }
144}
145
146RfbPlayerClass baseClass;
147
148//
149// -=- RfbFrameClass
150
151//
152// Window class used to displaying the rfb data
153//
154
155class RfbFrameClass {
156public:
157 RfbFrameClass();
158 ~RfbFrameClass();
159 ATOM classAtom;
160 HINSTANCE instance;
161};
162
163LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
164 LRESULT result;
165
166 if (msg == WM_CREATE)
167 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
168 else if (msg == WM_DESTROY)
169 SetWindowLong(hwnd, GWL_USERDATA, 0);
170 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
171 if (!_this) {
172 vlog.info("null _this in %x, message %u", hwnd, msg);
173 return DefWindowProc(hwnd, msg, wParam, lParam);
174 }
175
176 try {
177 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
178 } catch (rdr::Exception& e) {
179 vlog.error("untrapped: %s", e.str());
180 }
181
182 return result;
183}
184
185RfbFrameClass::RfbFrameClass() : classAtom(0) {
186 WNDCLASS wndClass;
187 wndClass.style = 0;
188 wndClass.lpfnWndProc = FrameProc;
189 wndClass.cbClsExtra = 0;
190 wndClass.cbWndExtra = 0;
191 wndClass.hInstance = instance = GetModuleHandle(0);
192 wndClass.hIcon = 0;
193 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
194 wndClass.hbrBackground = 0;
195 wndClass.lpszMenuName = 0;
196 wndClass.lpszClassName = _T("RfbPlayerClass1");
197 classAtom = RegisterClass(&wndClass);
198 if (!classAtom) {
199 throw rdr::SystemException("unable to register RfbPlayer window class",
200 GetLastError());
201 }
202}
203
204RfbFrameClass::~RfbFrameClass() {
205 if (classAtom) {
206 UnregisterClass((const TCHAR*)classAtom, instance);
207 }
208}
209
210RfbFrameClass frameClass;
211
212//
213// -=- RfbPlayer instance implementation
214//
215
george825e7af742005-03-10 14:26:00 +0000216RfbPlayer::RfbPlayer(char *_fileName, PlayerOptions *_options)
217: RfbProto(_fileName), fileName(_fileName), buffer(0), client_size(0, 0, 32, 32),
218 window_size(0, 0, 32, 32), cutText(0), seekMode(false), lastPos(0),
219 timeStatic(0), speedEdit(0), posTrackBar(0), speedUpDown(0),
george823104aec2005-02-21 13:20:56 +0000220 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george825e7af742005-03-10 14:26:00 +0000221 imageDataStartTime(0), rewindFlag(false), stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000222
george825e7af742005-03-10 14:26:00 +0000223 // Save the player options
224 memcpy(&options, _options, sizeof(options));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000225
george823c8fbbf2005-01-24 11:09:08 +0000226 // Reset the full session time
227 strcpy(fullSessionTime, "00m:00s");
228
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000229 // Create the main window
230 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000231 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
232 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000233 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000234 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000235 if (!mainHwnd) {
236 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
237 }
238 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
239
240 // Create the backing buffer
241 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000242 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000243
george825e7af742005-03-10 14:26:00 +0000244 // If run with command-line parameters,
245 // open the session file with default settings, otherwise
246 // restore player settings from the registry
george8217e92cb2005-01-31 16:01:02 +0000247 if (fileName) {
248 openSessionFile(fileName);
george825e7af742005-03-10 14:26:00 +0000249 if (options.initTime > 0) setPos(options.initTime);
250 setSpeed(options.playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000251 } else {
george825e7af742005-03-10 14:26:00 +0000252 options.readFromRegistry();
george8263ebbcc2005-02-12 12:09:13 +0000253 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000254 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000255 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000256}
257
258RfbPlayer::~RfbPlayer() {
george825e7af742005-03-10 14:26:00 +0000259 options.writeToRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000260 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000261 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000262 delete rfbReader->join();
263 rfbReader = 0;
264 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000265 if (mainHwnd) {
266 setVisible(false);
267 DestroyWindow(mainHwnd);
268 mainHwnd = 0;
269 }
george825beb62a2005-02-09 13:04:32 +0000270 if (buffer) delete buffer;
271 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000272 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000273 vlog.debug("~RfbPlayer done");
274}
275
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000276LRESULT
277RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
278 switch (msg) {
279
280 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000281
282 case WM_CREATE:
283 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000284 // Create the frame window
285 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
286 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
287 hwnd, 0, frameClass.instance, this);
288
george82d070c692005-01-19 16:44:04 +0000289 createToolBar(hwnd);
290
george82006f2792005-02-05 07:40:47 +0000291 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000292
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000293 return 0;
294 }
295
george827214b822004-12-12 07:02:51 +0000296 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000297
298 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000299 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000300 case ID_OPENFILE:
301 {
302 char curDir[_MAX_DIR];
303 static char filename[_MAX_PATH];
304 OPENFILENAME ofn;
305 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
306 GetCurrentDirectory(sizeof(curDir), curDir);
307
308 ofn.lStructSize = sizeof(OPENFILENAME);
309 ofn.hwndOwner = getMainHandle();
310 ofn.lpstrFile = filename;
311 ofn.nMaxFile = sizeof(filename);
312 ofn.lpstrInitialDir = curDir;
313 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
314 "All files (*.*)\0*.*\0";
315 ofn.lpstrDefExt = "rfb";
316 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000317 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000318 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000319 }
george826e51fcc2005-02-06 13:30:49 +0000320 }
321 break;
george8271ca1772005-02-13 10:50:46 +0000322 case ID_CLOSEFILE:
323 closeSessionFile();
324 break;
george825c13c662005-01-27 14:48:23 +0000325 case ID_PLAY:
326 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000327 break;
328 case ID_PAUSE:
329 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000330 break;
331 case ID_STOP:
332 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000333 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000334 }
george825c13c662005-01-27 14:48:23 +0000335 break;
336 case ID_PLAYPAUSE:
337 if (isPaused()) {
338 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000339 } else {
340 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000341 }
george825c13c662005-01-27 14:48:23 +0000342 break;
george827549df42005-02-08 16:31:02 +0000343 case ID_GOTO:
344 {
345 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000346 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000347 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
348 setPos(gotoTime);
349 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000350 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000351 }
352 }
353 break;
george825c13c662005-01-27 14:48:23 +0000354 case ID_FULLSCREEN:
355 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
356 break;
george8231a36332005-02-06 17:27:34 +0000357 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000358 options.loopPlayback = !options.loopPlayback;
359 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000360 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
361 break;
george824ea27f62005-01-29 15:03:06 +0000362 case ID_RETURN:
363 // Update the speed if return pressed in speedEdit
364 if (speedEdit == GetFocus()) {
365 char speedStr[20], *stopStr;
366 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
367 double speed = strtod(speedStr, &stopStr);
368 if (speed > 0) {
369 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000370 } else {
371 speed = getSpeed();
372 }
373 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000374 }
375 break;
george825e7af742005-03-10 14:26:00 +0000376 case ID_OPTIONS:
377 {
378 OptionsDialog optionsDialog(&options);
george82d9957b72005-03-11 14:22:14 +0000379 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000380 }
381 break;
george8201aa6732005-02-06 17:13:03 +0000382 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000383 PostQuitMessage(0);
384 break;
george82ef5f7262005-02-08 15:09:26 +0000385 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000386 MessageBox(getMainHandle(),
387 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
388 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000389 }
390 break;
391
392 // Update frame's window size and add scrollbars if required
393
394 case WM_SIZE:
395 {
george82d070c692005-01-19 16:44:04 +0000396
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000397 Point old_offset = bufferToClient(Point(0, 0));
398
399 // Update the cached sizing information
400 RECT r;
401 GetClientRect(getMainHandle(), &r);
402 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
403 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
404
405 GetWindowRect(getFrameHandle(), &r);
406 window_size = Rect(r.left, r.top, r.right, r.bottom);
407 GetClientRect(getFrameHandle(), &r);
408 client_size = Rect(r.left, r.top, r.right, r.bottom);
409
410 // Determine whether scrollbars are required
411 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000412
413 // Resize the ToolBar
414 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000415
416 // Redraw if required
417 if (!old_offset.equals(bufferToClient(Point(0, 0))))
418 InvalidateRect(getFrameHandle(), 0, TRUE);
419 }
420 break;
george828a471482005-02-06 07:15:53 +0000421
422 // Process messages from posTrackBar
423
424 case WM_HSCROLL:
425 {
426 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
427 Pos *= sliderStepMs;
428
429 switch (LOWORD(wParam)) {
430 case TB_PAGEUP:
431 case TB_PAGEDOWN:
432 case TB_LINEUP:
433 case TB_LINEDOWN:
434 case TB_THUMBTRACK:
435 sliderDraging = true;
436 updatePos(Pos);
437 return 0;
george82bcc129b2005-03-15 17:11:40 +0000438 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000439 case TB_ENDTRACK:
440 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000441 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000442 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000443 sliderDraging = false;
444 return 0;
445 default:
446 break;
447 }
448 }
449 break;
george829e6e6cc2005-01-29 13:12:05 +0000450
451 case WM_NOTIFY:
452 switch (((NMHDR*)lParam)->code) {
453 case UDN_DELTAPOS:
454 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000455 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000456 char speedStr[20] = "\0";
457 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
458 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
459 double speed;
460
george824ea27f62005-01-29 15:03:06 +0000461 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000462 if (upDown->iDelta > 0) {
463 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
464 } else {
george824ea27f62005-01-29 15:03:06 +0000465 // It's need to round the UpDown position
466 if ((upDown->iPos * 0.5) != getSpeed()) {
467 upDown->iDelta = 0;
468 lResult = TRUE;
469 }
george829e6e6cc2005-01-29 13:12:05 +0000470 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
471 }
george829e6e6cc2005-01-29 13:12:05 +0000472 sprintf(speedStr, "%.2f", speed);
473 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000474 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000475 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000476 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000477 }
george824ea27f62005-01-29 15:03:06 +0000478 }
george829e6e6cc2005-01-29 13:12:05 +0000479 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000480
481 case WM_CLOSE:
482 vlog.debug("WM_CLOSE %x", getMainHandle());
483 PostQuitMessage(0);
484 break;
485 }
486
487 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
488}
489
490LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
491 switch (msg) {
492
493 case WM_PAINT:
494 {
george821e846ff2005-02-24 13:13:33 +0000495 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000496 seekMode = true;
497 return 0;
498 } else {
499 if (seekMode) {
500 seekMode = false;
501 InvalidateRect(getFrameHandle(), 0, true);
502 UpdateWindow(getFrameHandle());
503 return 0;
504 }
505 }
506
507 PAINTSTRUCT ps;
508 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
509 if (!paintDC)
510 throw SystemException("unable to BeginPaint", GetLastError());
511 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
512
513 if (!pr.is_empty()) {
514
515 if (buffer->bitmap) {
516
517 // Get device context
518 BitmapDC bitmapDC(paintDC, buffer->bitmap);
519
520 // Blit the border if required
521 Rect bufpos = bufferToClient(buffer->getRect());
522 if (!pr.enclosed_by(bufpos)) {
523 vlog.debug("draw border");
524 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
525 RECT r;
526 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
527 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
528 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
529 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
530 }
531
532 // Do the blit
533 Point buf_pos = clientToBuffer(pr.tl);
534 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
535 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
536 throw SystemException("unable to BitBlt to window", GetLastError());
537
538 } else {
539 // Blit a load of black
540 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
541 0, 0, 0, BLACKNESS))
542 throw SystemException("unable to BitBlt to blank window", GetLastError());
543 }
544 }
545 EndPaint(getFrameHandle(), &ps);
546 }
547 return 0;
george8203c01da2005-03-16 12:36:53 +0000548
george82aff63ab2005-03-16 13:48:59 +0000549 // Process play/pause by the left mouse button
550 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000551 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
552 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000553
554 case WM_VSCROLL:
555 case WM_HSCROLL:
556 {
557 Point delta;
558 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
559
560 switch (LOWORD(wParam)) {
561 case SB_PAGEUP: newpos -= 50; break;
562 case SB_PAGEDOWN: newpos += 50; break;
563 case SB_LINEUP: newpos -= 5; break;
564 case SB_LINEDOWN: newpos += 5; break;
565 case SB_THUMBTRACK:
566 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
567 default: vlog.info("received unknown scroll message");
568 };
569
570 if (msg == WM_HSCROLL)
571 setViewportOffset(Point(newpos, scrolloffset.y));
572 else
573 setViewportOffset(Point(scrolloffset.x, newpos));
574
575 SCROLLINFO si;
576 si.cbSize = sizeof(si);
577 si.fMask = SIF_POS;
578 si.nPos = newpos;
579 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
580 }
581 break;
582 }
583
584 return DefWindowProc(hwnd, msg, wParam, lParam);
585}
586
george82d070c692005-01-19 16:44:04 +0000587void RfbPlayer::createToolBar(HWND parentHwnd) {
588 RECT tRect;
589 InitCommonControls();
590
591 tb.create(ID_TOOLBAR, parentHwnd);
592 tb.addBitmap(4, IDB_TOOLBAR);
593
594 // Create the control buttons
595 tb.addButton(0, ID_PLAY);
596 tb.addButton(1, ID_PAUSE);
597 tb.addButton(2, ID_STOP);
598 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
599 tb.addButton(3, ID_FULLSCREEN);
600 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
601
602 // Create the static control for the time output
603 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
604 tb.getButtonRect(6, &tRect);
605 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
606 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
607 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
608 GetModuleHandle(0), 0);
609 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
610
611 // Create the trackbar control for the time position
612 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
613 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000614 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000615 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
616 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
617 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
618 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000619 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000620 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
621
622 // Create the label with "Speed:" caption
623 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
624 tb.getButtonRect(10, &tRect);
625 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
626 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
627 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
628
629 // Create the edit control and the spin for the speed managing
630 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
631 tb.getButtonRect(11, &tRect);
632 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
633 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
634 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
635 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
636 // It's need to send notify messages to toolbar parent window
637 SetParent(speedEdit, tb.getHandle());
638
639 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
640 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000641 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000642}
643
george82a21d2952005-02-12 11:30:03 +0000644void RfbPlayer::disableTBandMenuItems() {
645 // Disable the menu items
646 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
647 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
648 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
649 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
650 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
651 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
652 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
653 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
654 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
655
656 // Disable the toolbar buttons and child controls
657 tb.enableButton(ID_PLAY, false);
658 tb.enableButton(ID_PAUSE, false);
659 tb.enableButton(ID_STOP, false);
660 tb.enableButton(ID_FULLSCREEN, false);
661 EnableWindow(posTrackBar, false);
662 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000663 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000664}
665
george82f5043162005-02-12 11:37:18 +0000666void RfbPlayer::enableTBandMenuItems() {
667 // Enable the menu items
668 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
669 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
670 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
671 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
672 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
673 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
674 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
675 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
676 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
677
678 // Enable the toolbar buttons and child controls
679 tb.enableButton(ID_PLAY, true);
680 tb.enableButton(ID_PAUSE, true);
681 tb.enableButton(ID_STOP, true);
682 tb.enableButton(ID_FULLSCREEN, true);
683 EnableWindow(posTrackBar, true);
684 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000685 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000686}
687
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000688void RfbPlayer::setVisible(bool visible) {
689 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
690 if (visible) {
691 // When the window becomes visible, make it active
692 SetForegroundWindow(getMainHandle());
693 SetActiveWindow(getMainHandle());
694 }
695}
696
697void RfbPlayer::setTitle(const char *title) {
698 char _title[256];
699 strcpy(_title, AppName);
700 strcat(_title, " - ");
701 strcat(_title, title);
702 SetWindowText(getMainHandle(), _title);
703}
704
705void RfbPlayer::setFrameSize(int width, int height) {
706 // Calculate and set required size for main window
707 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000708 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000709 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
710 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
711 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000712 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
713 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
714 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000715 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
716
717 // Enable/disable scrollbars as appropriate
718 calculateScrollBars();
719}
720
721void RfbPlayer::calculateScrollBars() {
722 // Calculate the required size of window
723 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
724 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
725 DWORD old_style;
726 RECT r;
727 SetRect(&r, 0, 0, buffer->width(), buffer->height());
728 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
729 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
730
731 // Work out whether scroll bars are required
732 do {
733 old_style = style;
734
735 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
736 style |= WS_HSCROLL;
737 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
738 }
739 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
740 style |= WS_VSCROLL;
741 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
742 }
743 } while (style != old_style);
744
745 // Tell Windows to update the window style & cached settings
746 if (style != current_style) {
747 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
748 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
749 }
750
751 // Update the scroll settings
752 SCROLLINFO si;
753 if (style & WS_VSCROLL) {
754 si.cbSize = sizeof(si);
755 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
756 si.nMin = 0;
757 si.nMax = buffer->height();
758 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
759 maxscrolloffset.y = max(0, si.nMax-si.nPage);
760 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
761 si.nPos = scrolloffset.y;
762 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
763 }
764 if (style & WS_HSCROLL) {
765 si.cbSize = sizeof(si);
766 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
767 si.nMin = 0;
768 si.nMax = buffer->width();
769 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
770 maxscrolloffset.x = max(0, si.nMax-si.nPage);
771 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
772 si.nPos = scrolloffset.x;
773 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
774 }
george82a758a7a2005-03-15 16:34:57 +0000775
776 // Update the cached client size
777 GetClientRect(getFrameHandle(), &r);
778 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000779}
780
781bool RfbPlayer::setViewportOffset(const Point& tl) {
782/* ***
783 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
784 max(0, min(maxscrolloffset.y, tl.y)));
785 */
786 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
787 max(0, min(tl.y, buffer->height()-client_size.height())));
788 Point delta = np.translate(scrolloffset.negate());
789 if (!np.equals(scrolloffset)) {
790 scrolloffset = np;
791 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
792 UpdateWindow(getFrameHandle());
793 return true;
794 }
795 return false;
796}
797
798void RfbPlayer::close(const char* reason) {
799 setVisible(false);
800 if (reason) {
801 vlog.info("closing - %s", reason);
802 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
803 }
804 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
805}
806
807void RfbPlayer::blankBuffer() {
808 fillRect(buffer->getRect(), 0);
809}
810
811void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000812 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000813 blankBuffer();
814 newSession(fileName);
815 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000816 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000817 if (paused) is->pausePlayback();
818 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000819}
820
821void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000822 // Perform return if waitWhilePaused processed because
823 // rfbReader thread could receive the signal to close
824 if (waitWhilePaused()) return;
825
george8223e08562005-01-31 15:16:42 +0000826 static long update_time = GetTickCount();
827 try {
george828a471482005-02-06 07:15:53 +0000828 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
829 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000830 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000831 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000832 update_time = GetTickCount();
833 }
834 RfbProto::processMsg();
835 } catch (rdr::Exception e) {
836 if (strcmp(e.str(), "[End Of File]") == 0) {
837 rewind();
george825e7af742005-03-10 14:26:00 +0000838 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000839 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000840 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000841 return;
842 }
843 // It's a special exception to perform backward seeking.
844 // We only rewind the stream and seek the offset
845 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000846 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000847 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000848 rewind();
george820d2e19d2005-03-03 15:47:55 +0000849 if (!stopped) setPos(seekOffset);
850 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000851 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000852 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000853 return;
854 }
855 // It's a special exception which is used to terminate the playback
856 if (strcmp(e.str(), "[TERMINATE]") == 0) {
857 sessionTerminateThread *terminate = new sessionTerminateThread(this);
858 terminate->start();
george8223e08562005-01-31 15:16:42 +0000859 } else {
george820e980cc2005-03-10 18:18:34 +0000860 // Show the exception message and close the session playback
861 is->pausePlayback();
862 char message[256] = "\0";
863 strcat(message, e.str());
864 strcat(message, "\nMaybe you force wrong the pixel format for this session");
865 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
866 sessionTerminateThread *terminate = new sessionTerminateThread(this);
867 terminate->start();
george8223e08562005-01-31 15:16:42 +0000868 return;
869 }
870 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000871}
872
873void RfbPlayer::serverInit() {
874 RfbProto::serverInit();
875
george82b95503e2005-02-21 17:02:34 +0000876 // Save the image data start time
877 imageDataStartTime = is->getTimeOffset();
878
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000879 // Resize the backing buffer
880 buffer->setSize(cp.width, cp.height);
881
882 // Check on the true colour mode
883 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000884 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000885
886 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000887 static long pixelFormat = PF_AUTO;
888 if (options.askPixelFormat) {
889 ChoosePixelFormatDialog choosePixelFormatDialog(pixelFormat);
george82d9957b72005-03-11 14:22:14 +0000890 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george825e7af742005-03-10 14:26:00 +0000891 pixelFormat = choosePixelFormatDialog.getPF();
george822c7634b2005-03-10 18:03:27 +0000892 } else {
893 is->pausePlayback();
894 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000895 }
896 } else {
897 pixelFormat = options.pixelFormat;
898 }
george825caee412005-03-09 09:52:10 +0000899 switch (pixelFormat) {
900 case PF_AUTO:
george82193d8e42005-02-20 16:47:01 +0000901 break;
george825caee412005-03-09 09:52:10 +0000902 case PF_D8_RGB332:
george82193d8e42005-02-20 16:47:01 +0000903 cp.setPF(PixelFormat(8,8,0,1,7,7,3,0,3,6));
904 break;
george825caee412005-03-09 09:52:10 +0000905 case PF_D16_RGB655:
george82193d8e42005-02-20 16:47:01 +0000906 cp.setPF(PixelFormat(16,16,0,1,63,31,31,0,6,11));
907 break;
george825caee412005-03-09 09:52:10 +0000908 case PF_D24_RGB888:
george82193d8e42005-02-20 16:47:01 +0000909 cp.setPF(PixelFormat(32,24,0,1,255,255,255,16,8,0));
910 break;
911 default:
912 throw rdr::Exception("This color depth is not supported!");
913 }
914 buffer->setPF(cp.pf());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000915
916 // If the window is not maximised then resize it
917 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
918 setFrameSize(cp.width, cp.height);
919
920 // Set the window title and show it
921 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000922
george82d4d69e62005-02-05 09:23:18 +0000923 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000924 sessionTimeMs = calculateSessionTime(fileName);
925 sprintf(fullSessionTime, "%.2um:%.2us",
926 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000927 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000928 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
929 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000930 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000931
george825e7af742005-03-10 14:26:00 +0000932 setPaused(!options.autoPlay);
933 // Restore the parameters from registry,
934 // which was replaced by command-line parameters.
935 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000936}
937
938void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
939 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
940 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
941/* int i;
942 for (i=0;i<count;i++) {
943 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
944 }
945 // *** change to 0, 256?
946 refreshWindowPalette(first, count);
947 palette_changed = true;
948 InvalidateRect(getFrameHandle(), 0, FALSE);*/
949}
950
951void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000952 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000953 MessageBeep(-1);
954}
955
956void RfbPlayer::serverCutText(const char* str, int len) {
957 if (cutText != NULL)
958 delete [] cutText;
959 cutText = new char[len + 1];
960 memcpy(cutText, str, len);
961 cutText[len] = '\0';
962}
963
964void RfbPlayer::frameBufferUpdateEnd() {
965};
966
967void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
968}
969
970void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
971}
972
973
974void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
975 buffer->fillRect(r, pix);
976 invalidateBufferRect(r);
977}
978
979void RfbPlayer::imageRect(const Rect& r, void* pixels) {
980 buffer->imageRect(r, pixels);
981 invalidateBufferRect(r);
982}
983
984void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
985 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
986 invalidateBufferRect(r);
987}
988
989bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
990 Rect rect = bufferToClient(crect);
991 if (rect.intersect(client_size).is_empty()) return false;
992 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
993 InvalidateRect(getFrameHandle(), &invalid, FALSE);
994 return true;
995}
996
george820d2e19d2005-03-03 15:47:55 +0000997bool RfbPlayer::waitWhilePaused() {
998 bool result = false;
999 while(isPaused() && !isSeeking()) {
1000 Sleep(20);
1001 result = true;
1002 }
1003 return result;
1004}
1005
george8257f13522005-02-05 08:48:22 +00001006long RfbPlayer::calculateSessionTime(char *filename) {
1007 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001008 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001009 try {
1010 while (TRUE) {
1011 sessionFile.skip(1024);
1012 }
1013 } catch (rdr::Exception e) {
1014 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001015 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001016 } else {
1017 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1018 return 0;
1019 }
1020 }
1021 return 0;
1022}
1023
george826b87aff2005-02-13 10:48:21 +00001024void RfbPlayer::closeSessionFile() {
1025 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001026 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001027 RECT r;
1028
1029 // Uncheck all toolbar buttons
1030 if (tb.getHandle()) {
1031 tb.checkButton(ID_PLAY, false);
1032 tb.checkButton(ID_PAUSE, false);
1033 tb.checkButton(ID_STOP, false);
1034 }
1035
1036 // Stop playback and update the player state
1037 disableTBandMenuItems();
1038 if (rfbReader) {
1039 delete rfbReader->join();
1040 rfbReader = 0;
1041 delete [] fileName;
1042 fileName = 0;
1043 }
1044 blankBuffer();
1045 setTitle("None");
george827009c892005-02-19 12:49:42 +00001046 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001047 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001048 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001049 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1050 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001051 SetWindowText(speedEdit, speedStr);
1052 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1053
1054 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001055 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1056 dwStyle &= ~WS_MAXIMIZE;
1057 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1058 }
george822ff7a612005-02-19 17:05:24 +00001059 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1060 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1061 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001062 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001063 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001064 buffer->setSize(32, 32);
1065 calculateScrollBars();
1066
1067 // Update the cached sizing information and repaint the frame window
1068 GetWindowRect(getFrameHandle(), &r);
1069 window_size = Rect(r.left, r.top, r.right, r.bottom);
1070 GetClientRect(getFrameHandle(), &r);
1071 client_size = Rect(r.left, r.top, r.right, r.bottom);
1072 InvalidateRect(getFrameHandle(), 0, TRUE);
1073 UpdateWindow(getFrameHandle());
1074}
1075
george8217e92cb2005-01-31 16:01:02 +00001076void RfbPlayer::openSessionFile(char *_fileName) {
1077 fileName = strDup(_fileName);
1078
1079 // Close the previous reading thread
1080 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001081 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001082 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001083 }
1084 blankBuffer();
1085 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001086 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001087 rfbReader = new rfbSessionReader(this);
1088 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001089 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001090 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001091}
1092
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001093void RfbPlayer::setPaused(bool paused) {
1094 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001095 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001096 tb.checkButton(ID_PAUSE, true);
1097 tb.checkButton(ID_PLAY, false);
1098 tb.checkButton(ID_STOP, false);
1099 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1100 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001101 } else {
george825beb62a2005-02-09 13:04:32 +00001102 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001103 tb.checkButton(ID_PLAY, true);
1104 tb.checkButton(ID_STOP, false);
1105 tb.checkButton(ID_PAUSE, false);
1106 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1107 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001108 }
1109}
1110
george82006f2792005-02-05 07:40:47 +00001111void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001112 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001113 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001114 if (is) {
1115 is->pausePlayback();
1116 is->interruptFrameDelay();
1117 }
george82006f2792005-02-05 07:40:47 +00001118 tb.checkButton(ID_STOP, true);
1119 tb.checkButton(ID_PLAY, false);
1120 tb.checkButton(ID_PAUSE, false);
1121 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1122 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001123 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001124}
1125
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001126void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001127 if (speed > 0) {
1128 char speedStr[20] = "\0";
1129 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001130 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001131 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001132 SendMessage(speedUpDown, UDM_SETPOS,
1133 0, MAKELONG((short)(newSpeed / 0.5), 0));
1134 sprintf(speedStr, "%.2f", newSpeed);
1135 SetWindowText(speedEdit, speedStr);
1136 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001137}
1138
1139double RfbPlayer::getSpeed() {
1140 return is->getSpeed();
1141}
1142
1143void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001144 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001145}
1146
1147long RfbPlayer::getSeekOffset() {
1148 return is->getSeekOffset();
1149}
1150
1151bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001152 if (is) return is->isSeeking();
1153 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001154}
1155
1156bool RfbPlayer::isSeekMode() {
1157 return seekMode;
1158}
1159
1160bool RfbPlayer::isPaused() {
1161 return is->isPaused();
1162}
1163
1164long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001165 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001166}
1167
george828a471482005-02-06 07:15:53 +00001168void RfbPlayer::updatePos(long newPos) {
1169 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001170 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001171 long time = newPos / 1000;
1172 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001173 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001174
1175 // Update the position of slider
1176 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001177 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1178 sliderStepMs / double(newPos);
1179 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1180 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1181 }
george828a471482005-02-06 07:15:53 +00001182 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001183}
1184
1185void RfbPlayer::skipHandshaking() {
1186 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1187 is->skip(skipBytes);
1188 state_ = RFBSTATE_NORMAL;
1189}
1190
1191void programInfo() {
1192 win32::FileVersionInfo inf;
1193 _tprintf(_T("%s - %s, Version %s\n"),
1194 inf.getVerString(_T("ProductName")),
1195 inf.getVerString(_T("FileDescription")),
1196 inf.getVerString(_T("FileVersion")));
1197 printf("%s\n", buildTime);
1198 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1199}
1200
1201void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001202 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001203}
1204
george825beb62a2005-02-09 13:04:32 +00001205char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001206
1207// playerOptions is the player options with default parameters values,
1208// it is used only for run the player with command-line parameters
1209PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001210bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001211
1212bool processParams(int argc, char* argv[]) {
1213 for (int i = 1; i < argc; i++) {
1214 if ((strcasecmp(argv[i], "-help") == 0) ||
1215 (strcasecmp(argv[i], "--help") == 0) ||
1216 (strcasecmp(argv[i], "/help") == 0) ||
1217 (strcasecmp(argv[i], "-h") == 0) ||
1218 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001219 (strcasecmp(argv[i], "/?") == 0) ||
1220 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001221 print_usage = true;
1222 return true;
1223 }
1224
george825caee412005-03-09 09:52:10 +00001225 if ((strcasecmp(argv[i], "-pf") == 0) ||
1226 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001227 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001228 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001229 return false;
1230 }
george825e7af742005-03-10 14:26:00 +00001231 playerOptions.pixelFormat = pf;
george82193d8e42005-02-20 16:47:01 +00001232 continue;
1233 }
1234
1235
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001236 if ((strcasecmp(argv[i], "-speed") == 0) ||
1237 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001238 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001239 if (playbackSpeed <= 0) {
1240 return false;
1241 }
george825e7af742005-03-10 14:26:00 +00001242 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001243 continue;
1244 }
1245
1246 if ((strcasecmp(argv[i], "-pos") == 0) ||
1247 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001248 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001249 if (initTime <= 0)
1250 return false;
george825e7af742005-03-10 14:26:00 +00001251 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001252 continue;
1253 }
1254
1255 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1256 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001257 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001258 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001259 }
1260
1261 if (i != argc - 1)
1262 return false;
1263 }
1264
1265 fileName = strDup(argv[argc-1]);
1266 return true;
1267}
1268
1269//
1270// -=- WinMain
1271//
1272
1273int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1274
1275 // - Process the command-line
1276
1277 int argc = __argc;
1278 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001279 if ((argc > 1) && (!processParams(argc, argv))) {
1280 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1281 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001282 }
george82e6883de2005-02-08 14:42:12 +00001283
1284 if (print_usage) {
1285 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001286 return 0;
george8267cbcd02005-01-16 15:39:56 +00001287 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001288
george82e6883de2005-02-08 14:42:12 +00001289 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001290 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001291 try {
george825e7af742005-03-10 14:26:00 +00001292 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001293 } catch (rdr::Exception e) {
1294 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1295 delete player;
1296 return 0;
1297 }
1298
1299 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001300 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001301 MSG msg;
1302 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001303 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1304 TranslateMessage(&msg);
1305 DispatchMessage(&msg);
1306 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001307 }
1308
george82e6883de2005-02-08 14:42:12 +00001309 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001310 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001311 if (player) delete player;
1312 } catch (rdr::Exception e) {
1313 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1314 }
1315
1316 return 0;
1317};