blob: 061c7177ac20e0afc9e4677532c1ff59531eb8b5 [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"
george82ee455792005-04-13 12:56:15 +000067 " -autoplay \t- Runs the player in the playback mode.\r\n"
68 " -loop \t- Replays the rfb session.";
george82e6883de2005-02-08 14:42:12 +000069
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000070// -=- RfbPlayer's defines
71
72#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000073#define MAX_SPEED 10.00
74#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000075#define MAX_POS_TRACKBAR_RANGE 50
george825e7af742005-03-10 14:26:00 +000076#define CTRL_BAR_HEIGHT 28
george8268d25142005-02-13 09:33:22 +000077#define DEFAULT_PLAYER_WIDTH 640
78#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000079
george82d070c692005-01-19 16:44:04 +000080#define ID_TOOLBAR 500
81#define ID_PLAY 510
82#define ID_PAUSE 520
83#define ID_TIME_STATIC 530
84#define ID_SPEED_STATIC 540
85#define ID_SPEED_EDIT 550
86#define ID_POS_TRACKBAR 560
87#define ID_SPEED_UPDOWN 570
88
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000089//
george82357c9f52005-03-21 01:28:12 +000090// -=- AboutDialog global values
91//
92
93const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT;
94const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT;
95const WORD rfb::win32::AboutDialog::Version = IDC_VERSION;
96const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME;
97const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION;
98
99//
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000100// -=- RfbPlayerClass
101
102//
103// Window class used as the basis for RfbPlayer instance
104//
105
106class RfbPlayerClass {
107public:
108 RfbPlayerClass();
109 ~RfbPlayerClass();
110 ATOM classAtom;
111 HINSTANCE instance;
112};
113
114LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
115 LRESULT result;
116
117 if (msg == WM_CREATE)
118 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
119 else if (msg == WM_DESTROY) {
120 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000121 SetWindowLong(hwnd, GWL_USERDATA, 0);
122 }
123 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
124 if (!_this) {
125 vlog.info("null _this in %x, message %u", hwnd, msg);
126 return DefWindowProc(hwnd, msg, wParam, lParam);
127 }
128
129 try {
130 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
131 } catch (rdr::Exception& e) {
132 vlog.error("untrapped: %s", e.str());
133 }
134
135 return result;
136};
137
138RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
139 WNDCLASS wndClass;
140 wndClass.style = 0;
141 wndClass.lpfnWndProc = RfbPlayerProc;
142 wndClass.cbClsExtra = 0;
143 wndClass.cbWndExtra = 0;
144 wndClass.hInstance = instance = GetModuleHandle(0);
145 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000146 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000147 if (!wndClass.hIcon)
148 printf("unable to load icon:%ld", GetLastError());
149 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
150 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000151 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000152 wndClass.lpszClassName = _T("RfbPlayerClass");
153 classAtom = RegisterClass(&wndClass);
154 if (!classAtom) {
155 throw rdr::SystemException("unable to register RfbPlayer window class",
156 GetLastError());
157 }
158}
159
160RfbPlayerClass::~RfbPlayerClass() {
161 if (classAtom) {
162 UnregisterClass((const TCHAR*)classAtom, instance);
163 }
164}
165
166RfbPlayerClass baseClass;
167
168//
169// -=- RfbFrameClass
170
171//
172// Window class used to displaying the rfb data
173//
174
175class RfbFrameClass {
176public:
177 RfbFrameClass();
178 ~RfbFrameClass();
179 ATOM classAtom;
180 HINSTANCE instance;
181};
182
183LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
184 LRESULT result;
185
186 if (msg == WM_CREATE)
187 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
188 else if (msg == WM_DESTROY)
189 SetWindowLong(hwnd, GWL_USERDATA, 0);
190 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
191 if (!_this) {
192 vlog.info("null _this in %x, message %u", hwnd, msg);
193 return DefWindowProc(hwnd, msg, wParam, lParam);
194 }
195
196 try {
197 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
198 } catch (rdr::Exception& e) {
199 vlog.error("untrapped: %s", e.str());
200 }
201
202 return result;
203}
204
205RfbFrameClass::RfbFrameClass() : classAtom(0) {
206 WNDCLASS wndClass;
207 wndClass.style = 0;
208 wndClass.lpfnWndProc = FrameProc;
209 wndClass.cbClsExtra = 0;
210 wndClass.cbWndExtra = 0;
211 wndClass.hInstance = instance = GetModuleHandle(0);
212 wndClass.hIcon = 0;
213 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
214 wndClass.hbrBackground = 0;
215 wndClass.lpszMenuName = 0;
216 wndClass.lpszClassName = _T("RfbPlayerClass1");
217 classAtom = RegisterClass(&wndClass);
218 if (!classAtom) {
219 throw rdr::SystemException("unable to register RfbPlayer window class",
220 GetLastError());
221 }
222}
223
224RfbFrameClass::~RfbFrameClass() {
225 if (classAtom) {
226 UnregisterClass((const TCHAR*)classAtom, instance);
227 }
228}
229
230RfbFrameClass frameClass;
231
232//
233// -=- RfbPlayer instance implementation
234//
235
george825e7af742005-03-10 14:26:00 +0000236RfbPlayer::RfbPlayer(char *_fileName, PlayerOptions *_options)
237: RfbProto(_fileName), fileName(_fileName), buffer(0), client_size(0, 0, 32, 32),
238 window_size(0, 0, 32, 32), cutText(0), seekMode(false), lastPos(0),
239 timeStatic(0), speedEdit(0), posTrackBar(0), speedUpDown(0),
george823104aec2005-02-21 13:20:56 +0000240 rfbReader(0), sessionTimeMs(0), sliderDraging(false), sliderStepMs(0),
george8226af7652005-04-13 12:46:25 +0000241 imageDataStartTime(0), rewindFlag(false), stopped(false),
242 currentEncoding(-1) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000243
george825e7af742005-03-10 14:26:00 +0000244 // Save the player options
245 memcpy(&options, _options, sizeof(options));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000246
george823c8fbbf2005-01-24 11:09:08 +0000247 // Reset the full session time
248 strcpy(fullSessionTime, "00m:00s");
249
george820981b342005-03-19 11:19:00 +0000250 // Load the user defined pixel formats from the registry
251 supportedPF.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
252
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000253 // Create the main window
254 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000255 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
256 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000257 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000258 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000259 if (!mainHwnd) {
260 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
261 }
262 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
263
264 // Create the backing buffer
265 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000266 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000267
george825e7af742005-03-10 14:26:00 +0000268 // If run with command-line parameters,
269 // open the session file with default settings, otherwise
270 // restore player settings from the registry
george8217e92cb2005-01-31 16:01:02 +0000271 if (fileName) {
272 openSessionFile(fileName);
george825e7af742005-03-10 14:26:00 +0000273 if (options.initTime > 0) setPos(options.initTime);
274 setSpeed(options.playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000275 } else {
george825e7af742005-03-10 14:26:00 +0000276 options.readFromRegistry();
george8263ebbcc2005-02-12 12:09:13 +0000277 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000278 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000279 }
george82ee455792005-04-13 12:56:15 +0000280 init();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000281}
282
283RfbPlayer::~RfbPlayer() {
284 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000285 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000286 delete rfbReader->join();
287 rfbReader = 0;
288 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000289 if (mainHwnd) {
290 setVisible(false);
291 DestroyWindow(mainHwnd);
292 mainHwnd = 0;
293 }
george825beb62a2005-02-09 13:04:32 +0000294 if (buffer) delete buffer;
295 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000296 if (fileName) delete [] fileName;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000297 vlog.debug("~RfbPlayer done");
298}
299
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000300LRESULT
301RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
302 switch (msg) {
303
304 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000305
306 case WM_CREATE:
307 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000308 // Create the frame window
309 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
310 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
311 hwnd, 0, frameClass.instance, this);
312
george82d070c692005-01-19 16:44:04 +0000313 createToolBar(hwnd);
314
george82006f2792005-02-05 07:40:47 +0000315 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000316
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000317 return 0;
318 }
319
george827214b822004-12-12 07:02:51 +0000320 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000321
322 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000323 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000324 case ID_OPENFILE:
325 {
326 char curDir[_MAX_DIR];
327 static char filename[_MAX_PATH];
328 OPENFILENAME ofn;
329 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
330 GetCurrentDirectory(sizeof(curDir), curDir);
331
332 ofn.lStructSize = sizeof(OPENFILENAME);
333 ofn.hwndOwner = getMainHandle();
334 ofn.lpstrFile = filename;
335 ofn.nMaxFile = sizeof(filename);
336 ofn.lpstrInitialDir = curDir;
george82d7c81be2005-04-11 15:07:13 +0000337 ofn.lpstrFilter = "Rfb Session files (*.rfb, *.fbs)\0*.rfb;*.fbs\0" \
george826e51fcc2005-02-06 13:30:49 +0000338 "All files (*.*)\0*.*\0";
339 ofn.lpstrDefExt = "rfb";
340 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000341 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000342 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000343 }
george826e51fcc2005-02-06 13:30:49 +0000344 }
345 break;
george8271ca1772005-02-13 10:50:46 +0000346 case ID_CLOSEFILE:
347 closeSessionFile();
348 break;
george8226af7652005-04-13 12:46:25 +0000349 case ID_SESSION_INFO:
350 {
351 SessionInfoDialog sessionInfo(&cp, currentEncoding);
352 sessionInfo.showDialog(getMainHandle());
353 }
354 break;
george825c13c662005-01-27 14:48:23 +0000355 case ID_PLAY:
356 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000357 break;
358 case ID_PAUSE:
359 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000360 break;
361 case ID_STOP:
george824fd5a1d2005-04-13 13:05:53 +0000362 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000363 break;
364 case ID_PLAYPAUSE:
george82bc999f42005-03-19 11:48:19 +0000365 if (rfbReader) {
366 if (isPaused()) {
367 setPaused(false);
368 } else {
369 setPaused(true);
370 }
george825c13c662005-01-27 14:48:23 +0000371 }
george825c13c662005-01-27 14:48:23 +0000372 break;
george827549df42005-02-08 16:31:02 +0000373 case ID_GOTO:
374 {
375 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000376 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000377 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
378 setPos(gotoTime);
379 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000380 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000381 }
382 }
383 break;
george825c13c662005-01-27 14:48:23 +0000384 case ID_FULLSCREEN:
385 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
386 break;
george8231a36332005-02-06 17:27:34 +0000387 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000388 options.loopPlayback = !options.loopPlayback;
389 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000390 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
391 break;
george824ea27f62005-01-29 15:03:06 +0000392 case ID_RETURN:
393 // Update the speed if return pressed in speedEdit
394 if (speedEdit == GetFocus()) {
395 char speedStr[20], *stopStr;
396 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
397 double speed = strtod(speedStr, &stopStr);
398 if (speed > 0) {
399 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000400 } else {
401 speed = getSpeed();
402 }
403 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000404 }
405 break;
george825e7af742005-03-10 14:26:00 +0000406 case ID_OPTIONS:
407 {
george825a6df072005-03-20 09:56:17 +0000408 OptionsDialog optionsDialog(&options, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000409 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000410 }
411 break;
george8201aa6732005-02-06 17:13:03 +0000412 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000413 PostQuitMessage(0);
414 break;
george8245699e22005-03-29 12:08:52 +0000415 case ID_HOMEPAGE:
george828c72fe72005-03-29 15:05:57 +0000416 ShellExecute(getMainHandle(), _T("open"), _T("http://www.tightvnc.com/"),
417 NULL, NULL, SW_SHOWDEFAULT);
george8245699e22005-03-29 12:08:52 +0000418 break;
george82ef5f7262005-02-08 15:09:26 +0000419 case ID_HELP_COMMANDLINESWITCHES:
george820978ddf2005-03-28 15:53:45 +0000420 {
421 InfoDialog usageDialog(usage_msg);
george82ff5b29f2005-03-29 11:59:35 +0000422 usageDialog.showDialog(getMainHandle());
george820978ddf2005-03-28 15:53:45 +0000423 }
george8259f84532005-02-08 15:01:39 +0000424 break;
george82357c9f52005-03-21 01:28:12 +0000425 case ID_ABOUT:
426 AboutDialog::instance.showDialog();
427 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000428 }
429 break;
430
431 // Update frame's window size and add scrollbars if required
432
433 case WM_SIZE:
434 {
george82d070c692005-01-19 16:44:04 +0000435
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000436 Point old_offset = bufferToClient(Point(0, 0));
437
438 // Update the cached sizing information
439 RECT r;
440 GetClientRect(getMainHandle(), &r);
441 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
442 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
443
444 GetWindowRect(getFrameHandle(), &r);
445 window_size = Rect(r.left, r.top, r.right, r.bottom);
446 GetClientRect(getFrameHandle(), &r);
447 client_size = Rect(r.left, r.top, r.right, r.bottom);
448
449 // Determine whether scrollbars are required
450 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000451
452 // Resize the ToolBar
453 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000454
455 // Redraw if required
456 if (!old_offset.equals(bufferToClient(Point(0, 0))))
457 InvalidateRect(getFrameHandle(), 0, TRUE);
458 }
459 break;
george828a471482005-02-06 07:15:53 +0000460
461 // Process messages from posTrackBar
462
463 case WM_HSCROLL:
464 {
465 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
466 Pos *= sliderStepMs;
467
468 switch (LOWORD(wParam)) {
469 case TB_PAGEUP:
470 case TB_PAGEDOWN:
471 case TB_LINEUP:
472 case TB_LINEDOWN:
473 case TB_THUMBTRACK:
474 sliderDraging = true;
475 updatePos(Pos);
476 return 0;
george82bcc129b2005-03-15 17:11:40 +0000477 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000478 case TB_ENDTRACK:
479 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000480 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000481 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000482 sliderDraging = false;
483 return 0;
484 default:
485 break;
486 }
487 }
488 break;
george829e6e6cc2005-01-29 13:12:05 +0000489
490 case WM_NOTIFY:
491 switch (((NMHDR*)lParam)->code) {
492 case UDN_DELTAPOS:
493 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000494 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000495 char speedStr[20] = "\0";
496 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
497 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
498 double speed;
499
george824ea27f62005-01-29 15:03:06 +0000500 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000501 if (upDown->iDelta > 0) {
502 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
503 } else {
george824ea27f62005-01-29 15:03:06 +0000504 // It's need to round the UpDown position
505 if ((upDown->iPos * 0.5) != getSpeed()) {
506 upDown->iDelta = 0;
507 lResult = TRUE;
508 }
george829e6e6cc2005-01-29 13:12:05 +0000509 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
510 }
george829e6e6cc2005-01-29 13:12:05 +0000511 sprintf(speedStr, "%.2f", speed);
512 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000513 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000514 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000515 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000516 }
george824ea27f62005-01-29 15:03:06 +0000517 }
george829e6e6cc2005-01-29 13:12:05 +0000518 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000519
520 case WM_CLOSE:
521 vlog.debug("WM_CLOSE %x", getMainHandle());
522 PostQuitMessage(0);
523 break;
524 }
525
526 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
527}
528
529LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
530 switch (msg) {
531
532 case WM_PAINT:
533 {
george821e846ff2005-02-24 13:13:33 +0000534 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000535 seekMode = true;
536 return 0;
537 } else {
538 if (seekMode) {
539 seekMode = false;
540 InvalidateRect(getFrameHandle(), 0, true);
541 UpdateWindow(getFrameHandle());
542 return 0;
543 }
544 }
545
546 PAINTSTRUCT ps;
547 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
548 if (!paintDC)
549 throw SystemException("unable to BeginPaint", GetLastError());
550 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
551
552 if (!pr.is_empty()) {
553
554 if (buffer->bitmap) {
555
556 // Get device context
557 BitmapDC bitmapDC(paintDC, buffer->bitmap);
558
559 // Blit the border if required
560 Rect bufpos = bufferToClient(buffer->getRect());
561 if (!pr.enclosed_by(bufpos)) {
562 vlog.debug("draw border");
563 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
564 RECT r;
565 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
566 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
567 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
568 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
569 }
570
571 // Do the blit
572 Point buf_pos = clientToBuffer(pr.tl);
573 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
574 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
575 throw SystemException("unable to BitBlt to window", GetLastError());
576
577 } else {
578 // Blit a load of black
579 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
580 0, 0, 0, BLACKNESS))
581 throw SystemException("unable to BitBlt to blank window", GetLastError());
582 }
583 }
584 EndPaint(getFrameHandle(), &ps);
585 }
586 return 0;
george8203c01da2005-03-16 12:36:53 +0000587
george82aff63ab2005-03-16 13:48:59 +0000588 // Process play/pause by the left mouse button
589 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000590 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
591 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000592
593 case WM_VSCROLL:
594 case WM_HSCROLL:
595 {
596 Point delta;
597 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
598
599 switch (LOWORD(wParam)) {
600 case SB_PAGEUP: newpos -= 50; break;
601 case SB_PAGEDOWN: newpos += 50; break;
602 case SB_LINEUP: newpos -= 5; break;
603 case SB_LINEDOWN: newpos += 5; break;
604 case SB_THUMBTRACK:
605 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
606 default: vlog.info("received unknown scroll message");
607 };
608
609 if (msg == WM_HSCROLL)
610 setViewportOffset(Point(newpos, scrolloffset.y));
611 else
612 setViewportOffset(Point(scrolloffset.x, newpos));
613
614 SCROLLINFO si;
615 si.cbSize = sizeof(si);
616 si.fMask = SIF_POS;
617 si.nPos = newpos;
618 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
619 }
620 break;
621 }
622
623 return DefWindowProc(hwnd, msg, wParam, lParam);
624}
625
george82d070c692005-01-19 16:44:04 +0000626void RfbPlayer::createToolBar(HWND parentHwnd) {
627 RECT tRect;
628 InitCommonControls();
629
630 tb.create(ID_TOOLBAR, parentHwnd);
631 tb.addBitmap(4, IDB_TOOLBAR);
632
633 // Create the control buttons
634 tb.addButton(0, ID_PLAY);
635 tb.addButton(1, ID_PAUSE);
636 tb.addButton(2, ID_STOP);
637 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
638 tb.addButton(3, ID_FULLSCREEN);
639 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
640
641 // Create the static control for the time output
642 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
643 tb.getButtonRect(6, &tRect);
644 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
645 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
646 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
647 GetModuleHandle(0), 0);
648 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
649
650 // Create the trackbar control for the time position
651 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
652 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000653 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000654 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
655 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
656 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
657 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000658 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000659 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
660
661 // Create the label with "Speed:" caption
662 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
663 tb.getButtonRect(10, &tRect);
664 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
665 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
666 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
667
668 // Create the edit control and the spin for the speed managing
669 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
670 tb.getButtonRect(11, &tRect);
671 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
672 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
673 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
674 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
675 // It's need to send notify messages to toolbar parent window
676 SetParent(speedEdit, tb.getHandle());
677
678 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
679 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000680 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000681}
682
george82a21d2952005-02-12 11:30:03 +0000683void RfbPlayer::disableTBandMenuItems() {
684 // Disable the menu items
685 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000686 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000687 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
688 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
george82a21d2952005-02-12 11:30:03 +0000689 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
690 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
691 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
692 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000693 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
694 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
george82a21d2952005-02-12 11:30:03 +0000695
696 // Disable the toolbar buttons and child controls
697 tb.enableButton(ID_PLAY, false);
698 tb.enableButton(ID_PAUSE, false);
699 tb.enableButton(ID_STOP, false);
700 tb.enableButton(ID_FULLSCREEN, false);
701 EnableWindow(posTrackBar, false);
702 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000703 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000704}
705
george82f5043162005-02-12 11:37:18 +0000706void RfbPlayer::enableTBandMenuItems() {
707 // Enable the menu items
708 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000709 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000710 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
711 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
george82f5043162005-02-12 11:37:18 +0000712 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
713 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
714 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
715 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000716 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
717 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
george82f5043162005-02-12 11:37:18 +0000718
719 // Enable the toolbar buttons and child controls
720 tb.enableButton(ID_PLAY, true);
721 tb.enableButton(ID_PAUSE, true);
722 tb.enableButton(ID_STOP, true);
723 tb.enableButton(ID_FULLSCREEN, true);
724 EnableWindow(posTrackBar, true);
725 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000726 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000727}
728
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000729void RfbPlayer::setVisible(bool visible) {
730 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
731 if (visible) {
732 // When the window becomes visible, make it active
733 SetForegroundWindow(getMainHandle());
734 SetActiveWindow(getMainHandle());
735 }
736}
737
738void RfbPlayer::setTitle(const char *title) {
739 char _title[256];
740 strcpy(_title, AppName);
741 strcat(_title, " - ");
742 strcat(_title, title);
743 SetWindowText(getMainHandle(), _title);
744}
745
746void RfbPlayer::setFrameSize(int width, int height) {
747 // Calculate and set required size for main window
748 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000749 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000750 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
751 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
752 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000753 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
754 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
755 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000756 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
757
758 // Enable/disable scrollbars as appropriate
759 calculateScrollBars();
760}
761
762void RfbPlayer::calculateScrollBars() {
763 // Calculate the required size of window
764 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
765 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
766 DWORD old_style;
767 RECT r;
768 SetRect(&r, 0, 0, buffer->width(), buffer->height());
769 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
770 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
771
772 // Work out whether scroll bars are required
773 do {
774 old_style = style;
775
776 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
777 style |= WS_HSCROLL;
778 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
779 }
780 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
781 style |= WS_VSCROLL;
782 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
783 }
784 } while (style != old_style);
785
786 // Tell Windows to update the window style & cached settings
787 if (style != current_style) {
788 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
789 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
790 }
791
792 // Update the scroll settings
793 SCROLLINFO si;
794 if (style & WS_VSCROLL) {
795 si.cbSize = sizeof(si);
796 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
797 si.nMin = 0;
798 si.nMax = buffer->height();
799 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
800 maxscrolloffset.y = max(0, si.nMax-si.nPage);
801 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
802 si.nPos = scrolloffset.y;
803 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
804 }
805 if (style & WS_HSCROLL) {
806 si.cbSize = sizeof(si);
807 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
808 si.nMin = 0;
809 si.nMax = buffer->width();
810 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
811 maxscrolloffset.x = max(0, si.nMax-si.nPage);
812 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
813 si.nPos = scrolloffset.x;
814 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
815 }
george82a758a7a2005-03-15 16:34:57 +0000816
817 // Update the cached client size
818 GetClientRect(getFrameHandle(), &r);
819 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000820}
821
822bool RfbPlayer::setViewportOffset(const Point& tl) {
823/* ***
824 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
825 max(0, min(maxscrolloffset.y, tl.y)));
826 */
827 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
828 max(0, min(tl.y, buffer->height()-client_size.height())));
829 Point delta = np.translate(scrolloffset.negate());
830 if (!np.equals(scrolloffset)) {
831 scrolloffset = np;
832 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
833 UpdateWindow(getFrameHandle());
834 return true;
835 }
836 return false;
837}
838
839void RfbPlayer::close(const char* reason) {
840 setVisible(false);
841 if (reason) {
842 vlog.info("closing - %s", reason);
843 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
844 }
845 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
846}
847
848void RfbPlayer::blankBuffer() {
849 fillRect(buffer->getRect(), 0);
850}
851
852void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000853 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000854 blankBuffer();
855 newSession(fileName);
856 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000857 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000858 if (paused) is->pausePlayback();
859 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000860}
861
862void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000863 // Perform return if waitWhilePaused processed because
864 // rfbReader thread could receive the signal to close
865 if (waitWhilePaused()) return;
866
george8223e08562005-01-31 15:16:42 +0000867 static long update_time = GetTickCount();
868 try {
george828a471482005-02-06 07:15:53 +0000869 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
870 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000871 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000872 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000873 update_time = GetTickCount();
874 }
875 RfbProto::processMsg();
876 } catch (rdr::Exception e) {
877 if (strcmp(e.str(), "[End Of File]") == 0) {
878 rewind();
george825e7af742005-03-10 14:26:00 +0000879 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000880 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000881 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000882 return;
883 }
884 // It's a special exception to perform backward seeking.
885 // We only rewind the stream and seek the offset
886 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000887 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000888 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000889 rewind();
george820d2e19d2005-03-03 15:47:55 +0000890 if (!stopped) setPos(seekOffset);
891 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000892 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000893 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000894 return;
895 }
896 // It's a special exception which is used to terminate the playback
897 if (strcmp(e.str(), "[TERMINATE]") == 0) {
898 sessionTerminateThread *terminate = new sessionTerminateThread(this);
899 terminate->start();
george8223e08562005-01-31 15:16:42 +0000900 } else {
george820e980cc2005-03-10 18:18:34 +0000901 // Show the exception message and close the session playback
902 is->pausePlayback();
903 char message[256] = "\0";
904 strcat(message, e.str());
905 strcat(message, "\nMaybe you force wrong the pixel format for this session");
906 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
907 sessionTerminateThread *terminate = new sessionTerminateThread(this);
908 terminate->start();
george8223e08562005-01-31 15:16:42 +0000909 return;
910 }
911 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000912}
913
george82c7e9f792005-03-20 12:52:46 +0000914long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
915bool ChoosePixelFormatDialog::bigEndian = false;
916
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000917void RfbPlayer::serverInit() {
918 RfbProto::serverInit();
919
george82b95503e2005-02-21 17:02:34 +0000920 // Save the image data start time
921 imageDataStartTime = is->getTimeOffset();
922
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000923 // Resize the backing buffer
924 buffer->setSize(cp.width, cp.height);
925
926 // Check on the true colour mode
927 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000928 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000929
930 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000931 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000932 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000933 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000934 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000935 if (pixelFormatIndex < 0) {
936 options.autoDetectPF = true;
937 options.setPF((PixelFormat *)&cp.pf());
938 } else {
939 options.autoDetectPF = false;
george8210326862005-03-28 12:07:31 +0000940 options.setPF(&supportedPF[pixelFormatIndex]->PF);
george82c7e9f792005-03-20 12:52:46 +0000941 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000942 }
george822c7634b2005-03-10 18:03:27 +0000943 } else {
944 is->pausePlayback();
945 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000946 }
947 } else {
george82dfb557b2005-03-21 18:26:50 +0000948 if (!options.commandLineParam) {
949 if (options.autoDetectPF) {
950 options.setPF((PixelFormat *)&cp.pf());
951 } else {
george8210326862005-03-28 12:07:31 +0000952 options.setPF(&supportedPF[options.pixelFormatIndex]->PF);
george82dfb557b2005-03-21 18:26:50 +0000953 options.pixelFormat.bigEndian = options.bigEndianFlag;
954 }
george820eefed52005-03-29 13:02:21 +0000955 } else if (options.autoDetectPF) {
956 options.setPF((PixelFormat *)&cp.pf());
george825a6df072005-03-20 09:56:17 +0000957 }
george825e7af742005-03-10 14:26:00 +0000958 }
george825a6df072005-03-20 09:56:17 +0000959 cp.setPF(options.pixelFormat);
960 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000961
962 // If the window is not maximised then resize it
963 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
964 setFrameSize(cp.width, cp.height);
965
966 // Set the window title and show it
967 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000968
george82d4d69e62005-02-05 09:23:18 +0000969 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000970 sessionTimeMs = calculateSessionTime(fileName);
971 sprintf(fullSessionTime, "%.2um:%.2us",
972 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000973 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000974 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
975 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000976 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000977
george825e7af742005-03-10 14:26:00 +0000978 setPaused(!options.autoPlay);
979 // Restore the parameters from registry,
980 // which was replaced by command-line parameters.
george82dfb557b2005-03-21 18:26:50 +0000981 if (options.commandLineParam) {
982 options.readFromRegistry();
983 options.commandLineParam = false;
984 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000985}
986
987void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
988 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
989 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
990/* int i;
991 for (i=0;i<count;i++) {
992 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
993 }
994 // *** change to 0, 256?
995 refreshWindowPalette(first, count);
996 palette_changed = true;
997 InvalidateRect(getFrameHandle(), 0, FALSE);*/
998}
999
1000void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +00001001 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001002 MessageBeep(-1);
1003}
1004
1005void RfbPlayer::serverCutText(const char* str, int len) {
1006 if (cutText != NULL)
1007 delete [] cutText;
1008 cutText = new char[len + 1];
1009 memcpy(cutText, str, len);
1010 cutText[len] = '\0';
1011}
1012
1013void RfbPlayer::frameBufferUpdateEnd() {
1014};
1015
1016void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
george8226af7652005-04-13 12:46:25 +00001017 currentEncoding = encoding;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001018}
1019
1020void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
1021}
1022
1023
1024void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
1025 buffer->fillRect(r, pix);
1026 invalidateBufferRect(r);
1027}
1028
1029void RfbPlayer::imageRect(const Rect& r, void* pixels) {
1030 buffer->imageRect(r, pixels);
1031 invalidateBufferRect(r);
1032}
1033
1034void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
1035 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1036 invalidateBufferRect(r);
1037}
1038
1039bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
1040 Rect rect = bufferToClient(crect);
1041 if (rect.intersect(client_size).is_empty()) return false;
1042 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
1043 InvalidateRect(getFrameHandle(), &invalid, FALSE);
1044 return true;
1045}
1046
george820d2e19d2005-03-03 15:47:55 +00001047bool RfbPlayer::waitWhilePaused() {
1048 bool result = false;
1049 while(isPaused() && !isSeeking()) {
1050 Sleep(20);
1051 result = true;
1052 }
1053 return result;
1054}
1055
george8257f13522005-02-05 08:48:22 +00001056long RfbPlayer::calculateSessionTime(char *filename) {
1057 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001058 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001059 try {
1060 while (TRUE) {
1061 sessionFile.skip(1024);
1062 }
1063 } catch (rdr::Exception e) {
1064 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001065 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001066 } else {
1067 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1068 return 0;
1069 }
1070 }
1071 return 0;
1072}
1073
george82ee455792005-04-13 12:56:15 +00001074void RfbPlayer::init() {
1075 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
1076 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
1077}
1078
george826b87aff2005-02-13 10:48:21 +00001079void RfbPlayer::closeSessionFile() {
1080 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001081 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001082 RECT r;
1083
1084 // Uncheck all toolbar buttons
1085 if (tb.getHandle()) {
1086 tb.checkButton(ID_PLAY, false);
1087 tb.checkButton(ID_PAUSE, false);
1088 tb.checkButton(ID_STOP, false);
1089 }
1090
1091 // Stop playback and update the player state
1092 disableTBandMenuItems();
1093 if (rfbReader) {
1094 delete rfbReader->join();
1095 rfbReader = 0;
1096 delete [] fileName;
1097 fileName = 0;
1098 }
1099 blankBuffer();
1100 setTitle("None");
george827009c892005-02-19 12:49:42 +00001101 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001102 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001103 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001104 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1105 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001106 SetWindowText(speedEdit, speedStr);
1107 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1108
1109 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001110 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1111 dwStyle &= ~WS_MAXIMIZE;
1112 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1113 }
george822ff7a612005-02-19 17:05:24 +00001114 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1115 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1116 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001117 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001118 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001119 buffer->setSize(32, 32);
1120 calculateScrollBars();
1121
1122 // Update the cached sizing information and repaint the frame window
1123 GetWindowRect(getFrameHandle(), &r);
1124 window_size = Rect(r.left, r.top, r.right, r.bottom);
1125 GetClientRect(getFrameHandle(), &r);
1126 client_size = Rect(r.left, r.top, r.right, r.bottom);
1127 InvalidateRect(getFrameHandle(), 0, TRUE);
1128 UpdateWindow(getFrameHandle());
1129}
1130
george8217e92cb2005-01-31 16:01:02 +00001131void RfbPlayer::openSessionFile(char *_fileName) {
1132 fileName = strDup(_fileName);
1133
1134 // Close the previous reading thread
1135 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001136 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001137 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001138 }
1139 blankBuffer();
1140 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001141 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001142 rfbReader = new rfbSessionReader(this);
1143 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001144 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001145 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001146}
1147
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001148void RfbPlayer::setPaused(bool paused) {
1149 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001150 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001151 tb.checkButton(ID_PAUSE, true);
1152 tb.checkButton(ID_PLAY, false);
1153 tb.checkButton(ID_STOP, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001154 } else {
george825beb62a2005-02-09 13:04:32 +00001155 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001156 tb.checkButton(ID_PLAY, true);
1157 tb.checkButton(ID_STOP, false);
1158 tb.checkButton(ID_PAUSE, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001159 }
george824fd5a1d2005-04-13 13:05:53 +00001160 tb.enableButton(ID_PAUSE, true);
1161 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001162}
1163
george82006f2792005-02-05 07:40:47 +00001164void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001165 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001166 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001167 if (is) {
1168 is->pausePlayback();
1169 is->interruptFrameDelay();
1170 }
george82006f2792005-02-05 07:40:47 +00001171 tb.checkButton(ID_STOP, true);
1172 tb.checkButton(ID_PLAY, false);
1173 tb.checkButton(ID_PAUSE, false);
george824fd5a1d2005-04-13 13:05:53 +00001174 tb.enableButton(ID_PAUSE, false);
1175 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
george826da02d72005-02-06 17:02:34 +00001176 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001177}
1178
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001179void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001180 if (speed > 0) {
1181 char speedStr[20] = "\0";
1182 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001183 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001184 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001185 SendMessage(speedUpDown, UDM_SETPOS,
1186 0, MAKELONG((short)(newSpeed / 0.5), 0));
1187 sprintf(speedStr, "%.2f", newSpeed);
1188 SetWindowText(speedEdit, speedStr);
1189 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001190}
1191
1192double RfbPlayer::getSpeed() {
1193 return is->getSpeed();
1194}
1195
1196void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001197 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001198}
1199
1200long RfbPlayer::getSeekOffset() {
1201 return is->getSeekOffset();
1202}
1203
1204bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001205 if (is) return is->isSeeking();
1206 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001207}
1208
1209bool RfbPlayer::isSeekMode() {
1210 return seekMode;
1211}
1212
1213bool RfbPlayer::isPaused() {
1214 return is->isPaused();
1215}
1216
1217long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001218 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001219}
1220
george828a471482005-02-06 07:15:53 +00001221void RfbPlayer::updatePos(long newPos) {
1222 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001223 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001224 long time = newPos / 1000;
1225 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001226 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001227
1228 // Update the position of slider
1229 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001230 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1231 sliderStepMs / double(newPos);
1232 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1233 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1234 }
george828a471482005-02-06 07:15:53 +00001235 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001236}
1237
1238void RfbPlayer::skipHandshaking() {
1239 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1240 is->skip(skipBytes);
1241 state_ = RFBSTATE_NORMAL;
1242}
1243
1244void programInfo() {
1245 win32::FileVersionInfo inf;
1246 _tprintf(_T("%s - %s, Version %s\n"),
1247 inf.getVerString(_T("ProductName")),
1248 inf.getVerString(_T("FileDescription")),
1249 inf.getVerString(_T("FileVersion")));
1250 printf("%s\n", buildTime);
1251 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1252}
1253
1254void programUsage() {
george820978ddf2005-03-28 15:53:45 +00001255 InfoDialog usageDialog(usage_msg);
1256 usageDialog.showDialog();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001257}
1258
george825beb62a2005-02-09 13:04:32 +00001259char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001260
1261// playerOptions is the player options with default parameters values,
1262// it is used only for run the player with command-line parameters
1263PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001264bool print_usage = false;
george8205d86232005-03-28 12:16:08 +00001265bool print_upf_list = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001266
1267bool processParams(int argc, char* argv[]) {
george82dfb557b2005-03-21 18:26:50 +00001268 playerOptions.commandLineParam = true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001269 for (int i = 1; i < argc; i++) {
1270 if ((strcasecmp(argv[i], "-help") == 0) ||
1271 (strcasecmp(argv[i], "--help") == 0) ||
1272 (strcasecmp(argv[i], "/help") == 0) ||
1273 (strcasecmp(argv[i], "-h") == 0) ||
1274 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001275 (strcasecmp(argv[i], "/?") == 0) ||
1276 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001277 print_usage = true;
1278 return true;
1279 }
1280
george825caee412005-03-09 09:52:10 +00001281 if ((strcasecmp(argv[i], "-pf") == 0) ||
1282 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george82dfb557b2005-03-21 18:26:50 +00001283 char *pf = argv[++i];
1284 char rgb_order[4] = "\0";
1285 int order = RGB_ORDER;
1286 int r = -1, g = -1, b = -1;
1287 bool big_endian = false;
1288 if (strlen(pf) < 6) return false;
1289 while (strlen(pf)) {
1290 if ((pf[0] == 'r') || (pf[0] == 'R')) {
1291 if (r >=0 ) return false;
1292 r = atoi(++pf);
1293 strcat(rgb_order, "r");
1294 continue;
1295 }
1296 if ((pf[0] == 'g') || (pf[0] == 'G')) {
1297 if (g >=0 ) return false;
1298 g = atoi(++pf);
1299 strcat(rgb_order, "g");
1300 continue;
1301 }
1302 if (((pf[0] == 'b') || (pf[0] == 'B')) &&
1303 (pf[1] != 'e') && (pf[1] != 'E')) {
1304 if (b >=0 ) return false;
1305 b = atoi(++pf);
1306 strcat(rgb_order, "b");
1307 continue;
1308 }
1309 if ((pf[0] == 'l') || (pf[0] == 'L') ||
1310 (pf[0] == 'b') || (pf[0] == 'B')) {
1311 if (strcasecmp(pf, "le") == 0) break;
1312 if (strcasecmp(pf, "be") == 0) { big_endian = true; break;}
1313 return false;
1314 }
1315 pf++;
george82193d8e42005-02-20 16:47:01 +00001316 }
george82888b8fb2005-03-22 15:02:39 +00001317 if ((r < 0) || (g < 0) || (b < 0) || (r + g + b > 32)) return false;
george82dfb557b2005-03-21 18:26:50 +00001318 if (strcasecmp(rgb_order, "rgb") == 0) { order = RGB_ORDER; }
1319 else if (strcasecmp(rgb_order, "rbg") == 0) { order = RBG_ORDER; }
1320 else if (strcasecmp(rgb_order, "grb") == 0) { order = GRB_ORDER; }
1321 else if (strcasecmp(rgb_order, "gbr") == 0) { order = GBR_ORDER; }
1322 else if (strcasecmp(rgb_order, "bgr") == 0) { order = BGR_ORDER; }
1323 else if (strcasecmp(rgb_order, "brg") == 0) { order = BRG_ORDER; }
1324 else return false;
1325 playerOptions.autoDetectPF = false;
1326 playerOptions.setPF(order, r, g, b, big_endian);
george82193d8e42005-02-20 16:47:01 +00001327 continue;
1328 }
1329
george8205d86232005-03-28 12:16:08 +00001330 if ((strcasecmp(argv[i], "-upf") == 0) ||
1331 (strcasecmp(argv[i], "/upf") == 0) && (i < argc-1)) {
1332 if ((i == argc - 1) || (argv[++i][0] == '-')) {
1333 print_upf_list = true;
1334 return true;
1335 }
1336 PixelFormatList userPfList;
1337 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
1338 int index = userPfList.getIndexByPFName(argv[i]);
1339 if (index > 0) {
1340 playerOptions.autoDetectPF = false;
1341 playerOptions.setPF(&userPfList[index]->PF);
1342 } else {
1343 return false;
1344 }
1345 continue;
1346 }
george82193d8e42005-02-20 16:47:01 +00001347
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001348 if ((strcasecmp(argv[i], "-speed") == 0) ||
1349 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001350 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001351 if (playbackSpeed <= 0) {
1352 return false;
1353 }
george825e7af742005-03-10 14:26:00 +00001354 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001355 continue;
1356 }
1357
1358 if ((strcasecmp(argv[i], "-pos") == 0) ||
1359 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001360 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001361 if (initTime <= 0)
1362 return false;
george825e7af742005-03-10 14:26:00 +00001363 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001364 continue;
1365 }
1366
1367 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1368 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001369 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001370 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001371 }
1372
george82ee455792005-04-13 12:56:15 +00001373 if ((strcasecmp(argv[i], "-loop") == 0) ||
1374 (strcasecmp(argv[i], "/loop") == 0) && (i < argc-1)) {
1375 playerOptions.loopPlayback = true;
1376 continue;
1377 }
1378
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001379 if (i != argc - 1)
1380 return false;
1381 }
1382
1383 fileName = strDup(argv[argc-1]);
george822a559f62005-04-11 15:25:36 +00001384 if (fileName[0] == '-') return false;
1385 else return true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001386}
1387
1388//
1389// -=- WinMain
1390//
1391
1392int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1393
1394 // - Process the command-line
1395
1396 int argc = __argc;
1397 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001398 if ((argc > 1) && (!processParams(argc, argv))) {
1399 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1400 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001401 }
george82e6883de2005-02-08 14:42:12 +00001402
1403 if (print_usage) {
1404 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001405 return 0;
george8267cbcd02005-01-16 15:39:56 +00001406 }
george8205d86232005-03-28 12:16:08 +00001407 // Show the user defined pixel formats if required
1408 if (print_upf_list) {
george820978ddf2005-03-28 15:53:45 +00001409 int list_size = 256;
george8205d86232005-03-28 12:16:08 +00001410 char *upf_list = new char[list_size];
1411 PixelFormatList userPfList;
1412 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
george820978ddf2005-03-28 15:53:45 +00001413 strcpy(upf_list, "The list of the user defined pixel formats:\r\n");
george8205d86232005-03-28 12:16:08 +00001414 for (int i = userPfList.getDefaultPFCount(); i < userPfList.count(); i++) {
1415 if ((list_size - strlen(upf_list) - 1) <
1416 (strlen(userPfList[i]->format_name) + 2)) {
1417 char *tmpStr = new char[list_size =
1418 list_size + strlen(userPfList[i]->format_name) + 2];
1419 strcpy(tmpStr, upf_list);
1420 delete [] upf_list;
1421 upf_list = new char[list_size];
1422 strcpy(upf_list, tmpStr);
1423 delete [] tmpStr;
1424 }
1425 strcat(upf_list, userPfList[i]->format_name);
george820978ddf2005-03-28 15:53:45 +00001426 strcat(upf_list, "\r\n");
george8205d86232005-03-28 12:16:08 +00001427 }
george820978ddf2005-03-28 15:53:45 +00001428 InfoDialog upfInfoDialog(upf_list);
1429 upfInfoDialog.showDialog();
george8205d86232005-03-28 12:16:08 +00001430 delete [] upf_list;
1431 return 0;
1432 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001433
george82e6883de2005-02-08 14:42:12 +00001434 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001435 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001436 try {
george825e7af742005-03-10 14:26:00 +00001437 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001438 } catch (rdr::Exception e) {
1439 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1440 delete player;
1441 return 0;
1442 }
1443
1444 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001445 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001446 MSG msg;
1447 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001448 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1449 TranslateMessage(&msg);
1450 DispatchMessage(&msg);
1451 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001452 }
1453
george82e6883de2005-02-08 14:42:12 +00001454 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001455 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001456 if (player) delete player;
1457 } catch (rdr::Exception e) {
1458 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1459 }
1460
1461 return 0;
1462};