blob: 518fb2be119c6b422c1818ef304994aadccd1ae6 [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:
409 break;
george82ef5f7262005-02-08 15:09:26 +0000410 case ID_HELP_COMMANDLINESWITCHES:
george820978ddf2005-03-28 15:53:45 +0000411 {
412 InfoDialog usageDialog(usage_msg);
george82ff5b29f2005-03-29 11:59:35 +0000413 usageDialog.showDialog(getMainHandle());
george820978ddf2005-03-28 15:53:45 +0000414 }
george8259f84532005-02-08 15:01:39 +0000415 break;
george82357c9f52005-03-21 01:28:12 +0000416 case ID_ABOUT:
417 AboutDialog::instance.showDialog();
418 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000419 }
420 break;
421
422 // Update frame's window size and add scrollbars if required
423
424 case WM_SIZE:
425 {
george82d070c692005-01-19 16:44:04 +0000426
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000427 Point old_offset = bufferToClient(Point(0, 0));
428
429 // Update the cached sizing information
430 RECT r;
431 GetClientRect(getMainHandle(), &r);
432 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
433 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
434
435 GetWindowRect(getFrameHandle(), &r);
436 window_size = Rect(r.left, r.top, r.right, r.bottom);
437 GetClientRect(getFrameHandle(), &r);
438 client_size = Rect(r.left, r.top, r.right, r.bottom);
439
440 // Determine whether scrollbars are required
441 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000442
443 // Resize the ToolBar
444 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000445
446 // Redraw if required
447 if (!old_offset.equals(bufferToClient(Point(0, 0))))
448 InvalidateRect(getFrameHandle(), 0, TRUE);
449 }
450 break;
george828a471482005-02-06 07:15:53 +0000451
452 // Process messages from posTrackBar
453
454 case WM_HSCROLL:
455 {
456 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
457 Pos *= sliderStepMs;
458
459 switch (LOWORD(wParam)) {
460 case TB_PAGEUP:
461 case TB_PAGEDOWN:
462 case TB_LINEUP:
463 case TB_LINEDOWN:
464 case TB_THUMBTRACK:
465 sliderDraging = true;
466 updatePos(Pos);
467 return 0;
george82bcc129b2005-03-15 17:11:40 +0000468 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000469 case TB_ENDTRACK:
470 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000471 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000472 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000473 sliderDraging = false;
474 return 0;
475 default:
476 break;
477 }
478 }
479 break;
george829e6e6cc2005-01-29 13:12:05 +0000480
481 case WM_NOTIFY:
482 switch (((NMHDR*)lParam)->code) {
483 case UDN_DELTAPOS:
484 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000485 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000486 char speedStr[20] = "\0";
487 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
488 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
489 double speed;
490
george824ea27f62005-01-29 15:03:06 +0000491 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000492 if (upDown->iDelta > 0) {
493 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
494 } else {
george824ea27f62005-01-29 15:03:06 +0000495 // It's need to round the UpDown position
496 if ((upDown->iPos * 0.5) != getSpeed()) {
497 upDown->iDelta = 0;
498 lResult = TRUE;
499 }
george829e6e6cc2005-01-29 13:12:05 +0000500 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
501 }
george829e6e6cc2005-01-29 13:12:05 +0000502 sprintf(speedStr, "%.2f", speed);
503 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000504 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000505 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000506 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000507 }
george824ea27f62005-01-29 15:03:06 +0000508 }
george829e6e6cc2005-01-29 13:12:05 +0000509 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000510
511 case WM_CLOSE:
512 vlog.debug("WM_CLOSE %x", getMainHandle());
513 PostQuitMessage(0);
514 break;
515 }
516
517 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
518}
519
520LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
521 switch (msg) {
522
523 case WM_PAINT:
524 {
george821e846ff2005-02-24 13:13:33 +0000525 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000526 seekMode = true;
527 return 0;
528 } else {
529 if (seekMode) {
530 seekMode = false;
531 InvalidateRect(getFrameHandle(), 0, true);
532 UpdateWindow(getFrameHandle());
533 return 0;
534 }
535 }
536
537 PAINTSTRUCT ps;
538 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
539 if (!paintDC)
540 throw SystemException("unable to BeginPaint", GetLastError());
541 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
542
543 if (!pr.is_empty()) {
544
545 if (buffer->bitmap) {
546
547 // Get device context
548 BitmapDC bitmapDC(paintDC, buffer->bitmap);
549
550 // Blit the border if required
551 Rect bufpos = bufferToClient(buffer->getRect());
552 if (!pr.enclosed_by(bufpos)) {
553 vlog.debug("draw border");
554 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
555 RECT r;
556 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
557 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
558 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
559 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
560 }
561
562 // Do the blit
563 Point buf_pos = clientToBuffer(pr.tl);
564 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
565 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
566 throw SystemException("unable to BitBlt to window", GetLastError());
567
568 } else {
569 // Blit a load of black
570 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
571 0, 0, 0, BLACKNESS))
572 throw SystemException("unable to BitBlt to blank window", GetLastError());
573 }
574 }
575 EndPaint(getFrameHandle(), &ps);
576 }
577 return 0;
george8203c01da2005-03-16 12:36:53 +0000578
george82aff63ab2005-03-16 13:48:59 +0000579 // Process play/pause by the left mouse button
580 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000581 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
582 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000583
584 case WM_VSCROLL:
585 case WM_HSCROLL:
586 {
587 Point delta;
588 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
589
590 switch (LOWORD(wParam)) {
591 case SB_PAGEUP: newpos -= 50; break;
592 case SB_PAGEDOWN: newpos += 50; break;
593 case SB_LINEUP: newpos -= 5; break;
594 case SB_LINEDOWN: newpos += 5; break;
595 case SB_THUMBTRACK:
596 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
597 default: vlog.info("received unknown scroll message");
598 };
599
600 if (msg == WM_HSCROLL)
601 setViewportOffset(Point(newpos, scrolloffset.y));
602 else
603 setViewportOffset(Point(scrolloffset.x, newpos));
604
605 SCROLLINFO si;
606 si.cbSize = sizeof(si);
607 si.fMask = SIF_POS;
608 si.nPos = newpos;
609 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
610 }
611 break;
612 }
613
614 return DefWindowProc(hwnd, msg, wParam, lParam);
615}
616
george82d070c692005-01-19 16:44:04 +0000617void RfbPlayer::createToolBar(HWND parentHwnd) {
618 RECT tRect;
619 InitCommonControls();
620
621 tb.create(ID_TOOLBAR, parentHwnd);
622 tb.addBitmap(4, IDB_TOOLBAR);
623
624 // Create the control buttons
625 tb.addButton(0, ID_PLAY);
626 tb.addButton(1, ID_PAUSE);
627 tb.addButton(2, ID_STOP);
628 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
629 tb.addButton(3, ID_FULLSCREEN);
630 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
631
632 // Create the static control for the time output
633 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
634 tb.getButtonRect(6, &tRect);
635 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
636 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
637 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
638 GetModuleHandle(0), 0);
639 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
640
641 // Create the trackbar control for the time position
642 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
643 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000644 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000645 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
646 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
647 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
648 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000649 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000650 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
651
652 // Create the label with "Speed:" caption
653 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
654 tb.getButtonRect(10, &tRect);
655 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
656 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
657 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
658
659 // Create the edit control and the spin for the speed managing
660 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
661 tb.getButtonRect(11, &tRect);
662 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
663 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
664 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
665 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
666 // It's need to send notify messages to toolbar parent window
667 SetParent(speedEdit, tb.getHandle());
668
669 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
670 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000671 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000672}
673
george82a21d2952005-02-12 11:30:03 +0000674void RfbPlayer::disableTBandMenuItems() {
675 // Disable the menu items
676 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000677 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
678 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
george82a21d2952005-02-12 11:30:03 +0000679 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
680 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
681 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
682 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000683 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
684 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
george82a21d2952005-02-12 11:30:03 +0000685
686 // Disable the toolbar buttons and child controls
687 tb.enableButton(ID_PLAY, false);
688 tb.enableButton(ID_PAUSE, false);
689 tb.enableButton(ID_STOP, false);
690 tb.enableButton(ID_FULLSCREEN, false);
691 EnableWindow(posTrackBar, false);
692 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000693 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000694}
695
george82f5043162005-02-12 11:37:18 +0000696void RfbPlayer::enableTBandMenuItems() {
697 // Enable the menu items
698 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000699 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
700 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
george82f5043162005-02-12 11:37:18 +0000701 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
702 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
703 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
704 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000705 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
706 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
george82f5043162005-02-12 11:37:18 +0000707
708 // Enable the toolbar buttons and child controls
709 tb.enableButton(ID_PLAY, true);
710 tb.enableButton(ID_PAUSE, true);
711 tb.enableButton(ID_STOP, true);
712 tb.enableButton(ID_FULLSCREEN, true);
713 EnableWindow(posTrackBar, true);
714 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000715 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000716}
717
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000718void RfbPlayer::setVisible(bool visible) {
719 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
720 if (visible) {
721 // When the window becomes visible, make it active
722 SetForegroundWindow(getMainHandle());
723 SetActiveWindow(getMainHandle());
724 }
725}
726
727void RfbPlayer::setTitle(const char *title) {
728 char _title[256];
729 strcpy(_title, AppName);
730 strcat(_title, " - ");
731 strcat(_title, title);
732 SetWindowText(getMainHandle(), _title);
733}
734
735void RfbPlayer::setFrameSize(int width, int height) {
736 // Calculate and set required size for main window
737 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000738 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000739 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
740 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
741 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000742 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
743 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
744 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000745 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
746
747 // Enable/disable scrollbars as appropriate
748 calculateScrollBars();
749}
750
751void RfbPlayer::calculateScrollBars() {
752 // Calculate the required size of window
753 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
754 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
755 DWORD old_style;
756 RECT r;
757 SetRect(&r, 0, 0, buffer->width(), buffer->height());
758 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
759 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
760
761 // Work out whether scroll bars are required
762 do {
763 old_style = style;
764
765 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
766 style |= WS_HSCROLL;
767 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
768 }
769 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
770 style |= WS_VSCROLL;
771 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
772 }
773 } while (style != old_style);
774
775 // Tell Windows to update the window style & cached settings
776 if (style != current_style) {
777 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
778 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
779 }
780
781 // Update the scroll settings
782 SCROLLINFO si;
783 if (style & WS_VSCROLL) {
784 si.cbSize = sizeof(si);
785 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
786 si.nMin = 0;
787 si.nMax = buffer->height();
788 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
789 maxscrolloffset.y = max(0, si.nMax-si.nPage);
790 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
791 si.nPos = scrolloffset.y;
792 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
793 }
794 if (style & WS_HSCROLL) {
795 si.cbSize = sizeof(si);
796 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
797 si.nMin = 0;
798 si.nMax = buffer->width();
799 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
800 maxscrolloffset.x = max(0, si.nMax-si.nPage);
801 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
802 si.nPos = scrolloffset.x;
803 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
804 }
george82a758a7a2005-03-15 16:34:57 +0000805
806 // Update the cached client size
807 GetClientRect(getFrameHandle(), &r);
808 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000809}
810
811bool RfbPlayer::setViewportOffset(const Point& tl) {
812/* ***
813 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
814 max(0, min(maxscrolloffset.y, tl.y)));
815 */
816 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
817 max(0, min(tl.y, buffer->height()-client_size.height())));
818 Point delta = np.translate(scrolloffset.negate());
819 if (!np.equals(scrolloffset)) {
820 scrolloffset = np;
821 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
822 UpdateWindow(getFrameHandle());
823 return true;
824 }
825 return false;
826}
827
828void RfbPlayer::close(const char* reason) {
829 setVisible(false);
830 if (reason) {
831 vlog.info("closing - %s", reason);
832 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
833 }
834 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
835}
836
837void RfbPlayer::blankBuffer() {
838 fillRect(buffer->getRect(), 0);
839}
840
841void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000842 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000843 blankBuffer();
844 newSession(fileName);
845 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000846 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000847 if (paused) is->pausePlayback();
848 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000849}
850
851void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000852 // Perform return if waitWhilePaused processed because
853 // rfbReader thread could receive the signal to close
854 if (waitWhilePaused()) return;
855
george8223e08562005-01-31 15:16:42 +0000856 static long update_time = GetTickCount();
857 try {
george828a471482005-02-06 07:15:53 +0000858 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
859 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000860 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000861 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000862 update_time = GetTickCount();
863 }
864 RfbProto::processMsg();
865 } catch (rdr::Exception e) {
866 if (strcmp(e.str(), "[End Of File]") == 0) {
867 rewind();
george825e7af742005-03-10 14:26:00 +0000868 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000869 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000870 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000871 return;
872 }
873 // It's a special exception to perform backward seeking.
874 // We only rewind the stream and seek the offset
875 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000876 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000877 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000878 rewind();
george820d2e19d2005-03-03 15:47:55 +0000879 if (!stopped) setPos(seekOffset);
880 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000881 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000882 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000883 return;
884 }
885 // It's a special exception which is used to terminate the playback
886 if (strcmp(e.str(), "[TERMINATE]") == 0) {
887 sessionTerminateThread *terminate = new sessionTerminateThread(this);
888 terminate->start();
george8223e08562005-01-31 15:16:42 +0000889 } else {
george820e980cc2005-03-10 18:18:34 +0000890 // Show the exception message and close the session playback
891 is->pausePlayback();
892 char message[256] = "\0";
893 strcat(message, e.str());
894 strcat(message, "\nMaybe you force wrong the pixel format for this session");
895 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
896 sessionTerminateThread *terminate = new sessionTerminateThread(this);
897 terminate->start();
george8223e08562005-01-31 15:16:42 +0000898 return;
899 }
900 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000901}
902
george82c7e9f792005-03-20 12:52:46 +0000903long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
904bool ChoosePixelFormatDialog::bigEndian = false;
905
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000906void RfbPlayer::serverInit() {
907 RfbProto::serverInit();
908
george82b95503e2005-02-21 17:02:34 +0000909 // Save the image data start time
910 imageDataStartTime = is->getTimeOffset();
911
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000912 // Resize the backing buffer
913 buffer->setSize(cp.width, cp.height);
914
915 // Check on the true colour mode
916 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000917 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000918
919 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000920 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000921 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000922 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000923 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000924 if (pixelFormatIndex < 0) {
925 options.autoDetectPF = true;
926 options.setPF((PixelFormat *)&cp.pf());
927 } else {
928 options.autoDetectPF = false;
george8210326862005-03-28 12:07:31 +0000929 options.setPF(&supportedPF[pixelFormatIndex]->PF);
george82c7e9f792005-03-20 12:52:46 +0000930 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000931 }
george822c7634b2005-03-10 18:03:27 +0000932 } else {
933 is->pausePlayback();
934 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000935 }
936 } else {
george82dfb557b2005-03-21 18:26:50 +0000937 if (!options.commandLineParam) {
938 if (options.autoDetectPF) {
939 options.setPF((PixelFormat *)&cp.pf());
940 } else {
george8210326862005-03-28 12:07:31 +0000941 options.setPF(&supportedPF[options.pixelFormatIndex]->PF);
george82dfb557b2005-03-21 18:26:50 +0000942 options.pixelFormat.bigEndian = options.bigEndianFlag;
943 }
george820eefed52005-03-29 13:02:21 +0000944 } else if (options.autoDetectPF) {
945 options.setPF((PixelFormat *)&cp.pf());
george825a6df072005-03-20 09:56:17 +0000946 }
george825e7af742005-03-10 14:26:00 +0000947 }
george825a6df072005-03-20 09:56:17 +0000948 cp.setPF(options.pixelFormat);
949 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000950
951 // If the window is not maximised then resize it
952 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
953 setFrameSize(cp.width, cp.height);
954
955 // Set the window title and show it
956 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000957
george82d4d69e62005-02-05 09:23:18 +0000958 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000959 sessionTimeMs = calculateSessionTime(fileName);
960 sprintf(fullSessionTime, "%.2um:%.2us",
961 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000962 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000963 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
964 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000965 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000966
george825e7af742005-03-10 14:26:00 +0000967 setPaused(!options.autoPlay);
968 // Restore the parameters from registry,
969 // which was replaced by command-line parameters.
george82dfb557b2005-03-21 18:26:50 +0000970 if (options.commandLineParam) {
971 options.readFromRegistry();
972 options.commandLineParam = false;
973 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000974}
975
976void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
977 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
978 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
979/* int i;
980 for (i=0;i<count;i++) {
981 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
982 }
983 // *** change to 0, 256?
984 refreshWindowPalette(first, count);
985 palette_changed = true;
986 InvalidateRect(getFrameHandle(), 0, FALSE);*/
987}
988
989void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000990 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000991 MessageBeep(-1);
992}
993
994void RfbPlayer::serverCutText(const char* str, int len) {
995 if (cutText != NULL)
996 delete [] cutText;
997 cutText = new char[len + 1];
998 memcpy(cutText, str, len);
999 cutText[len] = '\0';
1000}
1001
1002void RfbPlayer::frameBufferUpdateEnd() {
1003};
1004
1005void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
1006}
1007
1008void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
1009}
1010
1011
1012void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
1013 buffer->fillRect(r, pix);
1014 invalidateBufferRect(r);
1015}
1016
1017void RfbPlayer::imageRect(const Rect& r, void* pixels) {
1018 buffer->imageRect(r, pixels);
1019 invalidateBufferRect(r);
1020}
1021
1022void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
1023 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1024 invalidateBufferRect(r);
1025}
1026
1027bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
1028 Rect rect = bufferToClient(crect);
1029 if (rect.intersect(client_size).is_empty()) return false;
1030 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
1031 InvalidateRect(getFrameHandle(), &invalid, FALSE);
1032 return true;
1033}
1034
george820d2e19d2005-03-03 15:47:55 +00001035bool RfbPlayer::waitWhilePaused() {
1036 bool result = false;
1037 while(isPaused() && !isSeeking()) {
1038 Sleep(20);
1039 result = true;
1040 }
1041 return result;
1042}
1043
george8257f13522005-02-05 08:48:22 +00001044long RfbPlayer::calculateSessionTime(char *filename) {
1045 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001046 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001047 try {
1048 while (TRUE) {
1049 sessionFile.skip(1024);
1050 }
1051 } catch (rdr::Exception e) {
1052 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001053 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001054 } else {
1055 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1056 return 0;
1057 }
1058 }
1059 return 0;
1060}
1061
george826b87aff2005-02-13 10:48:21 +00001062void RfbPlayer::closeSessionFile() {
1063 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001064 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001065 RECT r;
1066
1067 // Uncheck all toolbar buttons
1068 if (tb.getHandle()) {
1069 tb.checkButton(ID_PLAY, false);
1070 tb.checkButton(ID_PAUSE, false);
1071 tb.checkButton(ID_STOP, false);
1072 }
1073
1074 // Stop playback and update the player state
1075 disableTBandMenuItems();
1076 if (rfbReader) {
1077 delete rfbReader->join();
1078 rfbReader = 0;
1079 delete [] fileName;
1080 fileName = 0;
1081 }
1082 blankBuffer();
1083 setTitle("None");
george827009c892005-02-19 12:49:42 +00001084 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001085 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001086 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001087 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1088 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001089 SetWindowText(speedEdit, speedStr);
1090 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1091
1092 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001093 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1094 dwStyle &= ~WS_MAXIMIZE;
1095 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1096 }
george822ff7a612005-02-19 17:05:24 +00001097 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1098 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1099 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001100 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001101 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001102 buffer->setSize(32, 32);
1103 calculateScrollBars();
1104
1105 // Update the cached sizing information and repaint the frame window
1106 GetWindowRect(getFrameHandle(), &r);
1107 window_size = Rect(r.left, r.top, r.right, r.bottom);
1108 GetClientRect(getFrameHandle(), &r);
1109 client_size = Rect(r.left, r.top, r.right, r.bottom);
1110 InvalidateRect(getFrameHandle(), 0, TRUE);
1111 UpdateWindow(getFrameHandle());
1112}
1113
george8217e92cb2005-01-31 16:01:02 +00001114void RfbPlayer::openSessionFile(char *_fileName) {
1115 fileName = strDup(_fileName);
1116
1117 // Close the previous reading thread
1118 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001119 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001120 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001121 }
1122 blankBuffer();
1123 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001124 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001125 rfbReader = new rfbSessionReader(this);
1126 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001127 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001128 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001129}
1130
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001131void RfbPlayer::setPaused(bool paused) {
1132 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001133 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001134 tb.checkButton(ID_PAUSE, true);
1135 tb.checkButton(ID_PLAY, false);
1136 tb.checkButton(ID_STOP, false);
1137 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1138 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001139 } else {
george825beb62a2005-02-09 13:04:32 +00001140 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001141 tb.checkButton(ID_PLAY, true);
1142 tb.checkButton(ID_STOP, false);
1143 tb.checkButton(ID_PAUSE, false);
1144 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
1145 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001146 }
1147}
1148
george82006f2792005-02-05 07:40:47 +00001149void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001150 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001151 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001152 if (is) {
1153 is->pausePlayback();
1154 is->interruptFrameDelay();
1155 }
george82006f2792005-02-05 07:40:47 +00001156 tb.checkButton(ID_STOP, true);
1157 tb.checkButton(ID_PLAY, false);
1158 tb.checkButton(ID_PAUSE, false);
1159 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
1160 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +00001161 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001162}
1163
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001164void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001165 if (speed > 0) {
1166 char speedStr[20] = "\0";
1167 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001168 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001169 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001170 SendMessage(speedUpDown, UDM_SETPOS,
1171 0, MAKELONG((short)(newSpeed / 0.5), 0));
1172 sprintf(speedStr, "%.2f", newSpeed);
1173 SetWindowText(speedEdit, speedStr);
1174 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001175}
1176
1177double RfbPlayer::getSpeed() {
1178 return is->getSpeed();
1179}
1180
1181void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001182 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001183}
1184
1185long RfbPlayer::getSeekOffset() {
1186 return is->getSeekOffset();
1187}
1188
1189bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001190 if (is) return is->isSeeking();
1191 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001192}
1193
1194bool RfbPlayer::isSeekMode() {
1195 return seekMode;
1196}
1197
1198bool RfbPlayer::isPaused() {
1199 return is->isPaused();
1200}
1201
1202long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001203 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001204}
1205
george828a471482005-02-06 07:15:53 +00001206void RfbPlayer::updatePos(long newPos) {
1207 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001208 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001209 long time = newPos / 1000;
1210 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001211 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001212
1213 // Update the position of slider
1214 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001215 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1216 sliderStepMs / double(newPos);
1217 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1218 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1219 }
george828a471482005-02-06 07:15:53 +00001220 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001221}
1222
1223void RfbPlayer::skipHandshaking() {
1224 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1225 is->skip(skipBytes);
1226 state_ = RFBSTATE_NORMAL;
1227}
1228
1229void programInfo() {
1230 win32::FileVersionInfo inf;
1231 _tprintf(_T("%s - %s, Version %s\n"),
1232 inf.getVerString(_T("ProductName")),
1233 inf.getVerString(_T("FileDescription")),
1234 inf.getVerString(_T("FileVersion")));
1235 printf("%s\n", buildTime);
1236 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1237}
1238
1239void programUsage() {
george820978ddf2005-03-28 15:53:45 +00001240 InfoDialog usageDialog(usage_msg);
1241 usageDialog.showDialog();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001242}
1243
george825beb62a2005-02-09 13:04:32 +00001244char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001245
1246// playerOptions is the player options with default parameters values,
1247// it is used only for run the player with command-line parameters
1248PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001249bool print_usage = false;
george8205d86232005-03-28 12:16:08 +00001250bool print_upf_list = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001251
1252bool processParams(int argc, char* argv[]) {
george82dfb557b2005-03-21 18:26:50 +00001253 playerOptions.commandLineParam = true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001254 for (int i = 1; i < argc; i++) {
1255 if ((strcasecmp(argv[i], "-help") == 0) ||
1256 (strcasecmp(argv[i], "--help") == 0) ||
1257 (strcasecmp(argv[i], "/help") == 0) ||
1258 (strcasecmp(argv[i], "-h") == 0) ||
1259 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001260 (strcasecmp(argv[i], "/?") == 0) ||
1261 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001262 print_usage = true;
1263 return true;
1264 }
1265
george825caee412005-03-09 09:52:10 +00001266 if ((strcasecmp(argv[i], "-pf") == 0) ||
1267 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george82dfb557b2005-03-21 18:26:50 +00001268 char *pf = argv[++i];
1269 char rgb_order[4] = "\0";
1270 int order = RGB_ORDER;
1271 int r = -1, g = -1, b = -1;
1272 bool big_endian = false;
1273 if (strlen(pf) < 6) return false;
1274 while (strlen(pf)) {
1275 if ((pf[0] == 'r') || (pf[0] == 'R')) {
1276 if (r >=0 ) return false;
1277 r = atoi(++pf);
1278 strcat(rgb_order, "r");
1279 continue;
1280 }
1281 if ((pf[0] == 'g') || (pf[0] == 'G')) {
1282 if (g >=0 ) return false;
1283 g = atoi(++pf);
1284 strcat(rgb_order, "g");
1285 continue;
1286 }
1287 if (((pf[0] == 'b') || (pf[0] == 'B')) &&
1288 (pf[1] != 'e') && (pf[1] != 'E')) {
1289 if (b >=0 ) return false;
1290 b = atoi(++pf);
1291 strcat(rgb_order, "b");
1292 continue;
1293 }
1294 if ((pf[0] == 'l') || (pf[0] == 'L') ||
1295 (pf[0] == 'b') || (pf[0] == 'B')) {
1296 if (strcasecmp(pf, "le") == 0) break;
1297 if (strcasecmp(pf, "be") == 0) { big_endian = true; break;}
1298 return false;
1299 }
1300 pf++;
george82193d8e42005-02-20 16:47:01 +00001301 }
george82888b8fb2005-03-22 15:02:39 +00001302 if ((r < 0) || (g < 0) || (b < 0) || (r + g + b > 32)) return false;
george82dfb557b2005-03-21 18:26:50 +00001303 if (strcasecmp(rgb_order, "rgb") == 0) { order = RGB_ORDER; }
1304 else if (strcasecmp(rgb_order, "rbg") == 0) { order = RBG_ORDER; }
1305 else if (strcasecmp(rgb_order, "grb") == 0) { order = GRB_ORDER; }
1306 else if (strcasecmp(rgb_order, "gbr") == 0) { order = GBR_ORDER; }
1307 else if (strcasecmp(rgb_order, "bgr") == 0) { order = BGR_ORDER; }
1308 else if (strcasecmp(rgb_order, "brg") == 0) { order = BRG_ORDER; }
1309 else return false;
1310 playerOptions.autoDetectPF = false;
1311 playerOptions.setPF(order, r, g, b, big_endian);
george82193d8e42005-02-20 16:47:01 +00001312 continue;
1313 }
1314
george8205d86232005-03-28 12:16:08 +00001315 if ((strcasecmp(argv[i], "-upf") == 0) ||
1316 (strcasecmp(argv[i], "/upf") == 0) && (i < argc-1)) {
1317 if ((i == argc - 1) || (argv[++i][0] == '-')) {
1318 print_upf_list = true;
1319 return true;
1320 }
1321 PixelFormatList userPfList;
1322 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
1323 int index = userPfList.getIndexByPFName(argv[i]);
1324 if (index > 0) {
1325 playerOptions.autoDetectPF = false;
1326 playerOptions.setPF(&userPfList[index]->PF);
1327 } else {
1328 return false;
1329 }
1330 continue;
1331 }
george82193d8e42005-02-20 16:47:01 +00001332
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001333 if ((strcasecmp(argv[i], "-speed") == 0) ||
1334 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001335 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001336 if (playbackSpeed <= 0) {
1337 return false;
1338 }
george825e7af742005-03-10 14:26:00 +00001339 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001340 continue;
1341 }
1342
1343 if ((strcasecmp(argv[i], "-pos") == 0) ||
1344 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001345 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001346 if (initTime <= 0)
1347 return false;
george825e7af742005-03-10 14:26:00 +00001348 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001349 continue;
1350 }
1351
1352 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1353 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001354 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001355 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001356 }
1357
1358 if (i != argc - 1)
1359 return false;
1360 }
1361
1362 fileName = strDup(argv[argc-1]);
1363 return true;
1364}
1365
1366//
1367// -=- WinMain
1368//
1369
1370int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1371
1372 // - Process the command-line
1373
1374 int argc = __argc;
1375 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001376 if ((argc > 1) && (!processParams(argc, argv))) {
1377 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1378 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001379 }
george82e6883de2005-02-08 14:42:12 +00001380
1381 if (print_usage) {
1382 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001383 return 0;
george8267cbcd02005-01-16 15:39:56 +00001384 }
george8205d86232005-03-28 12:16:08 +00001385 // Show the user defined pixel formats if required
1386 if (print_upf_list) {
george820978ddf2005-03-28 15:53:45 +00001387 int list_size = 256;
george8205d86232005-03-28 12:16:08 +00001388 char *upf_list = new char[list_size];
1389 PixelFormatList userPfList;
1390 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
george820978ddf2005-03-28 15:53:45 +00001391 strcpy(upf_list, "The list of the user defined pixel formats:\r\n");
george8205d86232005-03-28 12:16:08 +00001392 for (int i = userPfList.getDefaultPFCount(); i < userPfList.count(); i++) {
1393 if ((list_size - strlen(upf_list) - 1) <
1394 (strlen(userPfList[i]->format_name) + 2)) {
1395 char *tmpStr = new char[list_size =
1396 list_size + strlen(userPfList[i]->format_name) + 2];
1397 strcpy(tmpStr, upf_list);
1398 delete [] upf_list;
1399 upf_list = new char[list_size];
1400 strcpy(upf_list, tmpStr);
1401 delete [] tmpStr;
1402 }
1403 strcat(upf_list, userPfList[i]->format_name);
george820978ddf2005-03-28 15:53:45 +00001404 strcat(upf_list, "\r\n");
george8205d86232005-03-28 12:16:08 +00001405 }
george820978ddf2005-03-28 15:53:45 +00001406 InfoDialog upfInfoDialog(upf_list);
1407 upfInfoDialog.showDialog();
george8205d86232005-03-28 12:16:08 +00001408 delete [] upf_list;
1409 return 0;
1410 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001411
george82e6883de2005-02-08 14:42:12 +00001412 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001413 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001414 try {
george825e7af742005-03-10 14:26:00 +00001415 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001416 } catch (rdr::Exception e) {
1417 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1418 delete player;
1419 return 0;
1420 }
1421
1422 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001423 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001424 MSG msg;
1425 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001426 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1427 TranslateMessage(&msg);
1428 DispatchMessage(&msg);
1429 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001430 }
1431
george82e6883de2005-02-08 14:42:12 +00001432 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001433 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001434 if (player) delete player;
1435 } catch (rdr::Exception e) {
1436 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1437 }
1438
1439 return 0;
1440};