blob: 149acd58dfa4e9091c0a89a8f77dbcd2834c6b6d [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[] =
george820978ddf2005-03-28 15:53:45 +000047 "usage: rfbplayer <options> <filename>\r\n"
48 "Command-line options:\r\n"
49 " -help \t- Provide usage information.\r\n"
50 " -pf <mode> \t- Forces the pixel format for the session.\r\n"
51 " \t <mode>=r<r_bits>g<g_bits>b<b_bits>[le|be],\r\n"
52 " \t r_bits - size the red component, in bits,\r\n"
53 " \t g_bits - size the green component, in bits,\r\n"
54 " \t b_bits - size the blue component, in bits,\r\n"
55 " \t le - little endian byte order (default),\r\n"
56 " \t be - big endian byte order.\r\n"
57 " \t The r, g, b component is in any order.\r\n"
58 " \t Default: auto detect the pixel format.\r\n"
59 " -upf <name> \t- Forces the user defined pixel format for\r\n"
60 " \t the session. If <name> is empty then application\r\n"
61 " \t shows the list of user defined pixel formats.\r\n"
62 " \t Don't use this option with -pf.\r\n"
63 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\r\n"
64 " \t is double speed, 0.5 is half speed. Default: 1.0.\r\n"
65 " -pos <ms> \t- Sets initial time position in the session file,\r\n"
66 " \t in milliseconds. Default: 0.\r\n"
67 " -autoplay \t- Runs the player in the playback mode.\r\n";
george82e6883de2005-02-08 14:42:12 +000068
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000069// -=- RfbPlayer's defines
70
71#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000072#define MAX_SPEED 10.00
73#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000074#define MAX_POS_TRACKBAR_RANGE 50
george825e7af742005-03-10 14:26:00 +000075#define CTRL_BAR_HEIGHT 28
george8268d25142005-02-13 09:33:22 +000076#define DEFAULT_PLAYER_WIDTH 640
77#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000078
george82d070c692005-01-19 16:44:04 +000079#define ID_TOOLBAR 500
80#define ID_PLAY 510
81#define ID_PAUSE 520
82#define ID_TIME_STATIC 530
83#define ID_SPEED_STATIC 540
84#define ID_SPEED_EDIT 550
85#define ID_POS_TRACKBAR 560
86#define ID_SPEED_UPDOWN 570
87
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000088//
george82357c9f52005-03-21 01:28:12 +000089// -=- AboutDialog global values
90//
91
92const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT;
93const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT;
94const WORD rfb::win32::AboutDialog::Version = IDC_VERSION;
95const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME;
96const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION;
97
98//
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000099// -=- RfbPlayerClass
100
101//
102// Window class used as the basis for RfbPlayer instance
103//
104
105class RfbPlayerClass {
106public:
107 RfbPlayerClass();
108 ~RfbPlayerClass();
109 ATOM classAtom;
110 HINSTANCE instance;
111};
112
113LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
114 LRESULT result;
115
116 if (msg == WM_CREATE)
117 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
118 else if (msg == WM_DESTROY) {
119 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000120 SetWindowLong(hwnd, GWL_USERDATA, 0);
121 }
122 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
123 if (!_this) {
124 vlog.info("null _this in %x, message %u", hwnd, msg);
125 return DefWindowProc(hwnd, msg, wParam, lParam);
126 }
127
128 try {
129 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
130 } catch (rdr::Exception& e) {
131 vlog.error("untrapped: %s", e.str());
132 }
133
134 return result;
135};
136
137RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
138 WNDCLASS wndClass;
139 wndClass.style = 0;
140 wndClass.lpfnWndProc = RfbPlayerProc;
141 wndClass.cbClsExtra = 0;
142 wndClass.cbWndExtra = 0;
143 wndClass.hInstance = instance = GetModuleHandle(0);
144 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000145 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000146 if (!wndClass.hIcon)
147 printf("unable to load icon:%ld", GetLastError());
148 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
149 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000150 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000151 wndClass.lpszClassName = _T("RfbPlayerClass");
152 classAtom = RegisterClass(&wndClass);
153 if (!classAtom) {
154 throw rdr::SystemException("unable to register RfbPlayer window class",
155 GetLastError());
156 }
157}
158
159RfbPlayerClass::~RfbPlayerClass() {
160 if (classAtom) {
161 UnregisterClass((const TCHAR*)classAtom, instance);
162 }
163}
164
165RfbPlayerClass baseClass;
166
167//
168// -=- RfbFrameClass
169
170//
171// Window class used to displaying the rfb data
172//
173
174class RfbFrameClass {
175public:
176 RfbFrameClass();
177 ~RfbFrameClass();
178 ATOM classAtom;
179 HINSTANCE instance;
180};
181
182LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
183 LRESULT result;
184
185 if (msg == WM_CREATE)
186 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
187 else if (msg == WM_DESTROY)
188 SetWindowLong(hwnd, GWL_USERDATA, 0);
189 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
190 if (!_this) {
191 vlog.info("null _this in %x, message %u", hwnd, msg);
192 return DefWindowProc(hwnd, msg, wParam, lParam);
193 }
194
195 try {
196 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
197 } catch (rdr::Exception& e) {
198 vlog.error("untrapped: %s", e.str());
199 }
200
201 return result;
202}
203
204RfbFrameClass::RfbFrameClass() : classAtom(0) {
205 WNDCLASS wndClass;
206 wndClass.style = 0;
207 wndClass.lpfnWndProc = FrameProc;
208 wndClass.cbClsExtra = 0;
209 wndClass.cbWndExtra = 0;
210 wndClass.hInstance = instance = GetModuleHandle(0);
211 wndClass.hIcon = 0;
212 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
213 wndClass.hbrBackground = 0;
214 wndClass.lpszMenuName = 0;
215 wndClass.lpszClassName = _T("RfbPlayerClass1");
216 classAtom = RegisterClass(&wndClass);
217 if (!classAtom) {
218 throw rdr::SystemException("unable to register RfbPlayer window class",
219 GetLastError());
220 }
221}
222
223RfbFrameClass::~RfbFrameClass() {
224 if (classAtom) {
225 UnregisterClass((const TCHAR*)classAtom, instance);
226 }
227}
228
229RfbFrameClass frameClass;
230
231//
232// -=- RfbPlayer instance implementation
233//
234
george825e7af742005-03-10 14:26:00 +0000235RfbPlayer::RfbPlayer(char *_fileName, PlayerOptions *_options)
236: RfbProto(_fileName), fileName(_fileName), buffer(0), client_size(0, 0, 32, 32),
237 window_size(0, 0, 32, 32), cutText(0), seekMode(false), lastPos(0),
238 timeStatic(0), speedEdit(0), posTrackBar(0), speedUpDown(0),
george823104aec2005-02-21 13:20:56 +0000239 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george825e7af742005-03-10 14:26:00 +0000240 imageDataStartTime(0), rewindFlag(false), stopped(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000241
george825e7af742005-03-10 14:26:00 +0000242 // Save the player options
243 memcpy(&options, _options, sizeof(options));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000244
george823c8fbbf2005-01-24 11:09:08 +0000245 // Reset the full session time
246 strcpy(fullSessionTime, "00m:00s");
247
george820981b342005-03-19 11:19:00 +0000248 // Load the user defined pixel formats from the registry
249 supportedPF.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
250
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000251 // Create the main window
252 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000253 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
254 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000255 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000256 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000257 if (!mainHwnd) {
258 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
259 }
260 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
261
262 // Create the backing buffer
263 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000264 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000265
george825e7af742005-03-10 14:26:00 +0000266 // If run with command-line parameters,
267 // open the session file with default settings, otherwise
268 // restore player settings from the registry
george8217e92cb2005-01-31 16:01:02 +0000269 if (fileName) {
270 openSessionFile(fileName);
george825e7af742005-03-10 14:26:00 +0000271 if (options.initTime > 0) setPos(options.initTime);
272 setSpeed(options.playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000273 } else {
george825e7af742005-03-10 14:26:00 +0000274 options.readFromRegistry();
george8263ebbcc2005-02-12 12:09:13 +0000275 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000276 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000277 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000278}
279
280RfbPlayer::~RfbPlayer() {
281 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000282 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000283 delete rfbReader->join();
284 rfbReader = 0;
285 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000286 if (mainHwnd) {
287 setVisible(false);
288 DestroyWindow(mainHwnd);
289 mainHwnd = 0;
290 }
george825beb62a2005-02-09 13:04:32 +0000291 if (buffer) delete buffer;
292 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000293 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000294 vlog.debug("~RfbPlayer done");
295}
296
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000297LRESULT
298RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
299 switch (msg) {
300
301 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000302
303 case WM_CREATE:
304 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000305 // Create the frame window
306 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
307 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
308 hwnd, 0, frameClass.instance, this);
309
george82d070c692005-01-19 16:44:04 +0000310 createToolBar(hwnd);
311
george82006f2792005-02-05 07:40:47 +0000312 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000313
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000314 return 0;
315 }
316
george827214b822004-12-12 07:02:51 +0000317 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000318
319 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000320 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000321 case ID_OPENFILE:
322 {
323 char curDir[_MAX_DIR];
324 static char filename[_MAX_PATH];
325 OPENFILENAME ofn;
326 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
327 GetCurrentDirectory(sizeof(curDir), curDir);
328
329 ofn.lStructSize = sizeof(OPENFILENAME);
330 ofn.hwndOwner = getMainHandle();
331 ofn.lpstrFile = filename;
332 ofn.nMaxFile = sizeof(filename);
333 ofn.lpstrInitialDir = curDir;
334 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
335 "All files (*.*)\0*.*\0";
336 ofn.lpstrDefExt = "rfb";
337 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000338 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000339 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000340 }
george826e51fcc2005-02-06 13:30:49 +0000341 }
342 break;
george8271ca1772005-02-13 10:50:46 +0000343 case ID_CLOSEFILE:
344 closeSessionFile();
345 break;
george825c13c662005-01-27 14:48:23 +0000346 case ID_PLAY:
347 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000348 break;
349 case ID_PAUSE:
350 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000351 break;
352 case ID_STOP:
353 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000354 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000355 }
george825c13c662005-01-27 14:48:23 +0000356 break;
357 case ID_PLAYPAUSE:
george82bc999f42005-03-19 11:48:19 +0000358 if (rfbReader) {
359 if (isPaused()) {
360 setPaused(false);
361 } else {
362 setPaused(true);
363 }
george825c13c662005-01-27 14:48:23 +0000364 }
george825c13c662005-01-27 14:48:23 +0000365 break;
george827549df42005-02-08 16:31:02 +0000366 case ID_GOTO:
367 {
368 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000369 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000370 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
371 setPos(gotoTime);
372 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000373 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000374 }
375 }
376 break;
george825c13c662005-01-27 14:48:23 +0000377 case ID_FULLSCREEN:
378 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
379 break;
george8231a36332005-02-06 17:27:34 +0000380 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000381 options.loopPlayback = !options.loopPlayback;
382 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000383 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
384 break;
george824ea27f62005-01-29 15:03:06 +0000385 case ID_RETURN:
386 // Update the speed if return pressed in speedEdit
387 if (speedEdit == GetFocus()) {
388 char speedStr[20], *stopStr;
389 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
390 double speed = strtod(speedStr, &stopStr);
391 if (speed > 0) {
392 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000393 } else {
394 speed = getSpeed();
395 }
396 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000397 }
398 break;
george825e7af742005-03-10 14:26:00 +0000399 case ID_OPTIONS:
400 {
george825a6df072005-03-20 09:56:17 +0000401 OptionsDialog optionsDialog(&options, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000402 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000403 }
404 break;
george8201aa6732005-02-06 17:13:03 +0000405 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000406 PostQuitMessage(0);
407 break;
george8245699e22005-03-29 12:08:52 +0000408 case ID_HOMEPAGE:
george828c72fe72005-03-29 15:05:57 +0000409 ShellExecute(getMainHandle(), _T("open"), _T("http://www.tightvnc.com/"),
410 NULL, NULL, SW_SHOWDEFAULT);
george8245699e22005-03-29 12:08:52 +0000411 break;
george82ef5f7262005-02-08 15:09:26 +0000412 case ID_HELP_COMMANDLINESWITCHES:
george820978ddf2005-03-28 15:53:45 +0000413 {
414 InfoDialog usageDialog(usage_msg);
george82ff5b29f2005-03-29 11:59:35 +0000415 usageDialog.showDialog(getMainHandle());
george820978ddf2005-03-28 15:53:45 +0000416 }
george8259f84532005-02-08 15:01:39 +0000417 break;
george82357c9f52005-03-21 01:28:12 +0000418 case ID_ABOUT:
419 AboutDialog::instance.showDialog();
420 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000421 }
422 break;
423
424 // Update frame's window size and add scrollbars if required
425
426 case WM_SIZE:
427 {
george82d070c692005-01-19 16:44:04 +0000428
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000429 Point old_offset = bufferToClient(Point(0, 0));
430
431 // Update the cached sizing information
432 RECT r;
433 GetClientRect(getMainHandle(), &r);
434 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
435 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
436
437 GetWindowRect(getFrameHandle(), &r);
438 window_size = Rect(r.left, r.top, r.right, r.bottom);
439 GetClientRect(getFrameHandle(), &r);
440 client_size = Rect(r.left, r.top, r.right, r.bottom);
441
442 // Determine whether scrollbars are required
443 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000444
445 // Resize the ToolBar
446 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000447
448 // Redraw if required
449 if (!old_offset.equals(bufferToClient(Point(0, 0))))
450 InvalidateRect(getFrameHandle(), 0, TRUE);
451 }
452 break;
george828a471482005-02-06 07:15:53 +0000453
454 // Process messages from posTrackBar
455
456 case WM_HSCROLL:
457 {
458 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
459 Pos *= sliderStepMs;
460
461 switch (LOWORD(wParam)) {
462 case TB_PAGEUP:
463 case TB_PAGEDOWN:
464 case TB_LINEUP:
465 case TB_LINEDOWN:
466 case TB_THUMBTRACK:
467 sliderDraging = true;
468 updatePos(Pos);
469 return 0;
george82bcc129b2005-03-15 17:11:40 +0000470 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000471 case TB_ENDTRACK:
472 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000473 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000474 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000475 sliderDraging = false;
476 return 0;
477 default:
478 break;
479 }
480 }
481 break;
george829e6e6cc2005-01-29 13:12:05 +0000482
483 case WM_NOTIFY:
484 switch (((NMHDR*)lParam)->code) {
485 case UDN_DELTAPOS:
486 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000487 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000488 char speedStr[20] = "\0";
489 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
490 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
491 double speed;
492
george824ea27f62005-01-29 15:03:06 +0000493 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000494 if (upDown->iDelta > 0) {
495 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
496 } else {
george824ea27f62005-01-29 15:03:06 +0000497 // It's need to round the UpDown position
498 if ((upDown->iPos * 0.5) != getSpeed()) {
499 upDown->iDelta = 0;
500 lResult = TRUE;
501 }
george829e6e6cc2005-01-29 13:12:05 +0000502 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
503 }
george829e6e6cc2005-01-29 13:12:05 +0000504 sprintf(speedStr, "%.2f", speed);
505 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000506 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000507 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000508 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000509 }
george824ea27f62005-01-29 15:03:06 +0000510 }
george829e6e6cc2005-01-29 13:12:05 +0000511 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000512
513 case WM_CLOSE:
514 vlog.debug("WM_CLOSE %x", getMainHandle());
515 PostQuitMessage(0);
516 break;
517 }
518
519 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
520}
521
522LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
523 switch (msg) {
524
525 case WM_PAINT:
526 {
george821e846ff2005-02-24 13:13:33 +0000527 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000528 seekMode = true;
529 return 0;
530 } else {
531 if (seekMode) {
532 seekMode = false;
533 InvalidateRect(getFrameHandle(), 0, true);
534 UpdateWindow(getFrameHandle());
535 return 0;
536 }
537 }
538
539 PAINTSTRUCT ps;
540 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
541 if (!paintDC)
542 throw SystemException("unable to BeginPaint", GetLastError());
543 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
544
545 if (!pr.is_empty()) {
546
547 if (buffer->bitmap) {
548
549 // Get device context
550 BitmapDC bitmapDC(paintDC, buffer->bitmap);
551
552 // Blit the border if required
553 Rect bufpos = bufferToClient(buffer->getRect());
554 if (!pr.enclosed_by(bufpos)) {
555 vlog.debug("draw border");
556 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
557 RECT r;
558 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
559 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
560 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
561 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
562 }
563
564 // Do the blit
565 Point buf_pos = clientToBuffer(pr.tl);
566 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
567 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
568 throw SystemException("unable to BitBlt to window", GetLastError());
569
570 } else {
571 // Blit a load of black
572 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
573 0, 0, 0, BLACKNESS))
574 throw SystemException("unable to BitBlt to blank window", GetLastError());
575 }
576 }
577 EndPaint(getFrameHandle(), &ps);
578 }
579 return 0;
george8203c01da2005-03-16 12:36:53 +0000580
george82aff63ab2005-03-16 13:48:59 +0000581 // Process play/pause by the left mouse button
582 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000583 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
584 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000585
586 case WM_VSCROLL:
587 case WM_HSCROLL:
588 {
589 Point delta;
590 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
591
592 switch (LOWORD(wParam)) {
593 case SB_PAGEUP: newpos -= 50; break;
594 case SB_PAGEDOWN: newpos += 50; break;
595 case SB_LINEUP: newpos -= 5; break;
596 case SB_LINEDOWN: newpos += 5; break;
597 case SB_THUMBTRACK:
598 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
599 default: vlog.info("received unknown scroll message");
600 };
601
602 if (msg == WM_HSCROLL)
603 setViewportOffset(Point(newpos, scrolloffset.y));
604 else
605 setViewportOffset(Point(scrolloffset.x, newpos));
606
607 SCROLLINFO si;
608 si.cbSize = sizeof(si);
609 si.fMask = SIF_POS;
610 si.nPos = newpos;
611 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
612 }
613 break;
614 }
615
616 return DefWindowProc(hwnd, msg, wParam, lParam);
617}
618
george82d070c692005-01-19 16:44:04 +0000619void RfbPlayer::createToolBar(HWND parentHwnd) {
620 RECT tRect;
621 InitCommonControls();
622
623 tb.create(ID_TOOLBAR, parentHwnd);
624 tb.addBitmap(4, IDB_TOOLBAR);
625
626 // Create the control buttons
627 tb.addButton(0, ID_PLAY);
628 tb.addButton(1, ID_PAUSE);
629 tb.addButton(2, ID_STOP);
630 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
631 tb.addButton(3, ID_FULLSCREEN);
632 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
633
634 // Create the static control for the time output
635 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
636 tb.getButtonRect(6, &tRect);
637 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
638 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
639 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
640 GetModuleHandle(0), 0);
641 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
642
643 // Create the trackbar control for the time position
644 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
645 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000646 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000647 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
648 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
649 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
650 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000651 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000652 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
653
654 // Create the label with "Speed:" caption
655 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
656 tb.getButtonRect(10, &tRect);
657 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
658 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
659 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
660
661 // Create the edit control and the spin for the speed managing
662 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
663 tb.getButtonRect(11, &tRect);
664 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
665 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
666 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
667 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
668 // It's need to send notify messages to toolbar parent window
669 SetParent(speedEdit, tb.getHandle());
670
671 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
672 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000673 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000674}
675
george82a21d2952005-02-12 11:30:03 +0000676void RfbPlayer::disableTBandMenuItems() {
677 // Disable the menu items
678 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000679 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
680 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
george82a21d2952005-02-12 11:30:03 +0000681 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
682 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
683 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
684 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000685 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
686 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
george82a21d2952005-02-12 11:30:03 +0000687
688 // Disable the toolbar buttons and child controls
689 tb.enableButton(ID_PLAY, false);
690 tb.enableButton(ID_PAUSE, false);
691 tb.enableButton(ID_STOP, false);
692 tb.enableButton(ID_FULLSCREEN, false);
693 EnableWindow(posTrackBar, false);
694 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000695 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000696}
697
george82f5043162005-02-12 11:37:18 +0000698void RfbPlayer::enableTBandMenuItems() {
699 // Enable the menu items
700 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000701 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
702 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
george82f5043162005-02-12 11:37:18 +0000703 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
704 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
705 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
706 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000707 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
708 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
george82f5043162005-02-12 11:37:18 +0000709
710 // Enable the toolbar buttons and child controls
711 tb.enableButton(ID_PLAY, true);
712 tb.enableButton(ID_PAUSE, true);
713 tb.enableButton(ID_STOP, true);
714 tb.enableButton(ID_FULLSCREEN, true);
715 EnableWindow(posTrackBar, true);
716 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000717 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000718}
719
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000720void RfbPlayer::setVisible(bool visible) {
721 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
722 if (visible) {
723 // When the window becomes visible, make it active
724 SetForegroundWindow(getMainHandle());
725 SetActiveWindow(getMainHandle());
726 }
727}
728
729void RfbPlayer::setTitle(const char *title) {
730 char _title[256];
731 strcpy(_title, AppName);
732 strcat(_title, " - ");
733 strcat(_title, title);
734 SetWindowText(getMainHandle(), _title);
735}
736
737void RfbPlayer::setFrameSize(int width, int height) {
738 // Calculate and set required size for main window
739 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000740 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000741 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
742 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
743 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000744 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
745 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
746 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000747 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
748
749 // Enable/disable scrollbars as appropriate
750 calculateScrollBars();
751}
752
753void RfbPlayer::calculateScrollBars() {
754 // Calculate the required size of window
755 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
756 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
757 DWORD old_style;
758 RECT r;
759 SetRect(&r, 0, 0, buffer->width(), buffer->height());
760 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
761 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
762
763 // Work out whether scroll bars are required
764 do {
765 old_style = style;
766
767 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
768 style |= WS_HSCROLL;
769 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
770 }
771 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
772 style |= WS_VSCROLL;
773 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
774 }
775 } while (style != old_style);
776
777 // Tell Windows to update the window style & cached settings
778 if (style != current_style) {
779 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
780 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
781 }
782
783 // Update the scroll settings
784 SCROLLINFO si;
785 if (style & WS_VSCROLL) {
786 si.cbSize = sizeof(si);
787 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
788 si.nMin = 0;
789 si.nMax = buffer->height();
790 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
791 maxscrolloffset.y = max(0, si.nMax-si.nPage);
792 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
793 si.nPos = scrolloffset.y;
794 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
795 }
796 if (style & WS_HSCROLL) {
797 si.cbSize = sizeof(si);
798 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
799 si.nMin = 0;
800 si.nMax = buffer->width();
801 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
802 maxscrolloffset.x = max(0, si.nMax-si.nPage);
803 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
804 si.nPos = scrolloffset.x;
805 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
806 }
george82a758a7a2005-03-15 16:34:57 +0000807
808 // Update the cached client size
809 GetClientRect(getFrameHandle(), &r);
810 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000811}
812
813bool RfbPlayer::setViewportOffset(const Point& tl) {
814/* ***
815 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
816 max(0, min(maxscrolloffset.y, tl.y)));
817 */
818 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
819 max(0, min(tl.y, buffer->height()-client_size.height())));
820 Point delta = np.translate(scrolloffset.negate());
821 if (!np.equals(scrolloffset)) {
822 scrolloffset = np;
823 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
824 UpdateWindow(getFrameHandle());
825 return true;
826 }
827 return false;
828}
829
830void RfbPlayer::close(const char* reason) {
831 setVisible(false);
832 if (reason) {
833 vlog.info("closing - %s", reason);
834 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
835 }
836 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
837}
838
839void RfbPlayer::blankBuffer() {
840 fillRect(buffer->getRect(), 0);
841}
842
843void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000844 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000845 blankBuffer();
846 newSession(fileName);
847 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000848 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000849 if (paused) is->pausePlayback();
850 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000851}
852
853void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000854 // Perform return if waitWhilePaused processed because
855 // rfbReader thread could receive the signal to close
856 if (waitWhilePaused()) return;
857
george8223e08562005-01-31 15:16:42 +0000858 static long update_time = GetTickCount();
859 try {
george828a471482005-02-06 07:15:53 +0000860 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
861 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000862 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000863 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000864 update_time = GetTickCount();
865 }
866 RfbProto::processMsg();
867 } catch (rdr::Exception e) {
868 if (strcmp(e.str(), "[End Of File]") == 0) {
869 rewind();
george825e7af742005-03-10 14:26:00 +0000870 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000871 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000872 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000873 return;
874 }
875 // It's a special exception to perform backward seeking.
876 // We only rewind the stream and seek the offset
877 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000878 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000879 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000880 rewind();
george820d2e19d2005-03-03 15:47:55 +0000881 if (!stopped) setPos(seekOffset);
882 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000883 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000884 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000885 return;
886 }
887 // It's a special exception which is used to terminate the playback
888 if (strcmp(e.str(), "[TERMINATE]") == 0) {
889 sessionTerminateThread *terminate = new sessionTerminateThread(this);
890 terminate->start();
george8223e08562005-01-31 15:16:42 +0000891 } else {
george820e980cc2005-03-10 18:18:34 +0000892 // Show the exception message and close the session playback
893 is->pausePlayback();
894 char message[256] = "\0";
895 strcat(message, e.str());
896 strcat(message, "\nMaybe you force wrong the pixel format for this session");
897 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
898 sessionTerminateThread *terminate = new sessionTerminateThread(this);
899 terminate->start();
george8223e08562005-01-31 15:16:42 +0000900 return;
901 }
902 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000903}
904
george82c7e9f792005-03-20 12:52:46 +0000905long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
906bool ChoosePixelFormatDialog::bigEndian = false;
907
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000908void RfbPlayer::serverInit() {
909 RfbProto::serverInit();
910
george82b95503e2005-02-21 17:02:34 +0000911 // Save the image data start time
912 imageDataStartTime = is->getTimeOffset();
913
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000914 // Resize the backing buffer
915 buffer->setSize(cp.width, cp.height);
916
917 // Check on the true colour mode
918 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000919 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000920
921 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000922 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000923 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000924 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000925 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000926 if (pixelFormatIndex < 0) {
927 options.autoDetectPF = true;
928 options.setPF((PixelFormat *)&cp.pf());
929 } else {
930 options.autoDetectPF = false;
george8210326862005-03-28 12:07:31 +0000931 options.setPF(&supportedPF[pixelFormatIndex]->PF);
george82c7e9f792005-03-20 12:52:46 +0000932 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000933 }
george822c7634b2005-03-10 18:03:27 +0000934 } else {
935 is->pausePlayback();
936 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000937 }
938 } else {
george82dfb557b2005-03-21 18:26:50 +0000939 if (!options.commandLineParam) {
940 if (options.autoDetectPF) {
941 options.setPF((PixelFormat *)&cp.pf());
942 } else {
george8210326862005-03-28 12:07:31 +0000943 options.setPF(&supportedPF[options.pixelFormatIndex]->PF);
george82dfb557b2005-03-21 18:26:50 +0000944 options.pixelFormat.bigEndian = options.bigEndianFlag;
945 }
george820eefed52005-03-29 13:02:21 +0000946 } else if (options.autoDetectPF) {
947 options.setPF((PixelFormat *)&cp.pf());
george825a6df072005-03-20 09:56:17 +0000948 }
george825e7af742005-03-10 14:26:00 +0000949 }
george825a6df072005-03-20 09:56:17 +0000950 cp.setPF(options.pixelFormat);
951 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000952
953 // If the window is not maximised then resize it
954 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
955 setFrameSize(cp.width, cp.height);
956
957 // Set the window title and show it
958 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000959
george82d4d69e62005-02-05 09:23:18 +0000960 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000961 sessionTimeMs = calculateSessionTime(fileName);
962 sprintf(fullSessionTime, "%.2um:%.2us",
963 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000964 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000965 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
966 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000967 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000968
george825e7af742005-03-10 14:26:00 +0000969 setPaused(!options.autoPlay);
970 // Restore the parameters from registry,
971 // which was replaced by command-line parameters.
george82dfb557b2005-03-21 18:26:50 +0000972 if (options.commandLineParam) {
973 options.readFromRegistry();
974 options.commandLineParam = false;
975 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000976}
977
978void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
979 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
980 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
981/* int i;
982 for (i=0;i<count;i++) {
983 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
984 }
985 // *** change to 0, 256?
986 refreshWindowPalette(first, count);
987 palette_changed = true;
988 InvalidateRect(getFrameHandle(), 0, FALSE);*/
989}
990
991void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000992 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000993 MessageBeep(-1);
994}
995
996void RfbPlayer::serverCutText(const char* str, int len) {
997 if (cutText != NULL)
998 delete [] cutText;
999 cutText = new char[len + 1];
1000 memcpy(cutText, str, len);
1001 cutText[len] = '\0';
1002}
1003
1004void RfbPlayer::frameBufferUpdateEnd() {
1005};
1006
1007void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
1008}
1009
1010void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
1011}
1012
1013
1014void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
1015 buffer->fillRect(r, pix);
1016 invalidateBufferRect(r);
1017}
1018
1019void RfbPlayer::imageRect(const Rect& r, void* pixels) {
1020 buffer->imageRect(r, pixels);
1021 invalidateBufferRect(r);
1022}
1023
1024void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
1025 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1026 invalidateBufferRect(r);
1027}
1028
1029bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
1030 Rect rect = bufferToClient(crect);
1031 if (rect.intersect(client_size).is_empty()) return false;
1032 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
1033 InvalidateRect(getFrameHandle(), &invalid, FALSE);
1034 return true;
1035}
1036
george820d2e19d2005-03-03 15:47:55 +00001037bool RfbPlayer::waitWhilePaused() {
1038 bool result = false;
1039 while(isPaused() && !isSeeking()) {
1040 Sleep(20);
1041 result = true;
1042 }
1043 return result;
1044}
1045
george8257f13522005-02-05 08:48:22 +00001046long RfbPlayer::calculateSessionTime(char *filename) {
1047 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001048 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001049 try {
1050 while (TRUE) {
1051 sessionFile.skip(1024);
1052 }
1053 } catch (rdr::Exception e) {
1054 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001055 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001056 } else {
1057 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1058 return 0;
1059 }
1060 }
1061 return 0;
1062}
1063
george826b87aff2005-02-13 10:48:21 +00001064void RfbPlayer::closeSessionFile() {
1065 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001066 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001067 RECT r;
1068
1069 // Uncheck all toolbar buttons
1070 if (tb.getHandle()) {
1071 tb.checkButton(ID_PLAY, false);
1072 tb.checkButton(ID_PAUSE, false);
1073 tb.checkButton(ID_STOP, false);
1074 }
1075
1076 // Stop playback and update the player state
1077 disableTBandMenuItems();
1078 if (rfbReader) {
1079 delete rfbReader->join();
1080 rfbReader = 0;
1081 delete [] fileName;
1082 fileName = 0;
1083 }
1084 blankBuffer();
1085 setTitle("None");
george827009c892005-02-19 12:49:42 +00001086 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001087 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001088 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001089 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1090 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001091 SetWindowText(speedEdit, speedStr);
1092 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1093
1094 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001095 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1096 dwStyle &= ~WS_MAXIMIZE;
1097 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1098 }
george822ff7a612005-02-19 17:05:24 +00001099 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1100 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1101 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001102 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001103 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001104 buffer->setSize(32, 32);
1105 calculateScrollBars();
1106
1107 // Update the cached sizing information and repaint the frame window
1108 GetWindowRect(getFrameHandle(), &r);
1109 window_size = Rect(r.left, r.top, r.right, r.bottom);
1110 GetClientRect(getFrameHandle(), &r);
1111 client_size = Rect(r.left, r.top, r.right, r.bottom);
1112 InvalidateRect(getFrameHandle(), 0, TRUE);
1113 UpdateWindow(getFrameHandle());
1114}
1115
george8217e92cb2005-01-31 16:01:02 +00001116void RfbPlayer::openSessionFile(char *_fileName) {
1117 fileName = strDup(_fileName);
1118
1119 // Close the previous reading thread
1120 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001121 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001122 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001123 }
1124 blankBuffer();
1125 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001126 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001127 rfbReader = new rfbSessionReader(this);
1128 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001129 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001130 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001131}
1132
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001133void RfbPlayer::setPaused(bool paused) {
1134 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001135 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001136 tb.checkButton(ID_PAUSE, true);
1137 tb.checkButton(ID_PLAY, false);
1138 tb.checkButton(ID_STOP, false);
1139 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1140 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001141 } else {
george825beb62a2005-02-09 13:04:32 +00001142 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001143 tb.checkButton(ID_PLAY, true);
1144 tb.checkButton(ID_STOP, false);
1145 tb.checkButton(ID_PAUSE, false);
1146 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1147 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001148 }
1149}
1150
george82006f2792005-02-05 07:40:47 +00001151void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001152 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001153 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001154 if (is) {
1155 is->pausePlayback();
1156 is->interruptFrameDelay();
1157 }
george82006f2792005-02-05 07:40:47 +00001158 tb.checkButton(ID_STOP, true);
1159 tb.checkButton(ID_PLAY, false);
1160 tb.checkButton(ID_PAUSE, false);
1161 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1162 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001163 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001164}
1165
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001166void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001167 if (speed > 0) {
1168 char speedStr[20] = "\0";
1169 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001170 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001171 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001172 SendMessage(speedUpDown, UDM_SETPOS,
1173 0, MAKELONG((short)(newSpeed / 0.5), 0));
1174 sprintf(speedStr, "%.2f", newSpeed);
1175 SetWindowText(speedEdit, speedStr);
1176 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001177}
1178
1179double RfbPlayer::getSpeed() {
1180 return is->getSpeed();
1181}
1182
1183void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001184 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001185}
1186
1187long RfbPlayer::getSeekOffset() {
1188 return is->getSeekOffset();
1189}
1190
1191bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001192 if (is) return is->isSeeking();
1193 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001194}
1195
1196bool RfbPlayer::isSeekMode() {
1197 return seekMode;
1198}
1199
1200bool RfbPlayer::isPaused() {
1201 return is->isPaused();
1202}
1203
1204long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001205 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001206}
1207
george828a471482005-02-06 07:15:53 +00001208void RfbPlayer::updatePos(long newPos) {
1209 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001210 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001211 long time = newPos / 1000;
1212 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001213 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001214
1215 // Update the position of slider
1216 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001217 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1218 sliderStepMs / double(newPos);
1219 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1220 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1221 }
george828a471482005-02-06 07:15:53 +00001222 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001223}
1224
1225void RfbPlayer::skipHandshaking() {
1226 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1227 is->skip(skipBytes);
1228 state_ = RFBSTATE_NORMAL;
1229}
1230
1231void programInfo() {
1232 win32::FileVersionInfo inf;
1233 _tprintf(_T("%s - %s, Version %s\n"),
1234 inf.getVerString(_T("ProductName")),
1235 inf.getVerString(_T("FileDescription")),
1236 inf.getVerString(_T("FileVersion")));
1237 printf("%s\n", buildTime);
1238 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1239}
1240
1241void programUsage() {
george820978ddf2005-03-28 15:53:45 +00001242 InfoDialog usageDialog(usage_msg);
1243 usageDialog.showDialog();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001244}
1245
george825beb62a2005-02-09 13:04:32 +00001246char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001247
1248// playerOptions is the player options with default parameters values,
1249// it is used only for run the player with command-line parameters
1250PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001251bool print_usage = false;
george8205d86232005-03-28 12:16:08 +00001252bool print_upf_list = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001253
1254bool processParams(int argc, char* argv[]) {
george82dfb557b2005-03-21 18:26:50 +00001255 playerOptions.commandLineParam = true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001256 for (int i = 1; i < argc; i++) {
1257 if ((strcasecmp(argv[i], "-help") == 0) ||
1258 (strcasecmp(argv[i], "--help") == 0) ||
1259 (strcasecmp(argv[i], "/help") == 0) ||
1260 (strcasecmp(argv[i], "-h") == 0) ||
1261 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001262 (strcasecmp(argv[i], "/?") == 0) ||
1263 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001264 print_usage = true;
1265 return true;
1266 }
1267
george825caee412005-03-09 09:52:10 +00001268 if ((strcasecmp(argv[i], "-pf") == 0) ||
1269 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george82dfb557b2005-03-21 18:26:50 +00001270 char *pf = argv[++i];
1271 char rgb_order[4] = "\0";
1272 int order = RGB_ORDER;
1273 int r = -1, g = -1, b = -1;
1274 bool big_endian = false;
1275 if (strlen(pf) < 6) return false;
1276 while (strlen(pf)) {
1277 if ((pf[0] == 'r') || (pf[0] == 'R')) {
1278 if (r >=0 ) return false;
1279 r = atoi(++pf);
1280 strcat(rgb_order, "r");
1281 continue;
1282 }
1283 if ((pf[0] == 'g') || (pf[0] == 'G')) {
1284 if (g >=0 ) return false;
1285 g = atoi(++pf);
1286 strcat(rgb_order, "g");
1287 continue;
1288 }
1289 if (((pf[0] == 'b') || (pf[0] == 'B')) &&
1290 (pf[1] != 'e') && (pf[1] != 'E')) {
1291 if (b >=0 ) return false;
1292 b = atoi(++pf);
1293 strcat(rgb_order, "b");
1294 continue;
1295 }
1296 if ((pf[0] == 'l') || (pf[0] == 'L') ||
1297 (pf[0] == 'b') || (pf[0] == 'B')) {
1298 if (strcasecmp(pf, "le") == 0) break;
1299 if (strcasecmp(pf, "be") == 0) { big_endian = true; break;}
1300 return false;
1301 }
1302 pf++;
george82193d8e42005-02-20 16:47:01 +00001303 }
george82888b8fb2005-03-22 15:02:39 +00001304 if ((r < 0) || (g < 0) || (b < 0) || (r + g + b > 32)) return false;
george82dfb557b2005-03-21 18:26:50 +00001305 if (strcasecmp(rgb_order, "rgb") == 0) { order = RGB_ORDER; }
1306 else if (strcasecmp(rgb_order, "rbg") == 0) { order = RBG_ORDER; }
1307 else if (strcasecmp(rgb_order, "grb") == 0) { order = GRB_ORDER; }
1308 else if (strcasecmp(rgb_order, "gbr") == 0) { order = GBR_ORDER; }
1309 else if (strcasecmp(rgb_order, "bgr") == 0) { order = BGR_ORDER; }
1310 else if (strcasecmp(rgb_order, "brg") == 0) { order = BRG_ORDER; }
1311 else return false;
1312 playerOptions.autoDetectPF = false;
1313 playerOptions.setPF(order, r, g, b, big_endian);
george82193d8e42005-02-20 16:47:01 +00001314 continue;
1315 }
1316
george8205d86232005-03-28 12:16:08 +00001317 if ((strcasecmp(argv[i], "-upf") == 0) ||
1318 (strcasecmp(argv[i], "/upf") == 0) && (i < argc-1)) {
1319 if ((i == argc - 1) || (argv[++i][0] == '-')) {
1320 print_upf_list = true;
1321 return true;
1322 }
1323 PixelFormatList userPfList;
1324 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
1325 int index = userPfList.getIndexByPFName(argv[i]);
1326 if (index > 0) {
1327 playerOptions.autoDetectPF = false;
1328 playerOptions.setPF(&userPfList[index]->PF);
1329 } else {
1330 return false;
1331 }
1332 continue;
1333 }
george82193d8e42005-02-20 16:47:01 +00001334
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001335 if ((strcasecmp(argv[i], "-speed") == 0) ||
1336 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001337 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001338 if (playbackSpeed <= 0) {
1339 return false;
1340 }
george825e7af742005-03-10 14:26:00 +00001341 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001342 continue;
1343 }
1344
1345 if ((strcasecmp(argv[i], "-pos") == 0) ||
1346 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001347 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001348 if (initTime <= 0)
1349 return false;
george825e7af742005-03-10 14:26:00 +00001350 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001351 continue;
1352 }
1353
1354 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1355 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001356 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001357 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001358 }
1359
1360 if (i != argc - 1)
1361 return false;
1362 }
1363
1364 fileName = strDup(argv[argc-1]);
1365 return true;
1366}
1367
1368//
1369// -=- WinMain
1370//
1371
1372int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1373
1374 // - Process the command-line
1375
1376 int argc = __argc;
1377 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001378 if ((argc > 1) && (!processParams(argc, argv))) {
1379 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1380 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001381 }
george82e6883de2005-02-08 14:42:12 +00001382
1383 if (print_usage) {
1384 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001385 return 0;
george8267cbcd02005-01-16 15:39:56 +00001386 }
george8205d86232005-03-28 12:16:08 +00001387 // Show the user defined pixel formats if required
1388 if (print_upf_list) {
george820978ddf2005-03-28 15:53:45 +00001389 int list_size = 256;
george8205d86232005-03-28 12:16:08 +00001390 char *upf_list = new char[list_size];
1391 PixelFormatList userPfList;
1392 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
george820978ddf2005-03-28 15:53:45 +00001393 strcpy(upf_list, "The list of the user defined pixel formats:\r\n");
george8205d86232005-03-28 12:16:08 +00001394 for (int i = userPfList.getDefaultPFCount(); i < userPfList.count(); i++) {
1395 if ((list_size - strlen(upf_list) - 1) <
1396 (strlen(userPfList[i]->format_name) + 2)) {
1397 char *tmpStr = new char[list_size =
1398 list_size + strlen(userPfList[i]->format_name) + 2];
1399 strcpy(tmpStr, upf_list);
1400 delete [] upf_list;
1401 upf_list = new char[list_size];
1402 strcpy(upf_list, tmpStr);
1403 delete [] tmpStr;
1404 }
1405 strcat(upf_list, userPfList[i]->format_name);
george820978ddf2005-03-28 15:53:45 +00001406 strcat(upf_list, "\r\n");
george8205d86232005-03-28 12:16:08 +00001407 }
george820978ddf2005-03-28 15:53:45 +00001408 InfoDialog upfInfoDialog(upf_list);
1409 upfInfoDialog.showDialog();
george8205d86232005-03-28 12:16:08 +00001410 delete [] upf_list;
1411 return 0;
1412 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001413
george82e6883de2005-02-08 14:42:12 +00001414 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001415 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001416 try {
george825e7af742005-03-10 14:26:00 +00001417 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001418 } catch (rdr::Exception e) {
1419 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1420 delete player;
1421 return 0;
1422 }
1423
1424 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001425 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001426 MSG msg;
1427 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001428 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1429 TranslateMessage(&msg);
1430 DispatchMessage(&msg);
1431 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001432 }
1433
george82e6883de2005-02-08 14:42:12 +00001434 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001435 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001436 if (player) delete player;
1437 } catch (rdr::Exception e) {
1438 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1439 }
1440
1441 return 0;
1442};