blob: b028d637d771cf8d7aedcb579ca4e3affe3a5f97 [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>
george82357c9f52005-03-21 01:28:12 +000027#include <rfb_win32/AboutDialog.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000028
george820981b342005-03-19 11:19:00 +000029#include <rfbplayer/PixelFormatList.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000030#include <rfbplayer/rfbplayer.h>
george82357c9f52005-03-21 01:28:12 +000031
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000032using namespace rfb;
33using namespace rfb::win32;
34
35// -=- Variables & consts
36
37static LogWriter vlog("RfbPlayer");
38
39TStr rfb::win32::AppName("RfbPlayer");
40extern const char* buildTime;
41
george82e6883de2005-02-08 14:42:12 +000042char wrong_cmd_msg[] =
43 "Wrong command-line parameters!\n"
44 "Use for help: rfbplayer -help";
45
46char usage_msg[] =
47 "usage: rfbplayer <options> <filename>\n"
48 "Command-line options:\n"
49 " -help \t- Provide usage information.\n"
george825caee412005-03-09 09:52:10 +000050 " -pf <mode> \t- Forces the pixel format for the session.\n"
51 " \t List of the pixel formats:\n"
52 " \t 0 - Auto,\n"
53 " \t 1 - depth 8 (RGB332),\n"
54 " \t 2 - depth 16 (RGB655),\n"
55 " \t 3 - depth 24 (RGB888).\n"
george82e6883de2005-02-08 14:42:12 +000056 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
57 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
58 " -pos <ms> \t- Sets initial time position in the session file,\n"
59 " \t in milliseconds. Default: 0.\n"
george825e7af742005-03-10 14:26:00 +000060 " -autoplay \t- Runs the player in the playback mode.\n";
george82e6883de2005-02-08 14:42:12 +000061
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000062// -=- RfbPlayer's defines
63
64#define strcasecmp _stricmp
george820981b342005-03-19 11:19:00 +000065#define UPF_REGISTRY_PATH "Software\\TightVnc\\RfbPlayer\\UserDefinedPF"
george825457d412005-02-19 06:43:09 +000066#define MAX_SPEED 10.00
67#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000068#define MAX_POS_TRACKBAR_RANGE 50
george825e7af742005-03-10 14:26:00 +000069#define CTRL_BAR_HEIGHT 28
george8268d25142005-02-13 09:33:22 +000070#define DEFAULT_PLAYER_WIDTH 640
71#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000072
george82d070c692005-01-19 16:44:04 +000073#define ID_TOOLBAR 500
74#define ID_PLAY 510
75#define ID_PAUSE 520
76#define ID_TIME_STATIC 530
77#define ID_SPEED_STATIC 540
78#define ID_SPEED_EDIT 550
79#define ID_POS_TRACKBAR 560
80#define ID_SPEED_UPDOWN 570
81
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000082//
george82357c9f52005-03-21 01:28:12 +000083// -=- AboutDialog global values
84//
85
86const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT;
87const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT;
88const WORD rfb::win32::AboutDialog::Version = IDC_VERSION;
89const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME;
90const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION;
91
92//
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000093// -=- RfbPlayerClass
94
95//
96// Window class used as the basis for RfbPlayer instance
97//
98
99class RfbPlayerClass {
100public:
101 RfbPlayerClass();
102 ~RfbPlayerClass();
103 ATOM classAtom;
104 HINSTANCE instance;
105};
106
107LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
108 LRESULT result;
109
110 if (msg == WM_CREATE)
111 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
112 else if (msg == WM_DESTROY) {
113 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000114 SetWindowLong(hwnd, GWL_USERDATA, 0);
115 }
116 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
117 if (!_this) {
118 vlog.info("null _this in %x, message %u", hwnd, msg);
119 return DefWindowProc(hwnd, msg, wParam, lParam);
120 }
121
122 try {
123 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
124 } catch (rdr::Exception& e) {
125 vlog.error("untrapped: %s", e.str());
126 }
127
128 return result;
129};
130
131RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
132 WNDCLASS wndClass;
133 wndClass.style = 0;
134 wndClass.lpfnWndProc = RfbPlayerProc;
135 wndClass.cbClsExtra = 0;
136 wndClass.cbWndExtra = 0;
137 wndClass.hInstance = instance = GetModuleHandle(0);
138 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000139 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000140 if (!wndClass.hIcon)
141 printf("unable to load icon:%ld", GetLastError());
142 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
143 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000144 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000145 wndClass.lpszClassName = _T("RfbPlayerClass");
146 classAtom = RegisterClass(&wndClass);
147 if (!classAtom) {
148 throw rdr::SystemException("unable to register RfbPlayer window class",
149 GetLastError());
150 }
151}
152
153RfbPlayerClass::~RfbPlayerClass() {
154 if (classAtom) {
155 UnregisterClass((const TCHAR*)classAtom, instance);
156 }
157}
158
159RfbPlayerClass baseClass;
160
161//
162// -=- RfbFrameClass
163
164//
165// Window class used to displaying the rfb data
166//
167
168class RfbFrameClass {
169public:
170 RfbFrameClass();
171 ~RfbFrameClass();
172 ATOM classAtom;
173 HINSTANCE instance;
174};
175
176LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
177 LRESULT result;
178
179 if (msg == WM_CREATE)
180 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
181 else if (msg == WM_DESTROY)
182 SetWindowLong(hwnd, GWL_USERDATA, 0);
183 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
184 if (!_this) {
185 vlog.info("null _this in %x, message %u", hwnd, msg);
186 return DefWindowProc(hwnd, msg, wParam, lParam);
187 }
188
189 try {
190 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
191 } catch (rdr::Exception& e) {
192 vlog.error("untrapped: %s", e.str());
193 }
194
195 return result;
196}
197
198RfbFrameClass::RfbFrameClass() : classAtom(0) {
199 WNDCLASS wndClass;
200 wndClass.style = 0;
201 wndClass.lpfnWndProc = FrameProc;
202 wndClass.cbClsExtra = 0;
203 wndClass.cbWndExtra = 0;
204 wndClass.hInstance = instance = GetModuleHandle(0);
205 wndClass.hIcon = 0;
206 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
207 wndClass.hbrBackground = 0;
208 wndClass.lpszMenuName = 0;
209 wndClass.lpszClassName = _T("RfbPlayerClass1");
210 classAtom = RegisterClass(&wndClass);
211 if (!classAtom) {
212 throw rdr::SystemException("unable to register RfbPlayer window class",
213 GetLastError());
214 }
215}
216
217RfbFrameClass::~RfbFrameClass() {
218 if (classAtom) {
219 UnregisterClass((const TCHAR*)classAtom, instance);
220 }
221}
222
223RfbFrameClass frameClass;
224
225//
226// -=- RfbPlayer instance implementation
227//
228
george825e7af742005-03-10 14:26:00 +0000229RfbPlayer::RfbPlayer(char *_fileName, PlayerOptions *_options)
230: RfbProto(_fileName), fileName(_fileName), buffer(0), client_size(0, 0, 32, 32),
231 window_size(0, 0, 32, 32), cutText(0), seekMode(false), lastPos(0),
232 timeStatic(0), speedEdit(0), posTrackBar(0), speedUpDown(0),
george823104aec2005-02-21 13:20:56 +0000233 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george825e7af742005-03-10 14:26:00 +0000234 imageDataStartTime(0), rewindFlag(false), stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000235
george825e7af742005-03-10 14:26:00 +0000236 // Save the player options
237 memcpy(&options, _options, sizeof(options));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000238
george823c8fbbf2005-01-24 11:09:08 +0000239 // Reset the full session time
240 strcpy(fullSessionTime, "00m:00s");
241
george820981b342005-03-19 11:19:00 +0000242 // Load the user defined pixel formats from the registry
243 supportedPF.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
244
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000245 // Create the main window
246 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000247 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
248 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000249 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000250 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000251 if (!mainHwnd) {
252 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
253 }
254 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
255
256 // Create the backing buffer
257 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000258 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000259
george825e7af742005-03-10 14:26:00 +0000260 // If run with command-line parameters,
261 // open the session file with default settings, otherwise
262 // restore player settings from the registry
george8217e92cb2005-01-31 16:01:02 +0000263 if (fileName) {
264 openSessionFile(fileName);
george825e7af742005-03-10 14:26:00 +0000265 if (options.initTime > 0) setPos(options.initTime);
266 setSpeed(options.playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000267 } else {
george825e7af742005-03-10 14:26:00 +0000268 options.readFromRegistry();
george8263ebbcc2005-02-12 12:09:13 +0000269 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000270 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000271 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000272}
273
274RfbPlayer::~RfbPlayer() {
george825e7af742005-03-10 14:26:00 +0000275 options.writeToRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000276 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000277 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000278 delete rfbReader->join();
279 rfbReader = 0;
280 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000281 if (mainHwnd) {
282 setVisible(false);
283 DestroyWindow(mainHwnd);
284 mainHwnd = 0;
285 }
george825beb62a2005-02-09 13:04:32 +0000286 if (buffer) delete buffer;
287 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000288 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000289 vlog.debug("~RfbPlayer done");
290}
291
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000292LRESULT
293RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
294 switch (msg) {
295
296 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000297
298 case WM_CREATE:
299 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000300 // Create the frame window
301 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
302 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
303 hwnd, 0, frameClass.instance, this);
304
george82d070c692005-01-19 16:44:04 +0000305 createToolBar(hwnd);
306
george82006f2792005-02-05 07:40:47 +0000307 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000308
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000309 return 0;
310 }
311
george827214b822004-12-12 07:02:51 +0000312 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000313
314 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000315 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000316 case ID_OPENFILE:
317 {
318 char curDir[_MAX_DIR];
319 static char filename[_MAX_PATH];
320 OPENFILENAME ofn;
321 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
322 GetCurrentDirectory(sizeof(curDir), curDir);
323
324 ofn.lStructSize = sizeof(OPENFILENAME);
325 ofn.hwndOwner = getMainHandle();
326 ofn.lpstrFile = filename;
327 ofn.nMaxFile = sizeof(filename);
328 ofn.lpstrInitialDir = curDir;
329 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
330 "All files (*.*)\0*.*\0";
331 ofn.lpstrDefExt = "rfb";
332 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000333 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000334 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000335 }
george826e51fcc2005-02-06 13:30:49 +0000336 }
337 break;
george8271ca1772005-02-13 10:50:46 +0000338 case ID_CLOSEFILE:
339 closeSessionFile();
340 break;
george825c13c662005-01-27 14:48:23 +0000341 case ID_PLAY:
342 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000343 break;
344 case ID_PAUSE:
345 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000346 break;
347 case ID_STOP:
348 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000349 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000350 }
george825c13c662005-01-27 14:48:23 +0000351 break;
352 case ID_PLAYPAUSE:
george82bc999f42005-03-19 11:48:19 +0000353 if (rfbReader) {
354 if (isPaused()) {
355 setPaused(false);
356 } else {
357 setPaused(true);
358 }
george825c13c662005-01-27 14:48:23 +0000359 }
george825c13c662005-01-27 14:48:23 +0000360 break;
george827549df42005-02-08 16:31:02 +0000361 case ID_GOTO:
362 {
363 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000364 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000365 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
366 setPos(gotoTime);
367 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000368 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000369 }
370 }
371 break;
george825c13c662005-01-27 14:48:23 +0000372 case ID_FULLSCREEN:
373 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
374 break;
george8231a36332005-02-06 17:27:34 +0000375 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000376 options.loopPlayback = !options.loopPlayback;
377 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000378 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
379 break;
george824ea27f62005-01-29 15:03:06 +0000380 case ID_RETURN:
381 // Update the speed if return pressed in speedEdit
382 if (speedEdit == GetFocus()) {
383 char speedStr[20], *stopStr;
384 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
385 double speed = strtod(speedStr, &stopStr);
386 if (speed > 0) {
387 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000388 } else {
389 speed = getSpeed();
390 }
391 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000392 }
393 break;
george825e7af742005-03-10 14:26:00 +0000394 case ID_OPTIONS:
395 {
george825a6df072005-03-20 09:56:17 +0000396 OptionsDialog optionsDialog(&options, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000397 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000398 }
399 break;
george8201aa6732005-02-06 17:13:03 +0000400 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000401 PostQuitMessage(0);
402 break;
george82ef5f7262005-02-08 15:09:26 +0000403 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000404 MessageBox(getMainHandle(),
405 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
406 break;
george82357c9f52005-03-21 01:28:12 +0000407 case ID_ABOUT:
408 AboutDialog::instance.showDialog();
409 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000410 }
411 break;
412
413 // Update frame's window size and add scrollbars if required
414
415 case WM_SIZE:
416 {
george82d070c692005-01-19 16:44:04 +0000417
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000418 Point old_offset = bufferToClient(Point(0, 0));
419
420 // Update the cached sizing information
421 RECT r;
422 GetClientRect(getMainHandle(), &r);
423 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
424 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
425
426 GetWindowRect(getFrameHandle(), &r);
427 window_size = Rect(r.left, r.top, r.right, r.bottom);
428 GetClientRect(getFrameHandle(), &r);
429 client_size = Rect(r.left, r.top, r.right, r.bottom);
430
431 // Determine whether scrollbars are required
432 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000433
434 // Resize the ToolBar
435 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000436
437 // Redraw if required
438 if (!old_offset.equals(bufferToClient(Point(0, 0))))
439 InvalidateRect(getFrameHandle(), 0, TRUE);
440 }
441 break;
george828a471482005-02-06 07:15:53 +0000442
443 // Process messages from posTrackBar
444
445 case WM_HSCROLL:
446 {
447 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
448 Pos *= sliderStepMs;
449
450 switch (LOWORD(wParam)) {
451 case TB_PAGEUP:
452 case TB_PAGEDOWN:
453 case TB_LINEUP:
454 case TB_LINEDOWN:
455 case TB_THUMBTRACK:
456 sliderDraging = true;
457 updatePos(Pos);
458 return 0;
george82bcc129b2005-03-15 17:11:40 +0000459 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000460 case TB_ENDTRACK:
461 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000462 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000463 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000464 sliderDraging = false;
465 return 0;
466 default:
467 break;
468 }
469 }
470 break;
george829e6e6cc2005-01-29 13:12:05 +0000471
472 case WM_NOTIFY:
473 switch (((NMHDR*)lParam)->code) {
474 case UDN_DELTAPOS:
475 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000476 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000477 char speedStr[20] = "\0";
478 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
479 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
480 double speed;
481
george824ea27f62005-01-29 15:03:06 +0000482 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000483 if (upDown->iDelta > 0) {
484 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
485 } else {
george824ea27f62005-01-29 15:03:06 +0000486 // It's need to round the UpDown position
487 if ((upDown->iPos * 0.5) != getSpeed()) {
488 upDown->iDelta = 0;
489 lResult = TRUE;
490 }
george829e6e6cc2005-01-29 13:12:05 +0000491 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
492 }
george829e6e6cc2005-01-29 13:12:05 +0000493 sprintf(speedStr, "%.2f", speed);
494 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000495 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000496 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000497 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000498 }
george824ea27f62005-01-29 15:03:06 +0000499 }
george829e6e6cc2005-01-29 13:12:05 +0000500 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000501
502 case WM_CLOSE:
503 vlog.debug("WM_CLOSE %x", getMainHandle());
504 PostQuitMessage(0);
505 break;
506 }
507
508 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
509}
510
511LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
512 switch (msg) {
513
514 case WM_PAINT:
515 {
george821e846ff2005-02-24 13:13:33 +0000516 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000517 seekMode = true;
518 return 0;
519 } else {
520 if (seekMode) {
521 seekMode = false;
522 InvalidateRect(getFrameHandle(), 0, true);
523 UpdateWindow(getFrameHandle());
524 return 0;
525 }
526 }
527
528 PAINTSTRUCT ps;
529 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
530 if (!paintDC)
531 throw SystemException("unable to BeginPaint", GetLastError());
532 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
533
534 if (!pr.is_empty()) {
535
536 if (buffer->bitmap) {
537
538 // Get device context
539 BitmapDC bitmapDC(paintDC, buffer->bitmap);
540
541 // Blit the border if required
542 Rect bufpos = bufferToClient(buffer->getRect());
543 if (!pr.enclosed_by(bufpos)) {
544 vlog.debug("draw border");
545 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
546 RECT r;
547 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
548 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
549 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
550 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
551 }
552
553 // Do the blit
554 Point buf_pos = clientToBuffer(pr.tl);
555 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
556 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
557 throw SystemException("unable to BitBlt to window", GetLastError());
558
559 } else {
560 // Blit a load of black
561 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
562 0, 0, 0, BLACKNESS))
563 throw SystemException("unable to BitBlt to blank window", GetLastError());
564 }
565 }
566 EndPaint(getFrameHandle(), &ps);
567 }
568 return 0;
george8203c01da2005-03-16 12:36:53 +0000569
george82aff63ab2005-03-16 13:48:59 +0000570 // Process play/pause by the left mouse button
571 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000572 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
573 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000574
575 case WM_VSCROLL:
576 case WM_HSCROLL:
577 {
578 Point delta;
579 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
580
581 switch (LOWORD(wParam)) {
582 case SB_PAGEUP: newpos -= 50; break;
583 case SB_PAGEDOWN: newpos += 50; break;
584 case SB_LINEUP: newpos -= 5; break;
585 case SB_LINEDOWN: newpos += 5; break;
586 case SB_THUMBTRACK:
587 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
588 default: vlog.info("received unknown scroll message");
589 };
590
591 if (msg == WM_HSCROLL)
592 setViewportOffset(Point(newpos, scrolloffset.y));
593 else
594 setViewportOffset(Point(scrolloffset.x, newpos));
595
596 SCROLLINFO si;
597 si.cbSize = sizeof(si);
598 si.fMask = SIF_POS;
599 si.nPos = newpos;
600 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
601 }
602 break;
603 }
604
605 return DefWindowProc(hwnd, msg, wParam, lParam);
606}
607
george82d070c692005-01-19 16:44:04 +0000608void RfbPlayer::createToolBar(HWND parentHwnd) {
609 RECT tRect;
610 InitCommonControls();
611
612 tb.create(ID_TOOLBAR, parentHwnd);
613 tb.addBitmap(4, IDB_TOOLBAR);
614
615 // Create the control buttons
616 tb.addButton(0, ID_PLAY);
617 tb.addButton(1, ID_PAUSE);
618 tb.addButton(2, ID_STOP);
619 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
620 tb.addButton(3, ID_FULLSCREEN);
621 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
622
623 // Create the static control for the time output
624 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
625 tb.getButtonRect(6, &tRect);
626 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
627 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
628 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
629 GetModuleHandle(0), 0);
630 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
631
632 // Create the trackbar control for the time position
633 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
634 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000635 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000636 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
637 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
638 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
639 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000640 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000641 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
642
643 // Create the label with "Speed:" caption
644 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
645 tb.getButtonRect(10, &tRect);
646 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
647 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
648 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
649
650 // Create the edit control and the spin for the speed managing
651 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
652 tb.getButtonRect(11, &tRect);
653 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
654 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
655 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
656 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
657 // It's need to send notify messages to toolbar parent window
658 SetParent(speedEdit, tb.getHandle());
659
660 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
661 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000662 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000663}
664
george82a21d2952005-02-12 11:30:03 +0000665void RfbPlayer::disableTBandMenuItems() {
666 // Disable the menu items
667 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
668 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
669 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
670 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
671 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
672 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
673 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
674 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
675 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
676
677 // Disable the toolbar buttons and child controls
678 tb.enableButton(ID_PLAY, false);
679 tb.enableButton(ID_PAUSE, false);
680 tb.enableButton(ID_STOP, false);
681 tb.enableButton(ID_FULLSCREEN, false);
682 EnableWindow(posTrackBar, false);
683 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000684 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000685}
686
george82f5043162005-02-12 11:37:18 +0000687void RfbPlayer::enableTBandMenuItems() {
688 // Enable the menu items
689 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
690 EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
691 EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
692 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
693 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
694 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
695 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
696 EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
697 EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
698
699 // Enable the toolbar buttons and child controls
700 tb.enableButton(ID_PLAY, true);
701 tb.enableButton(ID_PAUSE, true);
702 tb.enableButton(ID_STOP, true);
703 tb.enableButton(ID_FULLSCREEN, true);
704 EnableWindow(posTrackBar, true);
705 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000706 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000707}
708
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000709void RfbPlayer::setVisible(bool visible) {
710 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
711 if (visible) {
712 // When the window becomes visible, make it active
713 SetForegroundWindow(getMainHandle());
714 SetActiveWindow(getMainHandle());
715 }
716}
717
718void RfbPlayer::setTitle(const char *title) {
719 char _title[256];
720 strcpy(_title, AppName);
721 strcat(_title, " - ");
722 strcat(_title, title);
723 SetWindowText(getMainHandle(), _title);
724}
725
726void RfbPlayer::setFrameSize(int width, int height) {
727 // Calculate and set required size for main window
728 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000729 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000730 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
731 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
732 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000733 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
734 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
735 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000736 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
737
738 // Enable/disable scrollbars as appropriate
739 calculateScrollBars();
740}
741
742void RfbPlayer::calculateScrollBars() {
743 // Calculate the required size of window
744 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
745 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
746 DWORD old_style;
747 RECT r;
748 SetRect(&r, 0, 0, buffer->width(), buffer->height());
749 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
750 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
751
752 // Work out whether scroll bars are required
753 do {
754 old_style = style;
755
756 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
757 style |= WS_HSCROLL;
758 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
759 }
760 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
761 style |= WS_VSCROLL;
762 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
763 }
764 } while (style != old_style);
765
766 // Tell Windows to update the window style & cached settings
767 if (style != current_style) {
768 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
769 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
770 }
771
772 // Update the scroll settings
773 SCROLLINFO si;
774 if (style & WS_VSCROLL) {
775 si.cbSize = sizeof(si);
776 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
777 si.nMin = 0;
778 si.nMax = buffer->height();
779 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
780 maxscrolloffset.y = max(0, si.nMax-si.nPage);
781 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
782 si.nPos = scrolloffset.y;
783 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
784 }
785 if (style & WS_HSCROLL) {
786 si.cbSize = sizeof(si);
787 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
788 si.nMin = 0;
789 si.nMax = buffer->width();
790 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
791 maxscrolloffset.x = max(0, si.nMax-si.nPage);
792 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
793 si.nPos = scrolloffset.x;
794 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
795 }
george82a758a7a2005-03-15 16:34:57 +0000796
797 // Update the cached client size
798 GetClientRect(getFrameHandle(), &r);
799 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000800}
801
802bool RfbPlayer::setViewportOffset(const Point& tl) {
803/* ***
804 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
805 max(0, min(maxscrolloffset.y, tl.y)));
806 */
807 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
808 max(0, min(tl.y, buffer->height()-client_size.height())));
809 Point delta = np.translate(scrolloffset.negate());
810 if (!np.equals(scrolloffset)) {
811 scrolloffset = np;
812 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
813 UpdateWindow(getFrameHandle());
814 return true;
815 }
816 return false;
817}
818
819void RfbPlayer::close(const char* reason) {
820 setVisible(false);
821 if (reason) {
822 vlog.info("closing - %s", reason);
823 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
824 }
825 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
826}
827
828void RfbPlayer::blankBuffer() {
829 fillRect(buffer->getRect(), 0);
830}
831
832void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000833 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000834 blankBuffer();
835 newSession(fileName);
836 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000837 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000838 if (paused) is->pausePlayback();
839 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000840}
841
842void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000843 // Perform return if waitWhilePaused processed because
844 // rfbReader thread could receive the signal to close
845 if (waitWhilePaused()) return;
846
george8223e08562005-01-31 15:16:42 +0000847 static long update_time = GetTickCount();
848 try {
george828a471482005-02-06 07:15:53 +0000849 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
850 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000851 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000852 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000853 update_time = GetTickCount();
854 }
855 RfbProto::processMsg();
856 } catch (rdr::Exception e) {
857 if (strcmp(e.str(), "[End Of File]") == 0) {
858 rewind();
george825e7af742005-03-10 14:26:00 +0000859 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000860 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000861 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000862 return;
863 }
864 // It's a special exception to perform backward seeking.
865 // We only rewind the stream and seek the offset
866 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000867 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000868 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000869 rewind();
george820d2e19d2005-03-03 15:47:55 +0000870 if (!stopped) setPos(seekOffset);
871 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000872 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000873 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000874 return;
875 }
876 // It's a special exception which is used to terminate the playback
877 if (strcmp(e.str(), "[TERMINATE]") == 0) {
878 sessionTerminateThread *terminate = new sessionTerminateThread(this);
879 terminate->start();
george8223e08562005-01-31 15:16:42 +0000880 } else {
george820e980cc2005-03-10 18:18:34 +0000881 // Show the exception message and close the session playback
882 is->pausePlayback();
883 char message[256] = "\0";
884 strcat(message, e.str());
885 strcat(message, "\nMaybe you force wrong the pixel format for this session");
886 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
887 sessionTerminateThread *terminate = new sessionTerminateThread(this);
888 terminate->start();
george8223e08562005-01-31 15:16:42 +0000889 return;
890 }
891 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000892}
893
george82c7e9f792005-03-20 12:52:46 +0000894long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
895bool ChoosePixelFormatDialog::bigEndian = false;
896
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000897void RfbPlayer::serverInit() {
898 RfbProto::serverInit();
899
george82b95503e2005-02-21 17:02:34 +0000900 // Save the image data start time
901 imageDataStartTime = is->getTimeOffset();
902
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000903 // Resize the backing buffer
904 buffer->setSize(cp.width, cp.height);
905
906 // Check on the true colour mode
907 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000908 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000909
910 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000911 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000912 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000913 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000914 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000915 if (pixelFormatIndex < 0) {
916 options.autoDetectPF = true;
917 options.setPF((PixelFormat *)&cp.pf());
918 } else {
919 options.autoDetectPF = false;
920 options.setPF(&supportedPF[pixelFormatIndex].PF);
george82c7e9f792005-03-20 12:52:46 +0000921 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000922 }
george822c7634b2005-03-10 18:03:27 +0000923 } else {
924 is->pausePlayback();
925 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000926 }
927 } else {
george825a6df072005-03-20 09:56:17 +0000928 if (options.autoDetectPF) {
929 options.setPF((PixelFormat *)&cp.pf());
930 } else {
931 options.setPF(&supportedPF[options.pixelFormatIndex].PF);
george82c7e9f792005-03-20 12:52:46 +0000932 options.pixelFormat.bigEndian = options.bigEndianFlag;
george825a6df072005-03-20 09:56:17 +0000933 }
george825e7af742005-03-10 14:26:00 +0000934 }
george825a6df072005-03-20 09:56:17 +0000935 cp.setPF(options.pixelFormat);
936 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000937
938 // If the window is not maximised then resize it
939 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
940 setFrameSize(cp.width, cp.height);
941
942 // Set the window title and show it
943 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000944
george82d4d69e62005-02-05 09:23:18 +0000945 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000946 sessionTimeMs = calculateSessionTime(fileName);
947 sprintf(fullSessionTime, "%.2um:%.2us",
948 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000949 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000950 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
951 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000952 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000953
george825e7af742005-03-10 14:26:00 +0000954 setPaused(!options.autoPlay);
955 // Restore the parameters from registry,
956 // which was replaced by command-line parameters.
957 options.readFromRegistry();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000958}
959
960void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
961 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
962 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
963/* int i;
964 for (i=0;i<count;i++) {
965 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
966 }
967 // *** change to 0, 256?
968 refreshWindowPalette(first, count);
969 palette_changed = true;
970 InvalidateRect(getFrameHandle(), 0, FALSE);*/
971}
972
973void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000974 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000975 MessageBeep(-1);
976}
977
978void RfbPlayer::serverCutText(const char* str, int len) {
979 if (cutText != NULL)
980 delete [] cutText;
981 cutText = new char[len + 1];
982 memcpy(cutText, str, len);
983 cutText[len] = '\0';
984}
985
986void RfbPlayer::frameBufferUpdateEnd() {
987};
988
989void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
990}
991
992void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
993}
994
995
996void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
997 buffer->fillRect(r, pix);
998 invalidateBufferRect(r);
999}
1000
1001void RfbPlayer::imageRect(const Rect& r, void* pixels) {
1002 buffer->imageRect(r, pixels);
1003 invalidateBufferRect(r);
1004}
1005
1006void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
1007 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1008 invalidateBufferRect(r);
1009}
1010
1011bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
1012 Rect rect = bufferToClient(crect);
1013 if (rect.intersect(client_size).is_empty()) return false;
1014 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
1015 InvalidateRect(getFrameHandle(), &invalid, FALSE);
1016 return true;
1017}
1018
george820d2e19d2005-03-03 15:47:55 +00001019bool RfbPlayer::waitWhilePaused() {
1020 bool result = false;
1021 while(isPaused() && !isSeeking()) {
1022 Sleep(20);
1023 result = true;
1024 }
1025 return result;
1026}
1027
george8257f13522005-02-05 08:48:22 +00001028long RfbPlayer::calculateSessionTime(char *filename) {
1029 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001030 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001031 try {
1032 while (TRUE) {
1033 sessionFile.skip(1024);
1034 }
1035 } catch (rdr::Exception e) {
1036 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001037 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001038 } else {
1039 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1040 return 0;
1041 }
1042 }
1043 return 0;
1044}
1045
george826b87aff2005-02-13 10:48:21 +00001046void RfbPlayer::closeSessionFile() {
1047 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001048 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001049 RECT r;
1050
1051 // Uncheck all toolbar buttons
1052 if (tb.getHandle()) {
1053 tb.checkButton(ID_PLAY, false);
1054 tb.checkButton(ID_PAUSE, false);
1055 tb.checkButton(ID_STOP, false);
1056 }
1057
1058 // Stop playback and update the player state
1059 disableTBandMenuItems();
1060 if (rfbReader) {
1061 delete rfbReader->join();
1062 rfbReader = 0;
1063 delete [] fileName;
1064 fileName = 0;
1065 }
1066 blankBuffer();
1067 setTitle("None");
george827009c892005-02-19 12:49:42 +00001068 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001069 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001070 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001071 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1072 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001073 SetWindowText(speedEdit, speedStr);
1074 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1075
1076 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001077 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1078 dwStyle &= ~WS_MAXIMIZE;
1079 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1080 }
george822ff7a612005-02-19 17:05:24 +00001081 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1082 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1083 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001084 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001085 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001086 buffer->setSize(32, 32);
1087 calculateScrollBars();
1088
1089 // Update the cached sizing information and repaint the frame window
1090 GetWindowRect(getFrameHandle(), &r);
1091 window_size = Rect(r.left, r.top, r.right, r.bottom);
1092 GetClientRect(getFrameHandle(), &r);
1093 client_size = Rect(r.left, r.top, r.right, r.bottom);
1094 InvalidateRect(getFrameHandle(), 0, TRUE);
1095 UpdateWindow(getFrameHandle());
1096}
1097
george8217e92cb2005-01-31 16:01:02 +00001098void RfbPlayer::openSessionFile(char *_fileName) {
1099 fileName = strDup(_fileName);
1100
1101 // Close the previous reading thread
1102 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001103 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001104 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001105 }
1106 blankBuffer();
1107 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001108 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001109 rfbReader = new rfbSessionReader(this);
1110 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001111 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001112 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001113}
1114
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001115void RfbPlayer::setPaused(bool paused) {
1116 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001117 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001118 tb.checkButton(ID_PAUSE, true);
1119 tb.checkButton(ID_PLAY, false);
1120 tb.checkButton(ID_STOP, false);
1121 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1122 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001123 } else {
george825beb62a2005-02-09 13:04:32 +00001124 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001125 tb.checkButton(ID_PLAY, true);
1126 tb.checkButton(ID_STOP, false);
1127 tb.checkButton(ID_PAUSE, false);
1128 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1129 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001130 }
1131}
1132
george82006f2792005-02-05 07:40:47 +00001133void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001134 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001135 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001136 if (is) {
1137 is->pausePlayback();
1138 is->interruptFrameDelay();
1139 }
george82006f2792005-02-05 07:40:47 +00001140 tb.checkButton(ID_STOP, true);
1141 tb.checkButton(ID_PLAY, false);
1142 tb.checkButton(ID_PAUSE, false);
1143 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1144 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001145 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001146}
1147
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001148void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001149 if (speed > 0) {
1150 char speedStr[20] = "\0";
1151 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001152 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001153 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001154 SendMessage(speedUpDown, UDM_SETPOS,
1155 0, MAKELONG((short)(newSpeed / 0.5), 0));
1156 sprintf(speedStr, "%.2f", newSpeed);
1157 SetWindowText(speedEdit, speedStr);
1158 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001159}
1160
1161double RfbPlayer::getSpeed() {
1162 return is->getSpeed();
1163}
1164
1165void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001166 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001167}
1168
1169long RfbPlayer::getSeekOffset() {
1170 return is->getSeekOffset();
1171}
1172
1173bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001174 if (is) return is->isSeeking();
1175 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001176}
1177
1178bool RfbPlayer::isSeekMode() {
1179 return seekMode;
1180}
1181
1182bool RfbPlayer::isPaused() {
1183 return is->isPaused();
1184}
1185
1186long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001187 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001188}
1189
george828a471482005-02-06 07:15:53 +00001190void RfbPlayer::updatePos(long newPos) {
1191 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001192 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001193 long time = newPos / 1000;
1194 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001195 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001196
1197 // Update the position of slider
1198 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001199 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1200 sliderStepMs / double(newPos);
1201 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1202 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1203 }
george828a471482005-02-06 07:15:53 +00001204 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001205}
1206
1207void RfbPlayer::skipHandshaking() {
1208 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1209 is->skip(skipBytes);
1210 state_ = RFBSTATE_NORMAL;
1211}
1212
1213void programInfo() {
1214 win32::FileVersionInfo inf;
1215 _tprintf(_T("%s - %s, Version %s\n"),
1216 inf.getVerString(_T("ProductName")),
1217 inf.getVerString(_T("FileDescription")),
1218 inf.getVerString(_T("FileVersion")));
1219 printf("%s\n", buildTime);
1220 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1221}
1222
1223void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001224 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001225}
1226
george825beb62a2005-02-09 13:04:32 +00001227char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001228
1229// playerOptions is the player options with default parameters values,
1230// it is used only for run the player with command-line parameters
1231PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001232bool print_usage = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001233
1234bool processParams(int argc, char* argv[]) {
1235 for (int i = 1; i < argc; i++) {
1236 if ((strcasecmp(argv[i], "-help") == 0) ||
1237 (strcasecmp(argv[i], "--help") == 0) ||
1238 (strcasecmp(argv[i], "/help") == 0) ||
1239 (strcasecmp(argv[i], "-h") == 0) ||
1240 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001241 (strcasecmp(argv[i], "/?") == 0) ||
1242 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001243 print_usage = true;
1244 return true;
1245 }
1246
george825caee412005-03-09 09:52:10 +00001247 if ((strcasecmp(argv[i], "-pf") == 0) ||
1248 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001249 long pf = atoi(argv[++i]);
george825caee412005-03-09 09:52:10 +00001250 if ((pf < 0) || (pf > PF_MODES)) {
george82193d8e42005-02-20 16:47:01 +00001251 return false;
1252 }
george820981b342005-03-19 11:19:00 +00001253 playerOptions.pixelFormatIndex = pf;
george82193d8e42005-02-20 16:47:01 +00001254 continue;
1255 }
1256
1257
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001258 if ((strcasecmp(argv[i], "-speed") == 0) ||
1259 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001260 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001261 if (playbackSpeed <= 0) {
1262 return false;
1263 }
george825e7af742005-03-10 14:26:00 +00001264 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001265 continue;
1266 }
1267
1268 if ((strcasecmp(argv[i], "-pos") == 0) ||
1269 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001270 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001271 if (initTime <= 0)
1272 return false;
george825e7af742005-03-10 14:26:00 +00001273 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001274 continue;
1275 }
1276
1277 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1278 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001279 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001280 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001281 }
1282
1283 if (i != argc - 1)
1284 return false;
1285 }
1286
1287 fileName = strDup(argv[argc-1]);
1288 return true;
1289}
1290
1291//
1292// -=- WinMain
1293//
1294
1295int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1296
1297 // - Process the command-line
1298
1299 int argc = __argc;
1300 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001301 if ((argc > 1) && (!processParams(argc, argv))) {
1302 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1303 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001304 }
george82e6883de2005-02-08 14:42:12 +00001305
1306 if (print_usage) {
1307 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001308 return 0;
george8267cbcd02005-01-16 15:39:56 +00001309 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001310
george82e6883de2005-02-08 14:42:12 +00001311 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001312 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001313 try {
george825e7af742005-03-10 14:26:00 +00001314 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001315 } catch (rdr::Exception e) {
1316 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1317 delete player;
1318 return 0;
1319 }
1320
1321 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001322 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001323 MSG msg;
1324 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001325 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1326 TranslateMessage(&msg);
1327 DispatchMessage(&msg);
1328 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001329 }
1330
george82e6883de2005-02-08 14:42:12 +00001331 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001332 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001333 if (player) delete player;
1334 } catch (rdr::Exception e) {
1335 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1336 }
1337
1338 return 0;
1339};