blob: 60c1c3be3e06fcb5f92a9c1d2a4e1f226401b01f [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>
27
28#include <rfbplayer/rfbplayer.h>
29#include <rfbplayer/utils.h>
30#include <rfbplayer/resource.h>
george827549df42005-02-08 16:31:02 +000031#include <rfbplayer/GotoPosDialog.h>
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000032
33using namespace rfb;
34using namespace rfb::win32;
35
36// -=- Variables & consts
37
38static LogWriter vlog("RfbPlayer");
39
40TStr rfb::win32::AppName("RfbPlayer");
41extern const char* buildTime;
42
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[] =
48 "usage: rfbplayer <options> <filename>\n"
49 "Command-line options:\n"
50 " -help \t- Provide usage information.\n"
51 " -speed <value>\t- Sets playback speed, where 1 is normal speed,\n"
52 " \t is double speed, 0.5 is half speed. Default: 1.0.\n"
53 " -pos <ms> \t- Sets initial time position in the session file,\n"
54 " \t in milliseconds. Default: 0.\n"
55 " -autoplay \t- Runs the player in the playback mode.\n"
56 " -bell \t- Accepts the bell.\n";
57
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000058// -=- RfbPlayer's defines
59
60#define strcasecmp _stricmp
george824ea27f62005-01-29 15:03:06 +000061#define MAX_SPEED 10
george82d4d69e62005-02-05 09:23:18 +000062#define MAX_POS_TRACKBAR_RANGE 50
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000063
george82d070c692005-01-19 16:44:04 +000064#define ID_TOOLBAR 500
65#define ID_PLAY 510
66#define ID_PAUSE 520
67#define ID_TIME_STATIC 530
68#define ID_SPEED_STATIC 540
69#define ID_SPEED_EDIT 550
70#define ID_POS_TRACKBAR 560
71#define ID_SPEED_UPDOWN 570
72
73
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000074//
75// -=- RfbPlayerClass
76
77//
78// Window class used as the basis for RfbPlayer instance
79//
80
81class RfbPlayerClass {
82public:
83 RfbPlayerClass();
84 ~RfbPlayerClass();
85 ATOM classAtom;
86 HINSTANCE instance;
87};
88
89LRESULT CALLBACK RfbPlayerProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
90 LRESULT result;
91
92 if (msg == WM_CREATE)
93 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
94 else if (msg == WM_DESTROY) {
95 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +000096
97 // Resume playback (It's need to quit from FbsInputStream::waitWhilePaused())
98 _this->setPaused(false);
99 SetWindowLong(hwnd, GWL_USERDATA, 0);
100 }
101 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
102 if (!_this) {
103 vlog.info("null _this in %x, message %u", hwnd, msg);
104 return DefWindowProc(hwnd, msg, wParam, lParam);
105 }
106
107 try {
108 result = _this->processMainMessage(hwnd, msg, wParam, lParam);
109 } catch (rdr::Exception& e) {
110 vlog.error("untrapped: %s", e.str());
111 }
112
113 return result;
114};
115
116RfbPlayerClass::RfbPlayerClass() : classAtom(0) {
117 WNDCLASS wndClass;
118 wndClass.style = 0;
119 wndClass.lpfnWndProc = RfbPlayerProc;
120 wndClass.cbClsExtra = 0;
121 wndClass.cbWndExtra = 0;
122 wndClass.hInstance = instance = GetModuleHandle(0);
123 wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(0),
george827214b822004-12-12 07:02:51 +0000124 MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000125 if (!wndClass.hIcon)
126 printf("unable to load icon:%ld", GetLastError());
127 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
128 wndClass.hbrBackground = HBRUSH(COLOR_WINDOW);
george82c2c691f2004-12-08 18:04:14 +0000129 wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000130 wndClass.lpszClassName = _T("RfbPlayerClass");
131 classAtom = RegisterClass(&wndClass);
132 if (!classAtom) {
133 throw rdr::SystemException("unable to register RfbPlayer window class",
134 GetLastError());
135 }
136}
137
138RfbPlayerClass::~RfbPlayerClass() {
139 if (classAtom) {
140 UnregisterClass((const TCHAR*)classAtom, instance);
141 }
142}
143
144RfbPlayerClass baseClass;
145
146//
147// -=- RfbFrameClass
148
149//
150// Window class used to displaying the rfb data
151//
152
153class RfbFrameClass {
154public:
155 RfbFrameClass();
156 ~RfbFrameClass();
157 ATOM classAtom;
158 HINSTANCE instance;
159};
160
161LRESULT CALLBACK FrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
162 LRESULT result;
163
164 if (msg == WM_CREATE)
165 SetWindowLong(hwnd, GWL_USERDATA, (long)((CREATESTRUCT*)lParam)->lpCreateParams);
166 else if (msg == WM_DESTROY)
167 SetWindowLong(hwnd, GWL_USERDATA, 0);
168 RfbPlayer* _this = (RfbPlayer*) GetWindowLong(hwnd, GWL_USERDATA);
169 if (!_this) {
170 vlog.info("null _this in %x, message %u", hwnd, msg);
171 return DefWindowProc(hwnd, msg, wParam, lParam);
172 }
173
174 try {
175 result = _this->processFrameMessage(hwnd, msg, wParam, lParam);
176 } catch (rdr::Exception& e) {
177 vlog.error("untrapped: %s", e.str());
178 }
179
180 return result;
181}
182
183RfbFrameClass::RfbFrameClass() : classAtom(0) {
184 WNDCLASS wndClass;
185 wndClass.style = 0;
186 wndClass.lpfnWndProc = FrameProc;
187 wndClass.cbClsExtra = 0;
188 wndClass.cbWndExtra = 0;
189 wndClass.hInstance = instance = GetModuleHandle(0);
190 wndClass.hIcon = 0;
191 wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
192 wndClass.hbrBackground = 0;
193 wndClass.lpszMenuName = 0;
194 wndClass.lpszClassName = _T("RfbPlayerClass1");
195 classAtom = RegisterClass(&wndClass);
196 if (!classAtom) {
197 throw rdr::SystemException("unable to register RfbPlayer window class",
198 GetLastError());
199 }
200}
201
202RfbFrameClass::~RfbFrameClass() {
203 if (classAtom) {
204 UnregisterClass((const TCHAR*)classAtom, instance);
205 }
206}
207
208RfbFrameClass frameClass;
209
210//
211// -=- RfbPlayer instance implementation
212//
213
214RfbPlayer::RfbPlayer(char *_fileName, long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000215 bool _autoplay = false, bool _acceptBell = false)
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000216: RfbProto(_fileName), initTime(_initTime), playbackSpeed(_playbackSpeed),
george82e6883de2005-02-08 14:42:12 +0000217 autoplay(_autoplay), buffer(0), client_size(0, 0, 32, 32),
george82b4915432005-01-30 17:10:57 +0000218 window_size(0, 0, 32, 32), cutText(0), seekMode(false), fileName(_fileName),
george82d4d69e62005-02-05 09:23:18 +0000219 serverInitTime(0), lastPos(0), timeStatic(0), speedEdit(0), posTrackBar(0),
george828a471482005-02-06 07:15:53 +0000220 speedUpDown(0), acceptBell(_acceptBell), rfbReader(0), sessionTimeMs(0),
george8231a36332005-02-06 17:27:34 +0000221 sliderDraging(false), sliderStepMs(0), loopPlayback(false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000222
george82e6883de2005-02-08 14:42:12 +0000223 CTRL_BAR_HEIGHT = 28;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000224
george823c8fbbf2005-01-24 11:09:08 +0000225 // Reset the full session time
226 strcpy(fullSessionTime, "00m:00s");
227
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000228 // Create the main window
229 const TCHAR* name = _T("RfbPlayer");
230 mainHwnd = CreateWindow((const TCHAR*)baseClass.classAtom, name, WS_OVERLAPPEDWINDOW,
george8210313102005-01-17 13:11:40 +0000231 0, 0, 640, 480, 0, 0, baseClass.instance, this);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000232 if (!mainHwnd) {
233 throw rdr::SystemException("unable to create WMNotifier window instance", GetLastError());
234 }
235 vlog.debug("created window \"%s\" (%x)", (const char*)CStr(name), getMainHandle());
236
237 // Create the backing buffer
238 buffer = new win32::DIBSectionBuffer(getFrameHandle());
george8210313102005-01-17 13:11:40 +0000239 setVisible(true);
george825beb62a2005-02-09 13:04:32 +0000240
george8217e92cb2005-01-31 16:01:02 +0000241 // Open the session file
242 if (fileName) {
243 openSessionFile(fileName);
george82e6883de2005-02-08 14:42:12 +0000244 if (initTime > 0) setPos(initTime);
245 setSpeed(playbackSpeed);
george8217e92cb2005-01-31 16:01:02 +0000246 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000247}
248
249RfbPlayer::~RfbPlayer() {
250 vlog.debug("~RfbPlayer");
george82ce8dc3a2005-01-31 13:06:54 +0000251 if (rfbReader) {
george82ce8dc3a2005-01-31 13:06:54 +0000252 delete rfbReader->join();
253 rfbReader = 0;
254 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000255 if (mainHwnd) {
256 setVisible(false);
257 DestroyWindow(mainHwnd);
258 mainHwnd = 0;
259 }
george825beb62a2005-02-09 13:04:32 +0000260 if (buffer) delete buffer;
261 if (cutText) delete [] cutText;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000262 vlog.debug("~RfbPlayer done");
263}
264
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000265LRESULT
266RfbPlayer::processMainMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
267 switch (msg) {
268
269 // -=- Process standard window messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000270
271 case WM_CREATE:
272 {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000273 // Create the frame window
274 frameHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, (const TCHAR*)frameClass.classAtom,
275 0, WS_CHILD | WS_VISIBLE, 0, CTRL_BAR_HEIGHT, 10, CTRL_BAR_HEIGHT + 10,
276 hwnd, 0, frameClass.instance, this);
277
george82d070c692005-01-19 16:44:04 +0000278 createToolBar(hwnd);
279
george82006f2792005-02-05 07:40:47 +0000280 hMenu = GetMenu(hwnd);
george825c13c662005-01-27 14:48:23 +0000281
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000282 return 0;
283 }
284
george827214b822004-12-12 07:02:51 +0000285 // Process the main menu and toolbar's messages
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000286
287 case WM_COMMAND:
george825c13c662005-01-27 14:48:23 +0000288 switch (LOWORD(wParam)) {
george826e51fcc2005-02-06 13:30:49 +0000289 case ID_OPENFILE:
290 {
291 char curDir[_MAX_DIR];
292 static char filename[_MAX_PATH];
293 OPENFILENAME ofn;
294 memset((void *) &ofn, 0, sizeof(OPENFILENAME));
295 GetCurrentDirectory(sizeof(curDir), curDir);
296
297 ofn.lStructSize = sizeof(OPENFILENAME);
298 ofn.hwndOwner = getMainHandle();
299 ofn.lpstrFile = filename;
300 ofn.nMaxFile = sizeof(filename);
301 ofn.lpstrInitialDir = curDir;
302 ofn.lpstrFilter = "Rfb Session files (*.rfb)\0*.rfb\0" \
303 "All files (*.*)\0*.*\0";
304 ofn.lpstrDefExt = "rfb";
305 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
306 if (GetOpenFileName(&ofn))
307 openSessionFile(filename);
308 }
309 break;
george825c13c662005-01-27 14:48:23 +0000310 case ID_PLAY:
311 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000312 break;
313 case ID_PAUSE:
314 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000315 break;
316 case ID_STOP:
317 if (getTimeOffset() != 0) {
george82006f2792005-02-05 07:40:47 +0000318 stopPlayback();
george825c13c662005-01-27 14:48:23 +0000319 }
george825c13c662005-01-27 14:48:23 +0000320 break;
321 case ID_PLAYPAUSE:
322 if (isPaused()) {
323 setPaused(false);
george825c13c662005-01-27 14:48:23 +0000324 } else {
325 setPaused(true);
george825c13c662005-01-27 14:48:23 +0000326 }
george825c13c662005-01-27 14:48:23 +0000327 break;
george827549df42005-02-08 16:31:02 +0000328 case ID_GOTO:
329 {
330 GotoPosDialog gotoPosDlg;
331 if (gotoPosDlg.showDialog()) {
332 setPos(gotoPosDlg.getPos());
333 updatePos(getTimeOffset());
334 }
335 }
336 break;
george825c13c662005-01-27 14:48:23 +0000337 case ID_FULLSCREEN:
338 MessageBox(getMainHandle(), "It is not working yet!", "RfbPlayer", MB_OK);
339 break;
george8231a36332005-02-06 17:27:34 +0000340 case ID_LOOP:
341 loopPlayback = !loopPlayback;
342 if (loopPlayback) CheckMenuItem(hMenu, ID_LOOP, MF_CHECKED);
343 else CheckMenuItem(hMenu, ID_LOOP, MF_UNCHECKED);
344 break;
george824ea27f62005-01-29 15:03:06 +0000345 case ID_RETURN:
346 // Update the speed if return pressed in speedEdit
347 if (speedEdit == GetFocus()) {
348 char speedStr[20], *stopStr;
349 GetWindowText(speedEdit, speedStr, sizeof(speedStr));
350 double speed = strtod(speedStr, &stopStr);
351 if (speed > 0) {
352 speed = min(MAX_SPEED, speed);
353 // Update speedUpDown position
354 SendMessage(speedUpDown, UDM_SETPOS,
355 0, MAKELONG((short)(speed / 0.5), 0));
356 } else {
357 speed = getSpeed();
358 }
359 setSpeed(speed);
360 sprintf(speedStr, "%.2f", speed);
361 SetWindowText(speedEdit, speedStr);
362 }
363 break;
george8201aa6732005-02-06 17:13:03 +0000364 case ID_EXIT:
george825beb62a2005-02-09 13:04:32 +0000365 if (is) is->resumePlayback();
george8201aa6732005-02-06 17:13:03 +0000366 PostQuitMessage(0);
367 break;
george82ef5f7262005-02-08 15:09:26 +0000368 case ID_HELP_COMMANDLINESWITCHES:
george8259f84532005-02-08 15:01:39 +0000369 MessageBox(getMainHandle(),
370 usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
371 break;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000372 }
373 break;
374
375 // Update frame's window size and add scrollbars if required
376
377 case WM_SIZE:
378 {
george82d070c692005-01-19 16:44:04 +0000379
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000380 Point old_offset = bufferToClient(Point(0, 0));
381
382 // Update the cached sizing information
383 RECT r;
384 GetClientRect(getMainHandle(), &r);
385 MoveWindow(getFrameHandle(), 0, CTRL_BAR_HEIGHT, r.right - r.left,
386 r.bottom - r.top - CTRL_BAR_HEIGHT, TRUE);
387
388 GetWindowRect(getFrameHandle(), &r);
389 window_size = Rect(r.left, r.top, r.right, r.bottom);
390 GetClientRect(getFrameHandle(), &r);
391 client_size = Rect(r.left, r.top, r.right, r.bottom);
392
393 // Determine whether scrollbars are required
394 calculateScrollBars();
george82d070c692005-01-19 16:44:04 +0000395
396 // Resize the ToolBar
397 tb.autoSize();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000398
399 // Redraw if required
400 if (!old_offset.equals(bufferToClient(Point(0, 0))))
401 InvalidateRect(getFrameHandle(), 0, TRUE);
402 }
403 break;
george828a471482005-02-06 07:15:53 +0000404
405 // Process messages from posTrackBar
406
407 case WM_HSCROLL:
408 {
409 long Pos = SendMessage(posTrackBar, TBM_GETPOS, 0, 0);
410 Pos *= sliderStepMs;
411
412 switch (LOWORD(wParam)) {
413 case TB_PAGEUP:
414 case TB_PAGEDOWN:
415 case TB_LINEUP:
416 case TB_LINEDOWN:
417 case TB_THUMBTRACK:
418 sliderDraging = true;
419 updatePos(Pos);
420 return 0;
421 case TB_ENDTRACK:
422 setPos(Pos);
george828a471482005-02-06 07:15:53 +0000423 sliderDraging = false;
424 return 0;
425 default:
426 break;
427 }
428 }
429 break;
george829e6e6cc2005-01-29 13:12:05 +0000430
431 case WM_NOTIFY:
432 switch (((NMHDR*)lParam)->code) {
433 case UDN_DELTAPOS:
434 if ((int)wParam == ID_SPEED_UPDOWN) {
george824ea27f62005-01-29 15:03:06 +0000435 BOOL lResult = FALSE;
george829e6e6cc2005-01-29 13:12:05 +0000436 char speedStr[20] = "\0";
437 DWORD speedRange = SendMessage(speedUpDown, UDM_GETRANGE, 0, 0);
438 LPNM_UPDOWN upDown = (LPNM_UPDOWN)lParam;
439 double speed;
440
george824ea27f62005-01-29 15:03:06 +0000441 // The out of range checking
george829e6e6cc2005-01-29 13:12:05 +0000442 if (upDown->iDelta > 0) {
443 speed = min(upDown->iPos + upDown->iDelta, LOWORD(speedRange)) * 0.5;
444 } else {
george824ea27f62005-01-29 15:03:06 +0000445 // It's need to round the UpDown position
446 if ((upDown->iPos * 0.5) != getSpeed()) {
447 upDown->iDelta = 0;
448 lResult = TRUE;
449 }
george829e6e6cc2005-01-29 13:12:05 +0000450 speed = max(upDown->iPos + upDown->iDelta, HIWORD(speedRange)) * 0.5;
451 }
452 _gcvt(speed, 5, speedStr);
453 sprintf(speedStr, "%.2f", speed);
454 SetWindowText(speedEdit, speedStr);
455 setSpeed(speed);
george824ea27f62005-01-29 15:03:06 +0000456 return lResult;
george829e6e6cc2005-01-29 13:12:05 +0000457 }
george824ea27f62005-01-29 15:03:06 +0000458 }
george829e6e6cc2005-01-29 13:12:05 +0000459 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000460
461 case WM_CLOSE:
462 vlog.debug("WM_CLOSE %x", getMainHandle());
463 PostQuitMessage(0);
464 break;
465 }
466
467 return rfb::win32::SafeDefWindowProc(getMainHandle(), msg, wParam, lParam);
468}
469
470LRESULT RfbPlayer::processFrameMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
471 switch (msg) {
472
473 case WM_PAINT:
474 {
george825beb62a2005-02-09 13:04:32 +0000475 if (isSeeking()) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000476 seekMode = true;
477 return 0;
478 } else {
479 if (seekMode) {
480 seekMode = false;
481 InvalidateRect(getFrameHandle(), 0, true);
482 UpdateWindow(getFrameHandle());
483 return 0;
484 }
485 }
486
487 PAINTSTRUCT ps;
488 HDC paintDC = BeginPaint(getFrameHandle(), &ps);
489 if (!paintDC)
490 throw SystemException("unable to BeginPaint", GetLastError());
491 Rect pr = Rect(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
492
493 if (!pr.is_empty()) {
494
495 if (buffer->bitmap) {
496
497 // Get device context
498 BitmapDC bitmapDC(paintDC, buffer->bitmap);
499
500 // Blit the border if required
501 Rect bufpos = bufferToClient(buffer->getRect());
502 if (!pr.enclosed_by(bufpos)) {
503 vlog.debug("draw border");
504 HBRUSH black = (HBRUSH) GetStockObject(BLACK_BRUSH);
505 RECT r;
506 SetRect(&r, 0, 0, bufpos.tl.x, client_size.height()); FillRect(paintDC, &r, black);
507 SetRect(&r, bufpos.tl.x, 0, bufpos.br.x, bufpos.tl.y); FillRect(paintDC, &r, black);
508 SetRect(&r, bufpos.br.x, 0, client_size.width(), client_size.height()); FillRect(paintDC, &r, black);
509 SetRect(&r, bufpos.tl.x, bufpos.br.y, bufpos.br.x, client_size.height()); FillRect(paintDC, &r, black);
510 }
511
512 // Do the blit
513 Point buf_pos = clientToBuffer(pr.tl);
514 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
515 bitmapDC, buf_pos.x, buf_pos.y, SRCCOPY))
516 throw SystemException("unable to BitBlt to window", GetLastError());
517
518 } else {
519 // Blit a load of black
520 if (!BitBlt(paintDC, pr.tl.x, pr.tl.y, pr.width(), pr.height(),
521 0, 0, 0, BLACKNESS))
522 throw SystemException("unable to BitBlt to blank window", GetLastError());
523 }
524 }
525 EndPaint(getFrameHandle(), &ps);
526 }
527 return 0;
528
529 case WM_VSCROLL:
530 case WM_HSCROLL:
531 {
532 Point delta;
533 int newpos = (msg == WM_VSCROLL) ? scrolloffset.y : scrolloffset.x;
534
535 switch (LOWORD(wParam)) {
536 case SB_PAGEUP: newpos -= 50; break;
537 case SB_PAGEDOWN: newpos += 50; break;
538 case SB_LINEUP: newpos -= 5; break;
539 case SB_LINEDOWN: newpos += 5; break;
540 case SB_THUMBTRACK:
541 case SB_THUMBPOSITION: newpos = HIWORD(wParam); break;
542 default: vlog.info("received unknown scroll message");
543 };
544
545 if (msg == WM_HSCROLL)
546 setViewportOffset(Point(newpos, scrolloffset.y));
547 else
548 setViewportOffset(Point(scrolloffset.x, newpos));
549
550 SCROLLINFO si;
551 si.cbSize = sizeof(si);
552 si.fMask = SIF_POS;
553 si.nPos = newpos;
554 SetScrollInfo(getFrameHandle(), (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ, &si, TRUE);
555 }
556 break;
557 }
558
559 return DefWindowProc(hwnd, msg, wParam, lParam);
560}
561
562void RfbPlayer::setOptions(long _initTime = 0, double _playbackSpeed = 1.0,
george82e6883de2005-02-08 14:42:12 +0000563 bool _autoplay = false) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000564 autoplay = _autoplay;
565 playbackSpeed = _playbackSpeed;
566 initTime = _initTime;
567}
568
569void RfbPlayer::applyOptions() {
570 if (initTime >= 0)
571 setPos(initTime);
572 setSpeed(playbackSpeed);
573 setPaused(!autoplay);
george82d070c692005-01-19 16:44:04 +0000574}
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000575
george82d070c692005-01-19 16:44:04 +0000576void RfbPlayer::createToolBar(HWND parentHwnd) {
577 RECT tRect;
578 InitCommonControls();
579
580 tb.create(ID_TOOLBAR, parentHwnd);
581 tb.addBitmap(4, IDB_TOOLBAR);
582
583 // Create the control buttons
584 tb.addButton(0, ID_PLAY);
585 tb.addButton(1, ID_PAUSE);
586 tb.addButton(2, ID_STOP);
587 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
588 tb.addButton(3, ID_FULLSCREEN);
589 tb.addButton(0, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
590
591 // Create the static control for the time output
592 tb.addButton(125, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
593 tb.getButtonRect(6, &tRect);
594 timeStatic = CreateWindowEx(0, "Static", "00m:00s (00m:00s)",
595 WS_CHILD | WS_VISIBLE, tRect.left, tRect.top+2, tRect.right-tRect.left,
596 tRect.bottom-tRect.top, tb.getHandle(), (HMENU)ID_TIME_STATIC,
597 GetModuleHandle(0), 0);
598 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
599
600 // Create the trackbar control for the time position
601 tb.addButton(200, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
602 tb.getButtonRect(8, &tRect);
george82d4d69e62005-02-05 09:23:18 +0000603 posTrackBar = CreateWindowEx(0, TRACKBAR_CLASS, "Trackbar Control",
george82d070c692005-01-19 16:44:04 +0000604 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_ENABLESELRANGE,
605 tRect.left, tRect.top, tRect.right-tRect.left, tRect.bottom-tRect.top,
606 parentHwnd, (HMENU)ID_POS_TRACKBAR, GetModuleHandle(0), 0);
607 // It's need to send notify messages to toolbar parent window
george82d4d69e62005-02-05 09:23:18 +0000608 SetParent(posTrackBar, tb.getHandle());
george82d070c692005-01-19 16:44:04 +0000609 tb.addButton(0, 10, TBSTATE_ENABLED, TBSTYLE_SEP);
610
611 // Create the label with "Speed:" caption
612 tb.addButton(50, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
613 tb.getButtonRect(10, &tRect);
614 CreateWindowEx(0, "Static", "Speed:", WS_CHILD | WS_VISIBLE,
615 tRect.left, tRect.top+2, tRect.right-tRect.left, tRect.bottom-tRect.top,
616 tb.getHandle(), (HMENU)ID_SPEED_STATIC, GetModuleHandle(0), 0);
617
618 // Create the edit control and the spin for the speed managing
619 tb.addButton(60, 0, TBSTATE_ENABLED, TBSTYLE_SEP);
620 tb.getButtonRect(11, &tRect);
621 speedEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "1.00",
622 WS_CHILD | WS_VISIBLE | ES_RIGHT, tRect.left, tRect.top,
623 tRect.right-tRect.left, tRect.bottom-tRect.top, parentHwnd,
624 (HMENU)ID_SPEED_EDIT, GetModuleHandle(0), 0);
625 // It's need to send notify messages to toolbar parent window
626 SetParent(speedEdit, tb.getHandle());
627
628 speedUpDown = CreateUpDownControl(WS_CHILD | WS_VISIBLE
629 | WS_BORDER | UDS_ALIGNRIGHT, 0, 0, 0, 0, tb.getHandle(),
george829e6e6cc2005-01-29 13:12:05 +0000630 ID_SPEED_UPDOWN, GetModuleHandle(0), speedEdit, 20, 1, 2);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000631}
632
633void RfbPlayer::setVisible(bool visible) {
634 ShowWindow(getMainHandle(), visible ? SW_SHOW : SW_HIDE);
635 if (visible) {
636 // When the window becomes visible, make it active
637 SetForegroundWindow(getMainHandle());
638 SetActiveWindow(getMainHandle());
639 }
640}
641
642void RfbPlayer::setTitle(const char *title) {
643 char _title[256];
644 strcpy(_title, AppName);
645 strcat(_title, " - ");
646 strcat(_title, title);
647 SetWindowText(getMainHandle(), _title);
648}
649
650void RfbPlayer::setFrameSize(int width, int height) {
651 // Calculate and set required size for main window
652 RECT r = {0, 0, width, height};
653 AdjustWindowRectEx(&r, GetWindowLong(getFrameHandle(), GWL_STYLE), FALSE,
654 GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
655 r.bottom += CTRL_BAR_HEIGHT; // Include RfbPlayr's controls area
656 AdjustWindowRect(&r, GetWindowLong(getMainHandle(), GWL_STYLE), FALSE);
657 SetWindowPos(getMainHandle(), 0, 0, 0, r.right-r.left, r.bottom-r.top,
658 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
659
660 // Enable/disable scrollbars as appropriate
661 calculateScrollBars();
662}
663
664void RfbPlayer::calculateScrollBars() {
665 // Calculate the required size of window
666 DWORD current_style = GetWindowLong(getFrameHandle(), GWL_STYLE);
667 DWORD style = current_style & ~(WS_VSCROLL | WS_HSCROLL);
668 DWORD old_style;
669 RECT r;
670 SetRect(&r, 0, 0, buffer->width(), buffer->height());
671 AdjustWindowRectEx(&r, style, FALSE, GetWindowLong(getFrameHandle(), GWL_EXSTYLE));
672 Rect reqd_size = Rect(r.left, r.top, r.right, r.bottom);
673
674 // Work out whether scroll bars are required
675 do {
676 old_style = style;
677
678 if (!(style & WS_HSCROLL) && (reqd_size.width() > window_size.width())) {
679 style |= WS_HSCROLL;
680 reqd_size.br.y += GetSystemMetrics(SM_CXHSCROLL);
681 }
682 if (!(style & WS_VSCROLL) && (reqd_size.height() > window_size.height())) {
683 style |= WS_VSCROLL;
684 reqd_size.br.x += GetSystemMetrics(SM_CXVSCROLL);
685 }
686 } while (style != old_style);
687
688 // Tell Windows to update the window style & cached settings
689 if (style != current_style) {
690 SetWindowLong(getFrameHandle(), GWL_STYLE, style);
691 SetWindowPos(getFrameHandle(), NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
692 }
693
694 // Update the scroll settings
695 SCROLLINFO si;
696 if (style & WS_VSCROLL) {
697 si.cbSize = sizeof(si);
698 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
699 si.nMin = 0;
700 si.nMax = buffer->height();
701 si.nPage = buffer->height() - (reqd_size.height() - window_size.height());
702 maxscrolloffset.y = max(0, si.nMax-si.nPage);
703 scrolloffset.y = min(maxscrolloffset.y, scrolloffset.y);
704 si.nPos = scrolloffset.y;
705 SetScrollInfo(getFrameHandle(), SB_VERT, &si, TRUE);
706 }
707 if (style & WS_HSCROLL) {
708 si.cbSize = sizeof(si);
709 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
710 si.nMin = 0;
711 si.nMax = buffer->width();
712 si.nPage = buffer->width() - (reqd_size.width() - window_size.width());
713 maxscrolloffset.x = max(0, si.nMax-si.nPage);
714 scrolloffset.x = min(maxscrolloffset.x, scrolloffset.x);
715 si.nPos = scrolloffset.x;
716 SetScrollInfo(getFrameHandle(), SB_HORZ, &si, TRUE);
717 }
718}
719
720bool RfbPlayer::setViewportOffset(const Point& tl) {
721/* ***
722 Point np = Point(max(0, min(maxscrolloffset.x, tl.x)),
723 max(0, min(maxscrolloffset.y, tl.y)));
724 */
725 Point np = Point(max(0, min(tl.x, buffer->width()-client_size.width())),
726 max(0, min(tl.y, buffer->height()-client_size.height())));
727 Point delta = np.translate(scrolloffset.negate());
728 if (!np.equals(scrolloffset)) {
729 scrolloffset = np;
730 ScrollWindowEx(getFrameHandle(), -delta.x, -delta.y, 0, 0, 0, 0, SW_INVALIDATE);
731 UpdateWindow(getFrameHandle());
732 return true;
733 }
734 return false;
735}
736
737void RfbPlayer::close(const char* reason) {
738 setVisible(false);
739 if (reason) {
740 vlog.info("closing - %s", reason);
741 MessageBox(NULL, TStr(reason), "RfbPlayer", MB_ICONINFORMATION | MB_OK);
742 }
743 SendMessage(getFrameHandle(), WM_CLOSE, 0, 0);
744}
745
746void RfbPlayer::blankBuffer() {
747 fillRect(buffer->getRect(), 0);
748}
749
750void RfbPlayer::rewind() {
george8223e08562005-01-31 15:16:42 +0000751 bool paused = isPaused();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000752 blankBuffer();
753 newSession(fileName);
754 skipHandshaking();
george8223e08562005-01-31 15:16:42 +0000755 setSpeed(playbackSpeed);
george828a471482005-02-06 07:15:53 +0000756 if (paused) is->pausePlayback();
757 else is->resumePlayback();
george8223e08562005-01-31 15:16:42 +0000758}
759
760void RfbPlayer::processMsg() {
761 static long update_time = GetTickCount();
762 try {
george828a471482005-02-06 07:15:53 +0000763 if ((!isSeeking()) && ((GetTickCount() - update_time) > 250)
764 && (!sliderDraging)) {
george8223e08562005-01-31 15:16:42 +0000765 // Update pos in the toolbar 4 times in 1 second
george828a471482005-02-06 07:15:53 +0000766 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000767 update_time = GetTickCount();
768 }
769 RfbProto::processMsg();
770 } catch (rdr::Exception e) {
771 if (strcmp(e.str(), "[End Of File]") == 0) {
772 rewind();
george8231a36332005-02-06 17:27:34 +0000773 setPaused(!loopPlayback);
george828a471482005-02-06 07:15:53 +0000774 updatePos(getTimeOffset());
george829403bee2005-02-06 11:14:39 +0000775 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8223e08562005-01-31 15:16:42 +0000776 return;
777 }
778 // It's a special exception to perform backward seeking.
779 // We only rewind the stream and seek the offset
780 if (strcmp(e.str(), "[REWIND]") == 0) {
781 long initTime = getSeekOffset();
782 rewind();
783 setPos(initTime);
george828a471482005-02-06 07:15:53 +0000784 updatePos(getTimeOffset());
george8223e08562005-01-31 15:16:42 +0000785 } else {
786 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
787 return;
788 }
789 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000790}
791
792void RfbPlayer::serverInit() {
793 RfbProto::serverInit();
794
795 // Save the server init time for using in setPos()
796 serverInitTime = getTimeOffset() / getSpeed();
797
798 // Resize the backing buffer
799 buffer->setSize(cp.width, cp.height);
800
801 // Check on the true colour mode
802 if (!(cp.pf()).trueColour)
Peter Ã…strandc81a6522004-12-30 11:32:08 +0000803 throw rdr::Exception("This version plays only true color session!");
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000804
805 // Set the session pixel format
806 buffer->setPF(cp.pf());
807
808 // If the window is not maximised then resize it
809 if (!(GetWindowLong(getMainHandle(), GWL_STYLE) & WS_MAXIMIZE))
810 setFrameSize(cp.width, cp.height);
811
812 // Set the window title and show it
813 setTitle(cp.name());
george82006f2792005-02-05 07:40:47 +0000814
george82d4d69e62005-02-05 09:23:18 +0000815 // Calculate the full session time and update posTrackBar control
george828a471482005-02-06 07:15:53 +0000816 sessionTimeMs = calculateSessionTime(fileName);
817 sprintf(fullSessionTime, "%.2um:%.2us",
818 sessionTimeMs / 1000 / 60, sessionTimeMs / 1000 % 60);
george82d4d69e62005-02-05 09:23:18 +0000819 SendMessage(posTrackBar, TBM_SETRANGE,
george828a471482005-02-06 07:15:53 +0000820 TRUE, MAKELONG(0, min(sessionTimeMs / 1000, MAX_POS_TRACKBAR_RANGE)));
821 sliderStepMs = sessionTimeMs / SendMessage(posTrackBar, TBM_GETRANGEMAX, 0, 0);
george828a471482005-02-06 07:15:53 +0000822 updatePos(getTimeOffset());
george82d4d69e62005-02-05 09:23:18 +0000823
george82006f2792005-02-05 07:40:47 +0000824 setPaused(!autoplay);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000825}
826
827void RfbPlayer::setColourMapEntries(int first, int count, U16* rgbs) {
828 vlog.debug("setColourMapEntries: first=%d, count=%d", first, count);
829 throw rdr::Exception("Can't handle SetColourMapEntries message", "RfbPlayer");
830/* int i;
831 for (i=0;i<count;i++) {
832 buffer->setColour(i+first, rgbs[i*3], rgbs[i*3+1], rgbs[i*3+2]);
833 }
834 // *** change to 0, 256?
835 refreshWindowPalette(first, count);
836 palette_changed = true;
837 InvalidateRect(getFrameHandle(), 0, FALSE);*/
838}
839
840void RfbPlayer::bell() {
841 if (acceptBell)
842 MessageBeep(-1);
843}
844
845void RfbPlayer::serverCutText(const char* str, int len) {
846 if (cutText != NULL)
847 delete [] cutText;
848 cutText = new char[len + 1];
849 memcpy(cutText, str, len);
850 cutText[len] = '\0';
851}
852
853void RfbPlayer::frameBufferUpdateEnd() {
854};
855
856void RfbPlayer::beginRect(const Rect& r, unsigned int encoding) {
857}
858
859void RfbPlayer::endRect(const Rect& r, unsigned int encoding) {
860}
861
862
863void RfbPlayer::fillRect(const Rect& r, Pixel pix) {
864 buffer->fillRect(r, pix);
865 invalidateBufferRect(r);
866}
867
868void RfbPlayer::imageRect(const Rect& r, void* pixels) {
869 buffer->imageRect(r, pixels);
870 invalidateBufferRect(r);
871}
872
873void RfbPlayer::copyRect(const Rect& r, int srcX, int srcY) {
874 buffer->copyRect(r, Point(r.tl.x-srcX, r.tl.y-srcY));
875 invalidateBufferRect(r);
876}
877
878bool RfbPlayer::invalidateBufferRect(const Rect& crect) {
879 Rect rect = bufferToClient(crect);
880 if (rect.intersect(client_size).is_empty()) return false;
881 RECT invalid = {rect.tl.x, rect.tl.y, rect.br.x, rect.br.y};
882 InvalidateRect(getFrameHandle(), &invalid, FALSE);
883 return true;
884}
885
george8257f13522005-02-05 08:48:22 +0000886long RfbPlayer::calculateSessionTime(char *filename) {
887 FbsInputStream sessionFile(filename);
george828a471482005-02-06 07:15:53 +0000888 sessionFile.setTimeOffset(100000000);
george8257f13522005-02-05 08:48:22 +0000889 try {
890 while (TRUE) {
891 sessionFile.skip(1024);
892 }
893 } catch (rdr::Exception e) {
894 if (strcmp(e.str(), "[End Of File]") == 0) {
george828a471482005-02-06 07:15:53 +0000895 return sessionFile.getTimeOffset();
george8257f13522005-02-05 08:48:22 +0000896 } else {
897 MessageBox(getMainHandle(), e.str(), e.type(), MB_OK | MB_ICONERROR);
898 return 0;
899 }
900 }
901 return 0;
902}
903
george8217e92cb2005-01-31 16:01:02 +0000904void RfbPlayer::openSessionFile(char *_fileName) {
905 fileName = strDup(_fileName);
906
907 // Close the previous reading thread
908 if (rfbReader) {
george826e51fcc2005-02-06 13:30:49 +0000909 is->resumePlayback();
george8217e92cb2005-01-31 16:01:02 +0000910 delete rfbReader->join();
911 }
912 blankBuffer();
913 newSession(fileName);
914 setSpeed(playbackSpeed);
915 rfbReader = new rfbSessionReader(this);
916 rfbReader->start();
george826e51fcc2005-02-06 13:30:49 +0000917 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george8217e92cb2005-01-31 16:01:02 +0000918}
919
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000920void RfbPlayer::setPaused(bool paused) {
921 if (paused) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000922 is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000923 tb.checkButton(ID_PAUSE, true);
924 tb.checkButton(ID_PLAY, false);
925 tb.checkButton(ID_STOP, false);
926 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
927 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000928 } else {
george825beb62a2005-02-09 13:04:32 +0000929 if (is) is->resumePlayback();
george82006f2792005-02-05 07:40:47 +0000930 tb.checkButton(ID_PLAY, true);
931 tb.checkButton(ID_STOP, false);
932 tb.checkButton(ID_PAUSE, false);
933 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_CHECKED);
934 CheckMenuItem(hMenu, ID_STOP, MF_UNCHECKED);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000935 }
936}
937
george82006f2792005-02-05 07:40:47 +0000938void RfbPlayer::stopPlayback() {
939 setPos(0);
george825beb62a2005-02-09 13:04:32 +0000940 if (is) is->pausePlayback();
george82006f2792005-02-05 07:40:47 +0000941 tb.checkButton(ID_STOP, true);
942 tb.checkButton(ID_PLAY, false);
943 tb.checkButton(ID_PAUSE, false);
944 CheckMenuItem(hMenu, ID_STOP, MF_CHECKED);
945 CheckMenuItem(hMenu, ID_PLAYPAUSE, MF_UNCHECKED);
george826da02d72005-02-06 17:02:34 +0000946 SendMessage(posTrackBar, TBM_SETPOS, TRUE, 0);
george82006f2792005-02-05 07:40:47 +0000947}
948
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000949void RfbPlayer::setSpeed(double speed) {
950 serverInitTime = serverInitTime * getSpeed() / speed;
951 is->setSpeed(speed);
george8223e08562005-01-31 15:16:42 +0000952 playbackSpeed = speed;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000953}
954
955double RfbPlayer::getSpeed() {
956 return is->getSpeed();
957}
958
959void RfbPlayer::setPos(long pos) {
960 is->setTimeOffset(max(pos, serverInitTime));
961}
962
963long RfbPlayer::getSeekOffset() {
964 return is->getSeekOffset();
965}
966
967bool RfbPlayer::isSeeking() {
george825beb62a2005-02-09 13:04:32 +0000968 if (is) return is->isSeeking();
969 else return false;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000970}
971
972bool RfbPlayer::isSeekMode() {
973 return seekMode;
974}
975
976bool RfbPlayer::isPaused() {
977 return is->isPaused();
978}
979
980long RfbPlayer::getTimeOffset() {
george828a471482005-02-06 07:15:53 +0000981 return max(is->getTimeOffset(), is->getSeekOffset());
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000982}
983
george828a471482005-02-06 07:15:53 +0000984void RfbPlayer::updatePos(long newPos) {
985 // Update time pos in static control
george823c8fbbf2005-01-24 11:09:08 +0000986 char timePos[30] = "\0";
george829403bee2005-02-06 11:14:39 +0000987 long sliderPos = newPos;
george828a471482005-02-06 07:15:53 +0000988 newPos /= 1000;
george8244325492005-02-06 07:29:51 +0000989 sprintf(timePos, "%.2um:%.2us (%s)", newPos/60, newPos%60, fullSessionTime);
george823c8fbbf2005-01-24 11:09:08 +0000990 SetWindowText(timeStatic, timePos);
george828a471482005-02-06 07:15:53 +0000991
992 // Update the position of slider
993 if (!sliderDraging) {
994 sliderPos /= sliderStepMs;
george829403bee2005-02-06 11:14:39 +0000995 if (sliderPos > SendMessage(posTrackBar, TBM_GETPOS, 0, 0))
996 SendMessage(posTrackBar, TBM_SETPOS, TRUE, sliderPos);
george828a471482005-02-06 07:15:53 +0000997 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +0000998}
999
1000void RfbPlayer::skipHandshaking() {
1001 int skipBytes = 12 + 4 + 24 + strlen(cp.name());
1002 is->skip(skipBytes);
1003 state_ = RFBSTATE_NORMAL;
1004}
1005
1006void programInfo() {
1007 win32::FileVersionInfo inf;
1008 _tprintf(_T("%s - %s, Version %s\n"),
1009 inf.getVerString(_T("ProductName")),
1010 inf.getVerString(_T("FileDescription")),
1011 inf.getVerString(_T("FileVersion")));
1012 printf("%s\n", buildTime);
1013 _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright")));
1014}
1015
1016void programUsage() {
george82e6883de2005-02-08 14:42:12 +00001017 MessageBox(0, usage_msg, "RfbPlayer", MB_OK | MB_ICONINFORMATION);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001018}
1019
1020double playbackSpeed = 1.0;
1021long initTime = -1;
1022bool autoplay = false;
george825beb62a2005-02-09 13:04:32 +00001023char *fileName = 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001024bool print_usage = false;
1025bool acceptBell = false;
1026
1027bool processParams(int argc, char* argv[]) {
1028 for (int i = 1; i < argc; i++) {
1029 if ((strcasecmp(argv[i], "-help") == 0) ||
1030 (strcasecmp(argv[i], "--help") == 0) ||
1031 (strcasecmp(argv[i], "/help") == 0) ||
1032 (strcasecmp(argv[i], "-h") == 0) ||
1033 (strcasecmp(argv[i], "/h") == 0) ||
george82e6883de2005-02-08 14:42:12 +00001034 (strcasecmp(argv[i], "/?") == 0) ||
1035 (strcasecmp(argv[i], "-?") == 0)) {
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001036 print_usage = true;
1037 return true;
1038 }
1039
1040 if ((strcasecmp(argv[i], "-speed") == 0) ||
1041 (strcasecmp(argv[i], "/speed") == 0) && (i < argc-1)) {
1042 playbackSpeed = atof(argv[++i]);
1043 if (playbackSpeed <= 0) {
1044 return false;
1045 }
1046 continue;
1047 }
1048
1049 if ((strcasecmp(argv[i], "-pos") == 0) ||
1050 (strcasecmp(argv[i], "/pos") == 0) && (i < argc-1)) {
1051 initTime = atol(argv[++i]);
1052 if (initTime <= 0)
1053 return false;
1054 continue;
1055 }
1056
1057 if ((strcasecmp(argv[i], "-autoplay") == 0) ||
1058 (strcasecmp(argv[i], "/autoplay") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001059 autoplay = true;
1060 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001061 }
1062
1063 if ((strcasecmp(argv[i], "-bell") == 0) ||
1064 (strcasecmp(argv[i], "/bell") == 0) && (i < argc-1)) {
george82e6883de2005-02-08 14:42:12 +00001065 acceptBell = true;
1066 continue;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001067 }
1068
1069 if (i != argc - 1)
1070 return false;
1071 }
1072
1073 fileName = strDup(argv[argc-1]);
1074 return true;
1075}
1076
1077//
1078// -=- WinMain
1079//
1080
1081int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
1082
1083 // - Process the command-line
1084
1085 int argc = __argc;
1086 char** argv = __argv;
george82e6883de2005-02-08 14:42:12 +00001087 if ((argc > 1) && (!processParams(argc, argv))) {
1088 MessageBox(0, wrong_cmd_msg, "RfbPlayer", MB_OK | MB_ICONWARNING);
1089 return 0;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001090 }
george82e6883de2005-02-08 14:42:12 +00001091
1092 if (print_usage) {
1093 programUsage();
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001094 return 0;
george8267cbcd02005-01-16 15:39:56 +00001095 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001096
george82e6883de2005-02-08 14:42:12 +00001097 // Create the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001098 RfbPlayer *player = NULL;
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001099 try {
1100 player = new RfbPlayer(fileName, initTime, playbackSpeed, autoplay,
george82e6883de2005-02-08 14:42:12 +00001101 acceptBell);
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001102 } catch (rdr::Exception e) {
1103 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1104 delete player;
1105 return 0;
1106 }
1107
1108 // Run the player
george825bbd61b2004-12-09 17:47:37 +00001109 HACCEL hAccel = LoadAccelerators(inst, MAKEINTRESOURCE(IDR_ACCELERATOR));
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001110 MSG msg;
1111 while (GetMessage(&msg, NULL, 0, 0) > 0) {
george825bbd61b2004-12-09 17:47:37 +00001112 if(!TranslateAccelerator(player->getMainHandle(), hAccel, &msg)) {
1113 TranslateMessage(&msg);
1114 DispatchMessage(&msg);
1115 }
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001116 }
1117
george82e6883de2005-02-08 14:42:12 +00001118 // Destroy the player
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001119 try{
Constantin Kaplinskyfbfbb922004-11-14 18:28:51 +00001120 if (player) delete player;
1121 } catch (rdr::Exception e) {
1122 MessageBox(NULL, e.str(), e.type(), MB_OK | MB_ICONERROR);
1123 }
1124
1125 return 0;
1126};