blob: 0a928022f7b74c4dd09a7007cee85c6a458f4a9a [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
george82e09819b2005-09-04 10:41:50 +000021#include <windows.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000022
george82357c9f52005-03-21 01:28:12 +000023#include <rfb_win32/AboutDialog.h>
george82e09819b2005-09-04 10:41:50 +000024#include <rfb_win32/WMShatter.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000025
26#include <rfbplayer/rfbplayer.h>
george82e09819b2005-09-04 10:41:50 +000027#include <rfbplayer/ChoosePixelFormatDialog.h>
28#include <rfbplayer/GotoPosDialog.h>
29#include <rfbplayer/InfoDialog.h>
30#include <rfbplayer/SessionInfoDialog.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
george82e09819b2005-09-04 10:41:50 +000071// -=- RfbPlayer's defines and consts
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000072
73#define strcasecmp _stricmp
george8268d25142005-02-13 09:33:22 +000074#define DEFAULT_PLAYER_WIDTH 640
75#define DEFAULT_PLAYER_HEIGHT 480
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000076
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000077//
george82357c9f52005-03-21 01:28:12 +000078// -=- AboutDialog global values
79//
80
81const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT;
82const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT;
83const WORD rfb::win32::AboutDialog::Version = IDC_VERSION;
84const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME;
85const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION;
86
87//
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000088// -=- RfbPlayerClass
89
90//
91// Window class used as the basis for RfbPlayer instance
92//
93
94class RfbPlayerClass {
95public:
96 RfbPlayerClass();
97 ~RfbPlayerClass();
98 ATOM classAtom;
99 HINSTANCE instance;
100};
101
102LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
103 LRESULT result;
104
105 if (msg == WM_CREATE)
106 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
107 else if (msg == WM_DESTROY) {
108 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000109 SetWindowLong(hwnd, GWL_USERDATA, 0);
110 }
111 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
112 if (!_this) {
113 vlog.info("null _this in %x, message %u", hwnd, msg);
114 return DefWindowProc(hwnd, msg, wParam, lParam);
115 }
116
117 try {
118 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
119 } catch (rdr::Exception& e) {
120 vlog.error("untrapped: %s", e.str());
121 }
122
123 return result;
124};
125
126RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
127 WNDCLASS wndClass;
128 wndClass.style = 0;
129 wndClass.lpfnWndProc = RfbPlayerProc;
130 wndClass.cbClsExtra = 0;
131 wndClass.cbWndExtra = 0;
132 wndClass.hInstance = instance = GetModuleHandle(0);
133 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000134 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000135 if (!wndClass.hIcon)
136 printf("unable to load icon:%ld", GetLastError());
137 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
138 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000139 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000140 wndClass.lpszClassName = _T("RfbPlayerClass");
141 classAtom = RegisterClass(&wndClass);
142 if (!classAtom) {
143 throw rdr::SystemException("unable to register RfbPlayer window class",
144 GetLastError());
145 }
146}
147
148RfbPlayerClass::~RfbPlayerClass() {
149 if (classAtom) {
150 UnregisterClass((const TCHAR*)classAtom, instance);
151 }
152}
153
154RfbPlayerClass baseClass;
155
156//
157// -=- RfbFrameClass
158
159//
160// Window class used to displaying the rfb data
161//
162
163class RfbFrameClass {
164public:
165 RfbFrameClass();
166 ~RfbFrameClass();
167 ATOM classAtom;
168 HINSTANCE instance;
169};
170
171LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
172 LRESULT result;
173
174 if (msg == WM_CREATE)
175 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
176 else if (msg == WM_DESTROY)
177 SetWindowLong(hwnd, GWL_USERDATA, 0);
178 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
179 if (!_this) {
180 vlog.info("null _this in %x, message %u", hwnd, msg);
181 return DefWindowProc(hwnd, msg, wParam, lParam);
182 }
183
184 try {
185 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
186 } catch (rdr::Exception& e) {
187 vlog.error("untrapped: %s", e.str());
188 }
189
190 return result;
191}
192
193RfbFrameClass::RfbFrameClass() : classAtom(0) {
194 WNDCLASS wndClass;
195 wndClass.style = 0;
196 wndClass.lpfnWndProc = FrameProc;
197 wndClass.cbClsExtra = 0;
198 wndClass.cbWndExtra = 0;
199 wndClass.hInstance = instance = GetModuleHandle(0);
200 wndClass.hIcon = 0;
201 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
202 wndClass.hbrBackground = 0;
203 wndClass.lpszMenuName = 0;
204 wndClass.lpszClassName = _T("RfbPlayerClass1");
205 classAtom = RegisterClass(&wndClass);
206 if (!classAtom) {
207 throw rdr::SystemException("unable to register RfbPlayer window class",
208 GetLastError());
209 }
210}
211
212RfbFrameClass::~RfbFrameClass() {
213 if (classAtom) {
214 UnregisterClass((const TCHAR*)classAtom, instance);
215 }
216}
217
218RfbFrameClass frameClass;
219
220//
221// -=- RfbPlayer instance implementation
222//
223
george825e7af742005-03-10 14:26:00 +0000224RfbPlayer::RfbPlayer(char *_fileName, PlayerOptions *_options)
225: RfbProto(_fileName), fileName(_fileName), buffer(0), client_size(0, 0, 32, 32),
226 window_size(0, 0, 32, 32), cutText(0), seekMode(false), lastPos(0),
george82e09819b2005-09-04 10:41:50 +0000227 rfbReader(0), sessionTimeMs(0), sliderStepMs(0), imageDataStartTime(0),
228 rewindFlag(false), stopped(false), currentEncoding(-1) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000229
george825e7af742005-03-10 14:26:00 +0000230 // Save the player options
231 memcpy(&options, _options, sizeof(options));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000232
george823c8fbbf2005-01-24 11:09:08 +0000233 // Reset the full session time
234 strcpy(fullSessionTime, "00m:00s");
235
george820981b342005-03-19 11:19:00 +0000236 // Load the user defined pixel formats from the registry
237 supportedPF.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
238
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000239 // Create the main window
240 const TCHAR* name = _T("RfbPlayer");
george822ff7a612005-02-19 17:05:24 +0000241 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
242 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000243 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george822ff7a612005-02-19 17:05:24 +0000244 x, y, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000245 if (!mainHwnd) {
246 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
247 }
248 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
249
250 // Create the backing buffer
251 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000252 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000253
george825e7af742005-03-10 14:26:00 +0000254 // If run with command-line parameters,
255 // open the session file with default settings, otherwise
256 // restore player settings from the registry
george8217e92cb2005-01-31 16:01:02 +0000257 if (fileName) {
258 openSessionFile(fileName);
george825e7af742005-03-10 14:26:00 +0000259 if (options.initTime > 0) setPos(options.initTime);
260 setSpeed(options.playbackSpeed);
george8263ebbcc2005-02-12 12:09:13 +0000261 } else {
george825e7af742005-03-10 14:26:00 +0000262 options.readFromRegistry();
george8263ebbcc2005-02-12 12:09:13 +0000263 disableTBandMenuItems();
george82f5302762005-02-13 12:31:03 +0000264 setTitle("None");
george8217e92cb2005-01-31 16:01:02 +0000265 }
george82ee455792005-04-13 12:56:15 +0000266 init();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000267}
268
269RfbPlayer::~RfbPlayer() {
270 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000271 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000272 delete rfbReader->join();
273 rfbReader = 0;
274 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000275 if (mainHwnd) {
276 setVisible(false);
277 DestroyWindow(mainHwnd);
278 mainHwnd = 0;
279 }
george825beb62a2005-02-09 13:04:32 +0000280 if (buffer) delete buffer;
281 if (cutText) delete [] cutText;
george827009c892005-02-19 12:49:42 +0000282 if (fileName) delete [] fileName;
george82be7bf092005-04-17 17:21:36 +0000283 if (hFont) DeleteObject(hFont);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000284 vlog.debug("~RfbPlayer done");
285}
286
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000287LRESULT
288RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
george82e09819b2005-09-04 10:41:50 +0000289
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000290 switch (msg) {
291
292 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000293
294 case WM_CREATE:
295 {
george82e09819b2005-09-04 10:41:50 +0000296 tb.create(this, hwnd);
george82db90d3e2005-04-17 12:01:48 +0000297
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000298 // Create the frame window
299 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
george82db90d3e2005-04-17 12:01:48 +0000300 0, WS_CHILD | WS_VISIBLE, 0, tb.getHeight(), 10, tb.getHeight() + 10,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000301 hwnd, 0, frameClass.instance, this);
302
george82006f2792005-02-05 07:40:47 +0000303 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000304
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000305 return 0;
306 }
307
george827214b822004-12-12 07:02:51 +0000308 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000309
310 case WM_COMMAND:
george82e09819b2005-09-04 10:41:50 +0000311
george825c13c662005-01-27 14:48:23 +0000312 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000313 case ID_OPENFILE:
314 {
315 char curDir[_MAX_DIR];
316 static char filename[_MAX_PATH];
317 OPENFILENAME ofn;
318 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
319 GetCurrentDirectory(sizeof(curDir), curDir);
320
321 ofn.lStructSize = sizeof(OPENFILENAME);
322 ofn.hwndOwner = getMainHandle();
323 ofn.lpstrFile = filename;
324 ofn.nMaxFile = sizeof(filename);
325 ofn.lpstrInitialDir = curDir;
george82d7c81be2005-04-11 15:07:13 +0000326 ofn.lpstrFilter = "Rfb Session files (*.rfb, *.fbs)\0*.rfb;*.fbs\0" \
george826e51fcc2005-02-06 13:30:49 +0000327 "All files (*.*)\0*.*\0";
328 ofn.lpstrDefExt = "rfb";
329 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
george829d5129a2005-02-21 13:32:39 +0000330 if (GetOpenFileName(&ofn)) {
george826e51fcc2005-02-06 13:30:49 +0000331 openSessionFile(filename);
george829d5129a2005-02-21 13:32:39 +0000332 }
george826e51fcc2005-02-06 13:30:49 +0000333 }
334 break;
george8271ca1772005-02-13 10:50:46 +0000335 case ID_CLOSEFILE:
336 closeSessionFile();
337 break;
george8226af7652005-04-13 12:46:25 +0000338 case ID_SESSION_INFO:
339 {
340 SessionInfoDialog sessionInfo(&cp, currentEncoding);
341 sessionInfo.showDialog(getMainHandle());
342 }
343 break;
george825c13c662005-01-27 14:48:23 +0000344 case ID_PLAY:
345 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000346 break;
347 case ID_PAUSE:
348 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000349 break;
350 case ID_STOP:
george824fd5a1d2005-04-13 13:05:53 +0000351 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000352 break;
353 case ID_PLAYPAUSE:
george82bc999f42005-03-19 11:48:19 +0000354 if (rfbReader) {
355 if (isPaused()) {
356 setPaused(false);
357 } else {
358 setPaused(true);
359 }
george825c13c662005-01-27 14:48:23 +0000360 }
george825c13c662005-01-27 14:48:23 +0000361 break;
george827549df42005-02-08 16:31:02 +0000362 case ID_GOTO:
363 {
364 GotoPosDialog gotoPosDlg;
george82d9957b72005-03-11 14:22:14 +0000365 if (gotoPosDlg.showDialog(getMainHandle())) {
george821d5d40d2005-02-20 03:25:47 +0000366 long gotoTime = min(gotoPosDlg.getPos(), sessionTimeMs);
367 setPos(gotoTime);
george82e09819b2005-09-04 10:41:50 +0000368 tb.updatePos(gotoTime);
george82a6900d72005-02-21 17:24:26 +0000369 setPaused(isPaused());;
george827549df42005-02-08 16:31:02 +0000370 }
371 }
372 break;
george8231a36332005-02-06 17:27:34 +0000373 case ID_LOOP:
george825e7af742005-03-10 14:26:00 +0000374 options.loopPlayback = !options.loopPlayback;
375 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
george8231a36332005-02-06 17:27:34 +0000376 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
377 break;
george824ea27f62005-01-29 15:03:06 +0000378 case ID_RETURN:
george82e09819b2005-09-04 10:41:50 +0000379 tb.processWM_COMMAND(wParam, lParam);
george824ea27f62005-01-29 15:03:06 +0000380 break;
george825e7af742005-03-10 14:26:00 +0000381 case ID_OPTIONS:
382 {
george825a6df072005-03-20 09:56:17 +0000383 OptionsDialog optionsDialog(&options, &supportedPF);
george82d9957b72005-03-11 14:22:14 +0000384 optionsDialog.showDialog(getMainHandle());
george825e7af742005-03-10 14:26:00 +0000385 }
386 break;
george8201aa6732005-02-06 17:13:03 +0000387 case ID_EXIT:
george8201aa6732005-02-06 17:13:03 +0000388 PostQuitMessage(0);
389 break;
george8245699e22005-03-29 12:08:52 +0000390 case ID_HOMEPAGE:
george828c72fe72005-03-29 15:05:57 +0000391 ShellExecute(getMainHandle(), _T("open"), _T("http://www.tightvnc.com/"),
392 NULL, NULL, SW_SHOWDEFAULT);
george8245699e22005-03-29 12:08:52 +0000393 break;
george82ef5f7262005-02-08 15:09:26 +0000394 case ID_HELP_COMMANDLINESWITCHES:
george820978ddf2005-03-28 15:53:45 +0000395 {
396 InfoDialog usageDialog(usage_msg);
george82ff5b29f2005-03-29 11:59:35 +0000397 usageDialog.showDialog(getMainHandle());
george820978ddf2005-03-28 15:53:45 +0000398 }
george8259f84532005-02-08 15:01:39 +0000399 break;
george82357c9f52005-03-21 01:28:12 +0000400 case ID_ABOUT:
401 AboutDialog::instance.showDialog();
402 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000403 }
404 break;
405
406 // Update frame's window size and add scrollbars if required
407
408 case WM_SIZE:
409 {
george82d070c692005-01-19 16:44:04 +0000410
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000411 Point old_offset = bufferToClient(Point(0, 0));
412
413 // Update the cached sizing information
414 RECT r;
415 GetClientRect(getMainHandle(), &r);
george82db90d3e2005-04-17 12:01:48 +0000416 MoveWindow(getFrameHandle(), 0, tb.getHeight(), r.right - r.left,
417 r.bottom - r.top - tb.getHeight(), TRUE);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000418
419 GetWindowRect(getFrameHandle(), &r);
420 window_size = Rect(r.left, r.top, r.right, r.bottom);
421 GetClientRect(getFrameHandle(), &r);
422 client_size = Rect(r.left, r.top, r.right, r.bottom);
423
424 // Determine whether scrollbars are required
425 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000426
427 // Resize the ToolBar
428 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000429
430 // Redraw if required
431 if (!old_offset.equals(bufferToClient(Point(0, 0))))
432 InvalidateRect(getFrameHandle(), 0, TRUE);
433 }
434 break;
george828a471482005-02-06 07:15:53 +0000435
george828a471482005-02-06 07:15:53 +0000436 case WM_HSCROLL:
george82e09819b2005-09-04 10:41:50 +0000437 tb.processWM_HSCROLL(wParam, lParam);
george828a471482005-02-06 07:15:53 +0000438 break;
george829e6e6cc2005-01-29 13:12:05 +0000439
440 case WM_NOTIFY:
george82e09819b2005-09-04 10:41:50 +0000441 return tb.processWM_NOTIFY(wParam, lParam);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000442
443 case WM_CLOSE:
444 vlog.debug("WM_CLOSE %x", getMainHandle());
445 PostQuitMessage(0);
446 break;
447 }
448
449 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
450}
451
452LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
453 switch (msg) {
454
455 case WM_PAINT:
456 {
george821e846ff2005-02-24 13:13:33 +0000457 if (isSeeking() || rewindFlag) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000458 seekMode = true;
459 return 0;
460 } else {
461 if (seekMode) {
462 seekMode = false;
463 InvalidateRect(getFrameHandle(), 0, true);
464 UpdateWindow(getFrameHandle());
465 return 0;
466 }
467 }
468
469 PAINTSTRUCT ps;
470 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
471 if (!paintDC)
472 throw SystemException("unable to BeginPaint", GetLastError());
473 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
474
475 if (!pr.is_empty()) {
476
477 if (buffer->bitmap) {
478
479 // Get device context
480 BitmapDC bitmapDC(paintDC, buffer->bitmap);
481
482 // Blit the border if required
483 Rect bufpos = bufferToClient(buffer->getRect());
484 if (!pr.enclosed_by(bufpos)) {
485 vlog.debug("draw border");
486 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
487 RECT r;
488 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
489 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
490 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
491 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
492 }
493
494 // Do the blit
495 Point buf_pos = clientToBuffer(pr.tl);
496 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
497 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
498 throw SystemException("unable to BitBlt to window", GetLastError());
499
500 } else {
501 // Blit a load of black
502 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
503 0, 0, 0, BLACKNESS))
504 throw SystemException("unable to BitBlt to blank window", GetLastError());
505 }
506 }
507 EndPaint(getFrameHandle(), &ps);
508 }
509 return 0;
george8203c01da2005-03-16 12:36:53 +0000510
george82aff63ab2005-03-16 13:48:59 +0000511 // Process play/pause by the left mouse button
512 case WM_LBUTTONDOWN:
george8203c01da2005-03-16 12:36:53 +0000513 SendMessage(getMainHandle(), WM_COMMAND, ID_PLAYPAUSE, 0);
514 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000515
516 case WM_VSCROLL:
517 case WM_HSCROLL:
518 {
519 Point delta;
520 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
521
522 switch (LOWORD(wParam)) {
523 case SB_PAGEUP: newpos -= 50; break;
524 case SB_PAGEDOWN: newpos += 50; break;
525 case SB_LINEUP: newpos -= 5; break;
526 case SB_LINEDOWN: newpos += 5; break;
527 case SB_THUMBTRACK:
528 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
529 default: vlog.info("received unknown scroll message");
530 };
531
532 if (msg == WM_HSCROLL)
533 setViewportOffset(Point(newpos, scrolloffset.y));
534 else
535 setViewportOffset(Point(scrolloffset.x, newpos));
536
537 SCROLLINFO si;
538 si.cbSize = sizeof(si);
539 si.fMask = SIF_POS;
540 si.nPos = newpos;
541 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
542 }
543 break;
544 }
545
546 return DefWindowProc(hwnd, msg, wParam, lParam);
547}
548
george82a21d2952005-02-12 11:30:03 +0000549void RfbPlayer::disableTBandMenuItems() {
550 // Disable the menu items
551 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_GRAYED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000552 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000553 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_GRAYED | MF_BYCOMMAND);
554 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_GRAYED | MF_BYPOSITION);
george82a21d2952005-02-12 11:30:03 +0000555 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_GRAYED | MF_BYCOMMAND);
556 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
557 EnableMenuItem(hMenu, ID_GOTO, MF_GRAYED | MF_BYCOMMAND);
558 EnableMenuItem(hMenu, ID_LOOP, MF_GRAYED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000559 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_GRAYED | MF_BYCOMMAND);
560 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_GRAYED | MF_BYCOMMAND);
george82a21d2952005-02-12 11:30:03 +0000561
562 // Disable the toolbar buttons and child controls
george82e09819b2005-09-04 10:41:50 +0000563 tb.disable();
george82a21d2952005-02-12 11:30:03 +0000564}
565
george82f5043162005-02-12 11:37:18 +0000566void RfbPlayer::enableTBandMenuItems() {
567 // Enable the menu items
568 EnableMenuItem(hMenu, ID_CLOSEFILE, MF_ENABLED | MF_BYCOMMAND);
george8226af7652005-04-13 12:46:25 +0000569 EnableMenuItem(hMenu, ID_SESSION_INFO, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000570 ///EnableMenuItem(hMenu, ID_FULLSCREEN, MF_ENABLED | MF_BYCOMMAND);
571 ///EnableMenuItem(GetSubMenu(hMenu, 1), 1, MF_ENABLED | MF_BYPOSITION);
george82f5043162005-02-12 11:37:18 +0000572 EnableMenuItem(hMenu, ID_PLAYPAUSE, MF_ENABLED | MF_BYCOMMAND);
573 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
574 EnableMenuItem(hMenu, ID_GOTO, MF_ENABLED | MF_BYCOMMAND);
575 EnableMenuItem(hMenu, ID_LOOP, MF_ENABLED | MF_BYCOMMAND);
george8245699e22005-03-29 12:08:52 +0000576 ///EnableMenuItem(hMenu, ID_COPYTOCLIPBOARD, MF_ENABLED | MF_BYCOMMAND);
577 ///EnableMenuItem(hMenu, ID_FRAMEEXTRACT, MF_ENABLED | MF_BYCOMMAND);
george82f5043162005-02-12 11:37:18 +0000578
579 // Enable the toolbar buttons and child controls
george82e09819b2005-09-04 10:41:50 +0000580 tb.enable();
george82f5043162005-02-12 11:37:18 +0000581}
582
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000583void RfbPlayer::setVisible(bool visible) {
584 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
585 if (visible) {
586 // When the window becomes visible, make it active
587 SetForegroundWindow(getMainHandle());
588 SetActiveWindow(getMainHandle());
589 }
590}
591
592void RfbPlayer::setTitle(const char *title) {
593 char _title[256];
594 strcpy(_title, AppName);
595 strcat(_title, " - ");
596 strcat(_title, title);
597 SetWindowText(getMainHandle(), _title);
598}
599
600void RfbPlayer::setFrameSize(int width, int height) {
601 // Calculate and set required size for main window
602 RECT r = {0, 0, width, height};
george82e1169a12005-02-19 13:54:38 +0000603 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), TRUE,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000604 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
george82db90d3e2005-04-17 12:01:48 +0000605 r.bottom += tb.getHeight(); // Include RfbPlayr's controls area
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000606 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
george822ff7a612005-02-19 17:05:24 +0000607 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2);
608 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2);
609 SetWindowPos(getMainHandle(), 0, x, y, r.right-r.left, r.bottom-r.top,
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000610 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
611
612 // Enable/disable scrollbars as appropriate
613 calculateScrollBars();
614}
615
616void RfbPlayer::calculateScrollBars() {
617 // Calculate the required size of window
618 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
619 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
620 DWORD old_style;
621 RECT r;
622 SetRect(&r, 0, 0, buffer->width(), buffer->height());
623 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
624 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
625
626 // Work out whether scroll bars are required
627 do {
628 old_style = style;
629
630 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
631 style |= WS_HSCROLL;
632 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
633 }
634 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
635 style |= WS_VSCROLL;
636 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
637 }
638 } while (style != old_style);
639
640 // Tell Windows to update the window style & cached settings
641 if (style != current_style) {
642 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
643 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
644 }
645
646 // Update the scroll settings
647 SCROLLINFO si;
648 if (style & WS_VSCROLL) {
649 si.cbSize = sizeof(si);
650 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
651 si.nMin = 0;
652 si.nMax = buffer->height();
653 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
654 maxscrolloffset.y = max(0, si.nMax-si.nPage);
655 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
656 si.nPos = scrolloffset.y;
657 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
658 }
659 if (style & WS_HSCROLL) {
660 si.cbSize = sizeof(si);
661 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
662 si.nMin = 0;
663 si.nMax = buffer->width();
664 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
665 maxscrolloffset.x = max(0, si.nMax-si.nPage);
666 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
667 si.nPos = scrolloffset.x;
668 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
669 }
george82a758a7a2005-03-15 16:34:57 +0000670
671 // Update the cached client size
672 GetClientRect(getFrameHandle(), &r);
673 client_size = Rect(r.left, r.top, r.right, r.bottom);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000674}
675
676bool RfbPlayer::setViewportOffset(const Point& tl) {
677/* ***
678 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
679 max(0, min(maxscrolloffset.y, tl.y)));
680 */
681 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
682 max(0, min(tl.y, buffer->height()-client_size.height())));
683 Point delta = np.translate(scrolloffset.negate());
684 if (!np.equals(scrolloffset)) {
685 scrolloffset = np;
686 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
687 UpdateWindow(getFrameHandle());
688 return true;
689 }
690 return false;
691}
692
693void RfbPlayer::close(const char* reason) {
694 setVisible(false);
695 if (reason) {
696 vlog.info("closing - %s", reason);
697 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
698 }
699 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
700}
701
702void RfbPlayer::blankBuffer() {
703 fillRect(buffer->getRect(), 0);
704}
705
706void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000707 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000708 blankBuffer();
709 newSession(fileName);
710 skipHandshaking();
george825e7af742005-03-10 14:26:00 +0000711 is->setSpeed(options.playbackSpeed);
george828a471482005-02-06 07:15:53 +0000712 if (paused) is->pausePlayback();
713 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000714}
715
716void RfbPlayer::processMsg() {
george820d2e19d2005-03-03 15:47:55 +0000717 // Perform return if waitWhilePaused processed because
718 // rfbReader thread could receive the signal to close
719 if (waitWhilePaused()) return;
720
george8223e08562005-01-31 15:16:42 +0000721 static long update_time = GetTickCount();
722 try {
george828a471482005-02-06 07:15:53 +0000723 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
george82e09819b2005-09-04 10:41:50 +0000724 && (!tb.isPosSliderDragging())) {
george8223e08562005-01-31 15:16:42 +0000725 // Update pos in the toolbar 4 times in 1 second
george82e09819b2005-09-04 10:41:50 +0000726 tb.updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000727 update_time = GetTickCount();
728 }
729 RfbProto::processMsg();
730 } catch (rdr::Exception e) {
731 if (strcmp(e.str(), "[End Of File]") == 0) {
732 rewind();
george825e7af742005-03-10 14:26:00 +0000733 setPaused(!options.loopPlayback);
george82e09819b2005-09-04 10:41:50 +0000734 tb.updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000735 return;
736 }
737 // It's a special exception to perform backward seeking.
738 // We only rewind the stream and seek the offset
739 if (strcmp(e.str(), "[REWIND]") == 0) {
george821e846ff2005-02-24 13:13:33 +0000740 rewindFlag = true;
george82b95503e2005-02-21 17:02:34 +0000741 long seekOffset = max(getSeekOffset(), imageDataStartTime);
george8223e08562005-01-31 15:16:42 +0000742 rewind();
george820d2e19d2005-03-03 15:47:55 +0000743 if (!stopped) setPos(seekOffset);
744 else stopped = false;
george82e09819b2005-09-04 10:41:50 +0000745 tb.updatePos(seekOffset);
george821e846ff2005-02-24 13:13:33 +0000746 rewindFlag = false;
george822c7634b2005-03-10 18:03:27 +0000747 return;
george82e09819b2005-09-04 10:41:50 +0000748 }
george822c7634b2005-03-10 18:03:27 +0000749 // It's a special exception which is used to terminate the playback
750 if (strcmp(e.str(), "[TERMINATE]") == 0) {
751 sessionTerminateThread *terminate = new sessionTerminateThread(this);
752 terminate->start();
george8223e08562005-01-31 15:16:42 +0000753 } else {
george820e980cc2005-03-10 18:18:34 +0000754 // Show the exception message and close the session playback
755 is->pausePlayback();
756 char message[256] = "\0";
757 strcat(message, e.str());
758 strcat(message, "\nMaybe you force wrong the pixel format for this session");
Constantin Kaplinsky4aaf63b2005-09-28 16:49:36 +0000759 MessageBox(getMainHandle(), message, "RFB Player", MB_OK | MB_ICONERROR);
george820e980cc2005-03-10 18:18:34 +0000760 sessionTerminateThread *terminate = new sessionTerminateThread(this);
761 terminate->start();
george8223e08562005-01-31 15:16:42 +0000762 return;
763 }
764 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000765}
766
george82c7e9f792005-03-20 12:52:46 +0000767long ChoosePixelFormatDialog::pfIndex = DEFAULT_PF_INDEX;
768bool ChoosePixelFormatDialog::bigEndian = false;
769
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000770void RfbPlayer::serverInit() {
771 RfbProto::serverInit();
772
george82b95503e2005-02-21 17:02:34 +0000773 // Save the image data start time
774 imageDataStartTime = is->getTimeOffset();
775
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000776 // Resize the backing buffer
777 buffer->setSize(cp.width, cp.height);
778
779 // Check on the true colour mode
780 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000781 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000782
783 // Set the session pixel format
george825e7af742005-03-10 14:26:00 +0000784 if (options.askPixelFormat) {
george82c7e9f792005-03-20 12:52:46 +0000785 ChoosePixelFormatDialog choosePixelFormatDialog(&supportedPF);
george82d9957b72005-03-11 14:22:14 +0000786 if (choosePixelFormatDialog.showDialog(getMainHandle())) {
george82c7e9f792005-03-20 12:52:46 +0000787 long pixelFormatIndex = choosePixelFormatDialog.getPFIndex();
george825a6df072005-03-20 09:56:17 +0000788 if (pixelFormatIndex < 0) {
789 options.autoDetectPF = true;
790 options.setPF((PixelFormat *)&cp.pf());
791 } else {
792 options.autoDetectPF = false;
george8210326862005-03-28 12:07:31 +0000793 options.setPF(&supportedPF[pixelFormatIndex]->PF);
george82c7e9f792005-03-20 12:52:46 +0000794 options.pixelFormat.bigEndian = choosePixelFormatDialog.isBigEndian();
george825a6df072005-03-20 09:56:17 +0000795 }
george822c7634b2005-03-10 18:03:27 +0000796 } else {
797 is->pausePlayback();
798 throw rdr::Exception("[TERMINATE]");
george825e7af742005-03-10 14:26:00 +0000799 }
800 } else {
george82dfb557b2005-03-21 18:26:50 +0000801 if (!options.commandLineParam) {
802 if (options.autoDetectPF) {
803 options.setPF((PixelFormat *)&cp.pf());
804 } else {
george8210326862005-03-28 12:07:31 +0000805 options.setPF(&supportedPF[options.pixelFormatIndex]->PF);
george82dfb557b2005-03-21 18:26:50 +0000806 options.pixelFormat.bigEndian = options.bigEndianFlag;
807 }
george820eefed52005-03-29 13:02:21 +0000808 } else if (options.autoDetectPF) {
809 options.setPF((PixelFormat *)&cp.pf());
george825a6df072005-03-20 09:56:17 +0000810 }
george825e7af742005-03-10 14:26:00 +0000811 }
george825a6df072005-03-20 09:56:17 +0000812 cp.setPF(options.pixelFormat);
813 buffer->setPF(options.pixelFormat);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000814
815 // If the window is not maximised then resize it
816 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
817 setFrameSize(cp.width, cp.height);
818
819 // Set the window title and show it
820 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000821
george82e09819b2005-09-04 10:41:50 +0000822 // Calculate the full session time and update posTrackBar control in toolbar
george828a471482005-02-06 07:15:53 +0000823 sessionTimeMs = calculateSessionTime(fileName);
george82e09819b2005-09-04 10:41:50 +0000824 tb.init(sessionTimeMs);
825 tb.updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000826
george825e7af742005-03-10 14:26:00 +0000827 setPaused(!options.autoPlay);
828 // Restore the parameters from registry,
829 // which was replaced by command-line parameters.
george82dfb557b2005-03-21 18:26:50 +0000830 if (options.commandLineParam) {
831 options.readFromRegistry();
832 options.commandLineParam = false;
833 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000834}
835
836void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
837 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
Constantin Kaplinsky4aaf63b2005-09-28 16:49:36 +0000838 throw rdr::Exception("Can't handle SetColourMapEntries message");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000839/* int i;
840 for (i=0;i<count;i++) {
841 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
842 }
843 // *** change to 0, 256?
844 refreshWindowPalette(first, count);
845 palette_changed = true;
846 InvalidateRect(getFrameHandle(), 0, FALSE);*/
847}
848
849void RfbPlayer::bell() {
george825e7af742005-03-10 14:26:00 +0000850 if (options.acceptBell)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000851 MessageBeep(-1);
852}
853
854void RfbPlayer::serverCutText(const char* str, int len) {
855 if (cutText != NULL)
856 delete [] cutText;
857 cutText = new char[len + 1];
858 memcpy(cutText, str, len);
859 cutText[len] = '\0';
860}
861
862void RfbPlayer::frameBufferUpdateEnd() {
863};
864
865void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
george8226af7652005-04-13 12:46:25 +0000866 currentEncoding = encoding;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000867}
868
869void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
870}
871
872
873void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
874 buffer->fillRect(r, pix);
875 invalidateBufferRect(r);
876}
877
878void RfbPlayer::imageRect(const Rect& r, void* pixels) {
879 buffer->imageRect(r, pixels);
880 invalidateBufferRect(r);
881}
882
883void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
884 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
885 invalidateBufferRect(r);
886}
887
888bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
889 Rect rect = bufferToClient(crect);
890 if (rect.intersect(client_size).is_empty()) return false;
891 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
892 InvalidateRect(getFrameHandle(), &invalid, FALSE);
893 return true;
894}
895
george820d2e19d2005-03-03 15:47:55 +0000896bool RfbPlayer::waitWhilePaused() {
897 bool result = false;
898 while(isPaused() && !isSeeking()) {
899 Sleep(20);
900 result = true;
901 }
902 return result;
903}
904
george8257f13522005-02-05 08:48:22 +0000905long RfbPlayer::calculateSessionTime(char *filename) {
906 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000907 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000908 try {
909 while (TRUE) {
910 sessionFile.skip(1024);
911 }
912 } catch (rdr::Exception e) {
913 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000914 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000915 } else {
Constantin Kaplinsky4aaf63b2005-09-28 16:49:36 +0000916 MessageBox(getMainHandle(), e.str(), "RFB Player", MB_OK | MB_ICONERROR);
george8257f13522005-02-05 08:48:22 +0000917 return 0;
918 }
919 }
920 return 0;
921}
922
george82ee455792005-04-13 12:56:15 +0000923void RfbPlayer::init() {
924 if (options.loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
925 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
926}
927
george826b87aff2005-02-13 10:48:21 +0000928void RfbPlayer::closeSessionFile() {
george820bdb2842005-02-19 13:17:58 +0000929 DWORD dwStyle;
george826b87aff2005-02-13 10:48:21 +0000930 RECT r;
931
932 // Uncheck all toolbar buttons
933 if (tb.getHandle()) {
934 tb.checkButton(ID_PLAY, false);
935 tb.checkButton(ID_PAUSE, false);
936 tb.checkButton(ID_STOP, false);
937 }
938
939 // Stop playback and update the player state
940 disableTBandMenuItems();
941 if (rfbReader) {
942 delete rfbReader->join();
943 rfbReader = 0;
944 delete [] fileName;
945 fileName = 0;
946 }
947 blankBuffer();
948 setTitle("None");
george825e7af742005-03-10 14:26:00 +0000949 options.playbackSpeed = 1.0;
george82e09819b2005-09-04 10:41:50 +0000950 tb.init(0);
george826b87aff2005-02-13 10:48:21 +0000951
952 // Change the player window size and frame size to default
george820bdb2842005-02-19 13:17:58 +0000953 if ((dwStyle = GetWindowLong(getMainHandle(), GWL_STYLE)) & WS_MAXIMIZE) {
954 dwStyle &= ~WS_MAXIMIZE;
955 SetWindowLong(getMainHandle(), GWL_STYLE, dwStyle);
956 }
george822ff7a612005-02-19 17:05:24 +0000957 int x = max(0, (GetSystemMetrics(SM_CXSCREEN) - DEFAULT_PLAYER_WIDTH) / 2);
958 int y = max(0, (GetSystemMetrics(SM_CYSCREEN) - DEFAULT_PLAYER_HEIGHT) / 2);
959 SetWindowPos(getMainHandle(), 0, x, y,
george826b87aff2005-02-13 10:48:21 +0000960 DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_HEIGHT,
george822ff7a612005-02-19 17:05:24 +0000961 SWP_NOZORDER | SWP_FRAMECHANGED);
george826b87aff2005-02-13 10:48:21 +0000962 buffer->setSize(32, 32);
963 calculateScrollBars();
964
965 // Update the cached sizing information and repaint the frame window
966 GetWindowRect(getFrameHandle(), &r);
967 window_size = Rect(r.left, r.top, r.right, r.bottom);
968 GetClientRect(getFrameHandle(), &r);
969 client_size = Rect(r.left, r.top, r.right, r.bottom);
970 InvalidateRect(getFrameHandle(), 0, TRUE);
971 UpdateWindow(getFrameHandle());
972}
973
george8217e92cb2005-01-31 16:01:02 +0000974void RfbPlayer::openSessionFile(char *_fileName) {
975 fileName = strDup(_fileName);
976
977 // Close the previous reading thread
978 if (rfbReader) {
george8217e92cb2005-01-31 16:01:02 +0000979 delete rfbReader->join();
george82b4f969b2005-02-09 16:34:51 +0000980 rfbReader = 0;
george8217e92cb2005-01-31 16:01:02 +0000981 }
982 blankBuffer();
983 newSession(fileName);
george825e7af742005-03-10 14:26:00 +0000984 setSpeed(options.playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +0000985 rfbReader = new rfbSessionReader(this);
986 rfbReader->start();
george82e09819b2005-09-04 10:41:50 +0000987 tb.setTimePos(0);
george8263ebbcc2005-02-12 12:09:13 +0000988 enableTBandMenuItems();
george8217e92cb2005-01-31 16:01:02 +0000989}
990
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000991void RfbPlayer::setPaused(bool paused) {
992 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000993 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000994 tb.checkButton(ID_PAUSE, true);
995 tb.checkButton(ID_PLAY, false);
996 tb.checkButton(ID_STOP, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000997 } else {
george825beb62a2005-02-09 13:04:32 +0000998 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000999 tb.checkButton(ID_PLAY, true);
1000 tb.checkButton(ID_STOP, false);
1001 tb.checkButton(ID_PAUSE, false);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001002 }
george824fd5a1d2005-04-13 13:05:53 +00001003 tb.enableButton(ID_PAUSE, true);
1004 EnableMenuItem(hMenu, ID_STOP, MF_ENABLED | MF_BYCOMMAND);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001005}
1006
george82006f2792005-02-05 07:40:47 +00001007void RfbPlayer::stopPlayback() {
george820d2e19d2005-03-03 15:47:55 +00001008 stopped = true;
george820d2e19d2005-03-03 15:47:55 +00001009 setPos(0);
george828edfb7a2005-03-03 16:36:10 +00001010 if (is) {
1011 is->pausePlayback();
1012 is->interruptFrameDelay();
1013 }
george82006f2792005-02-05 07:40:47 +00001014 tb.checkButton(ID_STOP, true);
1015 tb.checkButton(ID_PLAY, false);
1016 tb.checkButton(ID_PAUSE, false);
george824fd5a1d2005-04-13 13:05:53 +00001017 tb.enableButton(ID_PAUSE, false);
george82e09819b2005-09-04 10:41:50 +00001018 tb.setTimePos(0);
george824fd5a1d2005-04-13 13:05:53 +00001019 EnableMenuItem(hMenu, ID_STOP, MF_GRAYED | MF_BYCOMMAND);
george82006f2792005-02-05 07:40:47 +00001020}
1021
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001022void RfbPlayer::setSpeed(double speed) {
george825f326fe2005-02-20 08:01:01 +00001023 if (speed > 0) {
1024 char speedStr[20] = "\0";
1025 double newSpeed = min(speed, MAX_SPEED);
george825f326fe2005-02-20 08:01:01 +00001026 is->setSpeed(newSpeed);
george825e7af742005-03-10 14:26:00 +00001027 options.playbackSpeed = newSpeed;
george82e09819b2005-09-04 10:41:50 +00001028 SendMessage(tb.getSpeedUpDownHwnd(), UDM_SETPOS,
george825f326fe2005-02-20 08:01:01 +00001029 0, MAKELONG((short)(newSpeed / 0.5), 0));
1030 sprintf(speedStr, "%.2f", newSpeed);
george82e09819b2005-09-04 10:41:50 +00001031 SetWindowText(tb.getSpeedEditHwnd(), speedStr);
george825f326fe2005-02-20 08:01:01 +00001032 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001033}
1034
1035double RfbPlayer::getSpeed() {
1036 return is->getSpeed();
1037}
1038
1039void RfbPlayer::setPos(long pos) {
george82b95503e2005-02-21 17:02:34 +00001040 is->setTimeOffset(max(pos, imageDataStartTime));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001041}
1042
1043long RfbPlayer::getSeekOffset() {
1044 return is->getSeekOffset();
1045}
1046
1047bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +00001048 if (is) return is->isSeeking();
1049 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001050}
1051
1052bool RfbPlayer::isSeekMode() {
1053 return seekMode;
1054}
1055
1056bool RfbPlayer::isPaused() {
1057 return is->isPaused();
1058}
1059
1060long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +00001061 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001062}
1063
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001064void RfbPlayer::skipHandshaking() {
1065 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1066 is->skip(skipBytes);
1067 state_ = RFBSTATE_NORMAL;
1068}
1069
1070void programInfo() {
1071 win32::FileVersionInfo inf;
1072 _tprintf(_T("%s - %s, Version %s\n"),
1073 inf.getVerString(_T("ProductName")),
1074 inf.getVerString(_T("FileDescription")),
1075 inf.getVerString(_T("FileVersion")));
1076 printf("%s\n", buildTime);
1077 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1078}
1079
1080void programUsage() {
george820978ddf2005-03-28 15:53:45 +00001081 InfoDialog usageDialog(usage_msg);
1082 usageDialog.showDialog();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001083}
1084
george825beb62a2005-02-09 13:04:32 +00001085char *fileName = 0;
george825e7af742005-03-10 14:26:00 +00001086
1087// playerOptions is the player options with default parameters values,
1088// it is used only for run the player with command-line parameters
1089PlayerOptions playerOptions;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001090bool print_usage = false;
george8205d86232005-03-28 12:16:08 +00001091bool print_upf_list = false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001092
1093bool processParams(int argc, char* argv[]) {
george82dfb557b2005-03-21 18:26:50 +00001094 playerOptions.commandLineParam = true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001095 for (int i = 1; i < argc; i++) {
1096 if ((strcasecmp(argv[i], "-help") == 0) ||
1097 (strcasecmp(argv[i], "--help") == 0) ||
1098 (strcasecmp(argv[i], "/help") == 0) ||
1099 (strcasecmp(argv[i], "-h") == 0) ||
1100 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001101 (strcasecmp(argv[i], "/?") == 0) ||
1102 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001103 print_usage = true;
1104 return true;
1105 }
1106
george825caee412005-03-09 09:52:10 +00001107 if ((strcasecmp(argv[i], "-pf") == 0) ||
1108 (strcasecmp(argv[i], "/pf") == 0) && (i < argc-1)) {
george82dfb557b2005-03-21 18:26:50 +00001109 char *pf = argv[++i];
1110 char rgb_order[4] = "\0";
1111 int order = RGB_ORDER;
1112 int r = -1, g = -1, b = -1;
1113 bool big_endian = false;
1114 if (strlen(pf) < 6) return false;
1115 while (strlen(pf)) {
1116 if ((pf[0] == 'r') || (pf[0] == 'R')) {
1117 if (r >=0 ) return false;
1118 r = atoi(++pf);
1119 strcat(rgb_order, "r");
1120 continue;
1121 }
1122 if ((pf[0] == 'g') || (pf[0] == 'G')) {
1123 if (g >=0 ) return false;
1124 g = atoi(++pf);
1125 strcat(rgb_order, "g");
1126 continue;
1127 }
1128 if (((pf[0] == 'b') || (pf[0] == 'B')) &&
1129 (pf[1] != 'e') && (pf[1] != 'E')) {
1130 if (b >=0 ) return false;
1131 b = atoi(++pf);
1132 strcat(rgb_order, "b");
1133 continue;
1134 }
1135 if ((pf[0] == 'l') || (pf[0] == 'L') ||
1136 (pf[0] == 'b') || (pf[0] == 'B')) {
1137 if (strcasecmp(pf, "le") == 0) break;
1138 if (strcasecmp(pf, "be") == 0) { big_endian = true; break;}
1139 return false;
1140 }
1141 pf++;
george82193d8e42005-02-20 16:47:01 +00001142 }
george82888b8fb2005-03-22 15:02:39 +00001143 if ((r < 0) || (g < 0) || (b < 0) || (r + g + b > 32)) return false;
george82dfb557b2005-03-21 18:26:50 +00001144 if (strcasecmp(rgb_order, "rgb") == 0) { order = RGB_ORDER; }
1145 else if (strcasecmp(rgb_order, "rbg") == 0) { order = RBG_ORDER; }
1146 else if (strcasecmp(rgb_order, "grb") == 0) { order = GRB_ORDER; }
1147 else if (strcasecmp(rgb_order, "gbr") == 0) { order = GBR_ORDER; }
1148 else if (strcasecmp(rgb_order, "bgr") == 0) { order = BGR_ORDER; }
1149 else if (strcasecmp(rgb_order, "brg") == 0) { order = BRG_ORDER; }
1150 else return false;
1151 playerOptions.autoDetectPF = false;
1152 playerOptions.setPF(order, r, g, b, big_endian);
george82193d8e42005-02-20 16:47:01 +00001153 continue;
1154 }
1155
george8205d86232005-03-28 12:16:08 +00001156 if ((strcasecmp(argv[i], "-upf") == 0) ||
1157 (strcasecmp(argv[i], "/upf") == 0) && (i < argc-1)) {
1158 if ((i == argc - 1) || (argv[++i][0] == '-')) {
1159 print_upf_list = true;
1160 return true;
1161 }
1162 PixelFormatList userPfList;
1163 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
1164 int index = userPfList.getIndexByPFName(argv[i]);
1165 if (index > 0) {
1166 playerOptions.autoDetectPF = false;
1167 playerOptions.setPF(&userPfList[index]->PF);
1168 } else {
1169 return false;
1170 }
1171 continue;
1172 }
george82193d8e42005-02-20 16:47:01 +00001173
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001174 if ((strcasecmp(argv[i], "-speed") == 0) ||
1175 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001176 double playbackSpeed = atof(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001177 if (playbackSpeed <= 0) {
1178 return false;
1179 }
george825e7af742005-03-10 14:26:00 +00001180 playerOptions.playbackSpeed = playbackSpeed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001181 continue;
1182 }
1183
1184 if ((strcasecmp(argv[i], "-pos") == 0) ||
1185 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001186 long initTime = atol(argv[++i]);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001187 if (initTime <= 0)
1188 return false;
george825e7af742005-03-10 14:26:00 +00001189 playerOptions.initTime = initTime;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001190 continue;
1191 }
1192
1193 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1194 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george825e7af742005-03-10 14:26:00 +00001195 playerOptions.autoPlay = true;
george82e6883de2005-02-08 14:42:12 +00001196 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001197 }
1198
george82ee455792005-04-13 12:56:15 +00001199 if ((strcasecmp(argv[i], "-loop") == 0) ||
1200 (strcasecmp(argv[i], "/loop") == 0) && (i < argc-1)) {
1201 playerOptions.loopPlayback = true;
1202 continue;
1203 }
1204
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001205 if (i != argc - 1)
1206 return false;
1207 }
1208
1209 fileName = strDup(argv[argc-1]);
george822a559f62005-04-11 15:25:36 +00001210 if (fileName[0] == '-') return false;
1211 else return true;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001212}
1213
1214//
1215// -=- WinMain
1216//
1217
1218int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1219
1220 // - Process the command-line
1221
1222 int argc = __argc;
1223 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001224 if ((argc > 1) && (!processParams(argc, argv))) {
1225 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1226 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001227 }
george82e6883de2005-02-08 14:42:12 +00001228
1229 if (print_usage) {
1230 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001231 return 0;
george8267cbcd02005-01-16 15:39:56 +00001232 }
george8205d86232005-03-28 12:16:08 +00001233 // Show the user defined pixel formats if required
1234 if (print_upf_list) {
george820978ddf2005-03-28 15:53:45 +00001235 int list_size = 256;
george8205d86232005-03-28 12:16:08 +00001236 char *upf_list = new char[list_size];
1237 PixelFormatList userPfList;
1238 userPfList.readUserDefinedPF(HKEY_CURRENT_USER, UPF_REGISTRY_PATH);
george820978ddf2005-03-28 15:53:45 +00001239 strcpy(upf_list, "The list of the user defined pixel formats:\r\n");
george8205d86232005-03-28 12:16:08 +00001240 for (int i = userPfList.getDefaultPFCount(); i < userPfList.count(); i++) {
1241 if ((list_size - strlen(upf_list) - 1) <
1242 (strlen(userPfList[i]->format_name) + 2)) {
1243 char *tmpStr = new char[list_size =
1244 list_size + strlen(userPfList[i]->format_name) + 2];
1245 strcpy(tmpStr, upf_list);
1246 delete [] upf_list;
1247 upf_list = new char[list_size];
1248 strcpy(upf_list, tmpStr);
1249 delete [] tmpStr;
1250 }
1251 strcat(upf_list, userPfList[i]->format_name);
george820978ddf2005-03-28 15:53:45 +00001252 strcat(upf_list, "\r\n");
george8205d86232005-03-28 12:16:08 +00001253 }
george820978ddf2005-03-28 15:53:45 +00001254 InfoDialog upfInfoDialog(upf_list);
1255 upfInfoDialog.showDialog();
george8205d86232005-03-28 12:16:08 +00001256 delete [] upf_list;
1257 return 0;
1258 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001259
george82e6883de2005-02-08 14:42:12 +00001260 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001261 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001262 try {
george825e7af742005-03-10 14:26:00 +00001263 player = new RfbPlayer(fileName, &playerOptions);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001264 } catch (rdr::Exception e) {
Constantin Kaplinsky4aaf63b2005-09-28 16:49:36 +00001265 MessageBox(NULL, e.str(), "RFB Player", MB_OK | MB_ICONERROR);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001266 delete player;
1267 return 0;
1268 }
1269
1270 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001271 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001272 MSG msg;
1273 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001274 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1275 TranslateMessage(&msg);
1276 DispatchMessage(&msg);
1277 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001278 }
1279
george82e6883de2005-02-08 14:42:12 +00001280 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001281 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001282 if (player) delete player;
1283 } catch (rdr::Exception e) {
Constantin Kaplinsky4aaf63b2005-09-28 16:49:36 +00001284 MessageBox(NULL, e.str(), "RFB Player", MB_OK | MB_ICONERROR);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001285 }
1286
1287 return 0;
1288};