blob: ba8450d0cc203e4827e933665d4511a63d828049 [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;
george8231a36332005-02-06 17:27:34 +0000384 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000385 options.loopPlayback = !options.loopPlayback;
386 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000387 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
388 break;
george824ea27f62005-01-29 15:03:06 +0000389 case ID_RETURN:
390 // Update the speed if return pressed in speedEdit
391 if (speedEdit == GetFocus()) {
392 char speedStr[20], *stopStr;
393 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
394 double speed = strtod(speedStr, &stopStr);
395 if (speed > 0) {
396 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000397 } else {
398 speed = getSpeed();
399 }
400 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000401 }
402 break;
george825e7af742005-03-10 14:26:00 +0000403 case ID_OPTIONS:
404 {
george825a6df072005-03-20 09:56:17 +0000405 OptionsDialog optionsDialog(&options, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000406 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000407 }
408 break;
george8201aa6732005-02-06 17:13:03 +0000409 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000410 PostQuitMessage(0);
411 break;
george8245699e22005-03-29 12:08:52 +0000412 case ID_HOMEPAGE:
george828c72fe72005-03-29 15:05:57 +0000413 ShellExecute(getMainHandle(), _T("open"), _T("http://www.tightvnc.com/"),
414 NULL, NULL, SW_SHOWDEFAULT);
george8245699e22005-03-29 12:08:52 +0000415 break;
george82ef5f7262005-02-08 15:09:26 +0000416 case ID_HELP_COMMANDLINESWITCHES:
george820978ddf2005-03-28 15:53:45 +0000417 {
418 InfoDialog usageDialog(usage_msg);
george82ff5b29f2005-03-29 11:59:35 +0000419 usageDialog.showDialog(getMainHandle());
george820978ddf2005-03-28 15:53:45 +0000420 }
george8259f84532005-02-08 15:01:39 +0000421 break;
george82357c9f52005-03-21 01:28:12 +0000422 case ID_ABOUT:
423 AboutDialog::instance.showDialog();
424 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000425 }
426 break;
427
428 // Update frame's window size and add scrollbars if required
429
430 case WM_SIZE:
431 {
george82d070c692005-01-19 16:44:04 +0000432
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000433 Point old_offset = bufferToClient(Point(0, 0));
434
435 // Update the cached sizing information
436 RECT r;
437 GetClientRect(getMainHandle(), &r);
438 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
439 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
440
441 GetWindowRect(getFrameHandle(), &r);
442 window_size = Rect(r.left, r.top, r.right, r.bottom);
443 GetClientRect(getFrameHandle(), &r);
444 client_size = Rect(r.left, r.top, r.right, r.bottom);
445
446 // Determine whether scrollbars are required
447 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000448
449 // Resize the ToolBar
450 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000451
452 // Redraw if required
453 if (!old_offset.equals(bufferToClient(Point(0, 0))))
454 InvalidateRect(getFrameHandle(), 0, TRUE);
455 }
456 break;
george828a471482005-02-06 07:15:53 +0000457
458 // Process messages from posTrackBar
459
460 case WM_HSCROLL:
461 {
462 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
463 Pos *= sliderStepMs;
464
465 switch (LOWORD(wParam)) {
466 case TB_PAGEUP:
467 case TB_PAGEDOWN:
468 case TB_LINEUP:
469 case TB_LINEDOWN:
470 case TB_THUMBTRACK:
471 sliderDraging = true;
472 updatePos(Pos);
473 return 0;
george82bcc129b2005-03-15 17:11:40 +0000474 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000475 case TB_ENDTRACK:
476 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000477 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000478 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000479 sliderDraging = false;
480 return 0;
481 default:
482 break;
483 }
484 }
485 break;
george829e6e6cc2005-01-29 13:12:05 +0000486
487 case WM_NOTIFY:
488 switch (((NMHDR*)lParam)->code) {
489 case UDN_DELTAPOS:
490 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000491 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000492 char speedStr[20] = "\0";
493 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
494 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
495 double speed;
496
george824ea27f62005-01-29 15:03:06 +0000497 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000498 if (upDown->iDelta > 0) {
499 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
500 } else {
george824ea27f62005-01-29 15:03:06 +0000501 // It's need to round the UpDown position
502 if ((upDown->iPos * 0.5) != getSpeed()) {
503 upDown->iDelta = 0;
504 lResult = TRUE;
505 }
george829e6e6cc2005-01-29 13:12:05 +0000506 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
507 }
george829e6e6cc2005-01-29 13:12:05 +0000508 sprintf(speedStr, "%.2f", speed);
509 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000510 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000511 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000512 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000513 }
george824ea27f62005-01-29 15:03:06 +0000514 }
george829e6e6cc2005-01-29 13:12:05 +0000515 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000516
517 case WM_CLOSE:
518 vlog.debug("WM_CLOSE %x", getMainHandle());
519 PostQuitMessage(0);
520 break;
521 }
522
523 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
524}
525
526LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
527 switch (msg) {
528
529 case WM_PAINT:
530 {
george821e846ff2005-02-24 13:13:33 +0000531 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000532 seekMode = true;
533 return 0;
534 } else {
535 if (seekMode) {
536 seekMode = false;
537 InvalidateRect(getFrameHandle(), 0, true);
538 UpdateWindow(getFrameHandle());
539 return 0;
540 }
541 }
542
543 PAINTSTRUCT ps;
544 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
545 if (!paintDC)
546 throw SystemException("unable to BeginPaint", GetLastError());
547 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
548
549 if (!pr.is_empty()) {
550
551 if (buffer->bitmap) {
552
553 // Get device context
554 BitmapDC bitmapDC(paintDC, buffer->bitmap);
555
556 // Blit the border if required
557 Rect bufpos = bufferToClient(buffer->getRect());
558 if (!pr.enclosed_by(bufpos)) {
559 vlog.debug("draw border");
560 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
561 RECT r;
562 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
563 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
564 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
565 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
566 }
567
568 // Do the blit
569 Point buf_pos = clientToBuffer(pr.tl);
570 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
571 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
572 throw SystemException("unable to BitBlt to window", GetLastError());
573
574 } else {
575 // Blit a load of black
576 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
577 0, 0, 0, BLACKNESS))
578 throw SystemException("unable to BitBlt to blank window", GetLastError());
579 }
580 }
581 EndPaint(getFrameHandle(), &ps);
582 }
583 return 0;
george8203c01da2005-03-16 12:36:53 +0000584
george82aff63ab2005-03-16 13:48:59 +0000585 // Process play/pause by the left mouse button
586 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000587 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
588 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000589
590 case WM_VSCROLL:
591 case WM_HSCROLL:
592 {
593 Point delta;
594 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
595
596 switch (LOWORD(wParam)) {
597 case SB_PAGEUP: newpos -= 50; break;
598 case SB_PAGEDOWN: newpos += 50; break;
599 case SB_LINEUP: newpos -= 5; break;
600 case SB_LINEDOWN: newpos += 5; break;
601 case SB_THUMBTRACK:
602 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
603 default: vlog.info("received unknown scroll message");
604 };
605
606 if (msg == WM_HSCROLL)
607 setViewportOffset(Point(newpos, scrolloffset.y));
608 else
609 setViewportOffset(Point(scrolloffset.x, newpos));
610
611 SCROLLINFO si;
612 si.cbSize = sizeof(si);
613 si.fMask = SIF_POS;
614 si.nPos = newpos;
615 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
616 }
617 break;
618 }
619
620 return DefWindowProc(hwnd, msg, wParam, lParam);
621}
622
george82d070c692005-01-19 16:44:04 +0000623void RfbPlayer::createToolBar(HWND parentHwnd) {
624 RECT tRect;
625 InitCommonControls();
626
627 tb.create(ID_TOOLBAR, parentHwnd);
628 tb.addBitmap(4, IDB_TOOLBAR);
629
630 // Create the control buttons
631 tb.addButton(0, ID_PLAY);
632 tb.addButton(1, ID_PAUSE);
633 tb.addButton(2, ID_STOP);
george8201b119e2005-04-17 02:21:00 +0000634/// tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
635/// tb.addButton(3, ID_FULLSCREEN);
george82d070c692005-01-19 16:44:04 +0000636 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
637
638 // Create the static control for the time output
639 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george8201b119e2005-04-17 02:21:00 +0000640 tb.getButtonRect(4, &tRect);
george82d070c692005-01-19 16:44:04 +0000641 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
642 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
643 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
644 GetModuleHandle(0), 0);
645 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
646
647 // Create the trackbar control for the time position
648 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george8201b119e2005-04-17 02:21:00 +0000649 tb.getButtonRect(6, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000650 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000651 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
652 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
653 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
654 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000655 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000656 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
657
658 // Create the label with "Speed:" caption
659 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george8201b119e2005-04-17 02:21:00 +0000660 tb.getButtonRect(8, &tRect);
george82d070c692005-01-19 16:44:04 +0000661 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
662 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
663 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
664
665 // Create the edit control and the spin for the speed managing
666 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george8201b119e2005-04-17 02:21:00 +0000667 tb.getButtonRect(9, &tRect);
george82d070c692005-01-19 16:44:04 +0000668 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
669 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
670 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
671 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
672 // It's need to send notify messages to toolbar parent window
673 SetParent(speedEdit, tb.getHandle());
674
675 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
676 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000677 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000678}
679
george82a21d2952005-02-12 11:30:03 +0000680void RfbPlayer::disableTBandMenuItems() {
681 // Disable the menu items
682 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000683 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000684 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
685 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
george82a21d2952005-02-12 11:30:03 +0000686 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
687 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
688 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
689 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000690 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
691 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
george82a21d2952005-02-12 11:30:03 +0000692
693 // Disable the toolbar buttons and child controls
694 tb.enableButton(ID_PLAY, false);
695 tb.enableButton(ID_PAUSE, false);
696 tb.enableButton(ID_STOP, false);
697 tb.enableButton(ID_FULLSCREEN, false);
698 EnableWindow(posTrackBar, false);
699 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000700 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000701}
702
george82f5043162005-02-12 11:37:18 +0000703void RfbPlayer::enableTBandMenuItems() {
704 // Enable the menu items
705 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000706 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000707 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
708 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
george82f5043162005-02-12 11:37:18 +0000709 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
710 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
711 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
712 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000713 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
714 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
george82f5043162005-02-12 11:37:18 +0000715
716 // Enable the toolbar buttons and child controls
717 tb.enableButton(ID_PLAY, true);
718 tb.enableButton(ID_PAUSE, true);
719 tb.enableButton(ID_STOP, true);
720 tb.enableButton(ID_FULLSCREEN, true);
721 EnableWindow(posTrackBar, true);
722 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000723 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000724}
725
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000726void RfbPlayer::setVisible(bool visible) {
727 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
728 if (visible) {
729 // When the window becomes visible, make it active
730 SetForegroundWindow(getMainHandle());
731 SetActiveWindow(getMainHandle());
732 }
733}
734
735void RfbPlayer::setTitle(const char *title) {
736 char _title[256];
737 strcpy(_title, AppName);
738 strcat(_title, " - ");
739 strcat(_title, title);
740 SetWindowText(getMainHandle(), _title);
741}
742
743void RfbPlayer::setFrameSize(int width, int height) {
744 // Calculate and set required size for main window
745 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000746 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000747 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
748 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
749 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000750 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
751 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
752 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000753 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
754
755 // Enable/disable scrollbars as appropriate
756 calculateScrollBars();
757}
758
759void RfbPlayer::calculateScrollBars() {
760 // Calculate the required size of window
761 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
762 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
763 DWORD old_style;
764 RECT r;
765 SetRect(&r, 0, 0, buffer->width(), buffer->height());
766 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
767 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
768
769 // Work out whether scroll bars are required
770 do {
771 old_style = style;
772
773 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
774 style |= WS_HSCROLL;
775 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
776 }
777 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
778 style |= WS_VSCROLL;
779 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
780 }
781 } while (style != old_style);
782
783 // Tell Windows to update the window style & cached settings
784 if (style != current_style) {
785 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
786 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
787 }
788
789 // Update the scroll settings
790 SCROLLINFO si;
791 if (style & WS_VSCROLL) {
792 si.cbSize = sizeof(si);
793 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
794 si.nMin = 0;
795 si.nMax = buffer->height();
796 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
797 maxscrolloffset.y = max(0, si.nMax-si.nPage);
798 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
799 si.nPos = scrolloffset.y;
800 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
801 }
802 if (style & WS_HSCROLL) {
803 si.cbSize = sizeof(si);
804 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
805 si.nMin = 0;
806 si.nMax = buffer->width();
807 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
808 maxscrolloffset.x = max(0, si.nMax-si.nPage);
809 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
810 si.nPos = scrolloffset.x;
811 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
812 }
george82a758a7a2005-03-15 16:34:57 +0000813
814 // Update the cached client size
815 GetClientRect(getFrameHandle(), &r);
816 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000817}
818
819bool RfbPlayer::setViewportOffset(const Point& tl) {
820/* ***
821 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
822 max(0, min(maxscrolloffset.y, tl.y)));
823 */
824 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
825 max(0, min(tl.y, buffer->height()-client_size.height())));
826 Point delta = np.translate(scrolloffset.negate());
827 if (!np.equals(scrolloffset)) {
828 scrolloffset = np;
829 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
830 UpdateWindow(getFrameHandle());
831 return true;
832 }
833 return false;
834}
835
836void RfbPlayer::close(const char* reason) {
837 setVisible(false);
838 if (reason) {
839 vlog.info("closing - %s", reason);
840 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
841 }
842 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
843}
844
845void RfbPlayer::blankBuffer() {
846 fillRect(buffer->getRect(), 0);
847}
848
849void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000850 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000851 blankBuffer();
852 newSession(fileName);
853 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000854 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000855 if (paused) is->pausePlayback();
856 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000857}
858
859void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000860 // Perform return if waitWhilePaused processed because
861 // rfbReader thread could receive the signal to close
862 if (waitWhilePaused()) return;
863
george8223e08562005-01-31 15:16:42 +0000864 static long update_time = GetTickCount();
865 try {
george828a471482005-02-06 07:15:53 +0000866 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
867 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000868 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000869 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000870 update_time = GetTickCount();
871 }
872 RfbProto::processMsg();
873 } catch (rdr::Exception e) {
874 if (strcmp(e.str(), "[End Of File]") == 0) {
875 rewind();
george825e7af742005-03-10 14:26:00 +0000876 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000877 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000878 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000879 return;
880 }
881 // It's a special exception to perform backward seeking.
882 // We only rewind the stream and seek the offset
883 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000884 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000885 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000886 rewind();
george820d2e19d2005-03-03 15:47:55 +0000887 if (!stopped) setPos(seekOffset);
888 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000889 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000890 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000891 return;
892 }
893 // It's a special exception which is used to terminate the playback
894 if (strcmp(e.str(), "[TERMINATE]") == 0) {
895 sessionTerminateThread *terminate = new sessionTerminateThread(this);
896 terminate->start();
george8223e08562005-01-31 15:16:42 +0000897 } else {
george820e980cc2005-03-10 18:18:34 +0000898 // Show the exception message and close the session playback
899 is->pausePlayback();
900 char message[256] = "\0";
901 strcat(message, e.str());
902 strcat(message, "\nMaybe you force wrong the pixel format for this session");
903 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
904 sessionTerminateThread *terminate = new sessionTerminateThread(this);
905 terminate->start();
george8223e08562005-01-31 15:16:42 +0000906 return;
907 }
908 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000909}
910
george82c7e9f792005-03-20 12:52:46 +0000911long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
912bool ChoosePixelFormatDialog::bigEndian = false;
913
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000914void RfbPlayer::serverInit() {
915 RfbProto::serverInit();
916
george82b95503e2005-02-21 17:02:34 +0000917 // Save the image data start time
918 imageDataStartTime = is->getTimeOffset();
919
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000920 // Resize the backing buffer
921 buffer->setSize(cp.width, cp.height);
922
923 // Check on the true colour mode
924 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000925 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000926
927 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000928 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000929 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000930 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000931 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000932 if (pixelFormatIndex < 0) {
933 options.autoDetectPF = true;
934 options.setPF((PixelFormat *)&cp.pf());
935 } else {
936 options.autoDetectPF = false;
george8210326862005-03-28 12:07:31 +0000937 options.setPF(&supportedPF[pixelFormatIndex]->PF);
george82c7e9f792005-03-20 12:52:46 +0000938 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000939 }
george822c7634b2005-03-10 18:03:27 +0000940 } else {
941 is->pausePlayback();
942 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000943 }
944 } else {
george82dfb557b2005-03-21 18:26:50 +0000945 if (!options.commandLineParam) {
946 if (options.autoDetectPF) {
947 options.setPF((PixelFormat *)&cp.pf());
948 } else {
george8210326862005-03-28 12:07:31 +0000949 options.setPF(&supportedPF[options.pixelFormatIndex]->PF);
george82dfb557b2005-03-21 18:26:50 +0000950 options.pixelFormat.bigEndian = options.bigEndianFlag;
951 }
george820eefed52005-03-29 13:02:21 +0000952 } else if (options.autoDetectPF) {
953 options.setPF((PixelFormat *)&cp.pf());
george825a6df072005-03-20 09:56:17 +0000954 }
george825e7af742005-03-10 14:26:00 +0000955 }
george825a6df072005-03-20 09:56:17 +0000956 cp.setPF(options.pixelFormat);
957 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000958
959 // If the window is not maximised then resize it
960 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
961 setFrameSize(cp.width, cp.height);
962
963 // Set the window title and show it
964 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000965
george82d4d69e62005-02-05 09:23:18 +0000966 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000967 sessionTimeMs = calculateSessionTime(fileName);
968 sprintf(fullSessionTime, "%.2um:%.2us",
969 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000970 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000971 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
972 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000973 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000974
george825e7af742005-03-10 14:26:00 +0000975 setPaused(!options.autoPlay);
976 // Restore the parameters from registry,
977 // which was replaced by command-line parameters.
george82dfb557b2005-03-21 18:26:50 +0000978 if (options.commandLineParam) {
979 options.readFromRegistry();
980 options.commandLineParam = false;
981 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000982}
983
984void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
985 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
986 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
987/* int i;
988 for (i=0;i<count;i++) {
989 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
990 }
991 // *** change to 0, 256?
992 refreshWindowPalette(first, count);
993 palette_changed = true;
994 InvalidateRect(getFrameHandle(), 0, FALSE);*/
995}
996
997void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000998 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000999 MessageBeep(-1);
1000}
1001
1002void RfbPlayer::serverCutText(const char* str, int len) {
1003 if (cutText != NULL)
1004 delete [] cutText;
1005 cutText = new char[len + 1];
1006 memcpy(cutText, str, len);
1007 cutText[len] = '\0';
1008}
1009
1010void RfbPlayer::frameBufferUpdateEnd() {
1011};
1012
1013void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
george8226af7652005-04-13 12:46:25 +00001014 currentEncoding = encoding;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001015}
1016
1017void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
1018}
1019
1020
1021void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
1022 buffer->fillRect(r, pix);
1023 invalidateBufferRect(r);
1024}
1025
1026void RfbPlayer::imageRect(const Rect& r, void* pixels) {
1027 buffer->imageRect(r, pixels);
1028 invalidateBufferRect(r);
1029}
1030
1031void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
1032 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1033 invalidateBufferRect(r);
1034}
1035
1036bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
1037 Rect rect = bufferToClient(crect);
1038 if (rect.intersect(client_size).is_empty()) return false;
1039 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
1040 InvalidateRect(getFrameHandle(), &invalid, FALSE);
1041 return true;
1042}
1043
george820d2e19d2005-03-03 15:47:55 +00001044bool RfbPlayer::waitWhilePaused() {
1045 bool result = false;
1046 while(isPaused() && !isSeeking()) {
1047 Sleep(20);
1048 result = true;
1049 }
1050 return result;
1051}
1052
george8257f13522005-02-05 08:48:22 +00001053long RfbPlayer::calculateSessionTime(char *filename) {
1054 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001055 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001056 try {
1057 while (TRUE) {
1058 sessionFile.skip(1024);
1059 }
1060 } catch (rdr::Exception e) {
1061 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001062 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001063 } else {
1064 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1065 return 0;
1066 }
1067 }
1068 return 0;
1069}
1070
george82ee455792005-04-13 12:56:15 +00001071void RfbPlayer::init() {
1072 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
1073 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
1074}
1075
george826b87aff2005-02-13 10:48:21 +00001076void RfbPlayer::closeSessionFile() {
1077 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001078 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001079 RECT r;
1080
1081 // Uncheck all toolbar buttons
1082 if (tb.getHandle()) {
1083 tb.checkButton(ID_PLAY, false);
1084 tb.checkButton(ID_PAUSE, false);
1085 tb.checkButton(ID_STOP, false);
1086 }
1087
1088 // Stop playback and update the player state
1089 disableTBandMenuItems();
1090 if (rfbReader) {
1091 delete rfbReader->join();
1092 rfbReader = 0;
1093 delete [] fileName;
1094 fileName = 0;
1095 }
1096 blankBuffer();
1097 setTitle("None");
george827009c892005-02-19 12:49:42 +00001098 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001099 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001100 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001101 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1102 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001103 SetWindowText(speedEdit, speedStr);
1104 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1105
1106 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001107 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1108 dwStyle &= ~WS_MAXIMIZE;
1109 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1110 }
george822ff7a612005-02-19 17:05:24 +00001111 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1112 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1113 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001114 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001115 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001116 buffer->setSize(32, 32);
1117 calculateScrollBars();
1118
1119 // Update the cached sizing information and repaint the frame window
1120 GetWindowRect(getFrameHandle(), &r);
1121 window_size = Rect(r.left, r.top, r.right, r.bottom);
1122 GetClientRect(getFrameHandle(), &r);
1123 client_size = Rect(r.left, r.top, r.right, r.bottom);
1124 InvalidateRect(getFrameHandle(), 0, TRUE);
1125 UpdateWindow(getFrameHandle());
1126}
1127
george8217e92cb2005-01-31 16:01:02 +00001128void RfbPlayer::openSessionFile(char *_fileName) {
1129 fileName = strDup(_fileName);
1130
1131 // Close the previous reading thread
1132 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001133 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001134 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001135 }
1136 blankBuffer();
1137 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001138 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001139 rfbReader = new rfbSessionReader(this);
1140 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001141 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001142 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001143}
1144
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001145void RfbPlayer::setPaused(bool paused) {
1146 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001147 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001148 tb.checkButton(ID_PAUSE, true);
1149 tb.checkButton(ID_PLAY, false);
1150 tb.checkButton(ID_STOP, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001151 } else {
george825beb62a2005-02-09 13:04:32 +00001152 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001153 tb.checkButton(ID_PLAY, true);
1154 tb.checkButton(ID_STOP, false);
1155 tb.checkButton(ID_PAUSE, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001156 }
george824fd5a1d2005-04-13 13:05:53 +00001157 tb.enableButton(ID_PAUSE, true);
1158 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001159}
1160
george82006f2792005-02-05 07:40:47 +00001161void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001162 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001163 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001164 if (is) {
1165 is->pausePlayback();
1166 is->interruptFrameDelay();
1167 }
george82006f2792005-02-05 07:40:47 +00001168 tb.checkButton(ID_STOP, true);
1169 tb.checkButton(ID_PLAY, false);
1170 tb.checkButton(ID_PAUSE, false);
george824fd5a1d2005-04-13 13:05:53 +00001171 tb.enableButton(ID_PAUSE, false);
1172 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
george826da02d72005-02-06 17:02:34 +00001173 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001174}
1175
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001176void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001177 if (speed > 0) {
1178 char speedStr[20] = "\0";
1179 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001180 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001181 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001182 SendMessage(speedUpDown, UDM_SETPOS,
1183 0, MAKELONG((short)(newSpeed / 0.5), 0));
1184 sprintf(speedStr, "%.2f", newSpeed);
1185 SetWindowText(speedEdit, speedStr);
1186 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001187}
1188
1189double RfbPlayer::getSpeed() {
1190 return is->getSpeed();
1191}
1192
1193void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001194 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001195}
1196
1197long RfbPlayer::getSeekOffset() {
1198 return is->getSeekOffset();
1199}
1200
1201bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001202 if (is) return is->isSeeking();
1203 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001204}
1205
1206bool RfbPlayer::isSeekMode() {
1207 return seekMode;
1208}
1209
1210bool RfbPlayer::isPaused() {
1211 return is->isPaused();
1212}
1213
1214long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001215 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001216}
1217
george828a471482005-02-06 07:15:53 +00001218void RfbPlayer::updatePos(long newPos) {
1219 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001220 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001221 long time = newPos / 1000;
1222 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001223 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001224
1225 // Update the position of slider
1226 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001227 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1228 sliderStepMs / double(newPos);
1229 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1230 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1231 }
george828a471482005-02-06 07:15:53 +00001232 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001233}
1234
1235void RfbPlayer::skipHandshaking() {
1236 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1237 is->skip(skipBytes);
1238 state_ = RFBSTATE_NORMAL;
1239}
1240
1241void programInfo() {
1242 win32::FileVersionInfo inf;
1243 _tprintf(_T("%s - %s, Version %s\n"),
1244 inf.getVerString(_T("ProductName")),
1245 inf.getVerString(_T("FileDescription")),
1246 inf.getVerString(_T("FileVersion")));
1247 printf("%s\n", buildTime);
1248 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1249}
1250
1251void programUsage() {
george820978ddf2005-03-28 15:53:45 +00001252 InfoDialog usageDialog(usage_msg);
1253 usageDialog.showDialog();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001254}
1255
george825beb62a2005-02-09 13:04:32 +00001256char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001257
1258// playerOptions is the player options with default parameters values,
1259// it is used only for run the player with command-line parameters
1260PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001261bool print_usage = false;
george8205d86232005-03-28 12:16:08 +00001262bool print_upf_list = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001263
1264bool processParams(int argc, char* argv[]) {
george82dfb557b2005-03-21 18:26:50 +00001265 playerOptions.commandLineParam = true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001266 for (int i = 1; i < argc; i++) {
1267 if ((strcasecmp(argv[i], "-help") == 0) ||
1268 (strcasecmp(argv[i], "--help") == 0) ||
1269 (strcasecmp(argv[i], "/help") == 0) ||
1270 (strcasecmp(argv[i], "-h") == 0) ||
1271 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001272 (strcasecmp(argv[i], "/?") == 0) ||
1273 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001274 print_usage = true;
1275 return true;
1276 }
1277
george825caee412005-03-09 09:52:10 +00001278 if ((strcasecmp(argv[i], "-pf") == 0) ||
1279 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george82dfb557b2005-03-21 18:26:50 +00001280 char *pf = argv[++i];
1281 char rgb_order[4] = "\0";
1282 int order = RGB_ORDER;
1283 int r = -1, g = -1, b = -1;
1284 bool big_endian = false;
1285 if (strlen(pf) < 6) return false;
1286 while (strlen(pf)) {
1287 if ((pf[0] == 'r') || (pf[0] == 'R')) {
1288 if (r >=0 ) return false;
1289 r = atoi(++pf);
1290 strcat(rgb_order, "r");
1291 continue;
1292 }
1293 if ((pf[0] == 'g') || (pf[0] == 'G')) {
1294 if (g >=0 ) return false;
1295 g = atoi(++pf);
1296 strcat(rgb_order, "g");
1297 continue;
1298 }
1299 if (((pf[0] == 'b') || (pf[0] == 'B')) &&
1300 (pf[1] != 'e') && (pf[1] != 'E')) {
1301 if (b >=0 ) return false;
1302 b = atoi(++pf);
1303 strcat(rgb_order, "b");
1304 continue;
1305 }
1306 if ((pf[0] == 'l') || (pf[0] == 'L') ||
1307 (pf[0] == 'b') || (pf[0] == 'B')) {
1308 if (strcasecmp(pf, "le") == 0) break;
1309 if (strcasecmp(pf, "be") == 0) { big_endian = true; break;}
1310 return false;
1311 }
1312 pf++;
george82193d8e42005-02-20 16:47:01 +00001313 }
george82888b8fb2005-03-22 15:02:39 +00001314 if ((r < 0) || (g < 0) || (b < 0) || (r + g + b > 32)) return false;
george82dfb557b2005-03-21 18:26:50 +00001315 if (strcasecmp(rgb_order, "rgb") == 0) { order = RGB_ORDER; }
1316 else if (strcasecmp(rgb_order, "rbg") == 0) { order = RBG_ORDER; }
1317 else if (strcasecmp(rgb_order, "grb") == 0) { order = GRB_ORDER; }
1318 else if (strcasecmp(rgb_order, "gbr") == 0) { order = GBR_ORDER; }
1319 else if (strcasecmp(rgb_order, "bgr") == 0) { order = BGR_ORDER; }
1320 else if (strcasecmp(rgb_order, "brg") == 0) { order = BRG_ORDER; }
1321 else return false;
1322 playerOptions.autoDetectPF = false;
1323 playerOptions.setPF(order, r, g, b, big_endian);
george82193d8e42005-02-20 16:47:01 +00001324 continue;
1325 }
1326
george8205d86232005-03-28 12:16:08 +00001327 if ((strcasecmp(argv[i], "-upf") == 0) ||
1328 (strcasecmp(argv[i], "/upf") == 0) && (i < argc-1)) {
1329 if ((i == argc - 1) || (argv[++i][0] == '-')) {
1330 print_upf_list = true;
1331 return true;
1332 }
1333 PixelFormatList userPfList;
1334 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
1335 int index = userPfList.getIndexByPFName(argv[i]);
1336 if (index > 0) {
1337 playerOptions.autoDetectPF = false;
1338 playerOptions.setPF(&userPfList[index]->PF);
1339 } else {
1340 return false;
1341 }
1342 continue;
1343 }
george82193d8e42005-02-20 16:47:01 +00001344
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001345 if ((strcasecmp(argv[i], "-speed") == 0) ||
1346 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001347 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001348 if (playbackSpeed <= 0) {
1349 return false;
1350 }
george825e7af742005-03-10 14:26:00 +00001351 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001352 continue;
1353 }
1354
1355 if ((strcasecmp(argv[i], "-pos") == 0) ||
1356 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001357 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001358 if (initTime <= 0)
1359 return false;
george825e7af742005-03-10 14:26:00 +00001360 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001361 continue;
1362 }
1363
1364 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1365 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001366 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001367 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001368 }
1369
george82ee455792005-04-13 12:56:15 +00001370 if ((strcasecmp(argv[i], "-loop") == 0) ||
1371 (strcasecmp(argv[i], "/loop") == 0) && (i < argc-1)) {
1372 playerOptions.loopPlayback = true;
1373 continue;
1374 }
1375
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001376 if (i != argc - 1)
1377 return false;
1378 }
1379
1380 fileName = strDup(argv[argc-1]);
george822a559f62005-04-11 15:25:36 +00001381 if (fileName[0] == '-') return false;
1382 else return true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001383}
1384
1385//
1386// -=- WinMain
1387//
1388
1389int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1390
1391 // - Process the command-line
1392
1393 int argc = __argc;
1394 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001395 if ((argc > 1) && (!processParams(argc, argv))) {
1396 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1397 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001398 }
george82e6883de2005-02-08 14:42:12 +00001399
1400 if (print_usage) {
1401 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001402 return 0;
george8267cbcd02005-01-16 15:39:56 +00001403 }
george8205d86232005-03-28 12:16:08 +00001404 // Show the user defined pixel formats if required
1405 if (print_upf_list) {
george820978ddf2005-03-28 15:53:45 +00001406 int list_size = 256;
george8205d86232005-03-28 12:16:08 +00001407 char *upf_list = new char[list_size];
1408 PixelFormatList userPfList;
1409 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
george820978ddf2005-03-28 15:53:45 +00001410 strcpy(upf_list, "The list of the user defined pixel formats:\r\n");
george8205d86232005-03-28 12:16:08 +00001411 for (int i = userPfList.getDefaultPFCount(); i < userPfList.count(); i++) {
1412 if ((list_size - strlen(upf_list) - 1) <
1413 (strlen(userPfList[i]->format_name) + 2)) {
1414 char *tmpStr = new char[list_size =
1415 list_size + strlen(userPfList[i]->format_name) + 2];
1416 strcpy(tmpStr, upf_list);
1417 delete [] upf_list;
1418 upf_list = new char[list_size];
1419 strcpy(upf_list, tmpStr);
1420 delete [] tmpStr;
1421 }
1422 strcat(upf_list, userPfList[i]->format_name);
george820978ddf2005-03-28 15:53:45 +00001423 strcat(upf_list, "\r\n");
george8205d86232005-03-28 12:16:08 +00001424 }
george820978ddf2005-03-28 15:53:45 +00001425 InfoDialog upfInfoDialog(upf_list);
1426 upfInfoDialog.showDialog();
george8205d86232005-03-28 12:16:08 +00001427 delete [] upf_list;
1428 return 0;
1429 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001430
george82e6883de2005-02-08 14:42:12 +00001431 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001432 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001433 try {
george825e7af742005-03-10 14:26:00 +00001434 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001435 } catch (rdr::Exception e) {
1436 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1437 delete player;
1438 return 0;
1439 }
1440
1441 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001442 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001443 MSG msg;
1444 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001445 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1446 TranslateMessage(&msg);
1447 DispatchMessage(&msg);
1448 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001449 }
1450
george82e6883de2005-02-08 14:42:12 +00001451 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001452 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001453 if (player) delete player;
1454 } catch (rdr::Exception e) {
1455 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1456 }
1457
1458 return 0;
1459};