blob: 302610599290e4cfda128627f74050c41d7674b1 [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;
george82be7bf092005-04-17 17:21:36 +000041HFONT hFont = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000042
george82e6883de2005-02-08 14:42:12 +000043char wrong_cmd_msg[] =
44 "Wrong command-line parameters!\n"
45 "Use for help: rfbplayer -help";
46
47char usage_msg[] =
george820978ddf2005-03-28 15:53:45 +000048 "usage: rfbplayer <options> <filename>\r\n"
49 "Command-line options:\r\n"
50 " -help \t- Provide usage information.\r\n"
51 " -pf <mode> \t- Forces the pixel format for the session.\r\n"
52 " \t <mode>=r<r_bits>g<g_bits>b<b_bits>[le|be],\r\n"
53 " \t r_bits - size the red component, in bits,\r\n"
54 " \t g_bits - size the green component, in bits,\r\n"
55 " \t b_bits - size the blue component, in bits,\r\n"
56 " \t le - little endian byte order (default),\r\n"
57 " \t be - big endian byte order.\r\n"
58 " \t The r, g, b component is in any order.\r\n"
59 " \t Default: auto detect the pixel format.\r\n"
60 " -upf <name> \t- Forces the user defined pixel format for\r\n"
61 " \t the session. If <name> is empty then application\r\n"
62 " \t shows the list of user defined pixel formats.\r\n"
63 " \t Don't use this option with -pf.\r\n"
64 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\r\n"
65 " \t is double speed, 0.5 is half speed. Default: 1.0.\r\n"
66 " -pos <ms> \t- Sets initial time position in the session file,\r\n"
67 " \t in milliseconds. Default: 0.\r\n"
george82ee455792005-04-13 12:56:15 +000068 " -autoplay \t- Runs the player in the playback mode.\r\n"
69 " -loop \t- Replays the rfb session.";
george82e6883de2005-02-08 14:42:12 +000070
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000071// -=- RfbPlayer's defines
72
73#define strcasecmp _stricmp
george825457d412005-02-19 06:43:09 +000074#define MAX_SPEED 10.00
75#define CALCULATION_ERROR MAX_SPEED / 1000
george82d4d69e62005-02-05 09:23:18 +000076#define MAX_POS_TRACKBAR_RANGE 50
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;
george82be7bf092005-04-17 17:21:36 +0000297 if (hFont) DeleteObject(hFont);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000298 vlog.debug("~RfbPlayer done");
299}
300
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000301LRESULT
302RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
303 switch (msg) {
304
305 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000306
307 case WM_CREATE:
308 {
george82db90d3e2005-04-17 12:01:48 +0000309 createToolBar(hwnd);
310
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000311 // Create the frame window
312 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
george82db90d3e2005-04-17 12:01:48 +0000313 0, WS_CHILD | WS_VISIBLE, 0, tb.getHeight(), 10, tb.getHeight() + 10,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000314 hwnd, 0, frameClass.instance, this);
315
george82006f2792005-02-05 07:40:47 +0000316 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000317
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000318 return 0;
319 }
320
george827214b822004-12-12 07:02:51 +0000321 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000322
323 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000324 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000325 case ID_OPENFILE:
326 {
327 char curDir[_MAX_DIR];
328 static char filename[_MAX_PATH];
329 OPENFILENAME ofn;
330 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
331 GetCurrentDirectory(sizeof(curDir), curDir);
332
333 ofn.lStructSize = sizeof(OPENFILENAME);
334 ofn.hwndOwner = getMainHandle();
335 ofn.lpstrFile = filename;
336 ofn.nMaxFile = sizeof(filename);
337 ofn.lpstrInitialDir = curDir;
george82d7c81be2005-04-11 15:07:13 +0000338 ofn.lpstrFilter = "Rfb Session files (*.rfb, *.fbs)\0*.rfb;*.fbs\0" \
george826e51fcc2005-02-06 13:30:49 +0000339 "All files (*.*)\0*.*\0";
340 ofn.lpstrDefExt = "rfb";
341 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000342 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000343 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000344 }
george826e51fcc2005-02-06 13:30:49 +0000345 }
346 break;
george8271ca1772005-02-13 10:50:46 +0000347 case ID_CLOSEFILE:
348 closeSessionFile();
349 break;
george8226af7652005-04-13 12:46:25 +0000350 case ID_SESSION_INFO:
351 {
352 SessionInfoDialog sessionInfo(&cp, currentEncoding);
353 sessionInfo.showDialog(getMainHandle());
354 }
355 break;
george825c13c662005-01-27 14:48:23 +0000356 case ID_PLAY:
357 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000358 break;
359 case ID_PAUSE:
360 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000361 break;
362 case ID_STOP:
george824fd5a1d2005-04-13 13:05:53 +0000363 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000364 break;
365 case ID_PLAYPAUSE:
george82bc999f42005-03-19 11:48:19 +0000366 if (rfbReader) {
367 if (isPaused()) {
368 setPaused(false);
369 } else {
370 setPaused(true);
371 }
george825c13c662005-01-27 14:48:23 +0000372 }
george825c13c662005-01-27 14:48:23 +0000373 break;
george827549df42005-02-08 16:31:02 +0000374 case ID_GOTO:
375 {
376 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000377 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000378 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
379 setPos(gotoTime);
380 updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000381 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000382 }
383 }
384 break;
george8231a36332005-02-06 17:27:34 +0000385 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000386 options.loopPlayback = !options.loopPlayback;
387 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000388 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
389 break;
george824ea27f62005-01-29 15:03:06 +0000390 case ID_RETURN:
391 // Update the speed if return pressed in speedEdit
392 if (speedEdit == GetFocus()) {
393 char speedStr[20], *stopStr;
394 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
395 double speed = strtod(speedStr, &stopStr);
396 if (speed > 0) {
397 speed = min(MAX_SPEED, speed);
george824ea27f62005-01-29 15:03:06 +0000398 } else {
399 speed = getSpeed();
400 }
401 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000402 }
403 break;
george825e7af742005-03-10 14:26:00 +0000404 case ID_OPTIONS:
405 {
george825a6df072005-03-20 09:56:17 +0000406 OptionsDialog optionsDialog(&options, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000407 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000408 }
409 break;
george8201aa6732005-02-06 17:13:03 +0000410 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000411 PostQuitMessage(0);
412 break;
george8245699e22005-03-29 12:08:52 +0000413 case ID_HOMEPAGE:
george828c72fe72005-03-29 15:05:57 +0000414 ShellExecute(getMainHandle(), _T("open"), _T("http://www.tightvnc.com/"),
415 NULL, NULL, SW_SHOWDEFAULT);
george8245699e22005-03-29 12:08:52 +0000416 break;
george82ef5f7262005-02-08 15:09:26 +0000417 case ID_HELP_COMMANDLINESWITCHES:
george820978ddf2005-03-28 15:53:45 +0000418 {
419 InfoDialog usageDialog(usage_msg);
george82ff5b29f2005-03-29 11:59:35 +0000420 usageDialog.showDialog(getMainHandle());
george820978ddf2005-03-28 15:53:45 +0000421 }
george8259f84532005-02-08 15:01:39 +0000422 break;
george82357c9f52005-03-21 01:28:12 +0000423 case ID_ABOUT:
424 AboutDialog::instance.showDialog();
425 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000426 }
427 break;
428
429 // Update frame's window size and add scrollbars if required
430
431 case WM_SIZE:
432 {
george82d070c692005-01-19 16:44:04 +0000433
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000434 Point old_offset = bufferToClient(Point(0, 0));
435
436 // Update the cached sizing information
437 RECT r;
438 GetClientRect(getMainHandle(), &r);
george82db90d3e2005-04-17 12:01:48 +0000439 MoveWindow(getFrameHandle(), 0, tb.getHeight(), r.right - r.left,
440 r.bottom - r.top - tb.getHeight(), TRUE);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000441
442 GetWindowRect(getFrameHandle(), &r);
443 window_size = Rect(r.left, r.top, r.right, r.bottom);
444 GetClientRect(getFrameHandle(), &r);
445 client_size = Rect(r.left, r.top, r.right, r.bottom);
446
447 // Determine whether scrollbars are required
448 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000449
450 // Resize the ToolBar
451 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000452
453 // Redraw if required
454 if (!old_offset.equals(bufferToClient(Point(0, 0))))
455 InvalidateRect(getFrameHandle(), 0, TRUE);
456 }
457 break;
george828a471482005-02-06 07:15:53 +0000458
459 // Process messages from posTrackBar
460
461 case WM_HSCROLL:
462 {
463 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
464 Pos *= sliderStepMs;
465
466 switch (LOWORD(wParam)) {
467 case TB_PAGEUP:
468 case TB_PAGEDOWN:
469 case TB_LINEUP:
470 case TB_LINEDOWN:
471 case TB_THUMBTRACK:
472 sliderDraging = true;
473 updatePos(Pos);
474 return 0;
george82bcc129b2005-03-15 17:11:40 +0000475 case TB_THUMBPOSITION:
george828a471482005-02-06 07:15:53 +0000476 case TB_ENDTRACK:
477 setPos(Pos);
george82a6900d72005-02-21 17:24:26 +0000478 setPaused(isPaused());;
george82bcc129b2005-03-15 17:11:40 +0000479 updatePos(Pos);
george828a471482005-02-06 07:15:53 +0000480 sliderDraging = false;
481 return 0;
482 default:
483 break;
484 }
485 }
486 break;
george829e6e6cc2005-01-29 13:12:05 +0000487
488 case WM_NOTIFY:
489 switch (((NMHDR*)lParam)->code) {
490 case UDN_DELTAPOS:
491 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000492 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000493 char speedStr[20] = "\0";
494 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
495 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
496 double speed;
497
george824ea27f62005-01-29 15:03:06 +0000498 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000499 if (upDown->iDelta > 0) {
500 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
501 } else {
george824ea27f62005-01-29 15:03:06 +0000502 // It's need to round the UpDown position
503 if ((upDown->iPos * 0.5) != getSpeed()) {
504 upDown->iDelta = 0;
505 lResult = TRUE;
506 }
george829e6e6cc2005-01-29 13:12:05 +0000507 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
508 }
george829e6e6cc2005-01-29 13:12:05 +0000509 sprintf(speedStr, "%.2f", speed);
510 SetWindowText(speedEdit, speedStr);
george825f326fe2005-02-20 08:01:01 +0000511 is->setSpeed(speed);
george825e7af742005-03-10 14:26:00 +0000512 options.playbackSpeed = speed;
george824ea27f62005-01-29 15:03:06 +0000513 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000514 }
george824ea27f62005-01-29 15:03:06 +0000515 }
george829e6e6cc2005-01-29 13:12:05 +0000516 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000517
518 case WM_CLOSE:
519 vlog.debug("WM_CLOSE %x", getMainHandle());
520 PostQuitMessage(0);
521 break;
522 }
523
524 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
525}
526
527LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
528 switch (msg) {
529
530 case WM_PAINT:
531 {
george821e846ff2005-02-24 13:13:33 +0000532 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000533 seekMode = true;
534 return 0;
535 } else {
536 if (seekMode) {
537 seekMode = false;
538 InvalidateRect(getFrameHandle(), 0, true);
539 UpdateWindow(getFrameHandle());
540 return 0;
541 }
542 }
543
544 PAINTSTRUCT ps;
545 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
546 if (!paintDC)
547 throw SystemException("unable to BeginPaint", GetLastError());
548 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
549
550 if (!pr.is_empty()) {
551
552 if (buffer->bitmap) {
553
554 // Get device context
555 BitmapDC bitmapDC(paintDC, buffer->bitmap);
556
557 // Blit the border if required
558 Rect bufpos = bufferToClient(buffer->getRect());
559 if (!pr.enclosed_by(bufpos)) {
560 vlog.debug("draw border");
561 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
562 RECT r;
563 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
564 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
565 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
566 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
567 }
568
569 // Do the blit
570 Point buf_pos = clientToBuffer(pr.tl);
571 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
572 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
573 throw SystemException("unable to BitBlt to window", GetLastError());
574
575 } else {
576 // Blit a load of black
577 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
578 0, 0, 0, BLACKNESS))
579 throw SystemException("unable to BitBlt to blank window", GetLastError());
580 }
581 }
582 EndPaint(getFrameHandle(), &ps);
583 }
584 return 0;
george8203c01da2005-03-16 12:36:53 +0000585
george82aff63ab2005-03-16 13:48:59 +0000586 // Process play/pause by the left mouse button
587 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000588 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
589 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000590
591 case WM_VSCROLL:
592 case WM_HSCROLL:
593 {
594 Point delta;
595 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
596
597 switch (LOWORD(wParam)) {
598 case SB_PAGEUP: newpos -= 50; break;
599 case SB_PAGEDOWN: newpos += 50; break;
600 case SB_LINEUP: newpos -= 5; break;
601 case SB_LINEDOWN: newpos += 5; break;
602 case SB_THUMBTRACK:
603 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
604 default: vlog.info("received unknown scroll message");
605 };
606
607 if (msg == WM_HSCROLL)
608 setViewportOffset(Point(newpos, scrolloffset.y));
609 else
610 setViewportOffset(Point(scrolloffset.x, newpos));
611
612 SCROLLINFO si;
613 si.cbSize = sizeof(si);
614 si.fMask = SIF_POS;
615 si.nPos = newpos;
616 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
617 }
618 break;
619 }
620
621 return DefWindowProc(hwnd, msg, wParam, lParam);
622}
623
george82d070c692005-01-19 16:44:04 +0000624void RfbPlayer::createToolBar(HWND parentHwnd) {
george82be7bf092005-04-17 17:21:36 +0000625 HDC hdc;
626 SIZE sz;
george82d070c692005-01-19 16:44:04 +0000627 RECT tRect;
george82be7bf092005-04-17 17:21:36 +0000628 NONCLIENTMETRICS nonClientMetrics;
629
630 // Get the default font for the main menu
631 nonClientMetrics.cbSize = sizeof(NONCLIENTMETRICS);
632 if (!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &nonClientMetrics, 0))
633 MessageBox(getMainHandle(), "Can't access to the system font.",
634 "RfbPlayer", MB_OK | MB_ICONERROR);
635 nonClientMetrics.lfMenuFont.lfHeight = 16;
636 hFont = CreateFontIndirect(&nonClientMetrics.lfMenuFont);
george82d070c692005-01-19 16:44:04 +0000637
george82db90d3e2005-04-17 12:01:48 +0000638 tb.create(ID_TOOLBAR, parentHwnd, WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | CCS_NORESIZE);
george82d070c692005-01-19 16:44:04 +0000639 tb.addBitmap(4, IDB_TOOLBAR);
640
641 // Create the control buttons
642 tb.addButton(0, ID_PLAY);
643 tb.addButton(1, ID_PAUSE);
644 tb.addButton(2, ID_STOP);
george82d070c692005-01-19 16:44:04 +0000645 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
646
647 // Create the static control for the time output
george82d070c692005-01-19 16:44:04 +0000648 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
george82be7bf092005-04-17 17:21:36 +0000649 WS_CHILD | WS_VISIBLE, 0, 0, 20, 20, tb.getHandle(), (HMENU)ID_TIME_STATIC,
george82d070c692005-01-19 16:44:04 +0000650 GetModuleHandle(0), 0);
george82be7bf092005-04-17 17:21:36 +0000651 SendMessage(timeStatic, WM_SETFONT,(WPARAM) hFont, TRUE);
652 hdc = GetDC(timeStatic);
653 SelectObject(hdc, hFont);
654 GetTextExtentPoint32(hdc, "00m:00s (00m:00s)", 16, &sz);
655 tb.addButton(sz.cx + 10, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george82d070c692005-01-19 16:44:04 +0000656 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
george82be7bf092005-04-17 17:21:36 +0000657 tb.getButtonRect(4, &tRect);
658 MoveWindow(timeStatic, tRect.left, tRect.top+2, tRect.right-tRect.left,
659 tRect.bottom-tRect.top, FALSE);
george82d070c692005-01-19 16:44:04 +0000660
661 // Create the trackbar control for the time position
662 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george8201b119e2005-04-17 02:21:00 +0000663 tb.getButtonRect(6, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000664 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000665 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
666 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
667 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
668 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000669 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000670 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
671
672 // Create the label with "Speed:" caption
george82be7bf092005-04-17 17:21:36 +0000673 HWND speedStatic = CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
674 0, 0, 5, 5, tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
675 SendMessage(speedStatic, WM_SETFONT,(WPARAM) hFont, TRUE);
676 hdc = GetDC(speedStatic);
677 SelectObject(hdc, hFont);
678 GetTextExtentPoint32(hdc, "Speed:", 6, &sz);
679 tb.addButton(sz.cx + 10, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george8201b119e2005-04-17 02:21:00 +0000680 tb.getButtonRect(8, &tRect);
george82be7bf092005-04-17 17:21:36 +0000681 MoveWindow(speedStatic, tRect.left, tRect.top+2, tRect.right-tRect.left,
682 tRect.bottom-tRect.top, FALSE);
george82d070c692005-01-19 16:44:04 +0000683
684 // Create the edit control and the spin for the speed managing
685 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
george8201b119e2005-04-17 02:21:00 +0000686 tb.getButtonRect(9, &tRect);
george82d070c692005-01-19 16:44:04 +0000687 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
688 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
689 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
690 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
george82be7bf092005-04-17 17:21:36 +0000691 SendMessage(speedEdit, WM_SETFONT,(WPARAM) hFont, TRUE);
george82d070c692005-01-19 16:44:04 +0000692 // It's need to send notify messages to toolbar parent window
693 SetParent(speedEdit, tb.getHandle());
694
695 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
696 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000697 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000698}
699
george82a21d2952005-02-12 11:30:03 +0000700void RfbPlayer::disableTBandMenuItems() {
701 // Disable the menu items
702 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000703 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000704 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
705 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
george82a21d2952005-02-12 11:30:03 +0000706 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
707 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
708 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
709 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000710 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
711 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
george82a21d2952005-02-12 11:30:03 +0000712
713 // Disable the toolbar buttons and child controls
714 tb.enableButton(ID_PLAY, false);
715 tb.enableButton(ID_PAUSE, false);
716 tb.enableButton(ID_STOP, false);
717 tb.enableButton(ID_FULLSCREEN, false);
718 EnableWindow(posTrackBar, false);
719 EnableWindow(speedEdit, false);
george82e0a28ab2005-02-19 06:54:47 +0000720 EnableWindow(speedUpDown, false);
george82a21d2952005-02-12 11:30:03 +0000721}
722
george82f5043162005-02-12 11:37:18 +0000723void RfbPlayer::enableTBandMenuItems() {
724 // Enable the menu items
725 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000726 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000727 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
728 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
george82f5043162005-02-12 11:37:18 +0000729 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
730 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
731 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
732 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000733 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
734 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
george82f5043162005-02-12 11:37:18 +0000735
736 // Enable the toolbar buttons and child controls
737 tb.enableButton(ID_PLAY, true);
738 tb.enableButton(ID_PAUSE, true);
739 tb.enableButton(ID_STOP, true);
740 tb.enableButton(ID_FULLSCREEN, true);
741 EnableWindow(posTrackBar, true);
742 EnableWindow(speedEdit, true);
george82e0a28ab2005-02-19 06:54:47 +0000743 EnableWindow(speedUpDown, true);
george82f5043162005-02-12 11:37:18 +0000744}
745
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000746void RfbPlayer::setVisible(bool visible) {
747 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
748 if (visible) {
749 // When the window becomes visible, make it active
750 SetForegroundWindow(getMainHandle());
751 SetActiveWindow(getMainHandle());
752 }
753}
754
755void RfbPlayer::setTitle(const char *title) {
756 char _title[256];
757 strcpy(_title, AppName);
758 strcat(_title, " - ");
759 strcat(_title, title);
760 SetWindowText(getMainHandle(), _title);
761}
762
763void RfbPlayer::setFrameSize(int width, int height) {
764 // Calculate and set required size for main window
765 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000766 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000767 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
george82db90d3e2005-04-17 12:01:48 +0000768 r.bottom += tb.getHeight(); // Include RfbPlayr's controls area
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000769 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000770 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
771 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
772 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000773 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
774
775 // Enable/disable scrollbars as appropriate
776 calculateScrollBars();
777}
778
779void RfbPlayer::calculateScrollBars() {
780 // Calculate the required size of window
781 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
782 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
783 DWORD old_style;
784 RECT r;
785 SetRect(&r, 0, 0, buffer->width(), buffer->height());
786 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
787 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
788
789 // Work out whether scroll bars are required
790 do {
791 old_style = style;
792
793 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
794 style |= WS_HSCROLL;
795 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
796 }
797 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
798 style |= WS_VSCROLL;
799 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
800 }
801 } while (style != old_style);
802
803 // Tell Windows to update the window style & cached settings
804 if (style != current_style) {
805 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
806 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
807 }
808
809 // Update the scroll settings
810 SCROLLINFO si;
811 if (style & WS_VSCROLL) {
812 si.cbSize = sizeof(si);
813 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
814 si.nMin = 0;
815 si.nMax = buffer->height();
816 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
817 maxscrolloffset.y = max(0, si.nMax-si.nPage);
818 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
819 si.nPos = scrolloffset.y;
820 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
821 }
822 if (style & WS_HSCROLL) {
823 si.cbSize = sizeof(si);
824 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
825 si.nMin = 0;
826 si.nMax = buffer->width();
827 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
828 maxscrolloffset.x = max(0, si.nMax-si.nPage);
829 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
830 si.nPos = scrolloffset.x;
831 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
832 }
george82a758a7a2005-03-15 16:34:57 +0000833
834 // Update the cached client size
835 GetClientRect(getFrameHandle(), &r);
836 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000837}
838
839bool RfbPlayer::setViewportOffset(const Point& tl) {
840/* ***
841 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
842 max(0, min(maxscrolloffset.y, tl.y)));
843 */
844 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
845 max(0, min(tl.y, buffer->height()-client_size.height())));
846 Point delta = np.translate(scrolloffset.negate());
847 if (!np.equals(scrolloffset)) {
848 scrolloffset = np;
849 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
850 UpdateWindow(getFrameHandle());
851 return true;
852 }
853 return false;
854}
855
856void RfbPlayer::close(const char* reason) {
857 setVisible(false);
858 if (reason) {
859 vlog.info("closing - %s", reason);
860 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
861 }
862 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
863}
864
865void RfbPlayer::blankBuffer() {
866 fillRect(buffer->getRect(), 0);
867}
868
869void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000870 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000871 blankBuffer();
872 newSession(fileName);
873 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000874 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000875 if (paused) is->pausePlayback();
876 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000877}
878
879void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000880 // Perform return if waitWhilePaused processed because
881 // rfbReader thread could receive the signal to close
882 if (waitWhilePaused()) return;
883
george8223e08562005-01-31 15:16:42 +0000884 static long update_time = GetTickCount();
885 try {
george828a471482005-02-06 07:15:53 +0000886 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
887 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000888 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000889 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000890 update_time = GetTickCount();
891 }
892 RfbProto::processMsg();
893 } catch (rdr::Exception e) {
894 if (strcmp(e.str(), "[End Of File]") == 0) {
895 rewind();
george825e7af742005-03-10 14:26:00 +0000896 setPaused(!options.loopPlayback);
george828a471482005-02-06 07:15:53 +0000897 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000898 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000899 return;
900 }
901 // It's a special exception to perform backward seeking.
902 // We only rewind the stream and seek the offset
903 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000904 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000905 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000906 rewind();
george820d2e19d2005-03-03 15:47:55 +0000907 if (!stopped) setPos(seekOffset);
908 else stopped = false;
george823104aec2005-02-21 13:20:56 +0000909 updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000910 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000911 return;
912 }
913 // It's a special exception which is used to terminate the playback
914 if (strcmp(e.str(), "[TERMINATE]") == 0) {
915 sessionTerminateThread *terminate = new sessionTerminateThread(this);
916 terminate->start();
george8223e08562005-01-31 15:16:42 +0000917 } else {
george820e980cc2005-03-10 18:18:34 +0000918 // Show the exception message and close the session playback
919 is->pausePlayback();
920 char message[256] = "\0";
921 strcat(message, e.str());
922 strcat(message, "\nMaybe you force wrong the pixel format for this session");
923 MessageBox(getMainHandle(), message, e.type(), MB_OK | MB_ICONERROR);
924 sessionTerminateThread *terminate = new sessionTerminateThread(this);
925 terminate->start();
george8223e08562005-01-31 15:16:42 +0000926 return;
927 }
928 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000929}
930
george82c7e9f792005-03-20 12:52:46 +0000931long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
932bool ChoosePixelFormatDialog::bigEndian = false;
933
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000934void RfbPlayer::serverInit() {
935 RfbProto::serverInit();
936
george82b95503e2005-02-21 17:02:34 +0000937 // Save the image data start time
938 imageDataStartTime = is->getTimeOffset();
939
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000940 // Resize the backing buffer
941 buffer->setSize(cp.width, cp.height);
942
943 // Check on the true colour mode
944 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000945 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000946
947 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000948 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000949 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000950 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000951 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000952 if (pixelFormatIndex < 0) {
953 options.autoDetectPF = true;
954 options.setPF((PixelFormat *)&cp.pf());
955 } else {
956 options.autoDetectPF = false;
george8210326862005-03-28 12:07:31 +0000957 options.setPF(&supportedPF[pixelFormatIndex]->PF);
george82c7e9f792005-03-20 12:52:46 +0000958 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000959 }
george822c7634b2005-03-10 18:03:27 +0000960 } else {
961 is->pausePlayback();
962 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000963 }
964 } else {
george82dfb557b2005-03-21 18:26:50 +0000965 if (!options.commandLineParam) {
966 if (options.autoDetectPF) {
967 options.setPF((PixelFormat *)&cp.pf());
968 } else {
george8210326862005-03-28 12:07:31 +0000969 options.setPF(&supportedPF[options.pixelFormatIndex]->PF);
george82dfb557b2005-03-21 18:26:50 +0000970 options.pixelFormat.bigEndian = options.bigEndianFlag;
971 }
george820eefed52005-03-29 13:02:21 +0000972 } else if (options.autoDetectPF) {
973 options.setPF((PixelFormat *)&cp.pf());
george825a6df072005-03-20 09:56:17 +0000974 }
george825e7af742005-03-10 14:26:00 +0000975 }
george825a6df072005-03-20 09:56:17 +0000976 cp.setPF(options.pixelFormat);
977 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000978
979 // If the window is not maximised then resize it
980 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
981 setFrameSize(cp.width, cp.height);
982
983 // Set the window title and show it
984 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000985
george82d4d69e62005-02-05 09:23:18 +0000986 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000987 sessionTimeMs = calculateSessionTime(fileName);
988 sprintf(fullSessionTime, "%.2um:%.2us",
989 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000990 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000991 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
992 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000993 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000994
george825e7af742005-03-10 14:26:00 +0000995 setPaused(!options.autoPlay);
996 // Restore the parameters from registry,
997 // which was replaced by command-line parameters.
george82dfb557b2005-03-21 18:26:50 +0000998 if (options.commandLineParam) {
999 options.readFromRegistry();
1000 options.commandLineParam = false;
1001 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001002}
1003
1004void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
1005 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
1006 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
1007/* int i;
1008 for (i=0;i<count;i++) {
1009 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
1010 }
1011 // *** change to 0, 256?
1012 refreshWindowPalette(first, count);
1013 palette_changed = true;
1014 InvalidateRect(getFrameHandle(), 0, FALSE);*/
1015}
1016
1017void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +00001018 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001019 MessageBeep(-1);
1020}
1021
1022void RfbPlayer::serverCutText(const char* str, int len) {
1023 if (cutText != NULL)
1024 delete [] cutText;
1025 cutText = new char[len + 1];
1026 memcpy(cutText, str, len);
1027 cutText[len] = '\0';
1028}
1029
1030void RfbPlayer::frameBufferUpdateEnd() {
1031};
1032
1033void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
george8226af7652005-04-13 12:46:25 +00001034 currentEncoding = encoding;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001035}
1036
1037void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
1038}
1039
1040
1041void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
1042 buffer->fillRect(r, pix);
1043 invalidateBufferRect(r);
1044}
1045
1046void RfbPlayer::imageRect(const Rect& r, void* pixels) {
1047 buffer->imageRect(r, pixels);
1048 invalidateBufferRect(r);
1049}
1050
1051void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
1052 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
1053 invalidateBufferRect(r);
1054}
1055
1056bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
1057 Rect rect = bufferToClient(crect);
1058 if (rect.intersect(client_size).is_empty()) return false;
1059 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
1060 InvalidateRect(getFrameHandle(), &invalid, FALSE);
1061 return true;
1062}
1063
george820d2e19d2005-03-03 15:47:55 +00001064bool RfbPlayer::waitWhilePaused() {
1065 bool result = false;
1066 while(isPaused() && !isSeeking()) {
1067 Sleep(20);
1068 result = true;
1069 }
1070 return result;
1071}
1072
george8257f13522005-02-05 08:48:22 +00001073long RfbPlayer::calculateSessionTime(char *filename) {
1074 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +00001075 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +00001076 try {
1077 while (TRUE) {
1078 sessionFile.skip(1024);
1079 }
1080 } catch (rdr::Exception e) {
1081 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +00001082 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +00001083 } else {
1084 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
1085 return 0;
1086 }
1087 }
1088 return 0;
1089}
1090
george82ee455792005-04-13 12:56:15 +00001091void RfbPlayer::init() {
1092 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
1093 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
1094}
1095
george826b87aff2005-02-13 10:48:21 +00001096void RfbPlayer::closeSessionFile() {
1097 char speedStr[10];
george820bdb2842005-02-19 13:17:58 +00001098 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +00001099 RECT r;
1100
1101 // Uncheck all toolbar buttons
1102 if (tb.getHandle()) {
1103 tb.checkButton(ID_PLAY, false);
1104 tb.checkButton(ID_PAUSE, false);
1105 tb.checkButton(ID_STOP, false);
1106 }
1107
1108 // Stop playback and update the player state
1109 disableTBandMenuItems();
1110 if (rfbReader) {
1111 delete rfbReader->join();
1112 rfbReader = 0;
1113 delete [] fileName;
1114 fileName = 0;
1115 }
1116 blankBuffer();
1117 setTitle("None");
george827009c892005-02-19 12:49:42 +00001118 SetWindowText(timeStatic,"00m:00s (00m:00s)");
george825e7af742005-03-10 14:26:00 +00001119 options.playbackSpeed = 1.0;
george826b87aff2005-02-13 10:48:21 +00001120 SendMessage(speedUpDown, UDM_SETPOS,
george825e7af742005-03-10 14:26:00 +00001121 0, MAKELONG((short)(options.playbackSpeed / 0.5), 0));
1122 sprintf(speedStr, "%.2f", options.playbackSpeed);
george826b87aff2005-02-13 10:48:21 +00001123 SetWindowText(speedEdit, speedStr);
1124 SendMessage(posTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 0));
1125
1126 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +00001127 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
1128 dwStyle &= ~WS_MAXIMIZE;
1129 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
1130 }
george822ff7a612005-02-19 17:05:24 +00001131 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
1132 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
1133 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +00001134 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +00001135 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +00001136 buffer->setSize(32, 32);
1137 calculateScrollBars();
1138
1139 // Update the cached sizing information and repaint the frame window
1140 GetWindowRect(getFrameHandle(), &r);
1141 window_size = Rect(r.left, r.top, r.right, r.bottom);
1142 GetClientRect(getFrameHandle(), &r);
1143 client_size = Rect(r.left, r.top, r.right, r.bottom);
1144 InvalidateRect(getFrameHandle(), 0, TRUE);
1145 UpdateWindow(getFrameHandle());
1146}
1147
george8217e92cb2005-01-31 16:01:02 +00001148void RfbPlayer::openSessionFile(char *_fileName) {
1149 fileName = strDup(_fileName);
1150
1151 // Close the previous reading thread
1152 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +00001153 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +00001154 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +00001155 }
1156 blankBuffer();
1157 newSession(fileName);
george825e7af742005-03-10 14:26:00 +00001158 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +00001159 rfbReader = new rfbSessionReader(this);
1160 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +00001161 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8263ebbcc2005-02-12 12:09:13 +00001162 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +00001163}
1164
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001165void RfbPlayer::setPaused(bool paused) {
1166 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001167 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +00001168 tb.checkButton(ID_PAUSE, true);
1169 tb.checkButton(ID_PLAY, false);
1170 tb.checkButton(ID_STOP, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001171 } else {
george825beb62a2005-02-09 13:04:32 +00001172 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +00001173 tb.checkButton(ID_PLAY, true);
1174 tb.checkButton(ID_STOP, false);
1175 tb.checkButton(ID_PAUSE, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001176 }
george824fd5a1d2005-04-13 13:05:53 +00001177 tb.enableButton(ID_PAUSE, true);
1178 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001179}
1180
george82006f2792005-02-05 07:40:47 +00001181void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001182 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001183 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001184 if (is) {
1185 is->pausePlayback();
1186 is->interruptFrameDelay();
1187 }
george82006f2792005-02-05 07:40:47 +00001188 tb.checkButton(ID_STOP, true);
1189 tb.checkButton(ID_PLAY, false);
1190 tb.checkButton(ID_PAUSE, false);
george824fd5a1d2005-04-13 13:05:53 +00001191 tb.enableButton(ID_PAUSE, false);
1192 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
george826da02d72005-02-06 17:02:34 +00001193 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +00001194}
1195
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001196void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001197 if (speed > 0) {
1198 char speedStr[20] = "\0";
1199 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001200 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001201 options.playbackSpeed = newSpeed;
george825f326fe2005-02-20 08:01:01 +00001202 SendMessage(speedUpDown, UDM_SETPOS,
1203 0, MAKELONG((short)(newSpeed / 0.5), 0));
1204 sprintf(speedStr, "%.2f", newSpeed);
1205 SetWindowText(speedEdit, speedStr);
1206 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001207}
1208
1209double RfbPlayer::getSpeed() {
1210 return is->getSpeed();
1211}
1212
1213void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001214 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001215}
1216
1217long RfbPlayer::getSeekOffset() {
1218 return is->getSeekOffset();
1219}
1220
1221bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001222 if (is) return is->isSeeking();
1223 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001224}
1225
1226bool RfbPlayer::isSeekMode() {
1227 return seekMode;
1228}
1229
1230bool RfbPlayer::isPaused() {
1231 return is->isPaused();
1232}
1233
1234long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001235 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001236}
1237
george828a471482005-02-06 07:15:53 +00001238void RfbPlayer::updatePos(long newPos) {
1239 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +00001240 char timePos[30] = "\0";
george825457d412005-02-19 06:43:09 +00001241 long time = newPos / 1000;
1242 sprintf(timePos, "%.2um:%.2us (%s)", time/60, time%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +00001243 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +00001244
1245 // Update the position of slider
1246 if (!sliderDraging) {
george825457d412005-02-19 06:43:09 +00001247 double error = SendMessage(posTrackBar, TBM_GETPOS, 0, 0) *
1248 sliderStepMs / double(newPos);
1249 if (!((error > 1 - CALCULATION_ERROR) && (error <= 1 + CALCULATION_ERROR))) {
1250 SendMessage(posTrackBar, TBM_SETPOS, TRUE, newPos / sliderStepMs);
1251 }
george828a471482005-02-06 07:15:53 +00001252 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001253}
1254
1255void RfbPlayer::skipHandshaking() {
1256 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1257 is->skip(skipBytes);
1258 state_ = RFBSTATE_NORMAL;
1259}
1260
1261void programInfo() {
1262 win32::FileVersionInfo inf;
1263 _tprintf(_T("%s - %s, Version %s\n"),
1264 inf.getVerString(_T("ProductName")),
1265 inf.getVerString(_T("FileDescription")),
1266 inf.getVerString(_T("FileVersion")));
1267 printf("%s\n", buildTime);
1268 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1269}
1270
1271void programUsage() {
george820978ddf2005-03-28 15:53:45 +00001272 InfoDialog usageDialog(usage_msg);
1273 usageDialog.showDialog();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001274}
1275
george825beb62a2005-02-09 13:04:32 +00001276char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001277
1278// playerOptions is the player options with default parameters values,
1279// it is used only for run the player with command-line parameters
1280PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001281bool print_usage = false;
george8205d86232005-03-28 12:16:08 +00001282bool print_upf_list = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001283
1284bool processParams(int argc, char* argv[]) {
george82dfb557b2005-03-21 18:26:50 +00001285 playerOptions.commandLineParam = true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001286 for (int i = 1; i < argc; i++) {
1287 if ((strcasecmp(argv[i], "-help") == 0) ||
1288 (strcasecmp(argv[i], "--help") == 0) ||
1289 (strcasecmp(argv[i], "/help") == 0) ||
1290 (strcasecmp(argv[i], "-h") == 0) ||
1291 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001292 (strcasecmp(argv[i], "/?") == 0) ||
1293 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001294 print_usage = true;
1295 return true;
1296 }
1297
george825caee412005-03-09 09:52:10 +00001298 if ((strcasecmp(argv[i], "-pf") == 0) ||
1299 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george82dfb557b2005-03-21 18:26:50 +00001300 char *pf = argv[++i];
1301 char rgb_order[4] = "\0";
1302 int order = RGB_ORDER;
1303 int r = -1, g = -1, b = -1;
1304 bool big_endian = false;
1305 if (strlen(pf) < 6) return false;
1306 while (strlen(pf)) {
1307 if ((pf[0] == 'r') || (pf[0] == 'R')) {
1308 if (r >=0 ) return false;
1309 r = atoi(++pf);
1310 strcat(rgb_order, "r");
1311 continue;
1312 }
1313 if ((pf[0] == 'g') || (pf[0] == 'G')) {
1314 if (g >=0 ) return false;
1315 g = atoi(++pf);
1316 strcat(rgb_order, "g");
1317 continue;
1318 }
1319 if (((pf[0] == 'b') || (pf[0] == 'B')) &&
1320 (pf[1] != 'e') && (pf[1] != 'E')) {
1321 if (b >=0 ) return false;
1322 b = atoi(++pf);
1323 strcat(rgb_order, "b");
1324 continue;
1325 }
1326 if ((pf[0] == 'l') || (pf[0] == 'L') ||
1327 (pf[0] == 'b') || (pf[0] == 'B')) {
1328 if (strcasecmp(pf, "le") == 0) break;
1329 if (strcasecmp(pf, "be") == 0) { big_endian = true; break;}
1330 return false;
1331 }
1332 pf++;
george82193d8e42005-02-20 16:47:01 +00001333 }
george82888b8fb2005-03-22 15:02:39 +00001334 if ((r < 0) || (g < 0) || (b < 0) || (r + g + b > 32)) return false;
george82dfb557b2005-03-21 18:26:50 +00001335 if (strcasecmp(rgb_order, "rgb") == 0) { order = RGB_ORDER; }
1336 else if (strcasecmp(rgb_order, "rbg") == 0) { order = RBG_ORDER; }
1337 else if (strcasecmp(rgb_order, "grb") == 0) { order = GRB_ORDER; }
1338 else if (strcasecmp(rgb_order, "gbr") == 0) { order = GBR_ORDER; }
1339 else if (strcasecmp(rgb_order, "bgr") == 0) { order = BGR_ORDER; }
1340 else if (strcasecmp(rgb_order, "brg") == 0) { order = BRG_ORDER; }
1341 else return false;
1342 playerOptions.autoDetectPF = false;
1343 playerOptions.setPF(order, r, g, b, big_endian);
george82193d8e42005-02-20 16:47:01 +00001344 continue;
1345 }
1346
george8205d86232005-03-28 12:16:08 +00001347 if ((strcasecmp(argv[i], "-upf") == 0) ||
1348 (strcasecmp(argv[i], "/upf") == 0) && (i < argc-1)) {
1349 if ((i == argc - 1) || (argv[++i][0] == '-')) {
1350 print_upf_list = true;
1351 return true;
1352 }
1353 PixelFormatList userPfList;
1354 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
1355 int index = userPfList.getIndexByPFName(argv[i]);
1356 if (index > 0) {
1357 playerOptions.autoDetectPF = false;
1358 playerOptions.setPF(&userPfList[index]->PF);
1359 } else {
1360 return false;
1361 }
1362 continue;
1363 }
george82193d8e42005-02-20 16:47:01 +00001364
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001365 if ((strcasecmp(argv[i], "-speed") == 0) ||
1366 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001367 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001368 if (playbackSpeed <= 0) {
1369 return false;
1370 }
george825e7af742005-03-10 14:26:00 +00001371 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001372 continue;
1373 }
1374
1375 if ((strcasecmp(argv[i], "-pos") == 0) ||
1376 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001377 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001378 if (initTime <= 0)
1379 return false;
george825e7af742005-03-10 14:26:00 +00001380 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001381 continue;
1382 }
1383
1384 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1385 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001386 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001387 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001388 }
1389
george82ee455792005-04-13 12:56:15 +00001390 if ((strcasecmp(argv[i], "-loop") == 0) ||
1391 (strcasecmp(argv[i], "/loop") == 0) && (i < argc-1)) {
1392 playerOptions.loopPlayback = true;
1393 continue;
1394 }
1395
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001396 if (i != argc - 1)
1397 return false;
1398 }
1399
1400 fileName = strDup(argv[argc-1]);
george822a559f62005-04-11 15:25:36 +00001401 if (fileName[0] == '-') return false;
1402 else return true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001403}
1404
1405//
1406// -=- WinMain
1407//
1408
1409int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1410
1411 // - Process the command-line
1412
1413 int argc = __argc;
1414 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001415 if ((argc > 1) && (!processParams(argc, argv))) {
1416 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1417 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001418 }
george82e6883de2005-02-08 14:42:12 +00001419
1420 if (print_usage) {
1421 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001422 return 0;
george8267cbcd02005-01-16 15:39:56 +00001423 }
george8205d86232005-03-28 12:16:08 +00001424 // Show the user defined pixel formats if required
1425 if (print_upf_list) {
george820978ddf2005-03-28 15:53:45 +00001426 int list_size = 256;
george8205d86232005-03-28 12:16:08 +00001427 char *upf_list = new char[list_size];
1428 PixelFormatList userPfList;
1429 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
george820978ddf2005-03-28 15:53:45 +00001430 strcpy(upf_list, "The list of the user defined pixel formats:\r\n");
george8205d86232005-03-28 12:16:08 +00001431 for (int i = userPfList.getDefaultPFCount(); i < userPfList.count(); i++) {
1432 if ((list_size - strlen(upf_list) - 1) <
1433 (strlen(userPfList[i]->format_name) + 2)) {
1434 char *tmpStr = new char[list_size =
1435 list_size + strlen(userPfList[i]->format_name) + 2];
1436 strcpy(tmpStr, upf_list);
1437 delete [] upf_list;
1438 upf_list = new char[list_size];
1439 strcpy(upf_list, tmpStr);
1440 delete [] tmpStr;
1441 }
1442 strcat(upf_list, userPfList[i]->format_name);
george820978ddf2005-03-28 15:53:45 +00001443 strcat(upf_list, "\r\n");
george8205d86232005-03-28 12:16:08 +00001444 }
george820978ddf2005-03-28 15:53:45 +00001445 InfoDialog upfInfoDialog(upf_list);
1446 upfInfoDialog.showDialog();
george8205d86232005-03-28 12:16:08 +00001447 delete [] upf_list;
1448 return 0;
1449 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001450
george82e6883de2005-02-08 14:42:12 +00001451 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001452 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001453 try {
george825e7af742005-03-10 14:26:00 +00001454 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001455 } catch (rdr::Exception e) {
1456 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1457 delete player;
1458 return 0;
1459 }
1460
1461 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001462 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001463 MSG msg;
1464 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001465 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1466 TranslateMessage(&msg);
1467 DispatchMessage(&msg);
1468 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001469 }
1470
george82e6883de2005-02-08 14:42:12 +00001471 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001472 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001473 if (player) delete player;
1474 } catch (rdr::Exception e) {
1475 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1476 }
1477
1478 return 0;
1479};